users_attempts_table.php 7.51 KB
Newer Older
0815-xyz's avatar
0815-xyz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * A class to display a table with users either with attempts or without them.
 *
 * @copyright  2022 onwards Vitaly Potenko <potenkov@gmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace mod_adaptivequiz\local\report\users_attempts;

use coding_exception;
use context;
use dml_exception;
use html_writer;
use mod_adaptivequiz\local\report\questions_difficulty_range;
use mod_adaptivequiz\local\report\users_attempts\filter\filter;
use mod_adaptivequiz\local\report\users_attempts\sql\sql_resolver;
use mod_adaptivequiz_renderer;
use moodle_exception;
use moodle_url;
use stdClass;
use table_sql;

final class users_attempts_table extends table_sql {

    private const UNIQUE_ID = 'usersattemptstable';

    /**
     * @var mod_adaptivequiz_renderer $renderer
     */
    private $renderer;

    /**
     * @var int $cmid
     */
    private $cmid;

    /**
     * @var questions_difficulty_range $questionsdifficultyrange
     */
    private $questionsdifficultyrange;

    /**
     * @throws coding_exception
     */
    public function __construct(
        mod_adaptivequiz_renderer $renderer,
        int $cmid,
        questions_difficulty_range $questionsdifficultyrange,
        moodle_url $baseurl,
        context $context,
        filter $filter
    ) {
        parent::__construct(self::UNIQUE_ID);

        $this->renderer = $renderer;
        $this->cmid = $cmid;
        $this->questionsdifficultyrange = $questionsdifficultyrange;

        $this->init($baseurl, $context, $filter);
    }

    /**
     * {@inheritdoc}
     * @throws dml_exception
     */
    public function query_db($pagesize, $useinitialsbar = true): void {
        global $DB;

        if (!$this->is_downloading()) {
            if ($this->countsql === null) {
                $this->countsql = 'SELECT COUNT(1) FROM '.$this->sql->from.' WHERE '.$this->sql->where;
                $this->countparams = $this->sql->params;
            }
            $grandtotal = $DB->count_records_sql($this->countsql, $this->countparams);
            if ($useinitialsbar && !$this->is_downloading()) {
                $this->initialbars(true);
            }

            list($wsql, $wparams) = $this->get_sql_where();
            if ($wsql) {
                $this->countsql .= ' AND ' . $wsql;
                $this->countparams = array_merge($this->countparams, $wparams);

                $this->sql->where .= ' AND ' . $wsql;
                $this->sql->params = array_merge($this->sql->params, $wparams);

                $total  = $DB->count_records_sql($this->countsql, $this->countparams);
            } else {
                $total = $grandtotal;
            }

            $this->pagesize($pagesize, $total);
        }

        $sort = $this->get_sql_sort();
        if ($sort) {
            $sort = "ORDER BY $sort";
        }

        $groupby = $this->sql->groupby ?? '';
        if ($groupby) {
            $groupby = "GROUP BY $groupby";
        }

        $sql = "SELECT {$this->sql->fields} FROM {$this->sql->from} WHERE {$this->sql->where} {$groupby} {$sort}";

        if (!$this->is_downloading()) {
            $this->rawdata = $DB->get_records_sql($sql, $this->sql->params, $this->get_page_start(),
                $this->get_page_size());
        } else {
            $this->rawdata = $DB->get_records_sql($sql, $this->sql->params);
        }
    }

    protected function col_attemptsnum(stdClass $row): string {
        if (!$row->attemptsnum) {
            return '-';
        }

        if (!$this->is_downloading()) {
            return html_writer::link(
                new moodle_url(
                    '/mod/adaptivequiz/viewattemptreport.php',
                    ['userid' => $row->id, 'cmid' => $this->cmid]
                ),
                $row->attemptsnum
            );
        }

        return $row->attemptsnum;
    }

    /**
     * @throws moodle_exception
     */
    protected function col_measure(stdClass $row): string {
        $formatmeasureparams = new stdClass();
        $formatmeasureparams->measure = $row->measure;
        $formatmeasureparams->highestlevel = $this->questionsdifficultyrange->highest_level();
        $formatmeasureparams->lowestlevel = $this->questionsdifficultyrange->lowest_level();

        $measure = $this->renderer->format_measure($formatmeasureparams);
        if (!$row->attemptid) {
            return $measure;
        }

        if (!$this->is_downloading()) {
            return html_writer::link(
                new moodle_url('/mod/adaptivequiz/reviewattempt.php', ['attempt' => $row->attemptid]),
                $measure
            );
        }

        return $measure;
    }

    protected function col_stderror(stdClass $row): string {
        $rendered = $this->renderer->format_standard_error($row);
        if (!$this->is_downloading()) {
            return $rendered;
        }

        return html_entity_decode($rendered, ENT_QUOTES, 'UTF-8');
    }

    /**
     * @throws coding_exception
     */
    protected function col_attempttimefinished(stdClass $row): string {
        return intval($row->attempttimefinished)
            ? userdate($row->attempttimefinished)
            : get_string('na', 'adaptivequiz');
    }

    /**
     * A convenience method to call a bunch of init methods.
     *
     * @param moodle_url $baseurl
     * @throws coding_exception
     */
    private function init(moodle_url $baseurl, context $context, filter $filter): void {
        $this->define_columns([
            'fullname', 'email', 'attemptsnum', 'measure', 'stderror', 'attempttimefinished',
        ]);
        $this->define_headers([
            get_string('fullname'),
            get_string('email'),
            get_string('numofattemptshdr', 'adaptivequiz'),
            get_string('bestscore', 'adaptivequiz'),
            get_string('bestscorestderror', 'adaptivequiz'),
            get_string('attemptfinishedtimestamp', 'adaptivequiz'),
        ]);
        $this->define_baseurl($baseurl);
        $this->set_attribute('class', $this->attributes['class'] . ' usersattemptstable');
        $this->set_content_alignment_in_columns();
        $this->collapsible(false);
        $this->sortable(true, 'lastname');
        $this->is_downloadable(true);

        $sqlandparams = sql_resolver::sql_and_params($filter, $context);
        $this->set_sql($sqlandparams->fields(), $sqlandparams->from(), $sqlandparams->where(), $sqlandparams->params());
        $this->set_group_by_sql($sqlandparams->group_by());
        $this->set_count_sql($sqlandparams->count_sql(), $sqlandparams->count_sql_params());
    }

    private function set_group_by_sql(?string $clause): void {
        $this->sql->groupby = $clause;
    }

    private function set_content_alignment_in_columns(): void {
        $this->column_class['attemptsnum'] .= ' text-center';
        $this->column_class['measure'] .= ' text-center';
        $this->column_class['stderror'] .= ' text-center';
    }
}