Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/report/questioninstances/index.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00026 require(dirname(__FILE__).'/../../config.php');
00027 require_once($CFG->libdir.'/adminlib.php');
00028 require_once($CFG->libdir.'/questionlib.php');
00029 
00030 // Get URL parameters.
00031 $requestedqtype = optional_param('qtype', '', PARAM_SAFEDIR);
00032 
00033 // Print the header & check permissions.
00034 admin_externalpage_setup('reportquestioninstances', '', null, '', array('pagelayout'=>'report'));
00035 echo $OUTPUT->header();
00036 
00037 // Log.
00038 add_to_log(SITEID, "admin", "report questioninstances", "report/questioninstances/index.php?qtype=$requestedqtype", $requestedqtype);
00039 
00040 // Prepare the list of capabilities to choose from
00041 $qtypes = question_bank::get_all_qtypes();
00042 $qtypechoices = array();
00043 foreach ($qtypes as $qtype) {
00044     $qtypechoices[$qtype->name()] = $qtype->local_name();
00045 }
00046 
00047 // Print the settings form.
00048 echo $OUTPUT->box_start('generalbox boxwidthwide boxaligncenter centerpara');
00049 echo '<form method="get" action="." id="settingsform"><div>';
00050 echo $OUTPUT->heading(get_string('reportsettings', 'report_questioninstances'));
00051 echo '<p id="intro">', get_string('intro', 'report_questioninstances') , '</p>';
00052 echo '<p><label for="menuqtype"> ' . get_string('questiontype', 'admin') . '</label> ';
00053 echo html_writer::select($qtypechoices, 'qtype', $requestedqtype, array('_all_'=>get_string('all')));
00054 echo '</p>';
00055 echo '<p><input type="submit" id="settingssubmit" value="' .
00056         get_string('getreport', 'report_questioninstances') . '" /></p>';
00057 echo '</div></form>';
00058 echo $OUTPUT->box_end();
00059 
00060 // If we have a qtype to report on, generate the report.
00061 if ($requestedqtype) {
00062 
00063     // Work out the bits needed for the SQL WHERE clauses.
00064     if ($requestedqtype == 'missingtype') {
00065         $title = get_string('reportformissingqtypes', 'report_questioninstances');
00066 
00067         $othertypes = array_keys($qtypes);
00068         $key = array_search('missingtype', $othertypes);
00069         unset($othertypes[$key]);
00070         list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
00071         $sqlqtypetest = 'WHERE qtype ' . $sqlqtypetest;
00072 
00073     } else if ($requestedqtype == '_all_') {
00074         $title = get_string('reportforallqtypes', 'report_questioninstances');
00075 
00076         $sqlqtypetest = '';
00077         $params = array();
00078 
00079     } else {
00080         $title = get_string('reportforqtype', 'report_questioninstances',
00081                 question_bank::get_qtype($requestedqtype)->local_name());
00082 
00083         $sqlqtypetest = 'WHERE qtype = ?';
00084         $params = array($requestedqtype);
00085     }
00086 
00087     // Get the question counts, and all the context information, for each
00088     // context. That is, rows of these results can be used as $context objects.
00089     $ctxpreload = context_helper::get_preload_record_columns_sql('con');
00090     $ctxgroupby = implode(',', array_keys(context_helper::get_preload_record_columns('con')));
00091     $counts = $DB->get_records_sql("
00092             SELECT qc.contextid, count(1) as numquestions, sum(hidden) as numhidden, $ctxpreload
00093             FROM {question} q
00094             JOIN {question_categories} qc ON q.category = qc.id
00095             JOIN {context} con ON con.id = qc.contextid
00096             $sqlqtypetest
00097             GROUP BY qc.contextid, $ctxgroupby
00098             ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
00099 
00100     // Print the report heading.
00101     echo $OUTPUT->heading($title);
00102 
00103     // Initialise the table.
00104     $table = new html_table();
00105     $table->head = array(
00106             get_string('context', 'role'),
00107             get_string('totalquestions', 'report_questioninstances'),
00108             get_string('visiblequestions', 'report_questioninstances'),
00109             get_string('hiddenquestions', 'report_questioninstances'));
00110     $table->data = array();
00111     $table->class = '';
00112     $table->id = '';
00113 
00114     // Add the data for each row.
00115     $totalquestions = 0;
00116     $totalvisible = 0;
00117     $totalhidden = 0;
00118     foreach ($counts as $count) {
00119         // Work out a link for editing questions in this context.
00120         context_helper::preload_from_record($count);
00121         $context = context::instance_by_id($count->contextid);
00122         $contextname = $context->get_context_name();
00123         $url = question_edit_url($context);
00124         if ($url) {
00125             $contextname = '<a href="' . $url . '" title="' .
00126                     get_string('editquestionshere', 'report_questioninstances') .
00127                     '">' . $contextname . '</a>';
00128         }
00129 
00130         // Put the scores in the table.
00131         $numvisible = $count->numquestions - $count->numhidden;
00132         $table->data[] = array(
00133                 $contextname,
00134                 $count->numquestions,
00135                 $numvisible,
00136                 $count->numhidden);
00137 
00138         // Update the totals.
00139         $totalquestions += $count->numquestions;
00140         $totalvisible += $numvisible;
00141         $totalhidden += $count->numhidden;
00142     }
00143 
00144     // Add a totals row.
00145     $table->data[] = array(
00146             '<b>' . get_string('total') . '</b>',
00147             $totalquestions,
00148             $totalvisible,
00149             $totalhidden);
00150 
00151     // Print it.
00152     echo html_writer::table($table);
00153 }
00154 
00155 // Footer.
00156 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations