Merge branch 'MDL-80871-main' of https://github.com/andrewnicols/moodle
[moodle.git] / mod / bigbluebuttonbn / amd / src / rooms.js
blob65edc278102b4a5a9f513e80d13599b7bc370f5d
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.
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/>.
16 /**
17  * JS actions for the rooms page for mod_bigbluebuttonbn.
18  *
19  * @module      mod_bigbluebuttonbn/rooms
20  * @copyright   2021 Blindside Networks Inc
21  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22  */
24 import './actions';
25 import * as repository from './repository';
26 import * as roomUpdater from './roomupdater';
27 import {
28     exception as displayException,
29     fetchNotifications,
30 } from 'core/notification';
31 import Pending from 'core/pending';
32 import {getString} from 'core/str';
33 import {add as addToast} from 'core/toast';
34 import {eventTypes, notifyCurrentSessionEnded} from './events';
36 /**
37  * Init the room
38  *
39  * @param {Number} bigbluebuttonbnid bigblubeutton identifier
40  * @param {Number} pollInterval poll interval in miliseconds
41  */
42 export const init = (bigbluebuttonbnid, pollInterval) => {
43     const completionElement = document.querySelector('a[href*=completion_validate]');
44     if (completionElement) {
45         completionElement.addEventListener("click", event => {
46             event.preventDefault();
48             const pendingPromise = new Pending('mod_bigbluebuttonbn/completion:validate');
50             repository.completionValidate(bigbluebuttonbnid)
51                 .then(() => getString('completionvalidatestatetriggered', 'mod_bigbluebuttonbn'))
52                 .then(str => addToast(str))
53                 .then(() => pendingPromise.resolve())
54                 .catch(displayException);
55         });
56     }
58     document.addEventListener('click', e => {
59         const joinButton = e.target.closest('[data-action="join"]');
60         if (joinButton) {
61             window.open(joinButton.href, 'bigbluebutton_conference');
62             e.preventDefault();
63             // Gives the user a bit of time to go into the meeting before polling the room.
64             setTimeout(() => {
65                 roomUpdater.updateRoom(true);
66             }, pollInterval);
67         }
68     });
70     document.addEventListener(eventTypes.sessionEnded, () => {
71         roomUpdater.stop();
72         roomUpdater.updateRoom();
73         fetchNotifications();
74     });
76     window.addEventListener(eventTypes.currentSessionEnded, () => {
77         roomUpdater.stop();
78         roomUpdater.updateRoom();
79         fetchNotifications();
80     });
81     // Room update.
82     roomUpdater.start(pollInterval);
85 /**
86  * Auto close child windows when clicking the End meeting button.
87  * @param {Number} closeDelay time to wait in miliseconds before closing the window
88  */
89 export const setupWindowAutoClose = (closeDelay = 2000) => {
90     notifyCurrentSessionEnded(window.opener);
91     window.addEventListener('onbeforeunload', () => {
92             window.opener.setTimeout(() => {
93                 roomUpdater.updateRoom(true);
94             }, closeDelay);
95         },
96         {
97             once: true
98         });
99     window.close(); // This does not work as scripts can only close windows that are opened by themselves.