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_ods extends grade_export {
  21  
  22      public $plugin = 'ods';
  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      function print_grades() {
  41          global $CFG;
  42          require_once($CFG->dirroot.'/lib/odslib.class.php');
  43  
  44          $export_tracking = $this->track_exports();
  45  
  46          $strgrades = get_string('grades');
  47  
  48          $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
  49  
  50          // Calculate file name
  51          $downloadfilename = clean_filename("$shortname $strgrades.ods");
  52          // Creating a workbook
  53          $workbook = new MoodleODSWorkbook("-");
  54          // Sending HTTP headers
  55          $workbook->send($downloadfilename);
  56          // Adding the worksheet
  57          $myxls = $workbook->add_worksheet($strgrades);
  58  
  59  
  60          // Print names of all the fields.
  61          $profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
  62          foreach ($profilefields as $id => $field) {
  63              $myxls->write_string(0, $id, $field->fullname);
  64          }
  65          $pos = count($profilefields);
  66          if (!$this->onlyactive) {
  67              $myxls->write_string(0, $pos++, get_string("suspended"));
  68          }
  69          foreach ($this->columns as $grade_item) {
  70              foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
  71                  $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, false, $gradedisplayname));
  72              }
  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_ods'));
  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  
  99              if (!$this->onlyactive) {
 100                  $issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
 101                  $myxls->write_string($i, $j++, $issuspended);
 102              }
 103              foreach ($userdata->grades as $itemid => $grade) {
 104                  if ($export_tracking) {
 105                      $status = $geub->track($grade);
 106                  }
 107  
 108                  foreach ($this->displaytype as $gradedisplayconst) {
 109                      $gradestr = $this->format_grade($grade, $gradedisplayconst);
 110                      if (is_numeric($gradestr)) {
 111                          $myxls->write_number($i, $j++, $gradestr);
 112                      } else {
 113                          $myxls->write_string($i, $j++, $gradestr);
 114                      }
 115                  }
 116  
 117                  // writing feedback if requested
 118                  if ($this->export_feedback) {
 119                      $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid], $grade));
 120                  }
 121              }
 122              // Time exported.
 123              $myxls->write_string($i, $j++, time());
 124          }
 125          $gui->close();
 126          $geub->close();
 127  
 128          // Close the workbook.
 129          $workbook->close();
 130  
 131          exit;
 132      }
 133  }
 134  
 135