Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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 . '/externallib.php');
  30  require_once($CFG->libdir . '/badgeslib.php');
  31  
  32  use core_badges\external\user_badge_exporter;
  33  
  34  /**
  35   * Badges external functions
  36   *
  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
  42   */
  43  class core_badges_external extends external_api {
  44  
  45      /**
  46       * Describes the parameters for get_user_badges.
  47       *
  48       * @return external_function_parameters
  49       * @since Moodle 3.1
  50       */
  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),
  60              )
  61          );
  62      }
  63  
  64      /**
  65       * Returns the list of badges awarded to a user.
  66       *
  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
  76       */
  77      public static function get_user_badges($userid = 0, $courseid = 0, $page = 0, $perpage = 0, $search = '', $onlypublic = false) {
  78          global $CFG, $USER, $PAGE;
  79  
  80          $warnings = array();
  81  
  82          $params = array(
  83              'userid' => $userid,
  84              'courseid' => $courseid,
  85              'page' => $page,
  86              'perpage' => $perpage,
  87              'search' => $search,
  88              'onlypublic' => $onlypublic,
  89          );
  90          $params = self::validate_parameters(self::get_user_badges_parameters(), $params);
  91  
  92          if (empty($CFG->enablebadges)) {
  93              throw new moodle_exception('badgesdisabled', 'badges');
  94          }
  95  
  96          if (empty($CFG->badges_allowcoursebadges) && $params['courseid'] != 0) {
  97              throw new moodle_exception('coursebadgesdisabled', 'badges');
  98          }
  99  
 100          // Default value for userid.
 101          if (empty($params['userid'])) {
 102              $params['userid'] = $USER->id;
 103          }
 104  
 105          // Validate the user.
 106          $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
 107          core_user::require_active_user($user);
 108  
 109          $usercontext = context_user::instance($user->id);
 110          self::validate_context($usercontext);
 111  
 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;
 116          }
 117  
 118          $userbadges = badges_get_user_badges($user->id, $params['courseid'], $params['page'], $params['perpage'], $params['search'],
 119                                                  $params['onlypublic']);
 120  
 121          $result = array();
 122          $result['badges'] = array();
 123          $result['warnings'] = $warnings;
 124  
 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);
 128  
 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,
 147                  );
 148              }
 149  
 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();
 155  
 156              if (!$canconfiguredetails) {
 157                  // Return only the properties visible by the user.
 158  
 159                  if (!empty($alignments)) {
 160                      foreach ($alignments as $alignment) {
 161                          unset($alignment->targetdescription);
 162                          unset($alignment->targetframework);
 163                          unset($alignment->targetcode);
 164                      }
 165                  }
 166  
 167                  if (!empty($relatedbadges)) {
 168                      foreach ($relatedbadges as $relatedbadge) {
 169                          unset($relatedbadge->version);
 170                          unset($relatedbadge->language);
 171                          unset($relatedbadge->type);
 172                      }
 173                  }
 174              }
 175  
 176              $related = array(
 177                  'context' => $context,
 178                  'endorsement' => $endorsement ? $endorsement : null,
 179                  'alignment' => $alignments,
 180                  'relatedbadges' => $relatedbadges,
 181              );
 182  
 183              $exporter = new user_badge_exporter($badge, $related);
 184              $result['badges'][] = $exporter->export($PAGE->get_renderer('core'));
 185          }
 186  
 187          return $result;
 188      }
 189  
 190      /**
 191       * Describes the get_user_badges return value.
 192       *
 193       * @return external_single_structure
 194       * @since Moodle 3.1
 195       */
 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()
 201                  ),
 202                  'warnings' => new external_warnings(),
 203              )
 204          );
 205      }
 206  }