Skip to content

Commit

Permalink
Add settings to restrict groupchoices available depending on the
Browse files Browse the repository at this point in the history
grouping membership of the user
  • Loading branch information
NinaHerrmann committed Jan 24, 2023
1 parent af1d053 commit 06d9ebb
Show file tree
Hide file tree
Showing 10 changed files with 581 additions and 16 deletions.
23 changes: 23 additions & 0 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,23 @@ public static function submit_choicegroup_response($choicegroupid, $data) {
}

if (!choicegroup_get_user_answer($choicegroup, $USER) || $choicegroup->allowupdate) {
// Comparable to view.php.
if ($choicegroup->restrictbygrouping) {
$usergroupassigments = choicegroup_get_all_relevant_group_assignments($choicegroup->id);
$groupgrouping = choicegroup_structure_groupings_to_group($choicegroup->option);
}
if ($choicegroup->multipleenrollmentspossible) {
foreach($choicegroup->option as $optionid => $text) {
if (in_array($optionid, $responses)) {
if ($choicegroup->restrictbygrouping && !(empty($usergroupassigments))) {
$choicerecord = $DB->get_record('choicegroup_options', array('id' => $optionid),
'*', MUST_EXIST);
$groupavailable = choicegroup_check_group_available($choicerecord->groupid,
$usergroupassigments, $groupgrouping);
if (!$groupavailable) {
throw new moodle_exception('restrictgroupingsconflict', 'webservice');
}
}
choicegroup_user_submit_response($optionid, $choicegroup, $USER->id, $course, $cm);
} else {
// Remove group selection if selected.
Expand All @@ -315,6 +329,15 @@ public static function submit_choicegroup_response($choicegroupid, $data) {
} else { // !multipleenrollmentspossible
if (count($responses) == 1) {
$responses = reset($responses);
if ($choicegroup->restrictbygrouping && !(empty($usergroupassigments))) {
$choicerecord = $DB->get_record('choicegroup_options', array('id' => $responses->id),
'*', MUST_EXIST);
$groupavailable = choicegroup_check_group_available($choicerecord->groupid,
$usergroupassigments, $groupgrouping);
if (!$groupavailable) {
throw new moodle_exception('restrictgroupingsconflict', 'webservice');
}
}
choicegroup_user_submit_response($responses, $choicegroup, $USER->id, $course, $cm);
}
}
Expand Down
18 changes: 16 additions & 2 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
<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"/>
<FIELD NAME="showunanswered" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="allowupdate" NEXT="limitanswers"/>
<FIELD NAME="limitanswers" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showunanswered" NEXT="timeopen"/>
<FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="limitanswers" NEXT="timeclose"/>
<FIELD NAME="limitanswers" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="showunanswered" NEXT="restrictbygrouping"/>
<FIELD NAME="restrictbygrouping" TYPE="int" LENGTH="2" NOTNULL="false" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="limitanswers" NEXT="restrictchoicesbehaviour"/>
<FIELD NAME="restrictchoicesbehaviour" TYPE="int" LENGTH="2" NOTNULL="false" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="restrictbygrouping" NEXT="timeopen" COMMENT="0 Do not show choices unavailable, 1 dimm the choices, 2 show conflict, 3 show conflicting group"/>
<FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="restrictchoicesbehaviour" NEXT="timeclose"/>
<FIELD NAME="timeclose" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeopen" NEXT="timemodified"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="timeclose" NEXT="completionsubmit"/>
<FIELD NAME="completionsubmit" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="If this field is set to 1, then the activity will be automatically marked as 'complete' once the user submits their choicegroup." PREVIOUS="timemodified" NEXT="sortgroupsby"/>
Expand All @@ -46,5 +48,17 @@
<KEY NAME="choicegroupid" TYPE="foreign" FIELDS="choicegroupid" REFTABLE="choicegroup" REFFIELDS="id" PREVIOUS="primary"/>
</KEYS>
</TABLE>
<TABLE NAME="choicegroup_groupings" COMMENT="groupings which are saved as restriction" PREVIOUS="choicegroup_options">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="choicegroupid"/>
<FIELD NAME="choicegroupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="groupingid"/>
<FIELD NAME="groupingid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="choicegroupid" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="choicegroupid"/>
<KEY NAME="choicegroupid" TYPE="foreign" FIELDS="choicegroupid" REFTABLE="choicegroup" REFFIELDS="id" PREVIOUS="primary" NEXT="groupingid"/>
<KEY NAME="groupingid" TYPE="foreign" FIELDS="groupingid" REFTABLE="groupings" REFFIELDS="id" PREVIOUS="choicegroupid"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
35 changes: 34 additions & 1 deletion db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function xmldb_choicegroup_upgrade($oldversion) {

// Adding fields to table choicegroup
$newField = $table->add_field('multipleenrollmentspossible', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
$dbman->add_field($table, $newField);
$dbman->add_field($table, $newField);


upgrade_mod_savepoint(true, 2013070900, 'choicegroup');
Expand Down Expand Up @@ -96,6 +96,39 @@ function xmldb_choicegroup_upgrade($oldversion) {
// Group choice savepoint reached.
upgrade_mod_savepoint(true, 2021080500, 'choicegroup');
}
if ($oldversion < 2023011600) {

// Define field restrictbygrouping to be added to choicegroup.
$table = new xmldb_table('choicegroup');
$field = new xmldb_field('restrictbygrouping', XMLDB_TYPE_INTEGER, '2', null, null, null, '0', 'onlyactive');

// Conditionally launch add field restrictbygrouping.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Define field restrictchoicesbehaviour to be added to choicegroup.
$field = new xmldb_field('restrictchoicesbehaviour', XMLDB_TYPE_INTEGER, '2', null, null, null, '0', 'restrictbygrouping');

// Conditionally launch add field restrictchoicesbehaviour.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Choicegroup savepoint reached.

// Create Table for groupings.
// Define field id to be added to choicegroup_groupings.
$table = new xmldb_table('choicegroup_groupings');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null);
$table->add_field('choicegroupid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'id');
$table->add_field('groupingid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'choicegroupid');
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
// Conditionally create the table.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

upgrade_mod_savepoint(true, 2023011600, 'choicegroup');
}

return true;
}
20 changes: 20 additions & 0 deletions lang/en/choicegroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,23 @@
$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.';

$string['groupingrestrict'] = 'Restrict choices based on grouping assignments';
$string['selectedgroupings'] = 'Restrict by grouping?';
$string['selectedgroupings_help'] = 'If enabled, participants can only choose groups which do not belong to a grouping they are already assigned to (manually or by a group choice activity).';
$string['assignedtogrouping'] = '(You are already assigned to a group which contradicts this choice)';
$string['submitfailedgroupingrestriction'] = 'You are already assigned to a group which contradicts this choice.';
$string['limitationbehaviour'] = 'Limitation behaviour';
$string['restrictchoicesbehaviour'] = 'Choose limitation behaviour?';
$string['restrictchoicesbehaviour_help'] = '"Hide group from group list" - will hide all groups which can not be selected due to previous assignments<br>
"Show group as dimmed" - will show the choice but choice can not be selected <br>
"Show group with limitation notice" - inform user about conflicting groups ';
$string['hidegroup'] = 'Hide group from group list';
$string['dimmgroup'] = 'Show group as dimmed';
$string['informlimit'] = 'Show group with limitation notice';
$string['informlimitgroup'] = 'Show group with information on conflicting groups';
$string['reasongrouplimitationplural'] = '(You can not select this group as you are already assigned to the groups: {$a} )';
$string['reasongrouplimitationsingular'] = '(You can not select this group as you are already assigned to the group: {$a} )';
$string['reasongrouplimitationempty'] = '(You can not select this group, however no conflicting group can be found - this might be an error in the programm code)';
$string['hidechoicetostudents'] = '(If a student had the same group assigment, this choice would not be shown)';
$string['conflictinggroupassignment'] = 'Your Group assigment are conflicting. Therefore you can not participate in this groupchoice activity.';

Loading

0 comments on commit 06d9ebb

Please sign in to comment.