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.'; diff --git a/locallib.php b/locallib.php index 5238faf..1ab27f4 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'); @@ -281,6 +295,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'), ); } @@ -564,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; } } @@ -681,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; } @@ -746,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); }