edit.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * This file gives an overview of the monitors present in site.
  18. *
  19. * @package tool_monitor
  20. * @copyright 2014 onwards Simey Lameze <simey@moodle.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require(__DIR__ . '/../../../config.php');
  24. require_once($CFG->libdir.'/adminlib.php');
  25. $ruleid = optional_param('ruleid', 0, PARAM_INT);
  26. $courseid = optional_param('courseid', 0, PARAM_INT);
  27. // Validate course id.
  28. if (empty($courseid)) {
  29. require_login();
  30. $context = context_system::instance();
  31. $coursename = format_string($SITE->fullname, true, array('context' => $context));
  32. $PAGE->set_context($context);
  33. } else {
  34. $course = get_course($courseid);
  35. require_login($course);
  36. $context = context_course::instance($course->id);
  37. $coursename = format_string($course->fullname, true, array('context' => $context));
  38. }
  39. // Check for caps.
  40. require_capability('tool/monitor:managerules', $context);
  41. // Set up the page.
  42. $url = new moodle_url("/admin/tool/monitor/edit.php", array('courseid' => $courseid, 'ruleid' => $ruleid));
  43. $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
  44. $PAGE->set_url($url);
  45. $PAGE->set_pagelayout('report');
  46. $PAGE->set_title($coursename);
  47. $PAGE->set_heading($coursename);
  48. // Get data ready for mform.
  49. $eventlist = tool_monitor\eventlist::get_all_eventlist(true);
  50. $pluginlist = tool_monitor\eventlist::get_plugin_list();
  51. // Site level report.
  52. if (empty($courseid)) {
  53. admin_externalpage_setup('toolmonitorrules', '', null, '', array('pagelayout' => 'report'));
  54. } else {
  55. // Course level report.
  56. $PAGE->navigation->override_active_url($manageurl);
  57. }
  58. // Mform setup.
  59. if (!empty($ruleid)) {
  60. $rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data();
  61. $rule->minutes = $rule->timewindow / MINSECS;
  62. $subscriptioncount = \tool_monitor\subscription_manager::count_rule_subscriptions($ruleid);
  63. // Filter out events which cannot be triggered for some reason.
  64. $eventlist = array_filter($eventlist, function($classname) use ($rule) {
  65. // Filter out all deprecated events, except for the current one.
  66. return $classname === $rule->eventname || !$classname::is_deprecated();
  67. }, ARRAY_FILTER_USE_KEY);
  68. } else {
  69. $rule = new stdClass();
  70. $subscriptioncount = 0;
  71. // Filter out events which cannot be triggered for some reason.
  72. $eventlist = array_filter($eventlist, function($classname) {
  73. return !$classname::is_deprecated();
  74. }, ARRAY_FILTER_USE_KEY);
  75. }
  76. // Modify the lists to add the choosers.
  77. $eventlist = array_merge(array('' => get_string('choosedots')), $eventlist);
  78. $pluginlist = array_merge(array('' => get_string('choosedots')), $pluginlist);
  79. $mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule,
  80. 'courseid' => $courseid, 'subscriptioncount' => $subscriptioncount));
  81. if ($mform->is_cancelled()) {
  82. redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $courseid)));
  83. exit();
  84. }
  85. if ($mformdata = $mform->get_data()) {
  86. $rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata);
  87. if (empty($rule->id)) {
  88. \tool_monitor\rule_manager::add_rule($rule);
  89. } else {
  90. \tool_monitor\rule_manager::update_rule($rule);
  91. }
  92. redirect($manageurl);
  93. } else {
  94. // Set up the yui module.
  95. $PAGE->requires->yui_module('moodle-tool_monitor-dropdown', 'Y.M.tool_monitor.DropDown.init',
  96. array(array('eventlist' => $eventlist)));
  97. echo $OUTPUT->header();
  98. $mform->set_data($rule);
  99. // If there's any subscription for this rule, display an information message.
  100. if ($subscriptioncount > 0) {
  101. echo $OUTPUT->notification(get_string('disablefieldswarning', 'tool_monitor'), 'notifyproblem');
  102. }
  103. $mform->display();
  104. echo $OUTPUT->footer();
  105. exit;
  106. }
  107. echo $OUTPUT->header();
  108. if (!empty($ruleid)) {
  109. echo $OUTPUT->heading(get_string('editrule', 'tool_monitor'));
  110. } else {
  111. echo $OUTPUT->heading(get_string('addrule', 'tool_monitor'));
  112. }
  113. $mform->set_data($rule);
  114. $mform->display();
  115. echo $OUTPUT->footer();