MDL-14493 First checkin of Feedback module into HEAD.
[moodle.git] / mod / feedback / easy_excel.php
1 <?php // $Id$
2 /**
3 * makes it easier to use the spreadsheet-functions from pear
4 *
5 * @version $Id$
6 * @author Andreas Grabs
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
8 * @package feedback
9 */
11 require_once("../../config.php");
12 require_once("lib.php");
14 ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include_path'));
15 require_once 'Spreadsheet/Excel/Writer.php';
17 class EasyWorkbook extends Spreadsheet_Excel_Writer {
18     function &addWorksheet($name = ''){
19         global $CFG;
20         
21         $index      = count($this->_worksheets);
22         $sheetname = $this->_sheetname;
24         if ($name == '') {
25             $name = $sheetname.($index+1);
26         }
28         // Check that sheetname is <= 31 chars (Excel limit before BIFF8).
29         if ($this->_BIFF_version != 0x0600)
30         {
31             if (strlen($name) > 31) {
32                 return $this->raiseError("Sheetname $name must be <= 31 chars");
33             }
34         }
36         // Check that the worksheet name doesn't already exist: a fatal Excel error.
37         $total_worksheets = count($this->_worksheets);
38         for ($i = 0; $i < $total_worksheets; $i++) {
39             if ($this->_worksheets[$i]->getName() == $name) {
40                  return $this->raiseError("Worksheet '$name' already exists");
41             }
42         }
44         $worksheet =& new EasyWorksheet($this->_BIFF_version,
45                                           $name, $index,
46                                           $this->_activesheet, $this->_firstsheet,
47                                           $this->_str_total, $this->_str_unique,
48                                           $this->_str_table, $this->_url_format,
49                                           $this->_parser);
51         $this->_worksheets[$index] = &$worksheet;     // Store ref for iterator
52         $this->_sheetnames[$index] = $name;             // Store EXTERNSHEET names
53         $this->_parser->setExtSheet($name, $index);  // Register worksheet name with parser
55         if(!isset($CFG->latinexcelexport) || !$CFG->latinexcelexport) {
56             $worksheet->setInputEncoding('UTF-16LE');
57             // $worksheet->setInputEncoding('utf-8');
58         }
59         return $worksheet;
60     }
61 }
64 class EasyWorksheet extends Spreadsheet_Excel_Writer_Worksheet {
65     var $m_format;
66     var $m_formatbox = array();
67     var $m_workbook;
69     function set_workbook(&$workbook) {
70         $this->m_workbook =& $workbook;
71     }
72     
73     function write($row, $col, $token)
74     {
75         parent::write($row, $col, $token, $this->m_format);
76     }
78     function write_number($row, $col, $num)
79     {
80         parent::writeNumber($row, $col, $num, $this->m_format);
81     }
83     function write_string($row, $col, $str)
84     {
85         parent::writeString($row, $col, feedback_convert_to_win($str), $this->m_format);
86     }
88     function write_formula($row, $col, $formula)
89     {
90         parent::writeFormula($row, $col, $formula, $this->m_format);
91     }
93     function write_url($row, $col, $url, $string = '')
94     {
95         parent::writeUrl($row, $col, $url, $string, $this->m_format);
96     }
98     /** 
99      *  Setz das aktuelle Format, dass zum Schreiben verwendet wird
100      *  Der Formatstring setzt sich aus den folgenden Buchstaben mit folgender Bedeutung zusammen.
101      *  <f> = Fett
102      *  <k> = kursiv
103      *  <z> = zentriert
104      *  <l> = linksbündig
105      *  <r> = rechtsbündig
106      *  <vo> = vertikal oben
107      *  <vz> = vertikal zentriert
108      *  <vu> = vertikal unten
109      *  <uX> = unterstrichen X=1-einfach, X=2-doppelt
110      *  <w> = währungsformat
111      *  <pr> = prozentformat
112      *  <ruX> = Rahmen unten X=Stärke
113      *  <roX> = rahmen oben X=Stärke
114      *  <rrX> = rahmen rechts X=Stärke
115      *  <rlX> = rahmen links X=Stärke
116      *  <c:XXX> = Schriftfarbe, XXX kann einen der folgenden Farbwerte enthalten:
117      *      aqua,cyan,black,blue,brown,magenta,fuchsia,gray,
118      *      grey,green,lime,navy,orange,purple,red,silver,white,yellow
119      *      Wichtig: alle Werte müssen klein geschrieben werden.
120      *  @param string $formatString
121      *  @param int $size the size of font in pt
122      *  @param boolean $textWrap
123      *  @return void
124      */
125     function setFormat($formatString,$size = 10,$textWrap = true)
126     {
127         //looking for an existing format-signature at the $m_formatbox
128         //this prevents the overflow of formats
129         $signature = $formatString.'_'.$size.'_'.$textWrap;
130         if((count($this->m_formatbox) > 0) AND array_key_exists($signature, $this->m_formatbox)) {
131             $this->m_format = $this->m_formatbox[$signature];
132         }else {
133         
134             $this->m_format = &$this->m_workbook->addFormat();
135             if($textWrap)
136             {
137                 $this->m_format->setTextWrap();
138             }
140             if(preg_match("/<f>/i",$formatString) > 0)
141             {
142                 $this->m_format->setBold();
143             }
145             if(preg_match("/<k>/i",$formatString) > 0)
146             {
147                 $this->m_format->setItalic();
148             }
150             if(preg_match("/<z>/i",$formatString) > 0)
151             {
152                 $this->m_format->setAlign("center");
153             }
155             if(preg_match("/<l>/i",$formatString) > 0)
156             {
157                 $this->m_format->setAlign("left");
158             }
160             if(preg_match("/<r>/i",$formatString) > 0)
161             {
162                 $this->m_format->setAlign("right");
163             }
165             if(preg_match("/<vo>/i",$formatString) > 0)
166             {
167                 $this->m_format->setAlign("top");
168             }
170             if(preg_match("/<vz>/i",$formatString) > 0)
171             {
172                 $this->m_format->setAlign("vcenter");
173             }
175             if(preg_match("/<vu>/i",$formatString) > 0)
176             {
177                 $this->m_format->setAlign("bottom");
178             }
180             if(preg_match("/<u\d>/i",$formatString,$treffer) > 0)
181             {
182                 $this->m_format->setUnderline(substr($treffer[0],2,1));
183             }
185             if(preg_match("/<w>/i",$formatString) > 0)
186             {
187                 $this->m_format->setNumFormat("#,##0.00_)\80;[Red]-#,##0.00_)\80");
188             }
190             if(preg_match("/<pr>/i",$formatString) > 0)
191             {
192                 $this->m_format->setNumFormat("#,##0.00%");
193             }
195             if(preg_match("/<ru\d>/i",$formatString,$treffer) > 0)
196             {
197                 $this->m_format->setBottom(substr($treffer[0],3,1));
198             }
200             if(preg_match("/<ro\d>/i",$formatString,$treffer) > 0)
201             {
202                 $this->m_format->setTop(substr($treffer[0],3,1));
203             }
205             if(preg_match("/<rr\d>/i",$formatString,$treffer) > 0)
206             {
207                 $this->m_format->setRight(substr($treffer[0],3,1));
208             }
210             if(preg_match("/<rl\d>/i",$formatString,$treffer) > 0)
211             {
212                 $this->m_format->setLeft(substr($treffer[0],3,1));
213             }
215             if(preg_match("/<c\:[^>]+>/",$formatString,$treffer) > 0)
216             {
217                 $len = strlen($treffer[0]) - 4; //abzueglich der Zeichen <c:>
218                 $this->m_format->setColor(substr($treffer[0],3,$len));
219             }
221             $this->m_format->setSize($size);
222             
223             //save the format with its signature
224             $this->m_formatbox[$signature] = $this->m_format;
225         }
226     }
229 function feedback_convert_to_win($text) {
230     global $CFG;
231     static $textlib;
232     static $newwincharset;
233     static $oldcharset;
234     
235     if(!isset($textlib)) {
236         $textlib = textlib_get_instance();
237     }
238     
239     if(!isset($newwincharset)) {
240         if(!isset($CFG->latinexcelexport) || !$CFG->latinexcelexport) {
241             $newwincharset = get_string('UTF-16LE');
242         }else {
243             $newwincharset = get_string('localewincharset');
244             if($newwincharset == '') {
245                 $newwincharset = 'windows-1252';
246             }
247         }
248     }
249     
250     if(!isset($oldcharset)) {
251         $oldcharset = get_string('thischarset');
252     }
253     
254     //converting <br /> into newline
255     $newtext = str_ireplace('<br />', "\n", $text);
256     $newtext = str_ireplace('<br>', "\n", $newtext);
257     
258     return $textlib->convert($newtext, $oldcharset, $newwincharset);
261 ?>