Custom report Download button is not working

Custom report Download button is not working

by Alexander De La Garza -
Number of replies: 5

Good day,

Still new to PHP and moodle, I am creating a custom report which shows as a table, the download button shows but I cant get the download button to work,

It displays the button, but when clicking it, the URL appends  ?sesskey= {numbers}&downlaod=csv (or the format selected)

but there is no table and nothing downloads, 

I have been using https://docs.moodle.org/dev/lib/tablelib.php as a reference, below is my code:


   
    $records = $DB->get_records_sql($sql, $variables);

    $table->define_baseurl($url);
    $table->show_download_buttons_at(array(TABLE_P_BOTTOM));
    $table->is_downloading($download, 'test', 'testing123');

    $table->define_columns(array( All the variables));
    $table->define_headers(array( All the variables));

    $table->setup();

    if ($records) {
        foreach ($records as $record) {

            $row = array();
            $row[] = $record->All the variables;
            $table->add_data($row);
        }
    }

    $table->finish_output();



    if (!$table->is_downloading($download, $reportname)) {
        echo $OUTPUT->footer();
    }





Average of ratings: -
In reply to Alexander De La Garza

Re: Custom report Download button is not working

by Benjamin Ellis -
Picture of Particularly helpful Moodlers
Hello

downlaod=csv OR download=csv?  It might be the spelling mistake - but I am assuming you have something to do with the link that is created.


In reply to Benjamin Ellis

Re: Custom report Download button is not working

by Alexander De La Garza -
at the end of the URL its adding "?sesskey={bunch of chars}&download=csv"

so its not the spelling,
In reply to Alexander De La Garza

Re: Custom report Download button is not working

by Renaat Debleu -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
You skipped 1 line:

$download = optional_param('download', '', PARAM_ALPHA);

 
Average of ratings: Useful (1)
In reply to Renaat Debleu

Re: Custom report Download button is not working

by Alexander De La Garza -
Thanks, I have that at the top of the file in the variable declarations, is there a way i can edit my post to show it ?
In reply to Alexander De La Garza

Re: Custom report Download button is not working

by Alexander De La Garza -
I was able to get the CSV file to actually download but when I open it, its empty,

I tried testing by adding a echo "test
"; before the $table->finish_output(); ....and "test
" gets captured in the CSV file, but the table does not,

Out of curiosity I tried adding another echo "test2
"; after the table output, and that does not get added to the csv file

How can I get the table to show in the CSV file export?