PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/mod_form.php

https://github.com/nicolasconnault/moodle2.0
PHP | 255 lines | 202 code | 40 blank | 13 comment | 25 complexity | 0ef7d33b8eb5817d1e5c329e66cefc7f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  3. class mod_forum_mod_form extends moodleform_mod {
  4. function definition() {
  5. global $CFG, $COURSE, $DB;
  6. $mform =& $this->_form;
  7. //-------------------------------------------------------------------------------
  8. $mform->addElement('header', 'general', get_string('general', 'form'));
  9. $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
  10. if (!empty($CFG->formatstringstriptags)) {
  11. $mform->setType('name', PARAM_TEXT);
  12. } else {
  13. $mform->setType('name', PARAM_CLEAN);
  14. }
  15. $mform->addRule('name', null, 'required', null, 'client');
  16. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  17. $forum_types = forum_get_forum_types();
  18. asort($forum_types);
  19. $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forum_types);
  20. $mform->setHelpButton('type', array('forumtype', get_string('forumtype', 'forum'), 'forum'));
  21. $mform->setDefault('type', 'general');
  22. $this->add_intro_editor(true, get_string('forumintro', 'forum'));
  23. $options = array();
  24. $options[0] = get_string('no');
  25. $options[1] = get_string('yesforever', 'forum');
  26. $options[FORUM_INITIALSUBSCRIBE] = get_string('yesinitially', 'forum');
  27. $options[FORUM_DISALLOWSUBSCRIBE] = get_string('disallowsubscribe','forum');
  28. $mform->addElement('select', 'forcesubscribe', get_string('forcesubscribeq', 'forum'), $options);
  29. $mform->setHelpButton('forcesubscribe', array('subscription2', get_string('forcesubscribeq', 'forum'), 'forum'));
  30. $options = array();
  31. $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
  32. $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
  33. $options[FORUM_TRACKING_ON] = get_string('trackingon', 'forum');
  34. $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
  35. $mform->setHelpButton('trackingtype', array('trackingtype', get_string('trackingtype', 'forum'), 'forum'));
  36. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
  37. $choices[1] = get_string('uploadnotallowed');
  38. $choices[0] = get_string('courseuploadlimit') . ' ('.display_size($COURSE->maxbytes).')';
  39. $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
  40. $mform->setHelpButton('maxbytes', array('maxattachmentsize', get_string('maxattachmentsize', 'forum'), 'forum'));
  41. $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
  42. $choices = array(0,1,2,3,4,5,6,7,8,9);
  43. $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
  44. $mform->setHelpButton('maxattachments', array('maxattachments', get_string('maxattachments', 'forum'), 'forum'));
  45. $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
  46. if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
  47. //-------------------------------------------------------------------------------
  48. $mform->addElement('header', '', get_string('rss'));
  49. $choices = array();
  50. $choices[0] = get_string('none');
  51. $choices[1] = get_string('discussions', 'forum');
  52. $choices[2] = get_string('posts', 'forum');
  53. $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
  54. $mform->setHelpButton('rsstype', array('rsstype', get_string('rsstype'), 'forum'));
  55. $choices = array();
  56. $choices[0] = '0';
  57. $choices[1] = '1';
  58. $choices[2] = '2';
  59. $choices[3] = '3';
  60. $choices[4] = '4';
  61. $choices[5] = '5';
  62. $choices[10] = '10';
  63. $choices[15] = '15';
  64. $choices[20] = '20';
  65. $choices[25] = '25';
  66. $choices[30] = '30';
  67. $choices[40] = '40';
  68. $choices[50] = '50';
  69. $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
  70. $mform->setHelpButton('rssarticles', array('rssarticles', get_string('rssarticles'), 'forum'));
  71. }
  72. //-------------------------------------------------------------------------------
  73. $mform->addElement('header', '', get_string('grade'));
  74. $mform->addElement('select', 'assessed', get_string('aggregatetype', 'forum') , forum_get_aggregate_types());
  75. $mform->setDefault('assessed', 0);
  76. $mform->setHelpButton('assessed', array('assessaggregate', get_string('aggregatetype', 'forum'), 'forum'));
  77. $mform->addElement('modgrade', 'scale', get_string('grade'), false);
  78. $mform->disabledIf('scale', 'assessed', 'eq', 0);
  79. $mform->addElement('checkbox', 'ratingtime', get_string('ratingtime', 'forum'));
  80. $mform->disabledIf('ratingtime', 'assessed', 'eq', 0);
  81. $mform->addElement('date_time_selector', 'assesstimestart', get_string('from'));
  82. $mform->disabledIf('assesstimestart', 'assessed', 'eq', 0);
  83. $mform->disabledIf('assesstimestart', 'ratingtime');
  84. $mform->addElement('date_time_selector', 'assesstimefinish', get_string('to'));
  85. $mform->disabledIf('assesstimefinish', 'assessed', 'eq', 0);
  86. $mform->disabledIf('assesstimefinish', 'ratingtime');
  87. //-------------------------------------------------------------------------------
  88. $mform->addElement('header', '', get_string('blockafter', 'forum'));
  89. $options = array();
  90. $options[0] = get_string('blockperioddisabled','forum');
  91. $options[60*60*24] = '1 '.get_string('day');
  92. $options[60*60*24*2] = '2 '.get_string('days');
  93. $options[60*60*24*3] = '3 '.get_string('days');
  94. $options[60*60*24*4] = '4 '.get_string('days');
  95. $options[60*60*24*5] = '5 '.get_string('days');
  96. $options[60*60*24*6] = '6 '.get_string('days');
  97. $options[60*60*24*7] = '1 '.get_string('week');
  98. $mform->addElement('select', 'blockperiod', get_string("blockperiod", "forum") , $options);
  99. $mform->setHelpButton('blockperiod', array('manageposts', get_string('blockperiod', 'forum'),'forum'));
  100. $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
  101. $mform->setType('blockafter', PARAM_INT);
  102. $mform->setDefault('blockafter', '0');
  103. $mform->addRule('blockafter', null, 'numeric', null, 'client');
  104. $mform->setHelpButton('blockafter', array('manageposts', get_string('blockafter', 'forum'),'forum'));
  105. $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
  106. $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
  107. $mform->setType('warnafter', PARAM_INT);
  108. $mform->setDefault('warnafter', '0');
  109. $mform->addRule('warnafter', null, 'numeric', null, 'client');
  110. $mform->setHelpButton('warnafter', array('manageposts', get_string('warnafter', 'forum'),'forum'));
  111. $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
  112. //-------------------------------------------------------------------------------
  113. $this->standard_coursemodule_elements();
  114. //-------------------------------------------------------------------------------
  115. // buttons
  116. $this->add_action_buttons();
  117. }
  118. function definition_after_data() {
  119. parent::definition_after_data();
  120. $mform =& $this->_form;
  121. $type =& $mform->getElement('type');
  122. $typevalue = $mform->getElementValue('type');
  123. //we don't want to have these appear as possible selections in the form but
  124. //we want the form to display them if they are set.
  125. if ($typevalue[0]=='news') {
  126. $type->addOption(get_string('namenews', 'forum'), 'news');
  127. $type->setHelpButton(array('forumtypenews', get_string('forumtype', 'forum'), 'forum'));
  128. $type->freeze();
  129. $type->setPersistantFreeze(true);
  130. }
  131. if ($typevalue[0]=='social') {
  132. $type->addOption(get_string('namesocial', 'forum'), 'social');
  133. $type->freeze();
  134. $type->setPersistantFreeze(true);
  135. }
  136. }
  137. function data_preprocessing(&$default_values) {
  138. if (empty($default_values['scale'])) {
  139. $default_values['assessed'] = 0;
  140. }
  141. if (empty($default_values['assessed'])) {
  142. $default_values['ratingtime'] = 0;
  143. } else {
  144. $default_values['ratingtime']=
  145. ($default_values['assesstimestart'] && $default_values['assesstimefinish']) ? 1 : 0;
  146. }
  147. // Set up the completion checkboxes which aren't part of standard data.
  148. // We also make the default value (if you turn on the checkbox) for those
  149. // numbers to be 1, this will not apply unless checkbox is ticked.
  150. $default_values['completiondiscussionsenabled']=
  151. !empty($default_values['completiondiscussions']) ? 1 : 0;
  152. if (empty($default_values['completiondiscussions'])) {
  153. $default_values['completiondiscussions']=1;
  154. }
  155. $default_values['completionrepliesenabled']=
  156. !empty($default_values['completionreplies']) ? 1 : 0;
  157. if (empty($default_values['completionreplies'])) {
  158. $default_values['completionreplies']=1;
  159. }
  160. $default_values['completionpostsenabled']=
  161. !empty($default_values['completionposts']) ? 1 : 0;
  162. if (empty($default_values['completionposts'])) {
  163. $default_values['completionposts']=1;
  164. }
  165. }
  166. function add_completion_rules() {
  167. $mform =& $this->_form;
  168. $group=array();
  169. $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
  170. $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
  171. $mform->setType('completionposts',PARAM_INT);
  172. $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
  173. $mform->setHelpButton('completionpostsgroup', array('completion', get_string('completionpostshelp', 'forum'), 'forum'));
  174. $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
  175. $group=array();
  176. $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
  177. $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
  178. $mform->setType('completiondiscussions',PARAM_INT);
  179. $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
  180. $mform->setHelpButton('completiondiscussionsgroup', array('completion', get_string('completiondiscussionshelp', 'forum'), 'forum'));
  181. $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
  182. $group=array();
  183. $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
  184. $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
  185. $mform->setType('completionreplies',PARAM_INT);
  186. $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
  187. $mform->setHelpButton('completionrepliesgroup', array('completion', get_string('completionreplieshelp', 'forum'), 'forum'));
  188. $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
  189. return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
  190. }
  191. function completion_rule_enabled($data) {
  192. return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
  193. (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
  194. (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
  195. }
  196. function get_data() {
  197. $data = parent::get_data();
  198. if (!$data) {
  199. return false;
  200. }
  201. // Turn off completion settings if the checkboxes aren't ticked
  202. $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
  203. if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
  204. $data->completiondiscussions = 0;
  205. }
  206. if (empty($data->completionrepliesenabled) || !$autocompletion) {
  207. $data->completionreplies = 0;
  208. }
  209. if (empty($data->completionpostsenabled) || !$autocompletion) {
  210. $data->completionposts = 0;
  211. }
  212. return $data;
  213. }
  214. }
  215. ?>