MDL-66845 mod_forum: Convert user picker to class
[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';
45c0584c 27import getUserPicker from './local/grader/user_picker';
bae67469 28import {createLayout as createFullScreenWindow} from 'mod_forum/local/layout/fullscreen';
f281c616 29import getGradingPanelFunctions from './local/grader/gradingpanel';
77ee8778
AN
30import {add as addToast} from 'core/toast';
31import {get_string as getString} from 'core/str';
bae67469
MM
32
33const templateNames = {
34 grader: {
35 app: 'mod_forum/local/grades/grader',
36 },
37};
38
39const displayUserPicker = (root, html) => {
40 const pickerRegion = root.querySelector(Selectors.regions.pickerRegion);
41 Templates.replaceNodeContents(pickerRegion, html, '');
42};
43
f281c616
AN
44const fetchContentFromRender = (html, js) => {
45 return [html, js];
46};
47
48const getUpdateUserContentFunction = (root, getContentForUser, getGradeForUser) => {
bae67469
MM
49 return async(user) => {
50 const [
f281c616
AN
51 [html, js],
52 userGrade,
bae67469 53 ] = await Promise.all([
f281c616
AN
54 getContentForUser(user.id).then(fetchContentFromRender),
55 getGradeForUser(user.id),
bae67469
MM
56 ]);
57 Templates.replaceNodeContents(root.querySelector(Selectors.regions.moduleReplace), html, js);
f281c616
AN
58
59 const [
60 gradingPanelHtml,
61 gradingPanelJS
62 ] = await Templates.render(userGrade.templatename, userGrade.grade).then(fetchContentFromRender);
63 Templates.replaceNodeContents(root.querySelector(Selectors.regions.gradingPanel), gradingPanelHtml, gradingPanelJS);
bae67469
MM
64 };
65};
66
67const registerEventListeners = (graderLayout) => {
68 const graderContainer = graderLayout.getContainer();
69 graderContainer.addEventListener('click', (e) => {
70 if (e.target.closest(Selectors.buttons.toggleFullscreen)) {
71 e.stopImmediatePropagation();
72 e.preventDefault();
73 graderLayout.toggleFullscreen();
74 } else if (e.target.closest(Selectors.buttons.closeGrader)) {
75 e.stopImmediatePropagation();
76 e.preventDefault();
77
78 graderLayout.close();
79 }
80 });
81};
82
77ee8778
AN
83/**
84 * Get the function used to save a user grade.
85 *
86 * @param {Element} root The contaienr
87 * @param {Function} setGradeForUser The function that will be called.
88 * @return {Function}
89 */
f281c616 90const getSaveUserGradeFunction = (root, setGradeForUser) => {
77ee8778
AN
91 return async user => {
92 try {
93 const result = await setGradeForUser(user.id, root.querySelector(Selectors.regions.gradingPanel));
94 addToast(await getString('grades:gradesavedfor', 'mod_forum', user));
95
96 return result;
97 } catch (error) {
98 throw error;
99 }
f281c616
AN
100 };
101};
102
bae67469 103// Make this explicit rather than object
f281c616 104export const launch = async(getListOfUsers, getContentForUser, getGradeForUser, setGradeForUser, {
d3b890f8 105 initialUserId = 0, moduleName
bae67469
MM
106} = {}) => {
107
108 const [
109 graderLayout,
110 graderHTML,
111 userList,
112 ] = await Promise.all([
113 createFullScreenWindow({fullscreen: false, showLoader: false}),
d3b890f8 114 Templates.render(templateNames.grader.app, {moduleName: moduleName}),
bae67469
MM
115 getListOfUsers(),
116 ]);
117 const graderContainer = graderLayout.getContainer();
118
45c0584c
AN
119 const saveGradeFunction = getSaveUserGradeFunction(graderContainer, setGradeForUser);
120
bae67469
MM
121 Templates.replaceNodeContents(graderContainer, graderHTML, '');
122 registerEventListeners(graderLayout);
f281c616
AN
123 const updateUserContent = getUpdateUserContentFunction(graderContainer, getContentForUser, getGradeForUser);
124
45c0584c
AN
125 // Fetch the userpicker for display.
126 const userPicker = await getUserPicker(
f281c616
AN
127 userList,
128 initialUserId,
129 updateUserContent,
45c0584c 130 saveGradeFunction
f281c616 131 );
bae67469 132
45c0584c
AN
133 // Display the newly created user picker.
134 displayUserPicker(graderContainer, userPicker.rootNode);
bae67469 135};
f281c616
AN
136
137export {getGradingPanelFunctions};