Passed
Push — master ( b60c10...3e4e56 )
by Julito
10:19
created

ResultTable::__construct()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 28
nc 32
nop 5
dl 0
loc 44
rs 8.5386
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * Class ResultTable
6
 * Table to display results for an evaluation.
7
 *
8
 * @author Stijn Konings
9
 * @author Bert Steppé
10
 *
11
 * @package chamilo.gradebook
12
 */
13
class ResultTable extends SortableTable
14
{
15
    private $datagen;
16
    private $evaluation;
17
    private $allresults;
0 ignored issues
show
introduced by
The private property $allresults is not used, and could be removed.
Loading history...
18
    private $iscourse;
19
20
    /**
21
     * ResultTable constructor.
22
     *
23
     * @param string      $evaluation
24
     * @param array       $results
25
     * @param null|string $iscourse
26
     * @param array       $addparams
27
     * @param bool        $forprint
28
     */
29
    public function __construct(
30
        $evaluation,
31
        $results = [],
32
        $iscourse,
33
        $addparams = [],
34
        $forprint = false
35
    ) {
36
        parent:: __construct(
37
            'resultlist',
38
            null,
39
            null,
40
            api_is_western_name_order() ? 1 : 2
41
        );
42
43
        $this->datagen = new ResultsDataGenerator($evaluation, $results, true);
44
45
        $this->evaluation = $evaluation;
46
        $this->iscourse = $iscourse;
47
        $this->forprint = $forprint;
0 ignored issues
show
Bug Best Practice introduced by
The property forprint does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
49
        if (isset($addparams)) {
50
            $this->set_additional_parameters($addparams);
51
        }
52
        $scoredisplay = ScoreDisplay::instance();
53
        $column = 0;
54
        if ($this->iscourse == '1') {
55
            $this->set_header($column++, '', false);
56
            $this->set_form_actions([
57
                    'delete' => get_lang('Delete'),
58
            ]);
59
        }
60
        if (api_is_western_name_order()) {
61
            $this->set_header($column++, get_lang('FirstName'));
62
            $this->set_header($column++, get_lang('LastName'));
63
        } else {
64
            $this->set_header($column++, get_lang('LastName'));
65
            $this->set_header($column++, get_lang('FirstName'));
66
        }
67
        $this->set_header($column++, get_lang('Score'));
68
        if ($scoredisplay->is_custom()) {
69
            $this->set_header($column++, get_lang('Display'));
70
        }
71
        if (!$this->forprint) {
72
            $this->set_header($column++, get_lang('Modify'), false);
73
        }
74
    }
75
76
    /**
77
     * Function used by SortableTable to get total number of items in the table.
78
     */
79
    public function get_total_number_of_items()
80
    {
81
        return $this->datagen->get_total_results_count();
82
    }
83
84
    /**
85
     * Function used by SortableTable to generate the data to display.
86
     */
87
    public function get_table_data(
88
        $from = 1,
89
        $per_page = null,
90
        $column = null,
91
        $direction = null,
92
        $sort = null
93
    ) {
94
        $isWesternNameOrder = api_is_western_name_order();
95
        $scoredisplay = ScoreDisplay::instance();
96
97
        // determine sorting type
98
        $col_adjust = $this->iscourse == '1' ? 1 : 0;
99
100
        switch ($this->column) {
101
            // first name or last name
102
            case 0 + $col_adjust:
103
                if ($isWesternNameOrder) {
104
                    $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
105
                } else {
106
                    $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
107
                }
108
                break;
109
                // first name or last name
110
            case 1 + $col_adjust:
111
                if ($isWesternNameOrder) {
112
                    $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
113
                } else {
114
                    $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
115
                }
116
                break;
117
                // Score
118
            case 2 + $col_adjust:
119
                $sorting = ResultsDataGenerator::RDG_SORT_SCORE;
120
                break;
121
            case 3 + $col_adjust:
122
                $sorting = ResultsDataGenerator::RDG_SORT_MASK;
123
                break;
124
        }
125
126
        if ($this->direction == 'DESC') {
127
            $sorting |= ResultsDataGenerator::RDG_SORT_DESC;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sorting does not seem to be defined for all execution paths leading up to this point.
Loading history...
128
        } else {
129
            $sorting |= ResultsDataGenerator::RDG_SORT_ASC;
130
        }
131
132
        $data_array = $this->datagen->get_data($sorting, $from, $this->per_page);
133
134
        // generate the data to display
135
        $sortable_data = [];
136
        foreach ($data_array as $item) {
137
            $row = [];
138
            if ($this->iscourse == '1') {
139
                $row[] = $item['result_id'];
140
            }
141
            if ($isWesternNameOrder) {
142
                $row[] = $item['firstname'];
143
                $row[] = $item['lastname'];
144
            } else {
145
                $row[] = $item['lastname'];
146
                $row[] = $item['firstname'];
147
            }
148
149
            $row[] = Display::bar_progress(
150
                $item['percentage_score'],
151
                false,
152
                $item['score']
153
            );
154
155
            if ($scoredisplay->is_custom()) {
156
                $row[] = $item['display'];
157
            }
158
            if (!$this->forprint) {
159
                $row[] = $this->build_edit_column($item);
160
            }
161
            $sortable_data[] = $row;
162
        }
163
164
        return $sortable_data;
165
    }
166
167
    /**
168
     * @param Result $result
169
     * @param string $url
170
     *
171
     * @return string
172
     *
173
     */
174
    public static function getResultAttemptTable($result, $url = '')
175
    {
176
        if (empty($result)) {
177
178
            return '';
179
        }
180
181
        $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT);
0 ignored issues
show
Bug introduced by
The constant TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
182
183
        $sql = "SELECT * FROM $table WHERE result_id = ".$result->get_id().' ORDER BY created_at DESC';
184
        $resultQuery = Database::query($sql);
185
        $list = Database::store_result($resultQuery);
186
187
        $htmlTable = new HTML_Table(['class' => 'data_table']);
188
        $htmlTable->setHeaderContents(0, 0, get_lang('Score'));
189
        $htmlTable->setHeaderContents(0, 1, get_lang('Comment'));
190
        $htmlTable->setHeaderContents(0, 2, get_lang('CreatedAt'));
191
192
        if (!empty($url)) {
193
            $htmlTable->setHeaderContents(0, 3, get_lang('Actions'));
194
        }
195
196
        $row = 1;
197
        foreach ($list as $data) {
198
            $htmlTable->setCellContents($row, 0, $data['score']);
199
            $htmlTable->setCellContents($row, 1, $data['comment']);
200
            $htmlTable->setCellContents($row, 2, Display::dateToStringAgoAndLongDate($data['created_at']));
201
            if (!empty($url)) {
202
                $htmlTable->setCellContents(
203
                    $row,
204
                    3,
205
                    Display::url(
206
                        Display::return_icon('delete.png', get_lang('Delete')),
207
                        $url.'&action=delete_attempt&result_attempt_id='.$data['id']
208
                    )
209
                );
210
            }
211
            $row++;
212
        }
213
214
        return $htmlTable->toHtml();
215
    }
