MDL-31944 feedback: remove deprecated function feedback_get_participants()
[moodle.git] / report / questioninstances / index.php
blob393bfa67f2e5fc325a2b75b8acd8a5db952d8b79
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.
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/>.
17 /**
18 * For a given question type, list the number of
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
26 require(dirname(__FILE__).'/../../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
28 require_once($CFG->libdir.'/questionlib.php');
30 // Get URL parameters.
31 $requestedqtype = optional_param('qtype', '', PARAM_SAFEDIR);
33 // Print the header & check permissions.
34 admin_externalpage_setup('reportquestioninstances', '', null, '', array('pagelayout'=>'report'));
35 echo $OUTPUT->header();
37 // Log.
38 add_to_log(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype);
40 // Prepare the list of capabilities to choose from
41 $qtypes = question_bank::get_all_qtypes();
42 $qtypechoices = array();
43 foreach ($qtypes as $qtype) {
44 $qtypechoices[$qtype->name()] = $qtype->local_name();
47 // Print the settings form.
48 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
49 echo '<form method="get" action="." id="settingsform"><div>';
50 echo $OUTPUT->heading(get_string('reportsettings', 'report_questioninstances'));
51 echo '<p id="intro">', get_string('intro', 'report_questioninstances') , '</p>';
52 echo '<p><label for="menuqtype"> ' . get_string('questiontype', 'admin') . '</label> ';
53 echo html_writer::select($qtypechoices, 'qtype', $requestedqtype, array('_all_'=>get_string('all')));
54 echo '</p>';
55 echo '<p><input type="submit" id="settingssubmit" value="' .
56 get_string('getreport', 'report_questioninstances') . '" /></p>';
57 echo '</div></form>';
58 echo $OUTPUT->box_end();
60 // If we have a qtype to report on, generate the report.
61 if ($requestedqtype) {
63 // Work out the bits needed for the SQL WHERE clauses.
64 if ($requestedqtype == 'missingtype') {
65 $title = get_string('reportformissingqtypes', 'report_questioninstances');
67 $othertypes = array_keys($qtypes);
68 $key = array_search('missingtype', $othertypes);
69 unset($othertypes[$key]);
70 list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
71 $sqlqtypetest = 'WHERE qtype ' . $sqlqtypetest;
73 } else if ($requestedqtype == '_all_') {
74 $title = get_string('reportforallqtypes', 'report_questioninstances');
76 $sqlqtypetest = '';
77 $params = array();
79 } else {
80 $title = get_string('reportforqtype', 'report_questioninstances',
81 question_bank::get_qtype($requestedqtype)->local_name());
83 $sqlqtypetest = 'WHERE qtype = ?';
84 $params = array($requestedqtype);
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.
89 $ctxpreload = context_helper::get_preload_record_columns_sql('con');
90 $ctxgroupby = implode(',', array_keys(context_helper::get_preload_record_columns('con')));
91 $counts = $DB->get_records_sql("
92 SELECT qc.contextid, count(1) as numquestions, sum(hidden) as numhidden, $ctxpreload
93 FROM {question} q
94 JOIN {question_categories} qc ON q.category = qc.id
95 JOIN {context} con ON con.id = qc.contextid
96 $sqlqtypetest
97 GROUP BY qc.contextid, $ctxgroupby
98 ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
100 // Print the report heading.
101 echo $OUTPUT->heading($title);
103 // Initialise the table.
104 $table = new html_table();
105 $table->head = array(
106 get_string('context', 'role'),
107 get_string('totalquestions', 'report_questioninstances'),
108 get_string('visiblequestions', 'report_questioninstances'),
109 get_string('hiddenquestions', 'report_questioninstances'));
110 $table->data = array();
111 $table->class = '';
112 $table->id = '';
114 // Add the data for each row.
115 $totalquestions = 0;
116 $totalvisible = 0;
117 $totalhidden = 0;
118 foreach ($counts as $count) {
119 // Work out a link for editing questions in this context.
120 context_helper::preload_from_record($count);
121 $context = context::instance_by_id($count->contextid);
122 $contextname = $context->get_context_name();
123 $url = question_edit_url($context);
124 if ($url) {
125 $contextname = '<a href="' . $url . '" title="' .
126 get_string('editquestionshere', 'report_questioninstances') .
127 '">' . $contextname . '</a>';
130 // Put the scores in the table.
131 $numvisible = $count->numquestions - $count->numhidden;
132 $table->data[] = array(
133 $contextname,
134 $count->numquestions,
135 $numvisible,
136 $count->numhidden);
138 // Update the totals.
139 $totalquestions += $count->numquestions;
140 $totalvisible += $numvisible;
141 $totalhidden += $count->numhidden;
144 // Add a totals row.
145 $table->data[] = array(
146 '<b>' . get_string('total') . '</b>',
147 $totalquestions,
148 $totalvisible,
149 $totalhidden);
151 // Print it.
152 echo html_writer::table($table);
155 // Footer.
156 echo $OUTPUT->footer();