Skip to content

Commit

Permalink
Merge pull request #154 from sameer-ah/fix-142
Browse files Browse the repository at this point in the history
Max Enrollments Number
  • Loading branch information
ndunand authored Jul 16, 2021
2 parents 7860c0e + 3c016d4 commit 161d7df
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<FIELD NAME="introformat" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="intro" NEXT="publish"/>
<FIELD NAME="publish" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="introformat" NEXT="multipleenrollmentspossible"/>
<FIELD NAME="multipleenrollmentspossible" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="publish" NEXT="showresults"/>
<FIELD NAME="maxenrollments" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="showresults" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="multipleenrollmentspossible" NEXT="display"/>
<FIELD NAME="display" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showresults" NEXT="allowupdate"/>
<FIELD NAME="allowupdate" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="display" NEXT="showunanswered"/>
Expand Down
15 changes: 15 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions lang/en/choicegroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

7 changes: 7 additions & 0 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)';
Expand Down
16 changes: 16 additions & 0 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
}

Expand Down

0 comments on commit 161d7df

Please sign in to comment.