292 lines
7.7 KiB
PHP
292 lines
7.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];
|
|
|
|
$result = getRecordExternalData($result);
|
|
|
|
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], false);
|
|
$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");
|
|
}
|
|
}
|
|
|
|
$db->orderBy("start_time", Order::DESC);
|
|
$result = $db->execute();
|
|
|
|
if (count($result) <= 0) {
|
|
return null;
|
|
}
|
|
$result = $result[0];
|
|
|
|
$result = getRecordExternalData($result);
|
|
|
|
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 => $record) {
|
|
$results[$key] = getRecordExternalData($record);
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
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"));
|
|
|
|
$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 getRecordExternalData($record)
|
|
{
|
|
if ($record == null) {
|
|
return null;
|
|
}
|
|
|
|
// Duration
|
|
if ($record["end_time"] == NULL) {
|
|
$record["duration"] = calcDuration($record["start_time"]);
|
|
}
|
|
|
|
// Tags
|
|
$tags = array();
|
|
foreach (getTagsOnRecord($record["record_id"]) as $key => $tag) {
|
|
$data = [
|
|
"record_tag_id" => $tag["record_tag_id"],
|
|
"name" => $tag["name"],
|
|
"user_id" => $tag["user_id"]
|
|
];
|
|
$tags[] = $data;
|
|
}
|
|
$record["tags"] = $tags;
|
|
|
|
return $record;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
function getRecordTags($user_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("record_tags");
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
return $db->execute();
|
|
}
|
|
|
|
function getTagsOnRecord($record_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->select("tags_on_records");
|
|
$db->innerJoin("record_tags", "record_tag_id");
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
return $db->execute();
|
|
}
|
|
|
|
function addRecordTag($user_id, $tag_name)
|
|
{
|
|
$data = [
|
|
"user_id" => $user_id,
|
|
"name" => $tag_name
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("record_tags", $data);
|
|
$db->execute();
|
|
}
|
|
|
|
function addTagToRecord($tag_id, $record_id)
|
|
{
|
|
$data = [
|
|
"record_tag_id" => $tag_id,
|
|
"record_id" => $record_id
|
|
];
|
|
|
|
$db = new DbOperations();
|
|
$db->insert("tags_on_records", $data);
|
|
$db->execute();
|
|
}
|
|
|
|
function removeTagFromRecord($tag_id, $record_id)
|
|
{
|
|
$db = new DbOperations();
|
|
$db->delete("tags_on_records");
|
|
$db->where("record_tag_id", Comparison::EQUAL, $tag_id);
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
$db->execute();
|
|
}
|