| 1 | 
           efrain | 
           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 | 
           // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           require_once(__DIR__ . '/behat_question_base.php');
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use Behat\Gherkin\Node\TableNode as TableNode;
  | 
        
        
            | 
            | 
           22 | 
           use Behat\Mink\Exception\ExpectationException as ExpectationException;
  | 
        
        
            | 
            | 
           23 | 
           use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           /**
  | 
        
        
            | 
            | 
           26 | 
            * Steps definitions related with the question bank management.
  | 
        
        
            | 
            | 
           27 | 
            *
  | 
        
        
            | 
            | 
           28 | 
            * @package    core_question
  | 
        
        
            | 
            | 
           29 | 
            * @category   test
  | 
        
        
            | 
            | 
           30 | 
            * @copyright  2013 David Monllaó
  | 
        
        
            | 
            | 
           31 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
            | 
            | 
           33 | 
           class behat_core_question extends behat_question_base {
  | 
        
        
            | 
            | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
               /**
  | 
        
        
            | 
            | 
           36 | 
                * Convert page names to URLs for steps like 'When I am on the "[page name]" page'.
  | 
        
        
            | 
            | 
           37 | 
                *
  | 
        
        
            | 
            | 
           38 | 
                * Recognised page names are:
  | 
        
        
            | 
            | 
           39 | 
                * | None so far!      |                                                              |
  | 
        
        
            | 
            | 
           40 | 
                *
  | 
        
        
            | 
            | 
           41 | 
                * @param string $page name of the page, with the component name removed e.g. 'Admin notification'.
  | 
        
        
            | 
            | 
           42 | 
                * @return moodle_url the corresponding URL.
  | 
        
        
            | 
            | 
           43 | 
                * @throws Exception with a meaningful error message if the specified page cannot be found.
  | 
        
        
            | 
            | 
           44 | 
                */
  | 
        
        
            | 
            | 
           45 | 
               protected function resolve_page_url(string $page): moodle_url {
  | 
        
        
            | 
            | 
           46 | 
                   switch (strtolower($page)) {
  | 
        
        
            | 
            | 
           47 | 
                       default:
  | 
        
        
            | 
            | 
           48 | 
                           throw new Exception('Unrecognised core_question page type "' . $page . '."');
  | 
        
        
            | 
            | 
           49 | 
                   }
  | 
        
        
            | 
            | 
           50 | 
               }
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               /**
  | 
        
        
            | 
            | 
           53 | 
                * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  | 
        
        
            | 
            | 
           54 | 
                *
  | 
        
        
            | 
            | 
           55 | 
                * Recognised page names are:
  | 
        
        
           | 1441 | 
           ariadna | 
           56 | 
                * | pagetype               | name meaning               | description                                           |
  | 
        
        
            | 
            | 
           57 | 
                * | course question bank   | Course name                | The default question bank for a course                |
  | 
        
        
            | 
            | 
           58 | 
                * | course question import | Course name                | The import questions screen for a course default bank |
  | 
        
        
            | 
            | 
           59 | 
                * | course question export | Course name                | The export questions screen for a course default bank |
  | 
        
        
            | 
            | 
           60 | 
                * | question bank          | Question bank name         | The question bank module                              |
  | 
        
        
            | 
            | 
           61 | 
                * | question import        | Question bank name         | The import questions screen for a question bank       |
  | 
        
        
            | 
            | 
           62 | 
                * | question export        | Question bank name         | The export questions screen for a question bank       |
  | 
        
        
            | 
            | 
           63 | 
                * | preview                | Question name              | The screen to preview a question                      |
  | 
        
        
            | 
            | 
           64 | 
                * | edit                   | Question name              | The screen to edit a question                         |
  | 
        
        
           | 1 | 
           efrain | 
           65 | 
                *
  | 
        
        
            | 
            | 
           66 | 
                * @param string $type identifies which type of page this is, e.g. 'Preview'.
  | 
        
        
            | 
            | 
           67 | 
                * @param string $identifier identifies the particular page, e.g. 'My question'.
  | 
        
        
            | 
            | 
           68 | 
                * @return moodle_url the corresponding URL.
  | 
        
        
            | 
            | 
           69 | 
                * @throws Exception with a meaningful error message if the specified page cannot be found.
  | 
        
        
            | 
            | 
           70 | 
                */
  | 
        
        
            | 
            | 
           71 | 
               protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  | 
        
        
            | 
            | 
           72 | 
                   switch (strtolower($type)) {
  | 
        
        
            | 
            | 
           73 | 
                       case 'course question bank':
  | 
        
        
           | 11 | 
           efrain | 
           74 | 
                           // The question bank does not handle fields at the edge of the viewport well.
  | 
        
        
            | 
            | 
           75 | 
                           // Increase the size to avoid this.
  | 
        
        
            | 
            | 
           76 | 
                           $this->execute('behat_general::i_change_window_size_to', ['window', 'large']);
  | 
        
        
           | 1441 | 
           ariadna | 
           77 | 
                           $qbank = $this->get_default_bank_for_course_identifier($identifier);
  | 
        
        
           | 11 | 
           efrain | 
           78 | 
                           return new moodle_url('/question/edit.php', [
  | 
        
        
           | 1441 | 
           ariadna | 
           79 | 
                               'cmid' => $qbank->id,
  | 
        
        
           | 11 | 
           efrain | 
           80 | 
                           ]);
  | 
        
        
           | 1 | 
           efrain | 
           81 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           82 | 
                       case 'question bank':
  | 
        
        
            | 
            | 
           83 | 
                           // The question bank does not handle fields at the edge of the viewport well.
  | 
        
        
            | 
            | 
           84 | 
                           // Increase the size to avoid this.
  | 
        
        
            | 
            | 
           85 | 
                           $this->execute('behat_general::i_change_window_size_to', ['window', 'large']);
  | 
        
        
            | 
            | 
           86 | 
                           return new moodle_url('/question/edit.php',
  | 
        
        
            | 
            | 
           87 | 
                               ['cmid' => $this->get_cm_by_activity_name('qbank', $identifier)->id]
  | 
        
        
            | 
            | 
           88 | 
                           );
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
           | 1 | 
           efrain | 
           90 | 
                       case 'course question categories':
  | 
        
        
           | 1441 | 
           ariadna | 
           91 | 
                           $qbank = $this->get_default_bank_for_course_identifier($identifier);
  | 
        
        
           | 1 | 
           efrain | 
           92 | 
                           return new moodle_url('/question/bank/managecategories/category.php',
  | 
        
        
           | 1441 | 
           ariadna | 
           93 | 
                               ['cmid' => $qbank->id]
  | 
        
        
            | 
            | 
           94 | 
                           );
  | 
        
        
           | 1 | 
           efrain | 
           95 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           96 | 
                       case 'question categories':
  | 
        
        
            | 
            | 
           97 | 
                           return new moodle_url('/question/bank/managecategories/category.php',
  | 
        
        
            | 
            | 
           98 | 
                               ['cmid' => $this->get_cm_by_activity_name('qbank', $identifier)->id]
  | 
        
        
            | 
            | 
           99 | 
                           );
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
           | 1 | 
           efrain | 
           101 | 
                       case 'course question import':
  | 
        
        
           | 1441 | 
           ariadna | 
           102 | 
                           $qbank = $this->get_default_bank_for_course_identifier($identifier);
  | 
        
        
           | 1 | 
           efrain | 
           103 | 
                           return new moodle_url('/question/bank/importquestions/import.php',
  | 
        
        
           | 1441 | 
           ariadna | 
           104 | 
                               ['cmid' => $qbank->id]
  | 
        
        
            | 
            | 
           105 | 
                           );
  | 
        
        
           | 1 | 
           efrain | 
           106 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           107 | 
                       case 'question import':
  | 
        
        
            | 
            | 
           108 | 
                           return new moodle_url('/question/bank/importquestions/import.php',
  | 
        
        
            | 
            | 
           109 | 
                               ['cmid' => $this->get_cm_by_activity_name('qbank', $identifier)->id]
  | 
        
        
            | 
            | 
           110 | 
                           );
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
           | 1 | 
           efrain | 
           112 | 
                       case 'course question export':
  | 
        
        
           | 1441 | 
           ariadna | 
           113 | 
                           $qbank = $this->get_default_bank_for_course_identifier($identifier);
  | 
        
        
           | 1 | 
           efrain | 
           114 | 
                           return new moodle_url('/question/bank/exportquestions/export.php',
  | 
        
        
           | 1441 | 
           ariadna | 
           115 | 
                               ['cmid' => $qbank->id]
  | 
        
        
            | 
            | 
           116 | 
                           );
  | 
        
        
           | 1 | 
           efrain | 
           117 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           118 | 
                       case 'question export':
  | 
        
        
            | 
            | 
           119 | 
                           return new moodle_url('/question/bank/exportquestions/export.php',
  | 
        
        
            | 
            | 
           120 | 
                               ['cmid' => $this->get_cm_by_activity_name('qbank', $identifier)->id]
  | 
        
        
            | 
            | 
           121 | 
                           );
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
           | 1 | 
           efrain | 
           123 | 
                       case 'preview':
  | 
        
        
            | 
            | 
           124 | 
                           [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
  | 
        
        
            | 
            | 
           125 | 
                           return new moodle_url('/question/bank/previewquestion/preview.php',
  | 
        
        
            | 
            | 
           126 | 
                                   ['id' => $questionid, $otheridtype => $otherid]);
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
                       case 'edit':
  | 
        
        
            | 
            | 
           129 | 
                           [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
  | 
        
        
            | 
            | 
           130 | 
                           return new moodle_url('/question/bank/editquestion/question.php',
  | 
        
        
            | 
            | 
           131 | 
                                   ['id' => $questionid, $otheridtype => $otherid]);
  | 
        
        
            | 
            | 
           132 | 
              | 
        
        
            | 
            | 
           133 | 
                       default:
  | 
        
        
            | 
            | 
           134 | 
                           throw new Exception('Unrecognised core_question page type "' . $type . '."');
  | 
        
        
            | 
            | 
           135 | 
                   }
  | 
        
        
            | 
            | 
           136 | 
               }
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           139 | 
                * Get the course default question bank, creating it if it doesn't yet exist
  | 
        
        
            | 
            | 
           140 | 
                *
  | 
        
        
            | 
            | 
           141 | 
                * @param string $identifier The name of the course to add a default bank to.
  | 
        
        
            | 
            | 
           142 | 
                * @return cm_info The newly created default question bank.
  | 
        
        
            | 
            | 
           143 | 
                */
  | 
        
        
            | 
            | 
           144 | 
               private function get_default_bank_for_course_identifier(string $identifier): cm_info {
  | 
        
        
            | 
            | 
           145 | 
                   $course = get_course($this->get_course_id($identifier));
  | 
        
        
            | 
            | 
           146 | 
                   return \core_question\local\bank\question_bank_helper::get_default_open_instance_system_type($course, true);
  | 
        
        
            | 
            | 
           147 | 
               }
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           150 | 
                * Find a question, and where it is, from the question name.
  | 
        
        
            | 
            | 
           151 | 
                *
  | 
        
        
            | 
            | 
           152 | 
                * This is a helper used by resolve_page_instance_url.
  | 
        
        
            | 
            | 
           153 | 
                *
  | 
        
        
            | 
            | 
           154 | 
                * @param string $questionname
  | 
        
        
            | 
            | 
           155 | 
                * @return array with three elemnets, int question id, a string 'cmid' or 'courseid',
  | 
        
        
            | 
            | 
           156 | 
                *     and int either cmid or courseid as applicable.
  | 
        
        
            | 
            | 
           157 | 
                */
  | 
        
        
            | 
            | 
           158 | 
               protected function find_question_by_name(string $questionname): array {
  | 
        
        
            | 
            | 
           159 | 
                   global $DB;
  | 
        
        
            | 
            | 
           160 | 
                   $questionid = $DB->get_field('question', 'id', ['name' => $questionname], MUST_EXIST);
  | 
        
        
            | 
            | 
           161 | 
                   $question = question_bank::load_question_data($questionid);
  | 
        
        
            | 
            | 
           162 | 
                   $context = context_helper::instance_by_id($question->contextid);
  | 
        
        
            | 
            | 
           163 | 
              | 
        
        
            | 
            | 
           164 | 
                   if ($context->contextlevel == CONTEXT_MODULE) {
  | 
        
        
            | 
            | 
           165 | 
                       return [$questionid, 'cmid', $context->instanceid];
  | 
        
        
            | 
            | 
           166 | 
                   } else if ($context->contextlevel == CONTEXT_COURSE) {
  | 
        
        
            | 
            | 
           167 | 
                       return [$questionid, 'courseid', $context->instanceid];
  | 
        
        
            | 
            | 
           168 | 
                   } else {
  | 
        
        
            | 
            | 
           169 | 
                       throw new coding_exception('Unsupported context level ' . $context->contextlevel);
  | 
        
        
            | 
            | 
           170 | 
                   }
  | 
        
        
            | 
            | 
           171 | 
               }
  | 
        
        
            | 
            | 
           172 | 
              | 
        
        
            | 
            | 
           173 | 
               /**
  | 
        
        
            | 
            | 
           174 | 
                * Creates a question in the current course questions bank with the provided data.
  | 
        
        
            | 
            | 
           175 | 
                * This step can only be used when creating question types composed by a single form.
  | 
        
        
            | 
            | 
           176 | 
                *
  | 
        
        
            | 
            | 
           177 | 
                * @Given /^I add a "(?P<question_type_name_string>(?:[^"]|\\")*)" question filling the form with:$/
  | 
        
        
            | 
            | 
           178 | 
                * @param string $questiontypename The question type name
  | 
        
        
            | 
            | 
           179 | 
                * @param TableNode $questiondata The data to fill the question type form.
  | 
        
        
            | 
            | 
           180 | 
                */
  | 
        
        
            | 
            | 
           181 | 
               public function i_add_a_question_filling_the_form_with($questiontypename, TableNode $questiondata) {
  | 
        
        
            | 
            | 
           182 | 
                   // Click on create question.
  | 
        
        
            | 
            | 
           183 | 
                   $this->execute('behat_forms::press_button', get_string('createnewquestion', 'question'));
  | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
                   // Add question.
  | 
        
        
            | 
            | 
           186 | 
                   $this->finish_adding_question($questiontypename, $questiondata);
  | 
        
        
            | 
            | 
           187 | 
               }
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
               /**
  | 
        
        
            | 
            | 
           190 | 
                * Checks the state of the specified question.
  | 
        
        
            | 
            | 
           191 | 
                *
  | 
        
        
            | 
            | 
           192 | 
                * @Then /^the state of "(?P<question_description_string>(?:[^"]|\\")*)" question is shown as "(?P<state_string>(?:[^"]|\\")*)"$/
  | 
        
        
            | 
            | 
           193 | 
                * @throws ExpectationException
  | 
        
        
            | 
            | 
           194 | 
                * @throws ElementNotFoundException
  | 
        
        
            | 
            | 
           195 | 
                * @param string $questiondescription
  | 
        
        
            | 
            | 
           196 | 
                * @param string $state
  | 
        
        
            | 
            | 
           197 | 
                */
  | 
        
        
            | 
            | 
           198 | 
               public function the_state_of_question_is_shown_as($questiondescription, $state) {
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
                   // Using xpath literal to avoid quotes problems.
  | 
        
        
            | 
            | 
           201 | 
                   $questiondescriptionliteral = behat_context_helper::escape($questiondescription);
  | 
        
        
            | 
            | 
           202 | 
                   $stateliteral = behat_context_helper::escape($state);
  | 
        
        
            | 
            | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
                   // Split in two checkings to give more feedback in case of exception.
  | 
        
        
            | 
            | 
           205 | 
                   $exception = new ElementNotFoundException($this->getSession(), 'Question "' . $questiondescription . '" ');
  | 
        
        
            | 
            | 
           206 | 
                   $questionxpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' que ')]" .
  | 
        
        
            | 
            | 
           207 | 
                           "[contains(div[@class='content']/div[contains(concat(' ', normalize-space(@class), ' '), ' formulation ')]," .
  | 
        
        
            | 
            | 
           208 | 
                           "{$questiondescriptionliteral})]";
  | 
        
        
            | 
            | 
           209 | 
                   $this->find('xpath', $questionxpath, $exception);
  | 
        
        
            | 
            | 
           210 | 
              | 
        
        
            | 
            | 
           211 | 
                   $exception = new ExpectationException('Question "' . $questiondescription .
  | 
        
        
            | 
            | 
           212 | 
                           '" state is not "' . $state . '"', $this->getSession());
  | 
        
        
            | 
            | 
           213 | 
                   $xpath = $questionxpath . "/div[@class='info']/div[@class='state' and contains(., {$stateliteral})]";
  | 
        
        
            | 
            | 
           214 | 
                   $this->find('xpath', $xpath, $exception);
  | 
        
        
            | 
            | 
           215 | 
               }
  | 
        
        
            | 
            | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
               /**
  | 
        
        
            | 
            | 
           218 | 
                * Activates a particular action on a particular question in the question bank UI.
  | 
        
        
            | 
            | 
           219 | 
                *
  | 
        
        
            | 
            | 
           220 | 
                * @When I choose :action action for :questionname in the question bank
  | 
        
        
            | 
            | 
           221 | 
                * @param string $action the label for the action you want to activate.
  | 
        
        
            | 
            | 
           222 | 
                * @param string $questionname the question name.
  | 
        
        
            | 
            | 
           223 | 
                */
  | 
        
        
            | 
            | 
           224 | 
               public function i_action_the_question($action, $questionname) {
  | 
        
        
            | 
            | 
           225 | 
                   if ($this->running_javascript()) {
  | 
        
        
            | 
            | 
           226 | 
                       // This method isn't allowed unless Javascript is running.
  | 
        
        
            | 
            | 
           227 | 
                       $this->execute('behat_action_menu::i_open_the_action_menu_in', [
  | 
        
        
            | 
            | 
           228 | 
                           $questionname,
  | 
        
        
            | 
            | 
           229 | 
                           'table_row',
  | 
        
        
            | 
            | 
           230 | 
                       ]);
  | 
        
        
            | 
            | 
           231 | 
                       $this->execute('behat_action_menu::i_choose_in_the_open_action_menu', [
  | 
        
        
            | 
            | 
           232 | 
                           $action
  | 
        
        
            | 
            | 
           233 | 
                       ]);
  | 
        
        
            | 
            | 
           234 | 
                   } else {
  | 
        
        
            | 
            | 
           235 | 
                       // This method doesn't open the menu correctly when Javascript is running.
  | 
        
        
            | 
            | 
           236 | 
                       $this->execute('behat_action_menu::i_choose_in_the_named_menu_in_container', [
  | 
        
        
            | 
            | 
           237 | 
                           $action,
  | 
        
        
            | 
            | 
           238 | 
                           get_string('edit', 'core'),
  | 
        
        
            | 
            | 
           239 | 
                           $questionname,
  | 
        
        
            | 
            | 
           240 | 
                           'table_row',
  | 
        
        
            | 
            | 
           241 | 
                       ]);
  | 
        
        
            | 
            | 
           242 | 
                   }
  | 
        
        
            | 
            | 
           243 | 
               }
  | 
        
        
            | 
            | 
           244 | 
              | 
        
        
            | 
            | 
           245 | 
               /**
  | 
        
        
            | 
            | 
           246 | 
                * Checks that action does exist for a question.
  | 
        
        
            | 
            | 
           247 | 
                *
  | 
        
        
            | 
            | 
           248 | 
                * @Then the :action action should exist for the :questionname question in the question bank
  | 
        
        
            | 
            | 
           249 | 
                * @param string $action the label for the action you want to activate.
  | 
        
        
            | 
            | 
           250 | 
                * @param string $questionname the question name.
  | 
        
        
            | 
            | 
           251 | 
                */
  | 
        
        
            | 
            | 
           252 | 
               public function action_exists($action, $questionname) {
  | 
        
        
            | 
            | 
           253 | 
                   $this->execute('behat_action_menu::item_should_exist_in_the', [
  | 
        
        
            | 
            | 
           254 | 
                       $action,
  | 
        
        
            | 
            | 
           255 | 
                       get_string('edit', 'core'),
  | 
        
        
            | 
            | 
           256 | 
                       $questionname,
  | 
        
        
            | 
            | 
           257 | 
                       'table_row',
  | 
        
        
            | 
            | 
           258 | 
                   ]);
  | 
        
        
            | 
            | 
           259 | 
               }
  | 
        
        
            | 
            | 
           260 | 
              | 
        
        
            | 
            | 
           261 | 
               /**
  | 
        
        
            | 
            | 
           262 | 
                * Checks that action does not exist for a question.
  | 
        
        
            | 
            | 
           263 | 
                *
  | 
        
        
            | 
            | 
           264 | 
                * @Then the :action action should not exist for the :questionname question in the question bank
  | 
        
        
            | 
            | 
           265 | 
                * @param string $action the label for the action you want to activate.
  | 
        
        
            | 
            | 
           266 | 
                * @param string $questionname the question name.
  | 
        
        
            | 
            | 
           267 | 
                */
  | 
        
        
            | 
            | 
           268 | 
               public function action_not_exists($action, $questionname) {
  | 
        
        
            | 
            | 
           269 | 
                   $this->execute('behat_action_menu::item_should_not_exist_in_the', [
  | 
        
        
            | 
            | 
           270 | 
                       $action,
  | 
        
        
            | 
            | 
           271 | 
                       get_string('edit', 'core'),
  | 
        
        
            | 
            | 
           272 | 
                       $questionname,
  | 
        
        
            | 
            | 
           273 | 
                       'table_row',
  | 
        
        
            | 
            | 
           274 | 
                   ]);
  | 
        
        
            | 
            | 
           275 | 
               }
  | 
        
        
            | 
            | 
           276 | 
              | 
        
        
            | 
            | 
           277 | 
               /**
  | 
        
        
            | 
            | 
           278 | 
                * A particular bulk action is visible in the question bank UI.
  | 
        
        
            | 
            | 
           279 | 
                *
  | 
        
        
            | 
            | 
           280 | 
                * @When I should see question bulk action :action
  | 
        
        
            | 
            | 
           281 | 
                * @param string $action the value of the input for the action.
  | 
        
        
            | 
            | 
           282 | 
                */
  | 
        
        
            | 
            | 
           283 | 
               public function i_should_see_question_bulk_action($action) {
  | 
        
        
            | 
            | 
           284 | 
                   // Check if its visible.
  | 
        
        
            | 
            | 
           285 | 
                   $this->execute("behat_general::should_be_visible",
  | 
        
        
            | 
            | 
           286 | 
                       ["#bulkactionsui-container input[name='$action']", "css_element"]);
  | 
        
        
            | 
            | 
           287 | 
               }
  | 
        
        
            | 
            | 
           288 | 
              | 
        
        
            | 
            | 
           289 | 
               /**
  | 
        
        
            | 
            | 
           290 | 
                * A particular bulk action should not be visible in the question bank UI.
  | 
        
        
            | 
            | 
           291 | 
                *
  | 
        
        
            | 
            | 
           292 | 
                * @When I should not see question bulk action :action
  | 
        
        
            | 
            | 
           293 | 
                * @param string $action the value of the input for the action.
  | 
        
        
            | 
            | 
           294 | 
                */
  | 
        
        
            | 
            | 
           295 | 
               public function i_should_not_see_question_bulk_action($action) {
  | 
        
        
            | 
            | 
           296 | 
                   // Check if its visible.
  | 
        
        
            | 
            | 
           297 | 
                   $this->execute("behat_general::should_not_be_visible",
  | 
        
        
            | 
            | 
           298 | 
                       ["#bulkactionsui-container input[name='$action']", "css_element"]);
  | 
        
        
            | 
            | 
           299 | 
               }
  | 
        
        
            | 
            | 
           300 | 
              | 
        
        
            | 
            | 
           301 | 
               /**
  | 
        
        
            | 
            | 
           302 | 
                * A click on a particular bulk action in the question bank UI.
  | 
        
        
            | 
            | 
           303 | 
                *
  | 
        
        
            | 
            | 
           304 | 
                * @When I click on question bulk action :action
  | 
        
        
            | 
            | 
           305 | 
                * @param string $action the value of the input for the action.
  | 
        
        
            | 
            | 
           306 | 
                */
  | 
        
        
            | 
            | 
           307 | 
               public function i_click_on_question_bulk_action($action) {
  | 
        
        
            | 
            | 
           308 | 
                   // Click the bulk action.
  | 
        
        
            | 
            | 
           309 | 
                   $this->execute("behat_general::i_click_on",
  | 
        
        
            | 
            | 
           310 | 
                       ["#bulkactionsui-container input[name='$action']", "css_element"]);
  | 
        
        
            | 
            | 
           311 | 
               }
  | 
        
        
            | 
            | 
           312 | 
              | 
        
        
            | 
            | 
           313 | 
               /**
  | 
        
        
            | 
            | 
           314 | 
                * Change the question type of the give question to a type that does not exist.
  | 
        
        
            | 
            | 
           315 | 
                *
  | 
        
        
            | 
            | 
           316 | 
                * This is useful for testing robustness of the code when a question type
  | 
        
        
            | 
            | 
           317 | 
                * has been uninstalled, even though there are still questions of that type
  | 
        
        
            | 
            | 
           318 | 
                * or attempts at them.
  | 
        
        
            | 
            | 
           319 | 
                *
  | 
        
        
            | 
            | 
           320 | 
                * In order to set things up, you probably need to start by generating
  | 
        
        
            | 
            | 
           321 | 
                * questions of a valid type, then using this to change the type once the
  | 
        
        
            | 
            | 
           322 | 
                * data is created.
  | 
        
        
            | 
            | 
           323 | 
                *
  | 
        
        
            | 
            | 
           324 | 
                * @Given question :questionname is changed to simulate being of an uninstalled type
  | 
        
        
            | 
            | 
           325 | 
                * @param string $questionname the question name.
  | 
        
        
            | 
            | 
           326 | 
                */
  | 
        
        
            | 
            | 
           327 | 
               public function change_question_to_nonexistant_type($questionname) {
  | 
        
        
            | 
            | 
           328 | 
                   global $DB;
  | 
        
        
            | 
            | 
           329 | 
                   [$id] = $this->find_question_by_name($questionname);
  | 
        
        
            | 
            | 
           330 | 
              | 
        
        
            | 
            | 
           331 | 
                   // Check our assumption.
  | 
        
        
            | 
            | 
           332 | 
                   $nonexistanttype = 'invalidqtype';
  | 
        
        
            | 
            | 
           333 | 
                   if (question_bank::is_qtype_installed($nonexistanttype)) {
  | 
        
        
            | 
            | 
           334 | 
                       throw new coding_exception('This code assumes that the qtype_' . $nonexistanttype .
  | 
        
        
            | 
            | 
           335 | 
                               ' is not a valid plugin name, but that plugin now seems to exist!');
  | 
        
        
            | 
            | 
           336 | 
                   }
  | 
        
        
            | 
            | 
           337 | 
              | 
        
        
            | 
            | 
           338 | 
                   $DB->set_field('question', 'qtype', $nonexistanttype, ['id' => $id]);
  | 
        
        
            | 
            | 
           339 | 
                   question_bank::notify_question_edited($id);
  | 
        
        
            | 
            | 
           340 | 
               }
  | 
        
        
            | 
            | 
           341 | 
              | 
        
        
            | 
            | 
           342 | 
               /**
  | 
        
        
            | 
            | 
           343 | 
                * Forcibly delete a question from the database.
  | 
        
        
            | 
            | 
           344 | 
                *
  | 
        
        
            | 
            | 
           345 | 
                * This is useful for testing robustness of the code when a question
  | 
        
        
            | 
            | 
           346 | 
                * record is no longer in the database, even though it is referred to.
  | 
        
        
            | 
            | 
           347 | 
                * Obviously, this should never happen, but it has been known to in the past
  | 
        
        
            | 
            | 
           348 | 
                * and so we sometimes need to be able to test the code can handle this situation.
  | 
        
        
            | 
            | 
           349 | 
                *
  | 
        
        
            | 
            | 
           350 | 
                * In order to set things up, you probably need to start by generating
  | 
        
        
            | 
            | 
           351 | 
                * a valid questions, then using this to remove it once the data is created.
  | 
        
        
            | 
            | 
           352 | 
                *
  | 
        
        
            | 
            | 
           353 | 
                * @Given question :questionname no longer exists in the database
  | 
        
        
            | 
            | 
           354 | 
                * @param string $questionname the question name.
  | 
        
        
            | 
            | 
           355 | 
                */
  | 
        
        
            | 
            | 
           356 | 
               public function remove_question_from_db($questionname) {
  | 
        
        
            | 
            | 
           357 | 
                   global $DB;
  | 
        
        
            | 
            | 
           358 | 
                   [$id] = $this->find_question_by_name($questionname);
  | 
        
        
            | 
            | 
           359 | 
                   $DB->delete_records('question', ['id' => $id]);
  | 
        
        
            | 
            | 
           360 | 
                   question_bank::notify_question_edited($id);
  | 
        
        
            | 
            | 
           361 | 
               }
  | 
        
        
            | 
            | 
           362 | 
              | 
        
        
            | 
            | 
           363 | 
               /**
  | 
        
        
            | 
            | 
           364 | 
                * Add a question bank filter
  | 
        
        
            | 
            | 
           365 | 
                *
  | 
        
        
            | 
            | 
           366 | 
                * This will add the filter if it does not exist, but leave the value empty.
  | 
        
        
            | 
            | 
           367 | 
                *
  | 
        
        
            | 
            | 
           368 | 
                * @When I add question bank filter :filtertype
  | 
        
        
            | 
            | 
           369 | 
                * @param string $filtertype The filter we are adding
  | 
        
        
            | 
            | 
           370 | 
                */
  | 
        
        
            | 
            | 
           371 | 
               public function i_add_question_bank_filter(string $filtertype) {
  | 
        
        
            | 
            | 
           372 | 
                   $filter = $this->getSession()->getPage()->find('css',
  | 
        
        
            | 
            | 
           373 | 
                           '[data-filterregion=filter] [data-field-title="' . $filtertype . '"]');
  | 
        
        
            | 
            | 
           374 | 
                   if ($filter === null) {
  | 
        
        
            | 
            | 
           375 | 
                       $this->execute('behat_forms::press_button', [get_string('addcondition')]);
  | 
        
        
            | 
            | 
           376 | 
                       $this->execute('behat_forms::i_set_the_field_in_container_to', [
  | 
        
        
            | 
            | 
           377 | 
                               "type",
  | 
        
        
            | 
            | 
           378 | 
                               "[data-filterregion=filter]:last-child fieldset",
  | 
        
        
            | 
            | 
           379 | 
                               "css_element",
  | 
        
        
            | 
            | 
           380 | 
                               $filtertype
  | 
        
        
            | 
            | 
           381 | 
                       ]);
  | 
        
        
            | 
            | 
           382 | 
                   }
  | 
        
        
            | 
            | 
           383 | 
               }
  | 
        
        
            | 
            | 
           384 | 
              | 
        
        
            | 
            | 
           385 | 
               /**
  | 
        
        
            | 
            | 
           386 | 
                * Apply question bank filter.
  | 
        
        
            | 
            | 
           387 | 
                *
  | 
        
        
            | 
            | 
           388 | 
                * This will change the existing value of the specified filter, or add the filter and set its value if it doesn't already
  | 
        
        
            | 
            | 
           389 | 
                * exist.
  | 
        
        
            | 
            | 
           390 | 
                *
  | 
        
        
            | 
            | 
           391 | 
                * @When I apply question bank filter :filtertype with value :value
  | 
        
        
            | 
            | 
           392 | 
                * @param string $filtertype The filter to apply. This should match the get_title() return value from the
  | 
        
        
            | 
            | 
           393 | 
                *        filter's condition class.
  | 
        
        
            | 
            | 
           394 | 
                * @param string $value The value to set for the condition.
  | 
        
        
            | 
            | 
           395 | 
                */
  | 
        
        
            | 
            | 
           396 | 
               public function i_apply_question_bank_filter(string $filtertype, string $value) {
  | 
        
        
            | 
            | 
           397 | 
                   // Add the filter if needed.
  | 
        
        
            | 
            | 
           398 | 
                   $this->execute('behat_core_question::i_add_question_bank_filter', [
  | 
        
        
            | 
            | 
           399 | 
                       $filtertype,
  | 
        
        
            | 
            | 
           400 | 
                   ]);
  | 
        
        
            | 
            | 
           401 | 
              | 
        
        
            | 
            | 
           402 | 
                   // Set the filter value.
  | 
        
        
            | 
            | 
           403 | 
                   $this->execute('behat_forms::i_set_the_field_to', [
  | 
        
        
            | 
            | 
           404 | 
                       $filtertype,
  | 
        
        
            | 
            | 
           405 | 
                       $value
  | 
        
        
            | 
            | 
           406 | 
                   ]);
  | 
        
        
            | 
            | 
           407 | 
              | 
        
        
            | 
            | 
           408 | 
                   // Apply filters.
  | 
        
        
            | 
            | 
           409 | 
                   $this->execute("behat_forms::press_button", [get_string('applyfilters')]);
  | 
        
        
            | 
            | 
           410 | 
               }
  | 
        
        
           | 1441 | 
           ariadna | 
           411 | 
              | 
        
        
            | 
            | 
           412 | 
               /**
  | 
        
        
            | 
            | 
           413 | 
                * Record that a user has recently accessed the question bank related to a particular activity.
  | 
        
        
            | 
            | 
           414 | 
                *
  | 
        
        
            | 
            | 
           415 | 
                * @Given :user has recently viewed the :activityname :activitytype question bank
  | 
        
        
            | 
            | 
           416 | 
                * @param string $useridentifier The user's username or email.
  | 
        
        
            | 
            | 
           417 | 
                * @param string $activityname name of an activity.
  | 
        
        
            | 
            | 
           418 | 
                * @param string $activitytype type of an activity, e.g. 'quiz' or 'qbank'.
  | 
        
        
            | 
            | 
           419 | 
                */
  | 
        
        
            | 
            | 
           420 | 
               public function user_has_recently_viewed_question_bank(
  | 
        
        
            | 
            | 
           421 | 
                   string $useridentifier,
  | 
        
        
            | 
            | 
           422 | 
                   string $activityname,
  | 
        
        
            | 
            | 
           423 | 
                   string $activitytype,
  | 
        
        
            | 
            | 
           424 | 
               ): void {
  | 
        
        
            | 
            | 
           425 | 
                   global $USER;
  | 
        
        
            | 
            | 
           426 | 
                   $originaluser = $USER;
  | 
        
        
            | 
            | 
           427 | 
              | 
        
        
            | 
            | 
           428 | 
                   if (!plugin_supports('mod', $activitytype, FEATURE_USES_QUESTIONS)) {
  | 
        
        
            | 
            | 
           429 | 
                       throw new Exception($activitytype . ' do not have a question bank.');
  | 
        
        
            | 
            | 
           430 | 
                   }
  | 
        
        
            | 
            | 
           431 | 
              | 
        
        
            | 
            | 
           432 | 
                   $user = $this->get_user_by_identifier($useridentifier);
  | 
        
        
            | 
            | 
           433 | 
                   if (!$user) {
  | 
        
        
            | 
            | 
           434 | 
                       throw new Exception('Unknow user ' . $useridentifier . '.');
  | 
        
        
            | 
            | 
           435 | 
                   }
  | 
        
        
            | 
            | 
           436 | 
                   $USER = $user;
  | 
        
        
            | 
            | 
           437 | 
              | 
        
        
            | 
            | 
           438 | 
                   $cm = $this->get_cm_by_activity_name($activitytype, $activityname);
  | 
        
        
            | 
            | 
           439 | 
                   \core_question\local\bank\question_bank_helper::add_bank_context_to_recently_viewed($cm->context);
  | 
        
        
            | 
            | 
           440 | 
              | 
        
        
            | 
            | 
           441 | 
                   $USER = $originaluser;
  | 
        
        
            | 
            | 
           442 | 
               }
  | 
        
        
           | 1 | 
           efrain | 
           443 | 
           }
  |