From bdff6b85d9d02c62844ea6ac8ed09e931667c351 Mon Sep 17 00:00:00 2001
From: tjhunt <tjhunt>
Date: Wed, 10 Sep 2008 05:34:09 +0000
Subject: [PATCH] MDL-16409 - Finish the report.

---
 admin/report/questioninstances/index.php  | 74 +++++++++++++++++++----
 lang/en_utf8/report_questioninstances.php |  6 ++
 lang/en_utf8/role.php                     |  1 +
 lib/questionlib.php                       |  2 +-
 4 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/admin/report/questioninstances/index.php b/admin/report/questioninstances/index.php
index 56e65e03e9..598d4f4eb1 100644
--- a/admin/report/questioninstances/index.php
+++ b/admin/report/questioninstances/index.php
@@ -38,7 +38,7 @@ echo '<form method="get" action="." id="settingsform"><div>';
 print_heading(get_string('reportsettings', 'report_questioninstances'));
 echo '<p id="intro">', get_string('intro', 'report_questioninstances') , '</p>';
 echo '<p><label for="menuqtype"> ' . get_string('questiontype', 'admin') . '</label> ';
-choose_from_menu($qtypechoices, 'qtype', $requestedqtype);
+choose_from_menu($qtypechoices, 'qtype', $requestedqtype, get_string('all'), '', '_all_');
 echo '</p>';
 echo '<p><input type="submit" id="settingssubmit" value="' .
         get_string('getreport', 'report_questioninstances') . '" /></p>';
@@ -53,29 +53,81 @@ if ($requestedqtype) {
         $othertypes = array_keys($QTYPES);
         $key = array_search('missingtype', $othertypes);
         unset($othertypes[$key]);
-        list($sqlqtypetest, $params) = $DB->get_in_or_equals($othertypes, SQL_PARAMS_QM, '', false);
+        list($sqlqtypetest, $params) = $DB->get_in_or_equal($othertypes, SQL_PARAMS_QM, '', false);
+        $sqlqtypetest = 'WHERE qtype ' . $sqlqtypetest;
+        $title = get_string('reportformissingqtypes', 'report_questioninstances');
+    } else if ($requestedqtype == '_all_') {
+        $sqlqtypetest = '';
+        $params = array();
+        $title = get_string('reportforallqtypes', 'report_questioninstances');
     } else {
-        $sqlqtypetest = '= ?';
+        $sqlqtypetest = 'WHERE qtype = ?';
         $params = array($requestedqtype);
+        $title = get_string('reportforqtype', 'report_questioninstances', $QTYPES[$requestedqtype]->local_name());
     }
 
