Very strange behaviour

Very strange behaviour

by Daniele Cordella -
Number of replies: 7
Picture of Core developers Picture of Plugin developers
I am writing the code to download logs to excel, OO and txt.
I wrote this few days ago and I am testing this now.
All is fine on my desktop computer. I can download to excel and to txt all my logs both in Moodle 1.5.3+ and 1.6. Yesterday I moved my "addition" to my portable pc.
Well, what I found was that... my "addition" is working fine on the laptop in Moodle 1.5.3+ but not in Moodle 1.6. The excel file I download is not "well formatted" and, by opening it, rubbish is in it. I spent some hours to check and check again the code and at the end I went to download to Excel the "Item Analysis" of a quiz and I found the same error: "Excel file is not recognized as well formatted". I went back to the desktop computer and... the "Item Analysis" download works fine. What it can be? I made a fresh installation of Moodle 1.6 beta 3 this morning both on the desktop and the portable. They have both the same OS (MacOSX 10.4.6) with the same php version (4.3.6) and the same mysql version (4.1.18). Do you have suggestions?

Attached is my "addition". Rev03 is for Moodle 1.6 and rev21 is for Moodle 1.5.3+
To test it backup and overwrite the existing files (2 files in Moodle 1.5.3+ and three files in Moodle 1.6) . You know better than me where do they are. Languages files are included.

Please reply me with an answer. I am getting crazy.
And to close: where can I find a macintosh free php IDE???????????
Average of ratings: -
In reply to Daniele Cordella

Re: Very strange behaviour

by Gordon Bateson -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Hi Daniele,
Moodle 1.5 and Moodle 1.6 have different Excel libraries, so perhaps your PHP code does not take this into account?

Here is an example of PHP code which will create Excel files in Moodle 1.5 and Moodle 1.6. I offer it to you in case it may be useful:

$downloadfilename = 'excel-file.xls';

// uncomment the following line, if this code is in a function
// global $C
FG;

// create Excel workbook
if (file_exists("$CFG->libdir/excellib.class.php")) {
    // Moodle >= 1.6
    require_once("$CFG->libdir/excellib.class.php");
    $wb = new MoodleExcelWorkbook("-");
} else {
    // Moodle <= 1.5
    require_once("$CFG->libdir/excel/Worksheet.php");
    require_once("$CFG->libdir/excel/Workbook.php");
    $wb = new Workbook("-");
}

// send headers
if (method_exists($wb, 'send')) {

    // Moodle >=1.6
    $wb->send($downloadfilename);
} else {
    // Moodle <=1.5
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$downloadfilename" );
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
    header("Pragma: public");
}

// create worksheet
$ws = &$wb->add_worksheet('worksheet name');


// set cell format using:
//     $fmt_properties = array();
//     $fmt = &$wb->add_format($fmt_properties);

// print your cells here using:
//     $ws->write_number($row, $col, $cellcontent, $fmt);
//     $ws->write_string($row, $col, $cellcontent, $fmt);

// close the workbook (and send it to the browser)
$wb->close();

all the best
Gordon

In reply to Gordon Bateson

Re: Very strange behaviour

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Thank you Gordon.
I changed my 1.6 code but it still works on my desktop computer and doesn't work on the laptop.
I'll continue my investigation.
Just a question. With the new library how can I change the width of an Excel column?
I used: $worksheet[1]->set_column(1, 1, 20);
in 1.5.3+ but now this doesn't work any more.
In reply to Gordon Bateson

Re: Very strange behaviour

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Hei Gordon
I read the uncorrect Excel file with
hexdump -C wrongExcelFile.xls
and I found that an extra line feed is the first character of the file (0a). By removing it manually I can open the file properly.
Which can be the source of the extra line feed? Attached is the code I modified on the basis of your suggestions.
Thank you in advance.
In reply to Daniele Cordella

Re: Very strange behaviour

by Eloy Lafuente (stronk7) -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Hi Daniele,

I've slightly updated excellib.class.php to support both the workshet->set_column() and workshet->set_row() methods. I've attached it here...could you test them? (I'm running really out of time these days). Feedback will be welcome to send the updated lib to CVS.

TIA and ciao smile
In reply to Eloy Lafuente (stronk7)

Re: Very strange behaviour

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Hi Eloy and thank you for your support.
I tested your two updated function and they worked fine to me.
But I tested them only on my desktop PC because they didn't solve my problem of not "well formed excel file" on my laptop. I still have the extra 0a char at BOF.
In reply to Daniele Cordella

Re: Very strange behaviour

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Which can be the source of the extra line feed?

Most probably an extra line feed at the end of some included php file, after the closing '?>' tag. Usual suspect is config.php.

Saludos. Iñaki.
In reply to Iñaki Arenaza

Re: Very strange behaviour

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
GREEEEEEEEEEAT!!!!!!!!!!!!!

Thank you Iñaki
you were totally right.
I was quite a week I was changing all but not the first file in front of me.
Thank you.sorridentegrande sorriso
Thank you, thank you and thank you again.