Conditions | 26 |
Paths | 11524 |
Total Lines | 151 |
Code Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
88 | function _updateUsers( |
||
89 | $users, |
||
90 | $resetPassword = false, |
||
91 | $sendEmail = false, |
||
92 | $askNewPassword = false |
||
93 | ) { |
||
94 | $usergroup = new UserGroup(); |
||
95 | $extraFieldValue = new ExtraFieldValue('user'); |
||
96 | if (is_array($users)) { |
||
97 | foreach ($users as $user) { |
||
98 | if (isset($user['Status'])) { |
||
99 | $user['Status'] = api_status_key($user['Status']); |
||
100 | } |
||
101 | |||
102 | $userInfo = api_get_user_info_from_username($user['UserName']); |
||
103 | |||
104 | if (empty($userInfo)) { |
||
105 | continue; |
||
106 | } |
||
107 | /* |
||
108 | // In specific cases, you might want to only update if the e-mail |
||
109 | // in the CSV is different from the e-mail in the database |
||
110 | if (!empty($user['Email'])) { |
||
111 | if ($user['Email'] == $userInfo['email']) { |
||
112 | continue; |
||
113 | } |
||
114 | } |
||
115 | */ |
||
116 | |||
117 | $user_id = $userInfo['user_id']; |
||
118 | $firstName = $user['FirstName'] ?? $userInfo['firstname']; |
||
119 | $lastName = $user['LastName'] ?? $userInfo['lastname']; |
||
120 | $userName = $user['NewUserName'] ?? $userInfo['username']; |
||
121 | $changePassMethod = 0; |
||
122 | $password = null; |
||
123 | $authSource = $userInfo['auth_source']; |
||
124 | |||
125 | if ($resetPassword) { |
||
126 | $changePassMethod = 1; |
||
127 | } else { |
||
128 | if (isset($user['Password'])) { |
||
129 | $changePassMethod = 2; |
||
130 | $password = $user['Password']; |
||
131 | } |
||
132 | |||
133 | if (isset($user['AuthSource']) && $user['AuthSource'] != $authSource) { |
||
134 | $authSource = $user['AuthSource']; |
||
135 | $changePassMethod = 3; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | $email = $user['Email'] ?? $userInfo['email']; |
||
140 | $status = $user['Status'] ?? $userInfo['status']; |
||
141 | $officialCode = $user['OfficialCode'] ?? $userInfo['official_code']; |
||
142 | $phone = $user['PhoneNumber'] ?? $userInfo['phone']; |
||
143 | $pictureUrl = $user['PictureUri'] ?? $userInfo['picture_uri']; |
||
144 | $expirationDate = $user['ExpiryDate'] ?? $userInfo['expiration_date']; |
||
145 | // Fix wrong date in DB for old users (sometimes would be expiration_date = '9999-12-31 ********') where it should be null |
||
146 | if (substr($expirationDate, 0, 4) === '9999') { |
||
147 | $expirationDate = null; |
||
148 | } |
||
149 | $active = $userInfo['active']; |
||
150 | if (isset($user['Active'])) { |
||
151 | $user['Active'] = (int) $user['Active']; |
||
152 | if (-1 === $user['Active']) { |
||
153 | $user['Active'] = 0; |
||
154 | } |
||
155 | $active = $user['Active']; |
||
156 | } |
||
157 | |||
158 | $creatorId = $userInfo['creator_id']; |
||
159 | $hrDeptId = $userInfo['hr_dept_id']; |
||
160 | $language = $user['Language'] ?? $userInfo['language']; |
||
161 | //$sendEmail = isset($user['SendEmail']) ? $user['SendEmail'] : $userInfo['language']; |
||
162 | //$sendEmail = false; |
||
163 | // see BT#17893 |
||
164 | if ($resetPassword && $sendEmail == false) { |
||
165 | $sendEmail = true; |
||
166 | } |
||
167 | $extra = []; |
||
168 | if ($askNewPassword) { |
||
169 | $extra['ask_new_password'] = 1; |
||
170 | } |
||
171 | |||
172 | UserManager::update_user( |
||
173 | $user_id, |
||
174 | $firstName, |
||
175 | $lastName, |
||
176 | $userName, |
||
177 | $password, |
||
178 | $authSource, |
||
179 | $email, |
||
180 | $status, |
||
181 | $officialCode, |
||
182 | $phone, |
||
183 | $pictureUrl, |
||
184 | $expirationDate, |
||
185 | $active, |
||
186 | $creatorId, |
||
187 | $hrDeptId, |
||
188 | $extra, |
||
189 | $language, |
||
190 | '', |
||
191 | $sendEmail, |
||
192 | $changePassMethod |
||
193 | ); |
||
194 | |||
195 | if (!empty($user['Courses']) && !is_array($user['Courses'])) { |
||
196 | $user['Courses'] = [$user['Courses']]; |
||
197 | } |
||
198 | if (!empty($user['Courses']) && is_array($user['Courses'])) { |
||
199 | foreach ($user['Courses'] as $course) { |
||
200 | if (CourseManager::course_exists($course)) { |
||
201 | CourseManager::subscribeUser($user_id, $course, $user['Status']); |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | if (!empty($user['ClassId'])) { |
||
206 | $classId = explode('|', trim($user['ClassId'])); |
||
207 | foreach ($classId as $id) { |
||
208 | $usergroup->subscribe_users_to_usergroup( |
||
209 | $id, |
||
210 | [$user_id], |
||
211 | false |
||
212 | ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | // Saving extra fields. |
||
217 | global $extra_fields; |
||
218 | |||
219 | // We are sure that the extra field exists. |
||
220 | $userExtraFields = [ |
||
221 | 'item_id' => $user_id, |
||
222 | ]; |
||
223 | $add = false; |
||
224 | foreach ($extra_fields as $extras) { |
||
225 | if (isset($user[$extras[1]])) { |
||
226 | $key = $extras[1]; |
||
227 | $value = $user[$extras[1]]; |
||
228 | $userExtraFields["extra_$key"] = $value; |
||
229 | $add = true; |
||
230 | } |
||
231 | } |
||
232 | if ($add) { |
||
233 | $extraFieldValue->saveFieldValues($userExtraFields, true); |
||
234 | } |
||
235 | |||
236 | $userUpdated = api_get_user_info($user_id); |
||
237 | Display::addFlash( |
||
238 | Display::return_message(get_lang('UserUpdated').': '.$userUpdated['complete_name_with_username']) |
||
239 | ); |
||
440 |