Completed
Push — master ( d0e06e...1fcdba )
by Julito
09:05
created

DisplayGradebook   F

Complexity

Total Complexity 115

Size/Duplication

Total Lines 694
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 448
dl 0
loc 694
rs 2
c 0
b 0
f 0
wmc 115

5 Methods

Rating   Name   Duplication   Size   Complexity  
B display_reduce_header_gradebook() 0 40 9
F display_header_result() 0 116 23
F header() 0 328 72
B display_header_user() 0 51 6
B display_header_reduce_flatview() 0 101 5

How to fix   Complexity   

Complex Class

Complex classes like DisplayGradebook 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 DisplayGradebook, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
/**
6
 * Class DisplayGradebook.
7
 */
8
class DisplayGradebook
9
{
10
    /**
11
     * Displays the header for the result page containing the navigation tree and links.
12
     *
13
     * @param Evaluation $evalobj
14
     * @param $selectcat
15
     * @param $shownavbar 1=show navigation bar
0 ignored issues
show
Documentation Bug introduced by
The doc comment 1=show at position 0 could not be parsed: Unknown type name '1=show' at position 0 in 1=show.
Loading history...
16
     * @param $forpdf only output for pdf file
17
     */
18
    public static function display_header_result($evalobj, $selectcat, $page)
19
    {
20
        $header = null;
21
        if (api_is_allowed_to_edit(null, true)) {
22
            $header = '<div class="actions">';
23
            if ('statistics' !== $page) {
24
                $header .= '<a href="'.Category::getUrl().'selectcat='.$selectcat.'">'.
25
                    Display::return_icon('back.png', get_lang('Assessment home'), '', ICON_SIZE_MEDIUM)
26
                    .'</a>';
27
                if ((null != $evalobj->get_course_code()) && !$evalobj->has_results()) {
28
                    $header .= '<a href="gradebook_add_result.php?'.api_get_cidreq().'&selectcat='.$selectcat.'&selecteval='.$evalobj->get_id().'">
29
    				'.Display::return_icon('evaluation_rate.png', get_lang('Grade learners'), '', ICON_SIZE_MEDIUM).'</a>';
30
                }
31
32
                if (api_is_platform_admin() || false == $evalobj->is_locked()) {
33
                    $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&selecteval='.$evalobj->get_id().'&import=">'.
34
                        Display::return_icon('import_evaluation.png', get_lang('Import marks'), '', ICON_SIZE_MEDIUM).'</a>';
35
                }
36
37
                if ($evalobj->has_results()) {
38
                    $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&selecteval='.$evalobj->get_id().'&export=">'.
39
                        Display::return_icon('export_evaluation.png', get_lang('PDF Report'), '', ICON_SIZE_MEDIUM).'</a>';
40
41
                    if (api_is_platform_admin() || false == $evalobj->is_locked()) {
42
                        $header .= '<a href="gradebook_edit_result.php?'.api_get_cidreq().'&selecteval='.$evalobj->get_id().'">'.
43
                            Display::return_icon('edit.png', get_lang('Grade learners'), '', ICON_SIZE_MEDIUM).'</a>';
44
                        $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&selecteval='.$evalobj->get_id().'&deleteall=" onclick="return confirmationall();">'.
45
                            Display::return_icon('delete.png', get_lang('Delete marks'), '', ICON_SIZE_MEDIUM).'</a>';
46
                    }
47
                }
48
                $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&print=&selecteval='.$evalobj->get_id().'" target="_blank">'.
49
                    Display::return_icon('printer.png', get_lang('Print'), '', ICON_SIZE_MEDIUM).'</a>';
50
            } else {
51
                $header .= '<a href="gradebook_view_result.php?'.api_get_cidreq().'&selecteval='.Security::remove_XSS($_GET['selecteval']).'"> '.
52
                    Display::return_icon('back.png', get_lang('Assessment home'), '', ICON_SIZE_MEDIUM).'</a>';
53
            }
54
            $header .= '</div>';
55
        }
56
57
        $scoredisplay = ScoreDisplay::instance();
58
        $student_score = '';
59
        $average = '';
60
        if ($evalobj->has_results()) {
61
            // TODO this check needed ?
62
            $score = $evalobj->calc_score();
63
            if (null != $score) {
64
                $model = ExerciseLib::getCourseScoreModel();
65
                if (empty($model)) {
66
                    $average = get_lang('Average').' :<b> '.$scoredisplay->display_score($score, SCORE_AVERAGE).'</b>';
67
                    $student_score = $evalobj->calc_score(api_get_user_id());
68
                    $student_score = Display::tag(
69
                        'h3',
70
                        get_lang('Score').': '.$scoredisplay->display_score($student_score, SCORE_DIV_PERCENT)
71
                    );
72
73
                    $allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts');
74
                    if ($allowMultipleAttempts) {
75
                        $results = Result::load(null, api_get_user_id(), $evalobj->get_id());
76
                        if (!empty($results)) {
77
                            /** @var Result $resultData */
78
                            foreach ($results as $resultData) {
79
                                $student_score .= ResultTable::getResultAttemptTable($resultData);
80
                            }
81
                        }
82
                    }
83
                }
84
            }
85
        }
86
87
        $description = '';
88
        if ('' == !$evalobj->get_description()) {
89
            $description = get_lang('Description').' :<b> '.$evalobj->get_description().'</b><br>';
90
        }
91
92
        if (null == $evalobj->get_course_code()) {
93
            $course = get_lang('Independent from course');
94
        } else {
95
            $course = CourseManager::getCourseNameFromCode($evalobj->get_course_code());
96
        }
97
98
        $evalinfo = '<table width="100%" border="0"><tr><td>';
99
        $evalinfo .= '<h2>'.$evalobj->get_name().'</h2><hr>';
100
        $evalinfo .= $description;
101
        $evalinfo .= get_lang('Course').' :<b> '.$course.'</b><br />';
102
        if (empty($model)) {
103
            $evalinfo .= get_lang('Maximum score').' :<b> '.$evalobj->get_max().'</b><br>'.$average;
104
        }
105
106
        if (!api_is_allowed_to_edit()) {
107
            $evalinfo .= $student_score;
108
        }
109
110
        if (!$evalobj->has_results()) {
111
            $evalinfo .= '<br /><i>'.get_lang('No results in evaluation for now').'</i>';
112
        }
113
114
        if ('statistics' != $page) {
115
            if (api_is_allowed_to_edit(null, true)) {
116
                $evalinfo .= '<br /><a href="gradebook_statistics.php?'.api_get_cidreq().'&selecteval='.Security::remove_XSS($_GET['selecteval']).'"> '.
117
                    Display::return_icon(
118
                        'statistics.png',
119
                        get_lang('Graphical view'),
120
                        '',
121
                        ICON_SIZE_MEDIUM
122
                    ).'</a>';
123
            }
124
        }
125
        $evalinfo .= '</td><td>'.
126
            Display::return_icon(
127
                'tutorial.gif',
128
                '',
129
                ['style' => 'float:right; position:relative;']
130
            )
131
            .'</td></table>';
132
        echo $evalinfo;
133
        echo $header;
134
    }
135
136
    /**
137
     * Displays the header for the flatview page containing filters.
138
     *
139
     * @param $catobj
140
     * @param $showeval
141
     * @param $showlink
142
     */
143
    public static function display_header_reduce_flatview($catobj, $showeval, $showlink, $simple_search_form)
144
    {
145
        $header = '<div class="actions">';
146
        if (0 == $catobj->get_parent_id()) {
147
            $select_cat = $catobj->get_id();
148
            $url = Category::getUrl();
149
        } else {
150
            $select_cat = $catobj->get_parent_id();
151
            $url = 'gradebook_flatview.php';
152
        }
153
        $header .= '<a href="'.$url.'?'.api_get_cidreq().'&selectcat='.$select_cat.'">'.
154
            Display::return_icon('back.png', get_lang('Assessment home'), '', ICON_SIZE_MEDIUM).'</a>';
155
156
        $pageNum = isset($_GET['flatviewlist_page_nr']) ? intval($_GET['flatviewlist_page_nr']) : null;
157
        $perPage = isset($_GET['flatviewlist_per_page']) ? intval($_GET['flatviewlist_per_page']) : null;
158
        $offset = isset($_GET['offset']) ? $_GET['offset'] : '0';
159
160
        $exportCsvUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
161
            'export_format' => 'csv',
162
            'export_report' => 'export_report',
163
            'selectcat' => $catobj->get_id(),
164
        ]);
