Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Rule manager class.
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Simey Lameze <lameze@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_monitor;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Rule manager class.
  30   *
  31   * @package    tool_monitor
  32   * @copyright  2014 onwards Simey Lameze <lameze@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class rule_manager {
  36  
  37      /**
  38       * Create a new rule.
  39       *
  40       * @param \stdClass $ruledata data to insert as new rule entry.
  41       *
  42       * @return rule An instance of rule class.
  43       */
  44      public static function add_rule($ruledata) {
  45          global $DB;
  46  
  47          $now = time();
  48          $ruledata->timecreated = $now;
  49          $ruledata->timemodified = $now;
  50  
  51          $ruledata->id = $DB->insert_record('tool_monitor_rules', $ruledata);
  52  
  53          // Trigger a rule created event.
  54          if ($ruledata->id) {
  55              if (!empty($ruledata->courseid)) {
  56                  $courseid = $ruledata->courseid;
  57                  $context = \context_course::instance($ruledata->courseid);
  58              } else {
  59                  $courseid = 0;
  60                  $context = \context_system::instance();
  61              }
  62  
  63              $params = array(
  64                  'objectid' => $ruledata->id,
  65                  'courseid' => $courseid,
  66                  'context' => $context
  67              );
  68              $event = \tool_monitor\event\rule_created::create($params);
  69              $event->trigger();
  70          }
  71  
  72          return new rule($ruledata);
  73      }
  74  
  75      /**
  76       * Clean data submitted by mform.
  77       *
  78       * @param \stdClass $mformdata data to insert as new rule entry.
  79       *
  80       * @return \stdClass Cleaned rule data.
  81       */
  82      public static function clean_ruledata_form($mformdata) {
  83          global $USER;
  84  
  85          $rule = new \stdClass();
  86          if (!empty($mformdata->ruleid)) {
  87              $rule->id = $mformdata->ruleid;
  88          }
  89          $rule->userid = empty($mformdata->userid) ? $USER->id : $mformdata->userid;
  90          $rule->courseid = $mformdata->courseid;
  91          $rule->name = $mformdata->name;
  92          $rule->plugin = $mformdata->plugin;
  93          $rule->eventname = $mformdata->eventname;
  94          $rule->description = $mformdata->description['text'];
  95          $rule->descriptionformat = $mformdata->description['format'];
  96          $rule->frequency = $mformdata->frequency;
  97          $rule->timewindow = $mformdata->minutes * MINSECS;
  98          $rule->template = $mformdata->template['text'];
  99          $rule->templateformat = $mformdata->template['format'];
 100  
 101          return $rule;
 102      }
 103  
 104      /**
 105       * Delete a rule and associated subscriptions, by rule id.
 106       *
 107       * @param int $ruleid id of rule to be deleted.
 108       * @param \context|null $coursecontext the context of the course - this is passed when we
 109       *      can not get the context via \context_course as the course has been deleted.
 110       *
 111       * @return bool
 112       */
 113      public static function delete_rule($ruleid, $coursecontext = null) {
 114          global $DB;
 115  
 116          subscription_manager::remove_all_subscriptions_for_rule($ruleid, $coursecontext);
 117  
 118          // Retrieve the rule from the DB before we delete it, so we have a record when we trigger a rule deleted event.
 119          $rule = $DB->get_record('tool_monitor_rules', array('id' => $ruleid));
 120  
 121          $success = $DB->delete_records('tool_monitor_rules', array('id' => $ruleid));
 122  
 123          // If successful trigger a rule deleted event.
 124          if ($success) {
 125              // It is possible that we are deleting rules associated with a deleted course, so we should be
 126              // passing the context as the second parameter.
 127              if (!is_null($coursecontext)) {
 128                  $context = $coursecontext;
 129                  $courseid = $rule->courseid;
 130              } else if (!empty($rule->courseid) && ($context = \context_course::instance($rule->courseid,
 131                      IGNORE_MISSING))) {
 132                  $courseid = $rule->courseid;
 133              } else {
 134                  $courseid = 0;
 135                  $context = \context_system::instance();
 136              }
 137  
 138              $params = array(
 139                  'objectid' => $rule->id,
 140                  'courseid' => $courseid,
 141                  'context' => $context
 142              );
 143              $event = \tool_monitor\event\rule_deleted::create($params);
 144              $event->add_record_snapshot('tool_monitor_rules', $rule);
 145              $event->trigger();
 146          }
 147  
 148          return $success;
 149      }
 150  
 151      /**
 152       * Get an instance of rule class.
 153       *
 154       * @param \stdClass|int $ruleorid A rule object from database or rule id.
 155       *
 156       * @return rule object with rule id.
 157       */
 158      public static function get_rule($ruleorid) {
 159          global $DB;
 160          if (!is_object($ruleorid)) {
 161              $rule = $DB->get_record('tool_monitor_rules', array('id' => $ruleorid), '*', MUST_EXIST);
 162          } else {
 163              $rule = $ruleorid;
 164          }
 165  
 166          return new rule($rule);
 167      }
 168  
 169      /**
 170       * Update rule data.
 171       *
 172       * @throws \coding_exception if $record->ruleid is invalid.
 173       * @param object $ruledata rule data to be updated.
 174       *
 175       * @return bool
 176       */
 177      public static function update_rule($ruledata) {
 178          global $DB;
 179          if (!self::get_rule($ruledata->id)) {
 180              throw new \coding_exception('Invalid rule ID.');
 181          }
 182          $ruledata->timemodified = time();
 183  
 184          $success = $DB->update_record('tool_monitor_rules', $ruledata);
 185  
 186          // If successful trigger a rule updated event.
 187          if ($success) {
 188              // If we do not have the course id we need to retrieve it.
 189              if (!isset($ruledata->courseid)) {
 190                  $courseid = $DB->get_field('tool_monitor_rules', 'courseid', array('id' => $ruledata->id), MUST_EXIST);
 191              } else {
 192                  $courseid = $ruledata->courseid;
 193              }
 194  
 195              if (!empty($courseid)) {
 196                  $context = \context_course::instance($courseid);
 197              } else {
 198                  $context = \context_system::instance();
 199              }
 200  
 201              $params = array(
 202                  'objectid' => $ruledata->id,
 203                  'courseid' => $courseid,
 204                  'context' => $context
 205              );
 206              $event = \tool_monitor\event\rule_updated::create($params);
 207              $event->trigger();
 208          }
 209  
 210          return $success;
 211      }
 212  
 213      /**
 214       * Get rules by course id.
 215       *
 216       * @param int $courseid course id of the rule.
 217       * @param int $limitfrom Limit from which to fetch rules.
 218       * @param int $limitto  Limit to which rules need to be fetched.
 219       * @param bool $includesite Determines whether we return site wide rules or not.
 220       *
 221       * @return array List of rules for the given course id, if specified will also include site rules.
 222       */
 223      public static function get_rules_by_courseid($courseid, $limitfrom = 0, $limitto = 0, $includesite = true) {
 224          global $DB;
 225  
 226          $select = 'courseid = ?';
 227          $params = array();
 228          $params[] = $courseid;
 229          if ($includesite) {
 230              $select .= ' OR courseid = ?';
 231              $params[] = 0;
 232          }
 233          $orderby = 'courseid DESC, name ASC';
 234  
 235          return self::get_instances($DB->get_records_select('tool_monitor_rules', $select, $params, $orderby,
 236                  '*', $limitfrom, $limitto));
 237      }
 238  
 239      /**
 240       * Get rule count by course id.
 241       *
 242       * @param int $courseid course id of the rule.
 243       *
 244       * @return int count of rules present in system visible in the given course id.
 245       */
 246      public static function count_rules_by_courseid($courseid) {
 247          global $DB;
 248          $select = "courseid = ? OR courseid = ?";
 249          return $DB->count_records_select('tool_monitor_rules', $select, array(0, $courseid));
 250      }
 251  
 252      /**
 253       * Get rules by plugin name.
 254       *
 255       * @param string $plugin plugin name of the rule.
 256       *
 257       * @return array List of rules for the given plugin name.
 258       */
 259      public static function get_rules_by_plugin($plugin) {
 260          global $DB;
 261          return self::get_instances($DB->get_records('tool_monitor_rules', array('plugin' => $plugin)));
 262      }
 263  
 264      /**
 265       * Get rules by event name.
 266       *
 267       * @param string $eventname event name of the rule.
 268       *
 269       * @return array List of rules for the given event.
 270       */
 271      public static function get_rules_by_event($eventname) {
 272          global $DB;
 273          return self::get_instances($DB->get_records('tool_monitor_rules', array('eventname' => $eventname)));
 274      }
 275  
 276      /**
 277       * Helper method to convert db records to instances.
 278       *
 279       * @param array $arr of rules.
 280       *
 281       * @return array of rules as instances.
 282       */
 283      protected static function get_instances($arr) {
 284          $result = array();
 285          foreach ($arr as $key => $sub) {
 286              $result[$key] = new rule($sub);
 287          }
 288          return $result;
 289      }
 290  }