Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 monitor data generator
  19   *
  20   * @package    tool_monitor
  21   * @category   test
  22   * @copyright  2014 onwards Simey Lameze <simey@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Event monitor data generator class
  30   *
  31   * @since       Moodle 2.8
  32   * @package     tool_monitor
  33   * @category    test
  34   * @copyright   2014 onwards Simey Lameze <simey@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class tool_monitor_generator extends testing_module_generator {
  38  
  39      /**
  40       * @var int keep track of how many rules have been created.
  41       */
  42      protected $rulecount;
  43  
  44      /**
  45       * Function to generate rule data.
  46       *
  47       * @param \stdClass|array $record data to insert as rule entry.
  48       *
  49       * @return \tool_monitor\rule An instance of rule class.
  50       */
  51      public function create_rule($record = null) {
  52          global $USER;
  53  
  54          $this->rulecount++;
  55          $i = $this->rulecount;
  56          $now = time();
  57          $record = (object)(array)$record;
  58  
  59          if (!isset($record->userid)) {
  60              $record->userid = $USER->id;
  61          }
  62          if (!isset($record->courseid)) {
  63              $record->courseid = 0;
  64          }
  65          if (!isset($record->name)) {
  66              $record->name = 'Test rule ' . $i;
  67          }
  68          if (!isset($record->description)) {
  69              $record->description = 'Rule description ' . $i;
  70          }
  71          if (!isset($record->descriptionformat)) {
  72              $record->descriptionformat = FORMAT_HTML;
  73          }
  74          if (!isset($record->frequency)) {
  75              $record->frequency = 5;
  76          }
  77          if (!isset($record->minutes)) {
  78              $record->minutes = 5;
  79          }
  80          if (!isset($record->template)) {
  81              $record->template = 'Rule message template ' . $i;
  82          }
  83          if (!isset($record->templateformat)) {
  84              $record->templateformat = FORMAT_HTML;
  85          }
  86          if (!isset($record->timewindow)) {
  87              $record->timewindow = $record->minutes * 60;
  88          }
  89          if (!isset($record->timecreated)) {
  90              $record->timecreated = $now;
  91          }
  92          if (!isset($record->timemodified)) {
  93              $record->timemodified = $now;
  94          }
  95          if (!isset($record->plugin)) {
  96              $record->plugin = 'core';
  97          }
  98          if (!isset($record->eventname)) {
  99              $record->eventname = '\core\event\blog_entry_created';
 100          }
 101  
 102          unset($record->minutes); // Remove the minutes shortcut to the timewindow.
 103          return \tool_monitor\rule_manager::add_rule($record);
 104      }
 105  
 106      /**
 107       * Function to generate subscription data.
 108       *
 109       * @throws coding_exception if $record->ruleid or $record->userid not present.
 110       * @param \stdClass|array $record data to insert as subscription entry.
 111       *
 112       * @return \tool_monitor\subscription An instance of the subscription class.
 113       */
 114      public function create_subscription($record = null) {
 115  
 116          if (!isset($record->timecreated)) {
 117              $record->timecreated = time();
 118          }
 119          if (!isset($record->courseid)) {
 120              $record->courseid = 0;
 121          }
 122          if (!isset($record->ruleid)) {
 123              throw new coding_exception('$record->ruleid must be present in tool_monitor_generator::create_subscription()');
 124          }
 125          if (!isset($record->cmid)) {
 126              $record->cmid = 0;
 127          }
 128          if (!isset($record->userid)) {
 129              throw new coding_exception('$record->userid must be present in tool_monitor_generator::create_subscription()');
 130          }
 131  
 132          $sid = \tool_monitor\subscription_manager::create_subscription($record->ruleid, $record->courseid,
 133                  $record->cmid, $record->userid);
 134          return \tool_monitor\subscription_manager::get_subscription($sid);
 135      }
 136  
 137      /**
 138       * Function to generate event entries.
 139       *
 140       * @param \stdClass|array $record data to insert as event entry.
 141       *
 142       * @return \stdClass $record An object representing the newly created event entry.
 143       */
 144      public function create_event_entries($record = null) {
 145          global $DB, $CFG;
 146  
 147          $record = (object)(array)$record;
 148          $context = \context_system::instance();
 149  
 150          if (!isset($record->eventname)) {
 151              $record->eventname = '\core\event\user_loggedin';
 152          }
 153          if (!isset($record->contextid)) {
 154              $record->contextid = $context->id;
 155          }
 156          if (!isset($record->contextlevel)) {
 157              $record->contextlevel = $context->contextlevel;
 158          }
 159          if (!isset($record->contextinstanceid)) {
 160              $record->contextinstanceid = $context->instanceid;
 161          }
 162          if (!isset($record->link)) {
 163              $record->link = $CFG->wwwroot . '/user/profile.php';
 164          }
 165          if (!isset($record->courseid)) {
 166              $record->courseid = 0;
 167          }
 168          if (!isset($record->timecreated)) {
 169              $record->timecreated = time();
 170          }
 171          $record->id = $DB->insert_record('tool_monitor_events', $record, true);
 172  
 173          return $record;
 174      }
 175  
 176      /**
 177       * Function to generate history data.
 178       *
 179       * @throws coding_exception if $record->sid or $record->userid not present.
 180       * @param \stdClass|array $record data to insert as history entry.
 181       *
 182       * @return \stdClass $record An object representing the newly created history entry.
 183       */
 184      public function create_history($record = null) {
 185          global $DB;
 186          $record = (object)(array)$record;
 187          if (!isset($record->sid)) {
 188              throw new coding_exception('subscription ID must be present in tool_monitor_generator::create_history() $record');
 189          }
 190          if (!isset($record->userid)) {
 191              throw new coding_exception('user ID must be present in tool_monitor_generator::create_history() $record');
 192          }
 193          if (!isset($record->timesent)) {
 194              $record->timesent = time();
 195          }
 196          $record->id = $DB->insert_record('tool_monitor_history', $record, true);
 197  
 198          return $record;
 199      }
 200  }