Merge branch 'MDL-46599-master' of git://github.com/lameze/moodle
authorDan Poltawski <dan@moodle.com>
Thu, 16 Oct 2014 16:30:27 +0000 (17:30 +0100)
committerDan Poltawski <dan@moodle.com>
Thu, 16 Oct 2014 16:30:27 +0000 (17:30 +0100)
14 files changed:
grade/export/ods/grade_export_ods.php
grade/export/ods/lang/en/gradeexport_ods.php
grade/export/txt/grade_export_txt.php
grade/export/txt/lang/en/gradeexport_txt.php
grade/export/xls/grade_export_xls.php
grade/export/xls/lang/en/gradeexport_xls.php
grade/import/csv/classes/load_data.php
grade/import/csv/index.php
grade/import/csv/tests/load_data_test.php
grade/import/direct/classes/import_form.php
grade/import/direct/classes/mapping_form.php
grade/import/direct/index.php
grade/import/grade_import_form.php
lang/en/grades.php

index 468496c..5454456 100644 (file)
@@ -76,6 +76,8 @@ class grade_export_ods extends grade_export {
                 $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true));
             }
         }
+        // Last downloaded column header.
+        $myxls->write_string(0, $pos++, get_string('timeexported', 'gradeexport_ods'));
 
         // Print all the lines of data.
         $i = 0;
@@ -117,6 +119,8 @@ class grade_export_ods extends grade_export {
                     $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid]));
                 }
             }
+            // Time exported.
+            $myxls->write_string($i, $j++, time());
         }
         $gui->close();
         $geub->close();
index 8c4432c..dd65582 100644 (file)
@@ -26,3 +26,4 @@
 $string['pluginname'] = 'OpenDocument spreadsheet';
 $string['ods:publish'] = 'Publish ODS grade export';
 $string['ods:view'] = 'Use OpenDocument grade export';
+$string['timeexported'] = 'Last downloaded from this course';
\ No newline at end of file
index 109bce6..7ec6aa4 100644 (file)
@@ -76,6 +76,8 @@ class grade_export_txt extends grade_export {
                 $exporttitle[] = $this->format_column_name($grade_item, true);
             }
         }
+        // Last downloaded column header.
+        $exporttitle[] = get_string('timeexported', 'gradeexport_txt');
         $csvexport->add_data($exporttitle);
 
         // Print all the lines of data.
@@ -110,6 +112,8 @@ class grade_export_txt extends grade_export {
                     $exportdata[] = $this->format_feedback($userdata->feedbacks[$itemid]);
                 }
             }
+            // Time exported.
+            $exportdata[] = time();
             $csvexport->add_data($exportdata);
         }
         $gui->close();
index d2d0ec0..db10755 100644 (file)
@@ -24,5 +24,6 @@
  */
 
 $string['pluginname'] = 'Plain text file';
+$string['timeexported'] = 'Last downloaded from this course';
 $string['txt:publish'] = 'Publish TXT grade export';
 $string['txt:view'] = 'Use text grade export';
index 384d768..83e720d 100644 (file)
@@ -73,6 +73,8 @@ class grade_export_xls extends grade_export {
                 $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true));
             }
         }
+        // Last downloaded column header.
+        $myxls->write_string(0, $pos++, get_string('timeexported', 'gradeexport_xls'));
 
         // Print all the lines of data.
         $i = 0;
@@ -111,6 +113,8 @@ class grade_export_xls extends grade_export {
                     $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid]));
                 }
             }
+            // Time exported.
+            $myxls->write_string($i, $j++, time());
         }
         $gui->close();
         $geub->close();
index f2bd760..32b96c5 100644 (file)
@@ -24,5 +24,6 @@
  */
 
 $string['pluginname'] = 'Excel spreadsheet';
+$string['timeexported'] = 'Last downloaded from this course';
 $string['xls:publish'] = 'Publish XLS grade export';
 $string['xls:view'] = 'Use Excel grade export';
