Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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          // If this file was requested from a form, then mark download as complete (before sending headers).
  49          \core_form\util::form_download_complete();
  50  
  51          // Calculate file name
  52          $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
  53          $downloadfilename = clean_filename("$shortname $strgrades.xls");
  54          // Creating a workbook
  55          $workbook = new MoodleExcelWorkbook("-");
  56          // Sending HTTP headers
  57          $workbook->send($downloadfilename);
  58          // Adding the worksheet
  59          $myxls = $workbook->add_worksheet($strgrades);
  60  
  61          // Print names of all the fields
  62          $profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
  63          foreach ($profilefields as $id => $field) {
  64              $myxls->write_string(0, $id, $field->fullname);
  65          }
  66          $pos = count($profilefields);
  67          if (!$this->onlyactive) {
  68              $myxls->write_string(0, $pos++, get_string("suspended"));
  69          }
  70          foreach ($this->columns as $grade_item) {
  71              foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
  72                  $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, false, $gradedisplayname));
  73              }
  74              // Add a column_feedback column
  75              if ($this->export_feedback) {
  76                  $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true));
  77              }
  78          }
  79          // Last downloaded column header.
  80          $myxls->write_string(0, $pos++, get_string('timeexported', 'gradeexport_xls'));
  81  
  82          // Print all the lines of data.
  83          $i = 0;
  84          $geub = new grade_export_update_buffer();
  85          $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
  86          $gui->require_active_enrolment($this->onlyactive);
  87          $gui->allow_user_custom_fields($this->usercustomfields);
  88          $gui->init();
  89          while ($userdata = $gui->next_user()) {
  90              $i++;
  91              $user = $userdata->user;
  92  
  93              foreach ($profilefields as $id => $field) {
  94                  $fieldvalue = grade_helper::get_user_field_value($user, $field);
  95                  $myxls->write_string($i, $id, $fieldvalue);
  96              }
  97              $j = count($profilefields);
  98              if (!$this->onlyactive) {
  99                  $issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
 100                  $myxls->write_string($i, $j++, $issuspended);
 101              }
 102              foreach ($userdata->grades as $itemid => $grade) {
 103                  if ($export_tracking) {
 104                      $status = $geub->track($grade);
 105                  }
 106                  foreach ($this->displaytype as $gradedisplayconst) {
 107                      $gradestr = $this->format_grade($grade, $gradedisplayconst);
 108                      if (is_numeric($gradestr)) {
 109                          $myxls->write_number($i, $j++, $gradestr);
 110                      } else {
 111                          $myxls->write_string($i, $j++, $gradestr);
 112                      }
 113                  }
 114                  // writing feedback if requested
 115                  if ($this->export_feedback) {
 116                      $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid], $grade));
 117                  }
 118              }
 119              // Time exported.
 120              $myxls->write_string($i, $j++, time());
 121          }
 122          $gui->close();
 123          $geub->close();
 124  
 125      /// Close the workbook
 126          $workbook->close();
 127  
 128          exit;
 129      }
 130  }
 131  
 132