From 35f02934901fbaacb22f2dbf4b0c94b428b9ea80 Mon Sep 17 00:00:00 2001 From: Sabiha S Date: Mon, 22 Mar 2021 15:52:57 +0400 Subject: [PATCH 1/6] Add 'emailattachment' string --- lang/en/report_customsql.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 2b07f33..e7370ab 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -71,6 +71,7 @@ $string['editreportx'] = 'Edit query \'{$a}\''; $string['emailnumberofrows'] = 'Just the number of rows and the link'; $string['emailresults'] = 'Put the results in the email body'; +$string['emailattachment'] = 'Attach CSV to email body'; $string['emailink'] = 'To access the report, click this link: {$a}'; $string['emailrow'] = 'The report returned {$a} row.'; $string['emailrows'] = 'The report returned {$a} rows.'; From 1d945850fc4155ae962b1a68d4f01ed197fda13e Mon Sep 17 00:00:00 2001 From: Chalhoub University Date: Mon, 22 Mar 2021 16:00:43 +0400 Subject: [PATCH 2/6] Add option to report_customsql_email_options --- locallib.php | 1 + 1 file changed, 1 insertion(+) diff --git a/locallib.php b/locallib.php index 5238faf..6b94cd4 100644 --- a/locallib.php +++ b/locallib.php @@ -281,6 +281,7 @@ function report_customsql_daily_at_options() { function report_customsql_email_options() { return array('emailnumberofrows' => get_string('emailnumberofrows', 'report_customsql'), 'emailresults' => get_string('emailresults', 'report_customsql'), + 'emailattachment' => get_string('emailattachment', 'report_customsql'), ); } From dba062fc119b0ccf0a157e8e05572ecee008afde Mon Sep 17 00:00:00 2001 From: Chalhoub University Date: Mon, 22 Mar 2021 16:03:34 +0400 Subject: [PATCH 3/6] Using FileAPI to create a record --- locallib.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/locallib.php b/locallib.php index 6b94cd4..ce7c21f 100644 --- a/locallib.php +++ b/locallib.php @@ -110,6 +110,7 @@ function report_customsql_generate_csv($report, $timenow) { $csvfilenames = array(); $csvtimestamp = null; $count = 0; + $file = null; foreach ($rs as $row) { if (!$csvtimestamp) { list($csvfilename, $csvtimestamp) = report_customsql_csv_filename($report, $timenow); @@ -117,6 +118,19 @@ function report_customsql_generate_csv($report, $timenow) { if (!file_exists($csvfilename)) { $handle = fopen($csvfilename, 'w'); + $fs = get_file_storage(); + $file = $fs->create_file_from_pathname( + [ + 'contextid' => context_system::instance()->id, + 'component' => 'report_customsql', + 'filearea' => 'admin_report_customsql', + 'itemid' => 0, + 'filepath' => dirname($csvfilename) . '/', + 'filename' => basename($csvfilename), + ], + $csvfilename + ); + report_customsql_start_csv($handle, $row, $report); } else { $handle = fopen($csvfilename, 'a'); From e4355c117c19a2b36a33337ce79bde6068d576e7 Mon Sep 17 00:00:00 2001 From: Chalhoub University Date: Mon, 22 Mar 2021 16:05:24 +0400 Subject: [PATCH 4/6] Use FileAPI to delete & unlink --- locallib.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/locallib.php b/locallib.php index ce7c21f..3180ffd 100644 --- a/locallib.php +++ b/locallib.php @@ -579,7 +579,23 @@ function report_customsql_delete_old_temp_files($upto) { } foreach ($files as $file) { if (basename($file) < $comparison) { - unlink($file); + $fs = get_file_storage(); + + $fileinfo = array( + 'component' => 'report_customsql', + 'filearea' => 'admin_report_customsql', + 'itemid' => 0, + 'contextid' => context_system::instance()->id, + 'filepath' => dirname($file) . '/', + 'filename' => basename($file)); + + $file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'], + $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']); + + // Delete it if it exists + if ($file) { + $file->delete(); + } $count += 1; } } From bdd3b6acae9d4b601221c85e9f15edd735c24333 Mon Sep 17 00:00:00 2001 From: Chalhoub University Date: Mon, 22 Mar 2021 16:09:57 +0400 Subject: [PATCH 5/6] Use FileAPI to read file & attach Attaching using Messaging API --- locallib.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/locallib.php b/locallib.php index 3180ffd..91ceba9 100644 --- a/locallib.php +++ b/locallib.php @@ -712,6 +712,22 @@ function report_customsql_get_message($report, $csvfilename) { $message->fullmessagehtml = $fullmessagehtml; $message->smallmessage = null; + $fs = get_file_storage(); + + // Issue using GetFile to fetch the file + $file = $fs->get_file( + context_system::instance()->id, + 'report_customsql', + 'admin_report_customsql', + 0, + dirname($csvfilename) . '/', + basename($csvfilename) + ); + + if($report->emailwhat === 'emailattachment'){ + $message->attachment = $file; + } + return $message; } From 98bbf1481e3403de6de7c73a17235ccb826bee00 Mon Sep 17 00:00:00 2001 From: Chalhoub University Date: Mon, 22 Mar 2021 16:11:35 +0400 Subject: [PATCH 6/6] Add attachment to message body --- locallib.php | 1 + 1 file changed, 1 insertion(+) diff --git a/locallib.php b/locallib.php index 91ceba9..1ab27f4 100644 --- a/locallib.php +++ b/locallib.php @@ -793,6 +793,7 @@ function report_customsql_send_email_notification($recipient, $message) { $eventdata->fullmessageformat = $message->fullmessageformat; $eventdata->fullmessagehtml = $message->fullmessagehtml; $eventdata->smallmessage = $message->smallmessage; + $eventdata->attachment = $message->attachment; return message_send($eventdata); }