Exemple #1
0
/**
 * Lockout user and send notification email.
 *
 * @param stdClass $user
 */
function login_lock_account($user)
{
    global $CFG;
    if ($user->mnethostid != $CFG->mnet_localhost_id) {
        return;
    }
    if (isguestuser($user)) {
        return;
    }
    if (get_user_preferences('login_lockout_ignored', 0, $user)) {
        // This user can not be locked out.
        return;
    }
    $alreadylockedout = get_user_preferences('login_lockout', 0, $user);
    set_user_preference('login_lockout', time(), $user);
    if ($alreadylockedout == 0) {
        $secret = random_string(15);
        set_user_preference('login_lockout_secret', $secret, $user);
        $oldforcelang = force_current_language($user->lang);
        $site = get_site();
        $supportuser = core_user::get_support_user();
        $data = new stdClass();
        $data->firstname = $user->firstname;
        $data->lastname = $user->lastname;
        $data->username = $user->username;
        $data->sitename = format_string($site->fullname);
        $data->link = $CFG->wwwroot . '/login/unlock_account.php?u=' . $user->id . '&s=' . $secret;
        $data->admin = generate_email_signoff();
        $message = get_string('lockoutemailbody', 'admin', $data);
        $subject = get_string('lockoutemailsubject', 'admin', format_string($site->fullname));
        if ($message) {
            // Directly email rather than using the messaging system to ensure its not routed to a popup or jabber.
            email_to_user($user, $supportuser, $subject, $message);
        }
        force_current_language($oldforcelang);
    }
}
Exemple #2
0
/**
 * Returns parent language of current active language if defined
 *
 * @category string
 * @param string $lang null means current language
 * @return string
 */
