[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 . '/externallib.php'); 30 require_once($CFG->libdir . '/badgeslib.php'); 31 32 /** 33 * Badges external functions 34 * 35 * @package core_badges 36 * @category external 37 * @copyright 2016 Juan Leyva <juan@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 * @since Moodle 3.1 40 */ 41 class core_badges_external extends external_api { 42 43 /** 44 * Describes the parameters for get_user_badges. 45 * 46 * @return external_external_function_parameters 47 * @since Moodle 3.1 48 */ 49 public static function get_user_badges_parameters() { 50 return new external_function_parameters ( 51 array( 52 'userid' => new external_value(PARAM_INT, 'Badges only for this user id, empty for current user', VALUE_DEFAULT, 0), 53 'courseid' => new external_value(PARAM_INT, 'Filter badges by course id, empty all the courses', VALUE_DEFAULT, 0), 54 'page' => new external_value(PARAM_INT, 'The page of records to return.', VALUE_DEFAULT, 0), 55 'perpage' => new external_value(PARAM_INT, 'The number of records to return per page', VALUE_DEFAULT, 0), 56 'search' => new external_value(PARAM_RAW, 'A simple string to search for', VALUE_DEFAULT, ''), 57 'onlypublic' => new external_value(PARAM_BOOL, 'Whether to return only public badges', VALUE_DEFAULT, false), 58 ) 59 ); 60 } 61 62 /** 63 * Returns the list of badges awarded to a user. 64 * 65 * @param int $userid user id 66 * @param int $courseid course id 67 * @param int $page page of records to return 68 * @param int $perpage number of records to return per page 69 * @param string $search a simple string to search for 70 * @param bool $onlypublic whether to return only public badges 71 * @return array array containing warnings and the awarded badges 72 * @since Moodle 3.1 73 * @throws moodle_exception 74 */ 75 public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) { 76 global $CFG, $USER; 77 78 $warnings = array(); 79 80 $params = array( 81 'userid' => $userid, 82 'courseid' => $courseid, 83 'page' => $page, 84 'perpage' => $perpage, 85 'search' => $search, 86 'onlypublic' => $onlypublic, 87 ); 88 $params = self::validate_parameters(self::get_user_badges_parameters(), $params); 89 90 if (empty($CFG->enablebadges)) { 91 throw new moodle_exception('badgesdisabled', 'badges'); 92 } 93 94 if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) { 95 throw new moodle_exception('coursebadgesdisabled', 'badges'); 96 } 97 98 // Default value for userid. 99 if (empty($params['userid'])) { 100 $params['userid'] = $USER->id; 101 } 102 103 // Validate the user. 104 $user = core_user::get_user($params['userid'], '*', MUST_EXIST); 105 core_user::require_active_user($user); 106 107 $usercontext = context_user::instance($user->id); 108 self::validate_context($usercontext); 109 110 if ($USER->id != $user->id) { 111 require_capability('moodle/badges:viewotherbadges', $usercontext); 112 // We are looking other user's badges, we must retrieve only public badges. 113 $params['onlypublic'] = true; 114 } 115 116 $userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'], 117 $params['onlypublic']); 118 119 $result = array(); 120 $result['badges'] = array(); 121 $result['warnings'] = $warnings; 122 123 foreach ($userbadges as $badge) { 124 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid); 125 $badge->badgeurl = moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/', 126 'f1')->out(false); 127 // Return all the information if we are requesting our own badges. 128 // Or, if we have permissions for configuring badges in the badge context. 129 if ($USER->id == $user->id or has_capability('moodle/badges:configuredetails', $context)) { 130 $result['badges'][] = (array) $badge; 131 } else { 132 $result['badges'][] = array( 133 'name' => $badge->name, 134 'description' => $badge->description, 135 'badgeurl' => $badge->badgeurl, 136 'issuername' => $badge->issuername, 137 'issuerurl' => $badge->issuerurl, 138 'issuercontact' => $badge->issuercontact, 139 'uniquehash' => $badge->uniquehash, 140 'dateissued' => $badge->dateissued, 141 'dateexpire' => $badge->dateexpire, 142 ); 143 } 144 } 145 146 return $result; 147 } 148 149 /** 150 * Describes the get_user_badges return value. 151 * 152 * @return external_single_structure 153 * @since Moodle 3.1 154 */ 155 public static function get_user_badges_returns() { 156 return new external_single_structure( 157 array( 158 'badges' => new external_multiple_structure( 159 new external_single_structure( 160 array( 161 'id' => new external_value(PARAM_INT, 'Badge id.', VALUE_OPTIONAL), 162 'name' => new external_value(PARAM_FILE, 'Badge name.'), 163 'description' => new external_value(PARAM_NOTAGS, 'Badge description.'), 164 'badgeurl' => new external_value(PARAM_URL, 'Badge URL.'), 165 'timecreated' => new external_value(PARAM_INT, 'Time created.', VALUE_OPTIONAL), 166 'timemodified' => new external_value(PARAM_INT, 'Time modified.', VALUE_OPTIONAL), 167 'usercreated' => new external_value(PARAM_INT, 'User created.', VALUE_OPTIONAL), 168 'usermodified' => new external_value(PARAM_INT, 'User modified.', VALUE_OPTIONAL), 169 'issuername' => new external_value(PARAM_NOTAGS, 'Issuer name.'), 170 'issuerurl' => new external_value(PARAM_URL, 'Issuer URL.'), 171 'issuercontact' => new external_value(PARAM_RAW, 'Issuer contact.'), 172 'expiredate' => new external_value(PARAM_INT, 'Expire date.', VALUE_OPTIONAL), 173 'expireperiod' => new external_value(PARAM_INT, 'Expire period.', VALUE_OPTIONAL), 174 'type' => new external_value(PARAM_INT, 'Type.', VALUE_OPTIONAL), 175 'courseid' => new external_value(PARAM_INT, 'Course id.', VALUE_OPTIONAL), 176 'message' => new external_value(PARAM_RAW, 'Message.', VALUE_OPTIONAL), 177 'messagesubject' => new external_value(PARAM_TEXT, 'Message subject.', VALUE_OPTIONAL), 178 'attachment' => new external_value(PARAM_INT, 'Attachment.', VALUE_OPTIONAL), 179 'status' => new external_value(PARAM_INT, 'Status.', VALUE_OPTIONAL), 180 'issuedid' => new external_value(PARAM_INT, 'Issued id.', VALUE_OPTIONAL), 181 'uniquehash' => new external_value(PARAM_ALPHANUM, 'Unique hash.'), 182 'dateissued' => new external_value(PARAM_INT, 'Date issued.'), 183 'dateexpire' => new external_value(PARAM_INT, 'Date expire.'), 184 'visible' => new external_value(PARAM_INT, 'Visible.', VALUE_OPTIONAL), 185 ) 186 ) 187 ), 188 'warnings' => new external_warnings(), 189 ) 190 ); 191 } 192 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |