PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod_form.php

https://github.com/NigelCunningham/moodle-mod_forumng
PHP | 479 lines | 360 code | 59 blank | 60 comment | 72 complexity | a146bcbe66c7fdfd6db6d94ec640bc78 MD5 | raw file
  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. require_once ($CFG->dirroot . '/course/moodleform_mod.php');
  17. require_once ($CFG->dirroot . '/mod/forumng/mod_forumng.php');
  18. /**
  19. * Form for editing module settings.
  20. * @package mod
  21. * @subpackage forumng
  22. * @copyright 2011 The Open University
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class mod_forumng_mod_form extends moodleform_mod {
  26. private $clone;
  27. function definition() {
  28. global $CFG, $COURSE, $DB;
  29. $mform =& $this->_form;
  30. $coursecontext = context_course::instance($COURSE->id);
  31. $forumng = $this->_instance
  32. ? $DB->get_record('forumng', array('id' => $this->_instance)) : null;
  33. $this->clone = $forumng ? $forumng->originalcmid : 0;
  34. // If this is a clone, don't show the normal form
  35. if ($this->clone) {
  36. $mform->addElement('hidden', 'name', $forumng->name);
  37. $mform->addElement('static', 'sharedthing', '', get_string(
  38. 'sharedinfo', 'forumng',
  39. $CFG->wwwroot . '/course/modedit.php?update=' .
  40. $this->clone . '&amp;return=1'));
  41. $this->shared_definition_part($coursecontext);
  42. return;
  43. }
  44. $mform->addElement('header', 'general', get_string('general', 'form'));
  45. // Forum name
  46. $mform->addElement('text', 'name', get_string('forumname', 'forumng'),
  47. array('size'=>'64'));
  48. if (!empty($CFG->formatstringstriptags)) {
  49. $mform->setType('name', PARAM_TEXT);
  50. } else {
  51. $mform->setType('name', PARAM_CLEAN);
  52. }
  53. $mform->addRule('name', null, 'required', null, 'client');
  54. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  55. // Forum types
  56. $types = forumngtype::get_all();
  57. $options = array();
  58. foreach ($types as $type) {
  59. if ($type->is_user_selectable()) {
  60. $options[$type->get_id()] = $type->get_name();
  61. }
  62. }
  63. $mform->addElement('select', 'type', get_string('forumtype', 'forumng'), $options);
  64. $mform->addHelpButton('type', 'forumtype', 'forumng');
  65. $mform->setDefault('type', 'general');
  66. $this->add_intro_editor(false, get_string('forumintro', 'forumng'));
  67. // Subscription option displays only if enabled at site level
  68. if ($CFG->forumng_subscription == -1) {
  69. $options = mod_forumng::get_subscription_options();
  70. $mform->addElement('select', 'subscription',
  71. get_string('subscription', 'forumng'), $options);
  72. $mform->setDefault('subscription', mod_forumng::SUBSCRIPTION_PERMITTED);
  73. $mform->addHelpButton('subscription', 'subscription', 'forumng');
  74. } else {
  75. // Hidden element contains default value (not used anyhow)
  76. $mform->addElement('hidden', 'subscription',
  77. mod_forumng::SUBSCRIPTION_PERMITTED);
  78. }
  79. // Max size of attachments
  80. $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes);
  81. $choices[-1] = get_string('uploadnotallowed');
  82. $choices[0] = get_string('courseuploadlimit') . ' (' .
  83. display_size($COURSE->maxbytes) . ')';
  84. $mform->addElement('select', 'attachmentmaxbytes',
  85. get_string('attachmentmaxbytes', 'forumng'), $choices);
  86. $mform->addHelpButton('attachmentmaxbytes', 'attachmentmaxbytes', 'forumng');
  87. $mform->setDefault('attachmentmaxbytes', $CFG->forumng_attachmentmaxbytes);
  88. //Email address for reporting unacceptable post for this forum, default is blank
  89. $mform->addElement('text', 'reportingemail', get_string('reportingemail', 'forumng'),
  90. array('size'=>48));
  91. $mform->setType('reportingemail', PARAM_NOTAGS);
  92. $mform->addHelpButton('reportingemail', 'reportingemail', 'forumng');
  93. // Atom/RSS feed on/off/discussions-only
  94. if ($CFG->enablerssfeeds && !empty($CFG->forumng_enablerssfeeds)) {
  95. if ($CFG->forumng_feedtype == -1 || $CFG->forumng_feeditems == -1) {
  96. $mform->addElement('header', '', get_string('feeds', 'forumng'));
  97. }
  98. if ($CFG->forumng_feedtype == -1) {
  99. $mform->addElement('select', 'feedtype',
  100. get_string('feedtype', 'forumng'), mod_forumng::get_feedtype_options());
  101. $mform->setDefault('feedtype', mod_forumng::FEEDTYPE_ALL_POSTS);
  102. $mform->addHelpButton('feedtype', 'feedtype', 'forumng');
  103. }
  104. // Atom/RSS feed item count
  105. if ($CFG->forumng_feeditems == -1) {
  106. $mform->addElement('select', 'feeditems',
  107. get_string('feeditems', 'forumng'), mod_forumng::get_feeditems_options());
  108. $mform->setDefault('feeditems', 20);
  109. $mform->addHelpButton('feeditems', 'feeditems', 'forumng');
  110. }
  111. }
  112. // Ratings header
  113. /////////////////
  114. $mform->addElement('header', '', get_string('ratings', 'forumng'));
  115. $mform->addElement('checkbox', 'enableratings', get_string('enableratings', 'forumng'));
  116. $mform->addHelpButton('enableratings', 'enableratings', 'forumng');
  117. // Scale
  118. $mform->addElement('modgrade', 'ratingscale', get_string('scale'), null, true);
  119. $mform->disabledIf('ratingscale', 'enableratings', 'notchecked');
  120. $mform->setDefault('ratingscale', 5);
  121. // From/until times
  122. $mform->addElement('date_time_selector', 'ratingfrom',
  123. get_string('ratingfrom', 'forumng'), array('optional'=>true));
  124. $mform->disabledIf('ratingfrom', 'enableratings', 'notchecked');
  125. $mform->addElement('date_time_selector', 'ratinguntil',
  126. get_string('ratinguntil', 'forumng'), array('optional'=>true));
  127. $mform->disabledIf('ratinguntil', 'enableratings', 'notchecked');
  128. $mform->addElement('text', 'ratingthreshold',
  129. get_string('ratingthreshold', 'forumng'));
  130. $mform->setType('ratingthreshold', PARAM_INT);
  131. $mform->setDefault('ratingthreshold', 1);
  132. $mform->addRule('ratingthreshold',
  133. get_string('error_ratingthreshold', 'forumng'),
  134. 'regex', '/[1-9][0-9]*/', 'client');
  135. $mform->addHelpButton('ratingthreshold', 'ratingthreshold', 'forumng');
  136. $mform->disabledIf('ratingthreshold', 'enableratings', 'notchecked');
  137. // Grading
  138. $mform->addElement('select', 'grading', get_string('grade'),
  139. mod_forumng::get_grading_options());
  140. $mform->setDefault('grading', mod_forumng::GRADING_NONE);
  141. $mform->addHelpButton('grading', 'grading', 'forumng');
  142. $mform->disabledIf('grading', 'enableratings', 'notchecked');
  143. // Blocking header
  144. //////////////////
  145. $mform->addElement('header', '', get_string('limitposts', 'forumng'));
  146. // Post dates
  147. $mform->addElement('date_time_selector', 'postingfrom',
  148. get_string('postingfrom', 'forumng'), array('optional'=>true));
  149. $mform->addElement('date_time_selector', 'postinguntil',
  150. get_string('postinguntil', 'forumng'), array('optional'=>true));
  151. // User limits
  152. $limitgroup = array();
  153. $limitgroup[] = $mform->createElement(
  154. 'checkbox', 'enablelimit', '');
  155. $options = mod_forumng::get_max_posts_period_options();
  156. $limitgroup[] = $mform->createElement('text', 'maxpostsblock',
  157. '', array('size'=>3));
  158. $limitgroup[] = $mform->createElement('static', 'staticthing', '',
  159. ' ' . get_string('postsper', 'forumng') . ' ');
  160. $limitgroup[] = $mform->createElement('select', 'maxpostsperiod',
  161. '', $options);
  162. $mform->addGroup($limitgroup, 'limitgroup',
  163. get_string('enablelimit', 'forumng'));
  164. $mform->disabledIf('limitgroup[maxpostsblock]', 'limitgroup[enablelimit]');
  165. $mform->disabledIf('limitgroup[maxpostsperiod]', 'limitgroup[enablelimit]');
  166. $mform->addHelpButton('limitgroup', 'enablelimit', 'forumng');
  167. $mform->setType('limitgroup[maxpostsblock]', PARAM_INT);
  168. $mform->setDefault('limitgroup[maxpostsblock]', '10');
  169. // Remove old discussion
  170. $options = array();
  171. $options[0] = get_string('removeolddiscussionsdefault', 'forumng');
  172. for ($i = 1; $i <= 36; $i++) {
  173. $options[$i*2592000] = $i > 1 ? get_string('nummonths', 'moodle', $i) :
  174. get_string('onemonth', 'forumng');
  175. }
  176. $mform->addElement('header', '', get_string('removeolddiscussions', 'forumng'));
  177. $mform->addElement('select', 'removeafter',
  178. get_string('removeolddiscussionsafter', 'forumng'), $options);
  179. $mform->addHelpButton('removeafter', 'removeolddiscussions', 'forumng');
  180. $options = array();
  181. $options[0] = get_string('deletepermanently', 'forumng');
  182. $modinfo = get_fast_modinfo($COURSE);
  183. $targetforumngid = $this->_instance ? $this->_instance : 0;
  184. // Add all instances to drop down if the user can access them and
  185. // it's not the same as the current forum
  186. if (array_key_exists('forumng', $modinfo->instances)) {
  187. foreach ($modinfo->instances['forumng'] as $info) {
  188. if ($info->uservisible && $targetforumngid != $info->instance) {
  189. $options[$info->instance] = $info->name;
  190. }
  191. }
  192. }
  193. $mform->addElement('select', 'removeto',
  194. get_string('withremoveddiscussions', 'forumng'), $options);
  195. $mform->disabledIf('removeto', 'removeafter', 'eq', 0);
  196. $mform->addHelpButton('removeto', 'withremoveddiscussions', 'forumng');
  197. // Sharing options are advanced and for administrators only
  198. if ($CFG->forumng_enableadvanced && has_capability('moodle/site:config', $coursecontext)) {
  199. $mform->addElement('header', '', get_string('sharing', 'forumng'));
  200. $mform->addElement('advcheckbox', 'shared', get_string('shared', 'forumng'));
  201. $mform->addHelpButton('shared', 'shared', 'forumng');
  202. // Only when creating a forum, you can choose to make it a clone
  203. if (!$this->_instance) {
  204. $sharegroup = array();
  205. $sharegroup[] = $mform->createElement('checkbox', 'useshared', '');
  206. $sharegroup[] = $mform->createElement('text', 'originalcmidnumber', '');
  207. $mform->addGroup($sharegroup, 'usesharedgroup',
  208. get_string('useshared', 'forumng'));
  209. $mform->disabledIf('usesharedgroup[originalcmidnumber]',
  210. 'usesharedgroup[useshared]', 'notchecked');
  211. $mform->addHelpButton('usesharedgroup', 'useshared', 'forumng');
  212. }
  213. }
  214. // Do definition that is shared with clone version of form
  215. $this->shared_definition_part($coursecontext);
  216. if (count(mod_forumng_utils::get_convertible_forums($COURSE)) > 0 && !$this->_instance) {
  217. $mform->addElement('static', '', '', '<div class="forumng-convertoffer">' .
  218. get_string('offerconvert', 'forumng', $CFG->wwwroot .
  219. '/mod/forumng/convert.php?course=' . $COURSE->id) . '</div>');
  220. }
  221. }
  222. private function shared_definition_part($coursecontext) {
  223. $mform = $this->_form;
  224. // Standard behaviour
  225. $this->standard_coursemodule_elements();
  226. $this->add_action_buttons();
  227. }
  228. function validation($data, $files) {
  229. global $COURSE, $CFG, $DB;
  230. $errors = parent::validation($data, $files);
  231. if (isset($data['limitgroup']['maxpostsblock']) &&
  232. !preg_match('/^[0-9]{1,9}$/', $data['limitgroup']['maxpostsblock'])) {
  233. $errors['limitgroup'] = get_string('err_numeric', 'form');
  234. }
  235. if (!empty($data['reportingemail']) && !validate_email($data['reportingemail'])) {
  236. $errors['reportingemail'] = get_string('invalidemail', 'forumng');
  237. }
  238. // If old discussions are set to be moved to another forum...
  239. $targetforumngid = isset($data['removeto'])? $data['removeto'] : 0;
  240. $removeafter = isset($data['removeafter']) ? $data['removeafter'] : 0;
  241. if ($removeafter && $targetforumngid) {
  242. $modinfo = get_fast_modinfo($COURSE);
  243. // Look for target forum
  244. if (!array_key_exists($targetforumngid, $modinfo->instances['forumng'])) {
  245. $errors['removeto'] = get_string('errorinvalidforum', 'forumng');
  246. }
  247. }
  248. // If sharing is turned on, check requirements
  249. if (!empty($data['shared'])) {
  250. if (!empty($data['groupmode'])) {
  251. $errors['groupmode'] = get_string('error_notwhensharing', 'forumng');
  252. }
  253. if (!empty($data['grading'])) {
  254. $errors['grading'] = get_string('error_notwhensharing', 'forumng');
  255. }
  256. if (empty($data['cmidnumber'])) {
  257. $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
  258. } else {
  259. // Check it's unique
  260. $cmid = isset($data['coursemodule']) ? (int)$data['coursemodule'] : 0;
  261. if ($DB->count_records_select('course_modules', "idnumber = ? AND id <> ?",
  262. array($data['cmidnumber'], $cmid))) {
  263. $errors['cmidnumber'] = get_string('error_sharingrequiresidnumber', 'forumng');
  264. }
  265. }
  266. } else if (isset($data['shared'])) {
  267. // They are trying to turn sharing off. You aren't allowed to do
  268. // this if there are existing references.
  269. $cmid = isset($data['coursemodule']) ? (int)$data['coursemodule'] : -1;
  270. if ($DB->count_records('forumng', array('originalcmid' => $cmid))) {
  271. $errors['shared'] = get_string('error_sharinginuse', 'forumng');
  272. }
  273. }
  274. if (!empty($data['usesharedgroup']['useshared'])) {
  275. if (empty($data['usesharedgroup']['originalcmidnumber'])) {
  276. $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound', 'forumng');
  277. } else {
  278. // Check we can find it
  279. if (!mod_forumng::get_shared_cm_from_idnumber(
  280. $data['usesharedgroup']['originalcmidnumber'])) {
  281. $errors['usesharedgroup'] = get_string('error_sharingidnumbernotfound',
  282. 'forumng');
  283. }
  284. }
  285. }
  286. return $errors;
  287. }
  288. function data_preprocessing(&$data) {
  289. if (!empty($data['ratingscale'])) {
  290. $data['enableratings'] = 1;
  291. } else {
  292. $data['enableratings'] = 0;
  293. $data['ratingscale'] = 5;
  294. }
  295. if (!empty($data['maxpostsperiod']) && !empty($data['maxpostsblock'])) {
  296. $data['limitgroup[enablelimit]'] = 1;
  297. $data['limitgroup[maxpostsperiod]'] = $data['maxpostsperiod'];
  298. $data['limitgroup[maxpostsblock]'] = $data['maxpostsblock'];
  299. } else {
  300. $data['limitgroup[enablelimit]'] = 0;
  301. $data['limitgroup[maxpostsperiod]'] = 60*60*24;
  302. $data['limitgroup[maxpostsblock]'] = 10;
  303. }
  304. // Set up the completion checkboxes which aren't part of standard data.
  305. // We also make the default value (if you turn on the checkbox) for those
  306. // numbers to be 1, this will not apply unless checkbox is ticked.
  307. $data['completiondiscussionsenabled']=
  308. !empty($data['completiondiscussions']) ? 1 : 0;
  309. if (empty($data['completiondiscussions'])) {
  310. $data['completiondiscussions']=1;
  311. }
  312. $data['completionrepliesenabled']=
  313. !empty($data['completionreplies']) ? 1 : 0;
  314. if (empty($data['completionreplies'])) {
  315. $data['completionreplies']=1;
  316. }
  317. $data['completionpostsenabled']=
  318. !empty($data['completionposts']) ? 1 : 0;
  319. if (empty($data['completionposts'])) {
  320. $data['completionposts']=1;
  321. }
  322. }
  323. function add_completion_rules() {
  324. $mform =& $this->_form;
  325. $group = array();
  326. $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', '',
  327. get_string('completionposts', 'forumng'));
  328. $group[] =& $mform->createElement('text', 'completionposts', '', array('size'=>3));
  329. $mform->setType('completionposts', PARAM_INT);
  330. $mform->addGroup($group, 'completionpostsgroup',
  331. get_string('completionpostsgroup', 'forumng'), array(' '), false);
  332. $mform->addHelpButton('completionpostsgroup', 'completionpostsgroup', 'forumng');
  333. $mform->disabledIf('completionposts', 'completionpostsenabled', 'notchecked');
  334. $group = array();
  335. $group[] =& $mform->createElement('checkbox', 'completiondiscussionsenabled', '',
  336. get_string('completiondiscussions', 'forumng'));
  337. $group[] =& $mform->createElement('text', 'completiondiscussions', '', array('size'=>3));
  338. $mform->setType('completiondiscussions', PARAM_INT);
  339. $mform->addGroup($group, 'completiondiscussionsgroup',
  340. get_string('completiondiscussionsgroup', 'forumng'), array(' '), false);
  341. $mform->addHelpButton('completiondiscussionsgroup',
  342. 'completiondiscussionsgroup', 'forumng');
  343. $mform->disabledIf('completiondiscussions', 'completiondiscussionsenabled', 'notchecked');
  344. $group = array();
  345. $group[] =& $mform->createElement('checkbox', 'completionrepliesenabled', '',
  346. get_string('completionreplies', 'forumng'));
  347. $group[] =& $mform->createElement('text', 'completionreplies', '', array('size'=>3));
  348. $mform->setType('completionreplies', PARAM_INT);
  349. $mform->addGroup($group, 'completionrepliesgroup',
  350. get_string('completionrepliesgroup', 'forumng'), array(' '), false);
  351. $mform->addHelpButton('completionrepliesgroup', 'completionrepliesgroup', 'forumng');
  352. $mform->disabledIf('completionreplies', 'completionrepliesenabled', 'notchecked');
  353. return array('completiondiscussionsgroup',
  354. 'completionrepliesgroup', 'completionpostsgroup');
  355. }
  356. function completion_rule_enabled($data) {
  357. return (!empty($data['completiondiscussionsenabled']) &&
  358. $data['completiondiscussions']!=0) || (!empty($data['completionrepliesenabled']) &&
  359. $data['completionreplies']!=0) || (!empty($data['completionpostsenabled']) &&
  360. $data['completionposts']!=0);
  361. }
  362. function get_data() {
  363. $data = parent::get_data();
  364. if (!$data) {
  365. return false;
  366. }
  367. // Set the reportingemail to null if empty so that they are consistency
  368. if (empty($data->reportingemail)) {
  369. $data->reportingemail = null;
  370. }
  371. // Set the removeto to null if the default option 'Delete permanently' was select
  372. if (empty($data->removeto)) {
  373. $data->removeto = null;
  374. }
  375. // Turn off ratings/limit if required
  376. if (empty($data->enableratings)) {
  377. $data->ratingscale = 0;
  378. }
  379. if (empty($data->limitgroup['enablelimit'])) {
  380. $data->maxpostsperiod = 0;
  381. $data->maxpostsblock = 0;
  382. } else {
  383. $data->maxpostsperiod = $data->limitgroup['maxpostsperiod'];
  384. $data->maxpostsblock = $data->limitgroup['maxpostsblock'];
  385. }
  386. // Turn off completion settings if the checkboxes aren't ticked
  387. $autocompletion = !empty($data->completion) &&
  388. $data->completion==COMPLETION_TRACKING_AUTOMATIC;
  389. if (empty($data->completiondiscussionsenabled) || !$autocompletion) {
  390. $data->completiondiscussions = 0;
  391. }
  392. if (empty($data->completionrepliesenabled) || !$autocompletion) {
  393. $data->completionreplies = 0;
  394. }
  395. if (empty($data->completionpostsenabled) || !$autocompletion) {
  396. $data->completionposts = 0;
  397. }
  398. return $data;
  399. }
  400. function definition_after_data() {
  401. parent::definition_after_data();
  402. global $COURSE;
  403. $mform =& $this->_form;
  404. if ($this->clone) {
  405. $mform->removeElement('groupmode');
  406. return;
  407. }
  408. $targetforumngid = $mform->getElementValue('removeto');
  409. $targetforumngid = $targetforumngid[0];
  410. $removeafter = $mform->getElementValue('removeafter');
  411. $removeafter = $removeafter[0];
  412. if ($removeafter && $targetforumngid) {
  413. $modinfo = get_fast_modinfo($COURSE);
  414. if (!array_key_exists($targetforumngid, $modinfo->instances['forumng'])) {
  415. $mform->getElement('removeto')->addOption(
  416. get_string('invalidforum', 'forumng'), $targetforumngid);
  417. }
  418. }
  419. }
  420. }