| Conditions | 29 |
| Paths | > 20000 |
| Total Lines | 213 |
| Code Lines | 139 |
| 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 |
||
| 214 | private function build_basic_form() |
||
| 215 | { |
||
| 216 | $this->addText( |
||
| 217 | 'name', |
||
| 218 | get_lang('CategoryName'), |
||
| 219 | true, |
||
| 220 | ['maxlength' => '50'] |
||
| 221 | ); |
||
| 222 | $this->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||
| 223 | |||
| 224 | if (isset($this->category_object) && |
||
| 225 | $this->category_object->get_parent_id() == 0 |
||
| 226 | ) { |
||
| 227 | // we can't change the root category |
||
| 228 | $this->freeze('name'); |
||
| 229 | } |
||
| 230 | |||
| 231 | $global_weight = api_get_setting('gradebook_default_weight'); |
||
| 232 | |||
| 233 | $value = 100; |
||
| 234 | if (isset($global_weight)) { |
||
| 235 | $value = $global_weight; |
||
| 236 | } |
||
| 237 | |||
| 238 | $this->addFloat( |
||
| 239 | 'weight', |
||
| 240 | [ |
||
| 241 | get_lang('TotalWeight'), |
||
| 242 | get_lang('TotalSumOfWeights'), |
||
| 243 | ], |
||
| 244 | true, |
||
| 245 | ['value' => $value, 'maxlength' => '5'] |
||
| 246 | ); |
||
| 247 | |||
| 248 | $skillsDefaults = []; |
||
| 249 | if (api_is_platform_admin() || api_is_drh()) { |
||
| 250 | if (Skill::isToolAvailable()) { |
||
| 251 | $skillSelect = $this->addElement( |
||
| 252 | 'select_ajax', |
||
| 253 | 'skills', |
||
| 254 | [ |
||
| 255 | get_lang('Skills'), |
||
| 256 | get_lang('SkillsAchievedWhenAchievingThisGradebook'), |
||
| 257 | ], |
||
| 258 | null, |
||
| 259 | [ |
||
| 260 | 'id' => 'skills', |
||
| 261 | 'multiple' => 'multiple', |
||
| 262 | 'url' => api_get_path(WEB_AJAX_PATH).'skill.ajax.php?a=search_skills', |
||
| 263 | ] |
||
| 264 | ); |
||
| 265 | |||
| 266 | // The magic should be here |
||
| 267 | $skills = $this->category_object->get_skills(); |
||
| 268 | foreach ($skills as $skill) { |
||
| 269 | $skillsDefaults[] = $skill['id']; |
||
| 270 | $skillSelect->addOption($skill['name'], $skill['id']); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | $defaultCertification = 0; |
||
| 276 | if (isset($this->category_object) && |
||
| 277 | $this->category_object->get_parent_id() == 0 |
||
| 278 | ) { |
||
| 279 | $model = ExerciseLib::getCourseScoreModel(); |
||
| 280 | if (empty($model)) { |
||
| 281 | $this->addText( |
||
| 282 | 'certif_min_score', |
||
| 283 | get_lang('CertificateMinScore'), |
||
| 284 | true, |
||
| 285 | ['maxlength' => '5'] |
||
| 286 | ); |
||
| 287 | } else { |
||
| 288 | $questionWeighting = $value; |
||
| 289 | $defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2); |
||
| 290 | $select = $this->addSelect( |
||
| 291 | 'certif_min_score', |
||
| 292 | get_lang('CertificateMinScore'), |
||
| 293 | [], |
||
| 294 | ['disable_js' => true] |
||
| 295 | ); |
||
| 296 | |||
| 297 | foreach ($model['score_list'] as $item) { |
||
| 298 | $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2); |
||
| 299 | $model = ExerciseLib::getModelStyle($item, $i); |
||
| 300 | $attributes = ['class' => $item['css_class']]; |
||
| 301 | if ($defaultCertification == $i) { |
||
| 302 | $attributes['selected'] = 'selected'; |
||
| 303 | } |
||
| 304 | $select->addOption($model, $i, $attributes); |
||
| 305 | } |
||
| 306 | $select->updateSelectWithSelectedOption($this); |
||
| 307 | } |
||
| 308 | |||
| 309 | $this->addRule( |
||
| 310 | 'certif_min_score', |
||
| 311 | get_lang('OnlyNumbers'), |
||
| 312 | 'numeric' |
||
| 313 | ); |
||
| 314 | $this->addRule( |
||
| 315 | 'certif_min_score', |
||
| 316 | get_lang('NegativeValue'), |
||
| 317 | 'compare', |
||
| 318 | '>=', |
||
| 319 | 'server', |
||
| 320 | false, |
||
| 321 | false, |
||
| 322 | 0 |
||
| 323 | ); |
||
| 324 | } else { |
||
| 325 | $this->addElement('checkbox', 'visible', null, get_lang('Visible')); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->addElement('hidden', 'hid_user_id'); |
||
| 329 | $this->addElement('hidden', 'hid_parent_id'); |
||
| 330 | $this->addElement( |
||
| 331 | 'textarea', |
||
| 332 | 'description', |
||
| 333 | get_lang('Description') |
||
| 334 | ); |
||
| 335 | |||
| 336 | if (isset($this->category_object) && |
||
| 337 | $this->category_object->get_parent_id() == 0 && |
||
| 338 | (api_is_platform_admin() || api_get_setting('teachers_can_change_grade_model_settings') == 'true') |
||
| 339 | ) { |
||
| 340 | // Getting grade models |
||
| 341 | $obj = new GradeModel(); |
||
| 342 | $obj->fill_grade_model_select_in_form( |
||
| 343 | $this, |
||
| 344 | 'grade_model_id', |
||
| 345 | $this->category_object->get_grade_model_id() |
||
| 346 | ); |
||
| 347 | |||
| 348 | // Freeze or not |
||
| 349 | $course_code = api_get_course_id(); |
||
| 350 | $session_id = api_get_session_id(); |
||
| 351 | $test_cats = Category::load( |
||
| 352 | null, |
||
| 353 | null, |
||
| 354 | $course_code, |
||
| 355 | null, |
||
| 356 | null, |
||
| 357 | $session_id, |
||
| 358 | false |
||
| 359 | ); |
||
| 360 | $links = null; |
||
| 361 | if (!empty($test_cats[0])) { |
||
| 362 | $links = $test_cats[0]->get_links(); |
||
| 363 | } |
||
| 364 | |||
| 365 | if (count($test_cats) > 1 || !empty($links)) { |
||
| 366 | if (api_get_setting('gradebook_enable_grade_model') == 'true') { |
||
| 367 | $this->freeze('grade_model_id'); |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | $generateCertificatesParams = []; |
||
| 372 | if ($this->category_object->getGenerateCertificates()) { |
||
| 373 | $generateCertificatesParams['checked'] = 'checked'; |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->addElement( |
||
| 377 | 'checkbox', |
||
| 378 | 'generate_certificates', |
||
| 379 | null, |
||
| 380 | get_lang('GenerateCertificates'), |
||
| 381 | $generateCertificatesParams |
||
| 382 | ); |
||
| 383 | } |
||
| 384 | |||
| 385 | if (!empty($session_id)) { |
||
| 386 | $isRequirementCheckbox = $this->addCheckBox( |
||
| 387 | 'is_requirement', |
||
| 388 | [ |
||
| 389 | null, |
||
| 390 | get_lang('ConsiderThisGradebookAsRequirementForASessionSequence'), |
||
| 391 | ], |
||
| 392 | get_lang('IsRequirement') |
||
| 393 | ); |
||
| 394 | } |
||
| 395 | |||
| 396 | if ($this->category_object->getIsRequirement()) { |
||
| 397 | $isRequirementCheckbox->setChecked(true); |
||
| 398 | } |
||
| 399 | |||
| 400 | $documentId = $this->category_object->getDocumentId(); |
||
| 401 | if (!empty($documentId)) { |
||
| 402 | $documentData = DocumentManager::get_document_data_by_id($documentId, api_get_course_id()); |
||
| 403 | |||
| 404 | if (!empty($documentData)) { |
||
| 405 | $this->addLabel(get_lang('Certificate'), $documentData['title']); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | |||
| 409 | if ($this->form_type == self::TYPE_ADD) { |
||
| 410 | $this->addButtonCreate(get_lang('AddCategory')); |
||
| 411 | } else { |
||
| 412 | $this->addElement('hidden', 'editcat', intval($_GET['editcat'])); |
||
| 413 | $this->addButtonUpdate(get_lang('EditCategory')); |
||
| 414 | } |
||
| 415 | |||
| 416 | $setting = api_get_setting('tool_visible_by_default_at_creation'); |
||
| 417 | $visibility_default = 1; |
||
| 418 | if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') { |
||
| 419 | $visibility_default = 0; |
||
| 420 | } |
||
| 421 | |||
| 422 | $this->setDefaults( |
||
| 423 | [ |
||
| 424 | 'visible' => $visibility_default, |
||
| 425 | 'skills' => $skillsDefaults, |
||
| 426 | 'certif_min_score' => (string) $defaultCertification, |
||
| 427 | ] |
||
| 431 |