MDL-35668 filebrowser: cache enrolled courses

This commit is contained in:
Marina Glancy 2017-08-17 14:31:55 +08:00
parent 2d939622b8
commit ade66228fa
3 changed files with 22 additions and 2 deletions

View File

@ -60,6 +60,9 @@ require_once("$CFG->libdir/filebrowser/file_info_context_module.php");
*/
class file_browser {
/** @var array cached list of enrolled courses. */
protected $enrolledcourses = null;
/**
* Looks up file_info instance
*
@ -234,4 +237,21 @@ class file_browser {
return $level->get_file_info($component, $filearea, $itemid, $filepath, $filename);
}
/**
* Check if user is enrolled into the course
*
* This function keeps a cache of enrolled courses because it may be called multiple times for many courses in one request
*
* @param int $courseid
* @return bool
*/
public function is_enrolled($courseid) {
if ($this->enrolledcourses === null || PHPUNIT_TEST) {
// Since get_file_browser() returns a statically cached object we can't rely on cache
// inside the file_browser class in the unittests.
// TODO MDL-59964 remove this caching when it's implemented inside enrol_get_my_courses().
$this->enrolledcourses = enrol_get_my_courses(['id']);
}
return array_key_exists($courseid, $this->enrolledcourses);
}
}

View File

@ -68,7 +68,7 @@ class file_info_context_course extends file_info {
return null;
}
if (!is_viewing($this->context) and !is_enrolled($this->context)) {
if (!is_viewing($this->context) and !$this->browser->is_enrolled($this->course->id)) {
// no peaking here if not enrolled or inspector
return null;
}

View File

@ -99,7 +99,7 @@ class file_info_context_module extends file_info {
return null;
}
if (!is_viewing($this->context) and !is_enrolled($this->context)) {
if (!is_viewing($this->context) and !$this->browser->is_enrolled($this->course->id)) {
// no peaking here if not enrolled or inspector
return null;
}