Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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       * Return the list of partial named selectors.
  32       *
  33       * @return array
  34       */
  35      public static function get_partial_named_selectors(): array {
  36          return [
  37              new behat_component_named_selector(
  38                  'initials bar',
  39                  [".//*[contains(concat(' ', @class, ' '), ' initialbar ')]//span[contains(., %locator%)]/parent::div"]
  40              ),
  41              new behat_component_named_selector(
  42                  'grade_actions',
  43                  ["//td[count(//table[@id='user-grades']//th[contains(., %locator%)]/preceding-sibling::th)]//*[@data-type='grade']"]
  44              ),
  45          ];
  46      }
  47  
  48      /**
  49       * Convert page names to URLs for steps like 'When I am on the "[identifier]" "[page type]" page'.
  50       *
  51       * Recognised page names are:
  52       * | pagetype              | name meaning | description                                       |
  53       * | [report] view         | Course name  | The view page for the specified course and report |
  54       * | gradebook setup       | Course name  | The gradebook setup page for the specified course |
  55       * | course grade settings | Course name  | The grade settings page                           |
  56       * | outcomes              | Course name  | The grade outcomes page                           |
  57       * | scales                | Course name  | The grade scales page                             |
  58       *
  59       * @param string $type identifies which type of page this is - for example "Grader > View"
  60       * @param string $identifier identifies the particular page - for example "Course name"
  61       * @return moodle_url the corresponding URL.
  62       */
  63      protected function resolve_page_instance_url(string $type, string $identifier): moodle_url {
  64          $type = strtolower($type);
  65          if (strpos($type, '>') !== false) {
  66              [$pluginname, $type] = explode('>', $type);
  67              $pluginname = strtolower(trim($pluginname));
  68  
  69              // Fetch the list of plugins.
  70              $plugins = \core_component::get_plugin_list('gradereport');
  71  
  72              if (array_key_exists($pluginname, $plugins)) {
  73                  $plugin = $pluginname;
  74              } else {
  75                  $plugins = array_combine(
  76                      array_keys($plugins),
  77                      array_keys($plugins),
  78                  );
  79  
  80                  // This plugin is not in the list of plugins. Check the pluginname string.
  81                  $names = array_map(fn($name) => strtolower(get_string('pluginname', "gradereport_{$name}")), $plugins);
  82                  $result = array_search($pluginname, $names);
  83                  if ($result === false) {
  84                      throw new \coding_exception("Unknown plugin '{$pluginname}'");
  85                  }
  86                  $plugin = $result;
  87              }
  88          }
  89          $type = trim($type);
  90  
  91          switch ($type) {
  92              case 'view':
  93                  return new moodle_url(
  94                      "/grade/report/{$plugin}/index.php",
  95                      ['id' => $this->get_course_id($identifier)]
  96                  );
  97              case 'gradebook setup':
  98                  return new moodle_url(
  99                      "/grade/edit/tree/index.php",
 100                      ['id' => $this->get_course_id($identifier)]
 101                  );
 102              case 'course grade settings':
 103                  return new moodle_url(
 104                      "/grade/edit/settings/index.php",
 105                      ['id' => $this->get_course_id($identifier)]
 106                  );
 107              case 'outcomes':
 108                  return new moodle_url(
 109                      "/grade/edit/outcome/course.php",
 110                      ['id' => $this->get_course_id($identifier)]
 111                  );
 112              case 'scales':
 113                  return new moodle_url(
 114                      "/grade/edit/scale/index.php",
 115                      ['id' => $this->get_course_id($identifier)]
 116                  );
 117              default:
 118                  throw new \coding_exception(
 119                      "Unknown page type '$type' for page identifier '$identifier'"
 120                  );
 121          }
 122      }
 123  
 124      /**
 125       * Select a given element within a specific container instance.
 126       *
 127       * @Given /^I select "(?P<input_value>(?:[^"]|\\")*)" in the "(?P<instance>(?:[^"]|\\")*)" "(?P<instance_type>(?:[^"]|\\")*)"$/
 128       * @param string $value The Needle
 129       * @param string  $element The Haystack to select within
 130       * @param string $selectortype What type of haystack we are looking in
 131       */
 132      public function i_select_in_the($value, $element, $selectortype) {
 133          // Getting the container where the text should be found.
 134          $container = $this->get_selected_node($selectortype, $element);
 135          $node = $this->find('xpath', './/input[@value="' . $value . '"]', false, $container);
 136          $node->click();
 137      }
 138  
 139      /**
 140       * Gets the grade item id from its name.
 141       *
 142       * @throws Exception
 143       * @param string $itemname Item name
 144       * @return int
 145       */
 146      protected function get_grade_item_id(string $itemname): int {
 147  
 148          global $DB;
 149  
 150          if ($id = $DB->get_field('grade_items', 'id', ['itemname' => $itemname])) {
 151              return $id;
 152          }
 153  
 154          // The course total is a special case.
 155          if ($itemname === "Course total") {
 156              if (!$id = $DB->get_field('grade_items', 'id', ['itemtype' => 'course'])) {
 157                  throw new Exception('The specified grade_item with name "' . $itemname . '" does not exist');
 158              }
 159              return $id;
 160          }
 161  
 162          // Find a category with the name.
 163          if ($catid = $DB->get_field('grade_categories', 'id', ['fullname' => $itemname])) {
 164              if ($id = $DB->get_field('grade_items', 'id', ['iteminstance' => $catid])) {
 165                  return $id;
 166              }
 167          }
 168  
 169          throw new Exception('The specified grade_item with name "' . $itemname . '" does not exist');
 170      }
 171  
 172      /**
 173       * Gets course grade category id from coursename.
 174       *
 175       * @throws Exception
 176       * @param string $coursename
 177       * @return int
 178       */
 179      protected function get_course_grade_category_id(string $coursename): int {
 180  
 181          global $DB;
 182  
 183          $sql = "SELECT gc.id
 184                    FROM {grade_categories} gc
 185               LEFT JOIN {course} c
 186                      ON c.id = gc.courseid
 187                   WHERE c.fullname = ?
 188                     AND gc.depth = 1";
 189  
 190          if ($id = $DB->get_field_sql($sql, [$coursename])) {
 191              return $id;
 192          }
 193  
 194          throw new Exception('The specified course grade category with course name "' . $coursename . '" does not exist');
 195      }
 196  
 197      /**
 198       * Gets grade category id from its name.
 199       *
 200       * @throws Exception
 201       * @param string $categoryname
 202       * @return int
 203       */
 204      protected function get_grade_category_id(string $categoryname): int {
 205  
 206          global $DB;
 207  
 208          $sql = "SELECT gc.id
 209                    FROM {grade_categories} gc
 210               LEFT JOIN {course} c
 211                      ON c.id = gc.courseid
 212                   WHERE gc.fullname = ?";
 213  
 214          if ($id = $DB->get_field_sql($sql, [$categoryname])) {
 215              return $id;
 216          }
 217  
 218          throw new Exception('The specified grade category with name "' . $categoryname . '" does not exist');
 219      }
 220  
 221      /**
 222       * Clicks on given grade item menu.
 223       *
 224       * @Given /^I click on grade item menu "([^"]*)" of type "([^"]*)" on "([^"]*)" page$/
 225       * @param string $itemname Item name
 226       * @param string $itemtype Item type - grade item, category or course
 227       * @param string $page Page - setup or grader
 228       * @throws Exception
 229       */
 230      public function i_click_on_grade_item_menu(string $itemname, string $itemtype, string $page) {
 231  
 232          if ($itemtype == 'gradeitem') {
 233              $itemid = $this->get_grade_item_id($itemname);
 234          } else if ($itemtype == 'category') {
 235              $itemid = $this->get_grade_category_id($itemname);
 236          } else if ($itemtype == 'course') {
 237              $itemid = $this->get_course_grade_category_id($itemname);
 238          } else {
 239              throw new Exception('Unknown item type: ' . $itemtype);
 240          }
 241  
 242          $xpath = "//table[@id='grade_edit_tree_table']";
 243  
 244          if (($page == 'grader') || ($page == 'setup')) {
 245              if ($page == 'grader') {
 246                  $xpath = "//table[@id='user-grades']";
 247              }
 248  
 249              if ($itemtype == 'gradeitem') {
 250                  $xpath .= "//*[@data-type='item'][@data-id='" . $itemid . "']";
 251              } else if (($itemtype == 'category') || ($itemtype == 'course')) {
 252                  $xpath .= "//*[@data-type='category'][@data-id='" . $itemid . "']";
 253              } else {
 254                  throw new Exception('Unknown item type: ' . $itemtype);
 255              }
 256          } else {
 257              throw new Exception('Unknown page: ' . $page);
 258          }
 259          $this->execute("behat_general::i_click_on", [$this->escape($xpath), "xpath_element"]);
 260      }
 261  }