Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

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