Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Thematic 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Thematic, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Thematic |
||
| 13 | { |
||
| 14 | private $session_id; |
||
| 15 | private $thematic_id; |
||
| 16 | private $thematic_title; |
||
| 17 | private $thematic_content; |
||
| 18 | private $thematic_plan_id; |
||
|
|
|||
| 19 | private $thematic_plan_title; |
||
| 20 | private $thematic_plan_description; |
||
| 21 | private $thematic_plan_description_type; |
||
| 22 | private $thematic_advance_id; |
||
| 23 | private $attendance_id; |
||
| 24 | private $thematic_advance_content; |
||
| 25 | private $start_date; |
||
| 26 | private $duration; |
||
| 27 | private $course_int_id; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Constructor |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 36 | |||
| 37 | public function setCourseIntId($course_id) |
||
| 38 | { |
||
| 39 | $this->course_int_id = intval($course_id); |
||
| 40 | } |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * Get the total number of thematic inside current course and current session |
||
| 45 | * @see SortableTable#get_total_number_of_items() |
||
| 46 | */ |
||
| 47 | View Code Duplication | public function get_number_of_thematics() |
|
| 48 | { |
||
| 49 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 50 | $condition_session = ''; |
||
| 51 | if (!api_get_session_id()) { |
||
| 52 | $condition_session = api_get_session_condition(0); |
||
| 53 | } |
||
| 54 | $course_id = api_get_course_int_id(); |
||
| 55 | $sql = "SELECT COUNT(id) AS total_number_of_items |
||
| 56 | FROM $tbl_thematic |
||
| 57 | WHERE c_id = $course_id AND active = 1 $condition_session "; |
||
| 58 | $res = Database::query($sql); |
||
| 59 | $obj = Database::fetch_object($res); |
||
| 60 | |||
| 61 | return $obj->total_number_of_items; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the thematics to display on the current page (fill the sortable-table) |
||
| 66 | * @param int offset of first user to recover |
||
| 67 | * @param int Number of users to get |
||
| 68 | * @param int Column to sort on |
||
| 69 | * @param string Order (ASC,DESC) |
||
| 70 | * @return array |
||
| 71 | * @see SortableTable#get_table_data($from) |
||
| 72 | */ |
||
| 73 | public function get_thematic_data($from, $number_of_items, $column, $direction) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the maximum display order of the thematic item |
||
| 162 | * @return int Maximum display order |
||
| 163 | */ |
||
| 164 | View Code Duplication | public function get_max_thematic_item($use_session = true) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Move a thematic |
||
| 186 | * |
||
| 187 | * @param string $direction (up, down) |
||
| 188 | * @param int $thematic_id |
||
| 189 | */ |
||
| 190 | public function move_thematic($direction, $thematic_id) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * get thematic list |
||
| 251 | * @param int Thematic id (optional), get list by id |
||
| 252 | * @param integer $thematic_id |
||
| 253 | * @param string $course_code |
||
| 254 | * @param integer $session_id |
||
| 255 | * @return array Thematic data |
||
| 256 | */ |
||
| 257 | public static function get_thematic_list( |
||
| 258 | $thematic_id = null, |
||
| 259 | $course_code = null, |
||
| 260 | $session_id = null |
||
| 261 | ) { |
||
| 262 | // set current course and session |
||
| 263 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 264 | $course_info = api_get_course_info($course_code); |
||
| 265 | $course_id = $course_info['real_id']; |
||
| 266 | |||
| 267 | if (isset($session_id)) { |
||
| 268 | $session_id = intval($session_id); |
||
| 269 | } else { |
||
| 270 | $session_id = api_get_session_id(); |
||
| 271 | } |
||
| 272 | |||
| 273 | $data = array(); |
||
| 274 | if (isset($thematic_id)) { |
||
| 275 | $thematic_id = intval($thematic_id); |
||
| 276 | $condition = " WHERE id = $thematic_id AND active = 1 "; |
||
| 277 | } else { |
||
| 278 | if (empty($session_id)) { |
||
| 279 | $condition_session = api_get_session_condition(0); |
||
| 280 | } else { |
||
| 281 | $condition_session = api_get_session_condition($session_id, true, true); |
||
| 282 | } |
||
| 283 | $condition = " WHERE active = 1 $condition_session "; |
||
| 284 | } |
||
| 285 | $sql = "SELECT * FROM $tbl_thematic $condition AND c_id = $course_id |
||
| 286 | ORDER BY display_order "; |
||
| 287 | |||
| 288 | $res = Database::query($sql); |
||
| 289 | if (Database::num_rows($res) > 0) { |
||
| 290 | if (!empty($thematic_id)) { |
||
| 291 | $data = Database::fetch_array($res, 'ASSOC'); |
||
| 292 | } else { |
||
| 293 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 294 | $data[$row['id']] = $row; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | return $data; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * insert or update a thematic |
||
| 304 | * @return int last thematic id |
||
| 305 | */ |
||
| 306 | public function thematic_save() |
||
| 307 | { |
||
| 308 | $_course = api_get_course_info(); |
||
| 309 | // definition database table |
||
| 310 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 311 | |||
| 312 | // protect data |
||
| 313 | $id = intval($this->thematic_id); |
||
| 314 | $title = $this->thematic_title; |
||
| 315 | $content = $this->thematic_content; |
||
| 316 | $session_id = intval($this->session_id); |
||
| 317 | $user_id = api_get_user_id(); |
||
| 318 | |||
| 319 | // get the maximum display order of all the glossary items |
||
| 320 | $max_thematic_item = $this->get_max_thematic_item(false); |
||
| 321 | |||
| 322 | if (empty($id)) { |
||
| 323 | // insert |
||
| 324 | $params = [ |
||
| 325 | 'c_id' => $this->course_int_id, |
||
| 326 | 'title' => $title, |
||
| 327 | 'content' => $content, |
||
| 328 | 'active' => 1, |
||
| 329 | 'display_order' => intval($max_thematic_item) + 1, |
||
| 330 | 'session_id' => $session_id |
||
| 331 | ]; |
||
| 332 | $last_id = Database::insert($tbl_thematic, $params); |
||
| 333 | if ($last_id) { |
||
| 334 | $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id"; |
||
| 335 | Database::query($sql); |
||
| 336 | api_item_property_update( |
||
| 337 | $_course, |
||
| 338 | 'thematic', |
||
| 339 | $last_id, |
||
| 340 | "ThematicAdded", |
||
| 341 | $user_id |
||
| 342 | ); |
||
| 343 | } |
||
| 344 | } else { |
||
| 345 | // Update |
||
| 346 | $params = [ |
||
| 347 | 'title' => $title, |
||
| 348 | 'content' => $content, |
||
| 349 | 'session_id' => $session_id |
||
| 350 | ]; |
||
| 351 | |||
| 352 | Database::update( |
||
| 353 | $tbl_thematic, |
||
| 354 | $params, |
||
| 355 | ['id = ? AND c_id = ?' => [$id, $this->course_int_id]] |
||
| 356 | ); |
||
| 357 | |||
| 358 | $last_id = $id; |
||
| 359 | |||
| 360 | // save inside item property table |
||
| 361 | api_item_property_update( |
||
| 362 | $_course, |
||
| 363 | 'thematic', |
||
| 364 | $last_id, |
||
| 365 | "ThematicUpdated", |
||
| 366 | $user_id |
||
| 367 | ); |
||
| 368 | } |
||
| 369 | |||
| 370 | return $last_id; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Delete logically (set active field to 0) a thematic |
||
| 375 | * @param int|array One or many thematic ids |
||
| 376 | * @return int Affected rows |
||
| 377 | */ |
||
| 378 | public function thematic_destroy($thematic_id) |
||
| 379 | { |
||
| 380 | $_course = api_get_course_info(); |
||
| 381 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 382 | $affected_rows = 0; |
||
| 383 | $user_id = api_get_user_id(); |
||
| 384 | $course_id = api_get_course_int_id(); |
||
| 385 | |||
| 386 | if (is_array($thematic_id)) { |
||
| 387 | foreach ($thematic_id as $id) { |
||
| 388 | $id = intval($id); |
||
| 389 | $sql = "UPDATE $tbl_thematic SET active = 0 |
||
| 390 | WHERE c_id = $course_id AND id = $id"; |
||
| 391 | $result = Database::query($sql); |
||
| 392 | $affected_rows += Database::affected_rows($result); |
||
| 393 | if (!empty($affected_rows)) { |
||
| 394 | // update row item property table |
||
| 395 | api_item_property_update( |
||
| 396 | $_course, |
||
| 397 | 'thematic', |
||
| 398 | $id, |
||
| 399 | "ThematicDeleted", |
||
| 400 | $user_id |
||
| 401 | ); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | } else { |
||
| 405 | $thematic_id = intval($thematic_id); |
||
| 406 | $sql = "UPDATE $tbl_thematic SET active = 0 |
||
| 407 | WHERE c_id = $course_id AND id = $thematic_id"; |
||
| 408 | $result = Database::query($sql); |
||
| 409 | $affected_rows = Database::affected_rows($result); |
||
| 410 | if (!empty($affected_rows)) { |
||
| 411 | // update row item property table |
||
| 412 | api_item_property_update( |
||
| 413 | $_course, |
||
| 414 | 'thematic', |
||
| 415 | $thematic_id, |
||
| 416 | "ThematicDeleted", |
||
| 417 | $user_id |
||
| 418 | ); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | return $affected_rows; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param int $thematic_id |
||
| 427 | */ |
||
| 428 | public function copy($thematic_id) |
||
| 429 | { |
||
| 430 | $thematic = self::get_thematic_list($thematic_id, api_get_course_id(), 0); |
||
| 431 | $thematic_copy = new Thematic(); |
||
| 432 | $thematic_copy->set_thematic_attributes( |
||
| 433 | '', |
||
| 434 | $thematic['title'].' - '.get_lang('Copy'), |
||
| 435 | $thematic['content'], |
||
| 436 | api_get_session_id() |
||
| 437 | ); |
||
| 438 | |||
| 439 | $new_thematic_id = $thematic_copy->thematic_save(); |
||
| 440 | if (!empty($new_thematic_id)) { |
||
| 441 | $thematic_advanced = self::get_thematic_advance_by_thematic_id($thematic_id); |
||
| 442 | if (!empty($thematic_advanced)) { |
||
| 443 | foreach ($thematic_advanced as $item) { |
||
| 444 | $thematic = new Thematic(); |
||
| 445 | $thematic->set_thematic_advance_attributes( |
||
| 446 | 0, |
||
| 447 | $new_thematic_id, |
||
| 448 | 0, |
||
| 449 | $item['content'], |
||
| 450 | $item['start_date'], |
||
| 451 | $item['duration'] |
||
| 452 | ); |
||
| 453 | $thematic->thematic_advance_save(); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | $thematic_plan = self::get_thematic_plan_data($thematic_id); |
||
| 457 | if (!empty($thematic_plan)) { |
||
| 458 | foreach ($thematic_plan as $item) { |
||
| 459 | $thematic = new Thematic(); |
||
| 460 | $thematic->set_thematic_plan_attributes( |
||
| 461 | $new_thematic_id, |
||
| 462 | $item['title'], |
||
| 463 | $item['description'], |
||
| 464 | $item['description_type'] |
||
| 465 | ); |
||
| 466 | $thematic->thematic_plan_save(); |
||
| 467 | } |
||
| 468 | } |
||
| 469 | } |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Get the total number of thematic advance inside current course |
||
| 474 | * @see SortableTable#get_total_number_of_items() |
||
| 475 | */ |
||
| 476 | public static function get_number_of_thematic_advances() |
||
| 477 | { |
||
| 478 | global $thematic_id; |
||
| 479 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 480 | $course_id = api_get_course_int_id(); |
||
| 481 | |||
| 482 | $sql = "SELECT COUNT(id) AS total_number_of_items |
||
| 483 | FROM $tbl_thematic_advance |
||
| 484 | WHERE c_id = $course_id AND thematic_id = $thematic_id "; |
||
| 485 | $res = Database::query($sql); |
||
| 486 | $obj = Database::fetch_object($res); |
||
| 487 | |||
| 488 | return $obj->total_number_of_items; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the thematic advances to display on the current page (fill the sortable-table) |
||
| 493 | * @param int offset of first user to recover |
||
| 494 | * @param int Number of users to get |
||
| 495 | * @param int Column to sort on |
||
| 496 | * @param string Order (ASC,DESC) |
||
| 497 | * @return array |
||
| 498 | * @see SortableTable#get_table_data($from) |
||
| 499 | */ |
||
| 500 | public static function get_thematic_advance_data($from, $number_of_items, $column, $direction) |
||
| 501 | { |
||
| 502 | global $thematic_id; |
||
| 503 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 504 | $column = intval($column); |
||
| 505 | $from = intval($from); |
||
| 506 | $number_of_items = intval($number_of_items); |
||
| 507 | if (!in_array($direction, array('ASC','DESC'))) { |
||
| 508 | $direction = 'ASC'; |
||
| 509 | } |
||
| 510 | $data = array(); |
||
| 511 | $course_id = api_get_course_int_id(); |
||
| 512 | if (api_is_allowed_to_edit(null, true)) { |
||
| 513 | $sql = "SELECT id AS col0, start_date AS col1, duration AS col2, content AS col3 |
||
| 514 | FROM $tbl_thematic_advance |
||
| 515 | WHERE c_id = $course_id AND thematic_id = $thematic_id |
||
| 516 | ORDER BY col$column $direction |
||
| 517 | LIMIT $from,$number_of_items "; |
||
| 518 | |||
| 519 | $list = api_get_item_property_by_tool( |
||
| 520 | 'thematic_advance', |
||
| 521 | api_get_course_id(), |
||
| 522 | api_get_session_id() |
||
| 523 | ); |
||
| 524 | |||
| 525 | $elements = array(); |
||
| 526 | foreach ($list as $value) { |
||
| 527 | $elements[] = $value['ref']; |
||
| 528 | } |
||
| 529 | |||
| 530 | $res = Database::query($sql); |
||
| 531 | $i = 1; |
||
| 532 | while ($thematic_advance = Database::fetch_row($res)) { |
||
| 533 | if (in_array($thematic_advance[0], $elements)) { |
||
| 534 | $thematic_advance[1] = api_get_local_time($thematic_advance[1]); |
||
| 535 | $thematic_advance[1] = api_format_date($thematic_advance[1], DATE_TIME_FORMAT_LONG); |
||
| 536 | $actions = ''; |
||
| 537 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_edit&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.Display::return_icon('edit.png',get_lang('Edit'),'',22).'</a>'; |
||
| 538 | $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_advance_delete&thematic_id='.$thematic_id.'&thematic_advance_id='.$thematic_advance[0].'">'.Display::return_icon('delete.png',get_lang('Delete'),'',22).'</a></center>'; |
||
| 539 | $data[] = array($i, $thematic_advance[1], $thematic_advance[2], $thematic_advance[3], $actions); |
||
| 540 | $i++; |
||
| 541 | } |
||
| 542 | } |
||
| 543 | } |
||
| 544 | return $data; |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * get thematic advance data by thematic id |
||
| 549 | * @param int $thematic_id |
||
| 550 | * @param string Course code (optional) |
||
| 551 | * @return array data |
||
| 552 | */ |
||
| 553 | public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null) |
||
| 554 | { |
||
| 555 | $course_info = api_get_course_info($course_code); |
||
| 556 | $course_id = $course_info['real_id']; |
||
| 557 | |||
| 558 | // set current course |
||
| 559 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 560 | |||
| 561 | $thematic_id = intval($thematic_id); |
||
| 562 | $data = array(); |
||
| 563 | $sql = "SELECT * FROM $tbl_thematic_advance |
||
| 564 | WHERE c_id = $course_id AND thematic_id = $thematic_id "; |
||
| 565 | |||
| 566 | $elements = array(); |
||
| 567 | $list = api_get_item_property_by_tool( |
||
| 568 | 'thematic_advance', |
||
| 569 | $course_info['code'], |
||
| 570 | api_get_session_id() |
||
| 571 | ); |
||
| 572 | foreach ($list as $value) { |
||
| 573 | $elements[] = $value['ref']; |
||
| 574 | } |
||
| 575 | |||
| 576 | $res = Database::query($sql); |
||
| 577 | View Code Duplication | if (Database::num_rows($res) > 0) { |
|
| 578 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 579 | if (in_array($row['id'], $elements)) { |
||
| 580 | $data[] = $row; |
||
| 581 | } |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | return $data; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @param array $data |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function get_thematic_advance_div($data) |
||
| 593 | { |
||
| 594 | $return_array = array(); |
||
| 595 | $uinfo = api_get_user_info(); |
||
| 596 | |||
| 597 | foreach ($data as $thematic_id => $thematic_advance_data) { |
||
| 598 | foreach ($thematic_advance_data as $key => $thematic_advance) { |
||
| 599 | $session_star = ''; |
||
| 600 | View Code Duplication | if (api_is_allowed_to_edit(null, true)) { |
|
| 601 | if ($thematic_advance['session_id'] !=0) { |
||
| 602 | $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']); |
||
| 603 | } |
||
| 604 | } |
||
| 605 | // DATE_TIME_FORMAT_LONG |
||
| 606 | $thematic_advance_item = '<div><strong>'.api_convert_and_format_date($thematic_advance['start_date'], DATE_TIME_FORMAT_LONG).$session_star.'</strong></div>'; |
||
| 607 | // $thematic_advance_item .= '<div>'.get_lang('DurationInHours').' : '.$thematic_advance['duration'].'</div>'; |
||
| 608 | $thematic_advance_item .= '<div>'.$thematic_advance['duration'].' '.get_lang('HourShort').'</div>'; |
||
| 609 | $thematic_advance_item .= '<div>'.Security::remove_XSS($thematic_advance['content'], STUDENT).'</div>'; |
||
| 610 | $return_array[$thematic_id][$thematic_advance['id']] = $thematic_advance_item; |
||
| 611 | } |
||
| 612 | } |
||
| 613 | return $return_array; |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param array $data |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function get_thematic_plan_array($data) |
||
| 621 | { |
||
| 622 | $final_return = array(); |
||
| 623 | $uinfo = api_get_user_info(); |
||
| 624 | |||
| 625 | foreach ($data as $thematic_id => $thematic_plan_data) { |
||
| 626 | $new_thematic_plan_data = array(); |
||
| 627 | View Code Duplication | foreach($thematic_plan_data as $thematic_item) { |
|
| 628 | $thematic_simple_list[] = $thematic_item['description_type']; |
||
| 629 | $new_thematic_plan_data[$thematic_item['description_type']] = $thematic_item; |
||
| 630 | } |
||
| 631 | |||
| 632 | if (!empty($thematic_simple_list)) { |
||
| 633 | foreach($thematic_simple_list as $item) { |
||
| 634 | $default_thematic_plan_title[$item] = $new_thematic_plan_data[$item]['title']; |
||
| 635 | } |
||
| 636 | } |
||
| 637 | |||
| 638 | $session_star = ''; |
||
| 639 | $return = array(); |
||
| 640 | if (!empty($default_thematic_plan_title)) { |
||
| 641 | |||
| 642 | foreach ($default_thematic_plan_title as $id=>$title) { |
||
| 643 | //avoid others |
||
| 644 | if ($title == 'Others' && empty($data[$thematic_id][$id]['description'])) { |
||
| 645 | continue; |
||
| 646 | } |
||
| 647 | if (!empty($data[$thematic_id][$id]['title']) && !empty($data[$thematic_id][$id]['description'])) { |
||
| 648 | View Code Duplication | if (api_is_allowed_to_edit(null, true)) { |
|
| 649 | if ($data[$thematic_id][$id]['session_id'] !=0) { |
||
| 650 | $session_star = api_get_session_image(api_get_session_id(), $uinfo['status']); |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | $return[$id]['title'] = Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star; |
||
| 655 | $return[$id]['description'] = Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT); |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | $final_return[$thematic_id] = $return; |
||
| 660 | } |
||
| 661 | |||
| 662 | return $final_return; |
||
| 663 | } |
||
| 664 | |||
| 665 | /** |
||
| 666 | * get thematic advance list |
||
| 667 | * @param int $thematic_advance_id Thematic advance id (optional), get data by thematic advance list |
||
| 668 | * @param string $course_code Course code (optional) |
||
| 669 | * @param bool $force_session_id Force to have a session id |
||
| 670 | * @return array $data |
||
| 671 | */ |
||
| 672 | public function get_thematic_advance_list( |
||
| 673 | $thematic_advance_id = null, |
||
| 674 | $course_code = null, |
||
| 675 | $force_session_id = false |
||
| 676 | ) { |
||
| 677 | $course_info = api_get_course_info($course_code); |
||
| 678 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 679 | $data = array(); |
||
| 680 | $condition = ''; |
||
| 681 | if (isset($thematic_advance_id)) { |
||
| 682 | $thematic_advance_id = intval($thematic_advance_id); |
||
| 683 | $condition = " AND a.id = $thematic_advance_id "; |
||
| 684 | } |
||
| 685 | |||
| 686 | $course_id = $course_info['real_id']; |
||
| 687 | |||
| 688 | $sql = "SELECT * FROM $tbl_thematic_advance a |
||
| 689 | WHERE c_id = $course_id $condition |
||
| 690 | ORDER BY start_date "; |
||
| 691 | |||
| 692 | $elements = array(); |
||
| 693 | if ($force_session_id) { |
||
| 694 | $list = api_get_item_property_by_tool( |
||
| 695 | 'thematic_advance', |
||
| 696 | $course_info['code'], |
||
| 697 | api_get_session_id() |
||
| 698 | ); |
||
| 699 | foreach ($list as $value) { |
||
| 700 | $elements[$value['ref']] = $value; |
||
| 701 | } |
||
| 702 | } |
||
| 703 | |||
| 704 | $res = Database::query($sql); |
||
| 705 | if (Database::num_rows($res) > 0) { |
||
| 706 | if (!empty($thematic_advance_id)) { |
||
| 707 | $data = Database::fetch_array($res); |
||
| 708 | } else { |
||
| 709 | // group all data group by thematic id |
||
| 710 | $tmp = array(); |
||
| 711 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 712 | $tmp[] = $row['thematic_id']; |
||
| 713 | if (in_array($row['thematic_id'], $tmp)) { |
||
| 714 | if ($force_session_id) { |
||
| 715 | if (in_array($row['id'], array_keys($elements))) { |
||
| 716 | $row['session_id'] = $elements[$row['id']]['session_id']; |
||
| 717 | $data[$row['thematic_id']][$row['id']] = $row; |
||
| 718 | } |
||
| 719 | } else { |
||
| 720 | $data[$row['thematic_id']][$row['id']] = $row; |
||
| 721 | } |
||
| 722 | } |
||
| 723 | } |
||
| 724 | } |
||
| 725 | } |
||
| 726 | |||
| 727 | return $data; |
||
| 728 | } |
||
| 729 | |||
| 730 | /** |
||
| 731 | * insert or update a thematic advance |
||
| 732 | * @todo problem |
||
| 733 | * @return int last thematic advance id |
||
| 734 | */ |
||
| 735 | public function thematic_advance_save() |
||
| 736 | { |
||
| 737 | $_course = api_get_course_info(); |
||
| 738 | // definition database table |
||
| 739 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 740 | |||
| 741 | // protect data |
||
| 742 | $id = intval($this->thematic_advance_id); |
||
| 743 | $thematic_id = intval($this->thematic_id); |
||
| 744 | $attendance_id = intval($this->attendance_id); |
||
| 745 | $content = $this->thematic_advance_content; |
||
| 746 | $start_date = $this->start_date; |
||
| 747 | $duration = intval($this->duration); |
||
| 748 | $user_id = api_get_user_id(); |
||
| 749 | |||
| 750 | $last_id = null; |
||
| 751 | if (empty($id)) { |
||
| 752 | // Insert |
||
| 753 | $params = [ |
||
| 754 | 'c_id' => $this->course_int_id, |
||
| 755 | 'thematic_id' => $thematic_id, |
||
| 756 | 'attendance_id' => $attendance_id, |
||
| 757 | 'content' => $content, |
||
| 758 | 'start_date' => api_get_utc_datetime($start_date), |
||
| 759 | 'duration' => $duration, |
||
| 760 | 'done_advance' => 0 |
||
| 761 | ]; |
||
| 762 | $last_id = Database::insert($tbl_thematic_advance, $params); |
||
| 763 | |||
| 764 | if ($last_id) { |
||
| 765 | $sql = "UPDATE $tbl_thematic_advance SET id = iid WHERE iid = $last_id"; |
||
| 766 | Database::query($sql); |
||
| 767 | |||
| 768 | api_item_property_update( |
||
| 769 | $_course, |
||
| 770 | 'thematic_advance', |
||
| 771 | $last_id, |
||
| 772 | "ThematicAdvanceAdded", |
||
| 773 | $user_id |
||
| 774 | ); |
||
| 775 | } |
||
| 776 | } else { |
||
| 777 | $params = [ |
||
| 778 | 'thematic_id' => $thematic_id, |
||
| 779 | 'attendance_id' => $attendance_id, |
||
| 780 | 'content' => $content, |
||
| 781 | 'start_date' => api_get_utc_datetime($start_date), |
||
| 782 | 'duration' => $duration |
||
| 783 | ]; |
||
| 784 | |||
| 785 | Database::update( |
||
| 786 | $tbl_thematic_advance, |
||
| 787 | $params, |
||
| 788 | ['id = ? AND c_id = ?' => [$id, $this->course_int_id]] |
||
| 789 | ); |
||
| 790 | |||
| 791 | api_item_property_update( |
||
| 792 | $_course, |
||
| 793 | 'thematic_advance', |
||
| 794 | $id, |
||
| 795 | "ThematicAdvanceUpdated", |
||
| 796 | $user_id |
||
| 797 | ); |
||
| 798 | } |
||
| 799 | |||
| 800 | return $last_id; |
||
| 801 | } |
||
| 802 | |||
| 803 | /** |
||
| 804 | * delete thematic advance |
||
| 805 | * @param int Thematic advance id |
||
| 806 | * @param integer $thematic_advance_id |
||
| 807 | * @return int Affected rows |
||
| 808 | */ |
||
| 809 | View Code Duplication | public function thematic_advance_destroy($thematic_advance_id) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * get thematic plan data |
||
| 840 | * @param int Thematic id (optional), get data by thematic id |
||
| 841 | * @param int Thematic plan description type (optional), get data by description type |
||
| 842 | * @return array Thematic plan data |
||
| 843 | */ |
||
| 844 | public function get_thematic_plan_data($thematic_id = null, $description_type = null) |
||
| 845 | { |
||
| 846 | // definition database table |
||
| 847 | $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN); |
||
| 848 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 925 | |||
| 926 | /** |
||
| 927 | * insert or update a thematic plan |
||
| 928 | * @return int affected rows |
||
| 929 | */ |
||
| 930 | public function thematic_plan_save() |
||
| 1042 | |||
| 1043 | /** |
||
| 1044 | * delete a thematic plan description |
||
| 1045 | * @param int $thematic_id Thematic id |
||
| 1046 | * @param int $description_type Description type |
||
| 1047 | * @return int Affected rows |
||
| 1048 | */ |
||
| 1049 | public function thematic_plan_destroy($thematic_id, $description_type) |
||
| 1085 | |||
| 1086 | /** |
||
| 1087 | * Get next description type for a new thematic plan description (option 'others') |
||
| 1088 | * @param int Thematic id |
||
| 1089 | * @return int New Description type |
||
| 1090 | */ |
||
| 1091 | public function get_next_description_type($thematic_id) |
||
| 1118 | |||
| 1119 | /** |
||
| 1120 | * update done thematic advances from thematic details interface |
||
| 1121 | * @param int Thematic id |
||
| 1122 | * @param integer $thematic_advance_id |
||
| 1123 | * @return int Affected rows |
||
| 1124 | */ |
||
| 1125 | public function update_done_thematic_advances($thematic_advance_id) |
||
| 1236 | |||
| 1237 | /** |
||
| 1238 | * Get last done thematic advance from thematic details interface |
||
| 1239 | * @return int Last done thematic advance id |
||
| 1240 | */ |
||
| 1241 | public function get_last_done_thematic_advance() |
||
| 1270 | |||
| 1271 | /** |
||
| 1272 | * Get next thematic advance not done from thematic details interface |
||
| 1273 | * @param int Offset (if you want to get an item that is not directly the next) |
||
| 1274 | * @return int next thematic advance not done |
||
| 1275 | */ |
||
| 1276 | public function get_next_thematic_advance_not_done($offset = 1) |
||
| 1303 | |||
| 1304 | /** |
||
| 1305 | * Get total average of thematic advances |
||
| 1306 | * @param string $course_code (optional) |
||
| 1307 | * @param int $session_id (optional) |
||
| 1308 | * @return float Average of thematic advances |
||
| 1309 | */ |
||
| 1310 | public function get_total_average_of_thematic_advances($course_code = null, $session_id = null) |
||
| 1349 | |||
| 1350 | /** |
||
| 1351 | * Get average of advances by thematic |
||
| 1352 | * @param int Thematic id |
||
| 1353 | * @param string Course code (optional) |
||
| 1354 | * @param string $course_code |
||
| 1355 | * @return float Average of thematic advances |
||
| 1356 | */ |
||
| 1357 | public function get_average_of_advances_by_thematic($thematic_id, $course_code = null) |
||
| 1378 | |||
| 1379 | /** |
||
| 1380 | * set attributes for fields of thematic table |
||
| 1381 | * @param int Thematic id |
||
| 1382 | * @param string Thematic title |
||
| 1383 | * @param string Thematic content |
||
| 1384 | * @param int Session id |
||
| 1385 | * @return void |
||
| 1386 | */ |
||
| 1387 | public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0) |
||
| 1394 | |||
| 1395 | /** |
||
| 1396 | * set attributes for fields of thematic_plan table |
||
| 1397 | * @param int Thematic id |
||
| 1398 | * @param string Thematic plan title |
||
| 1399 | * @param string Thematic plan description |
||
| 1400 | * @param int Thematic plan description type |
||
| 1401 | * @return void |
||
| 1402 | */ |
||
| 1403 | public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0) |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * set attributes for fields of thematic_advance table |
||
| 1413 | * @param int Thematic advance id |
||
| 1414 | * @param int Thematic id |
||
| 1415 | * @param int Attendance id |
||
| 1416 | * @param string Content |
||
| 1417 | * @param string Date and time |
||
| 1418 | * @param int Duration in hours |
||
| 1419 | * @param integer $id |
||
| 1420 | * @return void |
||
| 1421 | */ |
||
| 1422 | public function set_thematic_advance_attributes( |
||
| 1437 | |||
| 1438 | /** |
||
| 1439 | * set thematic id |
||
| 1440 | * @param int Thematic id |
||
| 1441 | * @return void |
||
| 1442 | */ |
||
| 1443 | public function set_thematic_id($thematic_id) |
||
| 1447 | |||
| 1448 | /** |
||
| 1449 | * get thematic id |
||
| 1450 | * @return integer |
||
| 1451 | */ |
||
| 1452 | public function get_thematic_id() |
||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Get thematic plan titles by default |
||
| 1459 | * @return array |
||
| 1460 | */ |
||
| 1461 | public function get_default_thematic_plan_title() |
||
| 1473 | |||
| 1474 | /** |
||
| 1475 | * Get thematic plan icons by default |
||
| 1476 | * @return array |
||
| 1477 | */ |
||
| 1478 | public function get_default_thematic_plan_icon() |
||
| 1490 | |||
| 1491 | /** |
||
| 1492 | * Get questions by default for help |
||
| 1493 | * @return array |
||
| 1494 | */ |
||
| 1495 | public function get_default_question() |
||
| 1506 | } |
||
| 1507 |
This check marks private properties in classes that are never used. Those properties can be removed.