Merging language-pack-related updates from Eloy's MOODLE_16_UTF8 branch.
[moodle.git] / lib / excellib.class.php
CommitLineData
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// //
10// Copyright (C) 2001-3001 Martin Dougiamas http://dougiamas.com //
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
27/// We need to add this to allow "our" PEAR package to work smoothly
28/// without modifying one bit, putting it in the 1st place of the
29/// include_path to be localised by Moodle without problems
30ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
31
32require_once 'Spreadsheet/Excel/Writer.php';
33
34/**
35* Define and operate over one Moodle Workbook.
36*
37* A big part of this class acts as a wrapper over the PEAR
38* Spreadsheet_Excel_Writer_Workbook and OLE libraries
39* maintaining Moodle functions isolated from underlying code.
40*/
41class MoodleExcelWorkbook {
42
43 var $pear_excel_workbook;
44
45 /* Constructs one Moodle Workbook.
46 * @param string $filename The name of the file
47 */
48 function MoodleExcelWorkbook($filename) {
49 /// Internally, create one PEAR Spreadsheet_Excel_Writer_Workbook class
50 $this->pear_excel_workbook = new Spreadsheet_Excel_Writer($filename);
51 /// Prepare it to accept UTF-16LE data and to encode it properly
52 $this->pear_excel_workbook->setVersion(8);
53 }
54
55 /* Create one Moodle Worksheet
56 * @param string $name Name of the sheet
57 */
58 function &add_worksheet($name = '') {
59 /// Create the Moodle Worksheet. Returns one pointer to it
60 $ws =& new MoodleExcelWorksheet ($name, $this->pear_excel_workbook);
61 return $ws;
62 }
63
64 /* Create one Moodle Format
65 * @param array $properties array of properties [name]=value;
66 * valid names are set_XXXX existing
67 * functions without the set_ part
68 * i.e: [bold]=1 for set_bold(1)...Optional!
69 */
70 function &add_format($properties = array()) {
71 /// Create the Moodle Format. Returns one pointer to it
72 $ft =& new MoodleExcelFormat ($this->pear_excel_workbook, $properties);
73 return $ft;
74 }
75
76 /* Close the Moodle Workbook
77 */
78 function close() {
79 $this->pear_excel_workbook->close();
80 }
81
82 /* Write the correct HTTP headers
83 * @param string $name Name of the downloaded file
84 */
85 function send($filename) {
86 $this->pear_excel_workbook->send($filename);
87 }
88}
89
90/**
91* Define and operate over one Worksheet.
92*
93* A big part of this class acts as a wrapper over the PEAR
94* Spreadsheet_Excel_Writer_Workbook and OLE libraries
95* maintaining Moodle functions isolated from underlying code.
96*/
97class MoodleExcelWorksheet {
98
99 var $pear_excel_worksheet;
100
101 /* Constructs one Moodle Worksheet.
102 * @param string $filename The name of the file
103 * @param object $workbook The internal PEAR Workbook onject we are creating
104 */
105 function MoodleExcelWorksheet($name, &$workbook) {
106 /// Internally, add one sheet to the workbook
107 $this->pear_excel_worksheet =& $workbook->addWorksheet($name);
108 /// Set encoding to UTF-16LE
109 $this->pear_excel_worksheet->setInputEncoding('UTF-16LE');
110 }
111
112 /* Write one string somewhere in the worksheet
113 * @param integer $row Zero indexed row
114 * @param integer $col Zero indexed column
115 * @param string $str The string to write
116 * @param mixed $format The XF format for the cell
117 */
118 function write_string($row, $col, $str, $format=0) {
119 /// Calculate the internal PEAR format
120 $format = $this->MoodleExcelFormat2PearExcelFormat($format);
121 /// Loading the textlib singleton instance. We are going to need it.
122 $textlib = textlib_get_instance();
123 /// Convert the text from its original encoding to UTF-16LE
124 $str = $textlib->convert($str, current_charset(), 'utf-16le');
125 /// Add the string safely to the PEAR Worksheet
126 $this->pear_excel_worksheet->writeString($row, $col, $str, $format);
127 }
128
129 /* Write one number somewhere in the worksheet
130 * @param integer $row Zero indexed row
131 * @param integer $col Zero indexed column
132 * @param float $num The number to write
133 * @param mixed $format The XF format for the cell
134 */
135 function write_number($row, $col, $num, $format=0) {
136 /// Calculate the internal PEAR format
137 $format = $this->MoodleExcelFormat2PearExcelFormat($format);
138 /// Add the number safely to the PEAR Worksheet
139 $this->pear_excel_worksheet->writeNumber($row, $col, $num, $format);
140 }
141
142 /* Write one url somewhere in the worksheet
143 * @param integer $row Zero indexed row
144 * @param integer $col Zero indexed column
145 * @param string $url The url to write
146 * @param mixed $format The XF format for the cell
147 */
148 function write_url($row, $col, $url, $format=0) {
149 /// Calculate the internal PEAR format
150 $format = $this->MoodleExcelFormat2PearExcelFormat($format);
151 /// Add the url safely to the PEAR Worksheet
152 $this->pear_excel_worksheet->writeUrl($row, $col, $url, $format);
153 }
154
155 /* Write one formula somewhere in the worksheet
156 * @param integer $row Zero indexed row
157 * @param integer $col Zero indexed column
158 * @param string $formula The formula to write
159 * @param mixed $format The XF format for the cell
160 */
161 function write_formula($row, $col, $formula, $format=0) {
162 /// Calculate the internal PEAR format
163 $format = $this->MoodleExcelFormat2PearExcelFormat($format);
164 /// Add the formula safely to the PEAR Worksheet
165 $this->pear_excel_worksheet->writeFormula($row, $col, $formula, $format);
166 }
167
168 /* Write one blanck somewhere in the worksheet
169 * @param integer $row Zero indexed row
170 * @param integer $col Zero indexed column
171 * @param mixed $format The XF format for the cell
172 */
173 function write_blank($row, $col, $format=0) {
174 /// Calculate the internal PEAR format
175 $format = $this->MoodleExcelFormat2PearExcelFormat($format);
176 /// Add the blank safely to the PEAR Worksheet
177 $this->pear_excel_worksheet->writeBlank($row, $col, $format);
178 }
179
180 /* Write anything somewhere in the worksheet
181 * Type will be automatically detected
182 * @param integer $row Zero indexed row
183 * @param integer $col Zero indexed column
184 * @param mixed $token What we are writing
185 * @param mixed $format The XF format for the cell
186 */
187 function write($row, $col, $token, $format=0) {
188
189 /// Analyse what are we trying to send
190 if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
191 /// Match number
192 return $this->write_number($row, $col, $token, $format);
193 } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
194 /// Match http or ftp URL
195 return $this->write_url($row, $col, $token, '', $format);
196 } elseif (preg_match("/^mailto:/", $token)) {
197 /// Match mailto:
198 return $this->write_url($row, $col, $token, '', $format);
199 } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
200 /// Match internal or external sheet link
201 return $this->write_url($row, $col, $token, '', $format);
202 } elseif (preg_match("/^=/", $token)) {
203 /// Match formula
204 return $this->write_formula($row, $col, $token, $format);
205 } elseif (preg_match("/^@/", $token)) {
206 /// Match formula
207 return $this->write_formula($row, $col, $token, $format);
208 } elseif ($token == '') {
209 /// Match blank
210 return $this->write_blank($row, $col, $format);
211 } else {
212 /// Default: match string
213 return $this->write_string($row, $col, $token, $format);
214 }
215 }
216
217 /* Returns the PEAR Excel Format for one Moodle Excel Format
218 * @param mixed MoodleExcelFormat object
219 * @return mixed PEAR Excel Format object
220 */
221 function MoodleExcelFormat2PearExcelFormat($format) {
222 if ($format != 0) {
223 return $format->pear_excel_format;
224 } else {
225 return 0;
226 }
227 }
228}
229
230
231/**
232* Define and operate over one Format.
233*
234* A big part of this class acts as a wrapper over the PEAR
235* Spreadsheet_Excel_Writer_Workbook and OLE libraries
236* maintaining Moodle functions isolated from underlying code.
237*/
238class MoodleExcelFormat {
239
240 var $pear_excel_format;
241
242 /* Constructs one Moodle Format.
243 * @param object $workbook The internal PEAR Workbook onject we are creating
244 */
245 function MoodleExcelFormat(&$workbook, $properties = array()) {
246 /// Internally, add one sheet to the workbook
247 $this->pear_excel_format =& $workbook->addFormat();
248 /// If we have something in the array of properties, compute them
249 foreach($properties as $property => $value) {
250 if(method_exists($this,"set_$property")) {
251 $aux = 'set_'.$property;
252 $this->$aux($value);
253 }
254 }
255 }
256
257 /* Set weight of the format
258 * @param integer $weight Weight for the text, 0 maps to 400 (normal text),
259 * 1 maps to 700 (bold text). Valid range is: 100-1000.
260 * It's Optional, default is 1 (bold).
261 */
262 function set_bold($weight = 1) {
263 /// Set the bold safely to the PEAR Format
264 $this->pear_excel_format->setBold($weight);
265 }
266
267 /* Set underline of the format
268 * @param integer $underline The value for underline. Possible values are:
269 * 1 => underline, 2 => double underline
270 */
271 function set_underline($underline) {
272 /// Set the underline safely to the PEAR Format
273 $this->pear_excel_format->setUnderline($underline);
274 }
275
276 /* Set italic of the format
277 */
278 function set_italic() {
279 /// Set the italic safely to the PEAR Format
280 $this->pear_excel_format->setItalic();
281 }
282
283 /* Set strikeout of the format
284 */
285 function set_strikeout() {
286 /// Set the strikeout safely to the PEAR Format
287 $this->pear_excel_format->setStrikeOut();
288 }
289
290 /* Set outlining of the format
291 */
292 function set_outline() {
293 /// Set the outlining safely to the PEAR Format
294 $this->pear_excel_format->setOutLine();
295 }
296
297 /* Set shadow of the format
298 */
299 function set_shadow() {
300 /// Set the shadow safely to the PEAR Format
301 $this->pear_excel_format->setShadow();
302 }
303
304 /* Set the script of the text
305 * @param integer $script The value for script type. Possible values are:
306 * 1 => superscript, 2 => subscript
307 */
308 function set_script($script) {
309 /// Set the script safely to the PEAR Format
310 $this->pear_excel_format->setScript($script);
311 }
312
313 /* Set color of the format
314 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
315 */
316 function set_color($color) {
317 /// Set the background color safely to the PEAR Format
318 $this->pear_excel_format->setColor($color);
319 }
320
321 /* Set foreground color of the format
322 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
323 */
324 function set_fg_color($color) {
325 /// Set the foreground color safely to the PEAR Format
326 $this->pear_excel_format->setFgColor($color);
327 }
328
329 /* Set background color of the format
330 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
331 */
332 function set_bg_color($color) {
333 /// Set the background color safely to the PEAR Format
334 $this->pear_excel_format->setBgColor($color);
335 }
336
337 /* Set the fill pattern of the format
338 * @param integer Optional. Defaults to 1. Meaningful values are: 0-18
339 * 0 meaning no background.
340 */
341 function set_pattern($pattern=1) {
342 /// Set the fill pattern safely to the PEAR Format
343 $this->pear_excel_format->setPattern($pattern);
344 }
345
346 /* Set text wrap of the format
347 */
348 function set_text_wrap() {
349 /// Set the shadow safely to the PEAR Format
350 $this->pear_excel_format->setTextWrap();
351 }
352
353 /* Set the cell alignment of the format
354 * @param string $location alignment for the cell ('left', 'right', etc...)
355 */
356 function set_align($location) {
357 /// Set the alignment of the cell safely to the PEAR Format
358 $this->pear_excel_format->setAlign($location);
359 }
360
361 /* Set the cell horizontal alignment of the format
362 * @param string $location alignment for the cell ('left', 'right', etc...)
363 */
364 function set_h_align($location) {
365 /// Set the alignment of the cell safely to the PEAR Format
366 $this->pear_excel_format->setHAlign($location);
367 }
368
369 /* Set the cell vertical alignment of the format
370 * @param string $location alignment for the cell ('top', 'vleft', etc...)
371 */
372 function set_v_align($location) {
373 /// Set the alignment of the cell safely to the PEAR Format
374 $this->pear_excel_format->setVAlign($location);
375 }
376
377 /* Set the top border of the format
378 * @param integer $style style for the cell. 1 => thin, 2 => thick
379 */
380 function set_top($style) {
381 /// Set the top border of the cell safely to the PEAR Format
382 $this->pear_excel_format->setTop($style);
383 }
384
385 /* Set the bottom border of the format
386 * @param integer $style style for the cell. 1 => thin, 2 => thick
387 */
388 function set_bottom($style) {
389 /// Set the bottom border of the cell safely to the PEAR Format
390 $this->pear_excel_format->setBottom($style);
391 }
392
393 /* Set the left border of the format
394 * @param integer $style style for the cell. 1 => thin, 2 => thick
395 */
396 function set_left($style) {
397 /// Set the left border of the cell safely to the PEAR Format
398 $this->pear_excel_format->setLeft($style);
399 }
400
401 /* Set the right border of the format
402 * @param integer $style style for the cell. 1 => thin, 2 => thick
403 */
404 function set_right($style) {
405 /// Set the right border of the cell safely to the PEAR Format
406 $this->pear_excel_format->setRight($style);
407 }
408
409 /**
410 * Set cells borders to the same style
411 * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
412 */
413 function set_border($style) {
414 /// Set all the borders of the cell safely to the PEAR Format
415 $this->pear_excel_format->setBorder($style);
416 }
417
418 /* Set the numerical format of the format
419 * It can be date, time, currency, etc...
420 /* Set the numerical format of the format
421 * It can be date, time, currency, etc...
422 * @param integer $num_format The numeric format
423 */
424 function set_num_format($num_format) {
425 /// Set the numerical format safely to the PEAR Format
426 $this->pear_excel_format->setNumFormat($num_format);
427 }
428
429}
430
431?>