Skip to content

Commit

Permalink
Merge pull request #97 from mebis-lp/MBS-8971-Add-intro
Browse files Browse the repository at this point in the history
MBS-8971: Add intro
  • Loading branch information
TobiGa authored Jul 22, 2024
2 parents 7d172e8 + e1e4a53 commit c89e655
Show file tree
Hide file tree
Showing 25 changed files with 308 additions and 99 deletions.
2 changes: 1 addition & 1 deletion amd/build/learningmap.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/learningmap.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion amd/build/placestore.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/placestore.min.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions amd/src/learningmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const init = () => {

// DOM nodes for the editor
let mapdiv = document.getElementById('learningmap-editor-map');
let code = document.getElementById('id_introeditor_text');
let code = document.getElementById('id_svgcode');

// DOM nodes for the activity selector
let activitySetting = document.getElementById('learningmap-activity-setting');
Expand Down Expand Up @@ -230,7 +230,7 @@ export const init = () => {
activitySetting.style.display = 'none';
}

let backgroundfileNode = document.getElementById('id_introeditor_itemid_fieldset');
let backgroundfileNode = document.getElementById('id_backgroundfile_fieldset');
if (backgroundfileNode) {
let observer = new MutationObserver(refreshBackgroundImage);
observer.observe(backgroundfileNode, {attributes: true, childList: true, subtree: true});
Expand Down
4 changes: 2 additions & 2 deletions amd/src/placestore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let placestore = {
version: 2024022102,
version: 2024072201,
id: 0,
places: [],
paths: [],
Expand Down Expand Up @@ -36,7 +36,7 @@ let placestore = {
// eslint-disable-next-line no-empty
} catch { }
// Update version (only relevant if learning map is saved)
this.version = 2024022102;
this.version = 2024072201;
},
/**
* Returns placestore as a JSON string ()
Expand Down
14 changes: 13 additions & 1 deletion backup/moodle2/backup_learningmap_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,24 @@ protected function define_structure(): backup_nested_element {
$learningmap = new backup_nested_element(
'learningmap',
['id'],
['course', 'name', 'intro', 'introformat', 'timemodified', 'placestore', 'completiontype', 'backlink']
[
'course',
'name',
'intro',
'introformat',
'timemodified',
'placestore',
'completiontype',
'backlink',
'svgcode',
'showmaponcoursepage',
]
);

$learningmap->set_source_table('learningmap', ['id' => backup::VAR_ACTIVITYID]);

$learningmap->annotate_files('mod_learningmap', 'intro', null);
$learningmap->annotate_files('mod_learningmap', 'background', null);

return $this->prepare_activity_structure($learningmap);
}
Expand Down
32 changes: 20 additions & 12 deletions backup/moodle2/restore_learningmap_activity_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

use mod_learningmap\migrationhelper;

defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/mod/learningmap/backup/moodle2/restore_learningmap_stepslib.php');
Expand Down Expand Up @@ -53,7 +55,7 @@ protected function define_my_steps(): void {
*/
public static function define_decode_contents(): array {
$contents = [];
$contents[] = new restore_decode_content('learningmap', ['intro'], 'learningmap');
$contents[] = new restore_decode_content('learningmap', ['intro', 'svgcode'], 'learningmap');
return $contents;
}

Expand All @@ -78,6 +80,7 @@ public function after_restore(): void {
$modinfo = get_fast_modinfo($courseid);

$item = $DB->get_record('learningmap', ['id' => $this->get_activityid()], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('learningmap', $item->id, $courseid);

$placestore = json_decode($item->placestore);

Expand All @@ -99,25 +102,30 @@ public function after_restore(): void {
}
$oldmapid = $placestore->mapid;
$newmapid = uniqid();
$item->intro = str_replace('learningmap-svgmap-' . $oldmapid, 'learningmap-svgmap-' . $newmapid, $item->intro);
$placestore->mapid = $newmapid;

if (!isset($placestore->version) || $placestore->version < 2024022102) {
$placestore->version = 2024022102;
if (!isset($placestore->version) || $placestore->version < 2024072201) {
$placestore->version = 2024072201;
// Needs 1 as default value (otherwise all place strokes would be hidden).
if (!isset($placestore->strokeopacity)) {
$placestore->strokeopacity = 1;
}
$mapworker = new \mod_learningmap\mapworker($item->intro, (array)$placestore);
if (empty($item->svgcode)) {
$mapcode = $item->intro;
$item->intro = '';
$item->showmaponcoursepage = $cm->showdescription;
migrationhelper::move_files_to_background_filearea($item->id);
} else {
$mapcode = $item->svgcode;
}
$mapworker = new \mod_learningmap\mapworker($mapcode, (array)$placestore);
$mapworker->replace_stylesheet();
$mapworker->replace_defs();
$item->intro = $mapworker->get_svgcode();
$item->svgcode = $mapworker->get_svgcode();
}

$json = json_encode($placestore);

$DB->set_field('learningmap', 'placestore', $json, ['id' => $this->get_activityid()]);
$DB->set_field('learningmap', 'intro', $item->intro, ['id' => $this->get_activityid()]);
$DB->set_field('learningmap', 'course', $courseid, ['id' => $this->get_activityid()]);
$item->svgcode = str_replace('learningmap-svgmap-' . $oldmapid, 'learningmap-svgmap-' . $newmapid, $item->svgcode);
$item->placestore = json_encode($placestore);
$item->course = $courseid;
$DB->update_record('learningmap', $item);
}
}
1 change: 1 addition & 0 deletions backup/moodle2/restore_learningmap_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ protected function process_learningmap($data): void {
*/
protected function after_execute(): void {
$this->add_related_files('mod_learningmap', 'intro', null);
$this->add_related_files('mod_learningmap', 'background', null);
}
}
119 changes: 119 additions & 0 deletions classes/migrationhelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_learningmap;

/**
* This class provides helper functions for migration from learningmaps being stored in the intro field to the new column svgcode.
*
* @package mod_learningmap
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class migrationhelper {
/**
* Moves all files from the intro field to the new filearea 'background'.
* This is only executed if there are no files in the background filearea yet.
* @param int $instanceid The id of the learningmap instance.
*/
public static function move_files_to_background_filearea(int $instanceid) {
global $DB;
$fs = get_file_storage();

// Getting course modules via DB query as get_coursemodule_from_instance() is not available during upgrade.
$moduleid = $DB->get_field('modules', 'id', ['name' => 'learningmap']);

if (empty($moduleid)) {
return;
}

$cm = $DB->get_record('course_modules', ['instance' => $instanceid, 'module' => $moduleid]);

$contextid = \context_module::instance($cm->id)->id;

// Check if there are already files in the background filearea.
$backgroundfiles = $fs->get_area_files($contextid, 'mod_learningmap', 'background', 0, 'id', false);
if (count($backgroundfiles) != 0) {
return;
}

$files = $fs->get_area_files($contextid, 'mod_learningmap', 'intro', 0, 'id', false);
foreach ($files as $file) {
$filerecord = [
'contextid' => $file->get_contextid(),
'component' => 'mod_learningmap',
'filearea' => 'background',
'itemid' => 0,
'filepath' => '/',
'filename' => $file->get_filename(),
'timecreated' => time(),
'timemodified' => time(),
];
$fs->create_file_from_storedfile($filerecord, $file);
$file->delete();
}
}

/**
* Checks if the given learningmap instance has a SVG code stored in the 'svgcode' column.
*
* @param int $instanceid The id of the learningmap instance.
* @return bool
*/
public static function is_version_without_svgcode(int $instanceid): bool {
global $DB;
$entry = $DB->get_record('learningmap', ['id' => $instanceid]);
return empty($entry->svgcode);
}

/**
* Update one learningmap instance to have the SVG code stored in the 'svgcode' column.
*
* @param int $instanceid The id of the learningmap instance. If empty, all learningmaps will be updated.
* @return void
*/
public static function update_learningmaps_to_use_svgcode(int $instanceid = 0) {
global $DB;

// Getting course modules via DB query as get_coursemodule_from_instance() is not available during upgrade.
$moduleid = $DB->get_field('modules', 'id', ['name' => 'learningmap']);

if (empty($moduleid)) {
return;
}

$params = [];
$where = 'svgcode IS NULL';
if (!empty($instanceid)) {
$where .= ' AND id = :id';
$params['id'] = $instanceid;
}

$learningmaps = $DB->get_recordset_select('learningmap', $where, $params);
foreach ($learningmaps as $learningmap) {
$cm = $DB->get_record('course_modules', ['instance' => $learningmap->id, 'module' => $moduleid]);
$learningmap->svgcode = $learningmap->intro;
$learningmap->intro = '';
// To keep a consistent behavior with the old version, we set the showmaponcoursepage to the value of
// showdescription (which was responsible for displaying the map on course page before).
$learningmap->showmaponcoursepage = $cm->showdescription;
$DB->update_record('learningmap', $learningmap);
self::move_files_to_background_filearea($learningmap->id);
}
$learningmaps->close();
}
}
Loading

0 comments on commit c89e655

Please sign in to comment.