/**
 * Create a new context record for use by all roles-related stuff
 * assumes that the caller has done the homework.
 *
 * @param $level
 * @param $instanceid
 *
 * @return object newly created context
 */
function create_context($contextlevel, $instanceid)
{
    global $CFG;
    if ($contextlevel == CONTEXT_SYSTEM) {
        return create_system_context();
    }
    $context = new object();
    $context->contextlevel = $contextlevel;
    $context->instanceid = $instanceid;
    // Define $context->path based on the parent
    // context. In other words... Who is your daddy?
    $basepath = '/' . SYSCONTEXTID;
    $basedepth = 1;
    $result = true;
    switch ($contextlevel) {
        case CONTEXT_COURSECAT:
            $sql = "SELECT ctx.path, ctx.depth \n                    FROM {$CFG->prefix}context           ctx\n                    JOIN {$CFG->prefix}course_categories cc\n                      ON (cc.parent=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSECAT . ")\n                    WHERE cc.id={$instanceid}";
            if ($p = get_record_sql($sql)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($category = get_record('course_categories', 'id', $instanceid)) {
                    if (empty($category->parent)) {
                        // ok - this is a top category
                    } else {
                        if ($parent = get_context_instance(CONTEXT_COURSECAT, $category->parent)) {
                            $basepath = $parent->path;
                            $basedepth = $parent->depth;
                        } else {
                            // wrong parent category - no big deal, this can be fixed later
                            $basepath = null;
                            $basedepth = 0;
                        }
                    }
                } else {
                    // incorrect category id
                    $result = false;
                }
            }
            break;
        case CONTEXT_COURSE:
            $sql = "SELECT ctx.path, ctx.depth\n                    FROM {$CFG->prefix}context           ctx\n                    JOIN {$CFG->prefix}course            c\n                      ON (c.category=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSECAT . ")\n                    WHERE c.id={$instanceid} AND c.id !=" . SITEID;
            if ($p = get_record_sql($sql)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($course = get_record('course', 'id', $instanceid)) {
                    if ($course->id == SITEID) {
                        //ok - no parent category
                    } else {
                        if ($parent = get_context_instance(CONTEXT_COURSECAT, $course->category)) {
                            $basepath = $parent->path;
                            $basedepth = $parent->depth;
                        } else {
                            // wrong parent category of course - no big deal, this can be fixed later
                            $basepath = null;
                            $basedepth = 0;
                        }
                    }
                } else {
                    if ($instanceid == SITEID) {
                        // no errors for missing site course during installation
                        return false;
                    } else {
                        // incorrect course id
                        $result = false;
                    }
                }
            }
            break;
        case CONTEXT_MODULE:
            $sql = "SELECT ctx.path, ctx.depth\n                    FROM {$CFG->prefix}context           ctx\n                    JOIN {$CFG->prefix}course_modules    cm\n                      ON (cm.course=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSE . ")\n                    WHERE cm.id={$instanceid}";
            if ($p = get_record_sql($sql)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($cm = get_record('course_modules', 'id', $instanceid)) {
                    if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) {
                        $basepath = $parent->path;
                        $basedepth = $parent->depth;
                    } else {
                        // course does not exist - modules can not exist without a course
                        $result = false;
                    }
                } else {
                    // cm does not exist
                    $result = false;
                }
            }
            break;
        case CONTEXT_BLOCK:
            // Only non-pinned & course-page based
            $sql = "SELECT ctx.path, ctx.depth\n                    FROM {$CFG->prefix}context           ctx\n                    JOIN {$CFG->prefix}block_instance    bi\n                      ON (bi.pageid=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSE . ")\n                    WHERE bi.id={$instanceid} AND bi.pagetype='course-view'";
            if ($p = get_record_sql($sql)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($bi = get_record('block_instance', 'id', $instanceid)) {
                    if ($bi->pagetype != 'course-view') {
                        // ok - not a course block
                    } else {
                        if ($parent = get_context_instance(CONTEXT_COURSE, $bi->pageid)) {
                            $basepath = $parent->path;
                            $basedepth = $parent->depth;
                        } else {
                            // parent course does not exist - course blocks can not exist without a course
                            $result = false;
                        }
                    }
                } else {
                    // block does not exist
                    $result = false;
                }
            }
            break;
        case CONTEXT_USER:
            // default to basepath
            break;
    }
    // if grandparents unknown, maybe rebuild_context_path() will solve it later
    if ($basedepth != 0) {
        $context->depth = $basedepth + 1;
    }
    if ($result and $id = insert_record('context', $context)) {
        // can't set the full path till we know the id!
        if ($basedepth != 0 and !empty($basepath)) {
            set_field('context', 'path', $basepath . '/' . $id, 'id', $id);
        }
        return get_context_instance_by_id($id);
    } else {
        debugging('Error: could not insert new context level "' . s($contextlevel) . '", instance "' . s($instanceid) . '".');
        return false;
    }
}
示例#2
0
/**
 * Create a new context record for use by all roles-related stuff
 * assumes that the caller has done the homework.
 *
 * @param $level
 * @param $instanceid
 *
 * @return object newly created context
 */