216
217
    /**
218
     * @param array $item
219
     *
220
     * @return string
221
     */
222
    private function build_edit_column($item)
223
    {
224
        $locked_status = $this->evaluation->get_locked();
225
        $allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts');
226
        $baseUrl = api_get_self().'?selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq();
227
        if (api_is_allowed_to_edit(null, true) && $locked_status == 0) {
228
            $edit_column = '';
229
            if ($allowMultipleAttempts) {
230
                if (!empty($item['percentage_score'])) {
231
                    $edit_column .=
232
                        Display::url(
233
                            Display::return_icon('add.png', get_lang('AddAttempt'), '', '22'),
234
                            $baseUrl.'&action=add_attempt&editres='.$item['result_id']
235
                        );
236
                } else {
237
                    $edit_column .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
238
                        Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
239
                }
240
            } else {
241
                $edit_column .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
242
                    Display::return_icon('edit.png', get_lang('Modify'), '', '22').'</a>';
243
            }
244
            $edit_column .= ' <a href="'.api_get_self().'?delete_mark='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
245
                Display::return_icon('delete.png', get_lang('Delete'), '', '22').'</a>';
246
        }
247
248
        if ($this->evaluation->get_course_code() == null) {
249
            $edit_column .= '&nbsp;<a href="'.api_get_self().'?resultdelete='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'" onclick="return confirmationuser();">';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $edit_column does not seem to be defined for all execution paths leading up to this point.
Loading history...
250
            $edit_column .= Display::return_icon('delete.png', get_lang('Delete'));
251
            $edit_column .= '</a>';
252
            $edit_column .= '&nbsp;<a href="user_stats.php?userid='.$item['id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">';
253
            $edit_column .= Display::return_icon('statistics.gif', get_lang('Statistics'));
254
            $edit_column .= '</a>';
255
        }
256
257
        // Evaluation's origin is a link
258
        if ($this->evaluation->get_category_id() < 0) {
259
            $link = LinkFactory::get_evaluation_link($this->evaluation->get_id());
260
            $doc_url = $link->get_view_url($item['id']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $doc_url is correct as $link->get_view_url($item['id']) targeting AbstractLink::get_view_url() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
261
262
            if ($doc_url != null) {
263
                $edit_column .= '&nbsp;<a href="'.$doc_url.'" target="_blank">';
264
                $edit_column .= Display::return_icon('link.gif', get_lang('OpenDocument')).'</a>';
265
            }
266
        }
267
268
        return $edit_column;
269
    }
270
}
271