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
/**
18
 * For a given question type, list the number of
19
 *
20
 * @package    report
21
 * @subpackage questioninstances
22
 * @copyright  2008 Tim Hunt
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
require(__DIR__.'/../../config.php');
27
require_once($CFG->libdir.'/adminlib.php');
28
require_once($CFG->libdir.'/questionlib.php');
29
 
30
// Get URL parameters.
31
$requestedqtype = optional_param('qtype', '', PARAM_SAFEDIR);
32
 
33
// Print the header & check permissions.
34
admin_externalpage_setup('reportquestioninstances', '', null, '', array('pagelayout'=>'report'));
35
$PAGE->set_primary_active_tab('siteadminnode');
36
echo $OUTPUT->header();
37
 
38
// Log.
39
\report_questioninstances\event\report_viewed::create(array('other' => array('requestedqtype' => $requestedqtype)))->trigger();
40
 
41
// Prepare the list of capabilities to choose from
42
$qtypes = question_bank::get_all_qtypes();
43
$qtypechoices = array();
44
foreach ($qtypes as $qtype) {
45
    $qtypechoices[$qtype->name()] = $qtype->local_name();
46
}
47
 
1441 ariadna 48
// Sort by label.
49
core_collator::asort($qtypechoices);
50
 
1 efrain 51
// Print the settings form.
52
echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
53
echo '<form method="get" action="." id="settingsform"><div>';
54
echo $OUTPUT->heading(get_string('reportsettings', 'report_questioninstances'));
55
echo '<p id="intro">', get_string('intro', 'report_questioninstances') , '</p>';
56
echo '<p><label for="menuqtype"> ' . get_string('questiontype', 'admin') . '</label> ';
57
echo html_writer::select($qtypechoices, 'qtype', $requestedqtype, array('_all_'=>get_string('all')));
58
echo '</p>';
59
echo '<p><input type="submit" class="btn btn-secondary" id="settingssubmit" value="' .
60
        get_string('getreport', 'report_questioninstances') . '" /></p>';
61
echo '</div></form>';
62
echo $OUTPUT->box_end();
63
 
64
$params[] = \core_question\local\bank\question_version_status::QUESTION_STATUS_HIDDEN;
65
// If we have a qtype to report on, generate the report.
66
if ($requestedqtype) {
67
 
68
    // Work out the bits needed for the SQL WHERE clauses.
69
    if ($requestedqtype == 'missingtype') {
70
        $title = get_string('reportformissingqtypes', 'report_questioninstances');
71
 
72
        $othertypes = array_keys($qtypes);
73
        $key = array_search('missingtype', $othertypes);
74
        unset($othertypes[$key]);
1441 ariadna 75
 
76
        [$sqlqtypetest, $sqlqtypetestparams] = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
1 efrain 77
        $sqlqtypetest = 'WHERE qtype ' . $sqlqtypetest;
1441 ariadna 78
        $params = array_merge($params, $sqlqtypetestparams);
1 efrain 79
 
80
    } else if ($requestedqtype == '_all_') {
81
        $title = get_string('reportforallqtypes', 'report_questioninstances');
82
 
83
        $sqlqtypetest = '';
84
 
85
    } else {
86
        $title = get_string('reportforqtype', 'report_questioninstances',
87
                question_bank::get_qtype($requestedqtype)->local_name());
88
 
89
        $sqlqtypetest = 'WHERE qtype = ?';
90
        $params [] = $requestedqtype;
91
    }
92
 
93
    // Get the question counts, and all the context information, for each
94
    // context. That is, rows of these results can be used as $context objects.
95
    $ctxpreload = context_helper::get_preload_record_columns_sql('con');
96
    $ctxgroupby = implode(',', array_keys(context_helper::get_preload_record_columns('con')));
97
    $counts = $DB->get_records_sql("
98
            SELECT result.contextid, SUM(numquestions) AS numquestions, SUM(numhidden) AS numhidden, $ctxpreload
99
              FROM (SELECT data.contextid, data.versionid, COUNT(data.numquestions) AS numquestions,
100
                           (SELECT COUNT(qv.id)
101
                              FROM {question_versions} qv
102
                             WHERE qv.id = data.versionid
103
                                   AND qv.status = ?) AS numhidden
104
                      FROM (SELECT qv.id as versionid, qc.contextid, 1 AS numquestions
105
                              FROM {question} q
106
                              JOIN {question_versions} qv ON qv.questionid = q.id
107
                              JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
108
                              JOIN {question_categories} qc ON qc.id = qbe.questioncategoryid
109
                              JOIN {context} con ON con.id = qc.contextid
110
                              $sqlqtypetest
111
                                   AND qv.version = (SELECT MAX(v.version)
112
                                                       FROM {question_versions} v
113
                                                       JOIN {question_bank_entries} be
114
                                                         ON be.id = v.questionbankentryid
115
                                                      WHERE be.id = qbe.id)
116
                                   AND (q.parent = 0 OR q.parent = q.id)) data
117
                  GROUP BY data.contextid, data.versionid) result
118
              JOIN {context} con ON con.id = result.contextid
119
          GROUP BY result.contextid, $ctxgroupby
120
          ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
121
 
122
    // Print the report heading.
123
    echo $OUTPUT->heading($title);
124
 
125
    // Initialise the table.
126
    $table = new html_table();
127
    $table->head = array(
128
            get_string('context', 'role'),
129
            get_string('totalquestions', 'report_questioninstances'),
130
            get_string('visiblequestions', 'report_questioninstances'),
131
            get_string('hiddenquestions', 'report_questioninstances'));
132
    $table->data = array();
133
    $table->class = '';
134
    $table->id = '';
135
 
136
    // Add the data for each row.
137
    $totalquestions = 0;
138
    $totalvisible = 0;
139
    $totalhidden = 0;
140
    foreach ($counts as $count) {
141
        // Work out a link for editing questions in this context.
142
        context_helper::preload_from_record($count);
143
        $context = context::instance_by_id($count->contextid);
144
        $contextname = $context->get_context_name();
145
        $url = question_edit_url($context);
146
        if ($url) {
147
            $contextname = '<a href="' . $url . '" title="' .
148
                    get_string('editquestionshere', 'report_questioninstances') .
149
                    '">' . $contextname . '</a>';
150
        }
151
 
152
        // Put the scores in the table.
153
        $numvisible = $count->numquestions - $count->numhidden;
154
        $table->data[] = array(
155
                $contextname,
156
                $count->numquestions,
157
                $numvisible,
158
                $count->numhidden);
159
 
160
        // Update the totals.
161
        $totalquestions += $count->numquestions;
162
        $totalvisible += $numvisible;
163
        $totalhidden += $count->numhidden;
164
    }
165
 
166
    // Add a totals row.
167
    $table->data[] = array(
168
            '<b>' . get_string('total') . '</b>',
169
            $totalquestions,
170
            $totalvisible,
171
            $totalhidden);
172
 
173
    // Print it.
174
    echo html_writer::table($table);
175
}
176
 
177
// Footer.
178
echo $OUTPUT->footer();