/** * Test add_rule method. */ public function test_add_rule() { $this->setAdminUser(); $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $now = time(); $rule = new stdClass(); $rule->userid = $user->id; $rule->courseid = $course->id; $rule->name = 'test rule 1'; $rule->plugin = 'core'; $rule->eventname = '\\core\\event\\course_updated'; $rule->description = 'test description 1'; $rule->descriptionformat = FORMAT_HTML; $rule->frequency = 15; $rule->template = 'test template message'; $rule->templateformat = FORMAT_HTML; $rule->timewindow = 300; $rule->timecreated = $now; $rule->timemodified = $now; $ruledata = \tool_monitor\rule_manager::add_rule($rule); foreach ($rule as $prop => $value) { $this->assertEquals($ruledata->{$prop}, $value); } }
/** * Test the rule created event. */ public function test_rule_created() { // Create the items we need to create a rule. $course = $this->getDataGenerator()->create_course(); $user = $this->getDataGenerator()->create_user(); // Create the variables for the rule we want to create. $ruledata = new stdClass(); $ruledata->userid = $user->id; $ruledata->courseid = $course->id; $ruledata->plugin = 'mod_assign'; $ruledata->eventname = '\\mod_assign\\event\\submission_viewed'; $ruledata->description = 'Rule description'; $ruledata->descriptionformat = FORMAT_HTML; $ruledata->template = 'A message template'; $ruledata->templateformat = FORMAT_HTML; $ruledata->frequency = 1; $ruledata->timewindow = 60; // Trigger and capture the event. $sink = $this->redirectEvents(); $rule = \tool_monitor\rule_manager::add_rule($ruledata); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event contains the expected values. $this->assertInstanceOf('\\tool_monitor\\event\\rule_created', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals($rule->id, $event->objectid); $this->assertEventContextNotUsed($event); // Now let's add a system rule (courseid = 0). $ruledata->courseid = 0; // Trigger and capture the event. $sink = $this->redirectEvents(); \tool_monitor\rule_manager::add_rule($ruledata); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event uses the system context. $this->assertInstanceOf('\\tool_monitor\\event\\rule_created', $event); $this->assertEquals(context_system::instance(), $event->get_context()); }
/** * Function to generate rule data. * * @param \stdClass|array $record data to insert as rule entry. * * @return \tool_monitor\rule An instance of rule class. */ public function create_rule($record = null) { global $USER; $this->rulecount++; $i = $this->rulecount; $now = time(); $record = (object) (array) $record; if (!isset($record->userid)) { $record->userid = $USER->id; } if (!isset($record->courseid)) { $record->courseid = 0; } if (!isset($record->name)) { $record->name = 'Test rule ' . $i; } if (!isset($record->description)) { $record->description = 'Rule description ' . $i; } if (!isset($record->descriptionformat)) { $record->descriptionformat = FORMAT_HTML; } if (!isset($record->frequency)) { $record->frequency = 5; } if (!isset($record->minutes)) { $record->minutes = 5; } if (!isset($record->template)) { $record->template = 'Rule message template ' . $i; } if (!isset($record->templateformat)) { $record->templateformat = FORMAT_HTML; } if (!isset($record->timewindow)) { $record->timewindow = $record->minutes * 60; } if (!isset($record->timecreated)) { $record->timecreated = $now; } if (!isset($record->timemodified)) { $record->timemodified = $now; } if (!isset($record->plugin)) { $record->plugin = 'core'; } if (!isset($record->eventname)) { $record->eventname = '\\core\\event\\blog_entry_created'; } unset($record->minutes); // Remove the minutes shortcut to the timewindow. return \tool_monitor\rule_manager::add_rule($record); }
$rule = \tool_monitor\rule_manager::get_rule($ruleid)->get_mform_set_data(); $rule->minutes = $rule->timewindow / MINSECS; $subscriptioncount = \tool_monitor\subscription_manager::count_rule_subscriptions($ruleid); } else { $rule = new stdClass(); $subscriptioncount = 0; } $mform = new tool_monitor\rule_form(null, array('eventlist' => $eventlist, 'pluginlist' => $pluginlist, 'rule' => $rule, 'courseid' => $courseid, 'subscriptioncount' => $subscriptioncount)); if ($mform->is_cancelled()) { redirect(new moodle_url('/admin/tool/monitor/managerules.php', array('courseid' => $courseid))); exit; } if ($mformdata = $mform->get_data()) { $rule = \tool_monitor\rule_manager::clean_ruledata_form($mformdata); if (empty($rule->id)) { \tool_monitor\rule_manager::add_rule($rule); } else { \tool_monitor\rule_manager::update_rule($rule); } redirect($manageurl); } else { echo $OUTPUT->header(); $mform->set_data($rule); // If there's any subscription for this rule, display an information message. if ($subscriptioncount > 0) { echo $OUTPUT->notification(get_string('disablefieldswarning', 'tool_monitor'), 'notifyproblem'); } $mform->display(); echo $OUTPUT->footer(); exit; }