Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.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  require_once($CFG->libdir . '/csvlib.class.php');
  20  
  21  class grade_export_txt extends grade_export {
  22  
  23      public $plugin = 'txt';
  24  
  25      public $separator; // default separator
  26  
  27      /**
  28       * Constructor should set up all the private variables ready to be pulled
  29       * @param object $course
  30       * @param int $groupid id of selected group, 0 means all
  31       * @param stdClass $formdata The validated data from the grade export form.
  32       */
  33      public function __construct($course, $groupid, $formdata) {
  34          parent::__construct($course, $groupid, $formdata);
  35          $this->separator = $formdata->separator;
  36  
  37          // Overrides.
  38          $this->usercustomfields = true;
  39      }
  40  
  41      public function get_export_params() {
  42          $params = parent::get_export_params();
  43          $params['separator'] = $this->separator;
  44          return $params;
  45      }
  46  
  47      public function print_grades() {
  48          global $CFG;
  49  
  50          $export_tracking = $this->track_exports();
  51  
  52          $strgrades = get_string('grades');
  53          $profilefields = grade_helper::get_user_profile_fields($this->course->id, $this->usercustomfields);
  54  
  55          $shortname = format_string($this->course->shortname, true, array('context' => context_course::instance($this->course->id)));
  56          $downloadfilename = clean_filename("$shortname $strgrades");
  57          $csvexport = new csv_export_writer($this->separator);
  58          $csvexport->set_filename($downloadfilename);
  59  
  60          // Print names of all the fields
  61          $exporttitle = array();
  62          foreach ($profilefields as $field) {
  63              $exporttitle[] = $field->fullname;
  64          }
  65  
  66          if (!$this->onlyactive) {
  67              $exporttitle[] = get_string("suspended");
  68          }
  69  
  70          // Add grades and feedback columns.
  71          foreach ($this->columns as $grade_item) {
  72              foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
  73                  $exporttitle[] = $this->format_column_name($grade_item, false, $gradedisplayname);
  74              }
  75              if ($this->export_feedback) {
  76                  $exporttitle[] = $this->format_column_name($grade_item, true);
  77              }
  78          }
  79          // Last downloaded column header.
  80          $exporttitle[] = get_string('timeexported', 'gradeexport_txt');
  81          $csvexport->add_data($exporttitle);
  82  
  83          // Print all the lines of data.
  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  
  91              $exportdata = array();
  92              $user = $userdata->user;
  93  
  94              foreach ($profilefields as $field) {
  95                  $fieldvalue = grade_helper::get_user_field_value($user, $field);
  96                  $exportdata[] = $fieldvalue;
  97              }
  98              if (!$this->onlyactive) {
  99                  $issuspended = ($user->suspendedenrolment) ? get_string('yes') : '';
 100                  $exportdata[] = $issuspended;
 101              }
 102              foreach ($userdata->grades as $itemid => $grade) {
 103                  if ($export_tracking) {
 104                      $status = $geub->track($grade);
 105                  }
 106  
 107                  foreach ($this->displaytype as $gradedisplayconst) {
 108                      $exportdata[] = $this->format_grade($grade, $gradedisplayconst);
 109                  }
 110  
 111                  if ($this->export_feedback) {
 112                      $exportdata[] = $this->format_feedback($userdata->feedbacks[$itemid], $grade);
 113                  }
 114              }
 115              // Time exported.
 116              $exportdata[] = time();
 117              $csvexport->add_data($exportdata);
 118          }
 119          $gui->close();
 120          $geub->close();
 121          $csvexport->download_file();
 122          exit;
 123      }
 124  }
 125  
 126