예제 #1
0
 /**
  * Test update_rule method.
  */
 public function test_update_rule()
 {
     $this->setAdminUser();
     $this->resetAfterTest(true);
     $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
     $rule = $monitorgenerator->create_rule();
     $ruledata = new stdClass();
     $ruledata->id = $rule->id;
     $ruledata->frequency = 25;
     \tool_monitor\rule_manager::update_rule($ruledata);
     $this->assertEquals(25, $ruledata->frequency);
 }
예제 #2
0
    $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;
}
echo $OUTPUT->header();
if (!empty($ruleid)) {
 /**
  * Tests the cleaning up of events.
  */
 public function test_clean_events()
 {
     global $DB;
     // Create the necessary items for testing.
     $user = $this->getDataGenerator()->create_user();
     $course = $this->getDataGenerator()->create_course();
     $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book');
     $book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
     $bookcontext = context_module::instance($book->cmid);
     $bookchapter = $bookgenerator->create_chapter(array('bookid' => $book->id));
     $course2 = $this->getDataGenerator()->create_course();
     $book2 = $this->getDataGenerator()->create_module('book', array('course' => $course2->id));
     $book2context = context_module::instance($book2->cmid);
     $book2chapter = $bookgenerator->create_chapter(array('bookid' => $book2->id));
     $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
     // Let's set some data for the rules we need before we can generate them.
     $rule = new stdClass();
     $rule->userid = $user->id;
     $rule->courseid = $course->id;
     $rule->plugin = 'mod_book';
     $rule->eventname = '\\mod_book\\event\\course_module_viewed';
     $rule->timewindow = 500;
     // Let's add a few rules we want to monitor.
     $rule1 = $monitorgenerator->create_rule($rule);
     $rule->eventname = '\\mod_book\\event\\course_module_instance_list_viewed';
     $rule2 = $monitorgenerator->create_rule($rule);
     // Add the same rules for the same course, but this time with a lower timewindow (used to test that we do not
     // remove an event for a course if there is still a rule where the maximum timewindow has not been reached).
     $rule->eventname = '\\mod_book\\event\\course_module_viewed';
     $rule->timewindow = 200;
     $rule3 = $monitorgenerator->create_rule($rule);
     $rule->eventname = '\\mod_book\\event\\course_module_instance_list_viewed';
     $rule4 = $monitorgenerator->create_rule($rule);
     // Add another rule in a different course.
     $rule->courseid = $course2->id;
     $rule->eventname = '\\mod_book\\event\\chapter_viewed';
     $rule->timewindow = 200;
     $rule5 = $monitorgenerator->create_rule($rule);
     // Add a site wide rule.
     $rule->courseid = 0;
     $rule->eventname = '\\mod_book\\event\\chapter_viewed';
     $rule->timewindow = 500;
     $rule6 = $monitorgenerator->create_rule($rule);
     // Now let's populate the tool_monitor table with the events associated with those rules.
     \mod_book\event\course_module_viewed::create_from_book($book, $bookcontext)->trigger();
     \mod_book\event\course_module_instance_list_viewed::create_from_course($course)->trigger();
     \mod_book\event\chapter_viewed::create_from_chapter($book, $bookcontext, $bookchapter)->trigger();
     // Let's trigger the viewed events again, but in another course. The rules created for these events are
     // associated with another course, so these events should get deleted when we trigger the cleanup task.
     \mod_book\event\course_module_viewed::create_from_book($book2, $book2context)->trigger();
     \mod_book\event\course_module_instance_list_viewed::create_from_course($course2)->trigger();
     // Trigger a chapter_viewed event in this course - this should not get deleted as the rule is site wide.
     \mod_book\event\chapter_viewed::create_from_chapter($book2, $book2context, $book2chapter)->trigger();
     // Trigger a bunch of other events.
     $eventparams = array('context' => context_course::instance($course->id));
     for ($i = 0; $i < 5; $i++) {
         \mod_quiz\event\course_module_instance_list_viewed::create($eventparams)->trigger();
         \mod_scorm\event\course_module_instance_list_viewed::create($eventparams)->trigger();
     }
     // Check that the events exist - there will be additional events for creating courses, modules and rules.
     $this->assertEquals(26, $DB->count_records('tool_monitor_events'));
     // Run the task and check that all the quiz, scorm and rule events are removed as well as the course_module_*
     // viewed events in the second course.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     $events = $DB->get_records('tool_monitor_events');
     $this->assertEquals(4, count($events));
     $event1 = array_shift($events);
     $event2 = array_shift($events);
     $event3 = array_shift($events);
     $event4 = array_shift($events);
     $this->assertEquals('\\mod_book\\event\\course_module_viewed', $event1->eventname);
     $this->assertEquals($course->id, $event1->courseid);
     $this->assertEquals('\\mod_book\\event\\course_module_instance_list_viewed', $event2->eventname);
     $this->assertEquals($course->id, $event2->courseid);
     $this->assertEquals('\\mod_book\\event\\chapter_viewed', $event3->eventname);
     $this->assertEquals($course->id, $event3->courseid);
     $this->assertEquals('\\mod_book\\event\\chapter_viewed', $event4->eventname);
     $this->assertEquals($course2->id, $event4->courseid);
     // Update the timewindow for two of the rules.
     $updaterule = new stdClass();
     $updaterule->id = $rule1->id;
     $updaterule->timewindow = 0;
     \tool_monitor\rule_manager::update_rule($updaterule);
     $updaterule->id = $rule2->id;
     \tool_monitor\rule_manager::update_rule($updaterule);
     // Run the task and check that the events remain as we still have not reached the maximum timewindow.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     $this->assertEquals(4, $DB->count_records('tool_monitor_events'));
     // Now, remove the rules associated with course_module_* events so they get deleted.
     \tool_monitor\rule_manager::delete_rule($rule1->id);
     \tool_monitor\rule_manager::delete_rule($rule2->id);
     \tool_monitor\rule_manager::delete_rule($rule3->id);
     \tool_monitor\rule_manager::delete_rule($rule4->id);
     // Run the task and check all the course_module_* events are gone.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     // We now should only have the chapter_viewed events.
     $events = $DB->get_records('tool_monitor_events');
     $this->assertEquals(2, count($events));
     $event1 = array_shift($events);
     $event2 = array_shift($events);
     $this->assertEquals('\\mod_book\\event\\chapter_viewed', $event1->eventname);
     $this->assertEquals($course->id, $event1->courseid);
     $this->assertEquals('\\mod_book\\event\\chapter_viewed', $event2->eventname);
     $this->assertEquals($course2->id, $event2->courseid);
     // Set the timewindow of the rule for the event chapter_viewed in the second course to 0.
     $updaterule->id = $rule5->id;
     \tool_monitor\rule_manager::update_rule($updaterule);
     // Run the task.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     // Check that nothing was deleted as we still have a site wide rule for the chapter_viewed event.
     $this->assertEquals(2, $DB->count_records('tool_monitor_events'));
     // Set the timewindow back to 500.
     $updaterule->id = $rule5->id;
     $updaterule->timewindow = 500;
     \tool_monitor\rule_manager::update_rule($updaterule);
     // Set the site rule timewindow to 0.
     $updaterule->id = $rule6->id;
     $updaterule->timewindow = 0;
     \tool_monitor\rule_manager::update_rule($updaterule);
     // Run the task.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     // We now should only have one chapter_viewed event for the second course.
     $events = $DB->get_records('tool_monitor_events');
     $this->assertEquals(1, count($events));
     $event1 = array_shift($events);
     $this->assertEquals('\\mod_book\\event\\chapter_viewed', $event1->eventname);
     $this->assertEquals($course2->id, $event1->courseid);
     // Remove the site rule.
     \tool_monitor\rule_manager::delete_rule($rule6->id);
     // Remove the last remaining rule.
     \tool_monitor\rule_manager::delete_rule($rule5->id);
     // Run the task.
     $task = new \tool_monitor\task\clean_events();
     $task->execute();
     // There should be no events left.
     $this->assertEquals(0, $DB->count_records('tool_monitor_events'));
 }
예제 #4
0
 /**
  * Test the rule updated event.
  */
 public function test_rule_updated()
 {
     // Create the items we need.
     $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
     $course = $this->getDataGenerator()->create_course();
     // Create the rule we are going to update.
     $createrule = new stdClass();
     $createrule->courseid = $course->id;
     $rule = $monitorgenerator->create_rule($createrule);
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     $updaterule = new stdClass();
     $updaterule->id = $rule->id;
     \tool_monitor\rule_manager::update_rule($updaterule);
     $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_updated', $event);
     $this->assertEquals(context_course::instance($course->id), $event->get_context());
     $this->assertEquals($rule->id, $event->objectid);
     $this->assertEventContextNotUsed($event);
     // Now let's update a system rule (courseid = 0).
     $createrule->courseid = 0;
     $rule = $monitorgenerator->create_rule($createrule);
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     $updaterule = new stdClass();
     $updaterule->id = $rule->id;
     \tool_monitor\rule_manager::update_rule($updaterule);
     $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_updated', $event);
     $this->assertEquals(context_system::instance(), $event->get_context());
 }