| Total Complexity | 50 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AnnouncementEmail often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnouncementEmail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class AnnouncementEmail |
||
| 11 | { |
||
| 12 | public $session_id = null; |
||
| 13 | public $logger; |
||
| 14 | protected $course = null; |
||
| 15 | protected $announcement = null; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param array $courseInfo |
||
| 19 | * @param int $sessionId |
||
| 20 | * @param int $announcementId |
||
| 21 | * @param \Monolog\Logger $logger |
||
| 22 | */ |
||
| 23 | public function __construct($courseInfo, $sessionId, $announcementId, $logger = null) |
||
| 24 | { |
||
| 25 | if (empty($courseInfo)) { |
||
| 26 | $courseInfo = api_get_course_info(); |
||
| 27 | } |
||
| 28 | |||
| 29 | $this->course = $courseInfo; |
||
| 30 | $this->session_id = empty($sessionId) ? api_get_session_id() : (int) $sessionId; |
||
| 31 | |||
| 32 | if (is_numeric($announcementId)) { |
||
| 33 | $this->announcement = AnnouncementManager::get_by_id($courseInfo['real_id'], $announcementId); |
||
| 34 | } |
||
| 35 | $this->logger = $logger; |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Course info. |
||
| 40 | * |
||
| 41 | * @param string $key |
||
| 42 | * |
||
| 43 | * @return string|null |
||
| 44 | */ |
||
| 45 | public function course($key = '') |
||
| 46 | { |
||
| 47 | $result = $key ? $this->course[$key] : $this->course; |
||
| 48 | $result = $key == 'id' ? intval($result) : $result; |
||
| 49 | |||
| 50 | return $result; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Announcement info. |
||
| 55 | * |
||
| 56 | * @param string $key |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function announcement($key = '') |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Returns either all course users or all session users depending on whether |
||
| 70 | * session is turned on or not. |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function all_users() |
||
| 75 | { |
||
| 76 | $courseCode = $this->course('code'); |
||
| 77 | if (empty($this->session_id)) { |
||
| 78 | $group_id = api_get_group_id(); |
||
| 79 | if (empty($group_id)) { |
||
| 80 | $userList = CourseManager::get_user_list_from_course_code($courseCode); |
||
| 81 | } else { |
||
| 82 | $userList = GroupManager::get_users($group_id); |
||
| 83 | $new_user_list = []; |
||
| 84 | foreach ($userList as $user) { |
||
| 85 | $new_user_list[] = ['user_id' => $user]; |
||
| 86 | } |
||
| 87 | $userList = $new_user_list; |
||
| 88 | } |
||
| 89 | } else { |
||
| 90 | $userList = CourseManager::get_user_list_from_course_code( |
||
| 91 | $courseCode, |
||
| 92 | $this->session_id |
||
| 93 | ); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $userList; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Returns users and groups an announcement item has been sent to. |
||
| 101 | * |
||
| 102 | * @return array Array of users and groups to whom the element has been sent |
||
| 103 | */ |
||
| 104 | public function sent_to_info() |
||
| 105 | { |
||
| 106 | $result = []; |
||
| 107 | $result['groups'] = []; |
||
| 108 | $result['users'] = []; |
||
| 109 | |||
| 110 | $table = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 111 | $tool = TOOL_ANNOUNCEMENT; |
||
| 112 | $id = $this->announcement('id'); |
||
| 113 | $course_id = $this->course('real_id'); |
||
| 114 | $sessionCondition = api_get_session_condition($this->session_id); |
||
| 115 | |||
| 116 | $sql = "SELECT to_group_id, to_user_id |
||
| 117 | FROM $table |
||
| 118 | WHERE |
||
| 119 | c_id = $course_id AND |
||
| 120 | tool = '$tool' AND |
||
| 121 | ref = $id |
||
| 122 | $sessionCondition"; |
||
| 123 | |||
| 124 | $rs = Database::query($sql); |
||
| 125 | |||
| 126 | while ($row = Database::fetch_array($rs, 'ASSOC')) { |
||
| 127 | // if to_user_id <> 0 then it is sent to a specific user |
||
| 128 | $user_id = $row['to_user_id']; |
||
| 129 | if (!empty($user_id)) { |
||
| 130 | $result['users'][] = (int) $user_id; |
||
| 131 | // If user is set then skip the group |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | |||
| 135 | // if to_group_id is null then it is sent to a specific user |
||
| 136 | // if to_group_id = 0 then it is sent to everybody |
||
| 137 | $group_id = $row['to_group_id']; |
||
| 138 | if (!empty($group_id)) { |
||
| 139 | $result['groups'][] = (int) $group_id; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | return $result; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns the list of user info to which an announcement was sent. |
||
| 148 | * This function returns a list of actual users even when recipient |
||
| 149 | * are groups. |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function sent_to() |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Email subject. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function subject() |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Email message. |
||
| 206 | * |
||
| 207 | * @param int $receiverUserId |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function message($receiverUserId) |
||
| 212 | { |
||
| 213 | $content = $this->announcement('content'); |
||
| 214 | $session_id = $this->session_id; |
||
| 215 | $courseCode = $this->course('code'); |
||
| 216 | |||
| 217 | $content = AnnouncementManager::parseContent( |
||
| 218 | $receiverUserId, |
||
| 219 | $content, |
||
| 220 | $courseCode, |
||
| 221 | $session_id |
||
| 222 | ); |
||
| 223 | |||
| 224 | // Build the link by hand because api_get_cidreq() doesn't accept course params |
||
| 225 | $course_param = 'cidReq='.$courseCode.'&id_session='.$session_id.'&gidReq='.api_get_group_id(); |
||
| 226 | $course_name = $this->course('title'); |
||
| 227 | |||
| 228 | $result = "<div>$content</div>"; |
||
| 229 | |||
| 230 | // Adding attachment |
||
| 231 | $attachment = $this->attachment(); |
||
| 232 | if (!empty($attachment)) { |
||
| 233 | $result .= '<br />'; |
||
| 234 | $result .= Display::url( |
||
| 235 | $attachment['filename'], |
||
| 236 | api_get_path(WEB_CODE_PATH).'announcements/download.php?file='.basename($attachment['path']).'&'.$course_param |
||
| 237 | ); |
||
| 238 | $result .= '<br />'; |
||
| 239 | } |
||
| 240 | |||
| 241 | $result .= '<hr />'; |
||
| 242 | $userInfo = api_get_user_info(); |
||
| 243 | if (!empty($userInfo)) { |
||
| 244 | $result .= '<a href="mailto:'.$userInfo['mail'].'">'.$userInfo['complete_name'].'</a><br/>'; |
||
| 245 | } |
||
| 246 | $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.$course_param.'">'.$course_name.'</a><br/>'; |
||
| 247 | |||
| 248 | return $result; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the one file that can be attached to an announcement. |
||
| 253 | * |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | public function attachment() |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Send announcement by email to myself. |
||
| 279 | */ |
||
| 280 | public function sendAnnouncementEmailToMySelf() |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Send emails to users. |
||
| 297 | * |
||
| 298 | * @param bool $sendToUsersInSession |
||
| 299 | * @param bool $sendToDrhUsers send a copy of the message to the DRH users |
||
| 300 | * @param int $senderId related to the main user |
||
| 301 | * |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function send($sendToUsersInSession = false, $sendToDrhUsers = false, $senderId = 0) |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Store that emails where sent. |
||
| 390 | */ |
||
| 391 | public function logMailSent() |
||
| 406 |