source: moodle/trunk/fuentes/admin/tool/log/store/database/test_settings.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 3.9 KB
Line 
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 * Filter form.
19 *
20 * @package    logstore_database
21 * @copyright  2014 onwards Ankit Agarwal
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once('../../../../../config.php');
26require_once($CFG->dirroot . '/lib/adminlib.php');
27
28require_login();
29$context = context_system::instance();
30require_capability('moodle/site:config', $context);
31require_sesskey();
32
33navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section' => 'logsettingdatabase')));
34admin_externalpage_setup('logstoredbtestsettings');
35
36echo $OUTPUT->header();
37echo $OUTPUT->heading(get_string('testingsettings', 'logstore_database'));
38
39// NOTE: this is not localised intentionally, admins are supposed to understand English at least a bit...
40
41raise_memory_limit(MEMORY_HUGE);
42$dbtable = get_config('logstore_database', 'dbtable');
43if (empty($dbtable)) {
44    echo $OUTPUT->notification('External table not specified.', 'notifyproblem');
45    die();
46}
47
48$dbdriver = get_config('logstore_database', 'dbdriver');
49list($dblibrary, $dbtype) = explode('/', $dbdriver);
50if (!$db = \moodle_database::get_driver_instance($dbtype, $dblibrary, true)) {
51    echo $OUTPUT->notification("Unknown driver $dblibrary/$dbtype", "notifyproblem");
52    die();
53}
54
55$olddebug = $CFG->debug;
56$olddisplay = ini_get('display_errors');
57ini_set('display_errors', '1');
58$CFG->debug = DEBUG_DEVELOPER;
59error_reporting($CFG->debug);
60
61$dboptions = array();
62$dboptions['dbpersist'] = get_config('logstore_database', 'dbpersist');
63$dboptions['dbsocket'] = get_config('logstore_database', 'dbsocket');
64$dboptions['dbport'] = get_config('logstore_database', 'dbport');
65$dboptions['dbschema'] = get_config('logstore_database', 'dbschema');
66$dboptions['dbcollation'] = get_config('logstore_database', 'dbcollation');
67
68try {
69    $db->connect(get_config('logstore_database', 'dbhost'), get_config('logstore_database', 'dbuser'),
70        get_config('logstore_database', 'dbpass'), get_config('logstore_database', 'dbname'), false, $dboptions);
71} catch (\moodle_exception $e) {
72    echo $OUTPUT->notification('Cannot connect to the database.', 'notifyproblem');
73    $CFG->debug = $olddebug;
74    ini_set('display_errors', $olddisplay);
75    error_reporting($CFG->debug);
76    ob_end_flush();
77    echo $OUTPUT->footer();
78    die();
79}
80echo $OUTPUT->notification('Connection made.', 'notifysuccess');
81$tables = $db->get_tables();
82if (!in_array($dbtable, $tables)) {
83    echo $OUTPUT->notification('Cannot find the specified table ' . $dbtable, 'notifyproblem');
84    $CFG->debug = $olddebug;
85    ini_set('display_errors', $olddisplay);
86    error_reporting($CFG->debug);
87    ob_end_flush();
88    echo $OUTPUT->footer();
89    die();
90}
91echo $OUTPUT->notification('Table ' . $dbtable . ' found.', 'notifysuccess');
92
93$cols = $db->get_columns($dbtable);
94if (empty($cols)) {
95    echo $OUTPUT->notification('Can not read external table.', 'notifyproblem');
96} else {
97    $columns = array_keys((array)$cols);
98    echo $OUTPUT->notification('External table contains following columns:<br />' . implode(', ', $columns), 'notifysuccess');
99}
100
101$db->dispose();
102
103$CFG->debug = $olddebug;
104ini_set('display_errors', $olddisplay);
105error_reporting($CFG->debug);
106ob_end_flush();
107echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.