165
166
        $header .= Display::url(
167
            Display::return_icon(
168
                'export_csv.png',
169
                get_lang('CSV export'),
170
                '',
171
                ICON_SIZE_MEDIUM
172
            ),
173
            $exportCsvUrl
174
        );
175
176
        $exportXlsUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
177
            'export_format' => 'xls',
178
            'export_report' => 'export_report',
179
            'selectcat' => $catobj->get_id(),
180
        ]);
181
182
        $header .= Display::url(
183
            Display::return_icon(
184
                'export_excel.png',
185
                get_lang('Excel export'),
186
                '',
187
                ICON_SIZE_MEDIUM
188
            ),
189
            $exportXlsUrl
190
        );
191
192
        $exportDocUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
193
            'export_format' => 'doc',
194
            'export_report' => 'export_report',
195
            'selectcat' => $catobj->get_id(),
196
        ]);
197
198
        $header .= Display::url(
199
            Display::return_icon(
200
                'export_doc.png',
201
                get_lang('Export as .doc'),
202
                '',
203
                ICON_SIZE_MEDIUM
204
            ),
205
            $exportDocUrl
206
        );
207
208
        $exportPrintUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
209
                'print' => '',
210
                'selectcat' => $catobj->get_id(),
211
            ]);
212
213
        $header .= Display::url(
214
            Display::return_icon(
215
                'printer.png',
216
                get_lang('Print'),
217
                '',
218
                ICON_SIZE_MEDIUM
219
            ),
220
            $exportPrintUrl,
221
            ['target' => '_blank']
222
        );
