2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * The xml used here is derived from output of LibreOffice 3.6.4
21 * The design is based on Excel writer abstraction by Eloy Lafuente and others.
24 * @copyright 2006 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
32 * ODS workbook abstraction.
35 * @copyright 2006 Petr Skoda {@link http://skodak.org}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class MoodleODSWorkbook
{
39 protected $worksheets = array();
42 public function __construct($filename) {
43 $this->filename
= $filename;
47 * Create one Moodle Worksheet.
49 * @param string $name Name of the sheet
50 * @return MoodleODSWorksheet
52 public function add_worksheet($name = '') {
53 $ws = new MoodleODSWorksheet($name, $this->worksheets
);
54 $this->worksheets
[] = $ws;
59 * Create one Moodle Format.
61 * @param array $properties array of properties [name]=value;
62 * valid names are set_XXXX existing
63 * functions without the set_ part
64 * i.e: [bold]=1 for set_bold(1)...Optional!
65 * @return MoodleODSFormat
67 public function add_format($properties = array()) {
68 return new MoodleODSFormat($properties);
72 * Close the Moodle Workbook.
74 public function close() {
75 $writer = new MoodleODSWriter($this->worksheets
);
76 $contents = $writer->get_file_content();
78 send_file($contents, $this->filename
, 0, 0, true, true, $writer->get_ods_mimetype());
82 * Not required to use.
83 * @param string $filename Name of the downloaded file
85 public function send($filename) {
86 $this->filename
= $filename;
93 * ODS Cell abstraction.
96 * @copyright 2013 Petr Skoda {@link http://skodak.org}
97 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
108 * ODS Worksheet abstraction.
111 * @copyright 2006 Petr Skoda {@link http://skodak.org}
112 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
114 class MoodleODSWorksheet
{
115 public $data = array();
116 public $columns = array();
117 public $rows = array();
118 public $showgrid = true;
122 * Constructs one Moodle Worksheet.
124 * @param string $name The name of the file
125 * @param array $worksheets existing worksheets
127 public function __construct($name, array $worksheets) {
128 // Replace any characters in the name that Excel cannot cope with.
129 $name = strtr($name, '[]*/\?:', ' ');
133 $name = 'Sheet'.(count($worksheets)+
1);
140 * Write one string somewhere in the worksheet.
142 * @param integer $row Zero indexed row
143 * @param integer $col Zero indexed column
144 * @param string $str The string to write
145 * @param mixed $format The XF format for the cell
147 public function write_string($row, $col, $str, $format = null) {
148 if (!isset($this->data
[$row][$col])) {
149 $this->data
[$row][$col] = new MoodleODSCell();
151 if (is_array($format)) {
152 $format = new MoodleODSFormat($format);
154 $this->data
[$row][$col]->value
= $str;
155 $this->data
[$row][$col]->type
= 'string';
156 $this->data
[$row][$col]->format
= $format;
157 $this->data
[$row][$col]->formula
= null;
161 * Write one number somewhere in the worksheet.
163 * @param integer $row Zero indexed row
164 * @param integer $col Zero indexed column
165 * @param float $num The number to write
166 * @param mixed $format The XF format for the cell
168 public function write_number($row, $col, $num, $format = null) {
169 if (!isset($this->data
[$row][$col])) {
170 $this->data
[$row][$col] = new MoodleODSCell();
172 if (is_array($format)) {
173 $format = new MoodleODSFormat($format);
175 $this->data
[$row][$col]->value
= $num;
176 $this->data
[$row][$col]->type
= 'float';
177 $this->data
[$row][$col]->format
= $format;
178 $this->data
[$row][$col]->formula
= null;
182 * Write one url somewhere in the worksheet.
184 * @param integer $row Zero indexed row
185 * @param integer $col Zero indexed column
186 * @param string $url The url to write
187 * @param mixed $format The XF format for the cell
189 public function write_url($row, $col, $url, $format = null) {
190 if (!isset($this->data
[$row][$col])) {
191 $this->data
[$row][$col] = new MoodleODSCell();
193 if (is_array($format)) {
194 $format = new MoodleODSFormat($format);
196 $this->data
[$row][$col]->value
= $url;
197 $this->data
[$row][$col]->type
= 'string';
198 $this->data
[$row][$col]->format
= $format;
199 $this->data
[$row][$col]->formula
= null;
203 * Write one date somewhere in the worksheet.
205 * @param integer $row Zero indexed row
206 * @param integer $col Zero indexed column
207 * @param string $date The url to write
208 * @param mixed $format The XF format for the cell
210 public function write_date($row, $col, $date, $format = null) {
211 if (!isset($this->data
[$row][$col])) {
212 $this->data
[$row][$col] = new MoodleODSCell();
214 if (is_array($format)) {
215 $format = new MoodleODSFormat($format);
217 $this->data
[$row][$col]->value
= $date;
218 $this->data
[$row][$col]->type
= 'date';
219 $this->data
[$row][$col]->format
= $format;
220 $this->data
[$row][$col]->formula
= null;
224 * Write one formula somewhere in the worksheet.
226 * @param integer $row Zero indexed row
227 * @param integer $col Zero indexed column
228 * @param string $formula The formula to write
229 * @param mixed $format The XF format for the cell
231 public function write_formula($row, $col, $formula, $format = null) {
232 if (!isset($this->data
[$row][$col])) {
233 $this->data
[$row][$col] = new MoodleODSCell();
235 if (is_array($format)) {
236 $format = new MoodleODSFormat($format);
238 $this->data
[$row][$col]->formula
= $formula;
239 $this->data
[$row][$col]->format
= $format;
240 $this->data
[$row][$col]->value
= null;
241 $this->data
[$row][$col]->format
= null;
245 * Write one blank somewhere in the worksheet.
247 * @param integer $row Zero indexed row
248 * @param integer $col Zero indexed column
249 * @param mixed $format The XF format for the cell
251 public function write_blank($row, $col, $format = null) {
252 if (is_array($format)) {
253 $format = new MoodleODSFormat($format);
255 $this->write_string($row, $col, '', $format);
259 * Write anything somewhere in the worksheet,
260 * type will be automatically detected.
262 * @param integer $row Zero indexed row
263 * @param integer $col Zero indexed column
264 * @param mixed $token What we are writing
265 * @param mixed $format The XF format for the cell
267 public function write($row, $col, $token, $format = null) {
268 // Analyse what are we trying to send.
269 if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
271 return $this->write_number($row, $col, $token, $format);
272 } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
273 // Match http or ftp URL
274 return $this->write_url($row, $col, $token, '', $format);
275 } elseif (preg_match("/^mailto:/", $token)) {
277 return $this->write_url($row, $col, $token, '', $format);
278 } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
279 // Match internal or external sheet link
280 return $this->write_url($row, $col, $token, '', $format);
281 } elseif (preg_match("/^=/", $token)) {
283 return $this->write_formula($row, $col, $token, $format);
284 } elseif (preg_match("/^@/", $token)) {
286 return $this->write_formula($row, $col, $token, $format);
287 } elseif ($token == '') {
289 return $this->write_blank($row, $col, $format);
291 // Default: match string
292 return $this->write_string($row, $col, $token, $format);
297 * Sets the height (and other settings) of one row.
299 * @param integer $row The row to set
300 * @param integer $height Height we are giving to the row (null to set just format without setting the height)
301 * @param mixed $format The optional format we are giving to the row
302 * @param bool $hidden The optional hidden attribute
303 * @param integer $level The optional outline level (0-7)
305 public function set_row($row, $height, $format = null, $hidden = false, $level = 0) {
306 if (is_array($format)) {
307 $format = new MoodleODSFormat($format);
311 } else if ($level > 7) {
314 if (!isset($this->rows
[$row])) {
315 $this->rows
[$row] = new stdClass();
317 if (isset($height)) {
318 $this->rows
[$row]->height
= $height;
320 $this->rows
[$row]->format
= $format;
321 $this->rows
[$row]->hidden
= $hidden;
322 $this->rows
[$row]->level
= $level;
326 * Sets the width (and other settings) of one column.
328 * @param integer $firstcol first column on the range
329 * @param integer $lastcol last column on the range
330 * @param integer $width width to set (null to set just format without setting the width)
331 * @param mixed $format The optional format to apply to the columns
332 * @param bool $hidden The optional hidden attribute
333 * @param integer $level The optional outline level (0-7)
335 public function set_column($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) {
336 if (is_array($format)) {
337 $format = new MoodleODSFormat($format);
341 } else if ($level > 7) {
344 for($i=$firstcol; $i<=$lastcol; $i++
) {
345 if (!isset($this->columns
[$i])) {
346 $this->columns
[$i] = new stdClass();
349 $this->columns
[$i]->width
= $width*6.15; // 6.15 is a magic constant here!
351 $this->columns
[$i]->format
= $format;
352 $this->columns
[$i]->hidden
= $hidden;
353 $this->columns
[$i]->level
= $level;
358 * Set the option to hide gridlines on the printed page.
360 public function hide_gridlines() {
361 // Not implemented - always off.
365 * Set the option to hide gridlines on the worksheet (as seen on the screen).
367 public function hide_screen_gridlines() {
368 $this->showgrid
= false;
372 * Insert a 24bit bitmap image in a worksheet.
374 * @param integer $row The row we are going to insert the bitmap into
375 * @param integer $col The column we are going to insert the bitmap into
376 * @param string $bitmap The bitmap filename
377 * @param integer $x The horizontal position (offset) of the image inside the cell.
378 * @param integer $y The vertical position (offset) of the image inside the cell.
379 * @param integer $scale_x The horizontal scale
380 * @param integer $scale_y The vertical scale
382 public function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1) {
387 * Merges the area given by its arguments.
389 * @param integer $first_row First row of the area to merge
390 * @param integer $first_col First column of the area to merge
391 * @param integer $last_row Last row of the area to merge
392 * @param integer $last_col Last column of the area to merge
394 public function merge_cells($first_row, $first_col, $last_row, $last_col) {
395 if ($first_row > $last_row or $first_col > $last_col) {
399 if (!isset($this->data
[$first_row][$first_col])) {
400 $this->data
[$first_row][$first_col] = new MoodleODSCell();
403 $this->data
[$first_row][$first_col]->merge
= array('rows'=>($last_row-$first_row+
1), 'columns'=>($last_col-$first_col+
1));
409 * ODS cell format abstraction.
412 * @copyright 2006 Petr Skoda {@link http://skodak.org}
413 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
415 class MoodleODSFormat
{
417 public $properties = array();
420 * Constructs one Moodle Format.
422 * @param array $properties
424 public function __construct($properties = array()) {
429 foreach($properties as $property => $value) {
430 if (method_exists($this, "set_$property")) {
431 $aux = 'set_'.$property;
438 * Set the size of the text in the format (in pixels).
439 * By default all texts in generated sheets are 10pt.
441 * @param integer $size Size of the text (in points)
443 public function set_size($size) {
444 $this->properties
['size'] = $size;
448 * Set weight of the format.
450 * @param integer $weight Weight for the text, 0 maps to 400 (normal text),
451 * 1 maps to 700 (bold text). Valid range is: 100-1000.
452 * It's Optional, default is 1 (bold).
454 public function set_bold($weight = 1) {
458 $this->properties
['bold'] = ($weight > 400);
462 * Set underline of the format.
464 * @param integer $underline The value for underline. Possible values are:
465 * 1 => underline, 2 => double underline
467 public function set_underline($underline = 1) {
468 if ($underline == 1) {
469 $this->properties
['underline'] = 1;
470 } else if ($underline == 2) {
471 $this->properties
['underline'] = 2;
473 unset($this->properties
['underline']);
478 * Set italic of the format.
480 public function set_italic() {
481 $this->properties
['italic'] = true;
485 * Set strikeout of the format
487 public function set_strikeout() {
488 $this->properties
['strikeout'] = true;
492 * Set outlining of the format.
494 public function set_outline() {
499 * Set shadow of the format.
501 public function set_shadow() {
506 * Set the script of the text.
508 * @param integer $script The value for script type. Possible values are:
509 * 1 => superscript, 2 => subscript
511 public function set_script($script) {
513 $this->properties
['super_script'] = true;
514 unset($this->properties
['sub_script']);
516 } else if ($script == 2) {
517 $this->properties
['sub_script'] = true;
518 unset($this->properties
['super_script']);
521 unset($this->properties
['sub_script']);
522 unset($this->properties
['super_script']);
527 * Set color of the format.
529 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
531 public function set_color($color) {
532 $this->properties
['color'] = $this->parse_color($color);
538 * @param mixed $color
540 public function set_fg_color($color) {
545 * Set background color of the cell.
547 * @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
549 public function set_bg_color($color) {
550 $this->properties
['bg_color'] = $this->parse_color($color);
554 * Set the cell fill pattern.
556 * @deprecated use set_bg_color() instead.
559 public function set_pattern($pattern=1) {
561 if (!isset($this->properties
['bg_color'])) {
562 $this->properties
['bg_color'] = $this->parse_color('black');
565 unset($this->properties
['bg_color']);
571 * Set text wrap of the format
573 public function set_text_wrap() {
574 $this->properties
['wrap'] = true;
578 * Set the cell alignment of the format.
580 * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
582 public function set_align($location) {
583 if (in_array($location, array('left', 'centre', 'center', 'right', 'fill', 'merge', 'justify', 'equal_space'))) {
584 $this->set_h_align($location);
586 } else if (in_array($location, array('top', 'vcentre', 'vcenter', 'bottom', 'vjustify', 'vequal_space'))) {
587 $this->set_v_align($location);
592 * Set the cell horizontal alignment of the format.
594 * @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
596 public function set_h_align($location) {
599 $this->properties
['align'] = 'start';
603 $this->properties
['align'] = 'center';
606 $this->properties
['align'] = 'end';
612 * Set the cell vertical alignment of the format.
614 * @param string $location alignment for the cell ('top', 'bottom', 'center', 'justify')
616 public function set_v_align($location) {
619 $this->properties
['v_align'] = 'top';
625 $this->properties
['v_align'] = 'middle';
628 $this->properties
['v_align'] = 'bottom';
633 * Set the top border of the format.
635 * @param integer $style style for the cell. 1 => thin, 2 => thick
637 public function set_top($style) {
640 } else if ($style == 2) {
645 $this->properties
['border_top'] = $style;
649 * Set the bottom border of the format.
651 * @param integer $style style for the cell. 1 => thin, 2 => thick
653 public function set_bottom($style) {
656 } else if ($style == 2) {
661 $this->properties
['border_bottom'] = $style;
665 * Set the left border of the format.
667 * @param integer $style style for the cell. 1 => thin, 2 => thick
669 public function set_left($style) {
672 } else if ($style == 2) {
677 $this->properties
['border_left'] = $style;
681 * Set the right border of the format.
683 * @param integer $style style for the cell. 1 => thin, 2 => thick
685 public function set_right($style) {
688 } else if ($style == 2) {
693 $this->properties
['border_right'] = $style;
697 * Set cells borders to the same style
698 * @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
700 public function set_border($style) {
701 $this->set_top($style);
702 $this->set_bottom($style);
703 $this->set_left($style);
704 $this->set_right($style);
708 * Set the numerical format of the format.
709 * It can be date, time, currency, etc...
711 * @param mixed $num_format The numeric format
713 public function set_num_format($num_format) {
718 $numbers[2] = '0.00';
719 $numbers[3] = '#,##0';
720 $numbers[4] = '#,##0.00';
721 $numbers[11] = '0.00E+00';
722 $numbers[12] = '# ?/?';
723 $numbers[13] = '# ??/??';
724 $numbers[14] = 'mm-dd-yy';
725 $numbers[15] = 'd-mmm-yy';
726 $numbers[16] = 'd-mmm';
727 $numbers[17] = 'mmm-yy';
728 $numbers[22] = 'm/d/yy h:mm';
731 if ($num_format !== 0 and in_array($num_format, $numbers)) {
732 $flipped = array_flip($numbers);
733 $this->properties
['num_format'] = $flipped[$num_format];
735 if (!isset($numbers[$num_format])) {
739 $this->properties
['num_format'] = $num_format;
743 * Standardise colour name.
745 * @param mixed $color name of the color (i.e.: 'blue', 'red', etc..), or an integer (range is [8...63]).
746 * @return string the RGB color value
748 protected function parse_color($color) {
749 if (strpos($color, '#') === 0) {
750 // No conversion should be needed.
754 if ($color > 7 and $color < 53) {
773 if (isset($numbers[$color])) {
774 $color = $numbers[$color];
786 'fuchsia' => 'FF00FF',
791 'magenta' => 'FF00FF',
792 'maroon' => '800000',
794 'orange' => 'FFA500',
797 'purple' => '800080',
799 'silver' => 'C0C0C0',
802 'yellow' => 'FFFF00',
805 if (isset($colors[$color])) {
806 return('#'.$colors[$color]);
809 return('#'.$colors['black']);
818 * @copyright 2013 Petr Skoda {@link http://skodak.org}
819 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
821 class MoodleODSWriter
{
822 protected $worksheets;
824 public function __construct(array $worksheets) {
825 $this->worksheets
= $worksheets;
828 public function get_file_content() {
831 require_once($CFG->libdir
.'/filelib.php');
834 $dir = 'ods/'.time().'_'.rand(0, 10000);
835 } while (file_exists($CFG->tempdir
.'/'.$dir));
837 make_temp_directory($dir);
838 make_temp_directory($dir.'/META-INF');
839 $dir = "$CFG->tempdir/$dir";
842 $handle = fopen("$dir/mimetype", 'w');
843 fwrite($handle, $this->get_ods_mimetype());
844 $files[] = "$dir/mimetype";
846 $handle = fopen("$dir/content.xml", 'w');
847 fwrite($handle, $this->get_ods_content($this->worksheets
));
848 $files[] = "$dir/content.xml";
850 $handle = fopen("$dir/meta.xml", 'w');
851 fwrite($handle, $this->get_ods_meta());
852 $files[] = "$dir/meta.xml";
854 $handle = fopen("$dir/styles.xml", 'w');
855 fwrite($handle, $this->get_ods_styles());
856 $files[] = "$dir/styles.xml";
858 $handle = fopen("$dir/settings.xml", 'w');
859 fwrite($handle, $this->get_ods_settings());
860 $files[] = "$dir/settings.xml";
862 $handle = fopen("$dir/META-INF/manifest.xml", 'w');
863 fwrite($handle, $this->get_ods_manifest());
864 $files[] = "$dir/META-INF";
866 $filename = "$dir/result.ods";
867 zip_files($files, $filename);
869 $handle = fopen($filename, 'rb');
870 $contents = fread($handle, filesize($filename));
873 remove_dir($dir); // Cleanup the temp directory.
878 protected function get_ods_content() {
880 // Find out the size of worksheets and used styles.
886 foreach($this->worksheets
as $wsnum=>$ws) {
887 $this->worksheets
[$wsnum]->maxr
= 0;
888 $this->worksheets
[$wsnum]->maxc
= 0;
889 foreach($ws->data
as $rnum=>$row) {
890 if ($rnum > $this->worksheets
[$wsnum]->maxr
) {
891 $this->worksheets
[$wsnum]->maxr
= $rnum;
893 foreach($row as $cnum=>$cell) {
894 if ($cnum > $this->worksheets
[$wsnum]->maxc
) {
895 $this->worksheets
[$wsnum]->maxc
= $cnum;
897 if (!empty($cell->format
)) {
898 if (!array_key_exists($cell->format
->id
, $formats)) {
899 $formats[$cell->format
->id
] = $cell->format
;
905 foreach($ws->rows
as $rnum=>$row) {
906 if (!empty($row->format
)) {
907 if (!array_key_exists($row->format
->id
, $formats)) {
908 $formats[$row->format
->id
] = $row->format
;
911 if ($rnum > $this->worksheets
[$wsnum]->maxr
) {
912 $this->worksheets
[$wsnum]->maxr
= $rnum;
914 // Define all column styles.
915 if (!empty($ws->rows
[$rnum])) {
916 $rowstyles .= '<style:style style:name="ws'.$wsnum.'ro'.$rnum.'" style:family="table-row">';
917 if (isset($row->height
)) {
918 $rowstyles .= '<style:table-row-properties style:row-height="'.$row->height
.'pt"/>';
920 $rowstyles .= '</style:style>';
924 foreach($ws->columns
as $cnum=>$col) {
925 if (!empty($col->format
)) {
926 if (!array_key_exists($col->format
->id
, $formats)) {
927 $formats[$col->format
->id
] = $col->format
;
930 if ($cnum > $this->worksheets
[$wsnum]->maxc
) {
931 $this->worksheets
[$wsnum]->maxc
= $cnum;
933 // Define all column styles.
934 if (!empty($ws->columns
[$cnum])) {
935 $colstyles .= '<style:style style:name="ws'.$wsnum.'co'.$cnum.'" style:family="table-column">';
936 if (isset($col->width
)) {
937 $colstyles .= '<style:table-column-properties style:column-width="'.$col->width
.'pt"/>';
939 $colstyles .= '</style:style>';
944 foreach($formats as $format) {
949 foreach($format->properties
as $pname=>$pvalue) {
952 if (!empty($pvalue)) {
953 $textprop .= ' fo:font-size="'.$pvalue.'pt"';
957 if (!empty($pvalue)) {
958 $textprop .= ' fo:font-weight="bold"';
962 if (!empty($pvalue)) {
963 $textprop .= ' fo:font-style="italic"';
967 if (!empty($pvalue)) {
968 $textprop .= ' style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-width="auto"';
970 $textprop .= ' style:text-underline-type="double"';
975 if (!empty($pvalue)) {
976 $textprop .= ' style:text-line-through-style="solid"';
980 if ($pvalue !== false) {
981 $textprop .= ' fo:color="'.$pvalue.'"';
985 if ($pvalue !== false) {
986 $cellprop .= ' fo:background-color="'.$pvalue.'"';
990 $parprop .= ' fo:text-align="'.$pvalue.'"';
993 $cellprop .= ' style:vertical-align="'.$pvalue.'"';
997 $cellprop .= ' fo:wrap-option="wrap"';
1001 $cellprop .= ' fo:border-top="'.$pvalue.'pt solid #000000"';
1004 $cellprop .= ' fo:border-left="'.$pvalue.'pt solid #000000"';
1006 case 'border_bottom':
1007 $cellprop .= ' fo:border-bottom="'.$pvalue.'pt solid #000000"';
1009 case 'border_right':
1010 $cellprop .= ' fo:border-right="'.$pvalue.'pt solid #000000"';
1013 $dataformat = ' style:data-style-name="NUM'.$pvalue.'"';
1017 if (!empty($textprop)) {
1019 <style:text-properties'.$textprop.'/>';
1022 if (!empty($cellprop)) {
1024 <style:table-cell-properties'.$cellprop.'/>';
1027 if (!empty($parprop)) {
1029 <style:paragraph-properties'.$parprop.'/>';
1033 <style:style style:name="format'.$format->id
.'" style:family="table-cell"'.$dataformat.'>'.$textprop.$cellprop.$parprop.'
1037 // The text styles may be breaking older ODF validators.
1039 <style:style style:name="T1" style:family="text">
1040 <style:text-properties style:text-position="33% 58%"/>
1042 <style:style style:name="T2" style:family="text">
1043 <style:text-properties style:text-position="-33% 58%"/>
1048 '<?xml version="1.0" encoding="UTF-8"?>
1049 <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
1050 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
1051 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
1052 xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
1053 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
1054 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
1055 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/"
1056 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
1057 xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
1058 xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
1059 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
1060 xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
1061 xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
1062 xmlns:math="http://www.w3.org/1998/Math/MathML"
1063 xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
1064 xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
1065 xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer"
1066 xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events"
1067 xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
1068 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1069 xmlns:rpt="http://openoffice.org/2005/report"
1070 xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
1071 xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#"
1072 xmlns:tableooo="http://openoffice.org/2009/table"
1073 xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0"
1074 xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0"
1075 xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0"
1076 xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
1078 <office:font-face-decls>
1079 <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss"
1080 style:font-pitch="variable"/>
1081 <style:font-face style:name="Arial Unicode MS" svg:font-family="'Arial Unicode MS'"
1082 style:font-family-generic="system" style:font-pitch="variable"/>
1083 <style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-family-generic="system"
1084 style:font-pitch="variable"/>
1085 </office:font-face-decls>
1086 <office:automatic-styles>';
1087 $buffer .= $this->get_num_styles();
1089 <style:style style:name="ta1" style:family="table" style:master-page-name="Standard1">
1090 <style:table-properties table:display="true"/>
1093 $buffer .= $formatstyles;
1094 $buffer .= $rowstyles;
1095 $buffer .= $colstyles;
1096 $buffer .= $scriptstyles;
1099 </office:automatic-styles>
1101 <office:spreadsheet>
1104 foreach($this->worksheets
as $wsnum=>$ws) {
1106 // Worksheet header.
1107 $buffer .= '<table:table table:name="' . htmlspecialchars($ws->name
, ENT_QUOTES
, 'utf-8') . '" table:style-name="ta1">'."\n";
1109 // Define column properties.
1111 for($c=0; $c<=$ws->maxc
; $c++
) {
1112 if (array_key_exists($c, $ws->columns
)) {
1113 $column = $ws->columns
[$c];
1114 if ($column->level
> $level) {
1115 while ($column->level
> $level) {
1116 $buffer .= '<table:table-column-group>';
1119 } else if ($column->level
< $level) {
1120 while ($column->level
< $level) {
1121 $buffer .= '</table:table-column-group>';
1126 if (!empty($column->format
)) {
1127 $extra .= ' table:default-cell-style-name="format'.$column->format
->id
.'"';
1129 if ($column->hidden
) {
1130 $extra .= ' table:visibility="collapse"';
1132 $buffer .= '<table:table-column table:style-name="ws'.$wsnum.'co'.$c.'"'.$extra.'/>'."\n";
1134 while ($level > 0) {
1135 $buffer .= '</table:table-column-group>';
1138 $buffer .= '<table:table-column/>'."\n";
1141 while ($level > 0) {
1142 $buffer .= '</table:table-column-group>';
1148 for($r=0; $r<=$ws->maxr
; $r++
) {
1149 if (array_key_exists($r, $ws->rows
)) {
1150 $row = $ws->rows
[$r];
1151 if ($row->level
> $level) {
1152 while ($row->level
> $level) {
1153 $buffer .= '<table:table-row-group>';
1156 } else if ($row->level
< $level) {
1157 while ($row->level
< $level) {
1158 $buffer .= '</table:table-row-group>';
1163 if (!empty($row->format
)) {
1164 $extra .= ' table:default-cell-style-name="format'.$row->format
->id
.'"';
1167 $extra .= ' table:visibility="collapse"';
1169 $buffer .= '<table:table-row table:style-name="ws'.$wsnum.'ro'.$r.'"'.$extra.'>'."\n";
1171 while ($level > 0) {
1172 $buffer .= '</table:table-row-group>';
1175 $buffer .= '<table:table-row>'."\n";
1177 for($c=0; $c<=$ws->maxc
; $c++
) {
1178 if (isset($ws->data
[$r][$c])) {
1179 $cell = $ws->data
[$r][$c];
1181 if (!empty($cell->format
)) {
1182 $extra .= ' table:style-name="format'.$cell->format
->id
.'"';
1184 if (!empty($cell->merge
)) {
1185 $extra .= ' table:number-columns-spanned="'.$cell->merge
['columns'].'" table:number-rows-spanned="'.$cell->merge
['rows'].'"';
1187 $pretext = '<text:p>';
1188 $posttext = '</text:p>';
1189 if (!empty($cell->format
->properties
['sub_script'])) {
1190 $pretext = $pretext.'<text:span text:style-name="T2">';
1191 $posttext = '</text:span>'.$posttext;
1192 } else if (!empty($cell->format
->properties
['super_script'])) {
1193 $pretext = $pretext.'<text:span text:style-name="T1">';
1194 $posttext = '</text:span>'.$posttext;
1197 if (isset($cell->formula
)) {
1198 $buffer .= '<table:table-cell table:formula="of:'.$cell->formula
.'"'.$extra.'></table:table-cell>'."\n";
1199 } else if ($cell->type
== 'date') {
1200 $buffer .= '<table:table-cell office:value-type="date" office:date-value="' . strftime('%Y-%m-%dT%H:%M:%S', $cell->value
) . '"'.$extra.'>'
1201 . $pretext . strftime('%Y-%m-%dT%H:%M:%S', $cell->value
) . $posttext
1202 . '</table:table-cell>'."\n";
1203 } else if ($cell->type
== 'float') {
1204 $buffer .= '<table:table-cell office:value-type="float" office:value="' . htmlspecialchars($cell->value
, ENT_QUOTES
, 'utf-8') . '"'.$extra.'>'
1205 . $pretext . htmlspecialchars($cell->value
, ENT_QUOTES
, 'utf-8') . $posttext
1206 . '</table:table-cell>'."\n";
1207 } else if ($cell->type
== 'string') {
1208 $buffer .= '<table:table-cell office:value-type="string"'.$extra.'>'
1209 . $pretext . htmlspecialchars($cell->value
, ENT_QUOTES
, 'utf-8') . $posttext
1210 . '</table:table-cell>'."\n";
1212 $buffer .= '<table:table-cell office:value-type="string"'.$extra.'>'
1213 . $pretext . '!!Error - unknown type!!' . $posttext
1214 . '</table:table-cell>'."\n";
1217 $buffer .= '<table:table-cell/>'."\n";
1220 $buffer .= '</table:table-row>'."\n";
1222 while ($level > 0) {
1223 $buffer .= '</table:table-row-group>';
1226 $buffer .= '</table:table>'."\n";
1232 </office:spreadsheet>
1234 </office:document-content>';
1239 public function get_ods_mimetype() {
1240 return 'application/vnd.oasis.opendocument.spreadsheet';
1243 protected function get_ods_settings() {
1245 '<?xml version="1.0" encoding="UTF-8"?>
1246 <office:document-settings xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
1247 xmlns:xlink="http://www.w3.org/1999/xlink"
1248 xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0"
1249 xmlns:ooo="http://openoffice.org/2004/office" office:version="1.2">
1251 <config:config-item-set config:name="ooo:view-settings">
1252 <config:config-item config:name="VisibleAreaTop" config:type="int">0</config:config-item>
1253 <config:config-item config:name="VisibleAreaLeft" config:type="int">0</config:config-item>
1254 <config:config-item-map-indexed config:name="Views">
1255 <config:config-item-map-entry>
1256 <config:config-item config:name="ViewId" config:type="string">view1</config:config-item>
1257 <config:config-item-map-named config:name="Tables">
1259 foreach ($this->worksheets
as $ws) {
1260 $buffer .= ' <config:config-item-map-entry config:name="'.htmlspecialchars($ws->name
, ENT_QUOTES
, 'utf-8').'">'."\n";
1261 $buffer .= ' <config:config-item config:name="ShowGrid" config:type="boolean">'.($ws->showgrid ?
'true' : 'false').'</config:config-item>'."\n";
1262 $buffer .= ' </config:config-item-map-entry>."\n"';
1265 ' </config:config-item-map-named>
1266 </config:config-item-map-entry>
1267 </config:config-item-map-indexed>
1268 </config:config-item-set>
1269 <config:config-item-set config:name="ooo:configuration-settings">
1270 <config:config-item config:name="ShowGrid" config:type="boolean">true</config:config-item>
1271 </config:config-item-set>
1273 </office:document-settings>';
1277 protected function get_ods_meta() {
1281 '<?xml version="1.0" encoding="UTF-8"?>
1282 <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
1283 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/"
1284 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
1285 xmlns:ooo="http://openoffice.org/2004/office" xmlns:grddl="http://www.w3.org/2003/g/data-view#"
1286 office:version="1.2">
1288 <meta:generator>Moodle '.$CFG->release
.'</meta:generator>
1289 <meta:initial-creator>'.fullname($USER, true).'</meta:initial-creator>
1290 <meta:creation-date>'.strftime('%Y-%m-%dT%H:%M:%S').'</meta:creation-date>
1291 <meta:document-statistic meta:table-count="1" meta:cell-count="0" meta:object-count="0"/>
1293 </office:document-meta>';
1296 protected function get_ods_styles() {
1298 '<?xml version="1.0" encoding="UTF-8"?>
1299 <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
1300 xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
1301 xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
1302 xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
1303 xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
1304 xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
1305 xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/"
1306 xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
1307 xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
1308 xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
1309 xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
1310 xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
1311 xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
1312 xmlns:math="http://www.w3.org/1998/Math/MathML"
1313 xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
1314 xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
1315 xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer"
1316 xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events"
1317 xmlns:rpt="http://openoffice.org/2005/report"
1318 xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2"
1319 xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#"
1320 xmlns:tableooo="http://openoffice.org/2009/table"
1321 xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0"
1322 xmlns:css3t="http://www.w3.org/TR/css3-text/" office:version="1.2">
1323 <office:font-face-decls>
1324 <style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss"
1325 style:font-pitch="variable"/>
1326 <style:font-face style:name="Arial Unicode MS" svg:font-family="'Arial Unicode MS'"
1327 style:font-family-generic="system" style:font-pitch="variable"/>
1328 <style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-family-generic="system"
1329 style:font-pitch="variable"/>
1330 </office:font-face-decls>
1332 <style:default-style style:family="table-cell">
1333 <style:paragraph-properties style:tab-stop-distance="1.25cm"/>
1334 </style:default-style>
1335 <number:number-style style:name="N0">
1336 <number:number number:min-integer-digits="1"/>
1337 </number:number-style>
1338 <style:style style:name="Default" style:family="table-cell">
1339 <style:text-properties style:font-name-asian="Arial Unicode MS" style:font-name-complex="Arial Unicode MS"/>
1341 <style:style style:name="Result" style:family="table-cell" style:parent-style-name="Default">
1342 <style:text-properties fo:font-style="italic" style:text-underline-style="solid"
1343 style:text-underline-width="auto" style:text-underline-color="font-color"
1344 fo:font-weight="bold"/>
1346 <style:style style:name="Result2" style:family="table-cell" style:parent-style-name="Result"
1347 style:data-style-name="N104"/>
1348 <style:style style:name="Heading" style:family="table-cell" style:parent-style-name="Default">
1349 <style:table-cell-properties style:text-align-source="fix" style:repeat-content="false"/>
1350 <style:paragraph-properties fo:text-align="center"/>
1351 <style:text-properties fo:font-size="16pt" fo:font-style="italic" fo:font-weight="bold"/>
1353 <style:style style:name="Heading1" style:family="table-cell" style:parent-style-name="Heading">
1354 <style:table-cell-properties style:rotation-angle="90"/>
1357 <office:automatic-styles>
1358 <style:page-layout style:name="Mpm1">
1359 <style:page-layout-properties style:writing-mode="lr-tb"/>
1360 <style:header-style>
1361 <style:header-footer-properties fo:min-height="0.75cm" fo:margin-left="0cm" fo:margin-right="0cm"
1362 fo:margin-bottom="0.25cm"/>
1363 </style:header-style>
1364 <style:footer-style>
1365 <style:header-footer-properties fo:min-height="0.75cm" fo:margin-left="0cm" fo:margin-right="0cm"
1366 fo:margin-top="0.25cm"/>
1367 </style:footer-style>
1368 </style:page-layout>
1369 <style:page-layout style:name="Mpm2">
1370 <style:page-layout-properties style:writing-mode="lr-tb"/>
1371 <style:header-style>
1372 <style:header-footer-properties fo:min-height="0.75cm" fo:margin-left="0cm" fo:margin-right="0cm"
1373 fo:margin-bottom="0.25cm" fo:border="2.49pt solid #000000"
1374 fo:padding="0.018cm" fo:background-color="#c0c0c0">
1375 <style:background-image/>
1376 </style:header-footer-properties>
1377 </style:header-style>
1378 <style:footer-style>
1379 <style:header-footer-properties fo:min-height="0.75cm" fo:margin-left="0cm" fo:margin-right="0cm"
1380 fo:margin-top="0.25cm" fo:border="2.49pt solid #000000"
1381 fo:padding="0.018cm" fo:background-color="#c0c0c0">
1382 <style:background-image/>
1383 </style:header-footer-properties>
1384 </style:footer-style>
1385 </style:page-layout>
1386 </office:automatic-styles>
1387 <office:master-styles>
1388 <style:master-page style:name="Default" style:page-layout-name="Mpm1">
1391 <text:sheet-name>???</text:sheet-name>
1394 <style:header-left style:display="false"/>
1397 <text:page-number>1</text:page-number>
1400 <style:footer-left style:display="false"/>
1401 </style:master-page>
1402 <style:master-page style:name="Report" style:page-layout-name="Mpm2">
1406 <text:sheet-name>???</text:sheet-name>
1407 (<text:title>???</text:title>)
1409 </style:region-left>
1410 <style:region-right>
1411 <text:p><text:date style:data-style-name="N2" text:date-value="2013-01-05">00.00.0000</text:date>,
1412 <text:time>00:00:00</text:time>
1414 </style:region-right>
1416 <style:header-left style:display="false"/>
1419 <text:page-number>1</text:page-number>
1421 <text:page-count>99</text:page-count>
1424 <style:footer-left style:display="false"/>
1425 </style:master-page>
1426 </office:master-styles>
1427 </office:document-styles>';
1430 protected function get_ods_manifest() {
1432 '<?xml version="1.0" encoding="UTF-8"?>
1433 <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
1434 <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
1435 <manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
1436 <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
1437 <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
1438 <manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/>
1439 </manifest:manifest>';
1442 protected function get_num_styles() {
1444 <number:number-style style:name="NUM1">
1445 <number:number number:decimal-places="0" number:min-integer-digits="1"/>
1446 </number:number-style>
1447 <number:number-style style:name="NUM2">
1448 <number:number number:decimal-places="2" number:min-integer-digits="1"/>
1449 </number:number-style>
1450 <number:number-style style:name="NUM3">
1451 <number:number number:decimal-places="0" number:min-integer-digits="1" number:grouping="true"/>
1452 </number:number-style>
1453 <number:number-style style:name="NUM4">
1454 <number:number number:decimal-places="2" number:min-integer-digits="1" number:grouping="true"/>
1455 </number:number-style>
1456 <number:number-style style:name="NUM11">
1457 <number:scientific-number number:decimal-places="2" number:min-integer-digits="1"
1458 number:min-exponent-digits="2"/>
1459 </number:number-style>
1460 <number:number-style style:name="NUM12">
1461 <number:fraction number:min-integer-digits="0" number:min-numerator-digits="1"
1462 number:min-denominator-digits="1"/>
1463 </number:number-style>
1464 <number:number-style style:name="NUM13">
1465 <number:fraction number:min-integer-digits="0" number:min-numerator-digits="2"
1466 number:min-denominator-digits="2"/>
1467 </number:number-style>
1468 <number:date-style style:name="NUM14" number:automatic-order="true">
1469 <number:month number:style="long"/>
1470 <number:text>/</number:text>
1471 <number:day number:style="long"/>
1472 <number:text>/</number:text>
1474 </number:date-style>
1475 <number:date-style style:name="NUM15">
1477 <number:text>.</number:text>
1478 <number:month number:textual="true"/>
1479 <number:text>.</number:text>
1480 <number:year number:style="long"/>
1481 </number:date-style>
1482 <number:date-style style:name="NUM16" number:automatic-order="true">
1483 <number:month number:textual="true"/>
1484 <number:text></number:text>
1485 <number:day number:style="long"/>
1486 </number:date-style>
1487 <number:date-style style:name="NUM17">
1488 <number:month number:style="long"/>
1489 <number:text>-</number:text>
1490 <number:day number:style="long"/>
1491 </number:date-style>
1492 <number:date-style style:name="NUM22" number:automatic-order="true"
1493 number:format-source="language">
1495 <number:text>/</number:text>
1497 <number:text>/</number:text>
1499 <number:text></number:text>
1500 <number:hours number:style="long"/>
1501 <number:text>:</number:text>
1502 <number:minutes number:style="long"/>
1503 <number:text></number:text>
1505 </number:date-style>
1506 <number:text-style style:name="NUM49">
1507 <number:text-content/>
1508 </number:text-style>