]> git.mjollnir.org Git - moodle.git/commitdiff
Adding gradebook base class and export plugins. Refactored from old gradebook. Untest...
authortoyomoyo <toyomoyo>
Tue, 8 May 2007 08:59:41 +0000 (08:59 +0000)
committertoyomoyo <toyomoyo>
Tue, 8 May 2007 08:59:41 +0000 (08:59 +0000)
grade/export/lib.php [new file with mode: 0755]
grade/export/ods/grade_export_ods.php [new file with mode: 0755]
grade/export/txt/grade_export_txt.php [new file with mode: 0755]
grade/export/xls/grade_export_xls.php [new file with mode: 0755]

diff --git a/grade/export/lib.php b/grade/export/lib.php
new file mode 100755 (executable)
index 0000000..3ca3a41
--- /dev/null
@@ -0,0 +1,141 @@
+<?php
+
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.com                                            //
+//                                                                       //
+// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+class grade_export {
+    
+    var $format = ''; // export format
+    var $id; // course id
+    var $grades = array();    // Collect all grades in this array
+    var $gradeshtml= array(); // Collect all grades html formatted in this array
+    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 $course; // course
+    
+    // common strings
+    var $strgrades; 
+    var $strgrade;    
+    
+    /**
+     * Constructor should set up all the private variables ready to be pulled
+     */
+    function grade_export($id) {
+        
+        $this->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 (executable)
index 0000000..cb21fd0
--- /dev/null
@@ -0,0 +1,96 @@
+<?php
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.com                                            //
+//                                                                       //
+// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+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() { 
+        
+        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 (executable)
index 0000000..664571e
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.com                                            //
+//                                                                       //
+// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+require_once($CFG->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 (executable)
index 0000000..412ff4c
--- /dev/null
@@ -0,0 +1,97 @@
+<?php
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.com                                            //
+//                                                                       //
+// Copyright (C) 2001-2003  Martin Dougiamas  http://dougiamas.com       //
+//                                                                       //
+// This program is free software; you can redistribute it and/or modify  //
+// it under the terms of the GNU General Public License as published by  //
+// the Free Software Foundation; either version 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program is distributed in the hope that it will be useful,       //
+// but WITHOUT ANY WARRANTY; without even the implied warranty of        //
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
+// GNU General Public License for more details:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+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() { 
+        
+        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; 
+    }
+}
+
+?>