diff --git a/grade/export/grade_export_form.php b/grade/export/grade_export_form.php index 5b3cc5519f0..5441dbb43d5 100644 --- a/grade/export/grade_export_form.php +++ b/grade/export/grade_export_form.php @@ -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; + } } diff --git a/grade/export/lib.php b/grade/export/lib.php index 11fe75c514e..0f377118d8b 100644 --- a/grade/export/lib.php +++ b/grade/export/lib.php @@ -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); } /** diff --git a/grade/export/ods/export.php b/grade/export/ods/export.php index 82f512a87f1..827bd67d16d 100644 --- a/grade/export/ods/export.php +++ b/grade/export/ods/export.php @@ -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 diff --git a/grade/export/ods/grade_export_ods.php b/grade/export/ods/grade_export_ods.php index ec81be8180f..468496c9670 100644 --- a/grade/export/ods/grade_export_ods.php +++ b/grade/export/ods/grade_export_ods.php @@ -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 diff --git a/grade/export/ods/index.php b/grade/export/ods/index.php index 9901ccac736..29ee8a1daa4 100644 --- a/grade/export/ods/index.php +++ b/grade/export/ods/index.php @@ -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); diff --git a/grade/export/txt/export.php b/grade/export/txt/export.php index 07d8670c87f..bb3a7761c23 100644 --- a/grade/export/txt/export.php +++ b/grade/export/txt/export.php @@ -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(); diff --git a/grade/export/txt/grade_export_txt.php b/grade/export/txt/grade_export_txt.php index 0a379c231ee..109bce6fe9a 100644 --- a/grade/export/txt/grade_export_txt.php +++ b/grade/export/txt/grade_export_txt.php @@ -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]); diff --git a/grade/export/txt/index.php b/grade/export/txt/index.php index 571f49e1fec..c5420cef47e 100644 --- a/grade/export/txt/index.php +++ b/grade/export/txt/index.php @@ -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); diff --git a/grade/export/txt/tests/behat/export.feature b/grade/export/txt/tests/behat/export.feature index db0ba72ae52..c90e25424aa 100644 --- a/grade/export/txt/tests/behat/export.feature +++ b/grade/export/txt/tests/behat/export.feature @@ -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 diff --git a/grade/export/upgrade.txt b/grade/export/upgrade.txt index f9d04015a6a..1ce80f58db2 100644 --- a/grade/export/upgrade.txt +++ b/grade/export/upgrade.txt @@ -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 diff --git a/grade/export/xls/export.php b/grade/export/xls/export.php index 35068052c57..409ce480a70 100644 --- a/grade/export/xls/export.php +++ b/grade/export/xls/export.php @@ -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 diff --git a/grade/export/xls/grade_export_xls.php b/grade/export/xls/grade_export_xls.php index a3c75bd9a8f..384d76886a7 100644 --- a/grade/export/xls/grade_export_xls.php +++ b/grade/export/xls/grade_export_xls.php @@ -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])); diff --git a/grade/export/xls/index.php b/grade/export/xls/index.php index 517b95c7711..4d86ba8680b 100644 --- a/grade/export/xls/index.php +++ b/grade/export/xls/index.php @@ -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); diff --git a/grade/export/xml/export.php b/grade/export/xml/export.php index 5c44040f9a2..3ab49a47472 100644 --- a/grade/export/xml/export.php +++ b/grade/export/xml/export.php @@ -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 diff --git a/grade/export/xml/grade_export_xml.php b/grade/export/xml/grade_export_xml.php index de1b97989d5..8b259395e9c 100644 --- a/grade/export/xml/grade_export_xml.php +++ b/grade/export/xml/grade_export_xml.php @@ -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) { diff --git a/grade/export/xml/index.php b/grade/export/xml/index.php index ff1c268f5fa..3b70fd75564 100644 --- a/grade/export/xml/index.php +++ b/grade/export/xml/index.php @@ -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); diff --git a/lang/en/grades.php b/lang/en/grades.php index 84ae60a0ef7..727bf1a168b 100644 --- a/lang/en/grades.php +++ b/lang/en/grades.php @@ -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