From ddff2fa806a3679c9843008894fe9eeafcce9312 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 15 Aug 2007 23:51:07 +0000 Subject: [PATCH] MDL-10383 - yet more groups and groupigns refactoring, cleanup and bugfixing - grouping edit form now works --- course/import/groups/index.php | 18 +- group/db/dbgroupinglib.php | 429 ------------------------------ group/group.php | 15 +- group/group_form.php | 24 +- group/grouping.php | 173 +++++++------ group/grouping_edit_form.php | 41 --- group/grouping_form.php | 55 ++++ group/index.php | 4 +- group/lib.php | 231 ++++++++++++++++- group/lib/basicgrouplib.php | 97 ------- group/lib/groupinglib.php | 460 +-------------------------------- group/lib/utillib.php | 167 ------------ lang/en_utf8/group.php | 1 + lib/grouplib.php | 20 +- 14 files changed, 414 insertions(+), 1321 deletions(-) delete mode 100644 group/db/dbgroupinglib.php delete mode 100644 group/grouping_edit_form.php create mode 100644 group/grouping_form.php diff --git a/course/import/groups/index.php b/course/import/groups/index.php index 341a59f425b..831d1fe4d61 100755 --- a/course/import/groups/index.php +++ b/course/import/groups/index.php @@ -3,8 +3,8 @@ /// Bulk group creation registration script from a comma separated file require_once('../../../config.php'); - require_once('../../lib.php'); - require_once($CFG->dirroot . '/group/lib/basicgrouplib.php'); + require_once($CFG->dirroot.'/course/lib.php'); + require_once($CFG->dirroot.'/group/lib.php'); $id = required_param('id', PARAM_INT); // Course id @@ -156,7 +156,8 @@ //if courseid is set if (isset($newgroup->courseid)){ - + + $newgroup->courseid = (int)$newgroup->courseid; $newgroup->timecreated = time(); $linenum++; $groupname = $newgroup->name; @@ -164,20 +165,21 @@ ///Users cannot upload groups in courses they cannot update. if (!has_capability('moodle/course:managegroups', $newgrpcoursecontext)){ - notify( get_string('nopermissionforcreation','group',$newgroup->name) ); + notify(get_string('nopermissionforcreation','group',$groupname)); + } else { - if ( $group = groups_group_name_exists($newgroup->courseid, $groupname) || !($newgroup->id = groups_create_group($newgroup->courseid, $newgroup)) ) { + if ( $groupid = groups_get_group_by_name($newgroup->courseid, $groupname) || !($newgroup->id = groups_create_group($newgroup)) ) { //Record not added - probably because group is already registered //In this case, output groupname from previous registration - if ($group) { - notify("$newgroup->name :".get_string('groupexistforcourse', 'error', $groupname)); + if ($groupid) { + notify("$groupname :".get_string('groupexistforcourse', 'error', $groupname)); } else { notify(get_string('groupnotaddederror', 'error', $groupname)); } } else { - notify( get_string('groupaddedsuccesfully', 'group', $newgroup->name) ); + notify(get_string('groupaddedsuccesfully', 'group', $groupname)); } } } //close courseid validity check diff --git a/group/db/dbgroupinglib.php b/group/db/dbgroupinglib.php deleted file mode 100644 index 10b6407ba82..00000000000 --- a/group/db/dbgroupinglib.php +++ /dev/null @@ -1,429 +0,0 @@ -libdir.'/datalib.php'); - -/******************************************************************************* - Access/List functions - ******************************************************************************/ - -/** - * Gets a list of the groupings for a specified course - * @param int $courseid The id of the course - * @return array | false An array of the ids of the groupings, or false if there - * are none or there was an error. - */ -function groups_db_get_groupings($courseid) { - if (!$courseid) { - $groupingids = false; - } else { - $groupings = get_records('groups_courses_groupings', 'courseid ', - $courseid, '', $fields='id, groupingid'); - if (!$groupings) { - $groupingids = false; - } else { - // Put the results into an array - $groupingids = array(); - foreach ($groupings as $grouping) { - array_push($groupingids, $grouping->groupingid); - } - } - } - - return $groupingids; -} - - -/** - * Gets a list of the groups in a specified grouping - * @param int $groupingid The id of the grouping - * @return array | false. An array of the ids of the groups, or false if there - * are none or an error occurred. - */ -function groups_db_get_groups_in_grouping($groupingid) { - if (!$groupingid) { - $groupid = false; - } else { - - $groups = get_records('groupings_groups', 'groupingid ', - $groupingid, '', $fields='id, groupid'); - if (!$groups) { - $groupids = false; - } else { - // Put the results into an array - $groupids = array(); - foreach ($groups as $group) { - array_push($groupids, $group->groupid); - } - } - } - - return $groupids; -} - - -/* - * Gets the groupings that a group belongs to - * @param int $groupid The id of the group - * @return array An array of the ids of the groupings that the group belongs to, - * or false if there are none or if an error occurred. - */ -function groups_db_get_groupings_for_group($groupid) { - if (!$groupid) { - $groupingids = false; - } else { - $groupings = get_records('groupings_groups', 'groupid ', - $groupid, '', $fields='id, groupingid'); - if (!$groupings) { - $groupingids = false; - } else { - // Put the results into an array - $groupingids = array(); - foreach ($groupings as $grouping) { - array_push($groupingids, $grouping->groupingid); - } - } - } - - return $groupingids; -} - - -/** - * Gets the information about a specified grouping - * @param int $groupingid - * @return object The grouping settings object - properties are name and - * description. - */ -function groups_db_get_grouping_settings($groupingid) { - if (!$groupingid) { - $groupingsettings = false; - } else { - global $CFG; - $sql = "SELECT * - FROM {$CFG->prefix}groups_groupings - WHERE id = $groupingid"; - $groupingsettings = get_record_sql($sql); - } - - return $groupingsettings; -} - -/** - * Gets the grouping to use for a particular instance of a module in a course - * @param int $coursemoduleid The id of the instance of the module in the course - * @return int The id of the grouping or false if there is no such id recorded - * or if an error occurred. - */ -function groups_db_get_grouping_for_coursemodule($cm) { - if (is_object($cm) and isset($cm->course) and isset($cm->groupingid)) { - //Do NOT rely on cm->module! - return $cm->groupingid; - } elseif (is_numeric($cm)) { - // Treat param as the course module ID. - $coursemoduleid = $cm; - $record = get_record('course_modules', 'id', $coursemoduleid, 'id, groupingid'); - if ($record and isset($record->groupingid)) { - return $record->groupingid; - } - } - return false; -} - - -/******************************************************************************* - Membership functions - ******************************************************************************/ - - - /** - * Determines if a grouping with a specified id exists - * @param int $groupingid The grouping id. - * @return True if the grouping exists, false otherwise or if an error occurred. - */ -function groups_db_grouping_exists($groupingid) { - if (!$groupingid) { - $exists = false; - } else { - $exists = record_exists('groups_groupings', 'id', - $groupingid); - } - - return $exists; -} - - - /** - * Determines if a group belongs to any grouping for the course that it belongs - * to - * @param int $groupid The id of the group - * @return boolean. True if the group belongs to a grouping, false otherwise or - * if an error has occurred. - */ - function groups_db_belongs_to_any_grouping($groupid) { - if (!$groupid) { - $isingrouping = false; - } else { - $isingrouping = record_exists('groupings_groups', 'groupid', - $groupid); - } - - return $isingrouping; - } - - - /** - * Determines if a group belongs to a specified grouping - * @param int $groupid The id of the group - * @param int $groupingid The id of the grouping - * @return boolean. True if the group belongs to a grouping, false otherwise or - * if an error has occurred. - */ - function groups_db_belongs_to_grouping($groupid, $groupingid) { - if (!$groupid or !$groupingid) { - $isingrouping = false; - } else { - $isingrouping = record_exists('groupings_groups', 'groupid', - $groupid, 'groupingid', $groupingid); - } - - return $isingrouping; - } - - - /** - * Detemines if a specified user belongs to any group of a specified grouping. - * @param int $userid The id of the user - * @param int $groupingid The id of the grouping - * @return boolean True if the user belongs to some group in the grouping, - * false otherwise or if an error occurred. - */ - function groups_db_is_member_of_some_group_in_grouping($userid, $groupingid) { - if (!$userid or !$groupingid) { - $belongstogroup = false; - } else { - global $CFG; - $sql = "SELECT gm.id - FROM {$CFG->prefix}groupings_groups gg - INNER JOIN {$CFG->prefix}groups_members gm - ON gg.groupid = gm.groupid - WHERE gm.userid = '$userid' AND gg.groupingid = '$groupingid'"; - $belongstogroup = record_exists_sql($sql); - } - return $belongstogroup; - } - - - /** - * Determines if a grouping belongs to a specified course - * @param int $groupingid The id of the grouping - * @param int $courseid The id of the course - * @return boolean True if the grouping belongs to the course, false otherwise, - * or if an error occurred. - */ - function groups_db_grouping_belongs_to_course($groupingid, $courseid) { - if (!$groupingid or !$courseid) { - $belongstocourse = false; - } else { - $belongstocourse = record_exists('groups_courses_groupings', - 'groupingid', $groupingid, 'courseid', - $courseid); - } - - return $belongstocourse; - } - - - - -/******************************************************************************* - Creation/Update functions - ******************************************************************************/ - - -/** - * Marks a set of groups as a grouping. - * - * @param array $groupidarray An array of the ids of the groups to marks as a - * grouping. - * @param int $courseid The id of the course for which the groups should form - * a grouping - * @return int | false The id of the grouping, or false if an error occurred. - * Also returns false if any of the groups specified do not belong to the - * course. - */ -function groups_db_create_grouping($courseid, $groupingsettings = false) { - if (!$courseid or !groups_get_course_info($courseid)) { - $groupingid = false; - } else { - // Replace any empty groupsettings - $groupingsettings = groups_set_default_grouping_settings($groupingsettings); - $record = $groupingsettings; - $record->timecreated = time(); - - $groupingid = insert_record('groups_groupings', $record); - if ($groupingid != false) { - $record2 = new Object(); - $record2->courseid = $courseid; - $record2->groupingid = $groupingid; - $record2->timeadded = time(); - $id= insert_record('groups_courses_groupings', $record2); - if (!$id) { - $groupingid = false; - } - } - } - - return $groupingid; -} - - -/** - * Adds a specified group to a specified grouping. - * @param int $groupid The id of the group - * @param int $groupingid The id of the grouping - * @return boolean True if the group was added successfully, false otherwise - */ -function groups_db_add_group_to_grouping($groupid, $groupingid) { - if (!$groupid or !$groupingid or !groups_db_group_exists($groupid) - or !groups_db_grouping_exists($groupingid)) { - $success = false; - } else { - $success = true; - $record = new Object(); - $record->groupingid = $groupingid; - $record->groupid = $groupid; - $record->timeadded = time(); - - $results = insert_record('groupings_groups', $record); - if (!$results) { - $success = false; - } - } - - return $groupingid; -} - - -/** - * Set information about a grouping - * @param int $groupingid The grouping to update the info for. - * @param object $groupingsettings - */ -function groups_db_set_grouping_settings($groupingid, $groupingsettings) { - $success = true; - if (!$groupingid or !$groupingsettings - or !groups_db_grouping_exists($groupingid)) { - $success = false; - } else { - // Replace any empty group settings. - $record = $groupingsettings; - $record->id = $groupingid; - $record->timemodified = time(); - $result = update_record('groups_groupings', $record); - if (!$result) { - $success = false; - } - } - - return $success; -} - - -/** - * Sets a grouping to use for a particular instance of a module in a course - * @param int $groupingid The id of the grouping - * @param int $coursemoduleid The id of the instance of the module in the course - * @return boolean True if the operation was successful, false otherwise - */ -function groups_db_set_grouping_for_coursemodule($groupingid, $coursemoduleid) { - $success = true; - if (!$groupingid or !$coursemoduleid) { - $success = false; - } else { - $record = new Object(); - $record->id = $coursemoduleid; - $record->groupingid = $groupingid; - $result = update_record('course_modules', $record); - if (!$result) { - $success = false; - } - } - return $success; -} - - -/******************************************************************************* - Deletion functions - ******************************************************************************/ - - -/** - * Removes a specified group from a specified grouping. Note that this does - * not delete the group. - * @param int $groupid The id of the group. - * @param int $groupingid The id of the grouping - * @return boolean True if the deletion was successful, false otherwise. - */ -function groups_db_remove_group_from_grouping($groupid, $groupingid) { - $success = true; - if (!$groupingid or !$groupid) { - $success = false; - } else { - $results = delete_records('groupings_groups', 'groupid', - $groupid, 'groupingid', $groupingid); - // 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; - } - } - - return $success; -} - - -/** - * Removes a grouping from a course - note that this function removes but does - * not delete any of the groups in the grouping. - * @param int $groupingid The id of the grouping - * @return boolean True if the deletion was successful, false otherwise. - */ -function groups_db_delete_grouping($groupingid) { - $success = true; - if (!$groupingid) { - $success = false; - } else { - - $results = delete_records('groups_courses_groupings', 'groupingid', - $groupingid); - if ($results == false) { - $success = false; - } - - $results = delete_records('groupings_groups', 'groupingid', - $groupingid); - if ($results == false) { - $success = false; - } - - $results = delete_records('groups_groupings', 'id', $groupingid); - if ($results == false) { - $success = false; - } - } - - return $success; - -} - -?> diff --git a/group/group.php b/group/group.php index 71a307bc510..7951d9708ed 100644 --- a/group/group.php +++ b/group/group.php @@ -42,6 +42,7 @@ if ($id) { $group->courseid = $course->id; } +require_login($course); $context = get_context_instance(CONTEXT_COURSE, $course->id); require_capability('moodle/course:managegroups', $context); @@ -71,7 +72,7 @@ if ($id and $delete) { } /// First create the form -$editform = new group_edit_form(); +$editform = new group_form(); $editform->set_data($group); if ($editform->is_cancelled()) { @@ -79,24 +80,16 @@ if ($editform->is_cancelled()) { } elseif ($data = $editform->get_data()) { - $result = false; if ($data->id) { - if (!update_record('groups', $data)) { + if (!groups_update_group($data, $editform->_upload_manager)) { error('Error updating group'); } } else { - if (!$data->id = insert_record('groups', $data)) { + if (!groups_create_group($data, $editform->_upload_manager)) { error('Error updating group'); } } - //update image - require_once("$CFG->libdir/gdlib.php"); - if (save_profile_image($data->id, $editform->_upload_manager, 'groups')) { - $data->picture = 1; - update_record('groups', $data); - } - redirect($returnurl); } diff --git a/group/group_form.php b/group/group_form.php index c8cf93e4111..3daee0691bd 100644 --- a/group/group_form.php +++ b/group/group_form.php @@ -3,18 +3,16 @@ require_once($CFG->dirroot.'/lib/formslib.php'); /// get url variables -class group_edit_form extends moodleform { +class group_form extends moodleform { // Define the form function definition () { global $USER, $CFG, $COURSE; - $strrequired = get_string('required'); - $mform =& $this->_form; $mform->addElement('text','name', get_string('groupname', 'group'),'maxlength="254" size="50"'); - $mform->addRule('name', $strrequired, 'required', null, 'client'); + $mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->setType('name', PARAM_MULTILANG); $mform->addElement('htmleditor', 'description', get_string('groupdescription', 'group'), array('rows'=> '15', 'course' => $COURSE->id, 'cols'=>'45')); @@ -41,11 +39,7 @@ class group_edit_form extends moodleform { $mform->addElement('hidden','courseid'); $mform->setType('courseid', PARAM_INT); - $this->add_action_buttons(true); - } - - function definition_after_data() { - global $USER, $CFG; + $this->add_action_buttons(); } function validation($data) { @@ -53,18 +47,16 @@ class group_edit_form extends moodleform { $errors = array(); - $name = $data['name']; + $name = stripslashes($data['name']); if ($data['id'] and $group = get_record('groups', 'id', $data['id'])) { - if ($group->name != stripslashes($name)) { + if ($group->name != $name) { if (groups_get_group_by_name($COURSE->id, $name)) { - $errors['name'] = get_string('groupnameexists', 'group', stripslashes($name)); + $errors['name'] = get_string('groupnameexists', 'group', $name); } } - } else { - if (groups_get_group_by_name($COURSE->id, $name)) { - $errors['name'] = get_string('groupnameexists', 'group', stripslashes($name)); - } + } if (groups_get_group_by_name($COURSE->id, $name)) { + $errors['name'] = get_string('groupnameexists', 'group', $name); } if (count($errors) > 0) { diff --git a/group/grouping.php b/group/grouping.php index 79c8111039a..de6d4163264 100644 --- a/group/grouping.php +++ b/group/grouping.php @@ -4,107 +4,118 @@ * * @copyright © 2006 The Open University * @author N.D.Freear AT open.ac.uk - * @author J.White AT open.ac.uk + * @author J.White AT open.ac.uk * @license http://www.gnu.org/copyleft/gpl.html GNU Public License * @package groups */ - -//TODO: reimplement -die;die;die; - require_once('../config.php'); require_once('lib.php'); -require_once($CFG->libdir.'/moodlelib.php'); -require_once('grouping_edit_form.php'); +require_once('grouping_form.php'); -$courseid = required_param('courseid', PARAM_INT); -$id = optional_param('id', false, PARAM_INT); +/// get url variables +$courseid = optional_param('courseid', PARAM_INT); +$id = optional_param('id', 0, PARAM_INT); +$delete = optional_param('delete', 0, PARAM_BOOL); +$confirm = optional_param('confirm', 0, PARAM_BOOL); -$delete = optional_param('delete', false, PARAM_BOOL); - -if (empty($CFG->enablegroupings)) { - // NO GROUPIGS YET! - error('No groupings yet'); -} - -// Get the course information so we can print the header and -// check the course id is valid -$course = groups_get_course_info($courseid); -if (! $course) { - $success = false; - print_error('invalidcourse'); //'The course ID is invalid' -} -if (GROUP_NOT_IN_GROUPING == $id) { - print_error('errornotingroupingedit', 'group', groups_home_url($courseid), get_string('notingrouping', 'group')); -} - -/// basic access control checks if ($id) { - if (!$grouping = get_record('groups_groupings', 'id', $id)) { - error('Grouping ID was incorrect'); - } - $context = get_context_instance(CONTEXT_COURSE, $course->id); - require_capability('moodle/course:managegroups', $context); -} - -/// First create the form -$editform = new grouping_edit_form('grouping.php', compact('grouping', 'courseid')); + if (!$grouping = get_record('groupings', 'id', $id)) { + error('Group ID was incorrect'); + } + if (empty($courseid)) { + $courseid = $group->courseid; -/// Override defaults if group is set -if (!empty($grouping)) { - $editform->set_data($grouping); + } else if ($courseid != $group->courseid) { + error('Course ID was incorrect'); + } + + if (!$course = get_record('course', 'id', $courseid)) { + error('Course ID was incorrect'); + } + +} else { + if (!$course = get_record('course', 'id', $courseid)) { + error('Course ID was incorrect'); + } + $grouping = new object(); + $grouping->courseid = $course->id; } -// preprocess data -if ($delete) { - if (groups_delete_grouping($id)) { - redirect(groups_home_url($course->id)); - } else { - print_error('erroreditgrouping', 'group', groups_home_url($course->id)); +require_login($course); +$context = get_context_instance(CONTEXT_COURSE, $course->id); +require_capability('moodle/course:managegroups', $context); + +$returnurl = $CFG->wwwroot.'/group/index.php?id='.$course->id; + + +if ($id and $delete) { + if (!$confirm) { + print_header(get_string('deleteselectedgrouping', 'group'), get_string('deleteselectedgroup', 'group')); + $optionsyes = array('id'=>$id, 'delete'=>1, 'courseid'=>$courseid, 'sesskey'=>sesskey(), 'confirm'=>1); + $optionsno = array('id'=>$courseid); + notice_yesno(get_string('deletegroupingconfirm', 'group', $group->name), 'grouping.php', 'index.php', $optionsyes, $optionsno, 'get', 'get'); + print_footer(); + die; + + } else if (confirm_sesskey()){ + if (groups_delete_grouping($id)) { + // MDL-9983 + $eventdata = new object(); + $eventdata->group = $id; + $eventdata->course = $courseid; + events_trigger('grouping_deleted', $eventdata); + redirect('index.php?id='.$course->id); + } else { + print_error('erroreditgrouping', 'group', $returnurl); + } } } +/// First create the form +$editform = new grouping_form(); +$editform->set_data($grouping); + if ($editform->is_cancelled()) { - redirect(groups_home_url($courseid, false, $id, false)); + redirect($returnurl); + } elseif ($data = $editform->get_data()) { $success = true; - - if (empty($grouping)) { // New grouping - if (!$id = groups_create_grouping($course->id, $data)) { - print_error('erroreditgrouping'); - } else { - $success = (bool)$id; - $data->id = $id; + + if ($data->id) { + $data->timemodified = time(); + if (!update_record('groupings', $data)) { + error('Error updating group'); } - } else { // Updating grouping - if (!groups_update_grouping($data, $course->id)) { - print_error('groupingnotupdated'); - } - } - - if ($success) { - redirect(groups_home_url($courseid, false, $id, false)); + } else { - print_error('erroreditgrouping', 'group', groups_home_url($courseid)); + $data->timecreated = time(); + $data->timemodified = $data->timecreated; + if (!$data->id = insert_record('groupings', $data)) { + error('Error updating grouping'); + } } -} else { // Prepare and output form - $strgroups = get_string('groups'); - $strparticipants = get_string('participants'); - - if ($id) { - $strheading = get_string('editgroupingsettings', 'group'); - } else { - $strheading = get_string('creategrouping', 'group'); - } - print_header("$course->shortname: ". $strheading, - $course->fullname, - "wwwroot/course/view.php?id=$courseid\">$course->shortname ". - "-> wwwroot/user/index.php?id=$courseid\">$strparticipants ". - '-> $strgroups". - "-> $strheading", '', '', true, '', user_login_string($course, $USER)); - print_heading($strheading); - $editform->display(); - print_footer($course); + redirect($returnurl); + } + +$strgroups = get_string('groups'); +$strparticipants = get_string('participants'); + +if ($id) { + $strheading = get_string('editgroupingsettings', 'group'); +} else { + $strheading = get_string('creategrouping', 'group'); +} + +print_header("$course->shortname: ". $strheading, + $course->fullname, + "wwwroot/course/view.php?id=$courseid\">$course->shortname ". + "-> wwwroot/user/index.php?id=$courseid\">$strparticipants ". + "-> $strgroups". + "-> $strheading", '', '', true, '', user_login_string($course, $USER)); +print_heading($strheading); +$editform->display(); +print_footer($course); + ?> diff --git a/group/grouping_edit_form.php b/group/grouping_edit_form.php deleted file mode 100644 index 5d23ecae978..00000000000 --- a/group/grouping_edit_form.php +++ /dev/null @@ -1,41 +0,0 @@ -dirroot.'/lib/formslib.php'); - -/// get url variables -class grouping_edit_form extends moodleform { - - // Define the form - function definition () { - global $USER, $CFG, $COURSE; - - $strrequired = get_string('required'); - $buttonstr = get_string('creategrouping', 'group'); - - if (!empty($this->_customdata['grouping'])) { - $grouping = $this->_customdata['grouping']; - $id = $grouping->id; - } - - $courseid = $this->_customdata['courseid']; - - $mform =& $this->_form; - - $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"'); - $mform->setDefault('name', get_string('defaultgroupingname', 'group')); - $mform->addRule('name', get_string('missingname'), 'required', null, 'server'); - $mform->setType('name', PARAM_MULTILANG); - - $mform->addElement('htmleditor', 'description', get_string('groupdescription', 'group'), array('rows'=> '15', 'course' => $courseid, 'cols'=>'45')); - $mform->setType('description', PARAM_RAW); - - if (!empty($id)) { - $buttonstr = get_string('save', 'group'); - $mform->addElement('hidden','id', null); - $mform->setType('id', PARAM_INT); - } - $this->add_action_buttons(true, $buttonstr); - $mform->addElement('hidden', 'courseid', $courseid); - } -} -?> diff --git a/group/grouping_form.php b/group/grouping_form.php new file mode 100644 index 00000000000..2f2dc039e14 --- /dev/null +++ b/group/grouping_form.php @@ -0,0 +1,55 @@ +dirroot.'/lib/formslib.php'); + +/// get url variables +class grouping_form extends moodleform { + + // Define the form + function definition () { + global $USER, $CFG, $COURSE; + + $mform =& $this->_form; + + $mform->addElement('text','name', get_string('groupingname', 'group'),'maxlength="254" size="50"'); + $mform->addRule('name', get_string('required'), 'required', null, 'server'); + $mform->setType('name', PARAM_MULTILANG); + + $mform->addElement('htmleditor', 'description', get_string('groupingdescription', 'group'), array('rows'=> '15', 'course' => $COURSE->id, 'cols'=>'45')); + $mform->setType('description', PARAM_RAW); + + $mform->addElement('hidden','id'); + $mform->setType('id', PARAM_INT); + + $mform->addElement('hidden', 'courseid'); + $mform->setType('courseid', PARAM_INT); + + $this->add_action_buttons(); + } + + function validation($data) { + global $COURSE; + + $errors = array(); + + $name = stripslashes($data['name']); + if ($data['id'] and $grouping = get_record('groupings', 'id', $data['id'])) { + if ($grouping->name != $name) { + if (groups_get_grouping_by_name($COURSE->id, $name)) { + $errors['name'] = get_string('groupingnameexists', 'group', $name); + } + } + + } else if (groups_get_grouping_by_name($COURSE->id, $name)) { + $errors['name'] = get_string('groupingnameexists', 'group', $name); + } + + if (count($errors) > 0) { + return $errors; + } else { + return true; + } + } + +} +?> diff --git a/group/index.php b/group/index.php index ed64bb32d26..1025061e1a5 100644 --- a/group/index.php +++ b/group/index.php @@ -28,7 +28,7 @@ $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid; // Get the course information so we can print the header and // check the course id is valid -if (!$course = groups_get_course_info($courseid)) { +if (!$course = get_record('course', 'id',$courseid)) { $success = false; print_error('invalidcourse'); //'The course ID is invalid' } @@ -231,7 +231,7 @@ function groups_param_action($prefix = 'act_') { $form_vars = $_POST; } elseif ($_GET) { - $form_vars = $_GET; + $form_vars = $_GET; } if ($form_vars) { foreach ($form_vars as $key => $value) { diff --git a/group/lib.php b/group/lib.php index 9cd645db465..eb0234472d6 100644 --- a/group/lib.php +++ b/group/lib.php @@ -1,6 +1,6 @@ dirroot.'/group/lib/basicgrouplib.php'); +/* + * INTERNAL FUNCTIONS - to be used by moodle core only + * require_once $CFG->dirroot.'/group/lib.php' must be used + */ -require_once($CFG->dirroot.'/group/lib/groupinglib.php'); +/** + * Add a new group + * @param object $data group properties (with magic quotes); + * @param object $um upload manager with group picture + * @return id of group or false if error + */ +function groups_create_group($data, $um=false) { + global $CFG; + require_once("$CFG->libdir/gdlib.php"); -require_once($CFG->dirroot.'/group/lib/utillib.php'); + $data->timecreated = time(); + $data->timemodified = $data->timecreated; + $id = insert_record('groups', $data); -require_once($CFG->dirroot.'/group/lib/modulelib.php'); + if ($id and $um) { + //update image + if (save_profile_image($id, $um, 'groups')) { + set_field('groups', 'picture', 1, 'id', $id); + } + } -require_once($CFG->dirroot.'/group/lib/legacylib.php'); + return $id; +} + +/** + * Add a new group + * @param object $data group properties (with magic quotes); + * @param object $um upload manager with group picture + * @return boolean success + */ +function groups_update_group($data, $um=false) { + global $CFG; + require_once("$CFG->libdir/gdlib.php"); + + $data->timemodified = time(); + $result = update_record('groups', $data); + + if ($result and $um) { + //update image + if (save_profile_image($data->id, $um, 'groups')) { + set_field('groups', 'picture', 1, 'id', $data->id); + } + } + + return $result; +} + +/** + * Delete a group best effort, first removing members and links with courses and groupings. + * Removes group avatar too. + * @param int $groupid The group to delete + * @return boolean True if deletion was successful, false otherwise + */ +function groups_delete_group($groupid) { + global $CFG; + require_once($CFG->libdir.'/gdlib.php'); + + if (empty($groupid)) { + return false; + } + + //first delete usage in groupings_groups + delete_records('groupings_groups', 'groupid', $groupid); + //delete members + delete_records('groups_members', 'groupid', $groupid); + //then imge + delete_profile_image($groupid, 'groups'); + //group itself last + return delete_records('groups', 'id', $groupid); +} + +function groups_delete_grouping($groupingid) { + if (empty($groupingid)) { + return false; + + } + + //first delete usage in groupings_groups + delete_records('groupings_groups', 'groupingid', $groupingid); + // remove the default groupingid from course + set_field('course', 'defaultgroupingid', 0, 'defaultgroupingid', $groupingid); + // remove the groupingid from all course modules + set_field('course_modules', 'groupingid', 0, 'groupingid', $groupingid); + //group itself last + return delete_records('groupings', 'id', $groupingid); +} + +function groups_delete_group_members($courseid, $showfeedback=false) { + global $CFG; + + $sql = "DELETE FROM {$CFG->prefix}groups_members + WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)"; + + execute_sql($sql, false); + if ($showfeedback) { + notify(get_string('deleted').' groups_members'); + } + + return true; +} + +function groups_delete_groups($courseid, $showfeedback=false) { + global $CFG; + require_once($CFG->libdir.'/gdlib.php'); + + // delete any uses of groups + $sql = "DELETE FROM {$CFG->prefix}groupings_groups + WHERE groupid in (SELECT id FROM {$CFG->prefix}groups g WHERE g.courseid = $courseid)"; + execute_sql($sql, false); + + groups_delete_group_members($courseid, false); + + // delete group pictures + if ($groups = get_records('groups', 'courseid', $courseid)) { + foreach($groups as $group) { + delete_profile_image($group->id, 'groups'); + } + } + + delete_records('groups', 'courseid', $courseid); + if ($showfeedback) { + notify(get_string('deleted').' groups'); + } + + return true; +} + +function groups_delete_groupings($courseid, $showfeedback=false) { + global $CFG; + + // delete any uses of groupings + $sql = "DELETE FROM {$CFG->prefix}groupings_groups + WHERE groupingid in (SELECT id FROM {$CFG->prefix}groupings g WHERE g.courseid = $courseid)"; + execute_sql($sql, false); + + // remove the default groupingid from course + set_field('course', 'defaultgroupingid', 0, 'id', $courseid); + // remove the groupingid from all course modules + set_field('course_modules', 'groupingid', 0, 'courseid', $courseid); + + delete_records('groupings', 'courseid', $courseid); + if ($showfeedback) { + notify(get_string('deleted').' groupings'); + } + + return true; +} + +/* =================================== */ +/* various functions used by groups UI */ +/* =================================== */ + +/** + * Gets the users for a course who are not in a specified group + * @param int $groupid The id of the group + * @param string searchtext similar to searchtext in role assign, search + * @return array An array of the userids of the non-group members, or false if + * an error occurred. + * This function was changed to get_users_by_capability style + * mostly because of the searchtext requirement + */ +function groups_get_users_not_in_group($courseid, $groupid, $searchtext='') { + + global $CFG; + + $context = get_context_instance(CONTEXT_COURSE, $courseid); + + if ($searchtext !== '') { // Search for a subset of remaining users + $LIKE = sql_ilike(); + $FULLNAME = sql_fullname(); + $wheresearch = " AND u.id IN (SELECT id FROM {$CFG->prefix}user WHERE $FULLNAME $LIKE '%$searchtext%' OR email $LIKE '%$searchtext%' )"; + } else { + $wheresearch = ''; + } + + $capability = 'moodle/course:view'; + $doanything = false; + + // 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); + } + + $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; + } + $roleids = '('.implode(',', $validroleids).')'; + } else { + return false; // No need to continue, since no roles have this capability set + } + +/// Construct the main SQL + $select = " SELECT u.id, u.firstname, u.lastname"; + $from = " FROM {$CFG->prefix}user u + INNER JOIN {$CFG->prefix}role_assignments ra ON ra.userid = u.id + INNER JOIN {$CFG->prefix}role r ON r.id = ra.roleid"; + $where = " WHERE ra.contextid ".get_related_contexts_string($context)." + AND u.deleted = 0 + AND ra.roleid in $roleids + AND u.id NOT IN (SELECT userid + FROM {$CFG->prefix}groups_members + WHERE groupid = $groupid) + $wheresearch"; + + return get_records_sql($select.$from.$where);; +} ?> \ No newline at end of file diff --git a/group/lib/basicgrouplib.php b/group/lib/basicgrouplib.php index f18a42471a6..9369b5b63d6 100644 --- a/group/lib/basicgrouplib.php +++ b/group/lib/basicgrouplib.php @@ -200,43 +200,6 @@ function groups_set_default_group_settings($groupinfo = null) { Creation functions *****************************/ -/** - * Creates a group for a specified course - * All groups should really belong to a grouping (though there is nothing in - * this API that stops them not doing - * so, to allow plenty of flexibility) so you should be using this in - * conjunction with the function to add a group to - * a grouping. - * @param int $courseid The course to create the group for - * @return int | false The id of the group created or false if the group was - * not created successfully. - * See comment above on web service autoupdating. - */ -function groups_create_group($courseid, $groupsettings = false) { - return groups_db_create_group($courseid, $groupsettings); -} - -/** - * Restore a group for a specified course. - * For backup/restorelib.php - */ -function groups_restore_group($courseid, $groupsettings) { - return groups_db_create_group($courseid, $groupsettings, $copytime=true); -} - - -/** - * Sets the information about a group - * Only sets the string for the picture - does not upload the picture! - * @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_set_group_settings($groupid, $groupsettings) { - return groups_db_set_group_settings($groupid, $groupsettings); -} - - /** * Adds a specified user to a group * @param int $userid The user id @@ -268,23 +231,6 @@ function groups_add_member($groupid, $userid) { return $useradded; } -/** - * Restore a user to the group specified in $member. - * For backup/restorelib.php - * @param $member object Group member object. - */ -function groups_restore_member($member) { - $alreadymember = groups_is_member($member->groupid, $member->userid); - if (! groups_group_exists($member->groupid)) { - return false; - } elseif ($alreadymember) { - return true; - } else { - $useradded = groups_db_add_member($member->groupid, $member->userid, $member->timeadded); - } - return $useradded; -} - /***************************** Deletion functions @@ -306,47 +252,4 @@ function groups_remove_member($groupid, $userid) { return $success; } -/** - * Removes all users from the specified group. - * @param int $groupid The ID of the group. - * @return boolean True for success, false otherwise. - */ -function groups_remove_all_members($groupid) { - if (! groups_group_exists($groupid)) { - //Woops, delete group last! - return false; - } - $userids = groups_get_members($groupid); - if (! $userids) { - return false; - } - $success = true; - foreach ($userids as $id) { - $success = $success && groups_db_remove_member($groupid, $id); - } - $success = $success && groups_db_set_group_modified($groupid); - return $success; -} - -/* - * Update a group and return true or false - * - * @param object $data - all the data needed for an entry in the 'groups' table - */ -function groups_update_group($data, $courseid) { - $oldgroup = get_record('groups', 'id', $data->id); // should not fail, already tested above - - // Update with the new data - if (update_record('groups', $data)) { - - $group = get_record('groups', 'id', $data->id); - - add_to_log($group->id, "groups", "update", "edit.php?id=$courseid&group=$group->id", ""); - - return true; - - } - - return false; -} ?> diff --git a/group/lib/groupinglib.php b/group/lib/groupinglib.php index ddf450c4c64..6a6b57d30d0 100644 --- a/group/lib/groupinglib.php +++ b/group/lib/groupinglib.php @@ -15,70 +15,6 @@ require_once($CFG->dirroot.'/group/db/dbgroupinglib.php'); define('GROUP_NOT_IN_GROUPING', -1); define('GROUP_ANY_GROUPING', 0); -/***************************** - Access/List functions - *****************************/ - - -/** - * Gets a list of the groupings for a specified course - * @param int $courseid The id of the course - * @return array | false An array of the ids of the groupings, or false if there - * are none or there was an error. - */ -function groups_get_groupings($courseid) { - return groups_db_get_groupings($courseid); -} - - -function groups_get_grouping_records($courseid) { - global $CFG; - if (! $courseid) { - return false; - } - $sql = "SELECT gg.* - FROM {$CFG->prefix}groups_groupings gg - INNER JOIN {$CFG->prefix}groups_courses_groupings cg ON gg.id = cg.groupingid - WHERE cg.courseid = '$courseid'"; - $groupings = get_records_sql($sql); - return $groupings; -} - -/** - * Gets a list of the group IDs in a specified grouping - * @param int $groupingid The id of the grouping - * @return array | false. An array of the ids of the groups, or false if there - * are none or an error occurred. - */ -function groups_get_groups_in_grouping($groupingid) { - return groups_db_get_groups_in_grouping($groupingid); -} - -/** - * Gets data linking a grouping to each group it contains. - * @param int $groupingid The ID of the grouping. - * @return array | false An array of grouping-group records, or false on error. - */ -function groups_get_groups_in_grouping_records($groupingid) { - if (! $groupingid) { - return false; - } - $grouping_groups = get_records('groupings_groups', 'groupingid ', - $groupingid, '', $fields='id, groupid, timeadded'); - - return $grouping_groups; -} - - -/** - * Gets the groupings that a group belongs to - * @param int $groupid The id of the group - * @return array An array of the ids of the groupings that the group belongs to, - * or false if there are none or if an error occurred. - */ -function groups_get_groupings_for_group($groupid) { - return groups_db_get_groupings_for_group($groupid); -} /** * Gets the information about a specified grouping @@ -87,72 +23,7 @@ function groups_get_groupings_for_group($groupid) { * description. */ function groups_get_grouping_settings($groupingid) { - return groups_db_get_grouping_settings($groupingid); -} - -/** - * Set information about a grouping - * @param int $groupingid The grouping to update the info for. - * @param object $groupingsettings - */ -function groups_set_grouping_settings($groupingid, $groupingsettings) { - return groups_db_set_grouping_settings($groupingid, $groupingsettings); -} - - -/** - * Gets the name of a grouping with a specified ID - * @param int $groupid The grouping ID. - * @return string The name of the grouping. - */ -function groups_get_grouping_name($groupingid) { - if (GROUP_NOT_IN_GROUPING == $groupingid) { - return get_string('notingrouping', 'group'); - } - elseif (GROUP_ANY_GROUPING == $groupingid) { - return get_string('anygrouping', 'group'); - } - $settings = groups_get_grouping_settings($groupingid); - if ($settings && isset($settings->name)) { - return $settings->name; - } - return false; -} - - -/** - * Get array of group IDs for the user in a grouping. - * @param int $userid - * @param int $groupingid - * @return array If the user has groups an array of group IDs, else false. - */ -function groups_get_groups_for_user_in_grouping($userid, $groupingid) { - global $CFG; - $sql = "SELECT gg.groupid - FROM {$CFG->prefix}groupings_groups gg - INNER JOIN {$CFG->prefix}groups_members gm ON gm.groupid = gg.groupid - WHERE gm.userid = '$userid' - AND gg.groupingid = '$groupingid'"; - $records = get_records_sql($sql); - -//print_object($records); - return groups_groups_to_groupids($records); -} - -/** - * Gets a list of the groups not in a specified grouping - * @param int $groupingid The grouping specified - * @return array An array of the group ids - */ -function groups_get_groups_not_in_grouping($groupingid, $courseid) { - $allgroupids = groups_get_groups($courseid); - $groupids = array(); - foreach($allgroupids as $groupid) { - if (!groups_belongs_to_grouping($groupid, $groupingid)) { - array_push($groupids, $groupid); - } - } - return $groupids; + error('missing'); } /** @@ -182,334 +53,5 @@ function groups_get_groups_not_in_any_grouping($courseid) { return $groupids; } -/** - * Gets the users for the course who are not in any group of a grouping. - * @param int $courseid The id of the course - * @param int $groupingid The id of the grouping - * @param int $groupid Excludes members of a particular group - * @return array An array of the userids of the users not in any group of - * the grouping or false if an error occurred. - */ -function groups_get_users_not_in_any_group_in_grouping($courseid, $groupingid, - $groupid = false) { - $users = get_course_users($courseid); - $userids = groups_users_to_userids($users); - $nongroupmembers = array(); - if (! $userids) { - return $nongroupmembers; - } - foreach($userids as $userid) { - if (!groups_is_member_of_some_group_in_grouping($userid, $groupingid)) { - // If a group has been specified don't include members of that group - if ($groupid and !groups_is_member($userid, $groupid)) { - array_push($nongroupmembers, $userid); - } else { - ///array_push($nongroupmembers, $userid); - } - } - } - return $nongroupmembers; - } - - -/** - * Determines if a user is in more than one group in a grouping - * @param int $userid The id of the user - * @param int $groupingid The id of the grouping - * @return boolean True if the user is in more than one group, false otherwise - * or if an error occurred. - */ -function groups_user_is_in_multiple_groups($userid, $groupingid) { - $inmultiplegroups = false; - //TODO: $courseid? - $groupids = groups_get_groups_for_user($courseid); - if ($groupids != false) { - $groupinggroupids = array(); - foreach($groupids as $groupid) { - if (groups_belongs_to_grouping($groupid, $groupingid)) { - array_push($groupinggroupids, $groupid); - } - } - if (count($groupinggroupids) > 1) { - $inmultiplegroups = true; - } - } - return $inmultiplegroups; -} - - -/** - * Returns an object with the default grouping settings values - these can of - * course be overridden if desired. - * Can also be used to set the default for any values not set - * @return object The grouping settings object. - */ -function groups_set_default_grouping_settings($groupingsettings = null) { - - if (!isset($groupingsettings->name)) { - $groupingsettings->name = 'Temporary Grouping Name'; - } - - if (!isset($groupingsettings->description)) { - $groupingsettings->description = ''; - } - - if (!isset($groupingsettings->viewowngroup)) { - $groupingsettings->viewowngroup = 1; - } - - if (!isset($groupingsettings->viewallgroupsmembers)) { - $groupingsettings->viewallgroupsmembers = 0; - } - - if (!isset($groupingsettings->viewallgroupsactivities)) { - $groupingsettings->viewallgroupsactivities = 0; - } - - if (!isset($groupingsettings->teachersgroupmark)) { - $groupingsettings->teachersgroupmark = 0; - } - - if (!isset($groupingsettings->teachersgroupview)) { - $groupingsettings->teachersgroupview = 0; - } - - if (!isset($groupingsettings->teachersoverride)) { - $groupingsettings->teachersoverride = 1; - } - - return $groupingsettings; -} - - -/** - * Gets the grouping ID to use for a particular instance of a module in a course - * @param int $coursemoduleid The id of the instance of the module in the course - * @return int The id of the grouping or false if there is no such id recorded - * or if an error occurred. - */ -function groups_get_grouping_for_coursemodule($coursemodule) { - return groups_db_get_grouping_for_coursemodule($coursemodule); -} - -/***************************** - Membership functions - *****************************/ - - -/** - * Determines if a grouping with a specified id exists - * @param int $groupingid The grouping id. - * @return True if the grouping exists, false otherwise or if an error occurred. - */ -function groups_grouping_exists($groupingid) { - return groups_db_grouping_exists($groupingid); -} - -/** - * Determine if a course ID, grouping name and description match a grouping in the database. - * For backup/restorelib.php - * @return mixed A grouping-like object with $grouping->id, or false. - */ -function groups_grouping_matches($courseid, $gg_name, $gg_description) { - global $CFG; - $sql = "SELECT gg.id, gg.name, gg.description - FROM {$CFG->prefix}groups_groupings gg - INNER JOIN {$CFG->prefix}groups_courses_groupings cg ON gg.id = cg.groupingid - WHERE gg.name = '$gg_name' - AND gg.description = '$gg_description' - AND cg.courseid = '$courseid'"; - $records = get_records_sql($sql); - $grouping = false; - if ($records) { - $grouping = array_shift($records); - } - return $grouping; -} - -/** - * Determines if a group belongs to a specified grouping - * @param int $groupid The id of the group - * @param int $groupingid The id of the grouping - * @return boolean. True if the group belongs to a grouping, false otherwise or - * if an error has occurred. - */ -function groups_belongs_to_grouping($groupid, $groupingid) { - return groups_db_belongs_to_grouping($groupid, $groupingid); -} - - -/** - * Detemines if a specified user belongs to any group of a specified grouping. - * @param int $userid The id of the user - * @param int $groupingid The id of the grouping - * @return boolean True if the user belongs to some group in the grouping, - * false otherwise or if an error occurred. - */ -function groups_is_member_of_some_group_in_grouping($userid, $groupingid) { - return groups_db_is_member_of_some_group_in_grouping($userid, $groupingid); -} - -/** - * Determines if a grouping belongs to a specified course - * @param int $groupingid The id of the grouping - * @param int $courseid The id of the course - * @return boolean True if the grouping belongs to the course, false otherwise, - * or if an error occurred. - */ -function groups_grouping_belongs_to_course($groupingid, $courseid) { - return groups_db_grouping_belongs_to_course($groupingid, $courseid); -} - - -/***************************** - Creation functions -*****************************/ - - -/** - * Marks a set of groups as a grouping. This is a best effort operation. - * It can also be used to create an 'empty' grouping to which - * groups can be added by passing an empty array for the group ids. - * @param array $groupids An array of the ids of the groups to marks as a - * grouping. - * @param int $courseid The id of the course for which the groups should form - * a grouping - * @return int | false The id of the grouping, or false if an error occurred. - * Also returns false if any of the groups specified do not belong to the - * course. - */ -function groups_create_grouping($courseid, $groupingsettings = false) { - $groupingid = groups_db_create_grouping($courseid, $groupingsettings); - - return $groupingid; -} - - -/** - * Adds a specified group to a specified grouping. - * @param int $groupid The id of the group - * @param int $groupingid The id of the grouping - * @return boolean True if the group was added successfully or the group already - * belonged to the grouping, false otherwise. Also returns false if the group - * doesn't belong to the same course as the grouping. - */ -function groups_add_group_to_grouping($groupid, $groupingid) { - if (GROUP_NOT_IN_GROUPING == $groupingid) { - return true; - } - $belongstogrouping = groups_belongs_to_grouping($groupid, $groupingid); - - if (!groups_grouping_exists($groupingid)) { - $groupadded = false; - } elseif (!$belongstogrouping) { - $groupadded = groups_db_add_group_to_grouping($groupid, $groupingid); - } else { - $groupadded = true; - } - - return $groupadded; -} - - -/** - * Sets the name of a grouping overwriting any current name that the grouping - * has - * @param int $groupingid The id of the grouping specified - * @param string $name The name to give the grouping - * @return boolean True if the grouping settings was added successfully, false - * otherwise. - */ -function groups_set_grouping_name($groupingid, $name) { - return groups_db_set_grouping_name($groupingid, $name); -} - - -/** - * Sets a grouping to use for a particular instance of a module in a course - * @param int $groupingid The id of the grouping - * @param int $coursemoduleid The id of the instance of the module in the course - * @return boolean True if the operation was successful, false otherwise - */ -function groups_set_grouping_for_coursemodule($groupingid, $coursemoduleid) { - return groups_db_set_grouping_for_coursemodule($groupingid, - $coursemoduleid); -} - - -/***************************** - Update functions - *****************************/ - -function groups_update_grouping($data, $courseid) { - $oldgrouping = get_record('groups_groupings', 'id', $data->id); // should not fail, already tested above - - // Update with the new data - if (update_record('groups_groupings', $data)) { - - $grouping = get_record('groups_groupings', 'id', $data->id); - - add_to_log($grouping->id, "groups_groupings", "update", "grouping.php?courseid=$courseid&id=$grouping->id", ""); - - return true; - - } - - return false; - -} -/***************************** - Deletion functions - *****************************/ - -/** - * Removes a specified group from a grouping. Note that this does - * not delete the group. - * @param int $groupid The id of the group. - * @param int $groupingid The id of the grouping - * @return boolean True if the deletion was successful, false otherwise. - */ -function groups_remove_group_from_grouping($groupid, $groupingid) { - if (GROUP_NOT_IN_GROUPING == $groupingid) { - //Quietly ignore. - return true; - } - return groups_db_remove_group_from_grouping($groupid, $groupingid); -} - -/** - * Removes a grouping from a course - note that this function does not delete - * any of the groups in the grouping. - * @param int $groupingid The id of the grouping - * @return boolean True if the deletion was successful, false otherwise. - */ -function groups_delete_grouping($groupingid) { - if (GROUP_NOT_IN_GROUPING == $groupingid) { - return false; - } - return groups_db_delete_grouping($groupingid); - -} - -/** - * Delete all groupings from a course. Groups MUST be deleted first. - * TODO: If groups or groupings are to be shared between courses, think again! - * @param $courseid The course ID. - * @return boolean True if all deletes were successful, false otherwise. - */ -function groups_delete_all_groupings($courseid) { - if (! $courseid) { - return false; - } - $groupingids = groups_get_groupings($courseid); - if (! $groupingids) { - return true; - } - $success = true; - foreach ($groupingids as $gg_id) { - $success = $success && groups_db_delete_grouping($gg_id); - } - return $success; -} ?> diff --git a/group/lib/utillib.php b/group/lib/utillib.php index cf3a8e54e61..ee4826ae8ba 100644 --- a/group/lib/utillib.php +++ b/group/lib/utillib.php @@ -94,40 +94,6 @@ function groups_get_group_displayname($groupid) { return false; } - -/** - * Returns the display name of a grouping - the grouping name followed - * by the number of groups in the grouping in brackets. - * @param int $groupingid The grouping ID. - * @param int $courseid The related course. - * @return string The display name of the grouping - */ -function groups_get_grouping_displayname($groupingid, $courseid) { - if ($groupingname = groups_get_grouping_name($groupingid)) { - $count = groups_count_groups_in_grouping($groupingid, $courseid); - return "$groupingname ($count)"; - } - return false; -} - - -/** - * Takes an array of users (i.e of objects) and converts it in the corresponding - * array of user IDs. - * @param $users array The array of users - * @return array The array of user IDs, or false if an error occurred - */ -function groups_users_to_userids($users) { - if (! $users) { - return false; - } - $userids = array(); - foreach($users as $user) { - array_push($userids, $user->id); - } - return $userids; -} - /** * Get an sorted array of user-id/display-name objects. */ @@ -275,21 +241,6 @@ function groups_get_user($userid) { } -/** - * Gets the course information object for a given course id - * @param $courseid int The course id - * @return object The course info object, or false if an error occurred. - * TODO: need to put the database bit into a db file - */ -function groups_get_course_info($courseid){ - if (!$courseid) { - $courseinfo = false; - } else { - $courseinfo = get_record('course', 'id', $courseid); - } - return $courseinfo; -} - /** * Gets the course ID for a given group. */ @@ -301,123 +252,5 @@ function groups_get_course($groupid) { return false; } -/** - * Return the address for the group settings page. - * (For /user/index.php etc.) - * @param $courseid - * @param $groupid - * @param $groupingid Default false, or optionally a grouping ID. - * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects. - * @param $param Extra parameters. - * @return string An absolute URL. - */ -function groups_group_edit_url($courseid, $groupid, $groupingid=false, $html=true, $param=false) { - global $CFG; - $html ? $sep = '&' : $sep = '&'; - $url = $CFG->wwwroot.'/group/edit.php?courseid='.$courseid; - if ($groupid) { - $url .= $sep.'id='.$groupid; - } - if ($groupingid) { - $url .= $sep.'grouping='.$groupingid; - } - if ($param) { - $url .= $sep.$param; - } - return $url; -} - -/** - * Return the address for the grouping settings page - Internal group use only. - * @param $courseid - * @param $groupingid Default false, or optionally a grouping ID. - * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects. - * @param $param Extra parameters. - * @return string An absolute URL. - */ -function groups_grouping_edit_url($courseid, $groupingid=false, $html=true, $param=false) { - global $CFG; - $html ? $sep = '&' : $sep = '&'; - $url = $CFG->wwwroot.'/group/grouping.php?courseid='.$courseid; - if ($groupingid) { - $url .= $sep.'id='.$groupingid; - } - if ($param) { - $url .= $sep.$param; - } - return $url; -} - -/** - * Return the address for the add/remove users page - Internal group use only. - * @param $courseid - * @param $groupid - * @param $groupingid Default false, or optionally a grouping ID. - * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects. - * @return string An absolute URL. - */ -function groups_members_add_url($courseid, $groupid, $groupingid=false, $html=true) { - global $CFG; - $html ? $sep = '&' : $sep = '&'; - $url = $CFG->wwwroot.'/group/assign.php?courseid='.$courseid.$sep.'group='.$groupid; - if ($groupingid) { - $url .= $sep.'grouping='.$groupingid; - } - return $url; -} - -/** - * Return the address for the main group management page. (For admin block etc.) - * @param $courseid - * @param $groupid Default false, or optionally a group ID. - * @param $groupingid Default false, or optionally a grouping ID. - * @param $html Default true for HTML pages, eg. on error. False for HTTP redirects. - * @return string An absolute URL. - */ -function groups_home_url($courseid, $groupid=false, $groupingid=false, $html=true) { - global $CFG; - $html ? $sep = '&' : $sep = '&'; - $url = $CFG->wwwroot.'/group/index.php?id='.$courseid; - if ($groupid) { - $url .= $sep.'group='.$groupid; - } - if ($groupingid) { - $url .= $sep.'grouping='.$groupingid; - } - return $url; -} - -/** - * Returns the first button action with the given prefix, taken from - * POST or GET, otherwise returns false. - * See /lib/moodlelib.php function optional_param. - * @param $prefix 'act_' as in 'action'. - * @return string The action without the prefix, or false if no action found. - */ -function groups_param_action($prefix = 'act_') { - $action = false; -//($_SERVER['QUERY_STRING'] && preg_match("/$prefix(.+?)=(.+)/", $_SERVER['QUERY_STRING'], $matches)) { //b_(.*?)[&;]{0,1}/ - - if ($_POST) { - $form_vars = $_POST; - } - elseif ($_GET) { - $form_vars = $_GET; - } - if ($form_vars) { - foreach ($form_vars as $key => $value) { - if (preg_match("/$prefix(.+)/", $key, $matches)) { - $action = $matches[1]; - break; - } - } - } - if ($action && !preg_match('/^\w+$/', $action)) { - $action = false; - error('Action had wrong type.'); - } - ///if (debugging()) echo 'Debug: '.$action; - return $action; -} ?> diff --git a/lang/en_utf8/group.php b/lang/en_utf8/group.php index 0ada7d7561f..76bb0957dff 100644 --- a/lang/en_utf8/group.php +++ b/lang/en_utf8/group.php @@ -71,6 +71,7 @@ $string['createorphangroup'] = 'Create orphan group'; $string['groupname'] = 'Group name'; $string['groupnameexists'] = 'The group name \'$a\' already exists in this course, please choose another one.'; +$string['groupingnameexists'] = 'The grouping name \'$a\' already exists in this course, please choose another one.'; $string['defaultgroupname'] = 'Group'; $string['groupdescription'] = 'Group description'; $string['enrolmentkey'] = 'Enrolment key'; diff --git a/lib/grouplib.php b/lib/grouplib.php index 8d53694e59d..2657b3887e2 100644 --- a/lib/grouplib.php +++ b/lib/grouplib.php @@ -2,7 +2,6 @@ // folowing files will be removed soon require_once($CFG->dirroot.'/group/lib/basicgrouplib.php'); -require_once($CFG->dirroot.'/group/lib/groupinglib.php'); require_once($CFG->dirroot.'/group/lib/utillib.php'); require_once($CFG->dirroot.'/group/lib/legacylib.php'); @@ -14,11 +13,24 @@ require_once($CFG->dirroot.'/group/lib/legacylib.php'); * @return int $groupid */ function groups_get_group_by_name($courseid, $name) { - if (!$group = get_record('groups', 'courseid', $courseid, 'name', addslashes($name))) { - return false; + if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) { + return key($groups); } + return false; +} - return $group->id; +/** + * Returns the groupingid of a grouping with the name specified for the course. + * Grouping names should be unique in course + * @param int $courseid The id of the course + * @param string $name name of group (without magic quotes) + * @return int $groupid + */ +function groups_get_grouping_by_name($courseid, $name) { + if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) { + return key($groupings); + } + return false; } /**