. require_once($CFG->dirroot.'/lib/gradelib.php'); require_once($CFG->dirroot.'/grade/lib.php'); require_once($CFG->dirroot.'/grade/export/grade_export_form.php'); /** * Base export class */ class grade_export { var $plugin; // plgin name - must be filled in subclasses! var $grade_items; // list of all course grade items var $groupid; // groupid, 0 means all groups var $course; // course object var $columns; // array of grade_items selected for export var $previewrows; // number of rows in preview var $export_letters; // export letters var $export_feedback; // export feedback var $userkey; // export using private user key var $updatedgradesonly; // only export updated grades var $displaytype; // display type (e.g. real, percentages, letter) for exports var $decimalpoints; // number of decimal points for exports /** * Constructor should set up all the private variables ready to be pulled * @param object $course * @param int $groupid id of selected group, 0 means all * @param string $itemlist comma separated list of item ids, empty means all * @param boolean $export_feedback * @param boolean $export_letters * @note Exporting as letters will lead to data loss if that exported set it re-imported. */ function grade_export($course, $groupid=0, $itemlist='', $export_feedback=false, $updatedgradesonly = false, $displaytype = GRADE_DISPLAY_TYPE_REAL, $decimalpoints = 2) { $this->course = $course; $this->groupid = $groupid; $this->grade_items = grade_item::fetch_all(array('courseid'=>$this->course->id)); $this->columns = array(); if (!empty($itemlist)) { $itemids = explode(',', $itemlist); // remove items that are not requested foreach ($itemids as $itemid) { if (array_key_exists($itemid, $this->grade_items)) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } } else { foreach ($this->grade_items as $itemid=>$unused) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } $this->export_feedback = $export_feedback; $this->userkey = ''; $this->previewrows = false; $this->updatedgradesonly = $updatedgradesonly; $this->displaytype = $displaytype; $this->decimalpoints = $decimalpoints; } /** * Init object based using data from form * @param object $formdata */ function process_form($formdata) { global $USER; $this->columns = array(); if (!empty($formdata->itemids)) { foreach ($formdata->itemids as $itemid=>$selected) { if ($selected and array_key_exists($itemid, $this->grade_items)) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } } else { foreach ($this->grade_items as $itemid=>$unused) { $this->columns[$itemid] =& $this->grade_items[$itemid]; } } if (isset($formdata->key)) { if ($formdata->key == 1 && isset($formdata->iprestriction) && isset($formdata->validuntil)) { // Create a new key $formdata->key = create_user_key('grade/export', $USER->id, $this->course->id, $formdata->iprestriction, $formdata->validuntil); } $this->userkey = $formdata->key; } if (isset($formdata->export_letters)) { $this->export_letters = $formdata->export_letters; } if (isset($formdata->export_feedback)) { $this->export_feedback = $formdata->export_feedback; } if (isset($formdata->previewrows)) { $this->previewrows = $formdata->previewrows; } } /** * Update exported field in grade_grades table * @return boolean */ function track_exports() { global $CFG; /// Whether this plugin is entitled to update export time if ($expplugins = explode(",", $CFG->gradeexport)) { if (in_array($this->plugin, $expplugins)) { return true; } else { return false; } } else { return false; } } /** * Returns string representation of final grade * @param $object $grade instance of grade_grade class * @return string */ function format_grade($grade) { return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $this->displaytype, $this->decimalpoints); } /** * Returns the name of column in export * @param object $grade_item * @param boolena $feedback feedback colum * &return string */ function format_column_name($grade_item, $feedback=false) { if ($grade_item->itemtype == 'mod') { $name = get_string('modulename', $grade_item->itemmodule).': '.$grade_item->get_name(); } else { $name = $grade_item->get_name(); } if ($feedback) { $name .= ' ('.get_string('feedback').')'; } return strip_tags($name); } /** * Returns formatted grade feedback * @param object $feedback object with properties feedback and feedbackformat * @return string */ function format_feedback($feedback) { return strip_tags(format_text($feedback->feedback, $feedback->feedbackformat)); } /** * Implemented by child class */ function print_grades() { } /** * Prints preview of exported grades on screen as a feedback mechanism * @param bool $require_user_idnumber true means skip users without idnumber */ function display_preview($require_user_idnumber=false) { print_heading(get_string('previewrows', 'grades')); echo ''; echo ''; echo '". '". '". '". '". '"; foreach ($this->columns as $grade_item) { echo ''; /// add a column_feedback column if ($this->export_feedback) { echo ''; } } echo ''; /// Print all the lines of data. $i = 0; $gui = new graded_users_iterator($this->course, $this->columns, $this->groupid); $gui->init(); while ($userdata = $gui->next_user()) { // number of preview rows if ($this->previewrows and $this->previewrows <= $i) { break; } $user = $userdata->user; if ($require_user_idnumber and empty($user->idnumber)) { // some exports require user idnumber continue; } $gradeupdated = false; // if no grade is update at all for this user, do not display this row $rowstr = ''; foreach ($this->columns as $itemid=>$unused) { $gradetxt = $this->format_grade($userdata->grades[$itemid]); // get the status of this grade, and put it through track to get the status $g = new grade_export_update_buffer(); $grade_grade = new grade_grade(array('itemid'=>$itemid, 'userid'=>$user->id)); $status = $g->track($grade_grade); if ($this->updatedgradesonly && ($status == 'nochange' || $status == 'unknown')) { $rowstr .= ''; } else { $rowstr .= ""; $gradeupdated = true; } if ($this->export_feedback) { $rowstr .= ''; } } // if we are requesting updated grades only, we are not interested in this user at all if (!$gradeupdated && $this->updatedgradesonly) { continue; } echo ''; echo ""; echo $rowstr; echo ""; $i++; // increment the counter } echo '
'.get_string("firstname")."'.get_string("lastname")."'.get_string("idnumber")."'.get_string("institution")."'.get_string("department")."'.get_string("email")."'.$this->format_column_name($grade_item).''.$this->format_column_name($grade_item, true).'
'.get_string('unchangedgrade', 'grades').'$gradetxt'.$this->format_feedback($userdata->feedbacks[$itemid]).'
$user->firstname$user->lastname$user->idnumber$user->institution$user->department$user->email
'; $gui->close(); } /** * Returns array of parameters used by dump.php and export.php. * @return array */ function get_export_params() { $itemids = array_keys($this->columns); $params = array('id' =>$this->course->id, 'groupid' =>$this->groupid, 'itemids' =>implode(',', $itemids), 'export_letters' =>$this->export_letters, 'export_feedback' =>$this->export_feedback, 'updatedgradesonly' =>$this->updatedgradesonly, 'displaytype' =>$this->displaytype, 'decimalpoints' =>$this->decimalpoints); return $params; } /** * Either prints a "Export" box, which will redirect the user to the download page, * or prints the URL for the published data. * @return void */ function print_continue() { global $CFG; $params = $this->get_export_params(); print_heading(get_string('export', 'grades')); echo ''; } } /** * This class is used to update the exported field in grade_grades. * It does internal buffering to speedup the db operations. */ class grade_export_update_buffer { var $update_list; var $export_time; /** * Constructor - creates the buffer and initialises the time stamp */ function grade_export_update_buffer() { $this->update_list = array(); $this->export_time = time(); } function flush($buffersize) { global $CFG; if (count($this->update_list) > $buffersize) { $list = implode(',', $this->update_list); $sql = "UPDATE {$CFG->prefix}grade_grades SET exported = {$this->export_time} WHERE id IN ($list)"; execute_sql($sql, false); $this->update_list = array(); } } /** * Track grade export status * @param object $grade_grade * @return string $status (unknow, new, regrade, nochange) */ function track($grade_grade) { if (empty($grade_grade->exported) or empty($grade_grade->timemodified)) { if (is_null($grade_grade->finalgrade)) { // grade does not exist yet $status = 'unknown'; } else { $status = 'new'; $this->update_list[] = $grade_grade->id; } } else if ($grade_grade->exported < $grade_grade->timemodified) { $status = 'regrade'; $this->update_list[] = $grade_grade->id; } else if ($grade_grade->exported >= $grade_grade->timemodified) { $status = 'nochange'; } else { // something is wrong? $status = 'unknown'; } $this->flush(100); return $status; } /** * Flush and close the buffer. */ function close() { $this->flush(0); } } ?>