weekly release 3.9.3+
[moodle.git] / badges / classes / external.php
blobdf841f760d30e89d3caea7e1fbf5637b5d5f07fa
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.
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/>.
17 /**
18 * Badges external API
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
27 defined('MOODLE_INTERNAL') || die;
29 require_once($CFG->libdir . '/externallib.php');
30 require_once($CFG->libdir . '/badgeslib.php');
32 use core_badges\external\user_badge_exporter;
34 /**
35 * Badges external functions
37 * @package core_badges
38 * @category external
39 * @copyright 2016 Juan Leyva <juan@moodle.com>
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 * @since Moodle 3.1
43 class core_badges_external extends external_api {
45 /**
46 * Describes the parameters for get_user_badges.
48 * @return external_function_parameters
49 * @since Moodle 3.1
51 public static function get_user_badges_parameters() {
52 return new external_function_parameters (
53 array(
54 'userid' => new external_value(PARAM_INT, 'Badges only for this user id, empty for current user', VALUE_DEFAULT, 0),
55 'courseid' => new external_value(PARAM_INT, 'Filter badges by course id, empty all the courses', VALUE_DEFAULT, 0),
56 'page' => new external_value(PARAM_INT, 'The page of records to return.', VALUE_DEFAULT, 0),
57 'perpage' => new external_value(PARAM_INT, 'The number of records to return per page', VALUE_DEFAULT, 0),
58 'search' => new external_value(PARAM_RAW, 'A simple string to search for', VALUE_DEFAULT, ''),
59 'onlypublic' => new external_value(PARAM_BOOL, 'Whether to return only public badges', VALUE_DEFAULT, false),
64 /**
65 * Returns the list of badges awarded to a user.
67 * @param int $userid user id
68 * @param int $courseid course id
69 * @param int $page page of records to return
70 * @param int $perpage number of records to return per page
71 * @param string $search a simple string to search for
72 * @param bool $onlypublic whether to return only public badges
73 * @return array array containing warnings and the awarded badges
74 * @since Moodle 3.1
75 * @throws moodle_exception
77 public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {
78 global $CFG, $USER, $PAGE;
80 $warnings = array();
82 $params = array(
83 'userid' => $userid,
84 'courseid' => $courseid,
85 'page' => $page,
86 'perpage' => $perpage,
87 'search' => $search,
88 'onlypublic' => $onlypublic,
90 $params = self::validate_parameters(self::get_user_badges_parameters(), $params);
92 if (empty($CFG->enablebadges)) {
93 throw new moodle_exception('badgesdisabled', 'badges');
96 if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) {
97 throw new moodle_exception('coursebadgesdisabled', 'badges');
100 // Default value for userid.
101 if (empty($params['userid'])) {
102 $params['userid'] = $USER->id;
105 // Validate the user.
106 $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
107 core_user::require_active_user($user);
109 $usercontext = context_user::instance($user->id);
110 self::validate_context($usercontext);
112 if ($USER->id != $user->id) {
113 require_capability('moodle/badges:viewotherbadges', $usercontext);
114 // We are looking other user's badges, we must retrieve only public badges.
115 $params['onlypublic'] = true;
118 $userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'],
119 $params['onlypublic']);
121 $result = array();
122 $result['badges'] = array();
123 $result['warnings'] = $warnings;
125 foreach ($userbadges as $badge) {
126 $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
127 $canconfiguredetails = has_capability('moodle/badges:configuredetails', $context);
129 // If the user is viewing another user's badge and doesn't have the right capability return only part of the data.
130 if ($USER->id != $user->id and !$canconfiguredetails) {
131 $badge = (object) array(
132 'id' => $badge->id,
133 'name' => $badge->name,
134 'description' => $badge->description,
135 'issuername' => $badge->issuername,
136 'issuerurl' => $badge->issuerurl,
137 'issuercontact' => $badge->issuercontact,
138 'uniquehash' => $badge->uniquehash,
139 'dateissued' => $badge->dateissued,
140 'dateexpire' => $badge->dateexpire,
141 'version' => $badge->version,
142 'language' => $badge->language,
143 'imageauthorname' => $badge->imageauthorname,
144 'imageauthoremail' => $badge->imageauthoremail,
145 'imageauthorurl' => $badge->imageauthorurl,
146 'imagecaption' => $badge->imagecaption,
150 // Create a badge instance to be able to get the endorsement and other info.
151 $badgeinstance = new badge($badge->id);
152 $endorsement = $badgeinstance->get_endorsement();
153 $alignments = $badgeinstance->get_alignments();
154 $relatedbadges = $badgeinstance->get_related_badges();
156 if (!$canconfiguredetails) {
157 // Return only the properties visible by the user.
159 if (!empty($alignments)) {
160 foreach ($alignments as $alignment) {
161 unset($alignment->targetdescription);
162 unset($alignment->targetframework);
163 unset($alignment->targetcode);
167 if (!empty($relatedbadges)) {
168 foreach ($relatedbadges as $relatedbadge) {
169 unset($relatedbadge->version);
170 unset($relatedbadge->language);
171 unset($relatedbadge->type);
176 $related = array(
177 'context' => $context,
178 'endorsement' => $endorsement ? $endorsement : null,
179 'alignment' => $alignments,
180 'relatedbadges' => $relatedbadges,
183 $exporter = new user_badge_exporter($badge, $related);
184 $result['badges'][] = $exporter->export($PAGE->get_renderer('core'));
187 return $result;
191 * Describes the get_user_badges return value.
193 * @return external_single_structure
194 * @since Moodle 3.1
196 public static function get_user_badges_returns() {
197 return new external_single_structure(
198 array(
199 'badges' => new external_multiple_structure(
200 user_badge_exporter::get_read_structure()
202 'warnings' => new external_warnings(),