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
48 * Constructor should set up all the private variables ready to be pulled
49 * @param object $course
50 * @param int $groupid id of selected group, 0 means all
51 * @param string $itemlist comma separated list of item ids, empty means all
52 * @param boolean $export_feedback
53 * @param boolean $export_letters
54 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
56 function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $export_letters=false) {
57 $this->course
= $course;
58 $this->groupid
= $groupid;
59 $this->grade_items
= grade_item
::fetch_all(array('courseid'=>$this->course
->id
));
61 $this->columns
= array();
62 if (!empty($itemlist)) {
63 $itemids = explode(',', $itemlist);
64 // remove items that are not requested
65 foreach ($itemids as $itemid) {
66 if (array_key_exists($itemid, $this->grade_items
)) {
67 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
71 foreach ($this->grade_items
as $itemid=>$unused) {
72 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
76 $this->export_letters
= $export_letters;
77 $this->export_feedback
= $export_feedback;
79 $this->previewrows
= false;
83 * Init object based using data from form
84 * @param object $formdata
86 function process_form($formdata) {
89 $this->columns
= array();
90 if (!empty($formdata->itemids
)) {
91 foreach ($formdata->itemids
as $itemid=>$selected) {
92 if ($selected and array_key_exists($itemid, $this->grade_items
)) {
93 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
97 foreach ($this->grade_items
as $itemid=>$unused) {
98 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
102 if (isset($formdata->key
)) {
103 if ($formdata->key
== 1 && isset($formdata->iprestriction
) && isset($formdata->validuntil
)) {
105 $formdata->key
= create_user_key('grade/export', $USER->id
, $this->course
->id
, $formdata->iprestriction
, $formdata->validuntil
);
107 $this->userkey
= $formdata->key
;
110 if (isset($formdata->export_letters
)) {
111 $this->export_letters
= $formdata->export_letters
;
114 if (isset($formdata->export_feedback
)) {
115 $this->export_feedback
= $formdata->export_feedback
;
118 if (isset($formdata->previewrows
)) {
119 $this->previewrows
= $formdata->previewrows
;
125 * Update exported field in grade_grades table
128 function track_exports() {
131 /// Whether this plugin is entitled to update export time
132 if ($expplugins = explode(",", $CFG->gradeexport
)) {
133 if (in_array($this->plugin
, $expplugins)) {
144 * Returns string representation of final grade
145 * @param $object $grade instance of grade_grade class
148 function format_grade($grade) {
150 if ($this->export_letters
) {
151 $displaytype = GRADE_DISPLAY_TYPE_LETTER
;
154 return grade_format_gradevalue($grade->finalgrade
, $this->grade_items
[$grade->itemid
], false, $displaytype, null);
158 * Returns the name of column in export
159 * @param object $grade_item
160 * @param boolena $feedback feedback colum
163 function format_column_name($grade_item, $feedback=false) {
164 if ($grade_item->itemtype
== 'mod') {
165 $name = get_string('modulename', $grade_item->itemmodule
).': '.$grade_item->get_name();
167 $name = $grade_item->get_name();
171 $name .= ' ('.get_string('feedback').')';
174 return strip_tags($name);
178 * Returns formatted grade feedback
179 * @param object $feedback object with properties feedback and feedbackformat
182 function format_feedback($feedback) {
183 return strip_tags(format_text($feedback->feedback
, $feedback->feedbackformat
));
187 * Implemented by child class
189 function print_grades() { }
192 * Prints preview of exported grades on screen as a feedback mechanism
194 function display_preview() {
196 print_heading(get_string('previewrows', 'grades'));
200 echo '<th>'.get_string("firstname")."</th>".
201 '<th>'.get_string("lastname")."</th>".
202 '<th>'.get_string("idnumber")."</th>".
203 '<th>'.get_string("institution")."</th>".
204 '<th>'.get_string("department")."</th>".
205 '<th>'.get_string("email")."</th>";
206 foreach ($this->columns
as $grade_item) {
207 echo '<th>'.$this->format_column_name($grade_item).'</th>';
209 /// add a column_feedback column
210 if ($this->export_feedback
) {
211 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
215 /// Print all the lines of data.
218 $gui = new graded_users_iterator($this->course
, $this->columns
, $this->groupid
);
220 while ($userdata = $gui->next_user()) {
221 // number of preview rows
222 if ($this->previewrows
and $this->previewrows
< ++
$i) {
225 $user = $userdata->user
;
228 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>";
229 foreach ($this->columns
as $itemid=>$unused) {
230 $gradetxt = $this->format_grade($userdata->grades
[$itemid]);
231 echo "<td>$gradetxt</td>";
233 if ($this->export_feedback
) {
234 echo '<td>'.$this->format_feedback($userdata->feedbacks
[$itemid]).'</td>';
244 * Returns array of parameters used by dump.php and export.php.
247 function get_export_params() {
248 $itemids = array_keys($this->columns
);
250 $params = array('id' =>$this->course
->id
,
251 'groupid' =>$this->groupid
,
252 'itemids' =>implode(',', $itemids),
253 'export_letters' =>$this->export_letters
,
254 'export_feedback'=>$this->export_feedback
);
260 * Either prints a "Export" box, which will redirect the user to the download page,
261 * or prints the URL for the published data.
264 function print_continue() {
267 $params = $this->get_export_params();
270 print_heading(get_string('export', 'grades'));
272 echo '<div class="gradeexportlink">';
273 if (!$this->userkey
) { // this button should trigger a download prompt
274 print_single_button($CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/export.php',
275 $params, get_string('download', 'admin'));
280 foreach($params as $name=>$value) {
281 $paramstr .= $sep.$name.'='.$value;
285 $link = $CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/dump.php'.$paramstr.'&key='.$this->userkey
;
287 echo get_string('download', 'admin').': <a href="'.$link.'">'.$link.'</a>';
294 * This class is used to update the exported field in grade_grades.
295 * It does internal buffering to speedup the db operations.
297 class grade_export_update_buffer
{
302 * Constructor - creates the buffer and initialises the time stamp
304 function grade_export_update_buffer() {
305 $this->update_list
= array();
306 $this->export_time
= time();
309 function flush($buffersize) {
312 if (count($this->update_list
) > $buffersize) {
313 $list = implode(',', $this->update_list
);
314 $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)";
315 execute_sql($sql, false);
316 $this->update_list
= array();
321 * Track grade export status
322 * @param object $grade_grade
323 * @return string $status (unknow, new, regrade, nochange)
325 function track($grade_grade) {
326 if (empty($grade_grade->exported
) or empty($grade_grade->timemodified
)) {
327 if (is_null($grade_grade->finalgrade
)) {
328 // grade does not exist yet
332 $this->update_list
[] = $grade_grade->id
;
335 } else if ($grade_grade->exported
< $grade_grade->timemodified
) {
337 $this->update_list
[] = $grade_grade->id
;
339 } else if ($grade_grade->exported
>= $grade_grade->timemodified
) {
340 $status = 'nochange';
343 // something is wrong?
353 * Flush and close the buffer.