223
224
        $exportPdfUrl = api_get_self().'?'.api_get_cidreq().'&'.http_build_query([
225
            'exportpdf' => '',
226
            'selectcat' => $catobj->get_id(),
227
            'offset' => $offset,
228
            'flatviewlist_page_nr' => $pageNum,
229
            'flatviewlist_per_page' => $perPage,
230
        ]);
231
232
        $header .= Display::url(
233
            Display::return_icon(
234
                'pdf.png',
235
                get_lang('Export to PDF'),
236
                '',
237
                ICON_SIZE_MEDIUM
238
            ),
239
            $exportPdfUrl
240
        );
241
242
        $header .= '</div>';
243
        echo $header;
244
    }
245
246
    /**
247
     * Displays the header for the gradebook containing the navigation tree and links.
248
     *
249
     * @param Category $catobj
250
     * @param int      $showtree '1' will show the browse tree and naviation buttons
251
     * @param $selectcat
252
     * @param bool  $is_course_admin
253
     * @param bool  $is_platform_admin
254
     * @param bool  $simple_search_form
255
     * @param bool  $show_add_qualification Whether to show or not the link to add a new qualification
256
     *                                      (we hide it in case of the course-embedded tool where we have only one
257
     *                                      per course or session)
258
     * @param bool  $show_add_link          Whether to show or not the link to add a new item inside
259
     *                                      the qualification (we hide it in case of the course-embedded tool
260
     *                                      where we have only one qualification per course or session)
261
     * @param array $certificateLinkInfo
262
     */
