MDL-54061 mod_assign: Added encoding and separator to upload form.
authorAdrian Greeve <adrian@moodle.com>
Fri, 6 May 2016 09:02:38 +0000 (17:02 +0800)
committerAdrian Greeve <adrian@moodle.com>
Wed, 18 May 2016 07:50:33 +0000 (15:50 +0800)
Added the fields of encoding and separator so that the user has
more control when uploading a csv assignment grading file.

mod/assign/feedback/offline/importgradesform.php
mod/assign/feedback/offline/importgradeslib.php
mod/assign/feedback/offline/locallib.php
mod/assign/feedback/offline/uploadgradesform.php

index 5400fcf..a81b207 100644 (file)
@@ -181,6 +181,12 @@ class assignfeedback_offline_import_grades_form extends moodleform implements re
         $mform->setType('pluginaction', PARAM_ALPHA);
         $mform->addElement('hidden', 'importid', $gradeimporter->importid);
         $mform->setType('importid', PARAM_INT);
+
+        $mform->addElement('hidden', 'encoding', $gradeimporter->get_encoding());
+        $mform->setType('encoding', PARAM_ALPHAEXT);
+        $mform->addElement('hidden', 'separator', $gradeimporter->get_separator());
+        $mform->setType('separator', PARAM_ALPHA);
+
         $mform->addElement('hidden', 'ignoremodified', $ignoremodified);
         $mform->setType('ignoremodified', PARAM_BOOL);
         $mform->addElement('hidden', 'draftid', $draftid);
index 5140b4d..7f741f1 100644 (file)
@@ -57,15 +57,23 @@ class assignfeedback_offline_grade_importer {
     /** @var array $feedbackcolumnindexes A lookup of column indexes for feedback plugin text import columns */
     private $feedbackcolumnindexes = array();
 
+    /** @var string $encoding Encoding to use when reading the csv file. Defaults to utf-8. */
+    private $encoding;
+
+    /** @var string $separator How each bit of information is separated in the file. Defaults to comma separated. */
+    private $separator;
+
     /**
      * Constructor
      *
      * @param string $importid A unique id for this import
      * @param assign $assignment The current assignment
      */
-    public function __construct($importid, assign $assignment) {
+    public function __construct($importid, assign $assignment, $encoding = 'utf-8', $separator = 'comma') {
         $this->importid = $importid;
         $this->assignment = $assignment;
+        $this->encoding = $encoding;
+        $this->separator = $separator;
     }
 
     /**
@@ -77,7 +85,7 @@ class assignfeedback_offline_grade_importer {
      */
     public function parsecsv($csvdata) {
         $this->csvreader = new csv_import_reader($this->importid, 'assignfeedback_offline');
-        $this->csvreader->load_csv_content($csvdata, 'utf-8', 'comma');
+        $this->csvreader->load_csv_content($csvdata, $this->encoding, $this->separator);
     }
 
     /**
@@ -140,6 +148,24 @@ class assignfeedback_offline_grade_importer {
         return true;
     }
 
+    /**
+     * Return the encoding for this csv import.
+     *
+     * @return string The encoding for this csv import.
+     */
+    public function get_encoding() {
+        return $this->encoding;
+    }
+
+    /**
+     * Return the separator for this csv import.
+     *
+     * @return string The separator for this csv import.
+     */
+    public function get_separator() {
+        return $this->separator;
+    }
+
     /**
      * Get the next row of data from the csv file (only the columns we care about)
      *
index e5ab111..f41848b 100644 (file)
@@ -84,15 +84,17 @@ class assign_feedback_offline extends assign_feedback_plugin {
      * @param int $draftid - The unique draft item id for this import
      * @param int $importid - The unique import ID for this csv import operation
      * @param bool $ignoremodified - Ignore the last modified date when checking fields
+     * @param string $encoding - Encoding of the file being processed.
+     * @param string $separator - The character used to separate the information.
      * @return string - The html response
      */
-    public function process_import_grades($draftid, $importid, $ignoremodified) {
+    public function process_import_grades($draftid, $importid, $ignoremodified, $encoding = 'utf-8', $separator = 'comma') {
         global $USER, $DB;
 
         require_sesskey();
         require_capability('mod/assign:grade', $this->assignment->get_context());
 
-        $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
+        $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment, $encoding, $separator);
 
         $context = context_user::instance($USER->id);
         $fs = get_file_storage();
@@ -114,7 +116,7 @@ class assign_feedback_offline extends assign_feedback_plugin {
                                                                      'pluginsubtype'=>'assignfeedback',
                                                                      'plugin'=>'offline',
                                                                      'pluginaction'=>'uploadgrades',
-                                                                     'id'=>$assignment->get_course_module()->id));
+                                                                     'id' => $this->assignment->get_course_module()->id));
             print_error('invalidgradeimport', 'assignfeedback_offline', $thisurl);
             return;
         }
