diff --git a/lang/en/report_customsql.php b/lang/en/report_customsql.php index 9728d06..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.'; @@ -138,6 +140,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/locallib.php b/locallib.php index b441c24..f903883 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))); } @@ -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; + // 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]; + $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); + } } /** 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(); 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')); } }