Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/qtypes.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00028 require_once(dirname(__FILE__) . '/../config.php');
00029 require_once($CFG->libdir . '/questionlib.php');
00030 require_once($CFG->libdir . '/adminlib.php');
00031 require_once($CFG->libdir . '/pluginlib.php');
00032 require_once($CFG->libdir . '/tablelib.php');
00033 
00034 // Check permissions.
00035 require_login();
00036 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00037 require_capability('moodle/question:config', $systemcontext);
00038 $canviewreports = has_capability('report/questioninstances:view', $systemcontext);
00039 
00040 admin_externalpage_setup('manageqtypes');
00041 $thispageurl = new moodle_url('/admin/qtypes.php');
00042 
00043 $qtypes = question_bank::get_all_qtypes();
00044 $pluginmanager = plugin_manager::instance();
00045 
00046 // Get some data we will need - question counts and which types are needed.
00047 $counts = $DB->get_records_sql("
00048         SELECT qtype, COUNT(1) as numquestions, SUM(hidden) as numhidden
00049         FROM {question} GROUP BY qtype", array());
00050 $needed = array();
00051 foreach ($qtypes as $qtypename => $qtype) {
00052     if (!isset($counts[$qtypename])) {
00053         $counts[$qtypename] = new stdClass;
00054         $counts[$qtypename]->numquestions = 0;
00055         $counts[$qtypename]->numhidden = 0;
00056     }
00057     $needed[$qtypename] = $counts[$qtypename]->numquestions > 0 ||
00058             $pluginmanager->other_plugins_that_require($qtype->plugin_name());
00059     $counts[$qtypename]->numquestions -= $counts[$qtypename]->numhidden;
00060 }
00061 $needed['missingtype'] = true; // The system needs the missing question type.
00062 foreach ($counts as $qtypename => $count) {
00063     if (!isset($qtypes[$qtypename])) {
00064         $counts['missingtype']->numquestions += $count->numquestions - $count->numhidden;
00065         $counts['missingtype']->numhidden += $count->numhidden;
00066     }
00067 }
00068 
00069 // Work of the correct sort order.
00070 $config = get_config('question');
00071 $sortedqtypes = array();
00072 foreach ($qtypes as $qtypename => $qtype) {
00073     $sortedqtypes[$qtypename] = $qtype->local_name();
00074 }
00075 $sortedqtypes = question_bank::sort_qtype_array($sortedqtypes, $config);
00076 
00077 // Process actions ============================================================
00078 
00079 // Disable.
00080 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
00081     if (!isset($qtypes[$disable])) {
00082         print_error('unknownquestiontype', 'question', $thispageurl, $disable);
00083     }
00084 
00085     set_config($disable . '_disabled', 1, 'question');
00086     redirect($thispageurl);
00087 }
00088 
00089 // Enable.
00090 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) {
00091     if (!isset($qtypes[$enable])) {
00092         print_error('unknownquestiontype', 'question', $thispageurl, $enable);
00093     }
00094 
00095     if (!$qtypes[$enable]->menu_name()) {
00096         print_error('cannotenable', 'question', $thispageurl, $enable);
00097     }
00098 
00099     unset_config($enable . '_disabled', 'question');
00100     redirect($thispageurl);
00101 }
00102 
00103 // Move up in order.
00104 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) {
00105     if (!isset($qtypes[$up])) {
00106         print_error('unknownquestiontype', 'question', $thispageurl, $up);
00107     }
00108 
00109     $neworder = question_reorder_qtypes($sortedqtypes, $up, -1);
00110     question_save_qtype_order($neworder, $config);
00111     redirect($thispageurl);
00112 }
00113 
00114 // Move down in order.
00115 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) {
00116     if (!isset($qtypes[$down])) {
00117         print_error('unknownquestiontype', 'question', $thispageurl, $down);
00118     }
00119 
00120     $neworder = question_reorder_qtypes($sortedqtypes, $down, +1);
00121     question_save_qtype_order($neworder, $config);
00122     redirect($thispageurl);
00123 }
00124 
00125 // Delete.
00126 if (($delete = optional_param('delete', '', PARAM_PLUGIN)) && confirm_sesskey()) {
00127     // Check it is OK to delete this question type.
00128     if ($delete == 'missingtype') {
00129         print_error('cannotdeletemissingqtype', 'question', $thispageurl);
00130     }
00131 
00132     if (!isset($qtypes[$delete])) {
00133         print_error('unknownquestiontype', 'question', $thispageurl, $delete);
00134     }
00135 
00136     $qtypename = $qtypes[$delete]->local_name();
00137     if ($counts[$delete]->numquestions + $counts[$delete]->numhidden > 0) {
00138         print_error('cannotdeleteqtypeinuse', 'question', $thispageurl, $qtypename);
00139     }
00140 
00141     if ($needed[$delete] > 0) {
00142         print_error('cannotdeleteqtypeneeded', 'question', $thispageurl, $qtypename);
00143     }
00144 
00145     // If not yet confirmed, display a confirmation message.
00146     if (!optional_param('confirm', '', PARAM_BOOL)) {
00147         $qtypename = $qtypes[$delete]->local_name();
00148         echo $OUTPUT->header();
00149         echo $OUTPUT->heading(get_string('deleteqtypeareyousure', 'question', $qtypename));
00150         echo $OUTPUT->confirm(get_string('deleteqtypeareyousuremessage', 'question', $qtypename),
00151                 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)),
00152                 $thispageurl);
00153         echo $OUTPUT->footer();
00154         exit;
00155     }
00156 
00157     // Do the deletion.
00158     echo $OUTPUT->header();
00159     echo $OUTPUT->heading(get_string('deletingqtype', 'question', $qtypename));
00160 
00161     // Delete any configuration records.
00162     if (!unset_all_config_for_plugin('qtype_' . $delete)) {
00163         echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qtype_' . $delete));
00164     }
00165     unset_config($delete . '_disabled', 'question');
00166     unset_config($delete . '_sortorder', 'question');
00167 
00168     // Then the tables themselves
00169     drop_plugin_tables($delete, $qtypes[$delete]->plugin_dir() . '/db/install.xml', false);
00170 
00171     // Remove event handlers and dequeue pending events
00172     events_uninstall('qtype_' . $delete);
00173 
00174     $a = new stdClass();
00175     $a->qtype = $qtypename;
00176     $a->directory = $qtypes[$delete]->plugin_dir();
00177     echo $OUTPUT->box(get_string('qtypedeletefiles', 'question', $a), 'generalbox', 'notice');
00178     echo $OUTPUT->continue_button($thispageurl);
00179     echo $OUTPUT->footer();
00180     exit;
00181 }
00182 
00183 // End of process actions ==================================================
00184 
00185 // Print the page heading.
00186 echo $OUTPUT->header();
00187 echo $OUTPUT->heading(get_string('manageqtypes', 'admin'));
00188 
00189 // Set up the table.
00190 $table = new flexible_table('qtypeadmintable');
00191 $table->define_baseurl($thispageurl);
00192 $table->define_columns(array('questiontype', 'numquestions', 'version', 'requires',
00193         'availableto', 'delete', 'settings'));
00194 $table->define_headers(array(get_string('questiontype', 'question'), get_string('numquestions', 'question'),
00195         get_string('version'), get_string('requires', 'admin'), get_string('availableq', 'question'),
00196         get_string('delete'), get_string('settings')));
00197 $table->set_attribute('id', 'qtypes');
00198 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
00199 $table->setup();
00200 
00201 // Add a row for each question type.
00202 $createabletypes = question_bank::get_creatable_qtypes();
00203 foreach ($sortedqtypes as $qtypename => $localname) {
00204     $qtype = $qtypes[$qtypename];
00205     $row = array();
00206 
00207     // Question icon and name.
00208     $fakequestion = new stdClass;
00209     $fakequestion->qtype = $qtypename;
00210     $icon = print_question_icon($fakequestion, true);
00211     $row[] = $icon . ' ' . $localname;
00212 
00213     // Number of questions of this type.
00214     if ($counts[$qtypename]->numquestions + $counts[$qtypename]->numhidden > 0) {
00215         if ($counts[$qtypename]->numhidden > 0) {
00216             $strcount = get_string('numquestionsandhidden', 'question', $counts[$qtypename]);
00217         } else {
00218             $strcount = $counts[$qtypename]->numquestions;
00219         }
00220         if ($canviewreports) {
00221             $row[] = html_writer::link(new moodle_url('/report/questioninstances/index.php',
00222                     array('qtype' => $qtypename)), $strcount, array('title' => get_string('showdetails', 'admin')));
00223         } else {
00224             $strcount;
00225         }
00226     } else {
00227         $row[] = 0;
00228     }
00229 
00230     // Question version number.
00231     $version = get_config('qtype_' . $qtypename, 'version');
00232     if ($version) {
00233         $row[] = $version;
00234     } else {
00235         $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled'));
00236     }
00237 
00238     // Other question types required by this one.
00239     $plugin = $pluginmanager->get_plugin_info($qtype->plugin_name());
00240     $requiredtypes = $plugin->get_other_required_plugins();
00241     $strtypes = array();
00242     if (!empty($requiredtypes)) {
00243         foreach ($requiredtypes as $required => $notused) {
00244             $strtypes[] = $pluginmanager->plugin_name($required);
00245         }
00246         $row[] = implode(', ', $strtypes);
00247     } else {
00248         $row[] = '';
00249     }
00250 
00251     // Are people allowed to create new questions of this type?
00252     $rowclass = '';
00253     if ($qtype->menu_name()) {
00254         $createable = isset($createabletypes[$qtypename]);
00255         $icons = question_types_enable_disable_icons($qtypename, $createable);
00256         if (!$createable) {
00257             $rowclass = 'dimmed_text';
00258         }
00259     } else {
00260         $icons = $OUTPUT->spacer() . ' ';
00261     }
00262 
00263     // Move icons.
00264     $icons .= question_type_icon_html('up', $qtypename, 't/up', get_string('up'), '');
00265     $icons .= question_type_icon_html('down', $qtypename, 't/down', get_string('down'), '');
00266     $row[] = $icons;
00267 
00268     // Delete link, if available.
00269     if ($needed[$qtypename]) {
00270         $row[] = '';
00271     } else {
00272         $row[] = html_writer::link(new moodle_url($thispageurl,
00273                 array('delete' => $qtypename, 'sesskey' => sesskey())), get_string('delete'),
00274                 array('title' => get_string('uninstallqtype', 'question')));
00275     }
00276 
00277     // Settings link, if available.
00278     $settings = admin_get_root()->locate('qtypesetting' . $qtypename);
00279     if ($settings instanceof admin_externalpage) {
00280         $row[] = html_writer::link($settings->url, get_string('settings'));
00281     } else if ($settings instanceof admin_settingpage) {
00282         $row[] = html_writer::link(new moodle_url('/admin/settings.php',
00283                 array('section' => 'qtypesetting' . $qtypename)), get_string('settings'));
00284     } else {
00285         $row[] = '';
00286     }
00287 
00288     $table->add_data($row, $rowclass);
00289 }
00290 
00291 $table->finish_output();
00292 
00293 echo $OUTPUT->footer();
00294 
00295 function question_types_enable_disable_icons($qtypename, $createable) {
00296     if ($createable) {
00297         return question_type_icon_html('disable', $qtypename, 'i/hide',
00298                 get_string('enabled', 'question'), get_string('disable'));
00299     } else {
00300         return question_type_icon_html('enable', $qtypename, 'i/show',
00301                 get_string('disabled', 'question'), get_string('enable'));
00302     }
00303 }
00304 
00305 function question_type_icon_html($action, $qtypename, $icon, $alt, $tip) {
00306     global $OUTPUT;
00307     return $OUTPUT->action_icon(new moodle_url('/admin/qtypes.php',
00308             array($action => $qtypename, 'sesskey' => sesskey())),
00309             new pix_icon($icon, $alt, 'moodle', array('title' => '')),
00310             null, array('title' => $tip)) . ' ';
00311 }
00312 
 All Data Structures Namespaces Files Functions Variables Enumerations