Skip to content

Commit

Permalink
Add lock user and time #320
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Sep 16, 2024
1 parent 0517a98 commit d1da761
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 3 deletions.
25 changes: 25 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ public static function course_display_name(int $courseid): string {
return $course->fullname;
}

/**
* Displays the user who locked the profile and the date it was locked.
* @param int|null $lockuserid
* @param int|null $locktime
* @return string
*/
public static function lock_display_info(?int $lockuserid, ?int $locktime): string {
GLOBAL $DB;

if (empty($lockuserid) && empty($locktime)) {
return '';
}

$lockuser = $DB->get_record('user', ['id' => $lockuserid]);
if ($lockuser) {
$link = new \moodle_url('/user/profile.php', ['id' => $lockuserid]);
$userdisplay = \html_writer::link($link, fullname($lockuser));
} else {
$userdisplay = get_string('unknown', 'tool_excimer');
}
$date = userdate($locktime, get_string('strftimedate', 'core_langconfig'));

return get_string('lockedinfo', 'tool_excimer', ['user' => $userdisplay, 'date' => $date]);
}

/**
* Returns a HTML link based on the lockwait url.
* @param string|null $lockwaiturl Relative lockwait url
Expand Down
20 changes: 20 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static function get_metadata(collection $collection): collection {
'pathinfo' => 'field_pathinfo',
'parameters' => 'field_parameters',
'referer' => 'field_referer',
'lockuser' => 'field_lockuser',
'lockreason' => 'field_lockreason',
'locktime' => 'field_locktime',
],
'privacy:metadata:tool_excimer_profiles'
);
Expand Down Expand Up @@ -82,6 +85,7 @@ public static function export_user_data(approved_contextlist $contextlist) {
$userid = $contextlist->get_user()->id;
foreach ($contextlist as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
// Profile data.
$list = [];
$rows = $DB->get_records(profile::TABLE, ['userid' => $userid]);
foreach ($rows as $row) {
Expand All @@ -94,6 +98,15 @@ public static function export_user_data(approved_contextlist $contextlist) {
'referer' => $row->referer,
];
}
// Lock data.
$rows = $DB->get_records(profile::TABLE, ['lockuser' => $userid]);
foreach ($rows as $row) {
$list[] = [
'lockuser' => $userid,
'lockreason' => $row->lockreason,
'locktime' => $row->locktime,
];
}
writer::with_context($context)->export_data(
[get_string('privacy:metadata:tool_excimer_profiles', 'tool_excimer')],
(object) $list
Expand Down Expand Up @@ -125,6 +138,9 @@ public static function delete_data_for_user(approved_contextlist $contextlist) {
foreach ($contextlist as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
$DB->delete_records(profile::TABLE, ['userid' => $userid]);
$DB->set_field(profile::TABLE, 'lockreason', '', ['lockuser' => $userid]);
$DB->set_field(profile::TABLE, 'locktime', null, ['lockuser' => $userid]);
$DB->set_field(profile::TABLE, 'lockuser', null, ['lockuser' => $userid]);
}
}
}
Expand All @@ -139,6 +155,7 @@ public static function get_users_in_context(userlist $userlist) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "SELECT * FROM {tool_excimer_profiles}";
$userlist->add_from_sql('userid', $sql, []);
$userlist->add_from_sql('lockuser', $sql, []);
}
}

Expand All @@ -154,6 +171,9 @@ public static function delete_data_for_users(approved_userlist $userlist) {
$users = $userlist->get_users();
foreach ($users as $user) {
$DB->delete_records(profile::TABLE, ['userid' => $user->id]);
$DB->set_field(profile::TABLE, 'lockreason', '', ['lockuser' => $user->id]);
$DB->set_field(profile::TABLE, 'locktime', null, ['lockuser' => $user->id]);
$DB->set_field(profile::TABLE, 'lockuser', null, ['lockuser' => $user->id]);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions classes/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class profile extends persistent {
'userid',
'courseid',
'lockreason',
'lockuser',
'locktime',
'lockwaiturl',
];

Expand Down Expand Up @@ -453,6 +455,8 @@ protected static function define_properties(): array {
'dbwrites' => ['type' => PARAM_INT, 'default' => 0],
'dbreplicareads' => ['type' => PARAM_INT, 'default' => 0],
'lockreason' => ['type' => PARAM_TEXT, 'default' => ''],
'lockuser' => ['type' => PARAM_INT, 'default' => null],
'locktime' => ['type' => PARAM_INT, 'default' => null],
'courseid' => ['type' => PARAM_INT, 'default' => null],
'lockheld' => ['type' => PARAM_FLOAT, 'default' => 0],
'lockwait' => ['type' => PARAM_FLOAT, 'default' => 0],
Expand Down
2 changes: 2 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<FIELD NAME="memoryusagemax" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false" COMMENT="Maximum memory usage recorded for this given run. Note that CLI profiles might store the max for previous connected runs within the same process."/>
<FIELD NAME="samplerate" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false" COMMENT="Base sample rate at the time the profile was recorded"/>
<FIELD NAME="lockreason" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Holds reasons for keeping this profile. Any non-empty value will prevent purging."/>
<FIELD NAME="lockuser" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The user who locked the profile"/>
<FIELD NAME="locktime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The timestamp at which the profile was locked"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The id of $COURSE at the time of profiling"/>
<FIELD NAME="lockheld" TYPE="number" LENGTH="12" NOTNULL="false" SEQUENCE="false" DECIMALS="6" COMMENT="Length of time a lock was held in seconds"/>
<FIELD NAME="lockwait" TYPE="number" LENGTH="12" NOTNULL="false" SEQUENCE="false" DECIMALS="6" COMMENT="Wait time on a lock in seconds"/>
Expand Down
23 changes: 23 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,5 +553,28 @@ function xmldb_tool_excimer_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2024082301, 'tool', 'excimer');
}

if ($oldversion < 2024091600) {

// Define field lockuser to be added to tool_excimer_profiles.
$table = new xmldb_table('tool_excimer_profiles');
$field = new xmldb_field('lockuser', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'lockreason');

// Conditionally launch add field lockuser.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field locktime to be added to tool_excimer_profiles.
$field = new xmldb_field('locktime', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'lockuser');

// Conditionally launch add field locktime.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Excimer savepoint reached.
upgrade_plugin_savepoint(true, 2024091600, 'tool', 'excimer');
}

return true;
}
4 changes: 4 additions & 0 deletions lang/en/tool_excimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
$string['field_hostname'] = 'Host name';
$string['field_useragent'] = 'User agent';
$string['field_versionhash'] = 'Version Hash';
$string['field_lockreason'] = 'Lock reason';
$string['field_lockuser'] = 'Lock user';
$string['field_locktime'] = 'Lock time';
$string['field_courseid'] = 'Course';
$string['field_lockheld'] = 'Session held';
$string['field_lockwait'] = 'Session wait';
Expand Down Expand Up @@ -191,6 +194,7 @@
// Lock reason form.
$string['lock_profile'] = 'Lock Profile';
$string['locked'] = 'Profile is locked';
$string['lockedinfo'] = 'Locked by {$a->user} on {$a->date}';
$string['lockreason'] = 'Lock Profile Reason';
$string['lockreason_help'] = 'Submitting text will prevent this profile from being deleted.
It will not be purged during cleanup tasks, nor can it be deleted manually (will also be excluded from group deletes).
Expand Down
21 changes: 20 additions & 1 deletion lock_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,26 @@
$form = new \tool_excimer\form\lock_reason_form($url);

if ($data = $form->get_data()) {
$DB->update_record('tool_excimer_profiles', (object) ['id' => $profileid, 'lockreason' => trim($data->lockreason)]);
$lockreason = trim($data->lockreason);
$previousreason = $profile->get('lockreason');
$updatefields = [
'id' => $profileid,
'lockreason' => $lockreason,
];

// Add info when creating a new lock.
if ($lockreason && !$previousreason) {
$updatefields['lockuser'] = $USER->id;
$updatefields['locktime'] = time();
}

// Clear info when removing a lock.
if (!$lockreason) {
$updatefields['lockuser'] = null;
$updatefields['locktime'] = null;
}

$DB->update_record('tool_excimer_profiles', (object) $updatefields);
redirect($data->returnurl);
} else {
$form->set_data(['lockreason' => $DB->get_field('tool_excimer_profiles', 'lockreason', ['id' => $profileid])]);
Expand Down
1 change: 1 addition & 0 deletions profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
$data['fullname'] = '-';
}
$data['lockreason'] = format_text($data['lockreason']);
$data['lockreasoninfo'] = helper::lock_display_info($data['lockuser'], $data['locktime']);
$tabs = new tabs($url);

// JavaScript locale string. Arguably "localecldr/langconfig" would be a better
Expand Down
3 changes: 3 additions & 0 deletions templates/profile.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
{{#lockreason}}
<div class="alert alert-info alert-block">
<h4>{{#str}} locked, tool_excimer {{/str}}</h4>
{{#lockreasoninfo}}
<div class="mb-2">{{{lockreasoninfo}}}</div>
{{/lockreasoninfo}}
{{{lockreason}}}
</div>
{{/lockreason}}
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024091200;
$plugin->release = 2024091200;
$plugin->version = 2024091600;
$plugin->release = 2024091600;
$plugin->requires = 2017051500; // Moodle 3.3 for Totara support.
$plugin->supported = [35, 401]; // Supports Moodle 3.5 or later.
// TODO $plugin->incompatible = ; // Available as of Moodle 3.9.0 or later.
Expand Down

0 comments on commit d1da761

Please sign in to comment.