Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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  
  25  require_once('../../../../../config.php');
  26  require_once($CFG->dirroot . '/lib/adminlib.php');
  27  
  28  require_sesskey();
  29  
  30  navigation_node::override_active_url(new moodle_url('/admin/settings.php', array('section' => 'logsettingdatabase')));
  31  admin_externalpage_setup('logstoredbtestsettings');
  32  
  33  echo $OUTPUT->header();
  34  echo $OUTPUT->heading(get_string('testingsettings', 'logstore_database'));
  35  
  36  // NOTE: this is not localised intentionally, admins are supposed to understand English at least a bit...
  37  
  38  raise_memory_limit(MEMORY_HUGE);
  39  $dbtable = get_config('logstore_database', 'dbtable');
  40  if (empty($dbtable)) {
  41      echo $OUTPUT->notification('External table not specified.', 'notifyproblem');
  42      die();
  43  }
  44  
  45  $dbdriver = get_config('logstore_database', 'dbdriver');
  46  list($dblibrary, $dbtype) = explode('/', $dbdriver);
  47  if (!$db = \moodle_database::get_driver_instance($dbtype, $dblibrary, true)) {
  48      echo $OUTPUT->notification("Unknown driver $dblibrary/$dbtype", "notifyproblem");
  49      die();
  50  }
  51  
  52  $olddebug = $CFG->debug;
  53  $olddisplay = ini_get('display_errors');
  54  ini_set('display_errors', '1');
  55  $CFG->debug = DEBUG_DEVELOPER;
  56  error_reporting($CFG->debug);
  57  
  58  $dboptions = array();
  59  $dboptions['dbpersist'] = get_config('logstore_database', 'dbpersist');
  60  $dboptions['dbsocket'] = get_config('logstore_database', 'dbsocket');
  61  $dboptions['dbport'] = get_config('logstore_database', 'dbport');
  62  $dboptions['dbschema'] = get_config('logstore_database', 'dbschema');
  63  $dboptions['dbcollation'] = get_config('logstore_database', 'dbcollation');
  64  $dboptions['dbhandlesoptions'] = get_config('logstore_database', 'dbhandlesoptions');
  65  
  66  try {
  67      $db->connect(get_config('logstore_database', 'dbhost'), get_config('logstore_database', 'dbuser'),
  68          get_config('logstore_database', 'dbpass'), get_config('logstore_database', 'dbname'), false, $dboptions);
  69  } catch (\moodle_exception $e) {
  70      echo $OUTPUT->notification('Cannot connect to the database.', 'notifyproblem');
  71      $CFG->debug = $olddebug;
  72      ini_set('display_errors', $olddisplay);
  73      error_reporting($CFG->debug);
  74      ob_end_flush();
  75      echo $OUTPUT->footer();
  76      die();
  77  }
  78  echo $OUTPUT->notification('Connection made.', 'notifysuccess');
  79  $tables = $db->get_tables();
  80  if (!in_array($dbtable, $tables)) {
  81      echo $OUTPUT->notification('Cannot find the specified table ' . $dbtable, 'notifyproblem');
  82      $CFG->debug = $olddebug;
  83      ini_set('display_errors', $olddisplay);
  84      error_reporting($CFG->debug);
  85      ob_end_flush();
  86      echo $OUTPUT->footer();
  87      die();
  88  }
  89  echo $OUTPUT->notification('Table ' . $dbtable . ' found.', 'notifysuccess');
  90  
  91  $cols = $db->get_columns($dbtable);
  92  if (empty($cols)) {
  93      echo $OUTPUT->notification('Can not read external table.', 'notifyproblem');
  94  } else {
  95      $columns = array_keys((array)$cols);
  96      echo $OUTPUT->notification('External table contains following columns:<br />' . implode(', ', $columns), 'notifysuccess');
  97  }
  98  
  99  $db->dispose();
 100  
 101  $CFG->debug = $olddebug;
 102  ini_set('display_errors', $olddisplay);
 103  error_reporting($CFG->debug);
 104  ob_end_flush();
 105  echo $OUTPUT->footer();