/** * Generates the message object for a give subscription and event. * * @param int $subscriptionid Subscription instance * @param \stdClass $eventobj Event data * * @return false|\stdClass message object */ protected function generate_message($subscriptionid, \stdClass $eventobj) { try { $subscription = subscription_manager::get_subscription($subscriptionid); } catch (\dml_exception $e) { // Race condition, someone deleted the subscription. return false; } $user = \core_user::get_user($subscription->userid); $context = \context_user::instance($user->id, IGNORE_MISSING); if ($context === false) { // User context doesn't exist. Should never happen, nothing to do return. return false; } $template = $subscription->template; $template = $this->replace_placeholders($template, $subscription, $eventobj, $context); $msgdata = new \stdClass(); $msgdata->component = 'tool_monitor'; // Your component name. $msgdata->name = 'notification'; // This is the message name from messages.php. $msgdata->userfrom = \core_user::get_noreply_user(); $msgdata->userto = $user; $msgdata->subject = $subscription->get_name($context); $msgdata->fullmessage = format_text($template, $subscription->templateformat, array('context' => $context)); $msgdata->fullmessageformat = $subscription->templateformat; $msgdata->fullmessagehtml = format_text($template, $subscription->templateformat, array('context' => $context)); $msgdata->smallmessage = ''; $msgdata->notification = 1; // This is only set to 0 for personal messages between users. return $msgdata; }
/** * Observer that monitors course module deleted event and delete user subscriptions. * * @param \core\event\course_module_deleted $event the event object. */ public static function course_module_deleted(\core\event\course_module_deleted $event) { $cmid = $event->contextinstanceid; subscription_manager::delete_cm_subscriptions($cmid); }
/** * Delete a rule and associated subscriptions, by rule id. * * @param int $ruleid id of rule to be deleted. * @param \context|null $coursecontext the context of the course - this is passed when we * can not get the context via \context_course as the course has been deleted. * * @return bool */ public static function delete_rule($ruleid, $coursecontext = null) { global $DB; subscription_manager::remove_all_subscriptions_for_rule($ruleid, $coursecontext); // Retrieve the rule from the DB before we delete it, so we have a record when we trigger a rule deleted event. $rule = $DB->get_record('tool_monitor_rules', array('id' => $ruleid)); $success = $DB->delete_records('tool_monitor_rules', array('id' => $ruleid)); // If successful trigger a rule deleted event. if ($success) { // It is possible that we are deleting rules associated with a deleted course, so we should be // passing the context as the second parameter. if (!is_null($coursecontext)) { $context = $coursecontext; $courseid = $rule->courseid; } else { if (!empty($rule->courseid) && ($context = \context_course::instance($rule->courseid, IGNORE_MISSING))) { $courseid = $rule->courseid; } else { $courseid = 0; $context = \context_system::instance(); } } $params = array('objectid' => $rule->id, 'courseid' => $courseid, 'context' => $context); $event = \tool_monitor\event\rule_deleted::create($params); $event->add_record_snapshot('tool_monitor_rules', $rule); $event->trigger(); } return $success; }
/** * 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); }