weekly release 4.0dev
[moodle.git] / report / questioninstances / index.php
CommitLineData
244bb396 1<?php
a54078f6
PS
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
3041a0df 17/**
20486a5a 18 * For a given question type, list the number of
3041a0df 19 *
a54078f6
PS
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
3041a0df 24 */
25
1fcf0ca8 26require(__DIR__.'/../../config.php');
3041a0df 27require_once($CFG->libdir.'/adminlib.php');
28require_once($CFG->libdir.'/questionlib.php');
29
3041a0df 30// Get URL parameters.
574a5e53 31$requestedqtype = optional_param('qtype', '', PARAM_SAFEDIR);
3041a0df 32
d994ced2 33// Print the header & check permissions.
98dd8e72 34admin_externalpage_setup('reportquestioninstances', '', null, '', array('pagelayout'=>'report'));
61ef8f9f 35echo $OUTPUT->header();
3041a0df 36
d994ced2 37// Log.
f40d61c1 38\report_questioninstances\event\report_viewed::create(array('other' => array('requestedqtype' => $requestedqtype)))->trigger();
d994ced2 39
eab8ed9f 40// Prepare the list of capabilities to choose from
d649fb02 41$qtypes = question_bank::get_all_qtypes();
3041a0df 42$qtypechoices = array();
d649fb02 43foreach ($qtypes as $qtype) {
3041a0df 44 $qtypechoices[$qtype->name()] = $qtype->local_name();
45}
46
47// Print the settings form.
20486a5a 48echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
3041a0df 49echo '<form method="get" action="." id="settingsform"><div>';
2fff8846 50echo $OUTPUT->heading(get_string('reportsettings', 'report_questioninstances'));
3041a0df 51echo '<p id="intro">', get_string('intro', 'report_questioninstances') , '</p>';
52echo '<p><label for="menuqtype"> ' . get_string('questiontype', 'admin') . '</label> ';
d776d59e 53echo html_writer::select($qtypechoices, 'qtype', $requestedqtype, array('_all_'=>get_string('all')));
3041a0df 54echo '</p>';
270b2e44 55echo '<p><input type="submit" class="btn btn-secondary" id="settingssubmit" value="' .
3041a0df 56 get_string('getreport', 'report_questioninstances') . '" /></p>';
57echo '</div></form>';
20486a5a 58echo $OUTPUT->box_end();
3041a0df 59
60// If we have a qtype to report on, generate the report.
61if ($requestedqtype) {
62
63 // Work out the bits needed for the SQL WHERE clauses.
64 if ($requestedqtype == 'missingtype') {
574a5e53
TH
65 $title = get_string('reportformissingqtypes', 'report_questioninstances');
66
d649fb02 67 $othertypes = array_keys($qtypes);
3041a0df 68 $key = array_search('missingtype', $othertypes);
69 unset($othertypes[$key]);
bdff6b85 70 list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
71 $sqlqtypetest = 'WHERE qtype ' . $sqlqtypetest;
574a5e53 72
bdff6b85 73 } else if ($requestedqtype == '_all_') {
574a5e53
TH
74 $title = get_string('reportforallqtypes', 'report_questioninstances');
75
bdff6b85 76 $sqlqtypetest = '';
77 $params = array();
574a5e53 78
3041a0df 79 } else {
d649fb02
TH
80 $title = get_string('reportforqtype', 'report_questioninstances',
81 question_bank::get_qtype($requestedqtype)->local_name());
574a5e53
TH
82
83 $sqlqtypetest = 'WHERE qtype = ?';
84 $params = array($requestedqtype);
3041a0df 85 }
86
bdff6b85 87 // Get the question counts, and all the context information, for each
88 // context. That is, rows of these results can be used as $context objects.
e922fe23
PS
89 $ctxpreload = context_helper::get_preload_record_columns_sql('con');
90 $ctxgroupby = implode(',', array_keys(context_helper::get_preload_record_columns('con')));
3041a0df 91 $counts = $DB->get_records_sql("
e922fe23 92 SELECT qc.contextid, count(1) as numquestions, sum(hidden) as numhidden, $ctxpreload
3041a0df 93 FROM {question} q
94 JOIN {question_categories} qc ON q.category = qc.id
95 JOIN {context} con ON con.id = qc.contextid
bdff6b85 96 $sqlqtypetest
7dcb9b06 97 AND (q.parent = 0 OR q.parent = q.id)
e922fe23 98 GROUP BY qc.contextid, $ctxgroupby
bdff6b85 99 ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
3041a0df 100
101 // Print the report heading.
2fff8846 102 echo $OUTPUT->heading($title);
3041a0df 103
bdff6b85 104 // Initialise the table.
414a4a91 105 $table = new html_table();
bdff6b85 106 $table->head = array(
107 get_string('context', 'role'),
108 get_string('totalquestions', 'report_questioninstances'),
109 get_string('visiblequestions', 'report_questioninstances'),
110 get_string('hiddenquestions', 'report_questioninstances'));
111 $table->data = array();
112 $table->class = '';
113 $table->id = '';
114
115 // Add the data for each row.
116 $totalquestions = 0;
117 $totalvisible = 0;
118 $totalhidden = 0;
119 foreach ($counts as $count) {
120 // Work out a link for editing questions in this context.
e922fe23
PS
121 context_helper::preload_from_record($count);
122 $context = context::instance_by_id($count->contextid);
123 $contextname = $context->get_context_name();
124 $url = question_edit_url($context);
bdff6b85 125 if ($url) {
126 $contextname = '<a href="' . $url . '" title="' .
127 get_string('editquestionshere', 'report_questioninstances') .
128 '">' . $contextname . '</a>';
129 }
130
131 // Put the scores in the table.
132 $numvisible = $count->numquestions - $count->numhidden;
133 $table->data[] = array(
134 $contextname,
135 $count->numquestions,
136 $numvisible,
137 $count->numhidden);
138
139 // Update the totals.
140 $totalquestions += $count->numquestions;
141 $totalvisible += $numvisible;
142 $totalhidden += $count->numhidden;
143 }
144
145 // Add a totals row.
146 $table->data[] = array(
147 '<b>' . get_string('total') . '</b>',
148 $totalquestions,
149 $totalvisible,
150 $totalhidden);
151
152 // Print it.
16be8974 153 echo html_writer::table($table);
3041a0df 154}
155
156// Footer.
73d6f52f 157echo $OUTPUT->footer();