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( |
||
| 1153 | $sessionId, |
||
| 1154 | $courseId, |
||
| 1155 | $studentId = 0, |
||
| 1156 | $profile = '', |
||
| 1157 | $date_from = '', |
||
| 1158 | $date_to = '', |
||
| 1159 | $options |
||
| 1160 | ) { |
||
| 1161 | //escaping variables |
||
| 1162 | $sessionId = intval($sessionId); |
||
| 1163 | $courseId = intval($courseId); |
||
| 1164 | $studentId = intval($studentId); |
||
| 1165 | $profile = intval($profile); |
||
| 1166 | $date_from = Database::escape_string($date_from); |
||
| 1167 | $date_to = Database::escape_string($date_to); |
||
| 1168 | |||
| 1169 | // database table definition |
||
| 1170 | $user = Database :: get_main_table(TABLE_MAIN_USER); |
||
| 1171 | $course = Database :: get_main_table(TABLE_MAIN_COURSE); |
||
| 1172 | $track_e_login = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN); |
||
| 1173 | $track_e_course_access = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_COURSE_ACCESS); |
||
| 1174 | $sessionTable = Database :: get_main_table(TABLE_MAIN_SESSION); |
||
| 1175 | |||
| 1176 | global $export_csv; |
||
| 1177 | if ($export_csv) { |
||
| 1178 | $is_western_name_order = api_is_western_name_order(PERSON_NAME_DATA_EXPORT); |
||
| 1179 | } else { |
||
| 1180 | $is_western_name_order = api_is_western_name_order(); |
||
| 1181 | } |
||
| 1182 | |||
| 1183 | $where = null; |
||
| 1184 | if (isset($sessionId) && !empty($sessionId)) { |
||
| 1185 | $where = sprintf(" WHERE a.session_id = %d", $sessionId); |
||
| 1186 | } |
||
| 1187 | if (isset($courseId) && !empty($courseId)) { |
||
| 1188 | $where .= sprintf(" AND c.id = %d", $courseId); |
||
| 1189 | } |
||
| 1190 | if (isset($studentId) && !empty($studentId)) { |
||
| 1191 | $where .= sprintf(" AND u.user_id = %d", $studentId); |
||
| 1192 | } |
||
| 1193 | if (isset($profile) && !empty($profile)) { |
||
| 1194 | $where .= sprintf(" AND u.status = %d", $profile); |
||
| 1195 | } |
||
| 1196 | if (!empty($date_to) && !empty($date_from)) { |
||
| 1197 | $where .= sprintf( |
||
| 1198 | " AND a.login_course_date >= '%s 00:00:00' |
||
| 1199 | AND a.login_course_date <= '%s 23:59:59'", |
||
| 1200 | $date_from, |
||
| 1201 | $date_to |
||
| 1202 | ); |
||
| 1203 | } |
||
| 1204 | |||
| 1205 | $limit = null; |
||
| 1206 | if (!empty($options['limit'])) { |
||
| 1207 | $limit = " LIMIT " . $options['limit']; |
||
| 1208 | } |
||
| 1209 | |||
| 1210 | if (!empty($options['where'])) { |
||
| 1211 | $where .= ' '.$options['where']; |
||
| 1212 | } |
||
| 1213 | |||
| 1214 | $order = null; |
||
| 1215 | if (!empty($options['order'])) { |
||
| 1216 | $order = " ORDER BY " . $options['order']; |
||
| 1217 | } |
||
| 1218 | |||
| 1219 | //TODO add course name |
||
| 1220 | $sql = "SELECT |
||
| 1221 | a.login_course_date , |
||
| 1222 | u.username , |
||
| 1223 | " . ($is_western_name_order ? " |
||
| 1224 | u.firstname, |
||
| 1225 | u.lastname, |
||
| 1226 | " : " |
||
| 1227 | u.lastname, |
||
| 1228 | u.firstname, |
||
| 1229 | ") . " |
||
| 1230 | a.logout_course_date, |
||
| 1231 | a.counter, |
||
| 1232 | c.title, |
||
| 1233 | c.code, |
||
| 1234 | u.user_id, |
||
| 1235 | a.session_id |
||
| 1236 | FROM $track_e_course_access a |
||
| 1237 | INNER JOIN $user u ON a.user_id = u.user_id |
||
| 1238 | INNER JOIN $course c ON a.c_id = c.id |
||
| 1239 | $where $order $limit"; |
||
| 1240 | $result = Database::query(sprintf($sql, $sessionId, $courseId)); |
||
| 1241 | |||
| 1242 | $data = array(); |
||
| 1243 | while ($user = Database::fetch_assoc($result)) { |
||
| 1244 | $data[] = $user; |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | //foreach |
||
| 1248 | foreach ($data as $key => $info) { |
||
| 1249 | |||
| 1250 | $sql = "SELECT |
||
| 1251 | name |
||
| 1252 | FROM $sessionTable |
||
| 1253 | WHERE |
||
| 1254 | id = {$info['session_id']}"; |
||
| 1255 | $result = Database::query($sql); |
||
| 1256 | $session = Database::fetch_assoc($result); |
||
| 1257 | |||
| 1258 | // building array to display |
||
| 1259 | $return[] = array( |
||
| 1260 | 'user_id' => $info['user_id'], |
||
| 1261 | 'logindate' => $info['login_course_date'], |
||
| 1262 | 'username' => $info['username'], |
||
| 1263 | 'firstname' => $info['firstname'], |
||
| 1264 | 'lastname' => $info['lastname'], |
||
| 1265 | 'clicks' => $info['counter'], //+ $clicks[$info['user_id']], |
||
| 1266 | 'ip' => '', |
||
| 1267 | 'timeLoggedIn' => gmdate("H:i:s", strtotime($info['logout_course_date']) - strtotime($info['login_course_date'])), |
||
| 1268 | 'session' => $session['name'] |
||
| 1269 | ); |
||
| 1270 | } |
||
| 1271 | |||
| 1272 | foreach ($return as $key => $info) { |
||
| 1273 | //Search for ip, we do less querys if we iterate the final array |
||
| 1274 | $sql = sprintf("SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date < '%s' ORDER BY login_date DESC LIMIT 1", $info['user_id'], $info['logindate']); //TODO add select by user too |
||
| 1275 | $result = Database::query($sql); |
||
| 1276 | $ip = Database::fetch_assoc($result); |
||
| 1277 | //if no ip founded, we search the closest higher ip |
||
| 1278 | if (empty($ip['user_ip'])) { |
||
| 1279 | $sql = sprintf("SELECT user_ip FROM $track_e_login WHERE login_user_id = %d AND login_date > '%s' ORDER BY login_date ASC LIMIT 1", $info['user_id'], $info['logindate']); //TODO add select by user too |
||
| 1280 | $result = Database::query($sql); |
||
| 1281 | $ip = Database::fetch_assoc($result); |
||
| 1282 | } |
||
| 1283 | #add ip to final array |
||
| 1284 | $return[$key]['ip'] = $ip['user_ip']; |
||
| 1285 | } |
||
| 1286 | return $return; |
||
| 1287 | } |
||
| 1288 | |||
| 1289 | /** |
||
| 1290 | * Creates a new course code based in given code |
||
| 1291 | * |
||
| 1292 | * @param string $session_name |
||
| 1293 | * <code> |
||
| 1294 | * $wanted_code = 'curse' if there are in the DB codes like curse1 curse2 the function will return: course3 |
||
| 1295 | * if the course code doest not exist in the DB the same course code will be returned |
||
| 1296 | * </code> |
||
| 1297 | * @return string wanted unused code |
||
| 1298 | */ |
||
| 1299 | View Code Duplication | public static function generateNextSessionName($session_name) |
|
| 1300 | { |
||
| 1301 | $session_name_ok = !self::session_name_exists($session_name); |
||
| 1302 | if (!$session_name_ok) { |
||
| 1303 | $table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1304 | $session_name = Database::escape_string($session_name); |
||
| 1305 | $sql = "SELECT count(*) as count FROM $table |
||
| 1306 | WHERE name LIKE '$session_name%'"; |
||
| 1307 | $result = Database::query($sql); |
||
| 1308 | if (Database::num_rows($result) > 0) { |
||
| 1309 | $row = Database::fetch_array($result); |
||
| 1310 | $count = $row['count'] + 1; |
||
| 1311 | $session_name = $session_name . '_' . $count; |
||
| 1312 | $result = self::session_name_exists($session_name); |
||
| 1313 | if (!$result) { |
||
| 1314 | return $session_name; |
||
| 1315 | } |
||
| 1316 | } |
||
| 1317 | return false; |
||
| 1318 | } |
||
| 1319 | return $session_name; |
||
| 1320 | } |
||
| 1321 | |||
| 1322 | /** |
||
| 1323 | * Edit a session |
||
| 1324 | * @author Carlos Vargas from existing code |
||
| 1325 | * @param integer $id Session primary key |
||
| 1326 | * @param string $name |
||
| 1327 | * @param string $startDate |
||
| 1328 | * @param string $endDate |
||
| 1329 | * @param string $displayStartDate |
||
| 1330 | * @param string $displayEndDate |
||
| 1331 | * @param string $coachStartDate |
||
| 1332 | * @param string $coachEndDate |
||
| 1333 | * @param integer $coachId |
||
| 1334 | * @param integer $sessionCategoryId |
||
| 1335 | * @param int $visibility |
||
| 1336 | * @param string $description |
||
| 1337 | * @param bool $showDescription |
||
| 1338 | * @param int $duration |
||
| 1339 | * @param array $extraFields |
||
| 1340 | * @param int $sessionAdminId |
||
| 1341 | * @param boolean $sendSubscriptionNotification Optional. |
||
| 1342 | * Whether send a mail notification to users being subscribed |
||
| 1343 | * @return mixed |
||
| 1344 | */ |
||
| 1345 | public static function edit_session( |
||
| 1346 | $id, |
||
| 1347 | $name, |
||
| 1348 | $startDate, |
||
| 1349 | $endDate, |
||
| 1350 | $displayStartDate, |
||
| 1351 | $displayEndDate, |
||
| 1352 | $coachStartDate, |
||
| 1353 | $coachEndDate, |
||
| 1354 | $coachId, |
||
| 1355 | $sessionCategoryId, |
||
| 1356 | $visibility, |
||
| 1357 | $description = null, |
||
| 1358 | $showDescription = 0, |
||
| 1359 | $duration = null, |
||
| 1360 | $extraFields = array(), |
||
| 1361 | $sessionAdminId = 0, |
||
| 1362 | $sendSubscriptionNotification = false |
||
| 1363 | ) { |
||
| 1364 | $name = trim(stripslashes($name)); |
||
| 1365 | $coachId = intval($coachId); |
||
| 1366 | $sessionCategoryId = intval($sessionCategoryId); |
||
| 1367 | $visibility = intval($visibility); |
||
| 1368 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1369 | |||
| 1370 | if (empty($name)) { |
||
| 1371 | Display::return_message(get_lang('SessionNameIsRequired'), 'warning'); |
||
| 1372 | |||
| 1373 | return false; |
||
| 1374 | } elseif (empty($coachId)) { |
||
| 1375 | Display::return_message(get_lang('CoachIsRequired'), 'warning'); |
||
| 1376 | |||
| 1377 | return false; |
||
| 1378 | } elseif (!empty($startDate) && !api_is_valid_date($startDate, 'Y-m-d H:i')) { |
||
| 1379 | Display::return_message(get_lang('InvalidStartDate'), 'warning'); |
||
| 1380 | |||
| 1381 | return false; |
||
| 1382 | } elseif (!empty($endDate) && !api_is_valid_date($endDate, 'Y-m-d H:i')) { |
||
| 1383 | Display::return_message(get_lang('InvalidEndDate'), 'warning'); |
||
| 1384 | |||
| 1385 | return false; |
||
| 1386 | } elseif (!empty($startDate) && !empty($endDate) && $startDate >= $endDate) { |
||
| 1387 | Display::return_message(get_lang('StartDateShouldBeBeforeEndDate'), 'warning'); |
||
| 1388 | |||
| 1389 | return false; |
||
| 1390 | } else { |
||
| 1391 | $sql = "SELECT id FROM $tbl_session WHERE name='" . Database::escape_string($name) . "'"; |
||
| 1392 | $rs = Database::query($sql); |
||
| 1393 | $exists = false; |
||
| 1394 | while ($row = Database::fetch_array($rs)) { |
||
| 1395 | if ($row['id'] != $id) { |
||
| 1396 | $exists = true; |
||
| 1397 | } |
||
| 1398 | } |
||
| 1399 | |||
| 1400 | if ($exists) { |
||
| 1401 | Display::return_message(get_lang('SessionNameAlreadyExists'), 'warning'); |
||
| 1402 | |||
| 1403 | return false; |
||
| 1404 | } else { |
||
| 1405 | $values = [ |
||
| 1406 | 'name' => $name, |
||
| 1407 | 'duration' => $duration, |
||
| 1408 | 'id_coach' => $coachId, |
||
| 1409 | 'description'=> $description, |
||
| 1410 | 'show_description' => intval($showDescription), |
||
| 1411 | 'visibility' => $visibility, |
||
| 1412 | 'send_subscription_notification' => $sendSubscriptionNotification |
||
| 1413 | ]; |
||
| 1414 | |||
| 1415 | if (!empty($sessionAdminId)) { |
||
| 1416 | $values['session_admin_id'] = $sessionAdminId; |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | if (!empty($startDate)) { |
||
| 1420 | $values['access_start_date'] = api_get_utc_datetime($startDate); |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | if (!empty($endDate)) { |
||
| 1424 | $values['access_end_date'] = api_get_utc_datetime($endDate); |
||
| 1425 | } |
||
| 1426 | |||
| 1427 | if (!empty($displayStartDate)) { |
||
| 1428 | $values['display_start_date'] = api_get_utc_datetime($displayStartDate); |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | if (!empty($displayEndDate)) { |
||
| 1432 | $values['display_end_date'] = api_get_utc_datetime($displayEndDate); |
||
| 1433 | } |
||
| 1434 | |||
| 1435 | if (!empty($coachStartDate)) { |
||
| 1436 | $values['coach_access_start_date'] = api_get_utc_datetime($coachStartDate); |
||
| 1437 | } |
||
| 1438 | if (!empty($coachEndDate)) { |
||
| 1439 | $values['coach_access_end_date'] = api_get_utc_datetime($coachEndDate); |
||
| 1440 | } |
||
| 1441 | |||
| 1442 | if (!empty($sessionCategoryId)) { |
||
| 1443 | $values['session_category_id'] = $sessionCategoryId; |
||
| 1444 | } |
||
| 1445 | |||
| 1446 | Database::update($tbl_session, $values, array( |
||
| 1447 | 'id = ?' => $id |
||
| 1448 | )); |
||
| 1449 | |||
| 1450 | if (!empty($extraFields)) { |
||
| 1451 | $extraFields['item_id'] = $id; |
||
| 1452 | $sessionFieldValue = new ExtraFieldValue('session'); |
||
| 1453 | $sessionFieldValue->saveFieldValues($extraFields); |
||
| 1454 | } |
||
| 1455 | |||
| 1456 | return $id; |
||
| 1457 | } |
||
| 1458 | } |
||
| 1459 | } |
||
| 1460 | |||
| 1461 | /** |
||
| 1462 | * Delete session |
||
| 1463 | * @author Carlos Vargas from existing code |
||
| 1464 | * @param array $id_checked an array to delete sessions |
||
| 1465 | * @param boolean $from_ws optional, true if the function is called |
||
| 1466 | * by a webservice, false otherwise. |
||
| 1467 | * @return void Nothing, or false on error |
||
| 1468 | * */ |
||
| 1469 | public static function delete($id_checked, $from_ws = false) |
||
| 1470 | { |
||
| 1471 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1472 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 1473 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 1474 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 1475 | $tbl_url_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 1476 | $tbl_item_properties = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
| 1477 | $em = Database::getManager(); |
||
| 1478 | |||
| 1479 | $userId = api_get_user_id(); |
||
| 1480 | |||
| 1481 | if (is_array($id_checked)) { |
||
| 1482 | foreach ($id_checked as $sessionId) { |
||
| 1483 | self::delete($sessionId); |
||
| 1484 | } |
||
| 1485 | } else { |
||
| 1486 | $id_checked = intval($id_checked); |
||
| 1487 | } |
||
| 1488 | |||
| 1489 | if (SessionManager::allowed($id_checked) && !$from_ws) { |
||
| 1490 | $qb = $em |
||
| 1491 | ->createQuery(' |
||
| 1492 | SELECT s.sessionAdminId FROM ChamiloCoreBundle:Session s |
||
| 1493 | WHERE s.id = ?1 |
||
| 1494 | ') |
||
| 1495 | ->setParameter(1, $id_checked); |
||
| 1496 | |||
| 1497 | $res = $qb->getSingleScalarResult(); |
||
| 1498 | |||
| 1499 | if ($res != $userId) { |
||
| 1500 | api_not_allowed(true); |
||
| 1501 | } |
||
| 1502 | } |
||
| 1503 | |||
| 1504 | // Delete documents inside a session |
||
| 1505 | $courses = SessionManager::getCoursesInSession($id_checked); |
||
| 1506 | foreach ($courses as $courseId) { |
||
| 1507 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 1508 | DocumentManager::deleteDocumentsFromSession($courseInfo, $id_checked); |
||
| 1509 | } |
||
| 1510 | |||
| 1511 | Database::query("DELETE FROM $tbl_session_rel_course WHERE session_id IN($id_checked)"); |
||
| 1512 | Database::query("DELETE FROM $tbl_session_rel_course_rel_user WHERE session_id IN($id_checked)"); |
||
| 1513 | Database::query("DELETE FROM $tbl_session_rel_user WHERE session_id IN($id_checked)"); |
||
| 1514 | Database::query("DELETE FROM $tbl_item_properties WHERE session_id IN ($id_checked)"); |
||
| 1515 | Database::query("DELETE FROM $tbl_url_session WHERE session_id IN($id_checked)"); |
||
| 1516 | |||
| 1517 | Database::query("DELETE FROM $tbl_session WHERE id IN ($id_checked)"); |
||
| 1518 | |||
| 1519 | $extraFieldValue = new ExtraFieldValue('session'); |
||
| 1520 | $extraFieldValue->deleteValuesByItem($id_checked); |
||
| 1521 | |||
| 1522 | /** @var \Chamilo\CoreBundle\Entity\Repository\SequenceRepository $repo */ |
||
| 1523 | $repo = Database::getManager()->getRepository('ChamiloCoreBundle:SequenceResource'); |
||
| 1524 | $repo->deleteResource( |
||
| 1525 | $id_checked, |
||
| 1526 | \Chamilo\CoreBundle\Entity\SequenceResource::SESSION_TYPE |
||
| 1527 | ); |
||
| 1528 | |||
| 1529 | // Add event to system log |
||
| 1530 | Event::addEvent( |
||
| 1531 | LOG_SESSION_DELETE, |
||
| 1532 | LOG_SESSION_ID, |
||
| 1533 | $id_checked, |
||
| 1534 | api_get_utc_datetime(), |
||
| 1535 | $userId |
||
| 1536 | ); |
||
| 1537 | } |
||
| 1538 | |||
| 1539 | /** |
||
| 1540 | * @param int $id_promotion |
||
| 1541 | * |
||
| 1542 | * @return bool |
||
| 1543 | */ |
||
| 1544 | View Code Duplication | public static function clear_session_ref_promotion($id_promotion) |
|
| 1545 | { |
||
| 1546 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1547 | $id_promotion = intval($id_promotion); |
||
| 1548 | $sql = "UPDATE $tbl_session SET promotion_id=0 |
||
| 1549 | WHERE promotion_id = $id_promotion"; |
||
| 1550 | if (Database::query($sql)) { |
||
| 1551 | return true; |
||
| 1552 | } else { |
||
| 1553 | return false; |
||
| 1554 | } |
||
| 1555 | } |
||
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Subscribes students to the given session and optionally (default) unsubscribes previous users |
||
| 1559 | * |
||
| 1560 | * @author Carlos Vargas from existing code |
||
| 1561 | * @author Julio Montoya. Cleaning code. |
||
| 1562 | * @param int $id_session |
||
| 1563 | * @param array $user_list |
||
| 1564 | * @param int $session_visibility |
||
| 1565 | * @param bool $empty_users |
||
| 1566 | * @return bool |
||
| 1567 | */ |
||
| 1568 | public static function suscribe_users_to_session( |
||
| 1569 | $id_session, |
||
| 1570 | $user_list, |
||
| 1571 | $session_visibility = SESSION_VISIBLE_READ_ONLY, |
||
| 1572 | $empty_users = true |
||
| 1573 | ) { |
||
| 1574 | if ($id_session != strval(intval($id_session))) { |
||
| 1575 | return false; |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | foreach ($user_list as $intUser) { |
||
| 1579 | if ($intUser != strval(intval($intUser))) { |
||
| 1580 | return false; |
||
| 1581 | } |
||
| 1582 | } |
||
| 1583 | |||
| 1584 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 1585 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 1586 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 1587 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1588 | |||
| 1589 | $entityManager = Database::getManager(); |
||
| 1590 | $session = $entityManager->find('ChamiloCoreBundle:Session', $id_session); |
||
| 1591 | |||
| 1592 | // from function parameter |
||
| 1593 | if (empty($session_visibility)) { |
||
| 1594 | $session_visibility = $session->getVisibility(); |
||
| 1595 | //default status loaded if empty |
||
| 1596 | if (empty($session_visibility)) |
||
| 1597 | $session_visibility = SESSION_VISIBLE_READ_ONLY; // by default readonly 1 |
||
| 1598 | } else { |
||
| 1599 | if (!in_array($session_visibility, array(SESSION_VISIBLE_READ_ONLY, SESSION_VISIBLE, SESSION_INVISIBLE))) { |
||
| 1600 | $session_visibility = SESSION_VISIBLE_READ_ONLY; |
||
| 1601 | } |
||
| 1602 | } |
||
| 1603 | |||
| 1604 | $sql = "SELECT user_id FROM $tbl_session_rel_course_rel_user |
||
| 1605 | WHERE session_id = $id_session AND status = 0"; |
||
| 1606 | $result = Database::query($sql); |
||
| 1607 | $existingUsers = array(); |
||
| 1608 | while ($row = Database::fetch_array($result)) { |
||
| 1609 | $existingUsers[] = $row['user_id']; |
||
| 1610 | } |
||
| 1611 | |||
| 1612 | $sql = "SELECT c_id FROM $tbl_session_rel_course |
||
| 1613 | WHERE session_id = $id_session"; |
||
| 1614 | $result = Database::query($sql); |
||
| 1615 | $course_list = array(); |
||
| 1616 | while ($row = Database::fetch_array($result)) { |
||
| 1617 | $course_list[] = $row['c_id']; |
||
| 1618 | } |
||
| 1619 | |||
| 1620 | if ( |
||
| 1621 | $session->getSendSubscriptionNotification() && |
||
| 1622 | is_array($user_list) |
||
| 1623 | ) { |
||
| 1624 | // Sending emails only |
||
| 1625 | foreach ($user_list as $user_id) { |
||
| 1626 | if (in_array($user_id, $existingUsers)) { |
||
| 1627 | continue; |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | $tplSubject = new Template(null, false, false, false, false, false); |
||
| 1631 | $layoutSubject = $tplSubject->get_template( |
||
| 1632 | 'mail/subject_subscription_to_session_confirmation.tpl' |
||
| 1633 | ); |
||
| 1634 | $subject = $tplSubject->fetch($layoutSubject); |
||
| 1635 | |||
| 1636 | $user_info = api_get_user_info($user_id); |
||
| 1637 | |||
| 1638 | $tplContent = new Template(null, false, false, false, false, false); |
||
| 1639 | // Variables for default template |
||
| 1640 | $tplContent->assign( |
||
| 1641 | 'complete_name', |
||
| 1642 | stripslashes($user_info['complete_name']) |
||
| 1643 | ); |
||
| 1644 | $tplContent->assign('session_name', $session->getName()); |
||
| 1645 | $tplContent->assign( |
||
| 1646 | 'session_coach', |
||
| 1647 | $session->getGeneralCoach()->getCompleteName() |
||
| 1648 | ); |
||
| 1649 | $layoutContent = $tplContent->get_template( |
||
| 1650 | 'mail/content_subscription_to_session_confirmation.tpl' |
||
| 1651 | ); |
||
| 1652 | $content = $tplContent->fetch($layoutContent); |
||
| 1653 | |||
| 1654 | api_mail_html( |
||
| 1655 | $user_info['complete_name'], |
||
| 1656 | $user_info['mail'], |
||
| 1657 | $subject, |
||
| 1658 | $content, |
||
| 1659 | api_get_person_name( |
||
| 1660 | api_get_setting('administratorName'), |
||
| 1661 | api_get_setting('administratorSurname') |
||
| 1662 | ), |
||
| 1663 | api_get_setting('emailAdministrator') |
||
| 1664 | ); |
||
| 1665 | } |
||
| 1666 | } |
||
| 1667 | |||
| 1668 | foreach ($course_list as $courseId) { |
||
| 1669 | // for each course in the session |
||
| 1670 | $nbr_users = 0; |
||
| 1671 | $courseId = intval($courseId); |
||
| 1672 | |||
| 1673 | $sql = "SELECT DISTINCT user_id |
||
| 1674 | FROM $tbl_session_rel_course_rel_user |
||
| 1675 | WHERE |
||
| 1676 | session_id = $id_session AND |
||
| 1677 | c_id = $courseId AND |
||
| 1678 | status = 0 |
||
| 1679 | "; |
||
| 1680 | $result = Database::query($sql); |
||
| 1681 | $existingUsers = array(); |
||
| 1682 | while ($row = Database::fetch_array($result)) { |
||
| 1683 | $existingUsers[] = $row['user_id']; |
||
| 1684 | } |
||
| 1685 | |||
| 1686 | // Delete existing users |
||
| 1687 | View Code Duplication | if ($empty_users) { |
|
| 1688 | foreach ($existingUsers as $existing_user) { |
||
| 1689 | if (!in_array($existing_user, $user_list)) { |
||
| 1690 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 1691 | WHERE |
||
| 1692 | session_id = $id_session AND |
||
| 1693 | c_id = $courseId AND |
||
| 1694 | user_id = $existing_user AND |
||
| 1695 | status = 0 "; |
||
| 1696 | $result = Database::query($sql); |
||
| 1697 | |||
| 1698 | Event::addEvent( |
||
| 1699 | LOG_SESSION_DELETE_USER_COURSE, |
||
| 1700 | LOG_USER_ID, |
||
| 1701 | $existing_user, |
||
| 1702 | api_get_utc_datetime(), |
||
| 1703 | api_get_user_id(), |
||
| 1704 | $courseId, |
||
| 1705 | $id_session |
||
| 1706 | ); |
||
| 1707 | |||
| 1708 | if (Database::affected_rows($result)) { |
||
| 1709 | $nbr_users--; |
||
| 1710 | } |
||
| 1711 | } |
||
| 1712 | } |
||
| 1713 | } |
||
| 1714 | |||
| 1715 | // Replace with this new function |
||
| 1716 | // insert new users into session_rel_course_rel_user and ignore if they already exist |
||
| 1717 | |||
| 1718 | foreach ($user_list as $enreg_user) { |
||
| 1719 | View Code Duplication | if (!in_array($enreg_user, $existingUsers)) { |
|
| 1720 | $enreg_user = Database::escape_string($enreg_user); |
||
| 1721 | $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility, status) |
||
| 1722 | VALUES($id_session, $courseId, $enreg_user, $session_visibility, 0)"; |
||
| 1723 | $result = Database::query($sql); |
||
| 1724 | |||
| 1725 | Event::addEvent( |
||
| 1726 | LOG_SESSION_ADD_USER_COURSE, |
||
| 1727 | LOG_USER_ID, |
||
| 1728 | $enreg_user, |
||
| 1729 | api_get_utc_datetime(), |
||
| 1730 | api_get_user_id(), |
||
| 1731 | $courseId, |
||
| 1732 | $id_session |
||
| 1733 | ); |
||
| 1734 | |||
| 1735 | if (Database::affected_rows($result)) { |
||
| 1736 | |||
| 1737 | $nbr_users++; |
||
| 1738 | } |
||
| 1739 | } |
||
| 1740 | } |
||
| 1741 | |||
| 1742 | // Count users in this session-course relation |
||
| 1743 | $sql = "SELECT COUNT(user_id) as nbUsers |
||
| 1744 | FROM $tbl_session_rel_course_rel_user |
||
| 1745 | WHERE session_id = $id_session AND c_id = $courseId AND status<>2"; |
||
| 1746 | $rs = Database::query($sql); |
||
| 1747 | list($nbr_users) = Database::fetch_array($rs); |
||
| 1748 | // update the session-course relation to add the users total |
||
| 1749 | $sql = "UPDATE $tbl_session_rel_course SET nbr_users = $nbr_users |
||
| 1750 | WHERE session_id = $id_session AND c_id = $courseId"; |
||
| 1751 | Database::query($sql); |
||
| 1752 | } |
||
| 1753 | |||
| 1754 | // Delete users from the session |
||
| 1755 | if ($empty_users === true) { |
||
| 1756 | $sql = "DELETE FROM $tbl_session_rel_user |
||
| 1757 | WHERE session_id = $id_session AND relation_type<>" . SESSION_RELATION_TYPE_RRHH . ""; |
||
| 1758 | Database::query($sql); |
||
| 1759 | } |
||
| 1760 | |||
| 1761 | // Insert missing users into session |
||
| 1762 | $nbr_users = 0; |
||
| 1763 | |||
| 1764 | foreach ($user_list as $enreg_user) { |
||
| 1765 | $enreg_user = Database::escape_string($enreg_user); |
||
| 1766 | $nbr_users++; |
||
| 1767 | $sql = "INSERT IGNORE INTO $tbl_session_rel_user (relation_type, session_id, user_id, registered_at) |
||
| 1768 | VALUES (0, $id_session, $enreg_user, '" . api_get_utc_datetime() . "')"; |
||
| 1769 | Database::query($sql); |
||
| 1770 | } |
||
| 1771 | |||
| 1772 | // update number of users in the session |
||
| 1773 | $nbr_users = count($user_list); |
||
| 1774 | if ($empty_users) { |
||
| 1775 | // update number of users in the session |
||
| 1776 | $sql = "UPDATE $tbl_session SET nbr_users= $nbr_users |
||
| 1777 | WHERE id = $id_session "; |
||
| 1778 | Database::query($sql); |
||
| 1779 | } else { |
||
| 1780 | $sql = "UPDATE $tbl_session SET nbr_users = nbr_users + $nbr_users |
||
| 1781 | WHERE id = $id_session"; |
||
| 1782 | Database::query($sql); |
||
| 1783 | } |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | /** |
||
| 1787 | * Returns user list of the current users subscribed in the course-session |
||
| 1788 | * @param int $sessionId |
||
| 1789 | * @param array $courseInfo |
||
| 1790 | * @param int $status |
||
| 1791 | * |
||
| 1792 | * @return array |
||
| 1793 | */ |
||
| 1794 | public static function getUsersByCourseSession( |
||
| 1795 | $sessionId, |
||
| 1796 | $courseInfo, |
||
| 1797 | $status = null |
||
| 1798 | ) { |
||
| 1799 | $sessionId = intval($sessionId); |
||
| 1800 | $courseCode = $courseInfo['code']; |
||
| 1801 | $courseId = $courseInfo['real_id']; |
||
| 1802 | |||
| 1803 | if (empty($sessionId) || empty($courseCode)) { |
||
| 1804 | return array(); |
||
| 1805 | } |
||
| 1806 | |||
| 1807 | $statusCondition = null; |
||
| 1808 | View Code Duplication | if (isset($status) && !is_null($status)) { |
|
| 1809 | $status = intval($status); |
||
| 1810 | $statusCondition = " AND status = $status"; |
||
| 1811 | } |
||
| 1812 | |||
| 1813 | $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 1814 | |||
| 1815 | $sql = "SELECT DISTINCT user_id |
||
| 1816 | FROM $table |
||
| 1817 | WHERE |
||
| 1818 | session_id = $sessionId AND |
||
| 1819 | c_id = $courseId |
||
| 1820 | $statusCondition |
||
| 1821 | "; |
||
| 1822 | $result = Database::query($sql); |
||
| 1823 | $existingUsers = array(); |
||
| 1824 | while ($row = Database::fetch_array($result)) { |
||
| 1825 | $existingUsers[] = $row['user_id']; |
||
| 1826 | } |
||
| 1827 | |||
| 1828 | return $existingUsers; |
||
| 1829 | } |
||
| 1830 | |||
| 1831 | /** |
||
| 1832 | * Remove a list of users from a course-session |
||
| 1833 | * @param array $userList |
||
| 1834 | * @param int $sessionId |
||
| 1835 | * @param array $courseInfo |
||
| 1836 | * @param int $status |
||
| 1837 | * @param bool $updateTotal |
||
| 1838 | * @return bool |
||
| 1839 | */ |
||
| 1840 | public static function removeUsersFromCourseSession( |
||
| 1841 | $userList, |
||
| 1842 | $sessionId, |
||
| 1843 | $courseInfo, |
||
| 1844 | $status = null, |
||
| 1845 | $updateTotal = true |
||
| 1846 | ) { |
||
| 1847 | $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 1848 | $tableSessionCourse = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 1849 | $sessionId = intval($sessionId); |
||
| 1850 | |||
| 1851 | if (empty($sessionId) || empty($userList) || empty($courseInfo)) { |
||
| 1852 | return false; |
||
| 1853 | } |
||
| 1854 | |||
| 1855 | is_array($courseInfo) ? $courseId = $courseInfo['real_id'] : $courseId = $courseInfo; |
||
| 1856 | |||
| 1857 | $statusCondition = null; |
||
| 1858 | View Code Duplication | if (isset($status) && !is_null($status)) { |
|
| 1859 | $status = intval($status); |
||
| 1860 | $statusCondition = " AND status = $status"; |
||
| 1861 | } |
||
| 1862 | |||
| 1863 | foreach ($userList as $userId) { |
||
| 1864 | $userId = intval($userId); |
||
| 1865 | $sql = "DELETE FROM $table |
||
| 1866 | WHERE |
||
| 1867 | session_id = $sessionId AND |
||
| 1868 | c_id = $courseId AND |
||
| 1869 | user_id = $userId |
||
| 1870 | $statusCondition |
||
| 1871 | "; |
||
| 1872 | Database::query($sql); |
||
| 1873 | } |
||
| 1874 | |||
| 1875 | if ($updateTotal) { |
||
| 1876 | // Count users in this session-course relation |
||
| 1877 | $sql = "SELECT COUNT(user_id) as nbUsers |
||
| 1878 | FROM $table |
||
| 1879 | WHERE |
||
| 1880 | session_id = $sessionId AND |
||
| 1881 | c_id = $courseId AND |
||
| 1882 | status <> 2"; |
||
| 1883 | $result = Database::query($sql); |
||
| 1884 | list($userCount) = Database::fetch_array($result); |
||
| 1885 | |||
| 1886 | // update the session-course relation to add the users total |
||
| 1887 | $sql = "UPDATE $tableSessionCourse |
||
| 1888 | SET nbr_users = $userCount |
||
| 1889 | WHERE |
||
| 1890 | session_id = $sessionId AND |
||
| 1891 | c_id = $courseId"; |
||
| 1892 | Database::query($sql); |
||
| 1893 | } |
||
| 1894 | } |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Subscribe a user to an specific course inside a session. |
||
| 1898 | * |
||
| 1899 | * @param array $user_list |
||
| 1900 | * @param int $session_id |
||
| 1901 | * @param string $course_code |
||
| 1902 | * @param int $session_visibility |
||
| 1903 | * @param bool $removeUsersNotInList |
||
| 1904 | * @return bool |
||
| 1905 | */ |
||
| 1906 | public static function subscribe_users_to_session_course( |
||
| 1907 | $user_list, |
||
| 1908 | $session_id, |
||
| 1909 | $course_code, |
||
| 1910 | $session_visibility = SESSION_VISIBLE_READ_ONLY, |
||
| 1911 | $removeUsersNotInList = false |
||
| 1912 | ) { |
||
| 1913 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 1914 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 1915 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 1916 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 1917 | |||
| 1918 | if (empty($session_id) || empty($course_code)) { |
||
| 1919 | return false; |
||
| 1920 | } |
||
| 1921 | |||
| 1922 | $session_id = intval($session_id); |
||
| 1923 | $course_code = Database::escape_string($course_code); |
||
| 1924 | $courseInfo = api_get_course_info($course_code); |
||
| 1925 | $courseId = $courseInfo['real_id']; |
||
| 1926 | |||
| 1927 | $session_visibility = intval($session_visibility); |
||
| 1928 | |||
| 1929 | if ($removeUsersNotInList) { |
||
| 1930 | |||
| 1931 | $currentUsers = self::getUsersByCourseSession($session_id, $courseInfo, 0); |
||
| 1932 | |||
| 1933 | if (!empty($user_list)) { |
||
| 1934 | $userToDelete = array_diff($currentUsers, $user_list); |
||
| 1935 | } else { |
||
| 1936 | $userToDelete = $currentUsers; |
||
| 1937 | } |
||
| 1938 | |||
| 1939 | if (!empty($userToDelete)) { |
||
| 1940 | self::removeUsersFromCourseSession( |
||
| 1941 | $userToDelete, |
||
| 1942 | $session_id, |
||
| 1943 | $courseInfo, |
||
| 1944 | 0, |
||
| 1945 | true |
||
| 1946 | ); |
||
| 1947 | } |
||
| 1948 | } |
||
| 1949 | |||
| 1950 | $nbr_users = 0; |
||
| 1951 | foreach ($user_list as $enreg_user) { |
||
| 1952 | $enreg_user = intval($enreg_user); |
||
| 1953 | // Checking if user exists in session - course - user table. |
||
| 1954 | $sql = "SELECT count(user_id) as count |
||
| 1955 | FROM $tbl_session_rel_course_rel_user |
||
| 1956 | WHERE |
||
| 1957 | session_id = $session_id AND |
||
| 1958 | c_id = $courseId and |
||
| 1959 | user_id = $enreg_user "; |
||
| 1960 | $result = Database::query($sql); |
||
| 1961 | $count = 0; |
||
| 1962 | |||
| 1963 | if (Database::num_rows($result) > 0) { |
||
| 1964 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1965 | $count = $row['count']; |
||
| 1966 | } |
||
| 1967 | |||
| 1968 | if ($count == 0) { |
||
| 1969 | $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id, visibility) |
||
| 1970 | VALUES ($session_id, $courseId, $enreg_user, $session_visibility)"; |
||
| 1971 | $result = Database::query($sql); |
||
| 1972 | if (Database::affected_rows($result)) { |
||
| 1973 | $nbr_users++; |
||
| 1974 | } |
||
| 1975 | } |
||
| 1976 | |||
| 1977 | // Checking if user exists in session - user table. |
||
| 1978 | $sql = "SELECT count(user_id) as count |
||
| 1979 | FROM $tbl_session_rel_user |
||
| 1980 | WHERE session_id = $session_id AND user_id = $enreg_user "; |
||
| 1981 | $result = Database::query($sql); |
||
| 1982 | $count = 0; |
||
| 1983 | |||
| 1984 | if (Database::num_rows($result) > 0) { |
||
| 1985 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 1986 | $count = $row['count']; |
||
| 1987 | } |
||
| 1988 | |||
| 1989 | View Code Duplication | if (empty($count)) { |
|
| 1990 | // If user is not registered to a session then add it. |
||
| 1991 | $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, registered_at) |
||
| 1992 | VALUES ($session_id, $enreg_user, '" . api_get_utc_datetime() . "')"; |
||
| 1993 | Database::query($sql); |
||
| 1994 | |||
| 1995 | $sql = "UPDATE $tbl_session SET nbr_users = nbr_users + 1 |
||
| 1996 | WHERE id = $session_id "; |
||
| 1997 | Database::query($sql); |
||
| 1998 | } |
||
| 1999 | } |
||
| 2000 | |||
| 2001 | // count users in this session-course relation |
||
| 2002 | $sql = "SELECT COUNT(user_id) as nbUsers |
||
| 2003 | FROM $tbl_session_rel_course_rel_user |
||
| 2004 | WHERE session_id = $session_id AND c_id = $courseId AND status <> 2"; |
||
| 2005 | $rs = Database::query($sql); |
||
| 2006 | list($nbr_users) = Database::fetch_array($rs); |
||
| 2007 | // update the session-course relation to add the users total |
||
| 2008 | $sql = "UPDATE $tbl_session_rel_course |
||
| 2009 | SET nbr_users = $nbr_users |
||
| 2010 | WHERE session_id = $session_id AND c_id = $courseId"; |
||
| 2011 | Database::query($sql); |
||
| 2012 | } |
||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Unsubscribe user from session |
||
| 2016 | * |
||
| 2017 | * @param int Session id |
||
| 2018 | * @param int User id |
||
| 2019 | * @return bool True in case of success, false in case of error |
||
| 2020 | */ |
||
| 2021 | public static function unsubscribe_user_from_session($session_id, $user_id) |
||
| 2022 | { |
||
| 2023 | $session_id = (int) $session_id; |
||
| 2024 | $user_id = (int) $user_id; |
||
| 2025 | |||
| 2026 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2027 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2028 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2029 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2030 | |||
| 2031 | $delete_sql = "DELETE FROM $tbl_session_rel_user |
||
| 2032 | WHERE |
||
| 2033 | session_id = $session_id AND |
||
| 2034 | user_id = $user_id AND |
||
| 2035 | relation_type <> " . SESSION_RELATION_TYPE_RRHH . ""; |
||
| 2036 | $result = Database::query($delete_sql); |
||
| 2037 | $return = Database::affected_rows($result); |
||
| 2038 | |||
| 2039 | // Update number of users |
||
| 2040 | $sql = "UPDATE $tbl_session |
||
| 2041 | SET nbr_users = nbr_users - $return |
||
| 2042 | WHERE id = $session_id "; |
||
| 2043 | Database::query($sql); |
||
| 2044 | |||
| 2045 | // Get the list of courses related to this session |
||
| 2046 | $course_list = SessionManager::get_course_list_by_session_id($session_id); |
||
| 2047 | |||
| 2048 | if (!empty($course_list)) { |
||
| 2049 | foreach ($course_list as $course) { |
||
| 2050 | $courseId = $course['id']; |
||
| 2051 | // Delete user from course |
||
| 2052 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2053 | WHERE session_id = $session_id AND c_id = $courseId AND user_id = $user_id"; |
||
| 2054 | $result = Database::query($sql); |
||
| 2055 | |||
| 2056 | Event::addEvent( |
||
| 2057 | LOG_SESSION_DELETE_USER_COURSE, |
||
| 2058 | LOG_USER_ID, |
||
| 2059 | $user_id, |
||
| 2060 | api_get_utc_datetime(), |
||
| 2061 | api_get_user_id(), |
||
| 2062 | $courseId, |
||
| 2063 | $session_id |
||
| 2064 | ); |
||
| 2065 | |||
| 2066 | if (Database::affected_rows($result)) { |
||
| 2067 | // Update number of users in this relation |
||
| 2068 | $sql = "UPDATE $tbl_session_rel_course SET nbr_users = nbr_users - 1 |
||
| 2069 | WHERE session_id = $session_id AND c_id = $courseId"; |
||
| 2070 | Database::query($sql); |
||
| 2071 | } |
||
| 2072 | } |
||
| 2073 | } |
||
| 2074 | |||
| 2075 | return true; |
||
| 2076 | } |
||
| 2077 | |||
| 2078 | /** |
||
| 2079 | * Subscribes courses to the given session and optionally (default) |
||
| 2080 | * unsubscribes previous users |
||
| 2081 | * @author Carlos Vargas from existing code |
||
| 2082 | * @param int $sessionId |
||
| 2083 | * @param array $courseList List of courses int ids |
||
| 2084 | * @param bool $removeExistingCoursesWithUsers Whether to unsubscribe |
||
| 2085 | * existing courses and users (true, default) or not (false) |
||
| 2086 | * @param $copyEvaluation from base course to session course |
||
| 2087 | * @return void Nothing, or false on error |
||
| 2088 | * */ |
||
| 2089 | public static function add_courses_to_session( |
||
| 2090 | $sessionId, |
||
| 2091 | $courseList, |
||
| 2092 | $removeExistingCoursesWithUsers = true, |
||
| 2093 | $copyEvaluation = false |
||
| 2094 | ) { |
||
| 2095 | $sessionId = intval($sessionId); |
||
| 2096 | |||
| 2097 | if (empty($sessionId) || empty($courseList)) { |
||
| 2098 | return false; |
||
| 2099 | } |
||
| 2100 | |||
| 2101 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2102 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2103 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2104 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2105 | |||
| 2106 | // Get list of courses subscribed to this session |
||
| 2107 | $sql = "SELECT c_id |
||
| 2108 | FROM $tbl_session_rel_course |
||
| 2109 | WHERE session_id = $sessionId"; |
||
| 2110 | $rs = Database::query($sql); |
||
| 2111 | $existingCourses = Database::store_result($rs); |
||
| 2112 | $nbr_courses = count($existingCourses); |
||
| 2113 | |||
| 2114 | // Get list of users subscribed to this session |
||
| 2115 | $sql = "SELECT user_id |
||
| 2116 | FROM $tbl_session_rel_user |
||
| 2117 | WHERE |
||
| 2118 | session_id = $sessionId AND |
||
| 2119 | relation_type<>" . SESSION_RELATION_TYPE_RRHH; |
||
| 2120 | $result = Database::query($sql); |
||
| 2121 | $user_list = Database::store_result($result); |
||
| 2122 | |||
| 2123 | // Remove existing courses from the session. |
||
| 2124 | if ($removeExistingCoursesWithUsers === true && !empty($existingCourses)) { |
||
| 2125 | |||
| 2126 | foreach ($existingCourses as $existingCourse) { |
||
| 2127 | if (!in_array($existingCourse['c_id'], $courseList)) { |
||
| 2128 | |||
| 2129 | $sql = "DELETE FROM $tbl_session_rel_course |
||
| 2130 | WHERE |
||
| 2131 | c_id = " . $existingCourse['c_id'] . " AND |
||
| 2132 | session_id = $sessionId"; |
||
| 2133 | Database::query($sql); |
||
| 2134 | |||
| 2135 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2136 | WHERE |
||
| 2137 | c_id = ".$existingCourse['c_id']." AND |
||
| 2138 | session_id = $sessionId"; |
||
| 2139 | Database::query($sql); |
||
| 2140 | |||
| 2141 | Event::addEvent( |
||
| 2142 | LOG_SESSION_DELETE_COURSE, |
||
| 2143 | LOG_COURSE_ID, |
||
| 2144 | $existingCourse['c_id'], |
||
| 2145 | api_get_utc_datetime(), |
||
| 2146 | api_get_user_id(), |
||
| 2147 | $existingCourse['c_id'], |
||
| 2148 | $sessionId |
||
| 2149 | ); |
||
| 2150 | |||
| 2151 | CourseManager::remove_course_ranking( |
||
| 2152 | $existingCourse['c_id'], |
||
| 2153 | $sessionId |
||
| 2154 | ); |
||
| 2155 | |||
| 2156 | $nbr_courses--; |
||
| 2157 | } |
||
| 2158 | } |
||
| 2159 | } |
||
| 2160 | |||
| 2161 | // Pass through the courses list we want to add to the session |
||
| 2162 | foreach ($courseList as $courseId) { |
||
| 2163 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 2164 | |||
| 2165 | // If course doesn't exists continue! |
||
| 2166 | if (empty($courseInfo)) { |
||
| 2167 | continue; |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | $exists = false; |
||
| 2171 | // check if the course we want to add is already subscribed |
||
| 2172 | foreach ($existingCourses as $existingCourse) { |
||
| 2173 | if ($courseId == $existingCourse['c_id']) { |
||
| 2174 | $exists = true; |
||
| 2175 | } |
||
| 2176 | } |
||
| 2177 | |||
| 2178 | if (!$exists) { |
||
| 2179 | // Copy gradebook categories and links (from base course) |
||
| 2180 | // to the new course session |
||
| 2181 | if ($copyEvaluation) { |
||
| 2182 | $cats = Category::load(null, null, $courseInfo['code']); |
||
| 2183 | if (!empty($cats)) { |
||
| 2184 | $categoryIdList = []; |
||
| 2185 | /** @var Category $cat */ |
||
| 2186 | foreach ($cats as $cat) { |
||
| 2187 | $categoryIdList[$cat->get_id()] = $cat->get_id(); |
||
| 2188 | } |
||
| 2189 | $newCategoryIdList = []; |
||
| 2190 | foreach ($cats as $cat) { |
||
| 2191 | $links = $cat->get_links(null, false, $courseInfo['code'], 0); |
||
| 2192 | |||
| 2193 | $cat->set_session_id($sessionId); |
||
| 2194 | $oldCategoryId= $cat->get_id(); |
||
| 2195 | $newId = $cat->add(); |
||
| 2196 | $newCategoryIdList[$oldCategoryId] = $newId; |
||
| 2197 | |||
| 2198 | $parentId = $cat->get_parent_id(); |
||
| 2199 | |||
| 2200 | if (!empty($parentId)) { |
||
| 2201 | $newParentId = $newCategoryIdList[$parentId]; |
||
| 2202 | $cat->set_parent_id($newParentId); |
||
| 2203 | $cat->save(); |
||
| 2204 | } |
||
| 2205 | |||
| 2206 | /** @var AbstractLink $link */ |
||
| 2207 | foreach ($links as $link) { |
||
| 2208 | $newCategoryId = $newCategoryIdList[$link->get_category_id()]; |
||
| 2209 | $link->set_category_id($newCategoryId); |
||
| 2210 | $link->add(); |
||
| 2211 | } |
||
| 2212 | } |
||
| 2213 | |||
| 2214 | // Create |
||
| 2215 | DocumentManager::generateDefaultCertificate( |
||
| 2216 | $courseInfo, |
||
| 2217 | true, |
||
| 2218 | $sessionId |
||
| 2219 | ); |
||
| 2220 | } |
||
| 2221 | } |
||
| 2222 | |||
| 2223 | // If the course isn't subscribed yet |
||
| 2224 | $sql = "INSERT INTO $tbl_session_rel_course (session_id, c_id) |
||
| 2225 | VALUES ($sessionId, $courseId)"; |
||
| 2226 | Database::query($sql); |
||
| 2227 | |||
| 2228 | Event::addEvent( |
||
| 2229 | LOG_SESSION_ADD_COURSE, |
||
| 2230 | LOG_COURSE_ID, |
||
| 2231 | $courseId, |
||
| 2232 | api_get_utc_datetime(), |
||
| 2233 | api_get_user_id(), |
||
| 2234 | $courseId, |
||
| 2235 | $sessionId |
||
| 2236 | ); |
||
| 2237 | |||
| 2238 | // We add the current course in the existing courses array, |
||
| 2239 | // to avoid adding another time the current course |
||
| 2240 | $existingCourses[] = array('c_id' => $courseId); |
||
| 2241 | $nbr_courses++; |
||
| 2242 | |||
| 2243 | // subscribe all the users from the session to this course inside the session |
||
| 2244 | $nbr_users = 0; |
||
| 2245 | View Code Duplication | foreach ($user_list as $enreg_user) { |
|
| 2246 | $enreg_user_id = intval($enreg_user['user_id']); |
||
| 2247 | $sql = "INSERT IGNORE INTO $tbl_session_rel_course_rel_user (session_id, c_id, user_id) |
||
| 2248 | VALUES ($sessionId, $courseId, $enreg_user_id)"; |
||
| 2249 | $result = Database::query($sql); |
||
| 2250 | |||
| 2251 | Event::addEvent( |
||
| 2252 | LOG_SESSION_ADD_USER_COURSE, |
||
| 2253 | LOG_USER_ID, |
||
| 2254 | $enreg_user_id, |
||
| 2255 | api_get_utc_datetime(), |
||
| 2256 | api_get_user_id(), |
||
| 2257 | $courseId, |
||
| 2258 | $sessionId |
||
| 2259 | ); |
||
| 2260 | |||
| 2261 | if (Database::affected_rows($result)) { |
||
| 2262 | $nbr_users++; |
||
| 2263 | } |
||
| 2264 | } |
||
| 2265 | $sql = "UPDATE $tbl_session_rel_course |
||
| 2266 | SET nbr_users = $nbr_users |
||
| 2267 | WHERE session_id = $sessionId AND c_id = $courseId"; |
||
| 2268 | Database::query($sql); |
||
| 2269 | } |
||
| 2270 | } |
||
| 2271 | |||
| 2272 | $sql = "UPDATE $tbl_session |
||
| 2273 | SET nbr_courses = $nbr_courses |
||
| 2274 | WHERE id = $sessionId"; |
||
| 2275 | Database::query($sql); |
||
| 2276 | } |
||
| 2277 | |||
| 2278 | /** |
||
| 2279 | * Unsubscribe course from a session |
||
| 2280 | * |
||
| 2281 | * @param int Session id |
||
| 2282 | * @param int Course id |
||
| 2283 | * @return bool True in case of success, false otherwise |
||
| 2284 | */ |
||
| 2285 | public static function unsubscribe_course_from_session($session_id, $course_id) |
||
| 2286 | { |
||
| 2287 | $session_id = (int) $session_id; |
||
| 2288 | $course_id = (int) $course_id; |
||
| 2289 | |||
| 2290 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2291 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2292 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2293 | |||
| 2294 | // Get course code |
||
| 2295 | $course_code = CourseManager::get_course_code_from_course_id($course_id); |
||
| 2296 | $course_id = intval($course_id); |
||
| 2297 | |||
| 2298 | if (empty($course_code)) { |
||
| 2299 | return false; |
||
| 2300 | } |
||
| 2301 | |||
| 2302 | // Unsubscribe course |
||
| 2303 | $sql = "DELETE FROM $tbl_session_rel_course |
||
| 2304 | WHERE c_id = $course_id AND session_id = $session_id"; |
||
| 2305 | $result = Database::query($sql); |
||
| 2306 | $nb_affected = Database::affected_rows($result); |
||
| 2307 | |||
| 2308 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2309 | WHERE c_id = $course_id AND session_id = $session_id"; |
||
| 2310 | Database::query($sql); |
||
| 2311 | |||
| 2312 | Event::addEvent( |
||
| 2313 | LOG_SESSION_DELETE_COURSE, |
||
| 2314 | LOG_COURSE_ID, |
||
| 2315 | $course_id, |
||
| 2316 | api_get_utc_datetime(), |
||
| 2317 | api_get_user_id(), |
||
| 2318 | $course_id, |
||
| 2319 | $session_id |
||
| 2320 | ); |
||
| 2321 | |||
| 2322 | if ($nb_affected > 0) { |
||
| 2323 | // Update number of courses in the session |
||
| 2324 | $sql = "UPDATE $tbl_session SET nbr_courses= nbr_courses - $nb_affected |
||
| 2325 | WHERE id = $session_id"; |
||
| 2326 | Database::query($sql); |
||
| 2327 | return true; |
||
| 2328 | } else { |
||
| 2329 | return false; |
||
| 2330 | } |
||
| 2331 | } |
||
| 2332 | |||
| 2333 | /** |
||
| 2334 | * Creates a new extra field for a given session |
||
| 2335 | * @param string $variable Field's internal variable name |
||
| 2336 | * @param int $fieldType Field's type |
||
| 2337 | * @param string $displayText Field's language var name |
||
| 2338 | * @return int new extra field id |
||
| 2339 | */ |
||
| 2340 | View Code Duplication | public static function create_session_extra_field($variable, $fieldType, $displayText) |
|
| 2341 | { |
||
| 2342 | $extraField = new ExtraField('session'); |
||
| 2343 | $params = [ |
||
| 2344 | 'variable' => $variable, |
||
| 2345 | 'field_type' => $fieldType, |
||
| 2346 | 'display_text' => $displayText, |
||
| 2347 | ]; |
||
| 2348 | |||
| 2349 | return $extraField->save($params); |
||
| 2350 | } |
||
| 2351 | |||
| 2352 | /** |
||
| 2353 | * Update an extra field value for a given session |
||
| 2354 | * @param integer Course ID |
||
| 2355 | * @param string Field variable name |
||
| 2356 | * @param string Field value |
||
| 2357 | * @return boolean true if field updated, false otherwise |
||
| 2358 | */ |
||
| 2359 | View Code Duplication | public static function update_session_extra_field_value($sessionId, $variable, $value = '') |
|
| 2360 | { |
||
| 2361 | $extraFieldValue = new ExtraFieldValue('session'); |
||
| 2362 | $params = [ |
||
| 2363 | 'item_id' => $sessionId, |
||
| 2364 | 'variable' => $variable, |
||
| 2365 | 'value' => $value, |
||
| 2366 | ]; |
||
| 2367 | $extraFieldValue->save($params); |
||
| 2368 | } |
||
| 2369 | |||
| 2370 | /** |
||
| 2371 | * Checks the relationship between a session and a course. |
||
| 2372 | * @param int $session_id |
||
| 2373 | * @param int $courseId |
||
| 2374 | * @return bool Returns TRUE if the session and the course are related, FALSE otherwise. |
||
| 2375 | * */ |
||
| 2376 | View Code Duplication | public static function relation_session_course_exist($session_id, $courseId) |
|
| 2377 | { |
||
| 2378 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2379 | $return_value = false; |
||
| 2380 | $sql = "SELECT c_id FROM $tbl_session_course |
||
| 2381 | WHERE |
||
| 2382 | session_id = " . intval($session_id) . " AND |
||
| 2383 | c_id = " . intval($courseId) . ""; |
||
| 2384 | $result = Database::query($sql); |
||
| 2385 | $num = Database::num_rows($result); |
||
| 2386 | if ($num > 0) { |
||
| 2387 | $return_value = true; |
||
| 2388 | } |
||
| 2389 | return $return_value; |
||
| 2390 | } |
||
| 2391 | |||
| 2392 | /** |
||
| 2393 | * Get the session information by name |
||
| 2394 | * @param string session name |
||
| 2395 | * @return mixed false if the session does not exist, array if the session exist |
||
| 2396 | * */ |
||
| 2397 | public static function get_session_by_name($session_name) |
||
| 2398 | { |
||
| 2399 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2400 | $session_name = trim($session_name); |
||
| 2401 | if (empty($session_name)) { |
||
| 2402 | return false; |
||
| 2403 | } |
||
| 2404 | |||
| 2405 | $sql = 'SELECT * |
||
| 2406 | FROM ' . $tbl_session . ' |
||
| 2407 | WHERE name = "' . Database::escape_string($session_name) . '"'; |
||
| 2408 | $result = Database::query($sql); |
||
| 2409 | $num = Database::num_rows($result); |
||
| 2410 | if ($num > 0) { |
||
| 2411 | return Database::fetch_array($result); |
||
| 2412 | } else { |
||
| 2413 | return false; |
||
| 2414 | } |
||
| 2415 | } |
||
| 2416 | |||
| 2417 | /** |
||
| 2418 | * Create a session category |
||
| 2419 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2420 | * @param string name |
||
| 2421 | * @param integer year_start |
||
| 2422 | * @param integer month_start |
||
| 2423 | * @param integer day_start |
||
| 2424 | * @param integer year_end |
||
| 2425 | * @param integer month_end |
||
| 2426 | * @param integer day_end |
||
| 2427 | * @return $id_session; |
||
| 2428 | * */ |
||
| 2429 | public static function create_category_session( |
||
| 2430 | $sname, |
||
| 2431 | $syear_start, |
||
| 2432 | $smonth_start, |
||
| 2433 | $sday_start, |
||
| 2434 | $syear_end, |
||
| 2435 | $smonth_end, |
||
| 2436 | $sday_end |
||
| 2437 | ) { |
||
| 2438 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2439 | $name = trim($sname); |
||
| 2440 | $year_start = intval($syear_start); |
||
| 2441 | $month_start = intval($smonth_start); |
||
| 2442 | $day_start = intval($sday_start); |
||
| 2443 | $year_end = intval($syear_end); |
||
| 2444 | $month_end = intval($smonth_end); |
||
| 2445 | $day_end = intval($sday_end); |
||
| 2446 | |||
| 2447 | $date_start = "$year_start-" . (($month_start < 10) ? "0$month_start" : $month_start) . "-" . (($day_start < 10) ? "0$day_start" : $day_start); |
||
| 2448 | $date_end = "$year_end-" . (($month_end < 10) ? "0$month_end" : $month_end) . "-" . (($day_end < 10) ? "0$day_end" : $day_end); |
||
| 2449 | |||
| 2450 | View Code Duplication | if (empty($name)) { |
|
| 2451 | $msg = get_lang('SessionCategoryNameIsRequired'); |
||
| 2452 | return $msg; |
||
| 2453 | } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) { |
||
| 2454 | $msg = get_lang('InvalidStartDate'); |
||
| 2455 | return $msg; |
||
| 2456 | } elseif (!$month_end && !$day_end && !$year_end) { |
||
| 2457 | $date_end = "null"; |
||
| 2458 | } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) { |
||
| 2459 | $msg = get_lang('InvalidEndDate'); |
||
| 2460 | return $msg; |
||
| 2461 | } elseif ($date_start >= $date_end) { |
||
| 2462 | $msg = get_lang('StartDateShouldBeBeforeEndDate'); |
||
| 2463 | return $msg; |
||
| 2464 | } |
||
| 2465 | |||
| 2466 | $access_url_id = api_get_current_access_url_id(); |
||
| 2467 | $params = [ |
||
| 2468 | 'name' => $name, |
||
| 2469 | 'date_start' => $date_start, |
||
| 2470 | 'date_end' => $date_end, |
||
| 2471 | 'access_url_id' => $access_url_id |
||
| 2472 | ]; |
||
| 2473 | $id_session = Database::insert($tbl_session_category, $params); |
||
| 2474 | |||
| 2475 | // Add event to system log |
||
| 2476 | $user_id = api_get_user_id(); |
||
| 2477 | Event::addEvent( |
||
| 2478 | LOG_SESSION_CATEGORY_CREATE, |
||
| 2479 | LOG_SESSION_CATEGORY_ID, |
||
| 2480 | $id_session, |
||
| 2481 | api_get_utc_datetime(), |
||
| 2482 | $user_id |
||
| 2483 | ); |
||
| 2484 | return $id_session; |
||
| 2485 | } |
||
| 2486 | |||
| 2487 | /** |
||
| 2488 | * Edit a sessions categories |
||
| 2489 | * @author Jhon Hinojosa <[email protected]>,from existing code |
||
| 2490 | * @param integer id |
||
| 2491 | * @param string name |
||
| 2492 | * @param integer year_start |
||
| 2493 | * @param integer month_start |
||
| 2494 | * @param integer day_start |
||
| 2495 | * @param integer year_end |
||
| 2496 | * @param integer month_end |
||
| 2497 | * @param integer day_end |
||
| 2498 | * @return $id; |
||
| 2499 | * The parameter id is a primary key |
||
| 2500 | * */ |
||
| 2501 | public static function edit_category_session( |
||
| 2502 | $id, |
||
| 2503 | $sname, |
||
| 2504 | $syear_start, |
||
| 2505 | $smonth_start, |
||
| 2506 | $sday_start, |
||
| 2507 | $syear_end, |
||
| 2508 | $smonth_end, |
||
| 2509 | $sday_end |
||
| 2510 | ) { |
||
| 2511 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2512 | $name = trim($sname); |
||
| 2513 | $year_start = intval($syear_start); |
||
| 2514 | $month_start = intval($smonth_start); |
||
| 2515 | $day_start = intval($sday_start); |
||
| 2516 | $year_end = intval($syear_end); |
||
| 2517 | $month_end = intval($smonth_end); |
||
| 2518 | $day_end = intval($sday_end); |
||
| 2519 | $id = intval($id); |
||
| 2520 | $date_start = "$year_start-" . (($month_start < 10) ? "0$month_start" : $month_start) . "-" . (($day_start < 10) ? "0$day_start" : $day_start); |
||
| 2521 | $date_end = "$year_end-" . (($month_end < 10) ? "0$month_end" : $month_end) . "-" . (($day_end < 10) ? "0$day_end" : $day_end); |
||
| 2522 | |||
| 2523 | View Code Duplication | if (empty($name)) { |
|
| 2524 | $msg = get_lang('SessionCategoryNameIsRequired'); |
||
| 2525 | return $msg; |
||
| 2526 | } elseif (!$month_start || !$day_start || !$year_start || !checkdate($month_start, $day_start, $year_start)) { |
||
| 2527 | $msg = get_lang('InvalidStartDate'); |
||
| 2528 | return $msg; |
||
| 2529 | } elseif (!$month_end && !$day_end && !$year_end) { |
||
| 2530 | $date_end = null; |
||
| 2531 | } elseif (!$month_end || !$day_end || !$year_end || !checkdate($month_end, $day_end, $year_end)) { |
||
| 2532 | $msg = get_lang('InvalidEndDate'); |
||
| 2533 | return $msg; |
||
| 2534 | } elseif ($date_start >= $date_end) { |
||
| 2535 | $msg = get_lang('StartDateShouldBeBeforeEndDate'); |
||
| 2536 | return $msg; |
||
| 2537 | } |
||
| 2538 | if ($date_end <> null) { |
||
| 2539 | $sql = "UPDATE $tbl_session_category |
||
| 2540 | SET |
||
| 2541 | name = '" . Database::escape_string($name) . "', |
||
| 2542 | date_start = '$date_start' , |
||
| 2543 | date_end = '$date_end' |
||
| 2544 | WHERE id= $id"; |
||
| 2545 | } else { |
||
| 2546 | $sql = "UPDATE $tbl_session_category SET |
||
| 2547 | name = '" . Database::escape_string($name) . "', |
||
| 2548 | date_start = '$date_start', |
||
| 2549 | date_end = NULL |
||
| 2550 | WHERE id= $id"; |
||
| 2551 | } |
||
| 2552 | $result = Database::query($sql); |
||
| 2553 | return ($result ? true : false); |
||
| 2554 | } |
||
| 2555 | |||
| 2556 | /** |
||
| 2557 | * Delete sessions categories |
||
| 2558 | * @author Jhon Hinojosa <[email protected]>, from existing code |
||
| 2559 | * @param array id_checked |
||
| 2560 | * @param bool include delete session |
||
| 2561 | * @param bool optional, true if the function is called by a webservice, false otherwise. |
||
| 2562 | * @return void Nothing, or false on error |
||
| 2563 | * The parameters is a array to delete sessions |
||
| 2564 | * */ |
||
| 2565 | public static function delete_session_category($id_checked, $delete_session = false, $from_ws = false) |
||
| 2566 | { |
||
| 2567 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2568 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2569 | if (is_array($id_checked)) { |
||
| 2570 | $id_checked = Database::escape_string(implode(',', $id_checked)); |
||
| 2571 | } else { |
||
| 2572 | $id_checked = intval($id_checked); |
||
| 2573 | } |
||
| 2574 | |||
| 2575 | //Setting session_category_id to 0 |
||
| 2576 | $sql = "UPDATE $tbl_session SET session_category_id = 0 |
||
| 2577 | WHERE session_category_id IN (" . $id_checked . ")"; |
||
| 2578 | Database::query($sql); |
||
| 2579 | |||
| 2580 | $sql = "SELECT id FROM $tbl_session WHERE session_category_id IN (" . $id_checked . ")"; |
||
| 2581 | $result = Database::query($sql); |
||
| 2582 | while ($rows = Database::fetch_array($result)) { |
||
| 2583 | $session_id = $rows['id']; |
||
| 2584 | if ($delete_session) { |
||
| 2585 | if ($from_ws) { |
||
| 2586 | SessionManager::delete($session_id, true); |
||
| 2587 | } else { |
||
| 2588 | SessionManager::delete($session_id); |
||
| 2589 | } |
||
| 2590 | } |
||
| 2591 | } |
||
| 2592 | $sql = "DELETE FROM $tbl_session_category WHERE id IN (" . $id_checked . ")"; |
||
| 2593 | Database::query($sql); |
||
| 2594 | |||
| 2595 | // Add event to system log |
||
| 2596 | $user_id = api_get_user_id(); |
||
| 2597 | Event::addEvent( |
||
| 2598 | LOG_SESSION_CATEGORY_DELETE, |
||
| 2599 | LOG_SESSION_CATEGORY_ID, |
||
| 2600 | $id_checked, |
||
| 2601 | api_get_utc_datetime(), |
||
| 2602 | $user_id |
||
| 2603 | ); |
||
| 2604 | |||
| 2605 | return true; |
||
| 2606 | } |
||
| 2607 | |||
| 2608 | /** |
||
| 2609 | * Get a list of sessions of which the given conditions match with an = 'cond' |
||
| 2610 | * @param array $conditions a list of condition example : |
||
| 2611 | * array('status' => STUDENT) or |
||
| 2612 | * array('s.name' => array('operator' => 'LIKE', value = '%$needle%')) |
||
| 2613 | * @param array $order_by a list of fields on which sort |
||
| 2614 | * @return array An array with all sessions of the platform. |
||
| 2615 | * @todo optional course code parameter, optional sorting parameters... |
||
| 2616 | */ |
||
| 2617 | public static function get_sessions_list($conditions = array(), $order_by = array(), $from = null, $to = null) |
||
| 2618 | { |
||
| 2619 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2620 | $session_category_table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2621 | $user_table = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2622 | $table_access_url_rel_session = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2623 | $session_course_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 2624 | $course_table = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 2625 | $access_url_id = api_get_current_access_url_id(); |
||
| 2626 | $return_array = array(); |
||
| 2627 | |||
| 2628 | $sql_query = " SELECT |
||
| 2629 | s.id, |
||
| 2630 | s.name, |
||
| 2631 | s.nbr_courses, |
||
| 2632 | s.access_start_date, |
||
| 2633 | s.access_end_date, |
||
| 2634 | u.firstname, |
||
| 2635 | u.lastname, |
||
| 2636 | sc.name as category_name, |
||
| 2637 | s.promotion_id |
||
| 2638 | FROM $session_table s |
||
| 2639 | INNER JOIN $user_table u ON s.id_coach = u.user_id |
||
| 2640 | INNER JOIN $table_access_url_rel_session ar ON ar.session_id = s.id |
||
| 2641 | LEFT JOIN $session_category_table sc ON s.session_category_id = sc.id |
||
| 2642 | LEFT JOIN $session_course_table sco ON (sco.session_id = s.id) |
||
| 2643 | INNER JOIN $course_table c ON sco.c_id = c.id |
||
| 2644 | WHERE ar.access_url_id = $access_url_id "; |
||
| 2645 | |||
| 2646 | $availableFields = array( |
||
| 2647 | 's.id', |
||
| 2648 | 's.name', |
||
| 2649 | 'c.id' |
||
| 2650 | ); |
||
| 2651 | |||
| 2652 | $availableOperator = array( |
||
| 2653 | 'like', |
||
| 2654 | '>=', |
||
| 2655 | '<=', |
||
| 2656 | '=', |
||
| 2657 | ); |
||
| 2658 | |||
| 2659 | if (count($conditions) > 0) { |
||
| 2660 | foreach ($conditions as $field => $options) { |
||
| 2661 | $operator = strtolower($options['operator']); |
||
| 2662 | $value = Database::escape_string($options['value']); |
||
| 2663 | $sql_query .= ' AND '; |
||
| 2664 | if (in_array($field, $availableFields) && in_array($operator, $availableOperator)) { |
||
| 2665 | $sql_query .= $field . " $operator '" . $value . "'"; |
||
| 2666 | } |
||
| 2667 | } |
||
| 2668 | } |
||
| 2669 | |||
| 2670 | $orderAvailableList = array('name'); |
||
| 2671 | |||
| 2672 | if (count($order_by) > 0) { |
||
| 2673 | $order = null; |
||
| 2674 | $direction = null; |
||
| 2675 | if (isset($order_by[0]) && in_array($order_by[0], $orderAvailableList)) { |
||
| 2676 | $order = $order_by[0]; |
||
| 2677 | } |
||
| 2678 | if (isset($order_by[1]) && in_array(strtolower($order_by[1]), array('desc', 'asc'))) { |
||
| 2679 | $direction = $order_by[1]; |
||
| 2680 | } |
||
| 2681 | |||
| 2682 | if (!empty($order)) { |
||
| 2683 | $sql_query .= " ORDER BY $order $direction "; |
||
| 2684 | } |
||
| 2685 | } |
||
| 2686 | |||
| 2687 | if (!is_null($from) && !is_null($to)) { |
||
| 2688 | $to = intval($to); |
||
| 2689 | $from = intval($from); |
||
| 2690 | $sql_query .= "LIMIT $from, $to"; |
||
| 2691 | } |
||
| 2692 | |||
| 2693 | $sql_result = Database::query($sql_query); |
||
| 2694 | if (Database::num_rows($sql_result) > 0) { |
||
| 2695 | while ($result = Database::fetch_array($sql_result)) { |
||
| 2696 | $return_array[$result['id']] = $result; |
||
| 2697 | } |
||
| 2698 | } |
||
| 2699 | |||
| 2700 | return $return_array; |
||
| 2701 | } |
||
| 2702 | |||
| 2703 | /** |
||
| 2704 | * Get the session category information by id |
||
| 2705 | * @param string session category ID |
||
| 2706 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2707 | */ |
||
| 2708 | public static function get_session_category($id) |
||
| 2709 | { |
||
| 2710 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2711 | $id = intval($id); |
||
| 2712 | $sql = "SELECT id, name, date_start, date_end |
||
| 2713 | FROM $tbl_session_category |
||
| 2714 | WHERE id= $id"; |
||
| 2715 | $result = Database::query($sql); |
||
| 2716 | $num = Database::num_rows($result); |
||
| 2717 | if ($num > 0) { |
||
| 2718 | return Database::fetch_array($result); |
||
| 2719 | } else { |
||
| 2720 | return false; |
||
| 2721 | } |
||
| 2722 | } |
||
| 2723 | |||
| 2724 | /** |
||
| 2725 | * Get all session categories (filter by access_url_id) |
||
| 2726 | * @return mixed false if the session category does not exist, array if the session category exists |
||
| 2727 | */ |
||
| 2728 | public static function get_all_session_category() |
||
| 2729 | { |
||
| 2730 | $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY); |
||
| 2731 | $id = api_get_current_access_url_id(); |
||
| 2732 | $sql = 'SELECT * FROM ' . $tbl_session_category . ' |
||
| 2733 | WHERE access_url_id = ' . $id . ' |
||
| 2734 | ORDER BY name ASC'; |
||
| 2735 | $result = Database::query($sql); |
||
| 2736 | if (Database::num_rows($result) > 0) { |
||
| 2737 | $data = Database::store_result($result, 'ASSOC'); |
||
| 2738 | return $data; |
||
| 2739 | } else { |
||
| 2740 | return false; |
||
| 2741 | } |
||
| 2742 | } |
||
| 2743 | |||
| 2744 | /** |
||
| 2745 | * Assign a coach to course in session with status = 2 |
||
| 2746 | * @param int $user_id |
||
| 2747 | * @param int $session_id |
||
| 2748 | * @param int $courseId |
||
| 2749 | * @param bool $nocoach optional, if is true the user don't be a coach now, |
||
| 2750 | * otherwise it'll assign a coach |
||
| 2751 | * @return bool true if there are affected rows, otherwise false |
||
| 2752 | */ |
||
| 2753 | public static function set_coach_to_course_session( |
||
| 2754 | $user_id, |
||
| 2755 | $session_id = 0, |
||
| 2756 | $courseId = 0, |
||
| 2757 | $nocoach = false |
||
| 2758 | ) { |
||
| 2759 | // Definition of variables |
||
| 2760 | $user_id = intval($user_id); |
||
| 2761 | |||
| 2762 | if (!empty($session_id)) { |
||
| 2763 | $session_id = intval($session_id); |
||
| 2764 | } else { |
||
| 2765 | $session_id = api_get_session_id(); |
||
| 2766 | } |
||
| 2767 | |||
| 2768 | if (!empty($courseId)) { |
||
| 2769 | $courseId = intval($courseId); |
||
| 2770 | } else { |
||
| 2771 | $courseId = api_get_course_id(); |
||
| 2772 | } |
||
| 2773 | |||
| 2774 | // Table definition |
||
| 2775 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 2776 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2777 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 2778 | |||
| 2779 | // check if user is a teacher |
||
| 2780 | $sql = "SELECT * FROM $tbl_user |
||
| 2781 | WHERE status = 1 AND user_id = $user_id"; |
||
| 2782 | |||
| 2783 | $rs_check_user = Database::query($sql); |
||
| 2784 | |||
| 2785 | if (Database::num_rows($rs_check_user) > 0) { |
||
| 2786 | if ($nocoach) { |
||
| 2787 | // check if user_id exists in session_rel_user (if the user is |
||
| 2788 | // subscribed to the session in any manner) |
||
| 2789 | $sql = "SELECT user_id FROM $tbl_session_rel_user |
||
| 2790 | WHERE |
||
| 2791 | session_id = $session_id AND |
||
| 2792 | user_id = $user_id"; |
||
| 2793 | $res = Database::query($sql); |
||
| 2794 | |||
| 2795 | View Code Duplication | if (Database::num_rows($res) > 0) { |
|
| 2796 | // The user is already subscribed to the session. Change the |
||
| 2797 | // record so the user is NOT a coach for this course anymore |
||
| 2798 | // and then exit |
||
| 2799 | $sql = "UPDATE $tbl_session_rel_course_rel_user |
||
| 2800 | SET status = 0 |
||
| 2801 | WHERE |
||
| 2802 | session_id = $session_id AND |
||
| 2803 | c_id = $courseId AND |
||
| 2804 | user_id = $user_id "; |
||
| 2805 | $result = Database::query($sql); |
||
| 2806 | if (Database::affected_rows($result) > 0) |
||
| 2807 | return true; |
||
| 2808 | else |
||
| 2809 | return false; |
||
| 2810 | } else { |
||
| 2811 | // The user is not subscribed to the session, so make sure |
||
| 2812 | // he isn't subscribed to a course in this session either |
||
| 2813 | // and then exit |
||
| 2814 | $sql = "DELETE FROM $tbl_session_rel_course_rel_user |
||
| 2815 | WHERE |
||
| 2816 | session_id = $session_id AND |
||
| 2817 | c_id = $courseId AND |
||
| 2818 | user_id = $user_id "; |
||
| 2819 | $result = Database::query($sql); |
||
| 2820 | if (Database::affected_rows($result) > 0) |
||
| 2821 | return true; |
||
| 2822 | else |
||
| 2823 | return false; |
||
| 2824 | } |
||
| 2825 | } else { |
||
| 2826 | // Assign user as a coach to course |
||
| 2827 | // First check if the user is registered to the course |
||
| 2828 | $sql = "SELECT user_id FROM $tbl_session_rel_course_rel_user |
||
| 2829 | WHERE |
||
| 2830 | session_id = $session_id AND |
||
| 2831 | c_id = $courseId AND |
||
| 2832 | user_id = $user_id"; |
||
| 2833 | $rs_check = Database::query($sql); |
||
| 2834 | |||
| 2835 | // Then update or insert. |
||
| 2836 | View Code Duplication | if (Database::num_rows($rs_check) > 0) { |
|
| 2837 | $sql = "UPDATE $tbl_session_rel_course_rel_user SET status = 2 |
||
| 2838 | WHERE |
||
| 2839 | session_id = $session_id AND |
||
| 2840 | c_id = $courseId AND |
||
| 2841 | user_id = $user_id "; |
||
| 2842 | $result = Database::query($sql); |
||
| 2843 | if (Database::affected_rows($result) > 0) { |
||
| 2844 | return true; |
||
| 2845 | } else { |
||
| 2846 | return false; |
||
| 2847 | } |
||
| 2848 | } else { |
||
| 2849 | $sql = "INSERT INTO $tbl_session_rel_course_rel_user(session_id, c_id, user_id, status) |
||
| 2850 | VALUES($session_id, $courseId, $user_id, 2)"; |
||
| 2851 | $result = Database::query($sql); |
||
| 2852 | if (Database::affected_rows($result) > 0) { |
||
| 2853 | return true; |
||
| 2854 | } else { |
||
| 2855 | return false; |
||
| 2856 | } |
||
| 2857 | } |
||
| 2858 | } |
||
| 2859 | } else { |
||
| 2860 | return false; |
||
| 2861 | } |
||
| 2862 | } |
||
| 2863 | |||
| 2864 | /** |
||
| 2865 | * Subscribes sessions to human resource manager (Dashboard feature) |
||
| 2866 | * @param array $userInfo Human Resource Manager info |
||
| 2867 | * @param array $sessions_list Sessions id |
||
| 2868 | * @param bool $sendEmail |
||
| 2869 | * @param bool $removeOldConnections |
||
| 2870 | * @return int |
||
| 2871 | * */ |
||
| 2872 | public static function suscribe_sessions_to_hr_manager( |
||
| 2873 | $userInfo, |
||
| 2874 | $sessions_list, |
||
| 2875 | $sendEmail = false, |
||
| 2876 | $removeOldConnections = true |
||
| 2877 | ) { |
||
| 2878 | // Database Table Definitions |
||
| 2879 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2880 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2881 | |||
| 2882 | if (empty($userInfo)) { |
||
| 2883 | |||
| 2884 | return 0; |
||
| 2885 | } |
||
| 2886 | |||
| 2887 | $userId = $userInfo['user_id']; |
||
| 2888 | |||
| 2889 | // Only subscribe DRH users. |
||
| 2890 | $rolesAllowed = array( |
||
| 2891 | DRH, |
||
| 2892 | SESSIONADMIN, |
||
| 2893 | PLATFORM_ADMIN, |
||
| 2894 | COURSE_TUTOR |
||
| 2895 | ); |
||
| 2896 | $isAdmin = api_is_platform_admin_by_id($userInfo['user_id']); |
||
| 2897 | if (!$isAdmin && !in_array($userInfo['status'], $rolesAllowed)) { |
||
| 2898 | |||
| 2899 | return 0; |
||
| 2900 | } |
||
| 2901 | |||
| 2902 | $affected_rows = 0; |
||
| 2903 | |||
| 2904 | // Deleting assigned sessions to hrm_id. |
||
| 2905 | if ($removeOldConnections) { |
||
| 2906 | if (api_is_multiple_url_enabled()) { |
||
| 2907 | $sql = "SELECT session_id |
||
| 2908 | FROM $tbl_session_rel_user s |
||
| 2909 | INNER JOIN $tbl_session_rel_access_url a ON (a.session_id = s.session_id) |
||
| 2910 | WHERE |
||
| 2911 | s.user_id = $userId AND |
||
| 2912 | relation_type=" . SESSION_RELATION_TYPE_RRHH . " AND |
||
| 2913 | access_url_id = " . api_get_current_access_url_id() . ""; |
||
| 2914 | } else { |
||
| 2915 | $sql = "SELECT session_id FROM $tbl_session_rel_user s |
||
| 2916 | WHERE user_id = $userId AND relation_type=" . SESSION_RELATION_TYPE_RRHH . ""; |
||
| 2917 | } |
||
| 2918 | $result = Database::query($sql); |
||
| 2919 | |||
| 2920 | View Code Duplication | if (Database::num_rows($result) > 0) { |
|
| 2921 | while ($row = Database::fetch_array($result)) { |
||
| 2922 | $sql = "DELETE FROM $tbl_session_rel_user |
||
| 2923 | WHERE |
||
| 2924 | session_id = {$row['session_id']} AND |
||
| 2925 | user_id = $userId AND |
||
| 2926 | relation_type=" . SESSION_RELATION_TYPE_RRHH . " "; |
||
| 2927 | Database::query($sql); |
||
| 2928 | } |
||
| 2929 | } |
||
| 2930 | } |
||
| 2931 | // Inserting new sessions list. |
||
| 2932 | if (!empty($sessions_list) && is_array($sessions_list)) { |
||
| 2933 | |||
| 2934 | foreach ($sessions_list as $session_id) { |
||
| 2935 | $session_id = intval($session_id); |
||
| 2936 | $sql = "INSERT IGNORE INTO $tbl_session_rel_user (session_id, user_id, relation_type, registered_at) |
||
| 2937 | VALUES ( |
||
| 2938 | $session_id, |
||
| 2939 | $userId, |
||
| 2940 | '" . SESSION_RELATION_TYPE_RRHH . "', |
||
| 2941 | '" . api_get_utc_datetime() . "' |
||
| 2942 | )"; |
||
| 2943 | |||
| 2944 | Database::query($sql); |
||
| 2945 | $affected_rows++; |
||
| 2946 | } |
||
| 2947 | } |
||
| 2948 | |||
| 2949 | return $affected_rows; |
||
| 2950 | } |
||
| 2951 | |||
| 2952 | /** |
||
| 2953 | * @param int $sessionId |
||
| 2954 | * @return array |
||
| 2955 | */ |
||
| 2956 | public static function getDrhUsersInSession($sessionId) |
||
| 2957 | { |
||
| 2958 | return self::get_users_by_session($sessionId, SESSION_RELATION_TYPE_RRHH); |
||
| 2959 | } |
||
| 2960 | |||
| 2961 | /** |
||
| 2962 | * @param int $userId |
||
| 2963 | * @param int $sessionId |
||
| 2964 | * @return array |
||
| 2965 | */ |
||
| 2966 | public static function getSessionFollowedByDrh($userId, $sessionId) |
||
| 2967 | { |
||
| 2968 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 2969 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 2970 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 2971 | |||
| 2972 | $userId = intval($userId); |
||
| 2973 | $sessionId = intval($sessionId); |
||
| 2974 | |||
| 2975 | $select = " SELECT * "; |
||
| 2976 | if (api_is_multiple_url_enabled()) { |
||
| 2977 | $sql = " $select FROM $tbl_session s |
||
| 2978 | INNER JOIN $tbl_session_rel_user sru ON (sru.session_id = s.id) |
||
| 2979 | LEFT JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id) |
||
| 2980 | WHERE |
||
| 2981 | sru.user_id = '$userId' AND |
||
| 2982 | sru.session_id = '$sessionId' AND |
||
| 2983 | sru.relation_type = '" . SESSION_RELATION_TYPE_RRHH . "' AND |
||
| 2984 | access_url_id = " . api_get_current_access_url_id() . " |
||
| 2985 | "; |
||
| 2986 | } else { |
||
| 2987 | $sql = "$select FROM $tbl_session s |
||
| 2988 | INNER JOIN $tbl_session_rel_user sru |
||
| 2989 | ON |
||
| 2990 | sru.session_id = s.id AND |
||
| 2991 | sru.user_id = '$userId' AND |
||
| 2992 | sru.session_id = '$sessionId' AND |
||
| 2993 | sru.relation_type = '" . SESSION_RELATION_TYPE_RRHH . "' |
||
| 2994 | "; |
||
| 2995 | } |
||
| 2996 | |||
| 2997 | $result = Database::query($sql); |
||
| 2998 | if (Database::num_rows($result)) { |
||
| 2999 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 3000 | $row['course_list'] = self::get_course_list_by_session_id($sessionId); |
||
| 3001 | |||
| 3002 | return $row; |
||
| 3003 | } |
||
| 3004 | |||
| 3005 | return array(); |
||
| 3006 | } |
||
| 3007 | |||
| 3008 | /** |
||
| 3009 | * Get sessions followed by human resources manager |
||
| 3010 | * @param int $userId |
||
| 3011 | * @param int $start |
||
| 3012 | * @param int $limit |
||
| 3013 | * @param bool $getCount |
||
| 3014 | * @param bool $getOnlySessionId |
||
| 3015 | * @param bool $getSql |
||
| 3016 | * @param string $orderCondition |
||
| 3017 | * @param string $description |
||
| 3018 | * |
||
| 3019 | * @return array sessions |
||
| 3020 | */ |
||
| 3021 | public static function get_sessions_followed_by_drh( |
||
| 3022 | $userId, |
||
| 3023 | $start = null, |
||
| 3024 | $limit = null, |
||
| 3025 | $getCount = false, |
||
| 3026 | $getOnlySessionId = false, |
||
| 3027 | $getSql = false, |
||
| 3028 | $orderCondition = null, |
||
| 3029 | $keyword = '', |
||
| 3030 | $description = '' |
||
| 3031 | ) { |
||
| 3032 | return self::getSessionsFollowedByUser( |
||
| 3033 | $userId, |
||
| 3034 | DRH, |
||
| 3035 | $start, |
||
| 3036 | $limit, |
||
| 3037 | $getCount, |
||
| 3038 | $getOnlySessionId, |
||
| 3039 | $getSql, |
||
| 3040 | $orderCondition, |
||
| 3041 | $keyword, |
||
| 3042 | $description |
||
| 3043 | ); |
||
| 3044 | } |
||
| 3045 | |||
| 3046 | /** |
||
| 3047 | * Get sessions followed by human resources manager |
||
| 3048 | * @param int $userId |
||
| 3049 | * @param int $start |
||
| 3050 | * @param int $limit |
||
| 3051 | * @param bool $getCount |
||
| 3052 | * @param bool $getOnlySessionId |
||
| 3053 | * @param bool $getSql |
||
| 3054 | * @param string $orderCondition |
||
| 3055 | * @param string $keyword |
||
| 3056 | * @param string $description |
||
| 3057 | * @return array sessions |
||
| 3058 | */ |
||
| 3059 | public static function getSessionsFollowedByUser( |
||
| 3060 | $userId, |
||
| 3061 | $status = null, |
||
| 3062 | $start = null, |
||
| 3063 | $limit = null, |
||
| 3064 | $getCount = false, |
||
| 3065 | $getOnlySessionId = false, |
||
| 3066 | $getSql = false, |
||
| 3067 | $orderCondition = null, |
||
| 3068 | $keyword = '', |
||
| 3069 | $description = '' |
||
| 3070 | ) { |
||
| 3071 | // Database Table Definitions |
||
| 3072 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3073 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3074 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3075 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3076 | |||
| 3077 | $userId = intval($userId); |
||
| 3078 | |||
| 3079 | $select = " SELECT DISTINCT * "; |
||
| 3080 | |||
| 3081 | if ($getCount) { |
||
| 3082 | $select = " SELECT count(DISTINCT(s.id)) as count "; |
||
| 3083 | } |
||
| 3084 | |||
| 3085 | if ($getOnlySessionId) { |
||
| 3086 | $select = " SELECT DISTINCT(s.id) "; |
||
| 3087 | } |
||
| 3088 | |||
| 3089 | $limitCondition = null; |
||
| 3090 | View Code Duplication | if (!empty($start) && !empty($limit)) { |
|
| 3091 | $limitCondition = " LIMIT " . intval($start) . ", " . intval($limit); |
||
| 3092 | } |
||
| 3093 | |||
| 3094 | if (empty($orderCondition)) { |
||
| 3095 | $orderCondition = " ORDER BY s.name "; |
||
| 3096 | } |
||
| 3097 | |||
| 3098 | $whereConditions = null; |
||
| 3099 | $sessionCourseConditions = null; |
||
| 3100 | $sessionConditions = null; |
||
| 3101 | $sessionQuery = null; |
||
| 3102 | $courseSessionQuery = null; |
||
| 3103 | |||
| 3104 | switch ($status) { |
||
| 3105 | case DRH: |
||
| 3106 | $sessionQuery = "SELECT sru.session_id |
||
| 3107 | FROM |
||
| 3108 | $tbl_session_rel_user sru |
||
| 3109 | WHERE |
||
| 3110 | sru.relation_type = '".SESSION_RELATION_TYPE_RRHH."' AND |
||
| 3111 | sru.user_id = $userId"; |
||
| 3112 | break; |
||
| 3113 | case COURSEMANAGER: |
||
| 3114 | $courseSessionQuery = " |
||
| 3115 | SELECT scu.session_id as id |
||
| 3116 | FROM $tbl_session_rel_course_rel_user scu |
||
| 3117 | WHERE (scu.status = 2 AND scu.user_id = $userId)"; |
||
| 3118 | |||
| 3119 | $whereConditions = " OR (s.id_coach = $userId) "; |
||
| 3120 | break; |
||
| 3121 | default: |
||
| 3122 | $sessionQuery = "SELECT sru.session_id |
||
| 3123 | FROM |
||
| 3124 | $tbl_session_rel_user sru |
||
| 3125 | WHERE |
||
| 3126 | sru.user_id = $userId"; |
||
| 3127 | break; |
||
| 3128 | } |
||
| 3129 | |||
| 3130 | $keywordCondition = ''; |
||
| 3131 | View Code Duplication | if (!empty($keyword)) { |
|
| 3132 | $keyword = Database::escape_string($keyword); |
||
| 3133 | $keywordCondition = " AND (s.name LIKE '%$keyword%' ) "; |
||
| 3134 | |||
| 3135 | if (!empty($description)) { |
||
| 3136 | $description = Database::escape_string($description); |
||
| 3137 | $keywordCondition = " AND (s.name LIKE '%$keyword%' OR s.description LIKE '%$description%' ) "; |
||
| 3138 | } |
||
| 3139 | } |
||
| 3140 | |||
| 3141 | $whereConditions .= $keywordCondition; |
||
| 3142 | |||
| 3143 | $subQuery = $sessionQuery.$courseSessionQuery; |
||
| 3144 | |||
| 3145 | $sql = " $select FROM $tbl_session s |
||
| 3146 | INNER JOIN $tbl_session_rel_access_url a ON (s.id = a.session_id) |
||
| 3147 | WHERE |
||
| 3148 | access_url_id = ".api_get_current_access_url_id()." AND |
||
| 3149 | s.id IN ( |
||
| 3150 | $subQuery |
||
| 3151 | ) |
||
| 3152 | $whereConditions |
||
| 3153 | $orderCondition |
||
| 3154 | $limitCondition"; |
||
| 3155 | |||
| 3156 | if ($getSql) { |
||
| 3157 | return $sql; |
||
| 3158 | } |
||
| 3159 | |||
| 3160 | $result = Database::query($sql); |
||
| 3161 | |||
| 3162 | if ($getCount) { |
||
| 3163 | $row = Database::fetch_array($result); |
||
| 3164 | return $row['count']; |
||
| 3165 | } |
||
| 3166 | |||
| 3167 | $sessions = array(); |
||
| 3168 | if (Database::num_rows($result) > 0) { |
||
| 3169 | $sysUploadPath = api_get_path(SYS_UPLOAD_PATH). 'sessions/'; |
||
| 3170 | $webUploadPath = api_get_path(WEB_UPLOAD_PATH). 'sessions/'; |
||
| 3171 | $imgPath = Display::returnIconPath('session_default_small.png'); |
||
| 3172 | |||
| 3173 | $tableExtraFields = Database::get_main_table(TABLE_EXTRA_FIELD); |
||
| 3174 | $sql = "SELECT id FROM " . $tableExtraFields . " |
||
| 3175 | WHERE extra_field_type = 3 AND variable='image'"; |
||
| 3176 | $resultField = Database::query($sql); |
||
| 3177 | $imageFieldId = Database::fetch_assoc($resultField); |
||
| 3178 | |||
| 3179 | while ($row = Database::fetch_array($result)) { |
||
| 3180 | |||
| 3181 | $row['image'] = null; |
||
| 3182 | $sessionImage = $sysUploadPath . $imageFieldId['id'] . '_' . $row['id'] . '.png'; |
||
| 3183 | |||
| 3184 | if (is_file($sessionImage)) { |
||
| 3185 | $sessionImage = $webUploadPath . $imageFieldId['id'] . '_' . $row['id'] . '.png'; |
||
| 3186 | $row['image'] = $sessionImage; |
||
| 3187 | } else { |
||
| 3188 | $row['image'] = $imgPath; |
||
| 3189 | } |
||
| 3190 | |||
| 3191 | $sessions[$row['id']] = $row; |
||
| 3192 | |||
| 3193 | } |
||
| 3194 | } |
||
| 3195 | |||
| 3196 | return $sessions; |
||
| 3197 | } |
||
| 3198 | |||
| 3199 | /** |
||
| 3200 | * Gets the list (or the count) of courses by session filtered by access_url |
||
| 3201 | * @param int $session_id The session id |
||
| 3202 | * @param string $course_name The course code |
||
| 3203 | * @param string $orderBy Field to order the data |
||
| 3204 | * @param boolean $getCount Optional. Count the session courses |
||
| 3205 | * @return array|int List of courses. Whether $getCount is true, return the count |
||
| 3206 | */ |
||
| 3207 | public static function get_course_list_by_session_id( |
||
| 3208 | $session_id, |
||
| 3209 | $course_name = '', |
||
| 3210 | $orderBy = null, |
||
| 3211 | $getCount = false |
||
| 3212 | ) { |
||
| 3213 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3214 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3215 | |||
| 3216 | $session_id = intval($session_id); |
||
| 3217 | |||
| 3218 | $sqlSelect = "SELECT *"; |
||
| 3219 | |||
| 3220 | if ($getCount) { |
||
| 3221 | $sqlSelect = "SELECT COUNT(1)"; |
||
| 3222 | } |
||
| 3223 | |||
| 3224 | // select the courses |
||
| 3225 | $sql = "SELECT *, c.id, c.id as real_id |
||
| 3226 | FROM $tbl_course c |
||
| 3227 | INNER JOIN $tbl_session_rel_course src |
||
| 3228 | ON c.id = src.c_id |
||
| 3229 | WHERE src.session_id = '$session_id' "; |
||
| 3230 | |||
| 3231 | if (!empty($course_name)) { |
||
| 3232 | $course_name = Database::escape_string($course_name); |
||
| 3233 | $sql .= " AND c.title LIKE '%$course_name%' "; |
||
| 3234 | } |
||
| 3235 | |||
| 3236 | if (!empty($orderBy)) { |
||
| 3237 | $orderBy = Database::escape_string($orderBy); |
||
| 3238 | $orderBy = " ORDER BY $orderBy"; |
||
| 3239 | } else { |
||
| 3240 | if (SessionManager::orderCourseIsEnabled()) { |
||
| 3241 | $orderBy .= " ORDER BY position "; |
||
| 3242 | } else { |
||
| 3243 | $orderBy .= " ORDER BY title "; |
||
| 3244 | } |
||
| 3245 | } |
||
| 3246 | |||
| 3247 | $sql .= Database::escape_string($orderBy); |
||
| 3248 | $result = Database::query($sql); |
||
| 3249 | $num_rows = Database::num_rows($result); |
||
| 3250 | $courses = array(); |
||
| 3251 | View Code Duplication | if ($num_rows > 0) { |
|
| 3252 | if ($getCount) { |
||
| 3253 | $count = Database::fetch_array($result); |
||
| 3254 | |||
| 3255 | return intval($count[0]); |
||
| 3256 | } |
||
| 3257 | |||
| 3258 | while ($row = Database::fetch_array($result,'ASSOC')) { |
||
| 3259 | $courses[$row['real_id']] = $row; |
||
| 3260 | } |
||
| 3261 | } |
||
| 3262 | |||
| 3263 | return $courses; |
||
| 3264 | } |
||
| 3265 | |||
| 3266 | /** |
||
| 3267 | * Gets the list of courses by session filtered by access_url |
||
| 3268 | * |
||
| 3269 | * @param $userId |
||
| 3270 | * @param $sessionId |
||
| 3271 | * @param null $from |
||
| 3272 | * @param null $limit |
||
| 3273 | * @param null $column |
||
| 3274 | * @param null $direction |
||
| 3275 | * @param bool $getCount |
||
| 3276 | * @return array |
||
| 3277 | */ |
||
| 3278 | public static function getAllCoursesFollowedByUser( |
||
| 3279 | $userId, |
||
| 3280 | $sessionId, |
||
| 3281 | $from = null, |
||
| 3282 | $limit = null, |
||
| 3283 | $column = null, |
||
| 3284 | $direction = null, |
||
| 3285 | $getCount = false, |
||
| 3286 | $keyword = null |
||
| 3287 | ) { |
||
| 3288 | if (empty($sessionId)) { |
||
| 3289 | $sessionsSQL = SessionManager::get_sessions_followed_by_drh( |
||
| 3290 | $userId, |
||
| 3291 | null, |
||
| 3292 | null, |
||
| 3293 | null, |
||
| 3294 | true, |
||
| 3295 | true |
||
| 3296 | ); |
||
| 3297 | } else { |
||
| 3298 | $sessionsSQL = intval($sessionId); |
||
| 3299 | } |
||
| 3300 | |||
| 3301 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3302 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3303 | |||
| 3304 | if ($getCount) { |
||
| 3305 | $select = "SELECT COUNT(DISTINCT(c.code)) as count "; |
||
| 3306 | } else { |
||
| 3307 | $select = "SELECT DISTINCT c.* "; |
||
| 3308 | } |
||
| 3309 | |||
| 3310 | $keywordCondition = null; |
||
| 3311 | if (!empty($keyword)) { |
||
| 3312 | $keyword = Database::escape_string($keyword); |
||
| 3313 | $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) "; |
||
| 3314 | } |
||
| 3315 | |||
| 3316 | // Select the courses |
||
| 3317 | $sql = "$select |
||
| 3318 | FROM $tbl_course c |
||
| 3319 | INNER JOIN $tbl_session_rel_course src |
||
| 3320 | ON c.id = src.c_id |
||
| 3321 | WHERE |
||
| 3322 | src.session_id IN ($sessionsSQL) |
||
| 3323 | $keywordCondition |
||
| 3324 | "; |
||
| 3325 | if ($getCount) { |
||
| 3326 | $result = Database::query($sql); |
||
| 3327 | $row = Database::fetch_array($result,'ASSOC'); |
||
| 3328 | return $row['count']; |
||
| 3329 | } |
||
| 3330 | |||
| 3331 | View Code Duplication | if (isset($from) && isset($limit)) { |
|
| 3332 | $from = intval($from); |
||
| 3333 | $limit = intval($limit); |
||
| 3334 | $sql .= " LIMIT $from, $limit"; |
||
| 3335 | } |
||
| 3336 | |||
| 3337 | $result = Database::query($sql); |
||
| 3338 | $num_rows = Database::num_rows($result); |
||
| 3339 | $courses = array(); |
||
| 3340 | |||
| 3341 | if ($num_rows > 0) { |
||
| 3342 | while ($row = Database::fetch_array($result,'ASSOC')) { |
||
| 3343 | $courses[$row['id']] = $row; |
||
| 3344 | } |
||
| 3345 | } |
||
| 3346 | |||
| 3347 | return $courses; |
||
| 3348 | } |
||
| 3349 | |||
| 3350 | /** |
||
| 3351 | * Gets the list of courses by session filtered by access_url |
||
| 3352 | * @param int $session_id |
||
| 3353 | * @param string $course_name |
||
| 3354 | * @return array list of courses |
||
| 3355 | */ |
||
| 3356 | public static function get_course_list_by_session_id_like($session_id, $course_name = '') |
||
| 3357 | { |
||
| 3358 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3359 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3360 | |||
| 3361 | $session_id = intval($session_id); |
||
| 3362 | $course_name = Database::escape_string($course_name); |
||
| 3363 | |||
| 3364 | // select the courses |
||
| 3365 | $sql = "SELECT c.id, c.title FROM $tbl_course c |
||
| 3366 | INNER JOIN $tbl_session_rel_course src |
||
| 3367 | ON c.id = src.c_id |
||
| 3368 | WHERE "; |
||
| 3369 | |||
| 3370 | if (!empty($session_id)) { |
||
| 3371 | $sql .= "src.session_id LIKE '$session_id' AND "; |
||
| 3372 | } |
||
| 3373 | |||
| 3374 | if (!empty($course_name)) { |
||
| 3375 | $sql .= "UPPER(c.title) LIKE UPPER('%$course_name%') "; |
||
| 3376 | } |
||
| 3377 | |||
| 3378 | $sql .= "ORDER BY title;"; |
||
| 3379 | $result = Database::query($sql); |
||
| 3380 | $num_rows = Database::num_rows($result); |
||
| 3381 | $courses = array(); |
||
| 3382 | if ($num_rows > 0) { |
||
| 3383 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 3384 | $courses[$row['id']] = $row; |
||
| 3385 | } |
||
| 3386 | } |
||
| 3387 | |||
| 3388 | return $courses; |
||
| 3389 | } |
||
| 3390 | |||
| 3391 | |||
| 3392 | /** |
||
| 3393 | * Gets the count of courses by session filtered by access_url |
||
| 3394 | * @param int session id |
||
| 3395 | * @return array list of courses |
||
| 3396 | */ |
||
| 3397 | public static function getCourseCountBySessionId($session_id, $keyword = null) |
||
| 3398 | { |
||
| 3399 | $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE); |
||
| 3400 | $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3401 | $session_id = intval($session_id); |
||
| 3402 | |||
| 3403 | // select the courses |
||
| 3404 | $sql = "SELECT COUNT(c.code) count |
||
| 3405 | FROM $tbl_course c |
||
| 3406 | INNER JOIN $tbl_session_rel_course src |
||
| 3407 | ON c.id = src.c_id |
||
| 3408 | WHERE src.session_id = '$session_id' "; |
||
| 3409 | |||
| 3410 | $keywordCondition = null; |
||
| 3411 | if (!empty($keyword)) { |
||
| 3412 | $keyword = Database::escape_string($keyword); |
||
| 3413 | $keywordCondition = " AND (c.code LIKE '%$keyword%' OR c.title LIKE '%$keyword%' ) "; |
||
| 3414 | } |
||
| 3415 | $sql .= $keywordCondition; |
||
| 3416 | |||
| 3417 | $result = Database::query($sql); |
||
| 3418 | $num_rows = Database::num_rows($result); |
||
| 3419 | if ($num_rows > 0) { |
||
| 3420 | $row = Database::fetch_array($result,'ASSOC'); |
||
| 3421 | return $row['count']; |
||
| 3422 | } |
||
| 3423 | |||
| 3424 | return null; |
||
| 3425 | } |
||
| 3426 | |||
| 3427 | /** |
||
| 3428 | * Get the session id based on the original id and field name in the extra fields. |
||
| 3429 | * Returns 0 if session was not found |
||
| 3430 | * |
||
| 3431 | * @param string $value Original session id |
||
| 3432 | * @param string $variable Original field name |
||
| 3433 | * @return int Session id |
||
| 3434 | */ |
||
| 3435 | public static function getSessionIdFromOriginalId($value, $variable) |
||
| 3436 | { |
||
| 3437 | $extraFieldValue = new ExtraFieldValue('session'); |
||
| 3438 | $result = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 3439 | $variable, |
||
| 3440 | $value |
||
| 3441 | ); |
||
| 3442 | |||
| 3443 | if (!empty($result)) { |
||
| 3444 | return $result['item_id']; |
||
| 3445 | } |
||
| 3446 | |||
| 3447 | return 0; |
||
| 3448 | } |
||
| 3449 | |||
| 3450 | /** |
||
| 3451 | * Get users by session |
||
| 3452 | * @param int $id session id |
||
| 3453 | * @param int $status filter by status coach = 2 |
||
| 3454 | * @return array a list with an user list |
||
| 3455 | */ |
||
| 3456 | public static function get_users_by_session($id, $status = null) |
||
| 3457 | { |
||
| 3458 | if (empty($id)) { |
||
| 3459 | return array(); |
||
| 3460 | } |
||
| 3461 | $id = intval($id); |
||
| 3462 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3463 | $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 3464 | $table_access_url_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER); |
||
| 3465 | |||
| 3466 | $sql = "SELECT |
||
| 3467 | u.user_id, |
||
| 3468 | lastname, |
||
| 3469 | firstname, |
||
| 3470 | username, |
||
| 3471 | relation_type, |
||
| 3472 | access_url_id |
||
| 3473 | FROM $tbl_user u |
||
| 3474 | INNER JOIN $tbl_session_rel_user |
||
| 3475 | ON u.user_id = $tbl_session_rel_user.user_id AND |
||
| 3476 | $tbl_session_rel_user.session_id = $id |
||
| 3477 | LEFT OUTER JOIN $table_access_url_user uu |
||
| 3478 | ON (uu.user_id = u.user_id) |
||
| 3479 | "; |
||
| 3480 | |||
| 3481 | $urlId = api_get_current_access_url_id(); |
||
| 3482 | if (isset($status) && $status != '') { |
||
| 3483 | $status = intval($status); |
||
| 3484 | $sql .= " WHERE relation_type = $status AND (access_url_id = $urlId OR access_url_id is null )"; |
||
| 3485 | } else { |
||
| 3486 | $sql .= " WHERE (access_url_id = $urlId OR access_url_id is null )"; |
||
| 3487 | } |
||
| 3488 | |||
| 3489 | $sql .= " ORDER BY relation_type, "; |
||
| 3490 | $sql .= api_sort_by_first_name() ? ' firstname, lastname' : ' lastname, firstname'; |
||
| 3491 | |||
| 3492 | $result = Database::query($sql); |
||
| 3493 | |||
| 3494 | $return = array(); |
||
| 3495 | while ($row = Database::fetch_array($result, 'ASSOC')) { |
||
| 3496 | $return[] = $row; |
||
| 3497 | } |
||
| 3498 | |||
| 3499 | return $return; |
||
| 3500 | } |
||
| 3501 | |||
| 3502 | /** |
||
| 3503 | * The general coach (field: session.id_coach) |
||
| 3504 | * @param int $user_id user id |
||
| 3505 | * @param boolean $asPlatformAdmin The user is platform admin, return everything |
||
| 3506 | * @return array |
||
| 3507 | */ |
||
| 3508 | public static function get_sessions_by_general_coach($user_id, $asPlatformAdmin = false) |
||
| 3509 | { |
||
| 3510 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3511 | $user_id = intval($user_id); |
||
| 3512 | |||
| 3513 | // Session where we are general coach |
||
| 3514 | $sql = "SELECT DISTINCT * |
||
| 3515 | FROM $session_table"; |
||
| 3516 | |||
| 3517 | if (!$asPlatformAdmin) { |
||
| 3518 | $sql .= " WHERE id_coach = $user_id"; |
||
| 3519 | } |
||
| 3520 | |||
| 3521 | if (api_is_multiple_url_enabled()) { |
||
| 3522 | $tbl_session_rel_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3523 | $access_url_id = api_get_current_access_url_id(); |
||
| 3524 | |||
| 3525 | $sqlCoach = ''; |
||
| 3526 | if (!$asPlatformAdmin) { |
||
| 3527 | $sqlCoach = " id_coach = $user_id AND "; |
||
| 3528 | } |
||
| 3529 | |||
| 3530 | if ($access_url_id != -1) { |
||
| 3531 | $sql = 'SELECT DISTINCT session.* |
||
| 3532 | FROM ' . $session_table . ' session INNER JOIN ' . $tbl_session_rel_access_url . ' session_rel_url |
||
| 3533 | ON (session.id = session_rel_url.session_id) |
||
| 3534 | WHERE '.$sqlCoach.' access_url_id = ' . $access_url_id; |
||
| 3535 | } |
||
| 3536 | } |
||
| 3537 | $sql .= ' ORDER by name'; |
||
| 3538 | $result = Database::query($sql); |
||
| 3539 | |||
| 3540 | return Database::store_result($result, 'ASSOC'); |
||
| 3541 | } |
||
| 3542 | |||
| 3543 | /** |
||
| 3544 | * @param int $user_id |
||
| 3545 | * @return array |
||
| 3546 | * @deprecated use get_sessions_by_general_coach() |
||
| 3547 | */ |
||
| 3548 | public static function get_sessions_by_coach($user_id) |
||
| 3549 | { |
||
| 3550 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3551 | return Database::select('*', $session_table, array('where' => array('id_coach = ?' => $user_id))); |
||
| 3552 | } |
||
| 3553 | |||
| 3554 | /** |
||
| 3555 | * @param int $user_id |
||
| 3556 | * @param int $courseId |
||
| 3557 | * @param int $session_id |
||
| 3558 | * @return array|bool |
||
| 3559 | */ |
||
| 3560 | View Code Duplication | public static function get_user_status_in_course_session($user_id, $courseId, $session_id) |
|
| 3561 | { |
||
| 3562 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3563 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3564 | $sql = "SELECT session_rcru.status |
||
| 3565 | FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user |
||
| 3566 | WHERE |
||
| 3567 | session_rcru.user_id = user.user_id AND |
||
| 3568 | session_rcru.session_id = '" . intval($session_id) . "' AND |
||
| 3569 | session_rcru.c_id ='" . intval($courseId) . "' AND |
||
| 3570 | user.user_id = " . intval($user_id); |
||
| 3571 | |||
| 3572 | $result = Database::query($sql); |
||
| 3573 | $status = false; |
||
| 3574 | if (Database::num_rows($result)) { |
||
| 3575 | $status = Database::fetch_row($result); |
||
| 3576 | $status = $status['0']; |
||
| 3577 | } |
||
| 3578 | |||
| 3579 | return $status; |
||
| 3580 | } |
||
| 3581 | |||
| 3582 | /** |
||
| 3583 | * Gets user status within a session |
||
| 3584 | * @param int $user_id |
||
| 3585 | * @param int $courseId |
||
| 3586 | * @param $session_id |
||
| 3587 | * @return int |
||
| 3588 | * @assert (null,null,null) === false |
||
| 3589 | */ |
||
| 3590 | public static function get_user_status_in_session($user_id, $courseId, $session_id) |
||
| 3591 | { |
||
| 3592 | if (empty($user_id) or empty($courseId) or empty($session_id)) { |
||
| 3593 | return false; |
||
| 3594 | } |
||
| 3595 | $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 3596 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 3597 | $sql = "SELECT session_rcru.status |
||
| 3598 | FROM $tbl_session_rel_course_rel_user session_rcru, $tbl_user user |
||
| 3599 | WHERE session_rcru.user_id = user.user_id AND |
||
| 3600 | session_rcru.session_id = '" . intval($session_id) . "' AND |
||
| 3601 | session_rcru.c_id ='" . intval($courseId) . "' AND |
||
| 3602 | user.user_id = " . intval($user_id); |
||
| 3603 | $result = Database::query($sql); |
||
| 3604 | $status = false; |
||
| 3605 | if (Database::num_rows($result)) { |
||
| 3606 | $status = Database::fetch_row($result); |
||
| 3607 | $status = $status['0']; |
||
| 3608 | } |
||
| 3609 | return $status; |
||
| 3610 | } |
||
| 3611 | |||
| 3612 | /** |
||
| 3613 | * @param int $id |
||
| 3614 | * @return array |
||
| 3615 | */ |
||
| 3616 | public static function get_all_sessions_by_promotion($id) |
||
| 3617 | { |
||
| 3618 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3619 | return Database::select('*', $t, array('where' => array('promotion_id = ?' => $id))); |
||
| 3620 | } |
||
| 3621 | |||
| 3622 | /** |
||
| 3623 | * @param int $promotion_id |
||
| 3624 | * @param array $list |
||
| 3625 | */ |
||
| 3626 | public static function suscribe_sessions_to_promotion($promotion_id, $list) |
||
| 3627 | { |
||
| 3628 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3629 | $params = array(); |
||
| 3630 | $params['promotion_id'] = 0; |
||
| 3631 | Database::update($t, $params, array('promotion_id = ?' => $promotion_id)); |
||
| 3632 | |||
| 3633 | $params['promotion_id'] = $promotion_id; |
||
| 3634 | if (!empty($list)) { |
||
| 3635 | foreach ($list as $session_id) { |
||
| 3636 | $session_id = intval($session_id); |
||
| 3637 | Database::update($t, $params, array('id = ?' => $session_id)); |
||
| 3638 | } |
||
| 3639 | } |
||
| 3640 | } |
||
| 3641 | |||
| 3642 | /** |
||
| 3643 | * Updates a session status |
||
| 3644 | * @param int session id |
||
| 3645 | * @param int status |
||
| 3646 | */ |
||
| 3647 | public static function set_session_status($session_id, $status) |
||
| 3648 | { |
||
| 3649 | $t = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3650 | $params['visibility'] = $status; |
||
| 3651 | Database::update($t, $params, array('id = ?' => $session_id)); |
||
| 3652 | } |
||
| 3653 | |||
| 3654 | /** |
||
| 3655 | * Copies a session with the same data to a new session. |
||
| 3656 | * The new copy is not assigned to the same promotion. @see suscribe_sessions_to_promotions() for that |
||
| 3657 | * @param int Session ID |
||
| 3658 | * @param bool Whether to copy the relationship with courses |
||
| 3659 | * @param bool Whether to copy the relationship with users |
||
| 3660 | * @param bool New courses will be created |
||
| 3661 | * @param bool Whether to set exercises and learning paths in the new session to invisible by default |
||
| 3662 | * @return int The new session ID on success, 0 otherwise |
||
| 3663 | * @todo make sure the extra session fields are copied too |
||
| 3664 | */ |
||
| 3665 | public static function copy( |
||
| 3666 | $id, |
||
| 3667 | $copy_courses = true, |
||
| 3668 | $copy_users = true, |
||
| 3669 | $create_new_courses = false, |
||
| 3670 | $set_exercises_lp_invisible = false |
||
| 3671 | ) { |
||
| 3672 | $id = intval($id); |
||
| 3673 | $s = self::fetch($id); |
||
| 3674 | // Check all dates before copying |
||
| 3675 | // Get timestamp for now in UTC - see http://php.net/manual/es/function.time.php#117251 |
||
| 3676 | $now = time() - date('Z'); |
||
| 3677 | // Timestamp in one month |
||
| 3678 | $inOneMonth = $now + (30*24*3600); |
||
| 3679 | $inOneMonth = api_get_local_time($inOneMonth); |
||
| 3680 | if (api_strtotime($s['access_start_date']) < $now) { |
||
| 3681 | $s['access_start_date'] = api_get_local_time($now); |
||
| 3682 | } |
||
| 3683 | if (api_strtotime($s['display_start_date']) < $now) { |
||
| 3684 | $s['display_start_date'] = api_get_local_time($now); |
||
| 3685 | } |
||
| 3686 | if (api_strtotime($s['coach_access_start_date']) < $now) { |
||
| 3687 | $s['coach_access_start_date'] = api_get_local_time($now); |
||
| 3688 | } |
||
| 3689 | if (api_strtotime($s['access_end_date']) < $now) { |
||
| 3690 | $s['access_end_date'] = $inOneMonth; |
||
| 3691 | } |
||
| 3692 | if (api_strtotime($s['display_end_date']) < $now) { |
||
| 3693 | $s['display_end_date'] = $inOneMonth; |
||
| 3694 | } |
||
| 3695 | if (api_strtotime($s['coach_access_end_date']) < $now) { |
||
| 3696 | $s['coach_access_end_date'] = $inOneMonth; |
||
| 3697 | } |
||
| 3698 | // Now try to create the session |
||
| 3699 | $sid = self::create_session( |
||
| 3700 | $s['name'] . ' ' . get_lang('CopyLabelSuffix'), |
||
| 3701 | $s['access_start_date'], |
||
| 3702 | $s['access_end_date'], |
||
| 3703 | $s['display_start_date'], |
||
| 3704 | $s['display_end_date'], |
||
| 3705 | $s['coach_access_start_date'], |
||
| 3706 | $s['coach_access_end_date'], |
||
| 3707 | (int)$s['id_coach'], |
||
| 3708 | $s['session_category_id'], |
||
| 3709 | (int)$s['visibility'], |
||
| 3710 | true |
||
| 3711 | ); |
||
| 3712 | |||
| 3713 | if (!is_numeric($sid) || empty($sid)) { |
||
| 3714 | return false; |
||
| 3715 | } |
||
| 3716 | |||
| 3717 | if ($copy_courses) { |
||
| 3718 | // Register courses from the original session to the new session |
||
| 3719 | $courses = self::get_course_list_by_session_id($id); |
||
| 3720 | |||
| 3721 | $short_courses = $new_short_courses = array(); |
||
| 3722 | if (is_array($courses) && count($courses) > 0) { |
||
| 3723 | foreach ($courses as $course) { |
||
| 3724 | $short_courses[] = $course; |
||
| 3725 | } |
||
| 3726 | } |
||
| 3727 | |||
| 3728 | $courses = null; |
||
| 3729 | |||
| 3730 | //We will copy the current courses of the session to new courses |
||
| 3731 | if (!empty($short_courses)) { |
||
| 3732 | if ($create_new_courses) { |
||
| 3733 | //Just in case |
||
| 3734 | if (function_exists('ini_set')) { |
||
| 3735 | api_set_memory_limit('256M'); |
||
| 3736 | ini_set('max_execution_time', 0); |
||
| 3737 | } |
||
| 3738 | $params = array(); |
||
| 3739 | $params['skip_lp_dates'] = true; |
||
| 3740 | |||
| 3741 | foreach ($short_courses as $course_data) { |
||
| 3742 | $course_info = CourseManager::copy_course_simple( |
||
| 3743 | $course_data['title'].' '.get_lang( |
||
| 3744 | 'CopyLabelSuffix' |
||
| 3745 | ), |
||
| 3746 | $course_data['course_code'], |
||
| 3747 | $id, |
||
| 3748 | $sid, |
||
| 3749 | $params |
||
| 3750 | ); |
||
| 3751 | |||
| 3752 | if ($course_info) { |
||
| 3753 | //By default new elements are invisible |
||
| 3754 | if ($set_exercises_lp_invisible) { |
||
| 3755 | $list = new LearnpathList('', $course_info['code'], $sid); |
||
| 3756 | $flat_list = $list->get_flat_list(); |
||
| 3757 | if (!empty($flat_list)) { |
||
| 3758 | foreach ($flat_list as $lp_id => $data) { |
||
| 3759 | api_item_property_update( |
||
| 3760 | $course_info, |
||
| 3761 | TOOL_LEARNPATH, |
||
| 3762 | $lp_id, |
||
| 3763 | 'invisible', |
||
| 3764 | api_get_user_id(), |
||
| 3765 | 0, |
||
| 3766 | 0, |
||
| 3767 | 0, |
||
| 3768 | 0, |
||
| 3769 | $sid |
||
| 3770 | ); |
||
| 3771 | } |
||
| 3772 | } |
||
| 3773 | $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 3774 | $course_id = $course_info['real_id']; |
||
| 3775 | //@todo check this query |
||
| 3776 | $sql = "UPDATE $quiz_table SET active = 0 |
||
| 3777 | WHERE c_id = $course_id AND session_id = $sid"; |
||
| 3778 | Database::query($sql); |
||
| 3779 | } |
||
| 3780 | $new_short_courses[] = $course_info['real_id']; |
||
| 3781 | } |
||
| 3782 | } |
||
| 3783 | } else { |
||
| 3784 | foreach ($short_courses as $course_data) { |
||
| 3785 | $new_short_courses[] = $course_data['id']; |
||
| 3786 | } |
||
| 3787 | } |
||
| 3788 | |||
| 3789 | $short_courses = $new_short_courses; |
||
| 3790 | self::add_courses_to_session($sid, $short_courses, true); |
||
| 3791 | $short_courses = null; |
||
| 3792 | } |
||
| 3793 | } |
||
| 3794 | if ($copy_users) { |
||
| 3795 | // Register users from the original session to the new session |
||
| 3796 | $users = self::get_users_by_session($id); |
||
| 3797 | $short_users = array(); |
||
| 3798 | if (is_array($users) && count($users) > 0) { |
||
| 3799 | foreach ($users as $user) { |
||
| 3800 | $short_users[] = $user['user_id']; |
||
| 3801 | } |
||
| 3802 | } |
||
| 3803 | $users = null; |
||
| 3804 | //Subscribing in read only mode |
||
| 3805 | self::suscribe_users_to_session($sid, $short_users, SESSION_VISIBLE_READ_ONLY, true); |
||
| 3806 | $short_users = null; |
||
| 3807 | } |
||
| 3808 | return $sid; |
||
| 3809 | } |
||
| 3810 | |||
| 3811 | /** |
||
| 3812 | * @param int $user_id |
||
| 3813 | * @param int $session_id |
||
| 3814 | * @return bool |
||
| 3815 | */ |
||
| 3816 | static function user_is_general_coach($user_id, $session_id) |
||
| 3817 | { |
||
| 3818 | $session_id = intval($session_id); |
||
| 3819 | $user_id = intval($user_id); |
||
| 3820 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3821 | $sql = "SELECT DISTINCT id |
||
| 3822 | FROM $session_table |
||
| 3823 | WHERE session.id_coach = '" . $user_id . "' AND id = '$session_id'"; |
||
| 3824 | $result = Database::query($sql); |
||
| 3825 | if ($result && Database::num_rows($result)) { |
||
| 3826 | return true; |
||
| 3827 | } |
||
| 3828 | return false; |
||
| 3829 | } |
||
| 3830 | |||
| 3831 | /** |
||
| 3832 | * Get the number of sessions |
||
| 3833 | * @param int ID of the URL we want to filter on (optional) |
||
| 3834 | * @return int Number of sessions |
||
| 3835 | */ |
||
| 3836 | public static function count_sessions($access_url_id = null) |
||
| 3837 | { |
||
| 3838 | $session_table = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3839 | $access_url_rel_session_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION); |
||
| 3840 | $sql = "SELECT count(id) FROM $session_table s"; |
||
| 3841 | if (!empty($access_url_id) && $access_url_id == intval($access_url_id)) { |
||
| 3842 | $sql .= ", $access_url_rel_session_table u " . |
||
| 3843 | " WHERE s.id = u.session_id AND u.access_url_id = $access_url_id"; |
||
| 3844 | } |
||
| 3845 | $res = Database::query($sql); |
||
| 3846 | $row = Database::fetch_row($res); |
||
| 3847 | return $row[0]; |
||
| 3848 | } |
||
| 3849 | |||
| 3850 | /** |
||
| 3851 | * Protect a session to be edited. |
||
| 3852 | * @param int $id |
||
| 3853 | * @param bool $checkSession |
||
| 3854 | */ |
||
| 3855 | public static function protectSession($id, $checkSession = true) |
||
| 3856 | { |
||
| 3857 | // api_protect_admin_script(true); |
||
| 3858 | if (self::allowToManageSessions()) { |
||
| 3859 | |||
| 3860 | if (api_is_platform_admin()) { |
||
| 3861 | return true; |
||
| 3862 | } |
||
| 3863 | |||
| 3864 | if ($checkSession) { |
||
| 3865 | if (self::allowed($id)) { |
||
| 3866 | return true; |
||
| 3867 | } else { |
||
| 3868 | api_not_allowed(true); |
||
| 3869 | } |
||
| 3870 | } |
||
| 3871 | } else { |
||
| 3872 | api_not_allowed(true); |
||
| 3873 | } |
||
| 3874 | } |
||
| 3875 | |||
| 3876 | /** |
||
| 3877 | * @param int $id |
||
| 3878 | * @return bool |
||
| 3879 | */ |
||
| 3880 | private static function allowed($id) |
||
| 3881 | { |
||
| 3882 | $sessionInfo = self::fetch($id); |
||
| 3883 | |||
| 3884 | if (empty($sessionInfo)) { |
||
| 3885 | return false; |
||
| 3886 | } |
||
| 3887 | |||
| 3888 | if (api_is_platform_admin()) { |
||
| 3889 | return true; |
||
| 3890 | } |
||
| 3891 | |||
| 3892 | $userId = api_get_user_id(); |
||
| 3893 | |||
| 3894 | if (api_is_session_admin() && |
||
| 3895 | api_get_setting('allow_session_admins_to_manage_all_sessions') != 'true' |
||
| 3896 | ) { |
||
| 3897 | if ($sessionInfo['session_admin_id'] != $userId) { |
||
| 3898 | return false; |
||
| 3899 | } |
||
| 3900 | } |
||
| 3901 | |||
| 3902 | if (api_is_teacher() && |
||
| 3903 | api_get_setting('allow_teachers_to_create_sessions') == 'true' |
||
| 3904 | ) { |
||
| 3905 | if ($sessionInfo['id_coach'] != $userId) { |
||
| 3906 | return false; |
||
| 3907 | } |
||
| 3908 | } |
||
| 3909 | |||
| 3910 | return true; |
||
| 3911 | } |
||
| 3912 | |||
| 3913 | /** |
||
| 3914 | * @return bool |
||
| 3915 | */ |
||
| 3916 | public static function allowToManageSessions() |
||
| 3917 | { |
||
| 3918 | if (self::allowManageAllSessions()) { |
||
| 3919 | return true; |
||
| 3920 | } |
||
| 3921 | |||
| 3922 | $setting = api_get_setting('allow_teachers_to_create_sessions'); |
||
| 3923 | |||
| 3924 | if (api_is_teacher() && $setting == 'true') { |
||
| 3925 | |||
| 3926 | return true; |
||
| 3927 | } |
||
| 3928 | |||
| 3929 | return false; |
||
| 3930 | } |
||
| 3931 | |||
| 3932 | /** |
||
| 3933 | * @return bool |
||
| 3934 | */ |
||
| 3935 | public static function allowOnlyMySessions() |
||
| 3936 | { |
||
| 3937 | if (self::allowToManageSessions() && |
||
| 3938 | !api_is_platform_admin() && |
||
| 3939 | api_is_teacher() |
||
| 3940 | ) { |
||
| 3941 | return true; |
||
| 3942 | } |
||
| 3943 | |||
| 3944 | return false; |
||
| 3945 | } |
||
| 3946 | |||
| 3947 | /** |
||
| 3948 | * @return bool |
||
| 3949 | */ |
||
| 3950 | public static function allowManageAllSessions() |
||
| 3951 | { |
||
| 3952 | if (api_is_platform_admin()) { |
||
| 3953 | return true; |
||
| 3954 | } |
||
| 3955 | |||
| 3956 | return false; |
||
| 3957 | } |
||
| 3958 | |||
| 3959 | /** |
||
| 3960 | * @param $id |
||
| 3961 | * @return bool |
||
| 3962 | */ |
||
| 3963 | public static function protect_teacher_session_edit($id) |
||
| 3964 | { |
||
| 3965 | if (!api_is_coach($id) && !api_is_platform_admin()) { |
||
| 3966 | api_not_allowed(true); |
||
| 3967 | } else { |
||
| 3968 | return true; |
||
| 3969 | } |
||
| 3970 | } |
||
| 3971 | |||
| 3972 | /** |
||
| 3973 | * @param int $courseId |
||
| 3974 | * @return array |
||
| 3975 | */ |
||
| 3976 | public static function get_session_by_course($courseId) |
||
| 3977 | { |
||
| 3978 | $table_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 3979 | $table_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 3980 | $courseId = intval($courseId); |
||
| 3981 | $sql = "SELECT name, s.id |
||
| 3982 | FROM $table_session_course sc |
||
| 3983 | INNER JOIN $table_session s ON (sc.session_id = s.id) |
||
| 3984 | WHERE sc.c_id = '$courseId' "; |
||
| 3985 | $result = Database::query($sql); |
||
| 3986 | |||
| 3987 | return Database::store_result($result); |
||
| 3988 | } |
||
| 3989 | |||
| 3990 | /** |
||
| 3991 | * @param int $user_id |
||
| 3992 | * @param bool $ignore_visibility_for_admins |
||
| 3993 | * @param bool $ignoreTimeLimit |
||
| 3994 | * |
||
| 3995 | * @return array |
||
| 3996 | */ |
||
| 3997 | public static function get_sessions_by_user($user_id, $ignore_visibility_for_admins = false, $ignoreTimeLimit = false) |
||
| 3998 | { |
||
| 3999 | $sessionCategories = UserManager::get_sessions_by_category( |
||
| 4000 | $user_id, |
||
| 4001 | false, |
||
| 4002 | $ignore_visibility_for_admins, |
||
| 4003 | $ignoreTimeLimit |
||
| 4004 | ); |
||
| 4005 | $sessionArray = array(); |
||
| 4006 | if (!empty($sessionCategories)) { |
||
| 4007 | foreach ($sessionCategories as $category) { |
||
| 4008 | if (isset($category['sessions'])) { |
||
| 4009 | foreach ($category['sessions'] as $session) { |
||
| 4010 | $sessionArray[] = $session; |
||
| 4011 | } |
||
| 4012 | } |
||
| 4013 | } |
||
| 4014 | } |
||
| 4015 | |||
| 4016 | return $sessionArray; |
||
| 4017 | } |
||
| 4018 | |||
| 4019 | /** |
||
| 4020 | * @param string $file |
||
| 4021 | * @param bool $updateSession options: |
||
| 4022 | * true: if the session exists it will be updated. |
||
| 4023 | * false: if session exists a new session will be created adding a counter session1, session2, etc |
||
| 4024 | * @param int $defaultUserId |
||
| 4025 | * @param mixed $logger |
||
| 4026 | * @param array $extraFields convert a file row to an extra field. Example in CSV file there's a SessionID then it will |
||
| 4027 | * converted to extra_external_session_id if you set this: array('SessionId' => 'extra_external_session_id') |
||
| 4028 | * @param string $extraFieldId |
||
| 4029 | * @param int $daysCoachAccessBeforeBeginning |
||
| 4030 | * @param int $daysCoachAccessAfterBeginning |
||
| 4031 | * @param int $sessionVisibility |
||
| 4032 | * @param array $fieldsToAvoidUpdate |
||
| 4033 | * @param bool $deleteUsersNotInList |
||
| 4034 | * @param bool $updateCourseCoaches |
||
| 4035 | * @param bool $sessionWithCoursesModifier |
||
| 4036 | * @param int $showDescription |
||
| 4037 | * @param array $teacherBackupList |
||
| 4038 | * @param array $groupBackup |
||
| 4039 | * @return array |
||
| 4040 | */ |
||
| 4041 | static function importCSV( |
||
| 4042 | $file, |
||
| 4043 | $updateSession, |
||
| 4044 | $defaultUserId = null, |
||
| 4045 | $logger = null, |
||
| 4046 | $extraFields = array(), |
||
| 4047 | $extraFieldId = null, |
||
| 4048 | $daysCoachAccessBeforeBeginning = null, |
||
| 4049 | $daysCoachAccessAfterBeginning = null, |
||
| 4050 | $sessionVisibility = 1, |
||
| 4051 | $fieldsToAvoidUpdate = array(), |
||
| 4052 | $deleteUsersNotInList = false, |
||
| 4053 | $updateCourseCoaches = false, |
||
| 4054 | $sessionWithCoursesModifier = false, |
||
| 4055 | $addOriginalCourseTeachersAsCourseSessionCoaches = true, |
||
| 4056 | $removeAllTeachersFromCourse = true, |
||
| 4057 | $showDescription = null, |
||
| 4058 | &$teacherBackupList = array(), |
||
| 4059 | &$groupBackup = array() |
||
| 4060 | ) { |
||
| 4061 | $content = file($file); |
||
| 4062 | |||
| 4063 | $error_message = null; |
||
| 4064 | $session_counter = 0; |
||
| 4065 | |||
| 4066 | if (empty($defaultUserId)) { |
||
| 4067 | $defaultUserId = api_get_user_id(); |
||
| 4068 | } |
||
| 4069 | |||
| 4070 | $eol = PHP_EOL; |
||
| 4071 | if (PHP_SAPI != 'cli') { |
||
| 4072 | $eol = '<br />'; |
||
| 4073 | } |
||
| 4074 | |||
| 4075 | $debug = false; |
||
| 4076 | if (isset($logger)) { |
||
| 4077 | $debug = true; |
||
| 4078 | } |
||
| 4079 | |||
| 4080 | $extraParameters = null; |
||
| 4081 | |||
| 4082 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4083 | $extraParameters .= ' , nb_days_access_before_beginning = '.intval($daysCoachAccessBeforeBeginning); |
||
| 4084 | $extraParameters .= ' , nb_days_access_after_end = '.intval($daysCoachAccessAfterBeginning); |
||
| 4085 | } |
||
| 4086 | |||
| 4087 | if (!is_null($showDescription)) { |
||
| 4088 | $extraParameters .= ' , show_description = '.intval($showDescription); |
||
| 4089 | } |
||
| 4090 | |||
| 4091 | $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION); |
||
| 4092 | $tbl_session_user = Database::get_main_table(TABLE_MAIN_SESSION_USER); |
||
| 4093 | $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE); |
||
| 4094 | $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER); |
||
| 4095 | |||
| 4096 | $sessions = array(); |
||
| 4097 | |||
| 4098 | if (!api_strstr($content[0], ';')) { |
||
| 4099 | $error_message = get_lang('NotCSV'); |
||
| 4100 | } else { |
||
| 4101 | $tag_names = array(); |
||
| 4102 | |||
| 4103 | View Code Duplication | foreach ($content as $key => $enreg) { |
|
| 4104 | $enreg = explode(';', trim($enreg)); |
||
| 4105 | if ($key) { |
||
| 4106 | foreach ($tag_names as $tag_key => $tag_name) { |
||
| 4107 | $sessions[$key - 1][$tag_name] = $enreg[$tag_key]; |
||
| 4108 | } |
||
| 4109 | } else { |
||
| 4110 | foreach ($enreg as $tag_name) { |
||
| 4111 | $tag_names[] = api_preg_replace('/[^a-zA-Z0-9_\-]/', '', $tag_name); |
||
| 4112 | } |
||
| 4113 | if (!in_array('SessionName', $tag_names) || |
||
| 4114 | !in_array('DateStart', $tag_names) || |
||
| 4115 | !in_array('DateEnd', $tag_names) |
||
| 4116 | ) { |
||
| 4117 | $error_message = get_lang('NoNeededData'); |
||
| 4118 | break; |
||
| 4119 | } |
||
| 4120 | } |
||
| 4121 | } |
||
| 4122 | |||
| 4123 | $sessionList = array(); |
||
| 4124 | // Looping the sessions. |
||
| 4125 | foreach ($sessions as $enreg) { |
||
| 4126 | $user_counter = 0; |
||
| 4127 | $course_counter = 0; |
||
| 4128 | |||
| 4129 | if (isset($extraFields) && !empty($extraFields)) { |
||
| 4130 | foreach ($extraFields as $original => $to) { |
||
| 4131 | $enreg[$to] = isset($enreg[$original]) ? $enreg[$original] : null; |
||
| 4132 | } |
||
| 4133 | } |
||
| 4134 | |||
| 4135 | $session_name = Database::escape_string($enreg['SessionName']); |
||
| 4136 | // Default visibility |
||
| 4137 | $visibilityAfterExpirationPerSession = $sessionVisibility; |
||
| 4138 | |||
| 4139 | if (isset($enreg['VisibilityAfterExpiration'])) { |
||
| 4140 | $visibility = $enreg['VisibilityAfterExpiration']; |
||
| 4141 | switch ($visibility) { |
||
| 4142 | case 'read_only': |
||
| 4143 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE_READ_ONLY; |
||
| 4144 | break; |
||
| 4145 | case 'accessible': |
||
| 4146 | $visibilityAfterExpirationPerSession = SESSION_VISIBLE; |
||
| 4147 | break; |
||
| 4148 | case 'not_accessible': |
||
| 4149 | $visibilityAfterExpirationPerSession = SESSION_INVISIBLE; |
||
| 4150 | break; |
||
| 4151 | } |
||
| 4152 | } |
||
| 4153 | |||
| 4154 | if (empty($session_name)) { |
||
| 4155 | continue; |
||
| 4156 | } |
||
| 4157 | |||
| 4158 | $date_start = $enreg['DateStart']; |
||
| 4159 | $date_end = $enreg['DateEnd']; |
||
| 4160 | $session_category_id = isset($enreg['SessionCategory']) ? $enreg['SessionCategory'] : null; |
||
| 4161 | $sessionDescription = isset($enreg['SessionDescription']) ? $enreg['SessionDescription'] : null; |
||
| 4162 | |||
| 4163 | $extraSessionParameters = null; |
||
| 4164 | if (!empty($sessionDescription)) { |
||
| 4165 | $extraSessionParameters = " , description = '".Database::escape_string($sessionDescription)."'"; |
||
| 4166 | } |
||
| 4167 | |||
| 4168 | // Searching a general coach. |
||
| 4169 | if (!empty($enreg['Coach'])) { |
||
| 4170 | $coach_id = UserManager::get_user_id_from_username($enreg['Coach']); |
||
| 4171 | if ($coach_id === false) { |
||
| 4172 | // If the coach-user does not exist - I'm the coach. |
||
| 4173 | $coach_id = $defaultUserId; |
||
| 4174 | } |
||
| 4175 | } else { |
||
| 4176 | $coach_id = $defaultUserId; |
||
| 4177 | } |
||
| 4178 | |||
| 4179 | if (!$updateSession) { |
||
| 4180 | // Always create a session. |
||
| 4181 | $unique_name = false; |
||
| 4182 | $i = 0; |
||
| 4183 | // Change session name, verify that session doesn't exist. |
||
| 4184 | $suffix = null; |
||
| 4185 | View Code Duplication | while (!$unique_name) { |
|
| 4186 | if ($i > 1) { |
||
| 4187 | $suffix = ' - ' . $i; |
||
| 4188 | } |
||
| 4189 | $sql = 'SELECT 1 FROM ' . $tbl_session . ' |
||
| 4190 | WHERE name="' . $session_name . $suffix . '"'; |
||
| 4191 | $rs = Database::query($sql); |
||
| 4192 | |||
| 4193 | if (Database::result($rs, 0, 0)) { |
||
| 4194 | $i++; |
||
| 4195 | } else { |
||
| 4196 | $unique_name = true; |
||
| 4197 | $session_name .= $suffix; |
||
| 4198 | } |
||
| 4199 | } |
||
| 4200 | |||
| 4201 | $sessionCondition = ''; |
||
| 4202 | if (!empty($session_category_id)) { |
||
| 4203 | $sessionCondition = "session_category_id = '$session_category_id',"; |
||
| 4204 | } |
||
| 4205 | |||
| 4206 | // Creating the session. |
||
| 4207 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4208 | name = '" . $session_name . "', |
||
| 4209 | id_coach = '$coach_id', |
||
| 4210 | access_start_date = '$date_start', |
||
| 4211 | access_end_date = '$date_end', |
||
| 4212 | visibility = '$visibilityAfterExpirationPerSession', |
||
| 4213 | $sessionCondition |
||
| 4214 | session_admin_id = " . intval($defaultUserId) . $extraParameters . $extraSessionParameters; |
||
| 4215 | Database::query($sql); |
||
| 4216 | |||
| 4217 | $session_id = Database::insert_id(); |
||
| 4218 | if ($debug) { |
||
| 4219 | if ($session_id) { |
||
| 4220 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4221 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4222 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4223 | } |
||
| 4224 | } |
||
| 4225 | |||
| 4226 | $logger->addInfo("Sessions - Session created: #$session_id - $session_name"); |
||
| 4227 | } else { |
||
| 4228 | $logger->addError("Sessions - Session NOT created: $session_name"); |
||
| 4229 | } |
||
| 4230 | } |
||
| 4231 | $session_counter++; |
||
| 4232 | } else { |
||
| 4233 | $sessionId = null; |
||
| 4234 | if (isset($extraFields) && !empty($extraFields) && !empty($enreg['extra_'.$extraFieldId])) { |
||
| 4235 | $sessionId = self::getSessionIdFromOriginalId($enreg['extra_'.$extraFieldId], $extraFieldId); |
||
| 4236 | if (empty($sessionId)) { |
||
| 4237 | $my_session_result = false; |
||
| 4238 | } else { |
||
| 4239 | $my_session_result = true; |
||
| 4240 | } |
||
| 4241 | } else { |
||
| 4242 | $my_session_result = self::get_session_by_name($enreg['SessionName']); |
||
| 4243 | } |
||
| 4244 | |||
| 4245 | if ($my_session_result === false) { |
||
| 4246 | |||
| 4247 | // Creating a session. |
||
| 4248 | $sql = "INSERT IGNORE INTO $tbl_session SET |
||
| 4249 | name = '$session_name', |
||
| 4250 | id_coach = '$coach_id', |
||
| 4251 | access_start_date = '$date_start', |
||
| 4252 | access_end_date = '$date_end', |
||
| 4253 | visibility = '$visibilityAfterExpirationPerSession', |
||
| 4254 | session_category_id = '$session_category_id' " . $extraParameters . $extraSessionParameters; |
||
| 4255 | |||
| 4256 | Database::query($sql); |
||
| 4257 | |||
| 4258 | // We get the last insert id. |
||
| 4259 | $my_session_result = SessionManager::get_session_by_name($enreg['SessionName']); |
||
| 4260 | $session_id = $my_session_result['id']; |
||
| 4261 | |||
| 4262 | if ($session_id) { |
||
| 4263 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4264 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4265 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4266 | } |
||
| 4267 | } |
||
| 4268 | if ($debug) { |
||
| 4269 | $logger->addInfo("Sessions - #$session_id created: $session_name"); |
||
| 4270 | } |
||
| 4271 | |||
| 4272 | // Delete session-user relation only for students |
||
| 4273 | $sql = "DELETE FROM $tbl_session_user |
||
| 4274 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4275 | Database::query($sql); |
||
| 4276 | |||
| 4277 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4278 | Database::query($sql); |
||
| 4279 | |||
| 4280 | // Delete session-course-user relationships students and coaches. |
||
| 4281 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4282 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4283 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4284 | Database::query($sql); |
||
| 4285 | } else { |
||
| 4286 | // Delete session-course-user relation ships *only* for students. |
||
| 4287 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4288 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4289 | Database::query($sql); |
||
| 4290 | } |
||
| 4291 | } |
||
| 4292 | } else { |
||
| 4293 | if ($debug) { |
||
| 4294 | $logger->addError("Sessions - Session to be updated: $session_name"); |
||
| 4295 | } |
||
| 4296 | |||
| 4297 | // Updating the session. |
||
| 4298 | $params = array( |
||
| 4299 | 'id_coach' => $coach_id, |
||
| 4300 | 'access_start_date' => $date_start, |
||
| 4301 | 'access_end_date' => $date_end, |
||
| 4302 | 'visibility' => $visibilityAfterExpirationPerSession, |
||
| 4303 | 'session_category_id' => $session_category_id, |
||
| 4304 | ); |
||
| 4305 | |||
| 4306 | if (!empty($sessionDescription)) { |
||
| 4307 | $params['description'] = $sessionDescription; |
||
| 4308 | } |
||
| 4309 | |||
| 4310 | if (!empty($fieldsToAvoidUpdate)) { |
||
| 4311 | foreach ($fieldsToAvoidUpdate as $field) { |
||
| 4312 | unset($params[$field]); |
||
| 4313 | } |
||
| 4314 | } |
||
| 4315 | |||
| 4316 | View Code Duplication | if (isset($sessionId) && !empty($sessionId)) { |
|
| 4317 | if (!empty($enreg['SessionName'])) { |
||
| 4318 | $params['name'] = $enreg['SessionName']; |
||
| 4319 | } |
||
| 4320 | $session_id = $sessionId; |
||
| 4321 | } else { |
||
| 4322 | $row = Database::query("SELECT id FROM $tbl_session WHERE name = '$session_name'"); |
||
| 4323 | list($session_id) = Database::fetch_array($row); |
||
| 4324 | } |
||
| 4325 | |||
| 4326 | if ($session_id) { |
||
| 4327 | |||
| 4328 | if ($debug) { |
||
| 4329 | $logger->addError("Sessions - Session to be updated #$session_id"); |
||
| 4330 | } |
||
| 4331 | |||
| 4332 | $sessionInfo = api_get_session_info($session_id); |
||
| 4333 | |||
| 4334 | $params['show_description'] = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : intval($showDescription); |
||
| 4335 | |||
| 4336 | if (!empty($daysCoachAccessBeforeBeginning) && !empty($daysCoachAccessAfterBeginning)) { |
||
| 4337 | View Code Duplication | if (empty($sessionInfo['nb_days_access_before_beginning']) || |
|
| 4338 | (!empty($sessionInfo['nb_days_access_before_beginning']) && |
||
| 4339 | $sessionInfo['nb_days_access_before_beginning'] < $daysCoachAccessBeforeBeginning) |
||
| 4340 | ) { |
||
| 4341 | $params['nb_days_access_before_beginning'] = intval($daysCoachAccessBeforeBeginning); |
||
| 4342 | } |
||
| 4343 | |||
| 4344 | View Code Duplication | if (empty($sessionInfo['nb_days_access_after_end']) || |
|
| 4345 | (!empty($sessionInfo['nb_days_access_after_end']) && |
||
| 4346 | $sessionInfo['nb_days_access_after_end'] < $daysCoachAccessAfterBeginning) |
||
| 4347 | ) { |
||
| 4348 | $params['nb_days_access_after_end'] = intval($daysCoachAccessAfterBeginning); |
||
| 4349 | } |
||
| 4350 | } |
||
| 4351 | |||
| 4352 | Database::update($tbl_session, $params, array('id = ?' => $session_id)); |
||
| 4353 | |||
| 4354 | View Code Duplication | foreach ($enreg as $key => $value) { |
|
| 4355 | if (substr($key, 0, 6) == 'extra_') { //an extra field |
||
| 4356 | self::update_session_extra_field_value($session_id, substr($key, 6), $value); |
||
| 4357 | } |
||
| 4358 | } |
||
| 4359 | |||
| 4360 | // Delete session-user relation only for students |
||
| 4361 | $sql = "DELETE FROM $tbl_session_user |
||
| 4362 | WHERE session_id = '$session_id' AND relation_type <> " . SESSION_RELATION_TYPE_RRHH; |
||
| 4363 | Database::query($sql); |
||
| 4364 | |||
| 4365 | $sql = "DELETE FROM $tbl_session_course WHERE session_id = '$session_id'"; |
||
| 4366 | Database::query($sql); |
||
| 4367 | |||
| 4368 | // Delete session-course-user relationships students and coaches. |
||
| 4369 | View Code Duplication | if ($updateCourseCoaches) { |
|
| 4370 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4371 | WHERE session_id = '$session_id' AND status in ('0', '2')"; |
||
| 4372 | Database::query($sql); |
||
| 4373 | } else { |
||
| 4374 | // Delete session-course-user relation ships *only* for students. |
||
| 4375 | $sql = "DELETE FROM $tbl_session_course_user |
||
| 4376 | WHERE session_id = '$session_id' AND status <> 2"; |
||
| 4377 | Database::query($sql); |
||
| 4378 | } |
||
| 4379 | } else { |
||
| 4380 | if ($debug) { |
||
| 4381 | $logger->addError( |
||
| 4382 | "Sessions - Session not found" |
||
| 4383 | ); |
||
| 4384 | } |
||
| 4385 | } |
||
| 4386 | } |
||
| 4387 | $session_counter++; |
||
| 4388 | } |
||
| 4389 | |||
| 4390 | $sessionList[] = $session_id; |
||
| 4391 | $users = explode('|', $enreg['Users']); |
||
| 4392 | |||
| 4393 | // Adding the relationship "Session - User" for students |
||
| 4394 | $userList = array(); |
||
| 4395 | |||
| 4396 | if (is_array($users)) { |
||
| 4397 | foreach ($users as $user) { |
||
| 4398 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4399 | if ($user_id !== false) { |
||
| 4400 | $userList[] = $user_id; |
||
| 4401 | // Insert new users. |
||
| 4402 | $sql = "INSERT IGNORE INTO $tbl_session_user SET |
||
| 4403 | user_id = '$user_id', |
||
| 4404 | session_id = '$session_id', |
||
| 4405 | registered_at = '" . api_get_utc_datetime() . "'"; |
||
| 4406 | Database::query($sql); |
||
| 4407 | if ($debug) { |
||
| 4408 | $logger->addInfo("Sessions - Adding User #$user_id ($user) to session #$session_id"); |
||
| 4409 | } |
||
| 4410 | $user_counter++; |
||
| 4411 | } |
||
| 4412 | } |
||
| 4413 | } |
||
| 4414 | |||
| 4415 | if ($deleteUsersNotInList) { |
||
| 4416 | // Getting user in DB in order to compare to the new list. |
||
| 4417 | $usersListInDatabase = self::get_users_by_session($session_id, 0); |
||
| 4418 | |||
| 4419 | if (!empty($usersListInDatabase)) { |
||
| 4420 | if (empty($userList)) { |
||
| 4421 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4422 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4423 | } |
||
| 4424 | } else { |
||
| 4425 | foreach ($usersListInDatabase as $userInfo) { |
||
| 4426 | if (!in_array($userInfo['user_id'], $userList)) { |
||
| 4427 | self::unsubscribe_user_from_session($session_id, $userInfo['user_id']); |
||
| 4428 | } |
||
| 4429 | } |
||
| 4430 | } |
||
| 4431 | } |
||
| 4432 | } |
||
| 4433 | |||
| 4434 | $courses = explode('|', $enreg['Courses']); |
||
| 4435 | |||
| 4436 | // See BT#6449 |
||
| 4437 | $onlyAddFirstCoachOrTeacher = false; |
||
| 4438 | |||
| 4439 | if ($sessionWithCoursesModifier) { |
||
| 4440 | if (count($courses) >= 2) { |
||
| 4441 | // Only first teacher in course session; |
||
| 4442 | $onlyAddFirstCoachOrTeacher = true; |
||
| 4443 | |||
| 4444 | // Remove all teachers from course. |
||
| 4445 | $removeAllTeachersFromCourse = false; |
||
| 4446 | } |
||
| 4447 | } |
||
| 4448 | |||
| 4449 | foreach ($courses as $course) { |
||
| 4450 | $courseArray = bracketsToArray($course); |
||
| 4451 | $course_code = $courseArray[0]; |
||
| 4452 | |||
| 4453 | if (CourseManager::course_exists($course_code)) { |
||
| 4454 | |||
| 4455 | $courseInfo = api_get_course_info($course_code); |
||
| 4456 | $courseId = $courseInfo['real_id']; |
||
| 4457 | |||
| 4458 | // Adding the course to a session. |
||
| 4459 | $sql = "INSERT IGNORE INTO $tbl_session_course |
||
| 4460 | SET c_id = '$courseId', session_id='$session_id'"; |
||
| 4461 | Database::query($sql); |
||
| 4462 | |||
| 4463 | SessionManager::installCourse($session_id, $courseInfo['real_id']); |
||
| 4464 | |||
| 4465 | if ($debug) { |
||
| 4466 | $logger->addInfo("Sessions - Adding course '$course_code' to session #$session_id"); |
||
| 4467 | } |
||
| 4468 | |||
| 4469 | $course_counter++; |
||
| 4470 | |||
| 4471 | $course_coaches = isset($courseArray[1]) ? $courseArray[1] : null; |
||
| 4472 | $course_users = isset($courseArray[2]) ? $courseArray[2] : null; |
||
| 4473 | |||
| 4474 | $course_users = explode(',', $course_users); |
||
| 4475 | $course_coaches = explode(',', $course_coaches); |
||
| 4476 | |||
| 4477 | // Checking if the flag is set TeachersWillBeAddedAsCoachInAllCourseSessions (course_edit.php) |
||
| 4478 | $addTeachersToSession = true; |
||
| 4479 | |||
| 4480 | if (array_key_exists('add_teachers_to_sessions_courses', $courseInfo)) { |
||
| 4481 | $addTeachersToSession = $courseInfo['add_teachers_to_sessions_courses']; |
||
| 4482 | } |
||
| 4483 | |||
| 4484 | // If any user provided for a course, use the users array. |
||
| 4485 | if (empty($course_users)) { |
||
| 4486 | if (!empty($userList)) { |
||
| 4487 | SessionManager::subscribe_users_to_session_course( |
||
| 4488 | $userList, |
||
| 4489 | $session_id, |
||
| 4490 | $course_code |
||
| 4491 | ); |
||
| 4492 | if ($debug) { |
||
| 4493 | $msg = "Sessions - Adding student list ".implode(', #', $userList)." to course: '$course_code' and session #$session_id"; |
||
| 4494 | $logger->addInfo($msg); |
||
| 4495 | } |
||
| 4496 | } |
||
| 4497 | } |
||
| 4498 | |||
| 4499 | // Adding coaches to session course user. |
||
| 4500 | if (!empty($course_coaches)) { |
||
| 4501 | $savedCoaches = array(); |
||
| 4502 | // only edit if add_teachers_to_sessions_courses is set. |
||
| 4503 | if ($addTeachersToSession) { |
||
| 4504 | if ($addOriginalCourseTeachersAsCourseSessionCoaches) { |
||
| 4505 | // Adding course teachers as course session teachers. |
||
| 4506 | $alreadyAddedTeachers = CourseManager::get_teacher_list_from_course_code( |
||
| 4507 | $course_code |
||
| 4508 | ); |
||
| 4509 | |||
| 4510 | if (!empty($alreadyAddedTeachers)) { |
||
| 4511 | $teachersToAdd = array(); |
||
| 4512 | foreach ($alreadyAddedTeachers as $user) { |
||
| 4513 | $teachersToAdd[] = $user['username']; |
||
| 4514 | } |
||
| 4515 | $course_coaches = array_merge( |
||
| 4516 | $course_coaches, |
||
| 4517 | $teachersToAdd |
||
| 4518 | ); |
||
| 4519 | } |
||
| 4520 | } |
||
| 4521 | |||
| 4522 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4523 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4524 | if ($coach_id !== false) { |
||
| 4525 | // Just insert new coaches |
||
| 4526 | SessionManager::updateCoaches($session_id, $courseId, array($coach_id), false); |
||
| 4527 | |||
| 4528 | if ($debug) { |
||
| 4529 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4530 | } |
||
| 4531 | $savedCoaches[] = $coach_id; |
||
| 4532 | } else { |
||
| 4533 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4534 | } |
||
| 4535 | } |
||
| 4536 | } |
||
| 4537 | |||
| 4538 | // Custom courses/session coaches |
||
| 4539 | $teacherToAdd = null; |
||
| 4540 | // Only one coach is added. |
||
| 4541 | if ($onlyAddFirstCoachOrTeacher == true) { |
||
| 4542 | |||
| 4543 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4544 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4545 | if ($coach_id !== false) { |
||
| 4546 | $teacherToAdd = $coach_id; |
||
| 4547 | break; |
||
| 4548 | } |
||
| 4549 | } |
||
| 4550 | |||
| 4551 | // Un subscribe everyone that's not in the list. |
||
| 4552 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4553 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4554 | foreach ($teacherList as $teacher) { |
||
| 4555 | if ($teacherToAdd != $teacher['user_id']) { |
||
| 4556 | |||
| 4557 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4558 | WHERE |
||
| 4559 | user_id = ".$teacher['user_id']." AND |
||
| 4560 | course_code = '".$course_code."' |
||
| 4561 | "; |
||
| 4562 | |||
| 4563 | $result = Database::query($sql); |
||
| 4564 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4565 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4566 | |||
| 4567 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4568 | WHERE |
||
| 4569 | user_id = ".$teacher['user_id']." AND |
||
| 4570 | c_id = '".$courseInfo['real_id']."' |
||
| 4571 | "; |
||
| 4572 | |||
| 4573 | $result = Database::query($sql); |
||
| 4574 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4575 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4576 | } |
||
| 4577 | |||
| 4578 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4579 | WHERE |
||
| 4580 | user_id = ".$teacher['user_id']." AND |
||
| 4581 | c_id = '".$courseInfo['real_id']."' |
||
| 4582 | "; |
||
| 4583 | |||
| 4584 | $result = Database::query($sql); |
||
| 4585 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4586 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4587 | } |
||
| 4588 | |||
| 4589 | CourseManager::unsubscribe_user( |
||
| 4590 | $teacher['user_id'], |
||
| 4591 | $course_code |
||
| 4592 | ); |
||
| 4593 | } |
||
| 4594 | } |
||
| 4595 | } |
||
| 4596 | |||
| 4597 | if (!empty($teacherToAdd)) { |
||
| 4598 | SessionManager::updateCoaches($session_id, $courseId, array($teacherToAdd), true); |
||
| 4599 | |||
| 4600 | $userCourseCategory = ''; |
||
| 4601 | View Code Duplication | if (isset($teacherBackupList[$teacherToAdd]) && |
|
| 4602 | isset($teacherBackupList[$teacherToAdd][$course_code]) |
||
| 4603 | ) { |
||
| 4604 | $courseUserData = $teacherBackupList[$teacherToAdd][$course_code]; |
||
| 4605 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4606 | } |
||
| 4607 | |||
| 4608 | CourseManager::subscribe_user( |
||
| 4609 | $teacherToAdd, |
||
| 4610 | $course_code, |
||
| 4611 | COURSEMANAGER, |
||
| 4612 | 0, |
||
| 4613 | $userCourseCategory |
||
| 4614 | ); |
||
| 4615 | |||
| 4616 | View Code Duplication | if (isset($groupBackup['user'][$teacherToAdd]) && |
|
| 4617 | isset($groupBackup['user'][$teacherToAdd][$course_code]) && |
||
| 4618 | !empty($groupBackup['user'][$teacherToAdd][$course_code]) |
||
| 4619 | ) { |
||
| 4620 | foreach ($groupBackup['user'][$teacherToAdd][$course_code] as $data) { |
||
| 4621 | GroupManager::subscribe_users( |
||
| 4622 | $teacherToAdd, |
||
| 4623 | $data['group_id'], |
||
| 4624 | $data['c_id'] |
||
| 4625 | ); |
||
| 4626 | } |
||
| 4627 | } |
||
| 4628 | |||
| 4629 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherToAdd]) && |
|
| 4630 | isset($groupBackup['tutor'][$teacherToAdd][$course_code]) && |
||
| 4631 | !empty($groupBackup['tutor'][$teacherToAdd][$course_code]) |
||
| 4632 | ) { |
||
| 4633 | foreach ($groupBackup['tutor'][$teacherToAdd][$course_code] as $data) { |
||
| 4634 | GroupManager::subscribe_tutors( |
||
| 4635 | $teacherToAdd, |
||
| 4636 | $data['group_id'], |
||
| 4637 | $data['c_id'] |
||
| 4638 | ); |
||
| 4639 | } |
||
| 4640 | } |
||
| 4641 | } |
||
| 4642 | } |
||
| 4643 | |||
| 4644 | // See BT#6449#note-195 |
||
| 4645 | // All coaches are added. |
||
| 4646 | if ($removeAllTeachersFromCourse) { |
||
| 4647 | $teacherToAdd = null; |
||
| 4648 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4649 | $coach_id = UserManager::get_user_id_from_username( |
||
| 4650 | $course_coach |
||
| 4651 | ); |
||
| 4652 | if ($coach_id !== false) { |
||
| 4653 | $teacherToAdd[] = $coach_id; |
||
| 4654 | } |
||
| 4655 | } |
||
| 4656 | |||
| 4657 | if (!empty($teacherToAdd)) { |
||
| 4658 | // Deleting all course teachers and adding the only coach as teacher. |
||
| 4659 | $teacherList = CourseManager::get_teacher_list_from_course_code($course_code); |
||
| 4660 | |||
| 4661 | View Code Duplication | if (!empty($teacherList)) { |
|
| 4662 | foreach ($teacherList as $teacher) { |
||
| 4663 | if (!in_array($teacher['user_id'], $teacherToAdd)) { |
||
| 4664 | |||
| 4665 | $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)." |
||
| 4666 | WHERE |
||
| 4667 | user_id = ".$teacher['user_id']." AND |
||
| 4668 | course_code = '".$course_code."' |
||
| 4669 | "; |
||
| 4670 | |||
| 4671 | $result = Database::query($sql); |
||
| 4672 | $userCourseData = Database::fetch_array($result, 'ASSOC'); |
||
| 4673 | $teacherBackupList[$teacher['user_id']][$course_code] = $userCourseData; |
||
| 4674 | |||
| 4675 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)." |
||
| 4676 | WHERE |
||
| 4677 | user_id = ".$teacher['user_id']." AND |
||
| 4678 | c_id = '".$courseInfo['real_id']."' |
||
| 4679 | "; |
||
| 4680 | |||
| 4681 | $result = Database::query($sql); |
||
| 4682 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4683 | $groupBackup['user'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4684 | } |
||
| 4685 | |||
| 4686 | $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)." |
||
| 4687 | WHERE |
||
| 4688 | user_id = ".$teacher['user_id']." AND |
||
| 4689 | c_id = '".$courseInfo['real_id']."' |
||
| 4690 | "; |
||
| 4691 | |||
| 4692 | $result = Database::query($sql); |
||
| 4693 | while ($groupData = Database::fetch_array($result, 'ASSOC')) { |
||
| 4694 | $groupBackup['tutor'][$teacher['user_id']][$course_code][$groupData['group_id']] = $groupData; |
||
| 4695 | } |
||
| 4696 | |||
| 4697 | CourseManager::unsubscribe_user( |
||
| 4698 | $teacher['user_id'], |
||
| 4699 | $course_code |
||
| 4700 | ); |
||
| 4701 | } |
||
| 4702 | } |
||
| 4703 | } |
||
| 4704 | |||
| 4705 | foreach ($teacherToAdd as $teacherId) { |
||
| 4706 | $userCourseCategory = ''; |
||
| 4707 | View Code Duplication | if (isset($teacherBackupList[$teacherId]) && |
|
| 4708 | isset($teacherBackupList[$teacherId][$course_code]) |
||
| 4709 | ) { |
||
| 4710 | $courseUserData = $teacherBackupList[$teacherId][$course_code]; |
||
| 4711 | $userCourseCategory = $courseUserData['user_course_cat']; |
||
| 4712 | } |
||
| 4713 | |||
| 4714 | CourseManager::subscribe_user( |
||
| 4715 | $teacherId, |
||
| 4716 | $course_code, |
||
| 4717 | COURSEMANAGER, |
||
| 4718 | 0, |
||
| 4719 | $userCourseCategory |
||
| 4720 | ); |
||
| 4721 | |||
| 4722 | View Code Duplication | if (isset($groupBackup['user'][$teacherId]) && |
|
| 4723 | isset($groupBackup['user'][$teacherId][$course_code]) && |
||
| 4724 | !empty($groupBackup['user'][$teacherId][$course_code]) |
||
| 4725 | ) { |
||
| 4726 | foreach ($groupBackup['user'][$teacherId][$course_code] as $data) { |
||
| 4727 | GroupManager::subscribe_users( |
||
| 4728 | $teacherId, |
||
| 4729 | $data['group_id'], |
||
| 4730 | $data['c_id'] |
||
| 4731 | ); |
||
| 4732 | } |
||
| 4733 | } |
||
| 4734 | |||
| 4735 | View Code Duplication | if (isset($groupBackup['tutor'][$teacherId]) && |
|
| 4736 | isset($groupBackup['tutor'][$teacherId][$course_code]) && |
||
| 4737 | !empty($groupBackup['tutor'][$teacherId][$course_code]) |
||
| 4738 | ) { |
||
| 4739 | foreach ($groupBackup['tutor'][$teacherId][$course_code] as $data) { |
||
| 4740 | GroupManager::subscribe_tutors( |
||
| 4741 | $teacherId, |
||
| 4742 | $data['group_id'], |
||
| 4743 | $data['c_id'] |
||
| 4744 | ); |
||
| 4745 | } |
||
| 4746 | } |
||
| 4747 | } |
||
| 4748 | } |
||
| 4749 | } |
||
| 4750 | |||
| 4751 | // Continue default behaviour. |
||
| 4752 | if ($onlyAddFirstCoachOrTeacher == false) { |
||
| 4753 | // Checking one more time see BT#6449#note-149 |
||
| 4754 | $coaches = SessionManager::getCoachesByCourseSession($session_id, $courseId); |
||
| 4755 | // Update coaches if only there's 1 course see BT#6449#note-189 |
||
| 4756 | if (empty($coaches) || count($courses) == 1) { |
||
| 4757 | View Code Duplication | foreach ($course_coaches as $course_coach) { |
|
| 4758 | $course_coach = trim($course_coach); |
||
| 4759 | $coach_id = UserManager::get_user_id_from_username($course_coach); |
||
| 4760 | if ($coach_id !== false) { |
||
| 4761 | // Just insert new coaches |
||
| 4762 | SessionManager::updateCoaches( |
||
| 4763 | $session_id, |
||
| 4764 | $courseId, |
||
| 4765 | array($coach_id), |
||
| 4766 | false |
||
| 4767 | ); |
||
| 4768 | |||
| 4769 | if ($debug) { |
||
| 4770 | $logger->addInfo("Sessions - Adding course coach: user #$coach_id ($course_coach) to course: '$course_code' and session #$session_id"); |
||
| 4771 | } |
||
| 4772 | $savedCoaches[] = $coach_id; |
||
| 4773 | } else { |
||
| 4774 | $error_message .= get_lang('UserDoesNotExist').' : '.$course_coach.$eol; |
||
| 4775 | } |
||
| 4776 | } |
||
| 4777 | } |
||
| 4778 | } |
||
| 4779 | } |
||
| 4780 | |||
| 4781 | // Adding Students, updating relationship "Session - Course - User". |
||
| 4782 | $course_users = array_filter($course_users); |
||
| 4783 | |||
| 4784 | if (!empty($course_users)) { |
||
| 4785 | View Code Duplication | foreach ($course_users as $user) { |
|
| 4786 | $user_id = UserManager::get_user_id_from_username($user); |
||
| 4787 | |||
| 4788 | if ($user_id !== false) { |
||
| 4789 | SessionManager::subscribe_users_to_session_course( |
||
| 4790 | array($user_id), |
||
| 4791 | $session_id, |
||
| 4792 | $course_code |
||
| 4793 | ); |
||
| 4794 | if ($debug) { |
||
| 4795 | $logger->addInfo("Sessions - Adding student: user #$user_id ($user) to course: '$course_code' and session #$session_id"); |
||
| 4796 | } |
||
| 4797 | } else { |
||
| 4798 | $error_message .= get_lang('UserDoesNotExist').': '.$user.$eol; |
||
| 4799 | } |
||
| 4800 | } |
||
| 4801 | } |
||
| 4802 | |||
| 4803 | $inserted_in_course[$course_code] = $courseInfo['title']; |
||
| 4804 | } |
||
| 4805 | } |
||
| 4806 | $access_url_id = api_get_current_access_url_id(); |
||
| 4807 | UrlManager::add_session_to_url($session_id, $access_url_id); |
||
| 4808 | $sql = "UPDATE $tbl_session SET nbr_users = '$user_counter', nbr_courses = '$course_counter' WHERE id = '$session_id'"; |
||
| 4809 | Database::query($sql); |
||
| 4810 | } |
||
| 4811 | } |
||
| 4812 | |||
| 4813 | return array( |
||
| 4814 | 'error_message' => $error_message, |
||
| 4815 | 'session_counter' => $session_counter, |
||
| 4816 | 'session_list' => $sessionList, |
||
| 4817 | ); |
||
| 4818 | } |
||
| 4819 | |||
| 4820 | /** |
||
| 4821 | * @param int $sessionId |
||
| 4822 | * @param int $courseId |
||
| 4823 | * @return array |
||
| 4824 | */ |
||
| 4825 | View Code Duplication | public static function getCoachesByCourseSession($sessionId, $courseId) |
|
| 4847 | |||
| 4848 | /** |
||
| 4849 | * @param int $sessionId |
||
| 4850 | * @param int $courseId |
||
| 4851 | * @return string |
||
| 4852 | */ |
||
| 4853 | public static function getCoachesByCourseSessionToString( |
||
| 4871 | |||
| 4872 | /** |
||
| 4873 | * Get all coaches added in the session - course relationship |
||
| 4874 | * @param int $sessionId |
||
| 4875 | * @return array |
||
| 4876 | */ |
||
| 4877 | public static function getCoachesBySession($sessionId) |
||
| 4896 | |||
| 4897 | /** |
||
| 4898 | * @param int $userId |
||
| 4899 | * @return array |
||
| 4900 | */ |
||
| 4901 | public static function getAllCoursesFromAllSessionFromDrh($userId) |
||
| 4915 | |||
| 4916 | /** |
||
| 4917 | * @param string $status |
||
| 4918 | * @param int $userId |
||
| 4919 | * @param bool $getCount |
||
| 4920 | * @param int $from |
||
| 4921 | * @param int $numberItems |
||
| 4922 | * @param int $column |
||
| 4923 | * @param string $direction |
||
| 4924 | * @param string $keyword |
||
| 4925 | * @param string $active |
||
| 4926 | * @param string $lastConnectionDate |
||
| 4927 | * @param array $sessionIdList |
||
| 4928 | * @param array $studentIdList |
||
| 4929 | * @param int $filterByStatus |
||
| 4930 | * @return array|int |
||
| 4931 | */ |
||
| 4932 | public static function getAllUsersFromCoursesFromAllSessionFromStatus( |
||
| 5104 | |||
| 5105 | /** |
||
| 5106 | * @param int $sessionId |
||
| 5107 | * @param int $courseId |
||
| 5108 | * @param array $coachList |
||
| 5109 | * @param bool $deleteCoachesNotInList |
||
| 5110 | */ |
||
| 5111 | public static function updateCoaches( |
||
| 5144 | |||
| 5145 | /** |
||
| 5146 | * @param array $sessions |
||
| 5147 | * @param array $sessionsDestination |
||
| 5148 | * @return string |
||
| 5149 | */ |
||
| 5150 | public static function copyStudentsFromSession($sessions, $sessionsDestination) |
||
| 5198 | |||
| 5199 | /** |
||
| 5200 | * Assign coaches of a session(s) as teachers to a given course (or courses) |
||
| 5201 | * @param array A list of session IDs |
||
| 5202 | * @param array A list of course IDs |
||
| 5203 | * @return string |
||
| 5204 | */ |
||
| 5205 | public static function copyCoachesFromSessionToCourse($sessions, $courses) |
||
| 5260 | |||
| 5261 | /** |
||
| 5262 | * @param string $keyword |
||
| 5263 | * @param string $active |
||
| 5264 | * @param string $lastConnectionDate |
||
| 5265 | * @param array $sessionIdList |
||
| 5266 | * @param array $studentIdList |
||
| 5267 | * @param int $userStatus STUDENT|COURSEMANAGER constants |
||
| 5268 | * |
||
| 5269 | * @return array|int |
||
| 5270 | */ |
||
| 5271 | public static function getCountUserTracking( |
||
| 5323 | |||
| 5324 | /** |
||
| 5325 | * Get teachers followed by a user |
||
| 5326 | * @param int $userId |
||
| 5327 | * @param int $active |
||
| 5328 | * @param string $lastConnectionDate |
||
| 5329 | * @param bool $getCount |
||
| 5330 | * @param array $sessionIdList |
||
| 5331 | * @return array|int |
||
| 5332 | */ |
||
| 5333 | public static function getTeacherTracking( |
||
| 5415 | |||
| 5416 | /** |
||
| 5417 | * Get the list of course tools that have to be dealt with in case of |
||
| 5418 | * registering any course to a session |
||
| 5419 | * @return array The list of tools to be dealt with (literal names) |
||
| 5420 | */ |
||
| 5421 | public static function getCourseToolToBeManaged() |
||
| 5428 | |||
| 5429 | /** |
||
| 5430 | * Calls the methods bound to each tool when a course is registered into a session |
||
| 5431 | * @param int $sessionId |
||
| 5432 | * @param int $courseId |
||
| 5433 | * @return void |
||
| 5434 | */ |
||
| 5435 | View Code Duplication | public static function installCourse($sessionId, $courseId) |
|
| 5447 | |||
| 5448 | /** |
||
| 5449 | * Calls the methods bound to each tool when a course is unregistered from |
||
| 5450 | * a session |
||
| 5451 | * @param int $sessionId |
||
| 5452 | * @param int $courseId |
||
| 5453 | */ |
||
| 5454 | View Code Duplication | public static function unInstallCourse($sessionId, $courseId) |
|
| 5466 | |||
| 5467 | /** |
||
| 5468 | * @param int $sessionId |
||
| 5469 | * @param int $courseId |
||
| 5470 | */ |
||
| 5471 | public static function addCourseIntroduction($sessionId, $courseId) |
||
| 5490 | |||
| 5491 | /** |
||
| 5492 | * @param int $sessionId |
||
| 5493 | * @param int $courseId |
||
| 5494 | */ |
||
| 5495 | public static function removeCourseIntroduction($sessionId, $courseId) |
||
| 5504 | |||
| 5505 | /** |
||
| 5506 | * @param int $sessionId |
||
| 5507 | * @param int $courseId |
||
| 5508 | */ |
||
| 5509 | public static function addCourseDescription($sessionId, $courseId) |
||
| 5516 | |||
| 5517 | /** |
||
| 5518 | * @param int $sessionId |
||
| 5519 | * @param int $courseId |
||
| 5520 | */ |
||
| 5521 | public static function removeCourseDescription($sessionId, $courseId) |
||
| 5525 | |||
| 5526 | /** |
||
| 5527 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5528 | * @param bool $sendEmail |
||
| 5529 | * @param bool $removeOldRelationShips |
||
| 5530 | * @return string |
||
| 5531 | */ |
||
| 5532 | public static function subscribeDrhToSessionList($userSessionList, $sendEmail, $removeOldRelationShips) |
||
| 5550 | |||
| 5551 | /** |
||
| 5552 | * @param array $userSessionList format see self::importSessionDrhCSV() |
||
| 5553 | * |
||
| 5554 | * @return string |
||
| 5555 | */ |
||
| 5556 | public static function checkSubscribeDrhToSessionList($userSessionList) |
||
| 5597 | |||
| 5598 | /** |
||
| 5599 | * @param string $file |
||
| 5600 | * @param bool $sendEmail |
||
| 5601 | * @param bool $removeOldRelationShips |
||
| 5602 | * |
||
| 5603 | * @return string |
||
| 5604 | */ |
||
| 5605 | public static function importSessionDrhCSV($file, $sendEmail, $removeOldRelationShips) |
||
| 5628 | |||
| 5629 | /** |
||
| 5630 | * Courses re-ordering in resume_session.php flag see BT#8316 |
||
| 5631 | */ |
||
| 5632 | public static function orderCourseIsEnabled() |
||
| 5633 | { |
||
| 5634 | $sessionCourseOrder = api_get_setting('session_course_ordering'); |
||
| 5635 | if ($sessionCourseOrder === 'true') { |
||
| 5636 | return true; |
||
| 5637 | } |
||
| 5638 | |||
| 5639 | return false; |
||
| 5640 | } |
||
| 5641 | |||
| 5642 | /** |
||
| 5643 | * @param string $direction (up/down) |
||
| 5644 | * @param int $sessionId |
||
| 5645 | * @param int $courseId |
||
| 5646 | * @return bool |
||
| 5647 | */ |
||
| 5648 | public static function move($direction, $sessionId, $courseId) |
||
| 5711 | |||
| 5712 | /** |
||
| 5713 | * @param int $sessionId |
||
| 5714 | * @param int $courseId |
||
| 5715 | * @return bool |
||
| 5716 | */ |
||
| 5717 | public static function moveUp($sessionId, $courseId) |
||
| 5721 | |||
| 5722 | /** |
||
| 5723 | * @param int $sessionId |
||
| 5724 | * @param string $courseCode |
||
| 5725 | * @return bool |
||
| 5726 | */ |
||
| 5727 | public static function moveDown($sessionId, $courseCode) |
||
| 5731 | |||
| 5732 | /** |
||
| 5733 | * Use the session duration to allow/block user access see BT#8317 |
||
| 5734 | * Needs these DB changes |
||
| 5735 | * ALTER TABLE session ADD COLUMN duration int; |
||
| 5736 | * ALTER TABLE session_rel_user ADD COLUMN duration int; |
||
| 5737 | */ |
||
| 5738 | public static function durationPerUserIsEnabled() |
||
| 5742 | |||
| 5743 | /** |
||
| 5744 | * Returns the number of days the student has left in a session when using |
||
| 5745 | * sessions durations |
||
| 5746 | * @param int $userId |
||
| 5747 | * @param int $sessionId |
||
| 5748 | * @param int $duration in days |
||
| 5749 | * @return int |
||
| 5750 | */ |
||
| 5751 | public static function getDayLeftInSession($sessionId, $userId, $duration) |
||
| 5774 | |||
| 5775 | /** |
||
| 5776 | * @param int $duration |
||
| 5777 | * @param int $userId |
||
| 5778 | * @param int $sessionId |
||
| 5779 | */ |
||
| 5780 | public static function editUserSessionDuration($duration, $userId, $sessionId) |
||
| 5795 | |||
| 5796 | /** |
||
| 5797 | * Gets one row from the session_rel_user table |
||
| 5798 | * @param int $userId |
||
| 5799 | * @param int $sessionId |
||
| 5800 | * |
||
| 5801 | * @return array |
||
| 5802 | */ |
||
| 5803 | public static function getUserSession($userId, $sessionId) |
||
| 5823 | |||
| 5824 | /** |
||
| 5825 | * Check if user is subscribed inside a session as student |
||
| 5826 | * @param int $sessionId The session id |
||
| 5827 | * @param int $userId The user id |
||
| 5828 | * @return boolean Whether is subscribed |
||
| 5829 | */ |
||
| 5830 | public static function isUserSubscribedAsStudent($sessionId, $userId) |
||
| 5853 | |||
| 5854 | /** |
||
| 5855 | * Get the session coached by a user (general coach and course-session coach) |
||
| 5856 | * @param int $coachId The coach id |
||
| 5857 | * @param boolean $checkSessionRelUserVisibility Check the session visibility |
||
| 5858 | * @param boolean $asPlatformAdmin The user is a platform admin and we want all sessions |
||
| 5859 | * @return array The session list |
||
| 5860 | */ |
||
| 5861 | public static function getSessionsCoachedByUser($coachId, $checkSessionRelUserVisibility = false, $asPlatformAdmin = false) |
||
| 5906 | |||
| 5907 | /** |
||
| 5908 | * Check if the course belongs to the session |
||
| 5909 | * @param int $sessionId The session id |
||
| 5910 | * @param string $courseCode The course code |
||
| 5911 | * |
||
| 5912 | * @return bool |
||
| 5913 | */ |
||
| 5914 | public static function sessionHasCourse($sessionId, $courseCode) |
||
| 5941 | |||
| 5942 | /** |
||
| 5943 | * Get the list of course coaches |
||
| 5944 | * @return array The list |
||
| 5945 | */ |
||
| 5946 | public static function getAllCourseCoaches() |
||
| 5984 | |||
| 5985 | /** |
||
| 5986 | * Calculate the total user time in the platform |
||
| 5987 | * @param int $userId The user id |
||
| 5988 | * @param string $from Optional. From date |
||
| 5989 | * @param string $until Optional. Until date |
||
| 5990 | * @return string The time (hh:mm:ss) |
||
| 5991 | */ |
||
| 5992 | public static function getTotalUserTimeInPlatform($userId, $from = '', $until = '') |
||
| 6021 | |||
| 6022 | /** |
||
| 6023 | * Get the courses list by a course coach |
||
| 6024 | * @param int $coachId The coach id |
||
| 6025 | * @return array (id, user_id, session_id, c_id, visibility, status, legal_agreement) |
||
| 6026 | */ |
||
| 6027 | public static function getCoursesListByCourseCoach($coachId) |
||
| 6039 | |||
| 6040 | /** |
||
| 6041 | * Get the count of user courses in session |
||
| 6042 | * @param int $sessionId The session id |
||
| 6043 | * @return array |
||
| 6044 | */ |
||
| 6045 | public static function getTotalUserCoursesInSession($sessionId) |
||
| 6064 | |||
| 6065 | |||
| 6066 | /** |
||
| 6067 | * Returns list of a few data from session (name, short description, start |
||
| 6068 | * date, end date) and the given extra fields if defined based on a |
||
| 6069 | * session category Id. |
||
| 6070 | * @param int $categoryId The internal ID of the session category |
||
| 6071 | * @param string $target Value to search for in the session field values |
||
| 6072 | * @param array $extraFields A list of fields to be scanned and returned |
||
| 6073 | * @return mixed |
||
| 6074 | */ |
||
| 6075 | public static function getShortSessionListAndExtraByCategory($categoryId, $target, $extraFields = null, $publicationDate = null) |
||
| 6169 | |||
| 6170 | /** |
||
| 6171 | * Return the Session Category id searched by name |
||
| 6172 | * @param string $categoryName Name attribute of session category used for search query |
||
| 6173 | * @param bool $force boolean used to get even if something is wrong (e.g not unique name) |
||
| 6174 | * @return int|array If success, return category id (int), else it will return an array |
||
| 6175 | * with the next structure: |
||
| 6176 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6177 | */ |
||
| 6178 | public static function getSessionCategoryIdByName($categoryName, $force = false) |
||
| 6215 | |||
| 6216 | /** |
||
| 6217 | * Return all data from sessions (plus extra field, course and coach data) by category id |
||
| 6218 | * @param int $sessionCategoryId session category id used to search sessions |
||
| 6219 | * @return array If success, return session list and more session related data, else it will return an array |
||
| 6220 | * with the next structure: |
||
| 6221 | * array('error' => true, 'errorMessage' => ERROR_MESSAGE) |
||
| 6222 | */ |
||
| 6223 | public static function getSessionListAndExtraByCategoryId($sessionCategoryId) |
||
| 6351 | |||
| 6352 | /** |
||
| 6353 | * Return session description from session id |
||
| 6354 | * @param int $sessionId |
||
| 6355 | * @return string |
||
| 6356 | */ |
||
| 6357 | public static function getDescriptionFromSessionId($sessionId) |
||
| 6384 | |||
| 6385 | /** |
||
| 6386 | * Get a session list filtered by name, description or any of the given extra fields |
||
| 6387 | * @param string $term The term to search |
||
| 6388 | * @param array $extraFieldsToInclude Extra fields to include in the session data |
||
| 6389 | * @return array The list |
||
| 6390 | */ |
||
| 6391 | public static function searchSession($term, $extraFieldsToInclude = array()) |
||
| 6429 | |||
| 6430 | /** |
||
| 6431 | * @param $sessionId |
||
| 6432 | * @param array $extraFieldsToInclude |
||
| 6433 | * @return array |
||
| 6434 | */ |
||
| 6435 | public static function getFilteredExtraFields($sessionId, $extraFieldsToInclude = array()) |
||
| 6489 | |||
| 6490 | /** |
||
| 6491 | * @param int $sessionId |
||
| 6492 | * |
||
| 6493 | * @return bool |
||
| 6494 | */ |
||
| 6495 | public static function isValidId($sessionId) |
||
| 6512 | |||
| 6513 | /** |
||
| 6514 | * Get list of sessions based on users of a group for a group admin |
||
| 6515 | * @param int $userId The user id |
||
| 6516 | * @return array |
||
| 6517 | */ |
||
| 6518 | View Code Duplication | public static function getSessionsFollowedForGroupAdmin($userId) |
|
| 6564 | |||
| 6565 | /** |
||
| 6566 | * @param array $sessionInfo |
||
| 6567 | * @return string |
||
| 6568 | */ |
||
| 6569 | public static function getSessionVisibility($sessionInfo) |
||
| 6580 | |||
| 6581 | /** |
||
| 6582 | * Converts "start date" and "end date" to "From start date to end date" string |
||
| 6583 | * @param string $startDate |
||
| 6584 | * @param string $endDate |
||
| 6585 | * |
||
| 6586 | * @return string |
||
| 6587 | */ |
||
| 6588 | private static function convertSessionDateToString($startDate, $endDate) |
||
| 6618 | |||
| 6619 | /** |
||
| 6620 | * Returns a human readable string |
||
| 6621 | * @params array $sessionInfo An array with all the session dates |
||
| 6622 | * @return string |
||
| 6623 | */ |
||
| 6624 | public static function parseSessionDates($sessionInfo) |
||
| 6649 | |||
| 6650 | /** |
||
| 6651 | * @param FormValidator $form |
||
| 6652 | * |
||
| 6653 | * @return array |
||
| 6654 | */ |
||
| 6655 | public static function setForm(FormValidator & $form, $sessionId = 0) |
||
| 6656 | { |
||
| 6657 | $categoriesList = SessionManager::get_all_session_category(); |
||
| 6658 | $userInfo = api_get_user_info(); |
||
| 6659 | |||
| 6660 | $categoriesOptions = array( |
||
| 6661 | '0' => get_lang('None'), |
||
| 6662 | ); |
||
| 6663 | |||
| 6664 | if ($categoriesList != false) { |
||
| 6665 | foreach ($categoriesList as $categoryItem) { |
||
| 6666 | $categoriesOptions[$categoryItem['id']] = $categoryItem['name']; |
||
| 6667 | } |
||
| 6668 | } |
||
| 6669 | |||
| 6670 | // Database Table Definitions |
||
| 6671 | $tbl_user = Database::get_main_table(TABLE_MAIN_USER); |
||
| 6672 | |||
| 6673 | $form->addElement('text', 'name', get_lang('SessionName'), array( |
||
| 6674 | 'maxlength' => 50, |
||
| 6675 | )); |
||
| 6676 | $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 6677 | $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name'); |
||
| 6678 | |||
| 6679 | if (!api_is_platform_admin() && api_is_teacher()) { |
||
| 6680 | $form->addElement( |
||
| 6681 | 'select', |
||
| 6682 | 'coach_username', |
||
| 6683 | get_lang('CoachName'), |
||
| 6684 | [api_get_user_id() => $userInfo['complete_name']], |
||
| 6685 | array( |
||
| 6686 | 'id' => 'coach_username', |
||
| 6687 | 'style' => 'width:370px;', |
||
| 6688 | ) |
||
| 6689 | ); |
||
| 6690 | } else { |
||
| 6691 | |||
| 6692 | $sql = "SELECT COUNT(1) FROM $tbl_user WHERE status = 1"; |
||
| 6693 | $rs = Database::query($sql); |
||
| 6694 | $countUsers = Database::result($rs, 0, 0); |
||
| 6695 | |||
| 6696 | if (intval($countUsers) < 50) { |
||
| 6697 | $orderClause = "ORDER BY "; |
||
| 6698 | $orderClause .= api_sort_by_first_name() ? "firstname, lastname, username" : "lastname, firstname, username"; |
||
| 6699 | |||
| 6700 | $sql = "SELECT user_id, lastname, firstname, username |
||
| 6900 | |||
| 6901 | /** |
||
| 6902 | * Gets the number of rows in the session table filtered through the given |
||
| 6903 | * array of parameters |
||
| 6904 | * @param array Array of options/filters/keys |
||
| 6905 | * @return integer The number of rows, or false on wrong param |
||
| 6906 | * @assert ('a') === false |
||
| 6907 | */ |
||
| 6908 | static function get_count_admin_complete($options = array()) |
||
| 7001 | |||
| 7002 | /** |
||
| 7003 | * @param string $list_type |
||
| 7004 | * @return array |
||
| 7005 | */ |
||
| 7006 | public static function getGridColumns($list_type = 'simple') |
||
| 7081 | |||
| 7082 | /** |
||
| 7083 | * Converts all dates sent through the param array (given form) to correct dates with timezones |
||
| 7084 | * @param array The dates The same array, with times converted |
||
| 7085 | * @param boolean $applyFormat Whether apply the DATE_TIME_FORMAT_SHORT format for sessions |
||
| 7086 | * @return array The same array, with times converted |
||
| 7087 | */ |
||
| 7088 | static function convert_dates_to_local($params, $applyFormat = false) |
||
| 7130 | |||
| 7131 | /** |
||
| 7132 | * Gets the admin session list callback of the session/session_list.php |
||
| 7133 | * page with all user/details in the right fomat |
||
| 7134 | * @param array |
||
| 7135 | * @result array Array of rows results |
||
| 7136 | * @asset ('a') === false |
||
| 7137 | */ |
||
| 7138 | public static function get_sessions_admin_complete($options = array()) |
||
| 7375 | |||
| 7376 | /** |
||
| 7377 | * Compare two arrays |
||
| 7378 | * @param array $array1 |
||
| 7379 | * @param array $array2 |
||
| 7380 | * |
||
| 7381 | * @return array |
||
| 7382 | */ |
||
| 7383 | static function compareArraysToMerge($array1, $array2) |
||
| 7398 | |||
| 7399 | /** |
||
| 7400 | * Get link to the admin page for this session |
||
| 7401 | * @param int $id Session ID |
||
| 7402 | * @return mixed URL to the admin page to manage the session, or false on error |
||
| 7403 | */ |
||
| 7404 | public static function getAdminPath($id) |
||
| 7413 | |||
| 7414 | /** |
||
| 7415 | * Get link to the user page for this session. |
||
| 7416 | * If a course is provided, build the link to the course |
||
| 7417 | * @param int $id Session ID |
||
| 7418 | * @param int $courseId Course ID (optional) in case the link has to send straight to the course |
||
| 7419 | * @return mixed URL to the page to use the session, or false on error |
||
| 7420 | */ |
||
| 7421 | public static function getPath($id, $courseId = 0) |
||
| 7439 | |||
| 7440 | /** |
||
| 7441 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7442 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7443 | * for course where user is coach |
||
| 7444 | * i.e. coach for the course or |
||
| 7445 | * main coach for a session the course is in |
||
| 7446 | * for a session category (or woth no session category if empty) |
||
| 7447 | * |
||
| 7448 | * @param $userId |
||
| 7449 | * |
||
| 7450 | * @return array |
||
| 7451 | */ |
||
| 7452 | public static function getSessionCourseForUser($userId) |
||
| 7478 | |||
| 7479 | /** |
||
| 7480 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7481 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7482 | * @param $userId |
||
| 7483 | * |
||
| 7484 | * @return array |
||
| 7485 | */ |
||
| 7486 | public static function getCoursesForCourseSessionCoach($userId) |
||
| 7513 | |||
| 7514 | /** |
||
| 7515 | * Return true if coach is allowed to access this session |
||
| 7516 | * @param int $sessionId |
||
| 7517 | * @return bool |
||
| 7518 | */ |
||
| 7519 | public static function isSessionDateOkForCoach($sessionId) |
||
| 7553 | |||
| 7554 | /** |
||
| 7555 | * Return an associative array 'id_course' => [id_session1, id_session2...] |
||
| 7556 | * where course id_course is in sessions id_session1, id_session2 |
||
| 7557 | * @param $userId |
||
| 7558 | * |
||
| 7559 | * @return array |
||
| 7560 | */ |
||
| 7561 | public static function getCoursesForMainSessionCoach($userId) |
||
| 7586 | |||
| 7587 | /** |
||
| 7588 | * Return an array of course_id used in session $sessionId |
||
| 7589 | * @param $sessionId |
||
| 7590 | * |
||
| 7591 | * @return array |
||
| 7592 | */ |
||
| 7593 | public static function getCoursesInSession($sessionId) |
||
| 7612 | |||
| 7613 | /** |
||
| 7614 | * Return an array of courses in session for user |
||
| 7615 | * and for each courses the list of session that use this course for user |
||
| 7616 | * |
||
| 7617 | * [0] => array |
||
| 7618 | * userCatId |
||
| 7619 | * userCatTitle |
||
| 7620 | * courseInUserCatList |
||
| 7621 | * [0] => array |
||
| 7622 | * courseId |
||
| 7623 | * title |
||
| 7624 | * courseCode |
||
| 7625 | * sessionCatList |
||
| 7626 | * [0] => array |
||
| 7627 | * catSessionId |
||
| 7628 | * catSessionName |
||
| 7629 | * sessionList |
||
| 7630 | * [0] => array |
||
| 7631 | * sessionId |
||
| 7632 | * sessionName |
||
| 7633 | * |
||
| 7634 | * @param $userId |
||
| 7635 | * |
||
| 7636 | * @return array |
||
| 7637 | * |
||
| 7638 | */ |
||
| 7639 | public static function getNamedSessionCourseForCoach($userId) |
||
| 7707 | |||
| 7708 | /** |
||
| 7709 | * @param array $listA |
||
| 7710 | * @param array $listB |
||
| 7711 | * @return int |
||
| 7712 | */ |
||
| 7713 | View Code Duplication | private static function compareCatSessionInfo($listA, $listB) |
|
| 7723 | |||
| 7724 | /** |
||
| 7725 | * @param array $listA |
||
| 7726 | * @param array $listB |
||
| 7727 | * @return int |
||
| 7728 | */ |
||
| 7729 | private static function compareBySessionName($listA, $listB) |
||
| 7743 | |||
| 7744 | /** |
||
| 7745 | * @param array $listA |
||
| 7746 | * @param array $listB |
||
| 7747 | * @return int |
||
| 7748 | */ |
||
| 7749 | View Code Duplication | private static function compareByUserCourseCat($listA, $listB) |
|
| 7759 | |||
| 7760 | /** |
||
| 7761 | * @param array $listA |
||
| 7762 | * @param array $listB |
||
| 7763 | * @return int |
||
| 7764 | */ |
||
| 7765 | View Code Duplication | private static function compareByCourse($listA, $listB) |
|
| 7775 | |||
| 7776 | /** |
||
| 7777 | * Return HTML code for displaying session_course_for_coach |
||
| 7778 | * @param $userId |
||
| 7779 | * @return string |
||
| 7780 | */ |
||
| 7781 | public static function getHtmlNamedSessionCourseForCoach($userId) |
||
| 7847 | } |
||
| 7848 |
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: