2 // Allows the admin to manage questiontypes
3 // This page is adapted from modules.php
5 // This page is not yet in service, the whole plug-in architecture
6 // for question types is still under construction.
8 require_once('../config.php');
10 $show = optional_param('show', '', PARAM_SAFEDIR
);
11 $hide = optional_param('hide', '', PARAM_SAFEDIR
);
12 $delete = optional_param('delete', '', PARAM_SAFEDIR
);
13 $confirm = optional_param('confirm', '', PARAM_BOOL
);
18 error("Only administrators can use this page!");
21 if (!$site = get_site()) {
22 error("Site isn't defined!");
28 $stradministration = get_string("administration");
29 $strconfiguration = get_string("configuration");
30 $strdelete = get_string("delete");
31 $strversion = get_string("version");
32 $strhide = get_string("hide");
33 $strshow = get_string("show");
34 $strsettings = get_string("settings");
35 $strquestions = get_string("questions");
36 $strquestiontype = get_string("questiontype", 'quiz');
38 print_header("$site->shortname: $strquestions", "$site->fullname",
39 "<a href=\"index.php\">$stradministration</a> -> ".
40 "<a href=\"configure.php\">$strconfiguration</a> -> $strquestions");
42 print_heading($strquestions);
45 /// If data submitted, then process and store.
47 if (!empty($hide) and confirm_sesskey()) {
48 if (!$qtype = get_record("question_types", "name", $hide)) {
49 error("Question type doesn't exist!");
51 set_field("question_types", "visible", "0", "id", $qtype->id
); // Hide question type
54 if (!empty($show) and confirm_sesskey()) {
55 if (!$qtype = get_record("question_types", "name", $show)) {
56 error("Question type doesn't exist!");
58 set_field("question_types", "visible", "1", "id", $qtype->id
); // Show question type
61 if (!empty($delete) and confirm_sesskey()) {
63 $strqtypename = get_string("qtypename", "qtype_$delete");
66 notice_yesno(get_string("qtypedeleteconfirm", "admin", $strqtypename),
67 "questiontypes.php?delete=$delete&confirm=1&sesskey=$USER->sesskey",
72 } else { // Delete everything!!
74 if ($delete == "random") {
75 error("You can not delete the random question type!!");
78 if (!$qtype = get_record("question_types", "name", $delete)) {
79 error("Question type doesn't exist!");
82 // OK, first delete all the questions
83 if ($questions = get_records("quiz_questions", "qtype", $qtype->id
)) {
84 foreach ($questions as $question) {
85 if (! quiz_delete_question($coursemod->id
, $coursemod->section
)) {
86 notify("Could not delete the $strqtypename with id = $question->id");
91 // And the qtype entry itself
92 if (!delete_records("question_types", "name", $qtype->name
)) {
93 notify("Error occurred while deleting the $strqtypename record from question_types table");
96 // Then the tables themselves
98 if ($tables = $db->Metatables()) {
99 $prefix = $CFG->prefix
.$qtype->name
;
100 foreach ($tables as $table) {
101 if (strpos($table, $prefix) === 0) {
102 if (!execute_sql("DROP TABLE $table", false)) {
103 notify("ERROR: while trying to drop table $table");
109 $a->qtype
= $strqtypename;
110 $a->directory
= "$CFG->dirroot/qtype/$delete";
111 notice(get_string("qtypedeletefiles", "", $a), "questiontypes.php");
115 /// Get and sort the existing questiontypes
117 if (!$qtypes = get_records("question_types")) {
118 error("No question types found!!"); // Should never happen
121 foreach ($qtypes as $qtype) {
122 $strqtypename = get_string("qtypename", "qtype_$qtype->name");
123 $qtypebyname[$strqtypename] = $qtype;
127 /// Print the table of all questiontypes
129 $table->head
= array ($strquestiontype, $strquestions, $strversion, "$strhide/$strshow", $strdelete, $strsettings);
130 $table->align
= array ("left", "right", "left", "center", "center", "center");
131 $table->wrap
= array ("nowrap", "", "", "", "","");
132 $table->size
= array ("100%", "10", "10", "10", "10","12");
133 $table->width
= "100";
135 foreach ($qtypebyname as $qtypename => $qtype) {
137 $icon = "<img src=\"$CFG->dirroot/mod/quiz/questiontypes/$qtype->name/icon.gif\" hspace=\"10\" height=\"16\" width=\"16\" border=\"0\" alt=\"\" />";
139 if (file_exists("$CFG->dirroot/mod/quiz/questiontypes/$qtype->name/config.html")) {
140 $settings = "<a href=\"questiontype.php?qtype=$qtype->name\">$strsettings</a>";
145 $count = count_records('quiz_questions', 'qtype', $qtype->id
);
147 $delete = $count ?
'' : "<a href=\"questiontypes.php?delete=$qtype->name&sesskey=$USER->sesskey\">$strdelete</a>";
149 if ($qtype->visible
) {
150 $visible = "<a href=\"questiontypes.php?hide=$qtype->name&sesskey=$USER->sesskey\" title=\"$strhide\">".
151 "<img src=\"$CFG->pixpath/i/hide.gif\" align=\"middle\" height=\"16\" width=\"16\" border=\"0\" alt=\"\" /></a>";
154 $visible = "<a href=\"questiontypes.php?show=$qtype->name&sesskey=$USER->sesskey\" title=\"$strshow\">".
155 "<img src=\"$CFG->pixpath/i/show.gif\" align=\"middle\" height=\"16\" width=\"16\" border=\"0\" alt=\"\" /></a>";
156 $class = "class=\"dimmed_text\"";
158 if ($qtype->name
== "random") {
163 $table->data
[] = array ("<span $class>$icon $qtypename</span>", $count, $qtype->version
, $visible, $delete, $settings);