MDL-44447 grade_export: Added multiple grade display types to grade export.
authorSimey Lameze <simey@moodle.com>
Fri, 22 Aug 2014 02:50:08 +0000 (10:50 +0800)
committerSimey Lameze <simey@moodle.com>
Mon, 15 Sep 2014 08:42:07 +0000 (16:42 +0800)
17 files changed:
grade/export/grade_export_form.php
grade/export/lib.php
grade/export/ods/export.php
grade/export/ods/grade_export_ods.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/txt/tests/behat/export.feature
grade/export/upgrade.txt
grade/export/xls/export.php
grade/export/xls/grade_export_xls.php
grade/export/xls/index.php
grade/export/xml/export.php
grade/export/xml/grade_export_xml.php
grade/export/xml/index.php
lang/en/grades.php

index 5b3cc55..5441dbb 100644 (file)
@@ -119,8 +119,24 @@ class grade_export_form extends moodleform {
             }
         }
         */
-        $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
-        $mform->setDefault('display', $CFG->grade_export_displaytype);
+        if ($features['multipledisplaytypes']) {
+            /*
+             * Using advcheckbox because we need the grade display type (name) as key and grade display type (constant) as value.
+             * The method format_column_name requires the lang file string and the format_grade method requires the constant.
+             */
+            $checkboxes = array();
+            $checkboxes[] = $mform->createElement('advcheckbox', 'display[real]', null, get_string('real', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_REAL));
+            $checkboxes[] = $mform->createElement('advcheckbox', 'display[percentage]', null, get_string('percentage', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_PERCENTAGE));
+            $checkboxes[] = $mform->createElement('advcheckbox', 'display[letter]', null, get_string('letter', 'grades'), null, array(0, GRADE_DISPLAY_TYPE_LETTER));
+            $mform->addGroup($checkboxes, 'displaytypes', get_string('gradeexportdisplaytypes', 'grades'), ' ', false);
+            $mform->setDefault('display[real]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_REAL);
+            $mform->setDefault('display[percentage]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE);
+            $mform->setDefault('display[letter]', $CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER);
+        } else {
+            // Only used by XML grade export format.
+            $mform->addElement('select', 'display', get_string('gradeexportdisplaytype', 'grades'), $options);
+            $mform->setDefault('display', $CFG->grade_export_displaytype);
+        }
 
         //$default_gradedecimals = $CFG->grade_export_decimalpoints;
         $options = array(0=>0, 1=>1, 2=>2, 3=>3, 4=>4, 5=>5);
@@ -184,5 +200,30 @@ class grade_export_form extends moodleform {
 
         $this->add_action_buttons(false, $submitstring);
     }
+
+    /**
+     * Overrides the mform get_data method.
+     *
+     * Created to force a value since the validation method does not work with multiple checkbox.
+     *
+     * @return stdClass form data object.
+     */
+    public function get_data() {
+        global $CFG;
+        $data = parent::get_data();
+        if ($data && $this->_customdata['multipledisplaytypes']) {
+            if (count(array_filter($data->display)) == 0) {
+                // Ensure that a value was selected as the export plugins expect at least one value.
+                if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_LETTER) {
+                    $data->display['letter'] = GRADE_DISPLAY_TYPE_LETTER;
+                } else if ($CFG->grade_export_displaytype == GRADE_DISPLAY_TYPE_PERCENTAGE) {
+                    $data->display['percentage'] = GRADE_DISPLAY_TYPE_PERCENTAGE;
+                } else {
+                    $data->display['real'] = GRADE_DISPLAY_TYPE_REAL;
+                }
+            }
+        }
+        return $data;
+    }
 }
 
