Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Send CSV Report as attachment #86

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lang/en/report_customsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down
50 changes: 49 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,27 @@ 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);
$csvfilenames[] = $csvfilename;

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');
Expand Down Expand Up @@ -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'),
);
}

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down