diff --git a/db/install.xml b/db/install.xml index 905dfff..529d99d 100644 --- a/db/install.xml +++ b/db/install.xml @@ -13,6 +13,7 @@ + diff --git a/db/upgrade.php b/db/upgrade.php index 88c4c6a..3a660e4 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -67,6 +67,21 @@ function xmldb_choicegroup_upgrade($oldversion) { upgrade_mod_savepoint(true, 2015022301, 'choicegroup'); } + if ($oldversion < 2021071400) { + + // Define field maxenrollments to be added to choicegroup. + $table = new xmldb_table('choicegroup'); + $field = new xmldb_field('maxenrollments', XMLDB_TYPE_INTEGER, '10', null, null, null, '0', 'sortgroupsby'); + + // Conditionally launch add field maxenrollments. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Choicegroup savepoint reached. + upgrade_mod_savepoint(true, 2021071400, 'choicegroup'); + } + return true; } diff --git a/lang/en/choicegroup.php b/lang/en/choicegroup.php index 1e74403..010a5cb 100644 --- a/lang/en/choicegroup.php +++ b/lang/en/choicegroup.php @@ -164,4 +164,7 @@ $string['activitydate:willopen'] = 'Opens:'; $string['activitydate:hasopened'] = 'Opened:'; $string['activitydate:willclose'] = 'Closes:'; +$string['mustchoosemax'] = 'You must choose a maximum of {$a} groups. Nothing was saved.'; +$string['maxenrollments'] = 'Max. enrollments'; +$string['maxenrollments_help'] = 'This option allows to limit the number of group enrollments for a participant. Use default value **0** if there is no limit.'; diff --git a/mod_form.php b/mod_form.php index e174aea..21f03e7 100644 --- a/mod_form.php +++ b/mod_form.php @@ -103,6 +103,13 @@ function definition() $mform->setExpanded('miscellaneoussettingshdr'); $mform->addElement('checkbox', 'multipleenrollmentspossible', get_string('multipleenrollmentspossible', 'choicegroup')); + $mform->addElement('text', 'maxenrollments', get_string('maxenrollments', 'choicegroup'), array('size' => '6')); + $mform->addHelpButton('maxenrollments', 'maxenrollments', 'choicegroup'); + $mform->setType('maxenrollments', PARAM_INT); + $mform->hideIf('maxenrollments', 'multipleenrollmentspossible'); + $mform->addRule('maxenrollments', get_string('error'), 'numeric', 'extraruledata', 'client', false, false); + $mform->setDefault('maxenrollments', 0); + $mform->addElement('select', 'showresults', get_string("publish", "choicegroup"), $CHOICEGROUP_SHOWRESULTS); $mform->setDefault('showresults', CHOICEGROUP_SHOWRESULTS_DEFAULT); diff --git a/version.php b/version.php index 711dc13..6f30c55 100644 --- a/version.php +++ b/version.php @@ -26,7 +26,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021060200; +$plugin->version = 2021071400; $plugin->requires = 2018051700; // Moodle 3.5 $plugin->maturity = MATURITY_STABLE; $plugin->release = '1.31 for Moodle 3.5-3.11 (Build: 2021060200)'; diff --git a/view.php b/view.php index a1c5b96..90db998 100644 --- a/view.php +++ b/view.php @@ -102,6 +102,20 @@ if ($choicegroup->multipleenrollmentspossible == 1) { $number_of_groups = optional_param('number_of_groups', '', PARAM_INT); + $enrollmentscount = 0; + + if ($choicegroup->maxenrollments > 0) { + for ($i = 0; $i < $number_of_groups; $i++) { + $answer_value = optional_param('answer_' . $i, '', PARAM_INT); + if ($answer_value != '') { + $enrollmentscount++; + } + } + if ($enrollmentscount > $choicegroup->maxenrollments) { + redirect(new moodle_url('/mod/choicegroup/view.php', + array('id' => $cm->id, 'notify' => 'mustchoosemax', 'sesskey' => sesskey()))); + } + } for ($i = 0; $i < $number_of_groups; $i++) { $answer_value = optional_param('answer_' . $i, '', PARAM_INT); @@ -163,6 +177,8 @@ echo $OUTPUT->notification(get_string('choicegroupsaved', 'choicegroup'), 'notifysuccess'); } else if ($notify === 'mustchooseone') { echo $OUTPUT->notification(get_string('mustchooseone', 'choicegroup'), 'notifyproblem'); + } else if ($notify === 'mustchoosemax') { + echo $OUTPUT->notification(get_string('mustchoosemax', 'choicegroup', $choicegroup->maxenrollments), 'notifyproblem'); } }