Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [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 /** 18 * Badges external API 19 * 20 * @package core_badges 21 * @category external 22 * @copyright 2016 Juan Leyva <juan@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.1 25 */ 26 27 defined('MOODLE_INTERNAL') || die; 28 29 require_once($CFG->libdir . '/badgeslib.php'); 30 31 use core_badges\external\user_badge_exporter; 32 use core_external\external_api; 33 use core_external\external_function_parameters; 34 use core_external\external_multiple_structure; 35 use core_external\external_single_structure; 36 use core_external\external_value; 37 use core_external\external_warnings; 38 39 /** 40 * Badges external functions 41 * 42 * @package core_badges 43 * @category external 44 * @copyright 2016 Juan Leyva <juan@moodle.com> 45 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 46 * @since Moodle 3.1 47 */ 48 class core_badges_external extends external_api { 49 50 /** 51 * Describes the parameters for get_user_badges. 52 * 53 * @return external_function_parameters 54 * @since Moodle 3.1 55 */ 56 public static function get_user_badges_parameters() { 57 return new external_function_parameters ( 58 array( 59 'userid' => new external_value(PARAM_INT, 'Badges only for this user id, empty for current user', VALUE_DEFAULT, 0), 60 'courseid' => new external_value(PARAM_INT, 'Filter badges by course id, empty all the courses', VALUE_DEFAULT, 0), 61 'page' => new external_value(PARAM_INT, 'The page of records to return.', VALUE_DEFAULT, 0), 62 'perpage' => new external_value(PARAM_INT, 'The number of records to return per page', VALUE_DEFAULT, 0), 63 'search' => new external_value(PARAM_RAW, 'A simple string to search for', VALUE_DEFAULT, ''), 64 'onlypublic' => new external_value(PARAM_BOOL, 'Whether to return only public badges', VALUE_DEFAULT, false), 65 ) 66 ); 67 } 68 69 /** 70 * Returns the list of badges awarded to a user. 71 * 72 * @param int $userid user id 73 * @param int $courseid course id 74 * @param int $page page of records to return 75 * @param int $perpage number of records to return per page 76 * @param string $search a simple string to search for 77 * @param bool $onlypublic whether to return only public badges 78 * @return array array containing warnings and the awarded badges 79 * @since Moodle 3.1 80 * @throws moodle_exception 81 */ 82 public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) { 83 global $CFG, $USER, $PAGE; 84 85 $warnings = array(); 86 87 $params = array( 88 'userid' => $userid, 89 'courseid' => $courseid, 90 'page' => $page, 91 'perpage' => $perpage, 92 'search' => $search, 93 'onlypublic' => $onlypublic, 94 ); 95 $params = self::validate_parameters(self::get_user_badges_parameters(), $params); 96 97 if (empty($CFG->enablebadges)) { 98 throw new moodle_exception('badgesdisabled', 'badges'); 99 } 100 101 if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) { 102 throw new moodle_exception('coursebadgesdisabled', 'badges'); 103 } 104 105 // Default value for userid. 106 if (empty($params['userid'])) { 107 $params['userid'] = $USER->id; 108 } 109 110 // Validate the user. 111 $user = core_user::get_user($params['userid'], '*', MUST_EXIST); 112 core_user::require_active_user($user); 113 114 $usercontext = context_user::instance($user->id); 115 self::validate_context($usercontext); 116 117 if ($USER->id != $user->id) { 118 require_capability('moodle/badges:viewotherbadges', $usercontext); 119 // We are looking other user's badges, we must retrieve only public badges. 120 $params['onlypublic'] = true; 121 } 122 123 $userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'], 124 $params['onlypublic']); 125 126 $result = array(); 127 $result['badges'] = array(); 128 $result['warnings'] = $warnings; 129 130 foreach ($userbadges as $badge) { 131 $result['badges'][] = badges_prepare_badge_for_external($badge, $user); 132 } 133 134 return $result; 135 } 136 137 /** 138 * Describes the get_user_badges return value. 139 * 140 * @return external_single_structure 141 * @since Moodle 3.1 142 */ 143 public static function get_user_badges_returns() { 144 return new external_single_structure( 145 array( 146 'badges' => new external_multiple_structure( 147 user_badge_exporter::get_read_structure() 148 ), 149 'warnings' => new external_warnings(), 150 ) 151 ); 152 } 153 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body