mirror of
https://github.com/moodle/moodle.git
synced 2025-04-09 02:12:22 +02:00
MDL-45758 tool_monitor: Implement a rule instance class
Original issue - MDL-45939
This commit is contained in:
parent
f235cae41f
commit
5f68d9ee2b
104
admin/tool/monitor/classes/helper_trait.php
Normal file
104
admin/tool/monitor/classes/helper_trait.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Common methods that are needed both in rule and subscription class.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_monitor;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once($CFG->dirroot . '/lib/moodlelib.php');
|
||||
|
||||
/**
|
||||
* Common methods that are needed both in rule and subscription class.
|
||||
*
|
||||
* @since Moodle 2.8
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
trait helper_trait {
|
||||
|
||||
/**
|
||||
* Method to get event name.
|
||||
*
|
||||
* @return string
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_event_name() {
|
||||
$eventclass = $this->eventname;
|
||||
if (class_exists($eventclass)) {
|
||||
return $eventclass::get_name();
|
||||
}
|
||||
return get_string('eventnotfound', 'tool_monitor');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filter description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_filters_description() {
|
||||
$a = new \stdClass();
|
||||
$a->freq = $this->frequency;
|
||||
$mins = $this->timewindow / MINSECS; // Convert seconds to minutes.
|
||||
$a->mins = $mins;
|
||||
return get_string('freqdesc', 'tool_monitor', $a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get properly formatted name of the rule associated.
|
||||
*
|
||||
* @param \context $context context where this name would be displayed.
|
||||
*
|
||||
* @return string Formatted name of the rule.
|
||||
*/
|
||||
public function get_name(\context $context) {
|
||||
return format_text($this->name, FORMAT_HTML, array('context' => $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get properly formatted description of the rule associated.
|
||||
*
|
||||
* @param \context $context context where this description would be displayed.
|
||||
*
|
||||
* @return string Formatted description of the rule.
|
||||
*/
|
||||
public function get_description(\context $context) {
|
||||
return format_text($this->description, $this->descriptionformat, array('context' => $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of the plugin associated with this rule
|
||||
*
|
||||
* @return string Plugin name.
|
||||
*/
|
||||
public function get_plugin_name() {
|
||||
if ($this->plugin === 'core') {
|
||||
$string = get_string('core', 'tool_monitor');
|
||||
} else if (get_string_manager()->string_exists('pluginname', $this->plugin)) {
|
||||
$string = get_string('pluginname', $this->plugin);
|
||||
} else {
|
||||
$string = $this->plugin;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
183
admin/tool/monitor/classes/rule.php
Normal file
183
admin/tool/monitor/classes/rule.php
Normal file
@ -0,0 +1,183 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Class represents a single rule.
|
||||
*
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace tool_monitor;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Class represents a single rule.
|
||||
*
|
||||
* @since Moodle 2.8
|
||||
* @package tool_monitor
|
||||
* @copyright 2014 onwards Ankit Agarwal <ankit.agrr@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class rule {
|
||||
use helper_trait;
|
||||
|
||||
/**
|
||||
* @var \stdClass The rule object form database.
|
||||
*/
|
||||
protected $rule;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \stdClass $rule A rule object from database.
|
||||
*/
|
||||
public function __construct($rule) {
|
||||
$this->rule = $rule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the current user manage this rule?
|
||||
*
|
||||
* @return bool true if the current user can manage this rule, else false.
|
||||
*/
|
||||
public function can_manage_rule() {
|
||||
$courseid = $this->courseid;
|
||||
$context = empty($courseid) ? \context_system::instance() : \context_course::instance($this->courseid);
|
||||
return has_capability('tool/monitor:managerules', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Api to duplicate a rule in a given courseid.
|
||||
*
|
||||
* @param int $finalcourseid Final course id.
|
||||
*/
|
||||
public function duplicate_rule($finalcourseid) {
|
||||
$rule = fullclone($this->rule);
|
||||
unset($rule->id);
|
||||
$rule->courseid = $finalcourseid;
|
||||
$time = time();
|
||||
$rule->timecreated = $time;
|
||||
$rule->timemodified = $time;
|
||||
rule_manager::add_rule($rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete this rule.
|
||||
*
|
||||
* Note: It also removes all associated subscriptions.
|
||||
*/
|
||||
public function delete_rule() {
|
||||
rule_manager::delete_rule($this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a select drop down with list of possible modules for a given course and rule.
|
||||
*
|
||||
* @param int $courseid course id
|
||||
*
|
||||
* @return \single_select a single select object
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_module_select($courseid) {
|
||||
global $CFG;
|
||||
$options = array();
|
||||
if (strpos($this->plugin, 'mod_') === 0) {
|
||||
$options[0] = get_string('allmodules', 'tool_monitor');
|
||||
} else {
|
||||
$options[0] = get_string('allevents', 'tool_monitor');
|
||||
}
|
||||
if (strpos($this->plugin, 'mod_') === 0) {
|
||||
if ($courseid == 0) {
|
||||
// They need to be in a course to select module instance.
|
||||
return get_string('selectcourse', 'tool_monitor');
|
||||
}
|
||||
// Let them select an instance.
|
||||
$cms = get_fast_modinfo($courseid);
|
||||
$instances = $cms->get_instances_of(str_replace('mod_', '', $this->plugin));
|
||||
foreach ($instances as $cminfo) {
|
||||
// Don't list instances that are not visible or available to the user.
|
||||
if ($cminfo->uservisible && $cminfo->available) {
|
||||
$options[$cminfo->id] = $cminfo->get_formatted_name();
|
||||
}
|
||||
}
|
||||
}
|
||||
$url = new \moodle_url($CFG->wwwroot. '/tool/monitor/index.php', array('id' => $courseid, 'ruleid' => $this->id,
|
||||
'action' => 'subscribe'));
|
||||
return new \single_select($url, 'cmid', $options, '', $nothing = array('' => 'choosedots'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe an user to this rule.
|
||||
*
|
||||
* @param int $courseid Course id.
|
||||
* @param int $cmid Course module id.
|
||||
* @param int $userid User id.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function subscribe_user($courseid, $cmid, $userid = 0) {
|
||||
global $USER;
|
||||
|
||||
if ($this->courseid != $courseid && $this->courseid != 0) {
|
||||
// Trying to subscribe to a rule that belongs to a different course. Should never happen.
|
||||
throw new \coding_exception('Can not subscribe to rules from a different course');
|
||||
}
|
||||
if ($cmid !== 0) {
|
||||
$cms = get_fast_modinfo($courseid);
|
||||
$cminfo = $cms->get_cm($cmid);
|
||||
if (!$cminfo->uservisible || !$cminfo->available) {
|
||||
// Trying to subscribe to a hidden or restricted cm. Should never happen.
|
||||
throw new \coding_exception('You cannot do that');
|
||||
}
|
||||
}
|
||||
$userid = empty($userid) ? $USER->id : $userid;
|
||||
|
||||
subscription_manager::create_subscription($this->id, $courseid, $cmid, $userid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic get method.
|
||||
*
|
||||
* @param string $prop property to get.
|
||||
*
|
||||
* @return mixed
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function __get($prop) {
|
||||
if (property_exists($this->rule, $prop)) {
|
||||
return $this->rule->$prop;
|
||||
}
|
||||
throw new \coding_exception('Property "' . $prop . '" doesn\'t exist');
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the rule data to be used while setting mform.
|
||||
*
|
||||
* @throws \coding_exception
|
||||
*/
|
||||
public function get_mform_set_data() {
|
||||
if (!empty($this->rule)) {
|
||||
$rule = fullclone($this->rule);
|
||||
$rule->description = array('text' => $rule->description, 'format' => $rule->descriptionformat);
|
||||
$rule->template = array('text' => $rule->template, 'format' => $rule->templateformat);
|
||||
return $rule;
|
||||
}
|
||||
throw new \coding_exception('Invalid call to get_mform_set_data.');
|
||||
}
|
||||
}
|
@ -24,6 +24,12 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
$string['allevents'] = 'All events';
|
||||
$string['allmodules'] = 'All modules';
|
||||
$string['core'] = 'Core';
|
||||
$string['eventnotfound'] = 'Event not found';
|
||||
$string['freqdesc'] = '{$a->freq} times in {$a->mins} minutes';
|
||||
$string['pluginname'] = 'Event monitor';
|
||||
$string['selectcourse'] = 'Visit this report at course level to get a list of possible modules';
|
||||
$string['tool/monitor:managerules'] = 'Manage event monitor rules';
|
||||
$string['tool/monitor:subscribe'] = 'Subscribe to event monitor rules';
|
||||
|
Loading…
x
Reference in New Issue
Block a user