force-notifications.php ➔ titleCase()   A
last analyzed

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
                $notificationPreferences = [];
73
                foreach($_REQUEST['notification_preferences'] as $notification => $settings) {
74
                    if (!empty($settings['enabled']) && $settings['enabled']) {
75
                        $notificationPreferences[$notification]['frequency'] = $settings['frequency'];
76
                    }
77
                }
78
                $affected = 0;
79
                if (!empty($notificationPreferences)) {
80
                    foreach ($toolbox->api_get("accounts/{$_REQUEST['account']}/users") as $user) {
81
                        $response = $customPrefs->query("SELECT * FROM `users` WHERE `id` = '{$user['id']}' AND `role` = '{$_REQUEST['role']}'");
82
                        if ($response && $response->num_rows > 0) {
83
                            $communicationChannels = $toolbox->api_get("users/{$user['id']}/communication_channels");
84
                            foreach ($communicationChannels as $channel) {
85
                                // FIXME not great to hard code in our domain
86
                                if ($channel['type'] == 'email' && preg_match('/.*@stmarksschool.org$/i', $channel['address'])) {
87
                                    $toolbox->api_put(
88
                                        "users/self/communication_channels/{$channel['id']}/notification_preferences",
89
                                        [
90
                                            'notification_preferences' => $notificationPreferences,
91
                                            'as_user_id' => $user['id']
92
                                        ]
93
                                    );
94
                                    $affected++;
95
                                    break;
96
                                }
97
                            }
98
                        }
99
                    }
100
                }
101
                $toolbox->smarty_addMessage(
102
                    'Notification Preferences Updated',
103
                    "$affected users'  notification preferences were updated."
104
                );
105
106
            } catch (Exception $e) {
107
                $toolbox->exceptionErrorMessage($e);
108
            }
109
        }
110
111
        /* flow into STEP_INSTRUCTIONS */
112
113
    case STEP_INSTRUCTIONS:
114
    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...
115
        if ($step != STEP_CONFIGURE) {
116
            $toolbox->smarty_assign([
117
                'accounts' => $toolbox->getAccountList(),
118
                'formHidden' => [
119
                    'step' => STEP_CONFIGURE
120
                ]
121
            ]);
122
            $toolbox->smarty_display(basename(__FILE__, '.php') . '/instructions.tpl');
123
        }
124
}
125
126
$toolbox->cache_popKey();
127