mirrored from git://git.moodle.org/moodle.git
/
mutations.js
95 lines (88 loc) · 3.76 KB
/
mutations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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/>.
/**
* Format topics mutations.
*
* An instance of this class will be used to add custom mutations to the course editor.
* To make sure the addMutations method find the proper functions, all functions must
* be declared as class attributes, not a simple methods. The reason is because many
* plugins can add extra mutations to the course editor.
*
* @module format_topics/mutations
* @copyright 2022 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
import DefaultMutations from 'core_courseformat/local/courseeditor/mutations';
import CourseActions from 'core_courseformat/local/content/actions';
class TopicsMutations extends DefaultMutations {
/**
* Highlight sections.
*
* It is important to note this mutation method is declared as a class attribute,
* See the class jsdoc for more details on why.
*
* @param {StateManager} stateManager the current state manager
* @param {array} sectionIds the list of section ids
*/
sectionHighlight = async function(stateManager, sectionIds) {
const logEntry = this._getLoggerEntry(
stateManager,
'section_highlight',
sectionIds,
{component: 'format_topics'}
);
const course = stateManager.get('course');
this.sectionLock(stateManager, sectionIds, true);
const updates = await this._callEditWebservice('section_highlight', course.id, sectionIds);
stateManager.processUpdates(updates);
this.sectionLock(stateManager, sectionIds, false);
stateManager.addLoggerEntry(await logEntry);
};
/**
* Unhighlight sections.
*
* It is important to note this mutation method is declared as a class attribute,
* See the class jsdoc for more details on why.
*
* @param {StateManager} stateManager the current state manager
* @param {array} sectionIds the list of section ids
*/
sectionUnhighlight = async function(stateManager, sectionIds) {
const logEntry = this._getLoggerEntry(
stateManager,
'section_unhighlight',
sectionIds,
{component: 'format_topics'}
);
const course = stateManager.get('course');
this.sectionLock(stateManager, sectionIds, true);
const updates = await this._callEditWebservice('section_unhighlight', course.id, sectionIds);
stateManager.processUpdates(updates);
this.sectionLock(stateManager, sectionIds, false);
stateManager.addLoggerEntry(await logEntry);
};
}
export const init = () => {
const courseEditor = getCurrentCourseEditor();
// Some plugin (activity or block) may have their own mutations already registered.
// This is why we use addMutations instead of setMutations here.
courseEditor.addMutations(new TopicsMutations());
// Add direct mutation content actions.
CourseActions.addActions({
sectionHighlight: 'sectionHighlight',
sectionUnhighlight: 'sectionUnhighlight',
});
};