[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/badges/classes/ -> observer.php (source)

   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  /**
  28   * Event observer for badges.
  29   */
  30  class core_badges_observer {
  31      /**
  32       * Triggered when 'course_module_completion_updated' event is triggered.
  33       *
  34       * @param \core\event\course_module_completion_updated $event
  35       */
  36      public static function course_module_criteria_review(\core\event\course_module_completion_updated $event) {
  37          global $DB, $CFG;
  38  
  39          if (!empty($CFG->enablebadges)) {
  40              require_once($CFG->dirroot.'/lib/badgeslib.php');
  41  
  42              $eventdata = $event->get_record_snapshot('course_modules_completion', $event->objectid);
  43              $userid = $event->relateduserid;
  44              $mod = $event->contextinstanceid;
  45  
  46              if ($eventdata->completionstate == COMPLETION_COMPLETE
  47                  || $eventdata->completionstate == COMPLETION_COMPLETE_PASS
  48                  || $eventdata->completionstate == COMPLETION_COMPLETE_FAIL) {
  49                  // Need to take into account that there can be more than one badge with the same activity in its criteria.
  50                  if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'module_' . $mod, 'value' => $mod))) {
  51                      foreach ($rs as $r) {
  52                          $bid = $DB->get_field('badge_criteria', 'badgeid', array('id' => $r->critid), MUST_EXIST);
  53                          $badge = new badge($bid);
  54                          if (!$badge->is_active() || $badge->is_issued($userid)) {
  55                              continue;
  56                          }
  57  
  58                          if ($badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->review($userid)) {
  59                              $badge->criteria[BADGE_CRITERIA_TYPE_ACTIVITY]->mark_complete($userid);
  60  
  61                              if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
  62                                  $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
  63                                  $badge->issue($userid);
  64                              }
  65                          }
  66                      }
  67                  }
  68              }
  69          }
  70      }
  71  
  72      /**
  73       * Triggered when 'course_completed' event is triggered.
  74       *
  75       * @param \core\event\course_completed $event
  76       */
  77      public static function course_criteria_review(\core\event\course_completed $event) {
  78          global $DB, $CFG;
  79  
  80          if (!empty($CFG->enablebadges)) {
  81              require_once($CFG->dirroot.'/lib/badgeslib.php');
  82  
  83              $eventdata = $event->get_record_snapshot('course_completions', $event->objectid);
  84              $userid = $event->relateduserid;
  85              $courseid = $event->courseid;
  86  
  87              // Need to take into account that course can be a part of course_completion and courseset_completion criteria.
  88              if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
  89                  foreach ($rs as $r) {
  90                      $crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
  91                      $badge = new badge($crit->badgeid);
  92                      if (!$badge->is_active() || $badge->is_issued($userid)) {
  93                          continue;
  94                      }
  95  
  96                      if ($badge->criteria[$crit->criteriatype]->review($userid)) {
  97                          $badge->criteria[$crit->criteriatype]->mark_complete($userid);
  98  
  99                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 100                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 101                              $badge->issue($userid);
 102                          }
 103                      }
 104                  }
 105              }
 106          }
 107      }
 108  
 109      /**
 110       * Triggered when 'user_updated' event happens.
 111       *
 112       * @param \core\event\user_updated $event event generated when user profile is updated.
 113       */
 114      public static function profile_criteria_review(\core\event\user_updated $event) {
 115          global $DB, $CFG;
 116  
 117          if (!empty($CFG->enablebadges)) {
 118              require_once($CFG->dirroot.'/lib/badgeslib.php');
 119              $userid = $event->objectid;
 120  
 121              if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
 122                  foreach ($rs as $r) {
 123                      $badge = new badge($r->badgeid);
 124                      if (!$badge->is_active() || $badge->is_issued($userid)) {
 125                          continue;
 126                      }
 127  
 128                      if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
 129                          $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
 130  
 131                          if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
 132                              $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
 133                              $badge->issue($userid);
 134                          }
 135                      }
 136                  }
 137              }
 138          }
 139      }
 140  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1