PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/question/type/edit_question_form.php

https://bitbucket.org/ngmares/moodle
PHP | 666 lines | 443 code | 91 blank | 132 comment | 62 complexity | 4f45104f9c25a44bf9d583273aa0c5d6 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, 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. * A base class for question editing forms.
  18. *
  19. * @package moodlecore
  20. * @subpackage questiontypes
  21. * @copyright 2006 The Open University
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once($CFG->libdir.'/formslib.php');
  27. abstract class question_wizard_form extends moodleform {
  28. /**
  29. * Add all the hidden form fields used by question/question.php.
  30. */
  31. protected function add_hidden_fields() {
  32. $mform = $this->_form;
  33. $mform->addElement('hidden', 'id');
  34. $mform->setType('id', PARAM_INT);
  35. $mform->addElement('hidden', 'inpopup');
  36. $mform->setType('inpopup', PARAM_INT);
  37. $mform->addElement('hidden', 'cmid');
  38. $mform->setType('cmid', PARAM_INT);
  39. $mform->addElement('hidden', 'courseid');
  40. $mform->setType('courseid', PARAM_INT);
  41. $mform->addElement('hidden', 'returnurl');
  42. $mform->setType('returnurl', PARAM_LOCALURL);
  43. $mform->addElement('hidden', 'scrollpos');
  44. $mform->setType('scrollpos', PARAM_INT);
  45. $mform->addElement('hidden', 'appendqnumstring');
  46. $mform->setType('appendqnumstring', PARAM_ALPHA);
  47. }
  48. }
  49. /**
  50. * Form definition base class. This defines the common fields that
  51. * all question types need. Question types should define their own
  52. * class that inherits from this one, and implements the definition_inner()
  53. * method.
  54. *
  55. * @copyright 2006 The Open University
  56. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  57. */
  58. abstract class question_edit_form extends question_wizard_form {
  59. const DEFAULT_NUM_HINTS = 2;
  60. /**
  61. * Question object with options and answers already loaded by get_question_options
  62. * Be careful how you use this it is needed sometimes to set up the structure of the
  63. * form in definition_inner but data is always loaded into the form with set_data.
  64. * @var object
  65. */
  66. protected $question;
  67. protected $contexts;
  68. protected $category;
  69. protected $categorycontext;
  70. /** @var object current context */
  71. public $context;
  72. /** @var array html editor options */
  73. public $editoroptions;
  74. /** @var array options to preapre draft area */
  75. public $fileoptions;
  76. /** @var object instance of question type */
  77. public $instance;
  78. public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) {
  79. global $DB;
  80. $this->question = $question;
  81. $this->contexts = $contexts;
  82. $record = $DB->get_record('question_categories',
  83. array('id' => $question->category), 'contextid');
  84. $this->context = get_context_instance_by_id($record->contextid);
  85. $this->editoroptions = array('subdirs' => 1, 'maxfiles' => EDITOR_UNLIMITED_FILES,
  86. 'context' => $this->context);
  87. $this->fileoptions = array('subdirs' => 1, 'maxfiles' => -1, 'maxbytes' => -1);
  88. $this->category = $category;
  89. $this->categorycontext = get_context_instance_by_id($category->contextid);
  90. parent::__construct($submiturl, null, 'post', '', null, $formeditable);
  91. }
  92. /**
  93. * Build the form definition.
  94. *
  95. * This adds all the form fields that the default question type supports.
  96. * If your question type does not support all these fields, then you can
  97. * override this method and remove the ones you don't want with $mform->removeElement().
  98. */
  99. protected function definition() {
  100. global $COURSE, $CFG, $DB;
  101. $qtype = $this->qtype();
  102. $langfile = "qtype_$qtype";
  103. $mform = $this->_form;
  104. // Standard fields at the start of the form.
  105. $mform->addElement('header', 'generalheader', get_string("general", 'form'));
  106. if (!isset($this->question->id)) {
  107. if (!empty($this->question->formoptions->mustbeusable)) {
  108. $contexts = $this->contexts->having_add_and_use();
  109. } else {
  110. $contexts = $this->contexts->having_cap('moodle/question:add');
  111. }
  112. // Adding question
  113. $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
  114. array('contexts' => $contexts));
  115. } else if (!($this->question->formoptions->canmove ||
  116. $this->question->formoptions->cansaveasnew)) {
  117. // Editing question with no permission to move from category.
  118. $mform->addElement('questioncategory', 'category', get_string('category', 'question'),
  119. array('contexts' => array($this->categorycontext)));
  120. } else if ($this->question->formoptions->movecontext) {
  121. // Moving question to another context.
  122. $mform->addElement('questioncategory', 'categorymoveto',
  123. get_string('category', 'question'),
  124. array('contexts' => $this->contexts->having_cap('moodle/question:add')));
  125. } else {
  126. // Editing question with permission to move from category or save as new q
  127. $currentgrp = array();
  128. $currentgrp[0] = $mform->createElement('questioncategory', 'category',
  129. get_string('categorycurrent', 'question'),
  130. array('contexts' => array($this->categorycontext)));
  131. if ($this->question->formoptions->canedit ||
  132. $this->question->formoptions->cansaveasnew) {
  133. //not move only form
  134. $currentgrp[1] = $mform->createElement('checkbox', 'usecurrentcat', '',
  135. get_string('categorycurrentuse', 'question'));
  136. $mform->setDefault('usecurrentcat', 1);
  137. }
  138. $currentgrp[0]->freeze();
  139. $currentgrp[0]->setPersistantFreeze(false);
  140. $mform->addGroup($currentgrp, 'currentgrp',
  141. get_string('categorycurrent', 'question'), null, false);
  142. $mform->addElement('questioncategory', 'categorymoveto',
  143. get_string('categorymoveto', 'question'),
  144. array('contexts' => array($this->categorycontext)));
  145. if ($this->question->formoptions->canedit ||
  146. $this->question->formoptions->cansaveasnew) {
  147. //not move only form
  148. $mform->disabledIf('categorymoveto', 'usecurrentcat', 'checked');
  149. }
  150. }
  151. $mform->addElement('text', 'name', get_string('questionname', 'question'),
  152. array('size' => 50));
  153. $mform->setType('name', PARAM_TEXT);
  154. $mform->addRule('name', null, 'required', null, 'client');
  155. $mform->addElement('editor', 'questiontext', get_string('questiontext', 'question'),
  156. array('rows' => 15), $this->editoroptions);
  157. $mform->setType('questiontext', PARAM_RAW);
  158. $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
  159. array('size' => 3));
  160. $mform->setType('defaultmark', PARAM_FLOAT);
  161. $mform->setDefault('defaultmark', 1);
  162. $mform->addRule('defaultmark', null, 'required', null, 'client');
  163. $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
  164. array('rows' => 10), $this->editoroptions);
  165. $mform->setType('generalfeedback', PARAM_RAW);
  166. $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
  167. // Any questiontype specific fields.
  168. $this->definition_inner($mform);
  169. if (!empty($CFG->usetags)) {
  170. $mform->addElement('header', 'tagsheader', get_string('tags'));
  171. $mform->addElement('tags', 'tags', get_string('tags'));
  172. }
  173. if (!empty($this->question->id)) {
  174. $mform->addElement('header', 'createdmodifiedheader',
  175. get_string('createdmodifiedheader', 'question'));
  176. $a = new stdClass();
  177. if (!empty($this->question->createdby)) {
  178. $a->time = userdate($this->question->timecreated);
  179. $a->user = fullname($DB->get_record(
  180. 'user', array('id' => $this->question->createdby)));
  181. } else {
  182. $a->time = get_string('unknown', 'question');
  183. $a->user = get_string('unknown', 'question');
  184. }
  185. $mform->addElement('static', 'created', get_string('created', 'question'),
  186. get_string('byandon', 'question', $a));
  187. if (!empty($this->question->modifiedby)) {
  188. $a = new stdClass();
  189. $a->time = userdate($this->question->timemodified);
  190. $a->user = fullname($DB->get_record(
  191. 'user', array('id' => $this->question->modifiedby)));
  192. $mform->addElement('static', 'modified', get_string('modified', 'question'),
  193. get_string('byandon', 'question', $a));
  194. }
  195. }
  196. $this->add_hidden_fields();
  197. $mform->addElement('hidden', 'movecontext');
  198. $mform->setType('movecontext', PARAM_BOOL);
  199. $mform->addElement('hidden', 'qtype');
  200. $mform->setType('qtype', PARAM_ALPHA);
  201. $buttonarray = array();
  202. if (!empty($this->question->id)) {
  203. // Editing / moving question
  204. if ($this->question->formoptions->movecontext) {
  205. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  206. get_string('moveq', 'question'));
  207. } else if ($this->question->formoptions->canedit) {
  208. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  209. get_string('savechanges'));
  210. }
  211. if ($this->question->formoptions->cansaveasnew) {
  212. $buttonarray[] = $mform->createElement('submit', 'makecopy',
  213. get_string('makecopy', 'question'));
  214. }
  215. $buttonarray[] = $mform->createElement('cancel');
  216. } else {
  217. // Adding new question
  218. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  219. get_string('savechanges'));
  220. $buttonarray[] = $mform->createElement('cancel');
  221. }
  222. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  223. $mform->closeHeaderBefore('buttonar');
  224. if ($this->question->formoptions->movecontext) {
  225. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
  226. } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
  227. $this->question->formoptions->cansaveasnew))) {
  228. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
  229. }
  230. }
  231. /**
  232. * Add any question-type specific form fields.
  233. *
  234. * @param object $mform the form being built.
  235. */
  236. protected function definition_inner($mform) {
  237. // By default, do nothing.
  238. }
  239. /**
  240. * Get the list of form elements to repeat, one for each answer.
  241. * @param object $mform the form being built.
  242. * @param $label the label to use for each option.
  243. * @param $gradeoptions the possible grades for each answer.
  244. * @param $repeatedoptions reference to array of repeated options to fill
  245. * @param $answersoption reference to return the name of $question->options
  246. * field holding an array of answers
  247. * @return array of form fields.
  248. */
  249. protected function get_per_answer_fields($mform, $label, $gradeoptions,
  250. &$repeatedoptions, &$answersoption) {
  251. $repeated = array();
  252. $repeated[] = $mform->createElement('header', 'answerhdr', $label);
  253. $repeated[] = $mform->createElement('text', 'answer',
  254. get_string('answer', 'question'), array('size' => 80));
  255. $repeated[] = $mform->createElement('select', 'fraction',
  256. get_string('grade'), $gradeoptions);
  257. $repeated[] = $mform->createElement('editor', 'feedback',
  258. get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
  259. $repeatedoptions['answer']['type'] = PARAM_RAW;
  260. $repeatedoptions['fraction']['default'] = 0;
  261. $answersoption = 'answers';
  262. return $repeated;
  263. }
  264. /**
  265. * Add a set of form fields, obtained from get_per_answer_fields, to the form,
  266. * one for each existing answer, with some blanks for some new ones.
  267. * @param object $mform the form being built.
  268. * @param $label the label to use for each option.
  269. * @param $gradeoptions the possible grades for each answer.
  270. * @param $minoptions the minimum number of answer blanks to display.
  271. * Default QUESTION_NUMANS_START.
  272. * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
  273. */
  274. protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
  275. $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
  276. $answersoption = '';
  277. $repeatedoptions = array();
  278. $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
  279. $repeatedoptions, $answersoption);
  280. if (isset($this->question->options)) {
  281. $countanswers = count($this->question->options->$answersoption);
  282. } else {
  283. $countanswers = 0;
  284. }
  285. if ($this->question->formoptions->repeatelements) {
  286. $repeatsatstart = max($minoptions, $countanswers + $addoptions);
  287. } else {
  288. $repeatsatstart = $countanswers;
  289. }
  290. $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
  291. 'noanswers', 'addanswers', $addoptions,
  292. get_string('addmorechoiceblanks', 'qtype_multichoice'));
  293. }
  294. protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
  295. $mform = $this->_form;
  296. $mform->addElement('header', 'combinedfeedbackhdr',
  297. get_string('combinedfeedback', 'question'));
  298. $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
  299. foreach ($fields as $feedbackname) {
  300. $mform->addElement('editor', $feedbackname, get_string($feedbackname, 'question'),
  301. array('rows' => 5), $this->editoroptions);
  302. $mform->setType($feedbackname, PARAM_RAW);
  303. if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
  304. $mform->addElement('advcheckbox', 'shownumcorrect',
  305. get_string('options', 'question'),
  306. get_string('shownumpartscorrectwhenfinished', 'question'));
  307. }
  308. }
  309. }
  310. protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
  311. $mform = $this->_form;
  312. $repeated = array();
  313. $repeated[] = $mform->createElement('header', 'hinthdr', get_string('hintn', 'question'));
  314. $repeated[] = $mform->createElement('editor', 'hint', get_string('hinttext', 'question'),
  315. array('rows' => 5), $this->editoroptions);
  316. $repeatedoptions['hint']['type'] = PARAM_RAW;
  317. if ($withclearwrong) {
  318. $repeated[] = $mform->createElement('advcheckbox', 'hintclearwrong',
  319. get_string('options', 'question'), get_string('clearwrongparts', 'question'));
  320. }
  321. if ($withshownumpartscorrect) {
  322. $repeated[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
  323. get_string('shownumpartscorrect', 'question'));
  324. }
  325. return array($repeated, $repeatedoptions);
  326. }
  327. protected function add_interactive_settings($withclearwrong = false,
  328. $withshownumpartscorrect = false) {
  329. $mform = $this->_form;
  330. $mform->addElement('header', 'multitriesheader',
  331. get_string('settingsformultipletries', 'question'));
  332. $penalties = array(
  333. 1.0000000,
  334. 0.5000000,
  335. 0.3333333,
  336. 0.2500000,
  337. 0.2000000,
  338. 0.1000000,
  339. 0.0000000
  340. );
  341. if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
  342. $penalties[] = $this->question->penalty;
  343. sort($penalties);
  344. }
  345. $penaltyoptions = array();
  346. foreach ($penalties as $penalty) {
  347. $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
  348. }
  349. $mform->addElement('select', 'penalty',
  350. get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
  351. $mform->addRule('penalty', null, 'required', null, 'client');
  352. $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
  353. $mform->setDefault('penalty', 0.3333333);
  354. if (isset($this->question->hints)) {
  355. $counthints = count($this->question->hints);
  356. } else {
  357. $counthints = 0;
  358. }
  359. if ($this->question->formoptions->repeatelements) {
  360. $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
  361. } else {
  362. $repeatsatstart = $counthints;
  363. }
  364. list($repeated, $repeatedoptions) = $this->get_hint_fields(
  365. $withclearwrong, $withshownumpartscorrect);
  366. $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
  367. 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'));
  368. }
  369. public function set_data($question) {
  370. question_bank::get_qtype($question->qtype)->set_default_options($question);
  371. // prepare question text
  372. $draftid = file_get_submitted_draft_itemid('questiontext');
  373. if (!empty($question->questiontext)) {
  374. $questiontext = $question->questiontext;
  375. } else {
  376. $questiontext = $this->_form->getElement('questiontext')->getValue();
  377. $questiontext = $questiontext['text'];
  378. }
  379. $questiontext = file_prepare_draft_area($draftid, $this->context->id,
  380. 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
  381. $this->fileoptions, $questiontext);
  382. $question->questiontext = array();
  383. $question->questiontext['text'] = $questiontext;
  384. $question->questiontext['format'] = empty($question->questiontextformat) ?
  385. editors_get_preferred_format() : $question->questiontextformat;
  386. $question->questiontext['itemid'] = $draftid;
  387. // prepare general feedback
  388. $draftid = file_get_submitted_draft_itemid('generalfeedback');
  389. if (empty($question->generalfeedback)) {
  390. $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
  391. $question->generalfeedback = $generalfeedback['text'];
  392. }
  393. $feedback = file_prepare_draft_area($draftid, $this->context->id,
  394. 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
  395. $this->fileoptions, $question->generalfeedback);
  396. $question->generalfeedback = array();
  397. $question->generalfeedback['text'] = $feedback;
  398. $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
  399. editors_get_preferred_format() : $question->generalfeedbackformat;
  400. $question->generalfeedback['itemid'] = $draftid;
  401. // Remove unnecessary trailing 0s form grade fields.
  402. if (isset($question->defaultgrade)) {
  403. $question->defaultgrade = 0 + $question->defaultgrade;
  404. }
  405. if (isset($question->penalty)) {
  406. $question->penalty = 0 + $question->penalty;
  407. }
  408. // Set any options.
  409. $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
  410. if (is_array($extraquestionfields) && !empty($question->options)) {
  411. array_shift($extraquestionfields);
  412. foreach ($extraquestionfields as $field) {
  413. if (isset($question->options->$field)) {
  414. $question->$field = $question->options->$field;
  415. }
  416. }
  417. }
  418. // subclass adds data_preprocessing code here
  419. $question = $this->data_preprocessing($question);
  420. parent::set_data($question);
  421. }
  422. /**
  423. * Perform an preprocessing needed on the data passed to {@link set_data()}
  424. * before it is used to initialise the form.
  425. * @param object $question the data being passed to the form.
  426. * @return object $question the modified data.
  427. */
  428. protected function data_preprocessing($question) {
  429. return $question;
  430. }
  431. /**
  432. * Perform the necessary preprocessing for the fields added by
  433. * {@link add_per_answer_fields()}.
  434. * @param object $question the data being passed to the form.
  435. * @return object $question the modified data.
  436. */
  437. protected function data_preprocessing_answers($question, $withanswerfiles = false) {
  438. if (empty($question->options->answers)) {
  439. return $question;
  440. }
  441. $key = 0;
  442. foreach ($question->options->answers as $answer) {
  443. if ($withanswerfiles) {
  444. // Prepare the feedback editor to display files in draft area
  445. $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
  446. $question->answer[$key]['text'] = file_prepare_draft_area(
  447. $draftitemid, // draftid
  448. $this->context->id, // context
  449. 'question', // component
  450. 'answer', // filarea
  451. !empty($answer->id) ? (int) $answer->id : null, // itemid
  452. $this->fileoptions, // options
  453. $answer->answer // text
  454. );
  455. $question->answer[$key]['itemid'] = $draftitemid;
  456. $question->answer[$key]['format'] = $answer->answerformat;
  457. } else {
  458. $question->answer[$key] = $answer->answer;
  459. }
  460. $question->fraction[$key] = 0 + $answer->fraction;
  461. $question->feedback[$key] = array();
  462. // Evil hack alert. Formslib can store defaults in two ways for
  463. // repeat elements:
  464. // ->_defaultValues['fraction[0]'] and
  465. // ->_defaultValues['fraction'][0].
  466. // The $repeatedoptions['fraction']['default'] = 0 bit above means
  467. // that ->_defaultValues['fraction[0]'] has already been set, but we
  468. // are using object notation here, so we will be setting
  469. // ->_defaultValues['fraction'][0]. That does not work, so we have
  470. // to unset ->_defaultValues['fraction[0]']
  471. unset($this->_form->_defaultValues["fraction[$key]"]);
  472. // Prepare the feedback editor to display files in draft area
  473. $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
  474. $question->feedback[$key]['text'] = file_prepare_draft_area(
  475. $draftitemid, // draftid
  476. $this->context->id, // context
  477. 'question', // component
  478. 'answerfeedback', // filarea
  479. !empty($answer->id) ? (int) $answer->id : null, // itemid
  480. $this->fileoptions, // options
  481. $answer->feedback // text
  482. );
  483. $question->feedback[$key]['itemid'] = $draftitemid;
  484. $question->feedback[$key]['format'] = $answer->feedbackformat;
  485. $key++;
  486. }
  487. return $question;
  488. }
  489. /**
  490. * Perform the necessary preprocessing for the fields added by
  491. * {@link add_combined_feedback_fields()}.
  492. * @param object $question the data being passed to the form.
  493. * @return object $question the modified data.
  494. */
  495. protected function data_preprocessing_combined_feedback($question,
  496. $withshownumcorrect = false) {
  497. if (empty($question->options)) {
  498. return $question;
  499. }
  500. $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
  501. foreach ($fields as $feedbackname) {
  502. $draftid = file_get_submitted_draft_itemid($feedbackname);
  503. $feedback = array();
  504. $feedback['text'] = file_prepare_draft_area(
  505. $draftid, // draftid
  506. $this->context->id, // context
  507. 'question', // component
  508. $feedbackname, // filarea
  509. !empty($question->id) ? (int) $question->id : null, // itemid
  510. $this->fileoptions, // options
  511. $question->options->$feedbackname // text
  512. );
  513. $feedbackformat = $feedbackname . 'format';
  514. $feedback['format'] = $question->options->$feedbackformat;
  515. $feedback['itemid'] = $draftid;
  516. $question->$feedbackname = $feedback;
  517. }
  518. if ($withshownumcorrect) {
  519. $question->shownumcorrect = $question->options->shownumcorrect;
  520. }
  521. return $question;
  522. }
  523. /**
  524. * Perform the necessary preprocessing for the hint fields.
  525. * @param object $question the data being passed to the form.
  526. * @return object $question the modified data.
  527. */
  528. protected function data_preprocessing_hints($question, $withclearwrong = false,
  529. $withshownumpartscorrect = false) {
  530. if (empty($question->hints)) {
  531. return $question;
  532. }
  533. $key = 0;
  534. foreach ($question->hints as $hint) {
  535. $question->hint[$key] = array();
  536. // prepare feedback editor to display files in draft area
  537. $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
  538. $question->hint[$key]['text'] = file_prepare_draft_area(
  539. $draftitemid, // draftid
  540. $this->context->id, // context
  541. 'question', // component
  542. 'hint', // filarea
  543. !empty($hint->id) ? (int) $hint->id : null, // itemid
  544. $this->fileoptions, // options
  545. $hint->hint // text
  546. );
  547. $question->hint[$key]['itemid'] = $draftitemid;
  548. $question->hint[$key]['format'] = $hint->hintformat;
  549. $key++;
  550. if ($withclearwrong) {
  551. $question->hintclearwrong[] = $hint->clearwrong;
  552. }
  553. if ($withshownumpartscorrect) {
  554. $question->hintshownumcorrect[] = $hint->shownumcorrect;
  555. }
  556. }
  557. return $question;
  558. }
  559. public function validation($fromform, $files) {
  560. $errors = parent::validation($fromform, $files);
  561. if (empty($fromform['makecopy']) && isset($this->question->id)
  562. && ($this->question->formoptions->canedit ||
  563. $this->question->formoptions->cansaveasnew)
  564. && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
  565. $errors['currentgrp'] = get_string('nopermissionmove', 'question');
  566. }
  567. return $errors;
  568. }
  569. /**
  570. * Override this in the subclass to question type name.
  571. * @return the question type name, should be the same as the name() method
  572. * in the question type class.
  573. */
  574. public abstract function qtype();
  575. }