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:
| 1 | <?php |
||
| 25 | class UserPortalController extends BaseController |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @Route("/add_course", name="add_course") |
||
| 29 | * @Method({"GET|POST"}) |
||
| 30 | * @Security("has_role('ROLE_USER')") |
||
| 31 | * |
||
| 32 | * @return Response |
||
| 33 | */ |
||
| 34 | public function addCourseAction() |
||
| 35 | { |
||
| 36 | // "Course validation" feature. This value affects the way of a new course creation: |
||
| 37 | // true - the new course is requested only and it is created after approval; |
||
| 38 | // false - the new course is created immediately, after filling this form. |
||
| 39 | $courseValidation = false; |
||
| 40 | if (api_get_setting('course.course_validation') == 'true' && |
||
| 41 | !api_is_platform_admin() |
||
| 42 | ) { |
||
| 43 | $courseValidation = true; |
||
| 44 | } |
||
| 45 | |||
| 46 | // Displaying the header. |
||
| 47 | $tool_name = $courseValidation ? get_lang('CreateCourseRequest') : get_lang('CreateSite'); |
||
| 48 | |||
| 49 | if (api_get_setting('course.allow_users_to_create_courses') == 'false' && |
||
| 50 | !api_is_platform_admin() |
||
| 51 | ) { |
||
| 52 | api_not_allowed(true); |
||
| 53 | } |
||
| 54 | |||
| 55 | // Check access rights. |
||
| 56 | if (!api_is_allowed_to_create_course()) { |
||
| 57 | api_not_allowed(true); |
||
| 58 | } |
||
| 59 | |||
| 60 | $url = $this->generateUrl('add_course'); |
||
| 61 | // Build the form. |
||
| 62 | $form = new \FormValidator('add_course', 'post', $url); |
||
| 63 | |||
| 64 | // Form title |
||
| 65 | $form->addElement('header', $tool_name); |
||
| 66 | |||
| 67 | // Title |
||
| 68 | $form->addElement( |
||
| 69 | 'text', |
||
| 70 | 'title', |
||
| 71 | array( |
||
| 72 | get_lang('CourseName'), |
||
| 73 | get_lang('Ex'), |
||
| 74 | ), |
||
| 75 | array( |
||
| 76 | 'id' => 'title', |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | $form->applyFilter('title', 'html_filter'); |
||
| 80 | $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 81 | |||
| 82 | $form->addButtonAdvancedSettings('advanced_params'); |
||
| 83 | $form->addElement( |
||
| 84 | 'html', |
||
| 85 | '<div id="advanced_params_options" style="display:none">' |
||
| 86 | ); |
||
| 87 | |||
| 88 | // Category category. |
||
| 89 | $url = api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_category'; |
||
| 90 | |||
| 91 | $form->addElement( |
||
| 92 | 'select_ajax', |
||
| 93 | 'category_code', |
||
| 94 | get_lang('CourseFaculty'), |
||
| 95 | null, |
||
| 96 | array('url' => $url) |
||
| 97 | ); |
||
| 98 | |||
| 99 | // Course code |
||
| 100 | $form->addText( |
||
| 101 | 'wanted_code', |
||
| 102 | array( |
||
| 103 | get_lang('Code'), |
||
| 104 | get_lang('OnlyLettersAndNumbers'), |
||
| 105 | ), |
||
| 106 | '', |
||
| 107 | array( |
||
| 108 | 'maxlength' => \CourseManager::MAX_COURSE_LENGTH_CODE, |
||
| 109 | 'pattern' => '[a-zA-Z0-9]+', |
||
| 110 | 'title' => get_lang('OnlyLettersAndNumbers'), |
||
| 111 | ) |
||
| 112 | ); |
||
| 113 | $form->applyFilter('wanted_code', 'html_filter'); |
||
| 114 | $form->addRule( |
||
| 115 | 'wanted_code', |
||
| 116 | get_lang('Max'), |
||
| 117 | 'maxlength', |
||
| 118 | \CourseManager::MAX_COURSE_LENGTH_CODE |
||
| 119 | ); |
||
| 120 | |||
| 121 | // The teacher |
||
| 122 | View Code Duplication | if ($courseValidation) { |
|
| 123 | |||
| 124 | // Description of the requested course. |
||
| 125 | $form->addElement( |
||
| 126 | 'textarea', |
||
| 127 | 'description', |
||
| 128 | get_lang('Description'), |
||
| 129 | array('rows' => '3') |
||
| 130 | ); |
||
| 131 | |||
| 132 | // Objectives of the requested course. |
||
| 133 | $form->addElement( |
||
| 134 | 'textarea', |
||
| 135 | 'objetives', |
||
| 136 | get_lang('Objectives'), |
||
| 137 | array('rows' => '3') |
||
| 138 | ); |
||
| 139 | |||
| 140 | // Target audience of the requested course. |
||
| 141 | $form->addElement( |
||
| 142 | 'textarea', |
||
| 143 | 'target_audience', |
||
| 144 | get_lang('TargetAudience'), |
||
| 145 | array('rows' => '3') |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | // Course language. |
||
| 150 | $form->addElement( |
||
| 151 | 'select_language', |
||
| 152 | 'course_language', |
||
| 153 | get_lang('Ln'), |
||
| 154 | array(), |
||
| 155 | array('style' => 'width:150px') |
||
| 156 | ); |
||
| 157 | $form->applyFilter('select_language', 'html_filter'); |
||
| 158 | |||
| 159 | // Exemplary content checkbox. |
||
| 160 | $form->addElement( |
||
| 161 | 'checkbox', |
||
| 162 | 'exemplary_content', |
||
| 163 | null, |
||
| 164 | get_lang('FillWithExemplaryContent') |
||
| 165 | ); |
||
| 166 | |||
| 167 | View Code Duplication | if ($courseValidation) { |
|
| 168 | |||
| 169 | // A special URL to terms and conditions that is set |
||
| 170 | // in the platform settings page. |
||
| 171 | $terms_and_conditions_url = trim( |
||
| 172 | api_get_setting('course_validation_terms_and_conditions_url') |
||
| 173 | ); |
||
| 174 | |||
| 175 | // If the special setting is empty, |
||
| 176 | // then we may get the URL from Chamilo's module "Terms and conditions", |
||
| 177 | // if it is activated. |
||
| 178 | if (empty($terms_and_conditions_url)) { |
||
| 179 | if (api_get_setting( |
||
| 180 | 'registration.allow_terms_conditions' |
||
| 181 | ) == 'true' |
||
| 182 | ) { |
||
| 183 | $terms_and_conditions_url = api_get_path(WEB_CODE_PATH); |
||
| 184 | $terms_and_conditions_url .= 'auth/inscription.php?legal'; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | if (!empty($terms_and_conditions_url)) { |
||
| 189 | // Terms and conditions to be accepted before sending a course request. |
||
| 190 | $form->addElement( |
||
| 191 | 'checkbox', |
||
| 192 | 'legal', |
||
| 193 | null, |
||
| 194 | get_lang('IAcceptTermsAndConditions'), |
||
| 195 | 1 |
||
| 196 | ); |
||
| 197 | $form->addRule( |
||
| 198 | 'legal', |
||
| 199 | get_lang('YouHaveToAcceptTermsAndConditions'), |
||
| 200 | 'required' |
||
| 201 | ); |
||
| 202 | // Link to terms and conditions. |
||
| 203 | $link_terms_and_conditions = ' |
||
| 204 | <script> |
||
| 205 | function MM_openBrWindow(theURL, winName, features) { //v2.0 |
||
| 206 | window.open(theURL,winName,features); |
||
| 207 | } |
||
| 208 | </script> |
||
| 209 | '; |
||
| 210 | $link_terms_and_conditions .= \Display::url( |
||
| 211 | get_lang('ReadTermsAndConditions'), |
||
| 212 | '#', |
||
| 213 | ['onclick' => "javascript:MM_openBrWindow('$terms_and_conditions_url', 'Conditions', 'scrollbars=yes, width=800');"] |
||
| 214 | ); |
||
| 215 | $form->addElement('label', null, $link_terms_and_conditions); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | $obj = new \GradeModel(); |
||
| 220 | $obj->fill_grade_model_select_in_form($form); |
||
| 221 | |||
| 222 | View Code Duplication | if (api_get_setting('course.teacher_can_select_course_template') === 'true') { |
|
| 223 | $form->addElement( |
||
| 224 | 'select_ajax', |
||
| 225 | 'course_template', |
||
| 226 | [ |
||
| 227 | get_lang('CourseTemplate'), |
||
| 228 | get_lang('PickACourseAsATemplateForThisNewCourse'), |
||
| 229 | ], |
||
| 230 | null, |
||
| 231 | ['url' => api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_course'] |
||
| 232 | ); |
||
| 233 | } |
||
| 234 | |||
| 235 | $form->addElement('html', '</div>'); |
||
| 236 | |||
| 237 | // Submit button. |
||
| 238 | $form->addButtonCreate( |
||
| 239 | $courseValidation ? get_lang( |
||
| 240 | 'CreateThisCourseRequest' |
||
| 241 | ) : get_lang('CreateCourseArea') |
||
| 242 | ); |
||
| 243 | |||
| 244 | // Set default values. |
||
| 245 | View Code Duplication | if (isset($_user['language']) && $_user['language'] != '') { |
|
|
|
|||
| 246 | $values['course_language'] = $_user['language']; |
||
| 247 | } else { |
||
| 248 | $values['course_language'] = api_get_setting('language.platform_language'); |
||
| 249 | } |
||
| 250 | |||
| 251 | $form->setDefaults($values); |
||
| 252 | $message = null; |
||
| 253 | $content = null; |
||
| 254 | |||
| 255 | // Validate the form. |
||
| 256 | if ($form->validate()) { |
||
| 257 | $course_values = $form->exportValues(); |
||
| 258 | |||
| 259 | $wanted_code = $course_values['wanted_code']; |
||
| 260 | //$category_code = $course_values['category_code']; |
||
| 261 | $category_code = ''; |
||
| 262 | $title = $course_values['title']; |
||
| 263 | $course_language = $course_values['course_language']; |
||
| 264 | $exemplary_content = !empty($course_values['exemplary_content']); |
||
| 265 | |||
| 266 | if ($courseValidation) { |
||
| 267 | $description = $course_values['description']; |
||
| 268 | $objetives = $course_values['objetives']; |
||
| 269 | $target_audience = $course_values['target_audience']; |
||
| 270 | } |
||
| 271 | |||
| 272 | if ($wanted_code == '') { |
||
| 273 | $wanted_code = \CourseManager::generate_course_code( |
||
| 274 | api_substr( |
||
| 275 | $title, |
||
| 276 | 0, |
||
| 277 | \CourseManager::MAX_COURSE_LENGTH_CODE |
||
| 278 | ) |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | |||
| 282 | // Check whether the requested course code has already been occupied. |
||
| 283 | View Code Duplication | if (!$courseValidation) { |
|
| 284 | $course_code_ok = !\CourseManager::course_code_exists( |
||
| 285 | $wanted_code |
||
| 286 | ); |
||
| 287 | } else { |
||
| 288 | $course_code_ok = !\CourseRequestManager::course_code_exists( |
||
| 289 | $wanted_code |
||
| 290 | ); |
||
| 291 | } |
||
| 292 | |||
| 293 | if ($course_code_ok) { |
||
| 294 | if (!$courseValidation) { |
||
| 295 | |||
| 296 | $params = array(); |
||
| 297 | $params['title'] = $title; |
||
| 298 | $params['exemplary_content'] = $exemplary_content; |
||
| 299 | $params['wanted_code'] = $wanted_code; |
||
| 300 | $params['course_category'] = $category_code; |
||
| 301 | $params['course_language'] = $course_language; |
||
| 302 | $params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null; |
||
| 303 | |||
| 304 | $course_info = \CourseManager::create_course($params); |
||
| 305 | |||
| 306 | if (!empty($course_info)) { |
||
| 307 | |||
| 308 | $url = api_get_path(WEB_CODE_PATH); |
||
| 309 | $url .= 'course_info/start.php?cidReq='; |
||
| 310 | $url .= $course_info['code']; |
||
| 311 | $url .= '&first=1'; |
||
| 312 | header('Location: '.$url); |
||
| 313 | exit; |
||
| 314 | } else { |
||
| 315 | $this->addFlash( |
||
| 316 | 'error', |
||
| 317 | $this->trans('CourseCreationFailed') |
||
| 318 | ); |
||
| 319 | // Display the form. |
||
| 320 | $content = $form->returnForm(); |
||
| 321 | } |
||
| 322 | } else { |
||
| 323 | // Create a request for a new course. |
||
| 324 | $request_id = \CourseRequestManager::create_course_request( |
||
| 325 | $wanted_code, |
||
| 326 | $title, |
||
| 327 | $description, |
||
| 328 | $category_code, |
||
| 329 | $course_language, |
||
| 330 | $objetives, |
||
| 331 | $target_audience, |
||
| 332 | api_get_user_id(), |
||
| 333 | $exemplary_content |
||
| 334 | ); |
||
| 335 | |||
| 336 | if ($request_id) { |
||
| 337 | $course_request_info = \CourseRequestManager::get_course_request_info( |
||
| 338 | $request_id |
||
| 339 | ); |
||
| 340 | $message = (is_array( |
||
| 341 | $course_request_info |
||
| 342 | ) ? '<strong>'.$course_request_info['code'].'</strong> : ' : '').get_lang( |
||
| 343 | 'CourseRequestCreated' |
||
| 344 | ); |
||
| 345 | \Display::return_message( |
||
| 346 | $message, |
||
| 347 | 'confirmation', |
||
| 348 | false |
||
| 349 | ); |
||
| 350 | \Display::return_message( |
||
| 351 | 'div', |
||
| 352 | \Display::url( |
||
| 353 | get_lang('Enter'), |
||
| 354 | api_get_path(WEB_PATH).'user_portal.php', |
||
| 355 | ['class' => 'btn btn-default'] |
||
| 356 | ), |
||
| 357 | ['style' => 'float: left; margin:0px; padding: 0px;'] |
||
| 358 | ); |
||
| 359 | } else { |
||
| 360 | \Display::return_message( |
||
| 361 | get_lang('CourseRequestCreationFailed'), |
||
| 362 | 'error', |
||
| 363 | false |
||
| 364 | ); |
||
| 365 | // Display the form. |
||
| 366 | $content = $form->returnForm(); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | } else { |
||
| 370 | \Display::return_message( |
||
| 371 | get_lang('CourseCodeAlreadyExists'), |
||
| 372 | 'error', |
||
| 373 | false |
||
| 374 | ); |
||
| 375 | // Display the form. |
||
| 376 | $content = $form->returnForm(); |
||
| 377 | } |
||
| 378 | } else { |
||
| 379 | if (!$courseValidation) { |
||
| 380 | $this->addFlash('warning', get_lang('Explanation')); |
||
| 381 | } |
||
| 382 | // Display the form. |
||
| 383 | $content = $form->returnForm(); |
||
| 384 | } |
||
| 385 | |||
| 386 | return $this->render( |
||
| 387 | 'ChamiloCoreBundle:Index:userportal.html.twig', |
||
| 388 | array( |
||
| 389 | 'content' => $content, |
||
| 390 | ) |
||
| 391 | ); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @Route("/{type}/{filter}", name="userportal") |
||
| 396 | * @Method({"GET"}) |
||
| 397 | * @Security("has_role('ROLE_USER')") |
||
| 398 | * |
||
| 399 | * @param string $type courses|sessions|mycoursecategories |
||
| 400 | * @param string $filter history|current for the userportal courses page |
||
| 401 | * @param int $coursePage |
||
| 402 | * @param Request $request |
||
| 403 | * |
||
| 404 | * @return Response |
||
| 405 | */ |
||
| 406 | public function indexAction( |
||
| 495 | |||
| 496 | |||
| 497 | |||
| 498 | } |
||
| 499 |
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.