Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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   * Behat navigation hooks for core_question.
  27   *
  28   * @package    core_question
  29   * @category   test
  30   * @copyright  2022 The Open University
  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:
  56       * | pagetype               | name meaning               | description                              |
  57       * | course question bank   | Course name                | The question bank for a course           |
  58       * | course question import | Course name                | The import questions screen for a course |
  59       * | course question export | Course name                | The export questions screen for a course |
  60       * | preview                | Question name              | The screen to preview a question         |
  61       * | edit                   | Question name              | The screen to edit a question            |
  62       *
  63       * @param string $type identifies which type of page this is, e.g. 'Preview'.
  64       * @param string $identifier identifies the particular page, e.g. 'My question'.
  65       * @return moodle_url the corresponding URL.
  66       * @throws Exception with a meaningful error message if the specified page cannot be found.
  67       */
  68      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  69          switch (strtolower($type)) {
  70              case 'course question bank':
  71                  return new moodle_url('/question/edit.php',
  72                          ['courseid' => $this->get_course_id($identifier)]);
  73  
  74              case 'course question import':
  75                  return new moodle_url('/question/import.php',
  76                          ['courseid' => $this->get_course_id($identifier)]);
  77  
  78              case 'course question export':
  79                  return new moodle_url('/question/export.php',
  80                          ['courseid' => $this->get_course_id($identifier)]);
  81  
  82              case 'preview':
  83                  [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
  84                  return new moodle_url('/question/preview.php',
  85                          ['id' => $questionid, $otheridtype => $otherid]);
  86  
  87              case 'edit':
  88                  [$questionid, $otheridtype, $otherid] = $this->find_question_by_name($identifier);
  89                  return new moodle_url('/question/question.php',
  90                          ['id' => $questionid, $otheridtype => $otherid]);
  91  
  92              default:
  93                  throw new Exception('Unrecognised core_question page type "' . $type . '."');
  94          }
  95      }
  96  
  97      /**
  98       * Find a question, and where it is, from the question name.
  99       *
 100       * This is a helper used by resolve_page_instance_url.
 101       *
 102       * @param string $questionname
 103       * @return array with three elemnets, int question id, a string 'cmid' or 'courseid',
 104       *     and int either cmid or courseid as applicable.
 105       */
 106      protected function find_question_by_name(string $questionname): array {
 107          global $DB;
 108          $questionid = $DB->get_field('question', 'id', ['name' => $questionname], MUST_EXIST);
 109          $question = question_bank::load_question_data($questionid);
 110          $context = context_helper::instance_by_id($question->contextid);
 111  
 112          if ($context->contextlevel == CONTEXT_MODULE) {
 113              return [$questionid, 'cmid', $context->instanceid];
 114          } else if ($context->contextlevel == CONTEXT_COURSE) {
 115              return [$questionid, 'courseid', $context->instanceid];
 116          } else {
 117              throw new coding_exception('Unsupported context level ' . $context->contextlevel);
 118          }
 119      }
 120  }