MDL-66828 core: Add toast-style notifications
[moodle.git] / mod / forum / amd / src / local / grades / grader.js
CommitLineData
bae67469
MM
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 * This module will tie together all of the different calls the gradable module will make.
18 *
19 * @module mod_forum/local/grades/grader
20 * @package mod_forum
21 * @copyright 2019 Mathew May <mathew.solutions>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24import Templates from 'core/templates';
25// TODO import Notification from 'core/notification';
26import Selectors from './local/grader/selectors';
27import * as UserPicker from './local/grader/user_picker';
28import {createLayout as createFullScreenWindow} from 'mod_forum/local/layout/fullscreen';
f281c616 29import getGradingPanelFunctions from './local/grader/gradingpanel';
bae67469
MM
30
31const templateNames = {
32 grader: {
33 app: 'mod_forum/local/grades/grader',
34 },
35};
36
37const displayUserPicker = (root, html) => {
38 const pickerRegion = root.querySelector(Selectors.regions.pickerRegion);
39 Templates.replaceNodeContents(pickerRegion, html, '');
40};
41
f281c616
AN
42const fetchContentFromRender = (html, js) => {
43 return [html, js];
44};
45
46const getUpdateUserContentFunction = (root, getContentForUser, getGradeForUser) => {
bae67469
MM
47 return async(user) => {
48 const [
f281c616
AN
49 [html, js],
50 userGrade,
bae67469 51 ] = await Promise.all([
f281c616
AN
52 getContentForUser(user.id).then(fetchContentFromRender),
53 getGradeForUser(user.id),
bae67469
MM
54 ]);
55 Templates.replaceNodeContents(root.querySelector(Selectors.regions.moduleReplace), html, js);
f281c616
AN
56
57 const [
58 gradingPanelHtml,
59 gradingPanelJS
60 ] = await Templates.render(userGrade.templatename, userGrade.grade).then(fetchContentFromRender);
61 Templates.replaceNodeContents(root.querySelector(Selectors.regions.gradingPanel), gradingPanelHtml, gradingPanelJS);
bae67469
MM
62 };
63};
64
65const registerEventListeners = (graderLayout) => {
66 const graderContainer = graderLayout.getContainer();
67 graderContainer.addEventListener('click', (e) => {
68 if (e.target.closest(Selectors.buttons.toggleFullscreen)) {
69 e.stopImmediatePropagation();
70 e.preventDefault();
71 graderLayout.toggleFullscreen();
72 } else if (e.target.closest(Selectors.buttons.closeGrader)) {
73 e.stopImmediatePropagation();
74 e.preventDefault();
75
76 graderLayout.close();
77 }
78 });
79};
80
f281c616
AN
81const getSaveUserGradeFunction = (root, setGradeForUser) => {
82 return user => {
83 return setGradeForUser(user.id, root.querySelector(Selectors.regions.gradingPanel));
84 };
85};
86
bae67469 87// Make this explicit rather than object
f281c616 88export const launch = async(getListOfUsers, getContentForUser, getGradeForUser, setGradeForUser, {
d3b890f8 89 initialUserId = 0, moduleName
bae67469
MM
90} = {}) => {
91
92 const [
93 graderLayout,
94 graderHTML,
95 userList,
96 ] = await Promise.all([
97 createFullScreenWindow({fullscreen: false, showLoader: false}),
d3b890f8 98 Templates.render(templateNames.grader.app, {moduleName: moduleName}),
bae67469
MM
99 getListOfUsers(),
100 ]);
101 const graderContainer = graderLayout.getContainer();
102
103 Templates.replaceNodeContents(graderContainer, graderHTML, '');
104 registerEventListeners(graderLayout);
f281c616
AN
105 const updateUserContent = getUpdateUserContentFunction(graderContainer, getContentForUser, getGradeForUser);
106
107 const pickerHTML = await UserPicker.buildPicker(
108 userList,
109 initialUserId,
110 updateUserContent,
111 getSaveUserGradeFunction(graderContainer, setGradeForUser)
112 );
bae67469 113
bae67469
MM
114 displayUserPicker(graderContainer, pickerHTML);
115};
f281c616
AN
116
117export {getGradingPanelFunctions};