/**
  * Determines whether a particular item is currently available
  * according to this availability condition.
  *
  * @param bool $not Set true if we are inverting the condition.
  * @param info $info Item we're checking.
  * @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 User ID to check availability for.
  * @return bool True if available.
  */
 public function is_available($not, \core_availability\info $info, $grabthelot, $userid)
 {
     $available = false;
     if ($this->requiredlvl > 0) {
         $currentlvl = $this->get_user_level($info->get_course()->id, $userid);
         if ($currentlvl >= $this->requiredlvl) {
             $available = true;
         } else {
             $available = false;
         }
         if ($not) {
             $available = !$available;
         }
     }
     return $available;
 }
 public function is_available($not, \core_availability\info $info, $grabthelot, $userid)
 {
     global $PAGE, $CFG;
     require_once $CFG->dirroot . '/enrol/locallib.php';
     $course = $info->get_course();
     $enrolmanager = new \course_enrolment_manager($PAGE, $course);
     $allow = true;
     if (!($enrolments = $enrolmanager->get_user_enrolments($userid))) {
         $allow = false;
     }
     foreach ($enrolments as $enrol) {
         if (time() - $enrol->timestart < $this->mintimesinceenrol) {
             $allow = false;
         }
     }
     if (!$not) {
         $allow = !$allow;
     }
     return $allow;
 }
Example #3
0
 public function get_description($full, $not, \core_availability\info $info)
 {
     $course = $info->get_course();
     // Display the fieldname into current lang.
     if ($this->customfield) {
         // Is a custom profile field (will use multilang).
         $customfields = self::get_custom_profile_fields();
         if (array_key_exists($this->customfield, $customfields)) {
             $translatedfieldname = $customfields[$this->customfield]->name;
         } else {
             $translatedfieldname = get_string('missing', 'availability_profile', $this->customfield);
         }
     } else {
         $translatedfieldname = get_user_field_name($this->standardfield);
     }
     $context = \context_course::instance($course->id);
     $a = new \stdClass();
     $a->field = format_string($translatedfieldname, true, array('context' => $context));
     $a->value = s($this->value);
     if ($not) {
         // When doing NOT strings, we replace the operator with its inverse.
         // Some of them don't have inverses, so for those we use a new
         // identifier which is only used for this lang string.
         switch ($this->operator) {
             case self::OP_CONTAINS:
                 $opname = self::OP_DOES_NOT_CONTAIN;
                 break;
             case self::OP_DOES_NOT_CONTAIN:
                 $opname = self::OP_CONTAINS;
                 break;
             case self::OP_ENDS_WITH:
                 $opname = 'notendswith';
                 break;
             case self::OP_IS_EMPTY:
                 $opname = self::OP_IS_NOT_EMPTY;
                 break;
             case self::OP_IS_EQUAL_TO:
                 $opname = 'notisequalto';
                 break;
             case self::OP_IS_NOT_EMPTY:
                 $opname = self::OP_IS_EMPTY;
                 break;
             case self::OP_STARTS_WITH:
                 $opname = 'notstartswith';
                 break;
             default:
                 throw new \coding_exception('Unexpected operator: ' . $this->operator);
         }
     } else {
         $opname = $this->operator;
     }
     return get_string('requires_' . $opname, 'availability_profile', $a);
 }
 public function get_description($full, $not, \core_availability\info $info)
 {
     $course = $info->get_course();
     // String depends on type of requirement. We are coy about
     // the actual numbers, in case grades aren't released to
     // students.
     if (is_null($this->min) && is_null($this->max)) {
         $string = 'any';
     } else {
         if (is_null($this->max)) {
             $string = 'min';
         } else {
             if (is_null($this->min)) {
                 $string = 'max';
             } else {
                 $string = 'range';
             }
         }
     }
     if ($not) {
         // The specific strings don't make as much sense with 'not'.
         if ($string === 'any') {
             $string = 'notany';
         } else {
             $string = 'notgeneral';
         }
     }
     $name = self::get_cached_grade_name($course->id, $this->gradeitemid);
     return get_string('requires_' . $string, 'availability_grade', $name);
 }