263
    public static function header(
264
        $catobj,
265
        $showtree,
266
        $selectcat,
267
        $is_course_admin,
268
        $is_platform_admin,
269
        $simple_search_form,
270
        $show_add_qualification = true,
271
        $show_add_link = true,
272
        $certificateLinkInfo = []
273
    ) {
274
        $userId = api_get_user_id();
275
        $courseId = api_get_course_int_id();
276
        $sessionId = api_get_session_id();
277
        if (!$is_course_admin) {
278
            $model = ExerciseLib::getCourseScoreModel();
279
            if (!empty($model)) {
280
                return '';
281
            }
282
        }
283
284
        // Student.
285
        $status = CourseManager::getUserInCourseStatus($userId, $courseId);
286
        $sessionStatus = 0;
287
288
        if (!empty($sessionId)) {
289
            $sessionStatus = SessionManager::get_user_status_in_course_session(
290
                $userId,
291
                $courseId,
292
                $sessionId
293
            );
294
        }
295
296
        $objcat = new Category();
297
        $course_id = CourseManager::get_course_by_category($selectcat);
298
        $message_resource = $objcat->show_message_resource_delete($course_id);
299
        $grade_model_id = $catobj->get_grade_model_id();
300
        $header = null;
301
        if (isset($catobj) && !empty($catobj)) {
302
            $categories = Category::load(
303
                null,
304
                null,
305
                null,
306
                $catobj->get_id(),
307
                null,
308
                $sessionId
309
            );
310
        }
311
312
        if (!$is_course_admin && (1 != $status || 0 == $sessionStatus) && 0 != $selectcat) {
313
            $catcourse = Category::load($catobj->get_id());
314
            /** @var Category $category */
315
            $category = $catcourse[0];
316
            $main_weight = $category->get_weight();
317
            $scoredisplay = ScoreDisplay::instance();
318
            $allevals = $category->get_evaluations($userId, true);
319
            $alllinks = $category->get_links($userId, true);
320
            $allEvalsLinks = array_merge($allevals, $alllinks);
321
            $item_value_total = 0;
322
            $scoreinfo = null;
323
324
            for ($count = 0; $count < count($allEvalsLinks); $count++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
325
                $item = $allEvalsLinks[$count];
326
                $score = $item->calc_score($userId);
327
                if (!empty($score)) {
328
                    $divide = 0 == $score[1] ? 1 : $score[1];
329
                    $item_value = $score[0] / $divide * $item->get_weight();
330
                    $item_value_total += $item_value;
331
                }
332
            }
333
334
            $item_total = $main_weight;
335
            $total_score = [$item_value_total, $item_total];
336
            $scorecourse_display = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
337
338
            if ('0' == !$catobj->get_id() && !isset($_GET['studentoverview']) && !isset($_GET['search'])) {
339
                $additionalButtons = null;
340
                if (!empty($certificateLinkInfo)) {
341
                    $additionalButtons .= '<div class="btn-group pull-right">';
342
                    $additionalButtons .= isset($certificateLinkInfo['certificate_link']) ? $certificateLinkInfo['certificate_link'] : '';
343
                    $additionalButtons .= isset($certificateLinkInfo['badge_link']) ? $certificateLinkInfo['badge_link'] : '';
344
                    $additionalButtons .= '</div>';
345
                }
346
                $scoreinfo .= '<strong>'.sprintf(get_lang('Total: %s'), $scorecourse_display.$additionalButtons).'</strong>';
347
            }
348
            echo Display::return_message($scoreinfo, 'normal', false);
349
        }
350
351
        // show navigation tree and buttons?
352
        if ('1' == $showtree || isset($_GET['studentoverview'])) {
353
            $header = '<div class="actions"><table>';
354
            $header .= '<tr>';
355
            if ('0' == !$selectcat) {
356
                $header .= '<td><a href="'.api_get_self().'?selectcat='.$catobj->get_parent_id().'">'.
357
                    Display::return_icon(
358
                        'back.png',
359
                        get_lang('Back to').' '.get_lang('Main folder'),
360
                        '',
361
                        ICON_SIZE_MEDIUM
362
                    ).
363
                    '</a></td>';
364
            }
365
            $header .= '<td>'.get_lang('Current course').'</td>'.
366
                    '<td><form name="selector"><select name="selectcat" onchange="document.selector.submit()">';
367
            $cats = Category::load();
368
369
            $tree = $cats[0]->get_tree();
370
            unset($cats);
371
            $line = null;
372
            foreach ($tree as $cat) {
373
                for ($i = 0; $i < $cat[2]; $i++) {
374
                    $line .= '&mdash;';
375
                }
376
                $line = isset($line) ? $line : '';
377
                if (isset($_GET['selectcat']) && $_GET['selectcat'] == $cat[0]) {
378
                    $header .= '<option selected value='.$cat[0].'>'.$line.' '.$cat[1].'</option>';
379
                } else {
380
                    $header .= '<option value='.$cat[0].'>'.$line.' '.$cat[1].'</option>';
381
                }
382
                $line = '';
383
            }
384
            $header .= '</select></form></td>';
385
            if (!empty($simple_search_form) && false === $message_resource) {
386
                $header .= '<td style="vertical-align: top;">'.$simple_search_form->toHtml().'</td>';
387
            } else {
388
                $header .= '<td></td>';
389
            }
390
            if (!($is_course_admin &&
391
                false === $message_resource &&
392
                isset($_GET['selectcat']) && 0 != $_GET['selectcat']) &&
393
                isset($_GET['studentoverview'])
394
            ) {
395
                $header .= '<td style="vertical-align: top;">
396
                                <a href="'.api_get_self().'?'.api_get_cidreq().'&studentoverview=&exportpdf=&selectcat='.$catobj->get_id().'" target="_blank">
397
							 '.Display::return_icon('pdf.png', get_lang('Export to PDF'), [], ICON_SIZE_MEDIUM).'
398
							'.get_lang('Export to PDF').'</a>';
399
            }
400
            $header .= '</td></tr>';
401
            $header .= '</table></div>';
402
        }
403
404
        // for course admin & platform admin add item buttons are added to the header
405
        $actionsLeft = '';
406
        $actionsRight = '';
407
        $my_api_cidreq = api_get_cidreq();
408
        $isCoach = api_is_coach(api_get_session_id(), api_get_course_int_id());
409
        $accessToRead = api_is_allowed_to_edit(null, true) || $isCoach;
410
        $accessToEdit = api_is_allowed_to_edit(null, true);
411
        $courseCode = api_get_course_id();
412
413
        if ($accessToRead) {
414
            $my_category = $catobj->showAllCategoryInfo($catobj->get_id());
415
            if ('0' != $selectcat && $accessToEdit) {
416
                if ('' == $my_api_cidreq) {
417
                    $my_api_cidreq = 'cidReq='.$my_category['course_code'];
418
                }
419
                if ($show_add_link && !$message_resource) {
420
                    $actionsLeft .= '<a href="gradebook_add_eval.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'" >'.
421
                        Display::return_icon('new_evaluation.png', get_lang('Add classroom activity'), '',
422
                            ICON_SIZE_MEDIUM).'</a>';
423
                    $cats = Category::load($selectcat);
424
425
                    if (null != $cats[0]->get_course_code() && !$message_resource) {
426
                        $actionsLeft .= '<a href="gradebook_add_link.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
427
                            Display::return_icon('new_online_evaluation.png', get_lang('Add online activity'), '',
428
                                ICON_SIZE_MEDIUM).'</a>';
429
                    } else {
430
                        $actionsLeft .= '<a href="gradebook_add_link_select_course.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
431
                            Display::return_icon('new_online_evaluation.png', get_lang('Add online activity'), '',
432
                                ICON_SIZE_MEDIUM).'</a>';
433
                    }
434
                }
435
            }
436
            if ((empty($grade_model_id) || -1 == $grade_model_id) && $accessToEdit) {
437
                $actionsLeft .= '<a href="gradebook_add_cat.php?'.api_get_cidreq().'&selectcat='.$catobj->get_id().'">'.
438
                    Display::return_icon(
439
                        'new_folder.png',
440
                        get_lang('Add assessment'),
441
                        [],
442
                        ICON_SIZE_MEDIUM
443
                    ).'</a></td>';
444
            }
445
446
            if ('0' != $selectcat && $accessToRead) {
447
                if (!$message_resource) {
448
                    $actionsLeft .= '<a href="gradebook_flatview.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
449
                        Display::return_icon('statistics.png', get_lang('List View'), '', ICON_SIZE_MEDIUM).'</a>';
450
451
                    if (1 == $my_category['generate_certificates']) {
452
                        $actionsLeft .= Display::url(
453
                            Display::return_icon(
454
                                'certificate_list.png',
455
                                get_lang('See list of learner certificates'),
456
                                '',
457
                                ICON_SIZE_MEDIUM
458
                            ),
459
                            "gradebook_display_certificate.php?$my_api_cidreq&cat_id=".$selectcat
460
                        );
461
                    }
462
463
                    $actionsLeft .= Display::url(
464
                        Display::return_icon(
465
                            'user.png',
466
                            get_lang('Students list report'),
467
                            '',
468
                            ICON_SIZE_MEDIUM
469
                        ),
470
                        "gradebook_display_summary.php?$my_api_cidreq&selectcat=".$selectcat
471
                    );
472
473
                    $allow = api_get_configuration_value('gradebook_custom_student_report');
474
                    if ($allow) {
475
                        $actionsLeft .= Display::url(
476
                            get_lang('Generate custom report'),
477
                            api_get_path(WEB_AJAX_PATH)."gradebook.ajax.php?$my_api_cidreq&a=generate_custom_report",
478
                            ['class' => 'btn btn-default ajax']
479
                        );
480
                    }
481
482
                    // Right icons
483
                    if ($accessToEdit) {
484
                        $actionsRight = '<a href="gradebook_edit_cat.php?editcat='.$catobj->get_id(
485
                            ).'&cidReq='.$catobj->get_course_code().'&id_session='.$catobj->get_session_id().'">'.
486
                            Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM).'</a>';
487
488
                        if ('true' == api_get_plugin_setting('customcertificate', 'enable_plugin_customcertificate') &&
489
                            1 == api_get_course_setting('customcertificate_course_enable')
490
                        ) {
491
                            $actionsRight .= '<a href="'.api_get_path(
492
                                    WEB_PLUGIN_PATH
493
                                ).'customcertificate/src/index.php?'.
494
                                $my_api_cidreq.'&origin=gradebook&selectcat='.$catobj->get_id().'">'.
495
                                Display::return_icon(
496
                                    'certificate.png',
497
                                    get_lang('Attach certificate'),
498
                                    '',
499
                                    ICON_SIZE_MEDIUM
500
                                ).'</a>';
501
                        } else {
502
                            $actionsRight .= '<a href="'.api_get_path(WEB_CODE_PATH).
503
                                'document/document.php?curdirpath=/certificates&'.
504
                                $my_api_cidreq.'&origin=gradebook&selectcat='.$catobj->get_id().'">'.
505
                                Display::return_icon(
506
                                    'certificate.png',
507
                                    get_lang('Attach certificate'),
508
                                    '',
509
                                    ICON_SIZE_MEDIUM
510
                                ).'</a>';
511
                        }
512
513
                        if (empty($categories)) {
514
                            $actionsRight .= '<a href="gradebook_edit_all.php?id_session='.api_get_session_id(
515
                                ).'&'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
516
                                Display::return_icon(
517
                                    'percentage.png',
518
                                    get_lang('Weight in Report'),
519
                                    '',
520
                                    ICON_SIZE_MEDIUM
521
                                ).'</a>';
522
                        }
523
                        $score_display_custom = api_get_setting('gradebook_score_display_custom');
524
                        if ('true' == api_get_setting('teachers_can_change_score_settings') &&
525
                            'true' == $score_display_custom
526
                        ) {
527
                            $actionsRight .= '<a href="gradebook_scoring_system.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
528
                                Display::return_icon('ranking.png', get_lang('Skills ranking'), '', ICON_SIZE_MEDIUM).'</a>';
529
                        }
530
                    }
531
                }
532
            }
533
        } elseif (isset($_GET['search'])) {
534
            echo $header = '<b>'.get_lang('Search results').' :</b>';
535
        }
