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 */ 36 class get_groups_for_selector extends external_api { 37 38 /** 39 * Returns description of method parameters. 40 * 41 * @return external_function_parameters 42 */ 43 public static function execute_parameters(): external_function_parameters { 44 return new external_function_parameters ( 45 [ 46 'courseid' => new external_value(PARAM_INT, 'Course Id', VALUE_REQUIRED), 47 ] 48 ); 49 } 50 51 /** 52 * Given a course ID find the existing user groups and map some fields to the returned array of group objects. 53 * 54 * @param int $courseid 55 * @return array Groups and warnings to pass back to the calling widget. 56 */ 57 public static function execute(int $courseid): array { 58 global $DB, $USER; 59 60 $params = self::validate_parameters( 61 self::execute_parameters(), 62 [ 63 'courseid' => $courseid, 64 ] 65 ); 66 67 $warnings = []; 68 $context = context_course::instance($params['courseid']); 69 parent::validate_context($context); 70 71 $mappedgroups = []; 72 $course = $DB->get_record('course', ['id' => $params['courseid']]); 73 // Initialise the grade tracking object. 74 if ($groupmode = $course->groupmode) { 75 $aag = has_capability('moodle/site:accessallgroups', $context); 76 77 $usergroups = []; 78 $groupuserid = 0; 79 if ($groupmode == VISIBLEGROUPS || $aag) { 80 // Get user's own groups and put to the top. 81 $usergroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid); 82 } else { 83 $groupuserid = $USER->id; 84 } 85 $allowedgroups = groups_get_all_groups($course->id, $groupuserid, $course->defaultgroupingid); 86 87 $allgroups = array_merge($allowedgroups, $usergroups); 88 // Filter out any duplicate groups. 89 $groupsmenu = array_intersect_key($allgroups, array_unique(array_column($allgroups, 'name'))); 90 91 if (!$allowedgroups || $groupmode == VISIBLEGROUPS || $aag) { 92 array_unshift($groupsmenu, (object) [ 93 'id' => 0, 94 'name' => get_string('allparticipants'), 95 ]); 96 } 97 98 $mappedgroups = array_map(function($group) use ($context) { 99 return (object) [ 100 'id' => $group->id, 101 'name' => format_string($group->name, true, ['context' => $context]) 102 ]; 103 }, $groupsmenu); 104 } 105 106 return [ 107 'groups' => $mappedgroups, 108 'warnings' => $warnings, 109 ]; 110 } 111 112 /** 113 * Returns description of what the group search for the widget should return. 114 * 115 * @return external_single_structure 116 */ 117 public static function execute_returns(): external_single_structure { 118 return new external_single_structure([ 119 'groups' => new external_multiple_structure(self::group_description()), 120 'warnings' => new external_warnings(), 121 ]); 122 } 123 124 /** 125 * Create group return value description. 126 * 127 * @return external_description 128 */ 129 public static function group_description(): external_description { 130 $groupfields = [ 131 'id' => new external_value(PARAM_ALPHANUM, 'An ID for the group', VALUE_REQUIRED), 132 'name' => new external_value(PARAM_TEXT, 'The full name of the group', VALUE_REQUIRED) 133 ]; 134 return new external_single_structure($groupfields); 135 } 136 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body