<?php
// /elms/admin/assessments/probe.php  (minimal + safe)
declare(strict_types=1);
ini_set('display_errors','1');
error_reporting(E_ALL);

function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'); }

echo "<h3>Criteria Manager Probe</h3>";

$ROOT = rtrim($_SERVER['DOCUMENT_ROOT'] ?: dirname(__DIR__,3), '/');
$paths = [
  $ROOT.'/elms/includes/db.php',
  __DIR__.'/../../includes/db.php',
  __DIR__.'/../includes/db.php',
  dirname(__DIR__).'/includes/db.php',
  dirname(__DIR__,2).'/includes/db.php',
  dirname(__DIR__,3).'/includes/db.php',
];

echo "<h4>DB include search</h4><ul>";
$found = null;
foreach ($paths as $p) {
  $ok = is_file($p);
  echo "<li>".h($p)." : ".($ok ? "<b>FOUND</b>" : "missing")."</li>";
  if ($ok && !$found) $found = $p;
}
echo "</ul>";

if (!$found) { exit("<b>DB include not found. Fix one of the paths above.</b>"); }
require_once $found;

// accept either $mysqli or $conn from your include
if (!isset($mysqli) || !($mysqli instanceof mysqli)) {
  if (isset($conn) && $conn instanceof mysqli) $mysqli = $conn;
}
if (!isset($mysqli) || !($mysqli instanceof mysqli)) {
  exit("<b>DB connection object (\$mysqli or \$conn) is not initialized in the include.</b>");
}
@$mysqli->set_charset('utf8mb4');

$ok = $mysqli->query("SELECT 1");
echo "<p>DB connection: ".($ok ? "<b>OK</b>" : ("<b>FAIL</b> — ".h($mysqli->error)))."</p>";

echo "<p>INFORMATION_SCHEMA access: ";
$test = $mysqli->query("SELECT 1 FROM INFORMATION_SCHEMA.TABLES LIMIT 1");
echo $test ? "<b>YES</b>" : ("<b>NO</b> — ".h($mysqli->error));
echo "</p>";

$dir = $ROOT.'/uploads/criteria';
if (!is_dir($dir)) @mkdir($dir, 0775, true);
echo "<p>Writable /uploads/criteria: ".(is_dir($dir) && is_writable($dir) ? "<b>YES</b>" : "<b>NO</b>")." (".h($dir).")</p>";

echo "<p>Done.</p>";
