Skip to content

Commit

Permalink
MDL-63729 badges: Create an exporter for badge info
Browse files Browse the repository at this point in the history
  • Loading branch information
dpalou committed Nov 9, 2018
1 parent 08c51ff commit c776e1d
Show file tree
Hide file tree
Showing 2 changed files with 242 additions and 38 deletions.
50 changes: 12 additions & 38 deletions badges/classes/external.php
Expand Up @@ -29,6 +29,8 @@
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->libdir . '/badgeslib.php');

use core_badges\external\user_badge_exporter;

/**
* Badges external functions
*
Expand Down Expand Up @@ -73,7 +75,7 @@ public static function get_user_badges_parameters() {
* @throws moodle_exception
*/
public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {
global $CFG, $USER;
global $CFG, $USER, $PAGE;

$warnings = array();

Expand Down Expand Up @@ -122,17 +124,13 @@ public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $p

foreach ($userbadges as $badge) {
$context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
$badge->badgeurl = moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $badge->id, '/',
'f1')->out(false);
// Return all the information if we are requesting our own badges.
// Or, if we have permissions for configuring badges in the badge context.
if ($USER->id == $user->id or has_capability('moodle/badges:configuredetails', $context)) {
$result['badges'][] = (array) $badge;
} else {
$result['badges'][] = array(

// If the user is viewing another user's badge and doesn't have the right capability return only part of the data.
if ($USER->id != $user->id and !has_capability('moodle/badges:configuredetails', $context)) {
$badge = (object) array(
'id' => $badge->id,
'name' => $badge->name,
'description' => $badge->description,
'badgeurl' => $badge->badgeurl,
'issuername' => $badge->issuername,
'issuerurl' => $badge->issuerurl,
'issuercontact' => $badge->issuercontact,
Expand All @@ -141,6 +139,9 @@ public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $p
'dateexpire' => $badge->dateexpire,
);
}

$exporter = new user_badge_exporter($badge, array('context' => $context));
$result['badges'][] = $exporter->export($PAGE->get_renderer('core'));
}

return $result;
Expand All @@ -156,34 +157,7 @@ public static function get_user_badges_returns() {
return new external_single_structure(
array(
'badges' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Badge id.', VALUE_OPTIONAL),
'name' => new external_value(PARAM_TEXT, 'Badge name.'),
'description' => new external_value(PARAM_NOTAGS, 'Badge description.'),
'badgeurl' => new external_value(PARAM_URL, 'Badge URL.'),
'timecreated' => new external_value(PARAM_INT, 'Time created.', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'Time modified.', VALUE_OPTIONAL),
'usercreated' => new external_value(PARAM_INT, 'User created.', VALUE_OPTIONAL),
'usermodified' => new external_value(PARAM_INT, 'User modified.', VALUE_OPTIONAL),
'issuername' => new external_value(PARAM_NOTAGS, 'Issuer name.'),
'issuerurl' => new external_value(PARAM_URL, 'Issuer URL.'),
'issuercontact' => new external_value(PARAM_RAW, 'Issuer contact.'),
'expiredate' => new external_value(PARAM_INT, 'Expire date.', VALUE_OPTIONAL),
'expireperiod' => new external_value(PARAM_INT, 'Expire period.', VALUE_OPTIONAL),
'type' => new external_value(PARAM_INT, 'Type.', VALUE_OPTIONAL),
'courseid' => new external_value(PARAM_INT, 'Course id.', VALUE_OPTIONAL),
'message' => new external_value(PARAM_RAW, 'Message.', VALUE_OPTIONAL),
'messagesubject' => new external_value(PARAM_TEXT, 'Message subject.', VALUE_OPTIONAL),
'attachment' => new external_value(PARAM_INT, 'Attachment.', VALUE_OPTIONAL),
'status' => new external_value(PARAM_INT, 'Status.', VALUE_OPTIONAL),
'issuedid' => new external_value(PARAM_INT, 'Issued id.', VALUE_OPTIONAL),
'uniquehash' => new external_value(PARAM_ALPHANUM, 'Unique hash.'),
'dateissued' => new external_value(PARAM_INT, 'Date issued.'),
'dateexpire' => new external_value(PARAM_INT, 'Date expire.'),
'visible' => new external_value(PARAM_INT, 'Visible.', VALUE_OPTIONAL),
)
)
user_badge_exporter::get_read_structure()
),
'warnings' => new external_warnings(),
)
Expand Down
230 changes: 230 additions & 0 deletions badges/classes/external/user_badge_exporter.php
@@ -0,0 +1,230 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Contains user badge class for displaying a badge issued to a user.
*
* @package core_badges
* @copyright 2018 Dani Palou <dani@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace core_badges\external;

defined('MOODLE_INTERNAL') || die();

use core\external\exporter;
use renderer_base;
use moodle_url;

/**
* Class for displaying a badge issued to a user.
*
* @package core_badges
* @copyright 2018 Dani Palou <dani@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_badge_exporter extends exporter {

/**
* Return the list of properties.
*
* @return array
*/
protected static function define_properties() {
return [
'id' => [
'type' => PARAM_INT,
'description' => 'Badge id',
'optional' => true,
],
'name' => [
'type' => PARAM_TEXT,
'description' => 'Badge name',
],
'description' => [
'type' => PARAM_NOTAGS,
'description' => 'Badge description',
'null' => NULL_ALLOWED,
],
'timecreated' => [
'type' => PARAM_INT,
'description' => 'Time created',
'optional' => true,
'default' => 0,
],
'timemodified' => [
'type' => PARAM_INT,
'description' => 'Time modified',
'optional' => true,
'default' => 0,
],
'usercreated' => [
'type' => PARAM_INT,
'description' => 'User created',
'optional' => true,
],
'usermodified' => [
'type' => PARAM_INT,
'description' => 'User modified',
'optional' => true,
],
'issuername' => [
'type' => PARAM_NOTAGS,
'description' => 'Issuer name',
],
'issuerurl' => [
'type' => PARAM_URL,
'description' => 'Issuer URL',
],
'issuercontact' => [
'type' => PARAM_RAW,
'description' => 'Issuer contact',
'null' => NULL_ALLOWED,
],
'expiredate' => [
'type' => PARAM_INT,
'description' => 'Expire date',
'optional' => true,
'null' => NULL_ALLOWED,
],
'expireperiod' => [
'type' => PARAM_INT,
'description' => 'Expire period',
'optional' => true,
'null' => NULL_ALLOWED,
],
'type' => [
'type' => PARAM_INT,
'description' => 'Type',
'optional' => true,
'default' => 1,
],
'courseid' => [
'type' => PARAM_INT,
'description' => 'Course id',
'optional' => true,
'null' => NULL_ALLOWED,
],
'message' => [
'type' => PARAM_RAW,
'description' => 'Message',
'optional' => true,
],
'messagesubject' => [
'type' => PARAM_TEXT,
'description' => 'Message subject',
'optional' => true,
],
'attachment' => [
'type' => PARAM_INT,
'description' => 'Attachment',
'optional' => true,
'default' => 1,
],
'notification' => [
'type' => PARAM_INT,
'description' => 'Whether to notify when badge is awarded',
'optional' => true,
'default' => 1,
],
'nextcron' => [
'type' => PARAM_INT,
'description' => 'Next cron',
'optional' => true,
'null' => NULL_ALLOWED,
],
'status' => [
'type' => PARAM_INT,
'description' => 'Status',
'optional' => true,
'default' => 0,
],
'issuedid' => [
'type' => PARAM_INT,
'description' => 'Issued id',
'optional' => true,
],
'uniquehash' => [
'type' => PARAM_ALPHANUM,
'description' => 'Unique hash',
],
'dateissued' => [
'type' => PARAM_INT,
'description' => 'Date issued',
'default' => 0,
],
'dateexpire' => [
'type' => PARAM_INT,
'description' => 'Date expire',
'null' => NULL_ALLOWED,
],
'visible' => [
'type' => PARAM_INT,
'description' => 'Visible',
'optional' => true,
'default' => 0,
],
'email' => [
'type' => PARAM_TEXT,
'description' => 'User email',
'optional' => true,
],
];
}

/**
* Returns a list of objects that are related.
*
* @return array
*/
protected static function define_related() {
return array(
'context' => 'context'
);
}

/**
* Return the list of additional properties.
*
* @return array
*/
protected static function define_other_properties() {
return [
'badgeurl' => [
'type' => PARAM_URL,
'description' => 'Badge URL',
],
];
}

/**
* Get the additional values to inject while exporting.
*
* @param renderer_base $output The renderer.
* @return array Keys are the property names, values are their values.
*/
protected function get_other_values(renderer_base $output) {
$context = $this->related['context'];

$values = array(
'badgeurl' => moodle_url::make_webservice_pluginfile_url($context->id, 'badges', 'badgeimage', $this->data->id, '/',
'f1')->out(false),
);

return $values;
}
}

0 comments on commit c776e1d

Please sign in to comment.