index 21477b9..801dec3 100644 (file)
@@ -271,7 +271,7 @@ class gradeimport_csv_load_data {
      * @param array $map Mapping information provided by the user.
      * @param int $key The line that we are currently working on.
      * @param bool $verbosescales Form setting for grading with scales.
-     * @param string $value The grade value .
+     * @param string $value The grade value.
      * @return array grades to be updated.
      */
     protected function update_grade_item($courseid, $map, $key, $verbosescales, $value) {
@@ -429,16 +429,20 @@ class gradeimport_csv_load_data {
         $this->headers = $header;
         $this->studentid = null;
         $this->gradebookerrors = null;
+        $forceimport = $formdata->forceimport;
         // Temporary array to keep track of what new headers are processed.
         $this->newgradeitems = array();
         $this->trim_headers();
-
+        $timeexportkey = null;
         $map = array();
         // Loops mapping_0, mapping_1 .. mapping_n and construct $map array.
         foreach ($header as $i => $head) {
             if (isset($formdata->{'mapping_'.$i})) {
                 $map[$i] = $formdata->{'mapping_'.$i};
             }
+            if ($head == get_string('timeexported', 'gradeexport_txt')) {
+                $timeexportkey = $i;
+            }
         }
 
         // If mapping information is supplied.
@@ -531,6 +535,24 @@ class gradeimport_csv_load_data {
                             $this->cleanup_import(get_string('gradelocked', 'grades'));
                             return $this->status;
                         }
+                        // Check if the force import option is disabled and the last exported date column is present.
+                        if (!$forceimport && !empty($timeexportkey)) {
+                            $exportedtime = $line[$timeexportkey];
+                            if (clean_param($exportedtime, PARAM_INT) != $exportedtime || $exportedtime > time() ||
+                                    $exportedtime < strtotime("-1 year", time())) {
+                                // The date is invalid, or in the future, or more than a year old.
+                                $this->cleanup_import(get_string('invalidgradeexporteddate', 'grades'));
+                                return $this->status;
+
+                            }
+                            $timemodified = $gradegrade->get_dategraded();
+                            if (!empty($timemodified) && ($exportedtime < $timemodified)) {
+                                // The item was graded after we exported it, we return here not to override it.
+                                $user = core_user::get_user($this->studentid);
+                                $this->cleanup_import(get_string('gradealreadyupdated', 'grades', fullname($user)));
+                                return $this->status;
+                            }
+                        }
                     }
                     $insertid = self::insert_grade_record($newgrade, $this->studentid);
                     // Check to see if the insert was successful.
index 21c7270..e2426f0 100644 (file)
@@ -27,6 +27,7 @@ $separator     = optional_param('separator', '', PARAM_ALPHA);
 $verbosescales = optional_param('verbosescales', 1, PARAM_BOOL);
 $iid           = optional_param('iid', null, PARAM_INT);
 $importcode    = optional_param('importcode', '', PARAM_FILE);
+$forceimport   = optional_param('forceimport', false, PARAM_BOOL);
 
 $url = new moodle_url('/grade/import/csv/index.php', array('id'=>$id));
 if ($separator !== '') {
@@ -100,6 +101,7 @@ $mappingformdata = array(
     'iid' => $iid,
     'id' => $id,
     'importcode' => $importcode,
+    'forceimport' => $forceimport,
     'verbosescales' => $verbosescales
 );
 // we create a form to handle mapping data from the file to the database.
index 1d57c7d..68a1521 100644 (file)
@@ -50,6 +50,11 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,,75.00';
 Anne,Able,,"Moodle HQ","Rock on!",student7@mail.com,56.00,56.00,78.00
 Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
 
+    /** @var string $csvtext CSV data to be imported with Last download from this course column. */
+    protected $csvtext = '"First name",Surname,"ID number",Institution,Department,"Email address","Assignment: Assignment for grape group", "Feedback: Assignment for grape group","Course total","Last downloaded from this course"
+Anne,Able,,"Moodle HQ","Rock on!",student7@mail.com,56.00,"We welcome feedback",56.00,{exportdate}
+Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,,75.00,{exportdate}';
+
     /** @var int $iid Import ID. */
     protected $iid;
 
@@ -419,10 +424,12 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
         $formdata->mapping_6 = 'new';
         $formdata->mapping_7 = 'feedback_2';
         $formdata->mapping_8 = 0;
+        $formdata->mapping_9 = 0;
         $formdata->map = 1;
         $formdata->id = 2;
         $formdata->iid = $this->iid;
         $formdata->importcode = $importcode;
+        $formdata->forceimport = false;
 
         // Blam go time.
         $testobject = new phpunit_gradeimport_csv_load_data();
@@ -431,4 +438,123 @@ Bobby,Bunce,,"Moodle HQ","Rock on!",student5@mail.com,75.00,75.00';
         // If everything inserted properly then this should be true.
         $this->assertTrue($dataloaded);
     }
+
+    /*
+     * Test importing csv data into the gradebook using "Last downloaded from this course" column and force import option.
+     */
+    public function test_force_import_option () {
+
+        // Need to add users into the system.
+        $user = new stdClass();
+        $user->firstname = 'Anne';
+        $user->lastname = 'Able';
+        $user->email = 'student7@mail.com';
+        $user->id_number = 1;
+        $user1 = $this->getDataGenerator()->create_user($user);
+        $user = new stdClass();
+        $user->firstname = 'Bobby';
+        $user->lastname = 'Bunce';
+        $user->email = 'student5@mail.com';
+        $user->id_number = 2;
+        $user2 = $this->getDataGenerator()->create_user($user);
+
+        // Create a new grade item.
+        $params = array(
+            'itemtype'  => 'manual',
+            'itemname'  => 'Grade item 1',
+            'gradetype' => GRADE_TYPE_VALUE,
+            'courseid'  => $this->courseid
+        );
+        $gradeitem = new grade_item($params, false);
+        $gradeitemid = $gradeitem->insert();
+
+        $importcode = 001;
+        $verbosescales = 0;
+
+        // Form data object.
+        $formdata = new stdClass();
+        $formdata->mapfrom = 5;
+        $formdata->mapto = 'useremail';
+        $formdata->mapping_0 = 0;
+        $formdata->mapping_1 = 0;
+        $formdata->mapping_2 = 0;
+        $formdata->mapping_3 = 0;
+        $formdata->mapping_4 = 0;
+        $formdata->mapping_5 = 0;
+        $formdata->mapping_6 = $gradeitemid;
+        $formdata->mapping_7 = 'feedback_2';
+        $formdata->mapping_8 = 0;
+        $formdata->mapping_9 = 0;
+        $formdata->map = 1;
+        $formdata->id = 2;
+        $formdata->iid = $this->iid;
+        $formdata->importcode = $importcode;
+        $formdata->forceimport = false;
+
+        // Add last download from this course column to csv content.
+        $exportdate = time();
+        $newcsvdata = str_replace('{exportdate}', $exportdate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertTrue($dataloaded);
+
+        // We must update the last modified date.
+        grade_import_commit($this->courseid, $importcode, false, false);
+
+        // Test using force import disabled and a date in the past.
+        $pastdate = strtotime('-1 day', time());
+        $newcsvdata = str_replace('{exportdate}', $pastdate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('gradealreadyupdated', 'grades', fullname($user1)));
+
+        // Test using force import enabled and a date in the past.
+        $formdata->forceimport = true;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertTrue($dataloaded);
+
+        // Test importing using an old exported file (2 years ago).
+        $formdata->forceimport = false;
+        $twoyearsago = strtotime('-2 year', time());
+        $newcsvdata = str_replace('{exportdate}', $twoyearsago, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+
+        // Test importing using invalid exported date.
+        $baddate = '0123A56B89';
+        $newcsvdata = str_replace('{exportdate}', $baddate, $this->csvtext);
+        $this->csv_load($newcsvdata);
+        $formdata->mapping_6 = $gradeitemid;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+                $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+
+        // Test importing using date in the future.
+        $oneyearahead = strtotime('+1 year', time());
+        $oldcsv = str_replace('{exportdate}', $oneyearahead, $this->csvtext);
+        $this->csv_load($oldcsv);
+        $formdata->mapping_6 = $gradeitemid;
+        $testobject = new phpunit_gradeimport_csv_load_data();
+        $dataloaded = $testobject->prepare_import_grade_data($this->columns, $formdata, $this->csvimport,
+            $this->courseid, '', '', $verbosescales);
+        $this->assertFalse($dataloaded);
+        $errors = $testobject->get_gradebookerrors();
+        $this->assertEquals($errors[0], get_string('invalidgradeexporteddate', 'grades'));
+    }
 }
index bab48ca..f157b8c 100644 (file)
@@ -66,6 +66,9 @@ class gradeimport_direct_import_form extends moodleform {
         $mform->setType('previewrows', PARAM_INT);
         $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
         $mform->setType('groupid', PARAM_INT);
+        $mform->addElement('advcheckbox', 'forceimport', get_string('forceimport', 'grades'));
+        $mform->addHelpButton('forceimport', 'forceimport', 'grades');
+        $mform->setDefault('forceimport', false);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
     }
 }
index 5dc7c56..136b45c 100644 (file)
@@ -106,6 +106,9 @@ class gradeimport_direct_mapping_form extends moodleform {
         $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
         $mform->setType('groupid', PARAM_INT);
         $mform->setConstant('groupid', groups_get_course_group($COURSE));
+        $mform->addElement('hidden', 'forceimport', $this->_customdata['forceimport']);
+        $mform->setType('forceimport', PARAM_BOOL);
+        $mform->setConstant('forceimport', $this->_customdata['forceimport']);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
     }
 }
index d1b6a4b..b014e43 100644 (file)
@@ -24,6 +24,7 @@ $id            = required_param('id', PARAM_INT); // Course id.
 $verbosescales = optional_param('verbosescales', 1, PARAM_BOOL);
 $iid           = optional_param('iid', null, PARAM_INT);
 $importcode    = optional_param('importcode', '', PARAM_FILE);
+$forceimport   = optional_param('forceimport', false, PARAM_BOOL);
 
 $url = new moodle_url('/grade/import/direct/index.php', array('id' => $id));
 
@@ -96,6 +97,7 @@ $mappingformdata = array(
     'header' => $header,
     'iid' => $iid,
     'id' => $id,
+    'forceimport' => $forceimport,
     'importcode' => $importcode,
     'verbosescales' => $verbosescales
 );
index 1bc546e..4894a08 100644 (file)
@@ -70,6 +70,10 @@ class grade_import_form extends moodleform {
         $options = array('10'=>10, '20'=>20, '100'=>100, '1000'=>1000, '100000'=>100000);
         $mform->addElement('select', 'previewrows', get_string('rowpreviewnum', 'grades'), $options); // TODO: localize
         $mform->setType('previewrows', PARAM_INT);
+        $mform->addElement('checkbox', 'forceimport', get_string('forceimport', 'grades'));
+        $mform->addHelpButton('forceimport', 'forceimport', 'grades');
+        $mform->setDefault('forceimport', false);
+        $mform->setType('forceimport', PARAM_BOOL);
         $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
         $mform->setType('groupid', PARAM_INT);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
@@ -152,6 +156,9 @@ class grade_import_mapping_form extends moodleform {
         $mform->addElement('hidden', 'groupid', groups_get_course_group($COURSE));
         $mform->setType('groupid', PARAM_INT);
         $mform->setConstant('groupid', groups_get_course_group($COURSE));
+        $mform->addElement('hidden', 'forceimport', $this->_customdata['forceimport']);
+        $mform->setType('forceimport', PARAM_BOOL);
+        $mform->setConstant('forceimport', $this->_customdata['forceimport']);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
 
     }
index 5fb6426..b290601 100644 (file)
@@ -223,6 +223,10 @@ $string['feedbacksaved'] = 'Feedback saved';
 $string['feedbackview'] = 'View feedback';
 $string['finalgrade'] = 'Final grade';
 $string['finalgrade_help'] = 'If the overridden checkbox is ticked, a grade may be added or amended.';
+$string['fixedstudents'] = 'Static students column';
+$string['fixedstudents_help'] = 'Allows grades to scroll horizontally without losing sight of the students column, by making it static.';
+$string['forceimport'] = 'Force import';
+$string['forceimport_help'] = 'Force import of grades even if the grades were updated after the import file was exported';
 $string['forceoff'] = 'Force: Off';
 $string['forceon'] = 'Force: On';
 $string['forelementtypes'] = 'for the selected {$a}';
@@ -232,6 +236,7 @@ $string['fullmode'] = 'Full view';
 $string['generalsettings'] = 'General settings';
 $string['grade'] = 'Grade';
 $string['gradeadministration'] = 'Grade administration';
+$string['gradealreadyupdated'] = '{$a} grades have not been imported because the grades in the import file are older than in the grader report. To proceed with the grade import anyway, use the force import option.';
 $string['gradeanalysis'] = 'Grade analysis';
 $string['gradebook'] = 'Gradebook';
 $string['gradebookhiddenerror'] = 'The gradebook is currently set to hide everything from students.';
@@ -391,6 +396,7 @@ $string['incorrectcustomscale'] = '(Incorrect custom scale, please change.)';
 $string['incorrectminmax'] = 'The minimum must be lower than the maximum';
 $string['inherit'] = 'Inherit';
 $string['intersectioninfo'] = 'Student/Grade info';
+$string['invalidgradeexporteddate'] = 'The export date is invalid because it is more than a year ago, or in the future, or because the format is invalid.';
 $string['item'] = 'Item';
 $string['iteminfo'] = 'Item info';
 $string['iteminfo_help'] = 'This setting provides space for entering information about the item. The information is not displayed anywhere else.';