MDL-11069 grade export refactoring + removing internally calculated totals (we have already various totals in categories and calculations can be used too for that)

This commit is contained in:
skodak 2007-08-30 15:03:51 +00:00
parent 8a275c7242
commit 11745964a5
5 changed files with 92 additions and 139 deletions

View File

@ -32,19 +32,16 @@ require_once($CFG->dirroot.'/grade/export/grade_export_form.php');
*/
class grade_export {
var $format = ''; // export format
var $id; // course id
var $itemids; // array of grade_item ids;
var $grade_items; // array of grade_items
var $grades = array(); // Collect all grades in this array
var $gradeshtml= array(); // Collect all grades html formatted in this array
var $comments = array(); // Collect all comments for each grade
var $totals = array(); // Collect all totals in this array
var $columns = array(); // Accumulate column names in this array.
var $columnhtml = array(); // Accumulate column html in this array.
var $columnidnumbers = array(); // Collect all gradeitem id numbers
var $students = array();
var $course; // course
var $publish; // Whether to publish this data via URL, or dump it to browser as usual
var $export_letters;
// common strings
var $strgrades;
@ -52,44 +49,50 @@ class grade_export {
/**
* Constructor should set up all the private variables ready to be pulled
* @param int $id course id
* @param string $itemids array of item ids
* @param int $courseid course id
* @param array $itemids array of grade item ids, empty means all
* @param boolean $export_letters Whether to export letter grade_items as literal letters, or as numerical values
* @param boolean $publish published using private user key
* @note Exporting as letters will lead to data loss if that exported set it re-imported.
*/
function grade_export($id, $itemids = null, $export_letters=false, $publish=false) {
function grade_export($courseid, $itemids=null, $export_letters=false, $publish=false) {
global $CFG;
$this->publish = $publish;
$this->export_letters = $export_letters;
$this->strgrades = get_string("grades");
$this->strgrade = get_string("grade");
if (is_array($itemids)) {
$this->itemids = $itemids;
} else {
$this->itemids = array();
}
$strmax = get_string("maximumshort");
if (! $course = get_record("course", "id", $id)) {
if (!$course = get_record("course", "id", $courseid)) {
error("Course ID was incorrect");
}
$context = get_context_instance(CONTEXT_COURSE, $id);
require_capability('moodle/grade:view', $context);
$context = get_context_instance(CONTEXT_COURSE, $course->id);
require_capability('moodle/grade:export', $context);
$this->id = $id;
$this->id = $course->id;
$this->course = $course;
if ($export_letters) {
require_once($CFG->dirroot . '/grade/report/lib.php');
$report = new grade_report($this->id, null, null);
$letters = $report->get_grade_letters();
// fetch all grade items
if (empty($itemids)) {
$this->grade_items = grade_item::fetch_all(array('courseid'=>$this->id));
} else {
$this->grade_items = array();
foreach ($itemids as $iid) {
if ($grade_item = grade_item::fetch(array('id'=>(int)$iid, 'courseid'=>$this->id))) {
$this->grade_items[$grade_item->id] = $grade_item;
}
}
}
// first make sure we have all final grades
// TODO: check that no grade_item has needsupdate set
grade_regrade_final_grades($id);
// init colums
foreach ($this->grade_items as $grade_item) {
if ($grade_item->itemtype == 'mod') {
$this->columns[$grade_item->id] = get_string('modulename', $grade_item->itemmodule).': '.$grade_item->get_name();
} else {
$this->columns[$grade_item->id] = $grade_item->get_name();
}
$this->columnidnumbers[$grade_item->id] = $grade_item->idnumber; // this might be needed for some export plugins
}
/// Check to see if groups are being used in this course
if ($groupmode = groupmode($course)) { // Groups are being used
@ -115,85 +118,50 @@ class grade_export {
if (!empty($this->students)) {
foreach ($this->students as $student) {
$this->grades[$student->id] = array(); // Collect all grades in this array
$this->gradeshtml[$student->id] = array(); // Collect all grades html formatted in this array
$this->totals[$student->id] = array(); // Collect all totals in this array
$this->comments[$student->id] = array(); // Collect all comments in tihs array
}
}
}
// if grade_item ids are specified
if (empty($this->itemids)) {
$gradeitems = array();
foreach ($this->itemids as $iid) {
$gradeitems[] = grade_item::fetch(array('id'=>(int)$iid, 'courseid'=>$this->id));
}
function load_grades() {
// first make sure we have all final grades
// TODO: check that no grade_item has needsupdate set
grade_regrade_final_grades($this->id);
if ($this->export_letters) {
require_once($CFG->dirroot . '/grade/report/lib.php');
$report = new grade_report($this->id, null, null);
$letters = $report->get_grade_letters();
} else {
// else we get all items for this course
$gradeitems = grade_item::fetch_all(array('courseid'=>$this->id));
$letters = null;
}
if ($gradeitems) {
foreach ($gradeitems as $gradeitem) {
if ($this->grade_items) {
foreach ($this->grade_items as $gradeitem) {
// load as an array of grade_final objects
if ($itemgrades = $gradeitem->get_final()) {
$this->columns[$gradeitem->id] = "$gradeitem->itemmodule: ".$gradeitem->get_name()." - $gradeitem->grademax";
$this->columnidnumbers[$gradeitem->id] = $gradeitem->idnumber; // this might be needed for some export plugins
if (!empty($gradeitem->grademax)) {
$maxgrade = "$strmax: $gradeitem->grademax";
} else {
$maxgrade = "";
}
if (!empty($this->students)) {
foreach ($this->students as $student) {
unset($studentgrade);
// TODO add support for comment here MDL-9634
if (!empty($itemgrades[$student->id])) {
$studentgrade = $itemgrades[$student->id];
}
if ($export_letters) {
$grade_item_displaytype = $report->get_pref('gradedisplaytype', $gradeitem->id);
// TODO Convert final grade to letter if export option is on, and grade_item is set to letter type MDL-10490
if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER && !empty($studentgrade)) {
$studentgrade->finalgrade = grade_grade::get_letter($letters, $studentgrade->finalgrade,
$gradeitem->grademin, $gradeitem->grademax);
}
}
if (!empty($studentgrade->finalgrade)) {
$this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->finalgrade;
} else {
$this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
$this->gradeshtml[$student->id][$gradeitem->id] = "";
}
if (!empty($maxgrade)) {
$total = (float)($this->totals[$student->id]) + (float)($currentstudentgrade);
} else {
$total = (float)($this->totals[$student->id]) + 0;
}
if ($export_letters) {
$total = grade_grade::get_letter($letters, $total, $gradeitem->grademin, $gradeitem->grademax);
}
$this->totals[$student->id] = $total;
if (!empty($comment)) {
// load comments here
if ($studentgrade) {
$studentgrade->load_text();
// get the actual comment
$comment = $studentgrade->grade_grade_text->feedback;
$this->comments[$student->id][$gradeitem->id] = $comment;
}
} else {
$this->comments[$student->id][$gradeitem->id] = '';
if ($itemgrades = $gradeitem->get_final() and !empty($this->students)) {
foreach ($this->students as $student) {
$finalgrade = null;
$feedback = '';
if (array_key_exists($student->id, $itemgrades)) {
$finalgrade = $itemgrades[$student->id]->finalgrade;
$grade = new grade_grade($itemgrades[$student->id], false);
if ($grade_text = $grade->load_text()) {
$feedback = format_text($grade_text->feedback, $grade_text->feedbackformat);
}
}
if ($this->export_letters) {
$grade_item_displaytype = $report->get_pref('gradedisplaytype', $gradeitem->id);
// TODO Convert final grade to letter if export option is on, and grade_item is set to letter type MDL-10490
if ($grade_item_displaytype == GRADE_REPORT_GRADE_DISPLAY_TYPE_LETTER) {
$finalgrade = grade_grade::get_letter($letters, $finalgrade,
$gradeitem->grademin, $gradeitem->grademax);
}
}
$this->grades[$student->id][$gradeitem->id] = $finalgrade;
$this->comments[$student->id][$gradeitem->id] = $feedback;
}
}
}
@ -211,6 +179,9 @@ class grade_export {
* TODO finish PHPdoc
*/
function display_grades($feedback=false, $rows=10) {
$this->load_grades();
echo '<table>';
echo '<tr>';
echo '<th>'.get_string("firstname")."</th>".
@ -228,7 +199,6 @@ class grade_export {
echo "<th>{$column}_feedback</th>";
}
}
echo '<th>'.get_string("total")."</th>";
echo '</tr>';
/// Print all the lines of data.
@ -241,21 +211,16 @@ class grade_export {
}
echo '<tr>';
$student = $this->students[$studentid];
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
echo "<td>$student->firstname</td><td>$student->lastname</td><td>$student->idnumber</td><td>$student->institution</td><td>$student->department</td><td>$student->email</td>";
foreach ($studentgrades as $grade) {
foreach ($studentgrades as $itemid=>$grade) {
$grade = strip_tags($grade);
echo "<td>$grade</td>";
if ($feedback) {
echo '<td>'.array_shift($this->comments[$student->id]).'</td>';
echo '<td>'.$this->comments[$studentid][$itemid].'</td>';
}
}
echo '<td>'.$this->totals[$student->id].'</td>';
echo "</tr>";
}
echo '</table>';

View File

@ -26,23 +26,22 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_ods extends grade_export {
var $format = 'ods'; // export format
/**
* To be implemented by child classes
*/
function print_grades($feedback = false) {
global $CFG;
$this->load_grades();
require_once($CFG->dirroot.'/lib/odslib.class.php');
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
if (in_array('ods', $expplugins)) {
$export = true;
} else {
$export = false;
$export = false;
}
} else {
$export = false;
@ -73,7 +72,6 @@ class grade_export_ods extends grade_export {
$myxls->write_string(0,$pos++,strip_tags($column."_feedback"));
}
}
$myxls->write_string(0,$pos,get_string("total"));
/// Print all the lines of data.
$i = 0;
@ -94,7 +92,7 @@ class grade_export_ods extends grade_export {
$j=6;
foreach ($studentgrades as $gradeitemid => $grade) {
if (is_numeric($grade)) {
$myxls->write_number($i,$j++,strip_tags($grade));
$myxls->write_number($i,$j++,$grade);
}
else {
$myxls->write_string($i,$j++,strip_tags($grade));
@ -102,7 +100,7 @@ class grade_export_ods extends grade_export {
// writing comment if requested
if ($feedback) {
$myxls->write_string($i,$j++,array_shift($this->comments[$student->id]));
$myxls->write_string($i,$j++,$this->comments[$student->id][$gradeitemid]);
}
/// if export flag needs to be set
@ -119,7 +117,6 @@ class grade_export_ods extends grade_export {
$grade_grade->update();
}
}
$myxls->write_number($i,$j,$this->totals[$student->id]);
}
}

View File

@ -26,7 +26,6 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_txt extends grade_export {
var $format = 'txt'; // export format
var $separator = "\t"; // default separator
function set_separator($separator) {
@ -41,17 +40,18 @@ class grade_export_txt extends grade_export {
* To be implemented by child classes
*/
function print_grades($feedback = false) {
global $CFG;
$this->load_grades();
$retval = '';
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
if (in_array('txt', $expplugins)) {
$export = true;
} else {
$export = false;
$export = false;
}
} else {
$export = false;
@ -79,15 +79,12 @@ class grade_export_txt extends grade_export {
$retval .= "{$this->separator}{$column}_feedback";
}
}
$retval .= "{$this->separator}".get_string("total")."\n";
/// Print all the lines of data.
foreach ($this->grades as $studentid => $studentgrades) {
$student = $this->students[$studentid];
if (empty($this->totals[$student->id])) {
$this->totals[$student->id] = '';
}
$retval .= "$student->firstname{$this->separator}$student->lastname{$this->separator}$student->idnumber{$this->separator}$student->institution{$this->separator}$student->department{$this->separator}$student->email";
foreach ($studentgrades as $gradeitemid => $grade) {
@ -95,13 +92,14 @@ class grade_export_txt extends grade_export {
$retval .= "{$this->separator}$grade";
if ($feedback) {
$retval .= "{$this->separator}".array_shift($this->comments[$student->id]);
$retval .= "{$this->separator}".$this->comments[$student->id][$gradeitemid];
}
/// if export flag needs to be set
/// construct the grade_grade object and update timestamp if CFG flag is set
if ($export) {
//this should be improved with sql
$params = new object();
$params->itemid = $gradeitemid;
$params->userid = $studentid;
@ -112,7 +110,6 @@ class grade_export_txt extends grade_export {
$grade_grade->update();
}
}
$retval .= "{$this->separator}".$this->totals[$student->id];
$retval .= "\n";
}

View File

@ -26,21 +26,20 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_xls extends grade_export {
var $format = 'xls'; // export format
/**
* To be implemented by child classes
*/
function print_grades($feedback = false) {
global $CFG;
$this->load_grades();
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
if (in_array('xls', $expplugins)) {
$export = true;
} else {
$export = false;
$export = false;
}
} else {
$export = false;
@ -72,7 +71,6 @@ class grade_export_xls extends grade_export {
$myxls->write_string(0,$pos++,strip_tags($column."_feedback"));
}
}
$myxls->write_string(0,$pos,get_string("total"));
/// Print all the lines of data.
$i = 0;
@ -93,7 +91,7 @@ class grade_export_xls extends grade_export {
$j=6;
foreach ($studentgrades as $gradeitemid => $grade) {
if (is_numeric($grade)) {
$myxls->write_number($i,$j++,strip_tags($grade));
$myxls->write_number($i,$j++,$grade);
}
else {
$myxls->write_string($i,$j++,strip_tags($grade));
@ -101,7 +99,7 @@ class grade_export_xls extends grade_export {
// writing comment if requested
if ($feedback) {
$myxls->write_string($i,$j++,array_shift($this->comments[$student->id]));
$myxls->write_string($i,$j++,$this->comments[$student->id][$gradeitemid]);
}
/// if export flag needs to be set
@ -118,7 +116,6 @@ class grade_export_xls extends grade_export {
$grade_grade->update();
}
}
$myxls->write_number($i,$j,$this->totals[$student->id]);
}
}

View File

@ -26,8 +26,6 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
class grade_export_xml extends grade_export {
var $format = 'xml'; // export format
/**
* To be implemented by child classes
* @param boolean $feedback
@ -35,24 +33,23 @@ class grade_export_xml extends grade_export {
* @return string
*/
function print_grades($feedback = false) {
global $CFG;
$this->load_grades();
$retval = '';
/// Whether this plugin is entitled to update export time
if ($expplugins = explode(",", $CFG->gradeexport)) {
if (in_array($this->format, $expplugins)) {
if (in_array('xml', $expplugins)) {
$export = true;
} else {
$export = false;
$export = false;
}
} else {
$export = false;
}
require_once($CFG->dirroot.'/lib/excellib.class.php');
/// Calculate file name
$downloadfilename = clean_filename("{$this->course->shortname} $this->strgrades.xml");