Beispiel #1
0
 /**
  * Determines whether this particular section is currently available
  * according to these criteria.
  *
  * - This does not include the 'visible' setting (i.e. this might return
  *   true even if visible is false); visible is handled independently.
  * - This does not take account of the viewhiddenactivities capability.
  *   That should apply later.
  *
  * @global moodle_database $DB
  * @global stdclass $USER
  * @param string $information If the item has availability restrictions,
  *   a string that describes the conditions will be stored in this variable;
  *   if this variable is set blank, that means don't display anything
  * @param bool $grabthelot Performance hint: if true, caches information
  *   required for all course-modules, to make the front page and similar
  *   pages work more quickly (works only for current user)
  * @param int $userid If set, specifies a different user ID to check availability for
  * @param object $modinfo Usually leave as null for default. Specify when
  *   calling recursively from inside get_fast_modinfo. The value supplied
  *   here must include list of all CMs with 'id' and 'name'
  * @return bool True if this item is available to the user, false otherwise
  */
 public function is_available(&$information, $grabthelot = false, $userid = 0, $modinfo = null)
 {
     global $DB, $USER, $CONDITIONLIB_PRIVATE;
     $available = parent::is_available($information, $grabthelot, $userid, $modinfo);
     // test if user is enrolled to a grouping which has access to the section
     if (!empty($this->item->groupingid)) {
         // Get real user id
         if (!$userid) {
             $userid = $USER->id;
         }
         $context = context_course::instance($this->item->course);
         if ($userid != $USER->id) {
             // We are requesting for a non-current user so check it individually
             // (no cache). Do grouping check first, it's probably faster
             // than the capability check
             $gotit = $DB->record_exists_sql('
                     SELECT
                         1
                     FROM
                         {groupings} g
                         JOIN {groupings_groups} gg ON g.id = gg.groupingid
                         JOIN {groups_members} gm ON gg.groupid = gm.groupid
                     WHERE
                         g.id = ? AND gm.userid = ?', array($this->item->groupingid, $userid));
             if (!$gotit && !has_capability('moodle/site:accessallgroups', $context, $userid)) {
                 $available = false;
                 $information .= get_string('groupingnoaccess', 'condition');
             }
         } else {
             // Request is for current user - use cache
             if (!array_key_exists($this->item->course, $CONDITIONLIB_PRIVATE->groupingscache)) {
                 if (has_capability('moodle/site:accessallgroups', $context)) {
                     $CONDITIONLIB_PRIVATE->groupingscache[$this->item->course] = true;
                 } else {
                     $groupings = $DB->get_records_sql('
                             SELECT
                                 g.id as gid
                             FROM
                                 {groupings} g
                                 JOIN {groupings_groups} gg ON g.id = gg.groupingid
                                 JOIN {groups_members} gm ON gg.groupid = gm.groupid
                             WHERE
                                 g.courseid = ? AND gm.userid = ?', array($this->item->course, $userid));
                     $list = array();
                     foreach ($groupings as $grouping) {
                         $list[$grouping->gid] = true;
                     }
                     $CONDITIONLIB_PRIVATE->groupingscache[$this->item->course] = $list;
                 }
             }
             $usergroupings = $CONDITIONLIB_PRIVATE->groupingscache[$this->item->course];
             if ($usergroupings !== true && !array_key_exists($this->item->groupingid, $usergroupings)) {
                 $available = false;
                 $information .= get_string('groupingnoaccess', 'condition');
             }
         }
     }
     $information = trim($information);
     return $available;
 }