function get_parent_language($lang = null)
{
    // Let's hack around the current language.
    if (!empty($lang)) {
        $oldforcelang = force_current_language($lang);
    }
    $parentlang = get_string('parentlanguage', 'langconfig');
    if ($parentlang === 'en') {
        $parentlang = '';
    }
    // Let's hack around the current language.
    if (!empty($lang)) {
        force_current_language($oldforcelang);
    }
    return $parentlang;
}
    /**
     * Notify person responsible for enrolments that some user enrolments will be expired soon,
     * it is called only if notification of enrollers (aka teachers) is enabled in course.
     *
     * This is called repeatedly every day for each course if there are any pending expiration
     * in the expiration threshold.
     *
     * @param int $eid
     * @param array $users
     * @param progress_trace $trace
     */
    protected function notify_expiry_enroller($eid, $users, progress_trace $trace) {
        global $DB;

        $name = $this->get_name();

        $instance = $DB->get_record('enrol', array('id'=>$eid, 'enrol'=>$name));
        $context = context_course::instance($instance->courseid);
        $course = $DB->get_record('course', array('id'=>$instance->courseid));

        $enroller = $this->get_enroller($instance->id);
        $admin = get_admin();

        $oldforcelang = force_current_language($enroller->lang);

        foreach($users as $key=>$info) {
            $users[$key] = '* '.$info['fullname'].' - '.userdate($info['timeend'], '', $enroller->timezone);
        }

        $a = new stdClass();
        $a->course    = format_string($course->fullname, true, array('context'=>$context));
        $a->threshold = get_string('numdays', '', $instance->expirythreshold / (60*60*24));
        $a->users     = implode("\n", $users);
        $a->extendurl = (string)new moodle_url('/enrol/users.php', array('id'=>$instance->courseid));

        $subject = get_string('expirymessageenrollersubject', 'enrol_'.$name, $a);
        $body = get_string('expirymessageenrollerbody', 'enrol_'.$name, $a);

        $message = new stdClass();
        $message->notification      = 1;
        $message->component         = 'enrol_'.$name;
        $message->name              = 'expiry_notification';
        $message->userfrom          = $admin;
        $message->userto            = $enroller;
        $message->subject           = $subject;
        $message->fullmessage       = $body;
        $message->fullmessageformat = FORMAT_MARKDOWN;
        $message->fullmessagehtml   = markdown_to_html($body);
        $message->smallmessage      = $subject;
        $message->contexturlname    = $a->course;
        $message->contexturl        = $a->extendurl;

        if (message_send($message)) {
            $trace->output("notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
        } else {
            $trace->output("error notifying user $enroller->id about all expiring $name enrolments in course $instance->courseid", 1);
        }

        force_current_language($oldforcelang);
    }
Exemple #4
0
 /**
  * Process user enrolment line.
  *
  * @param progress_trace $trace
  * @param string $action
  * @param int $roleid
  * @param stdClass $user
  * @param stdClass $course
  * @param int $timestart
  * @param int $timeend
  * @param bool $buffer_if_future
  */
 protected function process_records(progress_trace $trace, $action, $roleid, $user, $course, $timestart, $timeend, $buffer_if_future = true)
 {
     global $CFG, $DB;
     // Check if timestart is for future processing.
     if ($timestart > time() and $buffer_if_future) {
         // Populate into enrol_flatfile table as a future role to be assigned by cron.
         // Note: since 2.0 future enrolments do not cause problems if you disable guest access.
         $future_en = new stdClass();
         $future_en->action = $action;
         $future_en->roleid = $roleid;
         $future_en->userid = $user->id;
         $future_en->courseid = $course->id;
         $future_en->timestart = $timestart;
         $future_en->timeend = $timeend;
         $future_en->timemodified = time();
         $DB->insert_record('enrol_flatfile', $future_en);
         $trace->output("User {$user->id} will be enrolled later into course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1);
         return;
     }
     $context = context_course::instance($course->id);
     if ($action === 'add') {
         // Clear the buffer just in case there were some future enrolments.
         $DB->delete_records('enrol_flatfile', array('userid' => $user->id, 'courseid' => $course->id, 'roleid' => $roleid));
         $instance = $DB->get_record('enrol', array('courseid' => $course->id, 'enrol' => 'flatfile'));
         if (empty($instance)) {
             // Only add an enrol instance to the course if non-existent.
             $enrolid = $this->add_instance($course);
             $instance = $DB->get_record('enrol', array('id' => $enrolid));
         }
         $notify = false;
         if ($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $user->id))) {
             // Update only.
             $this->update_user_enrol($instance, $user->id, ENROL_USER_ACTIVE, $timestart, $timeend);
             if (!$DB->record_exists('role_assignments', array('contextid' => $context->id, 'roleid' => $roleid, 'userid' => $user->id, 'component' => 'enrol_flatfile', 'itemid' => $instance->id))) {
                 role_assign($roleid, $user->id, $context->id, 'enrol_flatfile', $instance->id);
             }
             $trace->output("User {$user->id} enrolment updated in course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1);
         } else {
             // Enrol the user with this plugin instance.
             $this->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
             $trace->output("User {$user->id} enrolled in course {$course->id} using role {$roleid} ({$timestart}, {$timeend})", 1);
             $notify = true;
         }
         if ($notify and $this->get_config('mailstudents')) {
             $oldforcelang = force_current_language($user->lang);
             // Send welcome notification to enrolled users.
             $a = new stdClass();
             $a->coursename = format_string($course->fullname, true, array('context' => $context));
             $a->profileurl = "{$CFG->wwwroot}/user/view.php?id={$user->id}&course={$course->id}";
             $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context)));
             $eventdata = new stdClass();
             $eventdata->modulename = 'moodle';
             $eventdata->component = 'enrol_flatfile';
             $eventdata->name = 'flatfile_enrolment';
             $eventdata->userfrom = $this->get_enroller($course->id);
             $eventdata->userto = $user;
             $eventdata->subject = $subject;
             $eventdata->fullmessage = get_string('welcometocoursetext', '', $a);
             $eventdata->fullmessageformat = FORMAT_PLAIN;
             $eventdata->fullmessagehtml = '';
             $eventdata->smallmessage = '';
             if (message_send($eventdata)) {
                 $trace->output("Notified enrolled user", 1);
             } else {
                 $trace->output("Failed to notify enrolled user", 1);
             }
             force_current_language($oldforcelang);
         }
         if ($notify and $this->get_config('mailteachers', 0)) {
             // Notify person responsible for enrolments.
             $enroller = $this->get_enroller($course->id);
             $oldforcelang = force_current_language($enroller->lang);
             $a = new stdClass();
             $a->course = format_string($course->fullname, true, array('context' => $context));
             $a->user = fullname($user);
             $subject = get_string('enrolmentnew', 'enrol', format_string($course->shortname, true, array('context' => $context)));
             $eventdata = new stdClass();
             $eventdata->modulename = 'moodle';
             $eventdata->component = 'enrol_flatfile';
             $eventdata->name = 'flatfile_enrolment';
             $eventdata->userfrom = get_admin();
             $eventdata->userto = $enroller;
             $eventdata->subject = $subject;
             $eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a);
             $eventdata->fullmessageformat = FORMAT_PLAIN;
             $eventdata->fullmessagehtml = '';
             $eventdata->smallmessage = '';
             if (message_send($eventdata)) {
                 $trace->output("Notified enroller {$eventdata->userto->id}", 1);
             } else {
                 $trace->output("Failed to notify enroller {$eventdata->userto->id}", 1);
             }
             force_current_language($oldforcelang);
         }
         return;
     } else {
         if ($action === 'del') {
             // Clear the buffer just in case there were some future enrolments.
             $DB->delete_records('enrol_flatfile', array('userid' => $user->id, 'courseid' => $course->id, 'roleid' => $roleid));
             $action = $this->get_config('unenrolaction');
             if ($action == ENROL_EXT_REMOVED_KEEP) {
                 $trace->output("del action is ignored", 1);
                 return;
             }
             // Loops through all enrolment methods, try to unenrol if roleid somehow matches.
             $instances = $DB->get_records('enrol', array('courseid' => $course->id));
             $unenrolled = false;
             foreach ($instances as $instance) {
                 if (!($ue = $DB->get_record('user_enrolments', array('enrolid' => $instance->id, 'userid' => $user->id)))) {
                     continue;
                 }
                 if ($instance->enrol === 'flatfile') {
                     $plugin = $this;
                 } else {
                     if (!enrol_is_enabled($instance->enrol)) {
                         continue;
                     }
                     if (!($plugin = enrol_get_plugin($instance->enrol))) {
                         continue;
                     }
                     if (!$plugin->allow_unenrol_user($instance, $ue)) {
                         continue;
                     }
                 }
                 // For some reason the del action includes a role name, this complicates everything.
                 $componentroles = array();
                 $manualroles = array();
                 $ras = $DB->get_records('role_assignments', array('userid' => $user->id, 'contextid' => $context->id));
                 foreach ($ras as $ra) {
                     if ($ra->component === '') {
                         $manualroles[$ra->roleid] = $ra->roleid;
                     } else {
                         if ($ra->component === 'enrol_' . $instance->enrol and $ra->itemid == $instance->id) {
                             $componentroles[$ra->roleid] = $ra->roleid;
                         }
                     }
                 }
                 if ($componentroles and !isset($componentroles[$roleid])) {
                     // Do not unenrol using this method, user has some other protected role!
                     continue;
                 } else {
                     if (empty($ras)) {
                         // If user does not have any roles then let's just suspend as many methods as possible.
                     } else {
                         if (!$plugin->roles_protected()) {
                             if (!$componentroles and $manualroles and !isset($manualroles[$roleid])) {
                                 // Most likely we want to keep users enrolled because they have some other course roles.
                                 continue;
                             }
                         }
                     }
                 }
                 if ($action == ENROL_EXT_REMOVED_UNENROL) {
                     $unenrolled = true;
                     if (!$plugin->roles_protected()) {
                         role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'roleid' => $roleid, 'component' => '', 'itemid' => 0), true);
                     }
                     $plugin->unenrol_user($instance, $user->id);
                     $trace->output("User {$user->id} was unenrolled from course {$course->id} (enrol_{$instance->enrol})", 1);
                 } else {
                     if ($action == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
                         if ($plugin->allow_manage($instance)) {
                             if ($ue->status == ENROL_USER_ACTIVE) {
                                 $unenrolled = true;
                                 $plugin->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
                                 if (!$plugin->roles_protected()) {
                                     role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'component' => 'enrol_' . $instance->enrol, 'itemid' => $instance->id), true);
                                     role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'roleid' => $roleid, 'component' => '', 'itemid' => 0), true);
                                 }
                                 $trace->output("User {$user->id} enrolment was suspended in course {$course->id} (enrol_{$instance->enrol})", 1);
                             }
                         }
                     }
                 }
             }
             if (!$unenrolled) {
                 if (0 == $DB->count_records('role_assignments', array('userid' => $user->id, 'contextid' => $context->id))) {
                     role_unassign_all(array('contextid' => $context->id, 'userid' => $user->id, 'component' => '', 'itemid' => 0), true);
                 }
                 $trace->output("User {$user->id} (with role {$roleid}) not unenrolled from course {$course->id}", 1);
             }
             return;
         }
     }
 }
