/** * Constructs a new Tracker instance. */ public function __construct() { try { $this->connection = new \PDO('mysql:host=' . Config::get('mysql', 'host') . ';port=' . Config::get('mysql', 'port') . ';dbname=' . Config::get('mysql', 'database'), Config::get('mysql', 'username'), Config::get('mysql', 'password')); } catch (\PDOException $e) { header('HTTP/1.1 500 Internal Server Error'); } // If the connection exists... if ($this->getConnection() != null) { $this->page .= Page::Alert('alert alert-success', Config::get('app', 'name') . ' - ' . Config::get('app', 'version')); if ($_GET['name'] != null && $_GET['time'] != null) { $time = $_GET['time']; if ($time == 'milliseconds' || $time == 'seconds' || $time == 'minutes' || $time == 'hours') { // Get ActivityTracker information and display it... $query = "SELECT * FROM activitytracker_users WHERE name=:value;"; $statement = $this->connection->prepare($query); // Bind the parameters. $statement->bindParam(':value', $_GET['name']); $statement->execute(); if ($statement->rowCount() == 0) { $query = "SELECT * FROM activitytracker_users WHERE unique_id=:value;"; $statement = $this->connection->prepare($query); // Bind the parameters. $statement->bindParam(':value', $_GET['name']); $statement->execute(); } // Execute the query. $record = $statement->fetchAll(); $record = $record[0]; if ($record != null) { $query = "SELECT * FROM activitytracker_logs WHERE user_id=:value;"; $statement = $this->connection->prepare($query); // Bind the parameters. $statement->bindParam(':value', $record['id']); $statement->execute(); if ($statement->rowCount() > 0) { $this->page .= Page::Graph($statement->fetchAll(), $time); } else { $this->page .= Page::Alert("alert alert-info", "The user specified hasn't played at all."); $this->page .= Page::Form(); } } else { $this->page .= Page::Alert("alert alert-danger", "A user by the name/uuid of: `" . $_GET['name'] . "` was not found."); $this->page .= Page::Form(); } } else { $this->page .= Page::Alert("alert alert-danger", "Unknown time measurement."); $this->page .= Page::Form(); } } else { $this->page .= Page::Form(); } } else { $this->page .= Page::Alert("alert alert-danger", "Unable to connect to the database."); } }
/** * Display an Activity graph for a player. * * @param $records the user record. * @return HTML content. */ public static function Graph($records, $measurement) { $data = ' <!-- The chart --> <canvas id="chart" width="500" height="500" style="max-width: 100%; margin: 25px auto; display: block;"></canvas> '; $data .= '<script>'; $data .= ' var ctx = document.getElementById("chart").getContext("2d"); '; $data .= ' var data = { labels: [ '; foreach ($records as $rec) { $data .= '"' . $rec['date'] . '", '; } $data .= '], datasets: [{ label: "My First dataset", fillColor: "rgba(220,220,220,0.2)", strokeColor: "rgba(220,220,220,1)", pointColor: "rgba(220,220,220,1)", pointStrokeColor: "#fff", pointHighlightFill: "#fff", pointHighlightStroke: "rgba(220,220,220,1)", data: [ '; foreach ($records as $rec) { if ($measurement == "seconds") { $data .= $rec['time'] / 1000 . ','; } else { if ($measurement == "minutes") { $data .= $rec['time'] / (1000 * 60) . ','; } else { if ($measurement == "hours") { $data .= $rec['time'] / (1000 * 60 * 60) . ','; } else { $data .= $rec['time'] . ','; } } } } $data .= '] }] }; '; $data .= ' var chart = new Chart(ctx).Line(data, Chart.defaults.Line); '; $data .= '</script>'; $data .= ' <a class="btn btn-default" href="' . Config::get('app', 'url') . '/index.php">Back</a> '; return $data; }