536
537
        $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh(
538
            api_get_user_id(),
539
            api_get_course_info()
540
        );
541
542
        if ($isDrhOfCourse) {
543
            $actionsLeft .= '<a href="gradebook_flatview.php?'.$my_api_cidreq.'&selectcat='.$catobj->get_id().'">'.
544
                Display::return_icon(
545
                    'statistics.png',
546
                    get_lang('List View'),
547
                    '',
548
                    ICON_SIZE_MEDIUM
549
                ).
550
                '</a>';
551
        }
552
553
        if ($isCoach || api_is_allowed_to_edit(null, true)) {
554
            echo $toolbar = Display::toolbarAction(
555
                'gradebook-actions',
556
                [$actionsLeft, $actionsRight]
557
            );
558
        }
559
560
        if ($accessToEdit || api_is_allowed_to_edit(null, true)) {
561
            $weight = intval($catobj->get_weight()) > 0 ? $catobj->get_weight() : 0;
562
            $weight = '<strong>'.get_lang('Total weight').' : </strong>'.$weight;
563
            $min_certification = intval($catobj->getCertificateMinScore() > 0) ? $catobj->getCertificateMinScore() : 0;
564
565
            if (!empty($min_certification)) {
566
                $model = ExerciseLib::getCourseScoreModel();
567
                if (!empty($model)) {
568
                    $defaultCertification = api_number_format($min_certification, 2);
569
                    $questionWeighting = $catobj->get_weight();
570
                    foreach ($model['score_list'] as $item) {
571
                        $i = api_number_format($item['score_to_qualify'] / 100 * $questionWeighting, 2);
572
                        $model = ExerciseLib::getModelStyle($item, $i);
573
                        if ($defaultCertification == $i) {
574
                            $min_certification = $model;
575
                            break;
576
                        }
577
                    }
578
                }
579
            }
580
581
            $min_certification = get_lang('Minimum certification score').' : '.$min_certification;
582
            $edit_icon = '<a href="gradebook_edit_cat.php?editcat='.$catobj->get_id().'&cidReq='.$catobj->get_course_code().'&id_session='.$catobj->get_session_id().'">'.
583
                Display::return_icon('edit.png', get_lang('Edit'), [], ICON_SIZE_SMALL).'</a>';
584
585
            $msg = $weight.' - '.$min_certification.$edit_icon;
586
            //@todo show description
587
            $description = (('' == $catobj->get_description() || is_null($catobj->get_description())) ? '' : '<strong>'.get_lang('Assessment description').'</strong>'.': '.$catobj->get_description());
588
            echo Display::return_message($msg, 'normal', false);
589
            if (!empty($description)) {
590
                echo Display::div($description, []);
591
            }
592
        }
