Use $this->error for only error report
[moodle-linuxchix.git] / course / groups.php
blob98f15c5876ec21697fa38afd3861bc88e9c1281e
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /// Editing interface to edit all the groups in a course
28 require_once('../config.php');
29 require_once('lib.php');
31 $courseid = required_param('id', PARAM_INT); // Course id
32 $selectedgroup = optional_param('group', NULL, PARAM_INT); // Current group id
33 $roleid = optional_param('roleid', 0, PARAM_INT); // Current role id
35 if (! $course = get_record('course', 'id', $courseid) ) {
36 error("That's an invalid course id");
39 require_login($course->id);
40 $context = get_context_instance(CONTEXT_COURSE, $course->id);
42 if (!has_capability('moodle/course:managegroups', $context)) {
43 redirect("group.php?id=$course->id"); // Not allowed to see all groups
46 /// Get the current list of groups and check the selection is valid
48 $groups = get_groups($course->id);
50 if ($selectedgroup and !isset($groups[$selectedgroup])) {
51 $selectedgroup = NULL;
55 /// Print the header of the page
57 $strgroup = get_string('group');
58 $strgroups = get_string('groups');
59 $streditgroupprofile = get_string('editgroupprofile');
60 $strgroupmembers = get_string('groupmembers');
61 $strgroupmemberssee = get_string('groupmemberssee');
62 $strparticipants = get_string('participants');
64 print_header("$course->shortname: $strgroups", $course->fullname,
65 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ".
66 "-> <a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">$strparticipants</a> ".
67 "-> $strgroups", "", "", true, '', user_login_string($course, $USER));
70 /// First, process any inputs there may be.
72 if ($data = data_submitted() and confirm_sesskey()) {
74 // Clean ALL incoming parameters which go in SQL queries here for good measure
75 $data->id = required_param('id', PARAM_INT);
76 $data->groups = optional_param('groups', 0, PARAM_INT);
77 $data->groupid = optional_param('groupid', 0, PARAM_INT);
78 $data->members = optional_param('members', array(), PARAM_INT);
80 if (!empty($data->nonmembersadd)) { /// Add people to a group
81 if (!empty($data->nonmembers) and !empty($data->groupid)) {
82 $groupmodified = false;
83 foreach ($data->nonmembers as $userid) {
84 //since we allow people to be in more than 1 group, this has to go.
85 if (!ismember($data->groupid,$userid)) {// Just to make sure (another teacher could be editing)
86 $record->groupid = $data->groupid;
87 $record->userid = $userid;
88 $record->timeadded = time();
89 if (!insert_record('groups_members', $record)) {
90 notify("Error occurred while adding user $userid to group $data->groupid");
92 $groupmodified = true;
95 if ($groupmodified) {
96 set_field('groups', 'timemodified', time(), 'id', $data->groupid);
99 $selectedgroup = $data->groupid;
102 } else if (!empty($data->nonmembersrandom)) { /// Add all non members to groups
103 notify("Random adding of people into groups is not functional yet.");
105 } else if (!empty($data->nonmembersinfo)) { /// Return info about the selected users
106 notify("You must turn Javascript on");
108 } else if (!empty($data->groupsremove)) { /// Remove a group, all members become nonmembers
109 if (!empty($data->groups)) {
110 if(!isset($groups[$data->groups])) {
111 error("This is not a valid group to remove");
113 delete_records("groups", "id", $data->groups);
114 delete_records("groups_members", "groupid", $data->groups);
115 unset($groups[$data->groups]);
119 } else if (!empty($data->groupsinfo)) { /// Display full info for a group
120 notify("You must turn Javascript on");
122 } else if (!empty($data->groupsadd)) { /// Create a new group
123 if (!empty($data->newgroupname)) {
124 $newgroup->name = $data->newgroupname;
125 $newgroup->courseid = $course->id;
126 $newgroup->lang = current_language();
127 $newgroup->timecreated = time();
128 $newgroup->description = ''; // can not be null MDL-7300
129 if (!insert_record("groups", $newgroup)) {
130 notify("Could not insert the new group '$newgroup->name'");
132 $groups = get_groups($course->id);
135 } else if (!empty($data->membersremove)) { /// Remove selected people from a particular group
137 if (!empty($data->members) and !empty($data->groupid)) {
138 foreach ($data->members as $userid) {
139 delete_records('groups_members', 'userid', $userid, "groupid", $data->groupid);
141 set_field('groups', 'timemodified', time(), 'id', $data->groupid);
143 $selectedgroup = $data->groupid;
145 } else if (!empty($data->membersinfo)) { /// Return info about the selected users
146 notify("You must turn Javascript on");
152 /// Calculate data ready to create the editing interface
154 $strmemberincourse = get_string('memberincourse');
155 $strgroupnonmembers = get_string('groupnonmembers');
156 $strgroupmembersselected = get_string('groupmembersselected');
157 $strgroupremovemembers = get_string('groupremovemembers');
158 $strgroupinfomembers = get_string('groupinfomembers');
159 $strgroupadd = get_string('groupadd');
160 $strgroupremove = get_string('groupremove');
161 $strgroupinfo = get_string('groupinfo');
162 $strgroupinfoedit = get_string('groupinfoedit');
163 $strgroupinfopeople = get_string('groupinfopeople');
164 $strgrouprandomassign = get_string('grouprandomassign');
165 $strgroupaddusers = get_string('groupaddusers');
166 $courseid = $course->id;
167 $listgroups = array();
168 $listmembers = array();
169 $nonmembers = array();
170 $groupcount = count($groups);
173 /// First, get everyone into the nonmembers array
175 if ($contextusers = get_role_users($roleid, $context)) {
176 foreach ($contextusers as $contextuser) {
177 $nonmembers[$contextuser->id] = fullname($contextuser, true);
180 unset($contextusers);
182 /// Pull out all the members into little arrays
184 if ($groups) {
185 foreach ($groups as $group) {
186 $countusers = 0;
187 $listmembers[$group->id] = array();
188 if ($groupusers = get_group_users($group->id, 'u.lastname ASC, u.firstname ASC')) {
189 foreach ($groupusers as $key=>$groupuser) {
190 if (!array_key_exists($groupuser->id, $nonmembers)) {
191 // group member with another role
192 unset($groupusers[$key]);
193 } else {
194 $listmembers[$group->id][$groupuser->id] = $nonmembers[$groupuser->id];
195 //we do not remove people from $nonmembers, everyone is displayed
196 //this is to enable people to be registered in multiple groups
197 //unset($nonmembers[$groupuser->id]);
198 $countusers++;
202 $listgroups[$group->id] = $group->name." ($countusers)";
204 natcasesort($listgroups);
207 if (empty($selectedgroup)) { // Choose the first group by default
208 if ($selectedgroup = array_shift($temparr = array_keys($listgroups))) {
209 $members = $listmembers[$selectedgroup];
211 } else {
212 $members = $listmembers[$selectedgroup];
215 $sesskey = !empty($USER->id) ? $USER->sesskey : '';
217 //DONOTCOMMIT: TODO:
218 if (debugging()) {
219 echo '<p>[ <a href="../group/groupui/?id='. $courseid .'">AJAX groups</a>
220 | <a href="../group/index.php?id='. $courseid .'">New groups</a> - debugging.]</p>';
223 /// Print out the complete form
225 print_heading(get_string('groups'));
227 include('groups-edit.html');
229 print_footer($course);