[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/tool/monitor/classes/ -> subscription_manager.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   * Class to manage subscriptions.
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Ankit Agarwal <[email protected]>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_monitor;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class to manage subscriptions.
  31   *
  32   * @since      Moodle 2.8
  33   * @package    tool_monitor
  34   * @copyright  2014 onwards Ankit Agarwal <[email protected]>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class subscription_manager {
  38      /**
  39       * Subscribe a user to a given rule.
  40       *
  41       * @param int $ruleid  Rule id.
  42       * @param int $courseid Course id.
  43       * @param int $cmid Course module id.
  44       * @param int $userid User who is subscribing, defaults to $USER.
  45       *
  46       * @return bool|int returns id of the created subscription.
  47       */
  48      public static function create_subscription($ruleid, $courseid, $cmid, $userid = 0) {
  49          global $DB, $USER;
  50  
  51          $subscription = new \stdClass();
  52          $subscription->ruleid = $ruleid;
  53          $subscription->courseid = $courseid;
  54          $subscription->cmid = $cmid;
  55          $subscription->userid = empty($userid) ? $USER->id : $userid;
  56          if ($DB->record_exists('tool_monitor_subscriptions', (array)$subscription)) {
  57              // Subscription already exists.
  58              return false;
  59          }
  60  
  61          $subscription->timecreated = time();
  62          $subscription->id = $DB->insert_record('tool_monitor_subscriptions', $subscription);
  63  
  64          // Trigger a subscription created event.
  65          if ($subscription->id) {
  66              if (!empty($subscription->courseid)) {
  67                  $courseid = $subscription->courseid;
  68                  $context = \context_course::instance($subscription->courseid);
  69              } else {
  70                  $courseid = 0;
  71                  $context = \context_system::instance();
  72              }
  73  
  74              $params = array(
  75                  'objectid' => $subscription->id,
  76                  'courseid' => $courseid,
  77                  'context' => $context
  78              );
  79              $event = \tool_monitor\event\subscription_created::create($params);
  80              $event->trigger();
  81  
  82              // Let's invalidate the cache.
  83              $cache = \cache::make('tool_monitor', 'eventsubscriptions');
  84              $cache->delete($courseid);
  85          }
  86  
  87          return $subscription->id;
  88      }
  89  
  90      /**
  91       * Delete a subscription.
  92       *
  93       * @param subscription|int $subscriptionorid an instance of subscription class or id.
  94       * @param bool $checkuser Check if the subscription belongs to current user before deleting.
  95       *
  96       * @return bool
  97       * @throws \coding_exception if $checkuser is true and the subscription doesn't belong to the current user.
  98       */
  99      public static function delete_subscription($subscriptionorid, $checkuser = true) {
 100          global $DB, $USER;
 101          if (is_object($subscriptionorid)) {
 102              $subscription = $subscriptionorid;
 103          } else {
 104              $subscription = self::get_subscription($subscriptionorid);
 105          }
 106          if ($checkuser && $subscription->userid != $USER->id) {
 107              throw new \coding_exception('Invalid subscription supplied');
 108          }
 109  
 110          // Store the subscription before we delete it.
 111          $subscription = $DB->get_record('tool_monitor_subscriptions', array('id' => $subscription->id));
 112  
 113          $success = $DB->delete_records('tool_monitor_subscriptions', array('id' => $subscription->id));
 114  
 115          // If successful trigger a subscription_deleted event.
 116          if ($success) {
 117              if (!empty($subscription->courseid)) {
 118                  $courseid = $subscription->courseid;
 119                  $context = \context_course::instance($subscription->courseid);
 120              } else {
 121                  $courseid = 0;
 122                  $context = \context_system::instance();
 123              }
 124  
 125              $params = array(
 126                  'objectid' => $subscription->id,
 127                  'courseid' => $courseid,
 128                  'context' => $context
 129              );
 130              $event = \tool_monitor\event\subscription_deleted::create($params);
 131              $event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
 132              $event->trigger();
 133  
 134              // Let's invalidate the cache.
 135              $cache = \cache::make('tool_monitor', 'eventsubscriptions');
 136              $cache->delete($courseid);
 137          }
 138  
 139          return $success;
 140      }
 141  
 142      /**
 143       * Delete all subscriptions for a user.
 144       *
 145       * @param int $userid user id.
 146       *
 147       * @return mixed
 148       */
 149      public static function delete_user_subscriptions($userid) {
 150          global $DB;
 151          return $DB->delete_records('tool_monitor_subscriptions', array('userid' => $userid));
 152      }
 153  
 154      /**
 155       * Delete all subscriptions for a course module.
 156       *
 157       * @param int $cmid cm id.
 158       *
 159       * @return mixed
 160       */
 161      public static function delete_cm_subscriptions($cmid) {
 162          global $DB;
 163          return $DB->delete_records('tool_monitor_subscriptions', array('cmid' => $cmid));
 164      }
 165  
 166      /**
 167       * Delete all subscribers for a given rule.
 168       *
 169       * @param int $ruleid rule id.
 170       * @param \context|null $coursecontext the context of the course - this is passed when we
 171       *      can not get the context via \context_course as the course has been deleted.
 172       *
 173       * @return bool
 174       */
 175      public static function remove_all_subscriptions_for_rule($ruleid, $coursecontext = null) {
 176          global $DB;
 177  
 178          // Store all the subscriptions we have to delete.
 179          $subscriptions = $DB->get_recordset('tool_monitor_subscriptions', array('ruleid' => $ruleid));
 180  
 181          // Now delete them.
 182          $success = $DB->delete_records('tool_monitor_subscriptions', array('ruleid' => $ruleid));
 183  
 184          // If successful and there were subscriptions that were deleted trigger a subscription deleted event.
 185          if ($success && $subscriptions) {
 186              foreach ($subscriptions as $subscription) {
 187                  // It is possible that we are deleting rules associated with a deleted course, so we should be
 188                  // passing the context as the second parameter.
 189                  if (!is_null($coursecontext)) {
 190                      $context = $coursecontext;
 191                      $courseid = $subscription->courseid;
 192                  } else if (!empty($subscription->courseid) && ($coursecontext =
 193                          \context_course::instance($subscription->courseid, IGNORE_MISSING))) {
 194                      $courseid = $subscription->courseid;
 195                      $context = $coursecontext;
 196                  } else {
 197                      $courseid = 0;
 198                      $context = \context_system::instance();
 199                  }
 200  
 201                  $params = array(
 202                      'objectid' => $subscription->id,
 203                      'courseid' => $courseid,
 204                      'context' => $context
 205                  );
 206                  $event = \tool_monitor\event\subscription_deleted::create($params);
 207                  $event->add_record_snapshot('tool_monitor_subscriptions', $subscription);
 208                  $event->trigger();
 209  
 210                  // Let's invalidate the cache.
 211                  $cache = \cache::make('tool_monitor', 'eventsubscriptions');
 212                  $cache->delete($courseid);
 213              }
 214          }
 215  
 216          $subscriptions->close();
 217  
 218          return $success;
 219      }
 220  
 221      /**
 222       * Get a subscription instance for an given subscription id.
 223       *
 224       * @param subscription|int $subscriptionorid an instance of subscription class or id.
 225       *
 226       * @return subscription returns a instance of subscription class.
 227       */
 228      public static function get_subscription($subscriptionorid) {
 229          global $DB;
 230  
 231          if (is_object($subscriptionorid)) {
 232              return new subscription($subscriptionorid);
 233          }
 234  
 235          $sql = self::get_subscription_join_rule_sql();
 236          $sql .= "WHERE s.id = :id";
 237          $sub = $DB->get_record_sql($sql, array('id' => $subscriptionorid), MUST_EXIST);
 238          return new subscription($sub);
 239      }
 240  
 241      /**
 242       * Get an array of subscriptions for a given user in a given course.
 243       *
 244       * @param int $courseid course id.
 245       * @param int $limitfrom Limit from which to fetch rules.
 246       * @param int $limitto  Limit to which rules need to be fetched.
 247       * @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
 248       * @param string $order Order to sort the subscriptions.
 249       *
 250       * @return array list of subscriptions
 251       */
 252      public static function get_user_subscriptions_for_course($courseid, $limitfrom = 0, $limitto = 0, $userid = 0,
 253              $order = 's.timecreated DESC' ) {
 254          global $DB, $USER;
 255          if ($userid == 0) {
 256              $userid = $USER->id;
 257          }
 258          $sql = self::get_subscription_join_rule_sql();
 259          $sql .= "WHERE s.courseid = :courseid AND s.userid = :userid ORDER BY $order";
 260  
 261          return self::get_instances($DB->get_records_sql($sql, array('courseid' => $courseid, 'userid' => $userid), $limitfrom,
 262                  $limitto));
 263      }
 264  
 265      /**
 266       * Get count of subscriptions for a given user in a given course.
 267       *
 268       * @param int $courseid course id.
 269       * @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
 270       *
 271       * @return int number of subscriptions
 272       */
 273      public static function count_user_subscriptions_for_course($courseid, $userid = 0) {
 274          global $DB, $USER;
 275          if ($userid == 0) {
 276              $userid = $USER->id;
 277          }
 278          $sql = self::get_subscription_join_rule_sql(true);
 279          $sql .= "WHERE s.courseid = :courseid AND s.userid = :userid";
 280  
 281          return $DB->count_records_sql($sql, array('courseid' => $courseid, 'userid' => $userid));
 282      }
 283  
 284      /**
 285       * Get an array of subscriptions for a given user.
 286       *
 287       * @param int $limitfrom Limit from which to fetch rules.
 288       * @param int $limitto  Limit to which rules need to be fetched.
 289       * @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
 290       * @param string $order Order to sort the subscriptions.
 291       *
 292       * @return array list of subscriptions
 293       */
 294      public static function get_user_subscriptions($limitfrom = 0, $limitto = 0, $userid = 0,
 295                                                               $order = 's.courseid ASC, r.name' ) {
 296          global $DB, $USER;
 297          if ($userid == 0) {
 298              $userid = $USER->id;
 299          }
 300          $sql = self::get_subscription_join_rule_sql();
 301          $sql .= "WHERE s.userid = :userid ORDER BY $order";
 302  
 303          return self::get_instances($DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitto));
 304      }
 305  
 306      /**
 307       * Get count of subscriptions for a given user.
 308       *
 309       * @param int $userid Id of the user for which the subscription needs to be fetched. Defaults to $USER;
 310       *
 311       * @return int number of subscriptions
 312       */
 313      public static function count_user_subscriptions($userid = 0) {
 314          global $DB, $USER;;
 315          if ($userid == 0) {
 316              $userid = $USER->id;
 317          }
 318          $sql = self::get_subscription_join_rule_sql(true);
 319          $sql .= "WHERE s.userid = :userid";
 320  
 321          return $DB->count_records_sql($sql, array('userid' => $userid));
 322      }
 323  
 324      /**
 325       * Return a list of subscriptions for a given event.
 326       *
 327       * @param \stdClass $event the event object.
 328       *
 329       * @return array
 330       */
 331      public static function get_subscriptions_by_event(\stdClass $event) {
 332          global $DB;
 333  
 334          $sql = self::get_subscription_join_rule_sql();
 335          if ($event->contextlevel == CONTEXT_MODULE && $event->contextinstanceid != 0) {
 336              $sql .= "WHERE r.eventname = :eventname AND s.courseid = :courseid AND (s.cmid = :cmid OR s.cmid = 0)";
 337              $params = array('eventname' => $event->eventname, 'courseid' => $event->courseid, 'cmid' => $event->contextinstanceid);
 338          } else {
 339              $sql .= "WHERE r.eventname = :eventname AND (s.courseid = :courseid OR s.courseid = 0)";
 340              $params = array('eventname' => $event->eventname, 'courseid' => $event->courseid);
 341          }
 342          return self::get_instances($DB->get_records_sql($sql, $params));
 343      }
 344  
 345      /**
 346       * Return sql to join rule and subscription table.
 347       *
 348       * @param bool $count Weather if this is a count query or not.
 349       *
 350       * @return string the sql.
 351       */
 352      protected static function get_subscription_join_rule_sql($count = false) {
 353          if ($count) {
 354              $select = "SELECT COUNT(s.id) ";
 355          } else {
 356              $select = "SELECT s.*, r.description, r.descriptionformat, r.name, r.userid as ruleuserid, r.courseid as rulecourseid,
 357              r.plugin, r.eventname, r.template, r.templateformat, r.frequency, r.timewindow";
 358          }
 359          $sql = $select . "
 360                    FROM {tool_monitor_rules} r
 361                    JOIN {tool_monitor_subscriptions} s
 362                          ON r.id = s.ruleid ";
 363          return $sql;
 364      }
 365  
 366      /**
 367       * Helper method to convert db records to instances.
 368       *
 369       * @param array $arr of subscriptions.
 370       *
 371       * @return array of subscriptions as instances.
 372       */
 373      protected static function get_instances($arr) {
 374          $result = array();
 375          foreach ($arr as $key => $sub) {
 376              $result[$key] = new subscription($sub);
 377          }
 378          return $result;
 379      }
 380  
 381      /**
 382       * Get count of subscriptions for a given rule.
 383       *
 384       * @param int $ruleid rule id of the subscription.
 385       *
 386       * @return int number of subscriptions
 387       */
 388      public static function count_rule_subscriptions($ruleid) {
 389          global $DB;
 390          $sql = self::get_subscription_join_rule_sql(true);
 391          $sql .= "WHERE s.ruleid = :ruleid";
 392  
 393          return $DB->count_records_sql($sql, array('ruleid' => $ruleid));
 394      }
 395  
 396      /**
 397       * Returns true if an event in a particular course has a subscription.
 398       *
 399       * @param string $eventname the name of the event
 400       * @param int $courseid the course id
 401       * @return bool returns true if the event has subscriptions in a given course, false otherwise.
 402       */
 403      public static function event_has_subscriptions($eventname, $courseid) {
 404          global $DB;
 405  
 406          // Check if we can return these from cache.
 407          $cache = \cache::make('tool_monitor', 'eventsubscriptions');
 408  
 409          // The SQL we will be using to fill the cache if it is empty.
 410          $sql = "SELECT DISTINCT(r.eventname)
 411                    FROM {tool_monitor_subscriptions} s
 412              INNER JOIN {tool_monitor_rules} r
 413                      ON s.ruleid = r.id
 414                   WHERE s.courseid = :courseid";
 415  
 416          $sitesubscriptions = $cache->get(0);
 417          // If we do not have the site subscriptions in the cache then return them from the DB.
 418          if ($sitesubscriptions === false) {
 419              // Set the array for the cache.
 420              $sitesubscriptions = array();
 421              if ($subscriptions = $DB->get_records_sql($sql, array('courseid' => 0))) {
 422                  foreach ($subscriptions as $subscription) {
 423                      $sitesubscriptions[$subscription->eventname] = true;
 424                  }
 425              }
 426              $cache->set(0, $sitesubscriptions);
 427          }
 428  
 429          // Check if a subscription exists for this event site wide.
 430          if (isset($sitesubscriptions[$eventname])) {
 431              return true;
 432          }
 433  
 434          // If the course id is for the site, and we reached here then there is no site wide subscription for this event.
 435          if (empty($courseid)) {
 436              return false;
 437          }
 438  
 439          $coursesubscriptions = $cache->get($courseid);
 440          // If we do not have the course subscriptions in the cache then return them from the DB.
 441          if ($coursesubscriptions === false) {
 442              // Set the array for the cache.
 443              $coursesubscriptions = array();
 444              if ($subscriptions = $DB->get_records_sql($sql, array('courseid' => $courseid))) {
 445                  foreach ($subscriptions as $subscription) {
 446                      $coursesubscriptions[$subscription->eventname] = true;
 447                  }
 448              }
 449              $cache->set($courseid, $coursesubscriptions);
 450          }
 451  
 452          // Check if a subscription exists for this event in this course.
 453          if (isset($coursesubscriptions[$eventname])) {
 454              return true;
 455          }
 456  
 457          return false;
 458      }
 459  }


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