overview.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Print an overview of groupings & group membership
  18. *
  19. * @copyright Matt Clarkson mattc@catalyst.net.nz
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. * @package core_group
  22. */
  23. require_once('../config.php');
  24. require_once($CFG->libdir . '/filelib.php');
  25. define('OVERVIEW_NO_GROUP', -1); // The fake group for users not in a group.
  26. define('OVERVIEW_GROUPING_GROUP_NO_GROUPING', -1); // The fake grouping for groups that have no grouping.
  27. define('OVERVIEW_GROUPING_NO_GROUP', -2); // The fake grouping for users with no group.
  28. $courseid = required_param('id', PARAM_INT);
  29. $groupid = optional_param('group', 0, PARAM_INT);
  30. $groupingid = optional_param('grouping', 0, PARAM_INT);
  31. $returnurl = $CFG->wwwroot.'/group/index.php?id='.$courseid;
  32. $rooturl = $CFG->wwwroot.'/group/overview.php?id='.$courseid;
  33. if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
  34. print_error('invalidcourse');
  35. }
  36. $url = new moodle_url('/group/overview.php', array('id'=>$courseid));
  37. if ($groupid !== 0) {
  38. $url->param('group', $groupid);
  39. }
  40. if ($groupingid !== 0) {
  41. $url->param('grouping', $groupingid);
  42. }
  43. $PAGE->set_url($url);
  44. // Make sure that the user has permissions to manage groups.
  45. require_login($course);
  46. $context = context_course::instance($courseid);
  47. require_capability('moodle/course:managegroups', $context);
  48. $strgroups = get_string('groups');
  49. $strparticipants = get_string('participants');
  50. $stroverview = get_string('overview', 'group');
  51. $strgrouping = get_string('grouping', 'group');
  52. $strgroup = get_string('group', 'group');
  53. $strnotingrouping = get_string('notingrouping', 'group');
  54. $strfiltergroups = get_string('filtergroups', 'group');
  55. $strnogroups = get_string('nogroups', 'group');
  56. $strdescription = get_string('description');
  57. $strnotingroup = get_string('notingrouplist', 'group');
  58. $strnogroup = get_string('nogroup', 'group');
  59. $strnogrouping = get_string('nogrouping', 'group');
  60. // Get all groupings and sort them by formatted name.
  61. $groupings = $DB->get_records('groupings', array('courseid'=>$courseid), 'name');
  62. foreach ($groupings as $gid => $grouping) {
  63. $groupings[$gid]->formattedname = format_string($grouping->name, true, array('context' => $context));
  64. }
  65. core_collator::asort_objects_by_property($groupings, 'formattedname');
  66. $members = array();
  67. foreach ($groupings as $grouping) {
  68. $members[$grouping->id] = array();
  69. }
  70. // Groups not in a grouping.
  71. $members[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = array();
  72. // Get all groups
  73. $groups = $DB->get_records('groups', array('courseid'=>$courseid), 'name');
  74. $params = array('courseid'=>$courseid);
  75. if ($groupid) {
  76. $groupwhere = "AND g.id = :groupid";
  77. $params['groupid'] = $groupid;
  78. } else {
  79. $groupwhere = "";
  80. }
  81. if ($groupingid) {
  82. if ($groupingid < 0) { // No grouping filter.
  83. $groupingwhere = "AND gg.groupingid IS NULL";
  84. } else {
  85. $groupingwhere = "AND gg.groupingid = :groupingid";
  86. $params['groupingid'] = $groupingid;
  87. }
  88. } else {
  89. $groupingwhere = "";
  90. }
  91. list($sort, $sortparams) = users_order_by_sql('u');
  92. $allnames = get_all_user_name_fields(true, 'u');
  93. $sql = "SELECT g.id AS groupid, gg.groupingid, u.id AS userid, $allnames, u.idnumber, u.username
  94. FROM {groups} g
  95. LEFT JOIN {groupings_groups} gg ON g.id = gg.groupid
  96. LEFT JOIN {groups_members} gm ON g.id = gm.groupid
  97. LEFT JOIN {user} u ON gm.userid = u.id
  98. WHERE g.courseid = :courseid $groupwhere $groupingwhere
  99. ORDER BY g.name, $sort";
  100. $rs = $DB->get_recordset_sql($sql, array_merge($params, $sortparams));
  101. foreach ($rs as $row) {
  102. $user = new stdClass();
  103. $user = username_load_fields_from_object($user, $row, null, array('id' => 'userid', 'username', 'idnumber'));
  104. if (!$row->groupingid) {
  105. $row->groupingid = OVERVIEW_GROUPING_GROUP_NO_GROUPING;
  106. }
  107. if (!array_key_exists($row->groupid, $members[$row->groupingid])) {
  108. $members[$row->groupingid][$row->groupid] = array();
  109. }
  110. if (!empty($user->id)) {
  111. $members[$row->groupingid][$row->groupid][] = $user;
  112. }
  113. }
  114. $rs->close();
  115. // Add 'no groupings' / 'no groups' selectors.
  116. $groupings[OVERVIEW_GROUPING_GROUP_NO_GROUPING] = (object)array(
  117. 'id' => OVERVIEW_GROUPING_GROUP_NO_GROUPING,
  118. 'formattedname' => $strnogrouping,
  119. );
  120. $groups[OVERVIEW_NO_GROUP] = (object)array(
  121. 'id' => OVERVIEW_NO_GROUP,
  122. 'courseid' => $courseid,
  123. 'idnumber' => '',
  124. 'name' => $strnogroup,
  125. 'description' => '',
  126. 'descriptionformat' => FORMAT_HTML,
  127. 'enrolmentkey' => '',
  128. 'picture' => 0,
  129. 'hidepicture' => 0,
  130. 'timecreated' => 0,
  131. 'timemodified' => 0,
  132. );
  133. // Add users who are not in a group.
  134. if ($groupid <= 0 && $groupingid <= 0) {
  135. list($esql, $params) = get_enrolled_sql($context, null, 0, true);
  136. $sql = "SELECT u.id, $allnames, u.idnumber, u.username
  137. FROM {user} u
  138. JOIN ($esql) e ON e.id = u.id
  139. LEFT JOIN (
  140. SELECT gm.userid
  141. FROM {groups_members} gm
  142. JOIN {groups} g ON g.id = gm.groupid
  143. WHERE g.courseid = :courseid
  144. ) grouped ON grouped.userid = u.id
  145. WHERE grouped.userid IS NULL";
  146. $params['courseid'] = $courseid;
  147. $nogroupusers = $DB->get_records_sql($sql, $params);
  148. if ($nogroupusers) {
  149. $members[OVERVIEW_GROUPING_NO_GROUP][OVERVIEW_NO_GROUP] = $nogroupusers;
  150. }
  151. }
  152. navigation_node::override_active_url(new moodle_url('/group/index.php', array('id'=>$courseid)));
  153. $PAGE->navbar->add(get_string('overview', 'group'));
  154. /// Print header
  155. $PAGE->set_title($strgroups);
  156. $PAGE->set_heading($course->fullname);
  157. $PAGE->set_pagelayout('standard');
  158. echo $OUTPUT->header();
  159. // Add tabs
  160. $currenttab = 'overview';
  161. require('tabs.php');
  162. /// Print overview
  163. echo $OUTPUT->heading(format_string($course->shortname, true, array('context' => $context)) .' '.$stroverview, 3);
  164. echo $strfiltergroups;
  165. $options = array();
  166. $options[0] = get_string('all');
  167. foreach ($groupings as $grouping) {
  168. $options[$grouping->id] = strip_tags($grouping->formattedname);
  169. }
  170. $popupurl = new moodle_url($rooturl.'&group='.$groupid);
  171. $select = new single_select($popupurl, 'grouping', $options, $groupingid, array());
  172. $select->label = $strgrouping;
  173. $select->formid = 'selectgrouping';
  174. echo $OUTPUT->render($select);
  175. $options = array();
  176. $options[0] = get_string('all');
  177. foreach ($groups as $group) {
  178. $options[$group->id] = strip_tags(format_string($group->name));
  179. }
  180. $popupurl = new moodle_url($rooturl.'&grouping='.$groupingid);
  181. $select = new single_select($popupurl, 'group', $options, $groupid, array());
  182. $select->label = $strgroup;
  183. $select->formid = 'selectgroup';
  184. echo $OUTPUT->render($select);
  185. /// Print table
  186. $printed = false;
  187. $hoverevents = array();
  188. foreach ($members as $gpgid=>$groupdata) {
  189. if ($groupingid and $groupingid != $gpgid) {
  190. if ($groupingid > 0 || $gpgid > 0) { // Still show 'not in group' when 'no grouping' selected.
  191. continue; // Do not show.
  192. }
  193. }
  194. $table = new html_table();
  195. $table->head = array(get_string('groupscount', 'group', count($groupdata)), get_string('groupmembers', 'group'), get_string('usercount', 'group'));
  196. $table->size = array('20%', '70%', '10%');
  197. $table->align = array('left', 'left', 'center');
  198. $table->width = '90%';
  199. $table->data = array();
  200. foreach ($groupdata as $gpid=>$users) {
  201. if ($groupid and $groupid != $gpid) {
  202. continue;
  203. }
  204. $line = array();
  205. $name = print_group_picture($groups[$gpid], $course->id, false, true, false) . format_string($groups[$gpid]->name);
  206. $description = file_rewrite_pluginfile_urls($groups[$gpid]->description, 'pluginfile.php', $context->id, 'group', 'description', $gpid);
  207. $options = new stdClass;
  208. $options->noclean = true;
  209. $options->overflowdiv = true;
  210. $jsdescription = trim(format_text($description, $groups[$gpid]->descriptionformat, $options));
  211. if (empty($jsdescription)) {
  212. $line[] = $name;
  213. } else {
  214. $line[] = html_writer::tag('span', $name, array('class' => 'group_hoverdescription', 'data-groupid' => $gpid));
  215. $hoverevents[$gpid] = $jsdescription;
  216. }
  217. $fullnames = array();
  218. foreach ($users as $user) {
  219. $fullnames[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.$course->id.'">'.fullname($user, true).'</a>';
  220. }
  221. $line[] = implode(', ', $fullnames);
  222. $line[] = count($users);
  223. $table->data[] = $line;
  224. }
  225. if ($groupid and empty($table->data)) {
  226. continue;
  227. }
  228. if ($gpgid < 0) {
  229. // Display 'not in group' for grouping id == OVERVIEW_GROUPING_NO_GROUP.
  230. if ($gpgid == OVERVIEW_GROUPING_NO_GROUP) {
  231. echo $OUTPUT->heading($strnotingroup, 3);
  232. } else {
  233. echo $OUTPUT->heading($strnotingrouping, 3);
  234. }
  235. } else {
  236. echo $OUTPUT->heading($groupings[$gpgid]->formattedname, 3);
  237. $description = file_rewrite_pluginfile_urls($groupings[$gpgid]->description, 'pluginfile.php', $context->id, 'grouping', 'description', $gpgid);
  238. $options = new stdClass;
  239. $options->overflowdiv = true;
  240. echo $OUTPUT->box(format_text($description, $groupings[$gpgid]->descriptionformat, $options), 'generalbox boxwidthnarrow boxaligncenter');
  241. }
  242. echo html_writer::table($table);
  243. $printed = true;
  244. }
  245. if (count($hoverevents)>0) {
  246. $PAGE->requires->string_for_js('description', 'moodle');
  247. $PAGE->requires->js_init_call('M.core_group.init_hover_events', array($hoverevents));
  248. }
  249. echo $OUTPUT->footer();