Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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  echo $OUTPUT->header();
  36  
  37  // Log.
  38  \report_questioninstances\event\report_viewed::create(array('other' => array('requestedqtype' => $requestedqtype)))->trigger();
  39  
  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();
  45  }
  46  
  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" class="btn btn-secondary" id="settingssubmit" value="' .
  56          get_string('getreport', 'report_questioninstances') . '" /></p>';
  57  echo '</div></form>';
  58  echo $OUTPUT->box_end();
  59  
  60  // If we have a qtype to report on, generate the report.
  61  if ($requestedqtype) {
  62  
  63      // Work out the bits needed for the SQL WHERE clauses.
  64      if ($requestedqtype == 'missingtype') {
  65          $title = get_string('reportformissingqtypes', 'report_questioninstances');
  66  
  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;
  72  
  73      } else if ($requestedqtype == '_all_') {
  74          $title = get_string('reportforallqtypes', 'report_questioninstances');
  75  
  76          $sqlqtypetest = '';
  77          $params = array();
  78  
  79      } else {
  80          $title = get_string('reportforqtype', 'report_questioninstances',
  81                  question_bank::get_qtype($requestedqtype)->local_name());
  82  
  83          $sqlqtypetest = 'WHERE qtype = ?';
  84          $params = array($requestedqtype);
  85      }
  86  
  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              AND (q.parent = 0 OR q.parent = q.id)
  98              GROUP BY qc.contextid, $ctxgroupby
  99              ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
 100  
 101      // Print the report heading.
 102      echo $OUTPUT->heading($title);
 103  
 104      // Initialise the table.
 105      $table = new html_table();
 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.
 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);
 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.
 153      echo html_writer::table($table);
 154  }
 155  
 156  // Footer.
 157  echo $OUTPUT->footer();