diff --git a/classes/ratings_and_allocations_table.php b/classes/ratings_and_allocations_table.php index 71e4a780..b34936f6 100644 --- a/classes/ratings_and_allocations_table.php +++ b/classes/ratings_and_allocations_table.php @@ -100,7 +100,7 @@ public function __construct(\mod_ratingallocate_renderer $renderer, $titles, $ra $this->shownames = true; // We only show the group column if at least one group is being used in at least one active restriction setting of a choice. $this->showgroups = !empty($allgroupsofchoices); - $this->showteams = (bool) $this->ratingallocate->get_teamvote_goups(); + $this->showteams = (bool) $this->ratingallocate->get_teamvote_groups(); } /** @@ -347,7 +347,7 @@ private function add_user_ratings_row($user, $userratings, $userallocations) { $row['groups'] = implode(';', $groupnames); } if ($this->showteams) { - $teamofuser = array_filter(array_keys($this->ratingallocate->get_teamvote_goups()), + $teamofuser = array_filter(array_keys($this->ratingallocate->get_teamvote_groups()), function($groupid) use ($user) { return groups_is_member($groupid,$user->id); } diff --git a/form_modify_choice.php b/form_modify_choice.php index 7fbdaf40..a8b695c6 100644 --- a/form_modify_choice.php +++ b/form_modify_choice.php @@ -113,10 +113,17 @@ public function definition() { $mform->addHelpButton($elementname, 'choice_usegroups', RATINGALLOCATE_MOD_NAME); $elementname = 'groupselector'; - $options = $this->ratingallocate->get_group_selections(); + if ($teamvotegroupingid = $this->ratingallocate->get_teamvote_groupingid()) { + $options = $this->ratingallocate->get_group_selections( + $this->ratingallocate->get_coarser_groups_for_grouping($teamvotegroupingid) + ); + } else { + $options = $this->ratingallocate->get_group_selections(); + } $selector = $mform->addelement('searchableselector', $elementname, get_string('choice_groupselect', RATINGALLOCATE_MOD_NAME), $options); $selector->setMultiple(true); + $mform->addHelpButton($elementname, 'choice_groupselect'); $mform->hideIf('groupselector', 'usegroups'); if ($this->choice) { diff --git a/lang/en/ratingallocate.php b/lang/en/ratingallocate.php index 4d17960e..40cc56f7 100644 --- a/lang/en/ratingallocate.php +++ b/lang/en/ratingallocate.php @@ -277,6 +277,8 @@ * Disabling the restriction means that this choice will be available to anyone. * Enabling the restriction without specifying a single group means that this choice will be *not* available for anyone.'; $string['choice_groupselect'] = 'Groups'; +$string['choice_groupselect_help'] = 'If voting in groups is enabled, the selection of groups to choose from has to be a coarser grouping than the grouping of vote in groups, in order to avoid interference. +Please change the groupingid of the group-voting-grouping in the activity settings, if your desired group is not listed.'; $string['edit_choice'] = 'Edit choice'; $string['rating_endtime'] = 'Rating ends at'; $string['rating_begintime'] = 'Rating begins at'; diff --git a/locallib.php b/locallib.php index 464132fc..5613688b 100644 --- a/locallib.php +++ b/locallib.php @@ -1317,7 +1317,7 @@ public function get_ratings_for_rateable_choices_with_teamvote() { * @return array|false Array of the form groupid => membercount if teamvote is enabled, false if not * @throws dml_exception */ - public function get_teamvote_goups() { + public function get_teamvote_groups() { if ($this->db->get_field(this_db\ratingallocate::TABLE, 'teamvote', ['id' => $this->ratingallocateid]) == 1) { $groupingid = $this->db->get_field(this_db\ratingallocate::TABLE, 'teamvotegroupingid', ['id' => $this->ratingallocateid]); @@ -1369,6 +1369,20 @@ public function get_teamvote_goups() { return false; } + /** + * Return the teamvote groupingid if teamvote is enabled, false if not. + * + * @return false|mixed false or the groupingid + * @throws dml_exception + */ + public function get_teamvote_groupingid() + { + if ($this->db->get_field(this_db\ratingallocate::TABLE, 'teamvote', ['id' => $this->ratingallocateid]) == 1) { + return $this->db->get_field(this_db\ratingallocate::TABLE, 'teamvotegroupingid', ['id' => $this->ratingallocateid]); + } + return false; + } + /** * Adds the groupid to all rating records with this userid. Should only be used for ratings with groupid 0. * @@ -2391,6 +2405,54 @@ public function update_choice_groups($choiceid, $groupids) { } } + /** + * Returns all groups that are coarser than the groups in the grouping. + * So all groups that only contain groups in the grouping completely or not at all. + * + * @param $groupingid + * @return array An array of groups + */ + public function get_coarser_groups_for_grouping($groupingid) { + + $courseid = $this->course->id; + $allgroups = groups_get_all_groups($courseid); + $groupsingrouping = groups_get_all_groups($courseid, 0, $groupingid); + + $coarsergroups = []; + + // Now iterate over all groups and check. + // If all groups in the grouping are either completely or not contained in the group. + foreach ($allgroups as $outergroup) { + $coarser = true; + foreach ($groupsingrouping as $innergroup) { + $innergroupmembers = groups_get_members($innergroup->id); + + $notcontained = true; + $completelycontained = true; + foreach ($innergroupmembers as $groupmember) { + // Check if innergroup is not at all conatained in outergroup. + if (groups_is_member($outergroup, $groupmember->id)) { + $notcontained = false; + } else { + // Now check if innergroup is completely contained in outergroup + $completelycontained = false; + } + } + // If innergroup is partially contained in outergroup, outergroup cannot be coarser. + if (!($notcontained || $completelycontained)) { + $coarser = false; + } + + } + if ($coarser) { + $coarsergroups[] = $outergroup; + } + } + + return $coarsergroups; + + } + /** * @return bool true, if all strategy settings are ok. */ diff --git a/solver/solver-template.php b/solver/solver-template.php index 1a4aa4df..96989870 100644 --- a/solver/solver-template.php +++ b/solver/solver-template.php @@ -89,7 +89,7 @@ public static function compute_target_function($ratings, $distribution) { */ public function distribute_users(\ratingallocate $ratingallocate) { - $teamvote = $ratingallocate->get_teamvote_goups(); + $teamvote = $ratingallocate->get_teamvote_groups(); // Load data from database. $choicerecords = $ratingallocate->get_rateable_choices();