Skip to content

Commit

Permalink
Improve handling of replace form data
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Nov 21, 2024
1 parent f802c12 commit 4c4faca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions classes/form/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function definition(): void {
// File upload.
$mform->addElement('filepicker', 'csvfile', get_string('selectfile', 'tool_advancedreplace'), null,
['accepted_types' => ['.csv']]);
$mform->addRule('csvfile', get_string('required'), 'required', null, 'client');

$this->add_action_buttons(true, get_string('replace', 'tool_advancedreplace'));

Expand Down
17 changes: 17 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,4 +750,21 @@ public static function handle_replace_csv(string $data) {
$csvimport->cleanup();
$csvimport->close();
}

/**
* Returns the file content of the replace csv. Required for confirmation.
* @param int $fileid
* @return string
*/
public static function get_replace_csv_content(int $fileid): string {
global $USER;

$fs = get_file_storage();
$context = \context_user::instance($USER->id);
if (!$files = $fs->get_area_files($context->id, 'user', 'draft', $fileid, 'id DESC', false)) {
return '';
}
$file = reset($files);
return $file->get_content();
}
}
12 changes: 7 additions & 5 deletions db_replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
global $CFG;
$replace = optional_param('delete', 0, PARAM_INT);
$confirm = optional_param('confirm', '', PARAM_BOOL);
$csvpostcontent = optional_param('csvpostcontent', '', PARAM_TEXT);
$fileid = optional_param('fileid', '', PARAM_TEXT);

$url = new moodle_url('/admin/tool/advancedreplace/db_replace.php');
$PAGE->set_url($url);
Expand All @@ -52,14 +52,16 @@
echo $OUTPUT->heading(get_string('replacepageheader', 'tool_advancedreplace'));
echo html_writer::div(get_string('replace_warning', 'tool_advancedreplace',
'$CFG->forced_plugin_settings[\'tool_advancedreplace\'][\'allowuireplace\'] = 1;'), 'alert alert-warning');
} else if ($csvcontent = ($form->get_file_content('csvfile') ?? $csvpostcontent)) {
} else if ($data = $form->get_data()) {
$returnurl = new moodle_url('/admin/tool/advancedreplace/db_replace.php');
$optionsyes = array('replace' => $replace, 'confirm' => 1, 'sesskey' => sesskey(), 'csvpostcontent' => $csvcontent);
$optionsyes = array('replace' => $replace, 'confirm' => 1, 'sesskey' => sesskey(), 'fileid' => $data->csvfile);
$deleteurl = new moodle_url($url, $optionsyes);
$deletebutton = new single_button($deleteurl, get_string('replace', 'tool_advancedreplace'), 'post');
echo $OUTPUT->confirm(get_string('replacecheck', 'tool_advancedreplace'), $deletebutton, $returnurl);
} else if ($confirm && isset($csvpostcontent)) {
helper::handle_replace_csv($csvpostcontent);
} else if ($confirm && !empty($fileid)) {
require_sesskey();
$contents = helper::get_replace_csv_content($fileid);
helper::handle_replace_csv($contents);
} else {
// Display form.
echo $OUTPUT->heading(get_string('replacepageheader', 'tool_advancedreplace'));
Expand Down

0 comments on commit 4c4faca

Please sign in to comment.