Пример #1
0
 /**
  * Test user capability check.
  */
 public function test_usercapabilitycheck()
 {
     global $DB, $USER;
     $role = $DB->get_record('role', array('shortname' => 'editingteacher'));
     // Assign the test user the editing teacher role on a test cluster.
     $ctx = \local_elisprogram\context\user::instance($this->tuserid);
     $this->assertNotEmpty(role_assign($role->id, $this->mdluserid, $ctx->id));
     load_role_access_by_context($role->id, $ctx, $USER->access);
     // We need to force the accesslib cache to refresh.
     // Validate the return value when looking at the 'user' level.
     $contextsuser = new pm_context_set();
     $contextsuser->contexts = array('user' => array($this->tuserid));
     $contextsuser->contextlevel = 'user';
     $contexts = pm_context_set::for_user_with_capability('user', 'local/elisprogram:userset_enrol_userset_user', $this->mdluserid);
     $this->assertEquals($contextsuser, $contexts);
     // Validate checking for users with the given capability on this context.
     $users = pm_get_users_by_capability('user', $this->tuserid, 'local/elisprogram:userset_enrol_userset_user');
     $this->assertEquals($this->mdluserid, current($users)->id);
 }
Пример #2
0
/**
 * 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 pm_get_users_by_capability($contextlevel, $instance_id, $capability)
{
    static $pm_context_extra_parents = array('course' => array('curriculum'), 'class' => array('track'), 'user' => array('cluster'));
    $ctxclass = \local_eliscore\context\helper::get_class_for_level(\local_eliscore\context\helper::get_level_from_name($contextlevel));
    $context = $ctxclass::instance($instance_id);
    $users = get_users_by_capability($context, $capability);
    $users = $users ? $users : array();
    // look in the parent contexts
    if (isset($pm_context_extra_parents[$contextlevel])) {
        foreach ($pm_context_extra_parents[$contextlevel] as $parentlevel) {
            $parents = call_user_func("_cmctx_get_{$parentlevel}_containing_{$contextlevel}", $instance_id);
            foreach ($parents as $parentid => $parent) {
                $newusers = pm_get_users_by_capability($parentlevel, $parentid, $capability);
                if ($newusers) {
                    $users = $users + $newusers;
                }
            }
            unset($parents);
        }
    }
    return $users;
}
Пример #3
0
 /**
  * Function to handle class not completed events.
  *
  * @param   student  $student  The class enrolment / student object who is "not completed"
  * @uses    $CFG
  * @uses    $DB
  * @return  boolean            TRUE is successful, otherwise FALSE
  */
 public static function class_notcompleted_handler($student)
 {
     global $CFG, $DB;
     require_once elispm::lib('notifications.php');
     /// Does the user receive a notification?
     $sendtouser = elis::$config->local_elisprogram->notify_classnotcompleted_user;
     $sendtorole = elis::$config->local_elisprogram->notify_classnotcompleted_role;
     $sendtosupervisor = elis::$config->local_elisprogram->notify_classnotcompleted_supervisor;
     /// If nobody receives a notification, we're done.
     if (!$sendtouser && !$sendtorole && !$sendtosupervisor) {
         return true;
     }
     if (!empty($student->moodlecourseid)) {
         if (!($context = context_course::instance($student->moodlecourseid))) {
             if (in_cron()) {
                 mtrace(get_string('invalidcontext'));
             } else {
                 debugging(get_string('invalidcontext'));
             }
             return true;
         }
     } else {
         $context = context_system::instance();
     }
     $message = new notification();
     /// Set up the text of the message
     $text = empty(elis::$config->local_elisprogram->notify_classnotcompleted_message) ? get_string('notifyclassnotcompletedmessagedef', self::LANG_FILE) : elis::$config->local_elisprogram->notify_classnotcompleted_message;
     $search = array('%%userenrolname%%', '%%classname%%', '%%coursename%%');
     $user = new user($student->userid);
     if (!$user) {
         if (in_cron()) {
             mtrace(get_string('nouser', 'local_elisprogram'));
         } else {
             debugging(get_string('nouser', 'local_elisprogram'));
         }
         return true;
     }
     $user->load();
     // Get course info
     $pmcourse = $DB->get_record(course::TABLE, array('id' => $student->courseid));
     $pmclass = $DB->get_record(pmclass::TABLE, array('id' => $student->classid));
     $replace = array($user->moodle_fullname(), $pmclass->idnumber, $pmcourse->name);
     $text = str_replace($search, $replace, $text);
     $eventlog = new Object();
     $eventlog->event = 'class_notcompleted';
     $eventlog->instance = $student->classid;
     $eventlog->fromuserid = $user->id;
     if ($sendtouser) {
         $message->send_notification($text, $user, null, $eventlog);
     }
     $users = array();
     if ($sendtorole) {
         /// Get all users with the notify_classnotcomplete capability.
         if ($roleusers = get_users_by_capability($context, 'local/elisprogram:notify_classnotcomplete')) {
             $users = $users + $roleusers;
         }
     }
     if ($sendtosupervisor) {
         /// Get parent-context users.
         if ($supervisors = pm_get_users_by_capability('user', $user->id, 'local/elisprogram:notify_classnotcomplete')) {
             $users = $users + $supervisors;
         }
     }
     // Send notifications to any users who need to receive them.
     foreach ($users as $touser) {
         $message->send_notification($text, $touser, $user, $eventlog);
     }
     return true;
 }
