case 'subscribe': $rule = \tool_monitor\rule_manager::get_rule($ruleid); $rule->subscribe_user($courseid, $cmid); echo $OUTPUT->header(); echo $OUTPUT->notification(get_string('subcreatesuccess', 'tool_monitor'), 'notifysuccess'); break; case 'unsubscribe': // If the subscription does not exist, then redirect back as the subscription must have already been deleted. if (!($subscription = $DB->record_exists('tool_monitor_subscriptions', array('id' => $subscriptionid)))) { redirect(new moodle_url('/admin/tool/monitor/index.php', array('courseid' => $courseid))); } // Set the URLs. $confirmurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid, 'courseid' => $courseid, 'action' => 'unsubscribe', 'confirm' => true, 'sesskey' => sesskey())); $cancelurl = new moodle_url('/admin/tool/monitor/index.php', array('subscriptionid' => $subscriptionid, 'courseid' => $courseid, 'sesskey' => sesskey())); if ($confirm) { \tool_monitor\subscription_manager::delete_subscription($subscriptionid); echo $OUTPUT->header(); echo $OUTPUT->notification(get_string('subdeletesuccess', 'tool_monitor'), 'notifysuccess'); } else { $subscription = \tool_monitor\subscription_manager::get_subscription($subscriptionid); echo $OUTPUT->header(); echo $OUTPUT->confirm(get_string('subareyousure', 'tool_monitor', $subscription->get_name($coursecontext)), $confirmurl, $cancelurl); echo $OUTPUT->footer(); exit; } break; default: } } else { echo $OUTPUT->header(); }
/** * Test the subscription deleted event. */ public function test_subscription_deleted() { // Create the items we need to test this. $user = $this->getDataGenerator()->create_user(); $course = $this->getDataGenerator()->create_course(); $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); // Create a rule to subscribe to. $rule = $monitorgenerator->create_rule(); $sub = new stdClass(); $sub->courseid = $course->id; $sub->userid = $user->id; $sub->ruleid = $rule->id; // Create the subscription we are going to delete. $subscription = $monitorgenerator->create_subscription($sub); // Trigger and capture the event. $sink = $this->redirectEvents(); \tool_monitor\subscription_manager::delete_subscription($subscription->id, false); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event contains the expected values. $this->assertInstanceOf('\\tool_monitor\\event\\subscription_deleted', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEquals($subscription->id, $event->objectid); $this->assertEventContextNotUsed($event); // Now let's delete a system subscription. $sub = new stdClass(); $sub->courseid = 0; $sub->userid = $user->id; $sub->ruleid = $rule->id; // Create the subscription we are going to delete. $subscription = $monitorgenerator->create_subscription($sub); // Trigger and capture the event. $sink = $this->redirectEvents(); \tool_monitor\subscription_manager::delete_subscription($subscription->id, false); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Confirm that the event uses the system context. $this->assertInstanceOf('\\tool_monitor\\event\\subscription_deleted', $event); $this->assertEquals(context_system::instance(), $event->get_context()); // Now, create a bunch of subscriptions for the rule we created. $subids = array(); $sub->courseid = $course->id; for ($i = 1; $i <= 10; $i++) { $sub->userid = $i; $subscription = $monitorgenerator->create_subscription($sub); $subids[$subscription->id] = $subscription; } // Trigger and capture the events. $sink = $this->redirectEvents(); \tool_monitor\subscription_manager::remove_all_subscriptions_for_rule($rule->id); $events = $sink->get_events(); // Check that there were 10 events in total. $this->assertCount(10, $events); // Get all the events and ensure they are valid. foreach ($events as $event) { $this->assertInstanceOf('\\tool_monitor\\event\\subscription_deleted', $event); $this->assertEquals(context_course::instance($course->id), $event->get_context()); $this->assertEventContextNotUsed($event); $this->assertArrayHasKey($event->objectid, $subids); unset($subids[$event->objectid]); } // We should have found all the subscriptions. $this->assertEmpty($subids); }