Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/excellib.class.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00030 require_once 'Spreadsheet/Excel/Writer.php';
00031 
00043 class MoodleExcelWorkbook {
00045     var $pear_excel_workbook;
00047     var $latin_output;
00048 
00055     function MoodleExcelWorkbook($filename) {
00056         global $CFG;
00058         $this->pear_excel_workbook = new Spreadsheet_Excel_Writer($filename);
00060         if (empty($CFG->latinexcelexport)) { 
00061             $this->pear_excel_workbook->setVersion(8);
00062             $this->latin_output = false;
00063         } else { 
00064             $this->latin_output = true;
00065         }
00067         make_temp_directory('excel');
00068         $this->pear_excel_workbook->setTempDir($CFG->tempdir.'/excel');
00069     }
00070 
00077     function &add_worksheet($name = '') {
00079         $ws = new MoodleExcelWorksheet ($name, $this->pear_excel_workbook, $this->latin_output);
00080         return $ws;
00081     }
00082 
00092     function &add_format($properties = array()) {
00094         $ft = new MoodleExcelFormat ($this->pear_excel_workbook, $properties);
00095         return $ft;
00096     }
00097 
00101     function close() {
00102         $this->pear_excel_workbook->close();
00103     }
00104 
00110     function send($filename) {
00111         $this->pear_excel_workbook->send($filename);
00112     }
00113 }
00114 
00126 class MoodleExcelWorksheet {
00128     var $pear_excel_worksheet;
00130     var $latin_output;
00131 
00139     function MoodleExcelWorksheet($name, &$workbook, $latin_output=false) {
00140 
00141         if (strlen($name) > 31) {
00142             // Excel does not seem able to cope with sheet names > 31 chars.
00143             // With $latin_output = false, it does not cope at all.
00144             // With $latin_output = true it is supposed to work, but in our experience,
00145             // it doesn't. Therefore, truncate in all circumstances.
00146             $textlib = textlib_get_instance();
00147             $name = $textlib->substr($name, 0, 31);
00148         }
00149 
00151         $this->pear_excel_worksheet =& $workbook->addWorksheet($name);
00152         $this->latin_output = $latin_output;
00154         if (!$this->latin_output) { 
00155             $this->pear_excel_worksheet->setInputEncoding('UTF-16LE');
00156         }
00157     }
00158 
00167     function write_string($row, $col, $str, $format=null) {
00169         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00171         $textlib = textlib_get_instance();
00173         if (!$this->latin_output) { 
00174             $str = $textlib->convert($str, 'utf-8', 'utf-16le');
00175         } else { 
00176             $str = $textlib->convert($str, 'utf-8', 'windows-1252');
00177         }
00179         $this->pear_excel_worksheet->writeString($row, $col, $str, $format);
00180     }
00181 
00190     function write_number($row, $col, $num, $format=null) {
00192         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00194         $this->pear_excel_worksheet->writeNumber($row, $col, $num, $format);
00195     }
00196 
00205     function write_url($row, $col, $url, $format=null) {
00207         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00209         $this->pear_excel_worksheet->writeUrl($row, $col, $url, $format);
00210     }
00211 
00219     function write_date($row, $col, $date, $format=null) {
00221         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00223         $timezone = get_user_timezone_offset();
00224         if ($timezone == 99) {
00225             // system timezone offset in seconds
00226             $offset = (int)date('Z');
00227         } else {
00228             $offset = (int)($timezone * HOURSECS * 2);
00229         }
00230         $value = ((usertime($date) + $offset) / 86400) + 25569;
00232         $this->pear_excel_worksheet->writeNumber($row, $col, $value, $format);
00233     }
00234 
00243     function write_formula($row, $col, $formula, $format=null) {
00245         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00247         $this->pear_excel_worksheet->writeFormula($row, $col, $formula, $format);
00248     }
00249 
00257     function write_blank($row, $col, $format=null) {
00259         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00261         $this->pear_excel_worksheet->writeBlank($row, $col, $format);
00262     }
00263 
00274     function write($row, $col, $token, $format=null) {
00275 
00277         if (preg_match("/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/", $token)) {
00279             return $this->write_number($row, $col, $token, $format);
00280         } elseif (preg_match("/^[fh]tt?p:\/\//", $token)) {
00282             return $this->write_url($row, $col, $token, '', $format);
00283         } elseif (preg_match("/^mailto:/", $token)) {
00285             return $this->write_url($row, $col, $token, '', $format);
00286         } elseif (preg_match("/^(?:in|ex)ternal:/", $token)) {
00288             return $this->write_url($row, $col, $token, '', $format);
00289         } elseif (preg_match("/^=/", $token)) {
00291             return $this->write_formula($row, $col, $token, $format);
00292         } elseif (preg_match("/^@/", $token)) {
00294             return $this->write_formula($row, $col, $token, $format);
00295         } elseif ($token == '') {
00297             return $this->write_blank($row, $col, $format);
00298         } else {
00300             return $this->write_string($row, $col, $token, $format);
00301         }
00302     }
00303 
00313     function set_row ($row, $height, $format = null, $hidden = false, $level = 0) {
00315         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00317         $this->pear_excel_worksheet->setRow($row, $height, $format, $hidden, $level);
00318     }
00319 
00330     function set_column ($firstcol, $lastcol, $width, $format = null, $hidden = false, $level = 0) {
00332         $format = $this->MoodleExcelFormat2PearExcelFormat($format);
00334         $this->pear_excel_worksheet->setColumn($firstcol, $lastcol, $width, $format, $hidden, $level);
00335     }
00336 
00342     function hide_gridlines() {
00343         $this->pear_excel_worksheet->hideGridLines();
00344     }
00345 
00351     function hide_screen_gridlines() {
00352         $this->pear_excel_worksheet->hideScreenGridlines();
00353     }
00354 
00367     function insert_bitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1) {
00369         $this->pear_excel_worksheet->insertBitmap($row, $col, $bitmap, $x, $y, $scale_x, $scale_y);
00370     }
00371 
00383     function merge_cells($first_row, $first_col, $last_row, $last_col) {
00385         $this->pear_excel_worksheet->mergeCells($first_row, $first_col, $last_row, $last_col);
00386     }
00387 
00394     function MoodleExcelFormat2PearExcelFormat($format) {
00395         if ($format) {
00396             return $format->pear_excel_format;
00397         } else {
00398             return null;
00399         }
00400     }
00401 }
00402 
00403 
00415 class MoodleExcelFormat {
00417     var $pear_excel_format;
00418 
00425     function MoodleExcelFormat(&$workbook, $properties = array()) {
00427         $this->pear_excel_format =& $workbook->addFormat();
00429         foreach($properties as $property => $value) {
00430             if(method_exists($this,"set_$property")) {
00431                 $aux = 'set_'.$property;
00432                 $this->$aux($value);
00433             }
00434         }
00435     }
00436 
00443     function set_size($size) {
00445         $this->pear_excel_format->setSize($size);
00446     }
00447 
00455     function set_bold($weight = 1) {
00457         $this->pear_excel_format->setBold($weight);
00458     }
00459 
00466     function set_underline($underline) {
00468         $this->pear_excel_format->setUnderline($underline);
00469     }
00470 
00474     function set_italic() {
00476         $this->pear_excel_format->setItalic();
00477     }
00478 
00482     function set_strikeout() {
00484         $this->pear_excel_format->setStrikeOut();
00485     }
00486 
00490     function set_outline() {
00492         $this->pear_excel_format->setOutLine();
00493     }
00494 
00498     function set_shadow() {
00500         $this->pear_excel_format->setShadow();
00501     }
00502 
00509     function set_script($script) {
00511         $this->pear_excel_format->setScript($script);
00512     }
00513 
00519     function set_color($color) {
00521         $this->pear_excel_format->setColor($color);
00522     }
00523 
00532     function set_fg_color($color) {
00534         $this->pear_excel_format->setFgColor($color);
00535     }
00536 
00546     function set_bg_color($color) {
00548         $this->pear_excel_format->setBgColor($color);
00549     }
00550 
00556     function set_pattern($pattern=1) {
00558         $this->pear_excel_format->setPattern($pattern);
00559     }
00560 
00564     function set_text_wrap() {
00566         $this->pear_excel_format->setTextWrap();
00567     }
00568 
00574     function set_align($location) {
00576         $this->pear_excel_format->setAlign($location);
00577     }
00578 
00584     function set_h_align($location) {
00586         $this->pear_excel_format->setHAlign($location);
00587     }
00588 
00594     function set_v_align($location) {
00596         $this->pear_excel_format->setVAlign($location);
00597     }
00598 
00604     function set_top($style) {
00606         $this->pear_excel_format->setTop($style);
00607     }
00608 
00614     function set_bottom($style) {
00616         $this->pear_excel_format->setBottom($style);
00617     }
00618 
00624     function set_left($style) {
00626         $this->pear_excel_format->setLeft($style);
00627     }
00628 
00634     function set_right($style) {
00636         $this->pear_excel_format->setRight($style);
00637     }
00638 
00644     function set_border($style) {
00646         $this->pear_excel_format->setBorder($style);
00647     }
00648 
00655     function set_num_format($num_format) {
00657         $this->pear_excel_format->setNumFormat($num_format);
00658     }
00659 
00660 }
 All Data Structures Namespaces Files Functions Variables Enumerations