|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00018 // Exports selected outcomes in CSV format. 00019 00020 require_once '../../../config.php'; 00021 require_once $CFG->dirroot.'/grade/lib.php'; 00022 require_once $CFG->libdir.'/gradelib.php'; 00023 00024 $courseid = optional_param('id', 0, PARAM_INT); 00025 $action = optional_param('action', '', PARAM_ALPHA); 00026 00028 if ($courseid) { 00029 if (!$course = $DB->get_record('course', array('id' => $courseid))) { 00030 print_error('nocourseid'); 00031 } 00032 require_login($course); 00033 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00034 require_capability('moodle/grade:manage', $context); 00035 00036 if (empty($CFG->enableoutcomes)) { 00037 redirect('../../index.php?id='.$courseid); 00038 } 00039 00040 } else { 00041 require_once $CFG->libdir.'/adminlib.php'; 00042 admin_externalpage_setup('outcomes'); 00043 } 00044 00045 require_sesskey(); 00046 00047 // $outcome = grade_outcome::fetch(array('id'=>$outcomeid)); 00048 00049 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00050 00051 header("Content-Type: text/csv; charset=utf-8"); 00052 // TODO: make the filename more useful, include a date, a specific name, something... 00053 header('Content-Disposition: attachment; filename=outcomes.csv'); 00054 00055 // sending header with clear names, to make 'what is what' as easy as possible to understand 00056 $header = array('outcome_name', 'outcome_shortname', 'outcome_description', 'scale_name', 'scale_items', 'scale_description'); 00057 echo format_csv($header, ';', '"'); 00058 00059 $outcomes = array(); 00060 if ( $courseid ) { 00061 $outcomes = array_merge(grade_outcome::fetch_all_global(), grade_outcome::fetch_all_local($courseid)); 00062 } else { 00063 $outcomes = grade_outcome::fetch_all_global(); 00064 } 00065 00066 foreach($outcomes as $outcome) { 00067 00068 $line = array(); 00069 00070 $line[] = $outcome->get_name(); 00071 $line[] = $outcome->get_shortname(); 00072 $line[] = $outcome->get_description(); 00073 00074 $scale = $outcome->load_scale(); 00075 $line[] = $scale->get_name(); 00076 $line[] = $scale->compact_items(); 00077 $line[] = $scale->get_description(); 00078 00079 echo format_csv($line, ';', '"'); 00080 } 00081 00091 function format_csv($fields = array(), $delimiter = ';', $enclosure = '"') { 00092 $str = ''; 00093 $escape_char = '\\'; 00094 foreach ($fields as $value) { 00095 if (strpos($value, $delimiter) !== false || 00096 strpos($value, $enclosure) !== false || 00097 strpos($value, "\n") !== false || 00098 strpos($value, "\r") !== false || 00099 strpos($value, "\t") !== false || 00100 strpos($value, ' ') !== false) { 00101 $str2 = $enclosure; 00102 $escaped = 0; 00103 $len = strlen($value); 00104 for ($i=0;$i<$len;$i++) { 00105 if ($value[$i] == $escape_char) { 00106 $escaped = 1; 00107 } else if (!$escaped && $value[$i] == $enclosure) { 00108 $str2 .= $enclosure; 00109 } else { 00110 $escaped = 0; 00111 } 00112 $str2 .= $value[$i]; 00113 } 00114 $str2 .= $enclosure; 00115 $str .= $str2.$delimiter; 00116 } else { 00117 $str .= $value.$delimiter; 00118 } 00119 } 00120 $str = substr($str,0,-1); 00121 $str .= "\n"; 00122 00123 return $str; 00124 } 00125