]> git.mjollnir.org Git - moodle.git/commitdiff
bug fixes for gradebook import/export
authortoyomoyo <toyomoyo>
Fri, 18 May 2007 08:49:00 +0000 (08:49 +0000)
committertoyomoyo <toyomoyo>
Fri, 18 May 2007 08:49:00 +0000 (08:49 +0000)
grade/export/lib.php
grade/export/ods/export.php
grade/export/ods/index.php
grade/export/txt/export.php
grade/export/txt/grade_export_txt.php
grade/export/txt/index.php
grade/export/xls/export.php
grade/export/xls/index.php
grade/export/xml/export.php
grade/export/xml/index.php
grade/import/csv/index.php

index 0474568219e43049d37ce1f5daf097e18ef8d3b5..bf7b2f66b7dde73daa1ba704e1b6facaa65345f4 100755 (executable)
@@ -29,7 +29,7 @@ include_once($CFG->dirroot.'/grade/lib.php');
  * Prints all grade items for selection
  * @input int id - course id
  */
-function print_gradeitem_selections($id) {
+function print_gradeitem_selections($id, $params = NULL) {
     
     // print all items for selections
     // make this a standard function in lib maybe
@@ -127,28 +127,29 @@ class grade_export {
                 $this->comments[$student->id] = array(); // Collect all comments in tihs array
             }
         }
-        
+
         // if grade_item ids are specified
-        if ($iids = explode(",", $itemids)) {
-            foreach ($iids as $iid) {
+        if ($itemids) {
+            foreach ($itemids as $iid) {
                 $params->id = $iid;
                 $gradeitems[] = new grade_item($params);               
             }  
         } else {
             // else we get all items for this course
             $gradeitems = grade_get_items($this->id);
-        }        
+        }
         
         if ($gradeitems) {
             
             foreach ($gradeitems as $gradeitem) {
-
+              
+                $gradeitem -> generate_final();
                 // load as an array of grade_final objects
-                if ($itemgrades = $gradeitem -> load_final()) {
+                if ($itemgrades = $gradeitem -> load_final()) {                    
                     
-                    $this->columns[$itemgrades->id] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->grademax";
+                    $this->columns[$gradeitem->id] = "$gradeitem->itemmodule: ".format_string($gradeitem->itemname,true)." - $gradeitem->grademax";
                 
-                    $this->columnidnumbers[$itemgrades->id] = $gradeitem->idnumber; // this might be needed for some export plugins  
+                    $this->columnidnumbers[$gradeitem->id] = $gradeitem->idnumber; // this might be needed for some export plugins  
             
                     if (!empty($gradeitem->grademax)) {
                         $maxgrade = "$strmax: $gradeitem->grademax";
@@ -163,10 +164,10 @@ class grade_export {
                             $studentgrade = $itemgrades[$student->id];            
                             
                             if (!empty($studentgrade->gradevalue)) {
-                                $this->grades[$student->id][$itemgrades->id] = $currentstudentgrade = $studentgrade->gradevalue;
+                                $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = $studentgrade->gradevalue;
                             } else {
-                                $this->grades[$student->id][$itemgrades->id] = $currentstudentgrade = "";
-                                $this->gradeshtml[$student->id][$itemgrades->id] = "";
+                                $this->grades[$student->id][$gradeitem->id] = $currentstudentgrade = "";
+                                $this->gradeshtml[$student->id][$gradeitem->id] = "";
                             }
                             if (!empty($maxgrade)) {
                                 $this->totals[$student->id] = (float)($this->totals[$student->id]) + (float)($currentstudentgrade);
@@ -180,9 +181,9 @@ class grade_export {
                             $comment = $studentgrade->grade_grades_text->feedback;
                             
                             if (!empty($comment)) {
-                                $this->comments[$student->id][$itemgrades->id] = $comment;
+                                $this->comments[$student->id][$gradeitem->id] = $comment;
                             } else {
-                                $this->comments[$student->id][$itemgrades->id] = '';  
+                                $this->comments[$student->id][$gradeitem->id] = '';  
                             }
                         }
                     }
index 44b573c3d3736ce37d223b6141224352ceac88a9..20fd2053b069cb25b21ede912d65e9a2a32e86aa 100755 (executable)
@@ -5,7 +5,7 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
 require_once('grade_export_ods.php');
  
 $id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
 $feedback = optional_param('feedback', '', PARAM_ALPHA); 
  
 // print all the exported data here
index da34e74d463f79a14d37613368c13af88781ab01..7cf75796116397f7f413f1d4b0aa57eb86fa30f8 100755 (executable)
@@ -29,7 +29,7 @@ $id = required_param('id', PARAM_INT); // course id
 $feedback = optional_param('feedback', '', PARAM_ALPHA);
 
 // process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
 
     if (!is_array($data->itemids)) {
         $itemidsurl = $data->itemids;
index acb349894f123838c0910b48081377d40f2b5ad4..635b19f826c408d87c89ec8da4915be37b2cb378 100755 (executable)
@@ -5,11 +5,12 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
 require_once('grade_export_txt.php');
  
 $id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
 $feedback = optional_param('feedback', '', PARAM_ALPHA); 
  
 // print all the exported data here
 $export = new grade_export_txt($id, $itemids);
+$export->set_separator(optional_param('separator'));
 $export->print_grades($feedback);
     
 ?>
\ No newline at end of file
index 22545b07e8ca79009d9f723253c8eade92c6f2d7..7655b40ffb3300373d051b0058fbc3a831a814cd 100755 (executable)
@@ -27,6 +27,15 @@ 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) {
+        if ($separator == 'comma') {
+            $this->separator = ",";
+        } else if ($separator == 'tab') {
+            $this->separator = "\t";  
+        }  
+    }
     
     /**
      * To be implemented by child classes
@@ -53,22 +62,22 @@ class grade_export_txt extends grade_export {
 
 /// 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".
+        echo get_string("firstname")."$this->separator".
+             get_string("lastname")."{$this->separator}".
+             get_string("idnumber")."{$this->separator}".
+             get_string("institution")."{$this->separator}".
+             get_string("department")."{$this->separator}".
              get_string("email");
         foreach ($this->columns as $column) {
             $column = strip_tags($column);
-            echo "\t$column";
+            echo "{$this->separator}$column";
         
             /// add a column_feedback column            
             if ($feedback) {
-                echo "\t{$column}_feedback";
+                echo "{$this->separator}{$column}_feedback";
             }        
         }
-        echo "\t".get_string("total")."\n";
+        echo "{$this->separator}".get_string("total")."\n";
     
 /// Print all the lines of data.
         foreach ($this->grades as $studentid => $studentgrades) {
@@ -77,14 +86,14 @@ class grade_export_txt extends grade_export {
             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";
+            echo "$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) {
                 $grade = strip_tags($grade);
-                echo "\t$grade";            
+                echo "{$this->separator}$grade";            
                 
                 if ($feedback) {
-                    echo "\t".array_shift($this->comments[$student->id]);
+                    echo "{$this->separator}".array_shift($this->comments[$student->id]);
                 }                
                 
                 /// if export flag needs to be set
@@ -101,7 +110,7 @@ class grade_export_txt extends grade_export {
                     $grade_grades_final->update();
                 }
             }
-            echo "\t".$this->totals[$student->id];
+            echo "{$this->separator}".$this->totals[$student->id];
             echo "\n";
         }
     
index ff6ebb2c0a15b8cc21f032950a96e752dd03a849..01bfd338d4ead75b16ba474b37a4b66e85e830b3 100755 (executable)
@@ -29,7 +29,7 @@ $id = required_param('id', PARAM_INT); // course id
 $feedback = optional_param('feedback', '', PARAM_ALPHA);
 
 // process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
 
     if (!is_array($data->itemids)) {
         $itemidsurl = $data->itemids;
@@ -43,9 +43,42 @@ if ($data = data_submitted() && confirm_sesskey()) {
     $export->display_grades($feedback);
     
     // this redirect should trigger a download prompt
-    redirect('export.php?id='.$id.'&amp;itemids='.$itemidsurl);
+    redirect('export.php?id='.$id.'&amp;itemids='.$itemidsurl.'&amp;separator='.$data->separator);
     exit; 
 }
 
-print_gradeitem_selections($id);
+
+$course = get_record('course', 'id', $id);
+$action = 'exporttxt';
+print_header($course->shortname.': '.get_string('grades'), $course->fullname, grade_nav($course, $action));
+
+// print_gradeitem_selections($id);
+// print all items for selections
+// make this a standard function in lib maybe
+if ($grade_items = grade_get_items($id)) {
+    echo '<form action="index.php" method="post">';
+    echo '<div>';
+    foreach ($grade_items as $grade_item) {
+            
+        echo '<br/><input type="checkbox" name="itemids[]" value="'.$grade_item->id.'" checked="checked"/>';
+            
+        if ($grade_item->itemtype == 'category') {
+            // grade categories should be displayed bold
+            echo '<b>'.$grade_item->itemname.'</b>';
+        } else {
+            echo $grade_item->itemname;
+        } 
+    }
+    echo '<br/>';
+    echo 'tab<input type="radio" name="separator" value="tab"/>';
+    echo 'comma<input type="radio" name="separator" value="comma"/>';
+    echo '<input type="hidden" name="id" value="'.$id.'"/>';
+    echo '<input type="hidden" name="sesskey" value="'.sesskey().'"/>';
+    echo '<br/>';
+    echo '<input type="submit" value="'.get_string('submit').'" />';
+    echo '</div>';
+    echo '</form>';
+}
+
+print_footer();
 ?>
\ No newline at end of file
index 8547d2f6761603f7aa27b5529ad09c9729f8f57e..d734cb28327295266a7d6cb5f10c046f663ced23 100755 (executable)
@@ -5,7 +5,7 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
 require_once('grade_export_xls.php');
  
 $id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
 $feedback = optional_param('feedback', '', PARAM_ALPHA); 
  
 // print all the exported data here
index 2f371fab5db4a40194122ac8da956a9e73f3fda4..0d57faae10dfc601cc88cbf872e7d3914cb23b41 100755 (executable)
@@ -29,7 +29,7 @@ $id = required_param('id', PARAM_INT); // course id
 $feedback = optional_param('feedback', '', PARAM_ALPHA);
 
 // process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
 
     if (!is_array($data->itemids)) {
         $itemidsurl = $data->itemids;
index 4db9b140c7a9ab2cfadbba208a4bab4b745a5737..d9b79f1884aaf225e898b6215136df992ffd7bd9 100755 (executable)
@@ -5,7 +5,7 @@ require_once($CFG->dirroot.'/grade/export/lib.php');
 require_once('grade_export_xml.php');
  
 $id = required_param('id', PARAM_INT); // course id
-$itemids = required_param('itemids', PARAM_NOTAGS);
+$itemids = explode(",", required_param('itemids', PARAM_NOTAGS));
 $feedback = optional_param('feedback', '', PARAM_ALPHA); 
  
 // print all the exported data here
index f9968d5eb02572a8b384e38740e6c6bb5868b1bd..28870ceb958d437edeee614170061e3e25f22fde 100755 (executable)
@@ -29,7 +29,7 @@ $id = required_param('id', PARAM_INT); // course id
 $feedback = optional_param('feedback', '', PARAM_ALPHA);
 
 // process post information
-if ($data = data_submitted() && confirm_sesskey()) {
+if (($data = data_submitted()) && confirm_sesskey()) {
 
     if (!is_array($data->itemids)) {
         $itemidsurl = $data->itemids;
index b899b047500709e37c98121157646fddad779a50..bd2debac043cc024e512040170223187eb6b2ddf 100755 (executable)
@@ -50,11 +50,12 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
 
     $map[$formdata->mapfrom] = $formdata->mapto;
 
-    $filename = $CFG->dataroot.'/temp/cvstemp';
-    
-    // Large files are likely to take their time and memory. Let PHP know
-    // that we'll take longer, and that the process should be recycled soon
-    // to free up memory.
+    // temporary file name supplied by form
+    $filename = $CFG->dataroot.'/temp/'.$formdata->filename;
+
+        // Large files are likely to take their time and memory. Let PHP know
+        // that we'll take longer, and that the process should be recycled soon
+        // to free up memory.
     @set_time_limit(0);
     @raise_memory_limit("192M");
     if (function_exists('apache_child_terminate')) {
@@ -62,67 +63,76 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
     }
     
     $text = my_file_get_contents($filename);    
-    $fp = fopen($filename, "r");
     
-    // --- get header (field names) ---
-    $header = split($csv_delimiter, fgets($fp,1024));
+    // we only operate if file is readable
+    if ($fp = fopen($filename, "r")) {
     
-    foreach ($header as $i => $h) {
-        $h = trim($h); $header[$i] = $h; // remove whitespace
-        // flag events to add columns if needed (?)
-    }
+        // --- get header (field names) ---
+        $header = split($csv_delimiter, fgets($fp,1024));
+    
+        foreach ($header as $i => $h) {
+            $h = trim($h); $header[$i] = $h; // remove whitespace
+            // flag events to add columns if needed (?)
+        }
     
-    while (!feof ($fp)) {
-        // add something
-        $line = split($csv_delimiter, fgets($fp,1024));            
+        while (!feof ($fp)) {
+            // add something
+            $line = split($csv_delimiter, fgets($fp,1024));            
         
-        // each line is a student record
-        unset ($studentid);
-        unset ($studentgrades);
-        print_object($map);
-        foreach ($line as $key => $value) {
+            // each line is a student record
+            unset ($studentid);
+            unset ($studentgrades);
+            print_object($map);
+            foreach ($line as $key => $value) {
                    
-            //decode encoded commas
-            $value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
-            switch ($map[$header[$key]]) {
-                case 'userid': // 
-                    $studentid = $value;
-                break;
-                case 'useridnumber':
-                    $user = get_record('user', 'idnumber', $value);
-                    $studentid = $user->id;
-                break;
-                case 'useremail':
-                    $user = get_record('user', 'email', $value);
-                    $studentid = $user->id;                
-                break;
-                case 'username':
-                    $user = get_record('user', 'username', $value);
-                    $studentid = $user->id;
-                break;
-                // might need to add columns for comments
-                default:
-                    if (!empty($map[$header[$key]])) {
-                        // case of an idnumber, only maps idnumber of a grade_item
-                        $studentgrades[$map[$header[$key]]] = $value;
-                    } // otherwise, we ignore this column altogether (e.g. institution, address etc)
-                break;  
+                //decode encoded commas
+                $value = preg_replace($csv_encode,$csv_delimiter2,trim($value));
+                switch ($map[$header[$key]]) {
+                    case 'userid': // 
+                        $studentid = $value;
+                    break;
+                    case 'useridnumber':
+                        $user = get_record('user', 'idnumber', $value);
+                        $studentid = $user->id;
+                    break;
+                    case 'useremail':
+                        $user = get_record('user', 'email', $value);
+                        $studentid = $user->id;                
+                    break;
+                    case 'username':
+                        $user = get_record('user', 'username', $value);
+                        $studentid = $user->id;
+                    break;
+                    // might need to add columns for comments
+                    default:
+                        if (!empty($map[$header[$key]])) {
+                            // case of an idnumber, only maps idnumber of a grade_item
+                            $studentgrades[$map[$header[$key]]] = $value;
+                        } // otherwise, we ignore this column altogether (e.g. institution, address etc)
+                    break;  
+                }
             }
-        }
-        if (!empty($studentgrades)) {
-            foreach ($studentgrades as $idnumber => $studentgrade) {
+            if (!empty($studentgrades)) {
+                foreach ($studentgrades as $idnumber => $studentgrade) {
                 
-                unset($eventdata);
-                $eventdata->idnumber = $idnumber;
-                $eventdata->userid = $studentid;
-                $eventdata->gradevalue = $studentgrade;
-                trigger_event('grade_added', $eventdata);               
+                    unset($eventdata);
+                    $eventdata->idnumber = $idnumber;
+                    $eventdata->userid = $studentid;
+                    $eventdata->gradevalue = $studentgrade;
+                    trigger_event('grade_added', $eventdata);               
                 
-                echo "<br/>triggering event for $idnumber... student id is $studentid and grade is $studentgrade";
+                    echo "<br/>triggering event for $idnumber... student id is $studentid and grade is $studentgrade";
             
+                }
             }
         }
+    
+        // temporary file can go now
+        unlink($filename);
+    } else {
+        error ('import file '.$filename.' not readable');  
     }
+
 } else if ( $formdata = $mform->get_data() ) {
 
     $filename = $mform->get_userfile_name();
@@ -164,12 +174,11 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
     $maptooptions = array('userid'=>'userid', 'username'=>'username', 'useridnumber'=>'useridnumber', 'useremail'=>'useremail', '0'=>'ignore');
     choose_from_menu($maptooptions, 'mapto');
     
-
-    
     $gradeitems = array();
     
     include_once($CFG->libdir.'/gradelib.php');
     if ($grade_items = grade_get_items($id)) {
+      
         foreach ($grade_items as $grade_item) {
             $gradeitems[$grade_item->idnumber] = $grade_item->itemname;      
         }
@@ -182,23 +191,17 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
         echo '<input type="hidden" name="maps[]" value="'.$h.'"/>';
         // this is what they map to
         
-        /**
-         * options are userid, 
-         * useridnumber, 
-         * useremail, 
-         * ignore (redundant column or 
-         * idnumber of grade_item (add individually);
-         */
-        
         $mapfromoptions = array_merge(array('0'=>'ignore'), $gradeitems);
 
         choose_from_menu($mapfromoptions, 'mapping[]', $h);
 
     }
-
+    $newfilename = 'cvstemp_'.time();
+    move_uploaded_file($filename, $CFG->dataroot.'/temp/'.$newfilename);
+    
     echo '<input type="hidden" name="map" value="1"/>';
     echo '<input type="hidden" name="id" value="'.$id.'"/>';    
-    echo '<input name="filename" value='.$filename.' type="hidden" />';
+    echo '<input name="filename" value='.$newfilename.' type="hidden" />';
     echo '<input type="submit" value="upload" />';
     echo '</form>';  
     
@@ -207,14 +210,12 @@ if (($formdata = data_submitted()) && !empty($formdata->map)) {
     //$mform2->display();
   
     // move file to $CFG->dataroot/temp
-    move_uploaded_file($filename, $CFG->dataroot.'/temp/cvstemp');
   
 } else {
     $mform->display();
 }
 print_footer();
 
-
 function my_file_get_contents($filename, $use_include_path = 0) {
     /// Returns the file as one big long string