Skip to content

Commit dc4c1ef

Browse files
committed
User cache
1 parent 34afa50 commit dc4c1ef

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

classes/models/user.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
class user extends table_base implements allocatable, moderatable {
5252
use allocatable_functions;
5353

54+
/**
55+
* Cache area where objects by ID are stored.
56+
* @var string
57+
*/
58+
const CACHE_AREA_IDS = 'userids';
59+
5460
/**
5561
* @var string
5662
*/
@@ -67,6 +73,23 @@ public function __construct($data = false) {
6773
parent::__construct($data);
6874
}
6975

76+
77+
/**
78+
* Overrride from parent.
79+
* Get child object from its ID, from the database.
80+
* @param int $id
81+
* @return \stdClass|null
82+
*/
83+
protected static function get_db_record_from_id(int $id, int $strictness): ?object {
84+
// In the case of user we override this so we don't get all DB fields, to avoid caching unnecessary data incl user hashed password.
85+
$userfields = 'id,email,suspended' . implode(',', \core_user\fields::get_name_fields());
86+
$user = \core_user::get_user($id, $userfields);
87+
if (!$user || $user->deleted) {
88+
return null;
89+
}
90+
return $user;
91+
}
92+
7093
/**
7194
* Get the user's full name.
7295
* @return string
@@ -88,7 +111,8 @@ public function name(): string {
88111
return core_user::get_fullname($data);
89112
}
90113

91-
return core_user::get_fullname($this->get_raw_record());
114+
$record = self::get_db_record_from_id($this->id, IGNORE_MISSING);
115+
return $record ? core_user::get_fullname($record) : '';
92116
}
93117

94118
/**

classes/observer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use core\event\group_member_removed;
2727
use core\event\role_assigned;
2828
use core\event\role_unassigned;
29+
use mod_coursework\models\user;
2930

3031
defined('MOODLE_INTERNAL') || die();
3132
require_once($CFG->dirroot . '/mod/coursework/lib.php');
@@ -81,4 +82,15 @@ public static function process_allocations_when_group_member_removed(group_membe
8182
public static function remove_teacher_from_dropdown_when_unenrolled(core\event\role_unassigned $event) {
8283
teacher_removed_allocated_not_graded($event);
8384
}
85+
86+
/**
87+
* @param core\event\user_updated $event
88+
* @throws dml_exception
89+
*/
90+
public static function user_updated(core\event\user_updated $event) {
91+
$user = user::get_from_id($event->objectid);
92+
if ($user) {
93+
$user->clear_cache();
94+
}
95+
}
8496
}

db/caches.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@
7979
'simplekeys' => true,
8080
'simpledata' => false,
8181
],
82+
user::CACHE_AREA_IDS => [
83+
'mode' => cache_store::MODE_APPLICATION,
84+
'simplekeys' => true,
85+
'simpledata' => false,
86+
],
8287
feedback::CACHE_AREA_BY_SUBMISSION => [
8388
'mode' => cache_store::MODE_APPLICATION,
8489
'simplekeys' => true,

db/events.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,8 @@
5959
'eventname' => 'core\event\role_unassigned',
6060
'callback' => 'mod_coursework_observer::remove_teacher_from_dropdown_when_unenrolled',
6161
],
62+
[
63+
'eventname' => 'core\event\user_updated',
64+
'callback' => 'mod_coursework_observer::user_updated',
65+
],
6266
];

db/upgrade.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,31 @@ function xmldb_coursework_upgrade($oldversion) {
365365
upgrade_mod_savepoint(true, 2026010801, 'coursework');
366366
}
367367

368+
//todo debugging only - delete this
369+
if ($oldversion < 2026022301) {
370+
371+
// Define table coursework_temp_logs to be created.
372+
$table = new xmldb_table('coursework_temp_logs');
373+
374+
// Adding fields to table coursework_temp_logs.
375+
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
376+
$table->add_field('classname', XMLDB_TYPE_CHAR, '255', null, null, null, null);
377+
$table->add_field('notes', XMLDB_TYPE_CHAR, '255', null, null, null, null);
378+
$table->add_field('stacktrace', XMLDB_TYPE_CHAR, '255', null, null, null, null);
379+
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
380+
381+
// Adding keys to table coursework_temp_logs.
382+
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
383+
384+
// Conditionally launch create table for coursework_temp_logs.
385+
if (!$dbman->table_exists($table)) {
386+
$dbman->create_table($table);
387+
}
388+
389+
// Coursework savepoint reached.
390+
upgrade_mod_savepoint(true, 2026022301, 'coursework');
391+
}
392+
368393
// Always needs to return true.
369394
return true;
370395
}

version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
$plugin->component = 'mod_coursework';
2626

27-
$plugin->version = 2026022000;
27+
$plugin->version = 2026022302;
2828
$plugin->requires = 2024100700;
2929

3030
$plugin->cron = 300; // Period for cron to check this module (secs).

0 commit comments

Comments
 (0)