From 858bcbab0aa9c655fb2b20a7977312acef5798a1 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Sun, 1 Aug 2021 23:25:40 +0100 Subject: [PATCH] MDL-72257 gradeexport_xml: detect absense of grade items to export. Where there are no grade items with idnumbers for selection, then nothing should be exported. Currently, this is being interpreted as the user exporting all grade items. --- grade/export/lib.php | 15 ++++++++------- grade/export/xml/grade_export_xml.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/grade/export/lib.php b/grade/export/lib.php index c05d31a9508..8a3fe3af910 100644 --- a/grade/export/lib.php +++ b/grade/export/lib.php @@ -24,6 +24,9 @@ require_once($CFG->dirroot.'/grade/export/grade_export_form.php'); */ abstract class grade_export { + /** @var int Value to state nothing is being exported. */ + protected const EXPORT_SELECT_NONE = -1; + public $plugin; // plgin name - must be filled in subclasses! public $grade_items; // list of all course grade items @@ -112,9 +115,8 @@ abstract class grade_export { //with an empty $itemlist then reconstruct it in process_form() using $formdata $this->columns = array(); if (!empty($itemlist)) { - if ($itemlist=='-1') { - //user deselected all items - } else { + // Check that user selected something. + if ($itemlist != self::EXPORT_SELECT_NONE) { $itemids = explode(',', $itemlist); // remove items that are not requested foreach ($itemids as $itemid) { @@ -149,9 +151,8 @@ abstract class grade_export { $this->columns = array(); if (!empty($formdata->itemids)) { - if ($formdata->itemids=='-1') { - //user deselected all items - } else { + // Check that user selected something. + if ($formdata->itemids != self::EXPORT_SELECT_NONE) { foreach ($formdata->itemids as $itemid=>$selected) { if ($selected and array_key_exists($itemid, $this->grade_items)) { $this->columns[$itemid] =& $this->grade_items[$itemid]; @@ -411,7 +412,7 @@ abstract class grade_export { $itemids = array_keys($this->columns); $itemidsparam = implode(',', $itemids); if (empty($itemidsparam)) { - $itemidsparam = '-1'; + $itemidsparam = self::EXPORT_SELECT_NONE; } // We have a single grade display type constant. diff --git a/grade/export/xml/grade_export_xml.php b/grade/export/xml/grade_export_xml.php index c0789196f97..865da1a979e 100644 --- a/grade/export/xml/grade_export_xml.php +++ b/grade/export/xml/grade_export_xml.php @@ -33,6 +33,20 @@ class grade_export_xml extends grade_export { return htmlspecialchars($idnumber, ENT_QUOTES | ENT_XML1); } + /** + * Handle form processing for export. Note we need to handle the case where there are no 'itemids[]' being included in the + * form, because each is disabled for selection due to having empty idnumber + * + * @param stdClass $formdata + */ + public function process_form($formdata) { + if (!isset($formdata->itemids)) { + $formdata->itemids = self::EXPORT_SELECT_NONE; + } + + parent::process_form($formdata); + } + /** * To be implemented by child classes * @param boolean $feedback