| Conditions | 12 |
| Paths | 21 |
| Total Lines | 60 |
| Code Lines | 39 |
| 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 |
||
| 94 | public function runCron() |
||
| 95 | { |
||
| 96 | $enable = $this->get('tool_enable'); |
||
| 97 | $enableDays = $this->get('cron_alert_users_if_inactive_days'); |
||
| 98 | |||
| 99 | if ($enable && !empty($enableDays)) { |
||
| 100 | $enableDaysList = explode(',', $enableDays); |
||
| 101 | rsort($enableDaysList); |
||
| 102 | |||
| 103 | $loginTable = Database::get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 104 | $userTable = Database::get_main_table(TABLE_MAIN_USER); |
||
| 105 | $now = api_get_utc_datetime(); |
||
| 106 | $usersNotificationPerDay = []; |
||
| 107 | $users = []; |
||
| 108 | foreach ($enableDaysList as $day) { |
||
| 109 | $day = (int) $day; |
||
| 110 | |||
| 111 | $sql = "SELECT |
||
| 112 | stats_login.user_id, |
||
| 113 | MAX(stats_login.login_course_date) max_date |
||
| 114 | FROM $loginTable stats_login |
||
| 115 | INNER JOIN $userTable u |
||
| 116 | ON (u.id = stats_login.user_id) |
||
| 117 | WHERE |
||
| 118 | u.status <> ".ANONYMOUS." AND |
||
| 119 | u.active = 1 |
||
| 120 | GROUP BY stats_login.user_id |
||
| 121 | HAVING DATE_SUB('$now', INTERVAL '$day' DAY) > max_date "; |
||
| 122 | |||
| 123 | $rs = Database::query($sql); |
||
| 124 | while ($user = Database::fetch_array($rs)) { |
||
| 125 | $userId = $user['user_id']; |
||
| 126 | |||
| 127 | if (in_array($userId, $users)) { |
||
| 128 | continue; |
||
| 129 | } |
||
| 130 | $users[] = $userId; |
||
| 131 | $usersNotificationPerDay[$day][] = $userId; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | if (!empty($usersNotificationPerDay)) { |
||
| 136 | ksort($usersNotificationPerDay); |
||
| 137 | $extraFieldValue = new ExtraFieldValue('user'); |
||
| 138 | foreach ($usersNotificationPerDay as $day => $userList) { |
||
| 139 | $template = new Template(); |
||
| 140 | $title = sprintf($this->get_lang('InactivityXDays'), $day); |
||
| 141 | |||
| 142 | foreach ($userList as $userId) { |
||
| 143 | $userInfo = api_get_user_info($userId); |
||
| 144 | $pause = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'pause_formation'); |
||
| 145 | if (!empty($pause) && isset($pause['value']) && 1 == $pause['value']) { |
||
| 146 | // Skip user because he paused his formation. |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | |||
| 150 | $template->assign('days', $day); |
||
| 151 | $template->assign('user', $userInfo); |
||
| 152 | $content = $template->fetch('pausetraining/view/notification_content.tpl'); |
||
| 153 | MessageManager::send_message($userId, $title, $content); |
||
| 154 | } |
||
| 195 |