| Total Complexity | 68 |
| Total Lines | 715 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WSCourse 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 WSCourse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class WSCourse extends WS |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Deletes a course. |
||
| 16 | * |
||
| 17 | * @param string API secret key |
||
| 18 | * @param string Course id field name |
||
| 19 | * @param string Course id value |
||
| 20 | */ |
||
| 21 | public function DeleteCourse( |
||
| 22 | $secret_key, |
||
| 23 | $course_id_field_name, |
||
| 24 | $course_id_value |
||
| 25 | ) { |
||
| 26 | $verifKey = $this->verifyKey($secret_key); |
||
| 27 | if ($verifKey instanceof WSError) { |
||
| 28 | $this->handleError($verifKey); |
||
| 29 | } else { |
||
| 30 | $result = $this->deleteCourseHelper( |
||
| 31 | $course_id_field_name, |
||
| 32 | $course_id_value |
||
| 33 | ); |
||
| 34 | if ($result instanceof WSError) { |
||
| 35 | $this->handleError($result); |
||
| 36 | } |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Deletes multiple courses. |
||
| 42 | * |
||
| 43 | * @param string API secret key |
||
| 44 | * @param array Array of courses with elements of the form |
||
| 45 | * array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') |
||
| 46 | * |
||
| 47 | * @return array Array with elements like |
||
| 48 | * array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). |
||
| 49 | * Note that if the result array contains a code different |
||
| 50 | * than 0, an error occured |
||
| 51 | */ |
||
| 52 | public function DeleteCourses($secret_key, $courses) |
||
| 53 | { |
||
| 54 | $verifKey = $this->verifyKey($secret_key); |
||
| 55 | if ($verifKey instanceof WSError) { |
||
| 56 | $this->handleError($verifKey); |
||
| 57 | } else { |
||
| 58 | $results = []; |
||
| 59 | foreach ($courses as $course) { |
||
| 60 | $result_tmp = []; |
||
| 61 | $result_op = $this->deleteCourseHelper( |
||
| 62 | $course['course_id_field_name'], |
||
| 63 | $course['course_id_value'] |
||
| 64 | ); |
||
| 65 | $result_tmp['course_id_value'] = $course['course_id_value']; |
||
| 66 | if ($result_op instanceof WSError) { |
||
| 67 | // Return the error in the results |
||
| 68 | $result_tmp['result'] = $result_op->toArray(); |
||
| 69 | } else { |
||
| 70 | $result_tmp['result'] = $this->getSuccessfulResult(); |
||
| 71 | } |
||
| 72 | $results[] = $result_tmp; |
||
| 73 | } |
||
| 74 | |||
| 75 | return $results; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Creates a course. |
||
| 81 | * |
||
| 82 | * @param string API secret key |
||
| 83 | * @param string Title |
||
| 84 | * @param string Category code |
||
| 85 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 86 | * @param string Tutor name |
||
| 87 | * @param string Course admin user id field name |
||
| 88 | * @param string Course admin user id value |
||
| 89 | * @param string Course language |
||
| 90 | * @param string Course id field name |
||
| 91 | * @param string Course id value |
||
| 92 | * @param array Course extra fields |
||
| 93 | * |
||
| 94 | * @return int Course id generated |
||
| 95 | */ |
||
| 96 | public function CreateCourse( |
||
| 97 | $secret_key, |
||
| 98 | $title, |
||
| 99 | $category_code, |
||
| 100 | $wanted_code, |
||
| 101 | $tutor_name, |
||
| 102 | $course_admin_user_id_field_name, |
||
| 103 | $course_admin_user_id_value, |
||
| 104 | $language, |
||
| 105 | $course_id_field_name, |
||
| 106 | $course_id_value, |
||
| 107 | $extras |
||
| 108 | ) { |
||
| 109 | // First, verify the secret key |
||
| 110 | $verifKey = $this->verifyKey($secret_key); |
||
| 111 | if ($verifKey instanceof WSError) { |
||
| 112 | $this->handleError($verifKey); |
||
| 113 | } else { |
||
| 114 | $result = $this->createCourseHelper( |
||
| 115 | $title, |
||
| 116 | $category_code, |
||
| 117 | $wanted_code, |
||
| 118 | $tutor_name, |
||
| 119 | $course_admin_user_id_field_name, |
||
| 120 | $course_admin_user_id_value, |
||
| 121 | $language, |
||
| 122 | $course_id_field_name, |
||
| 123 | $course_id_value, |
||
| 124 | $extras |
||
| 125 | ); |
||
| 126 | if ($result instanceof WSError) { |
||
| 127 | $this->handleError($result); |
||
| 128 | } else { |
||
| 129 | return $result; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Create multiple courses. |
||
| 136 | * |
||
| 137 | * @param string API secret key |
||
| 138 | * @param array Courses to be created, with elements following the structure presented in CreateCourse |
||
| 139 | * |
||
| 140 | * @return array Array with elements of the form |
||
| 141 | * array('course_id_value' => 'original value sent', 'course_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful')) |
||
| 142 | */ |
||
| 143 | public function CreateCourses($secret_key, $courses) |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Edits a course. |
||
| 186 | * |
||
| 187 | * @param string API secret key |
||
| 188 | * @param string Course id field name |
||
| 189 | * @param string Course id value |
||
| 190 | * @param string Title |
||
| 191 | * @param string Category code |
||
| 192 | * @param string Department name |
||
| 193 | * @param string Department url |
||
| 194 | * @param string Course language |
||
| 195 | * @param int Visibility |
||
| 196 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 197 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 198 | * @param string Visual code |
||
| 199 | * @param array Course extra fields |
||
| 200 | */ |
||
| 201 | public function EditCourse( |
||
| 202 | $secret_key, |
||
| 203 | $course_id_field_name, |
||
| 204 | $course_id_value, |
||
| 205 | $title, |
||
| 206 | $category_code, |
||
| 207 | $department_name, |
||
| 208 | $department_url, |
||
| 209 | $language, |
||
| 210 | $visibility, |
||
| 211 | $subscribe, |
||
| 212 | $unsubscribe, |
||
| 213 | $visual_code, |
||
| 214 | $extras |
||
| 215 | ) { |
||
| 216 | $verifKey = $this->verifyKey($secret_key); |
||
| 217 | if ($verifKey instanceof WSError) { |
||
| 218 | $this->handleError($verifKey); |
||
| 219 | } else { |
||
| 220 | $result = $this->editCourseHelper( |
||
| 221 | $course_id_field_name, |
||
| 222 | $course_id_value, |
||
| 223 | $title, |
||
| 224 | $category_code, |
||
| 225 | $department_name, |
||
| 226 | $department_url, |
||
| 227 | $language, |
||
| 228 | $visibility, |
||
| 229 | $subscribe, |
||
| 230 | $unsubscribe, |
||
| 231 | $visual_code, |
||
| 232 | $extras |
||
| 233 | ); |
||
| 234 | if ($result instanceof WSError) { |
||
| 235 | $this->handleError($result); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * List courses. |
||
| 242 | * |
||
| 243 | * @param string API secret key |
||
| 244 | * @param string A list of visibility filter we want to apply |
||
| 245 | * |
||
| 246 | * @return array An array with elements of the form |
||
| 247 | * ('id' => 'Course internal id', 'code' => 'Course code', 'title' => 'Course title', 'language' => 'Course language', 'visibility' => 'Course visibility', |
||
| 248 | * 'category_name' => 'Name of the category of the course', 'number_students' => 'Number of students in the course', 'external_course_id' => 'External course id') |
||
| 249 | */ |
||
| 250 | public function ListCourses( |
||
| 251 | $secret_key, |
||
| 252 | $visibility = 'public,public-registered,private,closed' |
||
| 253 | ) { |
||
| 254 | $verifKey = $this->verifyKey($secret_key); |
||
| 255 | if ($verifKey instanceof WSError) { |
||
| 256 | $this->handleError($verifKey); |
||
| 257 | } else { |
||
| 258 | $visibilities = split(',', $visibility); |
||
| 259 | $vis = [ |
||
| 260 | 'public' => '3', |
||
| 261 | 'public-registered' => '2', |
||
| 262 | 'private' => '1', |
||
| 263 | 'closed' => '0', |
||
| 264 | ]; |
||
| 265 | foreach ($visibilities as $p => $visibility) { |
||
| 266 | $visibilities[$p] = $vis[$visibility]; |
||
| 267 | } |
||
| 268 | $courses_result = []; |
||
| 269 | $category_names = []; |
||
| 270 | |||
| 271 | $courses = CourseManager::get_courses_list(); |
||
| 272 | foreach ($courses as $course) { |
||
| 273 | //skip elements that do not match required visibility |
||
| 274 | if (!in_array($course['visibility'], $visibilities)) { |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | $course_tmp = []; |
||
| 278 | $course_tmp['id'] = $course['id']; |
||
| 279 | $course_tmp['code'] = $course['code']; |
||
| 280 | $course_tmp['title'] = $course['title']; |
||
| 281 | $course_tmp['language'] = $course['course_language']; |
||
| 282 | $course_tmp['visibility'] = $course['visibility']; |
||
| 283 | |||
| 284 | // Determining category name |
||
| 285 | if ($category_names[$course['category_code']]) { |
||
| 286 | $course_tmp['category_name'] = $category_names[$course['category_code']]; |
||
| 287 | } else { |
||
| 288 | $category = CourseManager::get_course_category( |
||
| 289 | $course['category_code'] |
||
| 290 | ); |
||
| 291 | $category_names[$course['category_code']] = $category['name']; |
||
| 292 | $course_tmp['category_name'] = $category['name']; |
||
| 293 | } |
||
| 294 | |||
| 295 | // Determining number of students registered in course |
||
| 296 | $user_list = CourseManager::get_user_list_from_course_code( |
||
| 297 | $course['code'], |
||
| 298 | 0 |
||
| 299 | ); |
||
| 300 | $course_tmp['number_students'] = count($user_list); |
||
| 301 | |||
| 302 | // Determining external course id - this code misses the external course id field name |
||
| 303 | // $course_tmp['external_course_id'] = CourseManager::get_course_extra_field_value($course_field_name, $course['code']); |
||
| 304 | |||
| 305 | $courses_result[] = $course_tmp; |
||
| 306 | } |
||
| 307 | |||
| 308 | return $courses_result; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Subscribe user to a course. |
||
| 314 | * |
||
| 315 | * @param string API secret key |
||
| 316 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 317 | * @param string course id value |
||
| 318 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 319 | * @param string User id value |
||
| 320 | * @param int Status (1 = Teacher, 5 = Student) |
||
| 321 | */ |
||
| 322 | public function SubscribeUserToCourse( |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Unsusbscribe user from course. |
||
| 350 | * |
||
| 351 | * @param string API secret key |
||
| 352 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 353 | * @param string course id value |
||
| 354 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 355 | * @param string User id value |
||
| 356 | */ |
||
| 357 | public function UnsubscribeUserFromCourse( |
||
| 358 | $secret_key, |
||
| 359 | $course_id_field_name, |
||
| 360 | $course_id_value, |
||
| 361 | $user_id_field_name, |
||
| 362 | $user_id_value |
||
| 363 | ) { |
||
| 364 | $verifKey = $this->verifyKey($secret_key); |
||
| 365 | if ($verifKey instanceof WSError) { |
||
| 366 | $this->handleError($verifKey); |
||
| 367 | } else { |
||
| 368 | $result = $this->changeUserSubscription( |
||
| 369 | $course_id_field_name, |
||
| 370 | $course_id_value, |
||
| 371 | $user_id_field_name, |
||
| 372 | $user_id_value, |
||
| 373 | 0 |
||
| 374 | ); |
||
| 375 | if ($result instanceof WSError) { |
||
| 376 | $this->handleError($result); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns the descriptions of a course, along with their id. |
||
| 383 | * |
||
| 384 | * @param string API secret key |
||
| 385 | * @param string Course id field name |
||
| 386 | * @param string Course id value |
||
| 387 | * |
||
| 388 | * @return array Returns an array with elements of the form |
||
| 389 | * array('course_desc_id' => 1, 'course_desc_title' => 'Title', 'course_desc_content' => 'Content') |
||
| 390 | */ |
||
| 391 | public function GetCourseDescriptions( |
||
| 392 | $secret_key, |
||
| 393 | $course_id_field_name, |
||
| 394 | $course_id_value |
||
| 395 | ) { |
||
| 396 | $verifKey = $this->verifyKey($secret_key); |
||
| 397 | if ($verifKey instanceof WSError) { |
||
| 398 | $this->handleError($verifKey); |
||
| 399 | } else { |
||
| 400 | $course_id = $this->getCourseId( |
||
| 401 | $course_id_field_name, |
||
| 402 | $course_id_value |
||
| 403 | ); |
||
| 404 | if ($course_id instanceof WSError) { |
||
| 405 | return $course_id; |
||
| 406 | } else { |
||
| 407 | // Course exists, get its descriptions |
||
| 408 | $descriptions = CourseDescription::get_descriptions($course_id); |
||
| 409 | $results = []; |
||
| 410 | foreach ($descriptions as $description) { |
||
| 411 | $results[] = [ |
||
| 412 | 'course_desc_id' => $description->get_description_type( |
||
| 413 | ), |
||
| 414 | 'course_desc_title' => $description->get_title(), |
||
| 415 | 'course_desc_content' => $description->get_content(), |
||
| 416 | ]; |
||
| 417 | } |
||
| 418 | |||
| 419 | return $results; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Edit course description. |
||
| 426 | * |
||
| 427 | * @param string API secret key |
||
| 428 | * @param string Course id field name |
||
| 429 | * @param string Course id value |
||
| 430 | * @param int Category id from course description |
||
| 431 | * @param string Description title |
||
| 432 | * @param string Course description content |
||
| 433 | */ |
||
| 434 | public function EditCourseDescription( |
||
| 477 | } |
||
| 478 | } |
||
| 479 | } |
||
| 480 | } |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Deletes a course (helper method). |
||
| 484 | * |
||
| 485 | * @param string Course id field name |
||
| 486 | * @param string Course id value |
||
| 487 | * |
||
| 488 | * @return mixed True if the course was successfully deleted, WSError otherwise |
||
| 489 | */ |
||
| 490 | protected function deleteCourseHelper( |
||
| 507 | } |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Creates a course (helper method). |
||
| 512 | * |
||
| 513 | * @param string Title |
||
| 514 | * @param string Category code |
||
| 515 | * @param string Wanted code. If it's not defined, it will be generated automatically |
||
| 516 | * @param string Tutor name |
||
| 517 | * @param string Course admin user id field name |
||
| 518 | * @param string Course admin user id value |
||
| 519 | * @param string Course language |
||
| 520 | * @param string Course id field name |
||
| 521 | * @param string Course id value |
||
| 522 | * @param array Course extra fields |
||
| 523 | * |
||
| 524 | * @return mixed Generated id if creation was successful, WSError otherwise |
||
| 525 | */ |
||
| 526 | protected function createCourseHelper( |
||
| 527 | $title, |
||
| 528 | $category_code, |
||
| 529 | $wanted_code, |
||
| 530 | $tutor_name, |
||
| 531 | $course_admin_user_id_field_name, |
||
| 532 | $course_admin_user_id_value, |
||
| 533 | $language, |
||
| 534 | $course_id_field_name, |
||
| 535 | $course_id_value, |
||
| 536 | $extras |
||
| 537 | ) { |
||
| 538 | // Add the original course id field name and value to the extra fields if needed |
||
| 539 | $extras_associative = []; |
||
| 540 | if ($course_id_field_name != "chamilo_course_id") { |
||
| 541 | $extras_associative[$course_id_field_name] = $course_id_value; |
||
| 542 | } |
||
| 543 | foreach ($extras as $extra) { |
||
| 544 | $extras_associative[$extra['field_name']] = $extra['field_value']; |
||
| 545 | } |
||
| 546 | $course_admin_id = $this->getUserId( |
||
| 547 | $course_admin_user_id_field_name, |
||
| 548 | $course_admin_user_id_value |
||
| 549 | ); |
||
| 550 | if ($course_admin_id instanceof WSError) { |
||
| 551 | return $course_admin_id; |
||
| 552 | } |
||
| 553 | if ($wanted_code == '') { |
||
| 554 | $wanted_code = CourseManager::generate_course_code($title); |
||
| 555 | } |
||
| 556 | $result = create_course( |
||
| 557 | $wanted_code, |
||
| 558 | $title, |
||
| 559 | $tutor_name, |
||
| 560 | $category_code, |
||
| 561 | $language, |
||
| 562 | $course_admin_id, |
||
| 563 | $this->_configuration['db_prefix'], |
||
| 564 | 0 |
||
| 565 | ); |
||
| 566 | if (!$result) { |
||
| 567 | return new WSError(202, 'There was an error creating the course'); |
||
| 568 | } else { |
||
| 569 | // Update extra fields |
||
| 570 | foreach ($extras_associative as $fname => $fvalue) { |
||
| 571 | CourseManager::update_course_extra_field_value( |
||
| 572 | $result, |
||
| 573 | $fname, |
||
| 574 | $fvalue |
||
| 575 | ); |
||
| 576 | } |
||
| 577 | // Get course id |
||
| 578 | $course_info = CourseManager::get_course_information($result); |
||
| 579 | |||
| 580 | return $course_info['real_id']; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Edits a course (helper method). |
||
| 586 | * |
||
| 587 | * @param string Course id field name |
||
| 588 | * @param string Course id value |
||
| 589 | * @param string Title |
||
| 590 | * @param string Category code |
||
| 591 | * @param string Department name |
||
| 592 | * @param string Department url |
||
| 593 | * @param string Course language |
||
| 594 | * @param int Visibility |
||
| 595 | * @param int Subscribe (0 = denied, 1 = allowed) |
||
| 596 | * @param int Unsubscribe (0 = denied, 1 = allowed) |
||
| 597 | * @param string Visual code |
||
| 598 | * @param array Course extra fields |
||
| 599 | * |
||
| 600 | * @return mixed True in case of success, WSError otherwise |
||
| 601 | */ |
||
| 602 | protected function editCourseHelper( |
||
| 672 | } |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Subscribe or unsubscribe user to a course (helper method). |
||
| 677 | * |
||
| 678 | * @param string Course id field name. Use "chamilo_course_id" to use internal id |
||
| 679 | * @param string course id value |
||
| 680 | * @param string User id field name. Use "chamilo_user_id" to use internal id |
||
| 681 | * @param string User id value |
||
| 682 | * @param int Set to 1 to subscribe, 0 to unsubscribe |
||
| 683 | * @param int Status (STUDENT or TEACHER) Used for subscription only |
||
| 684 | * |
||
| 685 | * @return mixed True if subscription or unsubscription was successful, false otherwise |
||
| 686 | */ |
||
| 687 | protected function changeUserSubscription( |
||
| 727 | ); |
||
| 728 | } |
||
| 729 | } |
||
| 734 |