Skip to content

Commit 80b4d28

Browse files
Feature/logsendedmails (#218)
* added a table in step email and inserted the logging in the task * added german lang strings * codechecker... * added deletion after one year in the email notified table --------- Co-authored-by: Thomas Niedermaier <[email protected]>
1 parent 464ec82 commit 80b4d28

File tree

8 files changed

+100
-3
lines changed

8 files changed

+100
-3
lines changed

classes/task/lifecycle_cleanup_task.php

+2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function get_name() {
4848
public function execute() {
4949
global $DB;
5050
$twomonthago = time() - 60 * 24 * 60 * 60;
51+
$oneyearago = time() - 365 * 24 * 60 * 60;
5152
$DB->delete_records_select('tool_lifecycle_delayed', 'delayeduntil <= :time', ['time' => $twomonthago]);
5253
$DB->delete_records_select('tool_lifecycle_delayed_workf', 'delayeduntil <= :time', ['time' => $twomonthago]);
54+
$DB->delete_records_select('lifecyclestep_email_notified', 'timemailsent <= :time', ['time' => $oneyearago]);
5355
}
5456
}

lang/de/tool_lifecycle.php

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
$string['config_delay_duration_desc'] = 'Diese Einstellung definiert den Standardlänge einer Kursausschlusses in einem Workflow
5050
falls ein Prozess des Workflows zurückgesetzt oder beendigt wird. Die Länge des Kursausschlusses besagt, wie lange es dauert, bis
5151
der Kurs wieder vom Workflow bearbeitet wird.';
52+
$string['config_logreceivedmails'] = 'Zusätzliches Logging von E-mails zu Nutzern.';
53+
$string['config_logreceivedmails_desc'] = 'Das Schreiben in die Datenbank hat den Vorteil, dass es explizit nachgeguckt werden kann, allerdings verbraucht es Speicher.';
5254
$string['config_showcoursecounts'] = 'Zeige Anzahl der Kurse, die getriggert werden';
5355
$string['config_showcoursecounts_desc'] = 'Die Workflow-Konfigurationsseite zeigt normalerweise die Anzahl an Kursen, die durch
5456
die konfigurierten Trigger getriggert werden, was Performance-Probleme verursachen kann. Bei Performance-Problemen kann dies hiermit

lang/en/tool_lifecycle.php

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
$string['config_delay_duration_desc'] = 'This setting defines the default delay duration of a workflow
5858
in case one of its processes is rolled back or finishes.
5959
The delay duration determines how long a course will be excepted from being processed again in either of the cases.';
60+
$string['config_logreceivedmails'] = 'Save sent mails to the database';
61+
$string['config_logreceivedmails_desc'] = 'Additionally writing to the database has the advantage that it can be looked up, however it consumes memory.';
6062
$string['config_showcoursecounts'] = 'Show amount of courses which will be triggered';
6163
$string['config_showcoursecounts_desc'] = 'The workflow overview page by default shows the amount of courses which will be
6264
triggered by the configured triggers which can be load heavy. Disable this option if you experience issues loading the workflow

settings.php

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
get_string('config_showcoursecounts_desc', 'tool_lifecycle'),
4848
1));
4949

50+
$settings->add(new admin_setting_configcheckbox('tool_lifecycle/logreceivedmails',
51+
get_string('config_logreceivedmails', 'tool_lifecycle'),
52+
get_string('config_logreceivedmails_desc', 'tool_lifecycle'),
53+
0));
54+
5055
$ADMIN->add('lifecycle_category', new admin_externalpage('tool_lifecycle_workflow_drafts',
5156
get_string('workflow_drafts_header', 'tool_lifecycle'),
5257
new moodle_url(\tool_lifecycle\urls::WORKFLOW_DRAFTS)));

step/email/db/install.xml

