6ca697ad6072df449b700369334d15b4dbc30769
[moodle.git] / grade / export / txt / grade_export_txt.php
1 <?php
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/>.
18 require_once($CFG->dirroot.'/grade/export/lib.php');
20 class grade_export_txt extends grade_export {
22     public $plugin = 'txt';
24     public $separator; // default separator
26     public function grade_export_txt($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $separator='comma', $onlyactive = false) {
27         $this->grade_export($course, $groupid, $itemlist, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints, $onlyactive);
28         $this->separator = $separator;
29     }
31     public function __construct($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2, $separator = 'comma', $onlyactive = false) {
32         parent::__construct($course, $groupid, $itemlist, $export_feedback, $updatedgradesonly, $displaytype, $decimalpoints, $onlyactive);
33         $this->separator = $separator;
34     }
36     public function get_export_params() {
37         $params = parent::get_export_params();
38         $params['separator'] = $this->separator;
39         return $params;
40     }
42     public function print_grades() {
43         global $CFG;
45         $export_tracking = $this->track_exports();
47         $strgrades = get_string('grades');
49         switch ($this->separator) {
50             case 'comma':
51                 $separator = ",";
52                 break;
53             case 'tab':
54             default:
55                 $separator = "\t";
56         }
58         /// Print header to force download
59         if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
60             @header('Cache-Control: max-age=10');
61             @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
62             @header('Pragma: ');
63         } else { //normal http - prevent caching at all cost
64             @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
65             @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
66             @header('Pragma: no-cache');
67         }
68         header("Content-Type: application/download\n");
69         $shortname = format_string($this->course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $this->course->id)));
70         $downloadfilename = clean_filename("$shortname $strgrades");
71         header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\"");
73 /// Print names of all the fields
74         echo get_string("firstname").$separator.
75              get_string("lastname").$separator.
76              get_string("idnumber").$separator.
77              get_string("institution").$separator.
78              get_string("department").$separator.
79              get_string("email");
81         foreach ($this->columns as $grade_item) {
82             echo $separator.$this->format_column_name($grade_item);
84             /// add a feedback column
85             if ($this->export_feedback) {
86                 echo $separator.$this->format_column_name($grade_item, true);
87             }
88         }
89         echo "\n";
91 /// Print all the lines of data.
92         $geub = new grade_export_update_buffer();
93         $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
94         $gui->require_active_enrolment($this->onlyactive);
95         $gui->init();
96         while ($userdata = $gui->next_user()) {
98             $user = $userdata->user;
100             echo $user->firstname.$separator.$user->lastname.$separator.$user->idnumber.$separator.$user->institution.$separator.$user->department.$separator.$user->email;
102             foreach ($userdata->grades as $itemid => $grade) {
103                 if ($export_tracking) {
104                     $status = $geub->track($grade);
105                 }
107                 echo $separator.$this->format_grade($grade);
109                 if ($this->export_feedback) {
110                     echo $separator.$this->format_feedback($userdata->feedbacks[$itemid]);
111                 }
112             }
113             echo "\n";
114         }
115         $gui->close();
116         $geub->close();
118         exit;
119     }