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

/mod/assign/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 314 lines | 207 code | 51 blank | 56 comment | 34 complexity | 616dab2c61c069be6f0fd5059ee9c60b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-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. * This file contains the forms to create and edit an instance of this module
  18. *
  19. * @package mod_assign
  20. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') || die('Direct access to this script is forbidden.');
  24. require_once($CFG->dirroot.'/course/moodleform_mod.php');
  25. require_once($CFG->dirroot . '/mod/assign/locallib.php');
  26. /**
  27. * Assignment settings form.
  28. *
  29. * @package mod_assign
  30. * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class mod_assign_mod_form extends moodleform_mod {
  34. /**
  35. * Called to define this moodle form
  36. *
  37. * @return void
  38. */
  39. public function definition() {
  40. global $CFG, $COURSE, $DB, $PAGE;
  41. $mform = $this->_form;
  42. $mform->addElement('header', 'general', get_string('general', 'form'));
  43. $mform->addElement('text', 'name', get_string('assignmentname', 'assign'), array('size'=>'64'));
  44. if (!empty($CFG->formatstringstriptags)) {
  45. $mform->setType('name', PARAM_TEXT);
  46. } else {
  47. $mform->setType('name', PARAM_CLEANHTML);
  48. }
  49. $mform->addRule('name', null, 'required', null, 'client');
  50. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  51. $this->standard_intro_elements(get_string('description', 'assign'));
  52. $mform->addElement('filemanager', 'introattachments',
  53. get_string('introattachments', 'assign'),
  54. null, array('subdirs' => 0, 'maxbytes' => $COURSE->maxbytes) );
  55. $mform->addHelpButton('introattachments', 'introattachments', 'assign');
  56. $ctx = null;
  57. if ($this->current && $this->current->coursemodule) {
  58. $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  59. $ctx = context_module::instance($cm->id);
  60. }
  61. $assignment = new assign($ctx, null, null);
  62. if ($this->current && $this->current->course) {
  63. if (!$ctx) {
  64. $ctx = context_course::instance($this->current->course);
  65. }
  66. $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  67. $assignment->set_course($course);
  68. }
  69. $config = get_config('assign');
  70. $mform->addElement('header', 'availability', get_string('availability', 'assign'));
  71. $mform->setExpanded('availability', true);
  72. $name = get_string('allowsubmissionsfromdate', 'assign');
  73. $options = array('optional'=>true);
  74. $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
  75. $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
  76. $name = get_string('duedate', 'assign');
  77. $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
  78. $mform->addHelpButton('duedate', 'duedate', 'assign');
  79. $name = get_string('cutoffdate', 'assign');
  80. $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
  81. $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
  82. $name = get_string('gradingduedate', 'assign');
  83. $mform->addElement('date_time_selector', 'gradingduedate', $name, array('optional' => true));
  84. $mform->addHelpButton('gradingduedate', 'gradingduedate', 'assign');
  85. $name = get_string('alwaysshowdescription', 'assign');
  86. $mform->addElement('checkbox', 'alwaysshowdescription', $name);
  87. $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
  88. $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
  89. $assignment->add_all_plugin_settings($mform);
  90. $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
  91. $name = get_string('submissiondrafts', 'assign');
  92. $mform->addElement('selectyesno', 'submissiondrafts', $name);
  93. $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
  94. $name = get_string('requiresubmissionstatement', 'assign');
  95. $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
  96. $mform->addHelpButton('requiresubmissionstatement',
  97. 'requiresubmissionstatement',
  98. 'assign');
  99. $mform->setType('requiresubmissionstatement', PARAM_BOOL);
  100. $options = array(
  101. ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
  102. ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
  103. ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
  104. );
  105. $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
  106. $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
  107. $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
  108. $options += array_combine(range(1, 30), range(1, 30));
  109. $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
  110. $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
  111. $mform->hideIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
  112. $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
  113. $name = get_string('teamsubmission', 'assign');
  114. $mform->addElement('selectyesno', 'teamsubmission', $name);
  115. $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
  116. if ($assignment->has_submissions_or_grades()) {
  117. $mform->freeze('teamsubmission');
  118. }
  119. $name = get_string('preventsubmissionnotingroup', 'assign');
  120. $mform->addElement('selectyesno', 'preventsubmissionnotingroup', $name);
  121. $mform->addHelpButton('preventsubmissionnotingroup',
  122. 'preventsubmissionnotingroup',
  123. 'assign');
  124. $mform->setType('preventsubmissionnotingroup', PARAM_BOOL);
  125. $mform->hideIf('preventsubmissionnotingroup', 'teamsubmission', 'eq', 0);
  126. $name = get_string('requireallteammemberssubmit', 'assign');
  127. $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
  128. $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
  129. $mform->hideIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
  130. $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
  131. $groupings = groups_get_all_groupings($assignment->get_course()->id);
  132. $options = array();
  133. $options[0] = get_string('none');
  134. foreach ($groupings as $grouping) {
  135. $options[$grouping->id] = $grouping->name;
  136. }
  137. $name = get_string('teamsubmissiongroupingid', 'assign');
  138. $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
  139. $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
  140. $mform->hideIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
  141. if ($assignment->has_submissions_or_grades()) {
  142. $mform->freeze('teamsubmissiongroupingid');
  143. }
  144. $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
  145. $name = get_string('sendnotifications', 'assign');
  146. $mform->addElement('selectyesno', 'sendnotifications', $name);
  147. $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
  148. $name = get_string('sendlatenotifications', 'assign');
  149. $mform->addElement('selectyesno', 'sendlatenotifications', $name);
  150. $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
  151. $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
  152. $name = get_string('sendstudentnotificationsdefault', 'assign');
  153. $mform->addElement('selectyesno', 'sendstudentnotifications', $name);
  154. $mform->addHelpButton('sendstudentnotifications', 'sendstudentnotificationsdefault', 'assign');
  155. // Plagiarism enabling form. To be removed (deprecated) with MDL-67526.
  156. if (!empty($CFG->enableplagiarism)) {
  157. require_once($CFG->libdir . '/plagiarismlib.php');
  158. plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
  159. }
  160. $this->standard_grading_coursemodule_elements();
  161. $name = get_string('blindmarking', 'assign');
  162. $mform->addElement('selectyesno', 'blindmarking', $name);
  163. $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
  164. if ($assignment->has_submissions_or_grades() ) {
  165. $mform->freeze('blindmarking');
  166. }
  167. $name = get_string('hidegrader', 'assign');
  168. $mform->addElement('selectyesno', 'hidegrader', $name);
  169. $mform->addHelpButton('hidegrader', 'hidegrader', 'assign');
  170. $name = get_string('markingworkflow', 'assign');
  171. $mform->addElement('selectyesno', 'markingworkflow', $name);
  172. $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
  173. $name = get_string('markingallocation', 'assign');
  174. $mform->addElement('selectyesno', 'markingallocation', $name);
  175. $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
  176. $mform->hideIf('markingallocation', 'markingworkflow', 'eq', 0);
  177. $this->standard_coursemodule_elements();
  178. $this->apply_admin_defaults();
  179. $this->add_action_buttons();
  180. }
  181. /**
  182. * Perform minimal validation on the settings form
  183. * @param array $data
  184. * @param array $files
  185. */
  186. public function validation($data, $files) {
  187. $errors = parent::validation($data, $files);
  188. if (!empty($data['allowsubmissionsfromdate']) && !empty($data['duedate'])) {
  189. if ($data['duedate'] < $data['allowsubmissionsfromdate']) {
  190. $errors['duedate'] = get_string('duedatevalidation', 'assign');
  191. }
  192. }
  193. if (!empty($data['cutoffdate']) && !empty($data['duedate'])) {
  194. if ($data['cutoffdate'] < $data['duedate'] ) {
  195. $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
  196. }
  197. }
  198. if (!empty($data['allowsubmissionsfromdate']) && !empty($data['cutoffdate'])) {
  199. if ($data['cutoffdate'] < $data['allowsubmissionsfromdate']) {
  200. $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
  201. }
  202. }
  203. if ($data['gradingduedate']) {
  204. if ($data['allowsubmissionsfromdate'] && $data['allowsubmissionsfromdate'] > $data['gradingduedate']) {
  205. $errors['gradingduedate'] = get_string('gradingduefromdatevalidation', 'assign');
  206. }
  207. if ($data['duedate'] && $data['duedate'] > $data['gradingduedate']) {
  208. $errors['gradingduedate'] = get_string('gradingdueduedatevalidation', 'assign');
  209. }
  210. }
  211. if ($data['blindmarking'] && $data['attemptreopenmethod'] == ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS) {
  212. $errors['attemptreopenmethod'] = get_string('reopenuntilpassincompatiblewithblindmarking', 'assign');
  213. }
  214. return $errors;
  215. }
  216. /**
  217. * Any data processing needed before the form is displayed
  218. * (needed to set up draft areas for editor and filemanager elements)
  219. * @param array $defaultvalues
  220. */
  221. public function data_preprocessing(&$defaultvalues) {
  222. global $DB;
  223. $ctx = null;
  224. if ($this->current && $this->current->coursemodule) {
  225. $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  226. $ctx = context_module::instance($cm->id);
  227. }
  228. $assignment = new assign($ctx, null, null);
  229. if ($this->current && $this->current->course) {
  230. if (!$ctx) {
  231. $ctx = context_course::instance($this->current->course);
  232. }
  233. $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  234. $assignment->set_course($course);
  235. }
  236. $draftitemid = file_get_submitted_draft_itemid('introattachments');
  237. file_prepare_draft_area($draftitemid, $ctx->id, 'mod_assign', ASSIGN_INTROATTACHMENT_FILEAREA,
  238. 0, array('subdirs' => 0));
  239. $defaultvalues['introattachments'] = $draftitemid;
  240. $assignment->plugin_data_preprocessing($defaultvalues);
  241. }
  242. /**
  243. * Add any custom completion rules to the form.
  244. *
  245. * @return array Contains the names of the added form elements
  246. */
  247. public function add_completion_rules() {
  248. $mform =& $this->_form;
  249. $mform->addElement('advcheckbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
  250. // Enable this completion rule by default.
  251. $mform->setDefault('completionsubmit', 1);
  252. return array('completionsubmit');
  253. }
  254. /**
  255. * Determines if completion is enabled for this module.
  256. *
  257. * @param array $data
  258. * @return bool
  259. */
  260. public function completion_rule_enabled($data) {
  261. return !empty($data['completionsubmit']);
  262. }
  263. }