Example #5
0
 public function get_description($full, $not, \core_availability\info $info)
 {
     global $DB;
     $course = $info->get_course();
     // Need to get the name for the grouping. Unfortunately this requires
     // a database query. To save queries, get all groupings for course at
     // once in a static cache.
     $groupingid = $this->get_grouping_id($info);
     if (!array_key_exists($groupingid, self::$groupingnames)) {
         $coursegroupings = $DB->get_records('groupings', array('courseid' => $course->id), '', 'id, name');
         foreach ($coursegroupings as $rec) {
             self::$groupingnames[$rec->id] = $rec->name;
         }
     }
     // If it still doesn't exist, it must have been misplaced.
     if (!array_key_exists($groupingid, self::$groupingnames)) {
         $name = get_string('missing', 'availability_grouping');
     } else {
         $context = \context_course::instance($course->id);
         $name = format_string(self::$groupingnames[$groupingid], true, array('context' => $context));
     }
     return get_string($not ? 'requires_notgrouping' : 'requires_grouping', 'availability_grouping', $name);
 }
Example #6
0
 public function get_user_list_sql($not, \core_availability\info $info, $onlyactive)
 {
     global $DB;
     // Get enrolled users with access all groups. These always are allowed.
     list($aagsql, $aagparams) = get_enrolled_sql($info->get_context(), 'moodle/site:accessallgroups', 0, $onlyactive);
     // Get all enrolled users.
     list($enrolsql, $enrolparams) = get_enrolled_sql($info->get_context(), '', 0, $onlyactive);
     // Condition for specified or any group.
     $matchparams = array();
     if ($this->groupid) {
         $matchsql = "SELECT 1\n                           FROM {groups_members} gm\n                          WHERE gm.userid = userids.id\n                                AND gm.groupid = " . self::unique_sql_parameter($matchparams, $this->groupid);
     } else {
         $matchsql = "SELECT 1\n                           FROM {groups_members} gm\n                           JOIN {groups} g ON g.id = gm.groupid\n                          WHERE gm.userid = userids.id\n                                AND g.courseid = " . self::unique_sql_parameter($matchparams, $info->get_course()->id);
     }
     // Overall query combines all this.
     $condition = $not ? 'NOT' : '';
     $sql = "SELECT userids.id\n                  FROM ({$enrolsql}) userids\n                 WHERE (userids.id IN ({$aagsql})) OR {$condition} EXISTS ({$matchsql})";
     return array($sql, array_merge($enrolparams, $aagparams, $matchparams));
 }
Example #7
0
 public function filter_user_list(array $users, $not, \core_availability\info $info, \core_availability\capability_checker $checker)
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/grouplib.php';
     $course = $info->get_course();
     // List users for this course who match the condition.
     if ($this->groupid) {
         $groupusers = groups_get_members($this->groupid, 'u.id', 'u.id ASC');
     } else {
         $groupusers = $DB->get_records_sql("\n                    SELECT DISTINCT gm.userid\n                      FROM {groups} g\n                      JOIN {groups_members} gm ON gm.groupid = g.id\n                     WHERE g.courseid = ?", array($course->id));
     }
     // List users who have access all groups.
     $aagusers = $checker->get_users_by_capability('moodle/site:accessallgroups');
     // Filter the user list.
     $result = array();
     foreach ($users as $id => $user) {
         // Always include users with access all groups.
         if (array_key_exists($id, $aagusers)) {
             $result[$id] = $user;
             continue;
         }
         // Other users are included or not based on group membership.
         $allow = array_key_exists($id, $groupusers);
         if ($not) {
             $allow = !$allow;
         }
         if ($allow) {
             $result[$id] = $user;
         }
     }
     return $result;
 }
 public function get_description($full, $not, \core_availability\info $info)
 {
     global $DB;
     $context = \context_course::instance($info->get_course()->id);
     $role = $DB->get_record('role', array('id' => $this->roleid));
     if (!$role) {
         $missing = get_string('missing', 'availability_role');
         return get_string($not ? 'requires_notrole' : 'requires_role', 'availability_role', $missing);
     } else {
         $name = role_get_name($role, $context);
         return get_string($not ? 'requires_notrole' : 'requires_role', 'availability_role', $name);
     }
 }