PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/workshop/mod_form.php

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