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.
   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   * Badge listing viewed event.
  19   *
  20   * @package    core
  21   * @copyright  2016 Stephen Bourget
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\event;
  26  defined('MOODLE_INTERNAL') || die();
  27  require_once($CFG->libdir . '/badgeslib.php');
  28  /**
  29   * Event triggered after a badge is viewed.
  30   *
  31   * @property-read array $other {
  32   *      Extra information about the event.
  33   *
  34   *      - int badgetype: the type of badge (BADGE_TYPE_SITE or BADGE_TYPE_COURSE).
  35   *      - int courseid: The ID of the course (Not required for BADGE_TYPE_SITE).
  36   * }
  37   *
  38   * @package    core
  39   * @since      Moodle 3.2
  40   * @copyright  2016 Stephen Bourget
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class badge_listing_viewed extends base {
  44  
  45      /**
  46       * Set basic properties for the event.
  47       */
  48      protected function init() {
  49          $this->data['crud'] = 'r';
  50          $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
  51      }
  52  
  53      /**
  54       * Returns localised general event name.
  55       *
  56       * @return string
  57       */
  58      public static function get_name() {
  59          return get_string('eventbadgelistingviewed', 'badges');
  60      }
  61  
  62      /**
  63       * Returns non-localised event description with id's for admin use only.
  64       *
  65       * @return string
  66       */
  67      public function get_description() {
  68          if ($this->other['badgetype'] == BADGE_TYPE_SITE) {
  69              $return = "The user with id '$this->userid' has viewed the list of available badges for this site.";
  70          } else {
  71              $return = "The user with id '$this->userid' has viewed the list of available badges".
  72                      " for the course with the id '".$this->other['courseid']."'.";
  73          }
  74          return $return;
  75      }
  76  
  77      /**
  78       * Returns relevant URL.
  79       * @return \moodle_url
  80       */
  81      public function get_url() {
  82          if ($this->other['badgetype'] == BADGE_TYPE_SITE) {
  83              $params = array('type' => $this->other['badgetype']);
  84          } else {
  85              $params = array('id' => $this->other['courseid'], 'type' => $this->other['badgetype']);
  86          }
  87          return new \moodle_url('/badges/view.php', $params );
  88      }
  89  
  90      /**
  91       * Custom validations.
  92       *
  93       * @throws \coding_exception
  94       * @return void
  95       */
  96      protected function validate_data() {
  97          parent::validate_data();
  98  
  99          if (!isset($this->other['badgetype'])) {
 100              throw new \coding_exception('The \'badgetype\' must be set in other.');
 101          }
 102          if ($this->other['badgetype'] == BADGE_TYPE_COURSE) {
 103              if (!isset($this->other['courseid'])) {
 104                  throw new \coding_exception('The \'courseid\' must be set in other.');
 105              }
 106          }
 107      }
 108  
 109      /**
 110       * Used for maping events on restore
 111       *
 112       * @return bool
 113       */
 114      public static function get_other_mapping() {
 115          $othermapped = array();
 116          $othermapped['courseid'] = array('db' => 'course', 'restore' => 'course');
 117          return $othermapped;
 118      }
 119  }