Skip to content

Commit

Permalink
Issue #2: Added origin script flag to email logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peterburnett committed Nov 6, 2020
1 parent 438889a commit 159d413
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 8 deletions.
13 changes: 12 additions & 1 deletion classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,22 @@ static function purge($maxdays) {
}

static function log_mail($success, $msg, $user, $from, $subject, $messagetext, $messagehtml, $attachment, $attachname, $usetrueaddress, $replyto, $replytoname, $wordwrapwidth, $queuestatus=0) {
global $DB;
global $CFG, $DB;

if (!\get_config('local_maillog', 'logmails')) {
return true;
}

// Find originating script. It will be the layer with the first email_to_user call.
$stack = debug_backtrace();
foreach ($stack as $call) {
if ($call['function'] === 'email_to_user') {
$layer = $call;
break;
}
}
$originscript = '/' . str_replace($CFG->dirroot . '/', '', $layer['file']) . ':' . $layer['line'];

$transaction = $DB->start_delegated_transaction();

$todb = new \stdClass();
Expand All @@ -77,6 +87,7 @@ static function log_mail($success, $msg, $user, $from, $subject, $messagetext, $
$todb->success = $success ? 1 : 0;
$todb->returnmsg = substr($msg, 0, 255);
$todb->queuestatus = $queuestatus;
$todb->originscript = $originscript;

$newrecordid = $DB->insert_record('mail_log', $todb);

Expand Down
14 changes: 11 additions & 3 deletions classes/output/log_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ public function __construct($uniqueid, \moodle_url $url, $perpage = 100) {
parent::__construct($uniqueid);

$this->set_attribute('class', 'generaltable generalbox');
$this->define_columns(array(
$cols = [
'to',
'subject',
'content',
'hasattachment',
'timesent',
'originscript',
'status',
));
];

$this->define_columns($cols);
$this->define_headers(array(
get_string('to'),
get_string('subject', 'hub'),
get_string('content'),
get_string('hasattachment', 'local_maillog'),
get_string('timesent', 'local_maillog'),
get_string('originscript', 'local_maillog'),
get_string('status'),
)
);
Expand Down Expand Up @@ -88,4 +92,8 @@ public function col_timesent($row) {
public function col_status($row) {
return $row->success ? get_string('sent', 'local_maillog') : get_string('failed', 'local_maillog');
}
}

public function col_originscript($row) {
return $row->originscript ?? 'Unknown';
}
}
5 changes: 3 additions & 2 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="local/maillog/db" VERSION="2016042200" COMMENT="XMLDB file for Moodle local/maillog"
<XMLDB PATH="local/maillog/db" VERSION="20201106" COMMENT="XMLDB file for Moodle local/maillog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -24,6 +24,7 @@
<FIELD NAME="success" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="returnmsg" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="queuestatus" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="originscript" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand All @@ -38,4 +39,4 @@
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
48 changes: 48 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Maillog upgrade library.
*
* @package local_maillog
* @copyright 2020 Peter Burnett <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

function xmldb_local_maillog_upgrade($oldversion) {
global $DB;

$dbman = $DB->get_manager();

if ($oldversion < 20201106000) {

// Define field originscript to be added to mail_log.
$table = new xmldb_table('mail_log');
$field = new xmldb_field('originscript', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'queuestatus');

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

// Maillog savepoint reached.
upgrade_plugin_savepoint(true, 20201106000, 'local', 'maillog');
}

return true;
}
1 change: 1 addition & 0 deletions lang/en/local_maillog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
$string['mailqueue'] = 'Mail Queue';
$string['maxdays'] = 'Max days to keep emails in log/queue';
$string['maxdaysinfo'] = 'Any mail log entries older than the configured amount of days will be purged.';
$string['originscript'] = 'Originating script';
$string['pendingsend'] = 'Pending send';
$string['queued'] = 'Queued';
$string['queuemails'] = 'Queue emails';
Expand Down
2 changes: 1 addition & 1 deletion log_report.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
}

// Setup the SQL query and display
$fields = 'userid, subject, messagetext, messagehtml, attachname, timesent, success';
$fields = 'id, userid, subject, messagetext, messagehtml, attachname, timesent, originscript, success';
$from = '{mail_log}';
$where = 'queuestatus = 0';
$table->set_sql($fields, $from, $where);
Expand Down
3 changes: 2 additions & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

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

$plugin->version = 20170111100;
// Extra 0 due to broken previous version number.
$plugin->version = 20201106000;
$plugin->requires = 2015051100;
$plugin->cron = 0;
$plugin->component = 'local_maillog';
Expand Down

0 comments on commit 159d413

Please sign in to comment.