Пример #4
0
 /**
  * Function to handle curriculum not completed events.
  *
  */
 public static function curriculum_notcompleted_handler($curstudent)
 {
     global $CFG, $DB;
     require_once elispm::lib('notifications.php');
     /// Does the user receive a notification?
     $sendtouser = elis::$config->local_elisprogram->notify_curriculumnotcompleted_user;
     $sendtorole = elis::$config->local_elisprogram->notify_curriculumnotcompleted_role;
     $sendtosupervisor = elis::$config->local_elisprogram->notify_curriculumnotcompleted_supervisor;
     /// If nobody receives a notification, we're done.
     if (!$sendtouser && !$sendtorole && !$sendtosupervisor) {
         return true;
     }
     $context = context_system::instance();
     // Send notifications
     $message = new notification();
     /// Set up the text of the message
     $text = empty(elis::$config->local_elisprogram->notify_curriculumnotcompleted_message) ? get_string('notifycurriculumnotcompletedmessagedef', 'local_elisprogram') : elis::$config->local_elisprogram->notify_curriculumnotcompleted_message;
     $user = new user($curstudent->userid);
     if (!$user) {
         return true;
     }
     $user->load();
     // Get course info
     $program = $DB->get_record(curriculum::TABLE, array('id' => $curstudent->curriculumid));
     $search = array('%%userenrolname%%', '%%programname%%');
     $replace = array($user->moodle_fullname(), $program->name);
     $text = str_replace($search, $replace, $text);
     $eventlog = new Object();
     $eventlog->event = 'curriculum_notcompleted';
     $eventlog->instance = $curstudent->id;
     /// Store the assignment id.
     $eventlog->fromuserid = $user->id;
     if ($sendtouser) {
         $message->send_notification($text, $user, null, $eventlog);
     }
     $users = array();
     if ($sendtorole) {
         /// Get all users with the notify_curriculumnotcomplete capability.
         if ($roleusers = get_users_by_capability($context, 'local/elisprogram:notify_programnotcomplete')) {
             $users = $users + $roleusers;
         }
     }
     if ($sendtosupervisor) {
         /// Get parent-context users.
         if ($supervisors = pm_get_users_by_capability('user', $user->id, 'local/elisprogram:notify_programnotcomplete')) {
             $users = $users + $supervisors;
         }
     }
     foreach ($users as $u) {
         $message->send_notification($text, $u, $user, $eventlog);
     }
     return true;
 }
