6aaa17c7 |
1 | <?php // $Id$ |
2 | |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
b7064779 |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com // |
6aaa17c7 |
11 | // (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com // |
12 | // // |
13 | // This program is free software; you can redistribute it and/or modify // |
14 | // it under the terms of the GNU General Public License as published by // |
15 | // the Free Software Foundation; either version 2 of the License, or // |
16 | // (at your option) any later version. // |
17 | // // |
18 | // This program is distributed in the hope that it will be useful, // |
19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
21 | // GNU General Public License for more details: // |
22 | // // |
23 | // http://www.gnu.org/copyleft/gpl.html // |
24 | // // |
25 | /////////////////////////////////////////////////////////////////////////// |
26 | |
7fc1a27d |
27 | //setup.php icludes our hacked pear libs first |
6aaa17c7 |
28 | require_once 'Spreadsheet/Excel/Writer.php'; |
29 | |
30 | /** |
31 | * Define and operate over one Moodle Workbook. |
32 | * |
33 | * A big part of this class acts as a wrapper over the PEAR |
34 | * Spreadsheet_Excel_Writer_Workbook and OLE libraries |
35 | * maintaining Moodle functions isolated from underlying code. |
36 | */ |
37 | class MoodleExcelWorkbook { |
38 | |
39 | var $pear_excel_workbook; |
63620581 |
40 | var $latin_output; |
6aaa17c7 |
41 | |
42 | /* Constructs one Moodle Workbook. |
43 | * @param string $filename The name of the file |
44 | */ |
45 | function MoodleExcelWorkbook($filename) { |
ce5ce136 |
46 | global $CFG; |
6aaa17c7 |
47 | /// Internally, create one PEAR Spreadsheet_Excel_Writer_Workbook class |
48 | $this->pear_excel_workbook = new Spreadsheet_Excel_Writer($filename); |
49 | /// Prepare it to accept UTF-16LE data and to encode it properly |
767e6e45 |
50 | if (empty($CFG->latinexcelexport)) { /// Only if don't want to use latin (win1252) stronger output |
2f078622 |
51 | $this->pear_excel_workbook->setVersion(8); |
63620581 |
52 | $this->latin_output = false; |
53 | } else { /// We want latin (win1252) output |
54 | $this->latin_output = true; |
2f078622 |
55 | } |
ffbf004f |
56 | /// Choose our temporary directory - see MDL-7176, found by paulo.matos |
ce5ce136 |
57 | make_upload_directory('temp/excel', false); |
58 | $this->pear_excel_workbook->setTempDir($CFG->dataroot.'/temp/excel'); |
6aaa17c7 |
59 | } |
60 | |
61 | /* Create one Moodle Worksheet |
62 | * @param string $name Name of the sheet |
63 | */ |
64 | function &add_worksheet($name = '') { |
65 | /// Create the Moodle Worksheet. Returns one pointer to it |
63620581 |
66 | $ws =& new MoodleExcelWorksheet ($name, $this->pear_excel_workbook, $this->latin_output); |
6aaa17c7 |
67 | return $ws; |
68 | } |
69 | |
70 | /* Create one Moodle Format |
71 | * @param array $properties array of properties [name]=value; |
72 | * valid names are set_XXXX existing |
73 | * functions without the set_ part |
74 | * i.e: [bold]=1 for set_bold(1)...Optional! |
75 | */ |
76 | function &add_format($properties = array()) { |
77 | /// Create the Moodle Format. Returns one pointer to it |
78 | $ft =& new MoodleExcelFormat ($this->pear_excel_workbook, $properties); |
79 | return $ft; |
80 | } |
81 | |
82 | /* Close the Moodle Workbook |
83 | */ |
84 | function close() { |
85 | $this->pear_excel_workbook->close(); |
86 | } |
87 | |
88 | /* Write the correct HTTP headers |
89 | * @param string $name Name of the downloaded file |
90 | */ |
91 | function send($filename) { |
92 | $this->pear_excel_workbook->send($filename); |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * Define and operate over one Worksheet. |
98 | * |
99 | * A big part of this class acts as a wrapper over the PEAR |
100 | * Spreadsheet_Excel_Writer_Workbook and OLE libraries |
101 | * maintaining Moodle functions isolated from underlying code. |
102 | */ |
103 | class MoodleExcelWorksheet { |
104 | |
105 | var $pear_excel_worksheet; |
63620581 |
106 | var $latin_output; |
6aaa17c7 |
107 | |
108 | /* Constructs one Moodle Worksheet. |
109 | * @param string $filename The name of the file |
110 | * @param object $workbook The internal PEAR Workbook onject we are creating |
111 | */ |
63620581 |
112 | function MoodleExcelWorksheet($name, &$workbook, $latin_output=false) { |
2f078622 |
113 | |
6aaa17c7 |
114 | /// Internally, add one sheet to the workbook |
115 | $this->pear_excel_worksheet =& $workbook->addWorksheet($name); |
63620581 |
116 | $this->latin_output = $latin_output; |
6aaa17c7 |
117 | /// Set encoding to UTF-16LE |
63620581 |
118 | if (!$this->latin_output) { /// Only if don't want to use latin (win1252) stronger output |
2f078622 |
119 | $this->pear_excel_worksheet->setInputEncoding('UTF-16LE'); |
120 | } |
6aaa17c7 |
121 | } |
122 | |
123 | /* Write one string somewhere in the worksheet |
124 | * @param integer $row Zero indexed row |
125 | * @param integer $col Zero indexed column |
126 | * @param string $str The string to write |
127 | * @param mixed $format The XF format for the cell |
128 | */ |
21fed7a3 |
129 | function write_string($row, $col, $str, $format=null) { |
6aaa17c7 |
130 | /// Calculate the internal PEAR format |
131 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
132 | /// Loading the textlib singleton instance. We are going to need it. |
133 | $textlib = textlib_get_instance(); |
134 | /// Convert the text from its original encoding to UTF-16LE |
63620581 |
135 | if (!$this->latin_output) { /// Only if don't want to use latin (win1252) stronger output |
2f078622 |
136 | $str = $textlib->convert($str, 'utf-8', 'utf-16le'); |
63620581 |
137 | } else { /// else, convert to latin (win1252) |
138 | $str = $textlib->convert($str, 'utf-8', 'windows-1252'); |
2f078622 |
139 | } |
6aaa17c7 |
140 | /// Add the string safely to the PEAR Worksheet |
141 | $this->pear_excel_worksheet->writeString($row, $col, $str, $format); |
142 | } |
143 | |
144 | /* Write one number somewhere in the worksheet |
145 | * @param integer $row Zero indexed row |
146 | * @param integer $col Zero indexed column |
147 | * @param float $num The number to write |
148 | * @param mixed $format The XF format for the cell |
149 | */ |
21fed7a3 |
150 | function write_number($row, $col, $num, $format=null) { |
6aaa17c7 |
151 | /// Calculate the internal PEAR format |
152 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
153 | /// Add the number safely to the PEAR Worksheet |
154 | $this->pear_excel_worksheet->writeNumber($row, $col, $num, $format); |
155 | } |
156 | |
157 | /* Write one url somewhere in the worksheet |
158 | * @param integer $row Zero indexed row |
159 | * @param integer $col Zero indexed column |
160 | * @param string $url The url to write |
161 | * @param mixed $format The XF format for the cell |
162 | */ |
21fed7a3 |
163 | function write_url($row, $col, $url, $format=null) { |
6aaa17c7 |
164 | /// Calculate the internal PEAR format |
165 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
166 | /// Add the url safely to the PEAR Worksheet |
167 | $this->pear_excel_worksheet->writeUrl($row, $col, $url, $format); |
168 | } |
169 | |
170 | /* Write one formula somewhere in the worksheet |
171 | * @param integer $row Zero indexed row |
172 | * @param integer $col Zero indexed column |
173 | * @param string $formula The formula to write |
174 | * @param mixed $format The XF format for the cell |
175 | */ |
21fed7a3 |
176 | function write_formula($row, $col, $formula, $format=null) { |
6aaa17c7 |
177 | /// Calculate the internal PEAR format |
178 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
179 | /// Add the formula safely to the PEAR Worksheet |
180 | $this->pear_excel_worksheet->writeFormula($row, $col, $formula, $format); |
181 | } |
182 | |
183 | /* Write one blanck somewhere in the worksheet |
184 | * @param integer $row Zero indexed row |
185 | * @param integer $col Zero indexed column |
186 | * @param mixed $format The XF format for the cell |
187 | */ |
21fed7a3 |
188 | function write_blank($row, $col, $format=null) { |
6aaa17c7 |
189 | /// Calculate the internal PEAR format |
190 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
191 | /// Add the blank safely to the PEAR Worksheet |
192 | $this->pear_excel_worksheet->writeBlank($row, $col, $format); |
193 | } |
194 | |
195 | /* Write anything somewhere in the worksheet |
196 | * Type will be automatically detected |
197 | * @param integer $row Zero indexed row |
198 | * @param integer $col Zero indexed column |
199 | * @param mixed $token What we are writing |
200 | * @param mixed $format The XF format for the cell |
201 | */ |
21fed7a3 |
202 | function write($row, $col, $token, $format=null) { |
6aaa17c7 |
203 | |
204 | /// Analyse what are we trying to send |
205 | if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) { |
206 | /// Match number |
207 | return $this->write_number($row, $col, $token, $format); |
208 | } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) { |
209 | /// Match http or ftp URL |
210 | return $this->write_url($row, $col, $token, '', $format); |
211 | } elseif (preg_match("/^mailto:/", $token)) { |
212 | /// Match mailto: |
213 | return $this->write_url($row, $col, $token, '', $format); |
214 | } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) { |
215 | /// Match internal or external sheet link |
216 | return $this->write_url($row, $col, $token, '', $format); |
217 | } elseif (preg_match("/^=/", $token)) { |
218 | /// Match formula |
219 | return $this->write_formula($row, $col, $token, $format); |
220 | } elseif (preg_match("/^@/", $token)) { |
221 | /// Match formula |
222 | return $this->write_formula($row, $col, $token, $format); |
223 | } elseif ($token == '') { |
224 | /// Match blank |
225 | return $this->write_blank($row, $col, $format); |
226 | } else { |
227 | /// Default: match string |
228 | return $this->write_string($row, $col, $token, $format); |
229 | } |
230 | } |
231 | |
e6af2cad |
232 | /* Sets the height (and other settings) of one row |
233 | * @param integer $row The row to set |
234 | * @param integer $height Height we are giving to the row (null to set just format withouth setting the height) |
235 | * @param mixed $format The optional XF format we are giving to the row |
236 | * @param bool $hidden The optional hidden attribute |
237 | * @param integer $level The optional outline level (0-7) |
238 | */ |
21fed7a3 |
239 | function set_row ($row, $height, $format = null, $hidden = false, $level = 0) { |
e6af2cad |
240 | /// Calculate the internal PEAR format |
241 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
242 | /// Set the row safely to the PEAR Worksheet |
243 | $this->pear_excel_worksheet->setRow($row, $height, $format, $hidden, $level); |
244 | } |
245 | |
246 | /* Sets the width (and other settings) of one column |
247 | * @param integer $firstcol first column on the range |
248 | * @param integer $lastcol last column on the range |
249 | * @param integer $width width to set |
250 | * @param mixed $format The optional XF format to apply to the columns |
251 | * @param integer $hidden The optional hidden atribute |
252 | * @param integer $level The optional outline level (0-7) |
253 | */ |
21fed7a3 |
254 | function set_column ($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) { |
e6af2cad |
255 | /// Calculate the internal PEAR format |
256 | $format = $this->MoodleExcelFormat2PearExcelFormat($format); |
257 | /// Set the column safely to the PEAR Worksheet |
258 | $this->pear_excel_worksheet->setColumn($firstcol, $lastcol, $width, $format, $hidden, $level); |
259 | } |
260 | |
dd41247e |
261 | /** |
262 | * Set the option to hide gridlines on the printed page. |
263 | * |
264 | * @access public |
265 | */ |
266 | function hide_gridlines() { |
267 | $this->pear_excel_worksheet->hideGridLines(); |
268 | } |
269 | |
270 | /** |
271 | * Set the option to hide gridlines on the worksheet (as seen on the screen). |
272 | * |
273 | * @access public |
274 | */ |
275 | function hide_screen_gridlines() { |
276 | $this->pear_excel_worksheet->hideScreenGridlines(); |
277 | } |
278 | |
279 | /** |
280 | * Insert a 24bit bitmap image in a worksheet. |
281 | * |
282 | * @access public |
283 | * @param integer $row The row we are going to insert the bitmap into |
284 | * @param integer $col The column we are going to insert the bitmap into |
285 | * @param string $bitmap The bitmap filename |
286 | * @param integer $x The horizontal position (offset) of the image inside the cell. |
287 | * @param integer $y The vertical position (offset) of the image inside the cell. |
288 | * @param integer $scale_x The horizontal scale |
289 | * @param integer $scale_y The vertical scale |
290 | */ |
291 | function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1) { |
292 | /// Add the bitmap safely to the PEAR Worksheet |
293 | $this->pear_excel_worksheet->insertBitmap($row, $col, $bitmap, $x, $y, $scale_x, $scale_y); |
294 | } |
295 | |
296 | /** |
297 | * Merges the area given by its arguments. |
298 | * This is an Excel97/2000 method. It is required to perform more complicated |
299 | * merging than the normal setAlign('merge'). |
300 | * |
301 | * @access public |
302 | * @param integer $first_row First row of the area to merge |
303 | * @param integer $first_col First column of the area to merge |
304 | * @param integer $last_row Last row of the area to merge |
305 | * @param integer $last_col Last column of the area to merge |
306 | */ |
307 | function merge_cells($first_row, $first_col, $last_row, $last_col) { |
308 | /// Merge cells safely to the PEAR Worksheet |
309 | $this->pear_excel_worksheet->mergeCells($first_row, $first_col, $last_row, $last_col); |
310 | } |
311 | |
6aaa17c7 |
312 | /* Returns the PEAR Excel Format for one Moodle Excel Format |
313 | * @param mixed MoodleExcelFormat object |
314 | * @return mixed PEAR Excel Format object |
315 | */ |
316 | function MoodleExcelFormat2PearExcelFormat($format) { |
21fed7a3 |
317 | if ($format) { |
6aaa17c7 |
318 | return $format->pear_excel_format; |
319 | } else { |
21fed7a3 |
320 | return null; |
6aaa17c7 |
321 | } |
322 | } |
323 | } |
324 | |
325 | |
326 | /** |
327 | * Define and operate over one Format. |
328 | * |
329 | * A big part of this class acts as a wrapper over the PEAR |
330 | * Spreadsheet_Excel_Writer_Workbook and OLE libraries |
331 | * maintaining Moodle functions isolated from underlying code. |
332 | */ |
333 | class MoodleExcelFormat { |
334 | |
335 | var $pear_excel_format; |
336 | |
337 | /* Constructs one Moodle Format. |
338 | * @param object $workbook The internal PEAR Workbook onject we are creating |
339 | */ |
340 | function MoodleExcelFormat(&$workbook, $properties = array()) { |
341 | /// Internally, add one sheet to the workbook |
342 | $this->pear_excel_format =& $workbook->addFormat(); |
343 | /// If we have something in the array of properties, compute them |
344 | foreach($properties as $property => $value) { |
345 | if(method_exists($this,"set_$property")) { |
346 | $aux = 'set_'.$property; |
347 | $this->$aux($value); |
348 | } |
349 | } |
350 | } |
351 | |
21c36940 |
352 | /** |
353 | * Set the size of the text in the format (in pixels). |
354 | * By default all texts in generated sheets are 10px. |
355 | * @param integer $size Size of the text (in pixels) |
356 | */ |
357 | function set_size($size) { |
358 | /// Set the size safely to the PEAR Format |
359 | $this->pear_excel_format->setSize($size); |
360 | } |
361 | |
6aaa17c7 |
362 | /* Set weight of the format |
363 | * @param integer $weight Weight for the text, 0 maps to 400 (normal text), |
364 | * 1 maps to 700 (bold text). Valid range is: 100-1000. |
365 | * It's Optional, default is 1 (bold). |
366 | */ |
367 | function set_bold($weight = 1) { |
368 | /// Set the bold safely to the PEAR Format |
369 | $this->pear_excel_format->setBold($weight); |
370 | } |
371 | |
372 | /* Set underline of the format |
373 | * @param integer $underline The value for underline. Possible values are: |
374 | * 1 => underline, 2 => double underline |
375 | */ |
376 | function set_underline($underline) { |
377 | /// Set the underline safely to the PEAR Format |
378 | $this->pear_excel_format->setUnderline($underline); |
379 | } |
380 | |
381 | /* Set italic of the format |
382 | */ |
383 | function set_italic() { |
384 | /// Set the italic safely to the PEAR Format |
385 | $this->pear_excel_format->setItalic(); |
386 | } |
387 | |
388 | /* Set strikeout of the format |
389 | */ |
390 | function set_strikeout() { |
391 | /// Set the strikeout safely to the PEAR Format |
392 | $this->pear_excel_format->setStrikeOut(); |
393 | } |
394 | |
395 | /* Set outlining of the format |
396 | */ |
397 | function set_outline() { |
398 | /// Set the outlining safely to the PEAR Format |
399 | $this->pear_excel_format->setOutLine(); |
400 | } |
401 | |
402 | /* Set shadow of the format |
403 | */ |
404 | function set_shadow() { |
405 | /// Set the shadow safely to the PEAR Format |
406 | $this->pear_excel_format->setShadow(); |
407 | } |
408 | |
409 | /* Set the script of the text |
410 | * @param integer $script The value for script type. Possible values are: |
411 | * 1 => superscript, 2 => subscript |
412 | */ |
413 | function set_script($script) { |
414 | /// Set the script safely to the PEAR Format |
415 | $this->pear_excel_format->setScript($script); |
416 | } |
417 | |
418 | /* Set color of the format |
419 | * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]) |
420 | */ |
421 | function set_color($color) { |
422 | /// Set the background color safely to the PEAR Format |
423 | $this->pear_excel_format->setColor($color); |
424 | } |
425 | |
426 | /* Set foreground color of the format |
427 | * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]) |
428 | */ |
429 | function set_fg_color($color) { |
430 | /// Set the foreground color safely to the PEAR Format |
431 | $this->pear_excel_format->setFgColor($color); |
432 | } |
433 | |
434 | /* Set background color of the format |
435 | * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63]) |
436 | */ |
437 | function set_bg_color($color) { |
438 | /// Set the background color safely to the PEAR Format |
439 | $this->pear_excel_format->setBgColor($color); |
440 | } |
441 | |
442 | /* Set the fill pattern of the format |
443 | * @param integer Optional. Defaults to 1. Meaningful values are: 0-18 |
444 | * 0 meaning no background. |
445 | */ |
446 | function set_pattern($pattern=1) { |
447 | /// Set the fill pattern safely to the PEAR Format |
448 | $this->pear_excel_format->setPattern($pattern); |
449 | } |
450 | |
451 | /* Set text wrap of the format |
452 | */ |
453 | function set_text_wrap() { |
454 | /// Set the shadow safely to the PEAR Format |
455 | $this->pear_excel_format->setTextWrap(); |
456 | } |
457 | |
458 | /* Set the cell alignment of the format |
459 | * @param string $location alignment for the cell ('left', 'right', etc...) |
460 | */ |
461 | function set_align($location) { |
462 | /// Set the alignment of the cell safely to the PEAR Format |
463 | $this->pear_excel_format->setAlign($location); |
464 | } |
465 | |
466 | /* Set the cell horizontal alignment of the format |
467 | * @param string $location alignment for the cell ('left', 'right', etc...) |
468 | */ |
469 | function set_h_align($location) { |
470 | /// Set the alignment of the cell safely to the PEAR Format |
471 | $this->pear_excel_format->setHAlign($location); |
472 | } |
473 | |
474 | /* Set the cell vertical alignment of the format |
475 | * @param string $location alignment for the cell ('top', 'vleft', etc...) |
476 | */ |
477 | function set_v_align($location) { |
478 | /// Set the alignment of the cell safely to the PEAR Format |
479 | $this->pear_excel_format->setVAlign($location); |
480 | } |
481 | |
482 | /* Set the top border of the format |
483 | * @param integer $style style for the cell. 1 => thin, 2 => thick |
484 | */ |
485 | function set_top($style) { |
486 | /// Set the top border of the cell safely to the PEAR Format |
487 | $this->pear_excel_format->setTop($style); |
488 | } |
489 | |
490 | /* Set the bottom border of the format |
491 | * @param integer $style style for the cell. 1 => thin, 2 => thick |
492 | */ |
493 | function set_bottom($style) { |
494 | /// Set the bottom border of the cell safely to the PEAR Format |
495 | $this->pear_excel_format->setBottom($style); |
496 | } |
497 | |
498 | /* Set the left border of the format |
499 | * @param integer $style style for the cell. 1 => thin, 2 => thick |
500 | */ |
501 | function set_left($style) { |
502 | /// Set the left border of the cell safely to the PEAR Format |
503 | $this->pear_excel_format->setLeft($style); |
504 | } |
505 | |
506 | /* Set the right border of the format |
507 | * @param integer $style style for the cell. 1 => thin, 2 => thick |
508 | */ |
509 | function set_right($style) { |
510 | /// Set the right border of the cell safely to the PEAR Format |
511 | $this->pear_excel_format->setRight($style); |
512 | } |
513 | |
514 | /** |
515 | * Set cells borders to the same style |
516 | * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick. |
517 | */ |
518 | function set_border($style) { |
519 | /// Set all the borders of the cell safely to the PEAR Format |
520 | $this->pear_excel_format->setBorder($style); |
521 | } |
522 | |
523 | /* Set the numerical format of the format |
524 | * It can be date, time, currency, etc... |
525 | /* Set the numerical format of the format |
526 | * It can be date, time, currency, etc... |
527 | * @param integer $num_format The numeric format |
528 | */ |
529 | function set_num_format($num_format) { |
530 | /// Set the numerical format safely to the PEAR Format |
531 | $this->pear_excel_format->setNumFormat($num_format); |
532 | } |
533 | |
534 | } |
535 | |
536 | ?> |