c10ddfb3 |
1 | <?php |
2 | |
3 | /////////////////////////////////////////////////////////////////////////// |
4 | // // |
5 | // NOTICE OF COPYRIGHT // |
6 | // // |
7 | // Moodle - Modular Object-Oriented Dynamic Learning Environment // |
8 | // http://moodle.com // |
9 | // // |
9d1e2185 |
10 | // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com // |
c10ddfb3 |
11 | // // |
12 | // This program is free software; you can redistribute it and/or modify // |
13 | // it under the terms of the GNU General Public License as published by // |
14 | // the Free Software Foundation; either version 2 of the License, or // |
15 | // (at your option) any later version. // |
16 | // // |
17 | // This program is distributed in the hope that it will be useful, // |
18 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
19 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
20 | // GNU General Public License for more details: // |
21 | // // |
22 | // http://www.gnu.org/copyleft/gpl.html // |
23 | // // |
24 | /////////////////////////////////////////////////////////////////////////// |
ce34ed3a |
25 | |
89bd8357 |
26 | require_once($CFG->dirroot.'/lib/gradelib.php'); |
27 | require_once($CFG->dirroot.'/grade/lib.php'); |
a5bc4e6e |
28 | require_once($CFG->dirroot.'/grade/export/grade_export_form.php'); |
0eeada79 |
29 | |
1b074625 |
30 | /** |
31 | * Base export class |
32 | */ |
c10ddfb3 |
33 | class grade_export { |
ba74762b |
34 | |
caffc55a |
35 | var $plugin; // plgin name - must be filled in subclasses! |
36 | |
37 | var $grade_items; // list of all course grade items |
38 | var $groupid; // groupid, 0 means all groups |
39 | var $course; // course object |
40 | var $columns; // array of grade_items selected for export |
41 | |
42 | var $previewrows; // number of rows in preview |
43 | var $export_letters; // export letters - TODO: finish implementation |
44 | var $export_feedback; // export feedback |
45 | var $userkey; // export using private user key |
46 | |
47 | var $letters; // internal |
48 | var $report; // internal |
ba74762b |
49 | |
c10ddfb3 |
50 | /** |
51 | * Constructor should set up all the private variables ready to be pulled |
caffc55a |
52 | * @param object $course |
53 | * @param int $groupid id of selected group, 0 means all |
54 | * @param string $itemlist comma separated list of item ids, empty means all |
55 | * @param boolean $export_feedback |
56 | * @param boolean $export_letters |
0eeada79 |
57 | * @note Exporting as letters will lead to data loss if that exported set it re-imported. |
c10ddfb3 |
58 | */ |
caffc55a |
59 | function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $export_letters=false) { |
60 | $this->course = $course; |
61 | $this->groupid = $groupid; |
62 | $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id)); |
63 | |
64 | $this->columns = array(); |
65 | if (!empty($itemlist)) { |
66 | $itemids = explode(',', $itemlist); |
67 | // remove items that are not requested |
68 | foreach ($itemids as $itemid) { |
69 | if (array_key_exists($itemid, $this->grade_items)) { |
70 | $this->columns[$itemid] =& $this->grade_items[$itemid]; |
71 | } |
72 | } |
73 | } else { |
74 | foreach ($this->grade_items as $itemid=>$unused) { |
75 | $this->columns[$itemid] =& $this->grade_items[$itemid]; |
76 | } |
77 | } |
0e2d708e |
78 | |
caffc55a |
79 | $this->export_letters = $export_letters; |
80 | $this->export_feedback = $export_feedback; |
81 | $this->userkey = ''; |
82 | $this->previewrows = false; |
83 | } |
84 | |
85 | /** |
86 | * Init object based using data from form |
87 | * @param object $formdata |
88 | */ |
89 | function process_form($formdata) { |
90 | global $USER; |
91 | |
92 | $this->columns = array(); |
93 | if (!empty($formdata->itemids)) { |
94 | foreach ($formdata->itemids as $itemid=>$selected) { |
95 | if ($selected and array_key_exists($itemid, $this->grade_items)) { |
96 | $this->columns[$itemid] =& $this->grade_items[$itemid]; |
97 | } |
98 | } |
99 | } else { |
100 | foreach ($this->grade_items as $itemid=>$unused) { |
101 | $this->columns[$itemid] =& $this->grade_items[$itemid]; |
102 | } |
0e2d708e |
103 | } |
104 | |
0e2d708e |
105 | if (isset($formdata->key)) { |
caffc55a |
106 | if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) { |
107 | // Create a new key |
108 | $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil); |
0e2d708e |
109 | } |
110 | $this->userkey = $formdata->key; |
111 | } |
ba74762b |
112 | |
caffc55a |
113 | if (isset($formdata->export_letters)) { |
114 | $this->export_letters = $formdata->export_letters; |
c10ddfb3 |
115 | } |
ba74762b |
116 | |
caffc55a |
117 | if (isset($formdata->export_feedback)) { |
118 | $this->export_feedback = $formdata->export_feedback; |
119 | } |
c10ddfb3 |
120 | |
caffc55a |
121 | if (isset($formdata->previewrows)) { |
122 | $this->previewrows = $formdata->previewrows; |
0bfbab47 |
123 | } |
124 | |
caffc55a |
125 | } |
126 | |
127 | /** |
128 | * Update exported field in grade_grades table |
129 | * @return boolean |
130 | */ |
131 | function track_exports() { |
132 | global $CFG; |
133 | |
134 | /// Whether this plugin is entitled to update export time |
135 | if ($expplugins = explode(",", $CFG->gradeexport)) { |
136 | if (in_array($this->plugin, $expplugins)) { |
137 | return true; |
11745964 |
138 | } else { |
caffc55a |
139 | return false; |
140 | } |
141 | } else { |
142 | return false; |
11745964 |
143 | } |
caffc55a |
144 | } |
b8ff92b6 |
145 | |
caffc55a |
146 | /** |
147 | * internal |
148 | */ |
149 | function _init_letters() { |
150 | global $CFG; |
ba74762b |
151 | |
caffc55a |
152 | if (!isset($this->letters)) { |
153 | if ($this->export_letters) { |
154 | require_once($CFG->dirroot . '/grade/report/lib.php'); |
155 | $this->report = new grade_report($this->course->id, null, null); |
156 | $this->letters = $this->report->get_grade_letters(); |
c10ddfb3 |
157 | } else { |
caffc55a |
158 | $this->letters = false; // false prevents another fetching of grade letters |
c10ddfb3 |
159 | } |
c10ddfb3 |
160 | } |
caffc55a |
161 | } |
c10ddfb3 |
162 | |
caffc55a |
163 | /** |
164 | * Returns string representation of final grade |
165 | * @param $object $grade instance of grade_grade class |
166 | * @return string |
167 | */ |
168 | function format_grade($grade) { |
169 | $this->_init_letters(); |
1e124575 |
170 | |
caffc55a |
171 | //TODO: rewrite the letters handling code - this is slow |
172 | if ($this->letters) { |
173 | $grade_item = $this->grade_items[$grade->itemid]; |
174 | $grade_item_displaytype = $this->report->get_pref('gradedisplaytype', $grade_item->id); |
c10ddfb3 |
175 | |
caffc55a |
176 | if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) { |
177 | return grade_grade::get_letter($this->letters, $grade->finalgrade, $grade_item->grademin, $grade_item->grademax); |
c10ddfb3 |
178 | } |
179 | } |
0e2d708e |
180 | |
caffc55a |
181 | //TODO: format it somehow - scale/letter/number/etc. |
182 | return $grade->finalgrade; |
11745964 |
183 | } |
d9617050 |
184 | |
caffc55a |
185 | /** |
186 | * Returns the name of column in export |
187 | * @param object $grade_item |
188 | * @param boolena $feedback feedback colum |
189 | * &return string |
190 | */ |
191 | function format_column_name($grade_item, $feedback=false) { |
192 | if ($grade_item->itemtype == 'mod') { |
193 | $name = get_string('modulename', $grade_item->itemmodule).': '.$grade_item->get_name(); |
f67e327e |
194 | } else { |
caffc55a |
195 | $name = $grade_item->get_name(); |
d9617050 |
196 | } |
ba74762b |
197 | |
caffc55a |
198 | if ($feedback) { |
199 | $name .= ' ('.get_string('feedback').')'; |
ba74762b |
200 | } |
caffc55a |
201 | |
202 | return strip_tags($name); |
c10ddfb3 |
203 | } |
ba74762b |
204 | |
c10ddfb3 |
205 | /** |
caffc55a |
206 | * Returns formatted grade feedback |
207 | * @param object $feedback object with properties feedback and feedbackformat |
208 | * @return string |
c10ddfb3 |
209 | */ |
caffc55a |
210 | function format_feedback($feedback) { |
211 | return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat)); |
212 | } |
ba74762b |
213 | |
1b074625 |
214 | /** |
caffc55a |
215 | * Implemented by child class |
1b074625 |
216 | */ |
caffc55a |
217 | function print_grades() { } |
11745964 |
218 | |
caffc55a |
219 | /** |
220 | * Prints preview of exported grades on screen as a feedback mechanism |
221 | */ |
222 | function display_preview() { |
f5f4967e |
223 | echo '<table>'; |
224 | echo '<tr>'; |
225 | echo '<th>'.get_string("firstname")."</th>". |
226 | '<th>'.get_string("lastname")."</th>". |
227 | '<th>'.get_string("idnumber")."</th>". |
228 | '<th>'.get_string("institution")."</th>". |
229 | '<th>'.get_string("department")."</th>". |
230 | '<th>'.get_string("email")."</th>"; |
caffc55a |
231 | foreach ($this->columns as $grade_item) { |
232 | echo '<th>'.$this->format_column_name($grade_item).'</th>'; |
ba74762b |
233 | |
234 | /// add a column_feedback column |
caffc55a |
235 | if ($this->export_feedback) { |
236 | echo '<th>'.$this->format_column_name($grade_item, true).'</th>'; |
ba74762b |
237 | } |
1b074625 |
238 | } |
f5f4967e |
239 | echo '</tr>'; |
1b074625 |
240 | /// Print all the lines of data. |
ba74762b |
241 | |
f6eb15ad |
242 | $i = 0; |
caffc55a |
243 | $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid); |
244 | $gui->init(); |
245 | while ($userdata = $gui->next_user()) { |
f6eb15ad |
246 | // number of preview rows |
caffc55a |
247 | if ($this->previewrows and $this->previewrows < ++$i) { |
97599c0a |
248 | break; |
f6eb15ad |
249 | } |
caffc55a |
250 | $user = $userdata->user; |
ba74762b |
251 | |
caffc55a |
252 | echo '<tr>'; |
253 | echo "<td>$user->firstname</td><td>$user->lastname</td><td>$user->idnumber</td><td>$user->institution</td><td>$user->department</td><td>$user->email</td>"; |
254 | foreach ($this->columns as $itemid=>$unused) { |
255 | $gradetxt = $this->format_grade($userdata->grades[$itemid]); |
256 | echo "<td>$gradetxt</td>"; |
ba74762b |
257 | |
caffc55a |
258 | if ($this->export_feedback) { |
259 | echo '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>'; |
ba74762b |
260 | } |
1b074625 |
261 | } |
f5f4967e |
262 | echo "</tr>"; |
263 | } |
264 | echo '</table>'; |
caffc55a |
265 | $gui->close(); |
1b074625 |
266 | } |
0e2d708e |
267 | |
268 | /** |
caffc55a |
269 | * Returns array of parameters used by dump.php and export.php. |
270 | * @return array |
271 | */ |
272 | function get_export_params() { |
273 | $itemids = array_keys($this->columns); |
274 | |
275 | $params = array('id' =>$this->course->id, |
276 | 'groupid' =>$this->groupid, |
277 | 'itemids' =>implode(',', $itemids), |
278 | 'export_letters' =>$this->export_letters, |
279 | 'export_feedback'=>$this->export_feedback); |
280 | |
281 | return $params; |
282 | } |
283 | |
284 | /** |
285 | * Either prints a "Export" box, which will redirect the user to the download page, |
286 | * or prints the URL for the published data. |
0e2d708e |
287 | * @note exit() at the end of the method |
0e2d708e |
288 | * @return void |
289 | */ |
caffc55a |
290 | function print_continue() { |
0e2d708e |
291 | global $CFG; |
292 | |
caffc55a |
293 | $params = $this->get_export_params(); |
294 | |
295 | // this button should trigger a download prompt |
0e2d708e |
296 | if (!$this->userkey) { |
caffc55a |
297 | print_single_button($CFG->wwwroot.'/grade/export/'.$this->plugin.'/export.php', $params, get_string('export', 'grades')); |
0e2d708e |
298 | |
299 | } else { |
caffc55a |
300 | $paramstr = ''; |
301 | $sep = '?'; |
302 | foreach($params as $name=>$value) { |
303 | $paramstr .= $sep.$name.'='.$value; |
304 | $sep = '&'; |
305 | } |
306 | |
307 | $link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&key='.$this->userkey; |
0e2d708e |
308 | |
309 | echo '<p>'; |
310 | echo '<a href="'.$link.'">'.$link.'</a>'; |
311 | echo '</p>'; |
312 | print_footer(); |
313 | } |
314 | exit(); |
315 | } |
c10ddfb3 |
316 | } |
317 | |
9d1e2185 |
318 | ?> |