Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace qbank_history;
18
 
19
use core_question\local\bank\question_edit_contexts;
20
use core_question\local\bank\view;
21
use moodle_url;
22
use stdClass;
23
 
24
/**
25
 * Custom view class for the history page.
26
 *
27
 * @package    qbank_history
28
 * @copyright  2022 Catalyst IT Australia Pty Ltd
29
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class question_history_view extends view {
33
 
34
    /**
35
     * Entry id to get the versions
36
     *
37
     * @var int $entryid
38
     */
39
    protected $entryid;
40
 
41
    /**
42
     * Base url for the return.
43
     *
44
     * @var \moodle_url $basereturnurl
45
     */
46
    protected $basereturnurl;
47
 
48
    /**
49
     * Constructor for the history.
50
     * @param question_edit_contexts $contexts the contexts of api call
51
     * @param moodle_url $pageurl url of the page
52
     * @param stdClass $course course settings
53
     * @param stdClass|null $cm (optional) activity settings.
54
     * @param array $params the parameters required to initialize the api.
55
     * @param array $extraparams any extra parameters need to initialized if the api is extended, it will be passed to js.
56
     * @throws \moodle_exception
57
     */
58
    public function __construct(
59
        question_edit_contexts $contexts,
60
        moodle_url $pageurl,
61
        stdClass $course,
1441 ariadna 62
        ?stdClass $cm = null,
1 efrain 63
        array $params = [],
64
        array $extraparams = [],
65
    ) {
1441 ariadna 66
        if ($cm === null) {
67
            debugging('$cm is now a required field', DEBUG_DEVELOPER);
68
        }
69
 
1 efrain 70
        $this->entryid = $extraparams['entryid'];
71
        $this->basereturnurl = new \moodle_url($extraparams['returnurl']);
72
        parent::__construct($contexts, $pageurl, $course, $cm, $params, $extraparams);
73
    }
74
 
75
    protected function init_question_actions(): void {
76
        parent::init_question_actions();
77
        unset($this->questionactions['qbank_history\history_action']);
78
    }
79
 
80
    protected function wanted_columns(): array {
81
        $this->requiredcolumns = [];
82
        $questionbankcolumns = $this->get_question_bank_plugins();
83
        foreach ($questionbankcolumns as $classobject) {
84
            if (empty($classobject)) {
85
                continue;
86
            }
87
            $this->requiredcolumns[$classobject->get_column_name()] = $classobject;
88
        }
89
 
90
        return $this->requiredcolumns;
91
    }
92
 
1441 ariadna 93
    /**
94
     * @deprecated since Moodle 4.3 MDL-72321
95
     */
96
    #[\core\attribute\deprecated('filtering objects', since: '4.3', mdl: 'MDL-72321', final: true)]
1 efrain 97
    protected function display_advanced_search_form($advancedsearch): void {
1441 ariadna 98
        \core\deprecation::emit_deprecation([self::class, __FUNCTION__]);
1 efrain 99
    }
100
 
101
    public function allow_add_questions(): bool {
102
        // As we dont want to create questions in this page.
103
        return false;
104
    }
105
 
1441 ariadna 106
    #[\Override]
1 efrain 107
    protected function default_sort(): array {
1441 ariadna 108
        return ['qbank_history__version_number_column' => SORT_ASC];
1 efrain 109
    }
110
 
111
    protected function build_query(): void {
112
        // Get the required tables and fields.
113
        [$fields, $joins] = $this->get_component_requirements(array_merge($this->requiredcolumns, $this->questionactions));
114
 
115
        // Build the order by clause.
116
        $sorts = [];
117
        foreach ($this->sort as $sortname => $sortorder) {
118
            list($colname, $subsort) = $this->parse_subsort($sortname);
119
            $sorts[] = $this->requiredcolumns[$colname]->sort_expression($sortorder == SORT_DESC, $subsort);
120
        }
121
 
122
        // Build the where clause.
123
        $entryid = "qbe.id = $this->entryid";
124
        // Changes done here to get the questions only for the passed entryid.
125
        $tests = ['q.parent = 0', $entryid];
126
        $this->sqlparams = [];
127
        foreach ($this->searchconditions as $searchcondition) {
128
            if ($searchcondition->where()) {
129
                $tests[] = '((' . $searchcondition->where() .'))';
130
            }
131
            if ($searchcondition->params()) {
132
                $this->sqlparams = array_merge($this->sqlparams, $searchcondition->params());
133
            }
134
        }
135
        // Build the SQL.
136
        $sql = ' FROM {question} q ' . implode(' ', $joins);
137
        $sql .= ' WHERE ' . implode(' AND ', $tests);
138
        $this->countsql = 'SELECT count(1)' . $sql;
139
        $this->loadsql = 'SELECT ' . implode(', ', $fields) . $sql . ' ORDER BY ' . implode(', ', $sorts);
140
    }
141
 
142
    /**
143
     * Display the header for the question bank in the history page to include question name and type.
144
     */
145
    public function display_question_bank_header(): void {
146
        global $PAGE, $DB, $OUTPUT;
147
        $sql = 'SELECT q.*
148
                 FROM {question} q
149
                 JOIN {question_versions} qv ON qv.questionid = q.id
150
                 JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
151
                WHERE qv.version  = (SELECT MAX(v.version)
152
                                       FROM {question_versions} v
153
                                       JOIN {question_bank_entries} be
154
                                         ON be.id = v.questionbankentryid
155
                                      WHERE be.id = qbe.id)
156
                  AND qbe.id = ?';
157
        $latestquestiondata = $DB->get_record_sql($sql, [$this->entryid]);
158
        if ($latestquestiondata) {
159
            $historydata = [
160
                'questionname' => $latestquestiondata->name,
161
                'returnurl' => $this->basereturnurl,
162
                'questionicon' => print_question_icon($latestquestiondata)
163
            ];
164
            // Header for the page before the actual form from the api.
165
            echo $PAGE->get_renderer('qbank_history')->render_history_header($historydata);
166
        } else {
167
            // Continue when all the question versions are deleted.
168
            echo $OUTPUT->notification(get_string('allquestionversionsdeleted', 'qbank_history'), 'notifysuccess');
169
            echo $OUTPUT->continue_button($this->basereturnurl);
170
        }
171
    }
172
 
173
    public function is_listing_specific_versions(): bool {
174
        return true;
175
    }
176
 
177
    /**
178
     * Override wanted_filters so that we apply the filters provided by the URL, but don't display the filter UI.
179
     *
180
     * @return void
181
     */
182
    public function wanted_filters(): void {
183
        $this->display_question_bank_header();
184
        // Add search conditions.
185
        $this->add_standard_search_conditions();
186
    }
187
 
188
}