Issues (2029)

SendNotificationToPublishLp.php (3 issues)

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Class SendNotificationToPublishLp.
7
 */
8
class SendNotificationToPublishLp extends Plugin
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $notifyStudentField;
14
15
    /**
16
     * SendNotificationToPublishLp constructor.
17
     */
18
    protected function __construct()
19
    {
20
        $this->tblExtraFieldOption = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
21
        parent::__construct(
22
            '1.0',
23
            'Carlos Alvarado'
24
        );
25
        $field = new ExtraField('lp');
26
        $notifyStudentField = $field->get_handler_field_info_by_field_variable('notify_student_and_hrm_when_available');
27
28
        if (empty($notifyStudentField)) {
29
            $notifyStudentField = [
30
                'field_type' => ExtraField::FIELD_TYPE_RADIO,
31
                'variable' => 'notify_student_and_hrm_when_available',
32
                'display_text' => 'NotifyStudentAndHrmWhenAvailable',
33
                'default_value' => 0,
34
                'field_order' => 0,
35
                'visible_to_self' => 1,
36
                'visible_to_others' => 1,
37
                'changeable' => 1,
38
                'filter' => 0,
39
            ];
40
        }
41
        $this->notifyStudentField = $notifyStudentField;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notifyStudentField can also be of type boolean. However, the property $notifyStudentField is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
42
    }
43
44
    /**
45
     * Create a new instance of SendNotificationToPublishLp.
46
     *
47
     * @return SendNotificationToPublishLp
48
     */
49
    public static function create()
50
    {
51
        static $result = null;
52
53
        return $result ? $result : $result = new self();
54
    }
55
56
    /**
57
     * Perform the plugin installation.
58
     */
59
    public function install()
60
    {
61
        $this->saveNotificationField();
62
        $this->setNotifyExtrafieldData();
63
    }
64
65
    /**
66
     * Save the arrangement for notify_student_and_hrm_when_available, it is adjusted internally so that the values
67
     * match the necessary ones.
68
     */
69
    public function saveNotificationField()
70
    {
71
        $schedule = new ExtraField('lp');
72
        $data = $this->getDataNotificationField();
73
        $data['default_value'] = 0;
74
        $data['visible_to_self'] = 1;
75
        $data['visible_to_others'] = 1;
76
        $data['changeable'] = 1;
77
        if (isset($data['id'])) {
78
            $schedule->update($data);
79
        } else {
80
            $schedule->save($data);
81
        }
82
        $field = new ExtraField('lp');
83
        $notifyStudentField = $field->get_handler_field_info_by_field_variable('notify_student_and_hrm_when_available');
84
        $this->notifyStudentField = $notifyStudentField;
0 ignored issues
show
Documentation Bug introduced by
It seems like $notifyStudentField can also be of type boolean. However, the property $notifyStudentField is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
85
    }
86
87
    /**
88
     * Make a array clean of notify_student_and_hrm_when_available.
89
     *
90
     * @return array|bool
91
     */
92
    public function getDataNotificationField($install = true)
93
    {
94
        $data = $this->notifyStudentField;
95
96
        $data['field_type'] = ExtraField::FIELD_TYPE_RADIO;
97
        $data['field_order'] = isset($data['field_order']) ? $data['field_order'] : $data['field_order']; // at
98
        $data['variable'] = isset($data['variable']) ? $data['variable'] : 'notify_student_and_hrm_when_available';
99
        $data['display_text'] = isset($data['display_text']) ? $data['display_text'] : 'NotifyStudentAndHrmWhenAvailable';
100
        $data['default_value'] = (int) $install;
101
        $data['field_order'] = isset($data['field_order']) ? $data['field_order'] : 0;
102
        $data['visible_to_self'] = isset($data['visible_to_self']) ? $data['visible_to_self'] : 0;
103
        $data['visible_to_others'] = isset($data['visible_to_others']) ? $data['visible_to_others'] : 0;
104
        $data['changeable'] = isset($data['changeable']) ? $data['changeable'] : 0;
105
        $data['filter'] = isset($data['filter']) ? $data['filter'] : 0;
106
107
        return $data;
108
    }
109
110
    /**
111
     * Set default_value to 0.
112
     */
113
    public function uninstall()
114
    {
115
        $schedule = new ExtraField('lp');
116
        $data = $this->getDataNotificationField(false);
117
        $data['default_value'] = 0;
118
        $data['visible_to_self'] = 0;
119
        $data['visible_to_others'] = 0;
120
        $data['changeable'] = 0;
121
        if (isset($data['id'])) {
122
            $schedule->update($data);
123
        } else {
124
            $schedule->save($data);
125
        }
126
    }
127
128
    /**
129
     * Insert the option fields for notify with the generic values yes or not.
130
     */
131
    public function setNotifyExtrafieldData()
132
    {
133
        $options = [
134
            0 => get_lang('No'),
135
            1 => get_lang('Yes'),
136
        ];
137
        $notifyId = (int) $this->notifyStudentField['id'];
138
        if ($notifyId != 0) {
139
            for ($i = 0; $i < count($options); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
140
                $order = $i + 1;
141
                $extraFieldOptionValue = $options[$i];
142
                $query = "SELECT *
143
                          FROM ".$this->tblExtraFieldOption."
144
                          WHERE
145
                                option_value = $i AND
146
                                field_id = $notifyId";
147
148
                $extraFieldOption = Database::fetch_assoc(Database::query($query));
149
                $extraFieldId = isset($extraFieldOption['id']) ? (int) ($extraFieldOption['id']) : 0;
150
151
                if (
152
                    $extraFieldId != 0
153
                    && $extraFieldOption['field_id'] == $notifyId) {
154
                    // Update?
155
                    $query = "UPDATE ".$this->tblExtraFieldOption."
156
                        SET
157
                            option_value = $i,
158
                            option_order = $order,
159
                            display_text = '$extraFieldOptionValue'
160
                        WHERE
161
                            field_id = $notifyId
162
                            AND id = $extraFieldId";
163
                } else {
164
                    $query = "
165
                        INSERT INTO ".$this->tblExtraFieldOption."
166
                            (field_id, option_value, display_text, priority, priority_message, option_order) VALUES
167
                            ( '$notifyId', $i, '$extraFieldOptionValue', NULL, NULL, '$order');
168
                        ";
169
                }
170
                Database::query($query);
171
            }
172
        }
173
    }
174
}
175