MDL-44070 Conditional availability enhancements (2): subsystem, API
[moodle.git] / admin / tool / availabilityconditions / index.php
CommitLineData
d3db4b03 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.
13//
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/>.
16
17/**
18 * Provides an overview of installed availability conditions.
19 *
20 * You can also enable/disable them from this screen.
21 *
22 * @package tool_availabilityconditions
23 * @copyright 2014 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27require_once(__DIR__ . '/../../../config.php');
28require_once($CFG->libdir . '/adminlib.php');
29require_once($CFG->libdir . '/tablelib.php');
30
31admin_externalpage_setup('manageavailability');
32
33// Get sorted list of all availability condition plugins.
34$plugins = array();
35foreach (core_component::get_plugin_list('availability') as $plugin => $plugindir) {
36 if (get_string_manager()->string_exists('pluginname', 'availability_' . $plugin)) {
37 $strpluginname = get_string('pluginname', 'availability_' . $plugin);
38 } else {
39 $strpluginname = $plugin;
40 }
41 $plugins[$plugin] = $strpluginname;
42}
43core_collator::asort($plugins);
44
45// Do plugin actions.
46$pageurl = new moodle_url('/' . $CFG->admin . '/availabilityconditions.php');
47if (($plugin = optional_param('plugin', '', PARAM_PLUGIN))) {
48 require_sesskey();
49 if (!array_key_exists($plugin, $plugins)) {
50 print_error('invalidcomponent', 'error', $pageurl);
51 }
52 $action = required_param('action', PARAM_ALPHA);
53 switch ($action) {
54 case 'hide' :
55 set_config('disabled', 1, 'availability_' . $plugin);
56 break;
57 case 'show' :
58 unset_config('disabled', 'availability_' . $plugin);
59 break;
60 }
61 core_plugin_manager::reset_caches();
62
63 // Always redirect back after an action.
64 redirect($pageurl);
65}
66
67echo $OUTPUT->header();
68echo $OUTPUT->heading(get_string('manageplugins', 'availability'));
69
70// Show a table of installed availability conditions.
71$table = new flexible_table('availabilityconditions_administration_table');
72$table->define_columns(array('name', 'version', 'enable'));
73$table->define_headers(array(get_string('plugin'),
74 get_string('version'), get_string('hide') . '/' . get_string('show')));
75$table->define_baseurl($PAGE->url);
76$table->set_attribute('id', 'availabilityconditions');
77$table->set_attribute('class', 'admintable generaltable');
78$table->setup();
79
80$enabledlist = core\plugininfo\availability::get_enabled_plugins();
81foreach ($plugins as $plugin => $name) {
82
83 // Get version or ? if unknown.
84 $version = get_config('availability_' . $plugin);
85 if (!empty($version->version)) {
86 $version = $version->version;
87 } else {
88 $version = '?';
89 }
90
91 // Get enabled status and use to grey out name if necessary.
92 $enabled = in_array($plugin, $enabledlist);
93 if ($enabled) {
94 $enabledaction = 'hide';
95 $enabledstr = get_string('hide');
96 $class = '';
97 } else {
98 $enabledaction = 'show';
99 $enabledstr = get_string('show');
100 $class = 'dimmed_text';
101 }
102 $namespan = html_writer::span($name, $class);
103
104 // Make enable control. This is a POST request (using a form control rather
105 // than just a link) because it makes a database change.
106 $targeturl = new moodle_url('availabilityconditions.php', array(
107 'plugin' => $plugin, 'action' => $enabledaction, 'sesskey' => sesskey()));
108 $enablecontrol = html_writer::tag('form', html_writer::div(
109 html_writer::empty_tag('input', array('type' => 'hidden',
110 'name' => 'sesskey', 'value' => sesskey())) .
111 html_writer::empty_tag('input', array('type' => 'hidden',
112 'name' => 'plugin', 'value' => $plugin)) .
113 html_writer::empty_tag('input', array('type' => 'hidden',
114 'name' => 'action', 'value' => $enabledaction)) .
115 html_writer::empty_tag('input', array('type' => 'image',
116 'src' => $OUTPUT->pix_url('t/' . $enabledaction), 'alt' => $enabledstr,
117 'title' => $enabledstr))
118 ), array(
119 'method' => 'post', 'action' => 'availabilityconditions.php'));
120
121 $table->add_data(array($namespan, $version, $enablecontrol));
122}
123
124$table->print_html();
125
126echo $OUTPUT->footer();