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 - TODO: finish implementation
44 var $export_feedback; // export feedback
45 var $userkey; // export using private user key
47 var $letters; // internal
48 var $report; // internal
51 * Constructor should set up all the private variables ready to be pulled
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
57 * @note Exporting as letters will lead to data loss if that exported set it re-imported.
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
));
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];
74 foreach ($this->grade_items
as $itemid=>$unused) {
75 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
79 $this->export_letters
= $export_letters;
80 $this->export_feedback
= $export_feedback;
82 $this->previewrows
= false;
86 * Init object based using data from form
87 * @param object $formdata
89 function process_form($formdata) {
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];
100 foreach ($this->grade_items
as $itemid=>$unused) {
101 $this->columns
[$itemid] =& $this->grade_items
[$itemid];
105 if (isset($formdata->key
)) {
106 if ($formdata->key
== 1 && isset($formdata->iprestriction
) && isset($formdata->validuntil
)) {
108 $formdata->key
= create_user_key('grade/export', $USER->id
, $this->course
->id
, $formdata->iprestriction
, $formdata->validuntil
);
110 $this->userkey
= $formdata->key
;
113 if (isset($formdata->export_letters
)) {
114 $this->export_letters
= $formdata->export_letters
;
117 if (isset($formdata->export_feedback
)) {
118 $this->export_feedback
= $formdata->export_feedback
;
121 if (isset($formdata->previewrows
)) {
122 $this->previewrows
= $formdata->previewrows
;
128 * Update exported field in grade_grades table
131 function track_exports() {
134 /// Whether this plugin is entitled to update export time
135 if ($expplugins = explode(",", $CFG->gradeexport
)) {
136 if (in_array($this->plugin
, $expplugins)) {
149 function _init_letters() {
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();
158 $this->letters
= false; // false prevents another fetching of grade letters
164 * Returns string representation of final grade
165 * @param $object $grade instance of grade_grade class
168 function format_grade($grade) {
169 $this->_init_letters();
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
);
176 if ($grade_item_displaytype == GRADE_DISPLAY_TYPE_LETTER
) {
177 return grade_grade
::get_letter($this->letters
, $grade->finalgrade
, $grade_item->grademin
, $grade_item->grademax
);
181 //TODO: format it somehow - scale/letter/number/etc.
182 return $grade->finalgrade
;
186 * Returns the name of column in export
187 * @param object $grade_item
188 * @param boolena $feedback feedback colum
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();
195 $name = $grade_item->get_name();
199 $name .= ' ('.get_string('feedback').')';
202 return strip_tags($name);
206 * Returns formatted grade feedback
207 * @param object $feedback object with properties feedback and feedbackformat
210 function format_feedback($feedback) {
211 return strip_tags(format_text($feedback->feedback
, $feedback->feedbackformat
));
215 * Implemented by child class
217 function print_grades() { }
220 * Prints preview of exported grades on screen as a feedback mechanism
222 function display_preview() {
224 print_heading(get_string('previewrows', 'grades'));
228 echo '<th>'.get_string("firstname")."</th>".
229 '<th>'.get_string("lastname")."</th>".
230 '<th>'.get_string("idnumber")."</th>".
231 '<th>'.get_string("institution")."</th>".
232 '<th>'.get_string("department")."</th>".
233 '<th>'.get_string("email")."</th>";
234 foreach ($this->columns
as $grade_item) {
235 echo '<th>'.$this->format_column_name($grade_item).'</th>';
237 /// add a column_feedback column
238 if ($this->export_feedback
) {
239 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
243 /// Print all the lines of data.
246 $gui = new graded_users_iterator($this->course
, $this->columns
, $this->groupid
);
248 while ($userdata = $gui->next_user()) {
249 // number of preview rows
250 if ($this->previewrows
and $this->previewrows
< ++
$i) {
253 $user = $userdata->user
;
256 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>";
257 foreach ($this->columns
as $itemid=>$unused) {
258 $gradetxt = $this->format_grade($userdata->grades
[$itemid]);
259 echo "<td>$gradetxt</td>";
261 if ($this->export_feedback
) {
262 echo '<td>'.$this->format_feedback($userdata->feedbacks
[$itemid]).'</td>';
272 * Returns array of parameters used by dump.php and export.php.
275 function get_export_params() {
276 $itemids = array_keys($this->columns
);
278 $params = array('id' =>$this->course
->id
,
279 'groupid' =>$this->groupid
,
280 'itemids' =>implode(',', $itemids),
281 'export_letters' =>$this->export_letters
,
282 'export_feedback'=>$this->export_feedback
);
288 * Either prints a "Export" box, which will redirect the user to the download page,
289 * or prints the URL for the published data.
292 function print_continue() {
295 $params = $this->get_export_params();
298 print_heading(get_string('export', 'grades'));
300 echo '<div class="gradeexportlink">';
301 if (!$this->userkey
) { // this button should trigger a download prompt
302 print_single_button($CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/export.php',
303 $params, get_string('download', 'admin'));
308 foreach($params as $name=>$value) {
309 $paramstr .= $sep.$name.'='.$value;
313 $link = $CFG->wwwroot
.'/grade/export/'.$this->plugin
.'/dump.php'.$paramstr.'&key='.$this->userkey
;
315 echo get_string('download', 'admin').': <a href="'.$link.'">'.$link.'</a>';
322 * This class is used to update the exported field in grade_grades.
323 * It does internal buffering to speedup the db operations.
325 class grade_export_update_buffer
{
330 * Constructor - creates the buffer and initialises the time stamp
332 function grade_export_update_buffer() {
333 $this->update_list
= array();
334 $this->export_time
= time();
337 function flush($buffersize) {
340 if (count($this->update_list
) > $buffersize) {
341 $list = implode(',', $this->update_list
);
342 $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)";
343 execute_sql($sql, false);
344 $this->update_list
= array();
349 * Track grade export status
350 * @param object $grade_grade
351 * @return string $status (unknow, new, regrade, nochange)
353 function track($grade_grade) {
354 if (empty($grade_grade->exported
) or empty($grade_grade->timemodified
)) {
355 if (is_null($grade_grade->finalgrade
)) {
356 // grade does not exist yet
360 $this->update_list
[] = $grade_grade->id
;
363 } else if ($grade_grade->exported
< $grade_grade->timemodified
) {
365 $this->update_list
[] = $grade_grade->id
;
367 } else if ($grade_grade->exported
>= $grade_grade->timemodified
) {
368 $status = 'nochange';
371 // something is wrong?
381 * Flush and close the buffer.