| 1 | efrain | 1 | // This file is part of Moodle - http://moodle.org/
 | 
        
           |  |  | 2 | //
 | 
        
           |  |  | 3 | // Moodle is free software: you can redistribute it and/or modify
 | 
        
           |  |  | 4 | // it under the terms of the GNU General Public License as published by
 | 
        
           |  |  | 5 | // the Free Software Foundation, either version 3 of the License, or
 | 
        
           |  |  | 6 | // (at your option) any later version.
 | 
        
           |  |  | 7 | //
 | 
        
           |  |  | 8 | // Moodle is distributed in the hope that it will be useful,
 | 
        
           |  |  | 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
        
           |  |  | 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
        
           |  |  | 11 | // GNU General Public License for more details.
 | 
        
           |  |  | 12 | //
 | 
        
           |  |  | 13 | // You should have received a copy of the GNU General Public License
 | 
        
           |  |  | 14 | // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 | 
        
           |  |  | 15 |   | 
        
           |  |  | 16 | /**
 | 
        
           |  |  | 17 |  * Format topics mutations.
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * An instance of this class will be used to add custom mutations to the course editor.
 | 
        
           |  |  | 20 |  * To make sure the addMutations method find the proper functions, all functions must
 | 
        
           |  |  | 21 |  * be declared as class attributes, not a simple methods. The reason is because many
 | 
        
           |  |  | 22 |  * plugins can add extra mutations to the course editor.
 | 
        
           |  |  | 23 |  *
 | 
        
           |  |  | 24 |  * @module     format_topics/mutations
 | 
        
           |  |  | 25 |  * @copyright  2022 Ferran Recio <ferran@moodle.com>
 | 
        
           |  |  | 26 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 27 |  */
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | import {getCurrentCourseEditor} from 'core_courseformat/courseeditor';
 | 
        
           |  |  | 30 | import DefaultMutations from 'core_courseformat/local/courseeditor/mutations';
 | 
        
           |  |  | 31 | import CourseActions from 'core_courseformat/local/content/actions';
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | class TopicsMutations extends DefaultMutations {
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |     /**
 | 
        
           |  |  | 36 |      * Highlight sections.
 | 
        
           |  |  | 37 |      *
 | 
        
           |  |  | 38 |      * It is important to note this mutation method is declared as a class attribute,
 | 
        
           |  |  | 39 |      * See the class jsdoc for more details on why.
 | 
        
           |  |  | 40 |      *
 | 
        
           |  |  | 41 |      * @param {StateManager} stateManager the current state manager
 | 
        
           |  |  | 42 |      * @param {array} sectionIds the list of section ids
 | 
        
           |  |  | 43 |      */
 | 
        
           |  |  | 44 |     sectionHighlight = async function(stateManager, sectionIds) {
 | 
        
           |  |  | 45 |         const logEntry = this._getLoggerEntry(
 | 
        
           |  |  | 46 |             stateManager,
 | 
        
           |  |  | 47 |             'section_highlight',
 | 
        
           |  |  | 48 |             sectionIds,
 | 
        
           |  |  | 49 |             {component: 'format_topics'}
 | 
        
           |  |  | 50 |         );
 | 
        
           |  |  | 51 |         const course = stateManager.get('course');
 | 
        
           |  |  | 52 |         this.sectionLock(stateManager, sectionIds, true);
 | 
        
           |  |  | 53 |         const updates = await this._callEditWebservice('section_highlight', course.id, sectionIds);
 | 
        
           |  |  | 54 |         stateManager.processUpdates(updates);
 | 
        
           |  |  | 55 |         this.sectionLock(stateManager, sectionIds, false);
 | 
        
           |  |  | 56 |         stateManager.addLoggerEntry(await logEntry);
 | 
        
           |  |  | 57 |   | 
        
           |  |  | 58 |     };
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |     /**
 | 
        
           |  |  | 61 |      * Unhighlight sections.
 | 
        
           |  |  | 62 |      *
 | 
        
           |  |  | 63 |      * It is important to note this mutation method is declared as a class attribute,
 | 
        
           |  |  | 64 |      * See the class jsdoc for more details on why.
 | 
        
           |  |  | 65 |      *
 | 
        
           |  |  | 66 |      * @param {StateManager} stateManager the current state manager
 | 
        
           |  |  | 67 |      * @param {array} sectionIds the list of section ids
 | 
        
           |  |  | 68 |      */
 | 
        
           |  |  | 69 |     sectionUnhighlight = async function(stateManager, sectionIds) {
 | 
        
           |  |  | 70 |         const logEntry = this._getLoggerEntry(
 | 
        
           |  |  | 71 |             stateManager,
 | 
        
           |  |  | 72 |             'section_unhighlight',
 | 
        
           |  |  | 73 |             sectionIds,
 | 
        
           |  |  | 74 |             {component: 'format_topics'}
 | 
        
           |  |  | 75 |         );
 | 
        
           |  |  | 76 |         const course = stateManager.get('course');
 | 
        
           |  |  | 77 |         this.sectionLock(stateManager, sectionIds, true);
 | 
        
           |  |  | 78 |         const updates = await this._callEditWebservice('section_unhighlight', course.id, sectionIds);
 | 
        
           |  |  | 79 |         stateManager.processUpdates(updates);
 | 
        
           |  |  | 80 |         this.sectionLock(stateManager, sectionIds, false);
 | 
        
           |  |  | 81 |         stateManager.addLoggerEntry(await logEntry);
 | 
        
           |  |  | 82 |     };
 | 
        
           |  |  | 83 | }
 | 
        
           |  |  | 84 |   | 
        
           |  |  | 85 | export const init = () => {
 | 
        
           |  |  | 86 |     const courseEditor = getCurrentCourseEditor();
 | 
        
           |  |  | 87 |     // Some plugin (activity or block) may have their own mutations already registered.
 | 
        
           |  |  | 88 |     // This is why we use addMutations instead of setMutations here.
 | 
        
           |  |  | 89 |     courseEditor.addMutations(new TopicsMutations());
 | 
        
           |  |  | 90 |     // Add direct mutation content actions.
 | 
        
           |  |  | 91 |     CourseActions.addActions({
 | 
        
           |  |  | 92 |         sectionHighlight: 'sectionHighlight',
 | 
        
           |  |  | 93 |         sectionUnhighlight: 'sectionUnhighlight',
 | 
        
           |  |  | 94 |     });
 | 
        
           |  |  | 95 | };
 |