[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/forum/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   * Event observers used in forum.
  19   *
  20   * @package    mod_forum
  21   * @copyright  2013 Rajesh Taneja <[email protected]>
  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 mod_forum.
  29   */
  30  class mod_forum_observer {
  31  
  32      /**
  33       * Triggered via user_enrolment_deleted event.
  34       *
  35       * @param \core\event\user_enrolment_deleted $event
  36       */
  37      public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
  38          global $DB;
  39  
  40          // NOTE: this has to be as fast as possible.
  41          // Get user enrolment info from event.
  42          $cp = (object)$event->other['userenrolment'];
  43          if ($cp->lastenrol) {
  44              $params = array('userid' => $cp->userid, 'courseid' => $cp->courseid);
  45              $forumselect = "IN (SELECT f.id FROM {forum} f WHERE f.course = :courseid)";
  46  
  47              $DB->delete_records_select('forum_digests', 'userid = :userid AND forum '.$forumselect, $params);
  48              $DB->delete_records_select('forum_subscriptions', 'userid = :userid AND forum '.$forumselect, $params);
  49              $DB->delete_records_select('forum_track_prefs', 'userid = :userid AND forumid '.$forumselect, $params);
  50              $DB->delete_records_select('forum_read', 'userid = :userid AND forumid '.$forumselect, $params);
  51          }
  52      }
  53  
  54      /**
  55       * Observer for role_assigned event.
  56       *
  57       * @param \core\event\role_assigned $event
  58       * @return void
  59       */
  60      public static function role_assigned(\core\event\role_assigned $event) {
  61          global $CFG, $DB;
  62  
  63          $context = context::instance_by_id($event->contextid, MUST_EXIST);
  64  
  65          // If contextlevel is course then only subscribe user. Role assignment
  66          // at course level means user is enroled in course and can subscribe to forum.
  67          if ($context->contextlevel != CONTEXT_COURSE) {
  68              return;
  69          }
  70  
  71          // Forum lib required for the constant used below.
  72          require_once($CFG->dirroot . '/mod/forum/lib.php');
  73  
  74          $userid = $event->relateduserid;
  75          $sql = "SELECT f.id, f.course as course, cm.id AS cmid, f.forcesubscribe
  76                    FROM {forum} f
  77                    JOIN {course_modules} cm ON (cm.instance = f.id)
  78                    JOIN {modules} m ON (m.id = cm.module)
  79               LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
  80                   WHERE f.course = :courseid
  81                     AND f.forcesubscribe = :initial
  82                     AND m.name = 'forum'
  83                     AND fs.id IS NULL";
  84          $params = array('courseid' => $context->instanceid, 'userid' => $userid, 'initial' => FORUM_INITIALSUBSCRIBE);
  85  
  86          $forums = $DB->get_records_sql($sql, $params);
  87          foreach ($forums as $forum) {
  88              // If user doesn't have allowforcesubscribe capability then don't subscribe.
  89              $modcontext = context_module::instance($forum->cmid);
  90              if (has_capability('mod/forum:allowforcesubscribe', $modcontext, $userid)) {
  91                  \mod_forum\subscriptions::subscribe_user($userid, $forum, $modcontext);
  92              }
  93          }
  94      }
  95  
  96      /**
  97       * Observer for \core\event\course_module_created event.
  98       *
  99       * @param \core\event\course_module_created $event
 100       * @return void
 101       */
 102      public static function course_module_created(\core\event\course_module_created $event) {
 103          global $CFG;
 104  
 105          if ($event->other['modulename'] === 'forum') {
 106              // Include the forum library to make use of the forum_instance_created function.
 107              require_once($CFG->dirroot . '/mod/forum/lib.php');
 108  
 109              $forum = $event->get_record_snapshot('forum', $event->other['instanceid']);
 110              forum_instance_created($event->get_context(), $forum);
 111          }
 112      }
 113  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1