custom_view.php 7.15 KB
Newer Older
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
<?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/>.

/**
 * Defines the custom question bank view used on the Edit quiz page.
 *
 * @package   mod_quiz
 * @category  question
 * @copyright 1999 onwards Martin Dougiamas and others {@link http://moodle.com}
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace mod_quiz\question\bank;

28
use coding_exception;
29
30
31
32
33

/**
 * Subclass to customise the view of the question bank for the quiz editing screen.
 *
 * @copyright  2009 Tim Hunt
34
 * @author     2021 Safat Shahin <safatshahin@catalyst-au.net>
35
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
37
38
39
class custom_view extends \core_question\local\bank\view {

    /** @var bool $quizhasattempts whether the quiz this is used by has been attemptd. */
40
    protected $quizhasattempts = false;
41
42

    /** @var \stdClass $quiz the quiz settings. */
43
    protected $quiz = false;
44

45
46
47
48
    /** @var int The maximum displayed length of the category info. */
    const MAX_TEXT_LENGTH = 200;

    /**
49
     * Constructor for custom_view.
50
51
52
53
54
55
56
57
58
59
60
     * @param \question_edit_contexts $contexts
     * @param \moodle_url $pageurl
     * @param \stdClass $course course settings
     * @param \stdClass $cm activity settings.
     * @param \stdClass $quiz quiz settings.
     */
    public function __construct($contexts, $pageurl, $course, $cm, $quiz) {
        parent::__construct($contexts, $pageurl, $course, $cm);
        $this->quiz = $quiz;
    }

61
62
63
64
65
66
67
68
    protected function wanted_columns(): array {
        $quizquestionbankcolumns = [
            'mod_quiz\\question\\bank\\add_action_column',
            'core_question\\local\\bank\\checkbox_column',
            'qbank_viewquestiontype\\question_type_column',
            'mod_quiz\\question\\bank\\question_name_text_column',
            'qbank_previewquestion\\preview_action_column',
        ];
69
70
71

        foreach ($quizquestionbankcolumns as $fullname) {
            if (!class_exists($fullname)) {
72
                throw new coding_exception('Invalid quiz question bank column', $fullname);
73
74
75
76
77
78
            }
            $this->requiredcolumns[$fullname] = new $fullname($this);
        }
        return $this->requiredcolumns;
    }

79
    protected function heading_column(): string {
80
81
82
        return 'mod_quiz\\question\\bank\\question_name_text_column';
    }

83
84
85
    protected function default_sort(): array {
        return [
            'qbank_viewquestiontype\\question_type_column' => 1,
86
            'mod_quiz\\question\\bank\\question_name_text_column' => 1,
87
        ];
88
89
90
91
92
93
    }

    /**
     * Let the question bank display know whether the quiz has been attempted,
     * hence whether some bits of UI, like the add this question to the quiz icon,
     * should be displayed.
94
     *
95
96
     * @param bool $quizhasattempts whether the quiz has attempts.
     */
97
    public function set_quiz_has_attempts($quizhasattempts): void {
98
99
100
101
102
103
        $this->quizhasattempts = $quizhasattempts;
        if ($quizhasattempts && isset($this->visiblecolumns['addtoquizaction'])) {
            unset($this->visiblecolumns['addtoquizaction']);
        }
    }

104
105
106
107
108
109
    /**
     * Question preview url.
     *
     * @param \stdClass $question
     * @return \moodle_url
     */
110
111
112
113
    public function preview_question_url($question) {
        return quiz_question_preview_url($this->quiz, $question);
    }

114
115
116
117
118
119
    /**
     * URL of add to quiz.
     *
     * @param $questionid
     * @return \moodle_url
     */
120
121
122
123
124
125
126
127
128
129
130
131
132
    public function add_to_quiz_url($questionid) {
        $params = $this->baseurl->params();
        $params['addquestion'] = $questionid;
        $params['sesskey'] = sesskey();
        return new \moodle_url('/mod/quiz/edit.php', $params);
    }

    /**
     * Renders the html question bank (same as display, but returns the result).
     *
     * Note that you can only output this rendered result once per page, as
     * it contains IDs which must be unique.
     *
133
134
     * @param array $pagevars
     * @param string $tabname
135
136
     * @return string HTML code for the form
     */
137
    public function render($pagevars, $tabname): string {
138
        ob_start();
139
        $this->display($pagevars, $tabname);
140
141
142
143
144
        $out = ob_get_contents();
        ob_end_clean();
        return $out;
    }

145
    protected function display_bottom_controls($totalnumber, $recurse, $category, \context $catcontext, array $addcontexts): void {
146
147
148
149
150
        $cmoptions = new \stdClass();
        $cmoptions->hasattempts = !empty($this->quizhasattempts);

        $canuseall = has_capability('moodle/question:useall', $catcontext);

151
        echo \html_writer::start_tag('div', ['class' => 'modulespecificbuttonscontainer']);
152
153
154
        if ($canuseall) {
            // Add selected questions to the quiz.
            $params = array(
155
156
157
158
159
160
161
162
                'type' => 'submit',
                'name' => 'add',
                'class' => 'btn btn-primary',
                'value' => get_string('addselectedquestionstoquiz', 'quiz'),
                'data-action' => 'toggle',
                'data-togglegroup' => 'qbank',
                'data-toggle' => 'action',
                'disabled' => true,
163
164
165
            );
            echo \html_writer::empty_tag('input', $params);
        }
166
        echo \html_writer::end_tag('div');
167
168
    }

169
    protected function create_new_question_form($category, $canadd): void {
170
171
        // Don't display this.
    }
172
173

    /**
174
     * Override the base implementation in \core_question\local\bank\view
175
176
177
     * because we don't want to print the headers in the fragment
     * for the modal.
     */
178
    protected function display_question_bank_header(): void {
179
180
181
182
183
184
185
186
187
188
    }

    /**
     * Override the base implementation in \core_question\bank\view
     * because we don't want it to read from the $_POST global variables
     * for the sort parameters since they are not present in a fragment.
     *
     * Unfortunately the best we can do is to look at the URL for
     * those parameters (only marginally better really).
     */
189
    protected function init_sort_from_params(): void {
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        $this->sort = [];
        for ($i = 1; $i <= self::MAX_SORTS; $i++) {
            if (!$sort = $this->baseurl->param('qbs' . $i)) {
                break;
            }
            // Work out the appropriate order.
            $order = 1;
            if ($sort[0] == '-') {
                $order = -1;
                $sort = substr($sort, 1);
                if (!$sort) {
                    break;
                }
            }
            // Deal with subsorts.
205
206
            list($colname) = $this->parse_subsort($sort);
            $this->get_column_type($colname);
207
208
209
            $this->sort[$sort] = $order;
        }
    }
210
}