Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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__ . '/../../../lib/behat/behat_base.php');
  20  
  21  /**
  22   * Behat grade related steps definitions.
  23   *
  24   * @package    core_grades
  25   * @copyright  2022 Mathew May <mathew.solutions>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class behat_grades extends behat_base {
  29  
  30      /**
  31       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  32       *
  33       * Recognised page names are:
  34       * | pagetype              | name meaning | description                                       |
  35       * | [report] view         | Course name  | The view page for the specified course and report |
  36       * | gradebook setup       | Course name  | The gradebook setup page for the specified course |
  37       * | course grade settings | Course name  | The grade settings page                           |
  38       * | outcomes              | Course name  | The grade outcomes page                           |
  39       * | scales                | Course name  | The grade scales page                             |
  40       *
  41       * @param string $type identifies which type of page this is - for example "Grader > View"
  42       * @param string $identifier identifies the particular page - for example "Course name"
  43       * @return moodle_url the corresponding URL.
  44       */
  45      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  46          $type = strtolower($type);
  47          if (strpos($type, '>') !== false) {
  48              [$pluginname, $type] = explode('>', $type);
  49              $pluginname = strtolower(trim($pluginname));
  50  
  51              // Fetch the list of plugins.
  52              $plugins = \core_component::get_plugin_list('gradereport');
  53  
  54              if (array_key_exists($pluginname, $plugins)) {
  55                  $plugin = $pluginname;
  56              } else {
  57                  $plugins = array_combine(
  58                      array_keys($plugins),
  59                      array_keys($plugins),
  60                  );
  61  
  62                  // This plugin is not in the list of plugins. Check the pluginname string.
  63                  $names = array_map(fn($name) => strtolower(get_string('pluginname', "gradereport_{$name}")), $plugins);
  64                  $result = array_search($pluginname, $names);
  65                  if ($result === false) {
  66                      throw new \coding_exception("Unknown plugin '{$pluginname}'");
  67                  }
  68                  $plugin = $result;
  69              }
  70          }
  71          $type = trim($type);
  72  
  73          switch ($type) {
  74              case 'view':
  75                  return new moodle_url(
  76                      "/grade/report/{$plugin}/index.php",
  77                      ['id' => $this->get_course_id($identifier)]
  78                  );
  79              case 'gradebook setup':
  80                  return new moodle_url(
  81                      "/grade/edit/tree/index.php",
  82                      ['id' => $this->get_course_id($identifier)]
  83                  );
  84              case 'course grade settings':
  85                  return new moodle_url(
  86                      "/grade/edit/settings/index.php",
  87                      ['id' => $this->get_course_id($identifier)]
  88                  );
  89              case 'outcomes':
  90                  return new moodle_url(
  91                      "/grade/edit/outcome/course.php",
  92                      ['id' => $this->get_course_id($identifier)]
  93                  );
  94              case 'scales':
  95                  return new moodle_url(
  96                      "/grade/edit/scale/index.php",
  97                      ['id' => $this->get_course_id($identifier)]
  98                  );
  99              default:
 100                  throw new \coding_exception(
 101                      "Unknown page type '$type' for page identifier '$identifier'"
 102                  );
 103          }
 104      }
 105  }