aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+github@gmail.com>2024-04-11 17:03:23 +0200
committerJan-Hendrik Willms <tleilax+github@gmail.com>2024-04-11 17:03:23 +0200
commit4bfb3ed2dab81d004901fe9c46b664470e3af3c7 (patch)
tree5676d5d05ea42d7730828612ae08a1dfb1501142
parent714438f8fe377d9889e93814109e012bb0662421 (diff)
add cache for the sidebar widget used in the administration of a course, re #3999biest-3999-cache-for-sidebar-widget
-rw-r--r--lib/classes/AdminCourseFilter.class.php59
1 files changed, 46 insertions, 13 deletions
diff --git a/lib/classes/AdminCourseFilter.class.php b/lib/classes/AdminCourseFilter.class.php
index da8ce54..0d88347 100644
--- a/lib/classes/AdminCourseFilter.class.php
+++ b/lib/classes/AdminCourseFilter.class.php
@@ -27,8 +27,10 @@
*/
class AdminCourseFilter
{
- static protected $instance = null;
- public $query = null;
+ private const WIDGET_CACHE_EXPIRATION = 1 * 60 * 60;
+
+ protected static $instance = null;
+ public ?SQLQuery $query = null;
public $max_show_courses = 500;
public $settings = [];
@@ -127,7 +129,7 @@ class AdminCourseFilter
if ($GLOBALS['user']->cfg->MY_COURSES_TYPE_FILTER && $GLOBALS['user']->cfg->MY_COURSES_TYPE_FILTER !== 'all') {
if (str_contains($GLOBALS['user']->cfg->MY_COURSES_TYPE_FILTER, '_')) {
- list($sem_class_id, $sem_type_id) = explode('_', $GLOBALS['user']->cfg->MY_COURSES_TYPE_FILTER);
+ [$sem_class_id, $sem_type_id] = explode('_', $GLOBALS['user']->cfg->MY_COURSES_TYPE_FILTER);
$this->query->where('course_type', 'seminare.status = :course_type', ['course_type' => $sem_type_id]);
} else {
//sem class
@@ -206,16 +208,47 @@ class AdminCourseFilter
*/
public function getCoursesForAdminWidget(string $order_by = 'name')
{
- $count_courses = $this->countCourses();
- $order = 'seminare.name';
- if ($order_by === 'number') {
- $order = 'seminare.veranstaltungsnummer, seminare.name';
- }
- if ($count_courses && $count_courses <= $this->max_show_courses) {
- $this->query->orderBy($order);
- return $this->getCourses();
+ $index = __METHOD__ . '::' . User::findCurrent()->id;
+
+ $cache = app(StudipCache::class);
+ $cached = $cache->read($index);
+ $hash = md5(serialize($this->query->settings));
+
+ if ($cached === false || $cached['hash'] !== $hash) {
+ $courses = [];
+
+ $count_courses = $this->countCourses();
+ if ($count_courses && $count_courses <= $this->max_show_courses) {
+ $courses = $this->getCourses();
+ }
+
+ $cache->write(
+ $index,
+ [
+ 'hash' => $hash,
+ 'courses' => array_map(
+ fn(Course $course): array => $course->toRawArray(),
+ $courses
+ ),
+ ],
+ self::WIDGET_CACHE_EXPIRATION
+ );
+ } else {
+ $courses = array_map(
+ fn(array $data): Course => Course::buildExisting($data),
+ $cached['courses']
+ );
}
- return [];
- }
+ usort($courses, function (Course $a, Course $b) use ($order_by): int {
+ if ($order_by === 'number') {
+ return strnatcasecmp($a->veranstaltungsnummer, $b->veranstaltungsnummer)
+ ?? strnatcasecmp($a->name, $b->name);
+ }
+
+ return strnatcasecmp($a->name, $b->name);
+ });
+
+ return $courses;
+ }
}