MDL-51746 accesslib: Describe instanceid in context_something::instance

With no PHPDocs and $instanceid as name some module developers confuse
context_module::instance argument to be instanceid of the module while
it actually should be cmid.
This commit is contained in:
Oleg Sychev 2015-10-20 22:31:10 +03:00 committed by Eloy Lafuente (stronk7)
parent 0ee52b8214
commit 87b2ed6c1f

View File

@ -6222,7 +6222,7 @@ class context_system extends context {
* Returns system context instance.
*
* @static
* @param int $instanceid
* @param int $instanceid should be 0
* @param int $strictness
* @param bool $cache
* @return context_system context instance
@ -6474,19 +6474,19 @@ class context_user extends context {
* Returns user context instance.
*
* @static
* @param int $instanceid
* @param int $userid id from {user} table
* @param int $strictness
* @return context_user context instance
*/
public static function instance($instanceid, $strictness = MUST_EXIST) {
public static function instance($userid, $strictness = MUST_EXIST) {
global $DB;
if ($context = context::cache_get(CONTEXT_USER, $instanceid)) {
if ($context = context::cache_get(CONTEXT_USER, $userid)) {
return $context;
}
if (!$record = $DB->get_record('context', array('contextlevel'=>CONTEXT_USER, 'instanceid'=>$instanceid))) {
if ($user = $DB->get_record('user', array('id'=>$instanceid, 'deleted'=>0), 'id', $strictness)) {
if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_USER, 'instanceid' => $userid))) {
if ($user = $DB->get_record('user', array('id' => $userid, 'deleted' => 0), 'id', $strictness)) {
$record = context::insert_context_record(CONTEXT_USER, $user->id, '/'.SYSCONTEXTID, 0);
}
}
@ -6652,19 +6652,19 @@ class context_coursecat extends context {
* Returns course category context instance.
*
* @static
* @param int $instanceid
* @param int $categoryid id from {course_categories} table
* @param int $strictness
* @return context_coursecat context instance
*/
public static function instance($instanceid, $strictness = MUST_EXIST) {
public static function instance($categoryid, $strictness = MUST_EXIST) {
global $DB;
if ($context = context::cache_get(CONTEXT_COURSECAT, $instanceid)) {
if ($context = context::cache_get(CONTEXT_COURSECAT, $categoryid)) {
return $context;
}
if (!$record = $DB->get_record('context', array('contextlevel'=>CONTEXT_COURSECAT, 'instanceid'=>$instanceid))) {
if ($category = $DB->get_record('course_categories', array('id'=>$instanceid), 'id,parent', $strictness)) {
if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_COURSECAT, 'instanceid' => $categoryid))) {
if ($category = $DB->get_record('course_categories', array('id' => $categoryid), 'id,parent', $strictness)) {
if ($category->parent) {
$parentcontext = context_coursecat::instance($category->parent);
$record = context::insert_context_record(CONTEXT_COURSECAT, $category->id, $parentcontext->path);
@ -6906,19 +6906,19 @@ class context_course extends context {
* Returns course context instance.
*
* @static
* @param int $instanceid
* @param int $courseid id from {course} table
* @param int $strictness
* @return context_course context instance
*/
public static function instance($instanceid, $strictness = MUST_EXIST) {
public static function instance($courseid, $strictness = MUST_EXIST) {
global $DB;
if ($context = context::cache_get(CONTEXT_COURSE, $instanceid)) {
if ($context = context::cache_get(CONTEXT_COURSE, $courseid)) {
return $context;
}
if (!$record = $DB->get_record('context', array('contextlevel'=>CONTEXT_COURSE, 'instanceid'=>$instanceid))) {
if ($course = $DB->get_record('course', array('id'=>$instanceid), 'id,category', $strictness)) {
if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_COURSE, 'instanceid' => $courseid))) {
if ($course = $DB->get_record('course', array('id' => $courseid), 'id,category', $strictness)) {
if ($course->category) {
$parentcontext = context_coursecat::instance($course->category);
$record = context::insert_context_record(CONTEXT_COURSE, $course->id, $parentcontext->path);
@ -7167,19 +7167,19 @@ class context_module extends context {
* Returns module context instance.
*
* @static
* @param int $instanceid
* @param int $cmid id of the record from {course_modules} table; pass cmid there, NOT id in the instance column
* @param int $strictness
* @return context_module context instance
*/
public static function instance($instanceid, $strictness = MUST_EXIST) {
public static function instance($cmid, $strictness = MUST_EXIST) {
global $DB;
if ($context = context::cache_get(CONTEXT_MODULE, $instanceid)) {
if ($context = context::cache_get(CONTEXT_MODULE, $cmid)) {
return $context;
}
if (!$record = $DB->get_record('context', array('contextlevel'=>CONTEXT_MODULE, 'instanceid'=>$instanceid))) {
if ($cm = $DB->get_record('course_modules', array('id'=>$instanceid), 'id,course', $strictness)) {
if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_MODULE, 'instanceid' => $cmid))) {
if ($cm = $DB->get_record('course_modules', array('id' => $cmid), 'id,course', $strictness)) {
$parentcontext = context_course::instance($cm->course);
$record = context::insert_context_record(CONTEXT_MODULE, $cm->id, $parentcontext->path);
}
@ -7379,19 +7379,19 @@ class context_block extends context {
* Returns block context instance.
*
* @static
* @param int $instanceid
* @param int $blockinstanceid id from {block_instances} table.
* @param int $strictness
* @return context_block context instance
*/
public static function instance($instanceid, $strictness = MUST_EXIST) {
public static function instance($blockinstanceid, $strictness = MUST_EXIST) {
global $DB;
if ($context = context::cache_get(CONTEXT_BLOCK, $instanceid)) {
if ($context = context::cache_get(CONTEXT_BLOCK, $blockinstanceid)) {
return $context;
}
if (!$record = $DB->get_record('context', array('contextlevel'=>CONTEXT_BLOCK, 'instanceid'=>$instanceid))) {
if ($bi = $DB->get_record('block_instances', array('id'=>$instanceid), 'id,parentcontextid', $strictness)) {
if (!$record = $DB->get_record('context', array('contextlevel' => CONTEXT_BLOCK, 'instanceid' => $blockinstanceid))) {
if ($bi = $DB->get_record('block_instances', array('id' => $blockinstanceid), 'id,parentcontextid', $strictness)) {
$parentcontext = context::instance_by_id($bi->parentcontextid);
$record = context::insert_context_record(CONTEXT_BLOCK, $bi->id, $parentcontext->path);
}