Skip to content

Commit

Permalink
MBS-9793: Changes for format_learningmap
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-csg committed Jan 10, 2025
1 parent e9a101e commit d552cca
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 10 deletions.
11 changes: 11 additions & 0 deletions amd/build/initmainlearningmap.min.js

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

1 change: 1 addition & 0 deletions amd/build/initmainlearningmap.min.js.map

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

12 changes: 12 additions & 0 deletions amd/build/mainlearningmap.min.js

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

1 change: 1 addition & 0 deletions amd/build/mainlearningmap.min.js.map

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

34 changes: 34 additions & 0 deletions amd/src/initmainlearningmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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/>.

/**
* Load module for highlighting the main learningmap.
*
* @module mod_learningmap/initmainlearningmap
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import MainLearningmap from 'mod_learningmap/mainlearningmap';
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';

export const init = (cmid, isfirstlearningmap = false) => {
return new MainLearningmap({
element: document.querySelector('.activity[data-id="' + cmid + '"]'),
reactive: getCurrentCourseEditor(),
cmid: cmid,
isfirstlearningmap: isfirstlearningmap,
});
};
98 changes: 98 additions & 0 deletions amd/src/mainlearningmap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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/>.

/**
* Highlight main learningmap for format_learningmap.
*
* @module mod_learningmap/mainlearningmap
* @copyright 2024 ISB Bayern
* @author Stefan Hanauska <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import {BaseComponent} from 'core/reactive';
import {refreshModule} from 'core_course/actions';
import {render as renderTemplate} from 'core/templates';

/**
* The live updater component.
*/
export default class extends BaseComponent {
create(descriptor) {
this.element = descriptor.element;
this.reactive = descriptor.reactive;
this.cmid = descriptor.cmid;
this.isfirstlearningmap = descriptor.isfirstlearningmap;
}

getWatchers() {
const watchers = [
{watch: `cm:deleted`, handler: this._updateLearningmap},
{watch: `section:updated`, handler: this._updateLearningmap},
{watch: `cm:added`, handler: this._updateLearningmap},
{watch: `cm[${this.cmid}]:deleted`, handler: this.destroy},
];
return watchers;
}

async destroy() {
if (this._getFirstLearningmap() === undefined) {
await renderTemplate('format_learningmap/notification', {}).then((html) => {
document.querySelector('.format_learningmap-notification').innerHTML = html;
return true;
});
}
}

async _updateLearningmap() {
if (this._isFirstLearningmap() && !this.isfirstlearningmap) {
this.isfirstlearningmap = true;
this.getElement().classList.add('format_learningmap-firstlearningmap');
refreshModule(this.element, this.cmid);
document.querySelector('.format_learningmap-notification').innerHTML = '';
} else {
if (this.isfirstlearningmap) {
this.isfirstlearningmap = false;
this.getElement().classList.remove('format_learningmap-firstlearningmap');
refreshModule(this.element, this.cmid);
}
}
}

_isFirstLearningmap() {
let firstLearningmap = this._getFirstLearningmap();
return firstLearningmap == this.cmid;
}

_getFirstLearningmap() {
let state = this.reactive.stateManager.state;
let cmlist = this._getCmlist();
return cmlist.find((cmid) => {
let cm = state.cm.get(cmid);
return (cm.module == 'learningmap');
});
}

_getCmlist() {
let state = this.reactive.stateManager.state;
let cmlist = [];
state.course.sectionlist.forEach((sectionid) => {
let section = state.section.get(sectionid);
section.cmlist.forEach((cmid) => {
cmlist.push(cmid);
});
});
return cmlist;
}
}
41 changes: 41 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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;

/**
* Class helper
*
* @package mod_learningmap
* @copyright 2024 ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {
/**
* Returns whether the map should be shown on the course page. If course format is format_learningmap
* the setting in the database is ignored.
*
* @param cm_info $cm
* @return bool
*/
public static function show_map_on_course_page($cm): bool {
global $DB;
$showmaponcoursepage = $DB->get_field('learningmap', 'showmaponcoursepage', ['id' => $cm->instance]);
[$course, ] = get_course_and_cm_from_cmid($cm->id);
$courseformat = $course->format;
return !empty($showmaponcoursepage) && $courseformat != 'learningmap';
}
}
43 changes: 43 additions & 0 deletions classes/output/courseformat/activitybadge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\output\courseformat;

/**
* Class activitybadge
*
* @package mod_learningmap
* @copyright 2024 ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activitybadge extends \core_courseformat\output\activitybadge {
/**
* Updates the content of the activity badge.
*/
protected function update_content(): void {
$course = $this->cminfo->get_course();
if ($course->format == 'learningmap') {
$courseformat = course_get_format($course);
if ($courseformat->main_learningmap_exists()) {
$mainlearningmap = $courseformat->get_main_learningmap();
if ($this->cminfo->id == $mainlearningmap->id) {
$this->content = get_string('mainlearningmap', 'format_learningmap');
$this->style = 'badge-primary';
}
}
}
}
}
7 changes: 3 additions & 4 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

use mod_learningmap\cachemanager;
use mod_learningmap\helper;

/**
* Array with all features the plugin supports for advanced settings. Might be moved
Expand Down Expand Up @@ -213,10 +214,8 @@ function learningmap_get_coursemodule_info($cm): cached_cm_info {
* @return void
*/
function learningmap_cm_info_dynamic(cm_info $cm): void {
global $DB;
$showmaponcoursepage = $DB->get_field('learningmap', 'showmaponcoursepage', ['id' => $cm->instance]);
// Decides whether to display the link.
if (!empty($showmaponcoursepage)) {
if (helper::show_map_on_course_page($cm)) {
$cm->set_no_view_link(true);
}
}
Expand All @@ -241,7 +240,7 @@ function learningmap_cm_info_view(cm_info $cm): void {
}

// Only show map on course page if showmaponcoursepage is set.
if (!empty($learningmap->showmaponcoursepage)) {
if (helper::show_map_on_course_page($cm)) {
if (!empty($cm->groupmode)) {
$groupdropdown = groups_print_activity_menu(
$cm,
Expand Down
Loading

0 comments on commit d552cca

Please sign in to comment.