From c10ddfb3c00dbb6e9a016a82ed7b0ce515102fdf Mon Sep 17 00:00:00 2001 From: toyomoyo Date: Tue, 8 May 2007 08:59:41 +0000 Subject: [PATCH] Adding gradebook base class and export plugins. Refactored from old gradebook. Untested code. --- grade/export/lib.php | 141 ++++++++++++++++++++++++++ grade/export/ods/grade_export_ods.php | 96 ++++++++++++++++++ grade/export/txt/grade_export_txt.php | 75 ++++++++++++++ grade/export/xls/grade_export_xls.php | 97 ++++++++++++++++++ 4 files changed, 409 insertions(+) create mode 100755 grade/export/lib.php create mode 100755 grade/export/ods/grade_export_ods.php create mode 100755 grade/export/txt/grade_export_txt.php create mode 100755 grade/export/xls/grade_export_xls.php diff --git a/grade/export/lib.php b/grade/export/lib.php new file mode 100755 index 0000000000..3ca3a415bc --- /dev/null +++ b/grade/export/lib.php @@ -0,0 +1,141 @@ +strgrades = get_string("grades"); + $this->strgrade = get_string("grade"); + + $strmax = get_string("maximumshort"); + + if (! $course = get_record("course", "id", $id)) { + error("Course ID was incorrect"); + } + + require_capability('moodle/course:viewcoursegrades', get_context_instance(CONTEXT_COURSE, $id)); + + $this->id = $id; + $this->course = $course; + + /// Check to see if groups are being used in this course + if ($groupmode = groupmode($course)) { // Groups are being used + + if (isset($_GET['group'])) { + $changegroup = $_GET['group']; /// 0 or higher + } else { + $changegroup = -1; /// This means no group change was specified + } + + $currentgroup = get_and_set_current_group($course, $groupmode, $changegroup); + + } else { + $currentgroup = false; + } + + if ($currentgroup) { + $students = get_group_students($currentgroup, "u.lastname ASC"); + } else { + $students = grade_get_course_students($course->id); + } + + if (!empty($students)) { + foreach ($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 + } + } + + if ($gradeitems = grade_get_items($this->id)) { + + foreach ($gradeitems as $gradeitem) { + + $this->columns[] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->maxgrade"; + + if (!empty($gradeitem->maxgrade)) { + $maxgrade = "$strmax: $gradeitem->maxgrade"; + } else { + $maxgrade = ""; + } + + // load as an array of grade_final objects + if ($itemgrades = $gradeitem -> load_final()) { + + if (!empty($students)) { + foreach ($students as $student) { + + // add support for comment here MDL-9634 + + if (!empty($itemgrades[$student->id]->gradevalue)) { + $this->grades[$student->id][] = $currentstudentgrade = $itemgrades[$student->id]->gradevalue; + } else { + $this->grades[$student->id][] = $currentstudentgrade = ""; + $this->gradeshtml[$student->id][] = ""; + } + if (!empty($modgrades->maxgrade)) { + $this->totals[$student->id] = (float)($totals[$student->id]) + (float)($currentstudentgrade); + } else { + $this->totals[$student->id] = (float)($totals[$student->id]) + 0; + } + } + } + /* we might be able to loop by gradeitems instead of students + foreach ($itemgrades as $itemgrade) { + $grades[$itemgrade->userid][] = $itemgrade->gradevalue; + $totals[$itemgrade->userid] = (float)($totals[$itemgrade->userid]) + + (float)($itemgrade->gradevalue); + } + */ + } + } + } + } + + /** + * To be implemented by child classes + */ + function print_grades() { } + +} + +?> diff --git a/grade/export/ods/grade_export_ods.php b/grade/export/ods/grade_export_ods.php new file mode 100755 index 0000000000..cb21fd0783 --- /dev/null +++ b/grade/export/ods/grade_export_ods.php @@ -0,0 +1,96 @@ +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() { + + require_once($CFG->dirroot.'/lib/odslib.class.php'); + + /// Calculate file name + $downloadfilename = clean_filename("$course->shortname $this->strgrades.ods"); + /// Creating a workbook + $workbook = new MoodleODSWorkbook("-"); + /// Sending HTTP headers + $workbook->send($downloadfilename); + /// Adding the worksheet + $myxls =& $workbook->add_worksheet($this->strgrades); + + /// Print names of all the fields + $myxls->write_string(0,0,get_string("firstname")); + $myxls->write_string(0,1,get_string("lastname")); + $myxls->write_string(0,2,get_string("idnumber")); + $myxls->write_string(0,3,get_string("institution")); + $myxls->write_string(0,4,get_string("department")); + $myxls->write_string(0,5,get_string("email")); + $pos=6; + foreach ($this->columns as $column) { + $myxls->write_string(0,$pos++,strip_tags($column)); + } + $myxls->write_string(0,$pos,get_string("total")); + + /// Print all the lines of data. + $i = 0; + if (!empty($this->grades)) { + foreach ($this->grades as $studentid => $studentgrades) { + $i++; + $student = $students[$studentid]; + if (empty($this->totals[$student->id])) { + $this->totals[$student->id] = ''; + } + + $myxls->write_string($i,0,$student->firstname); + $myxls->write_string($i,1,$student->lastname); + $myxls->write_string($i,2,$student->idnumber); + $myxls->write_string($i,3,$student->institution); + $myxls->write_string($i,4,$student->department); + $myxls->write_string($i,5,$student->email); + $j=6; + foreach ($studentgrades as $grade) { + if (is_numeric($grade)) { + $myxls->write_number($i,$j++,strip_tags($grade)); + } + else { + $myxls->write_string($i,$j++,strip_tags($grade)); + } + } + $myxls->write_number($i,$j,$this->totals[$student->id]); + } + } + + /// Close the workbook + $workbook->close(); + + exit; + } +} + +?> diff --git a/grade/export/txt/grade_export_txt.php b/grade/export/txt/grade_export_txt.php new file mode 100755 index 0000000000..664571e15b --- /dev/null +++ b/grade/export/txt/grade_export_txt.php @@ -0,0 +1,75 @@ +dirroot.'/grade/export/lib.php'); + +class grade_export_txt extends grade_export { + + var $format = 'txt'; // export format + + /** + * To be implemented by child classes + */ + function print_grades() { + +/// Print header to force download + + header("Content-Type: application/download\n"); + $downloadfilename = clean_filename("$this->course->shortname $this->strgrades"); + header("Content-Disposition: attachment; filename=\"$downloadfilename.txt\""); + +/// Print names of all the fields + + echo get_string("firstname")."\t". + get_string("lastname")."\t". + get_string("idnumber")."\t". + get_string("institution")."\t". + get_string("department")."\t". + get_string("email"); + foreach ($this->columns as $column) { + $column = strip_tags($column); + echo "\t$column"; + } + echo "\t".get_string("total")."\n"; + +/// Print all the lines of data. + foreach ($this->grades as $studentid => $studentgrades) { + $student = $students[$studentid]; + if (empty($this->totals[$student->id])) { + $this->totals[$student->id] = ''; + } + echo "$student->firstname\t$student->lastname\t$student->idnumber\t$student->institution\t$student->department\t$student->email"; + foreach ($studentgrades as $grade) { + $grade = strip_tags($grade); + echo "\t$grade"; + } + echo "\t".$this->totals[$student->id]; + echo "\n"; + } + + exit; + } +} + +?> diff --git a/grade/export/xls/grade_export_xls.php b/grade/export/xls/grade_export_xls.php new file mode 100755 index 0000000000..412ff4c7e1 --- /dev/null +++ b/grade/export/xls/grade_export_xls.php @@ -0,0 +1,97 @@ +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() { + + global $CFG; + + require_once($CFG->dirroot.'/lib/excellib.class.php'); + + /// Calculate file name + $downloadfilename = clean_filename("$course->shortname $this->strgrades.xls"); + /// Creating a workbook + $workbook = new MoodleExcelWorkbook("-"); + /// Sending HTTP headers + $workbook->send($downloadfilename); + /// Adding the worksheet + $myxls =& $workbook->add_worksheet($this->strgrades); + + /// Print names of all the fields + $myxls->write_string(0,0,get_string("firstname")); + $myxls->write_string(0,1,get_string("lastname")); + $myxls->write_string(0,2,get_string("idnumber")); + $myxls->write_string(0,3,get_string("institution")); + $myxls->write_string(0,4,get_string("department")); + $myxls->write_string(0,5,get_string("email")); + $pos=6; + foreach ($this->columns as $column) { + $myxls->write_string(0,$pos++,strip_tags($column)); + } + $myxls->write_string(0,$pos,get_string("total")); + + /// Print all the lines of data. + $i = 0; + if (!empty($this->grades)) { + foreach ($this->grades as $studentid => $studentgrades) { + $i++; + $student = $students[$studentid]; + if (empty($this->totals[$student->id])) { + $this->totals[$student->id] = ''; + } + + $myxls->write_string($i,0,$student->firstname); + $myxls->write_string($i,1,$student->lastname); + $myxls->write_string($i,2,$student->idnumber); + $myxls->write_string($i,3,$student->institution); + $myxls->write_string($i,4,$student->department); + $myxls->write_string($i,5,$student->email); + $j=6; + foreach ($studentgrades as $grade) { + if (is_numeric($grade)) { + $myxls->write_number($i,$j++,strip_tags($grade)); + } + else { + $myxls->write_string($i,$j++,strip_tags($grade)); + } + } + $myxls->write_number($i,$j,$this->totals[$student->id]); + } + } + + /// Close the workbook + $workbook->close(); + exit; + } +} + +?> -- 2.39.5