Пример #5
0
/**
 * 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 pm_notify_track_assign_handler($eventdata)
{
    global $CFG, $DB, $USER;
    /// Does the user receive a notification?
    $sendtouser = isset(elis::$config->local_elisprogram->notify_trackenrol_user) ? elis::$config->local_elisprogram->notify_trackenrol_user : '';
    $sendtorole = isset(elis::$config->local_elisprogram->notify_trackenrol_role) ? elis::$config->local_elisprogram->notify_trackenrol_role : '';
    $sendtosupervisor = isset(elis::$config->local_elisprogram->notify_trackenrol_supervisor) ? elis::$config->local_elisprogram->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 = context_system::instance();
    /// Make sure this is a valid user.
    $enroluser = new user($eventdata->userid);
    if (!$enroluser) {
        if (in_cron()) {
            mtrace(getstring('nouser', 'local_elisprogram'));
        } else {
            print_error('nouser', 'local_elisprogram');
        }
        return true;
    }
    // Due to lazy loading, we need to pre-load this object
    $enroluser->load();
    if (empty($enroluser->id)) {
        if (in_cron()) {
            mtrace(getstring('nouser', 'local_elisprogram'));
        } else {
            print_error('nouser', 'local_elisprogram');
        }
        return true;
    }
    /// Get the track record from the track id.
    if (!($track = $DB->get_record('local_elisprogram_trk', array('id' => $eventdata->trackid)))) {
        if (in_cron()) {
            mtrace(get_string('notrack', 'local_elisprogram'));
        } else {
            print_error('notrack', 'local_elisprogram');
        }
        return true;
    }
    $message = new notification();
    /// Set up the text of the message
    $text = empty(elis::$config->local_elisprogram->notify_trackenrol_message) ? get_string('notifytrackenrolmessagedef', 'local_elisprogram') : elis::$config->local_elisprogram->notify_trackenrol_message;
    $search = array('%%userenrolname%%', '%%trackname%%');
    $replace = array($enroluser->moodle_fullname(), $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, 'local/elisprogram:notify_trackenrol')) {
            $users = $users + $roleusers;
        }
    }
    if ($sendtosupervisor) {
        /// Get parent-context users.
        if ($supervisors = pm_get_users_by_capability('user', $eventdata->userid, 'local/elisprogram: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;
}
Пример #6
0
 /**
  * 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 $DB;
     require_once elispm::lib('notifications.php');
     /// Does the user receive a notification?
     $sendtouser = elis::$config->local_elisprogram->notify_courserecurrence_user;
     $sendtorole = elis::$config->local_elisprogram->notify_courserecurrence_role;
     $sendtosupervisor = elis::$config->local_elisprogram->notify_courserecurrence_supervisor;
     /// If nobody receives a notification, we're done.
     if (!$sendtouser && !$sendtorole && !$sendtosupervisor) {
         return true;
     }
     $context = context_system::instance();
     /// Make sure this is a valid user.
     $enroluser = new user($user->userid);
     if (!$enroluser) {
         if (in_cron()) {
             mtrace(get_string('nouser', 'local_elisprogram'));
         } else {
             print_error('nouser', 'local_elisprogram');
         }
         return true;
     }
     // Due to lazy loading, we need to pre-load this object
     $enroluser->load();
     if (empty($enroluser->id)) {
         if (in_cron()) {
             mtrace(get_string('nouser', 'local_elisprogram'));
         } else {
             print_error('nouser', 'local_elisprogram');
         }
         return true;
     }
     /// Set up the text of the message
     $message = new notification();
     $text = empty(elis::$config->local_elisprogram->notify_courserecurrence_message) ? get_string('notifycourserecurrencemessagedef', 'local_elisprogram') : elis::$config->local_elisprogram->notify_courserecurrence_message;
     $search = array('%%userenrolname%%', '%%coursename%%');
     $replace = array($enroluser->moodle_fullname(), $user->coursename);
     $text = str_replace($search, $replace, $text);
     $eventlog = new Object();
     $eventlog->event = 'course_recurrence';
     $eventlog->instance = $user->enrolmentid;
     $eventlog->fromuserid = $enroluser->id;
     if ($sendtouser) {
         $message->send_notification($text, $enroluser, null, $eventlog);
     }
     $users = array();
     if ($sendtorole) {
         /// Get all users with the notify_courserecurrence capability.
         if ($roleusers = get_users_by_capability($context, 'local/elisprogram:notify_courserecurrence')) {
             $users = $users + $roleusers;
         }
     }
     if ($sendtosupervisor) {
         /// Get parent-context users.
         if ($supervisors = pm_get_users_by_capability('user', $enroluser->id, 'local/elisprogram:notify_courserecurrence')) {
             $users = $users + $supervisors;
         }
     }
     foreach ($users as $u) {
         $message->send_notification($text, $u, $enroluser, $eventlog);
     }
     return true;
 }