Skip to content

Commit

Permalink
fix interference with choices_usegroups by listing a coarser list of …
Browse files Browse the repository at this point in the history
…groups in the form selector
  • Loading branch information
irinahpe committed Aug 9, 2024
1 parent 43dcf7b commit 94b2878
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
4 changes: 2 additions & 2 deletions classes/ratings_and_allocations_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 8 additions & 1 deletion form_modify_choice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions lang/en/ratingallocate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
64 changes: 63 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion solver/solver-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 94b2878

Please sign in to comment.