function create_context($contextlevel, $instanceid)
{
    global $CFG, $DB;
    if ($contextlevel == CONTEXT_SYSTEM) {
        return create_system_context();
    }
    $context = new object();
    $context->contextlevel = $contextlevel;
    $context->instanceid = $instanceid;
    // Define $context->path based on the parent
    // context. In other words... Who is your daddy?
    $basepath = '/' . SYSCONTEXTID;
    $basedepth = 1;
    $result = true;
    $error_message = null;
    switch ($contextlevel) {
        case CONTEXT_COURSECAT:
            $sql = "SELECT ctx.path, ctx.depth\n                      FROM {context}           ctx\n                      JOIN {course_categories} cc\n                           ON (cc.parent=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSECAT . ")\n                     WHERE cc.id=?";
            $params = array($instanceid);
            if ($p = $DB->get_record_sql($sql, $params)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($category = $DB->get_record('course_categories', array('id' => $instanceid))) {
                    if (empty($category->parent)) {
                        // ok - this is a top category
                    } else {
                        if ($parent = get_context_instance(CONTEXT_COURSECAT, $category->parent)) {
                            $basepath = $parent->path;
                            $basedepth = $parent->depth;
                        } else {
                            // wrong parent category - no big deal, this can be fixed later
                            $basepath = null;
                            $basedepth = 0;
                        }
                    }
                } else {
                    // incorrect category id
                    $error_message = "incorrect course category id ({$instanceid})";
                    $result = false;
                }
            }
            break;
        case CONTEXT_COURSE:
            $sql = "SELECT ctx.path, ctx.depth\n                      FROM {context} ctx\n                      JOIN {course}  c\n                           ON (c.category=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSECAT . ")\n                     WHERE c.id=? AND c.id !=" . SITEID;
            $params = array($instanceid);
            if ($p = $DB->get_record_sql($sql, $params)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($course = $DB->get_record('course', array('id' => $instanceid))) {
                    if ($course->id == SITEID) {
                        //ok - no parent category
                    } else {
                        if ($parent = get_context_instance(CONTEXT_COURSECAT, $course->category)) {
                            $basepath = $parent->path;
                            $basedepth = $parent->depth;
                        } else {
                            // wrong parent category of course - no big deal, this can be fixed later
                            $basepath = null;
                            $basedepth = 0;
                        }
                    }
                } else {
                    if ($instanceid == SITEID) {
                        // no errors for missing site course during installation
                        return false;
                    } else {
                        // incorrect course id
                        $error_message = "incorrect course id ({$instanceid})";
                        $result = false;
                    }
                }
            }
            break;
        case CONTEXT_MODULE:
            $sql = "SELECT ctx.path, ctx.depth\n                      FROM {context}        ctx\n                      JOIN {course_modules} cm\n                           ON (cm.course=ctx.instanceid AND ctx.contextlevel=" . CONTEXT_COURSE . ")\n                     WHERE cm.id=?";
            $params = array($instanceid);
            if ($p = $DB->get_record_sql($sql, $params)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                if ($cm = $DB->get_record('course_modules', array('id' => $instanceid))) {
                    if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) {
                        $basepath = $parent->path;
                        $basedepth = $parent->depth;
                    } else {
                        // course does not exist - modules can not exist without a course
                        $error_message = "course does not exist ({$cm->course}) - modules can not exist without a course";
                        $result = false;
                    }
                } else {
                    // cm does not exist
                    $error_message = "cm with id {$instanceid} does not exist";
                    $result = false;
                }
            }
            break;
        case CONTEXT_BLOCK:
            // Only non-pinned & course-page based
            $sql = "SELECT ctx.path, ctx.depth\n                      FROM {context} ctx\n                      JOIN {block_instances} bi ON (bi.contextid=ctx.id)\n                     WHERE bi.id=? AND ctx.contextlevel=?";
            $params = array($instanceid, CONTEXT_COURSE);
            if ($p = $DB->get_record_sql($sql, $params)) {
                $basepath = $p->path;
                $basedepth = $p->depth;
            } else {
                // block does not exist
                $error_message = 'block or parent context does not exist';
                $result = false;
            }
            break;
        case CONTEXT_USER:
            // default to basepath
            break;
    }
    // if grandparents unknown, maybe rebuild_context_path() will solve it later
    if ($basedepth != 0) {
        $context->depth = $basedepth + 1;
    }
    if ($result and $id = $DB->insert_record('context', $context)) {
        // can't set the full path till we know the id!
        if ($basedepth != 0 and !empty($basepath)) {
            $DB->set_field('context', 'path', $basepath . '/' . $id, array('id' => $id));
        }
        return get_context_instance_by_id($id);
    } else {
        debugging('Error: could not insert new context level "' . s($contextlevel) . '", instance "' . s($instanceid) . '". ' . $error_message);
        return false;
    }
}
示例#3
0
/**
 * Create a new context record for use by all roles-related stuff
 * @param $level
 * @param $instanceid
 *
 * @return object newly created context (or existing one with a debug warning)
 */
function create_context($contextlevel, $instanceid)
{
    if (!($context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instanceid))) {
        if (!validate_context($contextlevel, $instanceid)) {
            debugging('Error: Invalid context creation request for level "' . s($contextlevel) . '", instance "' . s($instanceid) . '".');
            return NULL;
        }
        if ($contextlevel == CONTEXT_SYSTEM) {
            return create_system_context();
        }
        $context = new object();
        $context->contextlevel = $contextlevel;
        $context->instanceid = $instanceid;
        if ($id = insert_record('context', $context)) {
            // we need to populate context_rel for every new context inserted
            $c = get_record('context', 'id', $id);
            insert_context_rel($c);
            return $c;
        } else {
            debugging('Error: could not insert new context level "' . s($contextlevel) . '", instance "' . s($instanceid) . '".');
            return NULL;
        }
    } else {
        debugging('Warning: Context id "' . s($context->id) . '" not created, because it already exists.');
        return $context;
    }
}