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