From 372f6d7caf49b0004f3c67938b67d825e08639e2 Mon Sep 17 00:00:00 2001 From: Flotter Totte Date: Wed, 12 Apr 2017 13:16:50 +0800 Subject: [PATCH] Add info about max enrolled users on enrol screen Konstantin (CMD): Add info about max enrolled users on enrol screen. --- apply_form.php | 9 ++++++++- db/upgrade.php | 10 ++++++++++ edit.php | 2 ++ edit_form.php | 4 ++++ lang/en/enrol_apply.php | 8 ++++++++ lib.php | 9 +++++++++ version.php | 4 ++-- 7 files changed, 43 insertions(+), 3 deletions(-) diff --git a/apply_form.php b/apply_form.php index 33c2029..7919046 100644 --- a/apply_form.php +++ b/apply_form.php @@ -42,7 +42,7 @@ class enrol_apply_apply_form extends moodleform { } public function definition() { - global $USER; + global $USER, $DB; $mform = $this->_form; $instance = $this->_customdata; @@ -52,6 +52,13 @@ class enrol_apply_apply_form extends moodleform { $heading = $plugin->get_instance_name($instance); $mform->addElement('header', 'selfheader', $heading); + if ($instance->customint3 > 0) { + $count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id)); + if ($count < $instance->customint3) { + $mform->addElement('html', '
'.$count.' '.get_string('maxenrolled_tip_1', 'enrol_apply').' '.$instance->customint3.' '.get_string('maxenrolled_tip_2', 'enrol_apply').'
'); + } + } + $mform->addElement('html', '

'.$instance->customtext1.'

'); $mform->addElement('textarea', 'applydescription', get_string('comment', 'enrol_apply'), 'cols="80"'); $mform->setType('applydescription', PARAM_TEXT); diff --git a/db/upgrade.php b/db/upgrade.php index 0801784..e0b32eb 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -92,6 +92,16 @@ function xmldb_enrol_apply_upgrade($oldversion) { } } + if ($oldversion < 2017032400) { + $enrolapply = enrol_get_plugin('apply'); + + $instances = $DB->get_records('enrol', array('enrol' => 'apply')); + foreach ($instances as $instance) { + $instance->customint3 = 0; + $DB->update_record('enrol', $instance, true); + } + } + return true; } \ No newline at end of file diff --git a/edit.php b/edit.php index 4bd2917..e0a57c8 100644 --- a/edit.php +++ b/edit.php @@ -94,6 +94,7 @@ if ($mform->is_cancelled()) { $instance->customtext2 = $data->customtext2; $instance->customint1 = $data->customint1; $instance->customint2 = $data->customint2; + $instance->customint3 = $data->customint3; $instance->roleid = $data->roleid; $instance->timemodified = time(); $DB->update_record('enrol', $instance); @@ -105,6 +106,7 @@ if ($mform->is_cancelled()) { 'roleid' => $data->roleid, 'customint1' => $data->customint1, 'customint2' => $data->customint2, + 'customint3' => $data->customint3, 'customtext1' => $data->customtext1, 'customtext2' => $data->customtext2); $plugin->add_instance($course, $fields); diff --git a/edit_form.php b/edit_form.php index e3e2e8e..8853217 100644 --- a/edit_form.php +++ b/edit_form.php @@ -73,6 +73,10 @@ class enrol_apply_edit_form extends moodleform { $select = $mform->addElement('select', 'notify', get_string('notify_desc', 'enrol_apply'), $choices); $select->setMultiple(true); + $mform->addElement('text', 'customint3', get_string('maxenrolled', 'enrol_apply')); + $mform->setType('customint3', PARAM_INT); + $mform->setDefault('customint3', $plugin->get_config('customint3')); + $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 12249b5..8f2ba65 100644 --- a/lang/en/enrol_apply.php +++ b/lang/en/enrol_apply.php @@ -97,3 +97,11 @@ $string['user_profile'] = 'User Profile'; $string['show_standard_user_profile'] = 'Show standard user profile fields on enrolment screen'; $string['show_extra_user_profile'] = 'Show extra user profile fields on enrolment screen'; + +$string['maxenrolled'] = 'Max enrolled users'; +$string['maxenrolled_help'] = 'Specifies the maximum number of users that can self enrol. 0 means no limit.'; +$string['maxenrolledreached_left'] = 'Maximum number of users allowed'; +$string['maxenrolledreached_right'] = 'has already been reached.'; + +$string['maxenrolled_tip_1'] = 'out of'; +$string['maxenrolled_tip_2'] = 'seats already booked.'; diff --git a/lib.php b/lib.php index 0ea88ad..b822859 100644 --- a/lib.php +++ b/lib.php @@ -68,6 +68,15 @@ class enrol_apply_plugin extends enrol_plugin { return $OUTPUT->notification(get_string('notification', 'enrol_apply'), 'notifysuccess'); } + if ($instance->customint3 > 0) { + // Max enrol limit specified. + $count = $DB->count_records('user_enrolments', array('enrolid' => $instance->id)); + if ($count >= $instance->customint3) { + // Bad luck, no more self enrolments here. + return '
'.get_string('maxenrolledreached_left', 'enrol_apply')." (".$count.") ".get_string('maxenrolledreached_right', 'enrol_apply').'
'; + } + } + require_once("$CFG->dirroot/enrol/apply/apply_form.php"); $form = new enrol_apply_apply_form(null, $instance); diff --git a/version.php b/version.php index 2d8de48..32a6aa0 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2017020300; +$plugin->version = 2017032900; $plugin->requires = 2011080100; $plugin->maturity = MATURITY_STABLE; -$plugin->release = 'Enrolment upon approval plugin Version 3.1-b.3'; +$plugin->release = 'Enrolment upon approval plugin Version 3.1-c'; $plugin->component = 'enrol_apply';