.
/**
* ODS file writer.
* The xml used here is derived from output of LibreOffice 3.6.4
*
* The design is based on Excel writer abstraction by Eloy Lafuente and others.
*
* @package core
* @copyright 2006 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* ODS workbook abstraction.
*
* @package core
* @copyright 2006 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleODSWorkbook {
protected $worksheets = array();
protected $filename;
public function __construct($filename) {
$this->filename = $filename;
}
/**
* Create one Moodle Worksheet.
*
* @param string $name Name of the sheet
* @return MoodleODSWorksheet
*/
public function add_worksheet($name = '') {
$ws = new MoodleODSWorksheet($name, $this->worksheets);
$this->worksheets[] = $ws;
return $ws;
}
/**
* Create one Moodle Format.
*
* @param array $properties array of properties [name]=value;
* valid names are set_XXXX existing
* functions without the set_ part
* i.e: [bold]=1 for set_bold(1)...Optional!
* @return MoodleODSFormat
*/
public function add_format($properties = array()) {
return new MoodleODSFormat($properties);
}
/**
* Close the Moodle Workbook.
*/
public function close() {
global $CFG;
require_once($CFG->libdir . '/filelib.php');
$writer = new MoodleODSWriter($this->worksheets);
$contents = $writer->get_file_content();
send_file($contents, $this->filename, 0, 0, true, true, $writer->get_ods_mimetype());
}
/**
* Not required to use.
* @param string $filename Name of the downloaded file
*/
public function send($filename) {
$this->filename = $filename;
}
}
/**
* ODS Cell abstraction.
*
* @package core
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleODSCell {
public $value;
public $type;
public $format;
public $formula;
}
/**
* ODS Worksheet abstraction.
*
* @package core
* @copyright 2006 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleODSWorksheet {
public $data = array();
public $columns = array();
public $rows = array();
public $showgrid = true;
public $name;
/**
* Constructs one Moodle Worksheet.
*
* @param string $name The name of the file
* @param array $worksheets existing worksheets
*/
public function __construct($name, array $worksheets) {
// Replace any characters in the name that Excel cannot cope with.
$name = strtr($name, '[]*/\?:', ' ');
if ($name === '') {
// Name is required!
$name = 'Sheet'.(count($worksheets)+1);
}
$this->name = $name;
}
/**
* Write one string somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param string $str The string to write
* @param mixed $format The XF format for the cell
*/
public function write_string($row, $col, $str, $format = null) {
if (!isset($this->data[$row][$col])) {
$this->data[$row][$col] = new MoodleODSCell();
}
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->data[$row][$col]->value = $str;
$this->data[$row][$col]->type = 'string';
$this->data[$row][$col]->format = $format;
$this->data[$row][$col]->formula = null;
}
/**
* Write one number somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param float $num The number to write
* @param mixed $format The XF format for the cell
*/
public function write_number($row, $col, $num, $format = null) {
if (!isset($this->data[$row][$col])) {
$this->data[$row][$col] = new MoodleODSCell();
}
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->data[$row][$col]->value = $num;
$this->data[$row][$col]->type = 'float';
$this->data[$row][$col]->format = $format;
$this->data[$row][$col]->formula = null;
}
/**
* Write one url somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param string $url The url to write
* @param mixed $format The XF format for the cell
*/
public function write_url($row, $col, $url, $format = null) {
if (!isset($this->data[$row][$col])) {
$this->data[$row][$col] = new MoodleODSCell();
}
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->data[$row][$col]->value = $url;
$this->data[$row][$col]->type = 'string';
$this->data[$row][$col]->format = $format;
$this->data[$row][$col]->formula = null;
}
/**
* Write one date somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param string $date The url to write
* @param mixed $format The XF format for the cell
*/
public function write_date($row, $col, $date, $format = null) {
if (!isset($this->data[$row][$col])) {
$this->data[$row][$col] = new MoodleODSCell();
}
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->data[$row][$col]->value = $date;
$this->data[$row][$col]->type = 'date';
$this->data[$row][$col]->format = $format;
$this->data[$row][$col]->formula = null;
}
/**
* Write one formula somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param string $formula The formula to write
* @param mixed $format The XF format for the cell
*/
public function write_formula($row, $col, $formula, $format = null) {
if (!isset($this->data[$row][$col])) {
$this->data[$row][$col] = new MoodleODSCell();
}
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->data[$row][$col]->formula = $formula;
$this->data[$row][$col]->format = $format;
$this->data[$row][$col]->value = null;
$this->data[$row][$col]->format = null;
}
/**
* Write one blank somewhere in the worksheet.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param mixed $format The XF format for the cell
*/
public function write_blank($row, $col, $format = null) {
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
$this->write_string($row, $col, '', $format);
}
/**
* Write anything somewhere in the worksheet,
* type will be automatically detected.
*
* @param integer $row Zero indexed row
* @param integer $col Zero indexed column
* @param mixed $token What we are writing
* @param mixed $format The XF format for the cell
*/
public function write($row, $col, $token, $format = null) {
// Analyse what are we trying to send.
if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
// Match number
return $this->write_number($row, $col, $token, $format);
} elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
// Match http or ftp URL
return $this->write_url($row, $col, $token, '', $format);
} elseif (preg_match("/^mailto:/", $token)) {
// Match mailto:
return $this->write_url($row, $col, $token, '', $format);
} elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
// Match internal or external sheet link
return $this->write_url($row, $col, $token, '', $format);
} elseif (preg_match("/^=/", $token)) {
// Match formula
return $this->write_formula($row, $col, $token, $format);
} elseif (preg_match("/^@/", $token)) {
// Match formula
return $this->write_formula($row, $col, $token, $format);
} elseif ($token == '') {
// Match blank
return $this->write_blank($row, $col, $format);
} else {
// Default: match string
return $this->write_string($row, $col, $token, $format);
}
}
/**
* Sets the height (and other settings) of one row.
*
* @param integer $row The row to set
* @param integer $height Height we are giving to the row (null to set just format without setting the height)
* @param mixed $format The optional format we are giving to the row
* @param bool $hidden The optional hidden attribute
* @param integer $level The optional outline level (0-7)
*/
public function set_row($row, $height, $format = null, $hidden = false, $level = 0) {
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
if ($level < 0) {
$level = 0;
} else if ($level > 7) {
$level = 7;
}
if (!isset($this->rows[$row])) {
$this->rows[$row] = new stdClass();
}
if (isset($height)) {
$this->rows[$row]->height = $height;
}
$this->rows[$row]->format = $format;
$this->rows[$row]->hidden = $hidden;
$this->rows[$row]->level = $level;
}
/**
* Sets the width (and other settings) of one column.
*
* @param integer $firstcol first column on the range
* @param integer $lastcol last column on the range
* @param integer $width width to set (null to set just format without setting the width)
* @param mixed $format The optional format to apply to the columns
* @param bool $hidden The optional hidden attribute
* @param integer $level The optional outline level (0-7)
*/
public function set_column($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) {
if (is_array($format)) {
$format = new MoodleODSFormat($format);
}
if ($level < 0) {
$level = 0;
} else if ($level > 7) {
$level = 7;
}
for($i=$firstcol; $i<=$lastcol; $i++) {
if (!isset($this->columns[$i])) {
$this->columns[$i] = new stdClass();
}
if (isset($width)) {
$this->columns[$i]->width = $width*6.15; // 6.15 is a magic constant here!
}
$this->columns[$i]->format = $format;
$this->columns[$i]->hidden = $hidden;
$this->columns[$i]->level = $level;
}
}
/**
* Set the option to hide gridlines on the printed page.
*/
public function hide_gridlines() {
// Not implemented - always off.
}
/**
* Set the option to hide gridlines on the worksheet (as seen on the screen).
*/
public function hide_screen_gridlines() {
$this->showgrid = false;
}
/**
* Insert a 24bit bitmap image in a worksheet.
*
* @param integer $row The row we are going to insert the bitmap into
* @param integer $col The column we are going to insert the bitmap into
* @param string $bitmap The bitmap filename
* @param integer $x The horizontal position (offset) of the image inside the cell.
* @param integer $y The vertical position (offset) of the image inside the cell.
* @param integer $scale_x The horizontal scale
* @param integer $scale_y The vertical scale
*/
public function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1) {
// Not implemented.
}
/**
* Merges the area given by its arguments.
*
* @param integer $first_row First row of the area to merge
* @param integer $first_col First column of the area to merge
* @param integer $last_row Last row of the area to merge
* @param integer $last_col Last column of the area to merge
*/
public function merge_cells($first_row, $first_col, $last_row, $last_col) {
if ($first_row > $last_row or $first_col > $last_col) {
return;
}
if (!isset($this->data[$first_row][$first_col])) {
$this->data[$first_row][$first_col] = new MoodleODSCell();
}
$this->data[$first_row][$first_col]->merge = array('rows'=>($last_row-$first_row+1), 'columns'=>($last_col-$first_col+1));
}
}
/**
* ODS cell format abstraction.
*
* @package core
* @copyright 2006 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleODSFormat {
public $id;
public $properties = array();
/**
* Constructs one Moodle Format.
*
* @param array $properties
*/
public function __construct($properties = array()) {
static $fid = 1;
$this->id = $fid++;
foreach($properties as $property => $value) {
if (method_exists($this, "set_$property")) {
$aux = 'set_'.$property;
$this->$aux($value);
}
}
}
/**
* Set the size of the text in the format (in pixels).
* By default all texts in generated sheets are 10pt.
*
* @param integer $size Size of the text (in points)
*/
public function set_size($size) {
$this->properties['size'] = $size;
}
/**
* Set weight of the format.
*
* @param integer $weight Weight for the text, 0 maps to 400 (normal text),
* 1 maps to 700 (bold text). Valid range is: 100-1000.
* It's Optional, default is 1 (bold).
*/
public function set_bold($weight = 1) {
if ($weight == 1) {
$weight = 700;
}
$this->properties['bold'] = ($weight > 400);
}
/**
* Set underline of the format.
*
* @param integer $underline The value for underline. Possible values are:
* 1 => underline, 2 => double underline
*/
public function set_underline($underline = 1) {
if ($underline == 1) {
$this->properties['underline'] = 1;
} else if ($underline == 2) {
$this->properties['underline'] = 2;
} else {
unset($this->properties['underline']);
}
}
/**
* Set italic of the format.
*/
public function set_italic() {
$this->properties['italic'] = true;
}
/**
* Set strikeout of the format
*/
public function set_strikeout() {
$this->properties['strikeout'] = true;
}
/**
* Set outlining of the format.
*/
public function set_outline() {
// Not implemented.
}
/**
* Set shadow of the format.
*/
public function set_shadow() {
// Not implemented.
}
/**
* Set the script of the text.
*
* @param integer $script The value for script type. Possible values are:
* 1 => superscript, 2 => subscript
*/
public function set_script($script) {
if ($script == 1) {
$this->properties['super_script'] = true;
unset($this->properties['sub_script']);
} else if ($script == 2) {
$this->properties['sub_script'] = true;
unset($this->properties['super_script']);
} else {
unset($this->properties['sub_script']);
unset($this->properties['super_script']);
}
}
/**
* Set color of the format.
*
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
*/
public function set_color($color) {
$this->properties['color'] = $this->parse_color($color);
}
/**
* Not used.
*
* @param mixed $color
*/
public function set_fg_color($color) {
// Not implemented.
}
/**
* Set background color of the cell.
*
* @param mixed $color either a string (like 'blue'), or an integer (range is [8...63])
*/
public function set_bg_color($color) {
$this->properties['bg_color'] = $this->parse_color($color);
}
/**
* Set the cell fill pattern.
*
* @deprecated use set_bg_color() instead.
* @param integer
*/
public function set_pattern($pattern=1) {
if ($pattern > 0) {
if (!isset($this->properties['bg_color'])) {
$this->properties['bg_color'] = $this->parse_color('black');
}
} else {
unset($this->properties['bg_color']);
}
}
/**
* Set text wrap of the format
*/
public function set_text_wrap() {
$this->properties['wrap'] = true;
}
/**
* Set the cell alignment of the format.
*
* @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
*/
public function set_align($location) {
if (in_array($location, array('left', 'centre', 'center', 'right', 'fill', 'merge', 'justify', 'equal_space'))) {
$this->set_h_align($location);
} else if (in_array($location, array('top', 'vcentre', 'vcenter', 'bottom', 'vjustify', 'vequal_space'))) {
$this->set_v_align($location);
}
}
/**
* Set the cell horizontal alignment of the format.
*
* @param string $location alignment for the cell ('left', 'right', 'justify', etc...)
*/
public function set_h_align($location) {
switch ($location) {
case 'left':
$this->properties['align'] = 'start';
break;
case 'center':
case 'centre':
$this->properties['align'] = 'center';
break;
case 'right':
$this->properties['align'] = 'end';
break;
}
}
/**
* Set the cell vertical alignment of the format.
*
* @param string $location alignment for the cell ('top', 'bottom', 'center', 'justify')
*/
public function set_v_align($location) {
switch ($location) {
case 'top':
$this->properties['v_align'] = 'top';
break;
case 'vcentre':
case 'vcenter':
case 'centre':
case 'center':
$this->properties['v_align'] = 'middle';
break;
default:
$this->properties['v_align'] = 'bottom';
}
}
/**
* Set the top border of the format.
*
* @param integer $style style for the cell. 1 => thin, 2 => thick
*/
public function set_top($style) {
if ($style == 1) {
$style = 0.2;
} else if ($style == 2) {
$style = 0.5;
} else {
return;
}
$this->properties['border_top'] = $style;
}
/**
* Set the bottom border of the format.
*
* @param integer $style style for the cell. 1 => thin, 2 => thick
*/
public function set_bottom($style) {
if ($style == 1) {
$style = 0.2;
} else if ($style == 2) {
$style = 0.5;
} else {
return;
}
$this->properties['border_bottom'] = $style;
}
/**
* Set the left border of the format.
*
* @param integer $style style for the cell. 1 => thin, 2 => thick
*/
public function set_left($style) {
if ($style == 1) {
$style = 0.2;
} else if ($style == 2) {
$style = 0.5;
} else {
return;
}
$this->properties['border_left'] = $style;
}
/**
* Set the right border of the format.
*
* @param integer $style style for the cell. 1 => thin, 2 => thick
*/
public function set_right($style) {
if ($style == 1) {
$style = 0.2;
} else if ($style == 2) {
$style = 0.5;
} else {
return;
}
$this->properties['border_right'] = $style;
}
/**
* Set cells borders to the same style
* @param integer $style style to apply for all cell borders. 1 => thin, 2 => thick.
*/
public function set_border($style) {
$this->set_top($style);
$this->set_bottom($style);
$this->set_left($style);
$this->set_right($style);
}
/**
* Set the numerical format of the format.
* It can be date, time, currency, etc...
*
* @param mixed $num_format The numeric format
*/
public function set_num_format($num_format) {
$numbers = array();
$numbers[1] = '0';
$numbers[2] = '0.00';
$numbers[3] = '#,##0';
$numbers[4] = '#,##0.00';
$numbers[11] = '0.00E+00';
$numbers[12] = '# ?/?';
$numbers[13] = '# ??/??';
$numbers[14] = 'mm-dd-yy';
$numbers[15] = 'd-mmm-yy';
$numbers[16] = 'd-mmm';
$numbers[17] = 'mmm-yy';
$numbers[22] = 'm/d/yy h:mm';
$numbers[49] = '@';
if ($num_format !== 0 and in_array($num_format, $numbers)) {
$flipped = array_flip($numbers);
$this->properties['num_format'] = $flipped[$num_format];
}
if (!isset($numbers[$num_format])) {
return;
}
$this->properties['num_format'] = $num_format;
}
/**
* Standardise colour name.
*
* @param mixed $color name of the color (i.e.: 'blue', 'red', etc..), or an integer (range is [8...63]).
* @return string the RGB color value
*/
protected function parse_color($color) {
if (strpos($color, '#') === 0) {
// No conversion should be needed.
return $color;
}
if ($color > 7 and $color < 53) {
$numbers = array(
8 => 'black',
12 => 'blue',
16 => 'brown',
15 => 'cyan',
23 => 'gray',
17 => 'green',
11 => 'lime',
14 => 'magenta',
18 => 'navy',
53 => 'orange',
33 => 'pink',
20 => 'purple',
10 => 'red',
22 => 'silver',
9 => 'white',
13 => 'yellow',
);
if (isset($numbers[$color])) {
$color = $numbers[$color];
} else {
$color = 'black';
}
}
$colors = array(
'aqua' => '00FFFF',
'black' => '000000',
'blue' => '0000FF',
'brown' => 'A52A2A',
'cyan' => '00FFFF',
'fuchsia' => 'FF00FF',
'gray' => '808080',
'grey' => '808080',
'green' => '00FF00',
'lime' => '00FF00',
'magenta' => 'FF00FF',
'maroon' => '800000',
'navy' => '000080',
'orange' => 'FFA500',
'olive' => '808000',
'pink' => 'FAAFBE',
'purple' => '800080',
'red' => 'FF0000',
'silver' => 'C0C0C0',
'teal' => '008080',
'white' => 'FFFFFF',
'yellow' => 'FFFF00',
);
if (isset($colors[$color])) {
return('#'.$colors[$color]);
}
return('#'.$colors['black']);
}
}
/**
* ODS file writer.
*
* @package core
* @copyright 2013 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleODSWriter {
protected $worksheets;
public function __construct(array $worksheets) {
$this->worksheets = $worksheets;
}
/**
* Fetch the file ocntnet for the ODS.
*
* @return string
*/
public function get_file_content() {
$dir = make_request_directory();
$filename = $dir . '/result.ods';
$files = [
'mimetype' => [$this->get_ods_mimetype()],
'content.xml' => [$this->get_ods_content($this->worksheets)],
'meta.xml' => [$this->get_ods_meta()],
'styles.xml' => [$this->get_ods_styles()],
'settings.xml' => [$this->get_ods_settings()],
'META-INF/manifest.xml' => [$this->get_ods_manifest()],
];
$packer = get_file_packer('application/zip');
$packer->archive_to_pathname($files, $filename);
$contents = file_get_contents($filename);
remove_dir($dir);
return $contents;
}
protected function get_ods_content() {
// Find out the size of worksheets and used styles.
$formats = array();
$formatstyles = '';
$rowstyles = '';
$colstyles = '';
foreach($this->worksheets as $wsnum=>$ws) {
$this->worksheets[$wsnum]->maxr = 0;
$this->worksheets[$wsnum]->maxc = 0;
foreach($ws->data as $rnum=>$row) {
if ($rnum > $this->worksheets[$wsnum]->maxr) {
$this->worksheets[$wsnum]->maxr = $rnum;
}
foreach($row as $cnum=>$cell) {
if ($cnum > $this->worksheets[$wsnum]->maxc) {
$this->worksheets[$wsnum]->maxc = $cnum;
}
if (!empty($cell->format)) {
if (!array_key_exists($cell->format->id, $formats)) {
$formats[$cell->format->id] = $cell->format;
}
}
}
}
foreach($ws->rows as $rnum=>$row) {
if (!empty($row->format)) {
if (!array_key_exists($row->format->id, $formats)) {
$formats[$row->format->id] = $row->format;
}
}
if ($rnum > $this->worksheets[$wsnum]->maxr) {
$this->worksheets[$wsnum]->maxr = $rnum;
}
// Define all column styles.
if (!empty($ws->rows[$rnum])) {
$rowstyles .= '';
if (isset($row->height)) {
$rowstyles .= '';
}
$rowstyles .= '';
}
}
foreach($ws->columns as $cnum=>$col) {
if (!empty($col->format)) {
if (!array_key_exists($col->format->id, $formats)) {
$formats[$col->format->id] = $col->format;
}
}
if ($cnum > $this->worksheets[$wsnum]->maxc) {
$this->worksheets[$wsnum]->maxc = $cnum;
}
// Define all column styles.
if (!empty($ws->columns[$cnum])) {
$colstyles .= '';
if (isset($col->width)) {
$colstyles .= '';
}
$colstyles .= '';
}
}
}
foreach($formats as $format) {
$textprop = '';
$cellprop = '';
$parprop = '';
$dataformat = '';
foreach($format->properties as $pname=>$pvalue) {
switch ($pname) {
case 'size':
if (!empty($pvalue)) {
$textprop .= ' fo:font-size="'.$pvalue.'pt"';
}
break;
case 'bold':
if (!empty($pvalue)) {
$textprop .= ' fo:font-weight="bold"';
}
break;
case 'italic':
if (!empty($pvalue)) {
$textprop .= ' fo:font-style="italic"';
}
break;
case 'underline':
if (!empty($pvalue)) {
$textprop .= ' style:text-underline-color="font-color" style:text-underline-style="solid" style:text-underline-width="auto"';
if ($pvalue == 2) {
$textprop .= ' style:text-underline-type="double"';
}
}
break;
case 'strikeout':
if (!empty($pvalue)) {
$textprop .= ' style:text-line-through-style="solid"';
}
break;
case 'color':
if ($pvalue !== false) {
$textprop .= ' fo:color="'.$pvalue.'"';
}
break;
case 'bg_color':
if ($pvalue !== false) {
$cellprop .= ' fo:background-color="'.$pvalue.'"';
}
break;
case 'align':
$parprop .= ' fo:text-align="'.$pvalue.'"';
break;
case 'v_align':
$cellprop .= ' style:vertical-align="'.$pvalue.'"';
break;
case 'wrap':
if ($pvalue) {
$cellprop .= ' fo:wrap-option="wrap"';
}
break;
case 'border_top':
$cellprop .= ' fo:border-top="'.$pvalue.'pt solid #000000"';
break;
case 'border_left':
$cellprop .= ' fo:border-left="'.$pvalue.'pt solid #000000"';
break;
case 'border_bottom':
$cellprop .= ' fo:border-bottom="'.$pvalue.'pt solid #000000"';
break;
case 'border_right':
$cellprop .= ' fo:border-right="'.$pvalue.'pt solid #000000"';
break;
case 'num_format':
$dataformat = ' style:data-style-name="NUM'.$pvalue.'"';
break;
}
}
if (!empty($textprop)) {
$textprop = '
';
}
if (!empty($cellprop)) {
$cellprop = '
';
}
if (!empty($parprop)) {
$parprop = '
';
}
$formatstyles .= '
'.$textprop.$cellprop.$parprop.'
';
}
// The text styles may be breaking older ODF validators.
$scriptstyles ='
';
// Header.
$buffer =
'
';
$buffer .= $this->get_num_styles();
$buffer .= '
';
$buffer .= $formatstyles;
$buffer .= $rowstyles;
$buffer .= $colstyles;
$buffer .= $scriptstyles;
$buffer .= '
';
foreach($this->worksheets as $wsnum=>$ws) {
// Worksheet header.
$buffer .= ''."\n";
// Define column properties.
$level = 0;
for($c=0; $c<=$ws->maxc; $c++) {
if (array_key_exists($c, $ws->columns)) {
$column = $ws->columns[$c];
if ($column->level > $level) {
while ($column->level > $level) {
$buffer .= '';
$level++;
}
} else if ($column->level < $level) {
while ($column->level < $level) {
$buffer .= '';
$level--;
}
}
$extra = '';
if (!empty($column->format)) {
$extra .= ' table:default-cell-style-name="format'.$column->format->id.'"';
}
if ($column->hidden) {
$extra .= ' table:visibility="collapse"';
}
$buffer .= ''."\n";
} else {
while ($level > 0) {
$buffer .= '';
$level--;
}
$buffer .= ''."\n";
}
}
while ($level > 0) {
$buffer .= '';
$level--;
}
// Print all rows.
$level = 0;
for($r=0; $r<=$ws->maxr; $r++) {
if (array_key_exists($r, $ws->rows)) {
$row = $ws->rows[$r];
if ($row->level > $level) {
while ($row->level > $level) {
$buffer .= '';
$level++;
}
} else if ($row->level < $level) {
while ($row->level < $level) {
$buffer .= '';
$level--;
}
}
$extra = '';
if (!empty($row->format)) {
$extra .= ' table:default-cell-style-name="format'.$row->format->id.'"';
}
if ($row->hidden) {
$extra .= ' table:visibility="collapse"';
}
$buffer .= ''."\n";
} else {
while ($level > 0) {
$buffer .= '';
$level--;
}
$buffer .= ''."\n";
}
for($c=0; $c<=$ws->maxc; $c++) {
if (isset($ws->data[$r][$c])) {
$cell = $ws->data[$r][$c];
$extra = '';
if (!empty($cell->format)) {
$extra .= ' table:style-name="format'.$cell->format->id.'"';
}
if (!empty($cell->merge)) {
$extra .= ' table:number-columns-spanned="'.$cell->merge['columns'].'" table:number-rows-spanned="'.$cell->merge['rows'].'"';
}
$pretext = '';
$posttext = '';
if (!empty($cell->format->properties['sub_script'])) {
$pretext = $pretext.'';
$posttext = ''.$posttext;
} else if (!empty($cell->format->properties['super_script'])) {
$pretext = $pretext.'';
$posttext = ''.$posttext;
}
if (isset($cell->formula)) {
$buffer .= ''."\n";
} else if ($cell->type == 'date') {
$buffer .= ''
. $pretext . strftime('%Y-%m-%dT%H:%M:%S', $cell->value) . $posttext
. ''."\n";
} else if ($cell->type == 'float') {
$buffer .= ''
. $pretext . htmlspecialchars($cell->value, ENT_QUOTES, 'utf-8') . $posttext
. ''."\n";
} else if ($cell->type == 'string') {
$buffer .= ''
. $pretext . htmlspecialchars($cell->value, ENT_QUOTES, 'utf-8') . $posttext
. ''."\n";
} else {
$buffer .= ''
. $pretext . '!!Error - unknown type!!' . $posttext
. ''."\n";
}
} else {
$buffer .= ''."\n";
}
}
$buffer .= ''."\n";
}
while ($level > 0) {
$buffer .= '';
$level--;
}
$buffer .= ''."\n";
}
// Footer.
$buffer .= '
';
return $buffer;
}
public function get_ods_mimetype() {
return 'application/vnd.oasis.opendocument.spreadsheet';
}
protected function get_ods_settings() {
$buffer =
'
0
0
view1
';
foreach ($this->worksheets as $ws) {
$buffer .= ' '."\n";
$buffer .= ' '.($ws->showgrid ? 'true' : 'false').''."\n";
$buffer .= ' ."\n"';
}
$buffer .=
'
true
';
return $buffer;
}
protected function get_ods_meta() {
global $CFG, $USER;
return
'
Moodle '.$CFG->release.'
' . htmlspecialchars(fullname($USER, true), ENT_QUOTES, 'utf-8') . '
'.strftime('%Y-%m-%dT%H:%M:%S').'
';
}
protected function get_ods_styles() {
return
'
???
Page
1
???
(???)
00.00.0000,
00:00:00
Page
1
/
99
';
}
protected function get_ods_manifest() {
return
'
';
}
protected function get_num_styles() {
return '
/
/
.
.
-
/
/
:
';
}
}