table.php 5.83 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
<?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/>.

/**
 * Contains definition of the table class for the user attempts report.
 *
 * @package   mod_adaptivequiz
 * @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\individual_user_attempts;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir . '/tablelib.php');

use mod_adaptivequiz\local\attempt\attempt_state;
use mod_adaptivequiz\local\report\questions_difficulty_range;
use mod_adaptivequiz_renderer;
use moodle_url;
use stdClass;
use table_sql;

/**
 * Definition of the table class for the user attempts report.
 *
 * @package mod_adaptivequiz
 */
final class table extends table_sql {

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

    /**
     * @var filter $filter
     */
    private $filter;

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

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

    public function __construct(
        mod_adaptivequiz_renderer $renderer,
        filter $filter,
        moodle_url $baseurl,
        questions_difficulty_range $questionsdifficultyrange,
        int $cmid,
        //KNIGHT: Modifications with the purpose of hiding the column "reason for stopping the attempt" in the student review
        bool $showattemptstopcriteria

    ) {
        parent::__construct('individualuserattemptstable');

        $this->renderer = $renderer;
        $this->filter = $filter;
        $this->questionsdifficultyrange = $questionsdifficultyrange;
        //KNIGHT
        $this->showattemptstopcriteria = $showattemptstopcriteria;
        
        $this->cmid = $cmid;

        $this->init($baseurl);
    }

    public function get_sql_sort() {
        return 'timemodified DESC';
    }

    protected function col_attemptstate(stdClass $row): string {
        if (0 == strcmp(attempt_state::IN_PROGRESS, $row->attemptstate)) {
            return get_string('recentinprogress', 'adaptivequiz');
        }

        return get_string('recentcomplete', 'adaptivequiz');
    }

    protected function col_score(stdClass $row): string {
        if ($row->measure === null || $row->stderror === null || $row->stderror == 0.0) {
            return 'n/a';
        }

        $formatmeasureparams = new stdClass();
        $formatmeasureparams->measure = $row->measure;
        $formatmeasureparams->highestlevel = $this->questionsdifficultyrange->highest_level();
        $formatmeasureparams->lowestlevel = $this->questionsdifficultyrange->lowest_level();

        return $this->renderer->format_measure($formatmeasureparams) .
            ' ' . $this->renderer->format_standard_error($row);
    }

    protected function col_timecreated(stdClass $row): string {
        return userdate($row->timecreated);
    }

    protected function col_timemodified(stdClass $row): string {
        return userdate($row->timemodified);
    }

    /**
     * A hook to format the output of actions column for an attempt row.
     *
     * @param stdClass $row A row from the {adaptivequiz_attempt}.
     */
    protected function col_actions(stdClass $row): string {
        return $this->renderer->individual_user_attempt_actions($row, $this->showattemptstopcriteria);
    }

    private function init(moodle_url $baseurl): void {
            $columns = ['attemptstate', 'questionsattempted', 'score', 'timecreated', 'timemodified', 'actions'];
            $headers = [
            get_string('attemptstate', 'adaptivequiz'),
            get_string('questionsattempted', 'adaptivequiz'),
            get_string('score', 'adaptivequiz'),
            get_string('attemptstarttime', 'adaptivequiz'),
            get_string('attemptfinishedtimestamp', 'adaptivequiz'),
            '',
        ];
        $selectionstring = 'id, userid, uniqueid, attemptstate, questionsattempted, timemodified, timecreated, measure, standarderror AS stderror';

        //KNIGHT
        if($this->showattemptstopcriteria){
            array_splice($columns, 1, 0, array('attemptstopcriteria'));
            array_splice($headers, 1, 0, array(get_string('attemptstopcriteria', 'adaptivequiz')));
            $selectionstring = 'id, userid, uniqueid, attemptstopcriteria, attemptstate, questionsattempted, timemodified, timecreated, measure, standarderror AS stderror';
        }

        $this->define_columns($columns);
        $this->define_headers($headers);

        $this->set_content_alignment_in_columns();
        $this->define_baseurl($baseurl);
        $this->set_attribute('class', $this->attributes['class'] . ' ' . $this->uniqueid);
        $this->is_downloadable(false);
        $this->collapsible(false);
        $this->sortable(false);

        $this->set_sql(
            $selectionstring,
            '{adaptivequiz_attempt}',
            'instance = :adaptivequiz AND userid = :userid',
            ['adaptivequiz' => $this->filter->adaptivequizid, 'userid' => $this->filter->userid]
        );
    }

    private function set_content_alignment_in_columns(): void {
        foreach (array_keys($this->columns) as $column) {
            $this->column_class[$column] .= ' text-center';
        }
    }
}