PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/questionlib.php

https://github.com/mylescarrick/moodle
PHP | 3266 lines | 1761 code | 335 blank | 1170 comment | 312 complexity | cfcc2767f25930436a1dee24f347ec7e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  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. * Code for handling and processing questions
  18. *
  19. * This is code that is module independent, i.e., can be used by any module that
  20. * uses questions, like quiz, lesson, ..
  21. * This script also loads the questiontype classes
  22. * Code for handling the editing of questions is in {@link question/editlib.php}
  23. *
  24. * TODO: separate those functions which form part of the API
  25. * from the helper functions.
  26. *
  27. * Major Contributors
  28. * - Alex Smith, Julian Sedding and Gustav Delius {@link http://maths.york.ac.uk/serving_maths}
  29. *
  30. * @package core
  31. * @subpackage question
  32. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. defined('MOODLE_INTERNAL') || die();
  36. /// CONSTANTS ///////////////////////////////////
  37. /**#@+
  38. * The different types of events that can create question states
  39. */
  40. define('QUESTION_EVENTOPEN', '0'); // The state was created by Moodle
  41. define('QUESTION_EVENTNAVIGATE', '1'); // The responses were saved because the student navigated to another page (this is not currently used)
  42. define('QUESTION_EVENTSAVE', '2'); // The student has requested that the responses should be saved but not submitted or validated
  43. define('QUESTION_EVENTGRADE', '3'); // Moodle has graded the responses. A SUBMIT event can be changed to a GRADE event by Moodle.
  44. define('QUESTION_EVENTDUPLICATE', '4'); // The responses submitted were the same as previously
  45. define('QUESTION_EVENTVALIDATE', '5'); // The student has requested a validation. This causes the responses to be saved as well, but not graded.
  46. define('QUESTION_EVENTCLOSEANDGRADE', '6'); // Moodle has graded the responses. A CLOSE event can be changed to a CLOSEANDGRADE event by Moodle.
  47. define('QUESTION_EVENTSUBMIT', '7'); // The student response has been submitted but it has not yet been marked
  48. define('QUESTION_EVENTCLOSE', '8'); // The response has been submitted and the session has been closed, either because the student requested it or because Moodle did it (e.g. because of a timelimit). The responses have not been graded.
  49. define('QUESTION_EVENTMANUALGRADE', '9'); // Grade was entered by teacher
  50. define('QUESTION_EVENTS_GRADED', QUESTION_EVENTGRADE.','.
  51. QUESTION_EVENTCLOSEANDGRADE.','.
  52. QUESTION_EVENTMANUALGRADE);
  53. define('QUESTION_EVENTS_CLOSED', QUESTION_EVENTCLOSE.','.
  54. QUESTION_EVENTCLOSEANDGRADE.','.
  55. QUESTION_EVENTMANUALGRADE);
  56. define('QUESTION_EVENTS_CLOSED_OR_GRADED', QUESTION_EVENTGRADE.','.
  57. QUESTION_EVENTS_CLOSED);
  58. /**#@-*/
  59. /**#@+
  60. * The core question types.
  61. */
  62. define("SHORTANSWER", "shortanswer");
  63. define("TRUEFALSE", "truefalse");
  64. define("MULTICHOICE", "multichoice");
  65. define("RANDOM", "random");
  66. define("MATCH", "match");
  67. define("RANDOMSAMATCH", "randomsamatch");
  68. define("DESCRIPTION", "description");
  69. define("NUMERICAL", "numerical");
  70. define("MULTIANSWER", "multianswer");
  71. define("CALCULATED", "calculated");
  72. define("ESSAY", "essay");
  73. /**#@-*/
  74. /**
  75. * Constant determines the number of answer boxes supplied in the editing
  76. * form for multiple choice and similar question types.
  77. */
  78. define("QUESTION_NUMANS", "10");
  79. /**
  80. * Constant determines the number of answer boxes supplied in the editing
  81. * form for multiple choice and similar question types to start with, with
  82. * the option of adding QUESTION_NUMANS_ADD more answers.
  83. */
  84. define("QUESTION_NUMANS_START", 3);
  85. /**
  86. * Constant determines the number of answer boxes to add in the editing
  87. * form for multiple choice and similar question types when the user presses
  88. * 'add form fields button'.
  89. */
  90. define("QUESTION_NUMANS_ADD", 3);
  91. /**
  92. * The options used when popping up a question preview window in Javascript.
  93. */
  94. define('QUESTION_PREVIEW_POPUP_OPTIONS', 'scrollbars=yes&resizable=yes&width=700&height=540');
  95. /**#@+
  96. * Option flags for ->optionflags
  97. * The options are read out via bitwise operation using these constants
  98. */
  99. /**
  100. * Whether the questions is to be run in adaptive mode. If this is not set then
  101. * a question closes immediately after the first submission of responses. This
  102. * is how question is Moodle always worked before version 1.5
  103. */
  104. define('QUESTION_ADAPTIVE', 1);
  105. /**#@-*/
  106. /**#@+
  107. * Options for whether flags are shown/editable when rendering questions.
  108. */
  109. define('QUESTION_FLAGSHIDDEN', 0);
  110. define('QUESTION_FLAGSSHOWN', 1);
  111. define('QUESTION_FLAGSEDITABLE', 2);
  112. /**#@-*/
  113. /**
  114. * GLOBAL VARAIBLES
  115. * @global array $QTYPES
  116. * @name $QTYPES
  117. */
  118. global $QTYPES;
  119. /**
  120. * Array holding question type objects. Initialised via calls to
  121. * question_register_questiontype as the question type classes are included.
  122. */
  123. $QTYPES = array();
  124. /**
  125. * Add a new question type to the various global arrays above.
  126. *
  127. * @global object
  128. * @param object $qtype An instance of the new question type class.
  129. */
  130. function question_register_questiontype($qtype) {
  131. global $QTYPES;
  132. $name = $qtype->name();
  133. $QTYPES[$name] = $qtype;
  134. }
  135. require_once("$CFG->dirroot/question/type/questiontype.php");
  136. // Load the questiontype.php file for each question type
  137. // These files in turn call question_register_questiontype()
  138. // with a new instance of each qtype class.
  139. $qtypenames = get_plugin_list('qtype');
  140. foreach($qtypenames as $qtypename => $qdir) {
  141. // Instanciates all plug-in question types
  142. $qtypefilepath= "$qdir/questiontype.php";
  143. // echo "Loading $qtypename<br/>"; // Uncomment for debugging
  144. if (is_readable($qtypefilepath)) {
  145. require_once($qtypefilepath);
  146. }
  147. }
  148. /**
  149. * An array of question type names translated to the user's language, suitable for use when
  150. * creating a drop-down menu of options.
  151. *
  152. * Long-time Moodle programmers will realise that this replaces the old $QTYPE_MENU array.
  153. * The array returned will only hold the names of all the question types that the user should
  154. * be able to create directly. Some internal question types like random questions are excluded.
  155. *
  156. * @global object
  157. * @return array an array of question type names translated to the user's language.
  158. */
  159. function question_type_menu() {
  160. global $QTYPES;
  161. static $menuoptions = null;
  162. if (is_null($menuoptions)) {
  163. $config = get_config('question');
  164. $menuoptions = array();
  165. foreach ($QTYPES as $name => $qtype) {
  166. // Get the name if this qtype is enabled.
  167. $menuname = $qtype->menu_name();
  168. $enabledvar = $name . '_disabled';
  169. if ($menuname && !isset($config->$enabledvar)) {
  170. $menuoptions[$name] = $menuname;
  171. }
  172. }
  173. $menuoptions = question_sort_qtype_array($menuoptions, $config);
  174. }
  175. return $menuoptions;
  176. }
  177. /**
  178. * Sort an array of question type names according to the question type sort order stored in
  179. * config_plugins. Entries for which there is no xxx_sortorder defined will go
  180. * at the end, sorted according to textlib_get_instance()->asort($inarray).
  181. * @param $inarray an array $qtype => $QTYPES[$qtype]->local_name().
  182. * @param $config get_config('question'), if you happen to have it around, to save one DB query.
  183. * @return array the sorted version of $inarray.
  184. */
  185. function question_sort_qtype_array($inarray, $config = null) {
  186. if (is_null($config)) {
  187. $config = get_config('question');
  188. }
  189. $sortorder = array();
  190. foreach ($inarray as $name => $notused) {
  191. $sortvar = $name . '_sortorder';
  192. if (isset($config->$sortvar)) {
  193. $sortorder[$config->$sortvar] = $name;
  194. }
  195. }
  196. ksort($sortorder);
  197. $outarray = array();
  198. foreach ($sortorder as $name) {
  199. $outarray[$name] = $inarray[$name];
  200. unset($inarray[$name]);
  201. }
  202. textlib_get_instance()->asort($inarray);
  203. return array_merge($outarray, $inarray);
  204. }
  205. /**
  206. * Move one question type in a list of question types. If you try to move one element
  207. * off of the end, nothing will change.
  208. *
  209. * @param array $sortedqtypes An array $qtype => anything.
  210. * @param string $tomove one of the keys from $sortedqtypes
  211. * @param integer $direction +1 or -1
  212. * @return array an array $index => $qtype, with $index from 0 to n in order, and
  213. * the $qtypes in the same order as $sortedqtypes, except that $tomove will
  214. * have been moved one place.
  215. */
  216. function question_reorder_qtypes($sortedqtypes, $tomove, $direction) {
  217. $neworder = array_keys($sortedqtypes);
  218. // Find the element to move.
  219. $key = array_search($tomove, $neworder);
  220. if ($key === false) {
  221. return $neworder;
  222. }
  223. // Work out the other index.
  224. $otherkey = $key + $direction;
  225. if (!isset($neworder[$otherkey])) {
  226. return $neworder;
  227. }
  228. // Do the swap.
  229. $swap = $neworder[$otherkey];
  230. $neworder[$otherkey] = $neworder[$key];
  231. $neworder[$key] = $swap;
  232. return $neworder;
  233. }
  234. /**
  235. * Save a new question type order to the config_plugins table.
  236. * @global object
  237. * @param $neworder An arra $index => $qtype. Indices should start at 0 and be in order.
  238. * @param $config get_config('question'), if you happen to have it around, to save one DB query.
  239. */
  240. function question_save_qtype_order($neworder, $config = null) {
  241. global $DB;
  242. if (is_null($config)) {
  243. $config = get_config('question');
  244. }
  245. foreach ($neworder as $index => $qtype) {
  246. $sortvar = $qtype . '_sortorder';
  247. if (!isset($config->$sortvar) || $config->$sortvar != $index + 1) {
  248. set_config($sortvar, $index + 1, 'question');
  249. }
  250. }
  251. }
  252. /// OTHER CLASSES /////////////////////////////////////////////////////////
  253. /**
  254. * This holds the options that are set by the course module
  255. *
  256. * @package moodlecore
  257. * @subpackage question
  258. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  259. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  260. */
  261. class cmoptions {
  262. /**
  263. * Whether a new attempt should be based on the previous one. If true
  264. * then a new attempt will start in a state where all responses are set
  265. * to the last responses from the previous attempt.
  266. */
  267. var $attemptonlast = false;
  268. /**
  269. * Various option flags. The flags are accessed via bitwise operations
  270. * using the constants defined in the CONSTANTS section above.
  271. */
  272. var $optionflags = QUESTION_ADAPTIVE;
  273. /**
  274. * Determines whether in the calculation of the score for a question
  275. * penalties for earlier wrong responses within the same attempt will
  276. * be subtracted.
  277. */
  278. var $penaltyscheme = true;
  279. /**
  280. * The maximum time the user is allowed to answer the questions withing
  281. * an attempt. This is measured in minutes so needs to be multiplied by
  282. * 60 before compared to timestamps. If set to 0 no timelimit will be applied
  283. */
  284. var $timelimit = 0;
  285. /**
  286. * Timestamp for the closing time. Responses submitted after this time will
  287. * be saved but no credit will be given for them.
  288. */
  289. var $timeclose = 9999999999;
  290. /**
  291. * The id of the course from withing which the question is currently being used
  292. */
  293. var $course = SITEID;
  294. /**
  295. * Whether the answers in a multiple choice question should be randomly
  296. * shuffled when a new attempt is started.
  297. */
  298. var $shuffleanswers = true;
  299. /**
  300. * The number of decimals to be shown when scores are printed
  301. */
  302. var $decimalpoints = 2;
  303. }
  304. /// FUNCTIONS //////////////////////////////////////////////////////
  305. /**
  306. * Returns an array of names of activity modules that use this question
  307. *
  308. * @global object
  309. * @global object
  310. * @param object $questionid
  311. * @return array of strings
  312. */
  313. function question_list_instances($questionid) {
  314. global $CFG, $DB;
  315. $instances = array();
  316. $modules = $DB->get_records('modules');
  317. foreach ($modules as $module) {
  318. $fullmod = $CFG->dirroot . '/mod/' . $module->name;
  319. if (file_exists($fullmod . '/lib.php')) {
  320. include_once($fullmod . '/lib.php');
  321. $fn = $module->name.'_question_list_instances';
  322. if (function_exists($fn)) {
  323. $instances = $instances + $fn($questionid);
  324. }
  325. }
  326. }
  327. return $instances;
  328. }
  329. /**
  330. * Determine whether there arey any questions belonging to this context, that is whether any of its
  331. * question categories contain any questions. This will return true even if all the questions are
  332. * hidden.
  333. *
  334. * @global object
  335. * @param mixed $context either a context object, or a context id.
  336. * @return boolean whether any of the question categories beloning to this context have
  337. * any questions in them.
  338. */
  339. function question_context_has_any_questions($context) {
  340. global $DB;
  341. if (is_object($context)) {
  342. $contextid = $context->id;
  343. } else if (is_numeric($context)) {
  344. $contextid = $context;
  345. } else {
  346. print_error('invalidcontextinhasanyquestions', 'question');
  347. }
  348. return $DB->record_exists_sql("SELECT *
  349. FROM {question} q
  350. JOIN {question_categories} qc ON qc.id = q.category
  351. WHERE qc.contextid = ? AND q.parent = 0", array($contextid));
  352. }
  353. /**
  354. * Returns list of 'allowed' grades for grade selection
  355. * formatted suitably for dropdown box function
  356. * @return object ->gradeoptionsfull full array ->gradeoptions +ve only
  357. */
  358. function get_grade_options() {
  359. // define basic array of grades. This list comprises all fractions of the form:
  360. // a. p/q for q <= 6, 0 <= p <= q
  361. // b. p/10 for 0 <= p <= 10
  362. // c. 1/q for 1 <= q <= 10
  363. // d. 1/20
  364. $grades = array(
  365. 1.0000000,
  366. 0.9000000,
  367. 0.8333333,
  368. 0.8000000,
  369. 0.7500000,
  370. 0.7000000,
  371. 0.6666667,
  372. 0.6000000,
  373. 0.5000000,
  374. 0.4000000,
  375. 0.3333333,
  376. 0.3000000,
  377. 0.2500000,
  378. 0.2000000,
  379. 0.1666667,
  380. 0.1428571,
  381. 0.1250000,
  382. 0.1111111,
  383. 0.1000000,
  384. 0.0500000,
  385. 0.0000000);
  386. // iterate through grades generating full range of options
  387. $gradeoptionsfull = array();
  388. $gradeoptions = array();
  389. foreach ($grades as $grade) {
  390. $percentage = 100 * $grade;
  391. $neggrade = -$grade;
  392. $gradeoptions["$grade"] = "$percentage %";
  393. $gradeoptionsfull["$grade"] = "$percentage %";
  394. $gradeoptionsfull["$neggrade"] = -$percentage." %";
  395. }
  396. $gradeoptionsfull["0"] = $gradeoptions["0"] = get_string("none");
  397. // sort lists
  398. arsort($gradeoptions, SORT_NUMERIC);
  399. arsort($gradeoptionsfull, SORT_NUMERIC);
  400. // construct return object
  401. $grades = new stdClass;
  402. $grades->gradeoptions = $gradeoptions;
  403. $grades->gradeoptionsfull = $gradeoptionsfull;
  404. return $grades;
  405. }
  406. /**
  407. * match grade options
  408. * if no match return error or match nearest
  409. * @param array $gradeoptionsfull list of valid options
  410. * @param int $grade grade to be tested
  411. * @param string $matchgrades 'error' or 'nearest'
  412. * @return mixed either 'fixed' value or false if erro
  413. */
  414. function match_grade_options($gradeoptionsfull, $grade, $matchgrades='error') {
  415. // if we just need an error...
  416. if ($matchgrades=='error') {
  417. foreach($gradeoptionsfull as $value => $option) {
  418. // slightly fuzzy test, never check floats for equality :-)
  419. if (abs($grade-$value)<0.00001) {
  420. return $grade;
  421. }
  422. }
  423. // didn't find a match so that's an error
  424. return false;
  425. }
  426. // work out nearest value
  427. else if ($matchgrades=='nearest') {
  428. $hownear = array();
  429. foreach($gradeoptionsfull as $value => $option) {
  430. if ($grade==$value) {
  431. return $grade;
  432. }
  433. $hownear[ $value ] = abs( $grade - $value );
  434. }
  435. // reverse sort list of deltas and grab the last (smallest)
  436. asort( $hownear, SORT_NUMERIC );
  437. reset( $hownear );
  438. return key( $hownear );
  439. }
  440. else {
  441. return false;
  442. }
  443. }
  444. /**
  445. * Tests whether a category is in use by any activity module
  446. *
  447. * @global object
  448. * @return boolean
  449. * @param integer $categoryid
  450. * @param boolean $recursive Whether to examine category children recursively
  451. */
  452. function question_category_isused($categoryid, $recursive = false) {
  453. global $DB;
  454. //Look at each question in the category
  455. if ($questions = $DB->get_records('question', array('category'=>$categoryid), '', 'id,qtype')) {
  456. foreach ($questions as $question) {
  457. if (count(question_list_instances($question->id))) {
  458. return true;
  459. }
  460. }
  461. }
  462. //Look under child categories recursively
  463. if ($recursive) {
  464. if ($children = $DB->get_records('question_categories', array('parent'=>$categoryid))) {
  465. foreach ($children as $child) {
  466. if (question_category_isused($child->id, $recursive)) {
  467. return true;
  468. }
  469. }
  470. }
  471. }
  472. return false;
  473. }
  474. /**
  475. * Deletes all data associated to an attempt from the database
  476. *
  477. * @global object
  478. * @global object
  479. * @param integer $attemptid The id of the attempt being deleted
  480. */
  481. function delete_attempt($attemptid) {
  482. global $QTYPES, $DB;
  483. $states = $DB->get_records('question_states', array('attempt'=>$attemptid));
  484. if ($states) {
  485. $stateslist = implode(',', array_keys($states));
  486. // delete question-type specific data
  487. foreach ($QTYPES as $qtype) {
  488. $qtype->delete_states($stateslist);
  489. }
  490. }
  491. // delete entries from all other question tables
  492. // It is important that this is done only after calling the questiontype functions
  493. $DB->delete_records("question_states", array("attempt"=>$attemptid));
  494. $DB->delete_records("question_sessions", array("attemptid"=>$attemptid));
  495. $DB->delete_records("question_attempts", array("id"=>$attemptid));
  496. }
  497. /**
  498. * Deletes question and all associated data from the database
  499. *
  500. * It will not delete a question if it is used by an activity module
  501. *
  502. * @global object
  503. * @global object
  504. * @param object $question The question being deleted
  505. */
  506. function delete_question($questionid) {
  507. global $QTYPES, $DB;
  508. $question = $DB->get_record_sql('
  509. SELECT q.*, qc.contextid
  510. FROM {question} q
  511. JOIN {question_categories} qc ON qc.id = q.category
  512. WHERE q.id = ?', array($questionid));
  513. if (!$question) {
  514. // In some situations, for example if this was a child of a
  515. // Cloze question that was previously deleted, the question may already
  516. // have gone. In this case, just do nothing.
  517. return;
  518. }
  519. // Do not delete a question if it is used by an activity module
  520. if (count(question_list_instances($questionid))) {
  521. return;
  522. }
  523. // delete questiontype-specific data
  524. question_require_capability_on($question, 'edit');
  525. if (isset($QTYPES[$question->qtype])) {
  526. $QTYPES[$question->qtype]->delete_question($questionid, $question->contextid);
  527. }
  528. if ($states = $DB->get_records('question_states', array('question'=>$questionid))) {
  529. $stateslist = implode(',', array_keys($states));
  530. // delete questiontype-specific data
  531. foreach ($QTYPES as $qtype) {
  532. $qtype->delete_states($stateslist);
  533. }
  534. }
  535. // Delete entries from all other question tables
  536. // It is important that this is done only after calling the questiontype functions
  537. $DB->delete_records('question_answers', array('question' => $questionid));
  538. $DB->delete_records('question_states', array('question' => $questionid));
  539. $DB->delete_records('question_sessions', array('questionid' => $questionid));
  540. // Now recursively delete all child questions
  541. if ($children = $DB->get_records('question', array('parent' => $questionid), '', 'id,qtype')) {
  542. foreach ($children as $child) {
  543. if ($child->id != $questionid) {
  544. delete_question($child->id);
  545. }
  546. }
  547. }
  548. // Finally delete the question record itself
  549. $DB->delete_records('question', array('id'=>$questionid));
  550. }
  551. /**
  552. * All question categories and their questions are deleted for this course.
  553. *
  554. * @global object
  555. * @param object $mod an object representing the activity
  556. * @param boolean $feedback to specify if the process must output a summary of its work
  557. * @return boolean
  558. */
  559. function question_delete_course($course, $feedback=true) {
  560. global $DB, $OUTPUT;
  561. //To store feedback to be showed at the end of the process
  562. $feedbackdata = array();
  563. //Cache some strings
  564. $strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
  565. $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  566. $categoriescourse = $DB->get_records('question_categories', array('contextid'=>$coursecontext->id), 'parent', 'id, parent, name, contextid');
  567. if ($categoriescourse) {
  568. //Sort categories following their tree (parent-child) relationships
  569. //this will make the feedback more readable
  570. $categoriescourse = sort_categories_by_tree($categoriescourse);
  571. foreach ($categoriescourse as $category) {
  572. //Delete it completely (questions and category itself)
  573. //deleting questions
  574. if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
  575. foreach ($questions as $question) {
  576. delete_question($question->id);
  577. }
  578. $DB->delete_records("question", array("category"=>$category->id));
  579. }
  580. //delete the category
  581. $DB->delete_records('question_categories', array('id'=>$category->id));
  582. //Fill feedback
  583. $feedbackdata[] = array($category->name, $strcatdeleted);
  584. }
  585. //Inform about changes performed if feedback is enabled
  586. if ($feedback) {
  587. $table = new html_table();
  588. $table->head = array(get_string('category','quiz'), get_string('action'));
  589. $table->data = $feedbackdata;
  590. echo html_writer::table($table);
  591. }
  592. }
  593. return true;
  594. }
  595. /**
  596. * Category is about to be deleted,
  597. * 1/ All question categories and their questions are deleted for this course category.
  598. * 2/ All questions are moved to new category
  599. *
  600. * @global object
  601. * @param object $category course category object
  602. * @param object $newcategory empty means everything deleted, otherwise id of category where content moved
  603. * @param boolean $feedback to specify if the process must output a summary of its work
  604. * @return boolean
  605. */
  606. function question_delete_course_category($category, $newcategory, $feedback=true) {
  607. global $DB, $OUTPUT;
  608. $context = get_context_instance(CONTEXT_COURSECAT, $category->id);
  609. if (empty($newcategory)) {
  610. $feedbackdata = array(); // To store feedback to be showed at the end of the process
  611. $rescueqcategory = null; // See the code around the call to question_save_from_deletion.
  612. $strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
  613. // Loop over question categories.
  614. if ($categories = $DB->get_records('question_categories', array('contextid'=>$context->id), 'parent', 'id, parent, name')) {
  615. foreach ($categories as $category) {
  616. // Deal with any questions in the category.
  617. if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
  618. // Try to delete each question.
  619. foreach ($questions as $question) {
  620. delete_question($question->id);
  621. }
  622. // Check to see if there were any questions that were kept because they are
  623. // still in use somehow, even though quizzes in courses in this category will
  624. // already have been deteted. This could happen, for example, if questions are
  625. // added to a course, and then that course is moved to another category (MDL-14802).
  626. $questionids = $DB->get_records_menu('question', array('category'=>$category->id), '', 'id,1');
  627. if (!empty($questionids)) {
  628. if (!$rescueqcategory = question_save_from_deletion(array_keys($questionids),
  629. get_parent_contextid($context), print_context_name($context), $rescueqcategory)) {
  630. return false;
  631. }
  632. $feedbackdata[] = array($category->name, get_string('questionsmovedto', 'question', $rescueqcategory->name));
  633. }
  634. }
  635. // Now delete the category.
  636. if (!$DB->delete_records('question_categories', array('id'=>$category->id))) {
  637. return false;
  638. }
  639. $feedbackdata[] = array($category->name, $strcatdeleted);
  640. } // End loop over categories.
  641. }
  642. // Output feedback if requested.
  643. if ($feedback and $feedbackdata) {
  644. $table = new html_table();
  645. $table->head = array(get_string('questioncategory','question'), get_string('action'));
  646. $table->data = $feedbackdata;
  647. echo html_writer::table($table);
  648. }
  649. } else {
  650. // Move question categories ot the new context.
  651. if (!$newcontext = get_context_instance(CONTEXT_COURSECAT, $newcategory->id)) {
  652. return false;
  653. }
  654. $DB->set_field('question_categories', 'contextid', $newcontext->id, array('contextid'=>$context->id));
  655. if ($feedback) {
  656. $a = new stdClass;
  657. $a->oldplace = print_context_name($context);
  658. $a->newplace = print_context_name($newcontext);
  659. echo $OUTPUT->notification(get_string('movedquestionsandcategories', 'question', $a), 'notifysuccess');
  660. }
  661. }
  662. return true;
  663. }
  664. /**
  665. * Enter description here...
  666. *
  667. * @global object
  668. * @param string $questionids list of questionids
  669. * @param object $newcontext the context to create the saved category in.
  670. * @param string $oldplace a textual description of the think being deleted, e.g. from get_context_name
  671. * @param object $newcategory
  672. * @return mixed false on
  673. */
  674. function question_save_from_deletion($questionids, $newcontextid, $oldplace, $newcategory = null) {
  675. global $DB;
  676. // Make a category in the parent context to move the questions to.
  677. if (is_null($newcategory)) {
  678. $newcategory = new stdClass();
  679. $newcategory->parent = 0;
  680. $newcategory->contextid = $newcontextid;
  681. $newcategory->name = get_string('questionsrescuedfrom', 'question', $oldplace);
  682. $newcategory->info = get_string('questionsrescuedfrominfo', 'question', $oldplace);
  683. $newcategory->sortorder = 999;
  684. $newcategory->stamp = make_unique_id_code();
  685. $newcategory->id = $DB->insert_record('question_categories', $newcategory);
  686. }
  687. // Move any remaining questions to the 'saved' category.
  688. if (!question_move_questions_to_category($questionids, $newcategory->id)) {
  689. return false;
  690. }
  691. return $newcategory;
  692. }
  693. /**
  694. * All question categories and their questions are deleted for this activity.
  695. *
  696. * @global object
  697. * @param object $cm the course module object representing the activity
  698. * @param boolean $feedback to specify if the process must output a summary of its work
  699. * @return boolean
  700. */
  701. function question_delete_activity($cm, $feedback=true) {
  702. global $DB, $OUTPUT;
  703. //To store feedback to be showed at the end of the process
  704. $feedbackdata = array();
  705. //Cache some strings
  706. $strcatdeleted = get_string('unusedcategorydeleted', 'quiz');
  707. $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
  708. if ($categoriesmods = $DB->get_records('question_categories', array('contextid'=>$modcontext->id), 'parent', 'id, parent, name, contextid')){
  709. //Sort categories following their tree (parent-child) relationships
  710. //this will make the feedback more readable
  711. $categoriesmods = sort_categories_by_tree($categoriesmods);
  712. foreach ($categoriesmods as $category) {
  713. //Delete it completely (questions and category itself)
  714. //deleting questions
  715. if ($questions = $DB->get_records('question', array('category' => $category->id), '', 'id,qtype')) {
  716. foreach ($questions as $question) {
  717. delete_question($question->id);
  718. }
  719. $DB->delete_records("question", array("category"=>$category->id));
  720. }
  721. //delete the category
  722. $DB->delete_records('question_categories', array('id'=>$category->id));
  723. //Fill feedback
  724. $feedbackdata[] = array($category->name, $strcatdeleted);
  725. }
  726. //Inform about changes performed if feedback is enabled
  727. if ($feedback) {
  728. $table = new html_table();
  729. $table->head = array(get_string('category','quiz'), get_string('action'));
  730. $table->data = $feedbackdata;
  731. echo html_writer::table($table);
  732. }
  733. }
  734. return true;
  735. }
  736. /**
  737. * This function should be considered private to the question bank, it is called from
  738. * question/editlib.php question/contextmoveq.php and a few similar places to to the work of
  739. * acutally moving questions and associated data. However, callers of this function also have to
  740. * do other work, which is why you should not call this method directly from outside the questionbank.
  741. *
  742. * @global object
  743. * @param string $questionids a comma-separated list of question ids.
  744. * @param integer $newcategoryid the id of the category to move to.
  745. */
  746. function question_move_questions_to_category($questionids, $newcategoryid) {
  747. global $DB, $QTYPES;
  748. $newcontextid = $DB->get_field('question_categories', 'contextid',
  749. array('id' => $newcategoryid));
  750. list($questionidcondition, $params) = $DB->get_in_or_equal($questionids);
  751. $questions = $DB->get_records_sql("
  752. SELECT q.id, q.qtype, qc.contextid
  753. FROM {question} q
  754. JOIN {question_categories} qc ON q.category = qc.id
  755. WHERE q.id $questionidcondition", $params);
  756. foreach ($questions as $question) {
  757. if ($newcontextid != $question->contextid) {
  758. $QTYPES[$question->qtype]->move_files($question->id,
  759. $question->contextid, $newcontextid);
  760. }
  761. }
  762. // Move the questions themselves.
  763. $DB->set_field_select('question', 'category', $newcategoryid, "id $questionidcondition", $params);
  764. // Move any subquestions belonging to them.
  765. $DB->set_field_select('question', 'category', $newcategoryid, "parent $questionidcondition", $params);
  766. // TODO Deal with datasets.
  767. return true;
  768. }
  769. /**
  770. * This function helps move a question cateogry to a new context by moving all
  771. * the files belonging to all the questions to the new context.
  772. * Also moves subcategories.
  773. * @param integer $categoryid the id of the category being moved.
  774. * @param integer $oldcontextid the old context id.
  775. * @param integer $newcontextid the new context id.
  776. */
  777. function question_move_category_to_context($categoryid, $oldcontextid, $newcontextid) {
  778. global $DB, $QTYPES;
  779. $questionids = $DB->get_records_menu('question',
  780. array('category' => $categoryid), '', 'id,qtype');
  781. foreach ($questionids as $questionid => $qtype) {
  782. $QTYPES[$qtype]->move_files($questionid, $oldcontextid, $newcontextid);
  783. }
  784. $subcatids = $DB->get_records_menu('question_categories',
  785. array('parent' => $categoryid), '', 'id,1');
  786. foreach ($subcatids as $subcatid => $notused) {
  787. $DB->set_field('question_categories', 'contextid', $newcontextid, array('id' => $subcatid));
  788. question_move_category_to_context($subcatid, $oldcontextid, $newcontextid);
  789. }
  790. }
  791. /**
  792. * Given a list of ids, load the basic information about a set of questions from the questions table.
  793. * The $join and $extrafields arguments can be used together to pull in extra data.
  794. * See, for example, the usage in mod/quiz/attemptlib.php, and
  795. * read the code below to see how the SQL is assembled. Throws exceptions on error.
  796. *
  797. * @global object
  798. * @global object
  799. * @param array $questionids array of question ids.
  800. * @param string $extrafields extra SQL code to be added to the query.
  801. * @param string $join extra SQL code to be added to the query.
  802. * @param array $extraparams values for any placeholders in $join.
  803. * You are strongly recommended to use named placeholder.
  804. *
  805. * @return array partially complete question objects. You need to call get_question_options
  806. * on them before they can be properly used.
  807. */
  808. function question_preload_questions($questionids, $extrafields = '', $join = '', $extraparams = array()) {
  809. global $CFG, $DB;
  810. if (empty($questionids)) {
  811. return array();
  812. }
  813. if ($join) {
  814. $join = ' JOIN '.$join;
  815. }
  816. if ($extrafields) {
  817. $extrafields = ', ' . $extrafields;
  818. }
  819. list($questionidcondition, $params) = $DB->get_in_or_equal(
  820. $questionids, SQL_PARAMS_NAMED, 'qid0000');
  821. $sql = 'SELECT q.*' . $extrafields . ' FROM {question} q' . $join .
  822. ' WHERE q.id ' . $questionidcondition;
  823. // Load the questions
  824. if (!$questions = $DB->get_records_sql($sql, $extraparams + $params)) {
  825. return 'Could not load questions.';
  826. }
  827. foreach ($questions as $question) {
  828. $question->_partiallyloaded = true;
  829. }
  830. // Note, a possible optimisation here would be to not load the TEXT fields
  831. // (that is, questiontext and generalfeedback) here, and instead load them in
  832. // question_load_questions. That would add one DB query, but reduce the amount
  833. // of data transferred most of the time. I am not going to do this optimisation
  834. // until it is shown to be worthwhile.
  835. return $questions;
  836. }
  837. /**
  838. * Load a set of questions, given a list of ids. The $join and $extrafields arguments can be used
  839. * together to pull in extra data. See, for example, the usage in mod/quiz/attempt.php, and
  840. * read the code below to see how the SQL is assembled. Throws exceptions on error.
  841. *
  842. * @param array $questionids array of question ids.
  843. * @param string $extrafields extra SQL code to be added to the query.
  844. * @param string $join extra SQL code to be added to the query.
  845. * @param array $extraparams values for any placeholders in $join.
  846. * You are strongly recommended to use named placeholder.
  847. *
  848. * @return array question objects.
  849. */
  850. function question_load_questions($questionids, $extrafields = '', $join = '') {
  851. $questions = question_preload_questions($questionids, $extrafields, $join);
  852. // Load the question type specific information
  853. if (!get_question_options($questions)) {
  854. return 'Could not load the question options';
  855. }
  856. return $questions;
  857. }
  858. /**
  859. * Private function to factor common code out of get_question_options().
  860. *
  861. * @global object
  862. * @global object
  863. * @param object $question the question to tidy.
  864. * @param boolean $loadtags load the question tags from the tags table. Optional, default false.
  865. * @return boolean true if successful, else false.
  866. */
  867. function _tidy_question(&$question, $loadtags = false) {
  868. global $CFG, $QTYPES;
  869. if (!array_key_exists($question->qtype, $QTYPES)) {
  870. $question->qtype = 'missingtype';
  871. $question->questiontext = '<p>' . get_string('warningmissingtype', 'quiz') . '</p>' . $question->questiontext;
  872. }
  873. $question->name_prefix = question_make_name_prefix($question->id);
  874. if ($success = $QTYPES[$question->qtype]->get_question_options($question)) {
  875. if (isset($question->_partiallyloaded)) {
  876. unset($question->_partiallyloaded);
  877. }
  878. }
  879. if ($loadtags && !empty($CFG->usetags)) {
  880. require_once($CFG->dirroot . '/tag/lib.php');
  881. $question->tags = tag_get_tags_array('question', $question->id);
  882. }
  883. return $success;
  884. }
  885. /**
  886. * Updates the question objects with question type specific
  887. * information by calling {@link get_question_options()}
  888. *
  889. * Can be called either with an array of question objects or with a single
  890. * question object.
  891. *
  892. * @param mixed $questions Either an array of question objects to be updated
  893. * or just a single question object
  894. * @param boolean $loadtags load the question tags from the tags table. Optional, default false.
  895. * @return bool Indicates success or failure.
  896. */
  897. function get_question_options(&$questions, $loadtags = false) {
  898. if (is_array($questions)) { // deal with an array of questions
  899. foreach ($questions as $i => $notused) {
  900. if (!_tidy_question($questions[$i], $loadtags)) {
  901. return false;
  902. }
  903. }
  904. return true;
  905. } else { // deal with single question
  906. return _tidy_question($questions, $loadtags);
  907. }
  908. }
  909. /**
  910. * Load the basic state information for
  911. *
  912. * @global object
  913. * @param integer $attemptid the attempt id to load the states for.
  914. * @return array an array of state data from the database, you will subsequently
  915. * need to call question_load_states to get fully loaded states that can be
  916. * used by the question types. The states here should be sufficient for
  917. * basic tasks like rendering navigation.
  918. */
  919. function question_preload_states($attemptid) {
  920. global $DB;
  921. // Note, changes here probably also need to be reflected in
  922. // regrade_question_in_attempt and question_load_specific_state.
  923. // The questionid field must be listed first so that it is used as the
  924. // array index in the array returned by $DB->get_records_sql
  925. $statefields = 'n.questionid as question, s.id, s.attempt, ' .
  926. 's.seq_number, s.answer, s.timestamp, s.event, s.grade, s.raw_grade, ' .
  927. 's.penalty, n.sumpenalty, n.manualcomment, n.manualcommentformat, ' .
  928. 'n.flagged, n.id as questionsessionid';
  929. // Load the newest states for the questions
  930. $sql = "SELECT $statefields
  931. FROM {question_states} s, {question_sessions} n
  932. WHERE s.id = n.newest AND n.attemptid = ?";
  933. $states = $DB->get_records_sql($sql, array($attemptid));
  934. if (!$states) {
  935. return false;
  936. }
  937. // Load the newest graded states for the questions
  938. $sql = "SELECT $statefields
  939. FROM {question_states} s, {question_sessions} n
  940. WHERE s.id = n.newgraded AND n.attemptid = ?";
  941. $gradedstates = $DB->get_records_sql($sql, array($attemptid));
  942. // Hook the two together.
  943. foreach ($states as $questionid => $state) {
  944. $states[$questionid]->_partiallyloaded = true;
  945. if ($gradedstates[$questionid]) {
  946. $states[$questionid]->last_graded = $gradedstates[$questionid];
  947. $states[$questionid]->last_graded->_partiallyloaded = true;
  948. }
  949. }
  950. return $states;
  951. }
  952. /**
  953. * Finish loading the question states that were extracted from the database with
  954. * question_preload_states, creating new states for any question where there
  955. * is not a state in the database.
  956. *
  957. * @global object
  958. * @global object
  959. * @param array $questions the questions to load state for.
  960. * @param array $states the partially loaded states this array is updated.
  961. * @param object $cmoptions options from the module we are loading the states for. E.g. $quiz.
  962. * @param object $attempt The attempt for which the question sessions are
  963. * to be restored or created.
  964. * @param mixed either the id of a previous attempt, if this attmpt is
  965. * building on a previous one, or false for a clean attempt.
  966. * @return true or false for success or failure.
  967. */
  968. function question_load_states(&$questions, &$states, $cmoptions, $attempt, $lastattemptid = false) {
  969. global $QTYPES, $DB;
  970. // loop through all questions and set the last_graded states
  971. foreach (array_keys($questions) as $qid) {
  972. if (isset($states[$qid])) {
  973. restore_question_state($questions[$qid], $states[$qid]);
  974. if (isset($states[$qid]->_partiallyloaded)) {
  975. unset($states[$qid]->_partiallyloaded);
  976. }
  977. if (isset($states[$qid]->last_graded)) {
  978. restore_question_state($questions[$qid], $states[$qid]->last_graded);
  979. if (isset($states[$qid]->last_graded->_partiallyloaded)) {
  980. unset($states[$qid]->last_graded->_partiallyloaded);
  981. }
  982. } else {
  983. $states[$qid]->last_graded = clone($states[$qid]);
  984. }
  985. } else {
  986. if ($lastattemptid) {
  987. // If the new attempt is to be based on this previous attempt.
  988. // Find the responses from the previous attempt and save them to the new session
  989. // Load the last graded state for the question. Note, $statefields is
  990. // the same as above, except that we don't want n.manualcomment.
  991. $statefields = 'n.questionid as question, s.id, s.attempt, ' .
  992. 's.seq_number, s.answer, s.timestamp, s.event, s.grade, s.raw_grade, ' .
  993. 's.penalty, n.sumpenalty';
  994. $sql = "SELECT $statefields
  995. FROM {question_states} s, {question_sessions} n
  996. WHERE s.id = n.newest
  997. AND n.attemptid = ?
  998. AND n.questionid = ?";
  999. if (!$laststate = $DB->get_record_sql($sql, array($lastattemptid, $qid))) {
  1000. // Only restore previous responses that have been graded
  1001. continue;
  1002. }
  1003. // Restore the state so that the responses will be restored
  1004. restore_question_state($questions[$qid], $laststate);
  1005. $states[$qid] = clone($laststate);
  1006. unset($states[$qid]->id);
  1007. } else {
  1008. // create a new empty state
  1009. $states[$qid] = new stdClass();
  1010. $states[$qid]->question = $qid;
  1011. $states[$qid]->responses = array('' => '');
  1012. $states[$qid]->raw_grade = 0;
  1013. }
  1014. // now fill/overide initial values
  1015. $states[$qid]->attempt = $attempt->uniqueid;
  1016. $states[$qid]->seq_number = 0;
  1017. $states[$qid]->timestamp = $attempt->timestart;
  1018. $states[$qid]->event = ($attempt->timefinish) ? QUESTION_EVENTCLOSE : QUESTION_EVENTOPEN;
  1019. $states[$qid]->grade = 0;
  1020. $states[$qid]->penalty = 0;
  1021. $states[$qid]->sumpenalty = 0;
  1022. $states[$qid]->manualcomment = '';
  1023. $states[$qid]->manualcommentformat = FORMAT_HTML;
  1024. $states[$qid]->flagged = 0;
  1025. // Prevent further changes to the session from incrementing the
  1026. // sequence number
  1027. $states[$qid]->changed = true;
  1028. if ($lastattemptid) {
  1029. // prepare the previous responses for new processing
  1030. $action = new stdClass;
  1031. $action->responses = $laststate->responses;
  1032. $action->timestamp = $laststate->timestamp;
  1033. $action->event = QUESTION_EVENTSAVE; //emulate save of questions from all pages MDL-7631
  1034. // Process these responses ...
  1035. question_process_responses($questions[$qid], $states[$qid], $action, $cmoptions, $attempt);
  1036. // Fix for Bug #5506: When each attempt is built on the last one,
  1037. // preserve the options from any previous attempt.
  1038. if ( isset($laststate->options) ) {
  1039. $states[$qid]->options = $laststate->options;
  1040. }
  1041. } else {
  1042. // Create the empty question type specific information
  1043. if (!$QTYPES[$questions[$qid]->qtype]->create_session_and_responses(
  1044. $questions[$qid], $states[$qid], $cmoptions, $attempt)) {
  1045. return false;
  1046. }
  1047. }
  1048. $states[$qid]->last_graded = clone($states[$qid]);
  1049. }
  1050. }
  1051. return true;
  1052. }
  1053. /**
  1054. * Loads the most recent state of each question session from the database
  1055. * or create new one.
  1056. *
  1057. * For each question the most recent session state for the current attempt
  1058. * is loaded from the question_states table and the question type specific data and
  1059. * responses are added by calling {@link restore_question_state()} which in turn
  1060. * calls {@link restore_session_and_responses()} for each question.
  1061. * If no states exist for the question instance an empty state object is
  1062. * created representing the start of a session and empty question
  1063. * type specific information and responses are created by calling
  1064. * {@link create_session_and_responses()}.
  1065. *
  1066. * @return array An array of state objects representing the most recent
  1067. * states of the question sessions.
  1068. * @param array $questions The questions for which sessions are to be restored or
  1069. * created.
  1070. * @param object $cmoptions
  1071. * @param object $attempt The attempt for which the question sessions are
  1072. * to be restored or created.
  1073. * @param mixed either the id of a previous attempt, if this attmpt is
  1074. * building on a previous one, or false for a clean attempt.
  1075. */
  1076. function get_question_states(&$questions, $cmoptions, $attempt, $lastattemptid = false) {
  1077. // Preload the states.
  1078. $states = question_preload_states($attempt->uniqueid);
  1079. if (!$states) {
  1080. $states = array();
  1081. }
  1082. // Then finish the job.
  1083. if (!question_load_states($questions, $states, $cmoptions, $attempt, $lastattemptid)) {
  1084. return false;
  1085. }
  1086. return $states;
  1087. }
  1088. /**
  1089. * Load a particular previous state of a question.
  1090. *
  1091. * @global object
  1092. * @param array $question The question to load the state for.
  1093. * @param object $cmoptions Options from the specifica activity module, e.g. $quiz.
  1094. * @param integer $attemptid The question_attempts this is part of.
  1095. * @param integer $stateid The id of a specific state of this question.
  1096. * @return object the requested state. False on error.
  1097. */
  1098. function question_load_specific_state($question, $cmoptions, $attemptid, $stateid) {
  1099. global $DB;
  1100. // Load specified states for the question.
  1101. // sess.sumpenalty is probably wrong here shoul really be a sum of penalties from before the one we are asking for.
  1102. $sql = 'SELECT st.*, sess.sumpenalty, sess.manualcomment, sess.manualcommentformat,
  1103. sess.flagged, sess.id as questionsessionid
  1104. FROM {question_states} st, {question_sessions} sess
  1105. WHERE st.id = ?
  1106. AND st.attempt = ?
  1107. AND sess.attemptid = st.attempt
  1108. AND st.question = ?
  1109. AND sess.questionid = st.question';
  1110. $state = $DB->get_record_sql($sql, array($stateid, $attemptid, $question->id));
  1111. if (!$state) {
  1112. return false;
  1113. }
  1114. restore_question_state($question, $state);
  1115. // Load the most recent graded states for the questions before the specified one.
  1116. $sql = 'SELECT st.*, sess.sumpenalty, sess.manualcomment, sess.manualcommentformat,
  1117. sess.flagged, sess.id as questionsessionid
  1118. FROM {question_states} st, {question_sessions} sess
  1119. WHERE st.seq_number <= ?
  1120. AND st.attempt = ?
  1121. AND sess.attemptid = st.attempt
  1122. AND st.question = ?
  1123. AND sess.questionid = st.question
  1124. AND st.event IN ('.QUESTION_EVENTS_GRADED.') '.
  1125. 'ORDER BY st.seq_number DESC';
  1126. $gradedstates = $DB->get_records_sql($sql, array($state->seq_number, $attemptid, $question->id), 0, 1);
  1127. if (empty($gradedstates)) {
  1128. $state->last_graded = clone($state);
  1129. } else {
  1130. $gradedstate = reset($gradedstates);
  1131. restore_question_state($question, $gradedstate);
  1132. $state->last_graded = $gradedstate;
  1133. }
  1134. return $state;
  1135. }
  1136. /**
  1137. * Creates the run-time fields for the states
  1138. *
  1139. * Extends the state objects for a question by calling
  1140. * {@link restore_session_and_responses()}
  1141. *
  1142. * @global object
  1143. * @param object $question The question for which the state is needed
  1144. * @param object $state The state as loaded from the database
  1145. * @return boolean Represents success or failure
  1146. */
  1147. function restore_question_state(&$question, &$state) {
  1148. global $QTYPES;
  1149. // initialise response to the value in the answer field
  1150. $state->responses = array('' => $state->answer);
  1151. // Set the changed field to false; any code which changes the
  1152. // question session must set this to true and must increment
  1153. // ->seq_number. The save_question_session
  1154. // function will save the new state object to the database if the field is
  1155. // set to true.
  1156. $state->changed = false;
  1157. // Load the question type specific data
  1158. return $QTYPES[$question->qtype]->restore_session_and_responses($question, $state);
  1159. }
  1160. /**
  1161. * Saves the current state of the question…

Large files files are truncated, but you can click here to view the full file