Skip to content

Commit

Permalink
MBS-9291: Fix background upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-csg committed Aug 5, 2024
1 parent c89e655 commit 6e19cee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
40 changes: 35 additions & 5 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use core\plugininfo\format;
use mod_learningmap\cachemanager;

/**
Expand All @@ -50,7 +49,20 @@
*/
function learningmap_add_instance($data): int {
global $DB;
return $DB->insert_record("learningmap", $data);
$learningmapid = $DB->insert_record('learningmap', $data);

$context = context_module::instance($data->coursemodule);
if (!empty($data->backgroundfile)) {
file_save_draft_area_files(
$data->backgroundfile,
$context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1]
);
}
return $learningmapid;
}

/**
Expand All @@ -62,6 +74,22 @@ function learningmap_add_instance($data): int {
function learningmap_update_instance($data): int {
global $DB;
$data->id = $data->instance;

$context = context_module::instance($data->coursemodule);
if (!empty($data->backgroundfile)) {
// Delete old background files.
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_learningmap', 'background');
file_save_draft_area_files(
$data->backgroundfile,
$context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1]
);
}

return $DB->update_record("learningmap", $data);
}

Expand All @@ -74,8 +102,10 @@ function learningmap_update_instance($data): int {
function learningmap_delete_instance($id): int {
global $DB;

$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_learningmap', 'background');

return $DB->delete_records("learningmap", ["id" => $id]);
// ToDo: Check whether intro files are automatically deleted.
}

/**
Expand Down Expand Up @@ -237,13 +267,13 @@ function learningmap_cm_info_view(cm_info $cm): void {

$mapcontainer = $OUTPUT->render_from_template(
'mod_learningmap/rendercontainer',
['cmId' => $cm->id, 'enableLiveUpdater' => true]
['cmId' => $cm->id, 'enableLiveUpdater' => true, 'contentbeforemap' => $groupdropdown . $intro]
);

$cm->set_custom_cmlist_item(true);
}

$cm->set_content($groupdropdown . $intro . $mapcontainer, true);
$cm->set_content($mapcontainer, true);
}

/**
Expand Down
37 changes: 17 additions & 20 deletions mod_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ public function add_completion_rules(): array {
public function data_preprocessing(&$defaultvalues): void {
global $OUTPUT;

$draftitemid = file_get_submitted_draft_itemid('backgroundfile');

// Initialize a new learningmap instance.
if (!$this->current->instance) {
// Every map gets a unique id for applying CSS.
Expand Down Expand Up @@ -264,17 +262,20 @@ public function data_preprocessing(&$defaultvalues): void {
$mapworker->process_map_objects();
$mapworker->replace_stylesheet();
$defaultvalues['svgcode'] = $mapworker->get_svgcode();

$draftitemid = file_get_submitted_draft_itemid('backgroundfile');

$defaultvalues['svgcode'] = file_prepare_draft_area(
$draftitemid,
$context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1],
$defaultvalues['svgcode']
);
$defaultvalues['backgroundfile'] = $draftitemid;
}
$defaultvalues['svgcode'] = file_prepare_draft_area(
$draftitemid,
$context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1],
$defaultvalues['svgcode']
);
$defaultvalues['backgroundfile'] = $draftitemid;
}

/**
Expand All @@ -294,16 +295,12 @@ public function data_postprocessing($data): void {
$mapworker->replace_stylesheet();
$data->svgcode = $mapworker->get_svgcode();

$data->svgcode = file_save_draft_area_files(
$data->backgroundfile,
$this->context->id,
'mod_learningmap',
'background',
0,
['subdirs' => 0, 'maxfiles' => 1],
$data->svgcode
$data->svgcode = file_rewrite_urls_to_pluginfile(
$data->svgcode,
$data->backgroundfile
);
}
parent::data_postprocessing($data);
}

/**
Expand Down

0 comments on commit 6e19cee

Please sign in to comment.