2007-01-04 13:15:04 +13:00
|
|
|
<?php
|
|
|
|
/**
|
2007-08-15 23:51:07 +00:00
|
|
|
* Extra library for groups and groupings.
|
2007-01-04 13:15:04 +13:00
|
|
|
*
|
|
|
|
* @copyright © 2006 The Open University
|
2008-07-06 17:57:06 +00:00
|
|
|
* @author J.White AT open.ac.uk, Petr Skoda (skodak)
|
2007-01-04 13:15:04 +13:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
* @package groups
|
|
|
|
*/
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
/*
|
|
|
|
* INTERNAL FUNCTIONS - to be used by moodle core only
|
|
|
|
* require_once $CFG->dirroot.'/group/lib.php' must be used
|
|
|
|
*/
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
/**
|
|
|
|
* Adds a specified user to a group
|
2009-09-14 23:52:08 +00:00
|
|
|
* @param mixed $groupid The group id or group object
|
|
|
|
* @param mixed $userid The user id or user object
|
2007-11-19 20:31:57 +00:00
|
|
|
* @return boolean True if user added successfully or the user is already a
|
|
|
|
* member of the group, false otherwise.
|
2007-08-16 21:14:03 +00:00
|
|
|
*/
|
2009-09-14 23:52:08 +00:00
|
|
|
function groups_add_member($grouporid, $userorid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
if (is_object($userorid)) {
|
|
|
|
$userid = $userorid->id;
|
|
|
|
$user = $userorid;
|
|
|
|
} else {
|
|
|
|
$userid = $userorid;
|
2009-09-16 10:08:37 +00:00
|
|
|
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
|
2009-03-09 02:19:38 +00:00
|
|
|
}
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
if (is_object($grouporid)) {
|
|
|
|
$groupid = $grouporid->id;
|
|
|
|
$group = $grouporid;
|
|
|
|
} else {
|
|
|
|
$groupid = $grouporid;
|
|
|
|
$group = $DB->get_record('groups', array('id'=>$groupid), '*', MUST_EXIST);
|
2007-08-16 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 06:51:30 +00:00
|
|
|
//check if the user a participant of the group course
|
|
|
|
if (!is_course_participant ($userid, $group->courseid)) {
|
2009-09-14 23:52:08 +00:00
|
|
|
return false;
|
2009-03-13 06:51:30 +00:00
|
|
|
}
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
if (groups_is_member($groupid, $userid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$member = new object();
|
|
|
|
$member->groupid = $groupid;
|
|
|
|
$member->userid = $userid;
|
|
|
|
$member->timeadded = time();
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->insert_record('groups_members', $member);
|
2007-08-16 21:14:03 +00:00
|
|
|
|
|
|
|
//update group info
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('groups', 'timemodified', $member->timeadded, array('id'=>$groupid));
|
2007-08-16 21:14:03 +00:00
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
//trigger groups events
|
2007-08-16 21:14:03 +00:00
|
|
|
$eventdata = new object();
|
|
|
|
$eventdata->groupid = $groupid;
|
2008-07-06 17:57:06 +00:00
|
|
|
$eventdata->userid = $userid;
|
|
|
|
events_trigger('groups_member_added', $eventdata);
|
2007-08-16 21:14:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the link between the specified user and group.
|
2009-09-14 23:52:08 +00:00
|
|
|
* @param mixed $groupid The group id or group object
|
|
|
|
* @param mixed $userid The user id or user object
|
2007-08-16 21:14:03 +00:00
|
|
|
* @return boolean True if deletion was successful, false otherwise
|
|
|
|
*/
|
2009-09-14 23:52:08 +00:00
|
|
|
function groups_remove_member($grouporid, $userorid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
if (is_object($userorid)) {
|
|
|
|
$userid = $userorid->id;
|
|
|
|
$user = $userorid;
|
|
|
|
} else {
|
|
|
|
$userid = $userorid;
|
2009-09-16 10:08:37 +00:00
|
|
|
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
|
2009-03-09 02:19:38 +00:00
|
|
|
}
|
|
|
|
|
2009-09-14 23:52:08 +00:00
|
|
|
if (is_object($grouporid)) {
|
|
|
|
$groupid = $grouporid->id;
|
|
|
|
$group = $grouporid;
|
|
|
|
} else {
|
|
|
|
$groupid = $grouporid;
|
|
|
|
$group = $DB->get_record('groups', array('id'=>$groupid), '*', MUST_EXIST);
|
2007-08-16 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!groups_is_member($groupid, $userid)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->delete_records('groups_members', array('groupid'=>$groupid, 'userid'=>$userid));
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
//update group info
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('groups', 'timemodified', time(), array('id'=>$groupid));
|
2007-08-16 21:14:03 +00:00
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
//trigger groups events
|
|
|
|
$eventdata = new object();
|
|
|
|
$eventdata->groupid = $groupid;
|
|
|
|
$eventdata->userid = $userid;
|
|
|
|
events_trigger('groups_member_removed', $eventdata);
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
/**
|
|
|
|
* Add a new group
|
2007-11-19 20:31:57 +00:00
|
|
|
* @param object $data group properties (with magic quotes)
|
2007-08-15 23:51:07 +00:00
|
|
|
* @param object $um upload manager with group picture
|
|
|
|
* @return id of group or false if error
|
|
|
|
*/
|
2009-11-04 06:14:06 +00:00
|
|
|
function groups_create_group($data, $editform=false, $editoroptions=null) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $CFG, $DB;
|
2007-08-15 23:51:07 +00:00
|
|
|
require_once("$CFG->libdir/gdlib.php");
|
2009-11-01 16:48:45 +00:00
|
|
|
|
2009-03-09 02:19:38 +00:00
|
|
|
//check that courseid exists
|
2009-09-14 23:52:08 +00:00
|
|
|
$course = $DB->get_record('course', array('id' => $data->courseid), '*', MUST_EXIST);
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->timecreated = time();
|
2007-08-15 23:51:07 +00:00
|
|
|
$data->timemodified = $data->timecreated;
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->name = trim($data->name);
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
if ($editform) {
|
|
|
|
$data->description = $data->description_editor['text'];
|
|
|
|
$data->descriptionformat = $data->description_editor['format'];
|
|
|
|
}
|
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$id = $DB->insert_record('groups', $data);
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
$data->id = $id;
|
|
|
|
if ($editform) {
|
|
|
|
//update image
|
|
|
|
if (save_profile_image($id, $editform, 'groups')) {
|
|
|
|
$DB->set_field('groups', 'picture', 1, array('id'=>$id));
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
2009-02-17 16:44:48 +00:00
|
|
|
$data->picture = 1;
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
if (method_exists($editform, 'get_editor_options')) {
|
|
|
|
// Update description from editor with fixed files
|
|
|
|
$editoroptions = $editform->get_editor_options();
|
|
|
|
$description = new stdClass;
|
|
|
|
$description->id = $data->id;
|
|
|
|
$description->description_editor = $data->description_editor;
|
|
|
|
$description = file_postupdate_standard_editor($description, 'description', $editoroptions, $editoroptions['context'], 'course_group_description', $description->id);
|
|
|
|
$DB->update_record('groups', $description);
|
|
|
|
}
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_group_created', $data);
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-11-19 20:31:57 +00:00
|
|
|
* Add a new grouping
|
|
|
|
* @param object $data grouping properties (with magic quotes)
|
|
|
|
* @return id of grouping or false if error
|
|
|
|
*/
|
2009-11-04 06:14:06 +00:00
|
|
|
function groups_create_grouping($data, $editoroptions=null) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->timecreated = time();
|
2007-11-19 20:31:57 +00:00
|
|
|
$data->timemodified = $data->timecreated;
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->name = trim($data->name);
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
if ($editoroptions !== null) {
|
|
|
|
$data->description = $data->description_editor['text'];
|
|
|
|
$data->descriptionformat = $data->description_editor['format'];
|
|
|
|
}
|
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
$id = $DB->insert_record('groupings', $data);
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
//trigger groups events
|
|
|
|
$data->id = $id;
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
if ($editoroptions !== null) {
|
|
|
|
$description = new stdClass;
|
|
|
|
$description->id = $data->id;
|
|
|
|
$description->description_editor = $data->description_editor;
|
|
|
|
$description = file_postupdate_standard_editor($description, 'description', $editoroptions, $editoroptions['context'], 'course_grouping_description', $description->id);
|
|
|
|
$DB->update_record('groupings', $description);
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
events_trigger('groups_grouping_created', $data);
|
2008-07-06 17:57:06 +00:00
|
|
|
|
|
|
|
return $id;
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update group
|
|
|
|
* @param object $data group properties (with magic quotes)
|
2007-08-15 23:51:07 +00:00
|
|
|
* @param object $um upload manager with group picture
|
2009-02-17 16:44:48 +00:00
|
|
|
* @return boolean true or exception
|
2007-08-15 23:51:07 +00:00
|
|
|
*/
|
2008-07-31 22:15:30 +00:00
|
|
|
function groups_update_group($data, $editform=false) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $CFG, $DB;
|
2007-08-15 23:51:07 +00:00
|
|
|
require_once("$CFG->libdir/gdlib.php");
|
|
|
|
|
|
|
|
$data->timemodified = time();
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->name = trim($data->name);
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
if ($editform && method_exists($editform, 'get_editor_options')) {
|
|
|
|
$editoroptions = $editform->get_editor_options();
|
|
|
|
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'course_group_description', $data->id);
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->update_record('groups', $data);
|
2008-07-06 17:57:06 +00:00
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
if ($editform) {
|
|
|
|
//update image
|
|
|
|
if (save_profile_image($data->id, $editform, 'groups')) {
|
|
|
|
$DB->set_field('groups', 'picture', 1, array('id'=>$data->id));
|
|
|
|
$data->picture = 1;
|
|
|
|
}
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_group_updated', $data);
|
|
|
|
|
|
|
|
return true;
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
|
|
|
* Update grouping
|
|
|
|
* @param object $data grouping properties (with magic quotes)
|
2009-02-17 16:44:48 +00:00
|
|
|
* @return boolean true or exception
|
2007-11-19 20:31:57 +00:00
|
|
|
*/
|
2009-11-04 06:14:06 +00:00
|
|
|
function groups_update_grouping($data, $editoroptions=null) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
2007-11-19 20:31:57 +00:00
|
|
|
$data->timemodified = time();
|
2008-06-01 13:09:04 +00:00
|
|
|
$data->name = trim($data->name);
|
2009-11-04 06:14:06 +00:00
|
|
|
if ($editoroptions !== null) {
|
|
|
|
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $editoroptions['context'], 'course_grouping_description', $data->id);
|
|
|
|
}
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->update_record('groupings', $data);
|
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_grouping_updated', $data);
|
|
|
|
|
|
|
|
return true;
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
/**
|
|
|
|
* Delete a group best effort, first removing members and links with courses and groupings.
|
|
|
|
* Removes group avatar too.
|
2008-07-06 17:57:06 +00:00
|
|
|
* @param mixed $grouporid The id of group to delete or full group object
|
2007-08-15 23:51:07 +00:00
|
|
|
* @return boolean True if deletion was successful, false otherwise
|
|
|
|
*/
|
2008-07-06 17:57:06 +00:00
|
|
|
function groups_delete_group($grouporid) {
|
2009-09-16 10:06:05 +00:00
|
|
|
global $CFG, $DB;
|
|
|
|
require_once("$CFG->libdir/gdlib.php");
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
if (is_object($grouporid)) {
|
|
|
|
$groupid = $grouporid->id;
|
|
|
|
$group = $grouporid;
|
|
|
|
} else {
|
|
|
|
$groupid = $grouporid;
|
|
|
|
if (!$group = $DB->get_record('groups', array('id'=>$groupid))) {
|
2009-09-14 23:52:08 +00:00
|
|
|
//silently ignore attempts to delete missing already deleted groups ;-)
|
|
|
|
return true;
|
2008-07-06 17:57:06 +00:00
|
|
|
}
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
// delete group calendar events
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('event', array('groupid'=>$groupid));
|
2007-08-15 23:51:07 +00:00
|
|
|
//first delete usage in groupings_groups
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('groupings_groups', array('groupid'=>$groupid));
|
2007-08-15 23:51:07 +00:00
|
|
|
//delete members
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('groups_members', array('groupid'=>$groupid));
|
2007-08-15 23:51:07 +00:00
|
|
|
//then imge
|
|
|
|
delete_profile_image($groupid, 'groups');
|
|
|
|
//group itself last
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->delete_records('groups', array('id'=>$groupid));
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
// Delete all files associated with this group
|
2009-12-07 10:15:07 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $group->courseid);
|
2009-11-04 06:14:06 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$files = $fs->get_area_files($context->id, 'course_group_description', $groupid);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_group_deleted', $group);
|
2008-07-06 17:57:06 +00:00
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
return true;
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
|
|
|
* Delete grouping
|
|
|
|
* @param int $groupingid
|
|
|
|
* @return bool success
|
|
|
|
*/
|
2008-07-06 17:57:06 +00:00
|
|
|
function groups_delete_grouping($groupingorid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
if (is_object($groupingorid)) {
|
|
|
|
$groupingid = $groupingorid->id;
|
|
|
|
$grouping = $groupingorid;
|
|
|
|
} else {
|
|
|
|
$groupingid = $groupingorid;
|
|
|
|
if (!$grouping = $DB->get_record('groupings', array('id'=>$groupingorid))) {
|
2009-09-14 23:52:08 +00:00
|
|
|
//silently ignore attempts to delete missing already deleted groupings ;-)
|
|
|
|
return true;
|
2008-07-06 17:57:06 +00:00
|
|
|
}
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//first delete usage in groupings_groups
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('groupings_groups', array('groupingid'=>$groupingid));
|
2007-08-15 23:51:07 +00:00
|
|
|
// remove the default groupingid from course
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('course', 'defaultgroupingid', 0, array('defaultgroupingid'=>$groupingid));
|
2007-08-15 23:51:07 +00:00
|
|
|
// remove the groupingid from all course modules
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('course_modules', 'groupingid', 0, array('groupingid'=>$groupingid));
|
2007-08-15 23:51:07 +00:00
|
|
|
//group itself last
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->delete_records('groupings', array('id'=>$groupingid));
|
2009-11-04 06:14:06 +00:00
|
|
|
|
2009-12-07 10:15:07 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $grouping->courseid);
|
2009-11-04 06:14:06 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$files = $fs->get_area_files($context->id, 'course_grouping_description', $groupingid);
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$file->delete();
|
|
|
|
}
|
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_grouping_deleted', $grouping);
|
2008-07-06 17:57:06 +00:00
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
return true;
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
2008-07-06 22:54:46 +00:00
|
|
|
* Remove all users (or one user) from all groups in course
|
2007-11-19 20:31:57 +00:00
|
|
|
* @param int $courseid
|
2008-07-06 22:54:46 +00:00
|
|
|
* @param int $userid 0 means all users
|
2007-11-19 20:31:57 +00:00
|
|
|
* @param bool $showfeedback
|
|
|
|
* @return bool success
|
|
|
|
*/
|
2008-07-06 22:54:46 +00:00
|
|
|
function groups_delete_group_members($courseid, $userid=0, $showfeedback=false) {
|
2009-08-18 05:00:29 +00:00
|
|
|
global $DB, $OUTPUT;
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2008-07-06 22:54:46 +00:00
|
|
|
if (is_bool($userid)) {
|
|
|
|
debugging('Incorrect userid function parameter');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = array('courseid'=>$courseid);
|
|
|
|
|
|
|
|
if ($userid) {
|
|
|
|
$usersql = "AND userid = :userid";
|
|
|
|
$params['userid'] = $userid;
|
|
|
|
} else {
|
|
|
|
$usersql = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$groupssql = "SELECT id FROM {groups} g WHERE g.courseid = :courseid";
|
|
|
|
$DB->delete_records_select('groups_members', "groupid IN ($groupssql) $usersql", $params);
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
//trigger groups events
|
2008-07-06 22:54:46 +00:00
|
|
|
$eventdata = new object();
|
|
|
|
$eventdata->courseid = $courseid;
|
|
|
|
$eventdata->userid = $userid;
|
|
|
|
events_trigger('groups_members_removed', $eventdata);
|
2008-07-06 17:57:06 +00:00
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
if ($showfeedback) {
|
2009-08-18 05:00:29 +00:00
|
|
|
echo $OUTPUT->notification(get_string('deleted').' groups_members');
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
/**
|
|
|
|
* Remove all groups from all groupings in course
|
|
|
|
* @param int $courseid
|
|
|
|
* @param bool $showfeedback
|
|
|
|
* @return bool success
|
|
|
|
*/
|
|
|
|
function groups_delete_groupings_groups($courseid, $showfeedback=false) {
|
2009-08-18 05:00:29 +00:00
|
|
|
global $DB, $OUTPUT;
|
2007-11-29 14:43:04 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$groupssql = "SELECT id FROM {groups} g WHERE g.courseid = ?";
|
|
|
|
$DB->delete_records_select('groupings_groups', "groupid IN ($groupssql)", array($courseid));
|
2007-11-29 14:43:04 +00:00
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
// Delete all files associated with groupings for this course
|
2009-12-07 10:15:07 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
2009-11-04 06:14:06 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$fs->delete_area_files($context->id, 'course_group_description');
|
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_groupings_groups_removed', $courseid);
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
if ($showfeedback) {
|
2009-08-18 05:00:29 +00:00
|
|
|
echo $OUTPUT->notification(get_string('deleted').' groupings_groups');
|
2007-11-29 14:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
|
|
|
* Delete all groups from course
|
|
|
|
* @param int $courseid
|
|
|
|
* @param bool $showfeedback
|
|
|
|
* @return bool success
|
|
|
|
*/
|
2007-08-15 23:51:07 +00:00
|
|
|
function groups_delete_groups($courseid, $showfeedback=false) {
|
2009-08-18 05:00:29 +00:00
|
|
|
global $CFG, $DB, $OUTPUT;
|
2007-08-15 23:51:07 +00:00
|
|
|
require_once($CFG->libdir.'/gdlib.php');
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
// delete any uses of groups
|
2009-11-04 06:14:06 +00:00
|
|
|
// Any associated files are deleted as part of groups_delete_groupings_groups
|
2007-11-29 14:43:04 +00:00
|
|
|
groups_delete_groupings_groups($courseid, $showfeedback);
|
2008-07-06 22:54:46 +00:00
|
|
|
groups_delete_group_members($courseid, 0, $showfeedback);
|
2007-08-15 23:51:07 +00:00
|
|
|
|
|
|
|
// delete group pictures
|
2008-06-01 13:09:04 +00:00
|
|
|
if ($groups = $DB->get_records('groups', array('courseid'=>$courseid))) {
|
2007-08-15 23:51:07 +00:00
|
|
|
foreach($groups as $group) {
|
|
|
|
delete_profile_image($group->id, 'groups');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 14:43:04 +00:00
|
|
|
// delete group calendar events
|
2008-06-01 13:09:04 +00:00
|
|
|
$groupssql = "SELECT id FROM {groups} g WHERE g.courseid = ?";
|
|
|
|
$DB->delete_records_select('event', "groupid IN ($groupssql)", array($courseid));
|
2007-11-29 14:43:04 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('groups', array('courseid'=>$courseid));
|
2008-07-06 17:57:06 +00:00
|
|
|
|
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_groups_deleted', $courseid);
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
if ($showfeedback) {
|
2009-08-18 05:00:29 +00:00
|
|
|
echo $OUTPUT->notification(get_string('deleted').' groups');
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
|
|
|
* Delete all groupings from course
|
|
|
|
* @param int $courseid
|
|
|
|
* @param bool $showfeedback
|
|
|
|
* @return bool success
|
|
|
|
*/
|
2007-08-15 23:51:07 +00:00
|
|
|
function groups_delete_groupings($courseid, $showfeedback=false) {
|
2009-08-18 05:00:29 +00:00
|
|
|
global $DB, $OUTPUT;
|
2007-08-15 23:51:07 +00:00
|
|
|
|
|
|
|
// delete any uses of groupings
|
2008-06-01 13:09:04 +00:00
|
|
|
$sql = "DELETE FROM {groupings_groups}
|
|
|
|
WHERE groupingid in (SELECT id FROM {groupings} g WHERE g.courseid = ?)";
|
|
|
|
$DB->execute($sql, array($courseid));
|
2007-08-15 23:51:07 +00:00
|
|
|
|
|
|
|
// remove the default groupingid from course
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('course', 'defaultgroupingid', 0, array('id'=>$courseid));
|
2007-08-15 23:51:07 +00:00
|
|
|
// remove the groupingid from all course modules
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->set_field('course_modules', 'groupingid', 0, array('course'=>$courseid));
|
2007-08-15 23:51:07 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$DB->delete_records('groupings', array('courseid'=>$courseid));
|
2008-07-06 17:57:06 +00:00
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
// Delete all files associated with groupings for this course
|
2009-12-07 10:15:07 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
2009-11-04 06:14:06 +00:00
|
|
|
$fs = get_file_storage();
|
|
|
|
$fs->delete_area_files($context->id, 'course_grouping_description');
|
|
|
|
|
2008-07-06 17:57:06 +00:00
|
|
|
//trigger groups events
|
|
|
|
events_trigger('groups_groupings_deleted', $courseid);
|
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
if ($showfeedback) {
|
2009-08-18 05:00:29 +00:00
|
|
|
echo $OUTPUT->notification(get_string('deleted').' groupings');
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* =================================== */
|
|
|
|
/* various functions used by groups UI */
|
|
|
|
/* =================================== */
|
|
|
|
|
2008-01-17 11:28:54 +00:00
|
|
|
/**
|
|
|
|
* Obtains a list of the possible roles that group members might come from,
|
2009-11-01 16:48:45 +00:00
|
|
|
* on a course. Generally this includes all the roles who would have
|
2008-01-17 11:28:54 +00:00
|
|
|
* course:view on that course, except the doanything roles.
|
|
|
|
* @param object $context Context of course
|
|
|
|
* @return Array of role ID integers, or false if error/none.
|
|
|
|
*/
|
|
|
|
function groups_get_possible_roles($context) {
|
2007-08-15 23:51:07 +00:00
|
|
|
$capability = 'moodle/course:view';
|
|
|
|
$doanything = false;
|
2007-01-04 13:15:04 +13:00
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
// find all possible "student" roles
|
|
|
|
if ($possibleroles = get_roles_with_capability($capability, CAP_ALLOW, $context)) {
|
|
|
|
if (!$doanything) {
|
|
|
|
if (!$sitecontext = get_context_instance(CONTEXT_SYSTEM)) {
|
|
|
|
return false; // Something is seriously wrong
|
|
|
|
}
|
|
|
|
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
|
|
|
|
}
|
2007-01-04 13:15:04 +13:00
|
|
|
|
2007-08-15 23:51:07 +00:00
|
|
|
$validroleids = array();
|
|
|
|
foreach ($possibleroles as $possiblerole) {
|
|
|
|
if (!$doanything) {
|
|
|
|
if (isset($doanythingroles[$possiblerole->id])) { // We don't want these included
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($caps = role_context_capabilities($possiblerole->id, $context, $capability)) { // resolved list
|
|
|
|
if (isset($caps[$capability]) && $caps[$capability] > 0) { // resolved capability > 0
|
|
|
|
$validroleids[] = $possiblerole->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($validroleids)) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-01-17 11:28:54 +00:00
|
|
|
return $validroleids;
|
2007-08-15 23:51:07 +00:00
|
|
|
} else {
|
|
|
|
return false; // No need to continue, since no roles have this capability set
|
2009-11-01 16:48:45 +00:00
|
|
|
}
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets potential group members for grouping
|
|
|
|
* @param int $courseid The id of the course
|
|
|
|
* @param int $roleid The role to select users from
|
|
|
|
* @param string $orderby The colum to sort users by
|
|
|
|
* @return array An array of the users
|
|
|
|
*/
|
2007-11-19 20:31:57 +00:00
|
|
|
function groups_get_potential_members($courseid, $roleid = null, $orderby = 'lastname,firstname') {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
$rolenames = array();
|
|
|
|
$avoidroles = array();
|
|
|
|
|
|
|
|
if ($roles = get_roles_used_in_context($context, true)) {
|
|
|
|
|
|
|
|
$canviewroles = get_roles_with_capability('moodle/course:view', CAP_ALLOW, $context);
|
|
|
|
$doanythingroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW, $sitecontext);
|
|
|
|
|
|
|
|
foreach ($roles as $role) {
|
|
|
|
if (!isset($canviewroles[$role->id])) { // Avoid this role (eg course creator)
|
|
|
|
$avoidroles[] = $role->id;
|
|
|
|
unset($roles[$role->id]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isset($doanythingroles[$role->id])) { // Avoid this role (ie admin)
|
|
|
|
$avoidroles[] = $role->id;
|
|
|
|
unset($roles[$role->id]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$rolenames[$role->id] = strip_tags(role_get_name($role, $context)); // Used in menus etc later on
|
|
|
|
}
|
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
if ($avoidroles) {
|
2008-06-01 13:09:04 +00:00
|
|
|
list($adminroles, $params) = $DB->get_in_or_equal($avoidroles, SQL_PARAMS_NAMED, 'ar0', false);
|
|
|
|
$adminroles = "AND r.roleid $adminroles";
|
2007-09-24 21:55:15 +00:00
|
|
|
} else {
|
2008-06-01 13:09:04 +00:00
|
|
|
$adminroles = "";
|
|
|
|
$params = array();
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// we are looking for all users with this role assigned in this context or higher
|
|
|
|
if ($usercontexts = get_parent_contexts($context)) {
|
2008-06-01 13:09:04 +00:00
|
|
|
$listofcontexts = 'IN ('.implode(',', $usercontexts).')';
|
2007-09-24 21:55:15 +00:00
|
|
|
} else {
|
2008-06-01 13:09:04 +00:00
|
|
|
$listofcontexts = '='.$sitecontext->id.')'; // must be site
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
if ($roleid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
$selectrole = "AND r.roleid = :roleid";
|
|
|
|
$params['roleid'] = $roleid;
|
2007-09-24 21:55:15 +00:00
|
|
|
} else {
|
2008-06-01 13:09:04 +00:00
|
|
|
$selectrole = "";
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$sql = "SELECT u.id, u.username, u.firstname, u.lastname, u.idnumber
|
|
|
|
FROM {user} u
|
|
|
|
JOIN {role_assignments} r on u.id=r.userid
|
|
|
|
WHERE (r.contextid = :contextid OR r.contextid $listofcontexts)
|
|
|
|
AND u.deleted = 0 AND u.username != 'guest'
|
|
|
|
$selectrole $adminroles
|
|
|
|
ORDER BY $orderby";
|
|
|
|
$params['contextid'] = $context->id;
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
return $DB->get_records_sql($sql, $params);
|
2007-11-19 20:31:57 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-01-04 13:15:04 +13:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
/**
|
|
|
|
* Parse a group name for characters to replace
|
|
|
|
* @param string $format The format a group name will follow
|
|
|
|
* @param int $groupnumber The number of the group to be used in the parsed format string
|
|
|
|
* @return string the parsed format string
|
|
|
|
*/
|
|
|
|
function groups_parse_name($format, $groupnumber) {
|
|
|
|
if (strstr($format, '@') !== false) { // Convert $groupnumber to a character series
|
2007-11-19 20:31:57 +00:00
|
|
|
$letter = 'A';
|
|
|
|
for($i=0; $i<$groupnumber; $i++) {
|
|
|
|
$letter++;
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
2007-11-19 20:31:57 +00:00
|
|
|
$str = str_replace('@', $letter, $format);
|
2007-09-24 21:55:15 +00:00
|
|
|
} else {
|
2007-11-19 20:31:57 +00:00
|
|
|
$str = str_replace('#', $groupnumber+1, $format);
|
2007-09-24 21:55:15 +00:00
|
|
|
}
|
|
|
|
return($str);
|
2007-08-15 23:51:07 +00:00
|
|
|
}
|
2007-01-04 13:15:04 +13:00
|
|
|
|
2007-11-19 20:31:57 +00:00
|
|
|
/**
|
|
|
|
* Assigns group into grouping
|
|
|
|
* @param int groupingid
|
|
|
|
* @param int groupid
|
2009-02-17 16:44:48 +00:00
|
|
|
* @return bool true or exception
|
2007-11-19 20:31:57 +00:00
|
|
|
*/
|
|
|
|
function groups_assign_grouping($groupingid, $groupid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if ($DB->record_exists('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid))) {
|
2007-11-19 20:31:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$assign = new object();
|
|
|
|
$assign->groupingid = $groupingid;
|
2008-06-01 13:09:04 +00:00
|
|
|
$assign->groupid = $groupid;
|
|
|
|
$assign->timeadded = time();
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->insert_record('groupings_groups', $assign);
|
|
|
|
|
|
|
|
return true;
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unassigns group grom grouping
|
|
|
|
* @param int groupingid
|
|
|
|
* @param int groupid
|
|
|
|
* @return bool success
|
|
|
|
*/
|
|
|
|
function groups_unassign_grouping($groupingid, $groupid) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $DB;
|
2009-02-17 16:44:48 +00:00
|
|
|
$DB->delete_records('groupings_groups', array('groupingid'=>$groupingid, 'groupid'=>$groupid));
|
2008-06-01 13:09:04 +00:00
|
|
|
|
2009-02-17 16:44:48 +00:00
|
|
|
return true;
|
2007-11-19 20:31:57 +00:00
|
|
|
}
|
|
|
|
|
2008-01-17 11:28:54 +00:00
|
|
|
/**
|
|
|
|
* Lists users in a group based on their role on the course.
|
2009-11-01 16:48:45 +00:00
|
|
|
* Returns false if there's an error or there are no users in the group.
|
2008-01-17 11:28:54 +00:00
|
|
|
* Otherwise returns an array of role ID => role data, where role data includes:
|
|
|
|
* (role) $id, $shortname, $name
|
|
|
|
* $users: array of objects for each user which include the specified fields
|
|
|
|
* Users who do not have a role are stored in the returned array with key '-'
|
|
|
|
* and pseudo-role details (including a name, 'No role'). Users with multiple
|
|
|
|
* roles, same deal with key '*' and name 'Multiple roles'. You can find out
|
|
|
|
* which roles each has by looking in the $roles array of the user object.
|
|
|
|
* @param int $groupid
|
|
|
|
* @param int $courseid Course ID (should match the group's course)
|
|
|
|
* @param string $fields List of fields from user table prefixed with u, default 'u.*'
|
|
|
|
* @param string $sort SQL ORDER BY clause, default 'u.lastname ASC'
|
2008-10-29 08:18:24 +00:00
|
|
|
* @param string $extrawheretest extra SQL conditions ANDed with the existing where clause.
|
|
|
|
* @param array $whereparams any parameters required by $extrawheretest.
|
2008-01-17 11:28:54 +00:00
|
|
|
* @return array Complex array as described above
|
|
|
|
*/
|
2008-10-29 08:18:24 +00:00
|
|
|
function groups_get_members_by_role($groupid, $courseid, $fields='u.*',
|
|
|
|
$sort='u.lastname ASC', $extrawheretest='', $whereparams=array()) {
|
2008-06-01 13:09:04 +00:00
|
|
|
global $CFG, $DB;
|
2008-01-17 11:28:54 +00:00
|
|
|
|
|
|
|
// Retrieve information about all users and their roles on the course or
|
2009-11-01 16:48:45 +00:00
|
|
|
// parent ('related') contexts
|
2008-06-01 13:09:04 +00:00
|
|
|
$context = get_context_instance(CONTEXT_COURSE, $courseid);
|
|
|
|
|
2008-10-29 08:18:24 +00:00
|
|
|
if ($extrawheretest) {
|
|
|
|
$extrawheretest = ' AND ' . $extrawheretest;
|
|
|
|
}
|
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$sql = "SELECT r.id AS roleid, r.shortname AS roleshortname, r.name AS rolename,
|
|
|
|
u.id AS userid, $fields
|
|
|
|
FROM {groups_members} gm
|
|
|
|
JOIN {user} u ON u.id = gm.userid
|
2009-11-01 16:48:45 +00:00
|
|
|
JOIN {role_assignments} ra ON ra.userid = u.id
|
2008-06-01 13:09:04 +00:00
|
|
|
JOIN {role} r ON r.id = ra.roleid
|
|
|
|
WHERE gm.groupid=?
|
2008-10-29 08:18:24 +00:00
|
|
|
AND ra.contextid ".get_related_contexts_string($context).
|
|
|
|
$extrawheretest."
|
2008-06-01 13:09:04 +00:00
|
|
|
ORDER BY r.sortorder, $sort";
|
2008-10-29 08:18:24 +00:00
|
|
|
array_unshift($whereparams, $groupid);
|
|
|
|
$rs = $DB->get_recordset_sql($sql, $whereparams);
|
2008-06-01 13:09:04 +00:00
|
|
|
|
|
|
|
return groups_calculate_role_people($rs, $context);
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function used by groups_get_members_by_role to handle the
|
|
|
|
* results of a database query that includes a list of users and possible
|
|
|
|
* roles on a course.
|
|
|
|
*
|
|
|
|
* @param object $rs The record set (may be false)
|
2008-01-18 11:03:00 +00:00
|
|
|
* @param int $contextid ID of course context
|
2009-11-01 16:48:45 +00:00
|
|
|
* @return array As described in groups_get_members_by_role
|
2008-01-17 11:28:54 +00:00
|
|
|
*/
|
2008-06-01 13:09:04 +00:00
|
|
|
function groups_calculate_role_people($rs, $context) {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
if (!$rs) {
|
|
|
|
return array();
|
2008-01-18 11:03:00 +00:00
|
|
|
}
|
2008-01-17 11:28:54 +00:00
|
|
|
|
2008-06-01 13:09:04 +00:00
|
|
|
$roles = $DB->get_records_menu('role', null, 'name', 'id, name');
|
|
|
|
$aliasnames = role_fix_names($roles, $context);
|
|
|
|
|
2008-01-17 11:28:54 +00:00
|
|
|
// Array of all involved roles
|
2008-06-01 13:09:04 +00:00
|
|
|
$roles = array();
|
2008-01-17 11:28:54 +00:00
|
|
|
// Array of all retrieved users
|
2008-06-01 13:09:04 +00:00
|
|
|
$users = array();
|
2008-01-17 11:28:54 +00:00
|
|
|
// Fill arrays
|
2008-06-01 13:09:04 +00:00
|
|
|
foreach ($rs as $rec) {
|
2008-01-17 11:28:54 +00:00
|
|
|
// Create information about user if this is a new one
|
2008-06-01 13:09:04 +00:00
|
|
|
if (!array_key_exists($rec->userid, $users)) {
|
2008-01-17 11:28:54 +00:00
|
|
|
// User data includes all the optional fields, but not any of the
|
|
|
|
// stuff we added to get the role details
|
|
|
|
$userdata=clone($rec);
|
|
|
|
unset($userdata->roleid);
|
|
|
|
unset($userdata->roleshortname);
|
|
|
|
unset($userdata->rolename);
|
|
|
|
unset($userdata->userid);
|
2008-06-01 13:09:04 +00:00
|
|
|
$userdata->id = $rec->userid;
|
2008-01-17 11:28:54 +00:00
|
|
|
|
|
|
|
// Make an array to hold the list of roles for this user
|
2008-06-01 13:09:04 +00:00
|
|
|
$userdata->roles = array();
|
|
|
|
$users[$rec->userid] = $userdata;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
|
|
|
// If user has a role...
|
2008-06-01 13:09:04 +00:00
|
|
|
if (!is_null($rec->roleid)) {
|
2008-01-17 11:28:54 +00:00
|
|
|
// Create information about role if this is a new one
|
2008-06-01 13:09:04 +00:00
|
|
|
if (!array_key_exists($rec->roleid,$roles)) {
|
|
|
|
$roledata = new object();
|
|
|
|
$roledata->id = $rec->roleid;
|
|
|
|
$roledata->shortname = $rec->roleshortname;
|
|
|
|
if (array_key_exists($rec->roleid, $aliasnames)) {
|
|
|
|
$roledata->name = $aliasnames[$rec->roleid];
|
2008-01-18 11:03:00 +00:00
|
|
|
} else {
|
2008-06-01 13:09:04 +00:00
|
|
|
$roledata->name = $rec->rolename;
|
2008-01-18 11:03:00 +00:00
|
|
|
}
|
2008-06-01 13:09:04 +00:00
|
|
|
$roledata->users = array();
|
|
|
|
$roles[$roledata->id] = $roledata;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
|
|
|
// Record that user has role
|
|
|
|
$users[$rec->userid]->roles[] = $roles[$rec->roleid];
|
|
|
|
}
|
|
|
|
}
|
2008-06-01 13:09:04 +00:00
|
|
|
$rs->close();
|
2008-01-17 11:28:54 +00:00
|
|
|
|
|
|
|
// Return false if there weren't any users
|
2008-06-01 13:09:04 +00:00
|
|
|
if (count($users)==0) {
|
2008-01-17 11:28:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pseudo-role for multiple roles
|
2008-06-01 13:09:04 +00:00
|
|
|
$roledata = new object();
|
|
|
|
$roledata->name = get_string('multipleroles','role');
|
|
|
|
$roledata->users = array();
|
|
|
|
$roles['*'] = $roledata;
|
2008-01-17 11:28:54 +00:00
|
|
|
|
|
|
|
// Now we rearrange the data to store users by role
|
2008-06-01 13:09:04 +00:00
|
|
|
foreach ($users as $userid=>$userdata) {
|
|
|
|
$rolecount = count($userdata->roles);
|
|
|
|
if ($rolecount==0) {
|
2008-01-17 11:28:54 +00:00
|
|
|
debugging("Unexpected: user $userid is missing roles");
|
|
|
|
} else if($rolecount>1) {
|
2008-06-01 13:09:04 +00:00
|
|
|
$roleid = '*';
|
2008-01-17 11:28:54 +00:00
|
|
|
} else {
|
2008-06-01 13:09:04 +00:00
|
|
|
$roleid = $userdata->roles[0]->id;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
2008-06-01 13:09:04 +00:00
|
|
|
$roles[$roleid]->users[$userid] = $userdata;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete roles not used
|
2008-06-01 13:09:04 +00:00
|
|
|
foreach ($roles as $key=>$roledata) {
|
|
|
|
if (count($roledata->users)===0) {
|
2008-01-17 11:28:54 +00:00
|
|
|
unset($roles[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return list of roles containing their users
|
|
|
|
return $roles;
|
|
|
|
}
|