PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/question/type/edit_question_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 706 lines | 461 code | 97 blank | 148 comment | 63 complexity | f70ea60246003c906951cfe8c22c7fb1 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. * 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 = 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 = 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, 'maxlength' => 255));
  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->addRule('questiontext', null, 'required', null, 'client');
  159. $mform->addElement('text', 'defaultmark', get_string('defaultmark', 'question'),
  160. array('size' => 7));
  161. $mform->setType('defaultmark', PARAM_FLOAT);
  162. $mform->setDefault('defaultmark', 1);
  163. $mform->addRule('defaultmark', null, 'required', null, 'client');
  164. $mform->addElement('editor', 'generalfeedback', get_string('generalfeedback', 'question'),
  165. array('rows' => 10), $this->editoroptions);
  166. $mform->setType('generalfeedback', PARAM_RAW);
  167. $mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
  168. // Any questiontype specific fields.
  169. $this->definition_inner($mform);
  170. if (!empty($CFG->usetags)) {
  171. $mform->addElement('header', 'tagsheader', get_string('tags'));
  172. $mform->addElement('tags', 'tags', get_string('tags'));
  173. }
  174. if (!empty($this->question->id)) {
  175. $mform->addElement('header', 'createdmodifiedheader',
  176. get_string('createdmodifiedheader', 'question'));
  177. $a = new stdClass();
  178. if (!empty($this->question->createdby)) {
  179. $a->time = userdate($this->question->timecreated);
  180. $a->user = fullname($DB->get_record(
  181. 'user', array('id' => $this->question->createdby)));
  182. } else {
  183. $a->time = get_string('unknown', 'question');
  184. $a->user = get_string('unknown', 'question');
  185. }
  186. $mform->addElement('static', 'created', get_string('created', 'question'),
  187. get_string('byandon', 'question', $a));
  188. if (!empty($this->question->modifiedby)) {
  189. $a = new stdClass();
  190. $a->time = userdate($this->question->timemodified);
  191. $a->user = fullname($DB->get_record(
  192. 'user', array('id' => $this->question->modifiedby)));
  193. $mform->addElement('static', 'modified', get_string('modified', 'question'),
  194. get_string('byandon', 'question', $a));
  195. }
  196. }
  197. $this->add_hidden_fields();
  198. $mform->addElement('hidden', 'movecontext');
  199. $mform->setType('movecontext', PARAM_BOOL);
  200. $mform->addElement('hidden', 'qtype');
  201. $mform->setType('qtype', PARAM_ALPHA);
  202. $buttonarray = array();
  203. if (!empty($this->question->id)) {
  204. // Editing / moving question.
  205. if ($this->question->formoptions->movecontext) {
  206. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  207. get_string('moveq', 'question'));
  208. } else if ($this->question->formoptions->canedit) {
  209. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  210. get_string('savechanges'));
  211. }
  212. if ($this->question->formoptions->cansaveasnew) {
  213. $buttonarray[] = $mform->createElement('submit', 'makecopy',
  214. get_string('makecopy', 'question'));
  215. }
  216. $buttonarray[] = $mform->createElement('cancel');
  217. } else {
  218. // Adding new question.
  219. $buttonarray[] = $mform->createElement('submit', 'submitbutton',
  220. get_string('savechanges'));
  221. $buttonarray[] = $mform->createElement('cancel');
  222. }
  223. $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
  224. $mform->closeHeaderBefore('buttonar');
  225. if ($this->question->formoptions->movecontext) {
  226. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar'));
  227. } else if ((!empty($this->question->id)) && (!($this->question->formoptions->canedit ||
  228. $this->question->formoptions->cansaveasnew))) {
  229. $mform->hardFreezeAllVisibleExcept(array('categorymoveto', 'buttonar', 'currentgrp'));
  230. }
  231. }
  232. /**
  233. * Add any question-type specific form fields.
  234. *
  235. * @param object $mform the form being built.
  236. */
  237. protected function definition_inner($mform) {
  238. // By default, do nothing.
  239. }
  240. /**
  241. * Get the list of form elements to repeat, one for each answer.
  242. * @param object $mform the form being built.
  243. * @param $label the label to use for each option.
  244. * @param $gradeoptions the possible grades for each answer.
  245. * @param $repeatedoptions reference to array of repeated options to fill
  246. * @param $answersoption reference to return the name of $question->options
  247. * field holding an array of answers
  248. * @return array of form fields.
  249. */
  250. protected function get_per_answer_fields($mform, $label, $gradeoptions,
  251. &$repeatedoptions, &$answersoption) {
  252. $repeated = array();
  253. $answeroptions = array();
  254. $answeroptions[] = $mform->createElement('text', 'answer',
  255. $label, array('size' => 40));
  256. $answeroptions[] = $mform->createElement('select', 'fraction',
  257. get_string('grade'), $gradeoptions);
  258. $repeated[] = $mform->createElement('group', 'answeroptions',
  259. $label, $answeroptions, null, false);
  260. $repeated[] = $mform->createElement('editor', 'feedback',
  261. get_string('feedback', 'question'), array('rows' => 5), $this->editoroptions);
  262. $repeatedoptions['answer']['type'] = PARAM_RAW;
  263. $repeatedoptions['fraction']['default'] = 0;
  264. $answersoption = 'answers';
  265. return $repeated;
  266. }
  267. /**
  268. * Add a set of form fields, obtained from get_per_answer_fields, to the form,
  269. * one for each existing answer, with some blanks for some new ones.
  270. * @param object $mform the form being built.
  271. * @param $label the label to use for each option.
  272. * @param $gradeoptions the possible grades for each answer.
  273. * @param $minoptions the minimum number of answer blanks to display.
  274. * Default QUESTION_NUMANS_START.
  275. * @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
  276. */
  277. protected function add_per_answer_fields(&$mform, $label, $gradeoptions,
  278. $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
  279. $mform->addElement('header', 'answerhdr',
  280. get_string('answers', 'question'), '');
  281. $mform->setExpanded('answerhdr', 1);
  282. $answersoption = '';
  283. $repeatedoptions = array();
  284. $repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions,
  285. $repeatedoptions, $answersoption);
  286. if (isset($this->question->options)) {
  287. $repeatsatstart = count($this->question->options->$answersoption);
  288. } else {
  289. $repeatsatstart = $minoptions;
  290. }
  291. $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
  292. 'noanswers', 'addanswers', $addoptions,
  293. $this->get_more_choices_string(), true);
  294. }
  295. /**
  296. * Language string to use for 'Add {no} more {whatever we call answers}'.
  297. */
  298. protected function get_more_choices_string() {
  299. return get_string('addmorechoiceblanks', 'question');
  300. }
  301. protected function add_combined_feedback_fields($withshownumpartscorrect = false) {
  302. $mform = $this->_form;
  303. $mform->addElement('header', 'combinedfeedbackhdr',
  304. get_string('combinedfeedback', 'question'));
  305. $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
  306. foreach ($fields as $feedbackname) {
  307. $element = $mform->addElement('editor', $feedbackname,
  308. get_string($feedbackname, 'question'),
  309. array('rows' => 5), $this->editoroptions);
  310. $mform->setType($feedbackname, PARAM_RAW);
  311. // Using setValue() as setDefault() does not work for the editor class.
  312. $element->setValue(array('text'=>get_string($feedbackname.'default', 'question')));
  313. if ($withshownumpartscorrect && $feedbackname == 'partiallycorrectfeedback') {
  314. $mform->addElement('advcheckbox', 'shownumcorrect',
  315. get_string('options', 'question'),
  316. get_string('shownumpartscorrectwhenfinished', 'question'));
  317. $mform->setDefault('shownumcorrect', true);
  318. }
  319. }
  320. }
  321. /**
  322. * Create the form elements required by one hint.
  323. * @param string $withclearwrong whether this quesiton type uses the 'Clear wrong' option on hints.
  324. * @param string $withshownumpartscorrect whether this quesiton type uses the 'Show num parts correct' option on hints.
  325. * @return array form field elements for one hint.
  326. */
  327. protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
  328. $mform = $this->_form;
  329. $repeatedoptions = array();
  330. $repeated = array();
  331. $repeated[] = $mform->createElement('editor', 'hint', get_string('hintn', 'question'),
  332. array('rows' => 5), $this->editoroptions);
  333. $repeatedoptions['hint']['type'] = PARAM_RAW;
  334. $optionelements = array();
  335. if ($withclearwrong) {
  336. $optionelements[] = $mform->createElement('advcheckbox', 'hintclearwrong',
  337. get_string('options', 'question'), get_string('clearwrongparts', 'question'));
  338. }
  339. if ($withshownumpartscorrect) {
  340. $optionelements[] = $mform->createElement('advcheckbox', 'hintshownumcorrect', '',
  341. get_string('shownumpartscorrect', 'question'));
  342. }
  343. if (count($optionelements)) {
  344. $repeated[] = $mform->createElement('group', 'hintoptions',
  345. get_string('hintnoptions', 'question'), $optionelements, null, false);
  346. }
  347. return array($repeated, $repeatedoptions);
  348. }
  349. protected function add_interactive_settings($withclearwrong = false,
  350. $withshownumpartscorrect = false) {
  351. $mform = $this->_form;
  352. $mform->addElement('header', 'multitriesheader',
  353. get_string('settingsformultipletries', 'question'));
  354. $penalties = array(
  355. 1.0000000,
  356. 0.5000000,
  357. 0.3333333,
  358. 0.2500000,
  359. 0.2000000,
  360. 0.1000000,
  361. 0.0000000
  362. );
  363. if (!empty($this->question->penalty) && !in_array($this->question->penalty, $penalties)) {
  364. $penalties[] = $this->question->penalty;
  365. sort($penalties);
  366. }
  367. $penaltyoptions = array();
  368. foreach ($penalties as $penalty) {
  369. $penaltyoptions["$penalty"] = (100 * $penalty) . '%';
  370. }
  371. $mform->addElement('select', 'penalty',
  372. get_string('penaltyforeachincorrecttry', 'question'), $penaltyoptions);
  373. $mform->addHelpButton('penalty', 'penaltyforeachincorrecttry', 'question');
  374. $mform->setDefault('penalty', 0.3333333);
  375. if (isset($this->question->hints)) {
  376. $counthints = count($this->question->hints);
  377. } else {
  378. $counthints = 0;
  379. }
  380. if ($this->question->formoptions->repeatelements) {
  381. $repeatsatstart = max(self::DEFAULT_NUM_HINTS, $counthints);
  382. } else {
  383. $repeatsatstart = $counthints;
  384. }
  385. list($repeated, $repeatedoptions) = $this->get_hint_fields(
  386. $withclearwrong, $withshownumpartscorrect);
  387. $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions,
  388. 'numhints', 'addhint', 1, get_string('addanotherhint', 'question'), true);
  389. }
  390. public function set_data($question) {
  391. question_bank::get_qtype($question->qtype)->set_default_options($question);
  392. // Prepare question text.
  393. $draftid = file_get_submitted_draft_itemid('questiontext');
  394. if (!empty($question->questiontext)) {
  395. $questiontext = $question->questiontext;
  396. } else {
  397. $questiontext = $this->_form->getElement('questiontext')->getValue();
  398. $questiontext = $questiontext['text'];
  399. }
  400. $questiontext = file_prepare_draft_area($draftid, $this->context->id,
  401. 'question', 'questiontext', empty($question->id) ? null : (int) $question->id,
  402. $this->fileoptions, $questiontext);
  403. $question->questiontext = array();
  404. $question->questiontext['text'] = $questiontext;
  405. $question->questiontext['format'] = empty($question->questiontextformat) ?
  406. editors_get_preferred_format() : $question->questiontextformat;
  407. $question->questiontext['itemid'] = $draftid;
  408. // Prepare general feedback.
  409. $draftid = file_get_submitted_draft_itemid('generalfeedback');
  410. if (empty($question->generalfeedback)) {
  411. $generalfeedback = $this->_form->getElement('generalfeedback')->getValue();
  412. $question->generalfeedback = $generalfeedback['text'];
  413. }
  414. $feedback = file_prepare_draft_area($draftid, $this->context->id,
  415. 'question', 'generalfeedback', empty($question->id) ? null : (int) $question->id,
  416. $this->fileoptions, $question->generalfeedback);
  417. $question->generalfeedback = array();
  418. $question->generalfeedback['text'] = $feedback;
  419. $question->generalfeedback['format'] = empty($question->generalfeedbackformat) ?
  420. editors_get_preferred_format() : $question->generalfeedbackformat;
  421. $question->generalfeedback['itemid'] = $draftid;
  422. // Remove unnecessary trailing 0s form grade fields.
  423. if (isset($question->defaultgrade)) {
  424. $question->defaultgrade = 0 + $question->defaultgrade;
  425. }
  426. if (isset($question->penalty)) {
  427. $question->penalty = 0 + $question->penalty;
  428. }
  429. // Set any options.
  430. $extraquestionfields = question_bank::get_qtype($question->qtype)->extra_question_fields();
  431. if (is_array($extraquestionfields) && !empty($question->options)) {
  432. array_shift($extraquestionfields);
  433. foreach ($extraquestionfields as $field) {
  434. if (property_exists($question->options, $field)) {
  435. $question->$field = $question->options->$field;
  436. }
  437. }
  438. }
  439. // Subclass adds data_preprocessing code here.
  440. $question = $this->data_preprocessing($question);
  441. parent::set_data($question);
  442. }
  443. /**
  444. * Perform an preprocessing needed on the data passed to {@link set_data()}
  445. * before it is used to initialise the form.
  446. * @param object $question the data being passed to the form.
  447. * @return object $question the modified data.
  448. */
  449. protected function data_preprocessing($question) {
  450. return $question;
  451. }
  452. /**
  453. * Perform the necessary preprocessing for the fields added by
  454. * {@link add_per_answer_fields()}.
  455. * @param object $question the data being passed to the form.
  456. * @return object $question the modified data.
  457. */
  458. protected function data_preprocessing_answers($question, $withanswerfiles = false) {
  459. if (empty($question->options->answers)) {
  460. return $question;
  461. }
  462. $key = 0;
  463. foreach ($question->options->answers as $answer) {
  464. if ($withanswerfiles) {
  465. // Prepare the feedback editor to display files in draft area.
  466. $draftitemid = file_get_submitted_draft_itemid('answer['.$key.']');
  467. $question->answer[$key]['text'] = file_prepare_draft_area(
  468. $draftitemid, // Draftid
  469. $this->context->id, // context
  470. 'question', // component
  471. 'answer', // filarea
  472. !empty($answer->id) ? (int) $answer->id : null, // itemid
  473. $this->fileoptions, // options
  474. $answer->answer // text.
  475. );
  476. $question->answer[$key]['itemid'] = $draftitemid;
  477. $question->answer[$key]['format'] = $answer->answerformat;
  478. } else {
  479. $question->answer[$key] = $answer->answer;
  480. }
  481. $question->fraction[$key] = 0 + $answer->fraction;
  482. $question->feedback[$key] = array();
  483. // Evil hack alert. Formslib can store defaults in two ways for
  484. // repeat elements:
  485. // ->_defaultValues['fraction[0]'] and
  486. // ->_defaultValues['fraction'][0].
  487. // The $repeatedoptions['fraction']['default'] = 0 bit above means
  488. // that ->_defaultValues['fraction[0]'] has already been set, but we
  489. // are using object notation here, so we will be setting
  490. // ->_defaultValues['fraction'][0]. That does not work, so we have
  491. // to unset ->_defaultValues['fraction[0]'].
  492. unset($this->_form->_defaultValues["fraction[$key]"]);
  493. // Prepare the feedback editor to display files in draft area.
  494. $draftitemid = file_get_submitted_draft_itemid('feedback['.$key.']');
  495. $question->feedback[$key]['text'] = file_prepare_draft_area(
  496. $draftitemid, // Draftid
  497. $this->context->id, // context
  498. 'question', // component
  499. 'answerfeedback', // filarea
  500. !empty($answer->id) ? (int) $answer->id : null, // itemid
  501. $this->fileoptions, // options
  502. $answer->feedback // text.
  503. );
  504. $question->feedback[$key]['itemid'] = $draftitemid;
  505. $question->feedback[$key]['format'] = $answer->feedbackformat;
  506. $key++;
  507. }
  508. return $question;
  509. }
  510. /**
  511. * Perform the necessary preprocessing for the fields added by
  512. * {@link add_combined_feedback_fields()}.
  513. * @param object $question the data being passed to the form.
  514. * @return object $question the modified data.
  515. */
  516. protected function data_preprocessing_combined_feedback($question,
  517. $withshownumcorrect = false) {
  518. if (empty($question->options)) {
  519. return $question;
  520. }
  521. $fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
  522. foreach ($fields as $feedbackname) {
  523. $draftid = file_get_submitted_draft_itemid($feedbackname);
  524. $feedback = array();
  525. $feedback['text'] = file_prepare_draft_area(
  526. $draftid, // Draftid
  527. $this->context->id, // context
  528. 'question', // component
  529. $feedbackname, // filarea
  530. !empty($question->id) ? (int) $question->id : null, // itemid
  531. $this->fileoptions, // options
  532. $question->options->$feedbackname // text.
  533. );
  534. $feedbackformat = $feedbackname . 'format';
  535. $feedback['format'] = $question->options->$feedbackformat;
  536. $feedback['itemid'] = $draftid;
  537. $question->$feedbackname = $feedback;
  538. }
  539. if ($withshownumcorrect) {
  540. $question->shownumcorrect = $question->options->shownumcorrect;
  541. }
  542. return $question;
  543. }
  544. /**
  545. * Perform the necessary preprocessing for the hint fields.
  546. * @param object $question the data being passed to the form.
  547. * @return object $question the modified data.
  548. */
  549. protected function data_preprocessing_hints($question, $withclearwrong = false,
  550. $withshownumpartscorrect = false) {
  551. if (empty($question->hints)) {
  552. return $question;
  553. }
  554. $key = 0;
  555. foreach ($question->hints as $hint) {
  556. $question->hint[$key] = array();
  557. // Prepare feedback editor to display files in draft area.
  558. $draftitemid = file_get_submitted_draft_itemid('hint['.$key.']');
  559. $question->hint[$key]['text'] = file_prepare_draft_area(
  560. $draftitemid, // Draftid
  561. $this->context->id, // context
  562. 'question', // component
  563. 'hint', // filarea
  564. !empty($hint->id) ? (int) $hint->id : null, // itemid
  565. $this->fileoptions, // options
  566. $hint->hint // text.
  567. );
  568. $question->hint[$key]['itemid'] = $draftitemid;
  569. $question->hint[$key]['format'] = $hint->hintformat;
  570. $key++;
  571. if ($withclearwrong) {
  572. $question->hintclearwrong[] = $hint->clearwrong;
  573. }
  574. if ($withshownumpartscorrect) {
  575. $question->hintshownumcorrect[] = $hint->shownumcorrect;
  576. }
  577. }
  578. return $question;
  579. }
  580. public function validation($fromform, $files) {
  581. $errors = parent::validation($fromform, $files);
  582. if (empty($fromform['makecopy']) && isset($this->question->id)
  583. && ($this->question->formoptions->canedit ||
  584. $this->question->formoptions->cansaveasnew)
  585. && empty($fromform['usecurrentcat']) && !$this->question->formoptions->canmove) {
  586. $errors['currentgrp'] = get_string('nopermissionmove', 'question');
  587. }
  588. // Default mark.
  589. if (array_key_exists('defaultmark', $fromform) && $fromform['defaultmark'] < 0) {
  590. $errors['defaultmark'] = get_string('defaultmarkmustbepositive', 'question');
  591. }
  592. return $errors;
  593. }
  594. /**
  595. * Override this in the subclass to question type name.
  596. * @return the question type name, should be the same as the name() method
  597. * in the question type class.
  598. */
  599. public abstract function qtype();
  600. /**
  601. * Returns an array of editor options with collapsed options turned off.
  602. * @deprecated since 2.6
  603. * @return array
  604. */
  605. protected function get_non_collabsible_editor_options() {
  606. debugging('get_non_collabsible_editor_options() is deprecated, use $this->editoroptions instead.', DEBUG_DEVELOPER);
  607. return $this->editoroptions;
  608. }
  609. }