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 SessionManager 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 SessionManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class SessionManager |
||
| 18 | { |
||
| 19 | public static $_debug = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Constructor |
||
| 23 | */ |
||
| 24 | public function __construct() |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Fetches a session from the database |
||
| 30 | * @param int $id Session Id |
||
| 31 | * |
||
| 32 | * @return array Session details |
||
| 33 | */ |
||
| 34 | public static function fetch($id) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create a session |
||
| 51 | * @author Carlos Vargas <[email protected]>, from existing code |
||
| 52 | * @param string $name |
||
| 53 | * @param string $startDate (YYYY-MM-DD hh:mm:ss) |
||
| 54 | * @param string $endDate (YYYY-MM-DD hh:mm:ss) |
||
| 55 | * @param string $displayStartDate (YYYY-MM-DD hh:mm:ss) |
||
| 56 | * @param string $displayEndDate (YYYY-MM-DD hh:mm:ss) |
||
| 57 | * @param string $coachStartDate (YYYY-MM-DD hh:mm:ss) |
||
| 58 | * @param string $coachEndDate (YYYY-MM-DD hh:mm:ss) |
||
| 59 | * @param mixed $coachId If integer, this is the session coach id, if string, the coach ID will be looked for from the user table |
||
| 60 | * @param integer $sessionCategoryId ID of the session category in which this session is registered |
||
| 61 | * @param integer $visibility Visibility after end date (0 = read-only, 1 = invisible, 2 = accessible) |
||
| 62 | * @param bool $fixSessionNameIfExists |
||
| 63 | * @param string $duration |
||
| 64 | * @param string $description Optional. The session description |
||
| 65 | * @param int $showDescription Optional. Whether show the session description |
||
| 66 | * @param array $extraFields |
||
| 67 | * @param int $sessionAdminId Optional. If this sessions was created by a session admin, assign it to him |
||
| 68 | * @param boolean $sendSubscritionNotification Optional. |
||
| 69 | * Whether send a mail notification to users being subscribed |
||
| 70 | * @todo use an array to replace all this parameters or use the model.lib.php ... |
||
| 71 | * @return mixed Session ID on success, error message otherwise |
||
| 72 | * */ |
||
| 73 | public static function create_session( |
||
| 74 | $name, |
||
| 75 | $startDate, |
||
| 76 | $endDate, |
||
| 77 | $displayStartDate, |
||
| 78 | $displayEndDate, |
||
| 79 | $coachStartDate, |
||
| 80 | $coachEndDate, |
||
| 81 | $coachId, |
||
| 82 | $sessionCategoryId, |
||
| 83 | $visibility = 1, |
||
| 84 | $fixSessionNameIfExists = false, |
||
| 85 | $duration = null, |
||
| 86 | $description = null, |
||
| 87 | $showDescription = 0, |
||
| 88 | $extraFields = array(), |
||
| 89 | $sessionAdminId = 0, |
||
| 90 | $sendSubscritionNotification = false |
||
| 91 | ) { |
||
| 92 | global $_configuration; |
||
| 93 | |||
| 94 | //Check portal limits |
||
| 95 | $access_url_id = 1; |
||
| 96 | |||
| 97 | if (api_get_multiple_access_url()) { |
||
| 98 | $access_url_id = api_get_current_access_url_id(); |
||
| 99 | } |
||
| 100 | |||
| 101 | View Code Duplication | if (is_array($_configuration[$access_url_id]) && |
|
| 102 | isset($_configuration[$access_url_id]['hosting_limit_sessions']) && |
||
| 103 | $_configuration[$access_url_id]['hosting_limit_sessions'] > 0 |
||
| 104 | ) { |
||
| 105 | $num = self::count_sessions(); |
||
| 106 | if ($num >= $_configuration[$access_url_id]['hosting_limit_sessions']) { |
||
| 107 | api_warn_hosting_contact('hosting_limit_sessions'); |
||
| 108 | return get_lang('PortalSessionsLimitReached'); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | $name = Database::escape_string(trim($name)); |
||
| 113 | $sessionCategoryId = intval($sessionCategoryId); |
||
| 114 | $visibility = intval($visibility); |
||
| 115 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 116 | |||
| 117 | $startDate = Database::escape_string($startDate); |
||
| 118 | $endDate = Database::escape_string($endDate); |
||
| 119 | |||
| 120 | if (empty($name)) { |
||
| 121 | $msg = get_lang('SessionNameIsRequired'); |
||
| 122 | return $msg; |
||
| 123 | } elseif (empty($coachId)) { |
||
| 124 | $msg = get_lang('CoachIsRequired'); |
||
| 125 | return $msg; |
||
| 126 | } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i') && !api_is_valid_date($startDate, 'Y-m-d H:i:s')) { |
||
| 127 | $msg = get_lang('InvalidStartDate'); |
||
| 128 | return $msg; |
||
| 129 | } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i') && !api_is_valid_date($endDate, 'Y-m-d H:i:s')) { |
||
| 130 | $msg = get_lang('InvalidEndDate'); |
||
| 131 | return $msg; |
||
| 132 | } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) { |
||
| 133 | $msg = get_lang('StartDateShouldBeBeforeEndDate'); |
||
| 134 | return $msg; |
||
| 135 | } else { |
||
| 136 | $ready_to_create = false; |
||
| 137 | if ($fixSessionNameIfExists) { |
||
| 138 | $name = self::generateNextSessionName($name); |
||
| 139 | if ($name) { |
||
| 140 | $ready_to_create = true; |
||
| 141 | } else { |
||
| 142 | $msg = get_lang('SessionNameAlreadyExists'); |
||
| 143 | return $msg; |
||
| 144 | } |
||
| 145 | } else { |
||
| 146 | $rs = Database::query("SELECT 1 FROM $tbl_session WHERE name='" . $name . "'"); |
||
| 147 | if (Database::num_rows($rs)) { |
||
| 148 | $msg = get_lang('SessionNameAlreadyExists'); |
||
| 149 | return $msg; |
||
| 150 | } |
||
| 151 | $ready_to_create = true; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($ready_to_create) { |
||
| 155 | $sessionAdminId = !empty($sessionAdminId) ? $sessionAdminId : api_get_user_id(); |
||
| 156 | $values = array( |
||
| 157 | 'name' => $name, |
||
| 158 | 'id_coach' => $coachId, |
||
| 159 | 'session_admin_id' => $sessionAdminId, |
||
| 160 | 'visibility' => $visibility, |
||
| 161 | 'description' => $description, |
||
| 162 | 'show_description' => intval($showDescription), |
||
| 163 | 'send_subscription_notification' => $sendSubscritionNotification |
||
| 164 | ); |
||
| 165 | |||
| 166 | if (!empty($startDate)) { |
||
| 167 | $values['access_start_date'] = api_get_utc_datetime($startDate); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (!empty($endDate)) { |
||
| 171 | $values['access_end_date'] = api_get_utc_datetime($endDate); |
||
| 172 | } |
||
| 173 | |||
| 174 | if (!empty($displayStartDate)) { |
||
| 175 | $values['display_start_date'] = api_get_utc_datetime($displayStartDate); |
||
| 176 | } |
||
| 177 | |||
| 178 | if (!empty($displayEndDate)) { |
||
| 179 | $values['display_end_date'] = api_get_utc_datetime($displayEndDate); |
||
| 180 | } |
||
| 181 | |||
| 182 | if (!empty($coachStartDate)) { |
||
| 183 | $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate); |
||
| 184 | } |
||
| 185 | if (!empty($coachEndDate)) { |
||
| 186 | $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate); |
||
| 187 | } |
||
| 188 | |||
| 189 | if (!empty($sessionCategoryId)) { |
||
| 190 | $values['session_category_id'] = $sessionCategoryId; |
||
| 191 | } |
||
| 192 | |||
| 193 | $session_id = Database::insert($tbl_session, $values); |
||
| 194 | |||
| 195 | $duration = intval($duration); |
||
| 196 | |||
| 197 | View Code Duplication | if (!empty($duration)) { |
|
| 198 | $sql = "UPDATE $tbl_session SET |
||
| 199 | access_start_date = NULL, |
||
| 200 | access_end_date = NULL, |
||
| 201 | display_start_date = NULL, |
||
| 202 | display_end_date = NULL, |
||
| 203 | coach_access_start_date = NULL, |
||
| 204 | coach_access_end_date = NULL, |
||
| 205 | duration = $duration |
||
| 206 | WHERE id = $session_id"; |
||
| 207 | Database::query($sql); |
||
| 208 | } else { |
||
| 209 | $sql = "UPDATE $tbl_session |
||
| 210 | SET duration = 0 |
||
| 211 | WHERE id = $session_id"; |
||
| 212 | Database::query($sql); |
||
| 213 | } |
||
| 214 | |||
| 215 | if (!empty($session_id)) { |
||
| 216 | $extraFields['item_id'] = $session_id; |
||
| 217 | |||
| 218 | $sessionFieldValue = new ExtraFieldValue('session'); |
||
| 219 | $sessionFieldValue->saveFieldValues($extraFields); |
||
| 220 | |||
| 221 | /* |
||
| 222 | Sends a message to the user_id = 1 |
||
| 223 | |||
| 224 | $user_info = api_get_user_info(1); |
||
| 225 | $complete_name = $user_info['firstname'].' '.$user_info['lastname']; |
||
| 226 | $subject = api_get_setting('siteName').' - '.get_lang('ANewSessionWasCreated'); |
||
| 227 | $message = get_lang('ANewSessionWasCreated')." <br /> ".get_lang('NameOfTheSession').' : '.$name; |
||
| 228 | api_mail_html($complete_name, $user_info['email'], $subject, $message); |
||
| 229 | * |
||
| 230 | */ |
||
| 231 | //Adding to the correct URL |
||
| 232 | $access_url_id = api_get_current_access_url_id(); |
||
| 233 | UrlManager::add_session_to_url($session_id, $access_url_id); |
||
| 234 | |||
| 235 | // add event to system log |
||
| 236 | $user_id = api_get_user_id(); |
||
| 237 | Event::addEvent( |
||
| 238 | LOG_SESSION_CREATE, |
||
| 239 | LOG_SESSION_ID, |
||
| 240 | $session_id, |
||
| 241 | api_get_utc_datetime(), |
||
| 242 | $user_id |
||
| 243 | ); |
||
| 244 | } |
||
| 245 | |||
| 246 | return $session_id; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $name |
||
| 253 | * |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | public static function session_name_exists($name) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $where_condition |
||
| 268 | * |
||
| 269 | * @return mixed |
||
| 270 | */ |
||
| 271 | public static function get_count_admin($where_condition = '') |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Gets the admin session list callback of the session/session_list.php page |
||
| 386 | * @param array $options order and limit keys |
||
| 387 | * @param boolean $get_count Whether to get all the results or only the count |
||
| 388 | * @return mixed Integer for number of rows, or array of results |
||
| 389 | * @assert (array(),true) !== false |
||
| 390 | */ |
||
| 391 | public static function get_sessions_admin($options = array(), $get_count = false) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get total of records for progress of learning paths in the given session |
||
| 550 | * @param int session id |
||
| 551 | * @return int |
||
| 552 | */ |
||
| 553 | public static function get_count_session_lp_progress($sessionId = 0) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Gets the progress of learning paths in the given session |
||
| 577 | * @param int $sessionId |
||
| 578 | * @param int $courseId |
||
| 579 | * @param string $date_from |
||
| 580 | * @param string $date_to |
||
| 581 | * @param array options order and limit keys |
||
| 582 | * @return array table with user name, lp name, progress |
||
| 583 | */ |
||
| 584 | public static function get_session_lp_progress($sessionId = 0, $courseId = 0, $date_from, $date_to, $options) |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Gets the survey answers |
||
| 703 | * @param int $sessionId |
||
| 704 | * @param int $courseId |
||
| 705 | * @param int $surveyId |
||
| 706 | * @param array options order and limit keys |
||
| 707 | * @todo fix the query |
||
| 708 | * @return array table with user name, lp name, progress |
||
| 709 | */ |
||
| 710 | public static function get_survey_overview($sessionId = 0, $courseId = 0, $surveyId = 0, $date_from, $date_to, $options) |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Gets the progress of the given session |
||
| 814 | * @param int $sessionId |
||
| 815 | * @param int $courseId |
||
| 816 | * @param array options order and limit keys |
||
| 817 | * |
||
| 818 | * @return array table with user name, lp name, progress |
||
| 819 | */ |
||
| 820 | public static function get_session_progress($sessionId, $courseId, $date_from, $date_to, $options) |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * @return int |
||
| 1136 | */ |
||
| 1137 | public static function get_number_of_tracking_access_overview() |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Get the ip, total of clicks, login date and time logged in for all user, in one session |
||
| 1147 | * @todo track_e_course_access table should have ip so we dont have to look for it in track_e_login |
||
| 1148 | * |
||
| 1149 | * @author César Perales <[email protected]>, Beeznest Team |
||
| 1150 | * @version 1.9.6 |
||
| 1151 | */ |
||
| 1152 | public static function get_user_data_access_tracking_overview( |
||
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Creates a new course code based in given code |
||
| 1293 | * |
||
| 1294 | * @param string $session_name |
||
| 1295 | * <code> |
||
| 1296 | * $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3 |
||
| 1297 | * if the course code doest not exist in the DB the same course code will be returned |
||
| 1298 | * </code> |
||
| 1299 | * @return string wanted unused code |
||
| 1300 | */ |
||
| 1301 | View Code Duplication | public static function generateNextSessionName($session_name) |
|
| 1323 | |||
| 1324 | /** |
||
| 1325 | * Edit a session |
||
| 1326 | * @author Carlos Vargas from existing code |
||
| 1327 | * @param integer $id Session primary key |
||
| 1328 | * @param string $name |
||
| 1329 | * @param string $startDate |
||
| 1330 | * @param string $endDate |
||
| 1331 | * @param string $displayStartDate |
||
| 1332 | * @param string $displayEndDate |
||
| 1333 | * @param string $coachStartDate |
||
| 1334 | * @param string $coachEndDate |
||
| 1335 | * @param integer $coachId |
||
| 1336 | * @param integer $sessionCategoryId |
||
| 1337 | * @param int $visibility |
||
| 1338 | * @param string $description |
||
| 1339 | * @param bool $showDescription |
||
| 1340 | * @param int $duration |
||
| 1341 | * @param array $extraFields |
||
| 1342 | * @param int $sessionAdminId |
||
| 1343 | * @param boolean $sendSubscriptionNotification Optional. |
||
| 1344 | * Whether send a mail notification to users being subscribed |
||
| 1345 | * @return mixed |
||
| 1346 | */ |
||
| 1347 | public static function edit_session( |
||
| 1348 | $id, |
||
| 1349 | $name, |
||
| 1350 | $startDate, |
||
| 1351 | $endDate, |
||
| 1352 | $displayStartDate, |
||
| 1353 | $displayEndDate, |
||
| 1354 | $coachStartDate, |
||
| 1355 | $coachEndDate, |
||
| 1356 | $coachId, |
||
| 1357 | $sessionCategoryId, |
||
| 1358 | $visibility, |
||
| 1359 | $description = null, |
||
| 1360 | $showDescription = 0, |
||
| 1361 | $duration = null, |
||
| 1362 | $extraFields = array(), |
||
| 1363 | $sessionAdminId = 0, |
||
| 1364 | $sendSubscriptionNotification = false |
||
| 1365 | ) { |
||
| 1366 | $name = trim(stripslashes($name)); |
||
| 1367 | $coachId = intval($coachId); |
||
| 1368 | $sessionCategoryId = intval($sessionCategoryId); |
||
| 1369 | $visibility = intval($visibility); |
||
| 1370 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1371 | |||
| 1372 | if (empty($name)) { |
||
| 1373 | Display::return_message(get_lang('SessionNameIsRequired'), 'warning'); |
||
| 1374 | |||
| 1375 | return false; |
||
| 1376 | } elseif (empty($coachId)) { |
||
| 1377 | Display::return_message(get_lang('CoachIsRequired'), 'warning'); |
||
| 1378 | |||
| 1379 | return false; |
||
| 1380 | } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i')) { |
||
| 1381 | Display::return_message(get_lang('InvalidStartDate'), 'warning'); |
||
| 1382 | |||
| 1383 | return false; |
||
| 1384 | } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i')) { |
||
| 1385 | Display::return_message(get_lang('InvalidEndDate'), 'warning'); |
||
| 1386 | |||
| 1387 | return false; |
||
| 1388 | } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) { |
||
| 1389 | Display::return_message(get_lang('StartDateShouldBeBeforeEndDate'), 'warning'); |
||
| 1390 | |||
| 1391 | return false; |
||
| 1392 | } else { |
||
| 1393 | $sql = "SELECT id FROM $tbl_session WHERE name='" . Database::escape_string($name) . "'"; |
||
| 1394 | $rs = Database::query($sql); |
||
| 1395 | $exists = false; |
||
| 1396 | while ($row = Database::fetch_array($rs)) { |
||
| 1397 | if ($row['id'] != $id) { |
||
| 1398 | $exists = true; |
||
| 1399 | } |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | if ($exists) { |
||
| 1403 | Display::return_message(get_lang('SessionNameAlreadyExists'), 'warning'); |
||
| 1404 | |||
| 1405 | return false; |
||
| 1406 | } else { |
||
| 1407 | $values = [ |
||
| 1408 | 'name' => $name, |
||
| 1409 | 'duration' => $duration, |
||
| 1410 | 'id_coach' => $coachId, |
||
| 1411 | 'description'=> $description, |
||
| 1412 | 'show_description' => intval($showDescription), |
||
| 1413 | 'visibility' => $visibility, |
||
| 1414 | 'send_subscription_notification' => $sendSubscriptionNotification |
||
| 1415 | ]; |
||
| 1416 | |||
| 1417 | if (!empty($sessionAdminId)) { |
||
| 1418 | $values['session_admin_id'] = $sessionAdminId; |
||
| 1419 | } |
||
| 1420 | |||
| 1421 | if (!empty($startDate)) { |
||
| 1422 | $values['access_start_date'] = api_get_utc_datetime($startDate); |
||
| 1423 | } |
||
| 1424 | |||
| 1425 | if (!empty($endDate)) { |
||
| 1426 | $values['access_end_date'] = api_get_utc_datetime($endDate); |
||
| 1427 | } |
||
| 1428 | |||
| 1429 | if (!empty($displayStartDate)) { |
||
| 1430 | $values['display_start_date'] = api_get_utc_datetime($displayStartDate); |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | if (!empty($displayEndDate)) { |
||
| 1434 | $values['display_end_date'] = api_get_utc_datetime($displayEndDate); |
||
| 1435 | } |
||
| 1436 | |||
| 1437 | if (!empty($coachStartDate)) { |
||
| 1438 | $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate); |
||
| 1439 | } |
||
| 1440 | if (!empty($coachEndDate)) { |
||
| 1441 | $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate); |
||
| 1442 | } |
||
| 1443 | |||
| 1444 | if (!empty($sessionCategoryId)) { |
||
| 1445 | $values['session_category_id'] = $sessionCategoryId; |
||
| 1446 | } |
||
| 1447 | |||
| 1448 | Database::update($tbl_session, $values, array( |
||
| 1449 | 'id = ?' => $id |
||
| 1450 | )); |
||
| 1451 | |||
| 1452 | if (!empty($extraFields)) { |
||
| 1453 | $extraFields['item_id'] = $id; |
||
| 1454 | $sessionFieldValue = new ExtraFieldValue('session'); |
||
| 1455 | $sessionFieldValue->saveFieldValues($extraFields); |
||
| 1456 | } |
||
| 1457 | |||
| 1458 | return $id; |
||
| 1459 | } |
||
| 1460 | } |
||
| 1461 | } |
||
| 1462 | |||
| 1463 | /** |
||
| 1464 | * Delete session |
||
| 1465 | * @author Carlos Vargas from existing code |
||
| 1466 | * @param array $id_checked an array to delete sessions |
||
| 1467 | * @param boolean $from_ws optional, true if the function is called |
||
| 1468 | * by a webservice, false otherwise. |
||
| 1469 | * @return void Nothing, or false on error |
||
| 1470 | * */ |
||
| 1471 | public static function delete($id_checked, $from_ws = false) |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * @param int $id_promotion |
||
| 1536 | * |
||
| 1537 | * @return bool |
||
| 1538 | */ |
||
| 1539 | View Code Duplication | public static function clear_session_ref_promotion($id_promotion) |
|
| 1551 | |||
| 1552 | /** |
||
| 1553 | * Subscribes students to the given session and optionally (default) unsubscribes previous users |
||
| 1554 | * |
||
| 1555 | * @author Carlos Vargas from existing code |
||
| 1556 | * @author Julio Montoya. Cleaning code. |
||
| 1557 | * @param int $id_session |
||
| 1558 | * @param array $user_list |
||
| 1559 | * @param int $session_visibility |
||
| 1560 | * @param bool $empty_users |
||
| 1561 | * @return bool |
||
| 1562 | */ |
||
| 1563 | public static function suscribe_users_to_session( |
||
| 1780 | |||
| 1781 | /** |
||
| 1782 | * Returns user list of the current users subscribed in the course-session |
||
| 1783 | * @param int $sessionId |
||
| 1784 | * @param array $courseInfo |
||
| 1785 | * @param int $status |
||
| 1786 | * |
||
| 1787 | * @return array |
||
| 1788 | */ |
||
| 1789 | public static function getUsersByCourseSession( |
||
| 1825 | |||
| 1826 | /** |
||
| 1827 | * Remove a list of users from a course-session |
||
| 1828 | * @param array $userList |
||
| 1829 | * @param int $sessionId |
||
| 1830 | * @param array $courseInfo |
||
| 1831 | * @param int $status |
||
| 1832 | * @param bool $updateTotal |
||
| 1833 | * @return bool |
||
| 1834 | */ |
||
| 1835 | public static function removeUsersFromCourseSession( |
||
| 1890 | |||
| 1891 | /** |
||
| 1892 | * Subscribe a user to an specific course inside a session. |
||
| 1893 | * |
||
| 1894 | * @param array $user_list |
||
| 1895 | * @param int $session_id |
||
| 1896 | * @param string $course_code |
||
| 1897 | * @param int $session_visibility |
||
| 1898 | * @param bool $removeUsersNotInList |
||
| 1899 | * @return bool |
||
| 1900 | */ |
||
| 1901 | public static function subscribe_users_to_session_course( |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Unsubscribe user from session |
||
| 2011 | * |
||
| 2012 | * @param int Session id |
||
| 2013 | * @param int User id |
||
| 2014 | * @return bool True in case of success, false in case of error |
||
| 2015 | */ |
||
| 2016 | public static function unsubscribe_user_from_session($session_id, $user_id) |
||
| 2072 | |||
| 2073 | /** |
||
| 2074 | * Subscribes courses to the given session and optionally (default) |
||
| 2075 | * unsubscribes previous users |
||
| 2076 | * @author Carlos Vargas from existing code |
||
| 2077 | * @param int $sessionId |
||
| 2078 | * @param array $courseList List of courses int ids |
||
| 2079 | * @param bool $removeExistingCoursesWithUsers Whether to unsubscribe |
||
| 2080 | * existing courses and users (true, default) or not (false) |
||
| 2081 | * @param $copyEvaluation from base course to session course |
||
| 2082 | * @return void Nothing, or false on error |
||
| 2083 | * */ |
||
| 2084 | public static function add_courses_to_session( |
||
| 2272 | |||
| 2273 | /** |
||
| 2274 | * Unsubscribe course from a session |
||
| 2275 | * |
||
| 2276 | * @param int Session id |
||
| 2277 | * @param int Course id |
||
| 2278 | * @return bool True in case of success, false otherwise |
||
| 2279 | */ |
||
| 2280 | public static function unsubscribe_course_from_session($session_id, $course_id) |
||
| 2327 | |||
| 2328 | /** |
||
| 2329 | * Creates a new extra field for a given session |
||
| 2330 | * @param string $variable Field's internal variable name |
||
| 2331 | * @param int $fieldType Field's type |
||
| 2332 | * @param string $displayText Field's language var name |
||
| 2333 | * @return int new extra field id |
||
| 2334 | */ |
||
| 2335 | View Code Duplication | public static function create_session_extra_field($variable, $fieldType, $displayText) |
|
| 2346 | |||
| 2347 | /** |
||
| 2348 | * Update an extra field value for a given session |
||
| 2349 | * @param integer Course ID |
||
| 2350 | * @param string Field variable name |
||
| 2351 | * @param string Field value |
||
| 2352 | * @return boolean true if field updated, false otherwise |
||
| 2353 | */ |
||
| 2354 | View Code Duplication | public static function update_session_extra_field_value($sessionId, $variable, $value = '') |
|
| 2364 | |||
| 2365 | /** |
||
| 2366 | * Checks the relationship between a session and a course. |
||
| 2367 | * @param int $session_id |
||
| 2368 | * @param int $courseId |
||
| 2369 | * @return bool Returns TRUE if the session and the course are related, FALSE otherwise. |
||
| 2370 | * */ |
||
| 2371 | View Code Duplication | public static function relation_session_course_exist($session_id, $courseId) |
|
| 2386 | |||
| 2387 | /** |
||
| 2388 | * Get the session information by name |
||
| 2389 | * @param string session name |
||
| 2390 | * @return mixed false if the session does not exist, array if the session exist |
||
| 2391 | * */ |
||
| 2392 | public static function get_session_by_name($session_name) |
||
| 2411 | |||
| 2412 | /** |
||
| 2413 | * Create a session category |
||
| 2414 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2415 | * @param string name |
||
| 2416 | * @param integer year_start |
||
| 2417 | * @param integer month_start |
||
| 2418 | * @param integer day_start |
||
| 2419 | * @param integer year_end |
||
| 2420 | * @param integer month_end |
||
| 2421 | * @param integer day_end |
||
| 2422 | * @return $id_session; |
||
| 2423 | * */ |
||
| 2424 | public static function create_category_session( |
||
| 2481 | |||
| 2482 | /** |
||
| 2483 | * Edit a sessions categories |
||
| 2484 | * @author Jhon Hinojosa <[email protected]>,from existing code |
||
| 2485 | * @param integer id |
||
| 2486 | * @param string name |
||
| 2487 | * @param integer year_start |
||
| 2488 | * @param integer month_start |
||
| 2489 | * @param integer day_start |
||
| 2490 | * @param integer year_end |
||
| 2491 | * @param integer month_end |
||
| 2492 | * @param integer day_end |
||
| 2493 | * @return $id; |
||
| 2494 | * The parameter id is a primary key |
||
| 2495 | * */ |
||
| 2496 | public static function edit_category_session( |
||
| 2550 | |||
| 2551 | /** |
||
| 2552 | * Delete sessions categories |
||
| 2553 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2554 | * @param array id_checked |
||
| 2555 | * @param bool include delete session |
||
| 2556 | * @param bool optional, true if the function is called by a webservice, false otherwise. |
||
| 2557 | * @return void Nothing, or false on error |
||
| 2558 | * The parameters is a array to delete sessions |
||
| 2559 | * */ |
||
| 2560 | public static function delete_session_category($id_checked, $delete_session = false, $from_ws = false) |
||
| 2602 | |||
| 2603 | /** |
||
| 2604 | * Get a list of sessions of which the given conditions match with an = 'cond' |
||
| 2605 | * @param array $conditions a list of condition example : |
||
| 2606 | * array('status' => STUDENT) or |
||
| 2607 | * array('s.name' => array('operator' => 'LIKE', value = '%$needle%')) |
||
| 2608 | * @param array $order_by a list of fields on which sort |
||
| 2609 | * @return array An array with all sessions of the platform. |
||
| 2610 | * @todo optional course code parameter, optional sorting parameters... |
||
| 2611 | */ |
||
| 2612 | public static function get_sessions_list($conditions = array(), $order_by = array(), $from = null, $to = null) |
||
| 2613 | { |
||
| 2614 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2615 | $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2616 | $user_table = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2617 | $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2618 | $session_course_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2619 | $course_table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 2620 | $access_url_id = api_get_current_access_url_id(); |
||
| 2621 | $return_array = array(); |
||
| 2622 | |||
| 2623 | $sql_query = " SELECT |
||
| 2624 | s.id, |
||
| 2625 | s.name, |
||
| 2626 | s.nbr_courses, |
||
| 2627 | s.access_start_date, |
||
| 2628 | s.access_end_date, |
||
| 2629 | u.firstname, |
||
| 2630 | u.lastname, |
||
| 2631 | sc.name as category_name, |
||
| 2632 | s.promotion_id |
||
| 2633 | FROM $session_table s |
||
| 2634 | INNER JOIN $user_table u ON s.id_coach = u.user_id |
||
| 2635 | INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id |
||
| 2636 | LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id |
||
| 2637 | LEFT JOIN $session_course_table sco ON (sco.session_id = s.id) |
||
| 2638 | INNER JOIN $course_table c ON sco.c_id = c.id |
||
| 2639 | WHERE ar.access_url_id = $access_url_id "; |
||
| 2640 | |||
| 2641 | $availableFields = array( |
||
| 2642 | 's.id', |
||
| 2643 | 's.name', |
||
| 2644 | 'c.id' |
||
| 2645 | ); |
||
| 2646 | |||
| 2647 | $availableOperator = array( |
||
| 2648 | 'like', |
||
| 2649 | '>=', |
||
| 2650 | '<=', |
||
| 2651 | '=', |
||
| 2652 | ); |
||
| 2653 | |||
| 2654 | if (count($conditions) > 0) { |
||
| 2655 | foreach ($conditions as $field => $options) { |
||
| 2656 | $operator = strtolower($options['operator']); |
||
| 2657 | $value = Database::escape_string($options['value']); |
||
| 2658 | $sql_query .= ' AND '; |
||
| 2659 | if (in_array($field, $availableFields) && in_array($operator, $availableOperator)) { |
||
| 2660 | $sql_query .= $field . " $operator '" . $value . "'"; |
||
| 2661 | } |
||
| 2662 | } |
||
| 2663 | } |
||
| 2664 | |||
| 2665 | $orderAvailableList = array('name'); |
||
| 2666 | |||
| 2667 | if (count($order_by) > 0) { |
||
| 2668 | $order = null; |
||
| 2669 | $direction = null; |
||
| 2670 | if (isset($order_by[0]) && in_array($order_by[0], $orderAvailableList)) { |
||
| 2671 | $order = $order_by[0]; |
||
| 2672 | } |
||
| 2673 | if (isset($order_by[1]) && in_array(strtolower($order_by[1]), array('desc', 'asc'))) { |
||
| 2674 | $direction = $order_by[1]; |
||
| 2675 | } |
||
| 2676 | |||
| 2677 | if (!empty($order)) { |
||
| 2678 | $sql_query .= " ORDER BY $order $direction "; |
||
| 2679 | } |
||
| 2680 | } |
||
| 2681 | |||
| 2682 | if (!is_null($from) && !is_null($to)) { |
||
| 2683 | $to = intval($to); |
||
| 2684 | $from = intval($from); |
||
| 2685 | $sql_query .= "LIMIT $from, $to"; |
||
| 2686 | } |
||
| 2687 | |||
| 2688 | $sql_result = Database::query($sql_query); |
||
| 2689 | if (Database::num_rows($sql_result) > 0) { |
||
| 2690 | while ($result = Database::fetch_array($sql_result)) { |
||
| 2691 | $return_array[$result['id']] = $result; |
||
| 2692 | } |
||
| 2693 | } |
||
| 2694 | |||
| 2695 | return $return_array; |
||
| 2696 | } |
||
| 2697 | |||
| 2698 | /** |
||
| 2699 | * Get the session category information by id |
||
| 2700 | * @param string session category ID |
||
| 2701 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2702 | */ |
||
| 2703 | View Code Duplication | public static function get_session_category($id) |
|
| 2704 | { |
||
| 2705 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2706 | $id = intval($id); |
||
| 2707 | $sql = "SELECT id, name, date_start, date_end |
||
| 2708 | FROM $tbl_session_category |
||
| 2709 | WHERE id= $id"; |
||
| 2710 | $result = Database::query($sql); |
||
| 2711 | $num = Database::num_rows($result); |
||
| 2712 | if ($num > 0) { |
||
| 2713 | return Database::fetch_array($result); |
||
| 2714 | } else { |
||
| 2715 | return false; |
||
| 2716 | } |
||
| 2717 | } |
||
| 2718 | |||
| 2719 | /** |
||
| 2720 | * Get all session categories (filter by access_url_id) |
||
| 2721 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2722 | */ |
||
| 2723 | public static function get_all_session_category() |
||
| 2724 | { |
||
| 2725 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2726 | $id = api_get_current_access_url_id(); |
||
| 2727 | $sql = 'SELECT * FROM ' . $tbl_session_category . ' |
||
| 2728 | WHERE access_url_id = ' . $id . ' |
||
| 2729 | ORDER BY name ASC'; |
||
| 2730 | $result = Database::query($sql); |
||
| 2731 | if (Database::num_rows($result) > 0) { |
||
| 2732 | $data = Database::store_result($result, 'ASSOC'); |
||
| 2733 | return $data; |
||
| 2734 | } else { |
||
| 2735 | return false; |
||
| 2736 | } |
||
| 2737 | } |
||
| 2738 | |||
| 2739 | /** |
||
| 2740 | * Assign a coach to course in session with status = 2 |
||
| 2741 | * @param int $user_id |
||
| 2742 | * @param int $session_id |
||
| 2743 | * @param int $courseId |
||
| 2744 | * @param bool $nocoach optional, if is true the user don't be a coach now, |
||
| 2745 | * otherwise it'll assign a coach |
||
| 2746 | * @return bool true if there are affected rows, otherwise false |
||
| 2747 | */ |
||
| 2748 | public static function set_coach_to_course_session( |
||
| 2749 | $user_id, |
||
| 2750 | $session_id = 0, |
||
| 2751 | $courseId = 0, |
||
| 2752 | $nocoach = false |
||
| 2753 | ) { |
||
| 2754 | // Definition of variables |
||
| 2755 | $user_id = intval($user_id); |
||
| 2756 | |||
| 2757 | if (!empty($session_id)) { |
||
| 2758 | $session_id = intval($session_id); |
||
| 2759 | } else { |
||
| 2760 | $session_id = api_get_session_id(); |
||
| 2761 | } |
||
| 2762 | |||
| 2763 | if (!empty($courseId)) { |
||
| 2764 | $courseId = intval($courseId); |
||
| 2765 | } else { |
||
| 2766 | $courseId = api_get_course_id(); |
||
| 2767 | } |
||
| 2768 | |||
| 2769 | // Table definition |
||
| 2770 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2771 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2772 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2773 | |||
| 2774 | // check if user is a teacher |
||
| 2775 | $sql = "SELECT * FROM $tbl_user |
||
| 2776 | WHERE status = 1 AND user_id = $user_id"; |
||
| 2777 | |||
| 2778 | $rs_check_user = Database::query($sql); |
||
| 2779 | |||
| 2780 | if (Database::num_rows($rs_check_user) > 0) { |
||
| 2781 | if ($nocoach) { |
||
| 2782 | // check if user_id exists in session_rel_user (if the user is |
||
| 2783 | // subscribed to the session in any manner) |
||
| 2784 | $sql = "SELECT user_id FROM $tbl_session_rel_user |
||
| 2785 | WHERE |
||
| 2786 | session_id = $session_id AND |
||
| 2787 | user_id = $user_id"; |
||
| 2788 | $res = Database::query($sql); |
||
| 2789 | |||
| 2790 | View Code Duplication | if (Database::num_rows($res) > 0) { |
|
| 2791 | // The user is already subscribed to the session. Change the |
||
| 2792 | // record so the user is NOT a coach for this course anymore |
||
| 2793 | // and then exit |
||
| 2794 | $sql = "UPDATE $tbl_session_rel_course_rel_user |
||
| 2795 | SET status = 0 |
||
| 2796 | WHERE |
||
| 2797 | session_id = $session_id AND |
||
| 2798 | c_id = $courseId AND |
||
| 2799 | user_id = $user_id "; |
||
| 2800 | $result = Database::query($sql); |
||
| 2801 | if (Database::affected_rows($result) > 0) |
||
| 2802 | return true; |
||
| 2803 | else |
||
| 2804 | return false; |
||
| 2805 | } else { |
||
| 2806 | // The user is not subscribed to the session, so make sure |
||
| 2807 | // he isn't subscribed to a course in this session either |
||
| 2808 | // and then exit |
||
| 2809 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2810 | WHERE |
||
| 2811 | session_id = $session_id AND |
||
| 2812 | c_id = $courseId AND |
||
| 2813 | user_id = $user_id "; |
||
| 2814 | $result = Database::query($sql); |
||
| 2815 | if (Database::affected_rows($result) > 0) |
||
| 2816 | return true; |
||
| 2817 | else |
||
| 2818 | return false; |
||
| 2819 | } |
||
| 2820 | } else { |
||
| 2821 | // Assign user as a coach to course |
||
| 2822 | // First check if the user is registered to the course |
||
| 2823 | $sql = "SELECT user_id FROM $tbl_session_rel_course_rel_user |
||
| 2824 | WHERE |
||
| 2825 | session_id = $session_id AND |
||
| 2826 | c_id = $courseId AND |
||
| 2827 | user_id = $user_id"; |
||
| 2828 | $rs_check = Database::query($sql); |
||
| 2829 | |||
| 2830 | // Then update or insert. |
||
| 2831 | View Code Duplication | if (Database::num_rows($rs_check) > 0) { |
|
| 2832 | $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 2 |
||
| 2833 | WHERE |
||
| 2834 | session_id = $session_id AND |
||
| 2835 | c_id = $courseId AND |
||
| 2836 | user_id = $user_id "; |
||
| 2837 | $result = Database::query($sql); |
||
| 2838 | if (Database::affected_rows($result) > 0) { |
||
| 2839 | return true; |
||
| 2840 | } else { |
||
| 2841 | return false; |
||
| 2842 | } |
||
| 2843 | } else { |
||
| 2844 | $sql = "INSERT INTO $tbl_session_rel_course_rel_user(session_id, c_id, user_id, status) |
||
| 2845 | VALUES($session_id, $courseId, $user_id, 2)"; |
||
| 2846 | $result = Database::query($sql); |
||
| 2847 | if (Database::affected_rows($result) > 0) { |
||
| 2848 | return true; |
||
| 2849 | } else { |
||
| 2850 | return false; |
||
| 2851 | } |
||
| 2852 | } |
||
| 2853 | } |
||
| 2854 | } else { |
||
| 2855 | return false; |
||
| 2856 | } |
||
| 2857 | } |
||
| 2858 | |||
| 2859 | /** |
||
| 2860 | * Subscribes sessions to human resource manager (Dashboard feature) |
||
| 2861 | * @param array $userInfo Human Resource Manager info |
||
| 2862 | * @param array $sessions_list Sessions id |
||
| 2863 | * @param bool $sendEmail |
||
| 2864 | * @param bool $removeOldConnections |
||
| 2865 | * @return int |
||
| 2866 | * */ |
||
| 2867 | public static function suscribe_sessions_to_hr_manager( |
||
| 2868 | $userInfo, |
||
| 2869 | $sessions_list, |
||
| 2870 | $sendEmail = false, |
||
| 2871 | $removeOldConnections = true |
||
| 2872 | ) { |
||
| 2873 | // Database Table Definitions |
||
| 2874 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2875 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2876 | |||
| 2877 | if (empty($userInfo)) { |
||
| 2878 | |||
| 2879 | return 0; |
||
| 2880 | } |
||
| 2881 | |||
| 2882 | $userId = $userInfo['user_id']; |
||
| 2883 | |||
| 2884 | // Only subscribe DRH users. |
||
| 2885 | $rolesAllowed = array( |
||
| 2886 | DRH, |
||
| 2887 | SESSIONADMIN, |
||
| 2888 | PLATFORM_ADMIN, |
||
| 2889 | COURSE_TUTOR |
||
| 2890 | ); |
||
| 2891 | $isAdmin = api_is_platform_admin_by_id($userInfo['user_id']); |
||
| 2892 | if (!$isAdmin && !in_array($userInfo['status'], $rolesAllowed)) { |
||
| 2893 | |||
| 2894 | return 0; |
||
| 2895 | } |
||
| 2896 | |||
| 2897 | $affected_rows = 0; |
||
| 2898 | |||
| 2899 | // Deleting assigned sessions to hrm_id. |
||
| 2900 | if ($removeOldConnections) { |
||
| 2901 | if (api_is_multiple_url_enabled()) { |
||
| 2902 | $sql = "SELECT session_id |
||
| 2903 | FROM $tbl_session_rel_user s |
||
| 2904 | INNER JOIN $tbl_session_rel_access_url a ON (a.session_id = s.session_id) |
||
| 2905 | WHERE |
||
| 2906 | s.user_id = $userId AND |
||
| 2907 | relation_type=" . SESSION_RELATION_TYPE_RRHH . " AND |
||
| 2908 | access_url_id = " . api_get_current_access_url_id() . ""; |
||
| 2909 | } else { |
||
| 2910 | $sql = "SELECT session_id FROM $tbl_session_rel_user s |
||
| 2911 | WHERE user_id = $userId AND relation_type=" . SESSION_RELATION_TYPE_RRHH . ""; |
||
| 2912 | } |
||
| 2913 | $result = Database::query($sql); |
||
| 2914 | |||
| 2915 | View Code Duplication | if (Database::num_rows($result) > 0) { |
|
| 2916 | while ($row = Database::fetch_array($result)) { |
||
| 2917 | $sql = "DELETE FROM $tbl_session_rel_user |
||
| 2918 | WHERE |
||
| 2919 | session_id = {$row['session_id']} AND |
||
| 2920 | user_id = $userId AND |
||
| 2921 | relation_type=" . SESSION_RELATION_TYPE_RRHH . " "; |
||
| 2922 | Database::query($sql); |
||
| 2923 | } |
||
| 2924 | } |
||
| 2925 | } |
||
| 2926 | // Inserting new sessions list. |
||
| 2927 | if (!empty($sessions_list) && is_array($sessions_list)) { |
||
| 2928 | |||
| 2929 | foreach ($sessions_list as $session_id) { |
||
| 2930 | $session_id = intval($session_id); |
||
| 2931 | $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, relation_type, registered_at) |
||
| 2932 | VALUES ( |
||
| 2933 | $session_id, |
||
| 2934 | $userId, |
||
| 2935 | '" . SESSION_RELATION_TYPE_RRHH . "', |
||
| 2936 | '" . api_get_utc_datetime() . "' |
||
| 2937 | )"; |
||
| 2938 | |||
| 2939 | Database::query($sql); |
||
| 2940 | $affected_rows++; |
||
| 2941 | } |
||
| 2942 | } |
||
| 2943 | |||
| 2944 | return $affected_rows; |
||
| 2945 | } |
||
| 2946 | |||
| 2947 | /** |
||
| 2948 | * @param int $sessionId |
||
| 2949 | * @return array |
||
| 2950 | */ |
||
| 2951 | public static function getDrhUsersInSession($sessionId) |
||
| 2952 | { |
||
| 2953 | return self::get_users_by_session($sessionId, SESSION_RELATION_TYPE_RRHH); |
||
| 2954 | } |
||
| 2955 | |||
| 2956 | /** |
||
| 2957 | * @param int $userId |
||
| 2958 | * @param int $sessionId |
||
| 2959 | * @return array |
||
| 2960 | */ |
||
| 2961 | public static function getSessionFollowedByDrh($userId, $sessionId) |
||
| 2962 | { |
||
| 2963 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2964 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2965 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2966 | |||
| 2967 | $userId = intval($userId); |
||
| 2968 | $sessionId = intval($sessionId); |
||
| 2969 | |||
| 2970 | $select = " SELECT * "; |
||
| 2971 | if (api_is_multiple_url_enabled()) { |
||
| 2972 | $sql = " $select FROM $tbl_session s |
||
| 2973 | INNER JOIN $tbl_session_rel_user sru ON (sru.session_id = s.id) |
||
| 2974 | LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id) |
||
| 2975 | WHERE |
||
| 2976 | sru.user_id = '$userId' AND |
||
| 2977 | sru.session_id = '$sessionId' AND |
||
| 2978 | sru.relation_type = '" . SESSION_RELATION_TYPE_RRHH . "' AND |
||
| 2979 | access_url_id = " . api_get_current_access_url_id() . " |
||
| 2980 | "; |
||
| 2981 | } else { |
||
| 2982 | $sql = "$select FROM $tbl_session s |
||
| 2983 | INNER JOIN $tbl_session_rel_user sru |
||
| 2984 | ON |
||
| 2985 | sru.session_id = s.id AND |
||
| 2986 | sru.user_id = '$userId' AND |
||
| 2987 | sru.session_id = '$sessionId' AND |
||
| 2988 | sru.relation_type = '" . SESSION_RELATION_TYPE_RRHH . "' |
||
| 2989 | "; |
||
| 2990 | } |
||
| 2991 | |||
| 2992 | $result = Database::query($sql); |
||
| 2993 | if (Database::num_rows($result)) { |
||
| 2994 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 2995 | $row['course_list'] = self::get_course_list_by_session_id($sessionId); |
||
| 2996 | |||
| 2997 | return $row; |
||
| 2998 | } |
||
| 2999 | |||
| 3000 | return array(); |
||
| 3001 | } |
||
| 3002 | |||
| 3003 | /** |
||
| 3004 | * Get sessions followed by human resources manager |
||
| 3005 | * @param int $userId |
||
| 3006 | * @param int $start |
||
| 3007 | * @param int $limit |
||
| 3008 | * @param bool $getCount |
||
| 3009 | * @param bool $getOnlySessionId |
||
| 3010 | * @param bool $getSql |
||
| 3011 | * @param string $orderCondition |
||
| 3012 | * @param string $description |
||
| 3013 | * |
||
| 3014 | * @return array sessions |
||
| 3015 | */ |
||
| 3016 | public static function get_sessions_followed_by_drh( |
||
| 3017 | $userId, |
||
| 3018 | $start = null, |
||
| 3019 | $limit = null, |
||
| 3020 | $getCount = false, |
||
| 3021 | $getOnlySessionId = false, |
||
| 3022 | $getSql = false, |
||
| 3023 | $orderCondition = null, |
||
| 3024 | $keyword = '', |
||
| 3025 | $description = '' |
||
| 3026 | ) { |
||
| 3027 | return self::getSessionsFollowedByUser( |
||
| 3028 | $userId, |
||
| 3029 | DRH, |
||
| 3030 | $start, |
||
| 3031 | $limit, |
||
| 3032 | $getCount, |
||
| 3033 | $getOnlySessionId, |
||
| 3034 | $getSql, |
||
| 3035 | $orderCondition, |
||
| 3036 | $keyword, |
||
| 3037 | $description |
||
| 3038 | ); |
||
| 3039 | } |
||
| 3040 | |||
| 3041 | /** |
||
| 3042 | * Get sessions followed by human resources manager |
||
| 3043 | * @param int $userId |
||
| 3044 | * @param int $start |
||
| 3045 | * @param int $limit |
||
| 3046 | * @param bool $getCount |
||
| 3047 | * @param bool $getOnlySessionId |
||
| 3048 | * @param bool $getSql |
||
| 3049 | * @param string $orderCondition |
||
| 3050 | * @param string $keyword |
||
| 3051 | * @param string $description |
||
| 3052 | * @return array sessions |
||
| 3053 | */ |
||
| 3054 | public static function getSessionsFollowedByUser( |
||
| 3055 | $userId, |
||
| 3056 | $status = null, |
||
| 3057 | $start = null, |
||
| 3058 | $limit = null, |
||
| 3059 | $getCount = false, |
||
| 3060 | $getOnlySessionId = false, |
||
| 3061 | $getSql = false, |
||
| 3062 | $orderCondition = null, |
||
| 3063 | $keyword = '', |
||
| 3064 | $description = '' |
||
| 3065 | ) { |
||
| 3066 | // Database Table Definitions |
||
| 3067 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3068 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3069 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3070 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3071 | |||
| 3072 | $userId = intval($userId); |
||
| 3073 | |||
| 3074 | $select = " SELECT DISTINCT * "; |
||
| 3075 | |||
| 3076 | if ($getCount) { |
||
| 3077 | $select = " SELECT count(DISTINCT(s.id)) as count "; |
||
| 3078 | } |
||
| 3079 | |||
| 3080 | if ($getOnlySessionId) { |
||
| 3081 | $select = " SELECT DISTINCT(s.id) "; |
||
| 3082 | } |
||
| 3083 | |||
| 3084 | $limitCondition = null; |
||
| 3085 | View Code Duplication | if (!empty($start) && !empty($limit)) { |
|
| 3086 | $limitCondition = " LIMIT " . intval($start) . ", " . intval($limit); |
||
| 3087 | } |
||
| 3088 | |||
| 3089 | if (empty($orderCondition)) { |
||
| 3090 | $orderCondition = " ORDER BY s.name "; |
||
| 3091 | } |
||
| 3092 | |||
| 3093 | $whereConditions = null; |
||
| 3094 | $sessionCourseConditions = null; |
||
| 3095 | $sessionConditions = null; |
||
| 3096 | $sessionQuery = null; |
||
| 3097 | $courseSessionQuery = null; |
||
| 3098 | |||
| 3099 | switch ($status) { |
||
| 3100 | case DRH: |
||
| 3101 | $sessionQuery = "SELECT sru.session_id |
||
| 3102 | FROM |
||
| 3103 | $tbl_session_rel_user sru |
||
| 3104 | WHERE |
||
| 3105 | sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND |
||
| 3106 | sru.user_id = $userId"; |
||
| 3107 | break; |
||
| 3108 | case COURSEMANAGER: |
||
| 3109 | $courseSessionQuery = " |
||
| 3110 | SELECT scu.session_id as id |
||
| 3111 | FROM $tbl_session_rel_course_rel_user scu |
||
| 3112 | WHERE (scu.status = 2 AND scu.user_id = $userId)"; |
||
| 3113 | |||
| 3114 | $whereConditions = " OR (s.id_coach = $userId) "; |
||
| 3115 | break; |
||
| 3116 | default: |
||
| 3117 | $sessionQuery = "SELECT sru.session_id |
||
| 3118 | FROM |
||
| 3119 | $tbl_session_rel_user sru |
||
| 3120 | WHERE |
||
| 3121 | sru.user_id = $userId"; |
||
| 3122 | break; |
||
| 3123 | } |
||
| 3124 | |||
| 3125 | $keywordCondition = ''; |
||
| 3126 | View Code Duplication | if (!empty($keyword)) { |
|
| 3127 | $keyword = Database::escape_string($keyword); |
||
| 3128 | $keywordCondition = " AND (s.name LIKE '%$keyword%' ) "; |
||
| 3129 | |||
| 3130 | if (!empty($description)) { |
||
| 3131 | $description = Database::escape_string($description); |
||
| 3132 | $keywordCondition = " AND (s.name LIKE '%$keyword%' OR s.description LIKE '%$description%' ) "; |
||
| 3133 | } |
||
| 3134 | } |
||
| 3135 | |||
| 3136 | $whereConditions .= $keywordCondition; |
||
| 3137 | |||
| 3138 | $subQuery = $sessionQuery.$courseSessionQuery; |
||
| 3139 | |||
| 3140 | $sql = " $select FROM $tbl_session s |
||
| 3141 | INNER JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id) |
||
| 3142 | WHERE |
||
| 3143 | access_url_id = ".api_get_current_access_url_id()." AND |
||
| 3144 | s.id IN ( |
||
| 3145 | $subQuery |
||
| 3146 | ) |
||
| 3147 | $whereConditions |
||
| 3148 | $orderCondition |
||
| 3149 | $limitCondition"; |
||
| 3150 | |||
| 3151 | if ($getSql) { |
||
| 3152 | return $sql; |
||
| 3153 | } |
||
| 3154 | |||
| 3155 | $result = Database::query($sql); |
||
| 3156 | |||
| 3157 | if ($getCount) { |
||
| 3158 | $row = Database::fetch_array($result); |
||
| 3159 | return $row['count']; |
||
| 3160 | } |
||
| 3161 | |||
| 3162 | $sessions = array(); |
||
| 3163 | if (Database::num_rows($result) > 0) { |
||
| 3164 | $sysUploadPath = api_get_path(SYS_UPLOAD_PATH). 'sessions/'; |
||
| 3165 | $webUploadPath = api_get_path(WEB_UPLOAD_PATH). 'sessions/'; |
||
| 3166 | $imgPath = Display::returnIconPath('session_default_small.png'); |
||
| 3167 | |||
| 3168 | $tableExtraFields = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 3169 | $sql = "SELECT id FROM " . $tableExtraFields . " |
||
| 3170 | WHERE extra_field_type = 3 AND variable='image'"; |
||
| 3171 | $resultField = Database::query($sql); |
||
| 3172 | $imageFieldId = Database::fetch_assoc($resultField); |
||
| 3173 | |||
| 3174 | while ($row = Database::fetch_array($result)) { |
||
| 3175 | |||
| 3176 | $row['image'] = null; |
||
| 3177 | $sessionImage = $sysUploadPath . $imageFieldId['id'] . '_' . $row['id'] . '.png'; |
||
| 3178 | |||
| 3179 | if (is_file($sessionImage)) { |
||
| 3180 | $sessionImage = $webUploadPath . $imageFieldId['id'] . '_' . $row['id'] . '.png'; |
||
| 3181 | $row['image'] = $sessionImage; |
||
| 3182 | } else { |
||
| 3183 | $row['image'] = $imgPath; |
||
| 3184 | } |
||
| 3185 | |||
| 3186 | $sessions[$row['id']] = $row; |
||
| 3187 | |||
| 3188 | } |
||
| 3189 | } |
||
| 3190 | |||
| 3191 | return $sessions; |
||
| 3192 | } |
||
| 3193 | |||
| 3194 | /** |
||
| 3195 | * Gets the list (or the count) of courses by session filtered by access_url |
||
| 3196 | * @param int $session_id The session id |
||
| 3197 | * @param string $course_name The course code |
||
| 3198 | * @param string $orderBy Field to order the data |
||
| 3199 | * @param boolean $getCount Optional. Count the session courses |
||
| 3200 | * @return array|int List of courses. Whether $getCount is true, return the count |
||
| 3201 | */ |
||
| 3202 | public static function get_course_list_by_session_id( |
||
| 3203 | $session_id, |
||
| 3204 | $course_name = '', |
||
| 3205 | $orderBy = null, |
||
| 3206 | $getCount = false |
||
| 3207 | ) { |
||
| 3208 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3209 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3210 | |||
| 3211 | $session_id = intval($session_id); |
||
| 3212 | |||
| 3213 | $sqlSelect = "SELECT *"; |
||
| 3214 | |||
| 3215 | if ($getCount) { |
||
| 3216 | $sqlSelect = "SELECT COUNT(1)"; |
||
| 3217 | } |
||
| 3218 | |||
| 3219 | // select the courses |
||
| 3220 | $sql = "SELECT *, c.id, c.id as real_id |
||
| 3221 | FROM $tbl_course c |
||
| 3222 | INNER JOIN $tbl_session_rel_course src |
||
| 3223 | ON c.id = src.c_id |
||
| 3224 | WHERE src.session_id = '$session_id' "; |
||
| 3225 | |||
| 3226 | if (!empty($course_name)) { |
||
| 3227 | $course_name = Database::escape_string($course_name); |
||
| 3228 | $sql .= " AND c.title LIKE '%$course_name%' "; |
||
| 3229 | } |
||
| 3230 | |||
| 3231 | if (!empty($orderBy)) { |
||
| 3232 | $orderBy = Database::escape_string($orderBy); |
||
| 3233 | $orderBy = " ORDER BY $orderBy"; |
||
| 3234 | } else { |
||
| 3235 | if (SessionManager::orderCourseIsEnabled()) { |
||
| 3236 | $orderBy .= " ORDER BY position "; |
||
| 3237 | } else { |
||
| 3238 | $orderBy .= " ORDER BY title "; |
||
| 3239 | } |
||
| 3240 | } |
||
| 3241 | |||
| 3242 | $sql .= Database::escape_string($orderBy); |
||
| 3243 | $result = Database::query($sql); |
||
| 3244 | $num_rows = Database::num_rows($result); |
||
| 3245 | $courses = array(); |
||
| 3246 | View Code Duplication | if ($num_rows > 0) { |
|
| 3247 | if ($getCount) { |
||
| 3248 | $count = Database::fetch_array($result); |
||
| 3249 | |||
| 3250 | return intval($count[0]); |
||
| 3251 | } |
||
| 3252 | |||
| 3253 | while ($row = Database::fetch_array($result,'ASSOC')) { |
||
| 3254 | $courses[$row['real_id']] = $row; |
||
| 3255 | } |
||
| 3256 | } |
||
| 3257 | |||
| 3258 | return $courses; |
||
| 3259 | } |
||
| 3260 | |||
| 3261 | /** |
||
| 3262 | * Gets the list of courses by session filtered by access_url |
||
| 3263 | * |
||
| 3264 | * @param $userId |
||
| 3265 | * @param $sessionId |
||
| 3266 | * @param null $from |
||
| 3267 | * @param null $limit |
||
| 3268 | * @param null $column |
||
| 3269 | * @param null $direction |
||
| 3270 | * @param bool $getCount |
||
| 3271 | * @return array |
||
| 3272 | */ |
||
| 3273 | public static function getAllCoursesFollowedByUser( |
||
| 3274 | $userId, |
||
| 3275 | $sessionId, |
||
| 3276 | $from = null, |
||
| 3277 | $limit = null, |
||
| 3278 | $column = null, |
||
| 3279 | $direction = null, |
||
| 3280 | $getCount = false, |
||
| 3281 | $keyword = null |
||
| 3282 | ) { |
||
| 3283 | if (empty($sessionId)) { |
||
| 3284 | $sessionsSQL = SessionManager::get_sessions_followed_by_drh( |
||
| 3285 | $userId, |
||
| 3286 | null, |
||
| 3287 | null, |
||
| 3288 | null, |
||
| 3289 | true, |
||
| 3290 | true |
||
| 3291 | ); |
||
| 3292 | } else { |
||
| 3293 | $sessionsSQL = intval($sessionId); |
||
| 3294 | } |
||
| 3295 | |||
| 3296 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3297 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3298 | |||
| 3299 | if ($getCount) { |
||
| 3300 | $select = "SELECT COUNT(DISTINCT(c.code)) as count "; |
||
| 3301 | } else { |
||
| 3302 | $select = "SELECT DISTINCT c.* "; |
||
| 3303 | } |
||
| 3304 | |||
| 3305 | $keywordCondition = null; |
||
| 3306 | if (!empty($keyword)) { |
||
| 3307 | $keyword = Database::escape_string($keyword); |
||
| 3308 | $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) "; |
||
| 3309 | } |
||
| 3310 | |||
| 3311 | // Select the courses |
||
| 3312 | $sql = "$select |
||
| 3313 | FROM $tbl_course c |
||
| 3314 | INNER JOIN $tbl_session_rel_course src |
||
| 3315 | ON c.id = src.c_id |
||
| 3316 | WHERE |
||
| 3317 | src.session_id IN ($sessionsSQL) |
||
| 3318 | $keywordCondition |
||
| 3319 | "; |
||
| 3320 | if ($getCount) { |
||
| 3321 | $result = Database::query($sql); |
||
| 3322 | $row = Database::fetch_array($result,'ASSOC'); |
||
| 3323 | return $row['count']; |
||
| 3324 | } |
||
| 3325 | |||
| 3326 | View Code Duplication | if (isset($from) && isset($limit)) { |
|
| 3327 | $from = intval($from); |
||
| 3328 | $limit = intval($limit); |
||
| 3329 | $sql .= " LIMIT $from, $limit"; |
||
| 3330 | } |
||
| 3331 | |||
| 3332 | $result = Database::query($sql); |
||
| 3333 | $num_rows = Database::num_rows($result); |
||
| 3334 | $courses = array(); |
||
| 3335 | |||
| 3336 | if ($num_rows > 0) { |
||
| 3337 | while ($row = Database::fetch_array($result,'ASSOC')) { |
||
| 3338 | $courses[$row['id']] = $row; |
||
| 3339 | } |
||
| 3340 | } |
||
| 3341 | |||
| 3342 | return $courses; |
||
| 3343 | } |
||
| 3344 | |||
| 3345 | /** |
||
| 3346 | * Gets the list of courses by session filtered by access_url |
||
| 3347 | * @param int $session_id |
||
| 3348 | * @param string $course_name |
||
| 3349 | * @return array list of courses |
||
| 3350 | */ |
||
| 3351 | public static function get_course_list_by_session_id_like($session_id, $course_name = '') |
||
| 3352 | { |
||
| 3353 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3354 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3355 | |||
| 3356 | $session_id = intval($session_id); |
||
| 3357 | $course_name = Database::escape_string($course_name); |
||
| 3358 | |||
| 3359 | // select the courses |
||
| 3360 | $sql = "SELECT c.id, c.title FROM $tbl_course c |
||
| 3361 | INNER JOIN $tbl_session_rel_course src |
||
| 3362 | ON c.id = src.c_id |
||
| 3363 | WHERE "; |
||
| 3364 | |||
| 3365 | if (!empty($session_id)) { |
||
| 3366 | $sql .= "src.session_id LIKE '$session_id' AND "; |
||
| 3367 | } |
||
| 3368 | |||
| 3369 | if (!empty($course_name)) { |
||
| 3370 | $sql .= "UPPER(c.title) LIKE UPPER('%$course_name%') "; |
||
| 3371 | } |
||
| 3372 | |||
| 3373 | $sql .= "ORDER BY title;"; |
||
| 3374 | $result = Database::query($sql); |
||
| 3375 | $num_rows = Database::num_rows($result); |
||
| 3376 | $courses = array(); |
||
| 3377 | if ($num_rows > 0) { |
||
| 3378 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 3379 | $courses[$row['id']] = $row; |
||
| 3380 | } |
||
| 3381 | } |
||
| 3382 | |||
| 3383 | return $courses; |
||
| 3384 | } |
||
| 3385 | |||
| 3386 | |||
| 3387 | /** |
||
| 3388 | * Gets the count of courses by session filtered by access_url |
||
| 3389 | * @param int session id |
||
| 3390 | * @return array list of courses |
||
| 3391 | */ |
||
| 3392 | public static function getCourseCountBySessionId($session_id, $keyword = null) |
||
| 3393 | { |
||
| 3394 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3395 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3396 | $session_id = intval($session_id); |
||
| 3397 | |||
| 3398 | // select the courses |
||
| 3399 | $sql = "SELECT COUNT(c.code) count |
||
| 3400 | FROM $tbl_course c |
||
| 3401 | INNER JOIN $tbl_session_rel_course src |
||
| 3402 | ON c.id = src.c_id |
||
| 3403 | WHERE src.session_id = '$session_id' "; |
||
| 3404 | |||
| 3405 | $keywordCondition = null; |
||
| 3406 | if (!empty($keyword)) { |
||
| 3407 | $keyword = Database::escape_string($keyword); |
||
| 3408 | $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) "; |
||
| 3409 | } |
||
| 3410 | $sql .= $keywordCondition; |
||
| 3411 | |||
| 3412 | $result = Database::query($sql); |
||
| 3413 | $num_rows = Database::num_rows($result); |
||
| 3414 | if ($num_rows > 0) { |
||
| 3415 | $row = Database::fetch_array($result,'ASSOC'); |
||
| 3416 | return $row['count']; |
||
| 3417 | } |
||
| 3418 | |||
| 3419 | return null; |
||
| 3420 | } |
||
| 3421 | |||
| 3422 | /** |
||
| 3423 | * Get the session id based on the original id and field name in the extra fields. |
||
| 3424 | * Returns 0 if session was not found |
||
| 3425 | * |
||
| 3426 | * @param string $value Original session id |
||
| 3427 | * @param string $variable Original field name |
||
| 3428 | * @return int Session id |
||
| 3429 | */ |
||
| 3430 | public static function getSessionIdFromOriginalId($value, $variable) |
||
| 3431 | { |
||
| 3432 | $extraFieldValue = new ExtraFieldValue('session'); |
||
| 3433 | $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 3434 | $variable, |
||
| 3435 | $value |
||
| 3436 | ); |
||
| 3437 | |||
| 3438 | if (!empty($result)) { |
||
| 3439 | return $result['item_id']; |
||
| 3440 | } |
||
| 3441 | |||
| 3442 | return 0; |
||
| 3443 | } |
||
| 3444 | |||
| 3445 | /** |
||
| 3446 | * Get users by session |
||
| 3447 | * @param int $id session id |
||
| 3448 | * @param int $status filter by status coach = 2 |
||
| 3449 | * @return array a list with an user list |
||
| 3450 | */ |
||
| 3451 | public static function get_users_by_session($id, $status = null) |
||
| 3452 | { |
||
| 3453 | if (empty($id)) { |
||
| 3454 | return array(); |
||
| 3455 | } |
||
| 3456 | $id = intval($id); |
||
| 3457 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3458 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3459 | $table_access_url_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 3460 | |||
| 3461 | $sql = "SELECT |
||
| 3462 | u.user_id, |
||
| 3463 | lastname, |
||
| 3464 | firstname, |
||
| 3465 | username, |
||
| 3466 | relation_type, |
||
| 3467 | access_url_id |
||
| 3468 | FROM $tbl_user u |
||
| 3469 | INNER JOIN $tbl_session_rel_user |
||
| 3470 | ON u.user_id = $tbl_session_rel_user.user_id AND |
||
| 3471 | $tbl_session_rel_user.session_id = $id |
||
| 3472 | LEFT OUTER JOIN $table_access_url_user uu |
||
| 3473 | ON (uu.user_id = u.user_id) |
||
| 3474 | "; |
||
| 3475 | |||
| 3476 | $urlId = api_get_current_access_url_id(); |
||
| 3477 | if (isset($status) && $status != '') { |
||
| 3478 | $status = intval($status); |
||
| 3479 | $sql .= " WHERE relation_type = $status AND (access_url_id = $urlId OR access_url_id is null )"; |
||
| 3480 | } else { |
||
| 3481 | $sql .= " WHERE (access_url_id = $urlId OR access_url_id is null )"; |
||
| 3482 | } |
||
| 3483 | |||
| 3484 | $sql .= " ORDER BY relation_type, "; |
||
| 3485 | $sql .= api_sort_by_first_name() ? ' firstname, lastname' : ' lastname, firstname'; |
||
| 3486 | |||
| 3487 | $result = Database::query($sql); |
||
| 3488 | |||
| 3489 | $return = array(); |
||
| 3490 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 3491 | $return[] = $row; |
||
| 3492 | } |
||
| 3493 | |||
| 3494 | return $return; |
||
| 3495 | } |
||
| 3496 | |||
| 3497 | /** |
||
| 3498 | * The general coach (field: session.id_coach) |
||
| 3499 | * @param int $user_id user id |
||
| 3500 | * @param boolean $asPlatformAdmin The user is platform admin, return everything |
||
| 3501 | * @return array |
||
| 3502 | */ |
||
| 3503 | public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false) |
||
| 3504 | { |
||
| 3505 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3506 | $user_id = intval($user_id); |
||
| 3507 | |||
| 3508 | // Session where we are general coach |
||
| 3509 | $sql = "SELECT DISTINCT * |
||
| 3510 | FROM $session_table"; |
||
| 3511 | |||
| 3512 | if (!$asPlatformAdmin) { |
||
| 3513 | $sql .= " WHERE id_coach = $user_id"; |
||
| 3514 | } |
||
| 3515 | |||
| 3516 | if (api_is_multiple_url_enabled()) { |
||
| 3517 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3518 | $access_url_id = api_get_current_access_url_id(); |
||
| 3519 | |||
| 3520 | $sqlCoach = ''; |
||
| 3521 | if (!$asPlatformAdmin) { |
||
| 3522 | $sqlCoach = " id_coach = $user_id AND "; |
||
| 3523 | } |
||
| 3524 | |||
| 3525 | if ($access_url_id != -1) { |
||
| 3526 | $sql = 'SELECT DISTINCT session.* |
||
| 3527 | FROM ' . $session_table . ' session INNER JOIN ' . $tbl_session_rel_access_url . ' session_rel_url |
||
| 3528 | ON (session.id = session_rel_url.session_id) |
||
| 3529 | WHERE '.$sqlCoach.' access_url_id = ' . $access_url_id; |
||
| 3530 | } |
||
| 3531 | } |
||
| 3532 | $sql .= ' ORDER by name'; |
||
| 3533 | $result = Database::query($sql); |
||
| 3534 | |||
| 3535 | return Database::store_result($result, 'ASSOC'); |
||
| 3536 | } |
||
| 3537 | |||
| 3538 | /** |
||
| 3539 | * @param int $user_id |
||
| 3540 | * @return array |
||
| 3541 | * @deprecated use get_sessions_by_general_coach() |
||
| 3542 | */ |
||
| 3543 | public static function get_sessions_by_coach($user_id) |
||
| 3544 | { |
||
| 3545 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3546 | return Database::select('*', $session_table, array('where' => array('id_coach = ?' => $user_id))); |
||
| 3547 | } |
||
| 3548 | |||
| 3549 | /** |
||
| 3550 | * @param int $user_id |
||
| 3551 | * @param int $courseId |
||
| 3552 | * @param int $session_id |
||
| 3553 | * @return array|bool |
||
| 3554 | */ |
||
| 3555 | View Code Duplication | public static function get_user_status_in_course_session($user_id, $courseId, $session_id) |
|
| 3556 | { |
||
| 3557 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3558 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3559 | $sql = "SELECT session_rcru.status |
||
| 3560 | FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user |
||
| 3561 | WHERE |
||
| 3562 | session_rcru.user_id = user.user_id AND |
||
| 3563 | session_rcru.session_id = '" . intval($session_id) . "' AND |
||
| 3564 | session_rcru.c_id ='" . intval($courseId) . "' AND |
||
| 3565 | user.user_id = " . intval($user_id); |
||
| 3566 | |||
| 3567 | $result = Database::query($sql); |
||
| 3568 | $status = false; |
||
| 3569 | if (Database::num_rows($result)) { |
||
| 3570 | $status = Database::fetch_row($result); |
||
| 3571 | $status = $status['0']; |
||
| 3572 | } |
||
| 3573 | |||
| 3574 | return $status; |
||
| 3575 | } |
||
| 3576 | |||
| 3577 | /** |
||
| 3578 | * Gets user status within a session |
||
| 3579 | * @param int $user_id |
||
| 3580 | * @param int $courseId |
||
| 3581 | * @param $session_id |
||
| 3582 | * @return int |
||
| 3583 | * @assert (null,null,null) === false |
||
| 3584 | */ |
||
| 3585 | public static function get_user_status_in_session($user_id, $courseId, $session_id) |
||
| 3586 | { |
||
| 3587 | if (empty($user_id) or empty($courseId) or empty($session_id)) { |
||
| 3588 | return false; |
||
| 3589 | } |
||
| 3590 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3591 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3592 | $sql = "SELECT session_rcru.status |
||
| 3593 | FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user |
||
| 3594 | WHERE session_rcru.user_id = user.user_id AND |
||
| 3595 | session_rcru.session_id = '" . intval($session_id) . "' AND |
||
| 3596 | session_rcru.c_id ='" . intval($courseId) . "' AND |
||
| 3597 | user.user_id = " . intval($user_id); |
||
| 3598 | $result = Database::query($sql); |
||
| 3599 | $status = false; |
||
| 3600 | if (Database::num_rows($result)) { |
||
| 3601 | $status = Database::fetch_row($result); |
||
| 3602 | $status = $status['0']; |
||
| 3603 | } |
||
| 3604 | return $status; |
||
| 3605 | } |
||
| 3606 | |||
| 3607 | /** |
||
| 3608 | * @param int $id |
||
| 3609 | * @return array |
||
| 3610 | */ |
||
| 3611 | public static function get_all_sessions_by_promotion($id) |
||
| 3612 | { |
||
| 3613 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3614 | return Database::select('*', $t, array('where' => array('promotion_id = ?' => $id))); |
||
| 3615 | } |
||
| 3616 | |||
| 3617 | /** |
||
| 3618 | * @param int $promotion_id |
||
| 3619 | * @param array $list |
||
| 3620 | */ |
||
| 3621 | public static function suscribe_sessions_to_promotion($promotion_id, $list) |
||
| 3622 | { |
||
| 3623 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3624 | $params = array(); |
||
| 3625 | $params['promotion_id'] = 0; |
||
| 3626 | Database::update($t, $params, array('promotion_id = ?' => $promotion_id)); |
||
| 3627 | |||
| 3628 | $params['promotion_id'] = $promotion_id; |
||
| 3629 | if (!empty($list)) { |
||
| 3630 | foreach ($list as $session_id) { |
||
| 3631 | $session_id = intval($session_id); |
||
| 3632 | Database::update($t, $params, array('id = ?' => $session_id)); |
||
| 3633 | } |
||
| 3634 | } |
||
| 3635 | } |
||
| 3636 | |||
| 3637 | /** |
||
| 3638 | * Updates a session status |
||
| 3639 | * @param int session id |
||
| 3640 | * @param int status |
||
| 3641 | */ |
||
| 3642 | public static function set_session_status($session_id, $status) |
||
| 3643 | { |
||
| 3644 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3645 | $params['visibility'] = $status; |
||
| 3646 | Database::update($t, $params, array('id = ?' => $session_id)); |
||
| 3647 | } |
||
| 3648 | |||
| 3649 | /** |
||
| 3650 | * Copies a session with the same data to a new session. |
||
| 3651 | * The new copy is not assigned to the same promotion. @see suscribe_sessions_to_promotions() for that |
||
| 3652 | * @param int Session ID |
||
| 3653 | * @param bool Whether to copy the relationship with courses |
||
| 3654 | * @param bool Whether to copy the relationship with users |
||
| 3655 | * @param bool New courses will be created |
||
| 3656 | * @param bool Whether to set exercises and learning paths in the new session to invisible by default |
||
| 3657 | * @return int The new session ID on success, 0 otherwise |
||
| 3658 | * @todo make sure the extra session fields are copied too |
||
| 3659 | */ |
||
| 3660 | public static function copy( |
||
| 3661 | $id, |
||
| 3662 | $copy_courses = true, |
||
| 3663 | $copy_users = true, |
||
| 3664 | $create_new_courses = false, |
||
| 3665 | $set_exercises_lp_invisible = false |
||
| 3666 | ) { |
||
| 3667 | $id = intval($id); |
||
| 3668 | $s = self::fetch($id); |
||
| 3669 | // Check all dates before copying |
||
| 3670 | // Get timestamp for now in UTC - see http://php.net/manual/es/function.time.php#117251 |
||
| 3671 | $now = time() - date('Z'); |
||
| 3672 | // Timestamp in one month |
||
| 3673 | $inOneMonth = $now + (30*24*3600); |
||
| 3674 | $inOneMonth = api_get_local_time($inOneMonth); |
||
| 3675 | if (api_strtotime($s['access_start_date']) < $now) { |
||
| 3676 | $s['access_start_date'] = api_get_local_time($now); |
||
| 3677 | } |
||
| 3678 | if (api_strtotime($s['display_start_date']) < $now) { |
||
| 3679 | $s['display_start_date'] = api_get_local_time($now); |
||
| 3680 | } |
||
| 3681 | if (api_strtotime($s['coach_access_start_date']) < $now) { |
||
| 3682 | $s['coach_access_start_date'] = api_get_local_time($now); |
||
| 3683 | } |
||
| 3684 | if (api_strtotime($s['access_end_date']) < $now) { |
||
| 3685 | $s['access_end_date'] = $inOneMonth; |
||
| 3686 | } |
||
| 3687 | if (api_strtotime($s['display_end_date']) < $now) { |
||
| 3688 | $s['display_end_date'] = $inOneMonth; |
||
| 3689 | } |
||
| 3690 | if (api_strtotime($s['coach_access_end_date']) < $now) { |
||
| 3691 | $s['coach_access_end_date'] = $inOneMonth; |
||
| 3692 | } |
||
| 3693 | // Now try to create the session |
||
| 3694 | $sid = self::create_session( |
||
| 3695 | $s['name'] . ' ' . get_lang('CopyLabelSuffix'), |
||
| 3696 | $s['access_start_date'], |
||
| 3697 | $s['access_end_date'], |
||
| 3698 | $s['display_start_date'], |
||
| 3699 | $s['display_end_date'], |
||
| 3700 | $s['coach_access_start_date'], |
||
| 3701 | $s['coach_access_end_date'], |
||
| 3702 | (int)$s['id_coach'], |
||
| 3703 | $s['session_category_id'], |
||
| 3704 | (int)$s['visibility'], |
||
| 3705 | true |
||
| 3706 | ); |
||
| 3707 | |||
| 3708 | if (!is_numeric($sid) || empty($sid)) { |
||
| 3709 | return false; |
||
| 3710 | } |
||
| 3711 | |||
| 3712 | if ($copy_courses) { |
||
| 3713 | // Register courses from the original session to the new session |
||
| 3714 | $courses = self::get_course_list_by_session_id($id); |
||
| 3715 | |||
| 3716 | $short_courses = $new_short_courses = array(); |
||
| 3717 | if (is_array($courses) && count($courses) > 0) { |
||
| 3718 | foreach ($courses as $course) { |
||
| 3719 | $short_courses[] = $course; |
||
| 3720 | } |
||
| 3721 | } |
||
| 3722 | |||
| 3723 | $courses = null; |
||
| 3724 | |||
| 3725 | //We will copy the current courses of the session to new courses |
||
| 3726 | if (!empty($short_courses)) { |
||
| 3727 | if ($create_new_courses) { |
||
| 3728 | //Just in case |
||
| 3729 | if (function_exists('ini_set')) { |
||
| 3730 | api_set_memory_limit('256M'); |
||
| 3731 | ini_set('max_execution_time', 0); |
||
| 3732 | } |
||
| 3733 | $params = array(); |
||
| 3734 | $params['skip_lp_dates'] = true; |
||
| 3735 | |||
| 3736 | foreach ($short_courses as $course_data) { |
||
| 3737 | $course_info = CourseManager::copy_course_simple( |
||
| 3738 | $course_data['title'].' '.get_lang( |
||
| 3739 | 'CopyLabelSuffix' |
||
| 3740 | ), |
||
| 3741 | $course_data['course_code'], |
||
| 3742 | $id, |
||
| 3743 | $sid, |
||
| 3744 | $params |
||
| 3745 | ); |
||
| 3746 | |||
| 3747 | if ($course_info) { |
||
| 3748 | //By default new elements are invisible |
||
| 3749 | if ($set_exercises_lp_invisible) { |
||
| 3750 | $list = new LearnpathList('', $course_info['code'], $sid); |
||
| 3751 | $flat_list = $list->get_flat_list(); |
||
| 3752 | if (!empty($flat_list)) { |
||
| 3753 | foreach ($flat_list as $lp_id => $data) { |
||
| 3754 | api_item_property_update( |
||
| 3755 | $course_info, |
||
| 3756 | TOOL_LEARNPATH, |
||
| 3757 | $lp_id, |
||
| 3758 | 'invisible', |
||
| 3759 | api_get_user_id(), |
||
| 3760 | 0, |
||
| 3761 | 0, |
||
| 3762 | 0, |
||
| 3763 | 0, |
||
| 3764 | $sid |
||
| 3765 | ); |
||
| 3766 | } |
||
| 3767 | } |
||
| 3768 | $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 3769 | $course_id = $course_info['real_id']; |
||
| 3770 | //@todo check this query |
||
| 3771 | $sql = "UPDATE $quiz_table SET active = 0 |
||
| 3772 | WHERE c_id = $course_id AND session_id = $sid"; |
||
| 3773 | Database::query($sql); |
||
| 3774 | } |
||
| 3775 | $new_short_courses[] = $course_info['real_id']; |
||
| 3776 | } |
||
| 3777 | } |
||
| 3778 | } else { |
||
| 3779 | foreach ($short_courses as $course_data) { |
||
| 3780 | $new_short_courses[] = $course_data['id']; |
||
| 3781 | } |
||
| 3782 | } |
||
| 3783 | |||
| 3784 | $short_courses = $new_short_courses; |
||
| 3785 | self::add_courses_to_session($sid, $short_courses, true); |
||
| 3786 | $short_courses = null; |
||
| 3787 | } |
||
| 3788 | } |
||
| 3789 | if ($copy_users) { |
||
| 3790 | // Register users from the original session to the new session |
||
| 3791 | $users = self::get_users_by_session($id); |
||
| 3792 | $short_users = array(); |
||
| 3793 | if (is_array($users) && count($users) > 0) { |
||
| 3794 | foreach ($users as $user) { |
||
| 3795 | $short_users[] = $user['user_id']; |
||
| 3796 | } |
||
| 3797 | } |
||
| 3798 | $users = null; |
||
| 3799 | //Subscribing in read only mode |
||
| 3800 | self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true); |
||
| 3801 | $short_users = null; |
||
| 3802 | } |
||
| 3803 | return $sid; |
||
| 3804 | } |
||
| 3805 | |||
| 3806 | /** |
||
| 3807 | * @param int $user_id |
||
| 3808 | * @param int $session_id |
||
| 3809 | * @return bool |
||
| 3810 | */ |
||
| 3811 | static function user_is_general_coach($user_id, $session_id) |
||
| 3812 | { |
||
| 3813 | $session_id = intval($session_id); |
||
| 3814 | $user_id = intval($user_id); |
||
| 3815 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3816 | $sql = "SELECT DISTINCT id |
||
| 3817 | FROM $session_table |
||
| 3818 | WHERE session.id_coach = '" . $user_id . "' AND id = '$session_id'"; |
||
| 3819 | $result = Database::query($sql); |
||
| 3820 | if ($result && Database::num_rows($result)) { |
||
| 3821 | return true; |
||
| 3822 | } |
||
| 3823 | return false; |
||
| 3824 | } |
||
| 3825 | |||
| 3826 | /** |
||
| 3827 | * Get the number of sessions |
||
| 3828 | * @param int ID of the URL we want to filter on (optional) |
||
| 3829 | * @return int Number of sessions |
||
| 3830 | */ |
||
| 3831 | public static function count_sessions($access_url_id = null) |
||
| 3832 | { |
||
| 3833 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3834 | $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3835 | $sql = "SELECT count(id) FROM $session_table s"; |
||
| 3836 | if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) { |
||
| 3837 | $sql .= ", $access_url_rel_session_table u " . |
||
| 3838 | " WHERE s.id = u.session_id AND u.access_url_id = $access_url_id"; |
||
| 3839 | } |
||
| 3840 | $res = Database::query($sql); |
||
| 3841 | $row = Database::fetch_row($res); |
||
| 3842 | return $row[0]; |
||
| 3843 | } |
||
| 3844 | |||
| 3845 | /** |
||
| 3846 | * Protect a session to be edited. |
||
| 3847 | * @param int $id |
||
| 3848 | * @param bool $checkSession |
||
| 3849 | */ |
||
| 3850 | public static function protectSession($id, $checkSession = true) |
||
| 3851 | { |
||
| 3852 | // api_protect_admin_script(true); |
||
| 3853 | if (self::allowToManageSessions()) { |
||
| 3854 | |||
| 3855 | if (api_is_platform_admin()) { |
||
| 3856 | return true; |
||
| 3857 | } |
||
| 3858 | |||
| 3859 | if ($checkSession) { |
||
| 3860 | if (self::allowed($id)) { |
||
| 3861 | return true; |
||
| 3862 | } else { |
||
| 3863 | api_not_allowed(true); |
||
| 3864 | } |
||
| 3865 | } |
||
| 3866 | } else { |
||
| 3867 | api_not_allowed(true); |
||
| 3868 | } |
||
| 3869 | } |
||
| 3870 | |||
| 3871 | /** |
||
| 3872 | * @param int $id |
||
| 3873 | * @return bool |
||
| 3874 | */ |
||
| 3875 | private static function allowed($id) |
||
| 3876 | { |
||
| 3877 | $sessionInfo = self::fetch($id); |
||
| 3878 | |||
| 3879 | if (empty($sessionInfo)) { |
||
| 3880 | return false; |
||
| 3881 | } |
||
| 3882 | |||
| 3883 | $userId = api_get_user_id(); |
||
| 3884 | |||
| 3885 | if (api_is_session_admin() && |
||
| 3886 | api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true' |
||
| 3887 | ) { |
||
| 3888 | if ($sessionInfo['session_admin_id'] != $userId) { |
||
| 3889 | return false; |
||
| 3890 | } |
||
| 3891 | } |
||
| 3892 | |||
| 3893 | if (api_is_teacher() && |
||
| 3894 | api_get_setting('allow_teachers_to_create_sessions') == 'true' |
||
| 3895 | ) { |
||
| 3896 | if ($sessionInfo['id_coach'] != $userId) { |
||
| 3897 | return false; |
||
| 3898 | } |
||
| 3899 | } |
||
| 3900 | |||
| 3901 | return true; |
||
| 3902 | } |
||
| 3903 | |||
| 3904 | /** |
||
| 3905 | * @return bool |
||
| 3906 | */ |
||
| 3907 | public static function allowToManageSessions() |
||
| 3908 | { |
||
| 3909 | if (self::allowManageAllSessions()) { |
||
| 3910 | return true; |
||
| 3911 | } |
||
| 3912 | |||
| 3913 | $setting = api_get_setting('allow_teachers_to_create_sessions'); |
||
| 3914 | |||
| 3915 | if (api_is_teacher() && $setting == 'true') { |
||
| 3916 | |||
| 3917 | return true; |
||
| 3918 | } |
||
| 3919 | |||
| 3920 | return false; |
||
| 3921 | } |
||
| 3922 | |||
| 3923 | /** |
||
| 3924 | * @return bool |
||
| 3925 | */ |
||
| 3926 | public static function allowOnlyMySessions() |
||
| 3927 | { |
||
| 3928 | if (self::allowToManageSessions() && |
||
| 3929 | !api_is_platform_admin() && |
||
| 3930 | api_is_teacher() |
||
| 3931 | ) { |
||
| 3932 | return true; |
||
| 3933 | } |
||
| 3934 | |||
| 3935 | return false; |
||
| 3936 | } |
||
| 3937 | |||
| 3938 | /** |
||
| 3939 | * @return bool |
||
| 3940 | */ |
||
| 3941 | public static function allowManageAllSessions() |
||
| 3942 | { |
||
| 3943 | if (api_is_platform_admin()) { |
||
| 3944 | return true; |
||
| 3945 | } |
||
| 3946 | |||
| 3947 | return false; |
||
| 3948 | } |
||
| 3949 | |||
| 3950 | /** |
||
| 3951 | * @param $id |
||
| 3952 | * @return bool |
||
| 3953 | */ |
||
| 3954 | public static function protect_teacher_session_edit($id) |
||
| 3955 | { |
||
| 3956 | if (!api_is_coach($id) && !api_is_platform_admin()) { |
||
| 3957 | api_not_allowed(true); |
||
| 3958 | } else { |
||
| 3959 | return true; |
||
| 3960 | } |
||
| 3961 | } |
||
| 3962 | |||
| 3963 | /** |
||
| 3964 | * @param int $courseId |
||
| 3965 | * @return array |
||
| 3966 | */ |
||
| 3967 | public static function get_session_by_course($courseId) |
||
| 3968 | { |
||
| 3969 | $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3970 | $table_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3971 | $courseId = intval($courseId); |
||
| 3972 | $sql = "SELECT name, s.id |
||
| 3973 | FROM $table_session_course sc |
||
| 3974 | INNER JOIN $table_session s ON (sc.session_id = s.id) |
||
| 3975 | WHERE sc.c_id = '$courseId' "; |
||
| 3976 | $result = Database::query($sql); |
||
| 3977 | |||
| 3978 | return Database::store_result($result); |
||
| 3979 | } |
||
| 3980 | |||
| 3981 | /** |
||
| 3982 | * @param int $user_id |
||
| 3983 | * @param bool $ignore_visibility_for_admins |
||
| 3984 | * @param bool $ignoreTimeLimit |
||
| 3985 | * |
||
| 3986 | * @return array |
||
| 3987 | */ |
||
| 3988 | public static function get_sessions_by_user($user_id, $ignore_visibility_for_admins = false, $ignoreTimeLimit = false) |
||
| 3989 | { |
||
| 3990 | $sessionCategories = UserManager::get_sessions_by_category( |
||
| 3991 | $user_id, |
||
| 3992 | false, |
||
| 3993 | $ignore_visibility_for_admins, |
||
| 3994 | $ignoreTimeLimit |
||
| 3995 | ); |
||
| 3996 | $sessionArray = array(); |
||
| 3997 | if (!empty($sessionCategories)) { |
||
| 3998 | foreach ($sessionCategories as $category) { |
||
| 3999 | if (isset($category['sessions'])) { |
||
| 4000 | foreach ($category['sessions'] as $session) { |
||
| 4001 | $sessionArray[] = $session; |
||
| 4002 | } |
||
| 4003 | } |
||
| 4004 | } |
||
| 4005 | } |
||
| 4006 | |||
| 4007 | return $sessionArray; |
||
| 4008 | } |
||
| 4009 | |||
| 4010 | /** |
||
| 4011 | * @param string $file |
||
| 4012 | * @param bool $updateSession options: |
||
| 4013 | * true: if the session exists it will be updated. |
||
| 4014 | * false: if session exists a new session will be created adding a counter session1, session2, etc |
||
| 4015 | * @param int $defaultUserId |
||
| 4016 | * @param mixed $logger |
||
| 4017 | * @param array $extraFields convert a file row to an extra field. Example in CSV file there's a SessionID then it will |
||
| 4018 | * converted to extra_external_session_id if you set this: array('SessionId' => 'extra_external_session_id') |
||
| 4019 | * @param string $extraFieldId |
||
| 4020 | * @param int $daysCoachAccessBeforeBeginning |
||
| 4021 | * @param int $daysCoachAccessAfterBeginning |
||
| 4022 | * @param int $sessionVisibility |
||
| 4023 | * @param array $fieldsToAvoidUpdate |
||
| 4024 | * @param bool $deleteUsersNotInList |
||
| 4025 | * @param bool $updateCourseCoaches |
||
| 4026 | * @param bool $sessionWithCoursesModifier |
||
| 4027 | * @param int $showDescription |
||
| 4028 | * @param array $teacherBackupList |
||
| 4029 | * @param array $groupBackup |
||
| 4030 | * @return array |
||
| 4031 | */ |
||
| 4032 | static function importCSV( |
||
| 4033 | $file, |
||
| 4034 | $updateSession, |
||
| 4035 | $defaultUserId = null, |
||
| 4036 | $logger = null, |
||
| 4037 | $extraFields = array(), |
||
| 4038 | $extraFieldId = null, |
||
| 4039 | $daysCoachAccessBeforeBeginning = null, |
||
| 4040 | $daysCoachAccessAfterBeginning = null, |
||
| 4041 | $sessionVisibility = 1, |
||
| 4042 | $fieldsToAvoidUpdate = array(), |
||
| 4043 | $deleteUsersNotInList = false, |
||
| 4044 | $updateCourseCoaches = false, |
||
| 4045 | $sessionWithCoursesModifier = false, |
||
| 4046 | $addOriginalCourseTeachersAsCourseSessionCoaches = true, |
||
| 4047 | $removeAllTeachersFromCourse = true, |
||
| 4048 | $showDescription = null, |
||
| 4049 | &$teacherBackupList = array(), |
||
| 4050 | &$groupBackup = array() |
||
| 4051 | ) { |
||
| 4052 | $content = file($file); |
||
| 4053 | |||
| 4054 | $error_message = null; |
||
| 4055 | $session_counter = 0; |
||
| 4056 | |||
| 4057 | if (empty($defaultUserId)) { |
||
| 4058 | $defaultUserId = api_get_user_id(); |
||
| 4059 | } |
||
| 4060 | |||
| 4061 | $eol = PHP_EOL; |
||
| 4062 | if (PHP_SAPI != 'cli') { |
||
| 4063 | $eol = '<br />'; |
||
| 4064 | } |
||
| 4065 | |||
| 4066 | $debug = false; |
||
| 4067 | if (isset($logger)) { |
||
| 4068 | $debug = true; |
||
| 4069 | } |
||
| 4070 | |||
| 4071 | $extraParameters = null; |
||
| 4072 | |||
| 4073 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4074 | $extraParameters .= ' , nb_days_access_before_beginning = '.intval($daysCoachAccessBeforeBeginning); |
||
| 4075 | $extraParameters .= ' , nb_days_access_after_end = '.intval($daysCoachAccessAfterBeginning); |
||
| 4076 | } |
||
| 4077 | |||
| 4078 | if (!is_null($showDescription)) { |
||
| 4079 | $extraParameters .= ' , show_description = '.intval($showDescription); |
||
| 4080 | } |
||
| 4081 | |||
| 4082 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 4083 | $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 4084 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 4085 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4086 | |||
| 4087 | $sessions = array(); |
||
| 4088 | |||
| 4089 | if (!api_strstr($content[0], ';')) { |
||
| 4090 | $error_message = get_lang('NotCSV'); |
||
| 4091 | } else { |
||
| 4092 | $tag_names = array(); |
||
| 4093 | |||
| 4094 | View Code Duplication | foreach ($content as $key => $enreg) { |
|
| 4095 | $enreg = explode(';', trim($enreg)); |
||
| 4096 | if ($key) { |
||
| 4097 | foreach ($tag_names as $tag_key => $tag_name) { |
||
| 4098 | $sessions[$key - 1][$tag_name] = $enreg[$tag_key]; |
||
| 4099 | } |
||
| 4100 | } else { |
||
| 4101 | foreach ($enreg as $tag_name) { |
||
| 4102 | $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\-]/', '', $tag_name); |
||
| 4103 | } |
||
| 4104 | if (!in_array('SessionName', $tag_names) || |
||
| 4105 | !in_array('DateStart', $tag_names) || |
||
| 4106 | !in_array('DateEnd', $tag_names) |
||
| 4107 | ) { |
||
| 4108 | $error_message = get_lang('NoNeededData'); |
||
| 4109 | break; |
||
| 4110 | } |
||
| 4111 | } |
||
| 4112 | } |
||
| 4113 | |||
| 4114 | $sessionList = array(); |
||
| 4115 | // Looping the sessions. |
||
| 4116 | foreach ($sessions as $enreg) { |
||
| 4117 | $user_counter = 0; |
||
| 4118 | $course_counter = 0; |
||
| 4119 | |||
| 4120 | if (isset($extraFields) && !empty($extraFields)) { |
||
| 4121 | foreach ($extraFields as $original => $to) { |
||
| 4122 | $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null; |
||
| 4123 | } |
||
| 4124 | } |
||
| 4125 | |||
| 4126 | $session_name = Database::escape_string($enreg['SessionName']); |
||
| 4127 | // Default visibility |
||
| 4128 | $visibilityAfterExpirationPerSession = $sessionVisibility; |
||
| 4129 | |||
| 4130 | if (isset($enreg['VisibilityAfterExpiration'])) { |
||
| 4131 | $visibility = $enreg['VisibilityAfterExpiration']; |
||
| 4132 | switch ($visibility) { |
||
| 4133 | case 'read_only': |
||
| 4134 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY; |
||
| 4135 | break; |
||
| 4136 | case 'accessible': |
||
| 4137 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE; |
||
| 4138 | break; |
||
| 4139 | case 'not_accessible': |
||
| 4140 | $visibilityAfterExpirationPerSession = SESSION_INVISIBLE; |
||
| 4141 | break; |
||
| 4142 | } |
||
| 4143 | } |
||
| 4144 | |||
| 4145 | if (empty($session_name)) { |
||
| 4146 | continue; |
||
| 4147 | } |
||
| 4148 | |||
| 4149 | $date_start = $enreg['DateStart']; |
||
| 4150 | $date_end = $enreg['DateEnd']; |
||
| 4151 | $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null; |
||
| 4152 | $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null; |
||
| 4153 | |||
| 4154 | $extraSessionParameters = null; |
||
| 4155 | if (!empty($sessionDescription)) { |
||
| 4156 | $extraSessionParameters = " , description = '".Database::escape_string($sessionDescription)."'"; |
||
| 4157 | } |
||
| 4158 | |||
| 4159 | // Searching a general coach. |
||
| 4160 | if (!empty($enreg['Coach'])) { |
||
| 4161 | $coach_id = UserManager::get_user_id_from_username($enreg['Coach']); |
||
| 4162 | if ($coach_id === false) { |
||
| 4163 | // If the coach-user does not exist - I'm the coach. |
||
| 4164 | $coach_id = $defaultUserId; |
||
| 4165 | } |
||
| 4166 | } else { |
||
| 4167 | $coach_id = $defaultUserId; |
||
| 4168 | } |
||
| 4169 | |||
| 4170 | if (!$updateSession) { |
||
| 4171 | // Always create a session. |
||
| 4172 | $unique_name = false; |
||
| 4173 | $i = 0; |
||
| 4174 | // Change session name, verify that session doesn't exist. |
||
| 4175 | $suffix = null; |
||
| 4176 | View Code Duplication | while (!$unique_name) { |
|
| 4177 | if ($i > 1) { |
||
| 4178 | $suffix = ' - ' . $i; |
||
| 4179 | } |
||
| 4180 | $sql = 'SELECT 1 FROM ' . $tbl_session . ' |
||
| 4181 | WHERE name="' . $session_name . $suffix . '"'; |
||
| 4182 | $rs = Database::query($sql); |
||
| 4183 | |||
| 4184 | if (Database::result($rs, 0, 0)) { |
||
| 4185 | $i++; |
||
| 4186 | } else { |
||
| 4187 | $unique_name = true; |
||
| 4188 | $session_name .= $suffix; |
||
| 4189 | } |
||
| 4190 | } |
||
| 4191 | |||
| 4192 | $sessionCondition = ''; |
||
| 4193 | if (!empty($session_category_id)) { |
||
| 4194 | $sessionCondition = "session_category_id = '$session_category_id',"; |
||
| 4195 | } |
||
| 4196 | |||
| 4197 | // Creating the session. |
||
| 4198 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4199 | name = '" . $session_name . "', |
||
| 4200 | id_coach = '$coach_id', |
||
| 4201 | access_start_date = '$date_start', |
||
| 4202 | access_end_date = '$date_end', |
||
| 4203 | visibility = '$visibilityAfterExpirationPerSession', |
||
| 4204 | $sessionCondition |
||
| 4205 | session_admin_id = " . intval($defaultUserId) . $extraParameters . $extraSessionParameters; |
||
| 4206 | Database::query($sql); |
||
| 4207 | |||
| 4208 | $session_id = Database::insert_id(); |
||
| 4209 | if ($debug) { |
||
| 4210 | if ($session_id) { |
||
| 4211 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4212 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4213 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4214 | } |
||
| 4215 | } |
||
| 4216 | |||
| 4217 | $logger->addInfo("Sessions - Session created: #$session_id - $session_name"); |
||
| 4218 | } else { |
||
| 4219 | $logger->addError("Sessions - Session NOT created: $session_name"); |
||
| 4220 | } |
||
| 4221 | } |
||
| 4222 | $session_counter++; |
||
| 4223 | } else { |
||
| 4224 | $sessionId = null; |
||
| 4225 | if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_'.$extraFieldId])) { |
||
| 4226 | $sessionId = self::getSessionIdFromOriginalId($enreg['extra_'.$extraFieldId], $extraFieldId); |
||
| 4227 | if (empty($sessionId)) { |
||
| 4228 | $my_session_result = false; |
||
| 4229 | } else { |
||
| 4230 | $my_session_result = true; |
||
| 4231 | } |
||
| 4232 | } else { |
||
| 4233 | $my_session_result = self::get_session_by_name($enreg['SessionName']); |
||
| 4234 | } |
||
| 4235 | |||
| 4236 | if ($my_session_result === false) { |
||
| 4237 | |||
| 4238 | // Creating a session. |
||
| 4239 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4240 | name = '$session_name', |
||
| 4241 | id_coach = '$coach_id', |
||
| 4242 | access_start_date = '$date_start', |
||
| 4243 | access_end_date = '$date_end', |
||
| 4244 | visibility = '$visibilityAfterExpirationPerSession', |
||
| 4245 | session_category_id = '$session_category_id' " . $extraParameters . $extraSessionParameters; |
||
| 4246 | |||
| 4247 | Database::query($sql); |
||
| 4248 | |||
| 4249 | // We get the last insert id. |
||
| 4250 | $my_session_result = SessionManager::get_session_by_name($enreg['SessionName']); |
||
| 4251 | $session_id = $my_session_result['id']; |
||
| 4252 | |||
| 4253 | if ($session_id) { |
||
| 4254 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4255 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4256 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4257 | } |
||
| 4258 | } |
||
| 4259 | if ($debug) { |
||
| 4260 | $logger->addInfo("Sessions - #$session_id created: $session_name"); |
||
| 4261 | } |
||
| 4262 | |||
| 4263 | // Delete session-user relation only for students |
||
| 4264 | $sql = "DELETE FROM $tbl_session_user |
||
| 4265 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4266 | Database::query($sql); |
||
| 4267 | |||
| 4268 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4269 | Database::query($sql); |
||
| 4270 | |||
| 4271 | // Delete session-course-user relationships students and coaches. |
||
| 4272 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4273 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4274 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4275 | Database::query($sql); |
||
| 4276 | } else { |
||
| 4277 | // Delete session-course-user relation ships *only* for students. |
||
| 4278 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4279 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4280 | Database::query($sql); |
||
| 4281 | } |
||
| 4282 | } |
||
| 4283 | } else { |
||
| 4284 | if ($debug) { |
||
| 4285 | $logger->addError("Sessions - Session to be updated: $session_name"); |
||
| 4286 | } |
||
| 4287 | |||
| 4288 | // Updating the session. |
||
| 4289 | $params = array( |
||
| 4290 | 'id_coach' => $coach_id, |
||
| 4291 | 'access_start_date' => $date_start, |
||
| 4292 | 'access_end_date' => $date_end, |
||
| 4293 | 'visibility' => $visibilityAfterExpirationPerSession, |
||
| 4294 | 'session_category_id' => $session_category_id, |
||
| 4295 | ); |
||
| 4296 | |||
| 4297 | if (!empty($sessionDescription)) { |
||
| 4298 | $params['description'] = $sessionDescription; |
||
| 4299 | } |
||
| 4300 | |||
| 4301 | if (!empty($fieldsToAvoidUpdate)) { |
||
| 4302 | foreach ($fieldsToAvoidUpdate as $field) { |
||
| 4303 | unset($params[$field]); |
||
| 4304 | } |
||
| 4305 | } |
||
| 4306 | |||
| 4307 | View Code Duplication | if (isset($sessionId) && !empty($sessionId)) { |
|
| 4308 | if (!empty($enreg['SessionName'])) { |
||
| 4309 | $params['name'] = $enreg['SessionName']; |
||
| 4310 | } |
||
| 4311 | $session_id = $sessionId; |
||
| 4312 | } else { |
||
| 4313 | $row = Database::query("SELECT id FROM $tbl_session WHERE name = '$session_name'"); |
||
| 4314 | list($session_id) = Database::fetch_array($row); |
||
| 4315 | } |
||
| 4316 | |||
| 4317 | if ($session_id) { |
||
| 4318 | |||
| 4319 | if ($debug) { |
||
| 4320 | $logger->addError("Sessions - Session to be updated #$session_id"); |
||
| 4321 | } |
||
| 4322 | |||
| 4323 | $sessionInfo = api_get_session_info($session_id); |
||
| 4324 | |||
| 4325 | $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription); |
||
| 4326 | |||
| 4327 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4328 | View Code Duplication | if (empty($sessionInfo['nb_days_access_before_beginning']) || |
|
| 4329 | (!empty($sessionInfo['nb_days_access_before_beginning']) && |
||
| 4330 | $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning) |
||
| 4331 | ) { |
||
| 4332 | $params['nb_days_access_before_beginning'] = intval($daysCoachAccessBeforeBeginning); |
||
| 4333 | } |
||
| 4334 | |||
| 4335 | View Code Duplication | if (empty($sessionInfo['nb_days_access_after_end']) || |
|
| 4336 | (!empty($sessionInfo['nb_days_access_after_end']) && |
||
| 4337 | $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning) |
||
| 4338 | ) { |
||
| 4339 | $params['nb_days_access_after_end'] = intval($daysCoachAccessAfterBeginning); |
||
| 4340 | } |
||
| 4341 | } |
||
| 4342 | |||
| 4343 | Database::update($tbl_session, $params, array('id = ?' => $session_id)); |
||
| 4344 | |||
| 4345 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4346 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4347 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4348 | } |
||
| 4349 | } |
||
| 4350 | |||
| 4351 | // Delete session-user relation only for students |
||
| 4352 | $sql = "DELETE FROM $tbl_session_user |
||
| 4353 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4354 | Database::query($sql); |
||
| 4355 | |||
| 4356 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4357 | Database::query($sql); |
||
| 4358 | |||
| 4359 | // Delete session-course-user relationships students and coaches. |
||
| 4360 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4361 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4362 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4363 | Database::query($sql); |
||
| 4364 | } else { |
||
| 4365 | // Delete session-course-user relation ships *only* for students. |
||
| 4366 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4367 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4368 | Database::query($sql); |
||
| 4369 | } |
||
| 4370 | } else { |
||
| 4371 | if ($debug) { |
||
| 4372 | $logger->addError( |
||
| 4373 | "Sessions - Session not found" |
||
| 4374 | ); |
||
| 4375 | } |
||
| 4376 | } |
||
| 4377 | } |
||
| 4378 | $session_counter++; |
||
| 4379 | } |
||
| 4380 | |||
| 4381 | $sessionList[] = $session_id; |
||
| 4382 | $users = explode('|', $enreg['Users']); |
||
| 4383 | |||
| 4384 | // Adding the relationship "Session - User" for students |
||
| 4385 | $userList = array(); |
||
| 4386 | |||
| 4387 | if (is_array($users)) { |
||
| 4388 | foreach ($users as $user) { |
||
| 4389 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4390 | if ($user_id !== false) { |
||
| 4391 | $userList[] = $user_id; |
||
| 4392 | // Insert new users. |
||
| 4393 | $sql = "INSERT IGNORE INTO $tbl_session_user SET |
||
| 4394 | user_id = '$user_id', |
||
| 4395 | session_id = '$session_id', |
||
| 4396 | registered_at = '" . api_get_utc_datetime() . "'"; |
||
| 4397 | Database::query($sql); |
||
| 4398 | if ($debug) { |
||
| 4399 | $logger->addInfo("Sessions - Adding User #$user_id ($user) to session #$session_id"); |
||
| 4400 | } |
||
| 4401 | $user_counter++; |
||
| 4402 | } |
||
| 4403 | } |
||
| 4404 | } |
||
| 4405 | |||
| 4406 | if ($deleteUsersNotInList) { |
||
| 4407 | // Getting user in DB in order to compare to the new list. |
||
| 4408 | $usersListInDatabase = self::get_users_by_session($session_id, 0); |
||
| 4409 | |||
| 4410 | if (!empty($usersListInDatabase)) { |
||
| 4411 | if (empty($userList)) { |
||
| 4412 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4413 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4414 | } |
||
| 4415 | } else { |
||
| 4416 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4417 | if (!in_array($userInfo['user_id'], $userList)) { |
||
| 4418 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4419 | } |
||
| 4420 | } |
||
| 4421 | } |
||
| 4422 | } |
||
| 4423 | } |
||
| 4424 | |||
| 4425 | $courses = explode('|', $enreg['Courses']); |
||
| 4426 | |||
| 4427 | // See BT#6449 |
||
| 4428 | $onlyAddFirstCoachOrTeacher = false; |
||
| 4429 | |||
| 4430 | if ($sessionWithCoursesModifier) { |
||
| 4431 | if (count($courses) >= 2) { |
||
| 4432 | // Only first teacher in course session; |
||
| 4433 | $onlyAddFirstCoachOrTeacher = true; |
||
| 4434 | |||
| 4435 | // Remove all teachers from course. |
||
| 4436 | $removeAllTeachersFromCourse = false; |
||
| 4437 | } |
||
| 4438 | } |
||
| 4439 | |||
| 4440 | foreach ($courses as $course) { |
||
| 4441 | $courseArray = bracketsToArray($course); |
||
| 4442 | $course_code = $courseArray[0]; |
||
| 4443 | |||
| 4444 | if (CourseManager::course_exists($course_code)) { |
||
| 4445 | |||
| 4446 | $courseInfo = api_get_course_info($course_code); |
||
| 4447 | $courseId = $courseInfo['real_id']; |
||
| 4448 | |||
| 4449 | // Adding the course to a session. |
||
| 4450 | $sql = "INSERT IGNORE INTO $tbl_session_course |
||
| 4451 | SET c_id = '$courseId', session_id='$session_id'"; |
||
| 4452 | Database::query($sql); |
||
| 4453 | |||
| 4454 | SessionManager::installCourse($session_id, $courseInfo['real_id']); |
||
| 4455 | |||
| 4456 | if ($debug) { |
||
| 4457 | $logger->addInfo("Sessions - Adding course '$course_code' to session #$session_id"); |
||
| 4458 | } |
||
| 4459 | |||
| 4460 | $course_counter++; |
||
| 4461 | |||
| 4462 | $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null; |
||
| 4463 | $course_users = isset($courseArray[2]) ? $courseArray[2] : null; |
||
| 4464 | |||
| 4465 | $course_users = explode(',', $course_users); |
||
| 4466 | $course_coaches = explode(',', $course_coaches); |
||
| 4467 | |||
| 4468 | // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php) |
||
| 4469 | $addTeachersToSession = true; |
||
| 4470 | |||
| 4471 | if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) { |
||
| 4472 | $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses']; |
||
| 4473 | } |
||
| 4474 | |||
| 4475 | // If any user provided for a course, use the users array. |
||
| 4476 | if (empty($course_users)) { |
||
| 4477 | if (!empty($userList)) { |
||
| 4478 | SessionManager::subscribe_users_to_session_course( |
||
| 4479 | $userList, |
||
| 4480 | $session_id, |
||
| 4481 | $course_code |
||
| 4482 | ); |
||
| 4483 | if ($debug) { |
||
| 4484 | $msg = "Sessions - Adding student list ".implode(', #', $userList)." to course: '$course_code' and session #$session_id"; |
||
| 4485 | $logger->addInfo($msg); |
||
| 4486 | } |
||
| 4487 | } |
||
| 4488 | } |
||
| 4489 | |||
| 4490 | // Adding coaches to session course user. |
||
| 4491 | if (!empty($course_coaches)) { |
||
| 4492 | $savedCoaches = array(); |
||
| 4493 | // only edit if add_teachers_to_sessions_courses is set. |
||
| 4494 | if ($addTeachersToSession) { |
||
| 4495 | if ($addOriginalCourseTeachersAsCourseSessionCoaches) { |
||
| 4496 | // Adding course teachers as course session teachers. |
||
| 4497 | $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code( |
||
| 4498 | $course_code |
||
| 4499 | ); |
||
| 4500 | |||
| 4501 | if (!empty($alreadyAddedTeachers)) { |
||
| 4502 | $teachersToAdd = array(); |
||
| 4503 | foreach ($alreadyAddedTeachers as $user) { |
||
| 4504 | $teachersToAdd[] = $user['username']; |
||
| 4505 | } |
||
| 4506 | $course_coaches = array_merge( |
||
| 4507 | $course_coaches, |
||
| 4508 | $teachersToAdd |
||
| 4509 | ); |
||
| 4510 | } |
||
| 4511 | } |
||
| 4512 | |||
| 4513 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4514 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4515 | if ($coach_id !== false) { |
||
| 4516 | // Just insert new coaches |
||
| 4517 | SessionManager::updateCoaches($session_id, $courseId, array($coach_id), false); |
||
| 4518 | |||
| 4519 | if ($debug) { |
||
| 4520 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4521 | } |
||
| 4522 | $savedCoaches[] = $coach_id; |
||
| 4523 | } else { |
||
| 4524 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4525 | } |
||
| 4526 | } |
||
| 4527 | } |
||
| 4528 | |||
| 4529 | // Custom courses/session coaches |
||
| 4530 | $teacherToAdd = null; |
||
| 4531 | // Only one coach is added. |
||
| 4532 | if ($onlyAddFirstCoachOrTeacher == true) { |
||
| 4533 | |||
| 4534 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4535 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4536 | if ($coach_id !== false) { |
||
| 4537 | $teacherToAdd = $coach_id; |
||
| 4538 | break; |
||
| 4539 | } |
||
| 4540 | } |
||
| 4541 | |||
| 4542 | // Un subscribe everyone that's not in the list. |
||
| 4543 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4544 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4545 | foreach ($teacherList as $teacher) { |
||
| 4546 | if ($teacherToAdd != $teacher['user_id']) { |
||
| 4547 | |||
| 4548 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4549 | WHERE |
||
| 4550 | user_id = ".$teacher['user_id']." AND |
||
| 4551 | course_code = '".$course_code."' |
||
| 4552 | "; |
||
| 4553 | |||
| 4554 | $result = Database::query($sql); |
||
| 4555 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4556 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4557 | |||
| 4558 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4559 | WHERE |
||
| 4560 | user_id = ".$teacher['user_id']." AND |
||
| 4561 | c_id = '".$courseInfo['real_id']."' |
||
| 4562 | "; |
||
| 4563 | |||
| 4564 | $result = Database::query($sql); |
||
| 4565 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4566 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4567 | } |
||
| 4568 | |||
| 4569 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4570 | WHERE |
||
| 4571 | user_id = ".$teacher['user_id']." AND |
||
| 4572 | c_id = '".$courseInfo['real_id']."' |
||
| 4573 | "; |
||
| 4574 | |||
| 4575 | $result = Database::query($sql); |
||
| 4576 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4577 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4578 | } |
||
| 4579 | |||
| 4580 | CourseManager::unsubscribe_user( |
||
| 4581 | $teacher['user_id'], |
||
| 4582 | $course_code |
||
| 4583 | ); |
||
| 4584 | } |
||
| 4585 | } |
||
| 4586 | } |
||
| 4587 | |||
| 4588 | if (!empty($teacherToAdd)) { |
||
| 4589 | SessionManager::updateCoaches($session_id, $courseId, array($teacherToAdd), true); |
||
| 4590 | |||
| 4591 | $userCourseCategory = ''; |
||
| 4592 | View Code Duplication | if (isset($teacherBackupList[$teacherToAdd]) && |
|
| 4593 | isset($teacherBackupList[$teacherToAdd][$course_code]) |
||
| 4594 | ) { |
||
| 4595 | $courseUserData = $teacherBackupList[$teacherToAdd][$course_code]; |
||
| 4596 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4597 | } |
||
| 4598 | |||
| 4599 | CourseManager::subscribe_user( |
||
| 4600 | $teacherToAdd, |
||
| 4601 | $course_code, |
||
| 4602 | COURSEMANAGER, |
||
| 4603 | 0, |
||
| 4604 | $userCourseCategory |
||
| 4605 | ); |
||
| 4606 | |||
| 4607 | View Code Duplication | if (isset($groupBackup['user'][$teacherToAdd]) && |
|
| 4608 | isset($groupBackup['user'][$teacherToAdd][$course_code]) && |
||
| 4609 | !empty($groupBackup['user'][$teacherToAdd][$course_code]) |
||
| 4610 | ) { |
||
| 4611 | foreach ($groupBackup['user'][$teacherToAdd][$course_code] as $data) { |
||
| 4612 | GroupManager::subscribe_users( |
||
| 4613 | $teacherToAdd, |
||
| 4614 | $data['group_id'], |
||
| 4615 | $data['c_id'] |
||
| 4616 | ); |
||
| 4617 | } |
||
| 4618 | } |
||
| 4619 | |||
| 4620 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherToAdd]) && |
|
| 4621 | isset($groupBackup['tutor'][$teacherToAdd][$course_code]) && |
||
| 4622 | !empty($groupBackup['tutor'][$teacherToAdd][$course_code]) |
||
| 4623 | ) { |
||
| 4624 | foreach ($groupBackup['tutor'][$teacherToAdd][$course_code] as $data) { |
||
| 4625 | GroupManager::subscribe_tutors( |
||
| 4626 | $teacherToAdd, |
||
| 4627 | $data['group_id'], |
||
| 4628 | $data['c_id'] |
||
| 4629 | ); |
||
| 4630 | } |
||
| 4631 | } |
||
| 4632 | } |
||
| 4633 | } |
||
| 4634 | |||
| 4635 | // See BT#6449#note-195 |
||
| 4636 | // All coaches are added. |
||
| 4637 | if ($removeAllTeachersFromCourse) { |
||
| 4638 | $teacherToAdd = null; |
||
| 4639 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4640 | $coach_id = UserManager::get_user_id_from_username( |
||
| 4641 | $course_coach |
||
| 4642 | ); |
||
| 4643 | if ($coach_id !== false) { |
||
| 4644 | $teacherToAdd[] = $coach_id; |
||
| 4645 | } |
||
| 4646 | } |
||
| 4647 | |||
| 4648 | if (!empty($teacherToAdd)) { |
||
| 4649 | // Deleting all course teachers and adding the only coach as teacher. |
||
| 4650 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4651 | |||
| 4652 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4653 | foreach ($teacherList as $teacher) { |
||
| 4654 | if (!in_array($teacher['user_id'], $teacherToAdd)) { |
||
| 4655 | |||
| 4656 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4657 | WHERE |
||
| 4658 | user_id = ".$teacher['user_id']." AND |
||
| 4659 | course_code = '".$course_code."' |
||
| 4660 | "; |
||
| 4661 | |||
| 4662 | $result = Database::query($sql); |
||
| 4663 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4664 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4665 | |||
| 4666 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4667 | WHERE |
||
| 4668 | user_id = ".$teacher['user_id']." AND |
||
| 4669 | c_id = '".$courseInfo['real_id']."' |
||
| 4670 | "; |
||
| 4671 | |||
| 4672 | $result = Database::query($sql); |
||
| 4673 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4674 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4675 | } |
||
| 4676 | |||
| 4677 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4678 | WHERE |
||
| 4679 | user_id = ".$teacher['user_id']." AND |
||
| 4680 | c_id = '".$courseInfo['real_id']."' |
||
| 4681 | "; |
||
| 4682 | |||
| 4683 | $result = Database::query($sql); |
||
| 4684 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4685 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4686 | } |
||
| 4687 | |||
| 4688 | CourseManager::unsubscribe_user( |
||
| 4689 | $teacher['user_id'], |
||
| 4690 | $course_code |
||
| 4691 | ); |
||
| 4692 | } |
||
| 4693 | } |
||
| 4694 | } |
||
| 4695 | |||
| 4696 | foreach ($teacherToAdd as $teacherId) { |
||
| 4697 | $userCourseCategory = ''; |
||
| 4698 | View Code Duplication | if (isset($teacherBackupList[$teacherId]) && |
|
| 4699 | isset($teacherBackupList[$teacherId][$course_code]) |
||
| 4700 | ) { |
||
| 4701 | $courseUserData = $teacherBackupList[$teacherId][$course_code]; |
||
| 4702 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4703 | } |
||
| 4704 | |||
| 4705 | CourseManager::subscribe_user( |
||
| 4706 | $teacherId, |
||
| 4707 | $course_code, |
||
| 4708 | COURSEMANAGER, |
||
| 4709 | 0, |
||
| 4710 | $userCourseCategory |
||
| 4711 | ); |
||
| 4712 | |||
| 4713 | View Code Duplication | if (isset($groupBackup['user'][$teacherId]) && |
|
| 4714 | isset($groupBackup['user'][$teacherId][$course_code]) && |
||
| 4715 | !empty($groupBackup['user'][$teacherId][$course_code]) |
||
| 4716 | ) { |
||
| 4717 | foreach ($groupBackup['user'][$teacherId][$course_code] as $data) { |
||
| 4718 | GroupManager::subscribe_users( |
||
| 4719 | $teacherId, |
||
| 4720 | $data['group_id'], |
||
| 4721 | $data['c_id'] |
||
| 4722 | ); |
||
| 4723 | } |
||
| 4724 | } |
||
| 4725 | |||
| 4726 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherId]) && |
|
| 4727 | isset($groupBackup['tutor'][$teacherId][$course_code]) && |
||
| 4728 | !empty($groupBackup['tutor'][$teacherId][$course_code]) |
||
| 4729 | ) { |
||
| 4730 | foreach ($groupBackup['tutor'][$teacherId][$course_code] as $data) { |
||
| 4731 | GroupManager::subscribe_tutors( |
||
| 4732 | $teacherId, |
||
| 4733 | $data['group_id'], |
||
| 4734 | $data['c_id'] |
||
| 4735 | ); |
||
| 4736 | } |
||
| 4737 | } |
||
| 4738 | } |
||
| 4739 | } |
||
| 4740 | } |
||
| 4741 | |||
| 4742 | // Continue default behaviour. |
||
| 4743 | if ($onlyAddFirstCoachOrTeacher == false) { |
||
| 4744 | // Checking one more time see BT#6449#note-149 |
||
| 4745 | $coaches = SessionManager::getCoachesByCourseSession($session_id, $courseId); |
||
| 4746 | // Update coaches if only there's 1 course see BT#6449#note-189 |
||
| 4747 | if (empty($coaches) || count($courses) == 1) { |
||
| 4748 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4749 | $course_coach = trim($course_coach); |
||
| 4750 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4751 | if ($coach_id !== false) { |
||
| 4752 | // Just insert new coaches |
||
| 4753 | SessionManager::updateCoaches( |
||
| 4754 | $session_id, |
||
| 4755 | $courseId, |
||
| 4756 | array($coach_id), |
||
| 4757 | false |
||
| 4758 | ); |
||
| 4759 | |||
| 4760 | if ($debug) { |
||
| 4761 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4762 | } |
||
| 4763 | $savedCoaches[] = $coach_id; |
||
| 4764 | } else { |
||
| 4765 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4766 | } |
||
| 4767 | } |
||
| 4768 | } |
||
| 4769 | } |
||
| 4770 | } |
||
| 4771 | |||
| 4772 | // Adding Students, updating relationship "Session - Course - User". |
||
| 4773 | $course_users = array_filter($course_users); |
||
| 4774 | |||
| 4775 | if (!empty($course_users)) { |
||
| 4776 | View Code Duplication | foreach ($course_users as $user) { |
|
| 4777 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4778 | |||
| 4779 | if ($user_id !== false) { |
||
| 4780 | SessionManager::subscribe_users_to_session_course( |
||
| 4781 | array($user_id), |
||
| 4782 | $session_id, |
||
| 4783 | $course_code |
||
| 4784 | ); |
||
| 4785 | if ($debug) { |
||
| 4786 | $logger->addInfo("Sessions - Adding student: user #$user_id ($user) to course: '$course_code' and session #$session_id"); |
||
| 4787 | } |
||
| 4788 | } else { |
||
| 4789 | $error_message .= get_lang('UserDoesNotExist').': '.$user.$eol; |
||
| 4790 | } |
||
| 4791 | } |
||
| 4792 | } |
||
| 4793 | |||
| 4794 | $inserted_in_course[$course_code] = $courseInfo['title']; |
||
| 4795 | } |
||
| 4796 | } |
||
| 4797 | $access_url_id = api_get_current_access_url_id(); |
||
| 4798 | UrlManager::add_session_to_url($session_id, $access_url_id); |
||
| 4799 | $sql = "UPDATE $tbl_session SET nbr_users = '$user_counter', nbr_courses = '$course_counter' WHERE id = '$session_id'"; |
||
| 4800 | Database::query($sql); |
||
| 4801 | } |
||
| 4802 | } |
||
| 4803 | |||
| 4804 | return array( |
||
| 4805 | 'error_message' => $error_message, |
||
| 4806 | 'session_counter' => $session_counter, |
||
| 4807 | 'session_list' => $sessionList, |
||
| 4808 | ); |
||
| 4809 | } |
||
| 4810 | |||
| 4811 | /** |
||
| 4812 | * @param int $sessionId |
||
| 4813 | * @param int $courseId |
||
| 4814 | * @return array |
||
| 4815 | */ |
||
| 4816 | View Code Duplication | public static function getCoachesByCourseSession($sessionId, $courseId) |
|
| 4817 | { |
||
| 4818 | $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4819 | $sessionId = intval($sessionId); |
||
| 4820 | $courseId = intval($courseId); |
||
| 4821 | |||
| 4822 | $sql = "SELECT user_id FROM $table |
||
| 4823 | WHERE |
||
| 4824 | session_id = '$sessionId' AND |
||
| 4825 | c_id = '$courseId' AND |
||
| 4826 | status = 2"; |
||
| 4827 | $result = Database::query($sql); |
||
| 4828 | |||
| 4829 | $coaches = array(); |
||
| 4830 | if (Database::num_rows($result) > 0) { |
||
| 4831 | while ($row = Database::fetch_row($result)) { |
||
| 4832 | $coaches[] = $row[0]; |
||
| 4833 | } |
||
| 4834 | } |
||
| 4835 | |||
| 4836 | return $coaches; |
||
| 4837 | } |
||
| 4838 | |||
| 4839 | /** |
||
| 4840 | * @param int $sessionId |
||
| 4841 | * @param int $courseId |
||
| 4842 | * @return string |
||
| 4843 | */ |
||
| 4844 | public static function getCoachesByCourseSessionToString( |
||
| 4845 | $sessionId, |
||
| 4846 | $courseId |
||
| 4847 | ) { |
||
| 4848 | $coaches = self::getCoachesByCourseSession($sessionId, $courseId); |
||
| 4849 | $list = array(); |
||
| 4850 | View Code Duplication | if (!empty($coaches)) { |
|
| 4851 | foreach ($coaches as $coachId) { |
||
| 4852 | $userInfo = api_get_user_info($coachId); |
||
| 4853 | $list[] = api_get_person_name( |
||
| 4854 | $userInfo['firstname'], |
||
| 4855 | $userInfo['lastname'] |
||
| 4856 | ); |
||
| 4857 | } |
||
| 4858 | } |
||
| 4859 | |||
| 4860 | return array_to_string($list, CourseManager::USER_SEPARATOR); |
||
| 4861 | } |
||
| 4862 | |||
| 4863 | /** |
||
| 4864 | * Get all coaches added in the session - course relationship |
||
| 4865 | * @param int $sessionId |
||
| 4866 | * @return array |
||
| 4867 | */ |
||
| 4868 | public static function getCoachesBySession($sessionId) |
||
| 4869 | { |
||
| 4870 | $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4871 | $sessionId = intval($sessionId); |
||
| 4872 | |||
| 4873 | $sql = "SELECT DISTINCT user_id |
||
| 4874 | FROM $table |
||
| 4875 | WHERE session_id = '$sessionId' AND status = 2"; |
||
| 4876 | $result = Database::query($sql); |
||
| 4877 | |||
| 4878 | $coaches = array(); |
||
| 4879 | if (Database::num_rows($result) > 0) { |
||
| 4880 | while ($row = Database::fetch_array($result)) { |
||
| 4881 | $coaches[] = $row['user_id']; |
||
| 4882 | } |
||
| 4883 | } |
||
| 4884 | |||
| 4885 | return $coaches; |
||
| 4886 | } |
||
| 4887 | |||
| 4888 | /** |
||
| 4889 | * @param int $userId |
||
| 4890 | * @return array |
||
| 4891 | */ |
||
| 4892 | public static function getAllCoursesFromAllSessionFromDrh($userId) |
||
| 4893 | { |
||
| 4894 | $sessions = SessionManager::get_sessions_followed_by_drh($userId); |
||
| 4895 | $coursesFromSession = array(); |
||
| 4896 | if (!empty($sessions)) { |
||
| 4897 | foreach ($sessions as $session) { |
||
| 4898 | $courseList = SessionManager::get_course_list_by_session_id($session['id']); |
||
| 4899 | foreach ($courseList as $course) { |
||
| 4900 | $coursesFromSession[] = $course['code']; |
||
| 4901 | } |
||
| 4902 | } |
||
| 4903 | } |
||
| 4904 | return $coursesFromSession; |
||
| 4905 | } |
||
| 4906 | |||
| 4907 | /** |
||
| 4908 | * @param string $status |
||
| 4909 | * @param int $userId |
||
| 4910 | * @param bool $getCount |
||
| 4911 | * @param int $from |
||
| 4912 | * @param int $numberItems |
||
| 4913 | * @param int $column |
||
| 4914 | * @param string $direction |
||
| 4915 | * @param string $keyword |
||
| 4916 | * @param string $active |
||
| 4917 | * @param string $lastConnectionDate |
||
| 4918 | * @param array $sessionIdList |
||
| 4919 | * @param array $studentIdList |
||
| 4920 | * @param int $filterByStatus |
||
| 4921 | * @return array|int |
||
| 4922 | */ |
||
| 4923 | public static function getAllUsersFromCoursesFromAllSessionFromStatus( |
||
| 4924 | $status, |
||
| 4925 | $userId, |
||
| 4926 | $getCount = false, |
||
| 4927 | $from = null, |
||
| 4928 | $numberItems = null, |
||
| 4929 | $column = 1, |
||
| 4930 | $direction = 'asc', |
||
| 4931 | $keyword = null, |
||
| 4932 | $active = null, |
||
| 4933 | $lastConnectionDate = null, |
||
| 4934 | $sessionIdList = array(), |
||
| 4935 | $studentIdList = array(), |
||
| 4936 | $filterByStatus = null |
||
| 4937 | ) { |
||
| 4938 | $filterByStatus = intval($filterByStatus); |
||
| 4939 | |||
| 4940 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 4941 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 4942 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 4943 | $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 4944 | $tbl_course_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE); |
||
| 4945 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4946 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 4947 | |||
| 4948 | $direction = in_array(strtolower($direction), array('asc', 'desc')) ? $direction : 'asc'; |
||
| 4949 | $column = Database::escape_string($column); |
||
| 4950 | $userId = intval($userId); |
||
| 4951 | |||
| 4952 | $limitCondition = null; |
||
| 4953 | |||
| 4954 | View Code Duplication | if (isset($from) && isset($numberItems)) { |
|
| 4955 | $from = intval($from); |
||
| 4956 | $numberItems = intval($numberItems); |
||
| 4957 | $limitCondition = "LIMIT $from, $numberItems"; |
||
| 4958 | } |
||
| 4959 | |||
| 4960 | $urlId = api_get_current_access_url_id(); |
||
| 4961 | |||
| 4962 | $sessionConditions = null; |
||
| 4963 | $courseConditions = null; |
||
| 4964 | $userConditions = null; |
||
| 4965 | |||
| 4966 | if (isset($active)) { |
||
| 4967 | $active = intval($active); |
||
| 4968 | $userConditions .= " AND active = $active"; |
||
| 4969 | } |
||
| 4970 | |||
| 4971 | switch ($status) { |
||
| 4972 | case 'drh': |
||
| 4973 | // Classic DRH |
||
| 4974 | if (empty($studentIdList)) { |
||
| 4975 | $studentListSql = UserManager::get_users_followed_by_drh( |
||
| 4976 | $userId, |
||
| 4977 | $filterByStatus, |
||
| 4978 | true, |
||
| 4979 | false |
||
| 4980 | ); |
||
| 4981 | $studentIdList = array_keys($studentListSql); |
||
| 4982 | $studentListSql = "'".implode("','", $studentIdList)."'"; |
||
| 4983 | } else { |
||
| 4984 | $studentIdList = array_map('intval', $studentIdList); |
||
| 4985 | $studentListSql = "'".implode("','", $studentIdList)."'"; |
||
| 4986 | } |
||
| 4987 | if (!empty($studentListSql)) { |
||
| 4988 | $userConditions = " AND u.user_id IN (".$studentListSql.") "; |
||
| 4989 | } |
||
| 4990 | break; |
||
| 4991 | case 'drh_all': |
||
| 4992 | // Show all by DRH |
||
| 4993 | if (empty($sessionIdList)) { |
||
| 4994 | $sessionsListSql = SessionManager::get_sessions_followed_by_drh( |
||
| 4995 | $userId, |
||
| 4996 | null, |
||
| 4997 | null, |
||
| 4998 | false, |
||
| 4999 | true, |
||
| 5000 | true |
||
| 5001 | ); |
||
| 5002 | } else { |
||
| 5003 | $sessionIdList = array_map('intval', $sessionIdList); |
||
| 5004 | $sessionsListSql = "'".implode("','", $sessionIdList)."'"; |
||
| 5005 | } |
||
| 5006 | if (!empty($sessionsListSql)) { |
||
| 5007 | $sessionConditions = " AND s.id IN (".$sessionsListSql.") "; |
||
| 5008 | } |
||
| 5009 | break; |
||
| 5010 | case 'session_admin'; |
||
| 5011 | $sessionConditions = " AND s.id_coach = $userId "; |
||
| 5012 | break; |
||
| 5013 | case 'admin': |
||
| 5014 | break; |
||
| 5015 | case 'teacher': |
||
| 5016 | $sessionConditions = " AND s.id_coach = $userId "; |
||
| 5017 | break; |
||
| 5018 | } |
||
| 5019 | |||
| 5020 | $select = "SELECT DISTINCT u.* "; |
||
| 5021 | $masterSelect = "SELECT DISTINCT * FROM "; |
||
| 5022 | |||
| 5023 | if ($getCount) { |
||
| 5024 | $select = "SELECT DISTINCT u.user_id "; |
||
| 5025 | $masterSelect = "SELECT COUNT(DISTINCT(user_id)) as count FROM "; |
||
| 5026 | } |
||
| 5027 | |||
| 5028 | if (!empty($filterByStatus)) { |
||
| 5029 | $userConditions .= " AND u.status = ".$filterByStatus; |
||
| 5030 | } |
||
| 5031 | |||
| 5032 | if (!empty($lastConnectionDate)) { |
||
| 5033 | $lastConnectionDate = Database::escape_string($lastConnectionDate); |
||
| 5034 | $userConditions .= " AND u.last_login <= '$lastConnectionDate' "; |
||
| 5035 | } |
||
| 5036 | |||
| 5037 | View Code Duplication | if (!empty($keyword)) { |
|
| 5038 | $keyword = Database::escape_string($keyword); |
||
| 5039 | $userConditions .= " AND ( |
||
| 5040 | u.username LIKE '%$keyword%' OR |
||
| 5041 | u.firstname LIKE '%$keyword%' OR |
||
| 5042 | u.lastname LIKE '%$keyword%' OR |
||
| 5043 | u.official_code LIKE '%$keyword%' OR |
||
| 5044 | u.email LIKE '%$keyword%' |
||
| 5045 | )"; |
||
| 5046 | } |
||
| 5047 | |||
| 5048 | $where = " WHERE |
||
| 5049 | access_url_id = $urlId |
||
| 5050 | $userConditions |
||
| 5051 | "; |
||
| 5052 | |||
| 5053 | $sql = "$masterSelect ( |
||
| 5054 | ($select |
||
| 5055 | FROM $tbl_session s |
||
| 5056 | INNER JOIN $tbl_session_rel_course_rel_user su ON (s.id = su.session_id) |
||
| 5057 | INNER JOIN $tbl_user u ON (u.user_id = su.user_id) |
||
| 5058 | INNER JOIN $tbl_session_rel_access_url url ON (url.session_id = s.id) |
||
| 5059 | $where |
||
| 5060 | $sessionConditions |
||
| 5061 | ) |
||
| 5062 | UNION ( |
||
| 5063 | $select |
||
| 5064 | FROM $tbl_course c |
||
| 5065 | INNER JOIN $tbl_course_user cu ON (cu.c_id = c.id) |
||
| 5066 | INNER JOIN $tbl_user u ON (u.user_id = cu.user_id) |
||
| 5067 | INNER JOIN $tbl_course_rel_access_url url ON (url.c_id = c.id) |
||
| 5068 | $where |
||
| 5069 | $courseConditions |
||
| 5070 | ) |
||
| 5071 | ) as t1 |
||
| 5072 | "; |
||
| 5073 | |||
| 5074 | if ($getCount) { |
||
| 5075 | $result = Database::query($sql); |
||
| 5076 | $count = 0; |
||
| 5077 | if (Database::num_rows($result)) { |
||
| 5078 | $rows = Database::fetch_array($result); |
||
| 5079 | $count = $rows['count']; |
||
| 5080 | } |
||
| 5081 | return $count; |
||
| 5082 | } |
||
| 5083 | |||
| 5084 | View Code Duplication | if (!empty($column) && !empty($direction)) { |
|
| 5085 | $column = str_replace('u.', '', $column); |
||
| 5086 | $sql .= " ORDER BY $column $direction "; |
||
| 5087 | } |
||
| 5088 | |||
| 5089 | $sql .= $limitCondition; |
||
| 5090 | $result = Database::query($sql); |
||
| 5091 | $result = Database::store_result($result); |
||
| 5092 | |||
| 5093 | return $result ; |
||
| 5094 | } |
||
| 5095 | |||
| 5096 | /** |
||
| 5097 | * @param int $sessionId |
||
| 5098 | * @param int $courseId |
||
| 5099 | * @param array $coachList |
||
| 5100 | * @param bool $deleteCoachesNotInList |
||
| 5101 | */ |
||
| 5102 | public static function updateCoaches( |
||
| 5103 | $sessionId, |
||
| 5104 | $courseId, |
||
| 5105 | $coachList, |
||
| 5106 | $deleteCoachesNotInList = false |
||
| 5107 | ) { |
||
| 5108 | $currentCoaches = self::getCoachesByCourseSession($sessionId, $courseId); |
||
| 5109 | |||
| 5110 | if (!empty($coachList)) { |
||
| 5111 | foreach ($coachList as $userId) { |
||
| 5112 | self::set_coach_to_course_session($userId, $sessionId, $courseId); |
||
| 5113 | } |
||
| 5114 | } |
||
| 5115 | |||
| 5116 | if ($deleteCoachesNotInList) { |
||
| 5117 | if (!empty($coachList)) { |
||
| 5118 | $coachesToDelete = array_diff($currentCoaches, $coachList); |
||
| 5119 | } else { |
||
| 5120 | $coachesToDelete = $currentCoaches; |
||
| 5121 | } |
||
| 5122 | |||
| 5123 | if (!empty($coachesToDelete)) { |
||
| 5124 | foreach ($coachesToDelete as $userId) { |
||
| 5125 | self::set_coach_to_course_session( |
||
| 5126 | $userId, |
||
| 5127 | $sessionId, |
||
| 5128 | $courseId, |
||
| 5129 | true |
||
| 5130 | ); |
||
| 5131 | } |
||
| 5132 | } |
||
| 5133 | } |
||
| 5134 | } |
||
| 5135 | |||
| 5136 | /** |
||
| 5137 | * @param array $sessions |
||
| 5138 | * @param array $sessionsDestination |
||
| 5139 | * @return string |
||
| 5140 | */ |
||
| 5141 | public static function copyStudentsFromSession($sessions, $sessionsDestination) |
||
| 5142 | { |
||
| 5143 | $messages = array(); |
||
| 5144 | if (!empty($sessions)) { |
||
| 5145 | foreach ($sessions as $sessionId) { |
||
| 5146 | $sessionInfo = self::fetch($sessionId); |
||
| 5147 | $userList = self::get_users_by_session($sessionId, 0); |
||
| 5148 | if (!empty($userList)) { |
||
| 5149 | $newUserList = array(); |
||
| 5150 | $userToString = null; |
||
| 5151 | foreach ($userList as $userInfo) { |
||
| 5152 | $newUserList[] = $userInfo['user_id']; |
||
| 5153 | $userToString .= $userInfo['firstname'] . ' ' . $userInfo['lastname'] . '<br />'; |
||
| 5154 | } |
||
| 5155 | |||
| 5156 | if (!empty($sessionsDestination)) { |
||
| 5157 | foreach ($sessionsDestination as $sessionDestinationId) { |
||
| 5158 | $sessionDestinationInfo = self::fetch($sessionDestinationId); |
||
| 5159 | $messages[] = Display::return_message( |
||
| 5160 | sprintf(get_lang('AddingStudentsFromSessionXToSessionY'), $sessionInfo['name'], $sessionDestinationInfo['name']), 'info', false |
||
| 5161 | ); |
||
| 5162 | if ($sessionId == $sessionDestinationId) { |
||
| 5163 | $messages[] = Display::return_message(sprintf(get_lang('SessionXSkipped'), $sessionDestinationId), 'warning', false); |
||
| 5164 | continue; |
||
| 5165 | } |
||
| 5166 | $messages[] = Display::return_message(get_lang('StudentList') . '<br />' . $userToString, 'info', false); |
||
| 5167 | SessionManager::suscribe_users_to_session( |
||
| 5168 | $sessionDestinationId, |
||
| 5169 | $newUserList, |
||
| 5170 | SESSION_VISIBLE_READ_ONLY, |
||
| 5171 | false |
||
| 5172 | ); |
||
| 5173 | } |
||
| 5174 | } else { |
||
| 5175 | $messages[] = Display::return_message(get_lang('NoDestinationSessionProvided'), 'warning'); |
||
| 5176 | } |
||
| 5177 | } else { |
||
| 5178 | $messages[] = Display::return_message( |
||
| 5179 | get_lang('NoStudentsFoundForSession').' #'.$sessionInfo['name'], |
||
| 5180 | 'warning' |
||
| 5181 | ); |
||
| 5182 | } |
||
| 5183 | } |
||
| 5184 | } else { |
||
| 5185 | $messages[] = Display::return_message(get_lang('NoData'), 'warning'); |
||
| 5186 | } |
||
| 5187 | return $messages; |
||
| 5188 | } |
||
| 5189 | |||
| 5190 | /** |
||
| 5191 | * Assign coaches of a session(s) as teachers to a given course (or courses) |
||
| 5192 | * @param array A list of session IDs |
||
| 5193 | * @param array A list of course IDs |
||
| 5194 | * @return string |
||
| 5195 | */ |
||
| 5196 | public static function copyCoachesFromSessionToCourse($sessions, $courses) |
||
| 5197 | { |
||
| 5198 | $coachesPerSession = array(); |
||
| 5199 | foreach ($sessions as $sessionId) { |
||
| 5200 | $coaches = self::getCoachesBySession($sessionId); |
||
| 5201 | $coachesPerSession[$sessionId] = $coaches; |
||
| 5202 | } |
||
| 5203 | |||
| 5204 | $result = array(); |
||
| 5205 | |||
| 5206 | if (!empty($courses)) { |
||
| 5207 | foreach ($courses as $courseId) { |
||
| 5208 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 5209 | foreach ($coachesPerSession as $sessionId => $coachList) { |
||
| 5210 | CourseManager::updateTeachers( |
||
| 5211 | $courseInfo['real_id'], $coachList, false, false, false |
||
| 5212 | ); |
||
| 5213 | $result[$courseInfo['code']][$sessionId] = $coachList; |
||
| 5214 | } |
||
| 5215 | } |
||
| 5216 | } |
||
| 5217 | $sessionUrl = api_get_path(WEB_CODE_PATH) . 'admin/resume_session.php?id_session='; |
||
| 5218 | |||
| 5219 | $htmlResult = null; |
||
| 5220 | |||
| 5221 | if (!empty($result)) { |
||
| 5222 | foreach ($result as $courseCode => $data) { |
||
| 5223 | $url = api_get_course_url($courseCode); |
||
| 5224 | $htmlResult .= sprintf( |
||
| 5225 | get_lang('CoachesSubscribedAsATeacherInCourseX'), |
||
| 5226 | Display::url($courseCode, $url, array('target' => '_blank')) |
||
| 5227 | ); |
||
| 5228 | foreach ($data as $sessionId => $coachList) { |
||
| 5229 | $sessionInfo = self::fetch($sessionId); |
||
| 5230 | $htmlResult .= '<br />'; |
||
| 5231 | $htmlResult .= Display::url( |
||
| 5232 | get_lang('Session') . ': ' . $sessionInfo['name'] . ' <br />', $sessionUrl . $sessionId, array('target' => '_blank') |
||
| 5233 | ); |
||
| 5234 | $teacherList = array(); |
||
| 5235 | foreach ($coachList as $coachId) { |
||
| 5236 | $userInfo = api_get_user_info($coachId); |
||
| 5237 | $teacherList[] = $userInfo['complete_name']; |
||
| 5238 | } |
||
| 5239 | if (!empty($teacherList)) { |
||
| 5240 | $htmlResult .= implode(', ', $teacherList); |
||
| 5241 | } else { |
||
| 5242 | $htmlResult .= get_lang('NothingToAdd'); |
||
| 5243 | } |
||
| 5244 | } |
||
| 5245 | $htmlResult .= '<br />'; |
||
| 5246 | } |
||
| 5247 | $htmlResult = Display::return_message($htmlResult, 'normal', false); |
||
| 5248 | } |
||
| 5249 | return $htmlResult; |
||
| 5250 | } |
||
| 5251 | |||
| 5252 | /** |
||
| 5253 | * @param string $keyword |
||
| 5254 | * @param string $active |
||
| 5255 | * @param string $lastConnectionDate |
||
| 5256 | * @param array $sessionIdList |
||
| 5257 | * @param array $studentIdList |
||
| 5258 | * @param int $userStatus STUDENT|COURSEMANAGER constants |
||
| 5259 | * |
||
| 5260 | * @return array|int |
||
| 5261 | */ |
||
| 5262 | public static function getCountUserTracking( |
||
| 5263 | $keyword = null, |
||
| 5264 | $active = null, |
||
| 5265 | $lastConnectionDate = null, |
||
| 5266 | $sessionIdList = array(), |
||
| 5267 | $studentIdList = array(), |
||
| 5268 | $filterUserStatus = null |
||
| 5269 | ) { |
||
| 5270 | $userId = api_get_user_id(); |
||
| 5271 | $drhLoaded = false; |
||
| 5272 | |||
| 5273 | if (api_is_drh()) { |
||
| 5274 | if (api_drh_can_access_all_session_content()) { |
||
| 5275 | $count = self::getAllUsersFromCoursesFromAllSessionFromStatus( |
||
| 5276 | 'drh_all', |
||
| 5277 | $userId, |
||
| 5278 | true, |
||
| 5279 | null, |
||
| 5280 | null, |
||
| 5281 | null, |
||
| 5282 | null, |
||
| 5283 | $keyword, |
||
| 5284 | $active, |
||
| 5285 | $lastConnectionDate, |
||
| 5286 | $sessionIdList, |
||
| 5287 | $studentIdList, |
||
| 5288 | $filterUserStatus |
||
| 5289 | ); |
||
| 5290 | $drhLoaded = true; |
||
| 5291 | } |
||
| 5292 | } |
||
| 5293 | |||
| 5294 | if ($drhLoaded == false) { |
||
| 5295 | $count = UserManager::getUsersFollowedByUser( |
||
| 5296 | $userId, |
||
| 5297 | $filterUserStatus, |
||
| 5298 | false, |
||
| 5299 | false, |
||
| 5300 | true, |
||
| 5301 | null, |
||
| 5302 | null, |
||
| 5303 | null, |
||
| 5304 | null, |
||
| 5305 | $active, |
||
| 5306 | $lastConnectionDate, |
||
| 5307 | api_is_student_boss() ? STUDENT_BOSS : COURSEMANAGER, |
||
| 5308 | $keyword |
||
| 5309 | ); |
||
| 5310 | } |
||
| 5311 | |||
| 5312 | return $count; |
||
| 5313 | } |
||
| 5314 | |||
| 5315 | /** |
||
| 5316 | * Get teachers followed by a user |
||
| 5317 | * @param int $userId |
||
| 5318 | * @param int $active |
||
| 5319 | * @param string $lastConnectionDate |
||
| 5320 | * @param bool $getCount |
||
| 5321 | * @param array $sessionIdList |
||
| 5322 | * @return array|int |
||
| 5323 | */ |
||
| 5324 | public static function getTeacherTracking( |
||
| 5325 | $userId, |
||
| 5326 | $active = 1, |
||
| 5327 | $lastConnectionDate = null, |
||
| 5328 | $getCount = false, |
||
| 5329 | $sessionIdList = array() |
||
| 5330 | ) { |
||
| 5331 | $teacherListId = array(); |
||
| 5332 | |||
| 5333 | if (api_is_drh() || api_is_platform_admin()) { |
||
| 5334 | // Followed teachers by drh |
||
| 5335 | if (api_drh_can_access_all_session_content()) { |
||
| 5336 | if (empty($sessionIdList)) { |
||
| 5337 | $sessions = SessionManager::get_sessions_followed_by_drh($userId); |
||
| 5338 | $sessionIdList = array(); |
||
| 5339 | foreach ($sessions as $session) { |
||
| 5340 | $sessionIdList[] = $session['id']; |
||
| 5341 | } |
||
| 5342 | } |
||
| 5343 | |||
| 5344 | $sessionIdList = array_map('intval', $sessionIdList); |
||
| 5345 | $sessionToString = implode("', '", $sessionIdList); |
||
| 5346 | |||
| 5347 | $course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 5348 | $sessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 5349 | $courseUser = Database::get_main_table(TABLE_MAIN_COURSE_USER); |
||
| 5350 | |||
| 5351 | // Select the teachers. |
||
| 5352 | $sql = "SELECT DISTINCT(cu.user_id) FROM $course c |
||
| 5353 | INNER JOIN $sessionCourse src ON c.id = src.c_id |
||
| 5354 | INNER JOIN $courseUser cu ON (cu.c_id = c.id) |
||
| 5355 | WHERE src.session_id IN ('$sessionToString') AND cu.status = 1"; |
||
| 5356 | $result = Database::query($sql); |
||
| 5357 | while($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 5358 | $teacherListId[$row['user_id']] = $row['user_id']; |
||
| 5359 | } |
||
| 5360 | } else { |
||
| 5361 | $teacherResult = UserManager::get_users_followed_by_drh($userId, COURSEMANAGER); |
||
| 5362 | foreach ($teacherResult as $userInfo) { |
||
| 5363 | $teacherListId[] = $userInfo['user_id']; |
||
| 5364 | } |
||
| 5365 | } |
||
| 5366 | } |
||
| 5367 | |||
| 5368 | if (!empty($teacherListId)) { |
||
| 5369 | $tableUser = Database::get_main_table(TABLE_MAIN_USER); |
||
| 5370 | |||
| 5371 | $select = "SELECT DISTINCT u.* "; |
||
| 5372 | if ($getCount) { |
||
| 5373 | $select = "SELECT count(DISTINCT(u.user_id)) as count"; |
||
| 5374 | } |
||
| 5375 | |||
| 5376 | $sql = "$select FROM $tableUser u"; |
||
| 5377 | |||
| 5378 | if (!empty($lastConnectionDate)) { |
||
| 5379 | $tableLogin = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 5380 | //$sql .= " INNER JOIN $tableLogin l ON (l.login_user_id = u.user_id) "; |
||
| 5381 | } |
||
| 5382 | $active = intval($active); |
||
| 5383 | $teacherListId = implode("','", $teacherListId); |
||
| 5384 | $where = " WHERE u.active = $active AND u.user_id IN ('$teacherListId') "; |
||
| 5385 | |||
| 5386 | if (!empty($lastConnectionDate)) { |
||
| 5387 | $lastConnectionDate = Database::escape_string($lastConnectionDate); |
||
| 5388 | //$where .= " AND l.login_date <= '$lastConnectionDate' "; |
||
| 5389 | } |
||
| 5390 | |||
| 5391 | $sql .= $where; |
||
| 5392 | $result = Database::query($sql); |
||
| 5393 | if (Database::num_rows($result)) { |
||
| 5394 | if ($getCount) { |
||
| 5395 | $row = Database::fetch_array($result); |
||
| 5396 | return $row['count']; |
||
| 5397 | } else { |
||
| 5398 | |||
| 5399 | return Database::store_result($result, 'ASSOC'); |
||
| 5400 | } |
||
| 5401 | } |
||
| 5402 | } |
||
| 5403 | |||
| 5404 | return 0; |
||
| 5405 | } |
||
| 5406 | |||
| 5407 | /** |
||
| 5408 | * Get the list of course tools that have to be dealt with in case of |
||
| 5409 | * registering any course to a session |
||
| 5410 | * @return array The list of tools to be dealt with (literal names) |
||
| 5411 | */ |
||
| 5412 | public static function getCourseToolToBeManaged() |
||
| 5413 | { |
||
| 5414 | return array( |
||
| 5415 | 'courseDescription', |
||
| 5416 | 'courseIntroduction', |
||
| 5417 | ); |
||
| 5418 | } |
||
| 5419 | |||
| 5420 | /** |
||
| 5421 | * Calls the methods bound to each tool when a course is registered into a session |
||
| 5422 | * @param int $sessionId |
||
| 5423 | * @param int $courseId |
||
| 5424 | * @return void |
||
| 5425 | */ |
||
| 5426 | View Code Duplication | public static function installCourse($sessionId, $courseId) |
|
| 5427 | { |
||
| 5428 | return true; |
||
| 5429 | $toolList = self::getCourseToolToBeManaged(); |
||
| 5430 | |||
| 5431 | foreach ($toolList as $tool) { |
||
| 5432 | $method = 'add' . $tool; |
||
| 5433 | if (method_exists(get_class(), $method)) { |
||
| 5434 | self::$method($sessionId, $courseId); |
||
| 5435 | } |
||
| 5436 | } |
||
| 5437 | } |
||
| 5438 | |||
| 5439 | /** |
||
| 5440 | * Calls the methods bound to each tool when a course is unregistered from |
||
| 5441 | * a session |
||
| 5442 | * @param int $sessionId |
||
| 5443 | * @param int $courseId |
||
| 5444 | */ |
||
| 5445 | View Code Duplication | public static function unInstallCourse($sessionId, $courseId) |
|
| 5446 | { |
||
| 5447 | return true; |
||
| 5448 | $toolList = self::getCourseToolToBeManaged(); |
||
| 5449 | |||
| 5450 | foreach ($toolList as $tool) { |
||
| 5451 | $method = 'remove' . $tool; |
||
| 5452 | if (method_exists(get_class(), $method)) { |
||
| 5453 | self::$method($sessionId, $courseId); |
||
| 5454 | } |
||
| 5455 | } |
||
| 5456 | } |
||
| 5457 | |||
| 5458 | /** |
||
| 5459 | * @param int $sessionId |
||
| 5460 | * @param int $courseId |
||
| 5461 | */ |
||
| 5462 | public static function addCourseIntroduction($sessionId, $courseId) |
||
| 5463 | { |
||
| 5464 | // @todo create a tool intro lib |
||
| 5465 | $sessionId = intval($sessionId); |
||
| 5466 | $courseId = intval($courseId); |
||
| 5467 | |||
| 5468 | $TBL_INTRODUCTION = Database::get_course_table(TABLE_TOOL_INTRO); |
||
| 5469 | $sql = "SELECT * FROM $TBL_INTRODUCTION WHERE c_id = $courseId"; |
||
| 5470 | $result = Database::query($sql); |
||
| 5471 | $result = Database::store_result($result, 'ASSOC'); |
||
| 5472 | |||
| 5473 | if (!empty($result)) { |
||
| 5474 | foreach ($result as $result) { |
||
| 5475 | // @todo check if relation exits. |
||
| 5476 | $result['session_id'] = $sessionId; |
||
| 5477 | Database::insert($TBL_INTRODUCTION, $result); |
||
| 5478 | } |
||
| 5479 | } |
||
| 5480 | } |
||
| 5481 | |||
| 5482 | /** |
||
| 5483 | * @param int $sessionId |
||
| 5484 | * @param int $courseId |
||
| 5485 | */ |
||
| 5486 | public static function removeCourseIntroduction($sessionId, $courseId) |
||
| 5487 | { |
||
| 5488 | $sessionId = intval($sessionId); |
||
| 5489 | $courseId = intval($courseId); |
||
| 5490 | $TBL_INTRODUCTION = Database::get_course_table(TABLE_TOOL_INTRO); |
||
| 5491 | $sql = "DELETE FROM $TBL_INTRODUCTION |
||
| 5492 | WHERE c_id = $courseId AND session_id = $sessionId"; |
||
| 5493 | Database::query($sql); |
||
| 5494 | } |
||
| 5495 | |||
| 5496 | /** |
||
| 5497 | * @param int $sessionId |
||
| 5498 | * @param int $courseId |
||
| 5499 | */ |
||
| 5500 | public static function addCourseDescription($sessionId, $courseId) |
||
| 5501 | { |
||
| 5502 | /* $description = new CourseDescription(); |
||
| 5503 | $descriptions = $description->get_descriptions($courseId); |
||
| 5504 | foreach ($descriptions as $description) { |
||
| 5505 | } */ |
||
| 5506 | } |
||
| 5507 | |||
| 5508 | /** |
||
| 5509 | * @param int $sessionId |
||
| 5510 | * @param int $courseId |
||
| 5511 | */ |
||
| 5512 | public static function removeCourseDescription($sessionId, $courseId) |
||
| 5513 | { |
||
| 5514 | |||
| 5515 | } |
||
| 5516 | |||
| 5517 | /** |
||
| 5518 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5519 | * @param bool $sendEmail |
||
| 5520 | * @param bool $removeOldRelationShips |
||
| 5521 | * @return string |
||
| 5522 | */ |
||
| 5523 | public static function subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips) |
||
| 5524 | { |
||
| 5525 | if (!empty($userSessionList)) { |
||
| 5526 | foreach ($userSessionList as $userId => $data) { |
||
| 5527 | $sessionList = array(); |
||
| 5528 | foreach ($data['session_list'] as $sessionInfo) { |
||
| 5529 | $sessionList[] = $sessionInfo['session_id']; |
||
| 5530 | } |
||
| 5531 | $userInfo = $data['user_info']; |
||
| 5532 | self::suscribe_sessions_to_hr_manager( |
||
| 5533 | $userInfo, |
||
| 5534 | $sessionList, |
||
| 5535 | $sendEmail, |
||
| 5536 | $removeOldRelationShips |
||
| 5537 | ); |
||
| 5538 | } |
||
| 5539 | } |
||
| 5540 | } |
||
| 5541 | |||
| 5542 | /** |
||
| 5543 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5544 | * |
||
| 5545 | * @return string |
||
| 5546 | */ |
||
| 5547 | public static function checkSubscribeDrhToSessionList($userSessionList) |
||
| 5548 | { |
||
| 5549 | $message = null; |
||
| 5550 | if (!empty($userSessionList)) { |
||
| 5551 | if (!empty($userSessionList)) { |
||
| 5552 | foreach ($userSessionList as $userId => $data) { |
||
| 5553 | $userInfo = $data['user_info']; |
||
| 5554 | |||
| 5555 | $sessionListSubscribed = self::get_sessions_followed_by_drh($userId); |
||
| 5556 | if (!empty($sessionListSubscribed)) { |
||
| 5557 | $sessionListSubscribed = array_keys($sessionListSubscribed); |
||
| 5558 | } |
||
| 5559 | |||
| 5560 | $sessionList = array(); |
||
| 5561 | if (!empty($data['session_list'])) { |
||
| 5562 | foreach ($data['session_list'] as $sessionInfo) { |
||
| 5563 | if (in_array($sessionInfo['session_id'], $sessionListSubscribed)) { |
||
| 5564 | $sessionList[] = $sessionInfo['session_info']['name']; |
||
| 5565 | } |
||
| 5566 | } |
||
| 5567 | } |
||
| 5568 | |||
| 5569 | $message .= '<strong>' . get_lang('User') . '</strong> ' . $userInfo['complete_name'] . ' <br />'; |
||
| 5570 | |||
| 5571 | if (!in_array($userInfo['status'], array(DRH)) && !api_is_platform_admin_by_id($userInfo['user_id'])) { |
||
| 5572 | $message .= get_lang('UserMustHaveTheDrhRole') . '<br />'; |
||
| 5573 | continue; |
||
| 5574 | } |
||
| 5575 | |||
| 5576 | if (!empty($sessionList)) { |
||
| 5577 | $message .= '<strong>' . get_lang('Sessions') . ':</strong> <br />'; |
||
| 5578 | $message .= implode(', ', $sessionList) . '<br /><br />'; |
||
| 5579 | } else { |
||
| 5580 | $message .= get_lang('NoSessionProvided') . ' <br /><br />'; |
||
| 5581 | } |
||
| 5582 | } |
||
| 5583 | } |
||
| 5584 | } |
||
| 5585 | |||
| 5586 | return $message; |
||
| 5587 | } |
||
| 5588 | |||
| 5589 | /** |
||
| 5590 | * @param string $file |
||
| 5591 | * @param bool $sendEmail |
||
| 5592 | * @param bool $removeOldRelationShips |
||
| 5593 | * |
||
| 5594 | * @return string |
||
| 5595 | */ |
||
| 5596 | public static function importSessionDrhCSV($file, $sendEmail, $removeOldRelationShips) |
||
| 5597 | { |
||
| 5598 | $list = Import::csv_reader($file); |
||
| 5599 | |||
| 5600 | if (!empty($list)) { |
||
| 5601 | $userSessionList = array(); |
||
| 5602 | foreach ($list as $data) { |
||
| 5603 | $userInfo = api_get_user_info_from_username($data['Username']); |
||
| 5604 | $sessionInfo = self::get_session_by_name($data['SessionName']); |
||
| 5605 | |||
| 5606 | if (!empty($userInfo) && !empty($sessionInfo)) { |
||
| 5607 | $userSessionList[$userInfo['user_id']]['session_list'][] = array( |
||
| 5608 | 'session_id' => $sessionInfo['id'], |
||
| 5609 | 'session_info' => $sessionInfo, |
||
| 5610 | ); |
||
| 5611 | $userSessionList[$userInfo['user_id']]['user_info'] = $userInfo; |
||
| 5612 | } |
||
| 5613 | } |
||
| 5614 | |||
| 5615 | self::subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips); |
||
| 5616 | return self::checkSubscribeDrhToSessionList($userSessionList); |
||
| 5617 | } |
||
| 5618 | } |
||
| 5619 | |||
| 5620 | /** |
||
| 5621 | * Courses re-ordering in resume_session.php flag see BT#8316 |
||
| 5622 | */ |
||
| 5623 | public static function orderCourseIsEnabled() |
||
| 5624 | { |
||
| 5625 | $sessionCourseOrder = api_get_setting('session_course_ordering'); |
||
| 5626 | if ($sessionCourseOrder === 'true') { |
||
| 5627 | return true; |
||
| 5628 | } |
||
| 5629 | |||
| 5630 | return false; |
||
| 5631 | } |
||
| 5632 | |||
| 5633 | /** |
||
| 5634 | * @param string $direction (up/down) |
||
| 5635 | * @param int $sessionId |
||
| 5636 | * @param int $courseId |
||
| 5637 | * @return bool |
||
| 5638 | */ |
||
| 5639 | public static function move($direction, $sessionId, $courseId) |
||
| 5702 | |||
| 5703 | /** |
||
| 5704 | * @param int $sessionId |
||
| 5705 | * @param int $courseId |
||
| 5706 | * @return bool |
||
| 5707 | */ |
||
| 5708 | public static function moveUp($sessionId, $courseId) |
||
| 5712 | |||
| 5713 | /** |
||
| 5714 | * @param int $sessionId |
||
| 5715 | * @param string $courseCode |
||
| 5716 | * @return bool |
||
| 5717 | */ |
||
| 5718 | public static function moveDown($sessionId, $courseCode) |
||
| 5722 | |||
| 5723 | /** |
||
| 5724 | * Use the session duration to allow/block user access see BT#8317 |
||
| 5725 | * Needs these DB changes |
||
| 5726 | * ALTER TABLE session ADD COLUMN duration int; |
||
| 5727 | * ALTER TABLE session_rel_user ADD COLUMN duration int; |
||
| 5728 | */ |
||
| 5729 | public static function durationPerUserIsEnabled() |
||
| 5733 | |||
| 5734 | /** |
||
| 5735 | * Returns the number of days the student has left in a session when using |
||
| 5736 | * sessions durations |
||
| 5737 | * @param int $userId |
||
| 5738 | * @param int $sessionId |
||
| 5739 | * @param int $duration in days |
||
| 5740 | * @return int |
||
| 5741 | */ |
||
| 5742 | public static function getDayLeftInSession($sessionId, $userId, $duration) |
||
| 5765 | |||
| 5766 | /** |
||
| 5767 | * @param int $duration |
||
| 5768 | * @param int $userId |
||
| 5769 | * @param int $sessionId |
||
| 5770 | */ |
||
| 5771 | public static function editUserSessionDuration($duration, $userId, $sessionId) |
||
| 5786 | |||
| 5787 | /** |
||
| 5788 | * Gets one row from the session_rel_user table |
||
| 5789 | * @param int $userId |
||
| 5790 | * @param int $sessionId |
||
| 5791 | * |
||
| 5792 | * @return array |
||
| 5793 | */ |
||
| 5794 | public static function getUserSession($userId, $sessionId) |
||
| 5814 | |||
| 5815 | /** |
||
| 5816 | * Check if user is subscribed inside a session as student |
||
| 5817 | * @param int $sessionId The session id |
||
| 5818 | * @param int $userId The user id |
||
| 5819 | * @return boolean Whether is subscribed |
||
| 5820 | */ |
||
| 5821 | public static function isUserSubscribedAsStudent($sessionId, $userId) |
||
| 5844 | |||
| 5845 | /** |
||
| 5846 | * Get the session coached by a user (general coach and course-session coach) |
||
| 5847 | * @param int $coachId The coach id |
||
| 5848 | * @param boolean $checkSessionRelUserVisibility Check the session visibility |
||
| 5849 | * @param boolean $asPlatformAdmin The user is a platform admin and we want all sessions |
||
| 5850 | * @return array The session list |
||
| 5851 | */ |
||
| 5852 | public static function getSessionsCoachedByUser($coachId, $checkSessionRelUserVisibility = false, $asPlatformAdmin = false) |
||
| 5897 | |||
| 5898 | /** |
||
| 5899 | * Check if the course belongs to the session |
||
| 5900 | * @param int $sessionId The session id |
||
| 5901 | * @param string $courseCode The course code |
||
| 5902 | * |
||
| 5903 | * @return bool |
||
| 5904 | */ |
||
| 5905 | public static function sessionHasCourse($sessionId, $courseCode) |
||
| 5932 | |||
| 5933 | /** |
||
| 5934 | * Get the list of course coaches |
||
| 5935 | * @return array The list |
||
| 5936 | */ |
||
| 5937 | public static function getAllCourseCoaches() |
||
| 5975 | |||
| 5976 | /** |
||
| 5977 | * Calculate the total user time in the platform |
||
| 5978 | * @param int $userId The user id |
||
| 5979 | * @param string $from Optional. From date |
||
| 5980 | * @param string $until Optional. Until date |
||
| 5981 | * @return string The time (hh:mm:ss) |
||
| 5982 | */ |
||
| 5983 | public static function getTotalUserTimeInPlatform($userId, $from = '', $until = '') |
||
| 6012 | |||
| 6013 | /** |
||
| 6014 | * Get the courses list by a course coach |
||
| 6015 | * @param int $coachId The coach id |
||
| 6016 | * @return array (id, user_id, session_id, c_id, visibility, status, legal_agreement) |
||
| 6017 | */ |
||
| 6018 | public static function getCoursesListByCourseCoach($coachId) |
||
| 6030 | |||
| 6031 | /** |
||
| 6032 | * Get the count of user courses in session |
||
| 6033 | * @param int $sessionId The session id |
||
| 6034 | * @return array |
||
| 6035 | */ |
||
| 6036 | public static function getTotalUserCoursesInSession($sessionId) |
||
| 6055 | |||
| 6056 | |||
| 6057 | /** |
||
| 6058 | * Returns list of a few data from session (name, short description, start |
||
| 6059 | * date, end date) and the given extra fields if defined based on a |
||
| 6060 | * session category Id. |
||
| 6061 | * @param int $categoryId The internal ID of the session category |
||
| 6062 | * @param string $target Value to search for in the session field values |
||
| 6063 | * @param array $extraFields A list of fields to be scanned and returned |
||
| 6064 | * @return mixed |
||
| 6065 | */ |
||
| 6066 | public static function getShortSessionListAndExtraByCategory($categoryId, $target, $extraFields = null, $publicationDate = null) |
||
| 6160 | |||
| 6161 | /** |
||
| 6162 | * Return the Session Category id searched by name |
||
| 6163 | * @param string $categoryName Name attribute of session category used for search query |
||
| 6164 | * @param bool $force boolean used to get even if something is wrong (e.g not unique name) |
||
| 6165 | * @return int|array If success, return category id (int), else it will return an array |
||
| 6166 | * with the next structure: |
||
| 6167 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6168 | */ |
||
| 6169 | public static function getSessionCategoryIdByName($categoryName, $force = false) |
||
| 6206 | |||
| 6207 | /** |
||
| 6208 | * Return all data from sessions (plus extra field, course and coach data) by category id |
||
| 6209 | * @param int $sessionCategoryId session category id used to search sessions |
||
| 6210 | * @return array If success, return session list and more session related data, else it will return an array |
||
| 6211 | * with the next structure: |
||
| 6212 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6213 | */ |
||
| 6214 | public static function getSessionListAndExtraByCategoryId($sessionCategoryId) |
||
| 6342 | |||
| 6343 | /** |
||
| 6344 | * Return session description from session id |
||
| 6345 | * @param int $sessionId |
||
| 6346 | * @return string |
||
| 6347 | */ |
||
| 6348 | public static function getDescriptionFromSessionId($sessionId) |
||
| 6375 | |||
| 6376 | /** |
||
| 6377 | * Get a session list filtered by name, description or any of the given extra fields |
||
| 6378 | * @param string $term The term to search |
||
| 6379 | * @param array $extraFieldsToInclude Extra fields to include in the session data |
||
| 6380 | * @return array The list |
||
| 6381 | */ |
||
| 6382 | public static function searchSession($term, $extraFieldsToInclude = array()) |
||
| 6420 | |||
| 6421 | /** |
||
| 6422 | * @param $sessionId |
||
| 6423 | * @param array $extraFieldsToInclude |
||
| 6424 | * @return array |
||
| 6425 | */ |
||
| 6426 | public static function getFilteredExtraFields($sessionId, $extraFieldsToInclude = array()) |
||
| 6480 | |||
| 6481 | /** |
||
| 6482 | * @param int $sessionId |
||
| 6483 | * |
||
| 6484 | * @return bool |
||
| 6485 | */ |
||
| 6486 | public static function isValidId($sessionId) |
||
| 6503 | |||
| 6504 | /** |
||
| 6505 | * Get list of sessions based on users of a group for a group admin |
||
| 6506 | * @param int $userId The user id |
||
| 6507 | * @return array |
||
| 6508 | */ |
||
| 6509 | View Code Duplication | public static function getSessionsFollowedForGroupAdmin($userId) |
|
| 6555 | |||
| 6556 | /** |
||
| 6557 | * @param array $sessionInfo |
||
| 6558 | * @return string |
||
| 6559 | */ |
||
| 6560 | public static function getSessionVisibility($sessionInfo) |
||
| 6571 | |||
| 6572 | /** |
||
| 6573 | * Converts "start date" and "end date" to "From start date to end date" string |
||
| 6574 | * @param string $startDate |
||
| 6575 | * @param string $endDate |
||
| 6576 | * |
||
| 6577 | * @return string |
||
| 6578 | */ |
||
| 6579 | private static function convertSessionDateToString($startDate, $endDate) |
||
| 6609 | |||
| 6610 | /** |
||
| 6611 | * Returns a human readable string |
||
| 6612 | * @params array $sessionInfo An array with all the session dates |
||
| 6613 | * @return string |
||
| 6614 | */ |
||
| 6615 | public static function parseSessionDates($sessionInfo) |
||
| 6640 | |||
| 6641 | /** |
||
| 6642 | * @param FormValidator $form |
||
| 6643 | * |
||
| 6644 | * @return array |
||
| 6645 | */ |
||
| 6646 | public static function setForm(FormValidator & $form, $sessionId = 0) |
||
| 6647 | { |
||
| 6648 | $categoriesList = SessionManager::get_all_session_category(); |
||
| 6649 | $userInfo = api_get_user_info(); |
||
| 6650 | |||
| 6651 | $categoriesOptions = array( |
||
| 6652 | '0' => get_lang('None'), |
||
| 6653 | ); |
||
| 6654 | |||
| 6655 | if ($categoriesList != false) { |
||
| 6656 | foreach ($categoriesList as $categoryItem) { |
||
| 6657 | $categoriesOptions[$categoryItem['id']] = $categoryItem['name']; |
||
| 6658 | } |
||
| 6659 | } |
||
| 6660 | |||
| 6661 | // Database Table Definitions |
||
| 6662 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 6663 | |||
| 6664 | $form->addElement('text', 'name', get_lang('SessionName'), array( |
||
| 6665 | 'maxlength' => 50, |
||
| 6666 | )); |
||
| 6667 | $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 6668 | $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name'); |
||
| 6669 | |||
| 6670 | if (!api_is_platform_admin() && api_is_teacher()) { |
||
| 6671 | $form->addElement( |
||
| 6672 | 'select', |
||
| 6673 | 'coach_username', |
||
| 6674 | get_lang('CoachName'), |
||
| 6675 | [api_get_user_id() => $userInfo['complete_name']], |
||
| 6676 | array( |
||
| 6677 | 'id' => 'coach_username', |
||
| 6678 | 'style' => 'width:370px;', |
||
| 6679 | ) |
||
| 6680 | ); |
||
| 6681 | } else { |
||
| 6682 | |||
| 6683 | $sql = "SELECT COUNT(1) FROM $tbl_user WHERE status = 1"; |
||
| 6684 | $rs = Database::query($sql); |
||
| 6685 | $countUsers = Database::result($rs, 0, 0); |
||
| 6686 | |||
| 6687 | if (intval($countUsers) < 50) { |
||
| 6688 | $orderClause = "ORDER BY "; |
||
| 6689 | $orderClause .= api_sort_by_first_name() ? "firstname, lastname, username" : "lastname, firstname, username"; |
||
| 6690 | |||
| 6691 | $sql = "SELECT user_id, lastname, firstname, username |
||
| 6692 | FROM $tbl_user |
||
| 6693 | WHERE status = '1' ". |
||
| 6694 | $orderClause; |
||
| 6695 | |||
| 6696 | View Code Duplication | if (api_is_multiple_url_enabled()) { |
|
| 6697 | $userRelAccessUrlTable = Database::get_main_table( |
||
| 6891 | |||
| 6892 | /** |
||
| 6893 | * Gets the number of rows in the session table filtered through the given |
||
| 6894 | * array of parameters |
||
| 6895 | * @param array Array of options/filters/keys |
||
| 6896 | * @return integer The number of rows, or false on wrong param |
||
| 6897 | * @assert ('a') === false |
||
| 6898 | */ |
||
| 6899 | static function get_count_admin_complete($options = array()) |
||
| 6992 | |||
| 6993 | /** |
||
| 6994 | * @param string $list_type |
||
| 6995 | * @return array |
||
| 6996 | */ |
||
| 6997 | public static function getGridColumns($list_type = 'simple') |
||
| 7072 | |||
| 7073 | /** |
||
| 7074 | * Converts all dates sent through the param array (given form) to correct dates with timezones |
||
| 7075 | * @param array The dates The same array, with times converted |
||
| 7076 | * @param boolean $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions |
||
| 7077 | * @return array The same array, with times converted |
||
| 7078 | */ |
||
| 7079 | static function convert_dates_to_local($params, $applyFormat = false) |
||
| 7121 | |||
| 7122 | /** |
||
| 7123 | * Gets the admin session list callback of the session/session_list.php |
||
| 7124 | * page with all user/details in the right fomat |
||
| 7125 | * @param array |
||
| 7126 | * @result array Array of rows results |
||
| 7127 | * @asset ('a') === false |
||
| 7128 | */ |
||
| 7129 | public static function get_sessions_admin_complete($options = array()) |
||
| 7366 | |||
| 7367 | /** |
||
| 7368 | * Compare two arrays |
||
| 7369 | * @param array $array1 |
||
| 7370 | * @param array $array2 |
||
| 7371 | * |
||
| 7372 | * @return array |
||
| 7373 | */ |
||
| 7374 | static function compareArraysToMerge($array1, $array2) |
||
| 7389 | |||
| 7390 | /** |
||
| 7391 | * Get link to the admin page for this session |
||
| 7392 | * @param int $id Session ID |
||
| 7393 | * @return mixed URL to the admin page to manage the session, or false on error |
||
| 7394 | */ |
||
| 7395 | public static function getAdminPath($id) |
||
| 7404 | |||
| 7405 | /** |
||
| 7406 | * Get link to the user page for this session. |
||
| 7407 | * If a course is provided, build the link to the course |
||
| 7408 | * @param int $id Session ID |
||
| 7409 | * @param int $courseId Course ID (optional) in case the link has to send straight to the course |
||
| 7410 | * @return mixed URL to the page to use the session, or false on error |
||
| 7411 | */ |
||
| 7412 | public static function getPath($id, $courseId = 0) |
||
| 7430 | |||
| 7431 | /** |
||
| 7432 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7433 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7434 | * for course where user is coach |
||
| 7435 | * i.e. coach for the course or |
||
| 7436 | * main coach for a session the course is in |
||
| 7437 | * for a session category (or woth no session category if empty) |
||
| 7438 | * |
||
| 7439 | * @param $userId |
||
| 7440 | * |
||
| 7441 | * @return array |
||
| 7442 | */ |
||
| 7443 | public static function getSessionCourseForUser($userId) |
||
| 7469 | |||
| 7470 | /** |
||
| 7471 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7472 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7473 | * @param $userId |
||
| 7474 | * |
||
| 7475 | * @return array |
||
| 7476 | */ |
||
| 7477 | public static function getCoursesForCourseSessionCoach($userId) |
||
| 7504 | |||
| 7505 | /** |
||
| 7506 | * Return true if coach is allowed to access this session |
||
| 7507 | * @param int $sessionId |
||
| 7508 | * @return bool |
||
| 7509 | */ |
||
| 7510 | public static function isSessionDateOkForCoach($sessionId) |
||
| 7544 | |||
| 7545 | /** |
||
| 7546 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7547 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7548 | * @param $userId |
||
| 7549 | * |
||
| 7550 | * @return array |
||
| 7551 | */ |
||
| 7552 | public static function getCoursesForMainSessionCoach($userId) |
||
| 7577 | |||
| 7578 | /** |
||
| 7579 | * Return an array of course_id used in session $sessionId |
||
| 7580 | * @param $sessionId |
||
| 7581 | * |
||
| 7582 | * @return array |
||
| 7583 | */ |
||
| 7584 | public static function getCoursesInSession($sessionId) |
||
| 7603 | |||
| 7604 | /** |
||
| 7605 | * Return an array of courses in session for user |
||
| 7606 | * and for each courses the list of session that use this course for user |
||
| 7607 | * |
||
| 7608 | * [0] => array |
||
| 7609 | * userCatId |
||
| 7610 | * userCatTitle |
||
| 7611 | * courseInUserCatList |
||
| 7612 | * [0] => array |
||
| 7613 | * courseId |
||
| 7614 | * title |
||
| 7615 | * courseCode |
||
| 7616 | * sessionCatList |
||
| 7617 | * [0] => array |
||
| 7618 | * catSessionId |
||
| 7619 | * catSessionName |
||
| 7620 | * sessionList |
||
| 7621 | * [0] => array |
||
| 7622 | * sessionId |
||
| 7623 | * sessionName |
||
| 7624 | * |
||
| 7625 | * @param $userId |
||
| 7626 | * |
||
| 7627 | * @return array |
||
| 7628 | * |
||
| 7629 | */ |
||
| 7630 | public static function getNamedSessionCourseForCoach($userId) |
||
| 7698 | |||
| 7699 | /** |
||
| 7700 | * @param array $listA |
||
| 7701 | * @param array $listB |
||
| 7702 | * @return int |
||
| 7703 | */ |
||
| 7704 | View Code Duplication | private static function compareCatSessionInfo($listA, $listB) |
|
| 7714 | |||
| 7715 | /** |
||
| 7716 | * @param array $listA |
||
| 7717 | * @param array $listB |
||
| 7718 | * @return int |
||
| 7719 | */ |
||
| 7720 | private static function compareBySessionName($listA, $listB) |
||
| 7734 | |||
| 7735 | /** |
||
| 7736 | * @param array $listA |
||
| 7737 | * @param array $listB |
||
| 7738 | * @return int |
||
| 7739 | */ |
||
| 7740 | View Code Duplication | private static function compareByUserCourseCat($listA, $listB) |
|
| 7750 | |||
| 7751 | /** |
||
| 7752 | * @param array $listA |
||
| 7753 | * @param array $listB |
||
| 7754 | * @return int |
||
| 7755 | */ |
||
| 7756 | View Code Duplication | private static function compareByCourse($listA, $listB) |
|
| 7766 | |||
| 7767 | /** |
||
| 7768 | * Return HTML code for displaying session_course_for_coach |
||
| 7769 | * @param $userId |
||
| 7770 | * @return string |
||
| 7771 | */ |
||
| 7772 | public static function getHtmlNamedSessionCourseForCoach($userId) |
||
| 7838 | } |
||
| 7839 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: