[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/mod/hotpot/report/ -> default.php (source)

   1  <?PHP  // $Id: default.php,v 1.13.4.2 2008/03/06 07:34:04 gbateson Exp $
   2  
   3  ////////////////////////////////////////////////////////////////////
   4  /// Default class for report plugins
   5  ///
   6  /// Doesn't do anything on it's own -- it needs to be extended.
   7  /// This class displays quiz reports.  Because it is called from
   8  /// within /mod/quiz/report.php you can assume that the page header
   9  /// and footer are taken care of.
  10  ///
  11  /// This file can refer to itself as report.php to pass variables
  12  /// to itself - all these will also be globally available.  You must
  13  /// pass "id=$cm->id" or q=$quiz->id", and "mode=reportname".
  14  ////////////////////////////////////////////////////////////////////
  15  
  16  // Included by ../report.php
  17  
  18  class hotpot_default_report {
  19  
  20      function display($hotpot, $cm, $course, $users, $attempts, $questions, $options) {
  21          /// This function just displays the report
  22          // it is replaced by the "display" functions in the scripts in the "report" folder
  23          return true;
  24      }
  25  
  26      function add_question_headings(&$questions, &$table, $align='center', $size=50, $wrap=false, $fontsize=0) {
  27          $count = count($questions);
  28          for ($i=0; $i<$count; $i++) {
  29              $table->head[] = get_string('questionshort', 'hotpot', $i+1);
  30              if (isset($table->align)) {
  31                  $table->align[] = $align;
  32              }
  33              if (isset($table->size)) {
  34                  $table->size[] = $size;
  35              }
  36              if (isset($table->wrap)) {
  37                  $table->wrap[] = $wrap;
  38              }
  39              if (isset($table->fontsize)) {
  40                  $table->fontsize[] = $fontsize;
  41              }
  42          }
  43  
  44      }
  45  
  46      function set_legend(&$table, &$q, &$value, &$question) {
  47          // $q is the question number
  48          // $value is the value (=text) of the answer
  49  
  50          // check the legend is required
  51          if (isset($table->legend) && isset($value)) {
  52  
  53              // create question details array, if necessary
  54              if (empty($table->legend[$q])) {
  55                  $table->legend[$q] = array(
  56                      'name' => hotpot_get_question_name($question),
  57                      'answers' => array()
  58                  );
  59              }
  60  
  61              // search for this $value in answers array for this $q(uestion)
  62              $i_max = count($table->legend[$q]['answers']);
  63              for ($i=0; $i<$i_max; $i++) {
  64                  if ($table->legend[$q]['answers'][$i]==$value) {
  65                      break;
  66                  }
  67              }
  68  
  69              // add $value to answers array, if it was not there
  70              if ($i==$i_max) {
  71                  $table->legend[$q]['answers'][$i] = $value;
  72              }
  73  
  74              // convert $value to alphabetic index (A, B ... AA, AB ...)
  75              $value = $this->dec_to_ALPHA($i);
  76          }
  77      }
  78      function create_legend_table(&$tables, &$table) {
  79  
  80          if (isset($table->legend)) {
  81  
  82              $legend->width = '*';
  83              $legend->tablealign = '*';
  84              $legend->border = isset($table->border) ? $table->border : NULL;
  85              $legend->cellpadding = isset($table->cellpadding) ? $table->cellpadding : NULL;
  86              $legend->cellspacing = isset($table->cellspacing) ? $table->cellspacing : NULL;
  87              $legend->tableclass = isset($table->tableclass) ? $table->tableclass : NULL;
  88  
  89              $legend->caption = get_string('reportlegend', 'hotpot');
  90              $legend->align = array('right', 'left');
  91              $legend->statheadercols = array(0);
  92  
  93              $legend->stat = array();
  94  
  95              // put the questions in order
  96              ksort($table->legend);
  97  
  98              foreach($table->legend as $q=>$question) {
  99  
 100                  $legend->stat[] = array(
 101                      get_string('questionshort', 'hotpot', $q+1),
 102                      $question['name']
 103                  );
 104                  foreach($question['answers'] as $a=>$answer) {
 105                      $legend->stat[] = array(
 106                          $this->dec_to_ALPHA($a),
 107                          $answer
 108                      );
 109                  }
 110              }
 111  
 112              unset($table->legend);
 113              $tables[] = $legend;
 114          }
 115      }
 116      function dec_to_ALPHA($dec) {
 117          if ($dec < 26) {
 118              return chr(ord('A') + $dec);
 119          } else {
 120              return $this->dec_to_ALPHA(intval($dec/26)-1).$this->dec_to_ALPHA($dec % 26);
 121          }
 122      }
 123      function remove_column(&$table, $target_col) {
 124  
 125          if (is_array($table)) {
 126              unset($table[$target_col]);
 127              $table = array_values($table);
 128  
 129          } else if (is_object($table)) {
 130              $vars = get_object_vars($table);
 131              foreach ($vars as $name=>$value) {
 132                  switch ($name) {
 133                      case 'data' :
 134                      case 'stat' :
 135                      case 'foot' :
 136                          $skipcol = array();
 137                          $cells = &$table->$name;
 138  
 139                          $row_max = count($cells);
 140                          for ($row=0; $row<$row_max; $row++) {
 141  
 142                              $col = 0;
 143                              $col_max = count($cells[$row]);
 144  
 145                              $current_col = 0;
 146                              while ($current_col<$target_col && $col<$col_max) {
 147  
 148                                  if (empty($skipcol[$current_col])) {
 149  
 150                                      $cell = $cells[$row][$col++];
 151                                      if (is_object($cell)) {
 152                                          if (isset($cell->rowspan) && is_numeric($cell->rowspan) && ($cell->rowspan>0)) {
 153                                              // skip cells below this one
 154                                              $skipcol[$current_col] = $cell->rowspan-1;
 155                                          }
 156                                          if (isset($cell->colspan) && is_numeric($cell->colspan) && ($cell->colspan>0)) {
 157                                              // skip cells to the right of this one
 158                                              for ($c=1; $c<$cell->colspan; $c++) {
 159                                                  if (empty($skipcol[$current_col+$c])) {
 160                                                      $skipcol[$current_col+$c] = 1;
 161                                                  } else {
 162                                                      $skipcol[$current_col+$c] ++;
 163                                                  }
 164                                              }
 165                                          }
 166                                      }
 167                                  } else {
 168                                      $skipcol[$current_col]--;
 169                                  }
 170                                  $current_col++;
 171                              }
 172                              if ($current_col==$target_col && $col<$col_max) {
 173                                  $this->remove_column($cells[$row], $col);
 174                              }
 175                          } // end for $row
 176                          break;
 177                      case 'head' :
 178                      case 'align' :
 179                      case 'class' :
 180                      case 'fontsize' :
 181                      case 'size' :
 182                      case 'wrap' :
 183                          $this->remove_column($table->$name, $target_col);
 184                          break;
 185                      case 'statheadercols' :
 186                          $array = &$table->$name;
 187                          $count = count($array);
 188                          for ($i=0; $i<$count; $i++) {
 189                              if ($array[$i]>=$target_col) {
 190                                  $array[$i] --;
 191                              }
 192                          }
 193                          break;
 194                  } // end switch
 195              } // end foreach
 196          } // end if
 197      } // end function
 198  
 199  
 200      function expand_spans(&$table, $zone) {
 201          // expand multi-column and multi-row cells in a specified $zone of a $table
 202  
 203          // do nothing if this $zone is empty
 204          if (empty($table->$zone)) return;
 205  
 206          // shortcut to rows in this $table $zone
 207          $rows = &$table->{$zone};
 208  
 209          // loop through the rows
 210          foreach ($rows as $row=>$cells) {
 211  
 212              // check this is an array
 213              if (is_array($cells)) {
 214  
 215                  // loop through the cells in this row
 216                  foreach ($cells as $col=>$cell) {
 217  
 218                      if (is_object($cell)) {
 219                          if (isset($cell->rowspan) && is_numeric($cell->rowspan) && ($cell->rowspan>1)) {
 220                              // fill in cells below this one
 221                              $new_cell = array($cell->text);
 222                              for ($r=1; $r<$cell->rowspan; $r++) {
 223                                  array_splice($rows[$row+$r], $col, 0, $new_cell);
 224                              }
 225                          }
 226                          if (isset($cell->colspan) && is_numeric($cell->colspan) && ($cell->colspan>1)) {
 227                              // fill in cells to the right of this one
 228                              $new_cells = array();
 229                              for ($c=1; $c<$cell->colspan; $c++) {
 230                                  $new_cells[] = $cell->text;
 231                              }
 232                              array_splice($rows[$row], $col, 0, $new_cells);
 233                          }
 234                          // replace $cell object with plain text
 235                          $rows[$row][$col] = $cell->text;
 236                      }
 237                  }
 238              }
 239          }
 240      }
 241  
 242  /////////////////////////////////////////////////
 243  /// print a report in html, text or Excel format
 244  /////////////////////////////////////////////////
 245  
 246  // the stuff to print is contained in $table
 247  // which has the following properties:
 248  
 249  //   $table->border     border width for the table
 250  //   $table->cellpadding    padding on each cell
 251  //   $table->cellspacing    spacing between cells
 252  //   $table->tableclass class for table
 253  //   $table->width      table width
 254  
 255  //   $table->align    is an array of column alignments
 256  //   $table->class    is an array of column classes
 257  //   $table->size     is an array of column sizes
 258  //   $table->wrap     is an array of column wrap/nowrap switches
 259  //   $table->fontsize is an array of fontsizes
 260  
 261  //   $table->caption is a caption (=title) for the report
 262  //   $table->head    is an array of headings (all TH cells)
 263  //   $table->data[]  is an array of arrays containing the data (all TD cells)
 264  //          if a row is given as "hr", a "tabledivider" is inserted
 265  //          if a cell is a string, it is assumed to be the cell content
 266  //          a cell can also be an object, thus:
 267  //              $cell->text : the content of the cell
 268  //              $cell->rowspan : the row span of this cell
 269  //              $cell->colspan : the column span of this cell
 270  //          if rowspan or colspan are specified, neighboring cells are shifted accordingly
 271  //   $table->stat[]  is an array of arrays containing the statistics rows (TD and TH cells)
 272  //   $table->foot[]  is an array of arrays containing the footer rows (all TH cells)
 273  
 274  //   $table->statheadercols is an array of column numbers which are headers
 275  
 276  
 277  //////////////////////////////////////////
 278  /// print a report
 279  
 280      function print_report(&$course, &$hotpot, &$tables, &$options) {
 281          switch ($options['reportformat']) {
 282              case 'txt':
 283                  $this->print_text_report($course, $hotpot, $tables, $options);
 284                  break;
 285              case 'xls':
 286                  $this->print_excel_report($course, $hotpot, $tables, $options);
 287                  break;
 288              default: // 'htm' (and anything else)
 289                  $this->print_html_report($tables);
 290                  break;
 291          }
 292      }
 293  
 294      function print_report_start(&$course, &$hotpot, &$options, &$table) {
 295          switch ($options['reportformat']) {
 296              case 'txt':
 297                  $this->print_text_start($course, $hotpot, $options);
 298                  break;
 299              case 'xls':
 300                  $this->print_excel_start($course, $hotpot, $options);
 301                  break;
 302  
 303              case 'htm':
 304                  $this->print_html_start($course, $hotpot, $options);
 305                  break;
 306          }
 307      }
 308  
 309      function print_report_cells(&$table, &$options, $zone) {
 310          switch ($options['reportformat']) {
 311              case 'txt':
 312                  $fmt = 'text';
 313                  break;
 314              case 'xls':
 315                  $fmt = 'excel';
 316                  break;
 317              default: // 'htm' (and anything else)
 318                  $fmt = 'html';
 319                  break;
 320          }
 321          $fn = "print_{$fmt}_{$zone}";
 322          $this->$fn($table, $options);
 323      }
 324  
 325      function print_report_finish(&$course, &$hotpot, &$options) {
 326          switch ($options['reportformat']) {
 327              case 'txt' :
 328                  // do nothing
 329                  break;
 330              case 'xls':
 331                  $this->print_excel_finish($course, $hotpot, $options);
 332                  break;
 333              case 'htm':
 334                  $this->print_html_finish($course, $hotpot, $options);
 335                  break;
 336          }
 337      }
 338  
 339  //////////////////////////////////////////
 340  /// print an html report
 341  
 342      function print_html_report(&$tables) {
 343          $count = count($tables);
 344          foreach($tables as $i=>$table) {
 345  
 346              $this->print_html_start($table);
 347              $this->print_html_head($table);
 348              $this->print_html_data($table);
 349              $this->print_html_stat($table);
 350              $this->print_html_foot($table);
 351              $this->print_html_finish($table);
 352  
 353              if (($i+1)<$count) {
 354                  print_spacer(30, 10, true);
 355              }
 356          }
 357      }
 358      function print_html_start(&$table) {
 359  
 360          // default class for the table
 361          if (empty($table->tableclass)) {
 362              $table->tableclass = 'generaltable';
 363          }
 364  
 365          // default classes for TD and TH
 366          $d = $table->tableclass.'cell';
 367          $h = $table->tableclass.'header';
 368  
 369          $table->th_side = '<th valign="top" align="right" class="'.$h.'" scope="col">';
 370  
 371          $table->td = array();
 372          $table->th_top = array();
 373  
 374          if (empty($table->colspan)) {
 375              if (isset($table->head)) {
 376                  $table->colspan = count($table->head);
 377              } else if (isset($table->data)) {
 378                  $table->colspan = count($table->data[0]);
 379              } else if (isset($table->stat)) {
 380                  $table->colspan = count($table->stat);
 381              } else if (isset($table->foot)) {
 382                  $table->colspan = count($table->foot);
 383              } else {
 384                  $table->colspan = 0;
 385              }
 386          }
 387  
 388          for ($i=0; $i<$table->colspan; $i++) {
 389  
 390              $align = empty($table->align[$i]) ? '' : ' align="'.$table->align[$i].'"';
 391              $class = empty($table->class[$i]) ? $d : ' class="'.$table->class[$i].'"';
 392              $class = ' class="'.(empty($table->class[$i]) ? $d : $table->class[$i]).'"';
 393              $size  = empty($table->size[$i])  ? '' : ' width="'.$table->size[$i].'"';
 394              $wrap  = empty($table->wrap[$i])  ? '' : ' nowrap="nowrap"';
 395  
 396              $table->th_top[$i] = '<th align="center"'.$size.' class="'.$h.'" nowrap="nowrap" scope="col">';
 397  
 398              $table->td[$i] = '<td valign="top"'.$align.$class.$wrap.'>';
 399  
 400              if (!empty($table->fontsize[$i])) {
 401                  $table->td[$i] .= '<font size="'.$table->fontsize[$i].'">';
 402              }
 403          }
 404  
 405          if (empty($table->border)) {
 406              $table->border = 0;
 407          }
 408          if (empty($table->cellpadding)) {
 409              $table->cellpadding = 5;
 410          }
 411          if (empty($table->cellspacing)) {
 412              $table->cellspacing = 1;
 413          }
 414          if (empty($table->width)) {
 415              $table->width = "80%"; // actually the width of the "simple box"
 416          }
 417          if (empty($table->tablealign)) {
 418              $table->tablealign = "center";
 419          }
 420  
 421          if (isset($table->start)) {
 422              print $table->start."\n";
 423          }
 424  
 425          print_simple_box_start("$table->tablealign", "$table->width", "#ffffff", 0);
 426          print '<table width="100%" border="'.$table->border.'" valign="top" align="center"  cellpadding="'.$table->cellpadding.'" cellspacing="'.$table->cellspacing.'" class="'.$table->tableclass.'">'."\n";
 427  
 428          if (isset($table->caption)) {
 429              print '<tr><td colspan="'.$table->colspan.'" class="'.$table->tableclass.'header"><b>'.$table->caption.'</b></td></tr>'."\n";
 430          }
 431  
 432      }
 433      function print_html_head(&$table) {
 434          if (isset($table->head)) {
 435              print "<tr>\n";
 436              foreach ($table->head as $i=>$cell) {
 437                  $th = $table->th_top[$i];
 438                  print $th.$cell."</th>\n";
 439              }
 440              print "</tr>\n";
 441          }
 442      }
 443      function print_html_data(&$table) {
 444          if (isset($table->data)) {
 445              $skipcol = array();
 446              foreach ($table->data as $cells) {
 447                  print "<tr>\n";
 448                  if (is_array($cells)) {
 449                      $i = 0; // index on $cells
 450                      $col = 0; // column index
 451                      while ($col<$table->colspan && isset($cells[$i])) {
 452                          if (empty($skipcol[$col])) {
 453                              $cell = &$cells[$i++];
 454                              $td = $table->td[$col];
 455                              if (is_object($cell)) {
 456                                  $text = $cell->text;
 457                                  if (isset($cell->rowspan) && is_numeric($cell->rowspan) && ($cell->rowspan>0)) {
 458                                      $td = '<td rowspan="'.$cell->rowspan.'"'.substr($td, 3);
 459                                      // skip cells below this one
 460                                      $skipcol[$col] = $cell->rowspan-1;
 461                                  }
 462                                  if (isset($cell->colspan) && is_numeric($cell->colspan) && ($cell->colspan>0)) {
 463                                      $td = '<td colspan="'.$cell->colspan.'"'.substr($td, 3);
 464                                      // skip cells to the right of this one
 465                                      for ($c=1; $c<$cell->colspan; $c++) {
 466                                          if (empty($skipcol[$col+$c])) {
 467                                              $skipcol[$col+$c] = 1;
 468                                          } else {
 469                                              $skipcol[$col+$c] ++;
 470                                          }
 471                                      }
 472                                  }
 473                              } else { // $cell is a string
 474                                  $text = $cell;
 475                              }
 476                              print $td.$text.(empty($table->fontsize[$col]) ? '' : '</font>')."</td>\n";
 477                          } else {
 478                              $skipcol[$col]--;
 479                          }
 480                          $col++;
 481                      } // end while
 482                  } else if ($cells=='hr') {
 483                      print '<td colspan="'.$table->colspan.'"><div class="tabledivider"></div></td>'."\n";
 484                  }
 485                  print "</tr>\n";
 486              }
 487          }
 488      }
 489      function print_html_stat(&$table) {
 490          if (isset($table->stat)) {
 491              if (empty($table->statheadercols)) {
 492                  $table->statheadercols = array();
 493              }
 494              foreach ($table->stat as $cells) {
 495                  print '<tr>';
 496                  foreach ($cells as $i => $cell) {
 497                      if (in_array($i, $table->statheadercols)) {
 498                          $th = $table->th_side;
 499                          print $th.$cell."</th>\n";
 500                      } else {
 501                          $td = $table->td[$i];
 502                          print $td.$cell."</td>\n";
 503                      }
 504                  }
 505                  print "</tr>\n";
 506              }
 507          }
 508      }
 509      function print_html_foot(&$table) {
 510          if (isset($table->foot)) {
 511              foreach ($table->foot as $cells) {
 512                  print "<tr>\n";
 513                  foreach ($cells as $i => $cell) {
 514                      if ($i==0) {
 515                          $th = $table->th_side;
 516                          print $th.$cell."</th>\n";
 517                      } else {
 518                          $th = $table->th_top[$i];
 519                          print $th.$cell."</th>\n";
 520                      }
 521                  }
 522                  print "</tr>\n";
 523              }
 524          }
 525      }
 526      function print_html_finish(&$table) {
 527          print "</table>\n";
 528          print_simple_box_end();
 529  
 530          if (isset($table->finish)) {
 531              print $table->finish."\n";
 532          }
 533      }
 534  
 535  //////////////////////////////////////////
 536  /// print a text report
 537  
 538      function print_text_report(&$course, &$hotpot, &$tables, &$options) {
 539          $this->print_text_start($course, $hotpot, $options);
 540          foreach ($tables as $table) {
 541              $this->print_text_head($table, $options);
 542              $this->print_text_data($table, $options);
 543              $this->print_text_stat($table, $options);
 544              $this->print_text_foot($table, $options);
 545          }
 546      }
 547      function print_text_start(&$course, &$hotpot, &$options) {
 548          $downloadfilename = clean_filename("$course->shortname $hotpot->name.txt");
 549          header("Content-Type: application/download\n");
 550          header("Content-Disposition: attachment; filename=$downloadfilename");
 551          header("Expires: 0");
 552          header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
 553          header("Pragma: public");
 554      }
 555      function print_text_head(&$table, &$options) {
 556          if (isset($table->caption)) {
 557              $i = strlen($table->caption);
 558              $data = array(
 559                  array(str_repeat('=', $i)),
 560                  array($table->caption),
 561                  array(str_repeat('=', $i)),
 562              );
 563              foreach($data as $cells) {
 564                  $this->print_text_cells($cells, $options);
 565              }
 566          }
 567          if (isset($table->head)) {
 568              $this->expand_spans($table, 'head');
 569              $this->print_text_cells($table->head, $options);
 570          }
 571      }
 572      function print_text_data(&$table, &$options) {
 573          if (isset($table->data)) {
 574              $this->expand_spans($table, 'data');
 575              foreach ($table->data as $cells) {
 576                  $this->print_text_cells($cells, $options);
 577              }
 578          }
 579      }
 580      function print_text_stat(&$table, &$options) {
 581          if (isset($table->stat)) {
 582              $this->expand_spans($table, 'stat');
 583              foreach ($table->stat as $cells) {
 584                  $this->print_text_cells($cells, $options);
 585              }
 586          }
 587      }
 588      function print_text_foot(&$table, &$options) {
 589          if (isset($table->foot)) {
 590              $this->expand_spans($table, 'foot');
 591              foreach ($table->foot as $cells) {
 592                  $this->print_text_cells($cells, $options);
 593              }
 594          }
 595      }
 596      function print_text_cells(&$cells, &$options) {
 597  
 598          // do nothing if there are no cells
 599          if (empty($cells) || is_string($cells)) return;
 600  
 601          // convert to tab-delimted string
 602          $str = implode("\t", $cells);
 603  
 604          // replace newlines in string
 605          $str = preg_replace("/\n/", ",", $str);
 606  
 607          // set best newline for this browser (if it hasn't been done already)
 608          if (empty($this->nl)) {
 609              $s = &$_SERVER['HTTP_USER_AGENT'];
 610              $win = is_numeric(strpos($s, 'Win'));
 611              $mac = is_numeric(strpos($s, 'Mac')) && !is_numeric(strpos($s, 'OS X'));
 612              $this->nl = $win ? "\r\n" : ($mac ? "\r" : "\n");
 613          }
 614  
 615          print $str.$this->nl;
 616      }
 617  
 618  //////////////////////////////////////////
 619  /// print an Excel report
 620  
 621      function print_excel_report(&$course, &$hotpot, &$tables, &$options) {
 622          global $CFG;
 623  
 624          // create Excel workbook
 625          if (file_exists("$CFG->libdir/excellib.class.php")) {
 626              // Moodle >= 1.6
 627              require_once("$CFG->libdir/excellib.class.php");
 628              $wb = new MoodleExcelWorkbook("-");
 629              $wsnamelimit = 0; // no limit
 630          } else {
 631              // Moodle <= 1.5
 632              require_once("$CFG->libdir/excel/Worksheet.php");
 633              require_once("$CFG->libdir/excel/Workbook.php");
 634              $wb = new Workbook("-");
 635              $wsnamelimit = 31; // max length in chars
 636          }
 637  
 638          // send HTTP headers
 639          $this->print_excel_headers($wb, $course, $hotpot);
 640  
 641          // create one worksheet for each table
 642          foreach($tables as $table) {
 643              unset($ws);
 644              if (empty($table->caption)) {
 645                  $wsname = '';
 646              } else {
 647                  $wsname = strip_tags($table->caption);
 648                  if ($wsnamelimit && strlen($wsname) > $wsnamelimit) {
 649                      $wsname = substr($wsname, -$wsnamelimit); // end of string
 650                      // $wsname = substr($wsname, 0, $wsnamelimit); // start of string
 651                  }
 652              }
 653              $ws = &$wb->add_worksheet($wsname);
 654  
 655              $row = 0;
 656              $this->print_excel_head($wb, $ws, $table, $row, $options);
 657              $this->print_excel_data($wb, $ws, $table, $row, $options);
 658              $this->print_excel_stat($wb, $ws, $table, $row, $options);
 659              $this->print_excel_foot($wb, $ws, $table, $row, $options);
 660          }
 661  
 662          // close the workbook (and send it to the browser)
 663          $wb->close();
 664      }
 665      function print_excel_headers(&$wb, &$course, &$hotpot) {
 666          $downloadfilename = clean_filename("$course->shortname $hotpot->name.xls");
 667          if (method_exists($wb, 'send')) {
 668              // Moodle >=1.6
 669              $wb->send($downloadfilename);
 670          } else {
 671              // Moodle <=1.5
 672              header("Content-type: application/vnd.ms-excel");
 673              header("Content-Disposition: attachment; filename=$downloadfilename" );
 674              header("Expires: 0");
 675              header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
 676              header("Pragma: public");
 677          }
 678      }
 679      function print_excel_head(&$wb, &$ws, &$table, &$row, &$options) {
 680          // define format properties
 681          $properties = array(
 682              'bold'=>1,
 683              'align'=>'center',
 684              'v_align'=>'bottom',
 685              'text_wrap'=>1
 686          );
 687  
 688          // expand multi-column and multi-row cells
 689          $this->expand_spans($table, 'head');
 690  
 691          // print the headings
 692          $this->print_excel_cells($wb, $ws, $table, $row, $properties, $table->head, $options);
 693      }
 694      function print_excel_data(&$wb, &$ws, &$table, &$row, &$options) {
 695          // do nothing if there are no cells
 696          if (empty($table->data)) return;
 697  
 698          // define format properties
 699          $properties = array('text_wrap' => (empty($options['reportwrapdata']) ? 0 : 1));
 700  
 701          // expand multi-column and multi-row cells
 702          $this->expand_spans($table, 'data');
 703  
 704          // print rows
 705          foreach ($table->data as $cells) {
 706              $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options);
 707          }
 708      }
 709      function print_excel_stat(&$wb, &$ws, &$table, &$row, &$options) {
 710          // do nothing if there are no cells
 711          if (empty($table->stat)) return;
 712  
 713          // define format properties
 714          $properties = array('align'=>'right');
 715  
 716          // expand multi-column and multi-row cells
 717          $this->expand_spans($table, 'stat');
 718  
 719          // print rows
 720          $i_count = count($table->stat);
 721          foreach ($table->stat as $i => $cells) {
 722  
 723              // set border on top and bottom row
 724              $properties['top'] = ($i==0) ? 1 : 0;
 725              $properties['bottom'] = ($i==($i_count-1)) ? 1 : 0;
 726  
 727              // print this row
 728              $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options, $table->statheadercols);
 729          }
 730      }
 731      function print_excel_foot(&$wb, &$ws, &$table, &$row, &$options) {
 732          // do nothing if there are no cells
 733          if (empty($table->foot)) return;
 734  
 735          // define format properties
 736          $properties = array('bold'=>1, 'align'=>'center');
 737  
 738          // expand multi-column and multi-row cells
 739          $this->expand_spans($table, 'foot');
 740  
 741          // print rows
 742          $i_count = count($table->foot);
 743          foreach ($table->foot as $i => $cells) {
 744  
 745              // set border on top and bottom row
 746              $properties['top'] = ($i==0) ? 1 : 0;
 747              $properties['bottom'] = ($i==($i_count-1)) ? 1 : 0;
 748  
 749              // print this footer row
 750              $this->print_excel_cells($wb, $ws, $table, $row, $properties, $cells, $options);
 751          }
 752      }
 753  
 754      function print_excel_cells(&$wb, &$ws, &$table, &$row, &$properties, &$cells, &$options, $statheadercols=NULL) {
 755          // do nothing if there are no cells
 756          if (empty($cells) || is_string($cells)) return;
 757  
 758          // print cells
 759          foreach($cells as $col => $cell) {
 760  
 761              unset($fmt_properties);
 762              $fmt_properties = $properties;
 763  
 764              if (empty($fmt_properties['text_wrap'])) {
 765                  if (strlen("$cell")>=9) {
 766                      // long cell value
 767                      $fmt_properties['align'] = 'left';
 768                  }
 769              } else {
 770                  if (strlen("$cell")<9 && strpos("$cell", "\n")===false) {
 771                      // short cell value (wrapping not required)
 772                      $fmt_properties['text_wrap'] = 0;
 773                  }
 774              }
 775  
 776              // set bold, if required (for stat)
 777              if (isset($statheadercols)) {
 778                  $fmt_properties['bold'] = in_array($col, $statheadercols) ? 1 : 0;
 779                  $fmt_properties['align'] = in_array($col, $statheadercols) ? 'right' : $table->align[$col];
 780              }
 781  
 782              // set align, if required
 783              if (isset($table->align[$col]) && empty($fmt_properties['align'])) {
 784                  $fmt_properties['align'] =  $table->align[$col];
 785              }
 786  
 787              // check to see that an identical format object has not already been created
 788              unset($fmt);
 789  
 790              if (isset($wb->pear_excel_workbook)) {
 791                  // Moodle >=1.6
 792                  $fmt_properties_obj = (object)$fmt_properties;
 793                  foreach ($wb->pear_excel_workbook->_formats as $id=>$format) {
 794                      if ($format==$fmt_properties_obj) {
 795                          $fmt = &$wb->pear_excel_workbook->_formats[$id];
 796                          break;
 797                      }
 798                  }
 799              } else {
 800                  // Moodle <=1.5
 801                  foreach ($wb->formats as $id=>$format) {
 802                      if (isset($format->properties) && $format->properties==$fmt_properties) {
 803                          $fmt = &$wb->formats[$id];
 804                          break;
 805                      }
 806                  }
 807                  if (is_numeric($cell) || empty($options['reportencoding'])) {
 808                      // do nothing
 809                  } else {
 810                      $in_charset = '';
 811                      if (function_exists('mb_convert_encoding')) {
 812                          $in_charset = mb_detect_encoding($cell, 'auto');
 813                      }
 814                      if (empty($in_charset)) {
 815                          $in_charset = get_string('thischarset');
 816                      }
 817                      if ($in_charset != 'ASCII' && function_exists('mb_convert_encoding')) {
 818                          $cell = mb_convert_encoding($cell, $options['reportencoding'], $in_charset);
 819                      }
 820                  }
 821              }
 822  
 823              // create new format object, if necessary (to avoid "too many cell formats" error)
 824              if (!isset($fmt)) {
 825                  $fmt = &$wb->add_format($fmt_properties);
 826                  $fmt->properties = &$fmt_properties;
 827  
 828                  // set vertical alignment
 829                  if (isset($fmt->properties['v_align'])) {
 830                      $fmt->set_align($fmt->properties['v_align']);
 831                  } else {
 832                      $fmt->set_align('top'); // default
 833                  }
 834              }
 835  
 836              // write cell
 837              if (is_numeric($cell) && !preg_match("/^0./", $cell)) {
 838                  $ws->write_number($row, $col, $cell, $fmt);
 839              } else {
 840                  $ws->write_string($row, $col, $cell, $fmt);
 841              }
 842          } // end foreach $col
 843  
 844          // increment $row
 845          $row++;
 846      }
 847  }
 848  
 849  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7