| Conditions | 64 |
| Paths | > 20000 |
| Total Lines | 381 |
| Code Lines | 235 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function indexAction(Request $request) |
||
| 38 | { |
||
| 39 | $session = $this->getSession(); |
||
| 40 | $course = $this->getCourse(); |
||
| 41 | $courseCode = $course->getId(); |
||
| 42 | $result = $this->autoLaunch(); |
||
| 43 | |||
| 44 | $js = '<script>'.api_get_language_translate_html().'</script>'; |
||
| 45 | $htmlHeadXtra[] = $js; |
||
|
|
|||
| 46 | |||
| 47 | $user_id = api_get_user_id(); |
||
| 48 | $course_code = api_get_course_id(); |
||
| 49 | $courseId = api_get_course_int_id(); |
||
| 50 | $sessionId = api_get_session_id(); |
||
| 51 | $show_message = ''; |
||
| 52 | |||
| 53 | if (api_is_invitee()) { |
||
| 54 | $isInASession = $sessionId > 0; |
||
| 55 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 56 | $user_id, |
||
| 57 | $course_code, |
||
| 58 | $isInASession, |
||
| 59 | $sessionId |
||
| 60 | ); |
||
| 61 | |||
| 62 | if (!$isSubscribed) { |
||
| 63 | api_not_allowed(true); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | // Deleting group session |
||
| 68 | Session::erase('toolgroup'); |
||
| 69 | Session::erase('_gid'); |
||
| 70 | |||
| 71 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 72 | |||
| 73 | if ($isSpecialCourse) { |
||
| 74 | if (isset($_GET['autoreg']) && $_GET['autoreg'] == 1) { |
||
| 75 | if (CourseManager::subscribeUser($user_id, $course_code, STUDENT)) { |
||
| 76 | Session::write('is_allowed_in_course', true); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | $action = !empty($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; |
||
| 82 | |||
| 83 | if ($action == 'subscribe') { |
||
| 84 | if (Security::check_token('get')) { |
||
| 85 | Security::clear_token(); |
||
| 86 | $result = CourseManager::autoSubscribeToCourse($course_code); |
||
| 87 | if ($result) { |
||
| 88 | if (CourseManager::is_user_subscribed_in_course($user_id, $course_code)) { |
||
| 89 | Session::write('is_allowed_in_course', true); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | header('Location: '.api_get_self()); |
||
| 93 | exit; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /* Is the user allowed here? */ |
||
| 98 | api_protect_course_script(true); |
||
| 99 | |||
| 100 | /* STATISTICS */ |
||
| 101 | if (!isset($coursesAlreadyVisited[$course_code])) { |
||
| 102 | Event::accessCourse(); |
||
| 103 | $coursesAlreadyVisited[$course_code] = 1; |
||
| 104 | Session::write('coursesAlreadyVisited', $coursesAlreadyVisited); |
||
| 105 | } |
||
| 106 | |||
| 107 | $logInfo = [ |
||
| 108 | 'tool' => 'course-main', |
||
| 109 | 'tool_id' => 0, |
||
| 110 | 'tool_id_detail' => 0, |
||
| 111 | 'action' => $action, |
||
| 112 | ]; |
||
| 113 | Event::registerLog($logInfo); |
||
| 114 | |||
| 115 | /* Auto launch code */ |
||
| 116 | $autoLaunchWarning = ''; |
||
| 117 | $showAutoLaunchLpWarning = false; |
||
| 118 | $course_id = api_get_course_int_id(); |
||
| 119 | $lpAutoLaunch = api_get_course_setting('enable_lp_auto_launch'); |
||
| 120 | $session_id = api_get_session_id(); |
||
| 121 | $allowAutoLaunchForCourseAdmins = api_is_platform_admin() || api_is_allowed_to_edit(true, true) || api_is_coach(); |
||
| 122 | |||
| 123 | if (!empty($lpAutoLaunch)) { |
||
| 124 | if ($lpAutoLaunch == 2) { |
||
| 125 | // LP list |
||
| 126 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 127 | $showAutoLaunchLpWarning = true; |
||
| 128 | } else { |
||
| 129 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 130 | if (!isset($_SESSION[$session_key])) { |
||
| 131 | // Redirecting to the LP |
||
| 132 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 133 | $_SESSION[$session_key] = true; |
||
| 134 | header("Location: $url"); |
||
| 135 | exit; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 140 | $condition = ''; |
||
| 141 | if (!empty($session_id)) { |
||
| 142 | $condition = api_get_session_condition($session_id); |
||
| 143 | $sql = "SELECT id FROM $lp_table |
||
| 144 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 145 | LIMIT 1"; |
||
| 146 | $result = Database::query($sql); |
||
| 147 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 148 | if (Database::num_rows($result) == 0) { |
||
| 149 | $condition = ''; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $sql = "SELECT id FROM $lp_table |
||
| 154 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 155 | LIMIT 1"; |
||
| 156 | $result = Database::query($sql); |
||
| 157 | if (Database::num_rows($result) > 0) { |
||
| 158 | $lp_data = Database::fetch_array($result, 'ASSOC'); |
||
| 159 | if (!empty($lp_data['id'])) { |
||
| 160 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 161 | $showAutoLaunchLpWarning = true; |
||
| 162 | } else { |
||
| 163 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 164 | if (!isset($_SESSION[$session_key])) { |
||
| 165 | // Redirecting to the LP |
||
| 166 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['id']; |
||
| 167 | |||
| 168 | $_SESSION[$session_key] = true; |
||
| 169 | header("Location: $url"); |
||
| 170 | exit; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($showAutoLaunchLpWarning) { |
||
| 179 | $autoLaunchWarning = get_lang('The learning path auto-launch setting is ON. When learners enter this course, they will be automatically redirected to the learning path marked as auto-launch.'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $forumAutoLaunch = api_get_course_setting('enable_forum_auto_launch'); |
||
| 183 | if ($forumAutoLaunch == 1) { |
||
| 184 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 185 | if (empty($autoLaunchWarning)) { |
||
| 186 | $autoLaunchWarning = get_lang('The forum\'s auto-launch setting is on. Students will be redirected to the forum tool when entering this course.'); |
||
| 187 | } |
||
| 188 | } else { |
||
| 189 | $url = api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 190 | header("Location: $url"); |
||
| 191 | exit; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if (api_get_configuration_value('allow_exercise_auto_launch')) { |
||
| 196 | $exerciseAutoLaunch = (int) api_get_course_setting('enable_exercise_auto_launch'); |
||
| 197 | if ($exerciseAutoLaunch == 2) { |
||
| 198 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 199 | if (empty($autoLaunchWarning)) { |
||
| 200 | $autoLaunchWarning = get_lang( |
||
| 201 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToTheExerciseList' |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | } else { |
||
| 205 | // Redirecting to the document |
||
| 206 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 207 | header("Location: $url"); |
||
| 208 | exit; |
||
| 209 | } |
||
| 210 | } elseif ($exerciseAutoLaunch == 1) { |
||
| 211 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 212 | if (empty($autoLaunchWarning)) { |
||
| 213 | $autoLaunchWarning = get_lang( |
||
| 214 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise' |
||
| 215 | ); |
||
| 216 | } |
||
| 217 | } else { |
||
| 218 | // Redirecting to an exercise |
||
| 219 | $table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 220 | $condition = ''; |
||
| 221 | if (!empty($session_id)) { |
||
| 222 | $condition = api_get_session_condition($session_id); |
||
| 223 | $sql = "SELECT iid FROM $table |
||
| 224 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 225 | LIMIT 1"; |
||
| 226 | $result = Database::query($sql); |
||
| 227 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 228 | if (Database::num_rows($result) == 0) { |
||
| 229 | $condition = ''; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | $sql = "SELECT iid FROM $table |
||
| 234 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 235 | LIMIT 1"; |
||
| 236 | $result = Database::query($sql); |
||
| 237 | if (Database::num_rows($result) > 0) { |
||
| 238 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 239 | $exerciseId = $row['iid']; |
||
| 240 | $url = api_get_path(WEB_CODE_PATH). |
||
| 241 | 'exercise/overview.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'&id_session='.$session_id; |
||
| 242 | header("Location: $url"); |
||
| 243 | exit; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | $documentAutoLaunch = api_get_course_setting('enable_document_auto_launch'); |
||
| 250 | if ($documentAutoLaunch == 1) { |
||
| 251 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 252 | if (empty($autoLaunchWarning)) { |
||
| 253 | $autoLaunchWarning = get_lang('The document auto-launch feature configuration is enabled. Learners will be automatically redirected to document tool.'); |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | // Redirecting to the document |
||
| 257 | $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 258 | header("Location: $url"); |
||
| 259 | exit; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | // Used in different pages |
||
| 264 | $tool_table = Database::get_course_table(TABLE_TOOL_LIST); |
||
| 265 | |||
| 266 | /* Introduction section (editable by course admins) */ |
||
| 267 | $content = Display::return_introduction_section( |
||
| 268 | TOOL_COURSE_HOMEPAGE, |
||
| 269 | [ |
||
| 270 | 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/', |
||
| 271 | 'CreateDocumentDir' => 'document/', |
||
| 272 | 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/', |
||
| 273 | ] |
||
| 274 | ); |
||
| 275 | |||
| 276 | /* SWITCH TO A DIFFERENT HOMEPAGE VIEW |
||
| 277 | the setting homepage_view is adjustable through |
||
| 278 | the platform administration section */ |
||
| 279 | if (!empty($autoLaunchWarning)) { |
||
| 280 | $show_message .= Display::return_message( |
||
| 281 | $autoLaunchWarning, |
||
| 282 | 'warning' |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | //require 'activity.php'; |
||
| 287 | // Activity start |
||
| 288 | |||
| 289 | |||
| 290 | $id = isset($_GET['id']) ? (int) $_GET['id'] : null; |
||
| 291 | $course_id = api_get_course_int_id(); |
||
| 292 | $session_id = api_get_session_id(); |
||
| 293 | |||
| 294 | // Work with data post askable by admin of course |
||
| 295 | if (api_is_platform_admin()) { |
||
| 296 | // Show message to confirm that a tool it to be hidden from available tools |
||
| 297 | // visibility 0,1->2 |
||
| 298 | if (!empty($_GET['askDelete'])) { |
||
| 299 | $content .= '<div id="toolhide">'.get_lang('Do you really want to delete this link?').'<br /> |
||
| 300 | <a href="'.api_get_self().'">'.get_lang('No').'</a> | |
||
| 301 | <a href="'.api_get_self().'?delete=yes&id='.$id.'">'.get_lang('Yes').'</a> |
||
| 302 | </div>'; |
||
| 303 | } elseif (isset($_GET['delete']) && $_GET['delete']) { |
||
| 304 | /* |
||
| 305 | * Process hiding a tools from available tools. |
||
| 306 | */ |
||
| 307 | Database::query("DELETE FROM $tool_table WHERE c_id = $course_id AND id='$id' AND added_tool=1"); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | // Course legal |
||
| 312 | $enabled = api_get_plugin_setting('courselegal', 'tool_enable'); |
||
| 313 | $pluginExtra = null; |
||
| 314 | if ($enabled === 'true') { |
||
| 315 | require_once api_get_path(SYS_PLUGIN_PATH).'courselegal/config.php'; |
||
| 316 | $plugin = \CourseLegalPlugin::create(); |
||
| 317 | $pluginExtra = $plugin->getTeacherLink(); |
||
| 318 | } |
||
| 319 | |||
| 320 | // Start of tools for CourseAdmins (teachers/tutors) |
||
| 321 | if ($session_id === 0 && api_is_course_admin() && api_is_allowed_to_edit(null, true)) { |
||
| 322 | $content .= '<div class="alert alert-success" style="border:0px; margin-top: 0px;padding:0px;"> |
||
| 323 | <div class="normal-message" id="id_normal_message" style="display:none">'; |
||
| 324 | $content .= '<img src="'.api_get_path(WEB_PATH).'main/inc/lib/javascript/indicator.gif"/> '; |
||
| 325 | $content .= get_lang('Please stand by...'); |
||
| 326 | $content .= '</div> |
||
| 327 | <div class="alert alert-success" id="id_confirmation_message" style="display:none"></div> |
||
| 328 | </div>'; |
||
| 329 | $content .= $pluginExtra; |
||
| 330 | } elseif (api_is_coach()) { |
||
| 331 | $content .= $pluginExtra; |
||
| 332 | if (api_get_setting('show_session_data') === 'true' && $session_id > 0) { |
||
| 333 | $content .= '<div class="row"> |
||
| 334 | <div class="col-xs-12 col-md-12"> |
||
| 335 | <span class="viewcaption">'.get_lang('Session\'s data').'</span> |
||
| 336 | <table class="course_activity_home">'; |
||
| 337 | $content .= CourseHome::show_session_data($session_id); |
||
| 338 | $content .= '</table></div></div>'; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | $blocks = CourseHome::getUserBlocks(); |
||
| 343 | |||
| 344 | |||
| 345 | // Activity end |
||
| 346 | |||
| 347 | // Get session-career diagram |
||
| 348 | $diagram = ''; |
||
| 349 | $allow = api_get_configuration_value('allow_career_diagram'); |
||
| 350 | if ($allow === true) { |
||
| 351 | $htmlHeadXtra[] = api_get_js('jsplumb2.js'); |
||
| 352 | $extra = new ExtraFieldValue('session'); |
||
| 353 | $value = $extra->get_values_by_handler_and_field_variable( |
||
| 354 | api_get_session_id(), |
||
| 355 | 'external_career_id' |
||
| 356 | ); |
||
| 357 | |||
| 358 | if (!empty($value) && isset($value['value'])) { |
||
| 359 | $careerId = $value['value']; |
||
| 360 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 361 | $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 362 | 'external_career_id', |
||
| 363 | $careerId, |
||
| 364 | false, |
||
| 365 | false, |
||
| 366 | false |
||
| 367 | ); |
||
| 368 | |||
| 369 | if (!empty($item) && isset($item['item_id'])) { |
||
| 370 | $careerId = $item['item_id']; |
||
| 371 | $career = new Career(); |
||
| 372 | $careerInfo = $career->get($careerId); |
||
| 373 | if (!empty($careerInfo)) { |
||
| 374 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 375 | $item = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 376 | $careerId, |
||
| 377 | 'career_diagram', |
||
| 378 | false, |
||
| 379 | false, |
||
| 380 | false |
||
| 381 | ); |
||
| 382 | |||
| 383 | if (!empty($item) && isset($item['value']) && !empty($item['value'])) { |
||
| 384 | /** @var Graph $graph */ |
||
| 385 | $graph = UnserializeApi::unserialize( |
||
| 386 | 'career', |
||
| 387 | $item['value'] |
||
| 388 | ); |
||
| 389 | $diagram = Career::renderDiagram($careerInfo, $graph); |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | $content = '<div id="course_tools">'.$diagram.$content.'</div>'; |
||
| 397 | |||
| 398 | // Deleting the objects |
||
| 399 | Session::erase('_gid'); |
||
| 400 | Session::erase('oLP'); |
||
| 401 | Session::erase('lpobject'); |
||
| 402 | api_remove_in_gradebook(); |
||
| 403 | \Exercise::cleanSessionVariables(); |
||
| 404 | \DocumentManager::removeGeneratedAudioTempFile(); |
||
| 405 | |||
| 406 | return $this->render( |
||
| 407 | '@ChamiloTheme/Course/home.html.twig', |
||
| 408 | [ |
||
| 409 | 'course' => $course, |
||
| 410 | 'diagram' => $diagram, |
||
| 411 | // 'session_info' => $sessionInfo, |
||
| 412 | 'icons' => $result['content'], |
||
| 413 | 'blocks' => $blocks, |
||
| 414 | //'edit_icons' => $editIcons, |
||
| 415 | //'introduction_text' => $introduction, |
||
| 416 | 'exercise_warning' => null, |
||
| 417 | 'lp_warning' => null, |
||
| 418 | ] |
||
| 996 |