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
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);
25 require_once $CFG->libdir
.'/adminlib.php';
26 admin_externalpage_setup('outcomes');
29 if (!confirm_sesskey()) {
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, ';', '"');
46 $outcomes = array_merge(grade_outcome
::fetch_all_global(), grade_outcome
::fetch_all_local($courseid));
48 $outcomes = grade_outcome
::fetch_all_global();
51 foreach($outcomes as $outcome) {
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, ';', '"');
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 = '"') {
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) {
88 $len = strlen($value);
89 for ($i=0;$i<$len;$i++
) {
90 if ($value[$i] == $escape_char) {
92 } else if (!$escaped && $value[$i] == $enclosure) {
100 $str .= $str2.$delimiter;
102 $str .= $value.$delimiter;
105 $str = substr($str,0,-1);