MDL-71517 mod_data: remove invalid form custom data access.
authorPaul Holden <paulh@moodle.com>
Tue, 4 May 2021 14:25:57 +0000 (15:25 +0100)
committerPaul Holden <paulh@moodle.com>
Tue, 4 May 2021 14:25:57 +0000 (15:25 +0100)
On PHP7.4, trying to access array offsets of NULL triggers a notice.

mod/data/import.php
mod/data/import_form.php

index fb5ad44..b8894fe 100644 (file)
@@ -64,7 +64,6 @@ require_login($course, false, $cm);
 
 $context = context_module::instance($cm->id);
 require_capability('mod/data:manageentries', $context);
-$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
 
 /// Print the page header
 $PAGE->navbar->add(get_string('add', 'data'));
@@ -78,14 +77,10 @@ echo $OUTPUT->heading_with_help(get_string('uploadrecords', 'mod_data'), 'upload
 $currentgroup = groups_get_activity_group($cm);
 $groupmode = groups_get_activity_groupmode($cm);
 
+$form = new mod_data_import_form(new moodle_url('/mod/data/import.php'), ['dataid' => $data->id]);
 if (!$formdata = $form->get_data()) {
     /// Upload records section. Only for teachers and the admin.
     echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide');
-    require_once('import_form.php');
-    $form = new mod_data_import_form(new moodle_url('/mod/data/import.php'));
-    $formdata = new stdClass();
-    $formdata->d = $data->id;
-    $form->set_data($formdata);
     $form->display();
     echo $OUTPUT->box_end();
     echo $OUTPUT->footer();
index 4ae95c6..8f0b154 100644 (file)
@@ -9,9 +9,9 @@ require_once($CFG->libdir.'/csvlib.class.php');
 class mod_data_import_form extends moodleform {
 
     function definition() {
-        global $CFG;
         $mform =& $this->_form;
-        $cmid = $this->_customdata['id'];
+
+        $dataid = $this->_customdata['dataid'];
 
         $mform->addElement('filepicker', 'recordsfile', get_string('csvfile', 'data'));
 
@@ -21,15 +21,16 @@ class mod_data_import_form extends moodleform {
 
         $mform->addElement('text', 'fieldenclosure', get_string('fieldenclosure', 'data'));
         $mform->setType('fieldenclosure', PARAM_CLEANHTML);
+
         $choices = core_text::get_encodings();
         $mform->addElement('select', 'encoding', get_string('fileencoding', 'mod_data'), $choices);
         $mform->setDefault('encoding', 'UTF-8');
 
-        $submit_string = get_string('submit');
-        // data id
+        // Database activity ID.
         $mform->addElement('hidden', 'd');
         $mform->setType('d', PARAM_INT);
+        $mform->setDefault('d', $dataid);
 
-        $this->add_action_buttons(false, $submit_string);
+        $this->add_action_buttons(false, get_string('submit'));
     }
 }