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

Attach csv #56

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 3 additions & 0 deletions lang/en/report_customsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';
Expand Down Expand Up @@ -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';
Expand Down
57 changes: 36 additions & 21 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

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

/**
Expand Down
19 changes: 16 additions & 3 deletions view.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion view_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public function definition() {
}
}

$this->add_action_buttons(true, 'Run report');
$this->add_action_buttons(true, get_string('runreport', 'report_customsql'));
}
}