/**
  * 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)));
     }
 }
require_once $CFG->dirroot . '/blocks/skills_group/classes/skills_group_setting.class.php';
global $OUTPUT, $PAGE, $USER;
$courseid = required_param('courseid', PARAM_INT);
if (!blocks_skills_group_verify_access('block/skills_group:canmanageskillsgroups', true)) {
    redirect(new moodle_url('/course/view.php', array('id' => $courseid)));
}
$url = new moodle_url('/blocks/skills_group/edit_skills_group_settings.php', array('id' => $courseid, 'sesskey' => $USER->sesskey));
block_skills_group_setup_page($courseid, $url, get_string('editsettingstitle', BLOCK_SG_LANG_TABLE));
$editform = new edit_skills_group_settings_form($courseid);
$toform['courseid'] = $courseid;
// Retrieve any previously used settings.
$sgs = new skills_group_setting($courseid);
if ($sgs->exists()) {
    $toform['feedbacks'] = $sgs->get_feedback_id();
    $toform['groupings'] = $sgs->get_grouping_id();
    $toform['maxsize'] = $sgs->get_group_size();
    $toform['threshold'] = $sgs->get_threshold();
    $toform['allownaming'] = $sgs->get_allownaming();
    if ($sgs->date_restriction()) {
        $toform['datecheck'] = 1;
        $toform['date'] = $sgs->get_date();
    } else {
        $toform['datecheck'] = 0;
        $toform['date'] = null;
    }
}
$editform->set_data($toform);
if ($editform->is_cancelled()) {
    $courseurl = new moodle_url('/course/view.php', array('id' => $courseid));
    redirect($courseurl);
} else {
 /**
  * This function tests to see that the max group size gets correctly stored and retrieved
  * in the class.
  *
  */
 public function test_get_group_size()
 {
     $sgs = new skills_group_setting($this->courseid);
     // Test max group size setting.
     $sgs->update_record($this->get_skills_group_settings());
     $this->assertEquals($sgs->get_group_size(), self::MAXGROUPSIZE);
 }
/**
 * This function loads the YUI modules that I have written.  I've elected to
 * load these last since that is generally safest.
 *
 * @param int $courseid The ID of the course being used.
 * @param int $groupid The ID of the group that the user belongs to.
 *
 */
function load_yui_modules($courseid, $groupid, $error = null)
{
    global $PAGE;
    $params = array('courseid' => $courseid, 'groupid' => $groupid, 'errorstring' => $error);
    if ($error == null) {
        $sgsetting = new skills_group_setting($courseid);
        $sgrouping = new skills_grouping($courseid);
        $sgroup = new skills_group($groupid);
        // True max group size is: max group size - # of locked members.
        $params['maxgroupsize'] = $sgsetting->get_group_size() - count($sgroup->get_members_list(true));
        $potentialstudents = $sgrouping->get_potential_students();
        $params['availableids'] = array_keys($potentialstudents);
        $params['availablenames'] = array_values($potentialstudents);
        $unlockedstudents = $sgroup->get_members_list(false);
        $params['groupmemberids'] = array_keys($unlockedstudents);
        $params['groupmembernames'] = array_values($unlockedstudents);
    }
    $PAGE->requires->yui_module('moodle-block_skills_group-edit', 'M.block_skills_group.init_edit', array($params));
}