From 5bf243d1315c17ea4f1a1de3624a5edea7ea5f3b Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 16 Aug 2007 11:06:48 +0000 Subject: [PATCH] MDL-10383 - groups/groupings refactoring nearly finished ;-) --- admin/uploaduser.php | 2 +- blocks/quiz_results/block_quiz_results.php | 4 +- course/lib.php | 10 +- enrol/imsenterprise/enrol.php | 2 +- enrol/manual/enrol.php | 2 +- group/{lib => }/clientlib.js | 0 group/db/dbbasicgrouplib.php | 325 ----------------------------- group/index.php | 24 +-- group/lib/basicgrouplib.php | 240 --------------------- group/lib/groupinglib.php | 57 ----- group/lib/legacylib.php | 318 ---------------------------- group/lib/utillib.php | 256 ----------------------- lib/accesslib.php | 6 +- lib/deprecatedlib.php | 265 +++++++++++++++++++++++ lib/grouplib.php | 85 +++++++- lib/moodlelib.php | 87 ++------ mod/hotpot/report.php | 8 +- mod/quiz/locallib.php | 2 +- user/view.php | 10 +- 19 files changed, 396 insertions(+), 1307 deletions(-) rename group/{lib => }/clientlib.js (100%) delete mode 100644 group/db/dbbasicgrouplib.php delete mode 100644 group/lib/basicgrouplib.php delete mode 100644 group/lib/groupinglib.php delete mode 100644 group/lib/legacylib.php delete mode 100644 group/lib/utillib.php diff --git a/admin/uploaduser.php b/admin/uploaduser.php index ce86f5da9..8a2c91af3 100755 --- a/admin/uploaduser.php +++ b/admin/uploaduser.php @@ -339,7 +339,7 @@ if ( $formdata = $mform->get_data() ) { if ($course[$i] && $groupid[$i]) { $coursecontext = get_context_instance(CONTEXT_COURSE, $course[$i]->id); if (count(get_user_roles($coursecontext, $user->id))) { - if (add_user_to_group($groupid[$i], $user->id)) { + if (groups_add_member($groupid[$i], $user->id)) { notify('-->' . get_string('addedtogroup','',$addgroup[$i])); } else { notify('-->' . get_string('addedtogroupnot','',$addgroup[$i])); diff --git a/blocks/quiz_results/block_quiz_results.php b/blocks/quiz_results/block_quiz_results.php index d3f968a77..cd072cc26 100644 --- a/blocks/quiz_results/block_quiz_results.php +++ b/blocks/quiz_results/block_quiz_results.php @@ -127,10 +127,10 @@ class block_quiz_results extends block_base { } // Now find which groups these users belong in - $groupofuser = groups_get_groups_users($userids, $courseid); /*TODO: get_records_sql( + $groupofuser = get_records_sql( 'SELECT m.userid, m.groupid, g.name FROM '.$CFG->prefix.'groups g LEFT JOIN '.$CFG->prefix.'groups_members m ON g.id = m.groupid '. 'WHERE g.courseid = '.$courseid.' AND m.userid IN ('.implode(',', $userids).')' - );*/ + ); $groupgrades = array(); diff --git a/course/lib.php b/course/lib.php index 72f6ffe01..e6114ae31 100644 --- a/course/lib.php +++ b/course/lib.php @@ -177,15 +177,19 @@ function print_recent_selector_form($course, $advancedfilter=0, $selecteduser=0, $groupmode = groupmode($course); if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/course:managegroups', get_context_instance(CONTEXT_COURSE, $course->id)))) { - if ($groups_names = groups_get_groups_names($course->id)) { //TODO:check. - echo ''; + if ($groups = groups_get_all_groups($course->id)) { + $group_names = array(); + foreach($groups as $group) { + $group_names[$group->id] = format_string($group->name); + } + echo ''; if ($groupmode == VISIBLEGROUPS) { print_string('groupsvisible'); } else { print_string('groupsseparate'); } echo ':'; - choose_from_menu($groups_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", ""); + choose_from_menu($group_names, "selectedgroup", $selectedgroup, get_string("allgroups"), "", ""); echo ''; } } diff --git a/enrol/imsenterprise/enrol.php b/enrol/imsenterprise/enrol.php index ebc4530f2..96c7fac5a 100644 --- a/enrol/imsenterprise/enrol.php +++ b/enrol/imsenterprise/enrol.php @@ -795,7 +795,7 @@ function process_membership_tag($tagcontents){ } // Add the user-to-group association if it doesn't already exist if($member->groupid) { - add_user_to_group ($member->groupid, $memberstoreobj->userid); + groups_add_member($member->groupid, $memberstoreobj->userid); } } // End of group-enrolment (from member.role.extension.cohort tag) diff --git a/enrol/manual/enrol.php b/enrol/manual/enrol.php index 7a31408d4..347e7e610 100644 --- a/enrol/manual/enrol.php +++ b/enrol/manual/enrol.php @@ -147,7 +147,7 @@ function check_entry($form, $course) { } else { /// Update or add new enrolment if (enrol_into_course($course, $USER, 'manual')) { if ($groupid !== false) { - if (!add_user_to_group($groupid, $USER->id)) { + if (!groups_add_member($groupid, $USER->id)) { print_error('couldnotassigngroup'); } } diff --git a/group/lib/clientlib.js b/group/clientlib.js similarity index 100% rename from group/lib/clientlib.js rename to group/clientlib.js diff --git a/group/db/dbbasicgrouplib.php b/group/db/dbbasicgrouplib.php deleted file mode 100644 index 40e3e9ad0..000000000 --- a/group/db/dbbasicgrouplib.php +++ /dev/null @@ -1,325 +0,0 @@ -id); - } - - return $groupids; -} - - -/** - * Returns the ids of the users in the specified group. - * @param int $groupid The groupid to get the users for - * @return array| false Returns an array of the user ids for the specified - * group or false if no users or an error returned. - */ -function groups_db_get_members($groupid) { - if (!$groupid) { - $userids = false; - } else { - $users = get_records('groups_members', 'groupid ', $groupid, '', - $fields='id, userid'); - if (!$users) { - $userids = false; - } else { - $userids = array(); - foreach ($users as $user) { - array_push($userids, $user->userid); - } - } - } - return $userids; -} - - -/** - * Gets the groups to which a user belongs for a specified course. - * @uses $CFG - * @param int $userid The id of the specified user - * @param int $courseid The id of the course. - * @return array | false An array of the group ids of the groups to which the - * user belongs or false if there are no groups or an error occurred. - */ -function groups_db_get_groups_for_user($userid, $courseid) { - if (!$userid or !$courseid) { - $groupids = false; - } else { - global $CFG; - $sql = "SELECT g.id, gm.userid - FROM {$CFG->prefix}groups_members gm - INNER JOIN {$CFG->prefix}groups g - ON gm.groupid = g.id - WHERE g.courseid = '$courseid' AND gm.userid = '$userid'"; - - $groups = get_records_sql($sql); - $groupids = groups_groups_to_groupids($groups); - } - - return $groupids; -} - - -/** - * Get the group settings object for a group - this contains the following - * properties: - * name, description, picture, hidepicture - * @param int $groupid The id of the group - * @param $courseid Optionally add the course ID, for backwards compatibility. - * @return object The group settings object - */ -function groups_db_get_group_settings($groupid, $courseid=false, $alldata=false) { - if (!$groupid) { - $groupsettings = false; - } else { - global $CFG; - $select = ($alldata) ? '*' : 'id, name, description, picture, hidepicture'; - $sql = "SELECT $select - FROM {$CFG->prefix}groups - WHERE id = $groupid"; - $groupsettings = get_record_sql($sql); - if ($courseid && $groupsettings) { - $groupsettings->courseid = $courseid; - } - } - - return $groupsettings; - -} - -/******************************************************************************* - Membership functions - ******************************************************************************/ - - -/** - * Determines if a group with a given groupid exists. - * @param int $groupid The groupid to check for - * @return boolean True if the group exists, false otherwise or if an error - * occurred. - */ -function groups_db_group_exists($groupid) { - if (!$groupid) { - $exists = false; - } else { - $exists = record_exists($table = 'groups', 'id', $groupid); - } - - return $exists; -} - -/** - * Determines if a specified group is a group for a specified course - * @param int $groupid The group about which the request has been made - * @param int $courseid The course for which the request has been made - * @return boolean True if the group belongs to the course, false otherwise - */ -function groups_db_group_belongs_to_course($groupid, $courseid) { - if (!$groupid or !$courseid) { - $ismember = false; - } else { - $ismember = record_exists($table = 'groups', - 'id', $groupid, - 'courseid', $courseid); - } - - return $ismember; -} - - -/******************************************************************************* - Creation functions - ******************************************************************************/ - - -/** - * Creates a group for a specified course - * @param int $courseid The course to create the group for - * @return int The id of the group created or false if the create failed. - */ -function groups_db_create_group($courseid, $groupsettings=false, $copytime=false) { - // Check we have a valid course id - if (!$courseid) { - $groupid = false; - } else { - $groupsettings = groups_set_default_group_settings($groupsettings); - - $record = $groupsettings; - if (! $copytime) { - $now = time(); - $record->timecreated = $now; - $record->timemodified = $now; - } - //print_r($record); - $groupid = insert_record('groups', $record); - - } - return $groupid; -} - -/** - * Upgrades a group for a specified course. To preserve the group ID we do a raw insert. - * @param int $courseid The course to create the group for - * @return int The id of the group created or false if the insert failed. - */ -function groups_db_upgrade_group($courseid, $group) { - global $CFG; - // Check we have a valid course id - if (!$courseid || !$group || !isset($group->id)) { - return false; - } - - $r = addslashes_object($group); - $sql = "INSERT INTO {$CFG->prefix}groups - (id,courseid,name,description, enrolmentkey,picture,hidepicture, timecreated,timemodified) - VALUES ('$r->id','$r->courseid','$r->name','$r->description', '$r->enrolmentkey','$r->picture', - '$r->hidepicture', '$r->timecreated','$r->timemodified')"; - - $result = execute_sql($sql); - return $group->id; -} - - -/** - * Adds a specified user to a group - * @param int $groupid The group id - * @param int $userid The user id - * @return boolean True if user added successfully, false otherwise. - */ -function groups_db_add_member($groupid, $userid, $copytime=false) { - // Check that the user and group are valid - if (!$userid or !$groupid or !groups_db_group_exists($groupid)) { - $useradded = false; - // If the user is already a member of the group, just return success - } elseif (groups_is_member($groupid, $userid)) { - $useradded = true; - } else { - // Add the user to the group - $record = new Object(); - $record->groupid = $groupid; - $record->userid = $userid; - if ($copytime) { - $record->timeadded = $copytime; - } else { - $record->timeadded = time(); - } - $useradded = insert_record($table = 'groups_members', $record); - } - - return $useradded; -} - - -/** - * Sets the information about a group - * @param object $groupsettings An object containing some or all of the - * following properties: - * name, description, picture, hidepicture - * @return boolean True if info was added successfully, false otherwise. - */ -function groups_db_set_group_settings($groupid, $groupsettings) { - $success = true; - if (!$groupid or !$groupsettings or !groups_db_group_exists($groupid)) { - $success = false; - } else { - $record = $groupsettings; - $record->id = $groupid; - $record->timemodified = time(); - $result = update_record('groups', $record); - if (!$result) { - $success = false; - } - } - - return $success; -} - -/******************************************************************************* - Deletion functions - ******************************************************************************/ - - -/** - * Deletes the specified user from the specified group - * @param int $groupid The group to delete the user from - * @param int $userid The user to delete - * @return boolean True if deletion was successful, false otherwise - */ -function groups_db_remove_member($groupid, $userid) { - if (!$userid or !$groupid) { - $success = false; - } else { - $results = delete_records('groups_members', - 'groupid', $groupid, 'userid', $userid); - // delete_records returns an array of the results from the sql call, - // not a boolean, so we have to set our return variable - if ($results == false) { - $success = false; - } else { - $success = true; - } - } - - return $success; -} - - -/** - * Internal function to set the time a group was modified. - */ -function groups_db_set_group_modified($groupid) { - return set_field('groups', 'timemodified', time(), 'id', $groupid); -} - - -?> diff --git a/group/index.php b/group/index.php index f81944083..a12281f1e 100644 --- a/group/index.php +++ b/group/index.php @@ -16,7 +16,7 @@ require_js('yui_yahoo'); require_js('yui_dom'); require_js('yui_utilities'); require_js('yui_connection'); -require_js($CFG->wwwroot.'/group/lib/clientlib.js'); +require_js($CFG->wwwroot.'/group/clientlib.js'); $courseid = required_param('id', PARAM_INT); $groupid = optional_param('group', false, PARAM_INT); @@ -134,28 +134,22 @@ if (!$course = get_record('course', 'id',$courseid)) { echo '