[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/admin/tool/monitor/classes/ -> rule.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 represents a single rule.
  19   *
  20   * @package    tool_monitor
  21   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  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 represents a single rule.
  31   *
  32   * @since      Moodle 2.8
  33   * @package    tool_monitor
  34   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class rule {
  38  
  39      /**
  40       * @var \stdClass The rule object form database.
  41       */
  42      protected $rule;
  43  
  44      /**
  45       * Constructor.
  46       *
  47       * @param \stdClass $rule A rule object from database.
  48       */
  49      public function __construct($rule) {
  50          $this->rule = $rule;
  51      }
  52  
  53      /**
  54       * Can the current user manage this rule?
  55       *
  56       * @return bool true if the current user can manage this rule, else false.
  57       */
  58      public function can_manage_rule() {
  59          $courseid = $this->courseid;
  60          $context = empty($courseid) ? \context_system::instance() : \context_course::instance($this->courseid);
  61          return has_capability('tool/monitor:managerules', $context);
  62      }
  63  
  64      /**
  65       * Api to duplicate a rule in a given courseid.
  66       *
  67       * @param int $finalcourseid Final course id.
  68       */
  69      public function duplicate_rule($finalcourseid) {
  70          $rule = fullclone($this->rule);
  71          unset($rule->id);
  72          $rule->courseid = $finalcourseid;
  73          $time = time();
  74          $rule->timecreated = $time;
  75          $rule->timemodified = $time;
  76          rule_manager::add_rule($rule);
  77      }
  78  
  79      /**
  80       * Delete this rule.
  81       *
  82       * Note: It also removes all associated subscriptions.
  83       */
  84      public function delete_rule() {
  85          rule_manager::delete_rule($this->id);
  86      }
  87  
  88      /**
  89       * Gets the rule subscribe options for a given course and rule.
  90       *
  91       * Could be a select drop down with a list of possible module
  92       * instances or a single link to subscribe if the rule plugin
  93       * is not a module.
  94       *
  95       * @param int $courseid course id
  96       *
  97       * @return \single_select|\moodle_url|string
  98       * @throws \coding_exception
  99       */
 100      public function get_subscribe_options($courseid) {
 101          global $CFG;
 102  
 103          $url = new \moodle_url($CFG->wwwroot. '/admin/tool/monitor/index.php', array(
 104              'courseid' => $courseid,
 105              'ruleid' => $this->id,
 106              'action' => 'subscribe',
 107              'sesskey' => sesskey()
 108          ));
 109  
 110          if (strpos($this->plugin, 'mod_') !== 0) {
 111              return $url;
 112  
 113          } else {
 114              // Single select when the plugin is an activity.
 115              $options = array();
 116              $options[0] = get_string('allmodules', 'tool_monitor');
 117  
 118              if ($courseid == 0) {
 119                  // They need to be in a course to select module instance.
 120                  return get_string('selectcourse', 'tool_monitor');
 121              }
 122  
 123              // Let them select an instance.
 124              $cms = get_fast_modinfo($courseid);
 125              $instances = $cms->get_instances_of(str_replace('mod_', '',  $this->plugin));
 126              foreach ($instances as $cminfo) {
 127                  // Don't list instances that are not visible or available to the user.
 128                  if ($cminfo->uservisible && $cminfo->available) {
 129                      $options[$cminfo->id] = $cminfo->get_formatted_name();
 130                  }
 131              }
 132  
 133              return new \single_select($url, 'cmid', $options);
 134          }
 135      }
 136  
 137      /**
 138       * Subscribe an user to this rule.
 139       *
 140       * @param int $courseid Course id.
 141       * @param int $cmid Course module id.
 142       * @param int $userid User id.
 143       *
 144       * @throws \coding_exception
 145       */
 146      public function subscribe_user($courseid, $cmid, $userid = 0) {
 147          global $USER;
 148  
 149          if ($this->courseid != $courseid && $this->courseid != 0) {
 150              // Trying to subscribe to a rule that belongs to a different course. Should never happen.
 151              throw new \coding_exception('Can not subscribe to rules from a different course');
 152          }
 153          if ($cmid !== 0) {
 154              $cms = get_fast_modinfo($courseid);
 155              $cminfo = $cms->get_cm($cmid);
 156              if (!$cminfo->uservisible || !$cminfo->available) {
 157                  // Trying to subscribe to a hidden or restricted cm. Should never happen.
 158                  throw new \coding_exception('You cannot do that');
 159              }
 160          }
 161          $userid = empty($userid) ? $USER->id : $userid;
 162  
 163          subscription_manager::create_subscription($this->id, $courseid, $cmid, $userid);
 164      }
 165  
 166      /**
 167       * Magic get method.
 168       *
 169       * @param string $prop property to get.
 170       *
 171       * @return mixed
 172       * @throws \coding_exception
 173       */
 174      public function __get($prop) {
 175          if (property_exists($this->rule, $prop)) {
 176              return $this->rule->$prop;
 177          }
 178          throw new \coding_exception('Property "' . $prop . '" doesn\'t exist');
 179      }
 180  
 181      /**
 182       * Return the rule data to be used while setting mform.
 183       *
 184       * @throws \coding_exception
 185       */
 186      public function get_mform_set_data() {
 187          if (!empty($this->rule)) {
 188              $rule = fullclone($this->rule);
 189              $rule->description = array('text' => $rule->description, 'format' => $rule->descriptionformat);
 190              $rule->template = array('text' => $rule->template, 'format' => $rule->templateformat);
 191              return $rule;
 192          }
 193          throw new \coding_exception('Invalid call to get_mform_set_data.');
 194      }
 195  
 196      /**
 197       * Method to get event name.
 198       *
 199       * @return string
 200       * @throws \coding_exception
 201       */
 202      public function get_event_name() {
 203          $eventclass = $this->eventname;
 204          if (class_exists($eventclass)) {
 205              return $eventclass::get_name();
 206          }
 207          return get_string('eventnotfound', 'tool_monitor');
 208      }
 209  
 210      /**
 211       * Get filter description.
 212       *
 213       * @return string
 214       */
 215      public function get_filters_description() {
 216          $a = new \stdClass();
 217          $a->freq = $this->frequency;
 218          $mins = $this->timewindow / MINSECS; // Convert seconds to minutes.
 219          $a->mins = $mins;
 220          return get_string('freqdesc', 'tool_monitor', $a);
 221      }
 222  
 223      /**
 224       * Get properly formatted name of the course associated.
 225       *
 226       * @param \context $context context where this name would be displayed.
 227       * @return string The course fullname.
 228       */
 229      public function get_course_name($context) {
 230          $courseid = $this->courseid;
 231          if (empty($courseid)) {
 232              return get_string('site');
 233          } else {
 234              $course = get_course($courseid);
 235              return format_string($course->fullname, true, array('context' => $context));
 236          }
 237      }
 238  
 239      /**
 240       * Get properly formatted name of the rule associated.
 241       *
 242       * @param \context $context context where this name would be displayed.
 243       * @return string Formatted name of the rule.
 244       */
 245      public function get_name(\context $context) {
 246          return format_text($this->name, FORMAT_HTML, array('context' => $context));
 247      }
 248  
 249      /**
 250       * Get properly formatted description of the rule associated.
 251       *
 252       * @param \context $context context where this description would be displayed.
 253       * @return string Formatted description of the rule.
 254       */
 255      public function get_description(\context $context) {
 256          return format_text($this->description, $this->descriptionformat, array('context' => $context));
 257      }
 258  
 259      /**
 260       * Get name of the plugin associated with this rule
 261       *
 262       * @return string Plugin name.
 263       */
 264      public function get_plugin_name() {
 265          if ($this->plugin === 'core') {
 266              $string = get_string('core', 'tool_monitor');
 267          } else if (get_string_manager()->string_exists('pluginname', $this->plugin)) {
 268              $string = get_string('pluginname', $this->plugin);
 269          } else {
 270              $string = $this->plugin;
 271          }
 272          return $string;
 273      }
 274  }


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