/**
  * Singleton method.
  * @return tool_mergeusers_config singleton instance.
  */
 public static function instance()
 {
     if (is_null(self::$instance)) {
         self::$instance = new tool_mergeusers_config();
     }
     return self::$instance;
 }
if (has_capability('tool/mergeusers:mergeusers', context_system::instance())) {
    require_once $CFG->dirroot . '/' . $CFG->admin . '/tool/mergeusers/lib/autoload.php';
    require_once $CFG->dirroot . '/' . $CFG->admin . '/tool/mergeusers/lib.php';
    $ADMIN->add('accounts', new admin_category('tool_mergeusers', get_string('pluginname', 'tool_mergeusers')));
    $ADMIN->add('tool_mergeusers', new admin_externalpage('tool_mergeusers_merge', get_string('pluginname', 'tool_mergeusers'), $CFG->wwwroot . '/' . $CFG->admin . '/tool/mergeusers/index.php', 'tool/mergeusers:mergeusers'));
    $ADMIN->add('tool_mergeusers', new admin_externalpage('tool_mergeusers_viewlog', get_string('viewlog', 'tool_mergeusers'), $CFG->wwwroot . '/' . $CFG->admin . '/tool/mergeusers/view.php', 'tool/mergeusers:mergeusers'));
}
if ($hassiteconfig) {
    require_once $CFG->dirroot . '/' . $CFG->admin . '/tool/mergeusers/lib/autoload.php';
    require_once $CFG->dirroot . '/' . $CFG->admin . '/tool/mergeusers/lib.php';
    // Add configuration for making user suspension optional
    $settings = new admin_settingpage('mergeusers_settings', get_string('pluginname', 'tool_mergeusers'));
    $settings->add(new admin_setting_configcheckbox('tool_mergeusers/suspenduser', get_string('suspenduser_setting', 'tool_mergeusers'), get_string('suspenduser_setting_desc', 'tool_mergeusers'), 1));
    $supporting_lang = tool_mergeusers_transactionssupported() ? 'transactions_supported' : 'transactions_not_supported';
    $settings->add(new admin_setting_configcheckbox('tool_mergeusers/transactions_only', get_string('transactions_setting', 'tool_mergeusers'), get_string('transactions_setting_desc', 'tool_mergeusers') . '<br /><br />' . get_string($supporting_lang, 'tool_mergeusers'), 1));
    $config = tool_mergeusers_config::instance();
    $none = get_string('none');
    $options = array('none' => $none);
    foreach ($config->exceptions as $exception) {
        $options[$exception] = $exception;
    }
    unset($options['my_pages']);
    //duplicated records make MyMoodle does not work.
    $settings->add(new admin_setting_configmultiselect('tool_mergeusers/excluded_exceptions', get_string('excluded_exceptions', 'tool_mergeusers'), get_string('excluded_exceptions_desc', 'tool_mergeusers', $none), array('none'), $options));
    // quiz attempts
    $quizStrings = new stdClass();
    $quizStrings->{QuizAttemptsMerger::ACTION_RENUMBER} = get_string('qa_action_' . QuizAttemptsMerger::ACTION_RENUMBER, 'tool_mergeusers');
    $quizStrings->{QuizAttemptsMerger::ACTION_DELETE_FROM_SOURCE} = get_string('qa_action_' . QuizAttemptsMerger::ACTION_DELETE_FROM_SOURCE, 'tool_mergeusers');
    $quizStrings->{QuizAttemptsMerger::ACTION_DELETE_FROM_TARGET} = get_string('qa_action_' . QuizAttemptsMerger::ACTION_DELETE_FROM_TARGET, 'tool_mergeusers');
    $quizStrings->{QuizAttemptsMerger::ACTION_REMAIN} = get_string('qa_action_' . QuizAttemptsMerger::ACTION_REMAIN, 'tool_mergeusers');
    $quizOptions = array(QuizAttemptsMerger::ACTION_RENUMBER => $quizStrings->{QuizAttemptsMerger::ACTION_RENUMBER}, QuizAttemptsMerger::ACTION_DELETE_FROM_SOURCE => $quizStrings->{QuizAttemptsMerger::ACTION_DELETE_FROM_SOURCE}, QuizAttemptsMerger::ACTION_DELETE_FROM_TARGET => $quizStrings->{QuizAttemptsMerger::ACTION_DELETE_FROM_TARGET}, QuizAttemptsMerger::ACTION_REMAIN => $quizStrings->{QuizAttemptsMerger::ACTION_REMAIN});
 /**
  * Initializes
  * @global object $CFG
  * @param tool_mergeusers_config $config local configuration.
  * @param tool_mergeusers_logger $logger logger facility to save results of mergings.
  */
 public function __construct(tool_mergeusers_config $config = null, tool_mergeusers_logger $logger = null)
 {
     global $CFG;
     $this->logger = is_null($logger) ? new tool_mergeusers_logger() : $logger;
     $config = is_null($config) ? tool_mergeusers_config::instance() : $config;
     $this->supportedDatabase = true;
     $this->checkTransactionSupport();
     switch ($CFG->dbtype) {
         case 'sqlsrv':
         case 'mssql':
             $this->sqlListTables = "SELECT name FROM sys.Tables WHERE name LIKE '" . $CFG->prefix . "%' AND type = 'U' ORDER BY name";
             break;
         case 'mysqli':
         case 'mariadb':
             $this->sqlListTables = 'SHOW TABLES like "' . $CFG->prefix . '%"';
             break;
         case 'pgsql':
             $this->sqlListTables = "SELECT table_name FROM information_schema.tables WHERE table_name LIKE '" . $CFG->prefix . "%' AND table_schema = 'public'";
             break;
         default:
             $this->supportedDatabase = false;
             $this->sqlListTables = "";
     }
     // these are tables we don't want to modify due to logging or security reasons.
     // we flip key<-->value to accelerate lookups.
     $this->tablesToSkip = array_flip($config->exceptions);
     $excluded = explode(',', get_config('tool_mergeusers', 'excluded_exceptions'));
     $excluded = array_flip($excluded);
     if (!isset($excluded['none'])) {
         foreach ($excluded as $exclude => $nonused) {
             unset($this->tablesToSkip[$exclude]);
         }
     }
     // these are special cases, corresponding to tables with compound indexes that
     // need a special treatment.
     $this->tablesWithCompoundIndex = $config->compoundindexes;
     // Initializes user-related field names.
     $userFieldNames = array();
     foreach ($config->userfieldnames as $tablename => $fields) {
         $userFieldNames[$tablename] = "'" . implode("','", $fields) . "'";
     }
     $this->userFieldNames = $userFieldNames;
     // Load available TableMerger tools.
     $tableMergers = array();
     $tablesProcessedByTableMergers = array();
     foreach ($config->tablemergers as $tableName => $class) {
         $tm = new $class();
         // ensure any provided class is a class of TableMerger
         if (!$tm instanceof TableMerger) {
             // aborts execution by showing an error.
             if (CLI_SCRIPT) {
                 cli_error('Error: ' . __METHOD__ . ':: ' . get_string('notablemergerclass', 'tool_mergeusers', $class));
             } else {
                 print_error('notablemergerclass', 'tool_mergeusers', new moodle_url('/admin/tool/mergeusers/index.php'), $class);
             }
         }
         // append any additional table to skip.
         $tablesProcessedByTableMergers = array_merge($tablesProcessedByTableMergers, $tm->getTablesToSkip());
         $tableMergers[$tableName] = $tm;
     }
     $this->tableMergers = $tableMergers;
     $this->tablesProcessedByTableMergers = array_flip($tablesProcessedByTableMergers);
     // this will abort execution if local database is not supported.
     $this->checkDatabaseSupport();
     // initializes the list of fields and tables to check in the current database,
     // given the local configuration.
     $this->init();
 }