juggl/juggl-server/api/services/jugglDbApi.inc.php
2020-11-08 15:21:51 +01:00

206 lines
5.7 KiB
PHP

<?php
require_once(__DIR__ . "/dbOperations.inc.php");
function addStartRecord($user_id, $params, $project_id = null, $start_device_id = null)
{
$data = [
"user_id" => $user_id,
"start_time" => $params->get("start_time"),
"project_id" => $project_id,
"start_device_id" => $start_device_id
];
$db = new DbOperations();
$db->insert("time_records", $data);
$db->execute();
}
function addTimeRecord($user_id, $params, $project_id = null, $start_device_id = null)
{
$data = [
"user_id" => $user_id,
"start_time" => $params->get("start_time"),
"end_time" => $params->get("end_time"),
"duration" => $params->get("duration"),
"project_id" => $project_id,
"start_device_id" => $start_device_id
];
$db = new DbOperations();
$db->insert("time_records", $data);
$db->execute();
}
function getTimeRecord($user_id, $record_id)
{
$db = new DbOperations();
$db->select("time_records");
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("record_id", Comparison::EQUAL, $record_id);
$result = $db->execute();
if (count($result) <= 0) {
return null;
}
$result = $result[0];
// Is still running?
if ($result["end_time"] == null) {
$result["duration"] = calcDuration($result["start_time"]);
}
return $result;
}
function getProjects($user_id)
{
$db = new DbOperations();
$db->select("projects");
$db->where("user_id", Comparison::EQUAL, $user_id);
$results = $db->execute();
foreach ($results as $key => $project) {
$meta = getProjectRecordDerivedData($user_id, $project["project_id"]);
foreach ($meta as $metaKey => $value) {
$results[$key][$metaKey] = $value;
}
}
return $results;
}
function getProjectRecordDerivedData($user_id, $project_id)
{
$durationAttribute = "SUM(duration) AS total_duration";
$recordCountAttribute = "COUNT(*) AS record_count";
$db = new DbOperations();
$db->select("time_records", ["*", $durationAttribute, $recordCountAttribute]);
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("project_id", Comparison::EQUAL, $project_id);
$results = $db->execute();
if (count($results) <= 0) {
return ["duration" => 0, "record_count" => 0];
} else {
return [
"duration" => (int)$results[0]["total_duration"],
"record_count" => (int)$results[0]["record_count"]
];
}
}
function getProjectRecord($user_id, $project_id, $finished = null)
{
$db = new DbOperations();
$db->select("time_records");
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("project_id", Comparison::EQUAL, $project_id);
if ($finished != null) {
$comp = Comparison::UNEQUAL;
if ($finished == false) {
$comp = Comparison::EQUAL;
}
$db->where("end_time", $comp, null);
}
$db->orderBy("start_time", Order::DESC);
$result = $db->execute();
if (count($result) <= 0) {
return null;
}
$result = $result[0];
// Is still running?
if ($result["end_time"] == null) {
$result["duration"] = calcDuration($result["start_time"]);
}
return $result;
}
function updateEndRecord($user_id, $params)
{
$record_id = $params->get("record_id");
// Get start instance to calculate duration
$start_time = getTimeRecord($user_id, $record_id)[0]["start_time"];
$data = [
"end_time" => $params->get("end_time"),
"duration" => calcDuration($start_time, $params->get("end_time"))
];
$db = new DbOperations();
$db->update("time_records", $data);
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("record_id", Comparison::EQUAL, $record_id);
$db->execute();
}
function updateTimeRecord($user_id, $params)
{
$data = [];
$anythingUpdated = false;
if ($params->exists(["start_time"])) {
$data["start_time"] = $params->get("start_time");
$anythingUpdated = true;
}
if ($params->exists(["end_time"])) {
$data["end_time"] = $params->get("end_time");
$anythingUpdated = true;
}
if ($params->exists(["duration"])) {
$data["duration"] = $params->get("duration");
$anythingUpdated = true;
}
if ($params->exists(["project_id"])) {
$data["project_id"] = $params->get("project_id");
$anythingUpdated = true;
}
if ($params->exists(["start_device_id"])) {
$data["start_device_id"] = $params->get("start_device_id");
$anythingUpdated = true;
}
if ($anythingUpdated == false) {
return;
}
$db = new DbOperations();
$db->update("time_records", $data);
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->where("record_id", Comparison::EQUAL, $params->get("record_id"));
$db->execute();
}
function isProjectValid($project_id, $user_id)
{
$db = new DbOperations();
$db->select("projects");
$db->where("project_id", Comparison::EQUAL, $project_id);
$db->where("user_id", Comparison::EQUAL, $user_id);
return count($db->execute()) == 1;
}
function isDeviceValid($start_device_id, $user_id)
{
$db = new DbOperations();
$db->select("devices");
$db->where("start_device_id", Comparison::EQUAL, $start_device_id);
$db->where("user_id", Comparison::EQUAL, $user_id);
return count($db->execute()) == 1;
}
function calcDuration($start_time, $end_time = "NOW")
{
return (int)((new DateTime($start_time))->diff(new DateTime($end_time))->format("%s"));
}