diff --git a/lang/en/ratingallocate.php b/lang/en/ratingallocate.php index 31616865..4d17960e 100644 --- a/lang/en/ratingallocate.php +++ b/lang/en/ratingallocate.php @@ -73,6 +73,7 @@ $string['your_allocated_choice'] = 'Your Allocation'; $string['you_are_not_allocated'] = 'You were not allocated to any choice!'; $string['your_rating'] = 'Your Rating'; +$string['rating_for_team'] = 'Rating for Group {$a}'; $string['edit_rating'] = 'Edit Rating'; $string['delete_rating'] = 'Delete Rating'; $string['results_not_yet_published'] = 'Results have not yet been published.'; @@ -80,6 +81,7 @@ $string['too_few_choices_to_rate'] = 'There are too few choices to rate! Students have to rank at least {$a} choices!'; $string['at_least_one_rateable_choices_needed'] = 'You need at least one rateable choice.'; $string['no_rating_possible'] = 'Currently, there is no rating possible!'; +$string['no_member_of_team'] = 'This Ratingallocate requires voting in groups. You are not a member of any group, so you cannot submit a rating. Please contact your teacher to be added to a group.'; // // $string['allocation_manual_explain_only_raters'] = 'Select a choice to be assigned to a user. @@ -305,9 +307,11 @@ $string['teamvote_help'] = 'If enabled, students will be divided into groups based on the default set of groups or a custom grouping. A group rating will count for all group members.'; $string['teamvotegroupingid'] = 'Grouping for student groups'; $string['teamvotegroupingid_help'] = 'This is the grouping that the activity will use to find groups for student groups. If not set, the default set of groups will be used.'; +$string['user_with_multiple_groups'] = 'Users have to be uniquely mapped to a group in a Group-Voting grouping. There might be users who are members of multiple groups in the selected grouping. Please modify the groups in the course settings or choose another grouping.'; $string['teamvote_altered_after_preferences'] = 'Group Voting settings cannot be changed after preferences were submitted'; $string['preventvotenotingroup'] = 'Require group to vote'; $string['preventvotenotingroup_help'] = 'If enabled, users who are not members of a group will be unable to give a rating.'; +$string['teams'] = 'Team'; $string['err_required'] = 'You need to provide a value for this field.'; $string['err_minimum'] = 'The minimum value for this field is {$a}.'; diff --git a/locallib.php b/locallib.php index 543d7042..dfcb5fbd 100644 --- a/locallib.php +++ b/locallib.php @@ -325,39 +325,43 @@ private function process_action_give_rating() { if (!$DB->record_exists('ratingallocate_choices', array('ratingallocateid' => $this->ratingallocateid))) { $renderer->add_notification(get_string('no_choice_to_rate', RATINGALLOCATE_MOD_NAME)); } else if ($status === self::DISTRIBUTION_STATUS_RATING_IN_PROGRESS) { - // Rating is possible... - // Adde votegroup name zu form. + if ($this->db->get_field(this_db\ratingallocate::TABLE, 'preventvotenotingroup', ['id' => $this->ratingallocateid]) == 1 + && !$this->get_vote_group($USER->id)) { + $renderer->add_notification(get_string('no_member_of_team', RATINGALLOCATE_MOD_NAME)); + } else { + // Rating is possible... - // Suche das richtige Formular nach Strategie. - $strategyform = 'ratingallocate\\' . $this->ratingallocate->strategy . '\\mod_ratingallocate_view_form'; + // Suche das richtige Formular nach Strategie. + $strategyform = 'ratingallocate\\' . $this->ratingallocate->strategy . '\\mod_ratingallocate_view_form'; - $mform = new $strategyform($PAGE->url->out(), $this); - $mform->add_action_buttons(); + $mform = new $strategyform($PAGE->url->out(), $this); + $mform->add_action_buttons(); - if ($mform->is_cancelled()) { - // Return to view. - redirect("$CFG->wwwroot/mod/ratingallocate/view.php?id=" . $this->coursemodule->id); - return ""; - } else if ($mform->is_submitted() && $mform->is_validated() && $data = $mform->get_data()) { - // Save submitted data and call default page. - $this->save_ratings_to_db($USER->id, $data->data); + if ($mform->is_cancelled()) { + // Return to view. + redirect("$CFG->wwwroot/mod/ratingallocate/view.php?id=" . $this->coursemodule->id); + return ""; + } else if ($mform->is_submitted() && $mform->is_validated() && $data = $mform->get_data()) { + // Save submitted data and call default page. + $this->save_ratings_to_db($USER->id, $data->data); - // Return to view. - redirect( + // Return to view. + redirect( "$CFG->wwwroot/mod/ratingallocate/view.php?id=" . $this->coursemodule->id, get_string('ratings_saved', RATINGALLOCATE_MOD_NAME), null, \core\output\notification::NOTIFY_SUCCESS - ); - } + ); + } - $mform->definition_after_data(); + $mform->definition_after_data(); - $output .= $renderer->render_ratingallocate_strategyform($mform); - // Logging. - $event = \mod_ratingallocate\event\rating_viewed::create_simple( + $output .= $renderer->render_ratingallocate_strategyform($mform); + // Logging. + $event = \mod_ratingallocate\event\rating_viewed::create_simple( context_module::instance($this->coursemodule->id), $this->ratingallocateid); - $event->trigger(); + $event->trigger(); + } } } return $output; @@ -1230,6 +1234,7 @@ public function handle_view() { $choicestatus->showuserinfo = has_capability('mod/ratingallocate:give_rating', $this->context, null, false); $choicestatus->algorithmstarttime = $this->ratingallocate->algorithmstarttime; $choicestatus->algorithmstatus = $this->get_algorithm_status(); + $choicestatus->teamid = $this->get_vote_group($USER->id)->id; $choicestatusoutput = $renderer->render($choicestatus); } else { $choicestatusoutput = ""; @@ -1359,11 +1364,12 @@ public function add_groupid_to_ratings($userid, $groupid) { public function delete_groups_for_usersnogroup($usergroups) { - $sql = 'SELECT id FROM {groups} WHERE id IN ( :groups ) AND idnumber = :ratingallocateid AND name = :name'; + $sql = 'SELECT id FROM {groups} WHERE id IN ( :groups ) AND idnumber = :ratingallocateid AND name = :name AND courseid = :courseid'; $delgroups = $this->db->get_records_sql($sql, [ 'groups' => implode(" , ", array_keys($usergroups)), 'ratingallocateid' => $this->ratingallocateid, - 'name' => 'delete after algorithm run' + 'name' => 'delete after algorithm run', + 'courseid' => $this->ratingallocate->course ]); foreach ($delgroups as $group) { groups_delete_group($group); @@ -1371,24 +1377,6 @@ public function delete_groups_for_usersnogroup($usergroups) { } - /** - * Returns the group in the teamvotegrouping this user is a member of. - * (Should return only one groupid, please only call if teamvote is enabled). - * - * @param $userid - * @return false|mixed The groupid - * @throws dml_exception - */ - public function get_teamvotegroup_for_user($userid) { - - $sql = 'SELECT gm.groupid FROM {groups_members} gm INNER JOIN {groupings_groups} gg ON gm.groupid=gg.groupid - INNER JOIN {ratingallocate} r ON gg.groupingid=r.teamvotegroupingid - WHERE gm.userid = :userid AND r.id = :ratingallocateid'; - $groupid = $this->db->get_record_sql($sql, ['userid' => $userid, 'ratingallocateid' => $this->ratingallocateid]); - return $groupid->groupid; - - } - /** * distribution of choices for each user * take care about max_execution_time and memory_limit diff --git a/mod_form.php b/mod_form.php index b8089f3d..f7c1b1ef 100644 --- a/mod_form.php +++ b/mod_form.php @@ -295,6 +295,7 @@ public function definition_after_data() { * Checks that accesstimestart is before accesstimestop */ public function validation($data, $files) { + global $COURSE; $errors = parent::validation($data, $files); if ($data['accesstimestop'] <= $data['accesstimestart']) { @@ -332,6 +333,20 @@ public function validation($data, $files) { } } } + + // Check if group-user allocation in teamvotegrouping is 1:1. + $usersingrouping = groups_get_grouping_members($data['teamvotegroupingid'], 'u.id'); + $userinmultipleteams = false; + foreach ($usersingrouping as $user) { + if (count(groups_get_user_groups($COURSE->id, $user)[$data['teamvotegroupingid']]) > 1) { + $userinmultipleteams = true; + break; + } + } + if ($userinmultipleteams) { + $errors['teamvotegroupingid'] = get_string('user_with_multiple_groups', self::MOD_NAME); + } + return $errors; } diff --git a/renderable.php b/renderable.php index 96874eb4..c809b673 100644 --- a/renderable.php +++ b/renderable.php @@ -65,4 +65,5 @@ class ratingallocate_choice_status implements renderable { public $showuserinfo; public $algorithmstarttime; public $algorithmstatus; + public $teamid; } diff --git a/renderer.php b/renderer.php index a792df32..4839b7ea 100644 --- a/renderer.php +++ b/renderer.php @@ -64,7 +64,13 @@ public function render_ratingallocate_header(ratingallocate_header $header) { */ public function render_ratingallocate_strategyform($mform) { $o = ''; - $o .= $this->heading(get_string('your_rating', RATINGALLOCATE_MOD_NAME), 2); + + if ($teamid = $mform->get_teamid()) { + $o .= $this->heading(get_string('rating_for_team', RATINGALLOCATE_MOD_NAME, groups_get_group_name($teamid)), 2); + } else { + var_dump($teamid); + $o .= $this->heading(get_string('your_rating', RATINGALLOCATE_MOD_NAME), 2); + } $o .= $this->format_text($mform->get_strategy_description_header() . '
' . $mform->describe_strategy()); $o .= $mform->to_html(); @@ -134,7 +140,12 @@ public function render_ratingallocate_choice_status(ratingallocate_choice_status // Print own choices or full list of available choices. if (!empty($status->ownchoices) && $status->showuserinfo && $accesstimestart < $time) { $row = new html_table_row(); - $cell1 = new html_table_cell(get_string('your_rating', RATINGALLOCATE_MOD_NAME)); + + if ($status->teamid) { + $cell1 = new html_table_cell(get_string('rating_for_team', RATINGALLOCATE_MOD_NAME, groups_get_group_name($status->teamid))); + } else { + $cell1 = new html_table_cell(get_string('your_rating', RATINGALLOCATE_MOD_NAME)); + } $choiceshtml = array(); foreach ($status->ownchoices as $choice) { diff --git a/strategy/strategy_template.php b/strategy/strategy_template.php index 24caa823..cfbad391 100644 --- a/strategy/strategy_template.php +++ b/strategy/strategy_template.php @@ -196,12 +196,16 @@ abstract class ratingallocate_strategyform extends \moodleform { private $strategy; + private $teamid; + /** * * @param string $url The page url * @param \ratingallocate $ratingallocate The calling ratingallocate instance */ public function __construct($url, \ratingallocate $ratingallocate) { + global $USER; + $this->ratingallocate = $ratingallocate; // Load strategy options. $allstrategyoptions = json_decode($this->ratingallocate->ratingallocate->setting, true); @@ -212,6 +216,7 @@ public function __construct($url, \ratingallocate $ratingallocate) { $this->strategyoptions = array(); } $this->strategy = $this->construct_strategy($this->strategyoptions); + $this->teamid = $ratingallocate->get_vote_group($USER->id)->id; parent::__construct($url); } @@ -228,6 +233,10 @@ protected function get_strategy() { return $this->strategy; } + public function get_teamid() { + return $this->teamid; + } + /** * inherited from moodleform: a child class must call parent::definition() first to execute * ratingallocate_strategyform::definition