PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assign/mod_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 290 lines | 186 code | 47 blank | 57 comment | 27 complexity | b84306b6ba3fdaca966aad43a8bfd7f9 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. * 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, $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->add_intro_editor(true, get_string('description', 'assign'));
  52. $ctx = null;
  53. if ($this->current && $this->current->coursemodule) {
  54. $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  55. $ctx = context_module::instance($cm->id);
  56. }
  57. $assignment = new assign($ctx, null, null);
  58. if ($this->current && $this->current->course) {
  59. if (!$ctx) {
  60. $ctx = context_course::instance($this->current->course);
  61. }
  62. $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  63. $assignment->set_course($course);
  64. }
  65. $config = get_config('assign');
  66. $mform->addElement('header', 'availability', get_string('availability', 'assign'));
  67. $mform->setExpanded('availability', true);
  68. $name = get_string('allowsubmissionsfromdate', 'assign');
  69. $options = array('optional'=>true);
  70. $mform->addElement('date_time_selector', 'allowsubmissionsfromdate', $name, $options);
  71. $mform->addHelpButton('allowsubmissionsfromdate', 'allowsubmissionsfromdate', 'assign');
  72. $name = get_string('duedate', 'assign');
  73. $mform->addElement('date_time_selector', 'duedate', $name, array('optional'=>true));
  74. $mform->addHelpButton('duedate', 'duedate', 'assign');
  75. $name = get_string('cutoffdate', 'assign');
  76. $mform->addElement('date_time_selector', 'cutoffdate', $name, array('optional'=>true));
  77. $mform->addHelpButton('cutoffdate', 'cutoffdate', 'assign');
  78. $name = get_string('alwaysshowdescription', 'assign');
  79. $mform->addElement('checkbox', 'alwaysshowdescription', $name);
  80. $mform->addHelpButton('alwaysshowdescription', 'alwaysshowdescription', 'assign');
  81. $mform->disabledIf('alwaysshowdescription', 'allowsubmissionsfromdate[enabled]', 'notchecked');
  82. $assignment->add_all_plugin_settings($mform);
  83. $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'assign'));
  84. $name = get_string('submissiondrafts', 'assign');
  85. $mform->addElement('selectyesno', 'submissiondrafts', $name);
  86. $mform->addHelpButton('submissiondrafts', 'submissiondrafts', 'assign');
  87. $name = get_string('requiresubmissionstatement', 'assign');
  88. $mform->addElement('selectyesno', 'requiresubmissionstatement', $name);
  89. $mform->addHelpButton('requiresubmissionstatement',
  90. 'requiresubmissionstatement',
  91. 'assign');
  92. $mform->setType('requiresubmissionstatement', PARAM_BOOL);
  93. $options = array(
  94. ASSIGN_ATTEMPT_REOPEN_METHOD_NONE => get_string('attemptreopenmethod_none', 'mod_assign'),
  95. ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL => get_string('attemptreopenmethod_manual', 'mod_assign'),
  96. ASSIGN_ATTEMPT_REOPEN_METHOD_UNTILPASS => get_string('attemptreopenmethod_untilpass', 'mod_assign')
  97. );
  98. $mform->addElement('select', 'attemptreopenmethod', get_string('attemptreopenmethod', 'mod_assign'), $options);
  99. $mform->addHelpButton('attemptreopenmethod', 'attemptreopenmethod', 'mod_assign');
  100. $options = array(ASSIGN_UNLIMITED_ATTEMPTS => get_string('unlimitedattempts', 'mod_assign'));
  101. $options += array_combine(range(1, 30), range(1, 30));
  102. $mform->addElement('select', 'maxattempts', get_string('maxattempts', 'mod_assign'), $options);
  103. $mform->addHelpButton('maxattempts', 'maxattempts', 'assign');
  104. $mform->disabledIf('maxattempts', 'attemptreopenmethod', 'eq', ASSIGN_ATTEMPT_REOPEN_METHOD_NONE);
  105. $mform->addElement('header', 'groupsubmissionsettings', get_string('groupsubmissionsettings', 'assign'));
  106. $name = get_string('teamsubmission', 'assign');
  107. $mform->addElement('selectyesno', 'teamsubmission', $name);
  108. $mform->addHelpButton('teamsubmission', 'teamsubmission', 'assign');
  109. $name = get_string('requireallteammemberssubmit', 'assign');
  110. $mform->addElement('selectyesno', 'requireallteammemberssubmit', $name);
  111. $mform->addHelpButton('requireallteammemberssubmit', 'requireallteammemberssubmit', 'assign');
  112. $mform->disabledIf('requireallteammemberssubmit', 'teamsubmission', 'eq', 0);
  113. $mform->disabledIf('requireallteammemberssubmit', 'submissiondrafts', 'eq', 0);
  114. $groupings = groups_get_all_groupings($assignment->get_course()->id);
  115. $options = array();
  116. $options[0] = get_string('none');
  117. foreach ($groupings as $grouping) {
  118. $options[$grouping->id] = $grouping->name;
  119. }
  120. $name = get_string('teamsubmissiongroupingid', 'assign');
  121. $mform->addElement('select', 'teamsubmissiongroupingid', $name, $options);
  122. $mform->addHelpButton('teamsubmissiongroupingid', 'teamsubmissiongroupingid', 'assign');
  123. $mform->disabledIf('teamsubmissiongroupingid', 'teamsubmission', 'eq', 0);
  124. $mform->addElement('header', 'notifications', get_string('notifications', 'assign'));
  125. $name = get_string('sendnotifications', 'assign');
  126. $mform->addElement('selectyesno', 'sendnotifications', $name);
  127. $mform->addHelpButton('sendnotifications', 'sendnotifications', 'assign');
  128. $name = get_string('sendlatenotifications', 'assign');
  129. $mform->addElement('selectyesno', 'sendlatenotifications', $name);
  130. $mform->addHelpButton('sendlatenotifications', 'sendlatenotifications', 'assign');
  131. $mform->disabledIf('sendlatenotifications', 'sendnotifications', 'eq', 1);
  132. // Plagiarism enabling form.
  133. if (!empty($CFG->enableplagiarism)) {
  134. require_once($CFG->libdir . '/plagiarismlib.php');
  135. plagiarism_get_form_elements_module($mform, $ctx->get_course_context(), 'mod_assign');
  136. }
  137. $this->standard_grading_coursemodule_elements();
  138. $name = get_string('blindmarking', 'assign');
  139. $mform->addElement('selectyesno', 'blindmarking', $name);
  140. $mform->addHelpButton('blindmarking', 'blindmarking', 'assign');
  141. if ($assignment->has_submissions_or_grades() ) {
  142. $mform->freeze('blindmarking');
  143. }
  144. $name = get_string('markingworkflow', 'assign');
  145. $mform->addElement('selectyesno', 'markingworkflow', $name);
  146. $mform->addHelpButton('markingworkflow', 'markingworkflow', 'assign');
  147. $name = get_string('markingallocation', 'assign');
  148. $mform->addElement('selectyesno', 'markingallocation', $name);
  149. $mform->addHelpButton('markingallocation', 'markingallocation', 'assign');
  150. $mform->disabledIf('markingallocation', 'markingworkflow', 'eq', 0);
  151. $this->standard_coursemodule_elements();
  152. $this->apply_admin_defaults();
  153. $this->add_action_buttons();
  154. // Add warning popup/noscript tag, if grades are changed by user.
  155. $hasgrade = false;
  156. if (!empty($this->_instance)) {
  157. $hasgrade = $DB->record_exists_select('assign_grades',
  158. 'assignment = ? AND grade <> -1',
  159. array($this->_instance));
  160. }
  161. if ($mform->elementExists('grade') && $hasgrade) {
  162. $module = array(
  163. 'name' => 'mod_assign',
  164. 'fullpath' => '/mod/assign/module.js',
  165. 'requires' => array('node', 'event'),
  166. 'strings' => array(array('changegradewarning', 'mod_assign'))
  167. );
  168. $PAGE->requires->js_init_call('M.mod_assign.init_grade_change', null, false, $module);
  169. // Add noscript tag in case.
  170. $noscriptwarning = $mform->createElement('static',
  171. 'warning',
  172. null,
  173. html_writer::tag('noscript',
  174. get_string('changegradewarning', 'mod_assign')));
  175. $mform->insertElementBefore($noscriptwarning, 'grade');
  176. }
  177. }
  178. /**
  179. * Perform minimal validation on the settings form
  180. * @param array $data
  181. * @param array $files
  182. */
  183. public function validation($data, $files) {
  184. $errors = parent::validation($data, $files);
  185. if ($data['allowsubmissionsfromdate'] && $data['duedate']) {
  186. if ($data['allowsubmissionsfromdate'] > $data['duedate']) {
  187. $errors['duedate'] = get_string('duedatevalidation', 'assign');
  188. }
  189. }
  190. if ($data['duedate'] && $data['cutoffdate']) {
  191. if ($data['duedate'] > $data['cutoffdate']) {
  192. $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'assign');
  193. }
  194. }
  195. if ($data['allowsubmissionsfromdate'] && $data['cutoffdate']) {
  196. if ($data['allowsubmissionsfromdate'] > $data['cutoffdate']) {
  197. $errors['cutoffdate'] = get_string('cutoffdatefromdatevalidation', 'assign');
  198. }
  199. }
  200. return $errors;
  201. }
  202. /**
  203. * Any data processing needed before the form is displayed
  204. * (needed to set up draft areas for editor and filemanager elements)
  205. * @param array $defaultvalues
  206. */
  207. public function data_preprocessing(&$defaultvalues) {
  208. global $DB;
  209. $ctx = null;
  210. if ($this->current && $this->current->coursemodule) {
  211. $cm = get_coursemodule_from_instance('assign', $this->current->id, 0, false, MUST_EXIST);
  212. $ctx = context_module::instance($cm->id);
  213. }
  214. $assignment = new assign($ctx, null, null);
  215. if ($this->current && $this->current->course) {
  216. if (!$ctx) {
  217. $ctx = context_course::instance($this->current->course);
  218. }
  219. $course = $DB->get_record('course', array('id'=>$this->current->course), '*', MUST_EXIST);
  220. $assignment->set_course($course);
  221. }
  222. $assignment->plugin_data_preprocessing($defaultvalues);
  223. }
  224. /**
  225. * Add any custom completion rules to the form.
  226. *
  227. * @return array Contains the names of the added form elements
  228. */
  229. public function add_completion_rules() {
  230. $mform =& $this->_form;
  231. $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'assign'));
  232. return array('completionsubmit');
  233. }
  234. /**
  235. * Determines if completion is enabled for this module.
  236. *
  237. * @param array $data
  238. * @return bool
  239. */
  240. public function completion_rule_enabled($data) {
  241. return !empty($data['completionsubmit']);
  242. }
  243. }