From d4f7713f3791e8a5872b4706a471856807278f91 Mon Sep 17 00:00:00 2001 From: Hamish Dewe Date: Thu, 2 May 2019 13:19:54 +1200 Subject: [PATCH 1/5] Allow provision of queryparam values via querystring parameters --- view.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/view.php b/view.php index 497e2d7..0e71bea 100644 --- a/view.php +++ b/view.php @@ -71,8 +71,20 @@ redirect(report_customsql_url('index.php')); } - if ($newreport = $mform->get_data()) { - + // Allow form bypass if all queryparams supplied + if ($queryparams) { + foreach ($queryparams as $queryparam => $formparam) { + if ($qs_value = optional_param($queryparam, null, PARAM_RAW)) { + + $queryparams[$queryparam] = $qs_value; + $qs_params++; + } + } + } + if (count($queryparams) == $qs_params) { + // Fall through to display results + $report->queryparams = serialize($queryparams); + } else if ($newreport = $mform->get_data()) { // Pick up named parameters into serialised array. if ($queryparams) { foreach ($queryparams as $queryparam => $formparam) { @@ -96,7 +108,8 @@ $report->description = strip_tags($report->description); $queryparams = unserialize($report->queryparams); foreach ($queryparams as $param => $value) { - $report->{'queryparam'.$param} = $value; + $qs_value = optional_param($param, null, PARAM_RAW); + $report->{'queryparam'.$param} = !is_null($qs_value) ? $qs_value : $value; } $mform->set_data($report); $mform->display(); From e5187609b7d75c63cac56533eb531a21546f4349 Mon Sep 17 00:00:00 2001 From: Hamish Dewe Date: Thu, 2 May 2019 13:34:13 +1200 Subject: [PATCH 2/5] Update hardcoded string 'Run report' --- lang/en/report_customsql.php | 1 + view_form.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 9728d06..895fdbe 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -138,6 +138,7 @@ $string['requireint'] = 'Integer required'; $string['runable'] = 'Run'; $string['runablex'] = 'Run: {$a}'; +$string['runreport'] = 'Run report'; $string['selectcategory'] = 'Select category for this report'; $string['schedulednote'] = 'These queries are automatically run on the first day of each week or month, to report on the previous week or month. These links let you view the results that has already been accumulated.'; $string['scheduledqueries'] = 'Scheduled queries'; diff --git a/view_form.php b/view_form.php index 11a4f5c..63bded5 100644 --- a/view_form.php +++ b/view_form.php @@ -49,6 +49,6 @@ public function definition() { } } - $this->add_action_buttons(true, 'Run report'); + $this->add_action_buttons(true, get_string('runreport', 'report_customsql')); } } From 79e6968c145bc34818a2c1021e3774d1ae031d5d Mon Sep 17 00:00:00 2001 From: Hamish Dewe Date: Wed, 15 May 2019 09:56:31 +1200 Subject: [PATCH 3/5] Add options to attach csv file to email notification for scheduled reports --- lang/en/report_customsql.php | 2 ++ locallib.php | 59 ++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 895fdbe..d454e81 100644 --- a/lang/en/report_customsql.php +++ b/lang/en/report_customsql.php @@ -68,7 +68,9 @@ $string['editthiscategory'] = 'Edit this category'; $string['editthisreport'] = 'Edit this query'; $string['emailnumberofrows'] = 'Just the number of rows and the link'; +$string['emailnumberofrowsandcsv'] = 'The number of rows, link and attach csv'; $string['emailresults'] = 'Put the results in the email body'; +$string['emailresultsandcsv'] = 'Put the results in the email body and attach csv'; $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 b441c24..1a278c9 100644 --- a/locallib.php +++ b/locallib.php @@ -250,8 +250,11 @@ 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'), + return array( + 'emailnumberofrows' => get_string('emailnumberofrows', 'report_customsql'), + 'emailresults' => get_string('emailresults', 'report_customsql'), + 'emailnumberofrowsandcsv' => get_string('emailnumberofrowsandcsv', 'report_customsql'), + 'emailresultsandcsv' => get_string('emailresultsandcsv', 'report_customsql') ); } @@ -618,7 +621,7 @@ function report_customsql_get_message($report, $csvfilename) { // Construct message in html. $fullmessagehtml = null; - if ($report->emailwhat === 'emailresults') { + if ($report->emailwhat === 'emailresults' || $report->emailwhat === 'emailresultsandcsv') { $fullmessagehtml = html_writer::table($table); } $fullmessagehtml .= $fullmessage; @@ -652,7 +655,11 @@ function report_customsql_email_report($report, $csvfilename = null) { $usernames = preg_split("/[\s,;]+/", $report->emailto); foreach ($usernames as $username) { $recipient = $DB->get_record('user', array('username' => $username), '*', MUST_EXIST); - $messageid = report_customsql_send_email_notification($recipient, $message); + if ($report->emailwhat === 'emailresultsandcsv' || $report->emailwhat === 'emailnumberofrowsandcsv') { + $messageid = report_customsql_send_email_notification($recipient, $message, $csvfilename); + } else { + $messageid = report_customsql_send_email_notification($recipient, $message); + } if (!$messageid) { mtrace(get_string('emailsentfailed', 'report_customsql', fullname($recipient))); } @@ -682,7 +689,7 @@ function report_customsql_get_ready_to_run_daily_reports($timenow) { foreach ($reports as $id => $r) { // Check whether the report is ready to run. if (!report_customsql_is_daily_report_ready($r, $timenow)) { - continue; + //continue; } $reportstorun[$id] = $r; } @@ -695,23 +702,31 @@ function report_customsql_get_ready_to_run_daily_reports($timenow) { * @param object $recepient, the message recipient. * @param object $message, the message objectr. */ -function report_customsql_send_email_notification($recipient, $message) { - - // Prepare the message. - $eventdata = new stdClass(); - $eventdata->component = 'report_customsql'; - $eventdata->name = 'notification'; - $eventdata->notification = 1; - - $eventdata->userfrom = get_admin(); - $eventdata->userto = $recipient; - $eventdata->subject = $message->subject; - $eventdata->fullmessage = $message->fullmessage; - $eventdata->fullmessageformat = $message->fullmessageformat; - $eventdata->fullmessagehtml = $message->fullmessagehtml; - $eventdata->smallmessage = $message->smallmessage; - - return message_send($eventdata); +function report_customsql_send_email_notification($recipient, $message, $csvfilename = null) { + global $CFG; + // Messaging does not support attachments, so send an email if attachment is required + if ($csvfilename) { + $parts = explode('/', $csvfilename); + $filename = $parts[count($parts) - 1]; + $path = str_replace("{$CFG->dataroot}/", "", $csvfilename); + return email_to_user($recipient, get_admin(), $message->subject, $message->fullmessage, $message->fullmessagehtml, $path, $filename); + } else { + // Prepare the message. + $eventdata = new stdClass(); + $eventdata->component = 'report_customsql'; + $eventdata->name = 'notification'; + $eventdata->notification = 1; + + $eventdata->userfrom = get_admin(); + $eventdata->userto = $recipient; + $eventdata->subject = $message->subject; + $eventdata->fullmessage = $message->fullmessage; + $eventdata->fullmessageformat = $message->fullmessageformat; + $eventdata->fullmessagehtml = $message->fullmessagehtml; + $eventdata->smallmessage = $message->smallmessage; + + return message_send($eventdata); + } } /** From 86e163e35c74c27c55bb6ade32b9a8d4c0c95be1 Mon Sep 17 00:00:00 2001 From: Hamish Dewe Date: Wed, 15 May 2019 10:06:14 +1200 Subject: [PATCH 4/5] Re-enable the report_is_daily_report_ready check (disabled during testing) --- locallib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locallib.php b/locallib.php index 1a278c9..df14250 100644 --- a/locallib.php +++ b/locallib.php @@ -689,7 +689,7 @@ function report_customsql_get_ready_to_run_daily_reports($timenow) { foreach ($reports as $id => $r) { // Check whether the report is ready to run. if (!report_customsql_is_daily_report_ready($r, $timenow)) { - //continue; + continue; } $reportstorun[$id] = $r; } From b62f84c867c8803f6a175ae973556f58abdeed4b Mon Sep 17 00:00:00 2001 From: Hamish Dewe Date: Wed, 15 May 2019 12:08:22 +1200 Subject: [PATCH 5/5] Clarify reasons for using email_to_user instead of message_send --- locallib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locallib.php b/locallib.php index df14250..f903883 100644 --- a/locallib.php +++ b/locallib.php @@ -704,7 +704,7 @@ function report_customsql_get_ready_to_run_daily_reports($timenow) { */ function report_customsql_send_email_notification($recipient, $message, $csvfilename = null) { global $CFG; - // Messaging does not support attachments, so send an email if attachment is required + // Our attachment is not a stored_file, so is not supported by the Messaging API. Send an email if attachment is required if ($csvfilename) { $parts = explode('/', $csvfilename); $filename = $parts[count($parts) - 1];