MDL-46599 gradebook: Add force import option on CSV file and direct importing
authorSimey Lameze <simey@moodle.com>
Wed, 8 Oct 2014 02:57:45 +0000 (10:57 +0800)
committerSimey Lameze <simey@moodle.com>
Thu, 16 Oct 2014 02:22:45 +0000 (10:22 +0800)
grade/import/csv/classes/load_data.php
grade/import/csv/index.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 3cbb069..080c77e 100644 (file)
@@ -267,7 +267,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) {
@@ -425,16 +425,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.
@@ -528,6 +532,14 @@ class gradeimport_csv_load_data {
                             return $this->status;
                         }
                     }
+
+                    // The grade was modified since the export.
+                    if ($forceimport === 0 && !empty($timeexportkey) && ($line[$timeexportkey] < $gradegrade->get_dategraded())) {
+                        $user = core_user::get_user($this->studentid);
+                        $this->cleanup_import(get_string('gradealreadyupdated', 'grades', fullname($user)));
+                        break;
+                    }
+
                     $insertid = self::insert_grade_record($newgrade, $this->studentid);
                     // Check to see if the insert was successful.
                     if (empty($insertid)) {
index 21c7270..0c2a5d2 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', 0, PARAM_BOOL);
 
 $url = new moodle_url('/grade/import/csv/index.php', array('id'=>$id));
 if ($separator !== '') {
@@ -67,6 +68,7 @@ if (!$iid) {
 
     // If the import form has been submitted.
     if ($formdata = $mform->get_data()) {
+
         $text = $mform->get_file_content('userfile');
         $csvimport = new gradeimport_csv_load_data();
         $csvimport->load_csv_content($text, $formdata->encoding, $separator, $formdata->previewrows);
@@ -100,6 +102,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 bab48ca..0a10b11 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', 0);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
     }
 }
index 5dc7c56..a097332 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_INT);
+        $mform->setConstant('forceimport', $this->_customdata['forceimport']);
         $this->add_action_buttons(false, get_string('uploadgrades', 'grades'));
     }
 }
index 1a9fc67..c620e6f 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', null, PARAM_INT);
 
 $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..7ce9c94 100644 (file)
@@ -70,6 +70,9 @@ 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', 0);
         $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 +155,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 118d366..fce2324 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.';