Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * This page lets users to manage rules for a given course.
  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  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->libdir.'/adminlib.php');
  27  
  28  $courseid = optional_param('courseid', 0, PARAM_INT);
  29  $ruleid = optional_param('ruleid', 0, PARAM_INT);
  30  $action = optional_param('action', '', PARAM_ALPHA);
  31  $confirm = optional_param('confirm', false, PARAM_BOOL);
  32  $status = optional_param('status', 0, PARAM_BOOL);
  33  
  34  // Validate course id.
  35  if (empty($courseid)) {
  36      admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
  37      $context = context_system::instance();
  38      $coursename = format_string($SITE->fullname, true, array('context' => $context));
  39      $PAGE->set_context($context);
  40  } else {
  41      $course = get_course($courseid);
  42      require_login($course);
  43      $context = context_course::instance($course->id);
  44      $coursename = format_string($course->fullname, true, array('context' => $context));
  45  }
  46  
  47  // Check for caps.
  48  require_capability('tool/monitor:managerules', $context);
  49  
  50  // Set up the page.
  51  $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
  52  $PAGE->set_url($manageurl);
  53  $PAGE->set_pagelayout('report');
  54  $PAGE->set_title($coursename);
  55  $PAGE->set_heading($coursename);
  56  
  57  
  58  if (!empty($action) && $action == 'changestatus') {
  59      require_sesskey();
  60      require_capability('tool/monitor:managetool', context_system::instance());
  61      // Toggle status of the plugin.
  62      set_config('enablemonitor', $status, 'tool_monitor');
  63      redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => 0)));
  64  }
  65  
  66  // Copy/delete rule if needed.
  67  if (!empty($action) && $ruleid) {
  68      require_sesskey();
  69  
  70      // If the rule does not exist, then redirect back as the rule must have already been deleted.
  71      if (!$rule = $DB->get_record('tool_monitor_rules', array('id' => $ruleid), '*', IGNORE_MISSING)) {
  72          redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $courseid)));
  73      }
  74  
  75      echo $OUTPUT->header();
  76      $rule = \tool_monitor\rule_manager::get_rule($rule);
  77      switch ($action) {
  78          case 'copy':
  79              // No need to check for capability here as it is done at the start of the page.
  80              $rule->duplicate_rule($courseid);
  81              echo $OUTPUT->notification(get_string('rulecopysuccess', 'tool_monitor'), 'notifysuccess');
  82              break;
  83          case 'delete':
  84              if ($rule->can_manage_rule()) {
  85                  $confirmurl = new moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php',
  86                      array('ruleid' => $ruleid, 'courseid' => $courseid, 'action' => 'delete',
  87                          'confirm' => true, 'sesskey' => sesskey()));
  88                  $cancelurl = new moodle_url($CFG->wwwroot. '/admin/tool/monitor/managerules.php',
  89                      array('courseid' => $courseid));
  90                  if ($confirm) {
  91                      $rule->delete_rule();
  92                      echo $OUTPUT->notification(get_string('ruledeletesuccess', 'tool_monitor'), 'notifysuccess');
  93                  } else {
  94                      $strconfirm = get_string('ruleareyousure', 'tool_monitor', $rule->get_name($context));
  95                      if ($numberofsubs = $DB->count_records('tool_monitor_subscriptions', array('ruleid' => $ruleid))) {
  96                          $strconfirm .= '<br />';
  97                          $strconfirm .= get_string('ruleareyousureextra', 'tool_monitor', $numberofsubs);
  98                      }
  99                      echo $OUTPUT->confirm($strconfirm, $confirmurl, $cancelurl);
 100                      echo $OUTPUT->footer();
 101                      exit();
 102                  }
 103              } else {
 104                  // User doesn't have permissions. Should never happen for real users.
 105                  throw new moodle_exception('rulenopermissions', 'tool_monitor', $manageurl, $action);
 106              }
 107              break;
 108          default:
 109      }
 110  } else {
 111      echo $OUTPUT->header();
 112  }
 113  
 114  echo $OUTPUT->heading(get_string('managerules', 'tool_monitor'));
 115  $status = get_config('tool_monitor', 'enablemonitor');
 116  $help = new help_icon('enablehelp', 'tool_monitor');
 117  
 118  // Display option to enable/disable the plugin.
 119  if ($status) {
 120      if (has_capability('tool/monitor:managetool', context_system::instance())) {
 121          // We don't need to show enabled status to everyone.
 122          echo get_string('monitorenabled', 'tool_monitor');
 123          $disableurl = new moodle_url("/admin/tool/monitor/managerules.php",
 124                  array('courseid' => $courseid, 'action' => 'changestatus', 'status' => 0, 'sesskey' => sesskey()));
 125          echo ' ' . html_writer::link($disableurl, get_string('disable'));
 126          echo $OUTPUT->render($help);
 127      }
 128  } else {
 129      echo get_string('monitordisabled', 'tool_monitor');
 130      if (has_capability('tool/monitor:managetool', context_system::instance())) {
 131          $enableurl = new moodle_url("/admin/tool/monitor/managerules.php",
 132                  array('courseid' => $courseid, 'action' => 'changestatus', 'status' => 1, 'sesskey' => sesskey()));
 133          echo ' ' . html_writer::link($enableurl, get_string('enable'));
 134          echo $OUTPUT->render($help);
 135      } else {
 136          echo ' ' . get_string('contactadmin', 'tool_monitor');
 137      }
 138      echo $OUTPUT->footer(); // Do not render anything else.
 139      exit();
 140  }
 141  
 142  // Render the rule list.
 143  $renderable = new \tool_monitor\output\managerules\renderable('toolmonitorrules', $manageurl, $courseid);
 144  $renderer = $PAGE->get_renderer('tool_monitor', 'managerules');
 145  echo $renderer->render($renderable);
 146  if (has_capability('tool/monitor:subscribe', $context)) {
 147      $manageurl = new moodle_url("/admin/tool/monitor/index.php", array('courseid' => $courseid));
 148      echo $renderer->render_subscriptions_link($manageurl);
 149  }
 150  echo $OUTPUT->footer();