| Total Complexity | 133 |
| Total Lines | 956 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CourseHomeController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CourseHomeController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class CourseHomeController extends ToolBaseController |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @Route("/", name="course_home") |
||
| 32 | * @Route("/index.php", methods={"GET"}) |
||
| 33 | * |
||
| 34 | * @return Response |
||
| 35 | */ |
||
| 36 | public function indexAction(Request $request) |
||
| 37 | { |
||
| 38 | $session = $this->getSession(); |
||
| 39 | $course = $this->getCourse(); |
||
| 40 | $courseCode = $course->getId(); |
||
| 41 | $result = $this->autoLaunch(); |
||
| 42 | |||
| 43 | $js = '<script>'.api_get_language_translate_html().'</script>'; |
||
| 44 | $htmlHeadXtra[] = $js; |
||
|
|
|||
| 45 | |||
| 46 | $user_id = api_get_user_id(); |
||
| 47 | $course_code = api_get_course_id(); |
||
| 48 | $courseId = api_get_course_int_id(); |
||
| 49 | $sessionId = api_get_session_id(); |
||
| 50 | $show_message = ''; |
||
| 51 | |||
| 52 | if (api_is_invitee()) { |
||
| 53 | $isInASession = $sessionId > 0; |
||
| 54 | $isSubscribed = CourseManager::is_user_subscribed_in_course( |
||
| 55 | $user_id, |
||
| 56 | $course_code, |
||
| 57 | $isInASession, |
||
| 58 | $sessionId |
||
| 59 | ); |
||
| 60 | |||
| 61 | if (!$isSubscribed) { |
||
| 62 | api_not_allowed(true); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | // Deleting group session |
||
| 67 | Session::erase('toolgroup'); |
||
| 68 | Session::erase('_gid'); |
||
| 69 | |||
| 70 | $isSpecialCourse = CourseManager::isSpecialCourse($courseId); |
||
| 71 | |||
| 72 | if ($isSpecialCourse) { |
||
| 73 | if (isset($_GET['autoreg']) && $_GET['autoreg'] == 1) { |
||
| 74 | if (CourseManager::subscribeUser($user_id, $course_code, STUDENT)) { |
||
| 75 | Session::write('is_allowed_in_course', true); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $action = !empty($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; |
||
| 81 | |||
| 82 | if ($action == 'subscribe') { |
||
| 83 | if (Security::check_token('get')) { |
||
| 84 | Security::clear_token(); |
||
| 85 | $result = CourseManager::autoSubscribeToCourse($course_code); |
||
| 86 | if ($result) { |
||
| 87 | if (CourseManager::is_user_subscribed_in_course($user_id, $course_code)) { |
||
| 88 | Session::write('is_allowed_in_course', true); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | header('Location: '.api_get_self()); |
||
| 92 | exit; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /* Is the user allowed here? */ |
||
| 97 | api_protect_course_script(true); |
||
| 98 | |||
| 99 | /* STATISTICS */ |
||
| 100 | if (!isset($coursesAlreadyVisited[$course_code])) { |
||
| 101 | Event::accessCourse(); |
||
| 102 | $coursesAlreadyVisited[$course_code] = 1; |
||
| 103 | Session::write('coursesAlreadyVisited', $coursesAlreadyVisited); |
||
| 104 | } |
||
| 105 | |||
| 106 | $logInfo = [ |
||
| 107 | 'tool' => 'course-main', |
||
| 108 | 'action' => $action, |
||
| 109 | ]; |
||
| 110 | Event::registerLog($logInfo); |
||
| 111 | |||
| 112 | /* Auto launch code */ |
||
| 113 | $autoLaunchWarning = ''; |
||
| 114 | $showAutoLaunchLpWarning = false; |
||
| 115 | $course_id = api_get_course_int_id(); |
||
| 116 | $lpAutoLaunch = api_get_course_setting('enable_lp_auto_launch'); |
||
| 117 | $session_id = api_get_session_id(); |
||
| 118 | $allowAutoLaunchForCourseAdmins = api_is_platform_admin() || api_is_allowed_to_edit(true, true) || api_is_coach(); |
||
| 119 | |||
| 120 | if (!empty($lpAutoLaunch)) { |
||
| 121 | if ($lpAutoLaunch == 2) { |
||
| 122 | // LP list |
||
| 123 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 124 | $showAutoLaunchLpWarning = true; |
||
| 125 | } else { |
||
| 126 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 127 | if (!isset($_SESSION[$session_key])) { |
||
| 128 | // Redirecting to the LP |
||
| 129 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 130 | $_SESSION[$session_key] = true; |
||
| 131 | header("Location: $url"); |
||
| 132 | exit; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | $lp_table = Database::get_course_table(TABLE_LP_MAIN); |
||
| 137 | $condition = ''; |
||
| 138 | if (!empty($session_id)) { |
||
| 139 | $condition = api_get_session_condition($session_id); |
||
| 140 | $sql = "SELECT id FROM $lp_table |
||
| 141 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 142 | LIMIT 1"; |
||
| 143 | $result = Database::query($sql); |
||
| 144 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 145 | if (Database::num_rows($result) == 0) { |
||
| 146 | $condition = ''; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $sql = "SELECT id FROM $lp_table |
||
| 151 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 152 | LIMIT 1"; |
||
| 153 | $result = Database::query($sql); |
||
| 154 | if (Database::num_rows($result) > 0) { |
||
| 155 | $lp_data = Database::fetch_array($result, 'ASSOC'); |
||
| 156 | if (!empty($lp_data['id'])) { |
||
| 157 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 158 | $showAutoLaunchLpWarning = true; |
||
| 159 | } else { |
||
| 160 | $session_key = 'lp_autolaunch_'.$session_id.'_'.api_get_course_int_id().'_'.api_get_user_id(); |
||
| 161 | if (!isset($_SESSION[$session_key])) { |
||
| 162 | // Redirecting to the LP |
||
| 163 | $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.$lp_data['id']; |
||
| 164 | |||
| 165 | $_SESSION[$session_key] = true; |
||
| 166 | header("Location: $url"); |
||
| 167 | exit; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | if ($showAutoLaunchLpWarning) { |
||
| 176 | $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.'); |
||
| 177 | } |
||
| 178 | |||
| 179 | $forumAutoLaunch = api_get_course_setting('enable_forum_auto_launch'); |
||
| 180 | if ($forumAutoLaunch == 1) { |
||
| 181 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 182 | if (empty($autoLaunchWarning)) { |
||
| 183 | $autoLaunchWarning = get_lang('The forum\'s auto-launch setting is on. Students will be redirected to the forum tool when entering this course.'); |
||
| 184 | } |
||
| 185 | } else { |
||
| 186 | $url = api_get_path(WEB_CODE_PATH).'forum/index.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 187 | header("Location: $url"); |
||
| 188 | exit; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | if (api_get_configuration_value('allow_exercise_auto_launch')) { |
||
| 193 | $exerciseAutoLaunch = (int) api_get_course_setting('enable_exercise_auto_launch'); |
||
| 194 | if ($exerciseAutoLaunch == 2) { |
||
| 195 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 196 | if (empty($autoLaunchWarning)) { |
||
| 197 | $autoLaunchWarning = get_lang( |
||
| 198 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToTheExerciseList' |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | } else { |
||
| 202 | // Redirecting to the document |
||
| 203 | $url = api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 204 | header("Location: $url"); |
||
| 205 | exit; |
||
| 206 | } |
||
| 207 | } elseif ($exerciseAutoLaunch == 1) { |
||
| 208 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 209 | if (empty($autoLaunchWarning)) { |
||
| 210 | $autoLaunchWarning = get_lang( |
||
| 211 | 'TheExerciseAutoLaunchSettingIsONStudentsWillBeRedirectToAnSpecificExercise' |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | } else { |
||
| 215 | // Redirecting to an exercise |
||
| 216 | $table = Database::get_course_table(TABLE_QUIZ_TEST); |
||
| 217 | $condition = ''; |
||
| 218 | if (!empty($session_id)) { |
||
| 219 | $condition = api_get_session_condition($session_id); |
||
| 220 | $sql = "SELECT iid FROM $table |
||
| 221 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 222 | LIMIT 1"; |
||
| 223 | $result = Database::query($sql); |
||
| 224 | // If we found nothing in the session we just called the session_id = 0 autolaunch |
||
| 225 | if (Database::num_rows($result) == 0) { |
||
| 226 | $condition = ''; |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | $sql = "SELECT iid FROM $table |
||
| 231 | WHERE c_id = $course_id AND autolaunch = 1 $condition |
||
| 232 | LIMIT 1"; |
||
| 233 | $result = Database::query($sql); |
||
| 234 | if (Database::num_rows($result) > 0) { |
||
| 235 | $row = Database::fetch_array($result, 'ASSOC'); |
||
| 236 | $exerciseId = $row['iid']; |
||
| 237 | $url = api_get_path(WEB_CODE_PATH). |
||
| 238 | 'exercise/overview.php?exerciseId='.$exerciseId.'&'.api_get_cidreq().'&id_session='.$session_id; |
||
| 239 | header("Location: $url"); |
||
| 240 | exit; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | $documentAutoLaunch = api_get_course_setting('enable_document_auto_launch'); |
||
| 247 | if ($documentAutoLaunch == 1) { |
||
| 248 | if ($allowAutoLaunchForCourseAdmins) { |
||
| 249 | if (empty($autoLaunchWarning)) { |
||
| 250 | $autoLaunchWarning = get_lang('The document auto-launch feature configuration is enabled. Learners will be automatically redirected to document tool.'); |
||
| 251 | } |
||
| 252 | } else { |
||
| 253 | // Redirecting to the document |
||
| 254 | $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&id_session='.$session_id; |
||
| 255 | header("Location: $url"); |
||
| 256 | exit; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | // Used in different pages |
||
| 261 | $tool_table = Database::get_course_table(TABLE_TOOL_LIST); |
||
| 262 | |||
| 263 | /* Introduction section (editable by course admins) */ |
||
| 264 | $content = Display::return_introduction_section( |
||
| 265 | TOOL_COURSE_HOMEPAGE, |
||
| 266 | [ |
||
| 267 | 'CreateDocumentWebDir' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/document/', |
||
| 268 | 'CreateDocumentDir' => 'document/', |
||
| 269 | 'BaseHref' => api_get_path(WEB_COURSE_PATH).api_get_course_path().'/', |
||
| 270 | ] |
||
| 271 | ); |
||
| 272 | |||
| 273 | /* SWITCH TO A DIFFERENT HOMEPAGE VIEW |
||
| 274 | the setting homepage_view is adjustable through |
||
| 275 | the platform administration section */ |
||
| 276 | if (!empty($autoLaunchWarning)) { |
||
| 277 | $show_message .= Display::return_message( |
||
| 278 | $autoLaunchWarning, |
||
| 279 | 'warning' |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | //require 'activity.php'; |
||
| 284 | // Activity start |
||
| 285 | $id = isset($_GET['id']) ? (int) $_GET['id'] : null; |
||
| 286 | $course_id = api_get_course_int_id(); |
||
| 287 | $session_id = api_get_session_id(); |
||
| 288 | |||
| 289 | // Work with data post askable by admin of course |
||
| 290 | if (api_is_platform_admin()) { |
||
| 291 | // Show message to confirm that a tool it to be hidden from available tools |
||
| 292 | // visibility 0,1->2 |
||
| 293 | if (!empty($_GET['askDelete'])) { |
||
| 294 | $content .= '<div id="toolhide">'.get_lang('Do you really want to delete this link?').'<br /> |
||
| 295 | <a href="'.api_get_self().'">'.get_lang('No').'</a> | |
||
| 296 | <a href="'.api_get_self().'?delete=yes&id='.$id.'">'.get_lang('Yes').'</a> |
||
| 297 | </div>'; |
||
| 298 | } elseif (isset($_GET['delete']) && $_GET['delete']) { |
||
| 299 | /* |
||
| 300 | * Process hiding a tools from available tools. |
||
| 301 | */ |
||
| 302 | Database::query("DELETE FROM $tool_table WHERE c_id = $course_id AND id='$id' AND added_tool=1"); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | // Course legal |
||
| 307 | $enabled = api_get_plugin_setting('courselegal', 'tool_enable'); |
||
| 308 | $pluginExtra = null; |
||
| 309 | if ($enabled === 'true') { |
||
| 310 | require_once api_get_path(SYS_PLUGIN_PATH).'courselegal/config.php'; |
||
| 311 | $plugin = \CourseLegalPlugin::create(); |
||
| 312 | $pluginExtra = $plugin->getTeacherLink(); |
||
| 313 | } |
||
| 314 | |||
| 315 | // Start of tools for CourseAdmins (teachers/tutors) |
||
| 316 | if ($session_id === 0 && api_is_course_admin() && api_is_allowed_to_edit(null, true)) { |
||
| 317 | $content .= '<div class="alert alert-success" style="border:0px; margin-top: 0px;padding:0px;"> |
||
| 318 | <div class="normal-message" id="id_normal_message" style="display:none">'; |
||
| 319 | $content .= '<img src="'.api_get_path(WEB_PATH).'main/inc/lib/javascript/indicator.gif"/> '; |
||
| 320 | $content .= get_lang('Please stand by...'); |
||
| 321 | $content .= '</div> |
||
| 322 | <div class="alert alert-success" id="id_confirmation_message" style="display:none"></div> |
||
| 323 | </div>'; |
||
| 324 | $content .= $pluginExtra; |
||
| 325 | } elseif (api_is_coach()) { |
||
| 326 | $content .= $pluginExtra; |
||
| 327 | if (api_get_setting('show_session_data') === 'true' && $session_id > 0) { |
||
| 328 | $content .= '<div class="row"> |
||
| 329 | <div class="col-xs-12 col-md-12"> |
||
| 330 | <span class="viewcaption">'.get_lang('Session\'s data').'</span> |
||
| 331 | <table class="course_activity_home">'; |
||
| 332 | $content .= CourseHome::show_session_data($session_id); |
||
| 333 | $content .= '</table></div></div>'; |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | $blocks = CourseHome::getUserBlocks(); |
||
| 338 | // Activity end |
||
| 339 | |||
| 340 | // Get session-career diagram |
||
| 341 | $diagram = ''; |
||
| 342 | $allow = api_get_configuration_value('allow_career_diagram'); |
||
| 343 | if ($allow === true) { |
||
| 344 | $htmlHeadXtra[] = api_get_js('jsplumb2.js'); |
||
| 345 | $extra = new ExtraFieldValue('session'); |
||
| 346 | $value = $extra->get_values_by_handler_and_field_variable( |
||
| 347 | api_get_session_id(), |
||
| 348 | 'external_career_id' |
||
| 349 | ); |
||
| 350 | |||
| 351 | if (!empty($value) && isset($value['value'])) { |
||
| 352 | $careerId = $value['value']; |
||
| 353 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 354 | $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value( |
||
| 355 | 'external_career_id', |
||
| 356 | $careerId, |
||
| 357 | false, |
||
| 358 | false, |
||
| 359 | false |
||
| 360 | ); |
||
| 361 | |||
| 362 | if (!empty($item) && isset($item['item_id'])) { |
||
| 363 | $careerId = $item['item_id']; |
||
| 364 | $career = new Career(); |
||
| 365 | $careerInfo = $career->get($careerId); |
||
| 366 | if (!empty($careerInfo)) { |
||
| 367 | $extraFieldValue = new ExtraFieldValue('career'); |
||
| 368 | $item = $extraFieldValue->get_values_by_handler_and_field_variable( |
||
| 369 | $careerId, |
||
| 370 | 'career_diagram', |
||
| 371 | false, |
||
| 372 | false, |
||
| 373 | false |
||
| 374 | ); |
||
| 375 | |||
| 376 | if (!empty($item) && isset($item['value']) && !empty($item['value'])) { |
||
| 377 | /** @var Graph $graph */ |
||
| 378 | $graph = UnserializeApi::unserialize( |
||
| 379 | 'career', |
||
| 380 | $item['value'] |
||
| 381 | ); |
||
| 382 | $diagram = Career::renderDiagram($careerInfo, $graph); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | $content = '<div id="course_tools">'.$diagram.$content.'</div>'; |
||
| 390 | |||
| 391 | // Deleting the objects |
||
| 392 | Session::erase('_gid'); |
||
| 393 | Session::erase('oLP'); |
||
| 394 | Session::erase('lpobject'); |
||
| 395 | api_remove_in_gradebook(); |
||
| 396 | \Exercise::cleanSessionVariables(); |
||
| 397 | \DocumentManager::removeGeneratedAudioTempFile(); |
||
| 398 | |||
| 399 | return $this->render( |
||
| 400 | '@ChamiloTheme/Course/home.html.twig', |
||
| 401 | [ |
||
| 402 | 'course' => $course, |
||
| 403 | 'diagram' => $diagram, |
||
| 404 | // 'session_info' => $sessionInfo, |
||
| 405 | 'icons' => $result['content'], |
||
| 406 | 'blocks' => $blocks, |
||
| 407 | //'edit_icons' => $editIcons, |
||
| 408 | //'introduction_text' => $introduction, |
||
| 409 | 'exercise_warning' => null, |
||
| 410 | 'lp_warning' => null, |
||
| 411 | ] |
||
| 412 | ); |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @Route("/show/{iconId}", methods={"GET"}) |
||
| 417 | * |
||
| 418 | * @param $iconId |
||
| 419 | * |
||
| 420 | * @return string|null |
||
| 421 | */ |
||
| 422 | public function showIconAction($iconId) |
||
| 423 | { |
||
| 424 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 425 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId]; |
||
| 426 | $tool = $this->getRepository( |
||
| 427 | 'Chamilo\CourseBundle\Entity\CTool' |
||
| 428 | )->findOneBy($criteria); |
||
| 429 | if ($tool) { |
||
| 430 | $tool->setVisibility(1); |
||
| 431 | } |
||
| 432 | $entityManager->persist($tool); |
||
| 433 | //$entityManager->flush(); |
||
| 434 | return Display::return_message(get_lang('Visible'), 'confirmation'); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @Route("/hide/{iconId}", methods={"GET"}) |
||
| 439 | * |
||
| 440 | * @param $iconId |
||
| 441 | * |
||
| 442 | * @return string|null |
||
| 443 | */ |
||
| 444 | public function hideIconAction($iconId) |
||
| 445 | { |
||
| 446 | if (!$this->isCourseTeacher()) { |
||
| 447 | return $this->abort(404); |
||
| 448 | } |
||
| 449 | |||
| 450 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 451 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId]; |
||
| 452 | $tool = $this->getRepository( |
||
| 453 | 'Chamilo\CourseBundle\Entity\CTool' |
||
| 454 | )->findOneBy($criteria); |
||
| 455 | if ($tool) { |
||
| 456 | $tool->setVisibility(0); |
||
| 457 | } |
||
| 458 | $entityManager->persist($tool); |
||
| 459 | //$entityManager->flush(); |
||
| 460 | return Display::return_message(get_lang('The tool is now invisible.'), 'confirmation'); |
||
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @Route("/delete/{iconId}", methods={"GET"}) |
||
| 465 | * |
||
| 466 | * @param $iconId |
||
| 467 | * |
||
| 468 | * @return string|null |
||
| 469 | */ |
||
| 470 | public function deleteIcon($iconId) |
||
| 471 | { |
||
| 472 | if (!$this->isCourseTeacher()) { |
||
| 473 | return $this->abort(404); |
||
| 474 | } |
||
| 475 | |||
| 476 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 477 | $criteria = ['cId' => api_get_course_int_id(), 'id' => $iconId, 'added_tool' => 1]; |
||
| 478 | $tool = $this->getRepository( |
||
| 479 | 'Chamilo\CourseBundle\Entity\CTool' |
||
| 480 | )->findOneBy($criteria); |
||
| 481 | $entityManager->remove($tool); |
||
| 482 | //$entityManager->flush(); |
||
| 483 | return Display::return_message(get_lang('Deleted'), 'confirmation'); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @Route("/icon_list", methods={"GET"}) |
||
| 488 | */ |
||
| 489 | public function iconListAction(Request $request) |
||
| 490 | { |
||
| 491 | $em = $this->getDoctrine()->getManager(); |
||
| 492 | $repo = $this->getDoctrine()->getRepository('ChamiloCourseBundle:CTool'); |
||
| 493 | |||
| 494 | $sessionId = intval($request->get('id_session')); |
||
| 495 | $itemsFromSession = []; |
||
| 496 | if (!empty($sessionId)) { |
||
| 497 | $query = $repo->createQueryBuilder('a'); |
||
| 498 | $query->select('s'); |
||
| 499 | $query->from('Chamilo\CourseBundle\Entity\CTool', 's'); |
||
| 500 | $query->where('s.cId = :courseId AND s.sessionId = :sessionId') |
||
| 501 | ->setParameters( |
||
| 502 | [ |
||
| 503 | 'course' => $this->getCourse()->getId(), |
||
| 504 | 'sessionId' => $sessionId, |
||
| 505 | ] |
||
| 506 | ); |
||
| 507 | $itemsFromSession = $query->getQuery()->getResult(); |
||
| 508 | |||
| 509 | $itemNameList = []; |
||
| 510 | foreach ($itemsFromSession as $item) { |
||
| 511 | $itemNameList[] = $item->getName(); |
||
| 512 | } |
||
| 513 | |||
| 514 | //$itemsFromSession = $this->getRepository()->findBy($criteria); |
||
| 515 | $query = $repo->createQueryBuilder('a'); |
||
| 516 | $query->select('s'); |
||
| 517 | $query->from('Chamilo\CourseBundle\Entity\CTool', 's'); |
||
| 518 | $query->where('s.cId = :courseId AND s.sessionId = 0') |
||
| 519 | ->setParameters( |
||
| 520 | [ |
||
| 521 | 'courseId' => $this->getCourse()->getId(), |
||
| 522 | ] |
||
| 523 | ); |
||
| 524 | if (!empty($itemNameList)) { |
||
| 525 | $query->andWhere($query->expr()->notIn('s.name', $itemNameList)); |
||
| 526 | } |
||
| 527 | $itemsFromCourse = $query->getQuery()->getResult(); |
||
| 528 | } else { |
||
| 529 | $criteria = ['cId' => $this->getCourse()->getId(), 'sessionId' => 0]; |
||
| 530 | $itemsFromCourse = $repo->findBy($criteria); |
||
| 531 | } |
||
| 532 | |||
| 533 | return $this->render( |
||
| 534 | '@ChamiloCourse/Home/list.html.twig', |
||
| 535 | [ |
||
| 536 | 'items_from_course' => $itemsFromCourse, |
||
| 537 | 'items_from_session' => $itemsFromSession, |
||
| 538 | 'links' => '', |
||
| 539 | ] |
||
| 540 | ); |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * @Route("/{itemName}/add", methods={"GET", "POST"}) |
||
| 545 | * |
||
| 546 | * @param $itemName |
||
| 547 | * |
||
| 548 | * @return mixed |
||
| 549 | */ |
||
| 550 | public function addIconAction($itemName) |
||
| 551 | { |
||
| 552 | if (!$this->isCourseTeacher()) { |
||
| 553 | return $this->abort(404); |
||
| 554 | } |
||
| 555 | |||
| 556 | $sessionId = intval($this->getRequest()->get('id_session')); |
||
| 557 | |||
| 558 | if (empty($sessionId)) { |
||
| 559 | return $this->abort(500); |
||
| 560 | } |
||
| 561 | |||
| 562 | $criteria = ['cId' => $this->getCourse()->getId(), 'sessionId' => 0, 'name' => $itemName]; |
||
| 563 | $itemFromDatabase = $this->getRepository()->findOneBy($criteria); |
||
| 564 | |||
| 565 | if (!$itemFromDatabase) { |
||
| 566 | $this->createNotFoundException(); |
||
| 567 | } |
||
| 568 | /** @var CTool $item */ |
||
| 569 | $item = clone $itemFromDatabase; |
||
| 570 | $item->setId(null); |
||
| 571 | $item->setSessionId($sessionId); |
||
| 572 | $form = $this->createForm($this->getFormType(), $item); |
||
| 573 | |||
| 574 | $form->handleRequest($this->getRequest()); |
||
| 575 | |||
| 576 | if ($form->isValid()) { |
||
| 577 | $query = $this->getDoctrine()->getManager()->createQueryBuilder('a'); |
||
| 578 | $query->select('MAX(s.id) as id'); |
||
| 579 | $query->from('Chamilo\CourseBundle\Entity\CTool', 's'); |
||
| 580 | $query->where('s.cId = :courseId')->setParameter('courseId', $this->getCourse()->getId()); |
||
| 581 | $result = $query->getQuery()->getArrayResult(); |
||
| 582 | $maxId = $result[0]['id'] + 1; |
||
| 583 | $item->setId($maxId); |
||
| 584 | |||
| 585 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 586 | $entityManager->persist($item); |
||
| 587 | $entityManager->flush(); |
||
| 588 | $customIcon = $item->getCustomIcon(); |
||
| 589 | if (!empty($customIcon)) { |
||
| 590 | $item->createGrayIcon($this->get('imagine')); |
||
| 591 | } |
||
| 592 | |||
| 593 | $this->get('session')->getFlashBag()->add('success', "Added"); |
||
| 594 | $url = $this->generateUrl('course_home.controller:iconListAction', ['id_session' => $sessionId]); |
||
| 595 | |||
| 596 | return $this->redirect($url); |
||
| 597 | } |
||
| 598 | |||
| 599 | $this->getTemplate()->assign('item', $item); |
||
| 600 | $this->getTemplate()->assign('form', $form->createView()); |
||
| 601 | $this->getTemplate()->assign('links', $this->generateLinks()); |
||
| 602 | |||
| 603 | return $this->render('@ChamiloCourse/Home/add.html.twig'); |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @Route("/{itemId}/edit", methods={"GET"}) |
||
| 608 | */ |
||
| 609 | public function editIconAction($itemId) |
||
| 610 | { |
||
| 611 | if (!$this->isCourseTeacher()) { |
||
| 612 | return $this->abort(404); |
||
| 613 | } |
||
| 614 | |||
| 615 | $sessionId = intval($this->getRequest()->get('id_session')); |
||
| 616 | |||
| 617 | $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId]; |
||
| 618 | /** @var CTool $item */ |
||
| 619 | $item = $this->getRepository()->findOneBy($criteria); |
||
| 620 | |||
| 621 | $form = $this->createForm($this->getFormType(), $item); |
||
| 622 | $form->handleRequest($this->getRequest()); |
||
| 623 | |||
| 624 | if ($form->isValid()) { |
||
| 625 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 626 | $entityManager->persist($item); |
||
| 627 | $entityManager->flush(); |
||
| 628 | |||
| 629 | $customIcon = $item->getCustomIcon(); |
||
| 630 | if (!empty($customIcon)) { |
||
| 631 | $item->createGrayIcon($this->get('imagine')); |
||
| 632 | } |
||
| 633 | |||
| 634 | $this->get('session')->getFlashBag()->add('success', "Updated"); |
||
| 635 | $url = $this->generateUrl('course_home.controller:iconListAction', ['id_session' => $sessionId]); |
||
| 636 | |||
| 637 | return $this->redirect($url); |
||
| 638 | } |
||
| 639 | |||
| 640 | $this->getTemplate()->assign('item', $item); |
||
| 641 | $this->getTemplate()->assign('form', $form->createView()); |
||
| 642 | $this->getTemplate()->assign('links', $this->generateLinks()); |
||
| 643 | |||
| 644 | return $this->render('@ChamiloCourse/Home/edit.html.twig'); |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @Route("/{itemId}/delete", methods={"GET"}) |
||
| 649 | */ |
||
| 650 | public function deleteIconAction($itemId) |
||
| 651 | { |
||
| 652 | if (!$this->isCourseTeacher()) { |
||
| 653 | return $this->abort(404); |
||
| 654 | } |
||
| 655 | |||
| 656 | $criteria = ['cId' => $this->getCourse()->getId(), 'id' => $itemId]; |
||
| 657 | |||
| 658 | /** @var CTool $item */ |
||
| 659 | $item = $this->getRepository()->findOneBy($criteria); |
||
| 660 | $entityManager = $this->getDoctrine()->getManager(); |
||
| 661 | $sessionId = $item->getSessionId(); |
||
| 662 | if (!empty($sessionId)) { |
||
| 663 | $entityManager->remove($item); |
||
| 664 | } else { |
||
| 665 | $item->setCustomIcon(null); |
||
| 666 | $entityManager->persist($item); |
||
| 667 | } |
||
| 668 | $entityManager->flush(); |
||
| 669 | $this->get('session')->getFlashBag()->add('success', "Deleted"); |
||
| 670 | |||
| 671 | $this->getTemplate()->assign('links', $this->generateLinks()); |
||
| 672 | $url = $this->generateUrl('course_home.controller:iconListAction'); |
||
| 673 | |||
| 674 | return $this->redirect($url); |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param string $title |
||
| 679 | * @param string $content |
||
| 680 | * @param string $class |
||
| 681 | * |
||
| 682 | * @return string |
||
| 683 | */ |
||
| 684 | private function return_block($title, $content, $class = null) |
||
| 685 | { |
||
| 686 | $html = '<div class="row"> |
||
| 687 | <div class="col-xs-12 col-md-12"> |
||
| 688 | <div class="title-tools">'.$title.'</div> |
||
| 689 | </div> |
||
| 690 | </div> |
||
| 691 | <div class="row '.$class.'">'.$content.'</div>'; |
||
| 692 | |||
| 693 | return $html; |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @return array |
||
| 698 | */ |
||
| 699 | private function renderActivityView() |
||
| 847 | } |
||
| 848 | |||
| 849 | /** |
||
| 850 | * @return array |
||
| 851 | */ |
||
| 852 | private function autoLaunch() |
||
| 984 | ]; |
||
| 985 | } |
||
| 986 | } |
||
| 987 |