course/format/topics/amd/src/mutations.js

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