MDL-47397 assignfeedback_editpdf: Tests use reliable way to annotate PDF
[moodle.git] / admin / repository.php
blob31fd40cc0c6e7ff42c491bb1d913f923097307f3
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 require_once(dirname(dirname(__FILE__)) . '/config.php');
18 require_once($CFG->dirroot . '/repository/lib.php');
19 require_once($CFG->libdir . '/adminlib.php');
21 $repository = optional_param('repos', '', PARAM_ALPHANUMEXT);
22 $action = optional_param('action', '', PARAM_ALPHANUMEXT);
23 $sure = optional_param('sure', '', PARAM_ALPHA);
24 $downloadcontents = optional_param('downloadcontents', false, PARAM_BOOL);
26 $display = true; // fall through to normal display
28 $pagename = 'managerepositories';
30 if ($action == 'edit') {
31 $pagename = 'repositorysettings' . $repository;
32 } else if ($action == 'delete') {
33 $pagename = 'repositorydelete';
34 } else if (($action == 'newon') || ($action == 'newoff')) {
35 $pagename = 'repositorynew';
38 // Need to remember this for form
39 $formaction = $action;
41 // Check what visibility to show the new repository
42 if ($action == 'newon') {
43 $action = 'new';
44 $visible = true;
45 } else if ($action == 'newoff') {
46 $action = 'new';
47 $visible = false;
50 require_capability('moodle/site:config', context_system::instance());
51 admin_externalpage_setup($pagename);
53 $sesskeyurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php?sesskey=' . sesskey();
54 $baseurl = $CFG->wwwroot.'/'.$CFG->admin.'/repository.php';
56 $configstr = get_string('manage', 'repository');
58 $return = true;
60 if (!empty($action)) {
61 require_sesskey();
64 /**
65 * Helper function that generates a moodle_url object
66 * relevant to the repository
68 function repository_action_url($repository) {
69 global $baseurl;
70 return new moodle_url($baseurl, array('sesskey'=>sesskey(), 'repos'=>$repository));
73 if (($action == 'edit') || ($action == 'new')) {
74 $pluginname = '';
75 if ($action == 'edit') {
76 $repositorytype = repository::get_type_by_typename($repository);
77 $classname = 'repository_' . $repositorytype->get_typename();
78 $configs = call_user_func(array($classname, 'get_type_option_names'));
79 $plugin = $repositorytype->get_typename();
80 // looking for instance to edit plugin name
81 $instanceoptions = call_user_func(array($classname, 'get_instance_option_names'));
82 if (empty($instanceoptions)) {
83 $params = array();
84 $params['type'] = $plugin;
85 $instances = repository::get_instances($params);
86 if ($instance = array_pop($instances)) {
87 // use the one form db record
88 $pluginname = $instance->instance->name;
92 } else {
93 $repositorytype = null;
94 $plugin = $repository;
95 $typeid = $repository;
97 $PAGE->set_pagetype('admin-repository-' . $plugin);
98 // display the edit form for this instance
99 $mform = new repository_type_form('', array('pluginname'=>$pluginname, 'plugin' => $plugin, 'instance' => $repositorytype, 'action' => $formaction));
100 $fromform = $mform->get_data();
102 //detect if we create a new type without config (in this case if don't want to display a setting page during creation)
103 $nosettings = false;
104 if ($action == 'new') {
105 $adminconfignames = repository::static_function($repository, 'get_type_option_names');
106 $nosettings = empty($adminconfignames);
108 // end setup, begin output
110 if ($mform->is_cancelled()){
111 redirect($baseurl);
112 } else if (!empty($fromform) || $nosettings) {
113 require_sesskey();
114 if ($action == 'edit') {
115 $settings = array();
116 foreach($configs as $config) {
117 if (!empty($fromform->$config)) {
118 $settings[$config] = $fromform->$config;
119 } else {
120 // if the config name is not appear in $fromform
121 // empty this config value
122 $settings[$config] = '';
125 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
126 if (!empty($instanceoptionnames)) {
127 if (array_key_exists('enablecourseinstances', $fromform)) {
128 $settings['enablecourseinstances'] = $fromform->enablecourseinstances;
130 else {
131 $settings['enablecourseinstances'] = 0;
133 if (array_key_exists('enableuserinstances', $fromform)) {
134 $settings['enableuserinstances'] = $fromform->enableuserinstances;
136 else {
137 $settings['enableuserinstances'] = 0;
140 $success = $repositorytype->update_options($settings);
141 } else {
142 $type = new repository_type($plugin, (array)$fromform, $visible);
143 $success = true;
144 if (!$repoid = $type->create()) {
145 $success = false;
147 $data = data_submitted();
149 if ($success) {
150 // configs saved
151 core_plugin_manager::reset_caches();
152 redirect($baseurl);
153 } else {
154 print_error('instancenotsaved', 'repository', $baseurl);
156 exit;
157 } else {
158 echo $OUTPUT->header();
159 echo $OUTPUT->heading(get_string('configplugin', 'repository_'.$plugin));
160 $displaysettingform = true;
161 if ($action == 'edit') {
162 $typeoptionnames = repository::static_function($repository, 'get_type_option_names');
163 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
164 if (empty($typeoptionnames) && empty($instanceoptionnames)) {
165 $displaysettingform = false;
168 if ($displaysettingform){
169 $OUTPUT->box_start();
170 $mform->display();
171 $OUTPUT->box_end();
173 $return = false;
175 // Display instances list and creation form
176 if ($action == 'edit') {
177 $instanceoptionnames = repository::static_function($repository, 'get_instance_option_names');
178 if (!empty($instanceoptionnames)) {
179 repository::display_instances_list(context_system::instance(), $repository);
183 } else if ($action == 'show') {
184 if (!confirm_sesskey()) {
185 print_error('confirmsesskeybad', '', $baseurl);
187 $repositorytype = repository::get_type_by_typename($repository);
188 if (empty($repositorytype)) {
189 print_error('invalidplugin', 'repository', '', $repository);
191 $repositorytype->update_visibility(true);
192 core_plugin_manager::reset_caches();
193 $return = true;
194 } else if ($action == 'hide') {
195 if (!confirm_sesskey()) {
196 print_error('confirmsesskeybad', '', $baseurl);
198 $repositorytype = repository::get_type_by_typename($repository);
199 if (empty($repositorytype)) {
200 print_error('invalidplugin', 'repository', '', $repository);
202 $repositorytype->update_visibility(false);
203 core_plugin_manager::reset_caches();
204 $return = true;
205 } else if ($action == 'delete') {
206 $repositorytype = repository::get_type_by_typename($repository);
207 if ($sure) {
208 $PAGE->set_pagetype('admin-repository-' . $repository);
209 if (!confirm_sesskey()) {
210 print_error('confirmsesskeybad', '', $baseurl);
213 if ($repositorytype->delete($downloadcontents)) {
214 core_plugin_manager::reset_caches();
215 redirect($baseurl);
216 } else {
217 print_error('instancenotdeleted', 'repository', $baseurl);
219 exit;
220 } else {
221 echo $OUTPUT->header();
223 $message = get_string('confirmremove', 'repository', $repositorytype->get_readablename());
225 $output = $OUTPUT->box_start('generalbox', 'notice');
226 $output .= html_writer::tag('p', $message);
228 $removeurl = new moodle_url($sesskeyurl);
229 $removeurl->params(array(
230 'action' =>'delete',
231 'repos' => $repository,
232 'sure' => 'yes',
235 $removeanddownloadurl = new moodle_url($sesskeyurl);
236 $removeanddownloadurl->params(array(
237 'action' =>'delete',
238 'repos'=> $repository,
239 'sure' => 'yes',
240 'downloadcontents' => 1,
243 $output .= $OUTPUT->single_button($removeurl, get_string('continueuninstall', 'repository'));
244 $output .= $OUTPUT->single_button($removeanddownloadurl, get_string('continueuninstallanddownload', 'repository'));
245 $output .= $OUTPUT->single_button($baseurl, get_string('cancel'));
246 $output .= $OUTPUT->box_end();
248 echo $output;
250 $return = false;
252 } else if ($action == 'moveup') {
253 $repositorytype = repository::get_type_by_typename($repository);
254 $repositorytype->move_order('up');
255 } else if ($action == 'movedown') {
256 $repositorytype = repository::get_type_by_typename($repository);
257 $repositorytype->move_order('down');
258 } else {
259 // If page is loaded directly
260 echo $OUTPUT->header();
261 echo $OUTPUT->heading(get_string('manage', 'repository'));
263 // Get strings that are used
264 $strshow = get_string('on', 'repository');
265 $strhide = get_string('off', 'repository');
266 $strdelete = get_string('disabled', 'repository');
268 $actionchoicesforexisting = array(
269 'show' => $strshow,
270 'hide' => $strhide,
271 'delete' => $strdelete
274 $actionchoicesfornew = array(
275 'newon' => $strshow,
276 'newoff' => $strhide,
277 'delete' => $strdelete
280 $output = '';
281 $output .= $OUTPUT->box_start('generalbox');
283 // Set strings that are used multiple times
284 $settingsstr = get_string('settings');
285 $disablestr = get_string('disable');
287 // Table to list plug-ins
288 $table = new html_table();
289 $table->head = array(get_string('name'), get_string('isactive', 'repository'), get_string('order'), $settingsstr);
291 $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign');
292 $table->id = 'repositoriessetting';
293 $table->data = array();
294 $table->attributes['class'] = 'admintable generaltable';
296 // Get list of used plug-ins
297 $repositorytypes = repository::get_types();
298 // Array to store plugins being used
299 $alreadyplugins = array();
300 if (!empty($repositorytypes)) {
301 $totalrepositorytypes = count($repositorytypes);
302 $updowncount = 1;
303 foreach ($repositorytypes as $i) {
304 $settings = '';
305 $typename = $i->get_typename();
306 // Display edit link only if you can config the type or if it has multiple instances (e.g. has instance config)
307 $typeoptionnames = repository::static_function($typename, 'get_type_option_names');
308 $instanceoptionnames = repository::static_function($typename, 'get_instance_option_names');
310 if (!empty($typeoptionnames) || !empty($instanceoptionnames)) {
311 // Calculate number of instances in order to display them for the Moodle administrator
312 if (!empty($instanceoptionnames)) {
313 $params = array();
314 $params['context'] = array(context_system::instance());
315 $params['onlyvisible'] = false;
316 $params['type'] = $typename;
317 $admininstancenumber = count(repository::static_function($typename, 'get_instances', $params));
318 // site instances
319 $admininstancenumbertext = get_string('instancesforsite', 'repository', $admininstancenumber);
320 $params['context'] = array();
321 $instances = repository::static_function($typename, 'get_instances', $params);
322 $courseinstances = array();
323 $userinstances = array();
325 foreach ($instances as $instance) {
326 $repocontext = context::instance_by_id($instance->instance->contextid);
327 if ($repocontext->contextlevel == CONTEXT_COURSE) {
328 $courseinstances[] = $instance;
329 } else if ($repocontext->contextlevel == CONTEXT_USER) {
330 $userinstances[] = $instance;
333 // course instances
334 $instancenumber = count($courseinstances);
335 $courseinstancenumbertext = get_string('instancesforcourses', 'repository', $instancenumber);
337 // user private instances
338 $instancenumber = count($userinstances);
339 $userinstancenumbertext = get_string('instancesforusers', 'repository', $instancenumber);
340 } else {
341 $admininstancenumbertext = "";
342 $courseinstancenumbertext = "";
343 $userinstancenumbertext = "";
346 $settings .= '<a href="' . $sesskeyurl . '&amp;action=edit&amp;repos=' . $typename . '">' . $settingsstr .'</a>';
348 $settings .= $OUTPUT->container_start('mdl-left');
349 $settings .= '<br/>';
350 $settings .= $admininstancenumbertext;
351 $settings .= '<br/>';
352 $settings .= $courseinstancenumbertext;
353 $settings .= '<br/>';
354 $settings .= $userinstancenumbertext;
355 $settings .= $OUTPUT->container_end();
357 // Get the current visibility
358 if ($i->get_visible()) {
359 $currentaction = 'show';
360 } else {
361 $currentaction = 'hide';
364 $select = new single_select(repository_action_url($typename, 'repos'), 'action', $actionchoicesforexisting, $currentaction, null, 'applyto' . basename($typename));
365 $select->set_label(get_string('action'), array('class' => 'accesshide'));
366 // Display up/down link
367 $updown = '';
368 $spacer = $OUTPUT->spacer(array('height'=>15, 'width'=>15)); // should be done with CSS instead
370 if ($updowncount > 1) {
371 $updown .= "<a href=\"$sesskeyurl&amp;action=moveup&amp;repos=".$typename."\">";
372 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/up') . "\" alt=\"up\" /></a>&nbsp;";
374 else {
375 $updown .= $spacer;
377 if ($updowncount < $totalrepositorytypes) {
378 $updown .= "<a href=\"$sesskeyurl&amp;action=movedown&amp;repos=".$typename."\">";
379 $updown .= "<img src=\"" . $OUTPUT->pix_url('t/down') . "\" alt=\"down\" /></a>";
381 else {
382 $updown .= $spacer;
385 $updowncount++;
387 $table->data[] = array($i->get_readablename(), $OUTPUT->render($select), $updown, $settings);
389 if (!in_array($typename, $alreadyplugins)) {
390 $alreadyplugins[] = $typename;
395 // Get all the plugins that exist on disk
396 $plugins = core_component::get_plugin_list('repository');
397 if (!empty($plugins)) {
398 foreach ($plugins as $plugin => $dir) {
399 // Check that it has not already been listed
400 if (!in_array($plugin, $alreadyplugins)) {
401 $select = new single_select(repository_action_url($plugin, 'repos'), 'action', $actionchoicesfornew, 'delete', null, 'applyto' . basename($plugin));
402 $select->set_label(get_string('action'), array('class' => 'accesshide'));
403 $table->data[] = array(get_string('pluginname', 'repository_'.$plugin), $OUTPUT->render($select), '', '');
408 $output .= html_writer::table($table);
409 $output .= $OUTPUT->box_end();
410 print $output;
411 $return = false;
414 if ($return) {
415 redirect($baseurl);
417 echo $OUTPUT->footer();