| Conditions | 27 |
| Paths | 83 |
| Total Lines | 166 |
| Code Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 3 | Features | 1 |
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 | $users = []; |
||
| 127 | |||
| 128 | $now = api_get_utc_datetime(); |
||
| 129 | if (!empty($date)) { |
||
| 130 | $now = $date; |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($isTest) { |
||
| 134 | echo "-------------------------------------------".PHP_EOL; |
||
| 135 | echo "----- Testing date $now ----".PHP_EOL; |
||
| 136 | echo "-------------------------------------------".PHP_EOL; |
||
| 137 | } |
||
| 138 | |||
| 139 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 140 | foreach ($enableDaysList as $day) { |
||
| 141 | $day = (int) $day; |
||
| 142 | |||
| 143 | if (0 === $day) { |
||
| 144 | echo 'Day = 0 avoided '.PHP_EOL; |
||
| 145 | continue; |
||
| 146 | } |
||
| 147 | |||
| 148 | $hourStart = $day * 24; |
||
| 149 | $hourEnd = ($day - 1) * 24; |
||
| 150 | |||
| 151 | $date = new DateTime($now); |
||
| 152 | $date->sub(new DateInterval('PT'.$hourStart.'H')); |
||
| 153 | $hourStart = $date->format('Y-m-d H:i:s'); |
||
| 154 | |||
| 155 | $date = new DateTime($now); |
||
| 156 | $date->sub(new DateInterval('PT'.$hourEnd.'H')); |
||
| 157 | $hourEnd = $date->format('Y-m-d H:i:s'); |
||
| 158 | |||
| 159 | $sql = "SELECT |
||
| 160 | t.user_id, |
||
| 161 | MAX(t.logout_course_date) max_course_date, |
||
| 162 | MAX(l.logout_date) max_login_date |
||
| 163 | FROM $track t |
||
| 164 | INNER JOIN $userTable u |
||
| 165 | ON (u.id = t.user_id) |
||
| 166 | INNER JOIN $login l |
||
| 167 | ON (u.id = l.login_user_id) |
||
| 168 | WHERE |
||
| 169 | u.status <> ".ANONYMOUS." AND |
||
| 170 | u.active = 1 |
||
| 171 | GROUP BY t.user_id |
||
| 172 | HAVING |
||
| 173 | (max_course_date BETWEEN '$hourStart' AND '$hourEnd') AND |
||
| 174 | (max_login_date BETWEEN '$hourStart' AND '$hourEnd') |
||
| 175 | "; |
||
| 176 | |||
| 177 | $rs = Database::query($sql); |
||
| 178 | if (!Database::num_rows($rs)) { |
||
| 179 | echo "No users to process for day $day".PHP_EOL.PHP_EOL; |
||
| 180 | continue; |
||
| 181 | } |
||
| 182 | |||
| 183 | echo "Processing day $day".PHP_EOL.PHP_EOL; |
||
| 184 | |||
| 185 | while ($user = Database::fetch_array($rs)) { |
||
| 186 | $userId = $user['user_id']; |
||
| 187 | if (in_array($userId, $users)) { |
||
| 188 | continue; |
||
| 189 | } |
||
| 190 | |||
| 191 | // Take max date value. |
||
| 192 | $maxCourseDate = $user['max_course_date']; |
||
| 193 | $maxLoginDate = $user['max_login_date']; |
||
| 194 | $maxDate = $maxCourseDate; |
||
| 195 | if ($maxLoginDate > $maxCourseDate) { |
||
| 196 | $maxDate = $maxLoginDate; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Check if user has selected to pause formation. |
||
| 200 | $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); |
||
| 201 | if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { |
||
| 202 | $startDate = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 203 | $userId, |
||
| 204 | 'start_pause_date' |
||
| 205 | ); |
||
| 206 | $endDate = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 207 | $userId, |
||
| 208 | 'end_pause_date' |
||
| 209 | ); |
||
| 210 | |||
| 211 | if ( |
||
| 212 | !empty($startDate) && isset($startDate['value']) && !empty($startDate['value']) && |
||
| 213 | !empty($endDate) && isset($endDate['value']) && !empty($endDate['value']) |
||
| 214 | ) { |
||
| 215 | $startDate = $startDate['value']; |
||
| 216 | $endDate = $endDate['value']; |
||
| 217 | |||
| 218 | // Ignore date if is between the pause formation. |
||
| 219 | if ($maxDate > $startDate && $maxDate < $endDate) { |
||
| 220 | echo "Message skipped for user #$userId because latest login is $maxDate and pause formation between $startDate - $endDate ".PHP_EOL; |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | echo "User #$userId added to message queue because latest login is $maxDate between $hourStart AND $hourEnd".PHP_EOL; |
||
| 227 | |||
| 228 | $users[] = $userId; |
||
| 229 | $usersNotificationPerDay[$day][] = $userId; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | if (!empty($usersNotificationPerDay)) { |
||
| 234 | echo 'Now processing messages ...'.PHP_EOL; |
||
| 235 | |||
| 236 | ksort($usersNotificationPerDay); |
||
| 237 | foreach ($usersNotificationPerDay as $day => $userList) { |
||
| 238 | $template = new Template( |
||
| 239 | '', |
||
| 240 | true, |
||
| 241 | true, |
||
| 242 | false, |
||
| 243 | false, |
||
| 244 | true, |
||
| 245 | false |
||
| 246 | ); |
||
| 247 | $title = sprintf($this->get_lang('InactivityXDays'), $day); |
||
| 248 | |||
| 249 | foreach ($userList as $userId) { |
||
| 250 | $userInfo = api_get_user_info($userId); |
||
| 251 | $template->assign('days', $day); |
||
| 252 | $template->assign('user', $userInfo); |
||
| 253 | $content = $template->fetch('pausetraining/view/notification_content.tpl'); |
||
| 254 | echo 'Ready to send a message "'.$title.'" to user #'.$userId.' '.$userInfo['complete_name'].PHP_EOL; |
||
| 255 | if (false === $isTest) { |
||
| 256 | MessageManager::send_message_simple($userId, $title, $content, $senderId); |
||
| 257 | } else { |
||
| 258 | echo 'Message not send because is in test mode.'.PHP_EOL; |
||
| 259 | } |
||
| 321 |