Completed
Push — master ( b33fd6...031c70 )
by Seth
02:28
created

force-notifications.php ➔ titleCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once 'common.inc.php';
4
5
use Battis\DataUtilities;
6
7
function titleCase($s) {
8
    return DataUtilities::titleCase(str_replace('_', ' ', $s));
9
}
10
11
define('STEP_INSTRUCTIONS', 1);
12
define('STEP_CONFIGURE', 2);
13
define('STEP_FORCE', 3);
14
15
$toolbox->cache_pushKey(basename(__FILE__));
16
17
$step = (empty($_REQUEST['step']) ? STEP_INSTRUCTIONS : $_REQUEST['step']);
18
19
switch ($step) {
20
    case STEP_CONFIGURE:
21
        $toolbox->cache_pushKey($_REQUEST['account']);
22
        try {
23
            $users = $toolbox->api_get(
24
                "accounts/{$_REQUEST['account']}/users",
25
                [
26
                    'per_page' => 1
27
                ]
28
            );
29
            $communicationChannels = $toolbox->api_get("users/{$users[0]['id']}/communication_channels");
30
            $notifications = $toolbox->cache_get('notifications');
31
            if (empty($notifications)) {
32
                $response = $toolbox->api_get("users/{$users[0]['id']}/communication_channels/{$communicationChannels[0]['type']}/{$communicationChannels[0]['address']}/notification_preferences");
33
                $notifications = $response['notification_preferences'];
34
                $toolbox->cache_set('notifications', $notifications);
35
            }
36
            $response = $customPrefs->query("SELECT `role` FROM `users` GROUP BY `role` ORDER BY `role` ASC");
37
            $roles = [];
38
            while ($row = $response->fetch_assoc()) {
39
                $roles[] = $row['role'];
40
            }
41
42
            $toolbox->smarty_assign([
43
                'account' => $toolbox->api_get("accounts/{$_REQUEST['account']}"),
44
                'roles' => $roles,
45
                'notifications' => $notifications,
46
                'frequencies' => [
47
                    'immediately',
48
                    'daily',
49
                    'weekly',
50
                    'never'
51
                ],
52
                'formHidden' => [
53
                    'step' => STEP_FORCE,
54
                    'account' => $_REQUEST['account']
55
                ]
56
            ]);
57
            $toolbox->smarty_display(basename(__FILE__, '.php') . '/configure.tpl');
58
59
        } catch (Exception $e) {
60
            $toolbox->exceptionErrorMessage($e);
61
            $step = STEP_INSTRUCTIONS;
62
        }
63
64
        $toolbox->cache_popKey();
65
66
        /* flow into STEP_FORCE */
67
68
    case STEP_FORCE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69
70
        if ($step == STEP_FORCE) {
71
            try {
72
                $affected = 0;
73
                foreach ($toolbox->api_get("accounts/{$_REQUEST['account']}/users") as $user) {
74
                    $response = $customPrefs->query("SELECT * FROM `users` WHERE `id` = '{$user['id']}' AND `role` = '{$_REQUEST['role']}'");
75
                    if ($response && $response->num_rows > 0) {
76
                        $communicationChannels = $toolbox->api_get("users/{$user['id']}/communication_channels");
77
                        foreach ($communicationChannels as $channel) {
78
                            // FIXME not great to hard code in our domain
79
                            if ($channel['type'] == 'email' && preg_match('/.*@stmarksschool.org$/i', $channel['address'])) {
80
                                $toolbox->api_put(
81
                                    "users/self/communication_channels/{$channel['id']}/notification_preferences",
82
                                    [
83
                                        'notification_preferences' => $_REQUEST['notification_preferences'],
84
                                        'as_user_id' => $user['id']
85
                                    ]
86
                                );
87
                                $affected++;
88
                                break;
89
                            }
90
                        }
91
                    }
92
                }
93
                $toolbox->smarty_addMessage(
94
                    'Notification Preferences Updated',
95
                    "$affected users'  notification preferences were updated."
96
                );
97
98
            } catch (Exception $e) {
99
                $toolbox->exceptionErrorMessage($e);
100
            }
101
        }
102
103
        /* flow into STEP_INSTRUCTIONS */
104
105
    case STEP_INSTRUCTIONS:
106
    default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
        if ($step != STEP_CONFIGURE) {
108
            $toolbox->smarty_assign([
109
                'accounts' => $toolbox->getAccountList(),
110
                'formHidden' => [
111
                    'step' => STEP_CONFIGURE
112
                ]
113
            ]);
114
            $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl');
115
        }
116
}
117
118
$toolbox->cache_popKey();
119