[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/feedback/ -> analysis_to_excel.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * prints an analysed excel-spreadsheet of the feedback
  19   *
  20   * @author Andreas Grabs
  21   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  22   * @package mod_feedback
  23   */
  24  
  25  require_once("../../config.php");
  26  require_once ("lib.php");
  27  require_once("$CFG->libdir/excellib.class.php");
  28  
  29  feedback_load_feedback_items();
  30  
  31  $id = required_param('id', PARAM_INT);  //the POST dominated the GET
  32  $coursefilter = optional_param('coursefilter', '0', PARAM_INT);
  33  
  34  $url = new moodle_url('/mod/feedback/analysis_to_excel.php', array('id'=>$id));
  35  if ($coursefilter !== '0') {
  36      $url->param('coursefilter', $coursefilter);
  37  }
  38  $PAGE->set_url($url);
  39  
  40  $formdata = data_submitted();
  41  
  42  if (! $cm = get_coursemodule_from_id('feedback', $id)) {
  43      print_error('invalidcoursemodule');
  44  }
  45  
  46  if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
  47      print_error('coursemisconf');
  48  }
  49  
  50  if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) {
  51      print_error('invalidcoursemodule');
  52  }
  53  
  54  $context = context_module::instance($cm->id);
  55  
  56  require_login($course, true, $cm);
  57  
  58  require_capability('mod/feedback:viewreports', $context);
  59  
  60  //buffering any output
  61  //this prevents some output before the excel-header will be send
  62  ob_start();
  63  $fstring = new stdClass();
  64  $fstring->bold = get_string('bold', 'feedback');
  65  $fstring->page = get_string('page', 'feedback');
  66  $fstring->of = get_string('of', 'feedback');
  67  $fstring->modulenameplural = get_string('modulenameplural', 'feedback');
  68  $fstring->questions = get_string('questions', 'feedback');
  69  $fstring->itemlabel = get_string('item_label', 'feedback');
  70  $fstring->question = get_string('question', 'feedback');
  71  $fstring->responses = get_string('responses', 'feedback');
  72  $fstring->idnumber = get_string('idnumber');
  73  $fstring->username = get_string('username');
  74  $fstring->fullname = get_string('fullnameuser');
  75  $fstring->courseid = get_string('courseid', 'feedback');
  76  $fstring->course = get_string('course');
  77  $fstring->anonymous_user = get_string('anonymous_user', 'feedback');
  78  ob_end_clean();
  79  
  80  //get the questions (item-names)
  81  $params = array('feedback' => $feedback->id, 'hasvalue' => 1);
  82  if (!$items = $DB->get_records('feedback_item', $params, 'position')) {
  83      print_error('no_items_available_yet',
  84                  'feedback',
  85                  $CFG->wwwroot.'/mod/feedback/view.php?id='.$id);
  86      exit;
  87  }
  88  
  89  $filename = "feedback.xls";
  90  
  91  $mygroupid = groups_get_activity_group($cm);
  92  
  93  // Creating a workbook
  94  $workbook = new MoodleExcelWorkbook('-');
  95  $workbook->send($filename);
  96  
  97  //creating the needed formats
  98  $xls_formats = new stdClass();
  99  $xls_formats->head1 = $workbook->add_format(array(
 100                          'bold'=>1,
 101                          'size'=>12));
 102  
 103  $xls_formats->head2 = $workbook->add_format(array(
 104                          'align'=>'left',
 105                          'bold'=>1,
 106                          'bottum'=>2));
 107  
 108  $xls_formats->default = $workbook->add_format(array(
 109                          'align'=>'left',
 110                          'v_align'=>'top'));
 111  
 112  $xls_formats->value_bold = $workbook->add_format(array(
 113                          'align'=>'left',
 114                          'bold'=>1,
 115                          'v_align'=>'top'));
 116  
 117  $xls_formats->procent = $workbook->add_format(array(
 118                          'align'=>'left',
 119                          'bold'=>1,
 120                          'v_align'=>'top',
 121                          'num_format'=>'#,##0.00%'));
 122  
 123  // Creating the worksheets
 124  $sheetname = clean_param($feedback->name, PARAM_ALPHANUM);
 125  error_reporting(0);
 126  $worksheet1 = $workbook->add_worksheet(substr($sheetname, 0, 31));
 127  $worksheet2 = $workbook->add_worksheet('detailed');
 128  error_reporting($CFG->debug);
 129  $worksheet1->hide_gridlines();
 130  $worksheet1->set_column(0, 0, 10);
 131  $worksheet1->set_column(1, 1, 30);
 132  $worksheet1->set_column(2, 20, 15);
 133  
 134  //writing the table header
 135  $row_offset1 = 0;
 136  $worksheet1->write_string($row_offset1, 0, userdate(time()), $xls_formats->head1);
 137  
 138  ////////////////////////////////////////////////////////////////////////
 139  //print the analysed sheet
 140  ////////////////////////////////////////////////////////////////////////
 141  //get the completeds
 142  $completedscount = feedback_get_completeds_group_count($feedback, $mygroupid, $coursefilter);
 143  if ($completedscount > 0) {
 144      //write the count of completeds
 145      $row_offset1++;
 146      $worksheet1->write_string($row_offset1,
 147                                0,
 148                                $fstring->modulenameplural.': '.strval($completedscount),
 149                                $xls_formats->head1);
 150  }
 151  
 152  if (is_array($items)) {
 153      $row_offset1++;
 154      $worksheet1->write_string($row_offset1,
 155                                0,
 156                                $fstring->questions.': '. strval(count($items)),
 157                                $xls_formats->head1);
 158  }
 159  
 160  $row_offset1 += 2;
 161  $worksheet1->write_string($row_offset1, 0, $fstring->itemlabel, $xls_formats->head1);
 162  $worksheet1->write_string($row_offset1, 1, $fstring->question, $xls_formats->head1);
 163  $worksheet1->write_string($row_offset1, 2, $fstring->responses, $xls_formats->head1);
 164  $row_offset1++;
 165  
 166  if (empty($items)) {
 167       $items=array();
 168  }
 169  foreach ($items as $item) {
 170      //get the class of item-typ
 171      $itemobj = feedback_get_item_class($item->typ);
 172      $row_offset1 = $itemobj->excelprint_item($worksheet1,
 173                                              $row_offset1,
 174                                              $xls_formats,
 175                                              $item,
 176                                              $mygroupid,
 177                                              $coursefilter);
 178  }
 179  
 180  ////////////////////////////////////////////////////////////////////////
 181  //print the detailed sheet
 182  ////////////////////////////////////////////////////////////////////////
 183  //get the completeds
 184  
 185  $completeds = feedback_get_completeds_group($feedback, $mygroupid, $coursefilter);
 186  //important: for each completed you have to print each item, even if it is not filled out!!!
 187  //therefor for each completed we have to iterate over all items of the feedback
 188  //this is done by feedback_excelprint_detailed_items
 189  
 190  $row_offset2 = 0;
 191  //first we print the table-header
 192  $row_offset2 = feedback_excelprint_detailed_head($worksheet2, $xls_formats, $items, $row_offset2);
 193  
 194  
 195  if (is_array($completeds)) {
 196      foreach ($completeds as $completed) {
 197          $row_offset2 = feedback_excelprint_detailed_items($worksheet2,
 198                                                           $xls_formats,
 199                                                           $completed,
 200                                                           $items,
 201                                                           $row_offset2);
 202      }
 203  }
 204  
 205  
 206  $workbook->close();
 207  exit;
 208  ////////////////////////////////////////////////////////////////////////////////
 209  ////////////////////////////////////////////////////////////////////////////////
 210  //functions
 211  ////////////////////////////////////////////////////////////////////////////////
 212  
 213  
 214  function feedback_excelprint_detailed_head(&$worksheet, $xls_formats, $items, $row_offset) {
 215      global $fstring, $feedback;
 216  
 217      if (!$items) {
 218          return;
 219      }
 220      $col_offset = 0;
 221  
 222      $worksheet->write_string($row_offset + 1, $col_offset, $fstring->idnumber, $xls_formats->head2);
 223      $col_offset++;
 224  
 225      $worksheet->write_string($row_offset + 1, $col_offset, $fstring->username, $xls_formats->head2);
 226      $col_offset++;
 227  
 228      $worksheet->write_string($row_offset + 1, $col_offset, $fstring->fullname, $xls_formats->head2);
 229      $col_offset++;
 230  
 231      foreach ($items as $item) {
 232          $worksheet->write_string($row_offset, $col_offset, $item->name, $xls_formats->head2);
 233          $worksheet->write_string($row_offset + 1, $col_offset, $item->label, $xls_formats->head2);
 234          $col_offset++;
 235      }
 236  
 237      $worksheet->write_string($row_offset + 1, $col_offset, $fstring->courseid, $xls_formats->head2);
 238      $col_offset++;
 239  
 240      $worksheet->write_string($row_offset + 1, $col_offset, $fstring->course, $xls_formats->head2);
 241      $col_offset++;
 242  
 243      return $row_offset + 2;
 244  }
 245  
 246  function feedback_excelprint_detailed_items(&$worksheet, $xls_formats,
 247                                              $completed, $items, $row_offset) {
 248      global $DB, $fstring;
 249  
 250      if (!$items) {
 251          return;
 252      }
 253      $col_offset = 0;
 254      $courseid = 0;
 255  
 256      $feedback = $DB->get_record('feedback', array('id'=>$completed->feedback));
 257      //get the username
 258      //anonymous users are separated automatically because the userid in the completed is "0"
 259      if ($user = $DB->get_record('user', array('id'=>$completed->userid))) {
 260          if ($completed->anonymous_response == FEEDBACK_ANONYMOUS_NO) {
 261              $worksheet->write_string($row_offset, $col_offset, $user->idnumber, $xls_formats->head2);
 262              $col_offset++;
 263              $userfullname = fullname($user);
 264              $worksheet->write_string($row_offset, $col_offset, $user->username, $xls_formats->head2);
 265              $col_offset++;
 266          } else {
 267              $userfullname = $fstring->anonymous_user;
 268              $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
 269              $col_offset++;
 270              $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
 271              $col_offset++;
 272          }
 273      } else {
 274          $userfullname = $fstring->anonymous_user;
 275          $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
 276          $col_offset++;
 277          $worksheet->write_string($row_offset, $col_offset, '-', $xls_formats->head2);
 278          $col_offset++;
 279      }
 280  
 281      $worksheet->write_string($row_offset, $col_offset, $userfullname, $xls_formats->head2);
 282  
 283      $col_offset++;
 284      foreach ($items as $item) {
 285          $params = array('item' => $item->id, 'completed' => $completed->id);
 286          $value = $DB->get_record('feedback_value', $params);
 287  
 288          $itemobj = feedback_get_item_class($item->typ);
 289          $printval = $itemobj->get_printval($item, $value);
 290          $printval = trim($printval);
 291  
 292          if (is_numeric($printval)) {
 293              $worksheet->write_number($row_offset, $col_offset, $printval, $xls_formats->default);
 294          } else if ($printval != '') {
 295              $worksheet->write_string($row_offset, $col_offset, $printval, $xls_formats->default);
 296          }
 297          $printval = '';
 298          $col_offset++;
 299          $courseid = isset($value->course_id) ? $value->course_id : 0;
 300          if ($courseid == 0) {
 301              $courseid = $feedback->course;
 302          }
 303      }
 304      $worksheet->write_number($row_offset, $col_offset, $courseid, $xls_formats->default);
 305      $col_offset++;
 306      if (isset($courseid) AND $course = $DB->get_record('course', array('id' => $courseid))) {
 307          $coursecontext = context_course::instance($courseid);
 308          $shortname = format_string($course->shortname, true, array('context' => $coursecontext));
 309          $worksheet->write_string($row_offset, $col_offset, $shortname, $xls_formats->default);
 310      }
 311      return $row_offset + 1;
 312  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1