@@ -261,7 +263,8 @@ class assign_feedback_offline extends assign_feedback_plugin {
                    ($csvdata = $mform->get_file_content('gradesfile'))) {
 
             $importid = csv_import_reader::get_new_iid('assignfeedback_offline');
-            $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
+            $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment,
+                    $data->encoding, $data->separator);
             // File exists and was valid.
             $ignoremodified = !empty($data->ignoremodified);
 
@@ -283,11 +286,12 @@ class assign_feedback_offline extends assign_feedback_plugin {
             $o .= $renderer->render(new assign_form('confirmimport', $mform));
             $o .= $renderer->render_footer();
         } else if ($confirm) {
-
             $importid = optional_param('importid', 0, PARAM_INT);
             $draftid = optional_param('draftid', 0, PARAM_INT);
+            $encoding = optional_param('encoding', 'utf-8', PARAM_ALPHAEXT);
+            $separator = optional_param('separator', 'comma', PARAM_ALPHA);
             $ignoremodified = optional_param('ignoremodified', 0, PARAM_BOOL);
-            $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment);
+            $gradeimporter = new assignfeedback_offline_grade_importer($importid, $this->assignment, $encoding, $separator);
             $mform = new assignfeedback_offline_import_grades_form(null, array('assignment'=>$this->assignment,
                                                                        'csvdata'=>'',
                                                                        'ignoremodified'=>$ignoremodified,
@@ -300,7 +304,7 @@ class assign_feedback_offline extends assign_feedback_plugin {
                 return;
             }
 
-            $o .= $this->process_import_grades($draftid, $importid, $ignoremodified);
+            $o .= $this->process_import_grades($draftid, $importid, $ignoremodified, $encoding, $separator);
         } else {
 
             $o .= $renderer->render(new assign_header($this->assignment->get_instance(),
index 75355cf..857d9fc 100644 (file)
@@ -55,6 +55,19 @@ class assignfeedback_offline_upload_grades_form extends moodleform {
         $mform->addRule('gradesfile', get_string('uploadnofilefound'), 'required', null, 'client');
         $mform->addHelpButton('gradesfile', 'gradesfile', 'assignfeedback_offline');
 
+        $encodings = core_text::get_encodings();
+        $mform->addElement('select', 'encoding', get_string('encoding', 'grades'), $encodings);
+        $mform->addHelpButton('encoding', 'encoding', 'grades');
+
+        $radio = array();
+        $radio[] = $mform->createElement('radio', 'separator', null, get_string('septab', 'grades'), 'tab');
+        $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcomma', 'grades'), 'comma');
+        $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepcolon', 'grades'), 'colon');
+        $radio[] = $mform->createElement('radio', 'separator', null, get_string('sepsemicolon', 'grades'), 'semicolon');
+        $mform->addGroup($radio, 'separator', get_string('separator', 'grades'), ' ', false);
+        $mform->addHelpButton('separator', 'separator', 'grades');
+        $mform->setDefault('separator', 'comma');
+
         $mform->addElement('checkbox', 'ignoremodified', '', get_string('ignoremodified', 'assignfeedback_offline'));
         $mform->addHelpButton('ignoremodified', 'ignoremodified', 'assignfeedback_offline');