/** * Who has a capability in a CM context? Uses accesslib's * get_users_by_capability, but augments the result with the CM system's fake * hierarchy. */ function cm_get_users_by_capability($contextlevel, $instance_id, $capability) { static $cm_context_extra_parents = array('course' => array('curriculum'), 'class' => array('track'), 'user' => array('cluster')); $contextlevelnum = context_level_base::get_custom_context_level($contextlevel, 'block_curr_admin'); $context = get_context_instance($contextlevelnum, $instance_id); $users = get_users_by_capability($context, $capability); $users = $users ? $users : array(); // look in the parent contexts if (isset($cm_context_extra_parents[$contextlevel])) { foreach ($cm_context_extra_parents[$contextlevel] as $parentlevel) { $parents = call_user_func("_cmctx_get_{$parentlevel}_containing_{$contextlevel}", $instance_id); foreach ($parents as $parentid => $parent) { $newusers = cm_get_users_by_capability($parentlevel, $parentid, $capability); if ($newusers) { $users = $users + $newusers; } } } } return $users; }
/** * Function to handle course recurrence events. * * @param user $user CM user object representing the user in the course * * @return boolean TRUE is successful, otherwise FALSE */ public static function course_recurrence_handler($user) { global $CFG, $CURMAN; require_once $CFG->dirroot . '/curriculum/lib/notifications.php'; /// Does the user receive a notification? $sendtouser = $CURMAN->config->notify_courserecurrence_user; $sendtorole = $CURMAN->config->notify_courserecurrence_role; $sendtosupervisor = $CURMAN->config->notify_courserecurrence_supervisor; /// If nobody receives a notification, we're done. if (!$sendtouser && !$sendtorole && !$sendtosupervisor) { return true; } $context = get_system_context(); /// Make sure this is a valid user. $enroluser = new user($user->id); if (empty($enroluser->id)) { print_error('nouser', 'block_curr_admin'); return true; } $message = new notification(); /// Set up the text of the message $text = empty($CURMAN->config->notify_courserecurrence_message) ? get_string('notifycourserecurrencemessagedef', 'block_curr_admin') : $CURMAN->config->notify_courserecurrence_message; $search = array('%%userenrolname%%', '%%coursename%%'); $replace = array(fullname($user), $user->coursename); $text = str_replace($search, $replace, $text); $eventlog = new Object(); $eventlog->event = 'course_recurrence'; $eventlog->instance = $user->enrolmentid; $eventlog->fromuserid = $user->id; if ($sendtouser) { $message->send_notification($text, $user, null, $eventlog); } $users = array(); if ($sendtorole) { /// Get all users with the notify_courserecurrence capability. if ($roleusers = get_users_by_capability($context, 'block/curr_admin:notify_courserecurrence')) { $users = $users + $roleusers; } } if ($sendtosupervisor) { /// Get parent-context users. if ($supervisors = cm_get_users_by_capability('user', $user->id, 'block/curr_admin:notify_courserecurrence')) { $users = $users + $supervisors; } } foreach ($users as $u) { $message->send_notification($text, $u, $enroluser, $eventlog); } return true; }
/** * Triggered when a track assignment takes place. * This function should use the CM configured values to send messages to appropriate users when a track assignment * takes place. Users will be ones configured for the context, which can include the user that is assigned and users * assigned to configured roles for that context. The message template used should be the one configured as well. * * @param object $eventdata the track assignment record * @return boolean success * */ function cm_notify_track_assign_handler($eventdata) { global $CFG, $USER, $CURMAN; /// Does the user receive a notification? $sendtouser = isset($CURMAN->config->notify_trackenrol_user) ? $CURMAN->config->notify_trackenrol_user : ''; $sendtorole = isset($CURMAN->config->notify_trackenrol_role) ? $CURMAN->config->notify_trackenrol_role : ''; $sendtosupervisor = isset($CURMAN->config->notify_trackenrol_supervisor) ? $CURMAN->config->notify_trackenrol_supervisor : ''; /// If nobody receives a notification, we're done. if (!$sendtouser && !$sendtorole && !$sendtosupervisor) { return true; } /// We get all context assigns, so check that this is a class. If not, we're done. $context = get_system_context(); /// Make sure this is a valid user. $enroluser = new user($eventdata->userid); if (empty($enroluser->id)) { print_error('nouser', 'block_curr_admin'); return true; } /// Get the track record from the track id. if (!($track = get_record('crlm_track', 'id', $eventdata->trackid))) { print_error('notrack', 'block_curr_admin'); return true; } $message = new notification(); /// Set up the text of the message $text = empty($CURMAN->config->notify_trackenrol_message) ? get_string('notifytrackenrolmessagedef', 'block_curr_admin') : $CURMAN->config->notify_trackenrol_message; $search = array('%%userenrolname%%', '%%trackname%%'); $replace = array(fullname($enroluser), $track->name); $text = str_replace($search, $replace, $text); if ($sendtouser) { $message->send_notification($text, $enroluser); } $users = array(); if ($sendtorole) { /// Get all users with the notify_trackenrol capability. if ($roleusers = get_users_by_capability($context, 'block/curr_admin:notify_trackenrol')) { $users = $users + $roleusers; } } if ($sendtosupervisor) { /// Get parent-context users. if ($supervisors = cm_get_users_by_capability('user', $eventdata->userid, 'block/curr_admin:notify_trackenrol')) { $users = $users + $supervisors; } } foreach ($users as $user) { $message->send_notification($text, $user, $enroluser); } /// If you don't return true, the message queue will clog and no more will be sent. return true; }