juggl/juggl-server/api/services/jugglDbApi.inc.php

224 lines
6.2 KiB
PHP
Raw Normal View History

2020-11-08 15:21:51 +01:00
<?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();
2020-11-21 00:49:22 +01:00
$db->select("time_records", ["*", $durationAttribute, $recordCountAttribute], false);
2020-11-08 15:21:51 +01:00
$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) {
if ($finished) {
$db->addSql(" AND end_time IS NOT NULL");
} else {
$db->addSql(" AND end_time IS NULL");
2020-11-08 15:21:51 +01:00
}
}
$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 getRunningRecords($user_id)
{
$db = new DbOperations();
$db->select("time_records");
$db->where("user_id", Comparison::EQUAL, $user_id);
$db->addSql(" AND end_time IS NULL");
$results = $db->execute();
// Is still running?
foreach ($results as $key => $value) {
$results[$key]["duration"] = calcDuration($results[$key]["start_time"]);
}
return $results;
}
2020-11-08 15:21:51 +01:00
function updateEndRecord($user_id, $params)
{
$record_id = $params->get("record_id");
// Get start instance to calculate duration
$start_time = getTimeRecord($user_id, $record_id)["start_time"];
var_dump($start_time);
var_dump($params->get("end_time"));
2020-11-08 15:21:51 +01:00
$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)(strtotime($end_time) - strtotime($start_time));
2020-11-08 15:21:51 +01:00
}