Added cache for list of teacher's studyplans.
This commit is contained in:
parent
1aafb55c78
commit
5215d71082
|
@ -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])){
|
||||
|
|
|
@ -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;
|
||||
|
|
33
classes/task/refreshteacherlist.php
Normal file
33
classes/task/refreshteacherlist.php
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
namespace local_treestudyplan\task;
|
||||
require_once($CFG->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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
90
classes/teachingfinder.php
Normal file
90
classes/teachingfinder.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
namespace local_treestudyplan;
|
||||
|
||||
class teachingfinder {
|
||||
const TABLE = "local_treestudyplan_teachers";
|
||||
|
||||
|
||||
public static function list_my_plans(){
|
||||
global $USER,$DB;
|
||||
$userid = $USER->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);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<XMLDB PATH="local/treestudyplan/db" VERSION="20230626" COMMENT="XMLDB file for Moodle local/treestudyplan"
|
||||
<XMLDB PATH="local/treestudyplan/db" VERSION="20230630" COMMENT="XMLDB file for Moodle local/treestudyplan"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
|
||||
>
|
||||
|
@ -136,5 +136,16 @@
|
|||
<KEY NAME="scale_id-id" TYPE="foreign" FIELDS="scale_id" REFTABLE="scale" REFFIELDS="id"/>
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
<TABLE NAME="local_treestudyplan_teachers" COMMENT="Caches the links between teachers and studyplans">
|
||||
<FIELDS>
|
||||
<FIELD NAME="id" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="true"/>
|
||||
<FIELD NAME="teacher_id" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
<FIELD NAME="studyplan_id" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
<FIELD NAME="update_time" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
|
||||
</KEYS>
|
||||
</TABLE>
|
||||
</TABLES>
|
||||
</XMLDB>
|
||||
|
|
10
db/tasks.php
10
db/tasks.php
|
@ -9,4 +9,14 @@ $tasks = [
|
|||
'month' => '*',
|
||||
'dayofweek' => '*',
|
||||
],
|
||||
[
|
||||
'classname' => 'local_treestudyplan\task\refreshteacherlist',
|
||||
'blocking' => 0,
|
||||
'minute' => 'R',
|
||||
'hour' => '*',
|
||||
'day' => '*',
|
||||
'month' => '*',
|
||||
'dayofweek' => '*',
|
||||
],
|
||||
|
||||
];
|
|
@ -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;
|
||||
}
|
|
@ -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';
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
$plugin->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 = [
|
||||
|
|
Loading…
Reference in New Issue
Block a user