| Conditions | 39 |
| Paths | 8823 |
| Total Lines | 196 |
| Code Lines | 124 |
| 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 |
||
| 93 | public function runCron($date = '', $isTest = false) |
||
| 94 | { |
||
| 95 | $enable = $this->get('tool_enable'); |
||
| 96 | $senderId = $this->get('sender_id'); |
||
| 97 | $enableDays = $this->get('cron_alert_users_if_inactive_days'); |
||
| 98 | |||
| 99 | if ('true' !== $enable) { |
||
| 100 | echo 'Plugin not enabled'; |
||
| 101 | |||
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | if (empty($senderId)) { |
||
| 106 | echo 'Sender id not configured'; |
||
| 107 | |||
| 108 | return false; |
||
| 109 | } |
||
| 110 | |||
| 111 | $senderInfo = api_get_user_info($senderId); |
||
| 112 | |||
| 113 | if (empty($senderInfo)) { |
||
| 114 | echo "Sender #$senderId not found in Chamilo"; |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | $enableDaysList = explode(',', $enableDays); |
||
| 120 | rsort($enableDaysList); |
||
| 121 | |||
| 122 | $track = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 123 | $login = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 124 | $userTable = Database::get_main_table(TABLE_MAIN_USER); |
||
| 125 | $usersNotificationPerDay = []; |
||
| 126 | $now = api_get_utc_datetime(); |
||
| 127 | if (!empty($date)) { |
||
| 128 | $now = $date; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($isTest) { |
||
| 132 | echo "-------------------------------------------".PHP_EOL; |
||
| 133 | echo "----- Testing date $now ----".PHP_EOL; |
||
| 134 | echo "-------------------------------------------".PHP_EOL; |
||
| 135 | } |
||
| 136 | |||
| 137 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 138 | $sql = "SELECT u.id |
||
| 139 | FROM $userTable u |
||
| 140 | WHERE u.status <> ".ANONYMOUS." AND u.active = 1"; |
||
| 141 | $rs = Database::query($sql); |
||
| 142 | |||
| 143 | $users = []; |
||
| 144 | while ($user = Database::fetch_array($rs)) { |
||
| 145 | $userId = $user['id']; |
||
| 146 | |||
| 147 | $sql = "SELECT |
||
| 148 | MAX(t.logout_course_date) max_course_date, |
||
| 149 | MAX(l.logout_date) max_login_date |
||
| 150 | FROM $userTable u |
||
| 151 | LEFT JOIN $track t |
||
| 152 | ON (u.id = t.user_id) |
||
| 153 | LEFT JOIN $login l |
||
| 154 | ON (u.id = l.login_user_id) |
||
| 155 | WHERE |
||
| 156 | u.id = $userId |
||
| 157 | LIMIT 1 |
||
| 158 | "; |
||
| 159 | $result = Database::query($sql); |
||
| 160 | $data = Database::fetch_array($result); |
||
| 161 | $maxCourseDate = ''; |
||
| 162 | $maxLoginDate = ''; |
||
| 163 | |||
| 164 | // Take max date value. |
||
| 165 | if ($data) { |
||
| 166 | $maxCourseDate = $data['max_course_date']; |
||
| 167 | $maxLoginDate = $data['max_login_date']; |
||
| 168 | } |
||
| 169 | |||
| 170 | $maxEndPause = null; |
||
| 171 | $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); |
||
| 172 | if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { |
||
| 173 | $endDate = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 174 | $userId, |
||
| 175 | 'end_pause_date' |
||
| 176 | ); |
||
| 177 | if (!empty($endDate) && isset($endDate['value']) && !empty($endDate['value'])) { |
||
| 178 | $maxEndPause = $endDate['value']; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $maxDate = $maxCourseDate; |
||
| 183 | if ($maxLoginDate > $maxCourseDate) { |
||
| 184 | $maxDate = $maxLoginDate; |
||
| 185 | } |
||
| 186 | |||
| 187 | if ($maxEndPause > $maxDate) { |
||
| 188 | $maxDate = $maxEndPause; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (empty($maxDate)) { |
||
| 192 | // Nothing found for that user, skip. |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | $users[$userId] = $maxDate; |
||
| 196 | } |
||
| 197 | |||
| 198 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 199 | foreach ($enableDaysList as $day) { |
||
| 200 | $day = (int) $day; |
||
| 201 | |||
| 202 | if (0 === $day) { |
||
| 203 | echo 'Day = 0 avoided '.PHP_EOL; |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | $dayToCheck = $day + 1; |
||
| 207 | $hourStart = $dayToCheck * 24; |
||
| 208 | $hourEnd = ($dayToCheck - 1) * 24; |
||
| 209 | |||
| 210 | $date = new DateTime($now); |
||
| 211 | $date->sub(new DateInterval('PT'.$hourStart.'H')); |
||
| 212 | $hourStart = $date->format('Y-m-d H:i:s'); |
||
| 213 | |||
| 214 | $date = new DateTime($now); |
||
| 215 | $date->sub(new DateInterval('PT'.$hourEnd.'H')); |
||
| 216 | $hourEnd = $date->format('Y-m-d H:i:s'); |
||
| 217 | |||
| 218 | echo "Processing day $day: $hourStart - $hourEnd ".PHP_EOL.PHP_EOL; |
||
| 219 | |||
| 220 | foreach ($users as $userId => $maxDate) { |
||
| 221 | if (!($maxDate > $hourStart && $maxDate < $hourEnd)) { |
||
| 222 | //echo "Message skipped for user #$userId because max date found: $maxDate not in range $hourStart - $hourEnd ".PHP_EOL; |
||
| 223 | continue; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Check if user has selected to pause formation. |
||
| 227 | $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); |
||
| 228 | if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { |
||
| 229 | $startDate = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 230 | $userId, |
||
| 231 | 'start_pause_date' |
||
| 232 | ); |
||
| 233 | $endDate = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 234 | $userId, |
||
| 235 | 'end_pause_date' |
||
| 236 | ); |
||
| 237 | |||
| 238 | if ( |
||
| 239 | !empty($startDate) && isset($startDate['value']) && !empty($startDate['value']) && |
||
| 240 | !empty($endDate) && isset($endDate['value']) && !empty($endDate['value']) |
||
| 241 | ) { |
||
| 242 | $startDate = $startDate['value']; |
||
| 243 | $endDate = $endDate['value']; |
||
| 244 | |||
| 245 | if ($startDate > $hourStart && $startDate < $hourStart) { |
||
| 246 | //echo "Message skipped for user #$userId because process date $hourStart is in start pause in $startDate - $endDate ".PHP_EOL; |
||
| 247 | continue; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($endDate > $hourEnd && $endDate < $hourEnd) { |
||
| 251 | //echo "Message skipped for user #$userId because process date $hourEnd is in start pause in $startDate - $endDate ".PHP_EOL; |
||
| 252 | continue; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | echo "User #$userId added to message queue because latest login is $maxDate between $hourStart AND $hourEnd".PHP_EOL; |
||
| 258 | $users[] = $userId; |
||
| 259 | $usersNotificationPerDay[$day][] = $userId; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if (!empty($usersNotificationPerDay)) { |
||
| 264 | echo PHP_EOL.'Now processing messages ...'.PHP_EOL; |
||
| 265 | |||
| 266 | ksort($usersNotificationPerDay); |
||
| 267 | foreach ($usersNotificationPerDay as $day => $userList) { |
||
| 268 | $template = new Template( |
||
| 269 | '', |
||
| 270 | true, |
||
| 271 | true, |
||
| 272 | false, |
||
| 273 | false, |
||
| 274 | true, |
||
| 275 | false |
||
| 276 | ); |
||
| 277 | $title = sprintf($this->get_lang('InactivityXDays'), $day); |
||
| 278 | |||
| 279 | foreach ($userList as $userId) { |
||
| 280 | $userInfo = api_get_user_info($userId); |
||
| 281 | $template->assign('days', $day); |
||
| 282 | $template->assign('user', $userInfo); |
||
| 283 | $content = $template->fetch('pausetraining/view/notification_content.tpl'); |
||
| 284 | echo 'Ready to send a message "'.$title.'" to user #'.$userId.' '.$userInfo['complete_name'].PHP_EOL; |
||
| 285 | if (false === $isTest) { |
||
| 286 | MessageManager::send_message_simple($userId, $title, $content, $senderId); |
||
| 287 | } else { |
||
| 288 | echo 'Message not send because is in test mode.'.PHP_EOL; |
||
| 289 | } |
||
| 351 |