593
    }
594
595
    /**
596
     * @param Category $catobj
597
     * @param $is_course_admin
598
     * @param $is_platform_admin
599
     * @param $simple_search_form
600
     * @param bool $show_add_qualification
601
     * @param bool $show_add_link
602
     */
603
    public function display_reduce_header_gradebook(
604
        $catobj,
605
        $is_course_admin,
606
        $is_platform_admin,
607
        $simple_search_form,
608
        $show_add_qualification = true,
609
        $show_add_link = true
610
    ) {
611
        //student
612
        if (!$is_course_admin) {
613
            $user = api_get_user_info(api_get_user_id());
614
            $catcourse = Category::load($catobj->get_id());
615
            $scoredisplay = ScoreDisplay::instance();
616
            $scorecourse = $catcourse[0]->calc_score(api_get_user_id());
617
            $scorecourse_display = isset($scorecourse) ? $scoredisplay->display_score($scorecourse, SCORE_AVERAGE) : get_lang('No results available');
618
            $cattotal = Category::load(0);
619
            $scoretotal = $cattotal[0]->calc_score(api_get_user_id());
620
            $scoretotal_display = isset($scoretotal) ? $scoredisplay->display_score($scoretotal, SCORE_PERCENT) : get_lang('No results available');
621
            $scoreinfo = get_lang('Statistics of').' :<b> '.$user['complete_name'].'</b><br />';
622
            if (('0' == !$catobj->get_id()) && (!isset($_GET['studentoverview'])) && (!isset($_GET['search']))) {
623
                $scoreinfo .= '<br />'.get_lang('Total for this category.').' : <b>'.$scorecourse_display.'</b>';
624
            }
625
            $scoreinfo .= '<br />'.get_lang('Total').' : <b>'.$scoretotal_display.'</b>';
626
            Display::addFlash(
627
                Display::return_message($scoreinfo, 'normal', false)
628
            );
629
        }
630
        // show navigation tree and buttons?
631
        $header = '<div class="actions">';
632
633
        if ($is_course_admin) {
634
            $header .= '<a href="gradebook_flatview.php?'.api_get_cidreq().'&selectcat='.$catobj->get_id().'">'.Display::return_icon('statistics.png', get_lang('List View'), '', ICON_SIZE_MEDIUM).'</a>';
635
            $header .= '<a href="gradebook_scoring_system.php?'.api_get_cidreq().'&selectcat='.$catobj->get_id().'">'.Display::return_icon('settings.png', get_lang('Skills ranking'), '', ICON_SIZE_MEDIUM).'</a>';
636
        } elseif (!(isset($_GET['studentoverview']))) {
637
            $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&studentoverview=&selectcat='.$catobj->get_id().'">'.Display::return_icon('view_list.gif', get_lang('List View')).' '.get_lang('List View').'</a>';
638
        } else {
639
            $header .= '<a href="'.api_get_self().'?'.api_get_cidreq().'&studentoverview=&exportpdf=&selectcat='.$catobj->get_id().'" target="_blank">'.Display::return_icon('pdf.png', get_lang('Export to PDF'), '', ICON_SIZE_MEDIUM).'</a>';
640
        }
641
        $header .= '</div>';
642
        echo $header;
643
    }