index 11fe75c..0f37711 100644 (file)
@@ -36,7 +36,17 @@ abstract class grade_export {
     public $userkey;         // export using private user key
 
     public $updatedgradesonly; // only export updated grades
-    public $displaytype; // display type (e.g. real, percentages, letter) for exports
+
+    /**
+     *  Grade display type (real, percentages or letter).
+     *
+     *  This attribute is an integer for XML file export. Otherwise is an array for all other formats (ODS, XLS and TXT).
+     *
+     *  @var $displaytype Grade display type constant (1, 2 or 3) or an array of display types where the key is the name
+     *                    and the value is the grade display type constant or 0 for unchecked display types.
+     * @access public.
+     */
+    public $displaytype;
     public $decimalpoints; // number of decimal points for exports
     public $onlyactive; // only include users with an active enrolment
     public $usercustomfields; // include users custom fields
@@ -184,6 +194,12 @@ abstract class grade_export {
 
         if (isset($formdata->display)) {
             $this->displaytype = $formdata->display;
+
+            // Used by grade exports which accept multiple display types.
+            // If the checkbox value is 0 (unchecked) then remove it.
+            if (is_array($formdata->display)) {
+                $this->displaytype = array_filter($formdata->display);
+            }
         }
 
         if (isset($formdata->updatedgradesonly)) {
@@ -212,31 +228,38 @@ abstract class grade_export {
 
     /**
      * Returns string representation of final grade
-     * @param $object $grade instance of grade_grade class
+     * @param object $grade instance of grade_grade class
+     * @param integer $gradedisplayconst grade display type constant.
      * @return string
      */
-    public function format_grade($grade) {
-        return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $this->displaytype, $this->decimalpoints);
+    public function format_grade($grade, $gradedisplayconst = null) {
+        $displaytype = $this->displaytype;
+        if (is_array($this->displaytype) && !is_null($gradedisplayconst)) {
+            $displaytype = $gradedisplayconst;
+        }
+        return grade_format_gradevalue($grade->finalgrade, $this->grade_items[$grade->itemid], false, $displaytype, $this->decimalpoints);
     }
 
     /**
      * Returns the name of column in export
      * @param object $grade_item
-     * @param boolena $feedback feedback colum
-     * &return string
+     * @param boolean $feedback feedback colum
+     * @param string $gradedisplayname grade display name.
+     * @return string
      */
-    public function format_column_name($grade_item, $feedback=false) {
+    public function format_column_name($grade_item, $feedback=false, $gradedisplayname = null) {
+        $column = new stdClass();
+        
         if ($grade_item->itemtype == 'mod') {
-            $name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name();
+            $column->name = get_string('modulename', $grade_item->itemmodule).get_string('labelsep', 'langconfig').$grade_item->get_name();
         } else {
-            $name = $grade_item->get_name();
+            $column->name = $grade_item->get_name();
         }
 
-        if ($feedback) {
-            $name .= ' ('.get_string('feedback').')';
-        }
+        // We can't have feedback and display type at the same time.
+        $column->extra = ($feedback) ? get_string('feedback') : get_string($gradedisplayname, 'grades');
 
-        return html_to_text($name, 0, false);
+        return html_to_text(get_string('gradeexportcolumntype', 'grades', $column), 0, false);
     }
 
     /**
index 82f512a..827bd67 100644 (file)
@@ -37,7 +37,7 @@ if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('
         print_error('cannotaccessgroup', 'grades');
     }
 }
-$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true));
+$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true, 'multipledisplaytypes' => true));
 $data = $mform->get_data();
 
 // print all the exported data here
index ec81be8..468496c 100644 (file)
@@ -67,7 +67,9 @@ class grade_export_ods extends grade_export {
             $myxls->write_string(0, $pos++, get_string("suspended"));
         }
         foreach ($this->columns as $grade_item) {
-            $myxls->write_string(0, $pos++, $this->format_column_name($grade_item));
+            foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
+                $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, false, $gradedisplayname));
+            }
 
             // Add a column_feedback column.
             if ($this->export_feedback) {
@@ -101,12 +103,13 @@ class grade_export_ods extends grade_export {
                     $status = $geub->track($grade);
                 }
 
-                $gradestr = $this->format_grade($grade);
-                if (is_numeric($gradestr)) {
-                    $myxls->write_number($i,$j++,$gradestr);
-                }
-                else {
-                    $myxls->write_string($i,$j++,$gradestr);
+                foreach ($this->displaytype as $gradedisplayconst) {
+                    $gradestr = $this->format_grade($grade, $gradedisplayconst);
+                    if (is_numeric($gradestr)) {
+                        $myxls->write_number($i, $j++, $gradestr);
+                    } else {
+                        $myxls->write_string($i, $j++, $gradestr);
+                    }
                 }
 
                 // writing feedback if requested
index 9901cca..29ee8a1 100644 (file)
@@ -43,7 +43,8 @@ if (!empty($CFG->gradepublishing)) {
 $actionurl = new moodle_url('/grade/export/ods/export.php');
 $formoptions = array(
     'publishing' => true,
-    'simpleui' => true
+    'simpleui' => true,
+    'multipledisplaytypes' => true
 );
 
 $mform = new grade_export_form($actionurl, $formoptions);
index 07d8670..bb3a776 100644 (file)
@@ -41,7 +41,8 @@ if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('
 $params = array(
     'includeseparator'=>true,
     'publishing' => true,
-    'simpleui' => true
+    'simpleui' => true,
+    'multipledisplaytypes' => true
 );
 $mform = new grade_export_form(null, $params);
 $data = $mform->get_data();
index 0a379c2..109bce6 100644 (file)
@@ -67,9 +67,11 @@ class grade_export_txt extends grade_export {
             $exporttitle[] = get_string("suspended");
         }
 
-        // Add a feedback column.
+        // Add grades and feedback columns.
         foreach ($this->columns as $grade_item) {
-            $exporttitle[] = $this->format_column_name($grade_item);
+            foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
+                $exporttitle[] = $this->format_column_name($grade_item, false, $gradedisplayname);
+            }
             if ($this->export_feedback) {
                 $exporttitle[] = $this->format_column_name($grade_item, true);
             }
@@ -100,7 +102,9 @@ class grade_export_txt extends grade_export {
                     $status = $geub->track($grade);
                 }
 
-                $exportdata[] = $this->format_grade($grade);
+                foreach ($this->displaytype as $gradedisplayconst) {
+                    $exportdata[] = $this->format_grade($grade, $gradedisplayconst);
+                }
 
                 if ($this->export_feedback) {
                     $exportdata[] = $this->format_feedback($userdata->feedbacks[$itemid]);
index 571f49e..c5420ce 100644 (file)
@@ -44,7 +44,8 @@ $actionurl = new moodle_url('/grade/export/txt/export.php');
 $formoptions = array(
     'includeseparator'=>true,
     'publishing' => true,
-    'simpleui' => true
+    'simpleui' => true,
+    'multipledisplaytypes' => true
 );
 
 $mform = new grade_export_form($actionurl, $formoptions);
index db0ba72..c90e254 100644 (file)
@@ -39,11 +39,36 @@ Feature: I need to export grades as text
     And I should not see "80.00"
 
   @javascript
-  Scenario: Export grades as text using percentages
+  Scenario: Export grades as text using real
     When I set the field "Grade report" to "Plain text file"
     And I expand all fieldsets
-    And I set the field "Grade export display type" to "Percent"
+    And  I set the following fields to these values:
+      | Real        | 1                        |
     And I click on "Course total" "checkbox"
     And I press "Download"
     Then I should see "Student,1"
+    And I should see "80.00"
+
+  @javascript
+  Scenario: Export grades as text using percentages and letters
+    When I set the field "Grade report" to "Plain text file"
+    And  I set the following fields to these values:
+      | Percentage   | 1                        |
+      | Letter       | 1                        |
+    And I press "Download"
+    Then I should see "Student,1"
+    And I should see "80.00 %"
+    And I should see "B-"
+
+  @javascript
+  Scenario: Export grades as text using real, percentages and letters
+    When I set the field "Grade report" to "Plain text file"
+    And  I set the following fields to these values:
+      | Real         | 1                        |
+      | Percentage   | 1                        |
+      | Letter       | 1                        |
+    And I press "Download"
+    Then I should see "Student,1"
+    And I should see "80.00"
     And I should see "80.00 %"
+    And I should see "B-"
\ No newline at end of file
index f9d0401..1ce80f5 100644 (file)
@@ -4,4 +4,4 @@ information provided here is intended especially for developers.
 === 2.8 ===
 
 The UI for the grade export form was simplified down so it's all on one page. The export preview was removed because it was not useful (more useful on import than on export). To update your export plugins you must pass 'simpleui' => true as an option to the grade_export_form, and make your grade_export_form submit directly to your export script. It's easiest to look at a complete example - see "git show 1cc43058" for a complete example of updating the ods exporter.
-
+Also the grade export UI form was changed to support multiples display types. The combo box was removed from (Open Document, Plain Text, Excel) and for each selected display type a column is generated. We didn't extended this solution to XML export which remains with a combo and can only select a single display type.
\ No newline at end of file
index 3506805..409ce48 100644 (file)
@@ -37,7 +37,7 @@ if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('
         print_error('cannotaccessgroup', 'grades');
     }
 }
-$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true));
+$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true, 'multipledisplaytypes' => true));
 $formdata = $mform->get_data();
 
 // print all the exported data here
index a3c75bd..384d768 100644 (file)
@@ -65,8 +65,9 @@ class grade_export_xls extends grade_export {
             $myxls->write_string(0, $pos++, get_string("suspended"));
         }
         foreach ($this->columns as $grade_item) {
-            $myxls->write_string(0, $pos++, $this->format_column_name($grade_item));
-
+            foreach ($this->displaytype as $gradedisplayname => $gradedisplayconst) {
+                $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, false, $gradedisplayname));
+            }
             // Add a column_feedback column
             if ($this->export_feedback) {
                 $myxls->write_string(0, $pos++, $this->format_column_name($grade_item, true));
@@ -97,15 +98,14 @@ class grade_export_xls extends grade_export {
                 if ($export_tracking) {
                     $status = $geub->track($grade);
                 }
-
-                $gradestr = $this->format_grade($grade);
-                if (is_numeric($gradestr)) {
-                    $myxls->write_number($i,$j++,$gradestr);
+                foreach ($this->displaytype as $gradedisplayconst) {
+                    $gradestr = $this->format_grade($grade, $gradedisplayconst);
+                    if (is_numeric($gradestr)) {
+                        $myxls->write_number($i, $j++, $gradestr);
+                    } else {
+                        $myxls->write_string($i, $j++, $gradestr);
+                    }
                 }
-                else {
-                    $myxls->write_string($i,$j++,$gradestr);
-                }
-
                 // writing feedback if requested
                 if ($this->export_feedback) {
                     $myxls->write_string($i, $j++, $this->format_feedback($userdata->feedbacks[$itemid]));
index 517b95c..4d86ba8 100644 (file)
@@ -43,7 +43,8 @@ if (!empty($CFG->gradepublishing)) {
 $actionurl = new moodle_url('/grade/export/xls/export.php');
 $formoptions = array(
     'publishing' => true,
-    'simpleui' => true
+    'simpleui' => true,
+    'multipledisplaytypes' => true
 );
 
 $mform = new grade_export_form($actionurl, $formoptions);
index 5c44040..3ab49a4 100644 (file)
@@ -37,7 +37,7 @@ if (groups_get_course_groupmode($COURSE) == SEPARATEGROUPS and !has_capability('
         print_error('cannotaccessgroup', 'grades');
     }
 }
-$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true));
+$mform = new grade_export_form(null, array('publishing' => true, 'simpleui' => true, 'multipledisplaytypes' => false));
 $formdata = $mform->get_data();
 
 // print all the exported data here
index de1b979..8b25939 100644 (file)
@@ -68,7 +68,7 @@ class grade_export_xml extends grade_export {
             foreach ($userdata->grades as $itemid => $grade) {
                 $grade_item = $this->grade_items[$itemid];
                 $grade->grade_item =& $grade_item;
-                $gradestr = $this->format_grade($grade); // no formating for now
+                $gradestr = $this->format_grade($grade, $this->displaytype); // no formating for now
 
                 // MDL-11669, skip exported grades or bad grades (if setting says so)
                 if ($export_tracking) {
index ff1c268..3b70fd7 100644 (file)
@@ -46,7 +46,8 @@ $formoptions = array(
     'idnumberrequired' => true,
     'updategradesonly' => true,
     'publishing' => true,
-    'simpleui' => true
+    'simpleui' => true,
+    'multipledisplaytypes' => false
 );
 
 $mform = new grade_export_form($actionurl, $formoptions);
index 84ae60a..727bf1a 100644 (file)
@@ -708,3 +708,5 @@ $string['writinggradebookinfo'] = 'Writing gradebook settings';
 $string['xml'] = 'XML';
 $string['yes'] = 'Yes';
 $string['yourgrade'] = 'Your grade';
+$string['gradeexportdisplaytypes'] = 'Grade export display types';
+$string['gradeexportcolumntype'] = '{$a->name} ({$a->extra})';
\ No newline at end of file