/** * Rebuild context paths and depths at block context level. * * @static * @param $force */ protected static function build_paths($force) { global $DB; if ($force or $DB->record_exists_select('context', "contextlevel = " . CONTEXT_BLOCK . " AND (depth = 0 OR path IS NULL)")) { if ($force) { $ctxemptyclause = ''; } else { $ctxemptyclause = "AND (ctx.path IS NULL OR ctx.depth = 0)"; } // pctx.path IS NOT NULL prevents fatal problems with broken block instances that point to invalid context parent $sql = "INSERT INTO {context_temp} (id, path, depth)\n SELECT ctx.id, " . $DB->sql_concat('pctx.path', "'/'", 'ctx.id') . ", pctx.depth+1\n FROM {context} ctx\n JOIN {block_instances} bi ON (bi.id = ctx.instanceid AND ctx.contextlevel = " . CONTEXT_BLOCK . ")\n JOIN {context} pctx ON (pctx.id = bi.parentcontextid)\n WHERE (pctx.path IS NOT NULL AND pctx.depth > 0)\n {$ctxemptyclause}"; $trans = $DB->start_delegated_transaction(); $DB->delete_records('context_temp'); $DB->execute($sql); context::merge_context_temp_table(); $DB->delete_records('context_temp'); $trans->allow_commit(); } }
/** * Rebuild context paths and depths at course category context level. * * @static * @param bool $force */ protected static function build_paths($force) { global $DB; if ($force or $DB->record_exists_select('context', "contextlevel = " . CONTEXT_COSTCENTER . " AND (depth = 0 OR path IS NULL)")) { if ($force) { $ctxemptyclause = $emptyclause = ''; } else { $ctxemptyclause = "AND (ctx.path IS NULL OR ctx.depth = 0)"; $emptyclause = "AND ({context}.path IS NULL OR {context}.depth = 0)"; } $base = '/' . SYSCONTEXTID; // Normal top level categories $sql = "UPDATE {context} SET depth=2, path=" . $DB->sql_concat("'$base/'", 'id') . " WHERE contextlevel=" . CONTEXT_COSTCENTER . " AND EXISTS (SELECT 'x' FROM {local_costcenter} cc WHERE cc.id = {context}.instanceid AND cc.depth=1) $emptyclause"; $DB->execute($sql); // Deeper categories - one query per depthlevel $maxdepth = $DB->get_field_sql("SELECT MAX(depth) FROM {local_costcenter}"); for ($n = 2; $n <= $maxdepth; $n++) { $sql = "INSERT INTO {context_temp} (id, path, depth) SELECT ctx.id, " . $DB->sql_concat('pctx.path', "'/'", 'ctx.id') . ", pctx.depth+1 FROM {context} ctx JOIN {local_costcenter} cc ON (cc.id = ctx.instanceid AND ctx.contextlevel = " . CONTEXT_COSTCENTER . " AND cc.depth = $n) JOIN {context} pctx ON (pctx.instanceid = cc.parent AND pctx.contextlevel = " . CONTEXT_COSTCENTER . ") WHERE pctx.path IS NOT NULL AND pctx.depth > 0 $ctxemptyclause"; $trans = $DB->start_delegated_transaction(); $DB->delete_records('context_temp'); $DB->execute($sql); context::merge_context_temp_table(); $DB->delete_records('context_temp'); $trans->allow_commit(); } } }