Browse Source

Implements enrolperiod

Implements enrollib feature which allow to automatically unenrol users, based on an enrolment duration.
SABERES_37_STABLE
Arnaud Trouvé 8 years ago
parent
commit
48170f539e
  1. 36
      edit.php
  2. 5
      edit_form.php
  3. 10
      lang/en/enrol_apply.php
  4. 40
      lib.php
  5. 21
      settings.php

36
edit.php

@ -88,27 +88,29 @@ if ($mform->is_cancelled()) {
// Convert back to string for storing in enrol table. // Convert back to string for storing in enrol table.
$data->customtext2 = implode(',', $notify); $data->customtext2 = implode(',', $notify);
if ($instance->id) { if ($instance->id) {
$instance->status = $data->status; $instance->status = $data->status;
$instance->name = $data->name; $instance->name = $data->name;
$instance->customtext1 = $data->customtext1; $instance->customtext1 = $data->customtext1;
$instance->customtext2 = $data->customtext2; $instance->customtext2 = $data->customtext2;
$instance->customint1 = $data->customint1; $instance->customint1 = $data->customint1;
$instance->customint2 = $data->customint2; $instance->customint2 = $data->customint2;
$instance->customint3 = $data->customint3; $instance->customint3 = $data->customint3;
$instance->roleid = $data->roleid; $instance->roleid = $data->roleid;
$instance->enrolperiod = $data->enrolperiod;
$instance->timemodified = time(); $instance->timemodified = time();
$DB->update_record('enrol', $instance); $DB->update_record('enrol', $instance);
} else { } else {
$fields = array( $fields = array(
'status' => $data->status, 'status' => $data->status,
'name' => $data->name, 'name' => $data->name,
'roleid' => $data->roleid, 'roleid' => $data->roleid,
'customint1' => $data->customint1, 'customint1' => $data->customint1,
'customint2' => $data->customint2, 'customint2' => $data->customint2,
'customint3' => $data->customint3, 'customint3' => $data->customint3,
'customtext1' => $data->customtext1, 'customtext1' => $data->customtext1,
'customtext2' => $data->customtext2); 'customtext2' => $data->customtext2,
'enrolperiod' => $data->enrolperiod
);
$plugin->add_instance($course, $fields); $plugin->add_instance($course, $fields);
} }

5
edit_form.php

@ -77,6 +77,11 @@ class enrol_apply_edit_form extends moodleform {
$mform->setType('customint3', PARAM_INT); $mform->setType('customint3', PARAM_INT);
$mform->setDefault('customint3', $plugin->get_config('customint3')); $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->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT); $mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'courseid'); $mform->addElement('hidden', 'courseid');

10
lang/en/enrol_apply.php

@ -32,7 +32,7 @@ $string['confirmmail_desc'] = '';
$string['confirmmailsubject'] = 'Confirmation email subject'; $string['confirmmailsubject'] = 'Confirmation email subject';
$string['confirmmailsubject_desc'] = ''; $string['confirmmailsubject_desc'] = '';
$string['confirmmailcontent'] = 'Confirmation email content'; $string['confirmmailcontent'] = 'Confirmation email content';
$string['confirmmailcontent_desc'] = 'Please use the following special marks to replace email content with data from Moodle.<br/>{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.<br/>{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_heading'] = 'Waiting list email';
$string['waitmail_desc'] = ''; $string['waitmail_desc'] = '';
@ -106,3 +106,11 @@ $string['maxenrolledreached_right'] = 'has already been reached.';
$string['maxenrolled_tip_1'] = 'out of'; $string['maxenrolled_tip_1'] = 'out of';
$string['maxenrolled_tip_2'] = 'seats already booked.'; $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.';

40
lib.php

