|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 require_once(dirname(dirname(__FILE__)) . '/config.php'); 00004 require_once($CFG->dirroot . '/repository/lib.php'); 00005 require_once($CFG->libdir . '/adminlib.php'); 00006 00007 $repository = optional_param('repos', '', PARAM_FORMAT); 00008 $action = optional_param('action', '', PARAM_ALPHA); 00009 $sure = optional_param('sure', '', PARAM_ALPHA); 00010 00011 $display = true; // fall through to normal display 00012 00013 $pagename = 'managerepositories'; 00014 00015 if ($action == 'edit') { 00016 $pagename = 'repositorysettings' . $repository; 00017 } else if ($action == 'delete') { 00018 $pagename = 'repositorydelete'; 00019 } else if (($action == 'newon') || ($action == 'newoff')) { 00020 $pagename = 'repositorynew'; 00021 } 00022 00023 // Need to remember this for form 00024 $formaction = $action; 00025 00026 // Check what visibility to show the new repository 00027 if ($action == 'newon') { 00028 $action = 'new'; 00029 $visible = true; 00030 } else if ($action == 'newoff') { 00031 $action = 'new'; 00032 $visible = false; 00033 } 00034 00035 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); 00036 admin_externalpage_setup($pagename); 00037 00038 $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey(); 00039 $baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php'; 00040 00041 $configstr = get_string('manage', 'repository'); 00042 00043 $return = true; 00044 00049 function repository_action_url($repository) { 00050 global $baseurl; 00051 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository)); 00052 } 00053 00054 if (($action == 'edit') || ($action == 'new')) { 00055 $pluginname = ''; 00056 if ($action == 'edit') { 00057 $repositorytype = repository::get_type_by_typename($repository); 00058 $classname = 'repository_' . $repositorytype->get_typename(); 00059 $configs = call_user_func(array($classname, 'get_type_option_names')); 00060 $plugin = $repositorytype->get_typename(); 00061 // looking for instance to edit plugin name 00062 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names')); 00063 if (empty($instanceoptions)) { 00064 $params = array(); 00065 $params['type'] = $plugin; 00066 $instances = repository::get_instances($params); 00067 if ($instance = array_pop($instances)) { 00068 // use the one form db record 00069 $pluginname = $instance->instance->name; 00070 } 00071 } 00072 00073 } else { 00074 $repositorytype = null; 00075 $plugin = $repository; 00076 $typeid = $repository; 00077 } 00078 $PAGE->set_pagetype('admin-repository-' . $plugin); 00079 // display the edit form for this instance 00080 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction)); 00081 $fromform = $mform->get_data(); 00082 00083 //detect if we create a new type without config (in this case if don't want to display a setting page during creation) 00084 $nosettings = false; 00085 if ($action == 'new') { 00086 $adminconfignames = repository::static_function($repository, 'get_type_option_names'); 00087 $nosettings = empty($adminconfignames); 00088 } 00089 // end setup, begin output 00090 00091 if ($mform->is_cancelled()){ 00092 redirect($baseurl); 00093 } else if (!empty($fromform) || $nosettings) { 00094 require_sesskey(); 00095 if ($action == 'edit') { 00096 $settings = array(); 00097 foreach($configs as $config) { 00098 if (!empty($fromform->$config)) { 00099 $settings[$config] = $fromform->$config; 00100 } else { 00101 // if the config name is not appear in $fromform 00102 // empty this config value 00103 $settings[$config] = ''; 00104 } 00105 } 00106 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names'); 00107 if (!empty($instanceoptionnames)) { 00108 if (array_key_exists('enablecourseinstances', $fromform)) { 00109 $settings['enablecourseinstances'] = $fromform->enablecourseinstances; 00110 } 00111 else { 00112 $settings['enablecourseinstances'] = 0; 00113 } 00114 if (array_key_exists('enableuserinstances', $fromform)) { 00115 $settings['enableuserinstances'] = $fromform->enableuserinstances; 00116 } 00117 else { 00118 $settings['enableuserinstances'] = 0; 00119 } 00120 } 00121 $success = $repositorytype->update_options($settings); 00122 } else { 00123 $type = new repository_type($plugin, (array)$fromform, $visible); 00124 $type->create(); 00125 $success = true; 00126 $data = data_submitted(); 00127 } 00128 if ($success) { 00129 // configs saved 00130 redirect($baseurl); 00131 } else { 00132 print_error('instancenotsaved', 'repository', $baseurl); 00133 } 00134 exit; 00135 } else { 00136 echo $OUTPUT->header(); 00137 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin)); 00138 $displaysettingform = true; 00139 if ($action == 'edit') { 00140 $typeoptionnames = repository::static_function($repository, 'get_type_option_names'); 00141 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names'); 00142 if (empty($typeoptionnames) && empty($instanceoptionnames)) { 00143 $displaysettingform = false; 00144 } 00145 } 00146 if ($displaysettingform){ 00147 $OUTPUT->box_start(); 00148 $mform->display(); 00149 $OUTPUT->box_end(); 00150 } 00151 $return = false; 00152 00153 // Display instances list and creation form 00154 if ($action == 'edit') { 00155 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names'); 00156 if (!empty($instanceoptionnames)) { 00157 repository::display_instances_list(get_context_instance(CONTEXT_SYSTEM), $repository); 00158 } 00159 } 00160 } 00161 } else if ($action == 'show') { 00162 if (!confirm_sesskey()) { 00163 print_error('confirmsesskeybad', '', $baseurl); 00164 } 00165 $repositorytype = repository::get_type_by_typename($repository); 00166 if (empty($repositorytype)) { 00167 print_error('invalidplugin', 'repository', '', $repository); 00168 } 00169 $repositorytype->update_visibility(true); 00170 $return = true; 00171 } else if ($action == 'hide') { 00172 if (!confirm_sesskey()) { 00173 print_error('confirmsesskeybad', '', $baseurl); 00174 } 00175 $repositorytype = repository::get_type_by_typename($repository); 00176 if (empty($repositorytype)) { 00177 print_error('invalidplugin', 'repository', '', $repository); 00178 } 00179 $repositorytype->update_visibility(false); 00180 $return = true; 00181 } else if ($action == 'delete') { 00182 $repositorytype = repository::get_type_by_typename($repository); 00183 if ($sure) { 00184 $PAGE->set_pagetype('admin-repository-' . $repository); 00185 if (!confirm_sesskey()) { 00186 print_error('confirmsesskeybad', '', $baseurl); 00187 } 00188 if ($repositorytype->delete()) { 00189 redirect($baseurl); 00190 } else { 00191 print_error('instancenotdeleted', 'repository', $baseurl); 00192 } 00193 exit; 00194 } else { 00195 echo $OUTPUT->header(); 00196 echo $OUTPUT->confirm(get_string('confirmremove', 'repository', $repositorytype->get_readablename()), $sesskeyurl . '&action=delete&repos=' . $repository . '&sure=yes', $baseurl); 00197 $return = false; 00198 } 00199 } else if ($action == 'moveup') { 00200 $repositorytype = repository::get_type_by_typename($repository); 00201 $repositorytype->move_order('up'); 00202 } else if ($action == 'movedown') { 00203 $repositorytype = repository::get_type_by_typename($repository); 00204 $repositorytype->move_order('down'); 00205 } else { 00206 // If page is loaded directly 00207 echo $OUTPUT->header(); 00208 echo $OUTPUT->heading(get_string('manage', 'repository')); 00209 00210 // Get strings that are used 00211 $strshow = get_string('on', 'repository'); 00212 $strhide = get_string('off', 'repository'); 00213 $strdelete = get_string('disabled', 'repository'); 00214 00215 $actionchoicesforexisting = array( 00216 'show' => $strshow, 00217 'hide' => $strhide, 00218 'delete' => $strdelete 00219 ); 00220 00221 $actionchoicesfornew = array( 00222 'newon' => $strshow, 00223 'newoff' => $strhide, 00224 'delete' => $strdelete 00225 ); 00226 00227 $output = ''; 00228 $output .= $OUTPUT->box_start('generalbox'); 00229 00230 // Set strings that are used multiple times 00231 $settingsstr = get_string('settings'); 00232 $disablestr = get_string('disable'); 00233 00234 // Table to list plug-ins 00235 $table = new html_table(); 00236 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr); 00237 $table->align = array('left', 'center', 'center', 'center', 'center'); 00238 $table->data = array(); 00239 00240 // Get list of used plug-ins 00241 $instances = repository::get_types(); 00242 if (!empty($instances)) { 00243 // Array to store plugins being used 00244 $alreadyplugins = array(); 00245 $totalinstances = count($instances); 00246 $updowncount = 1; 00247 foreach ($instances as $i) { 00248 $settings = ''; 00249 $typename = $i->get_typename(); 00250 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config) 00251 $typeoptionnames = repository::static_function($typename, 'get_type_option_names'); 00252 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names'); 00253 00254 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) { 00255 // Calculate number of instances in order to display them for the Moodle administrator 00256 if (!empty($instanceoptionnames)) { 00257 $params = array(); 00258 $params['context'] = array(get_system_context()); 00259 $params['onlyvisible'] = false; 00260 $params['type'] = $typename; 00261 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params)); 00262 // site instances 00263 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber); 00264 $params['context'] = array(); 00265 $instances = repository::static_function($typename, 'get_instances', $params); 00266 $courseinstances = array(); 00267 $userinstances = array(); 00268 00269 foreach ($instances as $instance) { 00270 if ($instance->context->contextlevel == CONTEXT_COURSE) { 00271 $courseinstances[] = $instance; 00272 } else if ($instance->context->contextlevel == CONTEXT_USER) { 00273 $userinstances[] = $instance; 00274 } 00275 } 00276 // course instances 00277 $instancenumber = count($courseinstances); 00278 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber); 00279 00280 // user private instances 00281 $instancenumber = count($userinstances); 00282 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber); 00283 } else { 00284 $admininstancenumbertext = ""; 00285 $courseinstancenumbertext = ""; 00286 $userinstancenumbertext = ""; 00287 } 00288 00289 $settings .= '<a href="' . $sesskeyurl . '&action=edit&repos=' . $typename . '">' . $settingsstr .'</a>'; 00290 00291 $settings .= $OUTPUT->container_start('mdl-left'); 00292 $settings .= '<br/>'; 00293 $settings .= $admininstancenumbertext; 00294 $settings .= '<br/>'; 00295 $settings .= $courseinstancenumbertext; 00296 $settings .= '<br/>'; 00297 $settings .= $userinstancenumbertext; 00298 $settings .= $OUTPUT->container_end(); 00299 } 00300 // Get the current visibility 00301 if ($i->get_visible()) { 00302 $currentaction = 'show'; 00303 } else { 00304 $currentaction = 'hide'; 00305 } 00306 00307 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename)); 00308 00309 // Display up/down link 00310 $updown = ''; 00311 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead 00312 00313 if ($updowncount > 1) { 00314 $updown .= "<a href=\"$sesskeyurl&action=moveup&repos=".$typename."\">"; 00315 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a> "; 00316 } 00317 else { 00318 $updown .= $spacer; 00319 } 00320 if ($updowncount < $totalinstances) { 00321 $updown .= "<a href=\"$sesskeyurl&action=movedown&repos=".$typename."\">"; 00322 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>"; 00323 } 00324 else { 00325 $updown .= $spacer; 00326 } 00327 00328 $updowncount++; 00329 00330 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings); 00331 00332 if (!in_array($typename, $alreadyplugins)) { 00333 $alreadyplugins[] = $typename; 00334 } 00335 } 00336 } 00337 00338 // Get all the plugins that exist on disk 00339 $plugins = get_plugin_list('repository'); 00340 if (!empty($plugins)) { 00341 foreach ($plugins as $plugin => $dir) { 00342 // Check that it has not already been listed 00343 if (!in_array($plugin, $alreadyplugins)) { 00344 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin)); 00345 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', ''); 00346 } 00347 } 00348 } 00349 00350 $output .= html_writer::table($table); 00351 $output .= $OUTPUT->box_end(); 00352 print $output; 00353 $return = false; 00354 } 00355 00356 if ($return) { 00357 redirect($baseurl); 00358 } 00359 echo $OUTPUT->footer();