+13
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,18 @@
1414
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
1515
</KEYS>
1616
</TABLE>
17+
<TABLE NAME="lifecyclestep_email_notified" COMMENT="table containing the userid and the courseid that was sent">
18+
<FIELDS>
19+
<FIELD NAME="id" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="true" COMMENT="id of the process"/>
20+
<FIELD NAME="courseid" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="course id"/>
21+
<FIELD NAME="userid" TYPE="int" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="id of the user"/>
22+
<FIELD NAME="timemailsent" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="unix timestamp."/>
23+
</FIELDS>
24+
<KEYS>
25+
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
26+
<KEY NAME="courseid_fk" TYPE="foreign" FIELDS="courseid" REFTABLE="course" REFFIELDS="id" COMMENT="Foreign key on course table"/>
27+
<KEY NAME="userid_fk" TYPE="foreign" FIELDS="userid" REFTABLE="user" REFFIELDS="id"/>
28+
</KEYS>
29+
</TABLE>
1730
</TABLES>
1831
</XMLDB>

step/email/db/upgrade.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Update script for lifecyclestep_email plugin
19+
*
20+
* @package lifecyclestep_email
21+
* @copyright 2024 Nina Herrmann University of Münster
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
/**
26+
* Update script for lifecyclestep_email.
27+
* @param int $oldversion Version id of the previously installed version.
28+
* @return bool
29+
* @throws ddl_exception
30+
* @throws ddl_field_missing_exception
31+
* @throws ddl_table_missing_exception
32+
* @throws dml_exception
33+
* @throws downgrade_exception
34+
* @throws upgrade_exception
35+
*/
36+
function xmldb_lifecyclestep_email_upgrade($oldversion) {
37+
38+
global $DB;
39+
$dbman = $DB->get_manager();
40+
if ($oldversion < 2024061301) {
41+
$table = new xmldb_table('lifecyclestep_email_notified');
42+
43+
// Adding fields to table tool_lifecycle_proc_error.
44+
$table->add_field('id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
45+
$table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null);
46+
$table->add_field('userid', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null);
47+
$table->add_field('timemailsent', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
48+
49+
// Adding keys to table tool_lifecycle_proc_error.
50+
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
51+
$table->add_key('courseid_fk', XMLDB_KEY_FOREIGN, ['courseid'], 'course', ['id']);
52+
$table->add_key('userid_fk', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
53+
54+
// Conditionally launch create table for tool_lifecycle_proc_error.
55+
if (!$dbman->table_exists($table)) {
56+
$dbman->create_table($table);
57+
}
58+
59+
// Lifecycle savepoint reached.
60+
upgrade_plugin_savepoint(true, 2024061301, 'tool', 'lifecycle');
61+
}
62+
return true;
63+
}

step/email/lib.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,26 @@ public function post_processing_bulk_operation() {
119119
['instanceid' => $step->id,
120120
'touser' => $user->id, ]);
121121

122-
$parsedsettings = $this->replace_placeholders($settings, $user, $step->id, $mailentries);
122+
$parsedsettings = $this->replace_placeholders($settings, $user, $step->id, $mailentries);
123123

124124
$subject = $parsedsettings['subject'];
125125
$content = $parsedsettings['content'];
126126
$contenthtml = $parsedsettings['contenthtml'];
127-
// TODO: use course info to parse content template!
128127
$success = email_to_user($user, \core_user::get_noreply_user(), $subject, $content, $contenthtml);
129128
if (!$success) {
130129
mtrace("E-mail to user {$user->id} failed.");
131130
}
131+
$dblogging = get_config('tool_lifecycle', 'logreceivedmails');
132+
if ($dblogging && $success) {
133+
// Insert user id and course id in table tool_lifecycle_user_notified.
134+
$record = new \stdClass();
135+
$record->userid = $user->id;
136+
$record->timemailsent = time();
137+
foreach ($mailentries as $entry) {
138+
$record->courseid = $entry->courseid;
139+
$DB->insert_record('lifecyclestep_email_notified', $record);
140+
}
141+
}
132142
$DB->delete_records('lifecyclestep_email',
133143
['instanceid' => $step->id,
134144
'touser' => $user->id, ]);

step/email/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424

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

27-
$plugin->version = 2019070200;
27+
$plugin->version = 2024061301;
2828
$plugin->component = 'lifecyclestep_email';

0 commit comments

Comments
 (0)