PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 285 lines | 211 code | 41 blank | 33 comment | 27 complexity | d9d97eb8cb35606fb0d476c05b684824 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package mod-forum
  18. * @copyright Jamie Pratt <me@jamiep.org>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. if (!defined('MOODLE_INTERNAL')) {
  22. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  23. }
  24. require_once ($CFG->dirroot.'/course/moodleform_mod.php');
  25. class mod_forum_mod_form extends moodleform_mod {
  26. function definition() {
  27. global $CFG, $COURSE, $DB;
  28. $mform =& $this->_form;
  29. //-------------------------------------------------------------------------------
  30. $mform->addElement('header', 'general', get_string('general', 'form'));
  31. $mform->addElement('text', 'name', get_string('forumname', 'forum'), array('size'=>'64'));
  32. if (!empty($CFG->formatstringstriptags)) {
  33. $mform->setType('name', PARAM_TEXT);
  34. } else {
  35. $mform->setType('name', PARAM_CLEANHTML);
  36. }
  37. $mform->addRule('name', null, 'required', null, 'client');
  38. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  39. $this->add_intro_editor(true, get_string('forumintro', 'forum'));
  40. $forumtypes = forum_get_forum_types();
  41. core_collator::asort($forumtypes, core_collator::SORT_STRING);
  42. $mform->addElement('select', 'type', get_string('forumtype', 'forum'), $forumtypes);
  43. $mform->addHelpButton('type', 'forumtype', 'forum');
  44. $mform->setDefault('type', 'general');
  45. // Attachments and word count.
  46. $mform->addElement('header', 'attachmentswordcounthdr', get_string('attachmentswordcount', 'forum'));
  47. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes, 0, $CFG->forum_maxbytes);
  48. $choices[1] = get_string('uploadnotallowed');
  49. $mform->addElement('select', 'maxbytes', get_string('maxattachmentsize', 'forum'), $choices);
  50. $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'forum');
  51. $mform->setDefault('maxbytes', $CFG->forum_maxbytes);
  52. $choices = array(
  53. 0 => 0,
  54. 1 => 1,
  55. 2 => 2,
  56. 3 => 3,
  57. 4 => 4,
  58. 5 => 5,
  59. 6 => 6,
  60. 7 => 7,
  61. 8 => 8,
  62. 9 => 9,
  63. 10 => 10,
  64. 20 => 20,
  65. 50 => 50,
  66. 100 => 100
  67. );
  68. $mform->addElement('select', 'maxattachments', get_string('maxattachments', 'forum'), $choices);
  69. $mform->addHelpButton('maxattachments', 'maxattachments', 'forum');
  70. $mform->setDefault('maxattachments', $CFG->forum_maxattachments);
  71. $mform->addElement('selectyesno', 'displaywordcount', get_string('displaywordcount', 'forum'));
  72. $mform->addHelpButton('displaywordcount', 'displaywordcount', 'forum');
  73. $mform->setDefault('displaywordcount', 0);
  74. // Subscription and tracking.
  75. $mform->addElement('header', 'subscriptionandtrackinghdr', get_string('subscriptionandtracking', 'forum'));
  76. $options = array();
  77. $options[FORUM_CHOOSESUBSCRIBE] = get_string('subscriptionoptional', 'forum');
  78. $options[FORUM_FORCESUBSCRIBE] = get_string('subscriptionforced', 'forum');
  79. $options[FORUM_INITIALSUBSCRIBE] = get_string('subscriptionauto', 'forum');
  80. $options[FORUM_DISALLOWSUBSCRIBE] = get_string('subscriptiondisabled','forum');
  81. $mform->addElement('select', 'forcesubscribe', get_string('subscriptionmode', 'forum'), $options);
  82. $mform->addHelpButton('forcesubscribe', 'subscriptionmode', 'forum');
  83. $options = array();
  84. $options[FORUM_TRACKING_OPTIONAL] = get_string('trackingoptional', 'forum');
  85. $options[FORUM_TRACKING_OFF] = get_string('trackingoff', 'forum');
  86. if ($CFG->forum_allowforcedreadtracking) {
  87. $options[FORUM_TRACKING_FORCED] = get_string('trackingon', 'forum');
  88. }
  89. $mform->addElement('select', 'trackingtype', get_string('trackingtype', 'forum'), $options);
  90. $mform->addHelpButton('trackingtype', 'trackingtype', 'forum');
  91. $default = $CFG->forum_trackingtype;
  92. if ((!$CFG->forum_allowforcedreadtracking) && ($default == FORUM_TRACKING_FORCED)) {
  93. $default = FORUM_TRACKING_OPTIONAL;
  94. }
  95. $mform->setDefault('trackingtype', $default);
  96. if ($CFG->enablerssfeeds && isset($CFG->forum_enablerssfeeds) && $CFG->forum_enablerssfeeds) {
  97. //-------------------------------------------------------------------------------
  98. $mform->addElement('header', 'rssheader', get_string('rss'));
  99. $choices = array();
  100. $choices[0] = get_string('none');
  101. $choices[1] = get_string('discussions', 'forum');
  102. $choices[2] = get_string('posts', 'forum');
  103. $mform->addElement('select', 'rsstype', get_string('rsstype'), $choices);
  104. $mform->addHelpButton('rsstype', 'rsstype', 'forum');
  105. $choices = array();
  106. $choices[0] = '0';
  107. $choices[1] = '1';
  108. $choices[2] = '2';
  109. $choices[3] = '3';
  110. $choices[4] = '4';
  111. $choices[5] = '5';
  112. $choices[10] = '10';
  113. $choices[15] = '15';
  114. $choices[20] = '20';
  115. $choices[25] = '25';
  116. $choices[30] = '30';
  117. $choices[40] = '40';
  118. $choices[50] = '50';
  119. $mform->addElement('select', 'rssarticles', get_string('rssarticles'), $choices);
  120. $mform->addHelpButton('rssarticles', 'rssarticles', 'forum');
  121. $mform->disabledIf('rssarticles', 'rsstype', 'eq', '0');
  122. }
  123. //-------------------------------------------------------------------------------
  124. $mform->addElement('header', 'blockafterheader', get_string('blockafter', 'forum'));
  125. $options = array();
  126. $options[0] = get_string('blockperioddisabled','forum');
  127. $options[60*60*24] = '1 '.get_string('day');
  128. $options[60*60*24*2] = '2 '.get_string('days');
  129. $options[60*60*24*3] = '3 '.get_string('days');
  130. $options[60*60*24*4] = '4 '.get_string('days');
  131. $options[60*60*24*5] = '5 '.get_string('days');
  132. $options[60*60*24*6] = '6 '.get_string('days');
  133. $options[60*60*24*7] = '1 '.get_string('week');
  134. $mform->addElement('select', 'blockperiod', get_string('blockperiod', 'forum'), $options);
  135. $mform->addHelpButton('blockperiod', 'blockperiod', 'forum');
  136. $mform->addElement('text', 'blockafter', get_string('blockafter', 'forum'));
  137. $mform->setType('blockafter', PARAM_INT);
  138. $mform->setDefault('blockafter', '0');
  139. $mform->addRule('blockafter', null, 'numeric', null, 'client');
  140. $mform->addHelpButton('blockafter', 'blockafter', 'forum');
  141. $mform->disabledIf('blockafter', 'blockperiod', 'eq', 0);
  142. $mform->addElement('text', 'warnafter', get_string('warnafter', 'forum'));
  143. $mform->setType('warnafter', PARAM_INT);
  144. $mform->setDefault('warnafter', '0');
  145. $mform->addRule('warnafter', null, 'numeric', null, 'client');
  146. $mform->addHelpButton('warnafter', 'warnafter', 'forum');
  147. $mform->disabledIf('warnafter', 'blockperiod', 'eq', 0);
  148. $coursecontext = context_course::instance($COURSE->id);
  149. plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_forum');
  150. //-------------------------------------------------------------------------------
  151. $this->standard_grading_coursemodule_elements();
  152. $this->standard_coursemodule_elements();
  153. //-------------------------------------------------------------------------------
  154. // buttons
  155. $this->add_action_buttons();
  156. }
  157. function definition_after_data() {
  158. parent::definition_after_data();
  159. $mform =& $this->_form;
  160. $type =& $mform->getElement('type');
  161. $typevalue = $mform->getElementValue('type');
  162. //we don't want to have these appear as possible selections in the form but
  163. //we want the form to display them if they are set.
  164. if ($typevalue[0]=='news') {
  165. $type->addOption(get_string('namenews', 'forum'), 'news');
  166. $mform->addHelpButton('type', 'namenews', 'forum');
  167. $type->freeze();
  168. $type->setPersistantFreeze(true);
  169. }
  170. if ($typevalue[0]=='social') {
  171. $type->addOption(get_string('namesocial', 'forum'), 'social');
  172. $type->freeze();
  173. $type->setPersistantFreeze(true);
  174. }
  175. }
  176. function data_preprocessing(&$default_values) {
  177. parent::data_preprocessing($default_values);
  178. // Set up the completion checkboxes which aren't part of standard data.
  179. // We also make the default value (if you turn on the checkbox) for those
  180. // numbers to be 1, this will not apply unless checkbox is ticked.
  181. $default_values['completiondiscussionsenabled']=
  182. !empty($default_values['completiondiscussions']) ? 1 : 0;
  183. if (empty($default_values['completiondiscussions'])) {
  184. $default_values['completiondiscussions']=1;
  185. }
  186. $default_values['completionrepliesenabled']=
  187. !empty($default_values['completionreplies']) ? 1 : 0;
  188. if (empty($default_values['completionreplies'])) {
  189. $default_values['completionreplies']=1;
  190. }
  191. $default_values['completionpostsenabled']=
  192. !empty($default_values['completionposts']) ? 1 : 0;
  193. if (empty($default_values['completionposts'])) {
  194. $default_values['completionposts']=1;
  195. }
  196. }
  197. function add_completion_rules() {
  198. $mform =& $this->_form;
  199. $group=array();
  200. $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '', get_string('completionposts','forum'));
  201. $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
  202. $mform->setType('completionposts',PARAM_INT);
  203. $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup','forum'), array(' '), false);
  204. $mform->disabledIf('completionposts','completionpostsenabled','notchecked');
  205. $group=array();
  206. $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '', get_string('completiondiscussions','forum'));
  207. $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
  208. $mform->setType('completiondiscussions',PARAM_INT);
  209. $mform->addGroup($group, 'completiondiscussionsgroup', get_string('completiondiscussionsgroup','forum'), array(' '), false);
  210. $mform->disabledIf('completiondiscussions','completiondiscussionsenabled','notchecked');
  211. $group=array();
  212. $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '', get_string('completionreplies','forum'));
  213. $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
  214. $mform->setType('completionreplies',PARAM_INT);
  215. $mform->addGroup($group, 'completionrepliesgroup', get_string('completionrepliesgroup','forum'), array(' '), false);
  216. $mform->disabledIf('completionreplies','completionrepliesenabled','notchecked');
  217. return array('completiondiscussionsgroup','completionrepliesgroup','completionpostsgroup');
  218. }
  219. function completion_rule_enabled($data) {
  220. return (!empty($data['completiondiscussionsenabled']) && $data['completiondiscussions']!=0) ||
  221. (!empty($data['completionrepliesenabled']) && $data['completionreplies']!=0) ||
  222. (!empty($data['completionpostsenabled']) && $data['completionposts']!=0);
  223. }
  224. function get_data() {
  225. $data = parent::get_data();
  226. if (!$data) {
  227. return false;
  228. }
  229. // Turn off completion settings if the checkboxes aren't ticked
  230. if (!empty($data->completionunlocked)) {
  231. $autocompletion = !empty($data->completion) && $data->completion==COMPLETION_TRACKING_AUTOMATIC;
  232. if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
  233. $data->completiondiscussions = 0;
  234. }
  235. if (empty($data->completionrepliesenabled) || !$autocompletion) {
  236. $data->completionreplies = 0;
  237. }
  238. if (empty($data->completionpostsenabled) || !$autocompletion) {
  239. $data->completionposts = 0;
  240. }
  241. }
  242. return $data;
  243. }
  244. }