644
645
    /**
646
     * @param int $userId
647
     * @param int $categoryId
648
     *
649
     * @return string
650
     */
651
    public static function display_header_user($userId, $categoryId)
652
    {
653
        $user = api_get_user_info($userId);
654
        if (empty($user)) {
655
            return '';
656
        }
657
658
        $catcourse = Category::load($categoryId);
659
        $scoredisplay = ScoreDisplay::instance();
660
661
        // generating the total score for a course
662
        $allevals = $catcourse[0]->get_evaluations(
663
            $userId,
664
            true,
665
            api_get_course_id()
666
        );
667
        $alllinks = $catcourse[0]->get_links(
668
            $userId,
669
            true,
670
            api_get_course_id()
671
        );
672
        $evals_links = array_merge($allevals, $alllinks);
673
        $item_value = 0;
674
        $item_total = 0;
675
        for ($count = 0; $count < count($evals_links); $count++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
676
            $item = $evals_links[$count];
677
            $score = $item->calc_score($userId);
678
            if ($score) {
679
                $my_score_denom = ($score[1] == 0) ? 1 : $score[1];
680
                $item_value += $score[0] / $my_score_denom * $item->get_weight();
681
            }
682
            $item_total += $item->get_weight();
683
        }
684
        $item_value = api_number_format($item_value, 2);
685
        $total_score = [$item_value, $item_total];
686
        $scorecourse_display = $scoredisplay->display_score($total_score, SCORE_DIV_PERCENT);
687
688
        $info = '<div class="row"><div class="col-md-3">';
689
        $info .= '<div class="thumbnail"><img src="'.$user['avatar'].'" /></div>';
690
        $info .= '</div>';
691
        $info .= '<div class="col-md-6">';
692
        $info .= get_lang('Name').' :  '.$user['complete_name_with_message_link'].'<br />';
693
694
        if ('true' == api_get_setting('show_email_addresses')) {
695
            $info .= get_lang('e-mail').' : <a href="mailto:'.$user['email'].'">'.$user['email'].'</a><br />';
696
        }
697
698
        $info .= get_lang('Total for user').' : <b>'.$scorecourse_display.'</b>';
699
        $info .= '</div>';
700
        $info .= '</div>';
701
        echo $info;
702
    }
703
}
704