index.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 page lets users to manage rules for a given course.
  18. *
  19. * @package tool_monitor
  20. * @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once(__DIR__ . '/../../../config.php');
  24. require_once($CFG->libdir.'/adminlib.php');
  25. require_once($CFG->dirroot . '/admin/tool/monitor/lib.php');
  26. $courseid = optional_param('courseid', 0, PARAM_INT);
  27. $action = optional_param('action', '', PARAM_ALPHA);
  28. $cmid = optional_param('cmid', 0, PARAM_INT);
  29. $ruleid = optional_param('ruleid', 0, PARAM_INT);
  30. $subscriptionid = optional_param('subscriptionid', 0, PARAM_INT);
  31. $confirm = optional_param('confirm', false, PARAM_BOOL);
  32. $choose = false;
  33. // Validate course id.
  34. if (empty($courseid)) {
  35. require_login();
  36. $context = context_system::instance();
  37. // check system level capability.
  38. if (!has_capability('tool/monitor:subscribe', $context)) {
  39. // If not system level then check to see if they have access to any course level rules.
  40. if (tool_monitor_get_user_courses()){
  41. // Make them choose a course.
  42. $choose = true;
  43. } else {
  44. // return error.
  45. print_error('rulenopermission', 'tool_monitor');
  46. }
  47. }
  48. } else {
  49. // They might want to see rules for this course.
  50. $course = get_course($courseid);
  51. require_login($course);
  52. $context = context_course::instance($course->id);
  53. // Check for caps.
  54. require_capability('tool/monitor:subscribe', $context);
  55. }
  56. if (!get_config('tool_monitor', 'enablemonitor')) {
  57. // This should never happen as the this page does not appear in navigation when the tool is disabled.
  58. throw new coding_exception('Event monitoring is disabled');
  59. }
  60. // Use the user context here so that the header shows user information.
  61. $PAGE->set_context(context_user::instance($USER->id));
  62. // Set up the page.
  63. $indexurl = new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid));
  64. $PAGE->set_url($indexurl);
  65. $PAGE->set_pagelayout('report');
  66. $PAGE->set_title(get_string('managesubscriptions', 'tool_monitor'));
  67. $PAGE->set_heading(fullname($USER));
  68. $settingsnode = $PAGE->settingsnav->find('monitor', null);
  69. if ($settingsnode) {
  70. $settingsnode->make_active();
  71. }
  72. // Create/delete subscription if needed.
  73. if (!empty($action)) {
  74. require_sesskey();
  75. switch ($action) {
  76. case 'subscribe' :
  77. $rule = \tool_monitor\rule_manager::get_rule($ruleid);
  78. $rule->subscribe_user($courseid, $cmid);
  79. echo $OUTPUT->header();
  80. echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess');
  81. break;
  82. case 'unsubscribe' :
  83. // If the subscription does not exist, then redirect back as the subscription must have already been deleted.
  84. if (!$subscription = $DB->record_exists('tool_monitor_subscriptions', array('id' => $subscriptionid))) {
  85. redirect(new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid)));
  86. }
  87. // Set the URLs.
  88. $confirmurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
  89. 'courseid' => $courseid, 'action' => 'unsubscribe', 'confirm' => true,
  90. 'sesskey' => sesskey()));
  91. $cancelurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid,
  92. 'courseid' => $courseid, 'sesskey' => sesskey()));
  93. if ($confirm) {
  94. \tool_monitor\subscription_manager::delete_subscription($subscriptionid);
  95. echo $OUTPUT->header();
  96. echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess');
  97. } else {
  98. $subscription = \tool_monitor\subscription_manager::get_subscription($subscriptionid);
  99. echo $OUTPUT->header();
  100. echo $OUTPUT->confirm(get_string('subareyousure', 'tool_monitor', $subscription->get_name($context)),
  101. $confirmurl, $cancelurl);
  102. echo $OUTPUT->footer();
  103. exit();
  104. }
  105. break;
  106. default:
  107. }
  108. } else {
  109. echo $OUTPUT->header();
  110. }
  111. $renderer = $PAGE->get_renderer('tool_monitor', 'managesubs');
  112. // Render the course selector.
  113. $totalrules = \tool_monitor\rule_manager::count_rules_by_courseid($courseid);
  114. $rules = new \tool_monitor\output\managesubs\rules('toolmonitorrules', $indexurl, $courseid);
  115. $usercourses = $rules->get_user_courses_select($choose);
  116. // There must be user courses otherwise we wouldn't make it this far.
  117. echo $renderer->render($usercourses);
  118. // Render the current subscriptions list.
  119. $totalsubs = \tool_monitor\subscription_manager::count_user_subscriptions();
  120. if (!empty($totalsubs) && !$choose) {
  121. // Show the subscriptions section only if there are subscriptions.
  122. $subs = new \tool_monitor\output\managesubs\subs('toolmonitorsubs', $indexurl, $courseid);
  123. echo $OUTPUT->heading(get_string('currentsubscriptions', 'tool_monitor'), 3);
  124. echo $renderer->render($subs);
  125. }
  126. // Render the potential rules list.
  127. if (!$choose) {
  128. echo $OUTPUT->heading(get_string('rulescansubscribe', 'tool_monitor'), 3);
  129. echo $renderer->render($rules);
  130. }
  131. // Check if the user can manage the course rules we are viewing.
  132. $canmanagerules = has_capability('tool/monitor:managerules', $context);
  133. if (empty($totalrules)) {
  134. // No rules present. Show a link to manage rules page if permissions permit.
  135. echo html_writer::start_div();
  136. echo html_writer::tag('span', get_string('norules', 'tool_monitor'));
  137. if ($canmanagerules) {
  138. $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
  139. $a = html_writer::link($manageurl, get_string('managerules', 'tool_monitor'));
  140. $link = "&nbsp;";
  141. $link .= html_writer::tag('span', get_string('manageruleslink', 'tool_monitor', $a));
  142. echo $link;
  143. }
  144. echo html_writer::end_div();
  145. } else if ($canmanagerules) {
  146. $manageurl = new moodle_url("/admin/tool/monitor/managerules.php", array('courseid' => $courseid));
  147. echo $renderer->render_rules_link($manageurl);
  148. }
  149. echo $OUTPUT->footer();