| Total Complexity | 61 |
| Total Lines | 549 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseLegalPlugin 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 CourseLegalPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CourseLegalPlugin extends Plugin |
||
| 9 | { |
||
| 10 | public $isCoursePlugin = true; |
||
| 11 | |||
| 12 | // When creating a new course this settings are added to the course |
||
| 13 | public $course_settings = [ |
||
| 14 | [ |
||
| 15 | 'name' => 'courselegal', |
||
| 16 | 'type' => 'text', |
||
| 17 | ], |
||
| 18 | ]; |
||
| 19 | |||
| 20 | protected function __construct() |
||
| 21 | { |
||
| 22 | parent::__construct( |
||
| 23 | '0.1', |
||
| 24 | 'Julio Montoya', |
||
| 25 | [ |
||
| 26 | 'tool_enable' => 'boolean', |
||
| 27 | ] |
||
| 28 | ); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @return CourseLegalPlugin |
||
| 33 | */ |
||
| 34 | public static function create() |
||
| 35 | { |
||
| 36 | static $result = null; |
||
| 37 | |||
| 38 | return $result ?: $result = new self(); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | public function getTeacherLink() |
||
| 45 | { |
||
| 46 | $link = null; |
||
| 47 | if (api_is_allowed_to_edit()) { |
||
| 48 | $url = api_get_path(WEB_PLUGIN_PATH).'CourseLegal/start.php?'.api_get_cidreq(); |
||
| 49 | $link = Display::url( |
||
| 50 | $this->get_lang('CourseLegal'), |
||
| 51 | $url, |
||
| 52 | ['class' => 'btn'] |
||
| 53 | ); |
||
| 54 | } |
||
| 55 | |||
| 56 | return $link; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param int $userId |
||
| 61 | * @param int $courseId |
||
| 62 | * @param int $sessionId |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getUserAcceptedLegal($userId, $courseId, $sessionId) |
||
| 67 | { |
||
| 68 | $userId = (int) $userId; |
||
| 69 | $courseId = (int) $courseId; |
||
| 70 | $sessionId = (int) $sessionId; |
||
| 71 | |||
| 72 | $table = Database::get_main_table('session_rel_course_rel_user_legal'); |
||
| 73 | $sql = "SELECT * |
||
| 74 | FROM $table |
||
| 75 | WHERE user_id = $userId AND c_id = $courseId AND session_id = $sessionId"; |
||
| 76 | $result = Database::query($sql); |
||
| 77 | $data = []; |
||
| 78 | if (Database::num_rows($result) > 0) { |
||
| 79 | $data = Database::fetch_assoc($result); |
||
| 80 | } |
||
| 81 | |||
| 82 | return $data; |
||
|
|
|||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param int $userId |
||
| 87 | * @param string $courseCode |
||
| 88 | * @param int $sessionId |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | public function isUserAcceptedLegal($userId, $courseCode, $sessionId) |
||
| 93 | { |
||
| 94 | $courseInfo = api_get_course_info($courseCode); |
||
| 95 | $courseId = $courseInfo['real_id']; |
||
| 96 | $result = $this->getUserAcceptedLegal($userId, $courseId, $sessionId); |
||
| 97 | |||
| 98 | if (!empty($result)) { |
||
| 99 | if (1 == $result['mail_agreement'] && |
||
| 100 | 1 == $result['web_agreement'] |
||
| 101 | ) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param int $userId |
||
| 111 | * @param int $courseCode |
||
| 112 | * @param int $sessionId |
||
| 113 | * @param bool $sendEmail Optional. Indicate whether the mail must be sent. Default is true |
||
| 114 | */ |
||
| 115 | public function saveUserLegal($userId, $courseCode, $sessionId, $sendEmail = true) |
||
| 116 | { |
||
| 117 | $courseInfo = api_get_course_info($courseCode); |
||
| 118 | $courseId = $courseInfo['real_id']; |
||
| 119 | $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId); |
||
| 120 | |||
| 121 | $id = false; |
||
| 122 | if (empty($data)) { |
||
| 123 | $table = Database::get_main_table( |
||
| 124 | 'session_rel_course_rel_user_legal' |
||
| 125 | ); |
||
| 126 | $uniqueId = api_get_unique_id(); |
||
| 127 | $values = [ |
||
| 128 | 'user_id' => $userId, |
||
| 129 | 'c_id' => $courseId, |
||
| 130 | 'session_id' => $sessionId, |
||
| 131 | 'web_agreement' => 1, |
||
| 132 | 'web_agreement_date' => api_get_utc_datetime(), |
||
| 133 | 'mail_agreement_link' => $uniqueId, |
||
| 134 | ]; |
||
| 135 | $id = Database::insert($table, $values); |
||
| 136 | |||
| 137 | if ($sendEmail) { |
||
| 138 | $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | return $id; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $userId |
||
| 147 | * @param int $courseId |
||
| 148 | * @param int $sessionId |
||
| 149 | */ |
||
| 150 | public function updateMailAgreementLink($userId, $courseId, $sessionId) |
||
| 151 | { |
||
| 152 | $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId); |
||
| 153 | if (!empty($data)) { |
||
| 154 | $table = Database::get_main_table( |
||
| 155 | 'session_rel_course_rel_user_legal' |
||
| 156 | ); |
||
| 157 | $uniqueId = api_get_unique_id(); |
||
| 158 | Database::update( |
||
| 159 | $table, |
||
| 160 | ['mail_agreement_link' => $uniqueId], |
||
| 161 | ['id = ? ' => [$data['id']]] |
||
| 162 | ); |
||
| 163 | $this->sendMailLink($uniqueId, $userId, $courseId, $sessionId); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param int $userId |
||
| 169 | * @param int $courseId |
||
| 170 | * @param int $sessionId |
||
| 171 | */ |
||
| 172 | public function deleteUserAgreement($userId, $courseId, $sessionId) |
||
| 173 | { |
||
| 174 | $data = $this->getUserAcceptedLegal($userId, $courseId, $sessionId); |
||
| 175 | if (!empty($data)) { |
||
| 176 | $table = Database::get_main_table( |
||
| 177 | 'session_rel_course_rel_user_legal' |
||
| 178 | ); |
||
| 179 | Database::delete( |
||
| 180 | $table, |
||
| 181 | ['id = ? ' => [$data['id']]] |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $uniqueId |
||
| 188 | * @param int $userId |
||
| 189 | * @param int $courseId |
||
| 190 | * @param int $sessionId |
||
| 191 | */ |
||
| 192 | public function sendMailLink($uniqueId, $userId, $courseId, $sessionId) |
||
| 193 | { |
||
| 194 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 195 | $courseCode = $courseInfo['code']; |
||
| 196 | |||
| 197 | $url = api_get_path(WEB_CODE_PATH).'course_info/legal.php?web_agreement_link='.$uniqueId.'&course_code='.Security::remove_XSS($courseCode).'&session_id='.$sessionId; |
||
| 198 | $courseUrl = Display::url($url, $url); |
||
| 199 | $sessionInfo = api_get_session_info($sessionId); |
||
| 200 | $sesstionTitle = null; |
||
| 201 | |||
| 202 | if (!empty($sessionInfo)) { |
||
| 203 | $sesstionTitle = ' ('.$sessionInfo['title'].')'; |
||
| 204 | } |
||
| 205 | |||
| 206 | $courseTitle = $courseInfo['title'].$sesstionTitle; |
||
| 207 | |||
| 208 | $subject = $this->get_lang('MailAgreement'); |
||
| 209 | $message = sprintf($this->get_lang('MailAgreementWasSentWithClickX'), $courseTitle, $courseUrl); |
||
| 210 | MessageManager::send_message_simple($userId, $subject, $message); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $link |
||
| 215 | * @param int $userId |
||
| 216 | * @param int $courseId |
||
| 217 | * @param int $sessionId |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function saveUserMailLegal($link, $userId, $courseId, $sessionId) |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param int $courseId |
||
| 242 | * @param int $sessionId |
||
| 243 | * @param string $filePath |
||
| 244 | */ |
||
| 245 | public function warnUsersByEmail($courseId, $sessionId, $filePath = null) |
||
| 246 | { |
||
| 247 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 248 | $courseCode = $courseInfo['code']; |
||
| 249 | |||
| 250 | if (empty($sessionId)) { |
||
| 251 | $students = CourseManager::get_student_list_from_course_code($courseCode, false); |
||
| 252 | } else { |
||
| 253 | $students = CourseManager::get_student_list_from_course_code($courseCode, true, $sessionId); |
||
| 254 | } |
||
| 255 | |||
| 256 | $url = api_get_course_url($courseId, $sessionId); |
||
| 257 | $url = Display::url($url, $url); |
||
| 258 | |||
| 259 | $subject = $this->get_lang('AgreementUpdated'); |
||
| 260 | $message = sprintf($this->get_lang('AgreementWasUpdatedClickHere'), $url); |
||
| 261 | |||
| 262 | $dataFile = []; |
||
| 263 | if (!empty($filePath)) { |
||
| 264 | $dataFile = [ |
||
| 265 | 'path' => $filePath, |
||
| 266 | 'filename' => basename($filePath), |
||
| 267 | ]; |
||
| 268 | $message = sprintf($this->get_lang('AgreementWasUpdatedClickHere'), $url)." \n"; |
||
| 269 | $message .= $this->get_lang('TheAgreementIsAttachedInThisEmail'); |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!empty($students)) { |
||
| 273 | foreach ($students as $student) { |
||
| 274 | $userInfo = api_get_user_info($student['user_id']); |
||
| 275 | api_mail_html( |
||
| 276 | $userInfo['complete_name'], |
||
| 277 | $userInfo['email'], |
||
| 278 | $subject, |
||
| 279 | $message, |
||
| 280 | null, |
||
| 281 | null, |
||
| 282 | null, |
||
| 283 | $dataFile |
||
| 284 | ); |
||
| 285 | //MessageManager::send_message_simple($student['user_id'], $subject, $message); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param int $courseId |
||
| 292 | * @param int $sessionId |
||
| 293 | * @param string $order |
||
| 294 | * |
||
| 295 | * @return array |
||
| 296 | */ |
||
| 297 | public function getUserAgreementList($courseId, $sessionId, $order = null) |
||
| 298 | { |
||
| 299 | $courseId = (int) $courseId; |
||
| 300 | $sessionId = (int) $sessionId; |
||
| 301 | |||
| 302 | $table = Database::get_main_table('session_rel_course_rel_user_legal'); |
||
| 303 | $userTable = Database::get_main_table(TABLE_MAIN_USER); |
||
| 304 | $sql = "SELECT * |
||
| 305 | FROM $table s INNER JOIN $userTable u |
||
| 306 | ON u.id = s.user_id |
||
| 307 | WHERE c_id = $courseId AND session_id = $sessionId "; |
||
| 308 | |||
| 309 | if (!empty($order)) { |
||
| 310 | $sql .= $order; |
||
| 311 | } |
||
| 312 | $result = Database::query($sql); |
||
| 313 | $data = []; |
||
| 314 | if (Database::num_rows($result) > 0) { |
||
| 315 | $data = Database::store_result($result, 'ASSOC'); |
||
| 316 | } |
||
| 317 | |||
| 318 | return $data; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param int $courseId |
||
| 323 | * @param int $sessionId |
||
| 324 | */ |
||
| 325 | public function removePreviousAgreements($courseId, $sessionId) |
||
| 326 | { |
||
| 327 | $table = Database::get_main_table('session_rel_course_rel_user_legal'); |
||
| 328 | $sessionId = (int) $sessionId; |
||
| 329 | $courseId = (int) $courseId; |
||
| 330 | $sql = "DELETE FROM $table |
||
| 331 | WHERE c_id = '$courseId' AND session_id = $sessionId "; |
||
| 332 | Database::query($sql); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $values |
||
| 337 | * @param array $file $_FILES['uploaded_file'] |
||
| 338 | * @param bool $deleteFile |
||
| 339 | */ |
||
| 340 | public function save($values, $file = [], $deleteFile = false) |
||
| 341 | { |
||
| 342 | $table = Database::get_main_table('session_rel_course_legal'); |
||
| 343 | |||
| 344 | $courseId = $values['c_id']; |
||
| 345 | $sessionId = $values['session_id']; |
||
| 346 | |||
| 347 | $conditions = [ |
||
| 348 | 'c_id' => $courseId, |
||
| 349 | 'session_id' => $sessionId, |
||
| 350 | ]; |
||
| 351 | |||
| 352 | $course = api_get_course_info_by_id($courseId); |
||
| 353 | |||
| 354 | $legalData = $this->getData($courseId, $sessionId); |
||
| 355 | $coursePath = api_get_path(SYS_COURSE_PATH).$course['directory'].'/CourseLegal'; |
||
| 356 | $uploadResult = $coursePath.'/'.$legalData['filename']; |
||
| 357 | |||
| 358 | if (!is_dir($coursePath)) { |
||
| 359 | mkdir($coursePath, api_get_permissions_for_new_directories()); |
||
| 360 | } |
||
| 361 | $uploadOk = process_uploaded_file($file, false); |
||
| 362 | $fileName = null; |
||
| 363 | |||
| 364 | if ($uploadOk) { |
||
| 365 | $uploadResult = null; |
||
| 366 | /*$uploadResult = handle_uploaded_document( |
||
| 367 | $course, |
||
| 368 | $file, |
||
| 369 | $coursePath, |
||
| 370 | '/', |
||
| 371 | api_get_user_id(), |
||
| 372 | api_get_group_id(), |
||
| 373 | null, |
||
| 374 | false, |
||
| 375 | false, |
||
| 376 | false, |
||
| 377 | true |
||
| 378 | );*/ |
||
| 379 | |||
| 380 | if ($uploadResult) { |
||
| 381 | $fileName = basename($uploadResult); |
||
| 382 | // Delete old one if exists. |
||
| 383 | if ($legalData) { |
||
| 384 | if (!empty($legalData['filename'])) { |
||
| 385 | $fileToDelete = $coursePath.'/'.$legalData['filename']; |
||
| 386 | if (file_exists($fileToDelete)) { |
||
| 387 | unlink($fileToDelete); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | $conditions['content'] = $values['content']; |
||
| 395 | $conditions['filename'] = $fileName; |
||
| 396 | |||
| 397 | if (empty($legalData)) { |
||
| 398 | $id = Database::insert($table, $conditions); |
||
| 399 | } else { |
||
| 400 | $id = $legalData['id']; |
||
| 401 | |||
| 402 | $updateParams = [ |
||
| 403 | 'content' => $values['content'], |
||
| 404 | ]; |
||
| 405 | |||
| 406 | if (!empty($fileName)) { |
||
| 407 | $updateParams['filename'] = $fileName; |
||
| 408 | } |
||
| 409 | |||
| 410 | Database::update( |
||
| 411 | $table, |
||
| 412 | $updateParams, |
||
| 413 | ['id = ? ' => $id] |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | |||
| 417 | if ($deleteFile) { |
||
| 418 | Database::update( |
||
| 419 | $table, |
||
| 420 | ['filename' => ''], |
||
| 421 | ['id = ? ' => $id] |
||
| 422 | ); |
||
| 423 | if (!empty($legalData['filename'])) { |
||
| 424 | $fileToDelete = $coursePath.'/'.$legalData['filename']; |
||
| 425 | if (file_exists($fileToDelete)) { |
||
| 426 | unlink($fileToDelete); |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | if (isset($values['remove_previous_agreements']) && |
||
| 432 | !empty($values['remove_previous_agreements']) |
||
| 433 | ) { |
||
| 434 | $this->removePreviousAgreements($courseId, $sessionId); |
||
| 435 | } |
||
| 436 | |||
| 437 | $warnUsers = isset($values['warn_users_by_email']) ? $values['warn_users_by_email'] : null; |
||
| 438 | |||
| 439 | switch ($warnUsers) { |
||
| 440 | case '1': |
||
| 441 | // Nothing |
||
| 442 | break; |
||
| 443 | case '2': |
||
| 444 | // Send mail |
||
| 445 | $this->warnUsersByEmail($courseId, $sessionId); |
||
| 446 | |||
| 447 | break; |
||
| 448 | case '3': |
||
| 449 | // Send mail + attachment if exists. |
||
| 450 | if (!empty($legalData['filename'])) { |
||
| 451 | $this->warnUsersByEmail( |
||
| 452 | $courseId, |
||
| 453 | $sessionId, |
||
| 454 | $uploadResult |
||
| 455 | ); |
||
| 456 | } |
||
| 457 | |||
| 458 | break; |
||
| 459 | } |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param int $courseId |
||
| 464 | * @param int $sessionId |
||
| 465 | * |
||
| 466 | * @return array|mixed |
||
| 467 | */ |
||
| 468 | public function getData($courseId, $sessionId) |
||
| 469 | { |
||
| 470 | $table = Database::get_main_table('session_rel_course_legal'); |
||
| 471 | $conditions = [ |
||
| 472 | 'c_id = ? AND session_id = ? ' => [ |
||
| 473 | $courseId, |
||
| 474 | $sessionId, |
||
| 475 | ], |
||
| 476 | ]; |
||
| 477 | |||
| 478 | $result = Database::select('*', $table, ['where' => $conditions]); |
||
| 479 | |||
| 480 | return isset($result) && !empty($result) ? current($result) : []; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param int $courseId |
||
| 485 | * @param int $sessionId |
||
| 486 | * |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getCurrentFile($courseId, $sessionId) |
||
| 504 | ); |
||
| 505 | } |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | public function install() |
||
| 510 | { |
||
| 511 | $table = Database::get_main_table('session_rel_course_legal'); |
||
| 512 | $sql = "CREATE TABLE IF NOT EXISTS $table ( |
||
| 513 | id int PRIMARY KEY AUTO_INCREMENT, |
||
| 514 | c_id int, |
||
| 515 | session_id int, |
||
| 516 | content text, |
||
| 517 | filename varchar(255) |
||
| 518 | )"; |
||
| 519 | Database::query($sql); |
||
| 520 | |||
| 521 | $table = Database::get_main_table('session_rel_course_rel_user_legal'); |
||
| 522 | |||
| 523 | $sql = "CREATE TABLE IF NOT EXISTS $table ( |
||
| 524 | id int PRIMARY KEY AUTO_INCREMENT, |
||
| 525 | user_id int, |
||
| 526 | c_id int, |
||
| 527 | session_id int, |
||
| 528 | web_agreement varchar(255), |
||
| 529 | web_agreement_date datetime, |
||
| 530 | mail_agreement varchar(255), |
||
| 531 | mail_agreement_date datetime, |
||
| 532 | mail_agreement_link varchar(255) |
||
| 533 | )"; |
||
| 534 | Database::query($sql); |
||
| 535 | |||
| 536 | // Installing course settings |
||
| 537 | $this->install_course_fields_in_all_courses(false); |
||
| 538 | } |
||
| 539 | |||
| 540 | public function uninstall() |
||
| 541 | { |
||
| 542 | $table = Database::get_main_table('session_rel_course_legal'); |
||
| 543 | $sql = "DROP TABLE $table "; |
||
| 544 | Database::query($sql); |
||
| 545 | |||
| 546 | $table = Database::get_main_table('session_rel_course_rel_user_legal'); |
||
| 547 | $sql = "DROP TABLE $table "; |
||
| 548 | Database::query($sql); |
||
| 549 | |||
| 550 | // Deleting course settings |
||
| 551 | $this->uninstall_course_fields_in_all_courses($this->course_settings); |
||
| 552 | } |
||
| 553 | |||
| 554 | public function get_name() |
||
| 557 | } |
||
| 558 | } |
||
| 559 |