[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/group/ -> overview.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1