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 401 and 402]

   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  namespace core_grades\external;
  18  
  19  use context_course;
  20  use core_external\external_api;
  21  use core_external\external_description;
  22  use core_external\external_function_parameters;
  23  use core_external\external_multiple_structure;
  24  use core_external\external_single_structure;
  25  use core_external\external_value;
  26  use core_external\external_warnings;
  27  
  28  /**
  29   * External group report API implementation
  30   *
  31   * @package    core_grades
  32   * @copyright  2022 Mathew May <mathew.solutions>
  33   * @category   external
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @deprecated
  36   */
  37  class get_groups_for_search_widget extends external_api {
  38  
  39      /**
  40       * Returns description of method parameters.
  41       *
  42       * @return external_function_parameters
  43       * @deprecated since 4.2
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters (
  47              [
  48                  'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED),
  49                  'actionbaseurl' => new external_value(PARAM_URL, 'The base URL for the group action', VALUE_REQUIRED)
  50              ]
  51          );
  52      }
  53  
  54      /**
  55       * Given a course ID find the existing user groups and map some fields to the returned array of group objects.
  56       *
  57       * @param int $courseid
  58       * @param string $actionbaseurl The base URL for the group action.
  59       * @return array Groups and warnings to pass back to the calling widget.
  60       * @deprecated since 4.2
  61       */
  62      public static function execute(int $courseid, string $actionbaseurl): array {
  63          global $DB, $USER, $COURSE;
  64  
  65          $params = self::validate_parameters(
  66              self::execute_parameters(),
  67              [
  68                  'courseid' => $courseid,
  69                  'actionbaseurl' => $actionbaseurl
  70              ]
  71          );
  72  
  73          $warnings = [];
  74          $context = context_course::instance($params['courseid']);
  75          parent::validate_context($context);
  76  
  77          $mappedgroups = [];
  78          $course = $DB->get_record('course', ['id' => $params['courseid']]);
  79          // Initialise the grade tracking object.
  80          if ($groupmode = $course->groupmode) {
  81              $aag = has_capability('moodle/site:accessallgroups', $context);
  82  
  83              $usergroups = [];
  84              $groupuserid = 0;
  85              if ($groupmode == VISIBLEGROUPS || $aag) {
  86                  // Get user's own groups and put to the top.
  87                  $usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
  88              } else {
  89                  $groupuserid = $USER->id;
  90              }
  91              $allowedgroups = groups_get_all_groups($course->id, $groupuserid, $course->defaultgroupingid);
  92  
  93              $allgroups = array_merge($allowedgroups, $usergroups);
  94              // Filter out any duplicate groups.
  95              $groupsmenu = array_intersect_key($allgroups, array_unique(array_column($allgroups, 'name')));
  96  
  97              if (!$allowedgroups || $groupmode == VISIBLEGROUPS || $aag) {
  98                  array_unshift($groupsmenu, (object) [
  99                      'id' => 0,
 100                      'name' => get_string('allparticipants'),
 101                  ]);
 102              }
 103  
 104              $mappedgroups = array_map(function($group) use ($COURSE, $actionbaseurl, $context) {
 105                  $url = new \moodle_url($actionbaseurl, [
 106                      'id' => $COURSE->id,
 107                      'group' => $group->id
 108                  ]);
 109                  return (object) [
 110                      'id' => $group->id,
 111                      'name' => format_string($group->name, true, ['context' => $context]),
 112                      'url' => $url->out(false),
 113                      'active' => false
 114                  ];
 115              }, $groupsmenu);
 116          }
 117  
 118          return [
 119              'groups' => $mappedgroups,
 120              'warnings' => $warnings,
 121          ];
 122      }
 123  
 124      /**
 125       * Returns description of what the group search for the widget should return.
 126       *
 127       * @return external_single_structure
 128       * @deprecated since 4.2
 129       */
 130      public static function execute_returns(): external_single_structure {
 131          return new external_single_structure([
 132              'groups' => new external_multiple_structure(self::group_description()),
 133              'warnings' => new external_warnings(),
 134          ]);
 135      }
 136  
 137      /**
 138       * Create group return value description.
 139       *
 140       * @return external_description
 141       */
 142      public static function group_description(): external_description {
 143          $groupfields = [
 144              'id' => new external_value(PARAM_ALPHANUM, 'An ID for the group', VALUE_REQUIRED),
 145              'url' => new external_value(PARAM_URL, 'The link that applies the group action', VALUE_REQUIRED),
 146              'name' => new external_value(PARAM_TEXT, 'The full name of the group', VALUE_REQUIRED),
 147              'active' => new external_value(PARAM_BOOL, 'Are we currently on this item?', VALUE_REQUIRED)
 148          ];
 149          return new external_single_structure($groupfields);
 150      }
 151  
 152      /**
 153       * Mark the function as deprecated.
 154       * @return bool
 155       */
 156      public static function execute_is_deprecated() {
 157          return true;
 158      }
 159  }