/**
 * Construct an array with subtitution rules for mail templates, relating to
 * a single appointment. Any of the parameters can be null.
 * @param scheduler_instance $scheduler The scheduler instance
 * @param scheduler_slot $slot The slot data as an MVC object
 * @param user $attendant A {@link $USER} object describing the attendant (teacher)
 * @param user $attendee A {@link $USER} object describing the attendee (student)
 * @param object $course A course object relating to the ontext of the message
 * @param object $recipient A {@link $USER} object describing the recipient of the message (used for determining the message language)
 * @return array A hash with mail template substitutions
 */
function scheduler_get_mail_variables(scheduler_instance $scheduler, scheduler_slot $slot, $attendant, $attendee, $course, $recipient)
{
    global $CFG;
    $lang = scheduler_get_message_language($recipient, $course);
    // Force any string formatting to happen in the target language.
    $oldlang = force_current_language($lang);
    $tz = core_date::get_user_timezone($recipient);
    $vars = array();
    if ($scheduler) {
        $vars['MODULE'] = $scheduler->name;
        $vars['STAFFROLE'] = $scheduler->get_teacher_name();
        $vars['SCHEDULER_URL'] = $CFG->wwwroot . '/mod/scheduler/view.php?id=' . $scheduler->cmid;
    }
    if ($slot) {
        $vars['DATE'] = userdate($slot->starttime, get_string('strftimedate'), $tz);
        $vars['TIME'] = userdate($slot->starttime, get_string('strftimetime'), $tz);
        $vars['ENDTIME'] = userdate($slot->endtime, get_string('strftimetime'), $tz);
        $vars['LOCATION'] = format_string($slot->appointmentlocation);
    }
    if ($attendant) {
        $vars['ATTENDANT'] = fullname($attendant);
        $vars['ATTENDANT_URL'] = $CFG->wwwroot . '/user/view.php?id=' . $attendant->id . '&course=' . $scheduler->course;
    }
    if ($attendee) {
        $vars['ATTENDEE'] = fullname($attendee);
        $vars['ATTENDEE_URL'] = $CFG->wwwroot . '/user/view.php?id=' . $attendee->id . '&course=' . $scheduler->course;
    }
    // Reset language settings.
    force_current_language($oldlang);
    return $vars;
}