| Total Complexity | 45 |
| Total Lines | 528 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseDescription 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 CourseDescription, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class CourseDescription |
||
| 20 | { |
||
| 21 | private $id; |
||
| 22 | private $course_id; |
||
| 23 | private $title; |
||
| 24 | private $content; |
||
| 25 | private $session_id; |
||
| 26 | private $description_type; |
||
| 27 | private $progress; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor. |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Returns an array of objects of type CourseDescription corresponding to |
||
| 38 | * a specific course, without session ids (session id = 0). |
||
| 39 | * |
||
| 40 | * @param int $course_id |
||
| 41 | * |
||
| 42 | * @return array Array of CourseDescriptions |
||
| 43 | */ |
||
| 44 | public static function get_descriptions($course_id) |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get all data of course description by session id, |
||
| 75 | * first you must set session_id property with the object CourseDescription. |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function get_description_data() |
||
| 80 | { |
||
| 81 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 82 | $condition_session = api_get_session_condition( |
||
| 83 | $this->session_id, |
||
| 84 | true, |
||
| 85 | true |
||
| 86 | ); |
||
| 87 | $course_id = $this->course_id ?: api_get_course_int_id(); |
||
| 88 | $sql = "SELECT * FROM $table |
||
| 89 | WHERE c_id = $course_id $condition_session |
||
| 90 | ORDER BY id "; |
||
| 91 | $rs = Database::query($sql); |
||
| 92 | $data = []; |
||
| 93 | while ($description = Database::fetch_array($rs)) { |
||
| 94 | $data['descriptions'][$description['id']] = $description; |
||
| 95 | } |
||
| 96 | |||
| 97 | return $data; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get all data by description and session id, |
||
| 102 | * first you must set session_id property with the object CourseDescription. |
||
| 103 | * |
||
| 104 | * @param int $description_type Description type |
||
| 105 | * @param string $courseId Course code (optional) |
||
| 106 | * @param int $session_id Session id (optional) |
||
| 107 | * |
||
| 108 | * @return array List of fields from the descriptions found of the given type |
||
| 109 | */ |
||
| 110 | public function get_data_by_description_type( |
||
| 111 | $description_type, |
||
| 112 | $courseId = null, |
||
| 113 | $session_id = null |
||
| 114 | ) { |
||
| 115 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 116 | if (empty($courseId)) { |
||
| 117 | $courseId = api_get_course_int_id(); |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!isset($session_id)) { |
||
| 121 | $session_id = $this->session_id; |
||
| 122 | } |
||
| 123 | $condition_session = api_get_session_condition($session_id); |
||
| 124 | $description_type = intval($description_type); |
||
| 125 | $sql = "SELECT * FROM $table |
||
| 126 | WHERE |
||
| 127 | c_id = $courseId AND |
||
| 128 | description_type = '$description_type' |
||
| 129 | $condition_session "; |
||
| 130 | $rs = Database::query($sql); |
||
| 131 | $data = []; |
||
| 132 | if ($description = Database::fetch_array($rs)) { |
||
| 133 | $data['description_title'] = $description['title']; |
||
| 134 | $data['description_content'] = $description['content']; |
||
| 135 | $data['progress'] = $description['progress']; |
||
| 136 | $data['id'] = $description['id']; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $data; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param int $id |
||
| 144 | * @param string $course_code |
||
| 145 | * @param int $session_id |
||
| 146 | * |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | public function get_data_by_id($id, $course_code = '', $session_id = null) |
||
| 150 | { |
||
| 151 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 152 | $course_id = api_get_course_int_id(); |
||
| 153 | |||
| 154 | if (!isset($session_id)) { |
||
| 155 | $session_id = $this->session_id; |
||
| 156 | } |
||
| 157 | $condition_session = api_get_session_condition($session_id); |
||
| 158 | if (!empty($course_code)) { |
||
| 159 | $course_info = api_get_course_info($course_code); |
||
| 160 | $course_id = $course_info['real_id']; |
||
| 161 | } |
||
| 162 | $id = intval($id); |
||
| 163 | $sql = "SELECT * FROM $table |
||
| 164 | WHERE c_id = $course_id AND id='$id' $condition_session "; |
||
| 165 | $rs = Database::query($sql); |
||
| 166 | $data = []; |
||
| 167 | if ($description = Database::fetch_array($rs)) { |
||
| 168 | $data['description_type'] = $description['description_type']; |
||
| 169 | $data['description_title'] = $description['title']; |
||
| 170 | $data['description_content'] = $description['content']; |
||
| 171 | $data['progress'] = $description['progress']; |
||
| 172 | } |
||
| 173 | |||
| 174 | return $data; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get maximum description type by session id, |
||
| 179 | * first you must set session_id properties with the object CourseDescription. |
||
| 180 | * |
||
| 181 | * @return int maximum description time adding one |
||
| 182 | */ |
||
| 183 | public function get_max_description_type() |
||
| 184 | { |
||
| 185 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 186 | $course_id = api_get_course_int_id(); |
||
| 187 | |||
| 188 | $sql = "SELECT MAX(description_type) as MAX |
||
| 189 | FROM $table |
||
| 190 | WHERE c_id = $course_id AND session_id='".$this->session_id."'"; |
||
| 191 | $rs = Database::query($sql); |
||
| 192 | $max = Database::fetch_array($rs); |
||
| 193 | |||
| 194 | if ($max['MAX'] >= 8) { |
||
| 195 | $description_type = 8; |
||
| 196 | } else { |
||
| 197 | $description_type = $max['MAX'] + 1; |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($description_type < ADD_BLOCK) { |
||
| 201 | $description_type = ADD_BLOCK; |
||
| 202 | } |
||
| 203 | |||
| 204 | return $description_type; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Insert a description to the course_description table, |
||
| 209 | * first you must set description_type, title, content, progress and |
||
| 210 | * session_id properties with the object CourseDescription. |
||
| 211 | * |
||
| 212 | * @return int affected rows |
||
| 213 | */ |
||
| 214 | public function insert() |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Update a description, first you must set description_type, title, content, progress |
||
| 253 | * and session_id properties with the object CourseDescription. |
||
| 254 | * |
||
| 255 | * @return int affected rows |
||
| 256 | */ |
||
| 257 | public function update() |
||
| 258 | { |
||
| 259 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 260 | $params = [ |
||
| 261 | 'title' => $this->title, |
||
| 262 | 'content' => $this->content, |
||
| 263 | 'progress' => intval($this->progress), |
||
| 264 | ]; |
||
| 265 | |||
| 266 | Database::update( |
||
| 267 | $table, |
||
| 268 | $params, |
||
| 269 | [ |
||
| 270 | 'id = ? AND session_id = ? AND c_id = ?' => [ |
||
| 271 | $this->id, |
||
| 272 | $this->session_id, |
||
| 273 | $this->course_id ? $this->course_id : api_get_course_int_id(), |
||
| 274 | ], |
||
| 275 | ] |
||
| 276 | ); |
||
| 277 | |||
| 278 | if ($this->id > 0) { |
||
| 279 | // Insert into item_property |
||
| 280 | api_item_property_update( |
||
| 281 | api_get_course_info(), |
||
| 282 | TOOL_COURSE_DESCRIPTION, |
||
| 283 | $this->id, |
||
| 284 | 'CourseDescriptionUpdated', |
||
| 285 | api_get_user_id() |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | |||
| 289 | return 1; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Delete a description, first you must set description_type and session_id |
||
| 294 | * properties with the object CourseDescription. |
||
| 295 | * |
||
| 296 | * @return int affected rows |
||
| 297 | */ |
||
| 298 | public function delete() |
||
| 299 | { |
||
| 300 | $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION); |
||
| 301 | $course_id = api_get_course_int_id(); |
||
| 302 | $sql = "DELETE FROM $table |
||
| 303 | WHERE |
||
| 304 | c_id = $course_id AND |
||
| 305 | id = '".intval($this->id)."' AND |
||
| 306 | session_id = '".intval($this->session_id)."'"; |
||
| 307 | $result = Database::query($sql); |
||
| 308 | $affected_rows = Database::affected_rows($result); |
||
| 309 | if ($this->id > 0) { |
||
| 310 | //insert into item_property |
||
| 311 | api_item_property_update( |
||
| 312 | api_get_course_info(), |
||
| 313 | TOOL_COURSE_DESCRIPTION, |
||
| 314 | $this->id, |
||
| 315 | 'CourseDescriptionDeleted', |
||
| 316 | api_get_user_id() |
||
| 317 | ); |
||
| 318 | } |
||
| 319 | |||
| 320 | return $affected_rows; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Get description titles by default. |
||
| 325 | * |
||
| 326 | * @return array |
||
| 327 | */ |
||
| 328 | public function get_default_description_title() |
||
| 329 | { |
||
| 330 | $default_description_titles = []; |
||
| 331 | $default_description_titles[1] = get_lang('GeneralDescription'); |
||
| 332 | $default_description_titles[2] = get_lang('Objectives'); |
||
| 333 | $default_description_titles[3] = get_lang('Topics'); |
||
| 334 | $default_description_titles[4] = get_lang('Methodology'); |
||
| 335 | $default_description_titles[5] = get_lang('CourseMaterial'); |
||
| 336 | $default_description_titles[6] = get_lang('HumanAndTechnicalResources'); |
||
| 337 | $default_description_titles[7] = get_lang('Assessment'); |
||
| 338 | $default_description_titles[8] = get_lang('Other'); |
||
| 339 | |||
| 340 | return $default_description_titles; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Get description titles editable by default. |
||
| 345 | * |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | public function get_default_description_title_editable() |
||
| 349 | { |
||
| 350 | $default_description_title_editable = []; |
||
| 351 | $default_description_title_editable[1] = true; |
||
| 352 | $default_description_title_editable[2] = true; |
||
| 353 | $default_description_title_editable[3] = true; |
||
| 354 | $default_description_title_editable[4] = true; |
||
| 355 | $default_description_title_editable[5] = true; |
||
| 356 | $default_description_title_editable[6] = true; |
||
| 357 | $default_description_title_editable[7] = true; |
||
| 358 | //$default_description_title_editable[8] = true; |
||
| 359 | |||
| 360 | return $default_description_title_editable; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get description icons by default. |
||
| 365 | * |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | public function get_default_description_icon() |
||
| 369 | { |
||
| 370 | $default_description_icon = []; |
||
| 371 | $default_description_icon[1] = 'info.png'; |
||
| 372 | $default_description_icon[2] = 'objective.png'; |
||
| 373 | $default_description_icon[3] = 'topics.png'; |
||
| 374 | $default_description_icon[4] = 'strategy.png'; |
||
| 375 | $default_description_icon[5] = 'laptop.png'; |
||
| 376 | $default_description_icon[6] = 'teacher.png'; |
||
| 377 | $default_description_icon[7] = 'assessment.png'; |
||
| 378 | $default_description_icon[8] = 'wizard.png'; |
||
| 379 | |||
| 380 | return $default_description_icon; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get questions by default for help. |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | */ |
||
| 388 | public function get_default_question() |
||
| 389 | { |
||
| 390 | $question = []; |
||
| 391 | $question[1] = get_lang('GeneralDescriptionQuestions'); |
||
| 392 | $question[2] = get_lang('ObjectivesQuestions'); |
||
| 393 | $question[3] = get_lang('TopicsQuestions'); |
||
| 394 | $question[4] = get_lang('MethodologyQuestions'); |
||
| 395 | $question[5] = get_lang('CourseMaterialQuestions'); |
||
| 396 | $question[6] = get_lang('HumanAndTechnicalResourcesQuestions'); |
||
| 397 | $question[7] = get_lang('AssessmentQuestions'); |
||
| 398 | //$question[8]= get_lang('ThematicAdvanceQuestions'); |
||
| 399 | |||
| 400 | return $question; |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get informations by default for help. |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function get_default_information() |
||
| 409 | { |
||
| 410 | $information = []; |
||
| 411 | $information[1] = get_lang('GeneralDescriptionInformation'); |
||
| 412 | $information[2] = get_lang('ObjectivesInformation'); |
||
| 413 | $information[3] = get_lang('TopicsInformation'); |
||
| 414 | $information[4] = get_lang('MethodologyInformation'); |
||
| 415 | $information[5] = get_lang('CourseMaterialInformation'); |
||
| 416 | $information[6] = get_lang('HumanAndTechnicalResourcesInformation'); |
||
| 417 | $information[7] = get_lang('AssessmentInformation'); |
||
| 418 | //$information[8]= get_lang('ThematicAdvanceInformation'); |
||
| 419 | |||
| 420 | return $information; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set description id. |
||
| 425 | */ |
||
| 426 | public function set_id($id) |
||
| 427 | { |
||
| 428 | $this->id = $id; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Set description's course id. |
||
| 433 | * |
||
| 434 | * @param int $id Course ID |
||
| 435 | */ |
||
| 436 | public function set_course_id($id) |
||
| 437 | { |
||
| 438 | $this->course_id = intval($id); |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Set description title. |
||
| 443 | * |
||
| 444 | * @param string $title |
||
| 445 | */ |
||
| 446 | public function set_title($title) |
||
| 447 | { |
||
| 448 | $this->title = $title; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Set description content. |
||
| 453 | * |
||
| 454 | * @param string $content |
||
| 455 | */ |
||
| 456 | public function set_content($content) |
||
| 457 | { |
||
| 458 | $this->content = $content; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Set description session id. |
||
| 463 | * |
||
| 464 | * @param int $session_id |
||
| 465 | */ |
||
| 466 | public function set_session_id($session_id) |
||
| 467 | { |
||
| 468 | $this->session_id = $session_id; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Set description type. |
||
| 473 | */ |
||
| 474 | public function set_description_type($description_type) |
||
| 475 | { |
||
| 476 | $this->description_type = $description_type; |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set progress of a description. |
||
| 481 | * |
||
| 482 | * @param string $progress |
||
| 483 | */ |
||
| 484 | public function set_progress($progress) |
||
| 485 | { |
||
| 486 | $this->progress = $progress; |
||
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * get description id. |
||
| 491 | * |
||
| 492 | * @return int |
||
| 493 | */ |
||
| 494 | public function get_id() |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * get description title. |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function get_title() |
||
| 505 | { |
||
| 506 | return $this->title; |
||
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * get description content. |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | public function get_content() |
||
| 517 | } |
||
| 518 | |||
| 519 | /** |
||
| 520 | * get session id. |
||
| 521 | * |
||
| 522 | * @return int |
||
| 523 | */ |
||
| 524 | public function get_session_id() |
||
| 525 | { |
||
| 526 | return $this->session_id; |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * get description type. |
||
| 531 | * |
||
| 532 | * @return int |
||
| 533 | */ |
||
| 534 | public function get_description_type() |
||
| 537 | } |
||
| 538 | |||
| 539 | /** |
||
| 540 | * get progress of a description. |
||
| 541 | * |
||
| 542 | * @return int |
||
| 543 | */ |
||
| 544 | public function get_progress() |
||
| 547 | } |
||
| 548 | } |
||
| 549 |