2007-01-03 08:17:56 +00:00
|
|
|
<?php
|
2012-01-05 09:29:55 +08:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2007-01-03 08:17:56 +00:00
|
|
|
/**
|
|
|
|
* The main group management user interface.
|
|
|
|
*
|
2012-01-05 09:29:55 +08:00
|
|
|
* @copyright 2006 The Open University, N.D.Freear AT open.ac.uk, J.White AT open.ac.uk
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @package core_group
|
2007-01-03 08:17:56 +00:00
|
|
|
*/
|
|
|
|
require_once('../config.php');
|
|
|
|
require_once('lib.php');
|
2008-10-16 11:18:02 +00:00
|
|
|
|
2007-08-14 00:50:00 +00:00
|
|
|
$courseid = required_param('id', PARAM_INT);
|
|
|
|
$groupid = optional_param('group', false, PARAM_INT);
|
|
|
|
$userid = optional_param('user', false, PARAM_INT);
|
2022-12-15 10:56:05 +10:00
|
|
|
$action = optional_param('action', false, PARAM_TEXT);
|
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
// Support either single group= parameter, or array groups[].
|
2008-10-24 13:31:49 +00:00
|
|
|
if ($groupid) {
|
2010-03-31 07:41:31 +00:00
|
|
|
$groupids = array($groupid);
|
2008-10-24 13:31:49 +00:00
|
|
|
} else {
|
2011-08-17 16:27:15 +02:00
|
|
|
$groupids = optional_param_array('groups', array(), PARAM_INT);
|
2008-10-24 13:31:49 +00:00
|
|
|
}
|
2010-03-31 07:41:31 +00:00
|
|
|
$singlegroup = (count($groupids) == 1);
|
2007-01-24 03:03:10 +00:00
|
|
|
|
2007-08-14 00:50:00 +00:00
|
|
|
$returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
|
2007-01-24 03:03:10 +00:00
|
|
|
|
2007-01-03 08:17:56 +00:00
|
|
|
// Get the course information so we can print the header and
|
2022-12-15 10:54:53 +10:00
|
|
|
// check the course id is valid.
|
|
|
|
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
$url = new moodle_url('/group/index.php', array('id' => $courseid));
|
2016-11-03 11:35:25 +00:00
|
|
|
navigation_node::override_active_url($url);
|
2009-09-16 08:16:44 +00:00
|
|
|
if ($userid) {
|
|
|
|
$url->param('user', $userid);
|
|
|
|
}
|
|
|
|
if ($groupid) {
|
|
|
|
$url->param('group', $groupid);
|
|
|
|
}
|
|
|
|
$PAGE->set_url($url);
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
// Make sure that the user has permissions to manage groups.
|
|
|
|
require_login($course);
|
2007-01-03 08:17:56 +00:00
|
|
|
|
2012-08-02 11:20:48 +08:00
|
|
|
$context = context_course::instance($course->id);
|
2013-06-27 13:45:16 +10:00
|
|
|
require_capability('moodle/course:managegroups', $context);
|
|
|
|
|
2018-02-23 16:18:24 +07:00
|
|
|
$PAGE->requires->js('/group/clientlib.js', true);
|
|
|
|
$PAGE->requires->js('/group/module.js', true);
|
2007-01-03 08:17:56 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
// Check for multiple/no group errors.
|
2010-03-31 07:41:31 +00:00
|
|
|
if (!$singlegroup) {
|
2008-10-24 13:31:49 +00:00
|
|
|
switch($action) {
|
|
|
|
case 'ajax_getmembersingroup':
|
|
|
|
case 'showgroupsettingsform':
|
|
|
|
case 'showaddmembersform':
|
|
|
|
case 'updatemembers':
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('errorselectone', 'group', $returnurl);
|
2008-10-24 13:31:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
switch ($action) {
|
2022-12-15 10:54:53 +10:00
|
|
|
case false: // OK, display form.
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ajax_getmembersingroup':
|
2008-01-17 11:28:54 +00:00
|
|
|
$roles = array();
|
2019-11-29 07:45:55 +00:00
|
|
|
|
2021-03-29 09:38:50 +01:00
|
|
|
$userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
|
|
|
|
[
|
|
|
|
'selects' => $userfieldsselects,
|
|
|
|
'joins' => $userfieldsjoin,
|
|
|
|
'params' => $userfieldsparams
|
|
|
|
] = (array)$userfieldsapi->get_sql('u', true, '', '', false);
|
2021-03-15 15:36:32 +00:00
|
|
|
$extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
|
2019-11-29 07:45:55 +00:00
|
|
|
if ($groupmemberroles = groups_get_members_by_role($groupids[0], $courseid,
|
2021-03-29 09:38:50 +01:00
|
|
|
'u.id, ' . $userfieldsselects, null, '', $userfieldsparams, $userfieldsjoin)) {
|
2019-11-29 07:45:55 +00:00
|
|
|
|
2020-01-07 16:18:56 +00:00
|
|
|
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
|
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
foreach ($groupmemberroles as $roleid => $roledata) {
|
2010-03-31 07:41:31 +00:00
|
|
|
$shortroledata = new stdClass();
|
2023-03-21 12:01:55 +00:00
|
|
|
$shortroledata->name = html_entity_decode($roledata->name, ENT_QUOTES, 'UTF-8');
|
2010-03-31 07:41:31 +00:00
|
|
|
$shortroledata->users = array();
|
2022-12-15 10:54:53 +10:00
|
|
|
foreach ($roledata->users as $member) {
|
2010-03-31 07:41:31 +00:00
|
|
|
$shortmember = new stdClass();
|
|
|
|
$shortmember->id = $member->id;
|
2020-01-07 16:18:56 +00:00
|
|
|
$shortmember->name = fullname($member, $viewfullnames);
|
2019-11-29 07:45:55 +00:00
|
|
|
if ($extrafields) {
|
|
|
|
$extrafieldsdisplay = [];
|
|
|
|
foreach ($extrafields as $field) {
|
2021-06-03 08:50:07 +01:00
|
|
|
// No escaping here, handled client side in response to AJAX request.
|
|
|
|
$extrafieldsdisplay[] = $member->{$field};
|
2019-11-29 07:45:55 +00:00
|
|
|
}
|
|
|
|
$shortmember->name .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
|
|
|
|
}
|
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
$shortroledata->users[] = $shortmember;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
2010-03-31 07:41:31 +00:00
|
|
|
$roles[] = $shortroledata;
|
2007-01-19 04:57:46 +00:00
|
|
|
}
|
2007-08-16 21:14:03 +00:00
|
|
|
}
|
2008-10-03 07:13:16 +00:00
|
|
|
echo json_encode($roles);
|
2007-08-16 21:14:03 +00:00
|
|
|
die; // Client side JavaScript takes it from here.
|
2007-01-19 04:57:46 +00:00
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
case 'deletegroup':
|
2010-03-31 07:41:31 +00:00
|
|
|
if (count($groupids) == 0) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('errorselectsome', 'group', $returnurl);
|
2008-10-24 13:31:49 +00:00
|
|
|
}
|
2010-03-31 07:41:31 +00:00
|
|
|
$groupidlist = implode(',', $groupids);
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/delete.php', array('courseid' => $courseid, 'groups' => $groupidlist)));
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
case 'showcreateorphangroupform':
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/group.php', array('courseid' => $courseid)));
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2007-09-24 21:55:15 +00:00
|
|
|
case 'showautocreategroupsform':
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/autogroup.php', array('courseid' => $courseid)));
|
2007-11-19 20:31:57 +00:00
|
|
|
break;
|
2007-09-24 21:55:15 +00:00
|
|
|
|
2010-10-12 08:34:52 +00:00
|
|
|
case 'showimportgroups':
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/import.php', array('id' => $courseid)));
|
2010-10-12 08:34:52 +00:00
|
|
|
break;
|
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
case 'showgroupsettingsform':
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/group.php', array('courseid' => $courseid, 'id' => $groupids[0])));
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
case 'updategroups': // Currently reloading.
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-01-19 04:57:46 +00:00
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
case 'removemembers':
|
|
|
|
break;
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
case 'showaddmembersform':
|
2022-12-15 10:54:53 +10:00
|
|
|
redirect(new moodle_url('/group/members.php', array('group' => $groupids[0])));
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
case 'updatemembers': // Currently reloading.
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
2007-02-19 16:38:18 +00:00
|
|
|
|
2022-12-15 10:59:05 +10:00
|
|
|
case 'enablemessaging':
|
|
|
|
set_groups_messaging($groupids, true);
|
|
|
|
redirect($returnurl, get_string('messagingenabled', 'group', count($groupids)), null,
|
|
|
|
\core\output\notification::NOTIFY_SUCCESS);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'disablemessaging':
|
|
|
|
set_groups_messaging($groupids, false);
|
|
|
|
redirect($returnurl, get_string('messagingdisabled', 'group', count($groupids)), null,
|
|
|
|
\core\output\notification::NOTIFY_SUCCESS);
|
|
|
|
break;
|
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
default: // ERROR.
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('unknowaction', '', $returnurl);
|
2007-08-16 21:14:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-01-03 08:17:56 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
// Print the page and form.
|
2007-08-16 21:14:03 +00:00
|
|
|
$strgroups = get_string('groups');
|
|
|
|
$strparticipants = get_string('participants');
|
2007-01-03 08:17:56 +00:00
|
|
|
|
2022-12-15 10:54:53 +10:00
|
|
|
// Print header.
|
2009-09-07 06:43:02 +00:00
|
|
|
$PAGE->set_title($strgroups);
|
2010-05-20 03:16:50 +00:00
|
|
|
$PAGE->set_heading($course->fullname);
|
2014-01-10 12:26:50 +01:00
|
|
|
$PAGE->set_pagelayout('standard');
|
2009-09-07 06:43:02 +00:00
|
|
|
echo $OUTPUT->header();
|
|
|
|
|
2021-11-01 14:33:59 +08:00
|
|
|
echo $OUTPUT->render_participants_tertiary_nav($course);
|
2007-07-07 14:18:30 +12:00
|
|
|
|
2007-08-16 21:14:03 +00:00
|
|
|
$groups = groups_get_all_groups($courseid);
|
2017-05-17 16:45:51 +08:00
|
|
|
$selectedname = null;
|
2012-03-12 16:34:45 +00:00
|
|
|
$preventgroupremoval = array();
|
2007-08-16 09:28:18 +00:00
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
// Get list of groups to render.
|
|
|
|
$groupoptions = array();
|
2007-08-16 21:14:03 +00:00
|
|
|
if ($groups) {
|
|
|
|
foreach ($groups as $group) {
|
2017-05-17 16:45:51 +08:00
|
|
|
$selected = false;
|
|
|
|
$usercount = $DB->count_records('groups_members', array('groupid' => $group->id));
|
2023-03-21 12:01:55 +00:00
|
|
|
$groupname = format_string($group->name, true, ['context' => $context, 'escape' => false]) . ' (' . $usercount . ')';
|
2017-05-17 16:45:51 +08:00
|
|
|
if (in_array($group->id, $groupids)) {
|
|
|
|
$selected = true;
|
2008-10-24 13:31:49 +00:00
|
|
|
if ($singlegroup) {
|
2017-05-17 16:45:51 +08:00
|
|
|
// Only keep selected name if there is one group selected.
|
2008-10-24 13:31:49 +00:00
|
|
|
$selectedname = $groupname;
|
|
|
|
}
|
|
|
|
}
|
2012-03-12 16:34:45 +00:00
|
|
|
if (!empty($group->idnumber) && !has_capability('moodle/course:changeidnumber', $context)) {
|
|
|
|
$preventgroupremoval[$group->id] = true;
|
|
|
|
}
|
2009-11-01 16:48:45 +00:00
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
$groupoptions[] = (object) [
|
|
|
|
'value' => $group->id,
|
|
|
|
'selected' => $selected,
|
2021-06-03 08:50:07 +01:00
|
|
|
'text' => s($groupname)
|
2017-05-17 16:45:51 +08:00
|
|
|
];
|
2007-01-03 08:17:56 +00:00
|
|
|
}
|
2007-08-16 21:14:03 +00:00
|
|
|
}
|
2007-08-14 00:50:00 +00:00
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
// Get list of group members to render if there is a single selected group.
|
|
|
|
$members = array();
|
2008-10-24 13:31:49 +00:00
|
|
|
if ($singlegroup) {
|
2021-03-29 09:38:50 +01:00
|
|
|
$userfieldsapi = \core_user\fields::for_identity($context)->with_userpic();
|
|
|
|
[
|
|
|
|
'selects' => $userfieldsselects,
|
|
|
|
'joins' => $userfieldsjoin,
|
|
|
|
'params' => $userfieldsparams
|
|
|
|
] = (array)$userfieldsapi->get_sql('u', true, '', '', false);
|
2021-03-15 15:36:32 +00:00
|
|
|
$extrafields = $userfieldsapi->get_required_fields([\core_user\fields::PURPOSE_IDENTITY]);
|
2019-11-29 07:45:55 +00:00
|
|
|
if ($groupmemberroles = groups_get_members_by_role(reset($groupids), $courseid,
|
2021-03-29 09:38:50 +01:00
|
|
|
'u.id, ' . $userfieldsselects, null, '', $userfieldsparams, $userfieldsjoin)) {
|
2019-11-29 07:45:55 +00:00
|
|
|
|
2020-01-07 16:18:56 +00:00
|
|
|
$viewfullnames = has_capability('moodle/site:viewfullnames', $context);
|
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
foreach ($groupmemberroles as $roleid => $roledata) {
|
|
|
|
$users = array();
|
|
|
|
foreach ($roledata->users as $member) {
|
2019-11-29 07:45:55 +00:00
|
|
|
$shortmember = new stdClass();
|
|
|
|
$shortmember->value = $member->id;
|
2020-01-07 16:18:56 +00:00
|
|
|
$shortmember->text = fullname($member, $viewfullnames);
|
2019-11-29 07:45:55 +00:00
|
|
|
if ($extrafields) {
|
|
|
|
$extrafieldsdisplay = [];
|
|
|
|
foreach ($extrafields as $field) {
|
|
|
|
$extrafieldsdisplay[] = s($member->{$field});
|
|
|
|
}
|
|
|
|
$shortmember->text .= ' (' . implode(', ', $extrafieldsdisplay) . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$users[] = $shortmember;
|
2008-01-17 11:28:54 +00:00
|
|
|
}
|
2023-03-21 12:01:55 +00:00
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
$members[] = (object)[
|
2023-03-21 12:01:55 +00:00
|
|
|
'role' => html_entity_decode($roledata->name, ENT_QUOTES, 'UTF-8'),
|
2017-05-17 16:45:51 +08:00
|
|
|
'rolemembers' => $users
|
|
|
|
];
|
2007-01-03 08:17:56 +00:00
|
|
|
}
|
2008-10-03 07:13:16 +00:00
|
|
|
}
|
2007-08-16 21:14:03 +00:00
|
|
|
}
|
2008-10-03 07:13:16 +00:00
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
$disableaddedit = !$singlegroup;
|
|
|
|
$disabledelete = !empty($groupids);
|
2022-12-15 10:59:05 +10:00
|
|
|
$caneditmessaging = \core_message\api::can_create_group_conversation($USER->id, $context);
|
|
|
|
|
2017-05-17 16:45:51 +08:00
|
|
|
$renderable = new \core_group\output\index_page($courseid, $groupoptions, $selectedname, $members, $disableaddedit, $disabledelete,
|
2022-12-15 10:59:05 +10:00
|
|
|
$preventgroupremoval, $caneditmessaging);
|
2017-05-17 16:45:51 +08:00
|
|
|
$output = $PAGE->get_renderer('core_group');
|
|
|
|
echo $output->render($renderable);
|
2007-01-19 04:57:46 +00:00
|
|
|
|
2009-08-06 14:10:09 +00:00
|
|
|
echo $OUTPUT->footer();
|