Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Local stuff for category enrolment plugin.
  19   *
  20   * @package    core_badges
  21   * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use \core_badges\badge;
  28  /**
  29   * Event observer for badges.
  30   */
  31  class core_badges_observer {
  32      /**
  33       * Triggered when 'course_module_completion_updated' event is triggered.
  34       *
  35       * @param \core\event\course_module_completion_updated $event
  36       */
  37      public static function course_module_criteria_review(\core\event\course_module_completion_updated $event) {
  38          global $DB, $CFG;
  39  
  40          if (!empty($CFG->enablebadges)) {
  41              require_once($CFG->dirroot.'/lib/badgeslib.php');
  42  
  43              $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
  44              $userid = $event->relateduserid;
  45              $mod = $event->contextinstanceid;
  46  
  47              if ($eventdata->completionstate == COMPLETION_COMPLETE
  48                  || $eventdata->completionstate == COMPLETION_COMPLETE_PASS
  49                  || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) {
  50                  // Need to take into account that there can be more than one badge with the same activity in its criteria.
  51                  if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) {
  52                      foreach ($rs as $r) {
  53                          $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST);
  54                          $badge = new badge($bid);
  55                          if (!$badge->is_active() || $badge->is_issued($userid)) {
  56                              continue;
  57                          }
  58  
  59                          if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) {
  60                              $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid);
  61  
  62                              if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
  63                                  $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
  64                                  $badge->issue($userid);
  65                              }
  66                          }
  67                      }
  68                  }
  69              }
  70          }
  71      }
  72  
  73      /**
  74       * Triggered when '\core\event\competency_evidence_created' event is triggered.
  75       *
  76       * @param \core\event\competency_evidence_created $event
  77       */
  78      public static function competency_criteria_review(\core\event\competency_evidence_created $event) {
  79          global $DB, $CFG;
  80  
  81          if (!empty($CFG->enablebadges)) {
  82              require_once($CFG->dirroot.'/lib/badgeslib.php');
  83  
  84              if (!get_config('core_competency', 'enabled')) {
  85                  return;
  86              }
  87  
  88              $ucid = $event->other['usercompetencyid'];
  89              $cid = $event->other['competencyid'];
  90              $userid = $event->relateduserid;
  91  
  92              if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'competency_' . $cid, 'value' => $cid))) {
  93                  foreach ($rs as $r) {
  94                      $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
  95                      $badge = new badge($crit->badgeid);
  96                      // Only site badges are updated from site competencies.
  97                      if (!$badge->is_active() || $badge->is_issued($userid)) {
  98                          continue;
  99                      }
 100  
 101                      if ($badge->criteria[$crit->criteriatype]->review($userid)) {
 102                          $badge->criteria[$crit->criteriatype]->mark_complete($userid);
 103  
 104                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 105                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 106                              $badge->issue($userid);
 107                          }
 108                      }
 109                  }
 110              }
 111          }
 112      }
 113  
 114      /**
 115       * Triggered when 'course_completed' event is triggered.
 116       *
 117       * @param \core\event\course_completed $event
 118       */
 119      public static function course_criteria_review(\core\event\course_completed $event) {
 120          global $DB, $CFG;
 121  
 122          if (!empty($CFG->enablebadges)) {
 123              require_once($CFG->dirroot.'/lib/badgeslib.php');
 124  
 125              $userid = $event->relateduserid;
 126              $courseid = $event->courseid;
 127  
 128              // Need to take into account that course can be a part of course_completion and courseset_completion criteria.
 129              if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
 130                  foreach ($rs as $r) {
 131                      $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
 132                      $badge = new badge($crit->badgeid);
 133                      if (!$badge->is_active() || $badge->is_issued($userid)) {
 134                          continue;
 135                      }
 136  
 137                      if ($badge->criteria[$crit->criteriatype]->review($userid)) {
 138                          $badge->criteria[$crit->criteriatype]->mark_complete($userid);
 139  
 140                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 141                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 142                              $badge->issue($userid);
 143                          }
 144                      }
 145                  }
 146              }
 147          }
 148      }
 149  
 150      /**
 151       * Triggered when 'badge_awarded' event happens.
 152       *
 153       * @param \core\event\badge_awarded $event event generated when a badge is awarded.
 154       */
 155      public static function badge_criteria_review(\core\event\badge_awarded $event) {
 156          global $DB, $CFG;
 157  
 158          if (!empty($CFG->enablebadges)) {
 159              require_once($CFG->dirroot.'/lib/badgeslib.php');
 160              $userid = $event->relateduserid;
 161  
 162              if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_BADGE))) {
 163                  foreach ($rs as $r) {
 164                      $badge = new badge($r->badgeid);
 165                      if (!$badge->is_active() || $badge->is_issued($userid)) {
 166                          continue;
 167                      }
 168  
 169                      if ($badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->review($userid)) {
 170                          $badge->criteria[BADGE_CRITERIA_TYPE_BADGE]->mark_complete($userid);
 171  
 172                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 173                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 174                              $badge->issue($userid);
 175                          }
 176                      }
 177                  }
 178              }
 179          }
 180      }
 181      /**
 182       * Triggered when 'user_updated' event happens.
 183       *
 184       * @param \core\event\user_updated $event event generated when user profile is updated.
 185       */
 186      public static function profile_criteria_review(\core\event\user_updated $event) {
 187          global $DB, $CFG;
 188  
 189          if (!empty($CFG->enablebadges)) {
 190              require_once($CFG->dirroot.'/lib/badgeslib.php');
 191              $userid = $event->objectid;
 192  
 193              if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
 194                  foreach ($rs as $r) {
 195                      $badge = new badge($r->badgeid);
 196                      if (!$badge->is_active() || $badge->is_issued($userid)) {
 197                          continue;
 198                      }
 199  
 200                      if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
 201                          $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
 202  
 203                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 204                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 205                              $badge->issue($userid);
 206                          }
 207                      }
 208                  }
 209              }
 210          }
 211      }
 212  
 213      /**
 214       * Triggered when the 'cohort_member_added' event happens.
 215       *
 216       * @param \core\event\cohort_member_added $event generated when a user is added to a cohort
 217       */
 218      public static function cohort_criteria_review(\core\event\cohort_member_added $event) {
 219          global $DB, $CFG;
 220  
 221          if (!empty($CFG->enablebadges)) {
 222              require_once($CFG->dirroot.'/lib/badgeslib.php');
 223              $cohortid = $event->objectid;
 224              $userid = $event->relateduserid;
 225  
 226              // Get relevant badges.
 227              $badgesql = "SELECT badgeid
 228                  FROM {badge_criteria_param} cp
 229                  JOIN {badge_criteria} c ON cp.critid = c.id
 230                  WHERE c.criteriatype = ?
 231                  AND cp.name = ?";
 232              $badges = $DB->get_records_sql($badgesql, array(BADGE_CRITERIA_TYPE_COHORT, "cohort_{$cohortid}"));
 233              if (empty($badges)) {
 234                  return;
 235              }
 236  
 237              foreach ($badges as $b) {
 238                  $badge = new badge($b->badgeid);
 239                  if (!$badge->is_active()) {
 240                      continue;
 241                  }
 242                  if ($badge->is_issued($userid)) {
 243                      continue;
 244                  }
 245  
 246                  if ($badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->review($userid)) {
 247                      $badge->criteria[BADGE_CRITERIA_TYPE_COHORT]->mark_complete($userid);
 248  
 249                      if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 250                          $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 251                          $badge->issue($userid);
 252                      }
 253                  }
 254              }
 255          }
 256      }
 257  }