echo '<center>';
    print_single_button($CFG->wwwroot . '/course/report/trainingsessions/index.php', $options, get_string('generateXLS', 'report_trainingsessions'), 'get');
    echo '</center>';
} else {
    /// generate XLS
    if ($groupid) {
        $filename = 'training_group_' . $groupid . '_report_' . date('d-M-Y', time()) . '.xls';
    } else {
        $filename = 'training_course_' . $id . '_report_' . date('d-M-Y', time()) . '.xls';
    }
    $workbook = new MoodleExcelWorkbook("-");
    // Sending HTTP headers
    $workbook->send($filename);
    $xls_formats = training_reports_xls_formats($workbook);
    $startrow = 15;
    foreach ($targetusers as $auser) {
        $row = $startrow;
        $worksheet = training_reports_init_worksheet($auser->id, $row, $xls_formats, $workbook);
        $logusers = $auser->id;
        $logs = use_stats_extract_logs($from, time(), $auser->id, $course->id);
        $aggregate = use_stats_aggregate_logs($logs, 'module');
        $overall = training_reports_print_xls($worksheet, $coursestructure, $aggregate, $done, $row, $xls_formats);
        $data->items = $items;
        $data->done = $done;
        $data->from = $from;
        $data->elapsed = $overall->elapsed;
        $data->events = $overall->events;
        training_reports_print_header_xls($worksheet, $auser->id, $course->id, $data, $xls_formats);
    }
    $workbook->close();
}
/**
* a raster for xls printing of a report structure.
*
*/
function training_reports_print_xls(&$worksheet, &$structure, &$aggregate, &$done, &$row, &$xls_formats, $level = 1)
{
    if (empty($structure)) {
        $str = get_string('nostructure', 'report_trainingsessions');
        $worksheet->write_string($row, 1, $str);
        return;
    }
    // makes a blank dataobject.
    if (!isset($dataobject)) {
        $dataobject->elapsed = 0;
        $dataobject->events = 0;
        $dataobject->evaluating = 0;
        $dataobject->evaluatingevents = 0;
        $dataobject->preparing = 0;
        $dataobject->preparingevents = 0;
        $dataobject->executing = 0;
        $dataobject->executingevents = 0;
        $dataobject->mentored = 0;
        $dataobject->mentoredevents = 0;
        $dataobject->freerun = 0;
        $dataobject->freerunevents = 0;
    }
    if (is_array($structure)) {
        foreach ($structure as $element) {
            if (isset($element->instance) && empty($element->instance->visible)) {
                continue;
            }
            // non visible items should not be displayed
            $res = training_reports_print_xls($worksheet, $element, $aggregate, $done, $row, $xls_formats, $level);
            $dataobject->elapsed += $res->elapsed;
            $dataobject->events += $res->events;
            $dataobject->evaluating += $res->evaluating;
            $dataobject->evaluatingevents += $res->evaluatingevents;
            $dataobject->preparing += $res->preparing;
            $dataobject->preparingevents += $res->preparingevents;
            $dataobject->executing += $res->executing;
            $dataobject->executingevents += $res->executingevents;
            $dataobject->mentored += $res->mentored;
            $dataobject->mentoredevents += $res->mentoredevents;
            $dataobject->freerun += $res->freerun;
            $dataobject->freerunevents += $res->freerunevents;
        }
    } else {
        $format = isset($xls_formats['a' . $level]) ? $xls_formats['a' . $level] : $xls_formats['z'];
        $timeformat = $xls_formats['zt'];
        if (!isset($element->instance) || !empty($element->instance->visible)) {
            // non visible items should not be displayed
            if (!empty($structure->name)) {
                // write element title
                $indent = str_pad('', 3 * $level, ' ');
                $str = $indent . shorten_text($structure->name, 85);
                $worksheet->set_row($row, 18, $format);
                $worksheet->write_string($row, 0, $str, $format);
                $worksheet->write_blank($row, 1, $format);
                if (isset($structure->id) && !empty($aggregate[$structure->type][$structure->id])) {
                    $done++;
                    $dataobject = $aggregate[$structure->type][$structure->id];
                }
                $thisrow = $row;
                // saves the current row for post writing aggregates
                $row++;
                if (!empty($structure->subs)) {
                    debug_trace("with subs");
                    $res = training_reports_print_xls($worksheet, $structure->subs, $aggregate, $done, $row, $xls_formats, $level + 1);
                    $dataobject->elapsed += $res->elapsed;
                    $dataobject->events += $res->events;
                    $dataobject->preparing += $res->preparing;
                    $dataobject->preparingevents += $res->preparingevents;
                    $dataobject->executing += $res->executing;
                    $dataobject->executingevents += $res->executingevents;
                    $dataobject->evaluating += $res->evaluating;
                    $dataobject->evaluatingevents += $res->evaluatingevents;
                    $dataobject->mentored += $res->mentored;
                    $dataobject->mentoredevents += $res->mentoredevents;
                    $dataobject->freerun += $res->freerun;
                    $dataobject->freerunevents += $res->freerunevents;
                }
                $str = training_reports_format_time($dataobject->elapsed, 'xls');
                $worksheet->write_number($thisrow, 2, $str, $timeformat);
                $worksheet->write_number($thisrow, 3, $dataobject->events, $format);
                // plug here specific details
                if (!empty($dataobject->evaluating)) {
                    $str = training_reports_format_time($dataobject->evaluating, 'xls');
                    $worksheet->write_number($thisrow, 4, $str, $timeformat);
                    $worksheet->write_number($thisrow, 5, $dataobject->evaluatingevents, $format);
                } else {
                    $dataobject->evaluating = 0;
                    $dataobject->evaluatingevents = 0;
                }
                if (!empty($dataobject->preparing)) {
                    $str = training_reports_format_time($dataobject->preparing, 'xls');
                    $worksheet->write_number($thisrow, 6, $str, $timeformat);
                    $worksheet->write_number($thisrow, 7, $dataobject->preparingevents, $format);
                } else {
                    $dataobject->preparing = 0;
                    $dataobject->preparingevents = 0;
                }
                if (!empty($dataobject->executing)) {
                    $str = training_reports_format_time($dataobject->executing, 'xls');
                    $worksheet->write_number($thisrow, 8, $str, $timeformat);
                    $worksheet->write_number($thisrow, 9, $dataobject->executingevents, $format);
                } else {
                    $dataobject->executing = 0;
                    $dataobject->executingevents = 0;
                }
                // for trainees
                if (!empty($dataobject->mentored)) {
                    $str = training_reports_format_time($dataobject->mentored, 'xls');
                    $worksheet->write_number($thisrow, 10, $str, $timeformat);
                    $worksheet->write_number($thisrow, 11, $dataobject->mentoredevents, $format);
                } else {
                    $dataobject->mentored = 0;
                    $dataobject->mentoredevents = 0;
                }
                if (!empty($dataobject->freerun)) {
                    $str = training_reports_format_time($dataobject->freerun, 'xls');
                    $worksheet->write_number($thisrow, 12, $str, $timeformat);
                    $worksheet->write_number($thisrow, 13, $dataobject->freerunevents, $format);
                } else {
                    $dataobject->freerun = 0;
                    $dataobject->freerunevents = 0;
                }
            } else {
                // It is only a structural module that should not impact on level
                if (isset($structure->id) && !empty($aggregate[$structure->type][$structure->id])) {
                    $dataobject = $aggregate[$structure->type][$structure->id];
                }
                if (!empty($structure->subs)) {
                    $res = training_reports_print_xls($worksheet, $structure->subs, $aggregate, $done, $row, $xls_formats, $level);
                    $dataobject->elapsed += $res->elapsed;
                    $dataobject->events += $res->events;
                    $dataobject->preparing += $res->preparing;
                    $dataobject->preparingevents += $res->preparingevents;
                    $dataobject->executing += $res->executing;
                    $dataobject->executingevents += $res->executingevents;
                    $dataobject->evaluating += $res->evaluating;
                    $dataobject->evaluatingevents += $res->evaluatingevents;
                    $dataobject->mentored += $res->mentored;
                    $dataobject->mentoredevents += $res->mentoredevents;
                    $dataobject->freerun += $res->freerun;
                    $dataobject->freerunevents += $res->freerunevents;
                }
            }
        }
    }
    return $dataobject;
}