| Total Complexity | 173 |
| Total Lines | 1309 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TrackingCourseLog 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 TrackingCourseLog, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class TrackingCourseLog |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Counts the item resources and returns the result as a mixed type. |
||
| 12 | */ |
||
| 13 | public static function countItemResources(): mixed |
||
| 14 | { |
||
| 15 | $sessionId = api_get_session_id(); |
||
| 16 | $courseId = api_get_course_int_id(); |
||
| 17 | |||
| 18 | $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 19 | $tableUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 20 | |||
| 21 | $sql = "SELECT count(tool) AS total_number_of_items |
||
| 22 | FROM $tableItemProperty track_resource, $tableUser user |
||
| 23 | WHERE |
||
| 24 | track_resource.c_id = $courseId AND |
||
| 25 | track_resource.insert_user_id = user.user_id AND |
||
| 26 | session_id ".(empty($sessionId) ? ' IS NULL ' : " = $sessionId "); |
||
| 27 | |||
| 28 | if (isset($_GET['keyword'])) { |
||
| 29 | $keyword = Database::escape_string(trim($_GET['keyword'])); |
||
| 30 | $sql .= " AND ( |
||
| 31 | user.username LIKE '%".$keyword."%' OR |
||
| 32 | lastedit_type LIKE '%".$keyword."%' OR |
||
| 33 | tool LIKE '%".$keyword."%' |
||
| 34 | )"; |
||
| 35 | } |
||
| 36 | |||
| 37 | $sql .= " AND tool IN ( |
||
| 38 | 'document', |
||
| 39 | 'learnpath', |
||
| 40 | 'quiz', |
||
| 41 | 'glossary', |
||
| 42 | 'link', |
||
| 43 | 'course_description', |
||
| 44 | 'announcement', |
||
| 45 | 'thematic', |
||
| 46 | 'thematic_advance', |
||
| 47 | 'thematic_plan' |
||
| 48 | )"; |
||
| 49 | $res = Database::query($sql); |
||
| 50 | $obj = Database::fetch_object($res); |
||
| 51 | |||
| 52 | return $obj->total_number_of_items; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Retrieves item resources data with pagination and sorting options. |
||
| 57 | */ |
||
| 58 | public static function getItemResourcesData($from, $numberOfItems, $column, $direction): array |
||
| 59 | { |
||
| 60 | $sessionId = api_get_session_id(); |
||
| 61 | $courseId = api_get_course_int_id(); |
||
| 62 | |||
| 63 | $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 64 | $tableUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 65 | $tableSession = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 66 | $column = (int) $column; |
||
| 67 | $direction = !in_array(strtolower(trim($direction)), ['asc', 'desc']) ? 'asc' : $direction; |
||
| 68 | |||
| 69 | $sql = "SELECT |
||
| 70 | tool as col0, |
||
| 71 | lastedit_type as col1, |
||
| 72 | ref as ref, |
||
| 73 | user.username as col3, |
||
| 74 | insert_date as col6, |
||
| 75 | visibility as col7, |
||
| 76 | user.user_id as user_id |
||
| 77 | FROM $tableItemProperty track_resource, $tableUser user |
||
| 78 | WHERE |
||
| 79 | track_resource.c_id = $courseId AND |
||
| 80 | track_resource.insert_user_id = user.user_id AND |
||
| 81 | session_id ".(empty($sessionId) ? ' IS NULL ' : " = $sessionId "); |
||
| 82 | |||
| 83 | if (isset($_GET['keyword'])) { |
||
| 84 | $keyword = Database::escape_string(trim($_GET['keyword'])); |
||
| 85 | $sql .= " AND ( |
||
| 86 | user.username LIKE '%".$keyword."%' OR |
||
| 87 | lastedit_type LIKE '%".$keyword."%' OR |
||
| 88 | tool LIKE '%".$keyword."%' |
||
| 89 | ) "; |
||
| 90 | } |
||
| 91 | |||
| 92 | $sql .= " AND tool IN ( |
||
| 93 | 'document', |
||
| 94 | 'learnpath', |
||
| 95 | 'quiz', |
||
| 96 | 'glossary', |
||
| 97 | 'link', |
||
| 98 | 'course_description', |
||
| 99 | 'announcement', |
||
| 100 | 'thematic', |
||
| 101 | 'thematic_advance', |
||
| 102 | 'thematic_plan' |
||
| 103 | )"; |
||
| 104 | |||
| 105 | if (0 == $column) { |
||
| 106 | $column = '0'; |
||
| 107 | } |
||
| 108 | if ('' != $column && '' != $direction) { |
||
| 109 | if (2 != $column && 4 != $column) { |
||
| 110 | $sql .= " ORDER BY col$column $direction"; |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $sql .= " ORDER BY col6 DESC "; |
||
| 114 | } |
||
| 115 | |||
| 116 | $from = intval($from); |
||
| 117 | if ($from) { |
||
| 118 | $numberOfItems = intval($numberOfItems); |
||
| 119 | $sql .= " LIMIT $from, $numberOfItems "; |
||
| 120 | } |
||
| 121 | |||
| 122 | $res = Database::query($sql); |
||
| 123 | $resources = []; |
||
| 124 | $thematicTools = ['thematic', 'thematic_advance', 'thematic_plan']; |
||
| 125 | while ($row = Database::fetch_array($res)) { |
||
| 126 | $ref = $row['ref']; |
||
| 127 | $tableName = self::getToolNameTable($row['col0']); |
||
| 128 | $tableTool = Database::get_course_table($tableName['table_name']); |
||
| 129 | |||
| 130 | $id = $tableName['id_tool']; |
||
| 131 | $recorset = false; |
||
| 132 | |||
| 133 | if (in_array($row['col0'], ['thematic_plan', 'thematic_advance'])) { |
||
| 134 | $tblThematic = Database::get_course_table(TABLE_THEMATIC); |
||
| 135 | $sql = "SELECT thematic_id FROM $tableTool |
||
| 136 | WHERE c_id = $courseId AND id = $ref"; |
||
| 137 | $rsThematic = Database::query($sql); |
||
| 138 | if (Database::num_rows($rsThematic)) { |
||
| 139 | $rowThematic = Database::fetch_array($rsThematic); |
||
| 140 | $thematicId = $rowThematic['thematic_id']; |
||
| 141 | |||
| 142 | $sql = "SELECT session.id, session.name, user.username |
||
| 143 | FROM $tblThematic t, $tableSession session, $tableUser user |
||
| 144 | WHERE |
||
| 145 | t.c_id = $courseId AND |
||
| 146 | t.session_id = session.id AND |
||
| 147 | session.id_coach = user.user_id AND |
||
| 148 | t.id = $thematicId"; |
||
| 149 | $recorset = Database::query($sql); |
||
| 150 | } |
||
| 151 | } else { |
||
| 152 | $sql = "SELECT session.id, session.name, user.username |
||
| 153 | FROM $tableTool tool, $tableSession session, $tableUser user |
||
| 154 | WHERE |
||
| 155 | tool.c_id = $courseId AND |
||
| 156 | tool.session_id = session.id AND |
||
| 157 | session.id_coach = user.user_id AND |
||
| 158 | tool.$id = $ref"; |
||
| 159 | $recorset = Database::query($sql); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!empty($recorset)) { |
||
| 163 | $obj = Database::fetch_object($recorset); |
||
| 164 | |||
| 165 | $nameSession = ''; |
||
| 166 | $coachName = ''; |
||
| 167 | if (!empty($obj)) { |
||
| 168 | $nameSession = $obj->name; |
||
| 169 | $coachName = $obj->username; |
||
| 170 | } |
||
| 171 | |||
| 172 | $urlTool = api_get_path(WEB_CODE_PATH).$tableName['link_tool']; |
||
| 173 | |||
| 174 | if ($row['col6'] != 2) { |
||
| 175 | if (in_array($row['col0'], $thematicTools)) { |
||
| 176 | $expThematicTool = explode('_', $row['col0']); |
||
| 177 | $thematicTooltitle = ''; |
||
| 178 | if (is_array($expThematicTool)) { |
||
| 179 | foreach ($expThematicTool as $exp) { |
||
| 180 | $thematicTooltitle .= api_ucfirst($exp); |
||
| 181 | } |
||
| 182 | } else { |
||
| 183 | $thematicTooltitle = api_ucfirst($row['col0']); |
||
| 184 | } |
||
| 185 | |||
| 186 | $row[0] = '<a href="'.$urlTool.'?'.api_get_cidreq().'&action=thematic_details">'.get_lang( |
||
| 187 | $thematicTooltitle |
||
| 188 | ).'</a>'; |
||
| 189 | } else { |
||
| 190 | $row[0] = '<a href="'.$urlTool.'?'.api_get_cidreq().'">'.get_lang( |
||
| 191 | 'Tool'.api_ucfirst($row['col0']) |
||
| 192 | ).'</a>'; |
||
| 193 | } |
||
| 194 | } else { |
||
| 195 | $row[0] = api_ucfirst($row['col0']); |
||
| 196 | } |
||
| 197 | $row[1] = get_lang($row[1]); |
||
| 198 | $row[6] = api_convert_and_format_date($row['col6'], null, date_default_timezone_get()); |
||
| 199 | $row[5] = ''; |
||
| 200 | //@todo Improve this code please |
||
| 201 | switch ($tableName['table_name']) { |
||
| 202 | case 'document': |
||
| 203 | $sql = "SELECT tool.title as title FROM $tableTool tool |
||
| 204 | WHERE c_id = $courseId AND id = $ref"; |
||
| 205 | $rsDocument = Database::query($sql); |
||
| 206 | $objDocument = Database::fetch_object($rsDocument); |
||
| 207 | if ($objDocument) { |
||
| 208 | $row[5] = $objDocument->title; |
||
| 209 | } |
||
| 210 | break; |
||
| 211 | case 'quiz': |
||
| 212 | case 'course_description': |
||
| 213 | case 'announcement': |
||
| 214 | $sql = "SELECT title FROM $tableTool |
||
| 215 | WHERE c_id = $courseId AND id = $ref"; |
||
| 216 | $rsDocument = Database::query($sql); |
||
| 217 | $objDocument = Database::fetch_object($rsDocument); |
||
| 218 | if ($objDocument) { |
||
| 219 | $row[5] = $objDocument->title; |
||
| 220 | } |
||
| 221 | break; |
||
| 222 | case 'glossary': |
||
| 223 | $sql = "SELECT name FROM $tableTool |
||
| 224 | WHERE c_id = $courseId AND glossary_id = $ref"; |
||
| 225 | $rsDocument = Database::query($sql); |
||
| 226 | $objDocument = Database::fetch_object($rsDocument); |
||
| 227 | if ($objDocument) { |
||
| 228 | $row[5] = $objDocument->name; |
||
| 229 | } |
||
| 230 | break; |
||
| 231 | case 'lp': |
||
| 232 | $sql = "SELECT name |
||
| 233 | FROM $tableTool WHERE c_id = $courseId AND id = $ref"; |
||
| 234 | $rsDocument = Database::query($sql); |
||
| 235 | $objDocument = Database::fetch_object($rsDocument); |
||
| 236 | $row[5] = $objDocument->name; |
||
| 237 | break; |
||
| 238 | case 'thematic_plan': |
||
| 239 | case 'thematic': |
||
| 240 | $rs = Database::query("SELECT title FROM $tableTool WHERE c_id = $courseId AND id = $ref"); |
||
| 241 | if (Database::num_rows($rs) > 0) { |
||
| 242 | $obj = Database::fetch_object($rs); |
||
| 243 | if ($obj) { |
||
| 244 | $row[5] = $obj->title; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | break; |
||
| 248 | case 'thematic_advance': |
||
| 249 | $rs = Database::query("SELECT content FROM $tableTool WHERE c_id = $courseId AND id = $ref"); |
||
| 250 | if (Database::num_rows($rs) > 0) { |
||
| 251 | $obj = Database::fetch_object($rs); |
||
| 252 | if ($obj) { |
||
| 253 | $row[5] = $obj->content; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | break; |
||
| 257 | case 'thematic_plan': |
||
| 258 | $rs = Database::query("SELECT title FROM $tableTool WHERE c_id = $courseId AND id = $ref"); |
||
| 259 | if (Database::num_rows($rs) > 0) { |
||
| 260 | $obj = Database::fetch_object($rs); |
||
| 261 | if ($obj) { |
||
| 262 | $row[5] = $obj->title; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | break; |
||
| 266 | default: |
||
| 267 | break; |
||
| 268 | } |
||
| 269 | |||
| 270 | $row2 = $nameSession; |
||
| 271 | if (!empty($coachName)) { |
||
| 272 | $row2 .= '<br />'.get_lang('Coach').': '.$coachName; |
||
| 273 | } |
||
| 274 | $row[2] = $row2; |
||
| 275 | if (!empty($row['col3'])) { |
||
| 276 | $userInfo = api_get_user_info($row['user_id']); |
||
| 277 | $row['col3'] = Display::url( |
||
| 278 | $row['col3'], |
||
| 279 | $userInfo['profile_url'] |
||
| 280 | ); |
||
| 281 | $row[3] = $row['col3']; |
||
| 282 | |||
| 283 | $ip = Tracking::get_ip_from_user_event( |
||
| 284 | $row['user_id'], |
||
| 285 | $row['col6'], |
||
| 286 | true |
||
| 287 | ); |
||
| 288 | if (empty($ip)) { |
||
| 289 | $ip = get_lang('Unknown'); |
||
| 290 | } |
||
| 291 | $row[4] = $ip; |
||
| 292 | } |
||
| 293 | |||
| 294 | $resources[] = $row; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | return $resources; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Retrieves the name and associated table for a given tool. |
||
| 303 | */ |
||
| 304 | public static function getToolNameTable(string $tool): array |
||
| 369 | ]; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Displays additional profile fields, excluding specific fields if provided. |
||
| 374 | */ |
||
| 375 | public static function displayAdditionalProfileFields(array $exclude = [], $formAction = null): string |
||
| 376 | { |
||
| 377 | $formAction = $formAction ?: 'courseLog.php'; |
||
| 378 | |||
| 379 | // getting all the extra profile fields that are defined by the platform administrator |
||
| 380 | $extraFields = UserManager::get_extra_fields(0, 50); |
||
| 381 | |||
| 382 | // creating the form |
||
| 383 | $return = '<form action="'.$formAction.'" method="get" name="additional_profile_field_form" |
||
| 384 | id="additional_profile_field_form">'; |
||
| 385 | // the select field with the additional user profile fields, this is where we select the field of which we want to see |
||
| 386 | // the information the users have entered or selected. |
||
| 387 | $return .= '<div class="form-group">'; |
||
| 388 | $return .= '<select class="chzn-select" name="additional_profile_field[]" multiple>'; |
||
| 389 | $return .= '<option value="-">'.get_lang('SelectFieldToAdd').'</option>'; |
||
| 390 | $extraFieldsToShow = 0; |
||
| 391 | foreach ($extraFields as $field) { |
||
| 392 | // exclude extra profile fields by id |
||
| 393 | if (in_array($field[3], $exclude)) { |
||
| 394 | continue; |
||
| 395 | } |
||
| 396 | // show only extra fields that are visible + and can be filtered, added by J.Montoya |
||
| 397 | if ($field[6] == 1 && $field[8] == 1) { |
||
| 398 | if (isset($_GET['additional_profile_field']) && in_array($field[0], $_GET['additional_profile_field'])) { |
||
| 399 | $selected = 'selected="selected"'; |
||
| 400 | } else { |
||
| 401 | $selected = ''; |
||
| 402 | } |
||
| 403 | $extraFieldsToShow++; |
||
| 404 | $return .= '<option value="'.$field[0].'" '.$selected.'>'.$field[3].'</option>'; |
||
| 405 | } |
||
| 406 | } |
||
| 407 | $return .= '</select>'; |
||
| 408 | $return .= '</div>'; |
||
| 409 | |||
| 410 | // the form elements for the $_GET parameters (because the form is passed through GET |
||
| 411 | foreach ($_GET as $key => $value) { |
||
| 412 | if ($key != 'additional_profile_field') { |
||
| 413 | $return .= '<input type="hidden" name="'.Security::remove_XSS($key).'" value="'.Security::remove_XSS( |
||
| 414 | $value |
||
| 415 | ).'" />'; |
||
| 416 | } |
||
| 417 | } |
||
| 418 | // the submit button |
||
| 419 | $return .= '<div class="form-group">'; |
||
| 420 | $return .= '<button class="save btn btn-primary" type="submit">' |
||
| 421 | .get_lang('AddAdditionalProfileField').'</button>'; |
||
| 422 | $return .= '</div>'; |
||
| 423 | $return .= '</form>'; |
||
| 424 | |||
| 425 | return $extraFieldsToShow > 0 ? $return : ''; |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * This function gets all the information of a certrain ($field_id) |
||
| 430 | * additional profile field for a specific list of users is more efficent |
||
| 431 | * than get_addtional_profile_information_of_field() function |
||
| 432 | * It gets the information of all the users so that it can be displayed |
||
| 433 | * in the sortable table or in the csv or xls export. |
||
| 434 | * |
||
| 435 | * @param int $fieldId field id |
||
| 436 | * @param array $users list of user ids |
||
| 437 | * |
||
| 438 | * @author Julio Montoya <[email protected]> |
||
| 439 | * |
||
| 440 | * @since Nov 2009 |
||
| 441 | * |
||
| 442 | * @version 1.8.6.2 |
||
| 443 | */ |
||
| 444 | public static function getAdditionalProfileInformationOfFieldByUser($fieldId, $users): array |
||
| 522 | } |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get number of users for sortable with pagination. |
||
| 526 | */ |
||
| 527 | public static function getNumberOfUsers(array $conditions): int |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Get data for users list in sortable with pagination. |
||
| 536 | */ |
||
| 537 | public static function getUserData( |
||
| 538 | $from, |
||
| 539 | $numberOfItems, |
||
| 540 | $column, |
||
| 541 | $direction, |
||
| 542 | array $conditions = [], |
||
| 543 | bool $exerciseToCheckConfig = true, |
||
| 544 | bool $displaySessionInfo = false, |
||
| 545 | ?string $courseCode = null, |
||
| 546 | ?int $sessionId = null, |
||
| 547 | bool $exportCsv = false, |
||
| 548 | array $userIds = [] |
||
| 549 | ) { |
||
| 550 | $includeInvitedUsers = $conditions['include_invited_users'] ?? false; |
||
| 551 | $getCount = $conditions['get_count'] ?? false; |
||
| 552 | |||
| 553 | $csvContent = []; |
||
| 554 | $tblUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 555 | $tblUrlRelUser = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 556 | $accessUrlId = api_get_current_access_url_id(); |
||
| 557 | |||
| 558 | if (!empty($userIds) && is_array($userIds)) { |
||
| 559 | $userIds = array_map('intval', $userIds); |
||
| 560 | $conditionUser = " WHERE user.id IN (".implode(',', $userIds).") "; |
||
| 561 | } else { |
||
| 562 | $conditionUser = " WHERE user.id = " . (int) $userIds; |
||
| 563 | } |
||
| 564 | |||
| 565 | if (!empty($_GET['user_keyword'])) { |
||
| 566 | $keyword = trim(Database::escape_string($_GET['user_keyword'])); |
||
| 567 | $conditionUser .= " AND ( |
||
| 568 | user.firstname LIKE '%".$keyword."%' OR |
||
| 569 | user.lastname LIKE '%".$keyword."%' OR |
||
| 570 | user.username LIKE '%".$keyword."%' OR |
||
| 571 | user.email LIKE '%".$keyword."%' |
||
| 572 | ) "; |
||
| 573 | } |
||
| 574 | |||
| 575 | $urlTable = ''; |
||
| 576 | $urlCondition = ''; |
||
| 577 | if (api_is_multiple_url_enabled()) { |
||
| 578 | $urlTable = " INNER JOIN $tblUrlRelUser as url_users ON (user.id = url_users.user_id)"; |
||
| 579 | $urlCondition = " AND access_url_id = '$accessUrlId'"; |
||
| 580 | } |
||
| 581 | |||
| 582 | $invitedUsersCondition = ''; |
||
| 583 | if (!$includeInvitedUsers) { |
||
| 584 | $invitedUsersCondition = " AND user.status != ".INVITEE; |
||
| 585 | } |
||
| 586 | |||
| 587 | $select = ' |
||
| 588 | SELECT user.id as user_id, |
||
| 589 | user.official_code as col0, |
||
| 590 | user.lastname as col1, |
||
| 591 | user.firstname as col2, |
||
| 592 | user.username as col3, |
||
| 593 | user.email as col4'; |
||
| 594 | |||
| 595 | if ($getCount) { |
||
| 596 | $select = ' SELECT COUNT(distinct(user.id)) as count '; |
||
| 597 | } |
||
| 598 | |||
| 599 | $sqlInjectJoins = ''; |
||
| 600 | $where = 'AND 1 = 1 '; |
||
| 601 | $sqlInjectWhere = ''; |
||
| 602 | if (!empty($conditions)) { |
||
| 603 | if (isset($conditions['inject_joins'])) { |
||
| 604 | $sqlInjectJoins = $conditions['inject_joins']; |
||
| 605 | } |
||
| 606 | if (isset($conditions['where'])) { |
||
| 607 | $where = $conditions['where']; |
||
| 608 | } |
||
| 609 | if (isset($conditions['inject_where'])) { |
||
| 610 | $sqlInjectWhere = $conditions['inject_where']; |
||
| 611 | } |
||
| 612 | $injectExtraFields = !empty($conditions['inject_extra_fields']) ? $conditions['inject_extra_fields'] : 1; |
||
| 613 | $injectExtraFields = rtrim($injectExtraFields, ', '); |
||
| 614 | if (false === $getCount) { |
||
| 615 | $select .= " , $injectExtraFields"; |
||
| 616 | } |
||
| 617 | } |
||
| 618 | |||
| 619 | $sql = "$select |
||
| 620 | FROM $tblUser as user |
||
| 621 | $urlTable |
||
| 622 | $sqlInjectJoins |
||
| 623 | $conditionUser |
||
| 624 | $urlCondition |
||
| 625 | $invitedUsersCondition |
||
| 626 | $where |
||
| 627 | $sqlInjectWhere |
||
| 628 | "; |
||
| 629 | |||
| 630 | if (!in_array($direction, ['ASC', 'DESC'])) { |
||
| 631 | $direction = 'ASC'; |
||
| 632 | } |
||
| 633 | |||
| 634 | $column = $column <= 2 ? (int) $column : 0; |
||
| 635 | $from = (int) $from; |
||
| 636 | $numberOfItems = (int) $numberOfItems; |
||
| 637 | |||
| 638 | if ($getCount) { |
||
| 639 | $res = Database::query($sql); |
||
| 640 | $row = Database::fetch_array($res); |
||
| 641 | |||
| 642 | return $row['count']; |
||
| 643 | } |
||
| 644 | |||
| 645 | $sortByFirstName = api_sort_by_first_name(); |
||
| 646 | |||
| 647 | if ($sortByFirstName) { |
||
| 648 | if ($column == 1) { |
||
| 649 | $column = 2; |
||
| 650 | } elseif ($column == 2) { |
||
| 651 | $column = 1; |
||
| 652 | } |
||
| 653 | } |
||
| 654 | |||
| 655 | $sql .= " ORDER BY col$column $direction "; |
||
| 656 | $sql .= " LIMIT $from, $numberOfItems"; |
||
| 657 | |||
| 658 | $res = Database::query($sql); |
||
| 659 | $users = []; |
||
| 660 | |||
| 661 | $courseInfo = api_get_course_info($courseCode); |
||
| 662 | $courseId = $courseInfo['real_id']; |
||
| 663 | |||
| 664 | $totalSurveys = 0; |
||
| 665 | $totalExercises = ExerciseLib::get_all_exercises( |
||
| 666 | $courseInfo, |
||
| 667 | $sessionId |
||
| 668 | ); |
||
| 669 | |||
| 670 | if (empty($sessionId)) { |
||
| 671 | $surveyUserList = []; |
||
| 672 | $surveyList = SurveyManager::get_surveys($courseCode); |
||
| 673 | if ($surveyList) { |
||
| 674 | $totalSurveys = count($surveyList); |
||
| 675 | foreach ($surveyList as $survey) { |
||
| 676 | $userList = SurveyManager::get_people_who_filled_survey( |
||
| 677 | $survey['survey_id'], |
||
| 678 | false, |
||
| 679 | $courseId |
||
| 680 | ); |
||
| 681 | |||
| 682 | foreach ($userList as $user_id) { |
||
| 683 | isset($surveyUserList[$user_id]) ? $surveyUserList[$user_id]++ : $surveyUserList[$user_id] = 1; |
||
| 684 | } |
||
| 685 | } |
||
| 686 | } |
||
| 687 | } |
||
| 688 | |||
| 689 | $urlBase = api_get_path(WEB_CODE_PATH).'my_space/myStudents.php?details=true&cid='.$courseId. |
||
| 690 | '&course='.$courseCode.'&origin=tracking_course&sid='.$sessionId; |
||
| 691 | |||
| 692 | Session::write('user_id_list', []); |
||
| 693 | $userIdList = []; |
||
| 694 | |||
| 695 | if ($exerciseToCheckConfig) { |
||
| 696 | $addExerciseOption = api_get_setting('exercise.add_exercise_best_attempt_in_report', true); |
||
| 697 | $exerciseResultsToCheck = []; |
||
| 698 | if (!empty($addExerciseOption) && isset($addExerciseOption['courses']) && |
||
| 699 | isset($addExerciseOption['courses'][$courseCode]) |
||
| 700 | ) { |
||
| 701 | foreach ($addExerciseOption['courses'][$courseCode] as $exerciseId) { |
||
| 702 | $exercise = new Exercise(); |
||
| 703 | $exercise->read($exerciseId); |
||
| 704 | if ($exercise->iid) { |
||
| 705 | $exerciseResultsToCheck[] = $exercise; |
||
| 706 | } |
||
| 707 | } |
||
| 708 | } |
||
| 709 | } |
||
| 710 | |||
| 711 | $lpShowMaxProgress = 'true' === api_get_setting('lp.lp_show_max_progress_instead_of_average'); |
||
| 712 | if ('true' === api_get_setting('lp.lp_show_max_progress_or_average_enable_course_level_redefinition')) { |
||
| 713 | $lpShowProgressCourseSetting = api_get_course_setting('lp_show_max_or_average_progress', $courseInfo, true); |
||
| 714 | if (in_array($lpShowProgressCourseSetting, ['max', 'average'])) { |
||
| 715 | $lpShowMaxProgress = ('max' === $lpShowProgressCourseSetting); |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | while ($user = Database::fetch_array($res, 'ASSOC')) { |
||
| 720 | $userIdList[] = $user['user_id']; |
||
| 721 | $user['official_code'] = $user['col0']; |
||
| 722 | $user['username'] = $user['col3']; |
||
| 723 | $user['time'] = api_time_to_hms( |
||
| 724 | Tracking::get_time_spent_on_the_course( |
||
| 725 | $user['user_id'], |
||
| 726 | $courseId, |
||
| 727 | $sessionId |
||
| 728 | ) |
||
| 729 | ); |
||
| 730 | |||
| 731 | $avgStudentScore = Tracking::get_avg_student_score( |
||
| 732 | $user['user_id'], |
||
| 733 | api_get_course_entity($courseId), |
||
| 734 | [], |
||
| 735 | api_get_session_entity($sessionId) |
||
| 736 | ); |
||
| 737 | |||
| 738 | $averageBestScore = Tracking::get_avg_student_score( |
||
| 739 | $user['user_id'], |
||
| 740 | api_get_course_entity($courseId), |
||
| 741 | [], |
||
| 742 | api_get_session_entity($sessionId), |
||
| 743 | false, |
||
| 744 | false, |
||
| 745 | true |
||
| 746 | ); |
||
| 747 | |||
| 748 | $avgStudentProgress = Tracking::get_avg_student_progress( |
||
| 749 | $user['user_id'], |
||
| 750 | api_get_course_entity($courseId), |
||
| 751 | [], |
||
| 752 | api_get_session_entity($sessionId) |
||
| 753 | ); |
||
| 754 | |||
| 755 | if (empty($avgStudentProgress)) { |
||
| 756 | $avgStudentProgress = 0; |
||
| 757 | } |
||
| 758 | $user['average_progress'] = $avgStudentProgress.'%'; |
||
| 759 | |||
| 760 | $totalUserExercise = Tracking::get_exercise_student_progress( |
||
| 761 | $totalExercises, |
||
| 762 | $user['user_id'], |
||
| 763 | $courseId, |
||
| 764 | $sessionId |
||
| 765 | ); |
||
| 766 | |||
| 767 | $user['exercise_progress'] = $totalUserExercise; |
||
| 768 | |||
| 769 | $totalUserExercise = Tracking::get_exercise_student_average_best_attempt( |
||
| 770 | $totalExercises, |
||
| 771 | $user['user_id'], |
||
| 772 | $courseId, |
||
| 773 | $sessionId |
||
| 774 | ); |
||
| 775 | |||
| 776 | $user['exercise_average_best_attempt'] = $totalUserExercise; |
||
| 777 | |||
| 778 | if (is_numeric($avgStudentScore)) { |
||
| 779 | $user['student_score'] = $avgStudentScore.'%'; |
||
| 780 | } else { |
||
| 781 | $user['student_score'] = $avgStudentScore; |
||
| 782 | } |
||
| 783 | |||
| 784 | if (is_numeric($averageBestScore)) { |
||
| 785 | $user['student_score_best'] = $averageBestScore.'%'; |
||
| 786 | } else { |
||
| 787 | $user['student_score_best'] = $averageBestScore; |
||
| 788 | } |
||
| 789 | |||
| 790 | $exerciseResults = []; |
||
| 791 | if (!empty($exerciseResultsToCheck)) { |
||
| 792 | foreach ($exerciseResultsToCheck as $exercise) { |
||
| 793 | $bestExerciseResult = Event::get_best_attempt_exercise_results_per_user( |
||
| 794 | $user['user_id'], |
||
| 795 | $exercise->iid, |
||
| 796 | $courseId, |
||
| 797 | $sessionId, |
||
| 798 | false |
||
| 799 | ); |
||
| 800 | |||
| 801 | $best = null; |
||
| 802 | if ($bestExerciseResult) { |
||
| 803 | $best = $bestExerciseResult['exe_result'] / $bestExerciseResult['exe_weighting']; |
||
| 804 | $best = round($best, 2) * 100; |
||
| 805 | $best .= '%'; |
||
| 806 | } |
||
| 807 | $exerciseResults['exercise_'.$exercise->iid] = $best; |
||
| 808 | } |
||
| 809 | } |
||
| 810 | |||
| 811 | $user['first_connection'] = Tracking::get_first_connection_date_on_the_course( |
||
| 812 | $user['user_id'], |
||
| 813 | $courseId, |
||
| 814 | $sessionId, |
||
| 815 | !$exportCsv |
||
| 816 | ); |
||
| 817 | |||
| 818 | $user['last_connection'] = Tracking::get_last_connection_date_on_the_course( |
||
| 819 | $user['user_id'], |
||
| 820 | $courseInfo, |
||
| 821 | $sessionId, |
||
| 822 | !$exportCsv |
||
| 823 | ); |
||
| 824 | |||
| 825 | |||
| 826 | $user['count_assignments'] = Tracking::countStudentPublications( |
||
| 827 | $courseId, |
||
| 828 | $sessionId |
||
| 829 | ); |
||
| 830 | |||
| 831 | $user['count_messages'] = Tracking::countStudentMessages( |
||
| 832 | $courseId, |
||
| 833 | $sessionId |
||
| 834 | ); |
||
| 835 | |||
| 836 | |||
| 837 | $user['lp_finalization_date'] = Tracking::getCourseLpFinalizationDate( |
||
| 838 | $user['user_id'], |
||
| 839 | $courseId, |
||
| 840 | $sessionId, |
||
| 841 | !$exportCsv |
||
| 842 | ); |
||
| 843 | |||
| 844 | $user['quiz_finalization_date'] = Tracking::getCourseQuizLastFinalizationDate( |
||
| 845 | $user['user_id'], |
||
| 846 | $courseId, |
||
| 847 | $sessionId, |
||
| 848 | !$exportCsv |
||
| 849 | ); |
||
| 850 | |||
| 851 | if ($exportCsv) { |
||
| 852 | if (!empty($user['first_connection'])) { |
||
| 853 | $user['first_connection'] = api_get_local_time($user['first_connection']); |
||
| 854 | } else { |
||
| 855 | $user['first_connection'] = '-'; |
||
| 856 | } |
||
| 857 | if (!empty($user['last_connection'])) { |
||
| 858 | $user['last_connection'] = api_get_local_time($user['last_connection']); |
||
| 859 | } else { |
||
| 860 | $user['last_connection'] = '-'; |
||
| 861 | } |
||
| 862 | if (!empty($user['lp_finalization_date'])) { |
||
| 863 | $user['lp_finalization_date'] = api_get_local_time($user['lp_finalization_date']); |
||
| 864 | } else { |
||
| 865 | $user['lp_finalization_date'] = '-'; |
||
| 866 | } |
||
| 867 | if (!empty($user['quiz_finalization_date'])) { |
||
| 868 | $user['quiz_finalization_date'] = api_get_local_time($user['quiz_finalization_date']); |
||
| 869 | } else { |
||
| 870 | $user['quiz_finalization_date'] = '-'; |
||
| 871 | } |
||
| 872 | } |
||
| 873 | |||
| 874 | if (empty($sessionId)) { |
||
| 875 | $user['survey'] = ($surveyUserList[$user['user_id']] ?? 0).' / '.$totalSurveys; |
||
| 876 | } |
||
| 877 | |||
| 878 | $url = $urlBase.'&student='.$user['user_id']; |
||
| 879 | |||
| 880 | $user['link'] = '<a href="'.$url.'"> |
||
| 881 | '.Display::return_icon('2rightarrow.png', get_lang('Details')).' |
||
| 882 | </a>'; |
||
| 883 | |||
| 884 | // store columns in array $users |
||
| 885 | $userRow = []; |
||
| 886 | if ($displaySessionInfo && !empty($sessionId)) { |
||
| 887 | $sessionInfo = api_get_session_info($sessionId); |
||
| 888 | $userRow['session_name'] = $sessionInfo['name']; |
||
| 889 | $userRow['session_startdate'] = $sessionInfo['access_start_date']; |
||
| 890 | $userRow['session_enddate'] = $sessionInfo['access_end_date']; |
||
| 891 | $userRow['course_name'] = $courseInfo['name']; |
||
| 892 | } |
||
| 893 | $userRow['official_code'] = $user['official_code']; |
||
| 894 | if ($sortByFirstName) { |
||
| 895 | $userRow['firstname'] = $user['col2']; |
||
| 896 | $userRow['lastname'] = $user['col1']; |
||
| 897 | } else { |
||
| 898 | $userRow['lastname'] = $user['col1']; |
||
| 899 | $userRow['firstname'] = $user['col2']; |
||
| 900 | } |
||
| 901 | $userRow['username'] = $user['username']; |
||
| 902 | $userRow['time'] = $user['time']; |
||
| 903 | $userRow['average_progress'] = $user['average_progress']; |
||
| 904 | $userRow['exercise_progress'] = $user['exercise_progress']; |
||
| 905 | $userRow['exercise_average_best_attempt'] = $user['exercise_average_best_attempt']; |
||
| 906 | $userRow['student_score'] = $user['student_score']; |
||
| 907 | $userRow['student_score_best'] = $user['student_score_best']; |
||
| 908 | if (!empty($exerciseResults)) { |
||
| 909 | foreach ($exerciseResults as $exerciseId => $bestResult) { |
||
| 910 | $userRow[$exerciseId] = $bestResult; |
||
| 911 | } |
||
| 912 | } |
||
| 913 | |||
| 914 | $userRow['count_assignments'] = $user['count_assignments']; |
||
| 915 | $userRow['count_messages'] = $user['count_messages']; |
||
| 916 | |||
| 917 | $userGroupManager = new UserGroupModel(); |
||
| 918 | if ($exportCsv) { |
||
| 919 | $userRow['classes'] = implode( |
||
| 920 | ',', |
||
| 921 | $userGroupManager->getNameListByUser($user['user_id'], UserGroupModel::NORMAL_CLASS) |
||
| 922 | ); |
||
| 923 | } else { |
||
| 924 | $userRow['classes'] = $userGroupManager->getLabelsFromNameList( |
||
| 925 | $user['user_id'], |
||
| 926 | UserGroupModel::NORMAL_CLASS |
||
| 927 | ); |
||
| 928 | } |
||
| 929 | |||
| 930 | if (empty($sessionId)) { |
||
| 931 | $userRow['survey'] = $user['survey']; |
||
| 932 | } else { |
||
| 933 | $userSession = SessionManager::getUserSession($user['user_id'], $sessionId); |
||
| 934 | $userRow['registered_at'] = ''; |
||
| 935 | if ($userSession) { |
||
| 936 | $userRow['registered_at'] = api_get_local_time($userSession['registered_at']); |
||
| 937 | } |
||
| 938 | } |
||
| 939 | |||
| 940 | $userRow['first_connection'] = $user['first_connection']; |
||
| 941 | $userRow['last_connection'] = $user['last_connection']; |
||
| 942 | |||
| 943 | $userRow['lp_finalization_date'] = $user['lp_finalization_date']; |
||
| 944 | $userRow['quiz_finalization_date'] = $user['quiz_finalization_date']; |
||
| 945 | |||
| 946 | // we need to display an additional profile field |
||
| 947 | if (isset($_GET['additional_profile_field'])) { |
||
| 948 | $data = Session::read('additional_user_profile_info'); |
||
| 949 | |||
| 950 | $extraFieldInfo = Session::read('extra_field_info'); |
||
| 951 | foreach ($_GET['additional_profile_field'] as $fieldId) { |
||
| 952 | if (isset($data[$fieldId]) && isset($data[$fieldId][$user['user_id']])) { |
||
| 953 | if (is_array($data[$fieldId][$user['user_id']])) { |
||
| 954 | $userRow[$extraFieldInfo[$fieldId]['variable']] = implode( |
||
| 955 | ', ', |
||
| 956 | $data[$fieldId][$user['user_id']] |
||
| 957 | ); |
||
| 958 | } else { |
||
| 959 | $userRow[$extraFieldInfo[$fieldId]['variable']] = $data[$fieldId][$user['user_id']]; |
||
| 960 | } |
||
| 961 | } else { |
||
| 962 | $userRow[$extraFieldInfo[$fieldId]['variable']] = ''; |
||
| 963 | } |
||
| 964 | } |
||
| 965 | } |
||
| 966 | |||
| 967 | $data = Session::read('default_additional_user_profile_info'); |
||
| 968 | $defaultExtraFieldInfo = Session::read('default_extra_field_info'); |
||
| 969 | if (isset($defaultExtraFieldInfo) && isset($data)) { |
||
| 970 | foreach ($data as $key => $val) { |
||
| 971 | if (isset($val[$user['user_id']])) { |
||
| 972 | if (is_array($val[$user['user_id']])) { |
||
| 973 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = implode( |
||
| 974 | ', ', |
||
| 975 | $val[$user['user_id']] |
||
| 976 | ); |
||
| 977 | } else { |
||
| 978 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = $val[$user['user_id']]; |
||
| 979 | } |
||
| 980 | } else { |
||
| 981 | $userRow[$defaultExtraFieldInfo[$key]['variable']] = ''; |
||
| 982 | } |
||
| 983 | } |
||
| 984 | } |
||
| 985 | |||
| 986 | if (api_get_setting('show_email_addresses') === 'true') { |
||
| 987 | $userRow['email'] = $user['col4']; |
||
| 988 | } |
||
| 989 | |||
| 990 | $userRow['link'] = $user['link']; |
||
| 991 | |||
| 992 | if ($exportCsv) { |
||
| 993 | unset($userRow['link']); |
||
| 994 | $csvContent[] = $userRow; |
||
| 995 | } |
||
| 996 | $users[] = array_values($userRow); |
||
| 997 | } |
||
| 998 | |||
| 999 | if ($exportCsv) { |
||
| 1000 | Session::write('csv_content', $csvContent); |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | Session::erase('additional_user_profile_info'); |
||
| 1004 | Session::erase('extra_field_info'); |
||
| 1005 | Session::erase('default_additional_user_profile_info'); |
||
| 1006 | Session::erase('default_extra_field_info'); |
||
| 1007 | Session::write('user_id_list', $userIdList); |
||
| 1008 | |||
| 1009 | return $users; |
||
| 1010 | } |
||
| 1011 | |||
| 1012 | /** |
||
| 1013 | * Get data for users list in sortable with pagination. |
||
| 1014 | */ |
||
| 1015 | public static function getTotalTimeReport( |
||
| 1142 | } |
||
| 1143 | |||
| 1144 | /** |
||
| 1145 | * Determines the remaining actions for a session and returns a string with the results. |
||
| 1146 | */ |
||
| 1147 | public static function actionsLeft($current, $sessionId = 0): string |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | public static function calcBestScoreAverageNotInLP( |
||
| 1319 |