and numerous contributors.
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+require_once(__DIR__ . '/../../../config.php');
+require_once($CFG->libdir . '/questionlib.php');
+
+use qbank_previewquestion\form\preview_options_form;
+use qbank_previewquestion\question_preview_options;
+use qbank_previewquestion\helper;
+
+/**
+ * The maximum number of variants previewable. If there are more variants than this for a question
+ * then we only allow the selection of the first x variants.
+ *
+ * @var integer
+ */
+define('QUESTION_PREVIEW_MAX_VARIANTS', 100);
+
+\core_question\local\bank\helper::require_plugin_enabled('qbank_previewquestion');
+
+// Get and validate question id.
+$id = required_param('id', PARAM_INT);
+$returnurl = optional_param('returnurl', null, PARAM_RAW);
+$question = question_bank::load_question($id);
+
+if ($returnurl) {
+ $returnurl = new moodle_url($returnurl);
+}
+
+// Were we given a particular context to run the question in?
+// This affects things like filter settings, or forced theme or language.
+if ($cmid = optional_param('cmid', 0, PARAM_INT)) {
+ $cm = get_coursemodule_from_id(false, $cmid);
+ require_login($cm->course, false, $cm);
+ $context = context_module::instance($cmid);
+
+} else if ($courseid = optional_param('courseid', 0, PARAM_INT)) {
+ require_login($courseid);
+ $context = context_course::instance($courseid);
+
+} else {
+ require_login();
+ $category = $DB->get_record('question_categories', ['id' => $question->category], '*', MUST_EXIST);
+ $context = context::instance_by_id($category->contextid);
+ $PAGE->set_context($context);
+ // Note that in the other cases, require_login will set the correct page context.
+}
+question_require_capability_on($question, 'use');
+$PAGE->set_pagelayout('popup');
+
+// Get and validate display options.
+$maxvariant = min($question->get_num_variants(), QUESTION_PREVIEW_MAX_VARIANTS);
+$options = new question_preview_options($question);
+$options->load_user_defaults();
+$options->set_from_request();
+$PAGE->set_url(helper::question_preview_url($id, $options->behaviour, $options->maxmark,
+ $options, $options->variant, $context));
+
+// Get and validate existing preview, or start a new one.
+$previewid = optional_param('previewid', 0, PARAM_INT);
+
+if ($previewid) {
+ try {
+ $quba = question_engine::load_questions_usage_by_activity($previewid);
+
+ } catch (Exception $e) {
+ // This may not seem like the right error message to display, but
+ // actually from the user point of view, it makes sense.
+ throw new moodle_exception('submissionoutofsequencefriendlymessage', 'question',
+ helper::question_preview_url($question->id, $options->behaviour,
+ $options->maxmark, $options, $options->variant, $context), null, $e);
+ }
+
+ if ($quba->get_owning_context()->instanceid != $USER->id) {
+ throw new moodle_exception('notyourpreview', 'question');
+ }
+
+ $slot = $quba->get_first_question_number();
+ $usedquestion = $quba->get_question($slot, false);
+ if ($usedquestion->id != $question->id) {
+ throw new moodle_exception('questionidmismatch', 'question');
+ }
+ $question = $usedquestion;
+ $options->variant = $quba->get_variant($slot);
+
+} else {
+ $quba = question_engine::make_questions_usage_by_activity(
+ 'core_question_preview', context_user::instance($USER->id));
+ $quba->set_preferred_behaviour($options->behaviour);
+ $slot = $quba->add_question($question, $options->maxmark);
+
+ if ($options->variant) {
+ $options->variant = min($maxvariant, max(1, $options->variant));
+ } else {
+ $options->variant = rand(1, $maxvariant);
+ }
+
+ $quba->start_question($slot, $options->variant);
+
+ $transaction = $DB->start_delegated_transaction();
+ question_engine::save_questions_usage_by_activity($quba);
+ $transaction->allow_commit();
+}
+$options->behaviour = $quba->get_preferred_behaviour();
+$options->maxmark = $quba->get_question_max_mark($slot);
+
+// Create the settings form, and initialise the fields.
+$optionsform = new preview_options_form(helper::question_preview_form_url($question->id, $context, $previewid, $returnurl),
+ ['quba' => $quba, 'maxvariant' => $maxvariant]);
+$optionsform->set_data($options);
+
+// Process change of settings, if that was requested.
+if ($newoptions = $optionsform->get_submitted_data()) {
+ // Set user preferences.
+ $options->save_user_preview_options($newoptions);
+ if (!isset($newoptions->variant)) {
+ $newoptions->variant = $options->variant;
+ }
+ if (isset($newoptions->saverestart)) {
+ helper::restart_preview($previewid, $question->id, $newoptions, $context, $returnurl);
+ }
+}
+
+// Prepare a URL that is used in various places.
+$actionurl = helper::question_preview_action_url($question->id, $quba->get_id(), $options, $context, $returnurl);
+
+// Process any actions from the buttons at the bottom of the form.
+if (data_submitted() && confirm_sesskey()) {
+
+ try {
+
+ if (optional_param('restart', false, PARAM_BOOL)) {
+ helper::restart_preview($previewid, $question->id, $options, $context, $returnurl);
+
+ } else if (optional_param('fill', null, PARAM_BOOL)) {
+ $correctresponse = $quba->get_correct_response($slot);
+ if (!is_null($correctresponse)) {
+ $quba->process_action($slot, $correctresponse);
+
+ $transaction = $DB->start_delegated_transaction();
+ question_engine::save_questions_usage_by_activity($quba);
+ $transaction->allow_commit();
+ }
+ redirect($actionurl);
+
+ } else if (optional_param('finish', null, PARAM_BOOL)) {
+ $quba->process_all_actions();
+ $quba->finish_all_questions();
+
+ $transaction = $DB->start_delegated_transaction();
+ question_engine::save_questions_usage_by_activity($quba);
+ $transaction->allow_commit();
+ redirect($actionurl);
+
+ } else {
+ $quba->process_all_actions();
+
+ $transaction = $DB->start_delegated_transaction();
+ question_engine::save_questions_usage_by_activity($quba);
+ $transaction->allow_commit();
+
+ $scrollpos = optional_param('scrollpos', '', PARAM_RAW);
+ if ($scrollpos !== '') {
+ $actionurl->param('scrollpos', (int) $scrollpos);
+ }
+ redirect($actionurl);
+ }
+
+ } catch (question_out_of_sequence_exception $e) {
+ throw new moodle_exception('submissionoutofsequencefriendlymessage', 'question', $actionurl);
+
+ } catch (Exception $e) {
+ // This sucks, if we display our own custom error message, there is no way
+ // to display the original stack trace.
+ $debuginfo = '';
+ if (!empty($e->debuginfo)) {
+ $debuginfo = $e->debuginfo;
+ }
+ throw new moodle_exception('errorprocessingresponses', 'question', $actionurl,
+ $e->getMessage(), $debuginfo);
+ }
+}
+
+if ($question->length) {
+ $displaynumber = '1';
+} else {
+ $displaynumber = 'i';
+}
+$restartdisabled = [];
+$finishdisabled = [];
+$filldisabled = [];
+if ($quba->get_question_state($slot)->is_finished()) {
+ $finishdisabled = ['disabled' => 'disabled'];
+ $filldisabled = ['disabled' => 'disabled'];
+}
+// If question type cannot give us a correct response, disable this button.
+if (is_null($quba->get_correct_response($slot))) {
+ $filldisabled = ['disabled' => 'disabled'];
+}
+if (!$previewid) {
+ $restartdisabled = ['disabled' => 'disabled'];
+}
+
+// Prepare technical info to be output.
+$qa = $quba->get_question_attempt($slot);
+$technical = [];
+$technical[] = get_string('behaviourbeingused', 'question',
+ question_engine::get_behaviour_name($qa->get_behaviour_name()));
+$technical[] = get_string('technicalinfominfraction', 'question', $qa->get_min_fraction());
+$technical[] = get_string('technicalinfomaxfraction', 'question', $qa->get_max_fraction());
+$technical[] = get_string('technicalinfovariant', 'question', $qa->get_variant());
+$technical[] = get_string('technicalinfoquestionsummary', 'question', s($qa->get_question_summary()));
+$technical[] = get_string('technicalinforightsummary', 'question', s($qa->get_right_answer_summary()));
+$technical[] = get_string('technicalinforesponsesummary', 'question', s($qa->get_response_summary()));
+$technical[] = get_string('technicalinfostate', 'question', '' . $qa->get_state());
+
+// Start output.
+$title = get_string('previewquestion', 'question', format_string($question->name));
+$headtags = question_engine::initialise_js() . $quba->render_question_head_html($slot);
+$PAGE->set_title($title);
+$PAGE->set_heading($title);
+echo $OUTPUT->header();
+
+$previewdata = [];
+$previewdata['actionurl'] = $actionurl;
+$previewdata['session'] = sesskey();
+$previewdata['slot'] = $slot;
+// Output of the question.
+$previewdata['question'] = $quba->render_question($slot, $options, $displaynumber);
+$previewdata['restartdisabled'] = html_writer::attributes($restartdisabled);
+$previewdata['finishdisabled'] = html_writer::attributes($finishdisabled);
+$previewdata['filldisabled'] = html_writer::attributes($filldisabled);
+// Output the technical info.
+$previewdata['techinfo'] = print_collapsible_region_start('', 'techinfo', get_string('technicalinfo', 'question'),
+ 'core_question_preview_techinfo_collapsed', true, true, $OUTPUT->help_icon('technicalinfo', 'question'));
+foreach ($technical as $info) {
+ $previewdata['techinfo'] .= html_writer::tag('p', $info, ['class' => 'notifytiny']);
+}
+$previewdata['techinfo'] .= print_collapsible_region_end(true);
+
+// Output a link to export this single question.
+if (question_has_capability_on($question, 'view')) {
+ if (class_exists('qbank_exporttoxml\\exporttoxml_helper')) {
+ if (\core\plugininfo\qbank::is_plugin_enabled('qbank_exporttoxml')) {
+ $exportfunction = '\\qbank_exporttoxml\\exporttoxml_helper::question_get_export_single_question_url';
+ $previewdata['exporttoxml'] = html_writer::link($exportfunction($question),
+ get_string('exportonequestion', 'question'));
+ }
+ } else {
+ $exportfunction = 'question_get_export_single_question_url';
+ $previewdata['exporttoxml'] = html_writer::link($exportfunction($question),
+ get_string('exportonequestion', 'question'));
+ }
+}
+
+// Display the settings form.
+$previewdata['options'] = $optionsform->render();
+
+list($comment, $extraelements) = helper::get_preview_extra_elements($question, $COURSE->id);
+
+if (!empty($comment)) {
+ $previewdata['comments'] = $comment;
+}
+
+if (!empty($extraelements)) {
+ $elements = [];
+ foreach ($extraelements as $extraelement) {
+ $element = new stdClass();
+ $element->extrapreviewelements = $extraelement;
+ $elements[] = $element;
+ }
+ $previewdata['extrapreviewelements'] = $elements;
+}
+
+$previewdata['redirect'] = false;
+if (!is_null($returnurl)) {
+ $previewdata['redirect'] = true;
+ $previewdata['redirecturl'] = $returnurl;
+}
+
+echo $PAGE->get_renderer('qbank_previewquestion')->render_preview_page($previewdata);
+
+// Log the preview of this question.
+$event = \core\event\question_viewed::create_from_question_instance($question, $context);
+$event->trigger();
+
+$PAGE->requires->js_call_amd('qbank_previewquestion/preview', 'init', [$previewdata['redirect']]);
+echo $OUTPUT->footer();
diff --git a/question/bank/previewquestion/templates/preview_question.mustache b/question/bank/previewquestion/templates/preview_question.mustache
new file mode 100644
index 00000000000..1f2bdf05de0
--- /dev/null
+++ b/question/bank/previewquestion/templates/preview_question.mustache
@@ -0,0 +1,89 @@
+{{!
+ This file is part of Moodle - http://moodle.org/
+
+ Moodle is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ Moodle is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Moodle. If not, see .
+}}
+{{!
+ @template qbank_previewquestion/preview_question
+
+ The preview page for the question type preview.
+ * actionurl - Url to post to
+ * session - Moodle session
+ * slot - The identifying number of the first question that was added to this usage
+ * question - The html of the actual question from the engine
+ * restartdisabled - The attributes to enable or disable the button, same for finishdisabled and filldisabled
+ * techinfo - Technical information like fraction, state, behaviour etc
+ * exporttoxml - Link to export the question to xml
+ * redirecturl - Url to the base view
+ * comments - Comments api html
+ * extrapreviewelements - Any plugin implementing the callback and sending extra html to view something in the preview page
+
+ Example context (json):
+ {
+ "previewdata": [
+ {
+ "actionurl": "/",
+ "session": "E2PwCfrnzz",
+ "slot": "1",
+ "question": "question html
",
+ "restartdisabled": "disabled='disabled'",
+ "finishdisabled": "disabled='disabled'",
+ "filldisabled": "disabled='disabled'",
+ "techinfo": "Behaviour being used: Deferred feedback
",
+ "redirecturl": "/",
+ "exporttoxml": "Download this question in Moodle XML format",
+ "comments": "html from comments api",
+ "extrapreviewelements": "callback to get html from plugins need to show info in preview
"
+ }
+ ]
+ }
+}}
+
+{{{techinfo}}}
+{{{exporttoxml}}}
+{{#comments}}
+
+{{/comments}}
+{{^comments}}
+ {{{options}}}
+{{/comments}}
+{{#extrapreviewelements}}
+ {{{extrapreviewelements}}}
+{{/extrapreviewelements}}
diff --git a/question/tests/behat/preview_question.feature b/question/bank/previewquestion/tests/behat/preview_question.feature
similarity index 93%
rename from question/tests/behat/preview_question.feature
rename to question/bank/previewquestion/tests/behat/preview_question.feature
index e3630e34a39..9d1c6903c75 100644
--- a/question/tests/behat/preview_question.feature
+++ b/question/bank/previewquestion/tests/behat/preview_question.feature
@@ -1,4 +1,4 @@
-@core @core_question @javascript @_switch_window
+@qbank @qbank_previewquestion @javascript
Feature: A teacher can preview questions in the question bank
In order to ensure the questions are properly created
As a teacher
@@ -11,6 +11,9 @@ Feature: A teacher can preview questions in the question bank
And the following "courses" exist:
| fullname | shortname | format |
| Course 1 | C1 | weeks |
+ And the following "activities" exist:
+ | activity | name | course | idnumber |
+ | quiz | Test quiz | C1 | quiz1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
@@ -21,10 +24,9 @@ Feature: A teacher can preview questions in the question bank
| questioncategory | qtype | name |
| Test questions | numerical | Test question to be previewed |
And I log in as "teacher1"
- And I am on "Course 1" course homepage
+ And I am on the "Test quiz" "quiz activity" page
And I navigate to "Question bank > Questions" in current page administration
When I choose "Preview" action for "Test question to be previewed" in the question bank
- And I switch to "questionpreview" window
Scenario: Question preview shows the question and other information
Then the state of "What is pi to two d.p.?" question is shown as "Not yet answered"
diff --git a/question/bank/previewquestion/tests/behat/preview_question_action.feature b/question/bank/previewquestion/tests/behat/preview_question_action.feature
new file mode 100644
index 00000000000..c720b06a845
--- /dev/null
+++ b/question/bank/previewquestion/tests/behat/preview_question_action.feature
@@ -0,0 +1,50 @@
+@qbank @qbank_previewquestion
+Feature: Use the qbank plugin manager page for previewquestion
+ In order to check the plugin behaviour with enable and disable
+
+ Background:
+ Given the following "courses" exist:
+ | fullname | shortname | category |
+ | Course 1 | C1 | 0 |
+ And the following "activities" exist:
+ | activity | name | course | idnumber |
+ | quiz | Test quiz | C1 | quiz1 |
+ And the following "question categories" exist:
+ | contextlevel | reference | name |
+ | Course | C1 | Test questions |
+ And the following "questions" exist:
+ | questioncategory | qtype | name | questiontext |
+ | Test questions | truefalse | First question | Answer the first question |
+
+ @javascript
+ Scenario: Enable/disable previewquestion column from the base view
+ Given I log in as "admin"
+ When I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
+ And I should see "Preview question"
+ And I click on "Disable" "link" in the "Preview question" "table_row"
+ And I am on the "Test quiz" "quiz activity" page
+ And I navigate to "Question bank > Questions" in current page administration
+ And I click on "#action-menu-toggle-2" "css_element" in the "First question" "table_row"
+ Then I should not see "Preview" in the "region-main" "region"
+ And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
+ And I click on "Enable" "link" in the "Preview question" "table_row"
+ And I am on the "Test quiz" "quiz activity" page
+ And I navigate to "Question bank > Questions" in current page administration
+ And I click on "#action-menu-toggle-2" "css_element" in the "First question" "table_row"
+ And I should see "Preview" in the "region-main" "region"
+
+ Scenario: Enable/disable preview button from question edit form
+ Given I log in as "admin"
+ When I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
+ And I should see "Preview question"
+ And I click on "Disable" "link" in the "Preview question" "table_row"
+ And I am on the "Test quiz" "quiz activity" page
+ And I navigate to "Question bank > Questions" in current page administration
+ And I choose "Edit question" action for "First question" in the question bank
+ Then I should not see "Preview" in the "region-main" "region"
+ And I navigate to "Plugins > Question bank plugins > Manage question bank plugins" in site administration
+ And I click on "Enable" "link" in the "Preview question" "table_row"
+ And I am on the "Test quiz" "quiz activity" page
+ And I navigate to "Question bank > Questions" in current page administration
+ And I choose "Edit question" action for "First question" in the question bank
+ And I should see "Preview" in the "region-main" "region"
diff --git a/question/bank/previewquestion/tests/helper_test.php b/question/bank/previewquestion/tests/helper_test.php
new file mode 100644
index 00000000000..33214dcc110
--- /dev/null
+++ b/question/bank/previewquestion/tests/helper_test.php
@@ -0,0 +1,159 @@
+.
+
+namespace qbank_previewquestion;
+
+use core\plugininfo\qbank;
+
+/**
+ * Helper tests for question preview.
+ *
+ * @package qbank_previewquestion
+ * @copyright 2021 Catalyst IT Australia Pty Ltd
+ * @author Safat Shahin
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ * @coversDefaultClass \qbank_previewquestion\helper
+ */
+class helper_test extends \advanced_testcase {
+
+ /**
+ * @var bool|\context|\context_course $context
+ */
+ public $context;
+
+ /**
+ * @var object $questiondata;
+ */
+ public $questiondata;
+
+ /**
+ * @var \question_usage_by_activity $quba
+ */
+ public $quba;
+
+ /**
+ * @var question_preview_options $options
+ */
+ public $options;
+
+ /**
+ * @var \moodle_url $returnurl
+ */
+ public $returnurl;
+
+ /**
+ * Test set up.
+ *
+ * This is executed before running any test in this file.
+ */
+ public function setUp(): void {
+ global $USER;
+ $this->resetAfterTest();
+ $this->setAdminUser();
+ $generator = $this->getDataGenerator();
+ $questiongenerator = $generator->get_plugin_generator('core_question');
+ // Create a course.
+ $course = $generator->create_course();
+ $this->context = \context_course::instance($course->id);
+ // Create a question in the default category.
+ $contexts = new \question_edit_contexts($this->context);
+ $cat = question_make_default_categories($contexts->all());
+ $this->questiondata = $questiongenerator->create_question('numerical', null,
+ ['name' => 'Example question', 'category' => $cat->id]);
+ $this->quba = \question_engine::make_questions_usage_by_activity('core_question_preview', \context_user::instance($USER->id));
+ $this->options = new question_preview_options($this->questiondata);
+ $this->options->load_user_defaults();
+ $this->options->set_from_request();
+ $this->returnurl = new \moodle_url('/question/edit.php');
+ }
+
+ /**
+ * Test the preview action url from the helper class.
+ *
+ * @covers ::question_preview_action_url
+ */
+ public function test_question_preview_action_url() {
+ $actionurl = helper::question_preview_action_url($this->questiondata->id, $this->quba->get_id(), $this->options,
+ $this->context, $this->returnurl);
+ $params = [
+ 'id' => $this->questiondata->id,
+ 'previewid' => $this->quba->get_id(),
+ 'returnurl' => $this->returnurl,
+ 'courseid' => $this->context->instanceid
+ ];
+ $params = array_merge($params, $this->options->get_url_params());
+ $expectedurl = new \moodle_url('/question/bank/previewquestion/preview.php', $params);
+ $this->assertEquals($expectedurl, $actionurl);
+ }
+
+ /**
+ * Test the preview form url from the helper class.
+ *
+ * @covers ::question_preview_form_url
+ */
+ public function test_question_preview_form_url() {
+ $formurl = helper::question_preview_form_url($this->questiondata->id, $this->context, $this->quba->get_id(), $this->returnurl);
+ $params = [
+ 'id' => $this->questiondata->id,
+ 'previewid' => $this->quba->get_id(),
+ 'returnurl' => $this->returnurl,
+ 'courseid' => $this->context->instanceid
+ ];
+ $expectedurl = new \moodle_url('/question/bank/previewquestion/preview.php', $params);
+ $this->assertEquals($expectedurl, $formurl);
+ }
+
+ /**
+ * Test the preview url from the helper class.
+ *
+ * @covers ::question_preview_url
+ */
+ public function test_question_preview_url() {
+ $previewurl = helper::question_preview_url($this->questiondata->id, $this->options->behaviour, $this->options->maxmark,
+ $this->options, $this->options->variant, $this->context);
+ $params = [
+ 'id' => $this->questiondata->id,
+ 'behaviour' => $this->options->behaviour,
+ 'maxmark' => $this->options->maxmark,
+ 'courseid' => $this->context->instanceid
+ ];
+ // Extra params for options.
+ $params['correctness'] = $this->options->correctness;
+ $params['marks'] = $this->options->marks;
+ $params['markdp'] = $this->options->markdp;
+ $params['feedback'] = (bool) $this->options->feedback;
+ $params['generalfeedback'] = (bool) $this->options->generalfeedback;
+ $params['rightanswer'] = (bool) $this->options->rightanswer;
+ $params['history'] = (bool) $this->options->history;
+ $expectedurl = new \moodle_url('/question/bank/previewquestion/preview.php', $params);
+ $this->assertEquals($expectedurl, $previewurl);
+ }
+
+ /**
+ * Test the preview comment callback if available.
+ *
+ * @covers ::get_preview_extra_elements
+ */
+ public function test_get_preview_extra_elements() {
+ $question = \question_bank::load_question($this->questiondata->id);
+ list($comment, $extraelements) = helper::get_preview_extra_elements($question, $this->context->instanceid);
+ if (qbank::is_plugin_enabled('qbank_comment')) {
+ $this->assertEquals("
s go to nothing, not a space. And I drag "OU" to "345,230" in the drag and drop markers question And I drag "Railway station" to "262,197" in the drag and drop markers question @@ -40,28 +35,21 @@ Feature: Preview a drag-drop marker question And I press "Submit and finish" Then the state of "Please place the markers on the map of Milton Keynes" question is shown as "Correct" And I should see "Mark 1.00 out of 1.00" - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a question using the keyboard. When I choose "Preview" action for "Drag markers" in the question bank - And I switch to "questionpreview" window - # Increase window size and wait 2 seconds to ensure elements are placed properly by js. - # Keep window large else drag will scroll the window to find element. - And I change window size to "medium" - And I wait "2" seconds And I type "up" "88" times on marker "Railway station" in the drag and drop markers question And I type "right" "26" times on marker "Railway station" in the drag and drop markers question And I press "Submit and finish" Then the state of "Please place the markers on the map of Milton Keynes" question is shown as "Partially correct" And I should see "Mark 0.25 out of 1.00" - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a question in responsive mode. When I choose "Preview" action for "Drag markers" in the question bank - And I switch to "questionpreview" window - And I change window size to "large" And I drag "OU" to "345,230" in the drag and drop markers question And I drag "Railway station" to "262,197" in the drag and drop markers question And I drag "Railway station" to "334,319" in the drag and drop markers question diff --git a/question/type/ddwtos/tests/behat/preview.feature b/question/type/ddwtos/tests/behat/preview.feature index 3f2bb01bbe4..46cd2ef6a02 100644 --- a/question/type/ddwtos/tests/behat/preview.feature +++ b/question/type/ddwtos/tests/behat/preview.feature @@ -28,42 +28,35 @@ Feature: Preview a drag-drop into text question @javascript @_bug_phantomjs Scenario: Preview a question using the mouse. When I choose "Preview" action for "Drag to text" in the question bank - And I switch to "questionpreview" window - # Increase window size and wait 2 seconds to ensure elements are placed properly by js. - # Keep window large else drag will scroll the window to find element. - And I change window size to "medium" - And I wait "2" seconds And I drag "quick" to space "1" in the drag and drop into text question And I drag "fox" to space "2" in the drag and drop into text question And I drag "assiduous" to space "3" in the drag and drop into text question And I press "Submit and finish" Then the state of "The" question is shown as "Partially correct" And I should see "Mark 0.67 out of 1.00" - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a question using the keyboard & submit incomplete. When I choose "Preview" action for "Drag to text" in the question bank - And I switch to "questionpreview" window And I type " " into space "1" in the drag and drop onto image question And I type " " into space "2" in the drag and drop onto image question And I type " " into space "3" in the drag and drop onto image question And I press "Save" Then the state of "The" question is shown as "Incomplete answer" And I should see "Please put an answer in each box." - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a question using the keyboard. When I choose "Preview" action for "Drag to text" in the question bank - And I switch to "questionpreview" window And I type " " into space "1" in the drag and drop onto image question And I type " " into space "2" in the drag and drop onto image question And I type " " into space "3" in the drag and drop onto image question And I press "Submit and finish" Then the state of "The" question is shown as "Incorrect" And I should see "Mark 0.00 out of 1.00" - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a question that uses strange group numbers using the keyboard. @@ -72,24 +65,19 @@ Feature: Preview a drag-drop into text question | Test questions | ddwtos | Funny groups | oddgroups | And I reload the page When I choose "Preview" action for "Funny groups" in the question bank - And I switch to "questionpreview" window And I type " " into space "1" in the drag and drop onto image question And I type " " into space "2" in the drag and drop onto image question And I type " " into space "3" in the drag and drop onto image question And I press "Submit and finish" Then the state of "The" question is shown as "Correct" And I should see "Mark 1.00 out of 1.00" - And I switch to the main window + And I press "Close preview" @javascript Scenario: Preview a infinite question. When I choose "Preview" action for "Drag to text infinite" in the question bank - And I switch to "questionpreview" window - # Increase window size. - # Keep window large else drag will scroll the window to find element. - And I change window size to "medium" And I press "Fill in correct responses" Then I should see "Option1" in the home area of drag and drop into text question And I should see "Option2" in the home area of drag and drop into text question And I should see "Option3" in the home area of drag and drop into text question - And I switch to the main window + And I press "Close preview" diff --git a/question/type/description/tests/behat/preview.feature b/question/type/description/tests/behat/preview.feature index 13b4660369c..e4698a1c10b 100644 --- a/question/type/description/tests/behat/preview.feature +++ b/question/type/description/tests/behat/preview.feature @@ -27,8 +27,7 @@ Feature: Preview a Description question @javascript @_switch_window Scenario: Preview a Description question and submit a correct response. When I choose "Preview" action for "description-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I should see "Here is some information about the questions you are about to attempt." - And I switch to the main window + And I press "Close preview" diff --git a/question/type/edit_question_form.php b/question/type/edit_question_form.php index 384e689128b..c4d2969dfa7 100644 --- a/question/type/edit_question_form.php +++ b/question/type/edit_question_form.php @@ -256,17 +256,11 @@ abstract class question_edit_form extends question_wizard_form { $buttonarray[] = $mform->createElement('submit', 'updatebutton', get_string('savechangesandcontinueediting', 'question')); if ($this->can_preview()) { - // Todo MDL-72004 changes for class renaming and default sort. - if (class_exists('qbank_previewquestion\\preview_action_column')) { - if (\core\plugininfo\qbank::is_plugin_enabled('qbank_previewquestion')) { - $previewlink = $PAGE->get_renderer('qbank_previewquestion')->question_preview_link( - $this->question->id, $this->context, true); - } - } else { - $previewlink = $PAGE->get_renderer('core_question')->question_preview_link( + if (\core\plugininfo\qbank::is_plugin_enabled('qbank_previewquestion')) { + $previewlink = $PAGE->get_renderer('qbank_previewquestion')->question_preview_link( $this->question->id, $this->context, true); + $buttonarray[] = $mform->createElement('static', 'previewlink', '', $previewlink); } - $buttonarray[] = $mform->createElement('static', 'previewlink', '', $previewlink); } $mform->addGroup($buttonarray, 'updatebuttonar', '', array(' '), false); diff --git a/question/type/essay/tests/behat/max_file_size.feature b/question/type/essay/tests/behat/max_file_size.feature index 6f2f75b08da..584ec92db2a 100644 --- a/question/type/essay/tests/behat/max_file_size.feature +++ b/question/type/essay/tests/behat/max_file_size.feature @@ -27,15 +27,13 @@ I need to choose the appropriate maxbytes for attachments @javascript @_switch_window Scenario: Preview an Essay question and see the allowed maximum file sizes and number of attachments. When I choose "Preview" action for "essay-1-512KB" in the question bank - And I switch to "questionpreview" window Then I should see "Please write a story about a frog." And I should see "Maximum file size: 512KB, maximum number of files: 1" - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview an Essay question with Course upload limit and see the allowed maximum file size. When I choose "Preview" action for "essay-1-max" in the question bank - And I switch to "questionpreview" window Then I should see "Please write a story about a frog." And I should see "Maximum file size: 1MB, maximum number of files: 1" - And I switch to the main window + And I press "Close preview" diff --git a/question/type/essay/tests/behat/preview.feature b/question/type/essay/tests/behat/preview.feature index 142425c67f5..acd0272ffc2 100644 --- a/question/type/essay/tests/behat/preview.feature +++ b/question/type/essay/tests/behat/preview.feature @@ -29,27 +29,24 @@ Feature: Preview Essay questions @javascript @_switch_window Scenario: Preview an Essay question that uses the HTML editor. When I choose "Preview" action for "essay-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I should see "Please write a story about a frog." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview an Essay question that uses the HTML editor with embedded files. When I choose "Preview" action for "essay-002" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I should see "Please write a story about a frog." And I should see "You can drag and drop files here to add them." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview an Essay question that uses a plain text area. When I choose "Preview" action for "essay-003" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I should see "Please write a story about a frog." - And I switch to the main window + And I press "Close preview" diff --git a/question/type/gapselect/tests/behat/basic_test.feature b/question/type/gapselect/tests/behat/basic_test.feature index b2301ad35f0..6b6a4acacc5 100644 --- a/question/type/gapselect/tests/behat/basic_test.feature +++ b/question/type/gapselect/tests/behat/basic_test.feature @@ -39,7 +39,6 @@ Feature: Test all the basic functionality of this question type # Preview it. When I choose "Preview" action for "Select missing words 001" in the question bank - And I switch to "questionpreview" window # Gaps (drop-down menus) do not have labels. ids and names are generated # dynamically and therefore not reliable, i.e. this is an accessibility bug @@ -122,7 +121,7 @@ Feature: Test all the basic functionality of this question type Then I should see "Your answer is incorrect" And I should see "The cat sat on the mat" And I should see "The correct answer is: The [cat] [sat] on the [mat]." - And I switch to the main window + And I press "Close preview" # Backup the course and restore it. When I log out diff --git a/question/type/match/tests/behat/edit.feature b/question/type/match/tests/behat/edit.feature index 3f6e383d12c..0033ab46ae2 100644 --- a/question/type/match/tests/behat/edit.feature +++ b/question/type/match/tests/behat/edit.feature @@ -43,7 +43,6 @@ Feature: Test editing a Matching question And I press "id_submitbutton" Then I should see "Edited Matching name" When I choose "Preview" action for "Edited Matching name" in the question bank - And I switch to "questionpreview" window Then I should see "frog" And I should see "dog" And I should see "newt" diff --git a/question/type/match/tests/behat/preview.feature b/question/type/match/tests/behat/preview.feature index fc2b90c0d1f..9294014f0a4 100644 --- a/question/type/match/tests/behat/preview.feature +++ b/question/type/match/tests/behat/preview.feature @@ -31,7 +31,6 @@ Feature: Preview a Matching question | Shuffle | 0 | And I press "id_submitbutton" When I choose "Preview" action for "matching-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I set the field with xpath "//table[@class='answer']//td[@class='control']//select[contains(@id, '1_sub0')]" to "amphibian" @@ -40,7 +39,7 @@ Feature: Preview a Matching question And I press "Check" Then I should see "Well done!" And I should see "General feedback." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a Matching question and submit a partially correct response. @@ -49,7 +48,6 @@ Feature: Preview a Matching question | Shuffle | 0 | And I press "id_submitbutton" When I choose "Preview" action for "matching-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I set the field with xpath "//table[@class='answer']//td[@class='control']//select[contains(@id, '1_sub0')]" to "amphibian" @@ -58,7 +56,7 @@ Feature: Preview a Matching question And I press "Check" Then I should see "Parts, but only parts, of your response are correct." And I should see "General feedback." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a Matching question and submit an incorrect response. @@ -67,7 +65,6 @@ Feature: Preview a Matching question | Shuffle | 0 | And I press "id_submitbutton" When I choose "Preview" action for "matching-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I set the field with xpath "//table[@class='answer']//td[@class='control']//select[contains(@id, '1_sub0')]" to "mammal" @@ -76,4 +73,4 @@ Feature: Preview a Matching question And I press "Check" Then I should see "That is not right at all." And I should see "General feedback." - And I switch to the main window + And I press "Close preview" diff --git a/question/type/multichoice/tests/behat/preview.feature b/question/type/multichoice/tests/behat/preview.feature index 39e5a1775b5..f0d6d402e74 100644 --- a/question/type/multichoice/tests/behat/preview.feature +++ b/question/type/multichoice/tests/behat/preview.feature @@ -28,7 +28,6 @@ Feature: Preview a Multiple choice question @javascript @_switch_window Scenario: Preview a Multiple choice question and submit a partially correct response. When I choose "Preview" action for "Multi-choice-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "One" "qtype_multichoice > Answer" @@ -38,12 +37,11 @@ Feature: Preview a Multiple choice question And I should see "Two is even" And I should see "Mark 0.50 out of 1.00" And I should see "Parts, but only parts, of your response are correct." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a Multiple choice question and submit a correct response. When I choose "Preview" action for "Multi-choice-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "One" "qtype_multichoice > Answer" @@ -55,12 +53,11 @@ Feature: Preview a Multiple choice question And I should see "Well done!" And I should see "The odd numbers are One and Three." And I should see "The correct answers are: One, Three" - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a Multiple choice question and submit a correct response. When I choose "Preview" action for "Multi-choice-002" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "One" "qtype_multichoice > Answer" @@ -69,16 +66,15 @@ Feature: Preview a Multiple choice question And I should see "Mark 1.00 out of 1.00" And I should see "Well done!" And I should see "The correct answer is: One" - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a multiple choice question (single response) and clear a previous selected option. When I choose "Preview" action for "Multi-choice-002" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "One" "qtype_multichoice > Answer" Then I should see "Clear my choice" And I click on "Clear my choice" "text" And I should not see "Clear my choice" - And I switch to the main window + And I press "Close preview" diff --git a/question/type/numerical/tests/behat/preview.feature b/question/type/numerical/tests/behat/preview.feature index 747b9c360c2..78544fc7a65 100644 --- a/question/type/numerical/tests/behat/preview.feature +++ b/question/type/numerical/tests/behat/preview.feature @@ -31,7 +31,6 @@ Feature: Preview a Numerical question @javascript @_switch_window Scenario: Preview a Numerical question and submit a correct response. When I choose "Preview" action for "Numerical-001" in the question bank - And I switch to "questionpreview" window Then I should see "What is pi to two d.p.?" When I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" @@ -48,4 +47,4 @@ Feature: Preview a Numerical question And I press "Check" Then I should see "Very good." And I should see "Mark 1#00 out of 1#00" - And I switch to the main window + And I press "Close preview" diff --git a/question/type/shortanswer/tests/behat/edit.feature b/question/type/shortanswer/tests/behat/edit.feature index 996eb12003e..ccca1e979fa 100644 --- a/question/type/shortanswer/tests/behat/edit.feature +++ b/question/type/shortanswer/tests/behat/edit.feature @@ -43,7 +43,6 @@ Feature: Test editing a Short answer question And I press "id_submitbutton" Then I should see "Edited shortanswer-001 name" When I choose "Preview" action for "Edited shortanswer-001" in the question bank - And I switch to "questionpreview" window Then I should see "Name an amphibian:" # Set behaviour options And I set the following fields to these values: diff --git a/question/type/shortanswer/tests/behat/preview.feature b/question/type/shortanswer/tests/behat/preview.feature index 2772c38cfc5..b0e7d63ca1e 100644 --- a/question/type/shortanswer/tests/behat/preview.feature +++ b/question/type/shortanswer/tests/behat/preview.feature @@ -27,7 +27,6 @@ Feature: Preview a Short answer question @javascript @_switch_window Scenario: Preview a Short answer question with correct answer When I choose "Preview" action for "shortanswer-001" in the question bank - And I switch to "questionpreview" window Then I should see "Name an amphibian:" # Set behaviour options And I set the following fields to these values: @@ -42,7 +41,6 @@ Feature: Preview a Short answer question @javascript @_switch_window Scenario: Preview a Short answer question with almost correct answer When I choose "Preview" action for "shortanswer-001" in the question bank - And I switch to "questionpreview" window Then I should see "Name an amphibian:" # Set behaviour options And I set the following fields to these values: @@ -57,7 +55,6 @@ Feature: Preview a Short answer question @javascript @_switch_window Scenario: Preview a Short answer question with incorrect answer When I choose "Preview" action for "shortanswer-001" in the question bank - And I switch to "questionpreview" window Then I should see "Name an amphibian:" # Set behaviour options And I set the following fields to these values: diff --git a/question/type/truefalse/tests/behat/preview.feature b/question/type/truefalse/tests/behat/preview.feature index 83baede4749..b0300d6e4b3 100644 --- a/question/type/truefalse/tests/behat/preview.feature +++ b/question/type/truefalse/tests/behat/preview.feature @@ -27,19 +27,17 @@ Feature: Preview a Trtue/False question @javascript @_switch_window Scenario: Preview a True/False question and submit a correct response. When I choose "Preview" action for "true-false-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "True" "radio" And I press "Check" And I should see "This is the right answer." And I should see "The correct answer is 'True'." - And I switch to the main window + And I press "Close preview" @javascript @_switch_window Scenario: Preview a True/False question and submit an incorrect response. When I choose "Preview" action for "true-false-001" in the question bank - And I switch to "questionpreview" window And I set the field "How questions behave" to "Immediate feedback" And I press "Start again with these options" And I click on "False" "radio" @@ -47,4 +45,4 @@ Feature: Preview a Trtue/False question And I should see "This is the wrong answer." And I should see "You should have selected true." And I should see "The correct answer is 'True'." - And I switch to the main window + And I press "Close preview" diff --git a/question/upgrade.txt b/question/upgrade.txt index f2b7fedbf61..56d076b6e17 100644 --- a/question/upgrade.txt +++ b/question/upgrade.txt @@ -6,6 +6,11 @@ This files describes API changes for code that uses the question API. are divided in two different parts, base classes and feature classes. All the base classes are moved classes/local/bank and all the feature classes will be moved to the plugin for that feature. +2) The following methods are deprecated in previewlib and moved to the previewquestion plugin: + restart_preview(), question_preview_form_url(), question_preview_action_url(), + question_preview_question_pluginfile() + The following classes are deprecated in previewlib and moves the previewquestion plugin: + question_preview_options, preview_options_form. === 3.9 == diff --git a/question/yui/build/moodle-question-preview/moodle-question-preview-debug.js b/question/yui/build/moodle-question-preview/moodle-question-preview-debug.js index d338ff09477..28e4a52e1f5 100644 --- a/question/yui/build/moodle-question-preview/moodle-question-preview-debug.js +++ b/question/yui/build/moodle-question-preview/moodle-question-preview-debug.js @@ -23,9 +23,14 @@ YUI.add('moodle-question-preview', function (Y, NAME) { /** * JavaScript required by the question preview pop-up. * + * @deprecated since Moodle 4.0 + * @todo Final deprecation on Moodle 4.4 MDL-72438 * @module moodle-question-preview */ +Y.log("The moodle-question-preview module has been deprecated. " + + "Please use moodle-qbank_previewquestion-preview instead.", 'moodle-core-notification', 'warn'); + M.question = M.question || {}; M.question.preview = M.question.preview || {}; diff --git a/question/yui/build/moodle-question-preview/moodle-question-preview.js b/question/yui/build/moodle-question-preview/moodle-question-preview.js index d338ff09477..797d83a9c35 100644 --- a/question/yui/build/moodle-question-preview/moodle-question-preview.js +++ b/question/yui/build/moodle-question-preview/moodle-question-preview.js @@ -23,9 +23,12 @@ YUI.add('moodle-question-preview', function (Y, NAME) { /** * JavaScript required by the question preview pop-up. * + * @deprecated since Moodle 4.0 + * @todo Final deprecation on Moodle 4.4 MDL-72438 * @module moodle-question-preview */ + M.question = M.question || {}; M.question.preview = M.question.preview || {}; diff --git a/question/yui/src/preview/js/preview.js b/question/yui/src/preview/js/preview.js index ebfd0f42c56..e1a49be5745 100644 --- a/question/yui/src/preview/js/preview.js +++ b/question/yui/src/preview/js/preview.js @@ -21,9 +21,14 @@ /** * JavaScript required by the question preview pop-up. * + * @deprecated since Moodle 4.0 + * @todo Final deprecation on Moodle 4.4 MDL-72438 * @module moodle-question-preview */ +Y.log("The moodle-question-preview module has been deprecated. " + + "Please use moodle-qbank_previewquestion-preview instead.", 'moodle-core-notification', 'warn'); + M.question = M.question || {}; M.question.preview = M.question.preview || {};