Skip to content

Commit

Permalink
Merge pull request #83 from andil-elearning/enrol_period
Browse files Browse the repository at this point in the history
Implements enrolperiod
  • Loading branch information
Flottertotte authored Feb 27, 2018
2 parents 580ad76 + 48170f5 commit 964a818
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 30 deletions.
36 changes: 19 additions & 17 deletions edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,29 @@
// 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);
}

Expand Down
5 changes: 5 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ protected function definition() {
$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');
Expand Down
10 changes: 9 additions & 1 deletion lang/en/enrol_apply.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
$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.<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_desc'] = '';
Expand Down Expand Up @@ -106,3 +106,11 @@

$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.';
40 changes: 29 additions & 11 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public function get_instance_defaults() {
$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;
}
Expand All @@ -260,12 +261,19 @@ public function confirm_enrolment($enrols) {
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'));
Expand Down Expand Up @@ -293,7 +301,7 @@ public function wait_enrolment($enrols) {

$this->notify_applicant(
$instance,
$userenrolment->userid,
$userenrolment,
'waitinglist',
get_config('enrol_apply', 'waitmailsubject'),
get_config('enrol_apply', 'waitmailcontent'));
Expand Down Expand Up @@ -327,23 +335,23 @@ public function cancel_enrolment($enrols) {

$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,
Expand Down Expand Up @@ -476,12 +484,14 @@ public function get_notifyglobal_users() {
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);
}
Expand Down Expand Up @@ -513,4 +523,12 @@ public function restore_instance(restore_enrolments_structure_step $step, stdCla
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);
}
}
21 changes: 20 additions & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -97,6 +96,23 @@
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')));
Expand Down Expand Up @@ -132,6 +148,9 @@
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.
Expand Down

0 comments on commit 964a818

Please sign in to comment.