Moodle php code : To get download report in multiple format

This will display the download option at page

echo $OUTPUT->download_dataformat_selector("Download",
        '/local/plugin/report.php', 'export');

Use following code to call the function

if ($export) {

Function to download data in different format :

// dataform : csv/pdf/xls/html
function download_data($dataformat) {

    global $DB, $USER;
    if (ob_get_length()) {
        throw new coding_exception("Output can not be buffered before calling download_as_dataformat");
    $classname = 'dataformat_' . $dataformat . '\writer';
    if (!class_exists($classname)) {
        throw new coding_exception("Unable to locate dataformat/$dataformat/classes/writer.php");
    $format = new $classname;

    // The data format export could take a while to generate...

    // Close the session so that the users other tabs in the same session are not blocked.

    // If this file was requested from a form, then mark download as complete (before sending headers).

    $columns = array(
    'First Name',
    'Last Name',
    $filename = date("dmY");
    // This exists to support all dataformats - see MDL-56046.
    if (method_exists($format, 'write_header')) {
    } else {

        if ($users = $DB->get_records("users"))
            foreach ($users as $user)
                $assigned_users[$usercontext->instanceid] = fullname($usercontext);
                $row = [];
                $row[] = $user->firstname;
                $row[] = $user->lastname;
                $format->write_record($row, $c++);

    if (method_exists($format, 'write_footer')) {
    } else {

Leave a Reply

Your email address will not be published. Required fields are marked *