[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/grade/export/xls/ -> grade_export_xls.php (source)

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  require_once($CFG->dirroot.'/grade/export/lib.php');
  19  
  20  class grade_export_xls extends grade_export {
  21  
  22      public $plugin = 'xls';
  23  
  24      /**
  25       * Constructor should set up all the private variables ready to be pulled
  26       * @param object $course
  27       * @param int $groupid id of selected group, 0 means all
  28       * @param stdClass $formdata The validated data from the grade export form.
  29       */
  30      public function __construct($course, $groupid, $formdata) {
  31          parent::__construct($course, $groupid, $formdata);
  32  
  33          // Overrides.
  34          $this->usercustomfields = true;
  35      }
  36  
  37      /**
  38       * To be implemented by child classes
  39       */
  40      public function print_grades() {
  41          global $CFG;
  42          require_once($CFG->dirroot.'/lib/excellib.class.php');
  43  
  44          $export_tracking = $this->track_exports();
  45  
  46          $strgrades = get_string('grades');
  47  
  48          // Calculate file name
  49          $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
  50          $downloadfilename = clean_filename("$shortname $strgrades.xls");
  51          // Creating a workbook
  52          $workbook = new MoodleExcelWorkbook("-");
  53          // Sending HTTP headers
  54          $workbook->send($downloadfilename);
  55          // Adding the worksheet
  56          $myxls = $workbook->add_worksheet($strgrades);
  57  
  58          // Print names of all the fields
  59          $profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
  60          foreach ($profilefields as $id => $field) {
  61              $myxls->write_string(0, $id, $field->fullname);
  62          }
  63          $pos = count($profilefields);
  64          if (!$this->onlyactive) {
  65              $myxls->write_string(0, $pos++, get_string("suspended"));
  66          }
  67          foreach ($this->columns as $grade_item) {
  68              foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
  69                  $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, false, $gradedisplayname));
  70              }
  71              // Add a column_feedback column
  72              if ($this->export_feedback) {
  73                  $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true));
  74              }
  75          }
  76          // Last downloaded column header.
  77          $myxls->write_string(0, $pos++, get_string('timeexported', 'gradeexport_xls'));
  78  
  79          // Print all the lines of data.
  80          $i = 0;
  81          $geub = new grade_export_update_buffer();
  82          $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
  83          $gui->require_active_enrolment($this->onlyactive);
  84          $gui->allow_user_custom_fields($this->usercustomfields);
  85          $gui->init();
  86          while ($userdata = $gui->next_user()) {
  87              $i++;
  88              $user = $userdata->user;
  89  
  90              foreach ($profilefields as $id => $field) {
  91                  $fieldvalue = grade_helper::get_user_field_value($user, $field);
  92                  $myxls->write_string($i, $id, $fieldvalue);
  93              }
  94              $j = count($profilefields);
  95              if (!$this->onlyactive) {
  96                  $issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
  97                  $myxls->write_string($i, $j++, $issuspended);
  98              }
  99              foreach ($userdata->grades as $itemid => $grade) {
 100                  if ($export_tracking) {
 101                      $status = $geub->track($grade);
 102                  }
 103                  foreach ($this->displaytype as $gradedisplayconst) {
 104                      $gradestr = $this->format_grade($grade, $gradedisplayconst);
 105                      if (is_numeric($gradestr)) {
 106                          $myxls->write_number($i, $j++, $gradestr);
 107                      } else {
 108                          $myxls->write_string($i, $j++, $gradestr);
 109                      }
 110                  }
 111                  // writing feedback if requested
 112                  if ($this->export_feedback) {
 113                      $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid]));
 114                  }
 115              }
 116              // Time exported.
 117              $myxls->write_string($i, $j++, time());
 118          }
 119          $gui->close();
 120          $geub->close();
 121  
 122      /// Close the workbook
 123          $workbook->close();
 124  
 125          exit;
 126      }
 127  }
 128  
 129  


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1