Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/grade/export/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 require_once($CFG->dirroot.'/lib/gradelib.php');
00019 require_once($CFG->dirroot.'/grade/lib.php');
00020 require_once($CFG->dirroot.'/grade/export/grade_export_form.php');
00021 
00025 abstract class grade_export {
00026 
00027     public $plugin; // plgin name - must be filled in subclasses!
00028 
00029     public $grade_items; // list of all course grade items
00030     public $groupid;     // groupid, 0 means all groups
00031     public $course;      // course object
00032     public $columns;     // array of grade_items selected for export
00033 
00034     public $previewrows;     // number of rows in preview
00035     public $export_letters;  // export letters
00036     public $export_feedback; // export feedback
00037     public $userkey;         // export using private user key
00038 
00039     public $updatedgradesonly; // only export updated grades
00040     public $displaytype; // display type (e.g. real, percentages, letter) for exports
00041     public $decimalpoints; // number of decimal points for exports
00052     public function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2) {
00053         $this->course = $course;
00054         $this->groupid = $groupid;
00055         $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id));
00056 
00057         //Populating the columns here is required by /grade/export/(whatever)/export.php
00058         //however index.php, when the form is submitted, will construct the collection here
00059         //with an empty $itemlist then reconstruct it in process_form() using $formdata
00060         $this->columns = array();
00061         if (!empty($itemlist)) {
00062             if ($itemlist=='-1') {
00063                 //user deselected all items
00064             } else {
00065                 $itemids = explode(',', $itemlist);
00066                 // remove items that are not requested
00067                 foreach ($itemids as $itemid) {
00068                     if (array_key_exists($itemid, $this->grade_items)) {
00069                         $this->columns[$itemid] =& $this->grade_items[$itemid];
00070                     }
00071                 }
00072             }
00073         } else {
00074             foreach ($this->grade_items as $itemid=>$unused) {
00075                 $this->columns[$itemid] =& $this->grade_items[$itemid];
00076             }
00077         }
00078 
00079         $this->export_feedback = $export_feedback;
00080         $this->userkey         = '';
00081         $this->previewrows     = false;
00082         $this->updatedgradesonly = $updatedgradesonly;
00083 
00084         $this->displaytype = $displaytype;
00085         $this->decimalpoints = $decimalpoints;
00086     }
00087 
00092     function process_form($formdata) {
00093         global $USER;
00094 
00095         $this->columns = array();
00096         if (!empty($formdata->itemids)) {
00097             if ($formdata->itemids=='-1') {
00098                 //user deselected all items
00099             } else {
00100                 foreach ($formdata->itemids as $itemid=>$selected) {
00101                     if ($selected and array_key_exists($itemid, $this->grade_items)) {
00102                         $this->columns[$itemid] =& $this->grade_items[$itemid];
00103                     }
00104                 }
00105             }
00106         } else {
00107             foreach ($this->grade_items as $itemid=>$unused) {
00108                 $this->columns[$itemid] =& $this->grade_items[$itemid];
00109             }
00110         }
00111 
00112         if (isset($formdata->key)) {
00113             if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) {
00114                 // Create a new key
00115                 $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil);
00116             }
00117             $this->userkey = $formdata->key;
00118         }
00119 
00120         if (isset($formdata->export_letters)) {
00121             $this->export_letters = $formdata->export_letters;
00122         }
00123 
00124         if (isset($formdata->export_feedback)) {
00125             $this->export_feedback = $formdata->export_feedback;
00126         }
00127 
00128         if (isset($formdata->previewrows)) {
00129             $this->previewrows = $formdata->previewrows;
00130         }
00131 
00132     }
00133 
00138     public function track_exports() {
00139         global $CFG;
00140 
00142         if ($expplugins = explode(",", $CFG->gradeexport)) {
00143             if (in_array($this->plugin, $expplugins)) {
00144                 return true;
00145             } else {
00146                 return false;
00147           }
00148         } else {
00149             return false;
00150         }
00151     }
00152 
00158     public function format_grade($grade) {
00159         return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $this->displaytype, $this->decimalpoints);
00160     }
00161 
00168     public function format_column_name($grade_item, $feedback=false) {
00169         if ($grade_item->itemtype == 'mod') {
00170             $name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name();
00171         } else {
00172             $name = $grade_item->get_name();
00173         }
00174 
00175         if ($feedback) {
00176             $name .= ' ('.get_string('feedback').')';
00177         }
00178 
00179         return strip_tags($name);
00180     }
00181 
00187     public function format_feedback($feedback) {
00188         return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat));
00189     }
00190 
00194     public abstract function print_grades();
00195 
00200     public function display_preview($require_user_idnumber=false) {
00201         global $OUTPUT;
00202         echo $OUTPUT->heading(get_string('previewrows', 'grades'));
00203 
00204         echo '<table>';
00205         echo '<tr>';
00206         echo '<th>'.get_string("firstname")."</th>".
00207              '<th>'.get_string("lastname")."</th>".
00208              '<th>'.get_string("idnumber")."</th>".
00209              '<th>'.get_string("institution")."</th>".
00210              '<th>'.get_string("department")."</th>".
00211              '<th>'.get_string("email")."</th>";
00212         foreach ($this->columns as $grade_item) {
00213             echo '<th>'.$this->format_column_name($grade_item).'</th>';
00214 
00216             if ($this->export_feedback) {
00217                 echo '<th>'.$this->format_column_name($grade_item, true).'</th>';
00218             }
00219         }
00220         echo '</tr>';
00222 
00223         $i = 0;
00224         $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid);
00225         $gui->init();
00226         while ($userdata = $gui->next_user()) {
00227             // number of preview rows
00228             if ($this->previewrows and $this->previewrows <= $i) {
00229                 break;
00230             }
00231             $user = $userdata->user;
00232             if ($require_user_idnumber and empty($user->idnumber)) {
00233                 // some exports require user idnumber so we can match up students when importing the data
00234                 continue;
00235             }
00236 
00237             $gradeupdated = false; // if no grade is update at all for this user, do not display this row
00238             $rowstr = '';
00239             foreach ($this->columns as $itemid=>$unused) {
00240                 $gradetxt = $this->format_grade($userdata->grades[$itemid]);
00241 
00242                 // get the status of this grade, and put it through track to get the status
00243                 $g = new grade_export_update_buffer();
00244                 $grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id));
00245                 $status = $g->track($grade_grade);
00246 
00247                 if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) {
00248                     $rowstr .= '<td>'.get_string('unchangedgrade', 'grades').'</td>';
00249                 } else {
00250                     $rowstr .= "<td>$gradetxt</td>";
00251                     $gradeupdated = true;
00252                 }
00253 
00254                 if ($this->export_feedback) {
00255                     $rowstr .=  '<td>'.$this->format_feedback($userdata->feedbacks[$itemid]).'</td>';
00256                 }
00257             }
00258 
00259             // if we are requesting updated grades only, we are not interested in this user at all
00260             if (!$gradeupdated && $this->updatedgradesonly) {
00261                 continue;
00262             }
00263 
00264             echo '<tr>';
00265             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>";
00266             echo $rowstr;
00267             echo "</tr>";
00268 
00269             $i++; // increment the counter
00270         }
00271         echo '</table>';
00272         $gui->close();
00273     }
00274 
00279     public function get_export_params() {
00280         $itemids = array_keys($this->columns);
00281         $itemidsparam = implode(',', $itemids);
00282         if (empty($itemidsparam)) {
00283             $itemidsparam = '-1';
00284         }
00285 
00286         $params = array('id'                =>$this->course->id,
00287                         'groupid'           =>$this->groupid,
00288                         'itemids'           =>$itemidsparam,
00289                         'export_letters'    =>$this->export_letters,
00290                         'export_feedback'   =>$this->export_feedback,
00291                         'updatedgradesonly' =>$this->updatedgradesonly,
00292                         'displaytype'       =>$this->displaytype,
00293                         'decimalpoints'     =>$this->decimalpoints);
00294 
00295         return $params;
00296     }
00297 
00303     public function print_continue() {
00304         global $CFG, $OUTPUT;
00305 
00306         $params = $this->get_export_params();
00307 
00308         echo $OUTPUT->heading(get_string('export', 'grades'));
00309 
00310         echo $OUTPUT->container_start('gradeexportlink');
00311 
00312         if (!$this->userkey) {      // this button should trigger a download prompt
00313             echo $OUTPUT->single_button(new moodle_url('/grade/export/'.$this->plugin.'/export.php', $params), get_string('download', 'admin'));
00314 
00315         } else {
00316             $paramstr = '';
00317             $sep = '?';
00318             foreach($params as $name=>$value) {
00319                 $paramstr .= $sep.$name.'='.$value;
00320                 $sep = '&';
00321             }
00322 
00323             $link = $CFG->wwwroot.'/grade/export/'.$this->plugin.'/dump.php'.$paramstr.'&key='.$this->userkey;
00324 
00325             echo get_string('download', 'admin').': ' . html_writer::link($link, $link);
00326         }
00327         echo $OUTPUT->container_end();
00328     }
00329 }
00330 
00335 class grade_export_update_buffer {
00336     public $update_list;
00337     public $export_time;
00338 
00342     public function grade_export_update_buffer() {
00343         $this->update_list = array();
00344         $this->export_time = time();
00345     }
00346 
00347     public function flush($buffersize) {
00348         global $CFG, $DB;
00349 
00350         if (count($this->update_list) > $buffersize) {
00351             list($usql, $params) = $DB->get_in_or_equal($this->update_list);
00352             $params = array_merge(array($this->export_time), $params);
00353 
00354             $sql = "UPDATE {grade_grades} SET exported = ? WHERE id $usql";
00355             $DB->execute($sql, $params);
00356             $this->update_list = array();
00357         }
00358     }
00359 
00365     public function track($grade_grade) {
00366 
00367         if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) {
00368             if (is_null($grade_grade->finalgrade)) {
00369                 // grade does not exist yet
00370                 $status = 'unknown';
00371             } else {
00372                 $status = 'new';
00373                 $this->update_list[] = $grade_grade->id;
00374             }
00375 
00376         } else if ($grade_grade->exported < $grade_grade->timemodified) {
00377             $status = 'regrade';
00378             $this->update_list[] = $grade_grade->id;
00379 
00380         } else if ($grade_grade->exported >= $grade_grade->timemodified) {
00381             $status = 'nochange';
00382 
00383         } else {
00384             // something is wrong?
00385             $status = 'unknown';
00386         }
00387 
00388         $this->flush(100);
00389 
00390         return $status;
00391     }
00392 
00396     public function close() {
00397         $this->flush(0);
00398     }
00399 }
00400 
 All Data Structures Namespaces Files Functions Variables Enumerations