Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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              $context = ($badge->type == BADGE_TYPE_SITE) ? context_system::instance() : context_course::instance($badge->courseid);
 132              $canconfiguredetails = has_capability('moodle/badges:configuredetails', $context);
 133  
 134              // If the user is viewing another user's badge and doesn't have the right capability return only part of the data.
 135              if ($USER->id != $user->id and !$canconfiguredetails) {
 136                  $badge = (object) array(
 137                      'id' => $badge->id,
 138                      'name' => $badge->name,
 139                      'description' => $badge->description,
 140                      'issuername' => $badge->issuername,
 141                      'issuerurl' => $badge->issuerurl,
 142                      'issuercontact' => $badge->issuercontact,
 143                      'uniquehash' => $badge->uniquehash,
 144                      'dateissued' => $badge->dateissued,
 145                      'dateexpire' => $badge->dateexpire,
 146                      'version' => $badge->version,
 147                      'language' => $badge->language,
 148                      'imageauthorname' => $badge->imageauthorname,
 149                      'imageauthoremail' => $badge->imageauthoremail,
 150                      'imageauthorurl' => $badge->imageauthorurl,
 151                      'imagecaption' => $badge->imagecaption,
 152                  );
 153              }
 154  
 155              // Create a badge instance to be able to get the endorsement and other info.
 156              $badgeinstance = new badge($badge->id);
 157              $endorsement = $badgeinstance->get_endorsement();
 158              $alignments = $badgeinstance->get_alignments();
 159              $relatedbadges = $badgeinstance->get_related_badges();
 160  
 161              if (!$canconfiguredetails) {
 162                  // Return only the properties visible by the user.
 163  
 164                  if (!empty($alignments)) {
 165                      foreach ($alignments as $alignment) {
 166                          unset($alignment->targetdescription);
 167                          unset($alignment->targetframework);
 168                          unset($alignment->targetcode);
 169                      }
 170                  }
 171  
 172                  if (!empty($relatedbadges)) {
 173                      foreach ($relatedbadges as $relatedbadge) {
 174                          unset($relatedbadge->version);
 175                          unset($relatedbadge->language);
 176                          unset($relatedbadge->type);
 177                      }
 178                  }
 179              }
 180  
 181              $related = array(
 182                  'context' => $context,
 183                  'endorsement' => $endorsement ? $endorsement : null,
 184                  'alignment' => $alignments,
 185                  'relatedbadges' => $relatedbadges,
 186              );
 187  
 188              $exporter = new user_badge_exporter($badge, $related);
 189              $result['badges'][] = $exporter->export($PAGE->get_renderer('core'));
 190          }
 191  
 192          return $result;
 193      }
 194  
 195      /**
 196       * Describes the get_user_badges return value.
 197       *
 198       * @return external_single_structure
 199       * @since Moodle 3.1
 200       */
 201      public static function get_user_badges_returns() {
 202          return new external_single_structure(
 203              array(
 204                  'badges' => new external_multiple_structure(
 205                      user_badge_exporter::get_read_structure()
 206                  ),
 207                  'warnings' => new external_warnings(),
 208              )
 209          );
 210      }
 211  }