| Total Complexity | 45 |
| Total Lines | 459 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ScheduledAnnouncement 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 ScheduledAnnouncement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class ScheduledAnnouncement extends Model |
||
| 13 | { |
||
| 14 | public $table; |
||
| 15 | public $columns = ['id', 'subject', 'message', 'date', 'sent', 'session_id']; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Constructor. |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | parent::__construct(); |
||
| 23 | $this->table = 'scheduled_announcements'; |
||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param array $where_conditions |
||
| 28 | * |
||
| 29 | * @return array |
||
| 30 | */ |
||
| 31 | public function get_all($where_conditions = []) |
||
| 32 | { |
||
| 33 | return Database::select( |
||
| 34 | '*', |
||
| 35 | $this->table, |
||
| 36 | ['where' => $where_conditions, 'order' => 'subject ASC'] |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | public function get_count() |
||
| 44 | { |
||
| 45 | $row = Database::select( |
||
| 46 | 'count(*) as count', |
||
| 47 | $this->table, |
||
| 48 | [], |
||
| 49 | 'first' |
||
| 50 | ); |
||
| 51 | |||
| 52 | return $row['count']; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Displays the title + grid. |
||
| 57 | * |
||
| 58 | * @param int $sessionId |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function getGrid($sessionId) |
||
| 63 | { |
||
| 64 | // action links |
||
| 65 | $action = '<div class="actions" style="margin-bottom:20px">'; |
||
| 66 | $action .= Display::url( |
||
| 67 | Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM), |
||
| 68 | api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$sessionId |
||
| 69 | ); |
||
| 70 | |||
| 71 | $action .= '<a href="'.api_get_self().'?action=add&session_id='.$sessionId.'">'. |
||
| 72 | Display::return_icon('add.png', get_lang('Add'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
| 73 | $action .= '<a href="scheduled_announcement.php?action=run&session_id='.$sessionId.'">'. |
||
| 74 | Display::return_icon('tuning.png', get_lang('Send pending announcements manually'), '', ICON_SIZE_MEDIUM). |
||
| 75 | '</a>'; |
||
| 76 | |||
| 77 | $action .= '</div>'; |
||
| 78 | |||
| 79 | $html = $action; |
||
| 80 | $html .= '<div id="session-table" class="table-responsive">'; |
||
| 81 | $html .= Display::grid_html('programmed'); |
||
| 82 | $html .= '</div>'; |
||
| 83 | |||
| 84 | return $html; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a Form validator Obj. |
||
| 89 | * |
||
| 90 | * @param int $id |
||
| 91 | * @param string $url |
||
| 92 | * @param string $action add, edit |
||
| 93 | * @param array $sessionInfo |
||
| 94 | * |
||
| 95 | * @return FormValidator form validator obj |
||
| 96 | */ |
||
| 97 | public function returnSimpleForm($id, $url, $action, $sessionInfo = []) |
||
| 98 | { |
||
| 99 | $form = new FormValidator( |
||
| 100 | 'announcement', |
||
| 101 | 'post', |
||
| 102 | $url |
||
| 103 | ); |
||
| 104 | |||
| 105 | $form->addHidden('session_id', $sessionInfo['id']); |
||
| 106 | $form->addDateTimePicker('date', get_lang('Date')); |
||
| 107 | $form->addText('subject', get_lang('Subject')); |
||
| 108 | $form->addHtmlEditor('message', get_lang('Message')); |
||
| 109 | |||
| 110 | $extraField = new ExtraField('scheduled_announcement'); |
||
| 111 | $extra = $extraField->addElements($form, $id); |
||
| 112 | $js = $extra['jquery_ready_content']; |
||
| 113 | $form->addHtml("<script> $(function() { $js }); </script> "); |
||
| 114 | |||
| 115 | $this->setTagsInForm($form); |
||
| 116 | |||
| 117 | $form->addCheckBox('sent', null, get_lang('Message Sent')); |
||
| 118 | |||
| 119 | if ('edit' === $action) { |
||
| 120 | $form->addButtonUpdate(get_lang('Edit')); |
||
| 121 | } |
||
| 122 | |||
| 123 | return $form; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns a Form validator Obj. |
||
| 128 | * |
||
| 129 | * @todo the form should be auto generated |
||
| 130 | * |
||
| 131 | * @param string $url |
||
| 132 | * @param string $action add, edit |
||
| 133 | * @param array |
||
| 134 | * |
||
| 135 | * @return FormValidator form validator obj |
||
| 136 | */ |
||
| 137 | public function returnForm($url, $action, $sessionInfo = []) |
||
| 138 | { |
||
| 139 | // Setting the form elements |
||
| 140 | $header = get_lang('Add'); |
||
| 141 | |||
| 142 | if ('edit' === $action) { |
||
| 143 | $header = get_lang('Edit'); |
||
| 144 | } |
||
| 145 | |||
| 146 | $form = new FormValidator( |
||
| 147 | 'announcement', |
||
| 148 | 'post', |
||
| 149 | $url |
||
| 150 | ); |
||
| 151 | |||
| 152 | $form->addHeader($header); |
||
| 153 | if ('add' === $action) { |
||
| 154 | $form->addHtml( |
||
| 155 | Display::return_message( |
||
| 156 | nl2br( |
||
| 157 | get_lang( |
||
| 158 | 'This form allows scheduling announcements to be sent automatically to the students who are taking a course in a session.' |
||
| 159 | ) |
||
| 160 | ), |
||
| 161 | 'normal', |
||
| 162 | false |
||
| 163 | ) |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | $form->addHidden('session_id', $sessionInfo['id']); |
||
| 167 | |||
| 168 | $useBaseDate = false; |
||
| 169 | $startDate = $sessionInfo['access_start_date']; |
||
| 170 | $endDate = $sessionInfo['access_end_date']; |
||
| 171 | |||
| 172 | if (!empty($startDate) || !empty($endDate)) { |
||
| 173 | $useBaseDate = true; |
||
| 174 | } |
||
| 175 | |||
| 176 | $typeOptions = [ |
||
| 177 | 'specific_date' => get_lang('Specific dispatch date'), |
||
| 178 | ]; |
||
| 179 | |||
| 180 | if ($useBaseDate) { |
||
| 181 | $typeOptions['base_date'] = get_lang('BaseDate'); |
||
| 182 | } |
||
| 183 | |||
| 184 | $form->addSelect( |
||
| 185 | 'type', |
||
| 186 | get_lang('Type'), |
||
| 187 | $typeOptions, |
||
| 188 | [ |
||
| 189 | 'onchange' => "javascript: |
||
| 190 | if (this.options[this.selectedIndex].value == 'base_date') { |
||
| 191 | document.getElementById('options').style.display = 'block'; |
||
| 192 | document.getElementById('specific_date').style.display = 'none'; |
||
| 193 | } else { |
||
| 194 | document.getElementById('options').style.display = 'none'; |
||
| 195 | document.getElementById('specific_date').style.display = 'block'; |
||
| 196 | } |
||
| 197 | ", ] |
||
| 198 | ); |
||
| 199 | |||
| 200 | $form->addHtml('<div id="specific_date">'); |
||
| 201 | $form->addDateTimePicker('date', get_lang('Date')); |
||
| 202 | $form->addHtml('</div>'); |
||
| 203 | $form->addHtml('<div id="options" style="display:none">'); |
||
| 204 | |||
| 205 | $startDate = $sessionInfo['access_start_date']; |
||
| 206 | $endDate = $sessionInfo['access_end_date']; |
||
| 207 | |||
| 208 | $form->addText( |
||
| 209 | 'days', |
||
| 210 | get_lang('days'), |
||
| 211 | false |
||
| 212 | ); |
||
| 213 | |||
| 214 | $form->addSelect( |
||
| 215 | 'moment_type', |
||
| 216 | get_lang('After or before'), |
||
| 217 | [ |
||
| 218 | 'after' => get_lang('After'), |
||
| 219 | 'before' => get_lang('Before'), |
||
| 220 | ] |
||
| 221 | ); |
||
| 222 | |||
| 223 | if (!empty($startDate)) { |
||
| 224 | $options['start_date'] = get_lang('Start Date').' - '.$startDate; |
||
| 225 | } |
||
| 226 | |||
| 227 | if (!empty($endDate)) { |
||
| 228 | $options['end_date'] = get_lang('End Date').' - '.$endDate; |
||
| 229 | } |
||
| 230 | if (!empty($options)) { |
||
| 231 | $form->addSelect('base_date', get_lang('Dispatch based on the session\'s start/end dates'), $options); |
||
| 232 | } |
||
| 233 | |||
| 234 | $form->addHtml('</div>'); |
||
| 235 | $form->addText('subject', get_lang('Subject')); |
||
| 236 | $form->addHtmlEditor('message', get_lang('Message')); |
||
| 237 | |||
| 238 | $extraField = new ExtraField('scheduled_announcement'); |
||
| 239 | $extra = $extraField->addElements($form); |
||
| 240 | $js = $extra['jquery_ready_content']; |
||
| 241 | $form->addHtml("<script> $(function() { $js }); </script> "); |
||
| 242 | $this->setTagsInForm($form); |
||
| 243 | |||
| 244 | if ('edit' === $action) { |
||
| 245 | $form->addButtonUpdate(get_lang('Edit')); |
||
| 246 | } else { |
||
| 247 | $form->addButtonCreate(get_lang('Add')); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $form; |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param int $id |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getAttachmentToString($id) |
||
| 259 | { |
||
| 260 | $file = $this->getAttachment($id); |
||
| 261 | if (!empty($file) && !empty($file['value'])) { |
||
| 262 | $url = $file['url']; |
||
| 263 | |||
| 264 | return get_lang('Attachment').': '.Display::url(basename($file['url']), $url, ['target' => '_blank']); |
||
| 265 | } |
||
| 266 | |||
| 267 | return ''; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param int $id |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | public function getAttachment($id) |
||
| 276 | { |
||
| 277 | $extraFieldValue = new ExtraFieldValue('scheduled_announcement'); |
||
| 278 | |||
| 279 | return $extraFieldValue->get_values_by_handler_and_field_variable($id, 'attachment'); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param int $urlId |
||
| 284 | * |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function sendPendingMessages($urlId = 0) |
||
| 288 | { |
||
| 289 | if (!$this->allowed()) { |
||
| 290 | return 0; |
||
| 291 | } |
||
| 292 | |||
| 293 | $messagesSent = 0; |
||
| 294 | $now = api_get_utc_datetime(); |
||
| 295 | $result = $this->get_all(); |
||
| 296 | $extraFieldValue = new ExtraFieldValue('scheduled_announcement'); |
||
| 297 | |||
| 298 | foreach ($result as $result) { |
||
| 299 | if (empty($result['sent'])) { |
||
| 300 | if (!empty($result['date']) && $result['date'] < $now) { |
||
| 301 | $sessionId = $result['session_id']; |
||
| 302 | $sessionInfo = api_get_session_info($sessionId); |
||
| 303 | if (empty($sessionInfo)) { |
||
| 304 | continue; |
||
| 305 | } |
||
| 306 | $users = SessionManager::get_users_by_session( |
||
| 307 | $sessionId, |
||
| 308 | 0, |
||
| 309 | false, |
||
| 310 | $urlId |
||
| 311 | ); |
||
| 312 | |||
| 313 | $coachId = $sessionInfo['id_coach']; |
||
| 314 | |||
| 315 | if (empty($users) || empty($coachId)) { |
||
| 316 | continue; |
||
| 317 | } |
||
| 318 | |||
| 319 | $coachList = []; |
||
| 320 | if ($users) { |
||
| 321 | $sendToCoaches = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 322 | $result['id'], |
||
| 323 | 'send_to_coaches' |
||
| 324 | ); |
||
| 325 | $courseList = SessionManager::getCoursesInSession($sessionId); |
||
| 326 | if (!empty($sendToCoaches) && !empty($sendToCoaches['value']) && 1 == $sendToCoaches['value']) { |
||
| 327 | foreach ($courseList as $courseItemId) { |
||
| 328 | $coaches = SessionManager::getCoachesByCourseSession( |
||
| 329 | $sessionId, |
||
| 330 | $courseItemId |
||
| 331 | ); |
||
| 332 | $coachList = array_merge($coachList, $coaches); |
||
| 333 | } |
||
| 334 | $coachList = array_unique($coachList); |
||
| 335 | } |
||
| 336 | |||
| 337 | $this->update(['id' => $result['id'], 'sent' => 1]); |
||
| 338 | $attachments = $this->getAttachmentToString($result['id']); |
||
| 339 | $subject = $result['subject']; |
||
| 340 | |||
| 341 | $courseInfo = []; |
||
| 342 | if (!empty($courseList)) { |
||
| 343 | $courseId = current($courseList); |
||
| 344 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 345 | } |
||
| 346 | |||
| 347 | $message = ''; |
||
| 348 | foreach ($users as $user) { |
||
| 349 | // Take original message |
||
| 350 | $message = $result['message']; |
||
| 351 | $userInfo = api_get_user_info($user['user_id']); |
||
| 352 | |||
| 353 | $progress = ''; |
||
| 354 | if (!empty($sessionInfo) && !empty($courseInfo)) { |
||
| 355 | $progress = Tracking::get_avg_student_progress( |
||
| 356 | $user['user_id'], |
||
| 357 | $courseInfo['code'], |
||
| 358 | [], |
||
| 359 | $sessionId |
||
| 360 | ); |
||
| 361 | } |
||
| 362 | |||
| 363 | if (is_numeric($progress)) { |
||
| 364 | $progress = $progress.'%'; |
||
| 365 | } else { |
||
| 366 | $progress = '0%'; |
||
| 367 | } |
||
| 368 | |||
| 369 | $startTime = api_get_local_time( |
||
| 370 | $sessionInfo['access_start_date'], |
||
| 371 | null, |
||
| 372 | null, |
||
| 373 | true |
||
| 374 | ); |
||
| 375 | $endTime = api_get_local_time( |
||
| 376 | $sessionInfo['access_end_date'], |
||
| 377 | null, |
||
| 378 | null, |
||
| 379 | true |
||
| 380 | ); |
||
| 381 | |||
| 382 | $generalCoach = ''; |
||
| 383 | $generalCoachEmail = ''; |
||
| 384 | if (!empty($sessionInfo['id_coach'])) { |
||
| 385 | $coachInfo = api_get_user_info($sessionInfo['id_coach']); |
||
| 386 | if (!empty($coachInfo)) { |
||
| 387 | $generalCoach = $coachInfo['complete_name']; |
||
| 388 | $generalCoachEmail = $coachInfo['email']; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | $tags = [ |
||
| 393 | '((session_name))' => $sessionInfo['name'], |
||
| 394 | '((session_start_date))' => $startTime, |
||
| 395 | '((general_coach))' => $generalCoach, |
||
| 396 | '((general_coach_email))' => $generalCoachEmail, |
||
| 397 | '((session_end_date))' => $endTime, |
||
| 398 | '((user_complete_name))' => $userInfo['complete_name'], |
||
| 399 | '((user_firstname))' => $userInfo['firstname'], |
||
| 400 | '((user_lastname))' => $userInfo['lastname'], |
||
| 401 | '((user_first_name))' => $userInfo['firstname'], |
||
| 402 | '((user_last_name))' => $userInfo['lastname'], |
||
| 403 | '((lp_progress))' => $progress, |
||
| 404 | ]; |
||
| 405 | |||
| 406 | $message = str_replace(array_keys($tags), $tags, $message); |
||
| 407 | $message .= $attachments; |
||
| 408 | |||
| 409 | MessageManager::send_message_simple( |
||
| 410 | $userInfo['user_id'], |
||
| 411 | $subject, |
||
| 412 | $message, |
||
| 413 | $coachId |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | |||
| 417 | $message = get_lang('You\'re receiving a copy because, you\'re a course coach'). |
||
| 418 | '<br /><br />'.$message; |
||
| 419 | |||
| 420 | foreach ($coachList as $courseCoachId) { |
||
| 421 | MessageManager::send_message_simple( |
||
| 422 | $courseCoachId, |
||
| 423 | get_lang('You\'re receiving a copy because, you\'re a course coach').' '.$subject, |
||
| 424 | $message, |
||
| 425 | $coachId |
||
| 426 | ); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | $messagesSent++; |
||
| 430 | } |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | return $messagesSent; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | public function getTags() |
||
| 441 | { |
||
| 442 | return [ |
||
| 443 | '((session_name))', |
||
| 444 | '((session_start_date))', |
||
| 445 | '((session_end_date))', |
||
| 446 | '((general_coach))', |
||
| 447 | '((general_coach_email))', |
||
| 448 | '((user_complete_name))', |
||
| 449 | '((user_first_name))', |
||
| 450 | '((user_last_name))', |
||
| 451 | '((lp_progress))', |
||
| 452 | ]; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | public function allowed() |
||
| 461 | } |
||
| 462 | |||
| 463 | private function setTagsInForm(FormValidator $form) |
||
| 464 | { |
||
| 465 | $form->addLabel( |
||
| 466 | get_lang('Tags'), |
||
| 467 | Display::return_message( |
||
| 468 | implode('<br />', $this->getTags()), |
||
| 469 | 'normal', |
||
| 471 | ) |
||
| 472 | ); |
||
| 473 | } |
||
| 475 |