From 48170f539e2e4eef3806dbfec87b36dd2b97ebea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Trouv=C3=A9?= Date: Thu, 3 Aug 2017 18:52:09 +0200 Subject: [PATCH] Implements enrolperiod Implements enrollib feature which allow to automatically unenrol users, based on an enrolment duration. --- edit.php | 36 +++++++++++++++++++----------------- edit_form.php | 5 +++++ lang/en/enrol_apply.php | 10 +++++++++- lib.php | 40 +++++++++++++++++++++++++++++----------- settings.php | 21 ++++++++++++++++++++- 5 files changed, 82 insertions(+), 30 deletions(-) diff --git a/edit.php b/edit.php index e0a57c8..290626f 100644 --- a/edit.php +++ b/edit.php @@ -88,27 +88,29 @@ if ($mform->is_cancelled()) { // Convert back to string for storing in enrol table. $data->customtext2 = implode(',', $notify); if ($instance->id) { - $instance->status = $data->status; - $instance->name = $data->name; - $instance->customtext1 = $data->customtext1; - $instance->customtext2 = $data->customtext2; - $instance->customint1 = $data->customint1; - $instance->customint2 = $data->customint2; - $instance->customint3 = $data->customint3; - $instance->roleid = $data->roleid; + $instance->status = $data->status; + $instance->name = $data->name; + $instance->customtext1 = $data->customtext1; + $instance->customtext2 = $data->customtext2; + $instance->customint1 = $data->customint1; + $instance->customint2 = $data->customint2; + $instance->customint3 = $data->customint3; + $instance->roleid = $data->roleid; + $instance->enrolperiod = $data->enrolperiod; $instance->timemodified = time(); $DB->update_record('enrol', $instance); - } else { $fields = array( - 'status' => $data->status, - 'name' => $data->name, - 'roleid' => $data->roleid, - 'customint1' => $data->customint1, - 'customint2' => $data->customint2, - 'customint3' => $data->customint3, - 'customtext1' => $data->customtext1, - 'customtext2' => $data->customtext2); + 'status' => $data->status, + 'name' => $data->name, + 'roleid' => $data->roleid, + 'customint1' => $data->customint1, + 'customint2' => $data->customint2, + 'customint3' => $data->customint3, + 'customtext1' => $data->customtext1, + 'customtext2' => $data->customtext2, + 'enrolperiod' => $data->enrolperiod + ); $plugin->add_instance($course, $fields); } diff --git a/edit_form.php b/edit_form.php index 8853217..db7efae 100644 --- a/edit_form.php +++ b/edit_form.php @@ -77,6 +77,11 @@ class enrol_apply_edit_form extends moodleform { $mform->setType('customint3', PARAM_INT); $mform->setDefault('customint3', $plugin->get_config('customint3')); + $options = array('optional' => true, 'defaultunit' => 86400); + $mform->addElement('duration', 'enrolperiod', get_string('defaultperiod', 'enrol_apply'), $options); + $mform->setDefault('enrolperiod', $plugin->get_config('enrolperiod')); + $mform->addHelpButton('enrolperiod', 'defaultperiod', 'enrol_apply'); + $mform->addElement('hidden', 'id'); $mform->setType('id', PARAM_INT); $mform->addElement('hidden', 'courseid'); diff --git a/lang/en/enrol_apply.php b/lang/en/enrol_apply.php index b464561..ec2c31a 100644 --- a/lang/en/enrol_apply.php +++ b/lang/en/enrol_apply.php @@ -32,7 +32,7 @@ $string['confirmmail_desc'] = ''; $string['confirmmailsubject'] = 'Confirmation email subject'; $string['confirmmailsubject_desc'] = ''; $string['confirmmailcontent'] = 'Confirmation email content'; -$string['confirmmailcontent_desc'] = 'Please use the following special marks to replace email content with data from Moodle.
{firstname}:The first name of the user; {content}:The course name;{lastname}:The last name of the user;{username}:The users registration username'; +$string['confirmmailcontent_desc'] = 'Please use the following special marks to replace email content with data from Moodle.
{firstname}:The first name of the user; {content}:The course name;{lastname}:The last name of the user;{username}:The users registration username;{timeend}: The enrolment expiration date'; $string['waitmail_heading'] = 'Waiting list email'; $string['waitmail_desc'] = ''; @@ -106,3 +106,11 @@ $string['maxenrolledreached_right'] = 'has already been reached.'; $string['maxenrolled_tip_1'] = 'out of'; $string['maxenrolled_tip_2'] = 'seats already booked.'; + +$string['defaultperiod'] = 'Default enrolment duration'; +$string['defaultperiod_desc'] = 'Default length of time that the enrolment is valid. If set to zero, the enrolment duration will be unlimited by default.'; +$string['defaultperiod_help'] = 'Default length of time that the enrolment is valid, starting with the moment the user is enrolled. If disabled, the enrolment duration will be unlimited by default.'; +$string['expiry_heading'] = 'Expiry settings'; +$string['expiry_desc'] = ''; +$string['expiredaction'] = 'Enrolment expiry action'; +$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.'; diff --git a/lib.php b/lib.php index c8ae0f1..17abb1d 100644 --- a/lib.php +++ b/lib.php @@ -235,6 +235,7 @@ class enrol_apply_plugin extends enrol_plugin { $fields['customint1'] = $this->get_config('show_standard_user_profile'); $fields['customint2'] = $this->get_config('show_extra_user_profile'); $fields['customtext2'] = $this->get_config('notifycoursebased') ? '$@ALL@$' : ''; + $fields['enrolperiod'] = $this->get_config('enrolperiod', 0); return $fields; } @@ -260,12 +261,19 @@ class enrol_apply_plugin extends enrol_plugin { continue; } - $this->update_user_enrol($instance, $userenrolment->userid, ENROL_USER_ACTIVE, time()); + // Set timestart and timeend if an enrolment duration is set. + $userenrolment->timestart = time(); + $userenrolment->timeend = 0; + if ($instance->enrolperiod) { + $userenrolment->timeend = $userenrolment->timestart + $instance->enrolperiod; + } + + $this->update_user_enrol($instance, $userenrolment->userid, ENROL_USER_ACTIVE, $userenrolment->timestart, $userenrolment->timeend); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol)); $this->notify_applicant( $instance, - $userenrolment->userid, + $userenrolment, 'confirmation', get_config('enrol_apply', 'confirmmailsubject'), get_config('enrol_apply', 'confirmmailcontent')); @@ -293,7 +301,7 @@ class enrol_apply_plugin extends enrol_plugin { $this->notify_applicant( $instance, - $userenrolment->userid, + $userenrolment, 'waitinglist', get_config('enrol_apply', 'waitmailsubject'), get_config('enrol_apply', 'waitmailcontent')); @@ -327,23 +335,23 @@ class enrol_apply_plugin extends enrol_plugin { $this->notify_applicant( $instance, - $userenrolment->userid, + $userenrolment, 'cancelation', get_config('enrol_apply', 'cancelmailsubject'), get_config('enrol_apply', 'cancelmailcontent')); } } - private function notify_applicant($instance, $userid, $type, $subject, $content) { + private function notify_applicant($instance, $userenrolment, $type, $subject, $content) { global $CFG; require_once($CFG->dirroot.'/enrol/apply/notification.php'); // Required for course_get_url() function. require_once($CFG->dirroot.'/course/lib.php'); $course = get_course($instance->courseid); - $user = core_user::get_user($userid); + $user = core_user::get_user($userenrolment->userid); - $content = $this->update_mail_content($content, $course, $user); + $content = $this->update_mail_content($content, $course, $user, $userenrolment); $message = new enrol_apply_notification( $user, @@ -476,12 +484,14 @@ class enrol_apply_plugin extends enrol_plugin { return get_users_from_config($this->get_config('notifyglobal'), 'enrol/apply:manageapplications'); } - private function update_mail_content($content, $course, $user) { + private function update_mail_content($content, $course, $user, $userenrolment) { $replace = array( 'firstname' => $user->firstname, - 'content' => format_string($course->fullname), - 'lastname' => $user->lastname, - 'username' => $user->username); + 'content' => format_string($course->fullname), + 'lastname' => $user->lastname, + 'username' => $user->username, + 'timeend' => !empty($userenrolment->timeend) ? userdate($userenrolment->timeend) : '' + ); foreach ($replace as $key => $val) { $content = str_replace('{' . $key . '}', $val, $content); } @@ -513,4 +523,12 @@ class enrol_apply_plugin extends enrol_plugin { public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) { $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, $oldinstancestatus); } + /** + * Enrol cron support. + * @return void + */ + public function cron() { + $trace = new text_progress_trace(); + $this->process_expirations($trace); + } } diff --git a/settings.php b/settings.php index 7f4601e..b562c37 100644 --- a/settings.php +++ b/settings.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); if ($ADMIN->fulltree) { - $settings->add(new admin_setting_heading('enrol_apply_enrolname', '', get_string('pluginname_desc', 'enrol_apply'))); // Confirm mail settings... @@ -97,6 +96,23 @@ if ($ADMIN->fulltree) { array(), 'enrol/apply:manageapplications')); + // Expiry settings. + $settings->add(new admin_setting_heading( + 'enrol_apply_expiry', + get_string('expiry_heading', 'enrol_apply'), + get_string('expiry_desc', 'enrol_apply'))); + $options = array( + ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'), + ENROL_EXT_REMOVED_SUSPEND => get_string('extremovedsuspend', 'enrol'), + ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'), + ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'), + ); + $settings->add(new admin_setting_configselect('enrol_apply/expiredaction', + get_string('expiredaction', 'enrol_apply'), + get_string('expiredaction_help', 'enrol_apply'), + ENROL_EXT_REMOVED_KEEP, + $options)); + // Enrol instance defaults... $settings->add(new admin_setting_heading('enrol_manual_defaults', get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin'))); @@ -132,6 +148,9 @@ if ($ADMIN->fulltree) { get_string('notifycoursebased', 'enrol_apply'), get_string('notifycoursebased_desc', 'enrol_apply'), 0)); + + $settings->add(new admin_setting_configduration('enrol_apply/enrolperiod', + get_string('defaultperiod', 'enrol_apply'), get_string('defaultperiod_desc', 'enrol_apply'), 0)); } if ($hassiteconfig) { // Needs this condition or there is error on login page.