Conditions | 31 |
Paths | > 20000 |
Total Lines | 219 |
Code Lines | 140 |
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 |
||
223 | private function build_basic_form() |
||
224 | { |
||
225 | $this->addText( |
||
226 | 'name', |
||
227 | get_lang('Category name'), |
||
228 | true, |
||
229 | ['maxlength' => '50'] |
||
230 | ); |
||
231 | $this->addRule('name', get_lang('Required field'), 'required'); |
||
232 | |||
233 | if (!empty($this->category_object) && 0 == $this->category_object->get_parent_id()) { |
||
234 | // we can't change the root category |
||
235 | $this->freeze('name'); |
||
236 | } |
||
237 | |||
238 | $global_weight = api_get_setting('gradebook_default_weight'); |
||
239 | |||
240 | $value = 100; |
||
241 | if (isset($global_weight)) { |
||
242 | $value = $global_weight; |
||
243 | } |
||
244 | |||
245 | $this->addFloat( |
||
246 | 'weight', |
||
247 | [ |
||
248 | get_lang('Total weight'), |
||
249 | get_lang('The sum of all weights of activities inside this assessment has to be equivalent to this number.'), |
||
250 | ], |
||
251 | true, |
||
252 | ['value' => $value, 'maxlength' => '5'] |
||
253 | ); |
||
254 | |||
255 | $skillsDefaults = []; |
||
256 | |||
257 | $allowSkillEdit = api_is_platform_admin() || api_is_drh(); |
||
258 | if ('true' === api_get_setting('skill.skills_teachers_can_assign_skills')) { |
||
259 | $allowSkillEdit = $allowSkillEdit || api_is_allowed_to_edit(); |
||
260 | } |
||
261 | |||
262 | if ($allowSkillEdit) { |
||
263 | if (SkillModel::isToolAvailable()) { |
||
264 | $skillSelect = $this->addSelectAjax( |
||
265 | 'skills', |
||
266 | [ |
||
267 | get_lang('Skills'), |
||
268 | get_lang('Skills obtained when achieving this assessment'), |
||
269 | ], |
||
270 | [], |
||
271 | [ |
||
272 | 'id' => 'skills', |
||
273 | 'multiple' => 'multiple', |
||
274 | 'url' => api_get_path(WEB_AJAX_PATH).'skill.ajax.php?a=search_skills', |
||
275 | ] |
||
276 | ); |
||
277 | |||
278 | // The magic should be here |
||
279 | $skills = $this->category_object->get_skills(); |
||
280 | foreach ($skills as $skill) { |
||
281 | $skillsDefaults[] = $skill['id']; |
||
282 | $skillSelect->addOption($skill['name'], $skill['id']); |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | |||
287 | $defaultCertification = 0; |
||
288 | if (!empty($this->category_object)) { |
||
289 | $defaultCertification = $this->category_object->getCertificateMinScore(); |
||
290 | } |
||
291 | |||
292 | if (!empty($this->category_object) && |
||
293 | 0 == $this->category_object->get_parent_id() |
||
294 | ) { |
||
295 | $model = ExerciseLib::getCourseScoreModel(); |
||
296 | if (empty($model)) { |
||
297 | $this->addText( |
||
298 | 'certif_min_score', |
||
299 | get_lang('Minimum certification score'), |
||
300 | true, |
||
301 | ['maxlength' => '5'] |
||
302 | ); |
||
303 | } else { |
||
304 | $questionWeighting = $value; |
||
305 | $defaultCertification = api_number_format($this->category_object->getCertificateMinScore(), 2); |
||
306 | $select = $this->addSelect( |
||
307 | 'certif_min_score', |
||
308 | get_lang('Minimum certification score'), |
||
309 | [], |
||
310 | ['disable_js' => true] |
||
311 | ); |
||
312 | |||
313 | foreach ($model['score_list'] as $item) { |
||
314 | $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2); |
||
315 | $model = ExerciseLib::getModelStyle($item, $i); |
||
316 | $attributes = ['class' => $item['css_class']]; |
||
317 | if ($defaultCertification == $i) { |
||
318 | $attributes['selected'] = 'selected'; |
||
319 | } |
||
320 | $select->addOption($model, $i, $attributes); |
||
321 | } |
||
322 | $select->updateSelectWithSelectedOption($this); |
||
323 | } |
||
324 | |||
325 | $this->addRule( |
||
326 | 'certif_min_score', |
||
327 | get_lang('Only numbers'), |
||
328 | 'numeric' |
||
329 | ); |
||
330 | $this->addRule( |
||
331 | 'certif_min_score', |
||
332 | get_lang('Negative value'), |
||
333 | 'compare', |
||
334 | '>=', |
||
335 | 'server', |
||
336 | false, |
||
337 | false, |
||
338 | 0 |
||
339 | ); |
||
340 | } else { |
||
341 | $this->addElement('checkbox', 'visible', null, get_lang('Visible')); |
||
342 | } |
||
343 | |||
344 | $this->addElement('hidden', 'hid_user_id'); |
||
345 | $this->addElement('hidden', 'hid_parent_id'); |
||
346 | $this->addElement( |
||
347 | 'textarea', |
||
348 | 'description', |
||
349 | get_lang('Description') |
||
350 | ); |
||
351 | |||
352 | if (!empty($this->category_object) && |
||
353 | 0 == $this->category_object->get_parent_id() && |
||
354 | (api_is_platform_admin() || 'true' === api_get_setting('teachers_can_change_grade_model_settings')) |
||
355 | ) { |
||
356 | // Getting grade models |
||
357 | $obj = new GradeModel(); |
||
358 | $obj->fill_grade_model_select_in_form( |
||
359 | $this, |
||
360 | 'grade_model_id', |
||
361 | $this->category_object->get_grade_model_id() |
||
362 | ); |
||
363 | |||
364 | // Freeze or not |
||
365 | $session_id = api_get_session_id(); |
||
366 | $test_cats = Category::load( |
||
367 | null, |
||
368 | null, |
||
369 | api_get_course_int_id(), |
||
370 | null, |
||
371 | null, |
||
372 | $session_id, |
||
373 | null |
||
374 | ); |
||
375 | $links = null; |
||
376 | if (!empty($test_cats[0])) { |
||
377 | $links = $test_cats[0]->get_links(); |
||
378 | } |
||
379 | |||
380 | if (count($test_cats) > 1 || !empty($links)) { |
||
381 | if ('true' === api_get_setting('gradebook_enable_grade_model')) { |
||
382 | $this->freeze('grade_model_id'); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | $generateCertificatesParams = []; |
||
387 | if ($this->category_object->getGenerateCertificates()) { |
||
388 | $generateCertificatesParams['checked'] = 'checked'; |
||
389 | } |
||
390 | |||
391 | $this->addElement( |
||
392 | 'checkbox', |
||
393 | 'generate_certificates', |
||
394 | null, |
||
395 | get_lang('Generate certificates'), |
||
396 | $generateCertificatesParams |
||
397 | ); |
||
398 | } |
||
399 | |||
400 | $isRequirementCheckbox = $this->addCheckBox( |
||
401 | 'is_requirement', |
||
402 | [ |
||
403 | null, |
||
404 | get_lang('Consider this gradebook as a requirement to complete the course (influences the sessions sequences)'), |
||
405 | ], |
||
406 | get_lang('Is requirement') |
||
407 | ); |
||
408 | |||
409 | if ($this->category_object->getIsRequirement()) { |
||
410 | $isRequirementCheckbox->setChecked(true); |
||
411 | } |
||
412 | |||
413 | $documentId = $this->category_object->getDocumentId(); |
||
414 | if (!empty($documentId)) { |
||
415 | $repo = \Chamilo\CoreBundle\Framework\Container::getDocumentRepository(); |
||
416 | /** @var CDocument $documentData */ |
||
417 | $documentData = $repo->find($documentId); |
||
418 | |||
419 | if (!empty($documentData)) { |
||
420 | $this->addLabel(get_lang('Certificate'), $documentData->getTitle()); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | if (self::TYPE_ADD == $this->form_type) { |
||
425 | $this->addButtonCreate(get_lang('Add category')); |
||
426 | } else { |
||
427 | $this->addElement('hidden', 'editcat', (int) $_GET['editcat']); |
||
428 | $this->addButtonUpdate(get_lang('Edit this category')); |
||
429 | } |
||
430 | |||
431 | $setting = api_get_setting('tool_visible_by_default_at_creation'); |
||
432 | $visibility_default = 1; |
||
433 | if (isset($setting['gradebook']) && 'false' === $setting['gradebook']) { |
||
434 | $visibility_default = 0; |
||
435 | } |
||
436 | |||
437 | $this->setDefaults( |
||
438 | [ |
||
439 | 'visible' => $visibility_default, |
||
440 | 'skills' => $skillsDefaults, |
||
441 | 'certif_min_score' => (string) $defaultCertification, |
||
442 | ] |
||
446 |