@ -235,6 +235,7 @@ class enrol_apply_plugin extends enrol_plugin {
$fields['customint1'] = $this->get_config('show_standard_user_profile'); $fields['customint1'] = $this->get_config('show_standard_user_profile');
$fields['customint2'] = $this->get_config('show_extra_user_profile'); $fields['customint2'] = $this->get_config('show_extra_user_profile');
$fields['customtext2'] = $this->get_config('notifycoursebased') ? '$@ALL@$' : ''; $fields['customtext2'] = $this->get_config('notifycoursebased') ? '$@ALL@$' : '';
$fields['enrolperiod'] = $this->get_config('enrolperiod', 0);
return $fields; return $fields;
} }
@ -260,12 +261,19 @@ class enrol_apply_plugin extends enrol_plugin {
continue; 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)); $DB->delete_records('enrol_apply_applicationinfo', array('userenrolmentid' => $enrol));
$this->notify_applicant( $this->notify_applicant(
$instance, $instance,
$userenrolment->userid, $userenrolment,
'confirmation', 'confirmation',
get_config('enrol_apply', 'confirmmailsubject'), get_config('enrol_apply', 'confirmmailsubject'),
get_config('enrol_apply', 'confirmmailcontent')); get_config('enrol_apply', 'confirmmailcontent'));
@ -293,7 +301,7 @@ class enrol_apply_plugin extends enrol_plugin {
$this->notify_applicant( $this->notify_applicant(
$instance, $instance,
$userenrolment->userid, $userenrolment,
'waitinglist', 'waitinglist',
get_config('enrol_apply', 'waitmailsubject'), get_config('enrol_apply', 'waitmailsubject'),
get_config('enrol_apply', 'waitmailcontent')); get_config('enrol_apply', 'waitmailcontent'));
@ -327,23 +335,23 @@ class enrol_apply_plugin extends enrol_plugin {
$this->notify_applicant( $this->notify_applicant(
$instance, $instance,
$userenrolment->userid, $userenrolment,
'cancelation', 'cancelation',
get_config('enrol_apply', 'cancelmailsubject'), get_config('enrol_apply', 'cancelmailsubject'),
get_config('enrol_apply', 'cancelmailcontent')); 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; global $CFG;
require_once($CFG->dirroot.'/enrol/apply/notification.php'); require_once($CFG->dirroot.'/enrol/apply/notification.php');
// Required for course_get_url() function. // Required for course_get_url() function.
require_once($CFG->dirroot.'/course/lib.php'); require_once($CFG->dirroot.'/course/lib.php');
$course = get_course($instance->courseid); $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( $message = new enrol_apply_notification(
$user, $user,
@ -476,12 +484,14 @@ class enrol_apply_plugin extends enrol_plugin {
return get_users_from_config($this->get_config('notifyglobal'), 'enrol/apply:manageapplications'); 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( $replace = array(
'firstname' => $user->firstname, 'firstname' => $user->firstname,
'content' => format_string($course->fullname), 'content' => format_string($course->fullname),
'lastname' => $user->lastname, 'lastname' => $user->lastname,
'username' => $user->username); 'username' => $user->username,
'timeend' => !empty($userenrolment->timeend) ? userdate($userenrolment->timeend) : ''
);
foreach ($replace as $key => $val) { foreach ($replace as $key => $val) {
$content = str_replace('{' . $key . '}', $val, $content); $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) { 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); $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);
}
} }

21
settings.php

@ -25,7 +25,6 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) { if ($ADMIN->fulltree) {
$settings->add(new admin_setting_heading('enrol_apply_enrolname', '', get_string('pluginname_desc', 'enrol_apply'))); $settings->add(new admin_setting_heading('enrol_apply_enrolname', '', get_string('pluginname_desc', 'enrol_apply')));
// Confirm mail settings... // Confirm mail settings...
@ -97,6 +96,23 @@ if ($ADMIN->fulltree) {
array(), array(),
'enrol/apply:manageapplications')); '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... // Enrol instance defaults...
$settings->add(new admin_setting_heading('enrol_manual_defaults', $settings->add(new admin_setting_heading('enrol_manual_defaults',
get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin'))); get_string('enrolinstancedefaults', 'admin'), get_string('enrolinstancedefaults_desc', 'admin')));
@ -132,6 +148,9 @@ if ($ADMIN->fulltree) {
get_string('notifycoursebased', 'enrol_apply'), get_string('notifycoursebased', 'enrol_apply'),
get_string('notifycoursebased_desc', 'enrol_apply'), get_string('notifycoursebased_desc', 'enrol_apply'),
0)); 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. if ($hassiteconfig) { // Needs this condition or there is error on login page.

Loading…
Cancel
Save