From acdb1951abd79bb62bb7f41c5d42252edfe5f015 Mon Sep 17 00:00:00 2001 From: NinaHerrmann Date: Thu, 13 Jun 2024 15:38:46 +0200 Subject: [PATCH] added a table in step email and inserted the logging in the task --- lang/en/tool_lifecycle.php | 2 ++ settings.php | 5 +++ step/email/db/install.xml | 13 ++++++++ step/email/db/upgrade.php | 63 ++++++++++++++++++++++++++++++++++++++ step/email/lib.php | 14 +++++++-- step/email/version.php | 2 +- version.php | 2 +- 7 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 step/email/db/upgrade.php diff --git a/lang/en/tool_lifecycle.php b/lang/en/tool_lifecycle.php index 1d96728d..5718b20e 100644 --- a/lang/en/tool_lifecycle.php +++ b/lang/en/tool_lifecycle.php @@ -56,6 +56,8 @@ $string['config_delay_duration_desc'] = 'This setting defines the default delay duration of a workflow in case one of its processes is rolled back or finishes. The delay duration determines how long a course will be excepted from being processed again in either of the cases.'; +$string['config_logreceivedmails'] = 'Save sent mails to the database'; +$string['config_logreceivedmails_desc'] = 'Additionally writing to the database has the advantage that it can be looked up, however it consumes memory.'; $string['config_showcoursecounts'] = 'Show amount of courses which will be triggered'; $string['config_showcoursecounts_desc'] = 'The workflow overview page by default shows the amount of courses which will be triggered by the configured triggers which can be load heavy. Disable this option if you experience issues loading the workflow diff --git a/settings.php b/settings.php index 3f37c086..90f7529d 100644 --- a/settings.php +++ b/settings.php @@ -47,6 +47,11 @@ get_string('config_showcoursecounts_desc', 'tool_lifecycle'), 1)); + $settings->add(new admin_setting_configcheckbox('tool_lifecycle/logreceivedmails', + get_string('config_logreceivedmails', 'tool_lifecycle'), + get_string('config_logreceivedmails_desc', 'tool_lifecycle'), + 0)); + $ADMIN->add('lifecycle_category', new admin_externalpage('tool_lifecycle_workflow_drafts', get_string('workflow_drafts_header', 'tool_lifecycle'), new moodle_url(\tool_lifecycle\urls::WORKFLOW_DRAFTS))); diff --git a/step/email/db/install.xml b/step/email/db/install.xml index 1620fde5..a46bb088 100644 --- a/step/email/db/install.xml +++ b/step/email/db/install.xml @@ -14,5 +14,18 @@ + + + + + + + + + + + + +
\ No newline at end of file diff --git a/step/email/db/upgrade.php b/step/email/db/upgrade.php new file mode 100644 index 00000000..1e928180 --- /dev/null +++ b/step/email/db/upgrade.php @@ -0,0 +1,63 @@ +. + +/** + * Update script for lifecyclestep_email plugin + * + * @package lifecyclestep_email + * @copyright 2024 Nina Herrmann University of Münster + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Update script for lifecyclestep_email. + * @param int $oldversion Version id of the previously installed version. + * @return bool + * @throws ddl_exception + * @throws ddl_field_missing_exception + * @throws ddl_table_missing_exception + * @throws dml_exception + * @throws downgrade_exception + * @throws upgrade_exception + */ +function xmldb_lifecyclestep_email_upgrade($oldversion) { + + global $DB; + $dbman = $DB->get_manager(); + if ($oldversion < 2024061301) { + $table = new xmldb_table('lifecyclestep_email_notified'); + + // Adding fields to table tool_lifecycle_proc_error. + $table->add_field('id', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('courseid', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null); + $table->add_field('userid', XMLDB_TYPE_INTEGER, '20', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemailsent', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table tool_lifecycle_proc_error. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('courseid_fk', XMLDB_KEY_FOREIGN, ['courseid'], 'course', ['id']); + $table->add_key('userid_fk', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']); + + // Conditionally launch create table for tool_lifecycle_proc_error. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Lifecycle savepoint reached. + upgrade_plugin_savepoint(true, 2024061301, 'tool', 'lifecycle'); + } + return true; +} \ No newline at end of file diff --git a/step/email/lib.php b/step/email/lib.php index 4635aed8..06c29744 100644 --- a/step/email/lib.php +++ b/step/email/lib.php @@ -119,16 +119,26 @@ public function post_processing_bulk_operation() { ['instanceid' => $step->id, 'touser' => $user->id, ]); - $parsedsettings = $this->replace_placeholders($settings, $user, $step->id, $mailentries); + $parsedsettings = $this->replace_placeholders($settings, $user, $step->id, $mailentries); $subject = $parsedsettings['subject']; $content = $parsedsettings['content']; $contenthtml = $parsedsettings['contenthtml']; - // TODO: use course info to parse content template! $success = email_to_user($user, \core_user::get_noreply_user(), $subject, $content, $contenthtml); if (!$success) { mtrace("E-mail to user {$user->id} failed."); } + $dblogging = get_config('tool_lifecycle', 'logreceivedmails'); + if ($dblogging && $success) { + // Insert user id and course id in table tool_lifecycle_user_notified. + $record = new \stdClass(); + $record->userid = $user->id; + $record->timemailsent = time(); + foreach ($mailentries as $entry) { + $record->courseid = $entry->courseid; + $DB->insert_record('lifecyclestep_email_notified', $record); + } + } $DB->delete_records('lifecyclestep_email', ['instanceid' => $step->id, 'touser' => $user->id, ]); diff --git a/step/email/version.php b/step/email/version.php index 5b8d20f3..3bf08cd3 100644 --- a/step/email/version.php +++ b/step/email/version.php @@ -24,5 +24,5 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2019070200; +$plugin->version = 2024061301; $plugin->component = 'lifecyclestep_email'; diff --git a/version.php b/version.php index e8f6b54a..860e8349 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die; $plugin->maturity = MATURITY_BETA; -$plugin->version = 2024042300; +$plugin->version = 2024061302; $plugin->component = 'tool_lifecycle'; $plugin->requires = 2022112800; // Requires Moodle 4.1+. $plugin->release = 'v4.4-r1';