Completed
Push — master ( c9546d...95f607 )
by Julito
09:41
created

public/main/gradebook/lib/fe/resulttable.class.php (1 issue)

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
class ResultTable extends SortableTable
12
{
13
    private $datagen;
14
    private $evaluation;
15
    private $allresults;
16
    private $iscourse;
17
18
    /**
19
     * ResultTable constructor.
20
     *
21
     * @param string      $evaluation
22
     * @param array       $results
23
     * @param string|null $iscourse
24
     * @param array       $addparams
25
     * @param bool        $forprint
26
     */
27
    public function __construct(
28
        $evaluation,
29
        $results = [],
30
        $iscourse,
31
        $addparams = [],
32
        $forprint = false
33
    ) {
34
        parent:: __construct(
35
            'resultlist',
36
            null,
37
            null,
38
            api_is_western_name_order() ? 1 : 2
39
        );
40
41
        $this->datagen = new ResultsDataGenerator($evaluation, $results, true);
42
43
        $this->evaluation = $evaluation;
44
        $this->iscourse = $iscourse;
45
        $this->forprint = $forprint;
46
47
        if (isset($addparams)) {
48
            $this->set_additional_parameters($addparams);
49
        }
50
        $scoredisplay = ScoreDisplay::instance();
51
        $column = 0;
52
        if ('1' == $this->iscourse) {
53
            $this->set_header($column++, '', false);
54
            $this->set_form_actions([
55
                    'delete' => get_lang('Delete'),
56
            ]);
57
        }
58
        if (api_is_western_name_order()) {
59
            $this->set_header($column++, get_lang('First name'));
60
            $this->set_header($column++, get_lang('Last name'));
61
        } else {
62
            $this->set_header($column++, get_lang('Last name'));
63
            $this->set_header($column++, get_lang('First name'));
64
        }
65
66
        $model = ExerciseLib::getCourseScoreModel();
67
        if (empty($model)) {
68
            $this->set_header($column++, get_lang('Score'));
69
        }
70
71
        if ($scoredisplay->is_custom()) {
72
            $this->set_header($column++, get_lang('Ranking'));
73
        }
74
        if (!$this->forprint) {
75
            $this->set_header($column++, get_lang('Edit'), false);
76
        }
77
    }
78
79
    /**
80
     * Function used by SortableTable to get total number of items in the table.
81
     */
82
    public function get_total_number_of_items()
83
    {
84
        return $this->datagen->get_total_results_count();
85
    }
86
87
    /**
88
     * Function used by SortableTable to generate the data to display.
89
     */
90
    public function get_table_data(
91
        $from = 1,
92
        $per_page = null,
93
        $column = null,
94
        $direction = null,
95
        $sort = null
96
    ) {
97
        $isWesternNameOrder = api_is_western_name_order();
98
        $scoredisplay = ScoreDisplay::instance();
99
100
        // determine sorting type
101
        $col_adjust = '1' == $this->iscourse ? 1 : 0;
102
103
        switch ($this->column) {
104
            // first name or last name
105
            case 0 + $col_adjust:
106
                if ($isWesternNameOrder) {
107
                    $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
108
                } else {
109
                    $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
110
                }
111
                break;
112
                // first name or last name
113
            case 1 + $col_adjust:
114
                if ($isWesternNameOrder) {
115
                    $sorting = ResultsDataGenerator::RDG_SORT_LASTNAME;
116
                } else {
117
                    $sorting = ResultsDataGenerator::RDG_SORT_FIRSTNAME;
118
                }
119
                break;
120
                // Score
121
            case 2 + $col_adjust:
122
                $sorting = ResultsDataGenerator::RDG_SORT_SCORE;
123
                break;
124
            case 3 + $col_adjust:
125
                $sorting = ResultsDataGenerator::RDG_SORT_MASK;
126
                break;
127
        }
128
129
        if ('DESC' === $this->direction) {
130
            $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...
131
        } else {
132
            $sorting |= ResultsDataGenerator::RDG_SORT_ASC;
133
        }
134
135
        $data_array = $this->datagen->get_data($sorting, $from, $this->per_page);
136
137
        $model = ExerciseLib::getCourseScoreModel();
138
139
        // generate the data to display
140
        $sortable_data = [];
141
        foreach ($data_array as $item) {
142
            $row = [];
143
            if ('1' == $this->iscourse) {
144
                $row[] = $item['result_id'];
145
            }
146
            if ($isWesternNameOrder) {
147
                $row[] = $item['firstname'];
148
                $row[] = $item['lastname'];
149
            } else {
150
                $row[] = $item['lastname'];
151
                $row[] = $item['firstname'];
152
            }
153
154
            if (empty($model)) {
155
                $row[] = Display::bar_progress(
156
                    $item['percentage_score'],
157
                    false,
158
                    $item['score']
159
                );
160
            }
161
162
            if ($scoredisplay->is_custom()) {
163
                $row[] = $item['display'];
164
            }
165
            if (!$this->forprint) {
166
                $row[] = $this->build_edit_column($item);
167
            }
168
            $sortable_data[] = $row;
169
        }
170
171
        return $sortable_data;
172
    }
173
174
    /**
175
     * @param Result $result
176
     * @param string $url
177
     *
178
     * @return string
179
     */
180
    public static function getResultAttemptTable($result, $url = '')
181
    {
182
        if (empty($result)) {
183
            return '';
184
        }
185
186
        $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_ATTEMPT);
187
188
        $sql = "SELECT * FROM $table WHERE result_id = ".$result->get_id().' ORDER BY created_at DESC';
189
        $resultQuery = Database::query($sql);
190
        $list = Database::store_result($resultQuery);
191
192
        $htmlTable = new HTML_Table(['class' => 'data_table']);
193
        $htmlTable->setHeaderContents(0, 0, get_lang('Score'));
194
        $htmlTable->setHeaderContents(0, 1, get_lang('Comment'));
195
        $htmlTable->setHeaderContents(0, 2, get_lang('Created at'));
196
197
        if (!empty($url)) {
198
            $htmlTable->setHeaderContents(0, 3, get_lang('Detail'));
199
        }
200
201
        $row = 1;
202
        foreach ($list as $data) {
203
            $htmlTable->setCellContents($row, 0, $data['score']);
204
            $htmlTable->setCellContents($row, 1, $data['comment']);
205
            $htmlTable->setCellContents($row, 2, Display::dateToStringAgoAndLongDate($data['created_at']));
206
            if (!empty($url)) {
207
                $htmlTable->setCellContents(
208
                    $row,
209
                    3,
210
                    Display::url(
211
                        Display::return_icon('delete.png', get_lang('Delete')),
212
                        $url.'&action=delete_attempt&result_attempt_id='.$data['id']
213
                    )
214
                );
215
            }
216
            $row++;
217
        }
218
219
        return $htmlTable->toHtml();
220
    }
