Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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   * Renderable class to display a set of rules in the manage subscriptions page.
  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\output\managesubs;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  require_once($CFG->libdir . '/tablelib.php');
  30  
  31  /**
  32   * Renderable class to display a set of rules in the manage subscriptions page.
  33   *
  34   * @since      Moodle 2.8
  35   * @package    tool_monitor
  36   * @copyright  2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class rules extends \table_sql implements \renderable {
  40  
  41      /**
  42       * @var int course id.
  43       */
  44      public $courseid;
  45  
  46      /**
  47       * @var int total rules present.
  48       */
  49      public $totalcount = 0;
  50  
  51      /**
  52       * @var \context_course|\context_system context of the page to be rendered.
  53       */
  54      protected $context;
  55  
  56      /**
  57       * Sets up the table_log parameters.
  58       *
  59       * @param string $uniqueid unique id of form.
  60       * @param \moodle_url $url url where this table is displayed.
  61       * @param int $courseid course id.
  62       * @param int $perpage Number of rules to display per page.
  63       */
  64      public function __construct($uniqueid, \moodle_url $url, $courseid = 0, $perpage = 100) {
  65          parent::__construct($uniqueid);
  66  
  67          $this->set_attribute('class', 'toolmonitor subscriberules generaltable generalbox');
  68          $this->define_columns(array('name', 'description', 'course', 'plugin', 'eventname', 'filters', 'select'));
  69          $this->define_headers(array(
  70                  get_string('rulename', 'tool_monitor'),
  71                  get_string('description'),
  72                  get_string('course'),
  73                  get_string('area', 'tool_monitor'),
  74                  get_string('event', 'tool_monitor'),
  75                  get_string('frequency', 'tool_monitor'),
  76                  ''
  77              )
  78          );
  79          $this->courseid = $courseid;
  80          $this->pagesize = $perpage;
  81          $systemcontext = \context_system::instance();
  82          $this->context = empty($courseid) ? $systemcontext : \context_course::instance($courseid);
  83          $this->collapsible(false);
  84          $this->sortable(false);
  85          $this->pageable(true);
  86          $this->is_downloadable(false);
  87          $this->define_baseurl($url);
  88          $total = \tool_monitor\rule_manager::count_rules_by_courseid($this->courseid);
  89          $this->totalcount = $total;
  90      }
  91  
  92      /**
  93       * Generate content for name column.
  94       *
  95       * @param \tool_monitor\rule $rule rule object
  96       * @return string html used to display the rule name.
  97       */
  98      public function col_name(\tool_monitor\rule $rule) {
  99          return $rule->get_name($this->context);
 100      }
 101  
 102      /**
 103       * Generate content for description column.
 104       *
 105       * @param \tool_monitor\rule $rule rule object
 106       * @return string html used to display the description.
 107       */
 108      public function col_description(\tool_monitor\rule $rule) {
 109          return $rule->get_description($this->context);
 110      }
 111  
 112      /**
 113       * Generate content for course column.
 114       *
 115       * @param \tool_monitor\rule $rule rule object
 116       * @return string html used to display the course name.
 117       */
 118      public function col_course(\tool_monitor\rule $rule) {
 119          $coursename = $rule->get_course_name($this->context);
 120  
 121          $courseid = $rule->courseid;
 122          if (empty($courseid)) {
 123              return $coursename;
 124          } else {
 125              return \html_writer::link(new \moodle_url('/course/view.php', array('id' => $this->courseid)), $coursename);
 126          }
 127      }
 128  
 129      /**
 130       * Generate content for plugin column.
 131       *
 132       * @param \tool_monitor\rule $rule rule object
 133       * @return string html used to display the plugin name.
 134       */
 135      public function col_plugin(\tool_monitor\rule $rule) {
 136          return $rule->get_plugin_name();
 137      }
 138  
 139      /**
 140       * Generate content for eventname column.
 141       *
 142       * @param \tool_monitor\rule $rule rule object
 143       * @return string html used to display the event name.
 144       */
 145      public function col_eventname(\tool_monitor\rule $rule) {
 146          return $rule->get_event_name();
 147      }
 148  
 149      /**
 150       * Generate content for filters column.
 151       *
 152       * @param \tool_monitor\rule $rule rule object
 153       * @return string html used to display the filters.
 154       */
 155      public function col_filters(\tool_monitor\rule $rule) {
 156          return $rule->get_filters_description();
 157      }
 158  
 159      /**
 160       * Generate content for select column.
 161       *
 162       * @param \tool_monitor\rule $rule rule object
 163       * @return string html used to display the select field.
 164       */
 165      public function col_select(\tool_monitor\rule $rule) {
 166          global $OUTPUT;
 167  
 168          $options = $rule->get_subscribe_options($this->courseid);
 169          $text = get_string('subscribeto', 'tool_monitor', $rule->get_name($this->context));
 170  
 171          if ($options instanceof \single_select) {
 172              $options->set_label($text, array('class' => 'accesshide'));
 173              return $OUTPUT->render($options);
 174          } else if ($options instanceof \moodle_url) {
 175              // A \moodle_url to subscribe.
 176              $icon = $OUTPUT->pix_icon('t/add', $text);
 177              $link = new \action_link($options, $icon);
 178              return $OUTPUT->render($link);
 179          } else {
 180              return $options;
 181          }
 182      }
 183  
 184      /**
 185       * Query the reader. Store results in the object for use by build_table.
 186       *
 187       * @param int $pagesize size of page for paginated displayed table.
 188       * @param bool $useinitialsbar do you want to use the initials bar.
 189       */
 190      public function query_db($pagesize, $useinitialsbar = true) {
 191  
 192          $total = \tool_monitor\rule_manager::count_rules_by_courseid($this->courseid);
 193          $this->pagesize($pagesize, $total);
 194          $rules = \tool_monitor\rule_manager::get_rules_by_courseid($this->courseid, $this->get_page_start(),
 195                  $this->get_page_size());
 196          $this->rawdata = $rules;
 197          // Set initial bars.
 198          if ($useinitialsbar) {
 199              $this->initialbars($total > $pagesize);
 200          }
 201      }
 202  
 203      /**
 204       * Gets a list of courses where the current user can subscribe to rules as a dropdown.
 205       *
 206       * @param bool $choose A flag for whether to show the 'choose...' option in the select box.
 207       * @return \single_select|bool returns the list of courses, or false if the select box
 208       *      should not be displayed.
 209       */
 210      public function get_user_courses_select($choose = false) {
 211          $options = tool_monitor_get_user_courses();
 212          // If we have no options then don't create a select element.
 213          if (!$options) {
 214              return false;
 215          }
 216          $selected = $this->courseid;
 217          $nothing = array();
 218          if ($choose) {
 219              $selected = null;
 220              $nothing = array('choosedots');
 221          }
 222          $url = new \moodle_url('/admin/tool/monitor/index.php');
 223          $select = new \single_select($url, 'courseid', $options, $selected, $nothing);
 224          $select->set_label(get_string('selectacourse', 'tool_monitor'));
 225          return $select;
 226      }
 227  }