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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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   * Exports selected outcomes in CSV format
  19   *
  20   * @package   core_grades
  21   * @copyright 2008 Moodle Pty Ltd (http://moodle.com)
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once '../../../config.php';
  26  require_once $CFG->dirroot.'/grade/lib.php';
  27  require_once $CFG->libdir.'/gradelib.php';
  28  
  29  $courseid = optional_param('id', 0, PARAM_INT);
  30  $action   = optional_param('action', '', PARAM_ALPHA);
  31  
  32  /// Make sure they can even access this course
  33  if ($courseid) {
  34      if (!$course = $DB->get_record('course', array('id' => $courseid))) {
  35          throw new \moodle_exception('invalidcourseid');
  36      }
  37      require_login($course);
  38      $context = context_course::instance($course->id);
  39      require_capability('moodle/grade:manage', $context);
  40  
  41      if (empty($CFG->enableoutcomes)) {
  42          redirect('../../index.php?id='.$courseid);
  43      }
  44  
  45  } else {
  46      require_once $CFG->libdir.'/adminlib.php';
  47      admin_externalpage_setup('outcomes');
  48  }
  49  
  50  require_sesskey();
  51  
  52  header("Content-Type: text/csv; charset=utf-8");
  53  // TODO: make the filename more useful, include a date, a specific name, something...
  54  header('Content-Disposition: attachment; filename=outcomes.csv');
  55  
  56  // sending header with clear names, to make 'what is what' as easy as possible to understand
  57  $header = array('outcome_name', 'outcome_shortname', 'outcome_description', 'scale_name', 'scale_items', 'scale_description');
  58  echo format_csv($header, ';', '"');
  59  
  60  $outcomes = array();
  61  if ( $courseid ) {
  62      $outcomes = array_merge(grade_outcome::fetch_all_global(), grade_outcome::fetch_all_local($courseid));
  63  } else {
  64      $outcomes = grade_outcome::fetch_all_global();
  65  }
  66  
  67  foreach($outcomes as $outcome) {
  68  
  69      $line = array();
  70  
  71      $line[] = $outcome->get_name();
  72      $line[] = $outcome->get_shortname();
  73      $line[] = $outcome->get_description();
  74  
  75      $scale = $outcome->load_scale();
  76      $line[] = $scale->get_name();
  77      $line[] = $scale->compact_items();
  78      $line[] = $scale->get_description();
  79  
  80      echo format_csv($line, ';', '"');
  81  }
  82  
  83  /**
  84   * Formats and returns a line of data, in CSV format. This code
  85   * is from http://au2.php.net/manual/en/function.fputcsv.php#77866
  86   *
  87   * @param string[] $fields data to be exported
  88   * @param string $delimiter char to be used to separate fields
  89   * @param string $enclosure char used to enclose strings that contains newlines, spaces, tabs or the delimiter char itself
  90   * @returns string one line of csv data
  91   */
  92  function format_csv($fields = array(), $delimiter = ';', $enclosure = '"') {
  93      $str = '';
  94      $escape_char = '\\';
  95      foreach ($fields as $value) {
  96          if (strpos($value, $delimiter) !== false ||
  97                  strpos($value, $enclosure) !== false ||
  98                  strpos($value, "\n") !== false ||
  99                  strpos($value, "\r") !== false ||
 100                  strpos($value, "\t") !== false ||
 101                  strpos($value, ' ') !== false) {
 102              $str2 = $enclosure;
 103              $escaped = 0;
 104              $len = strlen($value);
 105              for ($i=0;$i<$len;$i++) {
 106                  if ($value[$i] == $escape_char) {
 107                      $escaped = 1;
 108                  } else if (!$escaped && $value[$i] == $enclosure) {
 109                      $str2 .= $enclosure;
 110                  } else {
 111                      $escaped = 0;
 112                  }
 113                  $str2 .= $value[$i];
 114              }
 115              $str2 .= $enclosure;
 116              $str .= $str2.$delimiter;
 117          } else {
 118              $str .= $value.$delimiter;
 119          }
 120      }
 121      $str = substr($str,0,-1);
 122      $str .= "\n";
 123  
 124      return $str;
 125  }
 126