2021-01-03 12:29:41 +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 addProject($user_id, $params)
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
"user_id" => $user_id,
|
|
|
|
"name" => $params->get("name"),
|
|
|
|
"start_date" => $params->get("start_date")
|
|
|
|
];
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->insert("projects", $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 getUser($user_id)
|
|
|
|
{
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->select("users", ["user_id", "name", "mail_address"]);
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$result = $db->execute();
|
|
|
|
|
|
|
|
if (count($result) <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$result = $result[0];
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-13 15:32:10 +02:00
|
|
|
function getRecords($user_id, $limit = NULL, $finished = NULL, $visible = NULL)
|
2021-01-03 12:29:41 +01:00
|
|
|
{
|
|
|
|
$db = new DbOperations();
|
2021-04-13 19:10:12 +02:00
|
|
|
$db->select("time_records", ["record_id", "start_time", "end_time", "duration", "user_id", "project_id", "start_device_id"]);
|
|
|
|
if ($visible != NULL) {
|
|
|
|
$db->innerJoin("projects", "project_id");
|
|
|
|
}
|
2021-04-13 19:21:19 +02:00
|
|
|
$db->where("ju_time_records.user_id", Comparison::EQUAL, $user_id);
|
|
|
|
if ($visible != NULL) {
|
|
|
|
$db->where("ju_projects.visible", Comparison::EQUAL, $visible);
|
|
|
|
}
|
2021-01-03 12:29:41 +01:00
|
|
|
if ($finished != NULL) {
|
|
|
|
if ($finished) {
|
|
|
|
$db->addSql(" AND end_time IS NOT NULL");
|
|
|
|
} else {
|
|
|
|
$db->addSql(" AND end_time IS NULL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($limit != NULL) {
|
|
|
|
$db->orderBy("record_id", Order::DESC);
|
|
|
|
$db->limit($limit);
|
|
|
|
}
|
|
|
|
$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"];
|
|
|
|
|
2021-01-03 20:37:15 +01:00
|
|
|
$record = [
|
|
|
|
"record_id" => $record_id,
|
2021-01-03 12:29:41 +01:00
|
|
|
"end_time" => $params->get("end_time"),
|
|
|
|
"duration" => calcDuration($start_time, $params->get("end_time"))
|
|
|
|
];
|
|
|
|
|
2021-01-03 20:37:15 +01:00
|
|
|
updateRecord($user_id, $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRecord($user_id, $record)
|
|
|
|
{
|
|
|
|
$record_id = $record["record_id"];
|
|
|
|
|
|
|
|
// Update given parameters
|
|
|
|
$data = [];
|
|
|
|
$props = ["end_time", "start_time", "duration", "project_id"];
|
|
|
|
foreach ($props as $p) {
|
|
|
|
if (array_key_exists ($p, $record)) {
|
|
|
|
$data[$p] = $record[$p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:29:41 +01:00
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:19:18 +01:00
|
|
|
function updateProject($user_id, $project)
|
|
|
|
{
|
|
|
|
$project_id = $project["project_id"];
|
|
|
|
|
|
|
|
// Update given parameters
|
|
|
|
$data = [];
|
2021-01-08 00:01:42 +01:00
|
|
|
$props = ["name", "start_date", "color", "visible"];
|
2021-01-07 23:19:18 +01:00
|
|
|
foreach ($props as $p) {
|
|
|
|
if (array_key_exists ($p, $project)) {
|
|
|
|
$data[$p] = $project[$p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->update("projects", $data);
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRecordTag($user_id, $tag)
|
|
|
|
{
|
|
|
|
$record_tag_id = $tag["record_tag_id"];
|
|
|
|
|
|
|
|
// Update given parameters
|
|
|
|
$data = [];
|
2021-04-13 00:27:41 +02:00
|
|
|
$props = ["name", "visible", "bundle"];
|
2021-01-07 23:19:18 +01:00
|
|
|
foreach ($props as $p) {
|
|
|
|
if (array_key_exists ($p, $tag)) {
|
|
|
|
$data[$p] = $tag[$p];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->update("record_tags", $data);
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:29:41 +01:00
|
|
|
function removeRecord($user_id, $params)
|
|
|
|
{
|
|
|
|
$record_id = $params->get("record_id");
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->delete("time_records");
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("record_id", Comparison::EQUAL, $record_id);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
|
2021-01-07 23:19:18 +01:00
|
|
|
function removeProject($user_id, $params)
|
|
|
|
{
|
|
|
|
$project_id = $params->get("project_id");
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->delete("projects");
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
|
|
$db->execute();
|
|
|
|
|
|
|
|
removeRecordsOfProject($user_id, $project_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeRecordsOfProject($user_id, $project_id)
|
|
|
|
{
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->delete("time_records");
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("project_id", Comparison::EQUAL, $project_id);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeRecordTag($user_id, $params)
|
|
|
|
{
|
|
|
|
$record_tag_id = $params->get("record_tag_id");
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->delete("record_tags");
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
|
|
$db->execute();
|
|
|
|
|
|
|
|
removeRecordTagFromAssociations($record_tag_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeRecordTagFromAssociations($record_tag_id)
|
|
|
|
{
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->delete("tags_on_records");
|
|
|
|
$db->where("record_tag_id", Comparison::EQUAL, $record_tag_id);
|
|
|
|
$db->execute();
|
|
|
|
}
|
|
|
|
|
2021-01-03 12:29:41 +01:00
|
|
|
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 and running
|
|
|
|
if ($record["end_time"] == NULL) {
|
|
|
|
$record["duration"] = calcDuration($record["start_time"]);
|
|
|
|
$record["running"] = true;
|
|
|
|
} else {
|
|
|
|
$record["running"] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tags
|
|
|
|
$tags = array();
|
|
|
|
foreach (getTagsOnRecord($record["record_id"]) as $key => $tag) {
|
|
|
|
$data = [
|
|
|
|
"record_tag_id" => $tag["record_tag_id"],
|
|
|
|
"name" => $tag["name"],
|
2021-04-13 00:27:41 +02:00
|
|
|
"user_id" => $tag["user_id"],
|
|
|
|
"visible" => $tag["visible"],
|
|
|
|
"bundle" => $tag["bundle"]
|
2021-01-03 12:29:41 +01:00
|
|
|
];
|
|
|
|
$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();
|
|
|
|
}
|
2021-05-23 00:43:12 +02:00
|
|
|
|
|
|
|
function getStats($user_id, $from_date, $until_date) {
|
|
|
|
$sum_duration = "SUM(ju_time_records.duration) AS duration";
|
|
|
|
$date = "DATE(ju_time_records.start_time) AS date";
|
|
|
|
$record_count = "COUNT(*) AS record_count";
|
|
|
|
|
|
|
|
$where1 = " AND DATE(ju_time_records.start_time) >= DATE( ";
|
|
|
|
$where2 = " ) AND DATE(ju_time_records.start_time) <= DATE( ";
|
|
|
|
$where3 = " ) AND ju_time_records.end_time IS NOT NULL ";
|
|
|
|
|
|
|
|
$db = new DbOperations();
|
|
|
|
$db->select("projects", ["name", "project_id", "color", "visible", $sum_duration, $date, $record_count], false);
|
|
|
|
$db->innerJoin("time_records", "project_id");
|
|
|
|
|
|
|
|
$db->where("user_id", Comparison::EQUAL, $user_id);
|
|
|
|
$db->addSql($where1);
|
|
|
|
$db->addValue($from_date);
|
|
|
|
$db->addSql($where2);
|
|
|
|
$db->addValue($until_date);
|
|
|
|
$db->addSql($where3);
|
|
|
|
|
|
|
|
$db->groupBy(["project_id", "date"]);
|
|
|
|
return $db->execute();
|
|
|
|
}
|