[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/lib/ -> grouplib.php (source)

   1  <?php  //$Id: grouplib.php,v 1.22.2.7 2008/04/13 21:58:31 skodak Exp $
   2  
   3  /**
   4   * Groups not used in course or activity
   5   */
   6  define('NOGROUPS', 0);
   7  
   8  /**
   9   * Groups used, users do not see other groups
  10   */
  11  define('SEPARATEGROUPS', 1);
  12  
  13  /**
  14   * Groups used, students see other groups
  15   */
  16  define('VISIBLEGROUPS', 2);
  17  
  18  
  19  /**
  20   * Determines if a group with a given groupid exists.
  21   * @param int $groupid The groupid to check for
  22   * @return boolean True if the group exists, false otherwise or if an error
  23   * occurred.
  24   */
  25  function groups_group_exists($groupid) {
  26      return record_exists('groups', 'id', $groupid);
  27  }
  28  
  29  /**
  30   * Gets the name of a group with a specified id
  31   * @param int $groupid The id of the group
  32   * @return string The name of the group
  33   */
  34  function groups_get_group_name($groupid) {
  35      return get_field('groups', 'name', 'id', $groupid);
  36  }
  37  
  38  /**
  39   * Gets the name of a grouping with a specified id
  40   * @param int $groupingid The id of the grouping
  41   * @return string The name of the grouping
  42   */
  43  function groups_get_grouping_name($groupingid) {
  44      return get_field('groupings', 'name', 'id', $groupingid);
  45  }
  46  
  47  /**
  48   * Returns the groupid of a group with the name specified for the course.
  49   * Group names should be unique in course
  50   * @param int $courseid The id of the course
  51   * @param string $name name of group (without magic quotes)
  52   * @return int $groupid
  53   */
  54  function groups_get_group_by_name($courseid, $name) {
  55      if ($groups = get_records_select('groups', "courseid=$courseid AND name='".addslashes($name)."'")) {
  56          return key($groups);
  57      }
  58      return false;
  59  }
  60  
  61  /**
  62   * Returns the groupingid of a grouping with the name specified for the course.
  63   * Grouping names should be unique in course
  64   * @param int $courseid The id of the course
  65   * @param string $name name of group (without magic quotes)
  66   * @return int $groupid
  67   */
  68  function groups_get_grouping_by_name($courseid, $name) {
  69      if ($groupings = get_records_select('groupings', "courseid=$courseid AND name='".addslashes($name)."'")) {
  70          return key($groupings);
  71      }
  72      return false;
  73  }
  74  
  75  /**
  76   * Get the group object
  77   * @param groupid ID of the group.
  78   * @return group object
  79   */
  80  function groups_get_group($groupid) {
  81      return get_record('groups', 'id', $groupid);
  82  }
  83  
  84  /**
  85   * Get the grouping object
  86   * @param groupingid ID of the group.
  87   * @return group object
  88   */
  89  function groups_get_grouping($groupingid) {
  90      return get_record('groupings', 'id', $groupingid);
  91  }
  92  
  93  /**
  94   * Gets array of all groups in a specified course.
  95   * @param int $courseid The id of the course.
  96   * @param mixed $userid optional user id or array of ids, returns only groups of the user.
  97   * @param int $groupingid optional returns only groups in the specified grouping.
  98   * @return array | false Returns an array of the group objects or false if no records
  99   * or an error occurred. (userid field returned if array in $userid)
 100   */
 101  function groups_get_all_groups($courseid, $userid=0, $groupingid=0, $fields='g.*') {
 102      global $CFG;
 103  
 104      // groupings are ignored when not enabled
 105      if (empty($CFG->enablegroupings)) {
 106          $groupingid = 0;
 107      }
 108  
 109      if (empty($userid)) {
 110          $userfrom  = "";
 111          $userwhere = "";
 112  
 113      } else if (is_array($userid)) {
 114          $userids = implode(',', $userid);
 115          $userfrom  = ", {$CFG->prefix}groups_members gm";
 116          $userwhere = "AND g.id = gm.groupid AND gm.userid IN ($userids)";
 117  
 118      } else {
 119          $userfrom  = ", {$CFG->prefix}groups_members gm";
 120          $userwhere = "AND g.id = gm.groupid AND gm.userid = '$userid'";
 121      }
 122  
 123      if (!empty($groupingid)) {
 124          $groupingfrom  = ", {$CFG->prefix}groupings_groups gg";
 125          $groupingwhere = "AND g.id = gg.groupid AND gg.groupingid = '$groupingid'";
 126      } else {
 127          $groupingfrom  = "";
 128          $groupingwhere = "";
 129      }
 130  
 131      return get_records_sql("SELECT $fields
 132                                FROM {$CFG->prefix}groups g $userfrom $groupingfrom
 133                               WHERE g.courseid = $courseid $userwhere $groupingwhere
 134                            ORDER BY name ASC");
 135  }
 136  
 137  /**
 138   * Returns info about user's groups in course.
 139   * @param int $courseid
 140   * @param int $userid $USER if not specified
 141   * @return array[groupingid][groupid] including grouping id 0 which means all groups
 142   */
 143  function groups_get_user_groups($courseid, $userid=0) {
 144      global $CFG, $USER;
 145  
 146      if (empty($userid)) {
 147          $userid = $USER->id;
 148      }
 149  
 150      if (!$rs = get_recordset_sql("SELECT g.id, gg.groupingid
 151                                      FROM {$CFG->prefix}groups g
 152                                           JOIN {$CFG->prefix}groups_members gm        ON gm.groupid = g.id
 153                                           LEFT JOIN {$CFG->prefix}groupings_groups gg ON gg.groupid = g.id
 154                                     WHERE gm.userid = $userid AND g.courseid = $courseid")) {
 155          return array('0' => array());
 156      }
 157  
 158      $result    = array();
 159      $allgroups = array();
 160      
 161      while ($group = rs_fetch_next_record($rs)) {
 162          $allgroups[$group->id] = $group->id;
 163          if (is_null($group->groupingid)) {
 164              continue;
 165          }
 166          if (!array_key_exists($group->groupingid, $result)) {
 167              $result[$group->groupingid] = array();
 168          }
 169          $result[$group->groupingid][$group->id] = $group->id;
 170      }
 171      rs_close($rs);
 172  
 173      $result['0'] = array_keys($allgroups); // all groups
 174  
 175      return $result;
 176  }
 177  
 178  /**
 179   * Gets array of all groupings in a specified course.
 180   * @param int $courseid return only groupings in this with this courseid
 181   * @return array | false Returns an array of the grouping objects or false if no records
 182   * or an error occurred.
 183   */
 184  function groups_get_all_groupings($courseid) {
 185      global $CFG;
 186  
 187      // groupings are ignored when not enabled
 188      if (empty($CFG->enablegroupings)) {
 189          return(false);
 190      }
 191      return get_records_sql("SELECT *
 192                                FROM {$CFG->prefix}groupings
 193                               WHERE courseid = $courseid
 194                            ORDER BY name ASC");
 195  }
 196  
 197  
 198  
 199  /**
 200   * Determines if the user is a member of the given group.
 201   *
 202   * @uses $USER If $userid is null, use the global object.
 203   * @param int $groupid The group to check for membership.
 204   * @param int $userid The user to check against the group.
 205   * @return boolean True if the user is a member, false otherwise.
 206   */
 207  function groups_is_member($groupid, $userid=null) {
 208      global $USER;
 209  
 210      if (!$userid) {
 211          $userid = $USER->id;
 212      }
 213  
 214      return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
 215  }
 216  
 217  /**
 218   * Determines if current or specified is member of any active group in activity
 219   * @param object $cm coruse module object
 220   * @param int $userid id of user, null menas $USER->id
 221   * @return booelan true if user member of at least one group used in activity
 222   */
 223  function groups_has_membership($cm, $userid=null) {
 224      global $CFG, $USER;
 225  
 226      static $cache = array();
 227  
 228      // groupings are ignored when not enabled
 229      if (empty($CFG->enablegroupings)) {
 230          $cm->groupingid = 0;
 231      }
 232  
 233      if (empty($userid)) {
 234          $userid = $USER->id;
 235      }
 236  
 237      $cachekey = $userid.'|'.$cm->course.'|'.$cm->groupingid;
 238      if (isset($cache[$cachekey])) {
 239          return($cache[$cachekey]);
 240      }
 241  
 242      if ($cm->groupingid) {
 243          // find out if member of any group in selected activity grouping
 244          $sql = "SELECT 'x'
 245                    FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groupings_groups gg
 246                   WHERE gm.userid = $userid AND gm.groupid = gg.groupid AND gg.groupingid = {$cm->groupingid}";
 247  
 248      } else {
 249          // no grouping used - check all groups in course
 250          $sql = "SELECT 'x'
 251                    FROM {$CFG->prefix}groups_members gm, {$CFG->prefix}groups g
 252                   WHERE gm.userid = $userid AND gm.groupid = g.id AND g.courseid = {$cm->course}";
 253      }
 254  
 255      $cache[$cachekey] = record_exists_sql($sql);
 256  
 257      return $cache[$cachekey];
 258  }
 259  
 260  /**
 261   * Returns the users in the specified group.
 262   * @param int $groupid The groupid to get the users for
 263   * @param int $fields The fields to return
 264   * @param int $sort optional sorting of returned users
 265   * @return array | false Returns an array of the users for the specified
 266   * group or false if no users or an error returned.
 267   */
 268  function groups_get_members($groupid, $fields='u.*', $sort='lastname ASC') {
 269      global $CFG;
 270  
 271      return get_records_sql("SELECT $fields
 272                                FROM {$CFG->prefix}user u, {$CFG->prefix}groups_members gm
 273                               WHERE u.id = gm.userid AND gm.groupid = '$groupid'
 274                            ORDER BY $sort");
 275  }
 276  
 277  
 278  /**
 279   * Returns the users in the specified grouping.
 280   * @param int $groupingid The groupingid to get the users for
 281   * @param int $fields The fields to return
 282   * @param int $sort optional sorting of returned users
 283   * @return array | false Returns an array of the users for the specified
 284   * group or false if no users or an error returned.
 285   */
 286  function groups_get_grouping_members($groupingid, $fields='u.*', $sort='lastname ASC') {
 287      global $CFG;
 288  
 289      return get_records_sql("SELECT $fields
 290                                FROM {$CFG->prefix}user u
 291                                  INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
 292                                  INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid
 293                               WHERE  gg.groupingid = $groupingid
 294                            ORDER BY $sort");
 295  }
 296  
 297  /**
 298   * Returns effective groupmode used in course
 299   * @return integer group mode
 300   */
 301  function groups_get_course_groupmode($course) {
 302      return $course->groupmode;
 303  }
 304  
 305  /**
 306   * Returns effective groupmode used in activity, course setting
 307   * overrides activity setting if groupmodeforce enabled.
 308   * @param $cm the course module object. Only the ->course and ->groupmode need to be set.
 309   * @param $course object optional course object to improve perf
 310   * @return integer group mode
 311   */
 312  function groups_get_activity_groupmode($cm, $course=null) {
 313      global $COURSE;
 314  
 315      // get course object (reuse COURSE if possible)
 316      if (isset($course->id) and $course->id == $cm->course) {
 317          //ok
 318      } else if ($cm->course == $COURSE->id) {
 319          $course = $COURSE;
 320      } else {
 321          if (!$course = get_record('course', 'id', $cm->course)) {
 322              error('Incorrect course id in cm');
 323          }
 324      }
 325  
 326      return empty($course->groupmodeforce) ? $cm->groupmode : $course->groupmode;
 327  }
 328  
 329  /**
 330   * Print group menu selector for course level.
 331   * @param object $course course object
 332   * @param string $urlroot return address
 333   * @param boolean $return return as string instead of printing
 334   * @return mixed void or string depending on $return param
 335   */
 336  function groups_print_course_menu($course, $urlroot, $return=false) {
 337      global $CFG, $USER, $SESSION;
 338  
 339      if (!$groupmode = $course->groupmode) {
 340          if ($return) {
 341              return '';
 342          } else {
 343              return;
 344          }
 345      }
 346  
 347      $context = get_context_instance(CONTEXT_COURSE, $course->id);
 348      if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
 349          $allowedgroups = groups_get_all_groups($course->id, 0);
 350          // detect changes related to groups and fix active group
 351          if (!empty($SESSION->activegroup[$course->id][VISIBLEGROUPS][0])) {
 352              if (!array_key_exists($SESSION->activegroup[$course->id][VISIBLEGROUPS][0], $allowedgroups)) {
 353                  // active does not exist anymore
 354                  unset($SESSION->activegroup[$course->id][VISIBLEGROUPS][0]);
 355              }
 356          }
 357          if (!empty($SESSION->activegroup[$course->id]['aag'][0])) {
 358              if (!array_key_exists($SESSION->activegroup[$course->id]['aag'][0], $allowedgroups)) {
 359                  // active group does not exist anymore
 360                  unset($SESSION->activegroup[$course->id]['aag'][0]);
 361              }
 362          }
 363  
 364      } else {
 365          $allowedgroups = groups_get_all_groups($course->id, $USER->id);
 366          // detect changes related to groups and fix active group
 367          if (isset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0])) {
 368              if ($SESSION->activegroup[$course->id][SEPARATEGROUPS][0] == 0) {
 369                  if ($allowedgroups) {
 370                      // somebody must have assigned at least one group, we can select it now - yay!
 371                      unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
 372                  }
 373              } else {
 374                  if (!array_key_exists($SESSION->activegroup[$course->id][SEPARATEGROUPS][0], $allowedgroups)) {
 375                      // active group not allowed or does not exist anymore
 376                      unset($SESSION->activegroup[$course->id][SEPARATEGROUPS][0]);
 377                  }
 378              }
 379          }
 380      }
 381  
 382      $activegroup = groups_get_course_group($course, true);
 383  
 384      $groupsmenu = array();
 385      if (!$allowedgroups or $groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
 386          $groupsmenu[0] = get_string('allparticipants');
 387      }
 388  
 389      if ($allowedgroups) {
 390          foreach ($allowedgroups as $group) {
 391              $groupsmenu[$group->id] = format_string($group->name);
 392          }
 393      }
 394  
 395      if ($groupmode == VISIBLEGROUPS) {
 396          $grouplabel = get_string('groupsvisible');
 397      } else {
 398          $grouplabel = get_string('groupsseparate');
 399      }
 400  
 401      if (count($groupsmenu) == 1) {
 402          $groupname = reset($groupsmenu);
 403          $output = $grouplabel.': '.$groupname;
 404      } else {
 405          $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
 406      }
 407  
 408      $output = '<div class="groupselector">'.$output.'</div>';
 409  
 410      if ($return) {
 411          return $output;
 412      } else {
 413          echo $output;
 414      }
 415  }
 416  
 417  /**
 418   * Print group menu selector for activity.
 419   * @param object $cm course module object
 420   * @param string $urlroot return address that users get to if they choose an option;
 421   *   should include any parameters needed, e.g. 'view.php?id=34'
 422   * @param boolean $return return as string instead of printing
 423   * @param boolean $hideallparticipants If true, this prevents the 'All participants'
 424   *   option from appearing in cases where it normally would. This is intended for
 425   *   use only by activities that cannot display all groups together. (Note that
 426   *   selecting this option does not prevent groups_get_activity_group from
 427   *   returning 0; it will still do that if the user has chosen 'all participants'
 428   *   in another activity, or not chosen anything.)
 429   * @return mixed void or string depending on $return param
 430   */
 431  function groups_print_activity_menu($cm, $urlroot, $return=false, $hideallparticipants=false) {
 432      global $CFG, $USER, $SESSION;
 433  
 434      // groupings are ignored when not enabled
 435      if (empty($CFG->enablegroupings)) {
 436          $cm->groupingid = 0;
 437      }
 438  
 439      if (!$groupmode = groups_get_activity_groupmode($cm)) {
 440          if ($return) {
 441              return '';
 442          } else {
 443              return;
 444          }
 445      }
 446  
 447      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 448      if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
 449          $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
 450          // detect changes related to groups and fix active group
 451          if (!empty($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid])) {
 452              if (!array_key_exists($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid], $allowedgroups)) {
 453                  // active group does not exist anymore
 454                  unset($SESSION->activegroup[$cm->course][VISIBLEGROUPS][$cm->groupingid]);
 455              }
 456          }
 457          if (!empty($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid])) {
 458              if (!array_key_exists($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid], $allowedgroups)) {
 459                  // active group does not exist anymore
 460                  unset($SESSION->activegroup[$cm->course]['aag'][$cm->groupingid]);
 461              }
 462          }
 463  
 464      } else {
 465          $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
 466          // detect changes related to groups and fix active group
 467          if (isset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid])) {
 468              if ($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid] == 0) {
 469                  if ($allowedgroups) {
 470                      // somebody must have assigned at least one group, we can select it now - yay!
 471                      unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
 472                  }
 473              } else {
 474                  if (!array_key_exists($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid], $allowedgroups)) {
 475                      // active group not allowed or does not exist anymore
 476                      unset($SESSION->activegroup[$cm->course][SEPARATEGROUPS][$cm->groupingid]);
 477                  }
 478              }
 479          }
 480      }
 481  
 482      $activegroup = groups_get_activity_group($cm, true);
 483  
 484      $groupsmenu = array();
 485      if ((!$allowedgroups or $groupmode == VISIBLEGROUPS or
 486        has_capability('moodle/site:accessallgroups', $context)) and !$hideallparticipants) {
 487          $groupsmenu[0] = get_string('allparticipants');
 488      }
 489  
 490      if ($allowedgroups) {
 491          foreach ($allowedgroups as $group) {
 492              $groupsmenu[$group->id] = format_string($group->name);
 493          }
 494      }
 495  
 496      if ($groupmode == VISIBLEGROUPS) {
 497          $grouplabel = get_string('groupsvisible');
 498      } else {
 499          $grouplabel = get_string('groupsseparate');
 500      }
 501  
 502      if (count($groupsmenu) == 1) {
 503          $groupname = reset($groupsmenu);
 504          $output = $grouplabel.': '.$groupname;
 505      } else {
 506          $output = popup_form($urlroot.'&amp;group=', $groupsmenu, 'selectgroup', $activegroup, '', '', '', true, 'self', $grouplabel);
 507      }
 508  
 509      $output = '<div class="groupselector">'.$output.'</div>';
 510  
 511      if ($return) {
 512          return $output;
 513      } else {
 514          echo $output;
 515      }
 516  }
 517  
 518  /**
 519   * Returns group active in course, changes the group by default if 'group' page param present
 520   *
 521   * @param object $course course bject
 522   * @param boolean $update change active group if group param submitted
 523   * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
 524   */
 525  function groups_get_course_group($course, $update=false) {
 526      global $CFG, $USER, $SESSION;
 527  
 528      if (!$groupmode = $course->groupmode) {
 529          // NOGROUPS used
 530          return false;
 531      }
 532  
 533      // init activegroup array
 534      if (!array_key_exists('activegroup', $SESSION)) {
 535          $SESSION->activegroup = array();
 536      }
 537      if (!array_key_exists($course->id, $SESSION->activegroup)) {
 538          $SESSION->activegroup[$course->id] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
 539      }
 540  
 541      $context = get_context_instance(CONTEXT_COURSE, $course->id);
 542      if (has_capability('moodle/site:accessallgroups', $context)) {
 543          $groupmode = 'aag';
 544      }
 545  
 546      // grouping used the first time - add first user group as default
 547      if (!array_key_exists(0, $SESSION->activegroup[$course->id][$groupmode])) {
 548          if ($groupmode == 'aag') {
 549              $SESSION->activegroup[$course->id][$groupmode][0] = 0; // all groups by default if user has accessallgroups
 550  
 551          } else if ($usergroups = groups_get_all_groups($course->id, $USER->id, 0)) {
 552              $fistgroup = reset($usergroups);
 553              $SESSION->activegroup[$course->id][$groupmode][0] = $fistgroup->id;
 554  
 555          } else {
 556              // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
 557              // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
 558              $SESSION->activegroup[$course->id][$groupmode][0] = 0;
 559          }
 560      }
 561  
 562      // set new active group if requested
 563      $changegroup = optional_param('group', -1, PARAM_INT);
 564      if ($update and $changegroup != -1) {
 565  
 566          if ($changegroup == 0) {
 567              // do not allow changing to all groups without accessallgroups capability
 568              if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
 569                  $SESSION->activegroup[$course->id][$groupmode][0] = 0;
 570              }
 571  
 572          } else {
 573              // first make list of allowed groups
 574              if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
 575                  $allowedgroups = groups_get_all_groups($course->id, 0, 0);
 576              } else {
 577                  $allowedgroups = groups_get_all_groups($course->id, $USER->id, 0);
 578              }
 579  
 580              if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
 581                  $SESSION->activegroup[$course->id][$groupmode][0] = $changegroup;
 582              }
 583          }
 584      }
 585  
 586      return $SESSION->activegroup[$course->id][$groupmode][0];
 587  }
 588  
 589  /**
 590   * Returns group active in activity, changes the group by default if 'group' page param present
 591   *
 592   * @param object $cm course module object
 593   * @param boolean $update change active group if group param submitted
 594   * @return mixed false if groups not used, int if groups used, 0 means all groups (access must be verified in SEPARATE mode)
 595   */
 596  function groups_get_activity_group($cm, $update=false) {
 597      global $CFG, $USER, $SESSION;
 598  
 599      // groupings are ignored when not enabled
 600      if (empty($CFG->enablegroupings)) {
 601          $cm->groupingid = 0;
 602      }
 603  
 604      if (!$groupmode = groups_get_activity_groupmode($cm)) {
 605          // NOGROUPS used
 606          return false;
 607      }
 608  
 609      // init activegroup array
 610      if (!array_key_exists('activegroup', $SESSION)) {
 611          $SESSION->activegroup = array();
 612      }
 613      if (!array_key_exists($cm->course, $SESSION->activegroup)) {
 614          $SESSION->activegroup[$cm->course] = array(SEPARATEGROUPS=>array(), VISIBLEGROUPS=>array(), 'aag'=>array());
 615      }
 616  
 617      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 618      if (has_capability('moodle/site:accessallgroups', $context)) {
 619          $groupmode = 'aag';
 620      }
 621  
 622      // grouping used the first time - add first user group as default
 623      if (!array_key_exists($cm->groupingid, $SESSION->activegroup[$cm->course][$groupmode])) {
 624          if ($groupmode == 'aag') {
 625              $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0; // all groups by default if user has accessallgroups
 626  
 627          } else if ($usergroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid)) {
 628              $fistgroup = reset($usergroups);
 629              $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $fistgroup->id;
 630  
 631          } else {
 632              // this happen when user not assigned into group in SEPARATEGROUPS mode or groups do not exist yet
 633              // mod authors must add extra checks for this when SEPARATEGROUPS mode used (such as when posting to forum)
 634              $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
 635          }
 636      }
 637  
 638      // set new active group if requested
 639      $changegroup = optional_param('group', -1, PARAM_INT);
 640      if ($update and $changegroup != -1) {
 641  
 642          if ($changegroup == 0) {
 643              // allgroups visible only in VISIBLEGROUPS or when accessallgroups
 644              if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
 645                  $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = 0;
 646              }
 647  
 648          } else {
 649              // first make list of allowed groups
 650              if ($groupmode == VISIBLEGROUPS or $groupmode == 'aag') {
 651                  $allowedgroups = groups_get_all_groups($cm->course, 0, $cm->groupingid); // any group in grouping (all if groupings not used)
 652              } else {
 653                  $allowedgroups = groups_get_all_groups($cm->course, $USER->id, $cm->groupingid); // only assigned groups
 654              }
 655  
 656              if ($allowedgroups and array_key_exists($changegroup, $allowedgroups)) {
 657                  $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid] = $changegroup;
 658              }
 659          }
 660      }
 661  
 662      return $SESSION->activegroup[$cm->course][$groupmode][$cm->groupingid];
 663  }
 664  
 665  /**
 666   * Gets a list of groups that the user is allowed to access within the
 667   * specified activity.
 668   * @param object $cm Course-module
 669   * @param int $userid User ID (defaults to current user)
 670   * @return array An array of group objects, or false if none
 671   */
 672  function groups_get_activity_allowed_groups($cm,$userid=0) {
 673      // Use current user by default
 674      global $USER;
 675      if(!$userid) {
 676          $userid=$USER->id;
 677      }
 678  
 679      // Get groupmode for activity, taking into account course settings
 680      $groupmode=groups_get_activity_groupmode($cm);
 681  
 682      // If visible groups mode, or user has the accessallgroups capability,
 683      // then they can access all groups for the activity...
 684      $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 685      if ($groupmode == VISIBLEGROUPS or has_capability('moodle/site:accessallgroups', $context)) {
 686          return groups_get_all_groups($cm->course, 0, $cm->groupingid);
 687      } else {
 688          // ...otherwise they can only access groups they belong to
 689          return groups_get_all_groups($cm->course, $userid, $cm->groupingid);
 690      }
 691  }
 692  
 693  /**
 694   * Determine if a course module is currently visible to a user
 695   * @uses $USER If $userid is null, use the global object.
 696   * @param int $cm The course module
 697   * @param int $userid The user to check against the group.
 698   * @return boolean True if the user can view the course module, false otherwise.
 699   */
 700  function groups_course_module_visible($cm, $userid=null) {
 701      global $CFG, $USER;
 702  
 703      if (empty($userid)) {
 704          $userid = $USER->id;
 705      }
 706      if (empty($CFG->enablegroupings)) {
 707          return true;
 708      }
 709      if (empty($cm->groupmembersonly)) {
 710          return true;
 711      }
 712      if (has_capability('moodle/site:accessallgroups', get_context_instance(CONTEXT_MODULE, $cm->id), $userid) or groups_has_membership($cm, $userid)) {
 713          return true;
 714      }
 715      return false;
 716  }
 717  
 718  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7