forked from davidpanderson/science_united
-
Notifications
You must be signed in to change notification settings - Fork 0
/
su_prefs.php
401 lines (365 loc) · 11.4 KB
/
su_prefs.php
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2019 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
require_once("../inc/util.inc");
require_once("../inc/keywords.inc");
require_once("su_db.inc");
require_once("su.inc");
// Interactive page for editing keyword prefs
//
// The PHP generates rows for all keywords, and hides the appropriate ones.
// The Javascript hides and unhides rows as needed.
//
// element IDs for keyword K
//
// rowK: the <tr> row element
// textK: the plus or minus sign, or spaces
// radioK: the radio buttons
// for each kw K
// nprefs(K) = how many descendants of K have nonzero pref
// expanded(K): true if user chose to expand K
// persists if ancestors are contracted;
// doesn't persist if nprefs becomes nonzero
//
// actions:
// click on expand or contract: set or clear expanded(K)
// radio: recompute nprefs for all ancestors
//
// render:
// for each terminal node K
// if nprefs(parent)
// unhide
// else if all ancesters are either nprefs<>0 or expanded
// unhide
// else
// hide
//
// for each nonterminal node K
// if nprefs(K)
// expanded=true
// unhide
// button=none
// else if nprefs(parent)
// unhide
// set button according to expanded
// else if all ancestors are expanded
// set button according to expanded
// else
// hide
// for each keyword K:
// - generate list of K's children
//
function keyword_setup($uprefs) {
global $job_keywords;
foreach ($job_keywords as $id=>$k) {
$job_keywords[$id]->children = array();
$job_keywords[$id]->expand = 0;
}
foreach ($job_keywords as $id=>$k) {
if (!$k->parent) continue;
$job_keywords[$k->parent]->children[] = $id;
}
foreach ($uprefs as $id=>$n) {
while (1) {
$id = $job_keywords[$id]->parent;
if (!$id) break;
$job_keywords[$id]->expand = true;
}
}
}
// output a keyword and (recursively) its descendants.
// If its parent is expanded, show it, else hide
//
function generate_html_kw($id, $uprefs) {
global $job_keywords;
$kw = $job_keywords[$id];
$u = $uprefs[$id];
$yes_checked = ($u == KW_YES)?"checked":"";
$no_checked = ($u == KW_NO)?"checked":"";
$maybe_checked = ($u == KW_MAYBE)?"checked":"";
echo sprintf('<tr id="%s" hidden>%s', "row$id", "\n");
echo sprintf(' <td width=50%% id="%s"></td>%s', "text$id", "\n");
echo sprintf(' <td><input onclick="radio(%d, 1)" type="radio" name="%s" id="%s" value="%d" %s></td>%s',
$id, "radio$id", "radio$id"."_0", KW_YES, $yes_checked, "\n"
);
echo sprintf(' <td><input onclick="radio(%d, 0)" type="radio" name="%s" id="%s" value="%d" %s></td>%s',
$id, "radio$id", "radio$id"."_1", KW_MAYBE, $maybe_checked, "\n"
);
echo sprintf(' <td><input onclick="radio(%d, -1)" type="radio" name="%s" id="%s" value="%d" %s></td>%s',
$id, "radio$id", "radio$id"."_2", KW_NO, $no_checked, "\n"
);
echo "</tr>\n";
foreach ($kw->children as $k) {
generate_html_kw($k, $uprefs);
}
}
function generate_html_category($category, $uprefs) {
global $job_keywords;
row_heading_array(array(
'',
tra('Prefer'),
tra('As needed'),
tra('Never')),
null,
'bg-default'
);
foreach ($job_keywords as $id=>$k) {
if ($k->category != $category) continue;
if ($k->parent) continue;
generate_html_kw($id, $uprefs);
}
}
function generate_javascript($uprefs) {
global $job_keywords;
echo "<script>
var ids = new Array;
var names = new Array;
var levels = new Array;
var parent = new Array;
var radio_value = new Array;
";
foreach ($job_keywords as $id=>$k) {
$val = $uprefs[$id];
echo "
names[$id] = '$k->name';
levels[$id] = $k->level;
parent[$id] = $k->parent;
radio_value[$id] = $val;
ids.push($id);
";
}
echo sprintf("var nkws = %d;\n", count($job_keywords));
echo <<<EOT
var rows = new Array;
var texts = new Array;
var expanded = new Array;
var terminal = new Array;
var nprefs = new Array;
// initialize stuff
for (i=0; i<nkws; i++) {
terminal[ids[i]] = true;
nprefs[ids[i]] = 0;
}
for (i=0; i<nkws; i++) {
var j = ids[i];
var rowid = "row"+j;
var textid = "text"+j;
rows[j] = document.getElementById(rowid);
texts[j] = document.getElementById(textid);
if (parent[j]) {
terminal[parent[j]] = false;
}
expanded[j] = false;
if (radio_value[j]) {
k = j;
while (1) {
k = parent[k];
if (!k) break;
nprefs[k]++;
}
}
}
// Firefox doesn't set radio buttons correctly.
//
for (i=0; i<nkws; i++) {
var j = ids[i];
if (!terminal[j]) continue;
for (k=0; k<3; k++) {
id = "radio"+j+"_"+k;
r = document.getElementById(id);
//console.log(id, r);
r.checked= (k == 1-radio_value[j]);
}
}
var font_size = [120, 108, 92, 80];
var indent = [0, 1.3, 2.6, 3.9];
var button_indent = 1.0;
// -1: show "contract" button
// 0: no button
// 1: show "expand" button
//
function set_expand(k, val) {
//console.log('set_expand ', k, val);
var level = levels[k];
var x = '<span style="font-size:'+font_size[level]+'%">';
x += '<span style="padding-left:'+indent[level]+'em"/>';
if (val < 0) {
x += '<a onclick="expand_contract('+k+')" id="t'+k+'" href=#/>⊟</a> ';
} else if (val == 0) {
x += '<span style="padding-left:'+button_indent+'em"/>';
} else {
x += '<a onclick="expand_contract('+k+')" id="t'+k+'" href=#/>⊞</a> ';
}
x += names[k];
texts[k].innerHTML = x;
}
function radio(k, val) {
//console.log('radio ', k, val, parent[k]);
old_val = radio_value[k];
radio_value[k] = val;
inc = 0;
if (val && !old_val) inc = 1;
if (!val && old_val) inc = -1;
if (inc) {
while (1) {
k = parent[k];
if (!k) break;
nprefs[k] += inc;
}
}
render();
}
// click on expand/contract link
//
function expand_contract(k) {
expanded[k] = !expanded[k];
set_expand(k, expanded[k]?-1:1);
var h = expanded[k]?false:true;
//console.log('expand_contract ', k, h);
render();
return false;
}
// return true if all ancestrors of i are expanded or nprefs>0
//
function ancestors_expanded(i) {
while (1) {
i = parent[i];
if (!i) break;
if (!nprefs[i] && !expanded[i]) return false;
}
return true;
}
function render() {
for (i=0; i<nkws; i++) {
j = ids[i];
if (terminal[j]) {
set_expand(j, 0);
if (nprefs[parent[j]]>0 || ancestors_expanded(j)) {
rows[j].hidden = false;
} else {
rows[j].hidden = true;
}
} else {
//console.log("nprefs ", j, nprefs[j]);
if (nprefs[j]) {
expanded[j] = true;
rows[j].hidden = false;
set_expand(j, 0);
} else {
p = parent[j];
if (p) {
if (nprefs[parent[j]]>0 || ancestors_expanded(j)) {
rows[j].hidden = false;
set_expand(j, expanded[j]?-1:1);
} else {
rows[j].hidden = true;
}
} else {
rows[j].hidden = false;
set_expand(j, expanded[j]?-1:1);
}
}
}
}
}
render();
EOT;
echo "</script>\n";
}
function prefs_edit_form($user, $show_saved) {
global $job_keywords;
$ukws = SUUserKeyword::enum("user_id = $user->id");
// convert user prefs to a map id=>-1/0/2
//
$uprefs = array();
foreach ($job_keywords as $id=>$kw) {
$uprefs[$id] = 0;
}
foreach ($ukws as $ukw) {
$uprefs[$ukw->keyword_id] = $ukw->yesno;
}
keyword_setup($uprefs);
page_head(tra("Science and location preferences"));
if ($show_saved) {
echo '<span class="text-success">'.tra("Preferences saved.").'</span><p><p>';
}
echo tra("Select science areas and locations you do or don't want to support. Click %1 for more detail, %2 for less. When done, click the Save button at the bottom.",
"<a>⊞</a>",
"<a>⊟</a>"
);
echo "
<p>
";
form_start("su_prefs.php");
form_input_hidden('action', 'submit');
echo "<h3>".tra("Science areas")."</h3>\n";
start_table("table-striped");
generate_html_category(KW_CATEGORY_SCIENCE, $uprefs);
end_table();
echo "<h3>".tra("Locations")."</h3>\n";
start_table("table-striped");
generate_html_category(KW_CATEGORY_LOC, $uprefs);
end_table();
echo '<button type="submit" class="btn btn-success">'.tra("Save").'</button>';
form_end();
generate_javascript($uprefs);
page_tail();
}
// return current user setting for the given keyword
//
function get_cur_val($kw_id, $ukws) {
foreach ($ukws as $ukw) {
if ($ukw->keyword_id != $kw_id) continue;
return $ukw->yesno;
}
return KW_MAYBE;
}
function prefs_edit_action($user) {
global $job_keywords;
$ukws = SUUserKeyword::enum("user_id=$user->id");
foreach ($job_keywords as $id=>$kw) {
$name = "radio$id";
$val = get_int($name, true);
$cur_val = get_cur_val($id, $ukws);
if ($val == $cur_val) continue;
switch ($val) {
case KW_NO:
case KW_YES:
if ($cur_val == KW_MAYBE) {
SUUserKeyword::insert("(user_id, keyword_id, yesno) values ($user->id, $id, $val)");
} else {
SUUserKeyword::update("yesno=$val where user_id=$user->id and keyword_id=$id");
}
break;
case KW_MAYBE:
$ukw = new SUUserKeyword;
$ukw->keyword_id = $id;
$ukw->user_id = $user->id;
$ukw->delete();
break;
}
}
}
$user = get_logged_in_user();
$action = get_str('action', true);
if ($action == "submit") {
prefs_edit_action($user);
prefs_edit_form($user, true);
} else {
prefs_edit_form($user, false);
}
?>