/** * This function loops through each group, calculates, and formats the scores. * * @return array List of all scores for each of the valid groups a user could join. * */ public function get_table_rows() { global $DB; $sgs = new skills_group_setting($this->courseid); $tablerows = array(); foreach ($this->records as $group) { $sgroup = new skills_group($group->groupid); if ($sgroup->count_members() < $sgs->get_group_size() && $sgroup->get_allow_others_to_join() === true) { $scores = $sgs->get_feedback_id() == 0 ? array() : $sgroup->get_join_form_score(); $name = $sgroup->get_group_name(); $temp = array_merge(array('id' => $group->groupid, 'name' => $name), $scores); $tablerows[] = $temp; } } return $tablerows; }
/** * This function allows the user to join a group if they are not already part * of a group. * */ private function join_group() { global $USER; $groupid = required_param('groupid', PARAM_INT); $groupingid = required_param('groupingid', PARAM_INT); $this->courseid = required_param('courseid', PARAM_INT); $sgs = new skills_group_setting($this->courseid); $sgrouping = new skills_grouping($this->courseid); $sgroup = new skills_group($groupid); if ($sgroup->count_members() < $sgs->get_group_size() && $sgroup->get_allow_others_to_join() === true) { if ($sgrouping->check_for_user_in_grouping($USER->id) === false) { groups_add_member($groupid, $USER->id); // Logging join group action. $params = array('context' => context_course::instance($this->courseid), 'objectid' => $groupid, 'courseid' => $this->courseid, 'userid' => $USER->id); $event = \block_skills_group\event\skillsgroup_joined::create($params); $event->trigger(); echo json_encode(array('result' => 'true', 'text' => get_string('groupjoinsuccess', BLOCK_SG_LANG_TABLE))); } else { echo json_encode(array('result' => 'false', 'text' => get_string('alreadyingroup', BLOCK_SG_LANG_TABLE))); } } else { echo json_encode(array('result' => 'false', 'text' => get_string('toomanymembers', BLOCK_SG_LANG_TABLE))); } }
/** * This function tests whether the class can count group members correctly. * */ public function test_count_members() { $sgroup = new skills_group($this->groupids[0]); // Only the last two users are in the group. $this->assertEquals($sgroup->count_members(), 2); }