function test_create_group()
 {
     $this->assertTrue($this->groupid = groups_create_group($this->courseid));
     $this->assertTrue(groups_group_exists($this->groupid));
     $this->assertTrue(groups_group_belongs_to_course($this->groupid, $this->courseid));
     $this->assertTrue($groupids = groups_get_groups($this->courseid));
     //array...
     $this->assertTrue($groupinfo = groups_set_default_group_settings());
     $groupinfo->name = 'Group ' . $this->getLabel();
     //'Temporary Group Name'
     $this->assertTrue(groups_set_group_settings($this->groupid, $groupinfo));
     $this->assertTrue($groupinfo->name == groups_get_group_name($this->groupid));
     $this->assertTrue($this->courseid == groups_get_course($this->groupid));
 }
Beispiel #2
0
/**
 * Get the group object, including the course ID by default.
 * @param groupid ID of the group.
 * @param getcourse (default true), include the course ID in the return.
 * @return group object, optionally including 'courseid'.
 */
function groups_get_group($groupid, $getcourse = true)
{
    $group = groups_db_get_group_settings($groupid);
    if ($group && $getcourse) {
        $group->courseid = groups_get_course($groupid);
    }
    return $group;
}
Beispiel #3
0
/**
 * Get a complete user record, which includes all the info
 * in the user record
 * Intended for setting as $USER session variable
 *
 * @uses $CFG
 * @uses SITEID
 * @param string $field The user field to be checked for a given value.
 * @param string $value The value to match for $field.
 * @return user A {@link $USER} object.
 */
function get_complete_user_data($field, $value, $mnethostid = null)
{
    global $CFG;
    if (!$field || !$value) {
        return false;
    }
    /// Build the WHERE clause for an SQL query
    $constraints = $field . ' = \'' . $value . '\' AND deleted <> \'1\'';
    // If we are loading user data based on anything other than id,
    // we must also restrict our search based on mnet host.
    if ($field != 'id') {
        if (empty($mnethostid)) {
            // if empty, we restrict to local users
            $mnethostid = $CFG->mnet_localhost_id;
        }
    }
    if (!empty($mnethostid)) {
        $mnethostid = (int) $mnethostid;
        $constraints .= ' AND mnethostid = ' . $mnethostid;
    }
    /// Get all the basic user data
    if (!($user = get_record_select('user', $constraints))) {
        return false;
    }
    /// Get various settings and preferences
    if ($displays = get_records('course_display', 'userid', $user->id)) {
        foreach ($displays as $display) {
            $user->display[$display->course] = $display->display;
        }
    }
    $user->preference = get_user_preferences(null, null, $user->id);
    if ($lastaccesses = get_records('user_lastaccess', 'userid', $user->id)) {
        foreach ($lastaccesses as $lastaccess) {
            $user->lastcourseaccess[$lastaccess->courseid] = $lastaccess->timeaccess;
        }
    }
    if ($groupids = groups_get_all_groups_for_user($user->id)) {
        //TODO:check.
        foreach ($groupids as $groupid) {
            $courseid = groups_get_course($groupid);
            //change this to 2D array so we can put multiple groups in a course
            $user->groupmember[$courseid][] = $groupid;
        }
    }
    /// Rewrite some variables if necessary
    if (!empty($user->description)) {
        $user->description = true;
        // No need to cart all of it around
    }
    if ($user->username == 'guest') {
        $user->lang = $CFG->lang;
        // Guest language always same as site
        $user->firstname = get_string('guestuser');
        // Name always in current language
        $user->lastname = ' ';
    }
    $user->sesskey = random_string(10);
    $user->sessionIP = md5(getremoteaddr());
    // Store the current IP in the session
    return $user;
}
Beispiel #4
0
/**
 * This is a recursive function that checks whether the capability in this
 * context, or the parent capabilities are set to prohibit.
 *
 * At this point, we can probably just use the values already set in the
 * session variable, since we are going down the level. Any prohit set in
 * parents would already reflect in the session.
 *
 * @param $capability - capability name
 * @param $sum - sum of all capabilities values
 * @param $context - the context object
 * @param $array - when loading another user caps, their caps are not stored in session but an array
 */
function capability_prohibits($capability, $context, $sum = '', $array = '')
{
    global $USER;
    // caching, mainly to save unnecessary sqls
    static $prohibits;
    //[capability][contextid]
    if (isset($prohibits[$capability][$context->id])) {
        return $prohibits[$capability][$context->id];
    }
    if (empty($context->id)) {
        $prohibits[$capability][$context->id] = false;
        return false;
    }
    if (empty($capability)) {
        $prohibits[$capability][$context->id] = false;
        return false;
    }
    if ($sum < CAP_PROHIBIT / 2) {
        // If this capability is set to prohibit.
        $prohibits[$capability][$context->id] = true;
        return true;
    }
    if (!empty($array)) {
        if (isset($array[$context->id][$capability]) && $array[$context->id][$capability] < CAP_PROHIBIT / 2) {
            $prohibits[$capability][$context->id] = true;
            return true;
        }
    } else {
        // Else if set in session.
        if (isset($USER->capabilities[$context->id][$capability]) && $USER->capabilities[$context->id][$capability] < CAP_PROHIBIT / 2) {
            $prohibits[$capability][$context->id] = true;
            return true;
        }
    }
    switch ($context->contextlevel) {
        case CONTEXT_SYSTEM:
            // By now it's a definite an inherit.
            return 0;
            break;
        case CONTEXT_PERSONAL:
            $parent = get_context_instance(CONTEXT_SYSTEM);
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        case CONTEXT_USER:
            $parent = get_context_instance(CONTEXT_SYSTEM);
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        case CONTEXT_COURSECAT:
        case CONTEXT_COURSE:
            $parents = get_parent_cats($context);
            // cached internally
            // no workaround for recursion now - it needs some more work and maybe fixing
            if (empty($parents)) {
                // system context - this is either top category or frontpage course
                $parent = get_context_instance(CONTEXT_SYSTEM);
            } else {
                // parent context - recursion
                $parentid = array_pop($parents);
                $parent = get_context_instance_by_id($parentid);
            }
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        case CONTEXT_GROUP:
            // 1 to 1 to course.
            if (!($courseid = groups_get_course($context->instanceid))) {
                $prohibits[$capability][$context->id] = false;
                return false;
            }
            $parent = get_context_instance(CONTEXT_COURSE, $courseid);
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        case CONTEXT_MODULE:
            // 1 to 1 to course.
            if (!($cm = get_record('course_modules', 'id', $context->instanceid))) {
                $prohibits[$capability][$context->id] = false;
                return false;
            }
            $parent = get_context_instance(CONTEXT_COURSE, $cm->course);
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        case CONTEXT_BLOCK:
            // 1 to 1 to course.
            if (!($block = get_record('block_instance', 'id', $context->instanceid))) {
                $prohibits[$capability][$context->id] = false;
                return false;
            }
            if ($block->pagetype == 'course-view') {
                $parent = get_context_instance(CONTEXT_COURSE, $block->pageid);
                // needs check
            } else {
                $parent = get_context_instance(CONTEXT_SYSTEM);
            }
            $prohibits[$capability][$context->id] = capability_prohibits($capability, $parent);
            return $prohibits[$capability][$context->id];
            break;
        default:
            print_error('unknowncontext');
            return false;
    }
}