Passed
Pull Request — master (#4871)
by David
08:36
created

default_prefs_global()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 32
nop 0
dl 0
loc 24
rs 9.0777
c 0
b 0
f 0
1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2022 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
// functions for display and editing global preferences.
21
// Preferences are represented in two ways:
22
// - As a PHP structure (usually called $prefs)
23
//   This has fields run_if_user_active, etc.
24
// - As XML (usually called $prefs_xml)
25
//
26
// This XML has the general structure
27
// <global_preferences>
28
//    <mod_time>...</mod_time>
29
//    <run_if_user_active/>
30
//    <work_buf_min_days>1.3</work_buf_min_days>
31
//    ...
32
//    <venue name="home">
33
//       <run_if_user_active/>
34
//       ...
35
//    </venue>
36
// </global_preferences>
37
//
38
39
// Various functions are defined below for converting between these forms,
40
// and also to/from HTML form elements
41
42
include_once("../inc/prefs_util.inc");
43
include_once("../inc/translation.inc");
44
45
global $in_use_prefs;
46
global $not_in_use_prefs;
47
global $job_prefs;
48
global $disk_prefs;
49
global $net_prefs;
50
51
$in_use_prefs = [
52
    new PREF_NUM(
53
        tra("'In use' means mouse/keyboard input in last"),
54
        tra("This determines when the computer is considered 'in use'."),
55
        "idle_time_to_run",
56
        new NUM_SPEC(tra("minutes"), 1, 9999, 3),
57
        true
0 ignored issues
show
Unused Code introduced by
The call to PREF_NUM::__construct() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
    /** @scrutinizer ignore-call */ 
58
    new PREF_NUM(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
58
    ),
59
    new PREF_BOOL(
60
        tra("Suspend all computing"),
61
        tra("Check this to suspend computing and file transfers when you're using the computer."),
62
        "run_if_user_active",
63
        true, true
64
    ),
65
    new PREF_BOOL(
66
        tra("Suspend GPU computing"),
67
        tra("Check this to suspend GPU computing when you're using the computer."),
68
        "run_gpu_if_user_active",
69
        false, true
70
    ),
71
    new PREF_NUM(
72
        tra("Use at most"),
73
        // xgettext:no-php-format
74
        tra("Keep some CPUs free for other applications. Example: 75% means use 6 cores on an 8-core CPU."),
75
        "max_ncpus_pct",
76
        // xgettext:no-php-format
77
        new NUM_SPEC(tra("% of the CPUs"), 1, 100, 100)
78
    ),
79
    new PREF_NUM(
80
        tra("Use at most"),
81
        // xgettext:no-php-format
82
        tra("Suspend/resume computing every few seconds to reduce CPU temperature and energy usage. Example: 75% means compute for 3 seconds, wait for 1 second, and repeat."),
83
        "cpu_usage_limit",
84
        // xgettext:no-php-format
85
        new NUM_SPEC(tra("% of CPU time"), 1, 100, 100)
86
    ),
87
    new PREF_OPT_NUM(
88
        tra("Suspend when non-BOINC CPU usage is above"),
89
        tra("Suspend computing when your computer is busy running other programs."),
90
        "suspend_cpu_usage",
91
        new NUM_SPEC("%", 0, 100, 25)
92
    ),
93
    new PREF_NUM(
94
        tra("Use at most"),
95
        tra("Limit the memory used by BOINC when you're using the computer."),
96
        "ram_max_used_busy_pct",
97
        // xgettext:no-php-format
98
        new NUM_SPEC(tra("% of memory"), 1, 100, 50)
99
    ),
100
];
101
$not_in_use_prefs = [
102
    new PREF_NUM(
103
        tra("Use at most")."<br><font size=-2>Requires BOINC 7.20.3+</font>",
104
        // xgettext:no-php-format
105
        tra("Keep some CPUs free for other applications. Example: 75% means use 6 cores on an 8-core CPU."),
106
        "niu_max_ncpus_pct",
107
        // xgettext:no-php-format
108
        new NUM_SPEC(tra("% of the CPUs"), 1, 100, 100)
109
    ),
110
    new PREF_NUM(
111
        tra("Use at most") ."<br><font size=-2>Requires BOINC 7.20.3+</font>",
112
        // xgettext:no-php-format
113
        tra("Suspend/resume computing every few seconds to reduce CPU temperature and energy usage. Example: 75% means compute for 3 seconds, wait for 1 second, and repeat."),
114
        "niu_cpu_usage_limit",
115
        // xgettext:no-php-format
116
        new NUM_SPEC(tra("% of CPU time"), 1, 100, 100)
117
    ),
118
    new PREF_OPT_NUM(
119
        tra("Suspend when non-BOINC CPU usage is above")."<br><font size=-2>Requires BOINC 7.20.3+</font>",
120
        tra("Suspend computing when your computer is busy running other programs."),
121
        "niu_suspend_cpu_usage",
122
        new NUM_SPEC("%", 0, 100, 25)
123
    ),
124
    new PREF_NUM(
125
        tra("Use at most"),
126
        tra("Limit the memory used by BOINC when you're not using the computer."),
127
        "ram_max_used_idle_pct",
128
        // xgettext:no-php-format
129
        new NUM_SPEC(tra("% of memory"), 1, 100, 90)
130
    ),
131
    new PREF_OPT_NUM(
132
        tra("Suspend when no mouse/keyboard input in last"),
133
        tra("This allows some computers to enter low-power mode when not in use."),
134
        "suspend_if_no_recent_input",
135
        new NUM_SPEC(tra("minutes"), 0, 9999, 0, 1, 60)
136
    ),
137
];
138
$job_prefs = [
139
    new PREF_BOOL(
140
        tra("Suspend when computer is on battery"),
141
        tra("Check this to suspend computing on portables when running on battery power."),
142
        "run_on_batteries",
143
        false, true
144
    ),
145
    new PREF_NUM(
146
        tra("Switch between tasks every"),
147
        tra("If you run several projects, BOINC may switch between them this often."),
148
        "cpu_scheduling_period_minutes",
149
        new NUM_SPEC(tra("minutes"), 1, 9999, 60)
150
    ),
151
    new PREF_NUM(
152
        tra("Request tasks to checkpoint at most every"),
153
        tra("This controls how often tasks save their state to disk, so that later they can be continued from that point."),
154
        "disk_interval",
155
        new NUM_SPEC(tra("seconds"), 0, 9999999, 60)
156
    ),
157
    new PREF_BOOL(
158
        tra("Leave non-GPU tasks in memory while suspended"),
159
        tra("If checked, suspended tasks stay in memory, and resume with no work lost. If unchecked, suspended tasks are removed from memory, and resume from their last checkpoint."),
160
        "leave_apps_in_memory",
161
        false
162
    ),
163
    new PREF_NUM(
164
        tra("Store at least"),
165
        tra("Store at least enough tasks to keep the computer busy for this long."),
166
        "work_buf_min_days",
167
        new NUM_SPEC(tra("days of work"), 0, 10, .1)
168
    ),
169
    new PREF_NUM(
170
        tra("Store up to an additional"),
171
        tra("Store additional tasks above the minimum level.  Determines how much work is requested when contacting a project."),
172
        "work_buf_additional_days",
173
        new NUM_SPEC(tra("days of work"), 0, 10, .5)
174
    ),
175
    new PREF_HOUR_RANGE(
176
        tra("Compute only between"),
177
        tra("Compute only during a particular period each day."),
178
        "start_hour", "end_hour"
179
    ),
180
];
181
182
$dp = get_disk_space_config();
183
184
$disk_prefs = array(
185
    new PREF_OPT_NUM(
186
        tra("Use no more than"),
187
        tra("Limit the total amount of disk space used by BOINC."),
188
        "disk_max_used_gb",
189
        new NUM_SPEC(tra("GB"), 0, 9999999, $dp->disk_max_used_gb, 1, 100)
190
    ),
191
    new PREF_OPT_NUM(
192
        tra("Leave at least"),
193
        tra("Limit disk usage to leave this much free space on the volume where BOINC stores data."),
194
        "disk_min_free_gb",
195
        new NUM_SPEC(tra("GB free"), 0, 9999999, $dp->disk_min_free_gb, 1, 1)
196
    ),
197
    new PREF_OPT_NUM(
198
        tra("Use no more than"),
199
        tra("Limit the percentage of disk space used by BOINC on the volume where it stores data."),
200
        "disk_max_used_pct",
201
        // xgettext:no-php-format
202
        new NUM_SPEC(tra("% of total"), 0, 100, $dp->disk_max_used_pct)
203
    ),
204
    new PREF_NUM(
205
        tra("Page/swap file: use at most"),
206
        tra("Limit the swap space (page file) used by BOINC."),
207
        "vm_max_used_pct",
208
        // xgettext:no-php-format
209
        new NUM_SPEC(tra("%"), 1, 100, 75)
210
    ),
211
);
212
213
$net_prefs = array(
214
    new PREF_OPT_NUM(
215
        tra("Limit download rate to"),
216
        tra("Limit the download rate of file transfers."),
217
        "max_bytes_sec_down",
218
        new NUM_SPEC(tra("KB/second"), 0, 9999999, 0, 1024, 100)
219
    ),
220
    new PREF_OPT_NUM(
221
        tra("Limit upload rate to"),
222
        tra("Limit the upload rate of file transfers."),
223
        "max_bytes_sec_up",
224
        new NUM_SPEC(tra("KB/second"), 0, 9999999, 0, 1024, 100)
225
    ),
226
    new PREF_NUM2(
227
        tra("Limit usage to"),
228
        tra("Example: BOINC should transfer at most 2000 MB of data every 30 days."),
229
        "daily_xfer_limit_mb",
230
        "daily_xfer_period_days",
231
        new NUM_SPEC(tra("MB every"), 0, 9999999, 0, 1, 10000),
232
        new NUM_SPEC(tra("days"), 0, 9999999, 0, 1, 30)
233
    ),
234
    new PREF_HOUR_RANGE(
235
        tra("Transfer files only between"),
236
        tra("Transfer files only during a particular period each day."),
237
        "net_start_hour", "net_end_hour"
238
    ),
239
    new PREF_BOOL(
240
        tra("Skip data verification for image files"),
241
        tra("Check this only if your Internet provider modifies image files. Skipping verification reduces the security of BOINC."),
242
        "dont_verify_images",
243
        false
244
    ),
245
    new PREF_BOOL(
246
        tra("Confirm before connecting to Internet"),
247
        tra("Useful only if you have a modem, ISDN or VPN connection."),
248
        "confirm_before_connecting",
249
        false
250
    ),
251
    new PREF_BOOL(
252
        tra("Disconnect when done"),
253
        tra("Useful only if you have a modem, ISDN or VPN connection."),
254
        "hangup_if_dialed",
255
        false
256
    ),
257
);
258
259
define("IN_USE_DESC", tra("When computer is in use"));
260
define("NOT_IN_USE_DESC", tra("When computer is not in use"));
261
define("JOBS_DESC", tra("General"));
262
define("DISK_DESC", tra("Disk"));
263
define("NET_DESC", tra("Network"));
264
265
// These texts are used in multiple places in prefs_edit.php and add_venue.php
266
define("PREFS_FORM_DESC1", tra("These preferences apply to all the BOINC projects in which you participate.")."<br><br>");
267
define("PREFS_FORM_ERROR_DESC",
268
    tra(
269
        "%1 Unable to update preferences. %2 The values marked in red below were out of range or not numeric.",
270
        "<strong>",
271
        "</strong>"
272
    ).
273
    "<br><br>"
274
);
275
276
global $text;
277
global $parse_result;
278
global $top_parse_result;
279
global $venue_name;
280
281
// get default settings for disk space usage so the default user
282
// preferences match the settings used by the scheduler.
283
// Defaults are set if the tags are missing, they depend on
284
// which scheduler is running:
285
// - 'old' has the default hardcoded 
286
// - 'new' uses config settings
287
// if running the old scheduler, set <scheduler_disk_space_check_hardcoded>
288
// in config.xml so the right default is set for minimum free disk space
289
// 
290
function get_disk_space_config() {
291
    global $config;
292
    $config = get_config();
293
    $dp = new StdClass;
294
    $dp->disk_max_used_gb = parse_config($config, "<default_disk_max_used_gb>");
295
    $dp->disk_max_used_pct = parse_config($config, "<default_disk_max_used_pct>");
296
    $dp->disk_min_free_gb = parse_config($config, "<default_disk_min_free_gb>");
297
    // set some defaults if not found
298
    if (!$dp->disk_max_used_gb) $dp->disk_max_used_gb = 0;  // no limit
299
    if (!$dp->disk_max_used_pct) $dp->disk_max_used_pct = 90; // 90 percent
300
    if (!$dp->disk_min_free_gb) $dp->disk_min_free_gb = 1;   // 1 GB
301
    // set mininimum free space scheduler allows 
302
    // - depends on which scheduler is running
303
    $dp->new_sched_flag = 1;
304
    $dp->sched_disk_min_free_gb = $dp->disk_min_free_gb;
305
    if (parse_config($config, "scheduler_disk_space_check_hardcoded>")) {
306
        $dp->new_sched_flag = 0;
307
        $dp->sched_disk_min_free_gb = 0;
308
    }    
309
       
310
    return $dp;
311
}
312
313
function group_header($t) {
314
    echo "<tr><th class=\"bg-info\">$t</th><td class=\"bg-info\" colspan=4><br></td></tr>\n";
315
}
316
317
// functions to parse preferences XML into a struct
318
//
319
320
function element_start_global($parser, $name, $attrs) {
321
    global $top_parse_result;
322
    global $parse_result;
323
    global $text;
324
    global $venue_name;
325
326
    switch($name) {
327
    case "venue":
328
        if (array_key_exists("name", $attrs)) {
329
            $venue_name = $attrs["name"];
330
        } else {
331
            $venue_name = "home";
332
        }
333
        $top_parse_result = $parse_result;
334
        $parse_result = default_prefs_global();
335
        break;
336
    }
337
    $text = "";
338
}
339
340
function element_end_global($parser, $name) {
341
    global $text;
342
    global $parse_result;
343
    global $top_parse_result;
344
    global $venue_name;
345
    global $in_use_prefs;
346
    global $not_in_use_prefs;
347
    global $job_prefs;
348
    global $disk_prefs;
349
    global $net_prefs;
350
351
    foreach ($in_use_prefs as $p) {
352
        if ($p->xml_parse($parse_result, $name, $text)) {
353
            return;
354
        }
355
    }
356
    foreach ($not_in_use_prefs as $p) {
357
        if ($p->xml_parse($parse_result, $name, $text)) {
358
            return;
359
        }
360
    }
361
    foreach ($job_prefs as $p) {
362
        if ($p->xml_parse($parse_result, $name, $text)) {
363
            return;
364
        }
365
    }
366
    foreach ($disk_prefs as $p) {
367
        if ($p->xml_parse($parse_result, $name, $text)) {
368
            return;
369
        }
370
    }
371
    foreach ($net_prefs as $p) {
372
        if ($p->xml_parse($parse_result, $name, $text)) {
373
            return;
374
        }
375
    }
376
    switch($name) {
377
    case "venue":
378
        $top_parse_result->$venue_name = $parse_result;
379
        $parse_result = $top_parse_result;
380
        break;
381
    case "mod_time":
382
        $parse_result->mod_time = $text;
383
        break;
384
    case "global_preferences":
0 ignored issues
show
Coding Style introduced by
Empty CASE statements are not allowed
Loading history...
385
        break;
386
    default:
0 ignored issues
show
Coding Style introduced by
DEFAULT keyword must be indented 4 spaces from SWITCH keyword
Loading history...
Coding Style introduced by
DEFAULT case must have a breaking statement
Loading history...
387
        //echo "Unknown tag: $name\n";
388
    }
389
}
390
391
function char_handler($parser, $x) {
392
    global $text;
393
    $text = $text.$x;
394
}
395
396
397
// state of prefs before parsing; defines prefs for new users
398
//
399
function default_prefs_global() {
400
    global $in_use_prefs;
401
    global $not_in_use_prefs;
402
    global $job_prefs;
403
    global $disk_prefs;
404
    global $net_prefs;
405
406
    $p = new StdClass;
407
    foreach ($in_use_prefs as $pref) {
408
        $pref->set_default($p);
409
    }
410
    foreach ($not_in_use_prefs as $pref) {
411
        $pref->set_default($p);
412
    }
413
    foreach ($job_prefs as $pref) {
414
        $pref->set_default($p);
415
    }
416
    foreach ($disk_prefs as $pref) {
417
        $pref->set_default($p);
418
    }
419
    foreach ($net_prefs as $pref) {
420
        $pref->set_default($p);
421
    }
422
    return $p;
423
}
424
425
// parse prefs from XML to a struct
426
//
427
428
function prefs_parse_global($prefs_xml) {
429
    global $parse_result;
430
    $parse_result = default_prefs_global();
431
    $xml_parser = xml_parser_create();
432
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
433
    xml_set_element_handler($xml_parser, "element_start_global", "element_end_global");
434
    xml_set_character_data_handler($xml_parser, "char_handler");
435
    xml_parse($xml_parser, $prefs_xml, 1);
436
    return $parse_result;
437
}
438
439
// Display all venues as columns next to descriptions
440
//
441
function prefs_show_columns_global($prefs) {
442
    global $in_use_prefs;
443
    global $not_in_use_prefs;
444
    global $job_prefs;
445
    global $disk_prefs;
446
    global $net_prefs;
447
448
    row_top(IN_USE_DESC);
449
    foreach ($in_use_prefs as $p) {
450
        $p->show_cols($prefs);
451
    }
452
    row_top(NOT_IN_USE_DESC);
453
    foreach ($not_in_use_prefs as $p) {
454
        $p->show_cols($prefs);
455
    }
456
    row_top(JOBS_DESC);
457
    foreach ($job_prefs as $p) {
458
        $p->show_cols($prefs);
459
    }
460
    row_top(DISK_DESC);
461
    foreach ($disk_prefs as $p) {
462
        $p->show_cols($prefs);
463
    }
464
    row_top(NET_DESC);
465
    foreach ($net_prefs as $p) {
466
        $p->show_cols($prefs);
467
    }
468
    row_links("global", $prefs);
469
}
470
471
function prefs_show_global($prefs) {
472
    global $in_use_prefs;
473
    global $not_in_use_prefs;
474
    global $job_prefs;
475
    global $disk_prefs;
476
    global $net_prefs;
477
478
    row1(IN_USE_DESC);
479
    foreach ($in_use_prefs as $p) {
480
        $p->show($prefs);
481
    }
482
    row1(NOT_IN_USE_DESC);
483
    foreach ($not_in_use_prefs as $p) {
484
        $p->show($prefs);
485
    }
486
    row1(JOBS_DESC);
487
    foreach ($job_prefs as $p) {
488
        $p->show($prefs);
489
    }
490
    row1(DISK_DESC);
491
    foreach ($disk_prefs as $p) {
492
        $p->show($prefs);
493
    }
494
    row1(NET_DESC);
495
    foreach ($net_prefs as $p) {
496
        $p->show($prefs);
497
    }
498
}
499
500
function subset_name($subset) {
501
    if ($subset == "global") return tra("Computing");
502
    return PROJECT;
503
}
504
505
function prefs_display_venue($prefs, $venue, $subset) {
506
    global $g_logged_in_user;
507
    $tokens = url_tokens($g_logged_in_user->authenticator);
508
    $x = false;
509
    if (isset($prefs->$venue)) $x = $prefs->$venue;
510
511
    if ($x) {
512
        start_table();
513
        row_heading(tra("Separate preferences for %1", $venue), 'bg-info');
514
        if ($subset == "global") {
515
            prefs_show_global($x);
516
        } else {
517
            prefs_show_project($x);
518
            prefs_show_project_specific($x);
519
        }
520
        row2("<br>",
521
            "<a href=prefs_edit.php?venue=$venue&subset=$subset$tokens>".tra("Edit preferences")."</a>
522
            | <a href=prefs_remove.php?venue=$venue&subset=$subset$tokens>".tra("Remove")."</a>
523
        ");
524
        end_table();
525
        echo "</td></tr>\n";
526
    } else {
527
        echo "<p>";
528
        show_button("add_venue.php?venue=$venue&subset=$subset$tokens",
529
            tra("Add separate preferences for %1", $venue),
530
            tra("Add separate preferences for %1", $venue),
531
            "btn-primary btn-sm"
532
        );
533
    }
534
}
535
536
function print_prefs_display_global($user, $columns=false) {
537
    $global_prefs = prefs_parse_global($user->global_prefs);
538
539
    echo tra("These settings apply to all computers using this account except")
540
        ."<ul><li>"
541
        .tra("computers where you have set preferences locally using the BOINC Manager")
542
        ."<li>"
543
        .tra("Android devices")
544
        ."</ul>
545
    ";
546
    $switch_link = " <font size=\"-1\"><a href=prefs.php?subset=global&cols=". (int)!$columns .">".tra("(Switch view)")."</a></font>";
547
    if ($columns) {
548
        echo "<h3>".tra("Combined preferences").$switch_link."</h3>";
549
        start_table();
550
        prefs_show_columns_global($global_prefs);
551
        end_table();
552
553
    } else {
554
        if (isset($global_prefs->home) || isset($global_prefs->work) || isset($global_prefs->school)) {
555
            echo "<h3>".tra("Primary (default) preferences").$switch_link."</h3>";
556
        }
557
        start_table();
558
        prefs_show_global($global_prefs);
559
        $tokens = url_tokens($user->authenticator);
560
        row2("<br>",
561
            button_text("prefs_edit.php?subset=global$tokens",
562
                tra("Edit preferences")
563
            )
564
        );
565
        end_table();
566
567
        prefs_display_venue($global_prefs, "home", "global");
568
        prefs_display_venue($global_prefs, "school", "global");
569
        prefs_display_venue($global_prefs, "work", "global");
570
    }
571
    if (isset($global_prefs->mod_time)) {
572
        echo "<p>".tra("Preferences last modified:")." ".pretty_time_str($global_prefs->mod_time)."<p>\n";
573
    }
574
}
575
576
// This functions is used in prefs_edit.php to be able to display
577
// the prefs form in case of an error again.
578
// $error and $project_error should be an object of the form:
579
// $error->idle_time_to_run=true if an error occurred
580
// otherwise false
581
//
582
function print_prefs_form(
583
    $action, $subset, $venue, $user, $prefs, $cols, $error=false,
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
584
    $project_error=false
585
){
0 ignored issues
show
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found 0 spaces
Loading history...
586
    if ($action == "add") {
587
        $script = "add_venue.php";
588
        $submit_value = tra("Add preferences");
589
    }
590
    if ($action == "edit") {
591
        $script = "prefs_edit.php";
592
        $submit_value = tra("Update preferences");
593
    }
594
    echo "<form class=\"form-inline\" action=$script><input type=hidden name=subset value=$subset>
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $script does not seem to be defined for all execution paths leading up to this point.
Loading history...
595
        ".form_tokens($user->authenticator);
596
    if ($venue) {
597
        echo "<input type=hidden name=venue value=$venue>\n";
598
    }
599
    if ($cols) {
600
        echo "<input type=hidden name=cols value=$cols>\n";
601
    }
602
    
603
    start_table();
604
    if ($subset == "global") {
605
        prefs_form_global($user, $prefs, $error);
606
    } else {
607
        prefs_form_project($prefs, $error);
608
        if (!$venue) {
609
            prefs_form_privacy($user);
610
            prefs_form_consent($user);
611
            venue_form($user);
612
        }
613
        prefs_form_project_specific($prefs->project_specific, $project_error);
614
    }
615
616
    row2("", "<input class=\"btn btn-success\" type=submit value=\"$submit_value\" name=\"action\">");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $submit_value does not seem to be defined for all execution paths leading up to this point.
Loading history...
617
    end_table();
618
    echo "</form>\n";
619
}
620
621
////////////////////////////////////////////
622
//
623
// Functions to display preference subsets as forms
624
//
625
function prefs_form_global($user, $prefs, $error=false) {
626
    global $in_use_prefs;
627
    global $not_in_use_prefs;
628
    global $job_prefs;
629
    global $disk_prefs;
630
    global $net_prefs;
631
632
    row1(IN_USE_DESC);
633
    foreach ($in_use_prefs as $p) {
634
        $p->show_form_row($prefs, $error);
635
    }
636
    row1(NOT_IN_USE_DESC);
637
    foreach ($not_in_use_prefs as $p) {
638
        $p->show_form_row($prefs, $error);
639
    }
640
    row1(JOBS_DESC);
641
    foreach ($job_prefs as $p) {
642
        $p->show_form_row($prefs, $error);
643
    }
644
    row1(DISK_DESC);
645
    foreach ($disk_prefs as $p) {
646
        $p->show_form_row($prefs, $error);
647
    }
648
    row1(NET_DESC);
649
    foreach ($net_prefs as $p) {
650
        $p->show_form_row($prefs, $error);
651
    }
652
}
653
654
// returns a set of translated yes/no radio buttons for editing prefs forms
655
// Example: prefs_form_radio_buttons("allow_beta_work", $user->allow_beta_work);
656
//
657
// @param string $name name of the radio buttons
658
// @param bool $yesno toggles the preset of the buttons; true=yes, false=no
659
//
660
function prefs_form_radio_buttons($name, $yesno) {
661
    $rb = tra("yes")." <input type=radio name=$name value=yes "
662
        .($yesno?"checked":"")
663
        ."> ".tra("no")." <input type=radio name=$name value=no "
664
        .($yesno?"":"checked")
665
        .">\n";
666
    return $rb;
667
}
668
669
// TODO: make this a subclass of PREF
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
670
//
671
define('VENUE_DESC', tra('Default computer location'));
672
define('VENUE_TOOLTIP', tra('New computers will use this location for computing and project preferences.'));
673
674
function tooltip_row2($t, $x, $y) {
675
    echo "<tr title=\"$t\">
676
        <td ".NAME_ATTRS.">$x</td>
677
        <td ".VALUE_ATTRS.">$y</td>
678
        </tr>
679
    ";
680
}
681
function venue_show($user) {
682
    $venue = $user->venue;
683
    if ($venue =='') $venue = '---';
684
    tooltip_row2(VENUE_TOOLTIP, VENUE_DESC, $venue);
685
}
686
687
function venue_form($user) {
688
    $n=$h=$w=$s=$m='';
689
    if ($user->venue == '') $n = 'selected';
690
    if ($user->venue == 'home') $h = 'selected';
691
    if ($user->venue == 'work') $w = 'selected';
692
    if ($user->venue == 'school') $s = 'selected';
693
    tooltip_row2(
694
        VENUE_TOOLTIP,
695
        VENUE_DESC,
696
        "<select class=\"form-control input-sm\" name=default_venue>
697
        <option value=\"\" $n>---
698
        <option value=home $h>".tra("Home")."
699
        <option value=work $w>".tra("Work")."
700
        <option value=school $s>".tra("School")."
701
        </select>
702
    ");
703
}
704
705
function venue_parse_form(&$user) {
706
    $user->venue = $_GET['default_venue'];
707
}
708
709
////////////////////////////////////////////
710
//
711
// Functions to parse form elements, modifying a preferences structure
712
// prefs is preferences object to modify
713
// returns an object with errorvalues or false in success case
714
//
715
function prefs_global_parse_form(&$prefs) {
716
    global $in_use_prefs;
717
    global $not_in_use_prefs;
718
    global $job_prefs;
719
    global $disk_prefs;
720
    global $net_prefs;
721
722
    $error = false;
723
    foreach ($in_use_prefs as $p) {
724
        $p->parse_form($prefs, $error);
725
    }
726
    foreach ($not_in_use_prefs as $p) {
727
        $p->parse_form($prefs, $error);
728
    }
729
    foreach ($job_prefs as $p) {
730
        $p->parse_form($prefs, $error);
731
    }
732
    foreach ($disk_prefs as $p) {
733
        $p->parse_form($prefs, $error);
734
    }
735
    foreach ($net_prefs as $p) {
736
        $p->parse_form($prefs, $error);
737
    }
738
    return $error;
739
}
740
741
742
////////////////////////////////////////////
743
//
744
// convert prefs from structure to XML
745
//
746
function global_prefs_make_xml($prefs, $primary=true) {
747
    global $in_use_prefs;
748
    global $not_in_use_prefs;
749
    global $job_prefs;
750
    global $disk_prefs;
751
    global $net_prefs;
752
753
    $xml = "";
754
    if ($primary) {
755
        $xml = "<global_preferences>\n";
756
        $now = time();
757
        $xml = $xml."<mod_time>$now</mod_time>\n";
758
    }
759
760
    foreach ($in_use_prefs as $p) {
761
        $xml .= $p->xml_string($prefs);
762
    }
763
    foreach ($not_in_use_prefs as $p) {
764
        $xml .= $p->xml_string($prefs);
765
    }
766
    foreach ($job_prefs as $p) {
767
        $xml .= $p->xml_string($prefs);
768
    }
769
    foreach ($disk_prefs as $p) {
770
        $xml .= $p->xml_string($prefs);
771
    }
772
    foreach ($net_prefs as $p) {
773
        $xml .= $p->xml_string($prefs);
774
    }
775
776
    if (isset($prefs->home)) {
777
        $xml = $xml."<venue name=\"home\">\n".global_prefs_make_xml($prefs->home, false)."</venue>\n";
778
    }
779
    if (isset($prefs->work)) {
780
        $xml = $xml."<venue name=\"work\">\n".global_prefs_make_xml($prefs->work, false)."</venue>\n";
781
    }
782
    if (isset($prefs->school)) {
783
        $xml = $xml."<venue name=\"school\">\n".global_prefs_make_xml($prefs->school, false)."</venue>\n";
784
    }
785
    if ($primary) {
786
        $xml = $xml."</global_preferences>\n";
787
    }
788
    return $xml;
789
}
790
791
////////////////////////////////////////////
792
//
793
// Update user's prefs in database, from a given structure
794
//
795
function global_prefs_update(&$user, $prefs) {
796
    $prefs_xml = BoincDb::escape_string(global_prefs_make_xml($prefs));
797
    $retval = $user->update("global_prefs='$prefs_xml'");
798
    if (!$retval) {
799
        return 1;
800
    }
801
    $user->global_prefs = $prefs_xml;
802
    return 0;
803
}
804
805
?>
806