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 | * @see SortableTable#get_table_data($from) |
||
| 71 | */ |
||
| 72 | public function get_thematic_data($from, $number_of_items, $column, $direction) |
||
| 73 | { |
||
| 74 | $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC); |
||
| 75 | $condition_session = ''; |
||
| 76 | if (!api_get_session_id()) { |
||
| 77 | $condition_session = api_get_session_condition(0); |
||
| 78 | } |
||
| 79 | $column = intval($column); |
||
| 80 | $from = intval($from); |
||
| 81 | $number_of_items = intval($number_of_items); |
||
| 82 | |||
| 83 | if (!in_array($direction, array('ASC','DESC'))) { |
||
| 84 | $direction = 'ASC'; |
||
| 85 | } |
||
| 86 | |||
| 87 | $course_id = api_get_course_int_id(); |
||
| 88 | |||
| 89 | $sql = "SELECT id AS col0, title AS col1, display_order AS col2, session_id |
||
| 90 | FROM $tbl_thematic |
||
| 91 | WHERE c_id = $course_id AND active = 1 $condition_session |
||
| 92 | ORDER BY col2 |
||
| 93 | LIMIT $from,$number_of_items "; |
||
| 94 | $res = Database::query($sql); |
||
| 95 | |||
| 96 | $thematics = array(); |
||
| 97 | $user_info = api_get_user_info(api_get_user_id()); |
||
| 98 | while ($thematic = Database::fetch_row($res)) { |
||
| 99 | $session_star = ''; |
||
| 100 | View Code Duplication | if (api_get_session_id() == $thematic[3]) { |
|
| 101 | $session_star = api_get_session_image(api_get_session_id(), $user_info['status']); |
||
| 102 | } |
||
| 103 | $thematic[1] = '<a href="index.php?'.api_get_cidreq().'&action=thematic_details&thematic_id='.$thematic[0].'">'. |
||
| 104 | Security::remove_XSS($thematic[1], STUDENT).$session_star.'</a>'; |
||
| 105 | if (api_is_allowed_to_edit(null, true)) { |
||
| 106 | $actions = ''; |
||
| 107 | |||
| 108 | if (api_get_session_id()) { |
||
| 109 | if (api_get_session_id() == $thematic[3]) { |
||
| 110 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'. |
||
| 111 | Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a> '; |
||
| 112 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'. |
||
| 113 | Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a> '; |
||
| 114 | |||
| 115 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'. |
||
| 116 | Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 117 | $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'. |
||
| 118 | Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 119 | } else { |
||
| 120 | $actions .= Display::return_icon('lesson_plan_na.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).' '; |
||
| 121 | $actions .= Display::return_icon('lesson_plan_calendar_na.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).' '; |
||
| 122 | $actions .= Display::return_icon('edit_na.png',get_lang('Edit'),'',ICON_SIZE_SMALL); |
||
| 123 | $actions .= Display::return_icon('delete_na.png',get_lang('Delete'),'',ICON_SIZE_SMALL).' '; |
||
| 124 | $actions .= Display::url( |
||
| 125 | Display::return_icon('cd.gif', get_lang('Copy')), |
||
| 126 | 'index.php?'.api_get_cidreq().'&action=thematic_copy&thematic_id='.$thematic[0] |
||
| 127 | ); |
||
| 128 | } |
||
| 129 | } else { |
||
| 130 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_plan_list&thematic_id='.$thematic[0].'">'. |
||
| 131 | Display::return_icon('lesson_plan.png',get_lang('ThematicPlan'),'',ICON_SIZE_SMALL).'</a> '; |
||
| 132 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_advance_list&thematic_id='.$thematic[0].'">'. |
||
| 133 | Display::return_icon('lesson_plan_calendar.png',get_lang('ThematicAdvance'),'',ICON_SIZE_SMALL).'</a> '; |
||
| 134 | |||
| 135 | View Code Duplication | if ($thematic[2] > 1) { |
|
| 136 | $actions .= '<a href="'.api_get_self().'?action=moveup&'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'. |
||
| 137 | Display::return_icon('up.png', get_lang('Up'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 138 | } else { |
||
| 139 | $actions .= Display::return_icon('up_na.png',' ','',ICON_SIZE_SMALL); |
||
| 140 | } |
||
| 141 | View Code Duplication | if ($thematic[2] < self::get_max_thematic_item()) { |
|
| 142 | $actions .= '<a href="'.api_get_self().'?action=movedown&a'.api_get_cidreq().'&thematic_id='.$thematic[0].'">'. |
||
| 143 | Display::return_icon('down.png',get_lang('Down'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 144 | } else { |
||
| 145 | $actions .= Display::return_icon('down_na.png',' ','',ICON_SIZE_SMALL); |
||
| 146 | } |
||
| 147 | $actions .= '<a href="index.php?'.api_get_cidreq().'&action=thematic_edit&thematic_id='.$thematic[0].'">'. |
||
| 148 | Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 149 | $actions .= '<a onclick="javascript:if(!confirm(\''.get_lang('AreYouSureToDelete').'\')) return false;" href="index.php?'.api_get_cidreq().'&action=thematic_delete&thematic_id='.$thematic[0].'">'. |
||
| 150 | Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>'; |
||
| 151 | } |
||
| 152 | $thematics[] = array($thematic[0], $thematic[1], $actions); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $thematics; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get the maximum display order of the thematic item |
||
| 161 | * @return int Maximum display order |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function get_max_thematic_item($use_session = true) |
|
| 164 | { |
||
| 165 | // Database table definition |
||
| 166 | $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC); |
||
| 167 | $session_id = api_get_session_id(); |
||
| 168 | if ($use_session) { |
||
| 169 | $condition_session = api_get_session_condition($session_id); |
||
| 170 | } else { |
||
| 171 | $condition_session = ''; |
||
| 172 | } |
||
| 173 | $course_id = api_get_course_int_id(); |
||
| 174 | $sql = "SELECT MAX(display_order) |
||
| 175 | FROM $tbl_thematic |
||
| 176 | WHERE c_id = $course_id AND active = 1 $condition_session"; |
||
| 177 | $rs = Database::query($sql); |
||
| 178 | $row = Database::fetch_array($rs); |
||
| 179 | |||
| 180 | return $row[0]; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Move a thematic |
||
| 185 | * |
||
| 186 | * @param string $direction (up, down) |
||
| 187 | * @param int $thematic_id |
||
| 188 | */ |
||
| 189 | public function move_thematic($direction, $thematic_id) |
||
| 190 | { |
||
| 191 | // Database table definition |
||
| 192 | $tbl_thematic = Database :: get_course_table(TABLE_THEMATIC); |
||
| 193 | |||
| 194 | // sort direction |
||
| 195 | if ($direction == 'up') { |
||
| 196 | $sortorder = 'DESC'; |
||
| 197 | } else { |
||
| 198 | $sortorder = 'ASC'; |
||
| 199 | } |
||
| 200 | $course_id = api_get_course_int_id(); |
||
| 201 | |||
| 202 | $session_id = api_get_session_id(); |
||
| 203 | $condition_session = api_get_session_condition($session_id); |
||
| 204 | |||
| 205 | $sql = "SELECT id, display_order |
||
| 206 | FROM $tbl_thematic |
||
| 207 | WHERE c_id = $course_id AND active = 1 $condition_session |
||
| 208 | ORDER BY display_order $sortorder"; |
||
| 209 | $res = Database::query($sql); |
||
| 210 | $found = false; |
||
| 211 | |||
| 212 | // Variable definition |
||
| 213 | $current_id = 0; |
||
| 214 | $next_id = 0; |
||
| 215 | |||
| 216 | while ($row = Database::fetch_array($res)) { |
||
| 217 | if ($found && empty($next_id)) { |
||
| 218 | $next_id = intval($row['id']); |
||
| 219 | $next_display_order = intval($row['display_order']); |
||
| 220 | } |
||
| 221 | |||
| 222 | if ($row['id'] == $thematic_id) { |
||
| 223 | $current_id = intval($thematic_id); |
||
| 224 | $current_display_order = intval($row['display_order']); |
||
| 225 | $found = true; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | // get last done thematic advance before move thematic list |
||
| 230 | $last_done_thematic_advance = $this->get_last_done_thematic_advance(); |
||
| 231 | |||
| 232 | if (!empty($next_display_order) && !empty($current_id)) { |
||
| 233 | $sql = "UPDATE $tbl_thematic SET display_order = $next_display_order |
||
| 234 | WHERE c_id = $course_id AND id = $current_id "; |
||
| 235 | Database::query($sql); |
||
| 236 | } |
||
| 237 | if (!empty($current_display_order) && !empty($next_id)) { |
||
| 238 | $sql = "UPDATE $tbl_thematic SET |
||
| 239 | display_order = $current_display_order |
||
| 240 | WHERE c_id = $course_id AND id = $next_id "; |
||
| 241 | Database::query($sql); |
||
| 242 | } |
||
| 243 | |||
| 244 | // update done advances with de current thematic list |
||
| 245 | $this->update_done_thematic_advances($last_done_thematic_advance); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * get thematic list |
||
| 250 | * @param int Thematic id (optional), get list by id |
||
| 251 | * @return array Thematic data |
||
| 252 | */ |
||
| 253 | public static function get_thematic_list( |
||
| 254 | $thematic_id = null, |
||
| 255 | $course_code = null, |
||
| 256 | $session_id = null |
||
| 257 | ) { |
||
| 258 | // set current course and session |
||
| 259 | $tbl_thematic = Database:: get_course_table(TABLE_THEMATIC); |
||
| 260 | $course_info = api_get_course_info($course_code); |
||
| 261 | $course_id = $course_info['real_id']; |
||
| 262 | |||
| 263 | if (isset($session_id)) { |
||
| 264 | $session_id = intval($session_id); |
||
| 265 | } else { |
||
| 266 | $session_id = api_get_session_id(); |
||
| 267 | } |
||
| 268 | |||
| 269 | $data = array(); |
||
| 270 | if (isset($thematic_id)) { |
||
| 271 | $thematic_id = intval($thematic_id); |
||
| 272 | $condition = " WHERE id = $thematic_id AND active = 1 "; |
||
| 273 | } else { |
||
| 274 | if (empty($session_id)) { |
||
| 275 | $condition_session = api_get_session_condition(0); |
||
| 276 | } else { |
||
| 277 | $condition_session = api_get_session_condition($session_id, true, true); |
||
| 278 | } |
||
| 279 | $condition = " WHERE active = 1 $condition_session "; |
||
| 280 | } |
||
| 281 | $sql = "SELECT * FROM $tbl_thematic $condition AND c_id = $course_id |
||
| 282 | ORDER BY display_order "; |
||
| 283 | |||
| 284 | $res = Database::query($sql); |
||
| 285 | if (Database::num_rows($res) > 0) { |
||
| 286 | if (!empty($thematic_id)) { |
||
| 287 | $data = Database::fetch_array($res, 'ASSOC'); |
||
| 288 | } else { |
||
| 289 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 290 | $data[$row['id']] = $row; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | return $data; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * insert or update a thematic |
||
| 300 | * @return int last thematic id |
||
| 301 | */ |
||
| 302 | public function thematic_save() |
||
| 303 | { |
||
| 304 | $_course = api_get_course_info(); |
||
| 305 | // definition database table |
||
| 306 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 307 | |||
| 308 | // protect data |
||
| 309 | $id = intval($this->thematic_id); |
||
| 310 | $title = $this->thematic_title; |
||
| 311 | $content = $this->thematic_content; |
||
| 312 | $session_id = intval($this->session_id); |
||
| 313 | $user_id = api_get_user_id(); |
||
| 314 | |||
| 315 | // get the maximum display order of all the glossary items |
||
| 316 | $max_thematic_item = $this->get_max_thematic_item(false); |
||
| 317 | |||
| 318 | if (empty($id)) { |
||
| 319 | // insert |
||
| 320 | $params = [ |
||
| 321 | 'c_id' => $this->course_int_id, |
||
| 322 | 'title' => $title, |
||
| 323 | 'content' => $content, |
||
| 324 | 'active' => 1, |
||
| 325 | 'display_order' => intval($max_thematic_item) + 1, |
||
| 326 | 'session_id' => $session_id |
||
| 327 | ]; |
||
| 328 | $last_id = Database::insert($tbl_thematic, $params); |
||
| 329 | if ($last_id) { |
||
| 330 | $sql = "UPDATE $tbl_thematic SET id = iid WHERE iid = $last_id"; |
||
| 331 | Database::query($sql); |
||
| 332 | api_item_property_update( |
||
| 333 | $_course, |
||
| 334 | 'thematic', |
||
| 335 | $last_id, |
||
| 336 | "ThematicAdded", |
||
| 337 | $user_id |
||
| 338 | ); |
||
| 339 | } |
||
| 340 | } else { |
||
| 341 | // Update |
||
| 342 | $params = [ |
||
| 343 | 'title' => $title, |
||
| 344 | 'content' => $content, |
||
| 345 | 'session_id' => $session_id |
||
| 346 | ]; |
||
| 347 | |||
| 348 | Database::update( |
||
| 349 | $tbl_thematic, |
||
| 350 | $params, |
||
| 351 | ['id = ? AND c_id = ?' => [$id, $this->course_int_id]] |
||
| 352 | ); |
||
| 353 | |||
| 354 | $last_id = $id; |
||
| 355 | |||
| 356 | // save inside item property table |
||
| 357 | api_item_property_update( |
||
| 358 | $_course, |
||
| 359 | 'thematic', |
||
| 360 | $last_id, |
||
| 361 | "ThematicUpdated", |
||
| 362 | $user_id |
||
| 363 | ); |
||
| 364 | } |
||
| 365 | |||
| 366 | return $last_id; |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Delete logically (set active field to 0) a thematic |
||
| 371 | * @param int|array One or many thematic ids |
||
| 372 | * @return int Affected rows |
||
| 373 | */ |
||
| 374 | public function thematic_destroy($thematic_id) |
||
| 375 | { |
||
| 376 | $_course = api_get_course_info(); |
||
| 377 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 378 | $affected_rows = 0; |
||
| 379 | $user_id = api_get_user_id(); |
||
| 380 | $course_id = api_get_course_int_id(); |
||
| 381 | |||
| 382 | if (is_array($thematic_id)) { |
||
| 383 | foreach ($thematic_id as $id) { |
||
| 384 | $id = intval($id); |
||
| 385 | $sql = "UPDATE $tbl_thematic SET active = 0 |
||
| 386 | WHERE c_id = $course_id AND id = $id"; |
||
| 387 | $result = Database::query($sql); |
||
| 388 | $affected_rows += Database::affected_rows($result); |
||
| 389 | if (!empty($affected_rows)) { |
||
| 390 | // update row item property table |
||
| 391 | api_item_property_update( |
||
| 392 | $_course, |
||
| 393 | 'thematic', |
||
| 394 | $id, |
||
| 395 | "ThematicDeleted", |
||
| 396 | $user_id |
||
| 397 | ); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | } else { |
||
| 401 | $thematic_id = intval($thematic_id); |
||
| 402 | $sql = "UPDATE $tbl_thematic SET active = 0 |
||
| 403 | WHERE c_id = $course_id AND id = $thematic_id"; |
||
| 404 | $result = Database::query($sql); |
||
| 405 | $affected_rows = Database::affected_rows($result); |
||
| 406 | if (!empty($affected_rows)) { |
||
| 407 | // update row item property table |
||
| 408 | api_item_property_update( |
||
| 409 | $_course, |
||
| 410 | 'thematic', |
||
| 411 | $thematic_id, |
||
| 412 | "ThematicDeleted", |
||
| 413 | $user_id |
||
| 414 | ); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | return $affected_rows; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param int $thematic_id |
||
| 423 | */ |
||
| 424 | public function copy($thematic_id) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get the total number of thematic advance inside current course |
||
| 470 | * @see SortableTable#get_total_number_of_items() |
||
| 471 | */ |
||
| 472 | public static function get_number_of_thematic_advances() |
||
| 486 | |||
| 487 | |||
| 488 | /** |
||
| 489 | * Get the thematic advances to display on the current page (fill the sortable-table) |
||
| 490 | * @param int offset of first user to recover |
||
| 491 | * @param int Number of users to get |
||
| 492 | * @param int Column to sort on |
||
| 493 | * @param string Order (ASC,DESC) |
||
| 494 | * @see SortableTable#get_table_data($from) |
||
| 495 | */ |
||
| 496 | public static function get_thematic_advance_data($from, $number_of_items, $column, $direction) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * get thematic advance data by thematic id |
||
| 548 | * @param int $thematic_id |
||
| 549 | * @param string Course code (optional) |
||
| 550 | * @return array data |
||
| 551 | */ |
||
| 552 | public function get_thematic_advance_by_thematic_id($thematic_id, $course_code = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param array $data |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public function get_thematic_advance_div($data) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param array $data |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | public function get_thematic_plan_div($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 | $no_data = true; |
||
| 639 | $session_star = ''; |
||
| 640 | $return = '<div id="thematic_plan_'.$thematic_id.'">'; |
||
| 641 | if (!empty($default_thematic_plan_title)) { |
||
| 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 | $return .= Display::tag('h3', Security::remove_XSS($data[$thematic_id][$id]['title'], STUDENT).$session_star); |
||
| 654 | $return .= Security::remove_XSS($data[$thematic_id][$id]['description'], STUDENT); |
||
| 655 | $no_data = false; |
||
| 656 | } |
||
| 657 | } |
||
| 658 | } |
||
| 659 | if ($no_data) { |
||
| 660 | $return .= '<div><em>'.get_lang('StillDoNotHaveAThematicPlan').'</em></div>'; |
||
| 661 | } |
||
| 662 | $return .= '</div>'; |
||
| 663 | $final_return[$thematic_id] = $return; |
||
| 664 | } |
||
| 665 | |||
| 666 | return $final_return; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * get thematic advance list |
||
| 671 | * @param int $thematic_advance_id Thematic advance id (optional), get data by thematic advance list |
||
| 672 | * @param string $course_code Course code (optional) |
||
| 673 | * @param bool $force_session_id Force to have a session id |
||
| 674 | * @return array $data |
||
| 675 | */ |
||
| 676 | public function get_thematic_advance_list($thematic_advance_id = null, $course_code = null, $force_session_id = false |
||
| 677 | ) { |
||
| 678 | $course_info = api_get_course_info($course_code); |
||
| 679 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 680 | $data = array(); |
||
| 681 | $condition = ''; |
||
| 682 | if (isset($thematic_advance_id)) { |
||
| 683 | $thematic_advance_id = intval($thematic_advance_id); |
||
| 684 | $condition = " AND a.id = $thematic_advance_id "; |
||
| 685 | } |
||
| 686 | |||
| 687 | $course_id = $course_info['real_id']; |
||
| 688 | |||
| 689 | $sql = "SELECT * FROM $tbl_thematic_advance a |
||
| 690 | WHERE c_id = $course_id $condition |
||
| 691 | ORDER BY start_date "; |
||
| 692 | |||
| 693 | $elements = array(); |
||
| 694 | if ($force_session_id) { |
||
| 695 | $list = api_get_item_property_by_tool( |
||
| 696 | 'thematic_advance', |
||
| 697 | $course_info['code'], |
||
| 698 | api_get_session_id() |
||
| 699 | ); |
||
| 700 | foreach ($list as $value) { |
||
| 701 | $elements[$value['ref']] = $value; |
||
| 702 | } |
||
| 703 | } |
||
| 704 | |||
| 705 | $res = Database::query($sql); |
||
| 706 | if (Database::num_rows($res) > 0) { |
||
| 707 | if (!empty($thematic_advance_id)) { |
||
| 708 | $data = Database::fetch_array($res); |
||
| 709 | } else { |
||
| 710 | // group all data group by thematic id |
||
| 711 | $tmp = array(); |
||
| 712 | while ($row = Database::fetch_array($res, 'ASSOC')) { |
||
| 713 | $tmp[] = $row['thematic_id']; |
||
| 714 | if (in_array($row['thematic_id'], $tmp)) { |
||
| 715 | if ($force_session_id) { |
||
| 716 | if (in_array($row['id'], array_keys($elements))) { |
||
| 717 | $row['session_id'] = $elements[$row['id']]['session_id']; |
||
| 718 | $data[$row['thematic_id']][$row['id']] = $row; |
||
| 719 | } |
||
| 720 | } else { |
||
| 721 | $data[$row['thematic_id']][$row['id']] = $row; |
||
| 722 | } |
||
| 723 | } |
||
| 724 | } |
||
| 725 | } |
||
| 726 | } |
||
| 727 | |||
| 728 | return $data; |
||
| 729 | } |
||
| 730 | |||
| 731 | /** |
||
| 732 | * insert or update a thematic advance |
||
| 733 | * @todo problem |
||
| 734 | * @return int last thematic advance id |
||
| 735 | */ |
||
| 736 | public function thematic_advance_save() |
||
| 737 | { |
||
| 738 | $_course = api_get_course_info(); |
||
| 739 | |||
| 740 | // definition database table |
||
| 741 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 742 | |||
| 743 | // protect data |
||
| 744 | $id = intval($this->thematic_advance_id); |
||
| 745 | $thematic_id = intval($this->thematic_id); |
||
| 746 | $attendance_id = intval($this->attendance_id); |
||
| 747 | $content = $this->thematic_advance_content; |
||
| 748 | $start_date = $this->start_date; |
||
| 749 | $duration = intval($this->duration); |
||
| 750 | $user_id = api_get_user_id(); |
||
| 751 | |||
| 752 | $last_id = null; |
||
| 753 | if (empty($id)) { |
||
| 754 | // Insert |
||
| 755 | $params = [ |
||
| 756 | 'c_id' => $this->course_int_id, |
||
| 757 | 'thematic_id' => $thematic_id, |
||
| 758 | 'attendance_id' => $attendance_id, |
||
| 759 | 'content' => $content, |
||
| 760 | 'start_date' => api_get_utc_datetime($start_date), |
||
| 761 | 'duration' => $duration, |
||
| 762 | 'done_advance' => 0 |
||
| 763 | ]; |
||
| 764 | $last_id = Database::insert($tbl_thematic_advance, $params); |
||
| 765 | |||
| 766 | if ($last_id) { |
||
| 767 | $sql = "UPDATE $tbl_thematic_advance SET id = iid WHERE iid = $last_id"; |
||
| 768 | Database::query($sql); |
||
| 769 | |||
| 770 | api_item_property_update( |
||
| 771 | $_course, |
||
| 772 | 'thematic_advance', |
||
| 773 | $last_id, |
||
| 774 | "ThematicAdvanceAdded", |
||
| 775 | $user_id |
||
| 776 | ); |
||
| 777 | } |
||
| 778 | } else { |
||
| 779 | $params = [ |
||
| 780 | 'thematic_id' => $thematic_id, |
||
| 781 | 'attendance_id' => $attendance_id, |
||
| 782 | 'content' => $content, |
||
| 783 | 'start_date' => api_get_utc_datetime($start_date), |
||
| 784 | 'duration' => $duration |
||
| 785 | ]; |
||
| 786 | |||
| 787 | Database::update( |
||
| 788 | $tbl_thematic_advance, |
||
| 789 | $params, |
||
| 790 | ['id = ? AND c_id = ?' => [$id, $this->course_int_id]] |
||
| 791 | ); |
||
| 792 | |||
| 793 | api_item_property_update( |
||
| 794 | $_course, |
||
| 795 | 'thematic_advance', |
||
| 796 | $id, |
||
| 797 | "ThematicAdvanceUpdated", |
||
| 798 | $user_id |
||
| 799 | ); |
||
| 800 | } |
||
| 801 | |||
| 802 | return $last_id; |
||
| 803 | } |
||
| 804 | |||
| 805 | /** |
||
| 806 | * delete thematic advance |
||
| 807 | * @param int Thematic advance id |
||
| 808 | * @return int Affected rows |
||
| 809 | */ |
||
| 810 | View Code Duplication | public function thematic_advance_destroy($thematic_advance_id) |
|
| 811 | { |
||
| 812 | $_course = api_get_course_info(); |
||
| 813 | $course_id = api_get_course_int_id(); |
||
| 814 | |||
| 815 | // definition database table |
||
| 816 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 817 | |||
| 818 | // protect data |
||
| 819 | $thematic_advance_id = intval($thematic_advance_id); |
||
| 820 | $user_id = api_get_user_id(); |
||
| 821 | |||
| 822 | $sql = "DELETE FROM $tbl_thematic_advance |
||
| 823 | WHERE c_id = $course_id AND id = $thematic_advance_id "; |
||
| 824 | $result = Database::query($sql); |
||
| 825 | $affected_rows = Database::affected_rows($result); |
||
| 826 | if ($affected_rows) { |
||
| 827 | api_item_property_update( |
||
| 828 | $_course, |
||
| 829 | 'thematic_advance', |
||
| 830 | $thematic_advance_id, |
||
| 831 | 'ThematicAdvanceDeleted', |
||
| 832 | $user_id |
||
| 833 | ); |
||
| 834 | } |
||
| 835 | |||
| 836 | return $affected_rows; |
||
| 837 | } |
||
| 838 | |||
| 839 | /** |
||
| 840 | * get thematic plan data |
||
| 841 | * @param int Thematic id (optional), get data by thematic id |
||
| 842 | * @param int Thematic plan description type (optional), get data by description type |
||
| 843 | * @return array Thematic plan data |
||
| 844 | */ |
||
| 845 | public function get_thematic_plan_data($thematic_id = null, $description_type = null) |
||
| 846 | { |
||
| 847 | // definition database table |
||
| 848 | $tbl_thematic_plan = Database::get_course_table(TABLE_THEMATIC_PLAN); |
||
| 849 | $tbl_thematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 850 | |||
| 851 | $course_id = api_get_course_int_id(); |
||
| 852 | |||
| 853 | $data = array(); |
||
| 854 | $condition = ''; |
||
| 855 | if (isset($thematic_id)) { |
||
| 856 | $thematic_id = intval($thematic_id); |
||
| 857 | $condition .= " AND thematic_id = $thematic_id "; |
||
| 858 | } |
||
| 859 | if (isset($description_type)) { |
||
| 860 | $description_type = intval($description_type); |
||
| 861 | $condition .= " AND description_type = $description_type "; |
||
| 862 | } |
||
| 863 | |||
| 864 | $items_from_course = api_get_item_property_by_tool( |
||
| 865 | 'thematic_plan', |
||
| 866 | api_get_course_id(), |
||
| 867 | 0 |
||
| 868 | ); |
||
| 869 | $items_from_session = api_get_item_property_by_tool( |
||
| 870 | 'thematic_plan', |
||
| 871 | api_get_course_id(), |
||
| 872 | api_get_session_id() |
||
| 873 | ); |
||
| 874 | |||
| 875 | $thematic_plan_complete_list = array(); |
||
| 876 | $thematic_plan_id_list = array(); |
||
| 877 | |||
| 878 | if (!empty($items_from_course)) { |
||
| 879 | foreach($items_from_course as $item) { |
||
| 880 | $thematic_plan_id_list[] = $item['ref']; |
||
| 881 | $thematic_plan_complete_list[$item['ref']] = $item; |
||
| 882 | } |
||
| 883 | } |
||
| 884 | |||
| 885 | if (!empty($items_from_session)) { |
||
| 886 | foreach($items_from_session as $item) { |
||
| 887 | $thematic_plan_id_list[] = $item['ref']; |
||
| 888 | $thematic_plan_complete_list[$item['ref']] = $item; |
||
| 889 | } |
||
| 890 | } |
||
| 891 | if (!empty($thematic_plan_id_list)) { |
||
| 892 | $sql = "SELECT |
||
| 893 | tp.id, thematic_id, tp.title, description, description_type, t.session_id |
||
| 894 | FROM $tbl_thematic_plan tp |
||
| 895 | INNER JOIN $tbl_thematic t |
||
| 896 | ON (t.id=tp.thematic_id) |
||
| 897 | WHERE |
||
| 898 | t.c_id = $course_id AND |
||
| 899 | tp.c_id = $course_id |
||
| 900 | $condition AND |
||
| 901 | tp.id IN (".implode(', ', $thematic_plan_id_list).") "; |
||
| 902 | |||
| 903 | $rs = Database::query($sql); |
||
| 904 | |||
| 905 | if (Database::num_rows($rs)) { |
||
| 906 | if (!isset($thematic_id) && !isset($description_type)) { |
||
| 907 | // group all data group by thematic id |
||
| 908 | $tmp = array(); |
||
| 909 | View Code Duplication | while ($row = Database::fetch_array($rs,'ASSOC')) { |
|
| 910 | $tmp[] = $row['thematic_id']; |
||
| 911 | if (in_array($row['thematic_id'], $tmp)) { |
||
| 912 | $row['session_id'] = $thematic_plan_complete_list[$row['id']]; |
||
| 913 | $data[$row['thematic_id']][$row['description_type']] = $row; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | } else { |
||
| 917 | while ($row = Database::fetch_array($rs,'ASSOC')) { |
||
| 918 | $row['session_id'] = $thematic_plan_complete_list[$row['id']]; |
||
| 919 | $data[] = $row; |
||
| 920 | } |
||
| 921 | } |
||
| 922 | } |
||
| 923 | } |
||
| 924 | |||
| 925 | return $data; |
||
| 926 | } |
||
| 927 | |||
| 928 | /** |
||
| 929 | * insert or update a thematic plan |
||
| 930 | * @return int affected rows |
||
| 931 | */ |
||
| 932 | public function thematic_plan_save() |
||
| 1045 | |||
| 1046 | /** |
||
| 1047 | * delete a thematic plan description |
||
| 1048 | * @param int $thematic_id Thematic id |
||
| 1049 | * @param int $description_type Description type |
||
| 1050 | * @return int Affected rows |
||
| 1051 | */ |
||
| 1052 | public function thematic_plan_destroy($thematic_id, $description_type) |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Get next description type for a new thematic plan description (option 'others') |
||
| 1091 | * @param int Thematic id |
||
| 1092 | * @return int New Description type |
||
| 1093 | */ |
||
| 1094 | public function get_next_description_type($thematic_id) |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * update done thematic advances from thematic details interface |
||
| 1124 | * @param int Thematic id |
||
| 1125 | * @return int Affected rows |
||
| 1126 | */ |
||
| 1127 | public function update_done_thematic_advances($thematic_advance_id) |
||
| 1128 | { |
||
| 1129 | $_course = api_get_course_info(); |
||
| 1130 | $thematic_data = $this->get_thematic_list(null, api_get_course_id()); |
||
| 1131 | $thematic_advance_data = $this->get_thematic_advance_list(null, api_get_course_id(), true); |
||
| 1132 | $tbl_thematic_advance = Database::get_course_table(TABLE_THEMATIC_ADVANCE); |
||
| 1133 | |||
| 1134 | $affected_rows = 0; |
||
| 1234 | |||
| 1235 | /** |
||
| 1236 | * Get last done thematic advance from thematic details interface |
||
| 1237 | * @return int Last done thematic advance id |
||
| 1238 | */ |
||
| 1239 | public function get_last_done_thematic_advance() |
||
| 1268 | |||
| 1269 | /** |
||
| 1270 | * Get next thematic advance not done from thematic details interface |
||
| 1271 | * @param int Offset (if you want to get an item that is not directly the next) |
||
| 1272 | * @return int next thematic advance not done |
||
| 1273 | */ |
||
| 1274 | public function get_next_thematic_advance_not_done($offset = 1) |
||
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Get total average of thematic advances |
||
| 1304 | * @param string $course_code (optional) |
||
| 1305 | * @param int $session_id (optional) |
||
| 1306 | * @return float Average of thematic advances |
||
| 1307 | */ |
||
| 1308 | public function get_total_average_of_thematic_advances($course_code = null, $session_id = null) |
||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * Get average of advances by thematic |
||
| 1350 | * @param int Thematic id |
||
| 1351 | * @param string Course code (optional) |
||
| 1352 | * @return float Average of thematic advances |
||
| 1353 | */ |
||
| 1354 | public function get_average_of_advances_by_thematic($thematic_id, $course_code = null) |
||
| 1375 | |||
| 1376 | /** |
||
| 1377 | * set attributes for fields of thematic table |
||
| 1378 | * @param int Thematic id |
||
| 1379 | * @param string Thematic title |
||
| 1380 | * @param string Thematic content |
||
| 1381 | * @param int Session id |
||
| 1382 | * @return void |
||
| 1383 | */ |
||
| 1384 | public function set_thematic_attributes($id = null, $title = '', $content = '', $session_id = 0) |
||
| 1391 | |||
| 1392 | /** |
||
| 1393 | * set attributes for fields of thematic_plan table |
||
| 1394 | * @param int Thematic id |
||
| 1395 | * @param string Thematic plan title |
||
| 1396 | * @param string Thematic plan description |
||
| 1397 | * @param int Thematic plan description type |
||
| 1398 | * @return void |
||
| 1399 | */ |
||
| 1400 | public function set_thematic_plan_attributes($thematic_id = 0, $title = '', $description = '', $description_type = 0) |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * set attributes for fields of thematic_advance table |
||
| 1410 | * @param int Thematic advance id |
||
| 1411 | * @param int Thematic id |
||
| 1412 | * @param int Attendance id |
||
| 1413 | * @param string Content |
||
| 1414 | * @param string Date and time |
||
| 1415 | * @param int Duration in hours |
||
| 1416 | * @return void |
||
| 1417 | */ |
||
| 1418 | public function set_thematic_advance_attributes( |
||
| 1433 | |||
| 1434 | /** |
||
| 1435 | * set thematic id |
||
| 1436 | * @param int Thematic id |
||
| 1437 | * @return void |
||
| 1438 | */ |
||
| 1439 | public function set_thematic_id($thematic_id) |
||
| 1443 | |||
| 1444 | /** |
||
| 1445 | * get thematic id |
||
| 1446 | * @return void |
||
| 1447 | */ |
||
| 1448 | public function get_thematic_id() |
||
| 1452 | |||
| 1453 | /** |
||
| 1454 | * Get thematic plan titles by default |
||
| 1455 | * @return array |
||
| 1456 | */ |
||
| 1457 | public function get_default_thematic_plan_title() |
||
| 1469 | |||
| 1470 | /** |
||
| 1471 | * Get thematic plan icons by default |
||
| 1472 | * @return array |
||
| 1473 | */ |
||
| 1474 | public function get_default_thematic_plan_icon() |
||
| 1486 | |||
| 1487 | /** |
||
| 1488 | * Get questions by default for help |
||
| 1489 | * @return array |
||
| 1490 | */ |
||
| 1491 | public function get_default_question() |
||
| 1502 | } |
||
| 1503 |
This check marks private properties in classes that are never used. Those properties can be removed.