-    // Get all the role_capabilities rows for this capability - that is, all
-    // role definitions, and all role overrides.
+    // Get the question counts, and all the context information, for each
+    // context. That is, rows of these results can be used as $context objects.
     $counts = $DB->get_records_sql("
             SELECT qc.contextid, count(1) as numquestions, sum(hidden) as numhidden, con.id, con.contextlevel, con.instanceid, con.path, con.depth 
             FROM {question} q
             JOIN {question_categories} qc ON q.category = qc.id
             JOIN {context} con ON con.id = qc.contextid
-            WHERE qtype $sqlqtypetest
+            $sqlqtypetest
             GROUP BY contextid, con.id, con.contextlevel, con.instanceid, con.path, con.depth
-            ORDER BY numquestions DESC, numhidden ASC", $params);
+            ORDER BY numquestions DESC, numhidden ASC, con.contextlevel ASC, con.id ASC", $params);
 
     // Print the report heading.
-    print_heading(get_string('reportforqtype', 'report_questioninstances', $QTYPES[$requestedqtype]->local_name()));
+    print_heading($title);
 
-    // Now, print the table of results.
-    // TODO
-    print_object($counts);
+    // Initialise the table.
+    $table = new stdClass;
+    $table->head = array(
+            get_string('context', 'role'),
+            get_string('totalquestions', 'report_questioninstances'),
+            get_string('visiblequestions', 'report_questioninstances'),
+            get_string('hiddenquestions', 'report_questioninstances'));
+    $table->data = array();
+    $table->class = '';
+    $table->id = '';
+
+    // Add the data for each row.
+    $totalquestions = 0;
+    $totalvisible = 0;
+    $totalhidden = 0;
+    foreach ($counts as $count) {
+        // Work out a link for editing questions in this context.
+        $contextname = print_context_name($count);
+        $url = question_edit_url($count);
+        if ($url) {
+            $contextname = '<a href="' . $url . '" title="' .
+                    get_string('editquestionshere', 'report_questioninstances') .
+                    '">' . $contextname . '</a>';
+        }
+
+        // Put the scores in the table.
+        $numvisible = $count->numquestions - $count->numhidden;
+        $table->data[] = array(
+                $contextname,
+                $count->numquestions,
+                $numvisible,
+                $count->numhidden);
+
+        // Update the totals.
+        $totalquestions += $count->numquestions;
+        $totalvisible += $numvisible;
+        $totalhidden += $count->numhidden;
+    }
+
+    // Add a totals row.
+    $table->data[] = array(
+            '<b>' . get_string('total') . '</b>',
+            $totalquestions,
+            $totalvisible,
+            $totalhidden);
+
+    // Print it.
+    print_table($table);
 }
 
 // Footer.
diff --git a/lang/en_utf8/report_questioninstances.php b/lang/en_utf8/report_questioninstances.php
index 009bbb3c5c..a11580cf72 100644
--- a/lang/en_utf8/report_questioninstances.php
+++ b/lang/en_utf8/report_questioninstances.php
@@ -1,8 +1,14 @@
 <?PHP  // $Id$
 
+$string['editquestionshere'] = 'Edit questions in this context';
 $string['getreport'] = 'Get the report';
+$string['hiddenquestions'] = 'Hidden';
 $string['intro'] = 'This report lists all the contexts in the system where there are questions of a particular type.';
+$string['reportforallqtypes'] = 'Report for all question types';
+$string['reportformissingqtypes'] = 'Report for question of unknown types';
 $string['reportforqtype'] = 'Report for question type \'$a\'';
 $string['reportsettings'] = 'Report settings';
 $string['questioninstances'] = 'Question instances';
+$string['totalquestions'] = 'Total';
+$string['visiblequestions'] = 'Visible';
 ?>
diff --git a/lang/en_utf8/role.php b/lang/en_utf8/role.php
index 471ec5212b..5bf339a5f9 100644
--- a/lang/en_utf8/role.php
+++ b/lang/en_utf8/role.php
@@ -25,6 +25,7 @@ $string['category:create'] = 'Create categories';
 $string['category:delete'] = 'Delete categories';
 $string['category:update'] = 'Update categories';
 $string['category:visibility'] = 'See hidden categories';
+$string['context'] = 'Context';
 $string['course:activityvisibility'] = 'Hide/show activities';
 $string['course:bulkmessaging'] = 'Send a message to many people';
 $string['course:create'] = 'Create courses';
diff --git a/lib/questionlib.php b/lib/questionlib.php
index 3c6e2ccd19..269440826a 100644
--- a/lib/questionlib.php
+++ b/lib/questionlib.php
@@ -2191,7 +2191,7 @@ function question_edit_url($context) {
     $baseurl = $CFG->wwwroot . '/question/edit.php?';
     $defaultcategory = question_get_default_category($context->id);
     if ($defaultcategory) {
-        $baseurl .= 'cat=' . $defaultcategory->id . '&amp;';
+        $baseurl .= 'cat=' . $defaultcategory->id . ',' . $context->id . '&amp;';
     }
     switch ($context->contextlevel) {
         case CONTEXT_SYSTEM:
-- 
2.39.5