3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.com //
10 // Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
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. //
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: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
26 require_once($CFG->dirroot
.'/lib/gradelib.php');
27 require_once($CFG->dirroot
.'/grade/lib.php');
28 require_once($CFG->dirroot
.'/grade/export/grade_export_form.php');
35 var $plugin; // plgin name - must be filled in subclasses!
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
42 var $previewrows; // number of rows in preview
43 var $export_letters; // export letters
44 var $export_feedback; // export feedback
45 var $userkey; // export using private user key
47 var $updatedgradesonly; // only export updated grades
49 * Constructor should set up all the private variables ready to be pulled
50 * @param object $course
51 * @param int $groupid id of selected group, 0 means all
52 * @param string $itemlist comma separated list of item ids, empty means all
53 * @param boolean $export_feedback
54 * @param boolean $export_letters
55 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
57 function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $export_letters=false, $updatedgradesonly = false) {
58 $this->course
= $course;
59 $this->groupid
= $groupid;
60 $this->grade_items
= grade_item
::fetch_all(array('courseid'=>$this->course
->id
));
62 $this->columns
= array();
63 if (!empty($itemlist)) {
64 $itemids = explode(',', $itemlist);
65 // remove items that are not requested
66 foreach ($itemids as $itemid) {
67 if (array_key_exists($itemid, $this->grade_items
)) {
68 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
72 foreach ($this->grade_items
as $itemid=>$unused) {
73 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
77 $this->export_letters
= $export_letters;
78 $this->export_feedback
= $export_feedback;
80 $this->previewrows
= false;
81 $this->updatedgradesonly
= $updatedgradesonly;
85 * Init object based using data from form
86 * @param object $formdata
88 function process_form($formdata) {
91 $this->columns
= array();
92 if (!empty($formdata->itemids
)) {
93 foreach ($formdata->itemids
as $itemid=>$selected) {
94 if ($selected and array_key_exists($itemid, $this->grade_items
)) {
95 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
99 foreach ($this->grade_items
as $itemid=>$unused) {
100 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
104 if (isset($formdata->key
)) {
105 if ($formdata->key
== 1 && isset($formdata->iprestriction
) && isset($formdata->validuntil
)) {
107 $formdata->key
= create_user_key('grade/export', $USER->id
, $this->course
->id
, $formdata->iprestriction
, $formdata->validuntil
);
109 $this->userkey
= $formdata->key
;
112 if (isset($formdata->export_letters
)) {
113 $this->export_letters
= $formdata->export_letters
;
116 if (isset($formdata->export_feedback
)) {
117 $this->export_feedback
= $formdata->export_feedback
;
120 if (isset($formdata->previewrows
)) {
121 $this->previewrows
= $formdata->previewrows
;
127 * Update exported field in grade_grades table
130 function track_exports() {
133 /// Whether this plugin is entitled to update export time
134 if ($expplugins = explode(",", $CFG->gradeexport
)) {
135 if (in_array($this->plugin
, $expplugins)) {
146 * Returns string representation of final grade
147 * @param $object $grade instance of grade_grade class
150 function format_grade($grade) {
152 if ($this->export_letters
) {
153 $displaytype = GRADE_DISPLAY_TYPE_LETTER
;
155 $displaytype = GRADE_DISPLAY_TYPE_REAL
;
158 return grade_format_gradevalue($grade->finalgrade
, $this->grade_items
[$grade->itemid
], false, $displaytype, null);
162 * Returns the name of column in export
163 * @param object $grade_item
164 * @param boolena $feedback feedback colum
167 function format_column_name($grade_item, $feedback=false) {
168 if ($grade_item->itemtype
== 'mod') {
169 $name = get_string('modulename', $grade_item->itemmodule
).': '.$grade_item->get_name();
171 $name = $grade_item->get_name();
175 $name .= ' ('.get_string('feedback').')';
178 return strip_tags($name);
182 * Returns formatted grade feedback
183 * @param object $feedback object with properties feedback and feedbackformat
186 function format_feedback($feedback) {
187 return strip_tags(format_text($feedback->feedback
, $feedback->feedbackformat
));
191 * Implemented by child class
193 function print_grades() { }
196 * Prints preview of exported grades on screen as a feedback mechanism
198 function display_preview() {
200 print_heading(get_string('previewrows', 'grades'));
204 echo '<th>'.get_string("firstname")."</th>".
205 '<th>'.get_string("lastname")."</th>".
206 '<th>'.get_string("idnumber")."</th>".
207 '<th>'.get_string("institution")."</th>".
208 '<th>'.get_string("department")."</th>".
209 '<th>'.get_string("email")."</th>";
210 foreach ($this->columns
as $grade_item) {
211 echo '<th>'.$this->format_column_name($grade_item).'</th>';
213 /// add a column_feedback column
214 if ($this->export_feedback
) {
215 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
219 /// Print all the lines of data.
222 $gui = new graded_users_iterator($this->course
, $this->columns
, $this->groupid
);
224 while ($userdata = $gui->next_user()) {
225 // number of preview rows
226 if ($this->previewrows
and $this->previewrows
< ++
$i) {
229 $user = $userdata->user
;
232 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>";
233 foreach ($this->columns
as $itemid=>$unused) {
234 $gradetxt = $this->format_grade($userdata->grades
[$itemid]);
236 // get the status of this grade, and put it through track to get the status
237 $g = new grade_export_update_buffer();
238 $grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id
));
239 $status = $g->track($grade_grade);
240 echo "status is $status";
241 if ($this->updatedgradesonly
&& ($status == 'nochange' ||
$status == 'unknown')) {
242 echo '<td>'.get_string('unchangedgrade', 'grade').'</td>';
244 echo "<td>$gradetxt</td>";
247 if ($this->export_feedback
) {
248 echo '<td>'.$this->format_feedback($userdata->feedbacks
[$itemid]).'</td>';
258 * Returns array of parameters used by dump.php and export.php.
261 function get_export_params() {
262 $itemids = array_keys($this->columns
);
264 $params = array('id' =>$this->course
->id
,
265 'groupid' =>$this->groupid
,
266 'itemids' =>implode(',', $itemids),
267 'export_letters' =>$this->export_letters
,
268 'export_feedback' =>$this->export_feedback
,
269 'updatedgradesonly' =>$this->updatedgradesonly
);
275 * Either prints a "Export" box, which will redirect the user to the download page,
276 * or prints the URL for the published data.
279 function print_continue() {
282 $params = $this->get_export_params();
285 print_heading(get_string('export', 'grades'));
287 echo '<div class="gradeexportlink">';
288 if (!$this->userkey
) { // this button should trigger a download prompt
289 print_single_button($CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/export.php',
290 $params, get_string('download', 'admin'));
295 foreach($params as $name=>$value) {
296 $paramstr .= $sep.$name.'='.$value;
300 $link = $CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/dump.php'.$paramstr.'&key='.$this->userkey
;
302 echo get_string('download', 'admin').': <a href="'.$link.'">'.$link.'</a>';
309 * This class is used to update the exported field in grade_grades.
310 * It does internal buffering to speedup the db operations.
312 class grade_export_update_buffer
{
317 * Constructor - creates the buffer and initialises the time stamp
319 function grade_export_update_buffer() {
320 $this->update_list
= array();
321 $this->export_time
= time();
324 function flush($buffersize) {
327 if (count($this->update_list
) > $buffersize) {
328 $list = implode(',', $this->update_list
);
329 $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)";
330 execute_sql($sql, false);
331 $this->update_list
= array();
336 * Track grade export status
337 * @param object $grade_grade
338 * @return string $status (unknow, new, regrade, nochange)
340 function track($grade_grade) {
342 if (empty($grade_grade->exported
) or empty($grade_grade->timemodified
)) {
343 if (is_null($grade_grade->finalgrade
)) {
344 // grade does not exist yet
348 $this->update_list
[] = $grade_grade->id
;
351 } else if ($grade_grade->exported
< $grade_grade->timemodified
) {
353 $this->update_list
[] = $grade_grade->id
;
355 } else if ($grade_grade->exported
>= $grade_grade->timemodified
) {
356 $status = 'nochange';
359 // something is wrong?
369 * Flush and close the buffer.