preview.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. * This page displays a preview of a question
  18. *
  19. * The preview uses the option settings from the activity within which the question
  20. * is previewed or the default settings if no activity is specified. The question session
  21. * information is stored in the session as an array of subsequent states rather
  22. * than in the database.
  23. *
  24. * @package moodlecore
  25. * @subpackage questionengine
  26. * @copyright Alex Smith {@link http://maths.york.ac.uk/serving_maths} and
  27. * numerous contributors.
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29. */
  30. require_once(__DIR__ . '/../config.php');
  31. require_once($CFG->libdir . '/questionlib.php');
  32. require_once(__DIR__ . '/previewlib.php');
  33. /**
  34. * The maximum number of variants previewable. If there are more variants than this for a question
  35. * then we only allow the selection of the first x variants.
  36. * @var integer
  37. */
  38. define('QUESTION_PREVIEW_MAX_VARIANTS', 100);
  39. // Get and validate question id.
  40. $id = required_param('id', PARAM_INT);
  41. $question = question_bank::load_question($id);
  42. // Were we given a particular context to run the question in?
  43. // This affects things like filter settings, or forced theme or language.
  44. if ($cmid = optional_param('cmid', 0, PARAM_INT)) {
  45. $cm = get_coursemodule_from_id(false, $cmid);
  46. require_login($cm->course, false, $cm);
  47. $context = context_module::instance($cmid);
  48. } else if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
  49. require_login($courseid);
  50. $context = context_course::instance($courseid);
  51. } else {
  52. require_login();
  53. $category = $DB->get_record('question_categories',
  54. array('id' => $question->category), '*', MUST_EXIST);
  55. $context = context::instance_by_id($category->contextid);
  56. $PAGE->set_context($context);
  57. // Note that in the other cases, require_login will set the correct page context.
  58. }
  59. question_require_capability_on($question, 'use');
  60. $PAGE->set_pagelayout('popup');
  61. // Get and validate display options.
  62. $maxvariant = min($question->get_num_variants(), QUESTION_PREVIEW_MAX_VARIANTS);
  63. $options = new question_preview_options($question);
  64. $options->load_user_defaults();
  65. $options->set_from_request();
  66. $PAGE->set_url(question_preview_url($id, $options->behaviour, $options->maxmark,
  67. $options, $options->variant, $context));
  68. // Get and validate existing preview, or start a new one.
  69. $previewid = optional_param('previewid', 0, PARAM_INT);
  70. if ($previewid) {
  71. try {
  72. $quba = question_engine::load_questions_usage_by_activity($previewid);
  73. } catch (Exception $e) {
  74. // This may not seem like the right error message to display, but
  75. // actually from the user point of view, it makes sense.
  76. print_error('submissionoutofsequencefriendlymessage', 'question',
  77. question_preview_url($question->id, $options->behaviour,
  78. $options->maxmark, $options, $options->variant, $context), null, $e);
  79. }
  80. if ($quba->get_owning_context()->instanceid != $USER->id) {
  81. print_error('notyourpreview', 'question');
  82. }
  83. $slot = $quba->get_first_question_number();
  84. $usedquestion = $quba->get_question($slot);
  85. if ($usedquestion->id != $question->id) {
  86. print_error('questionidmismatch', 'question');
  87. }
  88. $question = $usedquestion;
  89. $options->variant = $quba->get_variant($slot);
  90. } else {
  91. $quba = question_engine::make_questions_usage_by_activity(
  92. 'core_question_preview', context_user::instance($USER->id));
  93. $quba->set_preferred_behaviour($options->behaviour);
  94. $slot = $quba->add_question($question, $options->maxmark);
  95. if ($options->variant) {
  96. $options->variant = min($maxvariant, max(1, $options->variant));
  97. } else {
  98. $options->variant = rand(1, $maxvariant);
  99. }
  100. $quba->start_question($slot, $options->variant);
  101. $transaction = $DB->start_delegated_transaction();
  102. question_engine::save_questions_usage_by_activity($quba);
  103. $transaction->allow_commit();
  104. }
  105. $options->behaviour = $quba->get_preferred_behaviour();
  106. $options->maxmark = $quba->get_question_max_mark($slot);
  107. // Create the settings form, and initialise the fields.
  108. $optionsform = new preview_options_form(question_preview_form_url($question->id, $context, $previewid),
  109. array('quba' => $quba, 'maxvariant' => $maxvariant));
  110. $optionsform->set_data($options);
  111. // Process change of settings, if that was requested.
  112. if ($newoptions = $optionsform->get_submitted_data()) {
  113. // Set user preferences.
  114. $options->save_user_preview_options($newoptions);
  115. if (!isset($newoptions->variant)) {
  116. $newoptions->variant = $options->variant;
  117. }
  118. if (isset($newoptions->saverestart)) {
  119. restart_preview($previewid, $question->id, $newoptions, $context);
  120. }
  121. }
  122. // Prepare a URL that is used in various places.
  123. $actionurl = question_preview_action_url($question->id, $quba->get_id(), $options, $context);
  124. // Process any actions from the buttons at the bottom of the form.
  125. if (data_submitted() && confirm_sesskey()) {
  126. try {
  127. if (optional_param('restart', false, PARAM_BOOL)) {
  128. restart_preview($previewid, $question->id, $options, $context);
  129. } else if (optional_param('fill', null, PARAM_BOOL)) {
  130. $correctresponse = $quba->get_correct_response($slot);
  131. if (!is_null($correctresponse)) {
  132. $quba->process_action($slot, $correctresponse);
  133. $transaction = $DB->start_delegated_transaction();
  134. question_engine::save_questions_usage_by_activity($quba);
  135. $transaction->allow_commit();
  136. }
  137. redirect($actionurl);
  138. } else if (optional_param('finish', null, PARAM_BOOL)) {
  139. $quba->process_all_actions();
  140. $quba->finish_all_questions();
  141. $transaction = $DB->start_delegated_transaction();
  142. question_engine::save_questions_usage_by_activity($quba);
  143. $transaction->allow_commit();
  144. redirect($actionurl);
  145. } else {
  146. $quba->process_all_actions();
  147. $transaction = $DB->start_delegated_transaction();
  148. question_engine::save_questions_usage_by_activity($quba);
  149. $transaction->allow_commit();
  150. $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
  151. if ($scrollpos !== '') {
  152. $actionurl->param('scrollpos', (int) $scrollpos);
  153. }
  154. redirect($actionurl);
  155. }
  156. } catch (question_out_of_sequence_exception $e) {
  157. print_error('submissionoutofsequencefriendlymessage', 'question', $actionurl);
  158. } catch (Exception $e) {
  159. // This sucks, if we display our own custom error message, there is no way
  160. // to display the original stack trace.
  161. $debuginfo = '';
  162. if (!empty($e->debuginfo)) {
  163. $debuginfo = $e->debuginfo;
  164. }
  165. print_error('errorprocessingresponses', 'question', $actionurl,
  166. $e->getMessage(), $debuginfo);
  167. }
  168. }
  169. if ($question->length) {
  170. $displaynumber = '1';
  171. } else {
  172. $displaynumber = 'i';
  173. }
  174. $restartdisabled = array();
  175. $finishdisabled = array();
  176. $filldisabled = array();
  177. if ($quba->get_question_state($slot)->is_finished()) {
  178. $finishdisabled = array('disabled' => 'disabled');
  179. $filldisabled = array('disabled' => 'disabled');
  180. }
  181. // If question type cannot give us a correct response, disable this button.
  182. if (is_null($quba->get_correct_response($slot))) {
  183. $filldisabled = array('disabled' => 'disabled');
  184. }
  185. if (!$previewid) {
  186. $restartdisabled = array('disabled' => 'disabled');
  187. }
  188. // Prepare technical info to be output.
  189. $qa = $quba->get_question_attempt($slot);
  190. $technical = array();
  191. $technical[] = get_string('behaviourbeingused', 'question',
  192. question_engine::get_behaviour_name($qa->get_behaviour_name()));
  193. $technical[] = get_string('technicalinfominfraction', 'question', $qa->get_min_fraction());
  194. $technical[] = get_string('technicalinfomaxfraction', 'question', $qa->get_max_fraction());
  195. $technical[] = get_string('technicalinfovariant', 'question', $qa->get_variant());
  196. $technical[] = get_string('technicalinfoquestionsummary', 'question', s($qa->get_question_summary()));
  197. $technical[] = get_string('technicalinforightsummary', 'question', s($qa->get_right_answer_summary()));
  198. $technical[] = get_string('technicalinforesponsesummary', 'question', s($qa->get_response_summary()));
  199. $technical[] = get_string('technicalinfostate', 'question', '' . $qa->get_state());
  200. // Start output.
  201. $title = get_string('previewquestion', 'question', format_string($question->name));
  202. $headtags = question_engine::initialise_js() . $quba->render_question_head_html($slot);
  203. $PAGE->set_title($title);
  204. $PAGE->set_heading($title);
  205. echo $OUTPUT->header();
  206. // Start the question form.
  207. echo html_writer::start_tag('form', array('method' => 'post', 'action' => $actionurl,
  208. 'enctype' => 'multipart/form-data', 'id' => 'responseform'));
  209. echo html_writer::start_tag('div');
  210. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
  211. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'slots', 'value' => $slot));
  212. echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'scrollpos', 'value' => '', 'id' => 'scrollpos'));
  213. echo html_writer::end_tag('div');
  214. // Output the question.
  215. echo $quba->render_question($slot, $options, $displaynumber);
  216. // Finish the question form.
  217. echo html_writer::start_tag('div', array('id' => 'previewcontrols', 'class' => 'controls'));
  218. echo html_writer::empty_tag('input', $restartdisabled + array('type' => 'submit',
  219. 'name' => 'restart', 'value' => get_string('restart', 'question')));
  220. echo html_writer::empty_tag('input', $finishdisabled + array('type' => 'submit',
  221. 'name' => 'save', 'value' => get_string('save', 'question')));
  222. echo html_writer::empty_tag('input', $filldisabled + array('type' => 'submit',
  223. 'name' => 'fill', 'value' => get_string('fillincorrect', 'question')));
  224. echo html_writer::empty_tag('input', $finishdisabled + array('type' => 'submit',
  225. 'name' => 'finish', 'value' => get_string('submitandfinish', 'question')));
  226. echo html_writer::end_tag('div');
  227. echo html_writer::end_tag('form');
  228. // Output the technical info.
  229. print_collapsible_region_start('', 'techinfo', get_string('technicalinfo', 'question') .
  230. $OUTPUT->help_icon('technicalinfo', 'question'),
  231. 'core_question_preview_techinfo_collapsed', true);
  232. foreach ($technical as $info) {
  233. echo html_writer::tag('p', $info, array('class' => 'notifytiny'));
  234. }
  235. print_collapsible_region_end();
  236. // Display the settings form.
  237. $optionsform->display();
  238. $PAGE->requires->js_module('core_question_engine');
  239. $PAGE->requires->strings_for_js(array(
  240. 'closepreview',
  241. ), 'question');
  242. $PAGE->requires->yui_module('moodle-question-preview', 'M.question.preview.init');
  243. echo $OUTPUT->footer();