XLS Export Garbled

XLS Export Garbled

by Elvedin Trnjanin -
Number of replies: 1
I am trying to add support for exporting student ID numbers based on if the user was enrolled in that course through flat file enrollment and if not, just print an empty string for that row in the column.

The function to check for that is the wasflatfileenrolled() and it returns a boolean, or int 0 on failure such as NULL arguments. The problem is that upon adding the function which checks for flat file enrollment, the XLS output gets garbled beyond recognition. The same changes to the TXT export work fine however, and I haven't been able to test ODS exports.

The code is below and I've denoted my changes by the comment prefix DELTA

} else if ($download == "xls" and confirm_sesskey()) {
require_once("../lib/excellib.class.php");
global $COURSE; // DELTA: added to get $COURSE->id

/// Calculate file name
$downloadfilename = clean_filename("$course->shortname $strgrades.xls");
/// Creating a workbook
$workbook = new MoodleExcelWorkbook("-");
/// Sending HTTP headers
$workbook->send($downloadfilename);
/// Adding the worksheet
$myxls =& $workbook->add_worksheet($strgrades);

/// Print names of all the fields
$myxls->write_string(0,0,get_string("firstname"));
$myxls->write_string(0,1,get_string("lastname"));
$myxls->write_string(0,2,get_string("idnumber"));
$myxls->write_string(0,3,get_string("username")); // DELTA: added username column, incremented all column indexes to compensate
$myxls->write_string(0,4,get_string("institution"));
$myxls->write_string(0,5,get_string("department"));
$myxls->write_string(0,6,get_string("email"));
$pos=7; // DELTA: new column added, increment $pos
foreach ($columns as $column) {
$myxls->write_string(0,$pos++,strip_tags($column));
}
$myxls->write_string(0,$pos,get_string("total"));

/// Print all the lines of data.

$i = 0;
if (!empty($grades)) {
foreach ($grades as $studentid => $studentgrades) {
$i++;
$student = $students[$studentid];
if (empty($totals[$student->id])) {
$totals[$student->id] = '';
}

$myxls->write_string($i,0,$student->firstname);
$myxls->write_string($i,1,$student->lastname);
if (wasflatfileenrolled($studentid, $COURSE->id) === true) { // DELTA: function description is elsewhere; incremented column index for subsequent entries
$myxls->write_string($i,2,$student->idnumber);
}
else {
$myxls->write_string($i,2,"");
}
$myxls->write_string($i,3,$student->username);
$myxls->write_string($i,4,$student->institution);
$myxls->write_string($i,5,$student->department);
$myxls->write_string($i,6,$student->email);
$j=7; // DELTA: incremented for the extra column
foreach ($studentgrades as $grade) {
if (is_numeric($grade)) {
$myxls->write_number($i,$j++,strip_tags($grade));
}
else {
$myxls->write_string($i,$j++,strip_tags($grade));
}
}
$myxls->write_number($i,$j,$totals[$student->id]);
}
}

/// Close the workbook
$workbook->close();

exit;