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 * Store management setting.
19 *
20 * @package    tool_log
21 * @copyright  2013 Petr Skoda {@link http://skodak.org}
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once("$CFG->libdir/adminlib.php");
28
29class tool_log_setting_managestores extends admin_setting {
30    /**
31     * Calls parent::__construct with specific arguments
32     */
33    public function __construct() {
34        $this->nosave = true;
35        parent::__construct('tool_log_manageui', get_string('managelogging', 'tool_log'), '', '');
36    }
37
38    /**
39     * Always returns true, does nothing.
40     *
41     * @return true
42     */
43    public function get_setting() {
44        return true;
45    }
46
47    /**
48     * Always returns true, does nothing.
49     *
50     * @return true
51     */
52    public function get_defaultsetting() {
53        return true;
54    }
55
56    /**
57     * Always returns '', does not write anything.
58     *
59     * @param mixed $data ignored
60     * @return string Always returns ''
61     */
62    public function write_setting($data) {
63        // Do not write any setting.
64        return '';
65    }
66
67    /**
68     * Checks if $query is one of the available log plugins.
69     *
70     * @param string $query The string to search for
71     * @return bool Returns true if found, false if not
72     */
73    public function is_related($query) {
74        if (parent::is_related($query)) {
75            return true;
76        }
77
78        $query = core_text::strtolower($query);
79        $plugins = \tool_log\log\manager::get_store_plugins();
80        foreach ($plugins as $plugin => $fulldir) {
81            if (strpos(core_text::strtolower($plugin), $query) !== false) {
82                return true;
83            }
84            $localised = get_string('pluginname', $plugin);
85            if (strpos(core_text::strtolower($localised), $query) !== false) {
86                return true;
87            }
88        }
89        return false;
90    }
91
92    /**
93     * Builds the XHTML to display the control.
94     *
95     * @param string $data Unused
96     * @param string $query
97     * @return string
98     */
99    public function output_html($data, $query = '') {
100        global $OUTPUT, $PAGE;
101
102        // Display strings.
103        $strup = get_string('up');
104        $strdown = get_string('down');
105        $strsettings = get_string('settings');
106        $strenable = get_string('enable');
107        $strdisable = get_string('disable');
108        $struninstall = get_string('uninstallplugin', 'core_admin');
109        $strversion = get_string('version');
110
111        $pluginmanager = core_plugin_manager::instance();
112        $logmanager = new \tool_log\log\manager();
113        $available = $logmanager->get_store_plugins();
114        $enabled = get_config('tool_log', 'enabled_stores');
115        if (!$enabled) {
116            $enabled = array();
117        } else {
118            $enabled = array_flip(explode(',', $enabled));
119        }
120
121        $allstores = array();
122        foreach ($enabled as $key => $store) {
123            $allstores[$key] = true;
124            $enabled[$key] = true;
125        }
126        foreach ($available as $key => $store) {
127            $allstores[$key] = true;
128            $available[$key] = true;
129        }
130
131        $return = $OUTPUT->heading(get_string('actlogshdr', 'tool_log'), 3, 'main', true);
132        $return .= $OUTPUT->box_start('generalbox loggingui');
133
134        $table = new html_table();
135        $table->head = array(get_string('name'), get_string('reportssupported', 'tool_log'), $strversion, $strenable,
136                $strup . '/' . $strdown, $strsettings, $struninstall);
137        $table->colclasses = array('leftalign', 'centeralign', 'centeralign', 'centeralign', 'centeralign', 'centeralign',
138                'centeralign');
139        $table->id = 'logstoreplugins';
140        $table->attributes['class'] = 'admintable generaltable';
141        $table->data = array();
142
143        // Iterate through store plugins and add to the display table.
144        $updowncount = 1;
145        $storecount = count($enabled);
146        $url = new moodle_url('/admin/tool/log/stores.php', array('sesskey' => sesskey()));
147        $printed = array();
148        foreach ($allstores as $store => $unused) {
149            $plugininfo = $pluginmanager->get_plugin_info($store);
150            $version = get_config($store, 'version');
151            if ($version === false) {
152                $version = '';
153            }
154
155            if (get_string_manager()->string_exists('pluginname', $store)) {
156                $name = get_string('pluginname', $store);
157            } else {
158                $name = $store;
159            }
160
161            $reports = $logmanager->get_supported_reports($store);
162            if (!empty($reports)) {
163                $supportedreports = implode(', ', $reports);
164            } else {
165                $supportedreports = '-';
166            }
167
168            // Hide/show links.
169            if (isset($enabled[$store])) {
170                $aurl = new moodle_url($url, array('action' => 'disable', 'store' => $store));
171                $hideshow = "<a href=\"$aurl\">";
172                $hideshow .= $OUTPUT->pix_icon('t/hide', $strdisable) . '</a>';
173                $isenabled = true;
174                $displayname = "<span>$name</span>";
175            } else {
176                if (isset($available[$store])) {
177                    $aurl = new moodle_url($url, array('action' => 'enable', 'store' => $store));
178                    $hideshow = "<a href=\"$aurl\">";
179                    $hideshow .= $OUTPUT->pix_icon('t/show', $strenable) . '</a>';
180                    $isenabled = false;
181                    $displayname = "<span class=\"dimmed_text\">$name</span>";
182                } else {
183                    $hideshow = '';
184                    $isenabled = false;
185                    $displayname = '<span class="notifyproblem">' . $name . '</span>';
186                }
187            }
188            if ($PAGE->theme->resolve_image_location('icon', $store, false)) {
189                $icon = $OUTPUT->pix_icon('icon', '', $store, array('class' => 'icon pluginicon'));
190            } else {
191                $icon = $OUTPUT->spacer();
192            }
193
194            // Up/down link (only if store is enabled).
195            $updown = '';
196            if ($isenabled) {
197                if ($updowncount > 1) {
198                    $aurl = new moodle_url($url, array('action' => 'up', 'store' => $store));
199                    $updown .= "<a href=\"$aurl\">";
200                    $updown .= $OUTPUT->pix_icon('t/up', $strup) . '</a>&nbsp;';
201                } else {
202                    $updown .= $OUTPUT->spacer();
203                }
204                if ($updowncount < $storecount) {
205                    $aurl = new moodle_url($url, array('action' => 'down', 'store' => $store));
206                    $updown .= "<a href=\"$aurl\">";
207                    $updown .= $OUTPUT->pix_icon('t/down', $strdown) . '</a>&nbsp;';
208                } else {
209                    $updown .= $OUTPUT->spacer();
210                }
211                ++$updowncount;
212            }
213
214            // Add settings link.
215            if (!$version) {
216                $settings = '';
217            } else {
218                if ($surl = $plugininfo->get_settings_url()) {
219                    $settings = html_writer::link($surl, $strsettings);
220                } else {
221                    $settings = '';
222                }
223            }
224
225            // Add uninstall info.
226            $uninstall = '';
227            if ($uninstallurl = core_plugin_manager::instance()->get_uninstall_url($store, 'manage')) {
228                $uninstall = html_writer::link($uninstallurl, $struninstall);
229            }
230
231            // Add a row to the table.
232            $table->data[] = array($icon . $displayname, $supportedreports, $version, $hideshow, $updown, $settings, $uninstall);
233
234            $printed[$store] = true;
235        }
236
237        $return .= html_writer::table($table);
238        $return .= get_string('configlogplugins', 'tool_log') . '<br />' . get_string('tablenosave', 'admin');
239        $return .= $OUTPUT->box_end();
240        return highlight($query, $return);
241    }
242}
243