-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.gs
301 lines (270 loc) · 11.6 KB
/
ui.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* [ui] Automatically triggered function for opening the document (also when refreshed). Contains all frontend handles
* @return {null}
*/
function onOpen(){
Logger.log("This file was opened by: " + Session.getActiveUser().getEmail() +" ");
if(PropertiesService.getDocumentProperties().getProperty('status') == null){
ui.createMenu("Initialize KYPT Scoring System")
.addItem('Initialize Scoring System','initGate')
.addToUi();
ui.alert('This document was opened for the first time. Please Initialize with the dropdown menu above.')
return;
}
if(! initGate()){return;}
ui.createMenu("KYPT Script")
.addSubMenu(
ui.createMenu('Generate Documents')
.addItem('Draw','user_gen_draw')
.addItem('Leaderboard Status','user_gen_pf_status')
.addItem('Tournament Progression Status','user_gen_database')
.addItem('PF4 Questions Verdict','user_gen_pf4_problems')
.addItem('PF##, RM##', 'user_gen_pfrm')
.addItem('Total Summary of PF','user_gen_pf_summary')
.addItem('Finals','user_gen_final')
)
.addSubMenu(
ui.createMenu('Generate Templates')
.addItem('Write Templates','user_gen_write_template_all')
.addItem('Capture Templates','user_gen_capture_templates')
.addItem('Finals Templates','user_gen_write_template_final')
)
.addSeparator()
.addItem('Broadcast','user_broadcast')
.addSeparator()
.addSubMenu(
ui.createMenu('Developer Options')
.addItem('Internal Initialize','dev_init_internal')
.addItem('Duplicate','dev_duplicate')
.addItem('Load / Clear','dev_load')
.addItem('New Tournament Instance','dev_init_external')
.addSubMenu(ui.createMenu('Staff')
.addItem("Add Staff",'dev_add_staff')
.addItem('Clear Staff','dev_clear_staff')
)
)
.addToUi();
ui.createMenu('KYPT Chatbot')
.addItem('Activate Chatbot', 'user_show_chatbot')
.addToUi();
}
/**
* [ui] Check in the open of document of this script needs to be initialized. (Different logical flow)
*
* @return {boolean} if the gate is passed
*/
function initGate(){
if(PropertiesService.getDocumentProperties().getProperty('status') == null){
Logger.log("This file was initialized by: " + Session.getActiveUser().getEmail() +" ");
Logger.log("Internal Initialize Called (first time open)");
read_all_properties();
init_internal();
Logger.log("System Initialized Sucessfully.");
ui.alert(`Welcome to ${PropertiesService.getDocumentProperties().getProperty('category')}-${PropertiesService.getDocumentProperties().getProperty('callname')} Scoring System!\n Refresh page to access scripts.`)
// onOpen();
return false
}
else if(PropertiesService.getDocumentProperties().getProperty('status') == 'SOURCE'){
var result = promptUser(
"Source Editing Mode","Editing this file will change all future instances. Do you know what you are doing?",
ButtonSet = ui.ButtonSet.YES_NO,trueButton = ui.Button.YES
);
if(result != PropertiesService.getScriptProperties().getProperty('_devPw') &&
PropertiesService.getScriptProperties().getProperty('developers').search(Session.getActiveUser().getEmail()) == -1){
ui.alert("Authentication Unsuccessful.");
Logger.log("SOURCE authentication FAILED at: "+now);
return false
}
Logger.log("SOURCE authentication successful at: "+now);
return true
}
return true
}
/**
* [ui] Credential check on frontend for authorization in certain functions
*
* @param {string} mode supported modes: 'dev' (developer only access), 'user" (public access)
* @return{boolean} returns true if gate is passed
*/
function userGate(mode = 'user'){
Logger.log("KYPT Scoring System Script was Run : " + Session.getActiveUser().getEmail() + " as " + mode);
if(mode.search('dev') != -1){
if(Session.getActiveUser().getEmail() == ''){}
else if(PropertiesService.getScriptProperties().getProperty('developers').search(Session.getActiveUser().getEmail()) != -1){
return true
}
var result = promptUser("Developer Sign-In","Your credentials are not recognized as developer. Please enter Password.");
if(result == PropertiesService.getScriptProperties().getProperty('_devPw')){
return true
}
ui.alert("Invalid Password. Access Denied.");
Logger.log("User Failed Developer Identification");
return false;
}
else if(mode.search('user') != -1){
return true;
}
return false;
}
/**
* [ui] Wrapper for text input dialog boxes
*
* @param {string} title the title of dialog box
* @param {string} subtitle subtitle or description in dialog box
* @param {ButtonSet} buttons the button set to use
* @param {Button} trueButton the botton that corresponds to true
* @return{(boolean|string)} returns the input text if the trueButton is pressed, false if not.
*/
function promptUser(title,subtitle,buttons = ui.ButtonSet.OK_CANCEL,trueButton = ui.Button.OK){
var result = ui.prompt(title,subtitle,buttons);
if (result.getSelectedButton()==trueButton){
return result.getResponseText();
}
return false;
}
/**
* [ui] Wrapper for yes/no dialog boxes
*
* @param {string} text text to display on dialog box
* @param {ButtonSet} buttons the button set to use
* @param {Button} trueButton the botton that corresponds to true
* @return{boolean} returns if the trueButton is pressed
*/
function askUser(text,ButtonSet = ui.ButtonSet.YES_NO,trueButton = ui.Button.YES){
if(ui.alert(text,ButtonSet) == trueButton){
return true;
}
return false;
}
function htmlString_result(doc,pdf){
return '<div style="font-family:Arial; text-align:center">'+doc.getName()+'<br><br><a href = "'+
PropertiesService.getDocumentProperties().getProperty('folderURL_result')+
'" target = "_blank">Open Folder</a> <a href = "'+
doc.getUrl()+
'"target = "_blank">Open Docs</a> <a href = "'+
pdf.getDownloadUrl()+
'">Download PDF</a> </div>';
}
function user_gen_pfrm(){
if(userGate('user') == false){return false;}
// ui.alert("user_gen_pfrm() called");
var pf = promptUser("Enter PF Number","1,2,3,etc.");
var rm = promptUser("Enter Room Number","1,2,3 etc.");
var [doc,pdf] = gen_pfrm(pf,rm,undefined,true,true);
//ui.alert(`Document Successfully Generated!\n${ DriveApp.getFolderById(PropertiesService.getDocumentProperties().getProperty('folderID_result')).getUrl()}`);
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_pf_status(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_pf_status();
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_pf_summary(){
if(userGate('user') == false){return false;}
var pf = promptUser("Enter PF Number","1,2,3,etc.");
var total_rm = promptUser("Enter Total Number of Rooms","1,2,3 etc.");
var [doc,pdf] = gen_pf_summary(pf,total_rm,true,true);
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_final(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_final(undefined,true,true);
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_draw(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_draw();
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_database(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_database();
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_gen_write_template_all(){
if(userGate('user') == false){return false;}
var total_pf = promptUser("Enter Total Number of PF's","1,2,3,etc.");
var total_rm = promptUser("Enter Total Number of Rooms","1,2,3 etc.");
var [doc,pdf] = gen_write_template_all(total_pf,total_rm,true,true);
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Template Generation Successful');
}
function user_gen_write_template_final(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_final(undefined,true,true,true);
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Template Generation Successful');
}
function user_gen_capture_templates(){
if(userGate('user') == false){return false;}
//gen_capture_templates(total_pf = 4,total_rm = 6,docs = true,pdf = false)
var total_pf = promptUser("Enter Total Number of PF's","1,2,3,etc.");
var total_rm = promptUser("Enter Total Number of Rooms","1,2,3 etc.");
gen_capture_templates(total_pf,total_rm,true,false);
ui.alert(`Capture templates successfully generated. Check [TEMPLATES] folder`);
}
function user_gen_pf4_problems(){
if(userGate('user') == false){return false;}
var [doc,pdf] = gen_pf4_problems();
ui.showModalDialog(HtmlService.createHtmlOutput(htmlString_result(doc,pdf)).setWidth(420).setHeight(100), 'Document Generation Successful');
}
function user_broadcast(){
if(userGate('user') == false){return false;}
var message = promptUser("Enter Broadcast Message","Message Displayed in: '3. Leaderboard'!A29");
broadcast (message);
ui.alert('Broadcast Successful at : '+ now);
}
function user_show_chatbot() {
if(userGate('user') == false){return false;}
var html = HtmlService.createHtmlOutputFromFile('chatbot').setTitle('KYPT Chatbot');
ui.showSidebar(html);
}
function dev_init_internal(){
if(userGate('dev') == false){return false;}
init_internal();
}
function dev_init_external(){
if(userGate('dev') == false){return false;}
var category = promptUser("Enter Tournament Category","KYPT, I-YPT, etc.");
var callname = promptUser("Enter Callname","2018, 2019, etc.");
init_external (category,callname);
ui.alert("New Tournament Instance Successfully Created.")
}
function dev_duplicate(){
if(userGate('dev') == false){return false;}
var postfix = promptUser("Enter Postfix for Duplicated System","new name: current name + postfix")
if(postfix == ""){
duplicate();
}
else{duplicate(postfix);}
ui.alert("Duplication Successful.")
}
function dev_load(){
if(PropertiesService.getDocumentProperties().getProperty('status') == "SOURCE"){
ui.alert("SOUCE Doucement Cannot be Automatically Loaded / Cleared");
return false;
}
var range = promptUser("Enter Range","all,pre,fin,1,2,3,4");
var cmd = promptUser("Enter Command","load, clear");
if(cmd == null){ui.alert("No command given. Aborting.");return false;}
var srcId = '';
if(cmd.search('c') == -1){
srcId = promptUser("Enter Tournament Category","By default, KYPT2020 will be used (23 teams, 6 rooms, 4 PF's).");
if(srcId == ''){srcId = undefined;}
}
if(load(range,cmd,srcId) == 1){ui.alert(`Load / Clear Successful\nrange: ${range}\ncommand: ${cmd}`);}
else{ui.alert(`Load / Clear Failed\nrange: ${range}\ncommand: ${cmd}`);}
}
function dev_add_staff(){
if(userGate('dev') == false){return false;}
add_staff();
ui.alert("Staff Members Added");
}
function dev_clear_staff(){
if(userGate('dev') == false){return false;}
var result = askUser('Clear all staff members? They will lose all access to documents.');
if(! result){
ui.alert('Canceled');
return false;
}
clear_staff();
ui.alert("All staff members cleared");
}