|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00039 admin_externalpage_setup('manageqbehaviours'); 00040 $thispageurl = new moodle_url('/admin/qbehaviours.php'); 00041 00042 $behaviours = get_plugin_list('qbehaviour'); 00043 $pluginmanager = plugin_manager::instance(); 00044 00045 // Get some data we will need - question counts and which types are needed. 00046 $counts = $DB->get_records_sql_menu(" 00047 SELECT behaviour, COUNT(1) 00048 FROM {question_attempts} GROUP BY behaviour"); 00049 $needed = array(); 00050 $archetypal = array(); 00051 foreach ($behaviours as $behaviour => $notused) { 00052 if (!array_key_exists($behaviour, $counts)) { 00053 $counts[$behaviour] = 0; 00054 } 00055 $needed[$behaviour] = ($counts[$behaviour] > 0) || 00056 $pluginmanager->other_plugins_that_require('qbehaviour_' . $behaviour); 00057 $archetypal[$behaviour] = question_engine::is_behaviour_archetypal($behaviour); 00058 } 00059 foreach ($counts as $behaviour => $count) { 00060 if (!array_key_exists($behaviour, $behaviours)) { 00061 $counts['missing'] += $count; 00062 } 00063 } 00064 $needed['missing'] = true; 00065 00066 // Work of the correct sort order. 00067 $config = get_config('question'); 00068 $sortedbehaviours = array(); 00069 foreach ($behaviours as $behaviour => $notused) { 00070 $sortedbehaviours[$behaviour] = question_engine::get_behaviour_name($behaviour); 00071 } 00072 if (!empty($config->behavioursortorder)) { 00073 $sortedbehaviours = question_engine::sort_behaviours($sortedbehaviours, 00074 $config->behavioursortorder, ''); 00075 } 00076 00077 if (!empty($config->disabledbehaviours)) { 00078 $disabledbehaviours = explode(',', $config->disabledbehaviours); 00079 } else { 00080 $disabledbehaviours = array(); 00081 } 00082 00083 // Process actions ============================================================ 00084 00085 // Disable. 00086 if (($disable = optional_param('disable', '', PARAM_PLUGIN)) && confirm_sesskey()) { 00087 if (!isset($behaviours[$disable])) { 00088 print_error('unknownbehaviour', 'question', $thispageurl, $disable); 00089 } 00090 00091 if (array_search($disable, $disabledbehaviours) === false) { 00092 $disabledbehaviours[] = $disable; 00093 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question'); 00094 } 00095 redirect($thispageurl); 00096 } 00097 00098 // Enable. 00099 if (($enable = optional_param('enable', '', PARAM_PLUGIN)) && confirm_sesskey()) { 00100 if (!isset($behaviours[$enable])) { 00101 print_error('unknownbehaviour', 'question', $thispageurl, $enable); 00102 } 00103 00104 if (!$archetypal[$enable]) { 00105 print_error('cannotenablebehaviour', 'question', $thispageurl, $enable); 00106 } 00107 00108 if (($key = array_search($enable, $disabledbehaviours)) !== false) { 00109 unset($disabledbehaviours[$key]); 00110 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question'); 00111 } 00112 redirect($thispageurl); 00113 } 00114 00115 // Move up in order. 00116 if (($up = optional_param('up', '', PARAM_PLUGIN)) && confirm_sesskey()) { 00117 if (!isset($behaviours[$up])) { 00118 print_error('unknownbehaviour', 'question', $thispageurl, $up); 00119 } 00120 00121 // This function works fine for behaviours, as well as qtypes. 00122 $neworder = question_reorder_qtypes($sortedbehaviours, $up, -1); 00123 set_config('behavioursortorder', implode(',', $neworder), 'question'); 00124 redirect($thispageurl); 00125 } 00126 00127 // Move down in order. 00128 if (($down = optional_param('down', '', PARAM_PLUGIN)) && confirm_sesskey()) { 00129 if (!isset($behaviours[$down])) { 00130 print_error('unknownbehaviour', 'question', $thispageurl, $down); 00131 } 00132 00133 // This function works fine for behaviours, as well as qtypes. 00134 $neworder = question_reorder_qtypes($sortedbehaviours, $down, +1); 00135 set_config('behavioursortorder', implode(',', $neworder), 'question'); 00136 redirect($thispageurl); 00137 } 00138 00139 // Delete. 00140 if (($delete = optional_param('delete', '', PARAM_PLUGIN)) && confirm_sesskey()) { 00141 // Check it is OK to delete this question type. 00142 if ($delete == 'missing') { 00143 print_error('cannotdeletemissingbehaviour', 'question', $thispageurl); 00144 } 00145 00146 if (!isset($behaviours[$delete])) { 00147 print_error('unknownbehaviour', 'question', $thispageurl, $delete); 00148 } 00149 00150 $behaviourname = $sortedbehaviours[$delete]; 00151 if ($counts[$delete] > 0) { 00152 print_error('cannotdeletebehaviourinuse', 'question', $thispageurl, $behaviourname); 00153 } 00154 if ($needed[$delete] > 0) { 00155 print_error('cannotdeleteneededbehaviour', 'question', $thispageurl, $behaviourname); 00156 } 00157 00158 // If not yet confirmed, display a confirmation message. 00159 if (!optional_param('confirm', '', PARAM_BOOL)) { 00160 echo $OUTPUT->header(); 00161 echo $OUTPUT->heading(get_string('deletebehaviourareyousure', 'question', $behaviourname)); 00162 echo $OUTPUT->confirm( 00163 get_string('deletebehaviourareyousuremessage', 'question', $behaviourname), 00164 new moodle_url($thispageurl, array('delete' => $delete, 'confirm' => 1)), 00165 $thispageurl); 00166 echo $OUTPUT->footer(); 00167 exit; 00168 } 00169 00170 // Do the deletion. 00171 echo $OUTPUT->header(); 00172 echo $OUTPUT->heading(get_string('deletingbehaviour', 'question', $behaviourname)); 00173 00174 // Delete any configuration records. 00175 if (!unset_all_config_for_plugin('qbehaviour_' . $delete)) { 00176 echo $OUTPUT->notification(get_string('errordeletingconfig', 'admin', 'qbehaviour_' . $delete)); 00177 } 00178 if (($key = array_search($delete, $disabledbehaviours)) !== false) { 00179 unset($disabledbehaviours[$key]); 00180 set_config('disabledbehaviours', implode(',', $disabledbehaviours), 'question'); 00181 } 00182 $behaviourorder = array_keys($sortedbehaviours); 00183 if (($key = array_search($delete, $behaviourorder)) !== false) { 00184 unset($behaviourorder[$key]); 00185 set_config('behavioursortorder', implode(',', $behaviourorder), 'question'); 00186 } 00187 00188 // Then the tables themselves 00189 drop_plugin_tables($delete, get_plugin_directory('qbehaviour', $delete) . '/db/install.xml', false); 00190 00191 // Remove event handlers and dequeue pending events 00192 events_uninstall('qbehaviour_' . $delete); 00193 00194 $a = new stdClass(); 00195 $a->behaviour = $behaviourname; 00196 $a->directory = get_plugin_directory('qbehaviour', $delete); 00197 echo $OUTPUT->box(get_string('qbehaviourdeletefiles', 'question', $a), 'generalbox', 'notice'); 00198 echo $OUTPUT->continue_button($thispageurl); 00199 echo $OUTPUT->footer(); 00200 exit; 00201 } 00202 00203 // End of process actions ================================================== 00204 00205 // Print the page heading. 00206 echo $OUTPUT->header(); 00207 echo $OUTPUT->heading(get_string('manageqbehaviours', 'admin')); 00208 00209 // Set up the table. 00210 $table = new flexible_table('qbehaviouradmintable'); 00211 $table->define_baseurl($thispageurl); 00212 $table->define_columns(array('behaviour', 'numqas', 'version', 'requires', 00213 'available', 'delete')); 00214 $table->define_headers(array(get_string('behaviour', 'question'), get_string('numqas', 'question'), 00215 get_string('version'), get_string('requires', 'admin'), 00216 get_string('availableq', 'question'), get_string('delete'))); 00217 $table->set_attribute('id', 'qbehaviours'); 00218 $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide'); 00219 $table->setup(); 00220 00221 // Add a row for each question type. 00222 foreach ($sortedbehaviours as $behaviour => $behaviourname) { 00223 $row = array(); 00224 00225 // Question icon and name. 00226 $row[] = $behaviourname; 00227 00228 // Count 00229 $row[] = $counts[$behaviour]; 00230 00231 // Question version number. 00232 $version = get_config('qbehaviour_' . $behaviour, 'version'); 00233 if ($version) { 00234 $row[] = $version; 00235 } else { 00236 $row[] = html_writer::tag('span', get_string('nodatabase', 'admin'), array('class' => 'disabled')); 00237 } 00238 00239 // Other question types required by this one. 00240 $plugin = $pluginmanager->get_plugin_info('qbehaviour_' . $behaviour); 00241 $required = $plugin->get_other_required_plugins(); 00242 if (!empty($required)) { 00243 $strrequired = array(); 00244 foreach ($required as $component => $notused) { 00245 $strrequired[] = $pluginmanager->plugin_name($component); 00246 } 00247 $row[] = implode(', ', $strrequired); 00248 } else { 00249 $row[] = ''; 00250 } 00251 00252 // Are people allowed to select this behaviour? 00253 $rowclass = ''; 00254 if ($archetypal[$behaviour]) { 00255 $enabled = array_search($behaviour, $disabledbehaviours) === false; 00256 $icons = question_behaviour_enable_disable_icons($behaviour, $enabled); 00257 if (!$enabled) { 00258 $rowclass = 'dimmed_text'; 00259 } 00260 } else { 00261 $icons = $OUTPUT->spacer() . ' '; 00262 } 00263 00264 // Move icons. 00265 $icons .= question_behaviour_icon_html('up', $behaviour, 't/up', get_string('up'), null); 00266 $icons .= question_behaviour_icon_html('down', $behaviour, 't/down', get_string('down'), null); 00267 $row[] = $icons; 00268 00269 // Delete link, if available. 00270 if ($needed[$behaviour]) { 00271 $row[] = ''; 00272 } else { 00273 $row[] = html_writer::link(new moodle_url($thispageurl, 00274 array('delete' => $behaviour, 'sesskey' => sesskey())), get_string('delete'), 00275 array('title' => get_string('uninstallbehaviour', 'question'))); 00276 } 00277 00278 $table->add_data($row, $rowclass); 00279 } 00280 00281 $table->finish_output(); 00282 00283 echo $OUTPUT->footer(); 00284 00285 function question_behaviour_enable_disable_icons($behaviour, $enabled) { 00286 if ($enabled) { 00287 return question_behaviour_icon_html('disable', $behaviour, 'i/hide', 00288 get_string('enabled', 'question'), get_string('disable')); 00289 } else { 00290 return question_behaviour_icon_html('enable', $behaviour, 'i/show', 00291 get_string('disabled', 'question'), get_string('enable')); 00292 } 00293 } 00294 00295 function question_behaviour_icon_html($action, $behaviour, $icon, $alt, $tip) { 00296 global $OUTPUT; 00297 return $OUTPUT->action_icon(new moodle_url('/admin/qbehaviours.php', 00298 array($action => $behaviour, 'sesskey' => sesskey())), 00299 new pix_icon($icon, $alt, 'moodle', array('title' => '')), 00300 null, array('title' => $tip)) . ' '; 00301 } 00302