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

/mod/workshop/mod_form.php

https://bitbucket.org/moodle/moodle
PHP | 490 lines | 331 code | 70 blank | 89 comment | 52 complexity | d18f7b8a14d8250dab8b436245cedbe8 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. * The main workshop configuration form
  18. *
  19. * The UI mockup has been proposed in MDL-18688
  20. * It uses the standard core Moodle formslib. For more info about them, please
  21. * visit: http://docs.moodle.org/dev/lib/formslib.php
  22. *
  23. * @package mod_workshop
  24. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. defined('MOODLE_INTERNAL') || die();
  28. require_once($CFG->dirroot . '/course/moodleform_mod.php');
  29. require_once(__DIR__ . '/locallib.php');
  30. require_once($CFG->libdir . '/filelib.php');
  31. use core_grades\component_gradeitems;
  32. /**
  33. * Module settings form for Workshop instances
  34. */
  35. class mod_workshop_mod_form extends moodleform_mod {
  36. /** @var object the course this instance is part of */
  37. protected $course = null;
  38. /**
  39. * Constructor
  40. */
  41. public function __construct($current, $section, $cm, $course) {
  42. $this->course = $course;
  43. parent::__construct($current, $section, $cm, $course);
  44. }
  45. /**
  46. * Defines the workshop instance configuration form
  47. *
  48. * @return void
  49. */
  50. public function definition() {
  51. global $CFG, $PAGE;
  52. $workshopconfig = get_config('workshop');
  53. $mform = $this->_form;
  54. // General --------------------------------------------------------------------
  55. $mform->addElement('header', 'general', get_string('general', 'form'));
  56. // Workshop name
  57. $label = get_string('workshopname', 'workshop');
  58. $mform->addElement('text', 'name', $label, array('size' => '64'));
  59. if (!empty($CFG->formatstringstriptags)) {
  60. $mform->setType('name', PARAM_TEXT);
  61. } else {
  62. $mform->setType('name', PARAM_CLEANHTML);
  63. }
  64. $mform->addRule('name', null, 'required', null, 'client');
  65. $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  66. // Introduction
  67. $this->standard_intro_elements(get_string('introduction', 'workshop'));
  68. // Grading settings -----------------------------------------------------------
  69. $mform->addElement('header', 'gradingsettings', get_string('gradingsettings', 'workshop'));
  70. $mform->setExpanded('gradingsettings');
  71. $label = get_string('strategy', 'workshop');
  72. $mform->addElement('select', 'strategy', $label, workshop::available_strategies_list());
  73. $mform->setDefault('strategy', $workshopconfig->strategy);
  74. $mform->addHelpButton('strategy', 'strategy', 'workshop');
  75. $grades = workshop::available_maxgrades_list();
  76. $gradecategories = grade_get_categories_menu($this->course->id);
  77. $label = get_string('submissiongrade', 'workshop');
  78. $mform->addGroup(array(
  79. $mform->createElement('select', 'grade', '', $grades),
  80. $mform->createElement('select', 'gradecategory', '', $gradecategories),
  81. ), 'submissiongradegroup', $label, ' ', false);
  82. $mform->setDefault('grade', $workshopconfig->grade);
  83. $mform->addHelpButton('submissiongradegroup', 'submissiongrade', 'workshop');
  84. $mform->addElement('text', 'submissiongradepass', get_string('gradetopasssubmission', 'workshop'));
  85. $mform->addHelpButton('submissiongradepass', 'gradepass', 'grades');
  86. $mform->setDefault('submissiongradepass', '');
  87. $mform->setType('submissiongradepass', PARAM_RAW);
  88. $label = get_string('gradinggrade', 'workshop');
  89. $mform->addGroup(array(
  90. $mform->createElement('select', 'gradinggrade', '', $grades),
  91. $mform->createElement('select', 'gradinggradecategory', '', $gradecategories),
  92. ), 'gradinggradegroup', $label, ' ', false);
  93. $mform->setDefault('gradinggrade', $workshopconfig->gradinggrade);
  94. $mform->addHelpButton('gradinggradegroup', 'gradinggrade', 'workshop');
  95. $mform->addElement('text', 'gradinggradepass', get_string('gradetopassgrading', 'workshop'));
  96. $mform->addHelpButton('gradinggradepass', 'gradepass', 'grades');
  97. $mform->setDefault('gradinggradepass', '');
  98. $mform->setType('gradinggradepass', PARAM_RAW);
  99. $options = array();
  100. for ($i = 5; $i >= 0; $i--) {
  101. $options[$i] = $i;
  102. }
  103. $label = get_string('gradedecimals', 'workshop');
  104. $mform->addElement('select', 'gradedecimals', $label, $options);
  105. $mform->setDefault('gradedecimals', $workshopconfig->gradedecimals);
  106. // Submission settings --------------------------------------------------------
  107. $mform->addElement('header', 'submissionsettings', get_string('submissionsettings', 'workshop'));
  108. $label = get_string('instructauthors', 'workshop');
  109. $mform->addElement('editor', 'instructauthorseditor', $label, null,
  110. workshop::instruction_editors_options($this->context));
  111. $typeelements = [];
  112. foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
  113. $available = $type . 'available';
  114. $required = $type . 'required';
  115. $availablelabel = get_string($available, 'workshop');
  116. $requiredlabel = get_string($required, 'workshop');
  117. $typeelements[] = $mform->createElement('advcheckbox', $available, '', $availablelabel);
  118. $typeelements[] = $mform->createElement('advcheckbox', $required, '', $requiredlabel);
  119. $mform->setDefault($available, 1);
  120. }
  121. // We can't use <br> as the separator as it does not work well in this case with the Boost theme.
  122. // Instead, separate both tuples with a full-width empty div.
  123. $mform->addGroup($typeelements, 'submissiontypes', get_string('submissiontypes', 'workshop'),
  124. array(' ', '<div style="width:100%"></div>'), false);
  125. $options = array();
  126. for ($i = 7; $i >= 1; $i--) {
  127. $options[$i] = $i;
  128. }
  129. $label = get_string('nattachments', 'workshop');
  130. $mform->addElement('select', 'nattachments', $label, $options);
  131. $mform->setDefault('nattachments', 1);
  132. $mform->hideIf('nattachments', 'submissiontypefileavailable');
  133. $label = get_string('allowedfiletypesforsubmission', 'workshop');
  134. $mform->addElement('filetypes', 'submissionfiletypes', $label);
  135. $mform->addHelpButton('submissionfiletypes', 'allowedfiletypesforsubmission', 'workshop');
  136. $mform->hideIf('submissionfiletypes', 'submissiontypefileavailable');
  137. $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes, 0, $workshopconfig->maxbytes);
  138. $mform->addElement('select', 'maxbytes', get_string('maxbytes', 'workshop'), $options);
  139. $mform->setDefault('maxbytes', $workshopconfig->maxbytes);
  140. $mform->hideIf('maxbytes', 'submissiontypefileavailable');
  141. $label = get_string('latesubmissions', 'workshop');
  142. $text = get_string('latesubmissions_desc', 'workshop');
  143. $mform->addElement('checkbox', 'latesubmissions', $label, $text);
  144. $mform->addHelpButton('latesubmissions', 'latesubmissions', 'workshop');
  145. // Assessment settings --------------------------------------------------------
  146. $mform->addElement('header', 'assessmentsettings', get_string('assessmentsettings', 'workshop'));
  147. $label = get_string('instructreviewers', 'workshop');
  148. $mform->addElement('editor', 'instructreviewerseditor', $label, null,
  149. workshop::instruction_editors_options($this->context));
  150. $label = get_string('useselfassessment', 'workshop');
  151. $text = get_string('useselfassessment_desc', 'workshop');
  152. $mform->addElement('checkbox', 'useselfassessment', $label, $text);
  153. $mform->addHelpButton('useselfassessment', 'useselfassessment', 'workshop');
  154. // Feedback -------------------------------------------------------------------
  155. $mform->addElement('header', 'feedbacksettings', get_string('feedbacksettings', 'workshop'));
  156. $mform->addElement('select', 'overallfeedbackmode', get_string('overallfeedbackmode', 'mod_workshop'), array(
  157. 0 => get_string('overallfeedbackmode_0', 'mod_workshop'),
  158. 1 => get_string('overallfeedbackmode_1', 'mod_workshop'),
  159. 2 => get_string('overallfeedbackmode_2', 'mod_workshop')));
  160. $mform->addHelpButton('overallfeedbackmode', 'overallfeedbackmode', 'mod_workshop');
  161. $mform->setDefault('overallfeedbackmode', 1);
  162. $options = array();
  163. for ($i = 7; $i >= 0; $i--) {
  164. $options[$i] = $i;
  165. }
  166. $mform->addElement('select', 'overallfeedbackfiles', get_string('overallfeedbackfiles', 'workshop'), $options);
  167. $mform->setDefault('overallfeedbackfiles', 0);
  168. $mform->hideIf('overallfeedbackfiles', 'overallfeedbackmode', 'eq', 0);
  169. $label = get_string('allowedfiletypesforoverallfeedback', 'workshop');
  170. $mform->addElement('filetypes', 'overallfeedbackfiletypes', $label);
  171. $mform->addHelpButton('overallfeedbackfiletypes', 'allowedfiletypesforoverallfeedback', 'workshop');
  172. $mform->hideIf('overallfeedbackfiletypes', 'overallfeedbackfiles', 'eq', 0);
  173. $options = get_max_upload_sizes($CFG->maxbytes, $this->course->maxbytes);
  174. $mform->addElement('select', 'overallfeedbackmaxbytes', get_string('overallfeedbackmaxbytes', 'workshop'), $options);
  175. $mform->setDefault('overallfeedbackmaxbytes', $workshopconfig->maxbytes);
  176. $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackmode', 'eq', 0);
  177. $mform->hideIf('overallfeedbackmaxbytes', 'overallfeedbackfiles', 'eq', 0);
  178. $label = get_string('conclusion', 'workshop');
  179. $mform->addElement('editor', 'conclusioneditor', $label, null,
  180. workshop::instruction_editors_options($this->context));
  181. $mform->addHelpButton('conclusioneditor', 'conclusion', 'workshop');
  182. // Example submissions --------------------------------------------------------
  183. $mform->addElement('header', 'examplesubmissionssettings', get_string('examplesubmissions', 'workshop'));
  184. $label = get_string('useexamples', 'workshop');
  185. $text = get_string('useexamples_desc', 'workshop');
  186. $mform->addElement('checkbox', 'useexamples', $label, $text);
  187. $mform->addHelpButton('useexamples', 'useexamples', 'workshop');
  188. $label = get_string('examplesmode', 'workshop');
  189. $options = workshop::available_example_modes_list();
  190. $mform->addElement('select', 'examplesmode', $label, $options);
  191. $mform->setDefault('examplesmode', $workshopconfig->examplesmode);
  192. $mform->hideIf('examplesmode', 'useexamples');
  193. // Availability ---------------------------------------------------------------
  194. $mform->addElement('header', 'accesscontrol', get_string('availability', 'core'));
  195. $label = get_string('submissionstart', 'workshop');
  196. $mform->addElement('date_time_selector', 'submissionstart', $label, array('optional' => true));
  197. $label = get_string('submissionend', 'workshop');
  198. $mform->addElement('date_time_selector', 'submissionend', $label, array('optional' => true));
  199. $label = get_string('submissionendswitch', 'mod_workshop');
  200. $mform->addElement('checkbox', 'phaseswitchassessment', $label);
  201. $mform->hideIf('phaseswitchassessment', 'submissionend[enabled]');
  202. $mform->addHelpButton('phaseswitchassessment', 'submissionendswitch', 'mod_workshop');
  203. $label = get_string('assessmentstart', 'workshop');
  204. $mform->addElement('date_time_selector', 'assessmentstart', $label, array('optional' => true));
  205. $label = get_string('assessmentend', 'workshop');
  206. $mform->addElement('date_time_selector', 'assessmentend', $label, array('optional' => true));
  207. $coursecontext = context_course::instance($this->course->id);
  208. // To be removed (deprecated) with MDL-67526.
  209. plagiarism_get_form_elements_module($mform, $coursecontext, 'mod_workshop');
  210. // Common module settings, Restrict availability, Activity completion etc. ----
  211. $features = array('groups' => true, 'groupings' => true,
  212. 'outcomes' => true, 'gradecat' => false, 'idnumber' => false);
  213. $this->standard_coursemodule_elements();
  214. // Standard buttons, common to all modules ------------------------------------
  215. $this->add_action_buttons();
  216. $PAGE->requires->js_call_amd('mod_workshop/modform', 'init');
  217. }
  218. /**
  219. * Prepares the form before data are set
  220. *
  221. * Additional wysiwyg editor are prepared here, the introeditor is prepared automatically by core.
  222. * Grade items are set here because the core modedit supports single grade item only.
  223. *
  224. * @param array $data to be set
  225. * @return void
  226. */
  227. public function data_preprocessing(&$data) {
  228. if ($this->current->instance) {
  229. // editing an existing workshop - let us prepare the added editor elements (intro done automatically)
  230. $draftitemid = file_get_submitted_draft_itemid('instructauthors');
  231. $data['instructauthorseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
  232. 'mod_workshop', 'instructauthors', 0,
  233. workshop::instruction_editors_options($this->context),
  234. $data['instructauthors']);
  235. $data['instructauthorseditor']['format'] = $data['instructauthorsformat'];
  236. $data['instructauthorseditor']['itemid'] = $draftitemid;
  237. $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
  238. $data['instructreviewerseditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
  239. 'mod_workshop', 'instructreviewers', 0,
  240. workshop::instruction_editors_options($this->context),
  241. $data['instructreviewers']);
  242. $data['instructreviewerseditor']['format'] = $data['instructreviewersformat'];
  243. $data['instructreviewerseditor']['itemid'] = $draftitemid;
  244. $draftitemid = file_get_submitted_draft_itemid('conclusion');
  245. $data['conclusioneditor']['text'] = file_prepare_draft_area($draftitemid, $this->context->id,
  246. 'mod_workshop', 'conclusion', 0,
  247. workshop::instruction_editors_options($this->context),
  248. $data['conclusion']);
  249. $data['conclusioneditor']['format'] = $data['conclusionformat'];
  250. $data['conclusioneditor']['itemid'] = $draftitemid;
  251. // Set submission type checkboxes.
  252. foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
  253. $data[$type . 'available'] = 1;
  254. $data[$type . 'required'] = 0;
  255. if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_DISABLED) {
  256. $data[$type . 'available'] = 0;
  257. } else if ($data[$type] == WORKSHOP_SUBMISSION_TYPE_REQUIRED) {
  258. $data[$type . 'required'] = 1;
  259. }
  260. }
  261. } else {
  262. // adding a new workshop instance
  263. $draftitemid = file_get_submitted_draft_itemid('instructauthors');
  264. file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructauthors', 0); // no context yet, itemid not used
  265. $data['instructauthorseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
  266. $draftitemid = file_get_submitted_draft_itemid('instructreviewers');
  267. file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'instructreviewers', 0); // no context yet, itemid not used
  268. $data['instructreviewerseditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
  269. $draftitemid = file_get_submitted_draft_itemid('conclusion');
  270. file_prepare_draft_area($draftitemid, null, 'mod_workshop', 'conclusion', 0); // no context yet, itemid not used
  271. $data['conclusioneditor'] = array('text' => '', 'format' => editors_get_preferred_format(), 'itemid' => $draftitemid);
  272. }
  273. }
  274. /**
  275. * Combine submission type checkboxes into integer values for the database.
  276. *
  277. * @param stdClass $data The submitted form data.
  278. */
  279. public function data_postprocessing($data) {
  280. parent::data_postprocessing($data);
  281. foreach (['text', 'file'] as $type) {
  282. $field = 'submissiontype' . $type;
  283. $available = $field . 'available';
  284. $required = $field . 'required';
  285. if ($data->$required) {
  286. $data->$field = WORKSHOP_SUBMISSION_TYPE_REQUIRED;
  287. } else if ($data->$available) {
  288. $data->$field = WORKSHOP_SUBMISSION_TYPE_AVAILABLE;
  289. } else {
  290. $data->$field = WORKSHOP_SUBMISSION_TYPE_DISABLED;
  291. }
  292. unset($data->$available);
  293. unset($data->$required);
  294. }
  295. }
  296. /**
  297. * Set the grade item categories when editing an instance
  298. */
  299. public function definition_after_data() {
  300. $mform =& $this->_form;
  301. if ($id = $mform->getElementValue('update')) {
  302. $instance = $mform->getElementValue('instance');
  303. $gradeitems = grade_item::fetch_all(array(
  304. 'itemtype' => 'mod',
  305. 'itemmodule' => 'workshop',
  306. 'iteminstance' => $instance,
  307. 'courseid' => $this->course->id));
  308. if (!empty($gradeitems)) {
  309. foreach ($gradeitems as $gradeitem) {
  310. // here comes really crappy way how to set the value of the fields
  311. // gradecategory and gradinggradecategory - grrr QuickForms
  312. $decimalpoints = $gradeitem->get_decimals();
  313. if ($gradeitem->itemnumber == 0) {
  314. $mform->setDefault('submissiongradepass', format_float($gradeitem->gradepass, $decimalpoints));
  315. $group = $mform->getElement('submissiongradegroup');
  316. $elements = $group->getElements();
  317. foreach ($elements as $element) {
  318. if ($element->getName() == 'gradecategory') {
  319. $element->setValue($gradeitem->categoryid);
  320. }
  321. }
  322. } else if ($gradeitem->itemnumber == 1) {
  323. $mform->setDefault('gradinggradepass', format_float($gradeitem->gradepass, $decimalpoints));
  324. $group = $mform->getElement('gradinggradegroup');
  325. $elements = $group->getElements();
  326. foreach ($elements as $element) {
  327. if ($element->getName() == 'gradinggradecategory') {
  328. $element->setValue($gradeitem->categoryid);
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. $typevalues = $mform->getElementValue('submissiontypes');
  336. foreach (['submissiontypetext', 'submissiontypefile'] as $type) {
  337. // Don't leave a disabled "required" checkbox checked.
  338. if (!$typevalues[$type . 'available']) {
  339. $mform->setDefault($type . 'required', 0);
  340. }
  341. }
  342. parent::definition_after_data();
  343. }
  344. /**
  345. * Validates the form input
  346. *
  347. * @param array $data submitted data
  348. * @param array $files submitted files
  349. * @return array eventual errors indexed by the field name
  350. */
  351. public function validation($data, $files) {
  352. $errors = parent::validation($data, $files);
  353. // check the phases borders are valid
  354. if ($data['submissionstart'] > 0 and $data['submissionend'] > 0 and $data['submissionstart'] >= $data['submissionend']) {
  355. $errors['submissionend'] = get_string('submissionendbeforestart', 'mod_workshop');
  356. }
  357. if ($data['assessmentstart'] > 0 and $data['assessmentend'] > 0 and $data['assessmentstart'] >= $data['assessmentend']) {
  358. $errors['assessmentend'] = get_string('assessmentendbeforestart', 'mod_workshop');
  359. }
  360. // check the phases do not overlap
  361. if (max($data['submissionstart'], $data['submissionend']) > 0 and max($data['assessmentstart'], $data['assessmentend']) > 0) {
  362. $phasesubmissionend = max($data['submissionstart'], $data['submissionend']);
  363. $phaseassessmentstart = min($data['assessmentstart'], $data['assessmentend']);
  364. if ($phaseassessmentstart == 0) {
  365. $phaseassessmentstart = max($data['assessmentstart'], $data['assessmentend']);
  366. }
  367. if ($phasesubmissionend > 0 and $phaseassessmentstart > 0 and $phaseassessmentstart < $phasesubmissionend) {
  368. foreach (array('submissionend', 'submissionstart', 'assessmentstart', 'assessmentend') as $f) {
  369. if ($data[$f] > 0) {
  370. $errors[$f] = get_string('phasesoverlap', 'mod_workshop');
  371. break;
  372. }
  373. }
  374. }
  375. }
  376. // Check that the submission grade pass is a valid number.
  377. if (!empty($data['submissiongradepass'])) {
  378. $submissiongradefloat = unformat_float($data['submissiongradepass'], true);
  379. if ($submissiongradefloat === false) {
  380. $errors['submissiongradepass'] = get_string('err_numeric', 'form');
  381. } else {
  382. if ($submissiongradefloat > $data['grade']) {
  383. $errors['submissiongradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['grade']);
  384. }
  385. }
  386. }
  387. // Check that the grade pass is a valid number.
  388. if (!empty($data['gradinggradepass'])) {
  389. $gradepassfloat = unformat_float($data['gradinggradepass'], true);
  390. if ($gradepassfloat === false) {
  391. $errors['gradinggradepass'] = get_string('err_numeric', 'form');
  392. } else {
  393. if ($gradepassfloat > $data['gradinggrade']) {
  394. $errors['gradinggradepass'] = get_string('gradepassgreaterthangrade', 'grades', $data['gradinggrade']);
  395. }
  396. }
  397. }
  398. // We need to do a custom completion validation because workshop grade items identifiers divert from standard.
  399. // Refer to validation defined in moodleform_mod.php.
  400. if (isset($data['completionpassgrade']) && $data['completionpassgrade'] &&
  401. isset($data['completiongradeitemnumber'])) {
  402. $itemnames = component_gradeitems::get_itemname_mapping_for_component('mod_workshop');
  403. $gradepassfield = $itemnames[(int) $data['completiongradeitemnumber']] . 'gradepass';
  404. if (!isset($data[$gradepassfield]) || grade_floatval($data[$gradepassfield]) == 0) {
  405. $errors['completionpassgrade'] = get_string(
  406. 'activitygradetopassnotset',
  407. 'completion'
  408. );
  409. } else {
  410. // We have validated grade pass. Unset any errors.
  411. unset($errors['completionpassgrade']);
  412. }
  413. }
  414. if (!$data['submissiontypetextavailable'] && !$data['submissiontypefileavailable']) {
  415. // One submission type must be available.
  416. $errors['submissiontypes'] = get_string('nosubmissiontype', 'workshop');
  417. }
  418. return $errors;
  419. }
  420. }