From 5215d710826dbc0f6bfdc6c94376f1cf1fe1b949 Mon Sep 17 00:00:00 2001 From: PMKuipers Date: Fri, 30 Jun 2023 13:12:19 +0200 Subject: [PATCH] Added cache for list of teacher's studyplans. --- classes/studentstudyplanservice.php | 5 +- classes/studyplan.php | 41 ------------- classes/task/refreshteacherlist.php | 33 +++++++++++ classes/teachingfinder.php | 90 +++++++++++++++++++++++++++++ db/install.xml | 13 ++++- db/tasks.php | 10 ++++ db/upgrade.php | 22 +++++++ lang/en/local_treestudyplan.php | 2 +- lang/nl/local_treestudyplan.php | 1 + version.php | 2 +- 10 files changed, 173 insertions(+), 46 deletions(-) create mode 100644 classes/task/refreshteacherlist.php create mode 100644 classes/teachingfinder.php diff --git a/classes/studentstudyplanservice.php b/classes/studentstudyplanservice.php index d816643..a486643 100644 --- a/classes/studentstudyplanservice.php +++ b/classes/studentstudyplanservice.php @@ -252,8 +252,9 @@ class studentstudyplanservice extends \external_api { global $CFG, $DB, $USER; $userid = $USER->id; - - $studyplans = studyplan::find_teaching($userid); + + \external_api::validate_context(\context_system::instance()); + $studyplans = teachingfinder::list_my_plans(); if(isset($id) && $id > 0){ if(isset($studyplans[$id])){ diff --git a/classes/studyplan.php b/classes/studyplan.php index 1daedf2..3f20c41 100644 --- a/classes/studyplan.php +++ b/classes/studyplan.php @@ -316,47 +316,6 @@ class studyplan { } - /** - * Find The active studyplans where the specified user is a teacher - * (Has the mod/assign::grade capability in one of the linked courses) - * TODO: OPTIMIZE THIS CHECK!!! - */ - public static function find_teaching($userid){ - global $DB; - $list = []; - - // First find all active study plans - - $sql = "SELECT s.id FROM {local_treestudyplan} s - WHERE startdate <= NOW() and enddate >= NOW()"; - $plan_ids = $DB->get_fieldset_sql($sql, []); - - foreach($plan_ids as $planid) { - $sql = "SELECT i.course_id FROM mdl_local_treestudyplan_item i - INNER JOIN mdl_local_treestudyplan_line l ON i.line_id = l.id - WHERE l.studyplan_id = :plan_id AND i.course_id IS NOT NULL"; - $course_ids = $DB->get_fieldset_sql($sql, ["plan_id" => $planid]); - - $linked = false; - foreach($course_ids as $cid){ - $coursecontext = \context_course::instance($cid); - if (is_enrolled($coursecontext, $userid, 'mod/assign:grade')){ - $linked = true; - break; // No need to search further - } - } - - if($linked) - { - $list[$planid] = self::findById($planid); - } - - } - - return $list; - } - - static public function exist_for_user($userid) { global $DB; diff --git a/classes/task/refreshteacherlist.php b/classes/task/refreshteacherlist.php new file mode 100644 index 0000000..f258e33 --- /dev/null +++ b/classes/task/refreshteacherlist.php @@ -0,0 +1,33 @@ +dirroot.'/course/externallib.php'); +use local_treestudyplan\teachingfinder; + +class refreshteacherlist extends \core\task\scheduled_task { + const CACHE_TIME = 4 * 60 * 60; // 2 hours + + /** + * Return the task's name as shown in admin screens. + * + * @return string + */ + public function get_name() { + return get_string('refreshteacherlist_name', 'local_treestudyplan'); + } + + /** + * Execute the task. + */ + public function execute() { + \mtrace("Ververs lijst met leraren"); + $teacher_ids = teachingfinder::list_teacher_ids(); + foreach($teacher_ids as $tid){ + $utime = teachingfinder::get_update_time($tid); + if(time() - $utime > self::CACHE_TIME){ + \mtrace("Teacher {$tid} is due for a refresh of the list"); + teachingfinder::update_teaching_cache($tid); + } + } + + } +} \ No newline at end of file diff --git a/classes/teachingfinder.php b/classes/teachingfinder.php new file mode 100644 index 0000000..e3827e6 --- /dev/null +++ b/classes/teachingfinder.php @@ -0,0 +1,90 @@ +id; + + $records = $DB->get_records(self::TABLE,['teacher_id' => $userid]); + if(count($records) == 0){ + // initiate a search if the cache is empty + self::update_teaching_cache($userid); + $DB->get_records(self::TABLE,['teacher_id' => $userid]); + } + $list = []; + foreach($records as $r){ + $list[] = studyplan::findById($r->studyplan_id); + } + return $list; + } + + + /** + * Find The active studyplans where the specified user is a teacher + * (Has the mod/assign::grade capability in one of the linked courses) + * TODO: OPTIMIZE THIS CHECK!!! + */ + public static function update_teaching_cache($userid){ + global $DB; + $list = []; + + // First find all active study plans + + $sql = "SELECT s.id FROM {local_treestudyplan} s + WHERE startdate <= NOW() and enddate >= NOW()"; + $plan_ids = $DB->get_fieldset_sql($sql, []); + + // then parse them to see if the user has the grading permission in any of them + // (Which would make them a teacher for all intents and purposes) + + foreach($plan_ids as $planid) { + $sql = "SELECT i.course_id FROM mdl_local_treestudyplan_item i + INNER JOIN mdl_local_treestudyplan_line l ON i.line_id = l.id + WHERE l.studyplan_id = :plan_id AND i.course_id IS NOT NULL"; + $course_ids = $DB->get_fieldset_sql($sql, ["plan_id" => $planid]); + + $linked = false; + foreach($course_ids as $cid){ + $coursecontext = \context_course::instance($cid); + if (is_enrolled($coursecontext, $userid, 'mod/assign:grade')){ + $linked = true; + break; // No need to search further + } + } + + if($linked) + { + $list[] = $planid; + } + + } + + // Now, clear the database of all records for this user + $DB->delete_records(self::TABLE,["teacher_id"=>$userid]); + // And add new records for the found studyplans + $now = time(); + foreach($list as $planid){ + $DB->insert_record(self::TABLE,["teacher_id"=>$userid,"studyplan_id"=>$planid,"update_time"=>$now]); + } + + return $list; + } + + public static function list_teacher_ids(){ + global $DB; + return $DB->get_fieldset_sql("SELECT DISTINCT teacher_id FROM {".self::TABLE."}"); + } + + public static function get_update_time($teacher_id): int { + global $DB; + $r = $DB->get_field_sql("SELECT MIN(update_time)FROM {".self::TABLE."} WHERE teacher_id=:teacher_id", + ["teacher_id" => $teacher_id]); + return (int)($r->update_time); + } + + +} \ No newline at end of file diff --git a/db/install.xml b/db/install.xml index ab9a9d9..622bd0b 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -136,5 +136,16 @@ + + + + + + + + + + +
diff --git a/db/tasks.php b/db/tasks.php index 4c98609..e6e460f 100644 --- a/db/tasks.php +++ b/db/tasks.php @@ -9,4 +9,14 @@ $tasks = [ 'month' => '*', 'dayofweek' => '*', ], + [ + 'classname' => 'local_treestudyplan\task\refreshteacherlist', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => '*', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*', + ], + ]; \ No newline at end of file diff --git a/db/upgrade.php b/db/upgrade.php index ed7d1b2..3c8cd16 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -197,6 +197,28 @@ function xmldb_local_treestudyplan_upgrade($oldversion) { // Treestudyplan savepoint reached. upgrade_plugin_savepoint(true, 2023062603, 'local', 'treestudyplan'); } + if ($oldversion < 2023063002) { + + // Define table local_treestudyplan_teachers to be created. + $table = new xmldb_table('local_treestudyplan_teachers'); + + // Adding fields to table local_treestudyplan_teachers. + $table->add_field('id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('teacher_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('studyplan_id', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('update_time', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + + // Adding keys to table local_treestudyplan_teachers. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + + // Conditionally launch create table for local_treestudyplan_teachers. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Treestudyplan savepoint reached. + upgrade_plugin_savepoint(true, 2023063002, 'local', 'treestudyplan'); + } return true; } \ No newline at end of file diff --git a/lang/en/local_treestudyplan.php b/lang/en/local_treestudyplan.php index 58003a4..52366cf 100644 --- a/lang/en/local_treestudyplan.php +++ b/lang/en/local_treestudyplan.php @@ -72,7 +72,7 @@ $string['settingdesc_csync_remember_manual_csync_field'] = 'Mark cohort syncs th $string['setting_csync_users_field'] = 'Enrol linked users'; $string['settingdesc_csync_users_field'] = 'Also enrol all users that are explicitly linked to a studyplan in that studyplan\'s courses.'; $string['autocohortsync_name'] = 'Studyplan automatic cohort sync cascading'; - +$string['refreshteacherlist_name'] = "Refresh teacher's studyplan list"; $string['studyplan_add'] = 'Add study plan'; $string['studyplan_edit'] = 'Edit study plan'; diff --git a/lang/nl/local_treestudyplan.php b/lang/nl/local_treestudyplan.php index 5c5f9bc..9be2f36 100644 --- a/lang/nl/local_treestudyplan.php +++ b/lang/nl/local_treestudyplan.php @@ -74,6 +74,7 @@ $string['settingdesc_csync_remember_manual_csync_field'] = 'Markeer site-group s $string['setting_csync_users_field'] = 'Gekoppelde gebruikers automatisch inchrijven'; $string['settingdesc_csync_users_field'] = 'Ook alle gebruikers die expliciet aan een studieplan gekoppeld zijn inschrijven in de cursussen van dat studieplan.'; $string['autocohortsync_name'] = 'Studyplan automatisch site-group synchronisatie doorzetten'; +$string['refreshteacherlist_name'] = "Ververs lijst van studieplannen voor leraar"; $string['studyplan_add'] = 'Nieuw studieplan'; $string['studyplan_edit'] = 'Studieplan bewerken'; diff --git a/version.php b/version.php index d674ad2..956b0ec 100644 --- a/version.php +++ b/version.php @@ -1,6 +1,6 @@ component = 'local_treestudyplan'; // Recommended since 2.0.2 (MDL-26035). Required since 3.0 (MDL-48494) -$plugin->version = 2023063000; // YYYYMMDDHH (year, month, day, iteration) +$plugin->version = 2023063004; // YYYYMMDDHH (year, month, day, iteration) $plugin->requires = 2021051700; // YYYYMMDDHH (This is the release version for Moodle 3.11) $plugin->dependencies = [