"MDL-12304, fix double text"
[moodle-linuxchix.git] / grade / edit / outcome / export.php
blobc2ebf89e9dcb54d0fee902748f83f621308a9595
1 <?php // $Id$
2 // Exports selected outcomes in CSV format.
4 require_once '../../../config.php';
5 require_once $CFG->dirroot.'/grade/lib.php';
6 require_once $CFG->libdir.'/gradelib.php';
8 $courseid = optional_param('id', 0, PARAM_INT);
9 $action = optional_param('action', '', PARAM_ALPHA);
11 /// Make sure they can even access this course
12 if ($courseid) {
13 if (!$course = get_record('course', 'id', $courseid)) {
14 print_error('nocourseid');
16 require_login($course);
17 $context = get_context_instance(CONTEXT_COURSE, $course->id);
18 require_capability('moodle/grade:manage', $context);
20 if (empty($CFG->enableoutcomes)) {
21 redirect('../../index.php?id='.$courseid);
24 } else {
25 require_once $CFG->libdir.'/adminlib.php';
26 admin_externalpage_setup('outcomes');
29 if (!confirm_sesskey()) {
30 break;
32 // $outcome = grade_outcome::fetch(array('id'=>$outcomeid));
34 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
36 header("Content-Type: text/csv; charset=utf-8");
37 // TODO: make the filename more useful, include a date, a specific name, something...
38 header('Content-Disposition: attachment; filename=outcomes.csv');
40 // sending header with clear names, to make 'what is what' as easy as possible to understand
41 $header = array('outcome_name', 'outcome_shortname', 'outcome_description', 'scale_name', 'scale_items', 'scale_description');
42 echo format_csv($header, ';', '"');
44 $outcomes = array();
45 if ( $courseid ) {
46 $outcomes = array_merge(grade_outcome::fetch_all_global(), grade_outcome::fetch_all_local($courseid));
47 } else {
48 $outcomes = grade_outcome::fetch_all_global();
51 foreach($outcomes as $outcome) {
53 $line = array();
55 $line[] = $outcome->get_name();
56 $line[] = $outcome->get_shortname();
57 $line[] = $outcome->description;
59 $scale = $outcome->load_scale();
60 $line[] = $scale->get_name();
61 $line[] = $scale->compact_items();
62 $line[] = $scale->description;
64 echo format_csv($line, ';', '"');
67 /**
68 * Formats and returns a line of data, in CSV format. This code
69 * is from http://au2.php.net/manual/en/function.fputcsv.php#77866
71 * @params array-of-string $fields data to be exported
72 * @params char $delimiter char to be used to separate fields
73 * @params char $enclosure char used to enclose strings that contains newlines, spaces, tabs or the delimiter char itself
74 * @returns string one line of csv data
76 function format_csv($fields = array(), $delimiter = ';', $enclosure = '"') {
77 $str = '';
78 $escape_char = '\\';
79 foreach ($fields as $value) {
80 if (strpos($value, $delimiter) !== false ||
81 strpos($value, $enclosure) !== false ||
82 strpos($value, "\n") !== false ||
83 strpos($value, "\r") !== false ||
84 strpos($value, "\t") !== false ||
85 strpos($value, ' ') !== false) {
86 $str2 = $enclosure;
87 $escaped = 0;
88 $len = strlen($value);
89 for ($i=0;$i<$len;$i++) {
90 if ($value[$i] == $escape_char) {
91 $escaped = 1;
92 } else if (!$escaped && $value[$i] == $enclosure) {
93 $str2 .= $enclosure;
94 } else {
95 $escaped = 0;
97 $str2 .= $value[$i];
99 $str2 .= $enclosure;
100 $str .= $str2.$delimiter;
101 } else {
102 $str .= $value.$delimiter;
105 $str = substr($str,0,-1);
106 $str .= "\n";
108 return $str;