221
222
    /**
223
     * @param array $item
224
     *
225
     * @return string
226
     */
227
    private function build_edit_column($item)
228
    {
229
        $locked_status = $this->evaluation->get_locked();
230
        $allowMultipleAttempts = api_get_configuration_value('gradebook_multiple_evaluation_attempts');
231
        $baseUrl = api_get_self().'?selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq();
232
        $editColumn = '';
233
        if (api_is_allowed_to_edit(null, true) && 0 == $locked_status) {
234
            if ($allowMultipleAttempts) {
235
                if (!empty($item['percentage_score'])) {
236
                    $editColumn .=
237
                        Display::url(
238
                            Display::return_icon('add.png', get_lang('AddAttempt'), '', '22'),
239
                            $baseUrl.'&action=add_attempt&editres='.$item['result_id']
240
                        );
241
                } else {
242
                    $editColumn .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
243
                        Display::return_icon('edit.png', get_lang('Edit'), '', '22').'</a>';
244
                }
245
            } else {
246
                $editColumn .= '<a href="'.api_get_self().'?editres='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
247
                    Display::return_icon('edit.png', get_lang('Edit'), '', '22').'</a>';
248
            }
249
            $editColumn .= ' <a href="'.api_get_self().'?delete_mark='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">'.
250
                Display::return_icon('delete.png', get_lang('Delete'), '', '22').'</a>';
251
        }
252
253
        if (null == $this->evaluation->get_course_code()) {
254
            $editColumn .= '&nbsp;<a href="'.api_get_self().'?resultdelete='.$item['result_id'].'&selecteval='.$this->evaluation->get_id().'" onclick="return confirmationuser();">';
255
            $editColumn .= Display::return_icon('delete.png', get_lang('Delete'));
256
            $editColumn .= '</a>';
257
            $editColumn .= '&nbsp;<a href="user_stats.php?userid='.$item['id'].'&selecteval='.$this->evaluation->get_id().'&'.api_get_cidreq().'">';
258
            $editColumn .= Display::return_icon('statistics.gif', get_lang('Statistics'));
259
            $editColumn .= '</a>';
260
        }
261
262
        // Evaluation's origin is a link
263
        if ($this->evaluation->get_category_id() < 0) {
264
            $link = LinkFactory::get_evaluation_link($this->evaluation->get_id());
265
            $doc_url = $link->get_view_url($item['id']);
266
267
            if (null != $doc_url) {
268
                $editColumn .= '&nbsp;<a href="'.$doc_url.'" target="_blank">';
269
                $editColumn .= Display::return_icon('link.gif', get_lang('Open document')).'</a>';
270
            }
271
        }
272
273
        return $editColumn;
274
    }
275
}
276