summaryrefslogtreecommitdiff
path: root/plugins/Timetrackingeditor/Model/SubtaskTimeTrackingEditModel.php
blob: a6ee24dc90d658af5610e2f33c33d9069e0c472f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

namespace Kanboard\Plugin\Timetrackingeditor\Model;

use Kanboard\Core\Base;
use Kanboard\Model\SubtaskModel;
use Kanboard\Model\TaskModel;

/**
 * Task Creation
 *
 * @package  Kanboard\Plugin\Timetrackingeditor\Model
 * @author   Thomas Stinner
 */
class SubtaskTimeTrackingEditModel extends Base
{

  /**
   * GetOpenTimer
   * Returns the Open (started but not finished) Time Tracking entry
   * for a specific user and subtask
   *
   * @access public
   * @param  integer    $user_id   User id
   * @param  integer    $subtask_id Subtask id
   * @return \Picodb\Table
   */
   public function getOpenTimer($user_id, $subtask_id)
   {
     return $this->db 
                  ->table(SubtaskTimeTrackingModel::TABLE)
                  ->columns(
                      SubtaskTimeTrackingModel::TABLE.'.id',
                      SubtaskTimeTrackingModel::TABLE.'.subtask_id',
                      SubtaskTimeTrackingModel::TABLE.'.end',
                      SubtaskTimeTrackingModel::TABLE.'.start',
                      SubtaskTimeTrackingModel::TABLE.'.time_spent',
                      SubtaskTimeTrackingModel::TABLE.'.comment',
                      SubtaskTimeTrackingModel::TABLE.'.is_billable'
                  )
                  ->eq(SubtaskTimeTrackingModel::TABLE.'.subtask_id', $subtask_id)
                  ->eq(SubtaskTimeTrackingModel::TABLE.'.user_id', $user_id)
                  ->eq(SubtaskTimeTrackingModel::TABLE.'.end', 0)
                  ->findOne();
   }

    /**
     * Get by Id
     *
     * @access public
     * @param int $id TimetrackingId
     * @return \PicoDb\Table
     */
     public function getById($id)
     {
       return $this->db
                   ->table(SubtaskTimeTrackingModel::TABLE)
                   ->columns(
                       SubtaskTimeTrackingModel::TABLE.'.id',
                       SubtaskTimeTrackingModel::TABLE.'.subtask_id',
                       SubtaskTimeTrackingModel::TABLE.'.end',
                       SubtaskTimeTrackingModel::TABLE.'.start',
                       SubtaskTimeTrackingModel::TABLE.'.time_spent',
                       SubtaskTimeTrackingModel::TABLE.'.comment',
                       SubtaskTimeTrackingModel::TABLE.'.is_billable',
                       SubtaskModel::TABLE.'.task_id',
                       SubtaskModel::TABLE.'.title AS subtask_title',
                       TaskModel::TABLE.'.title AS task_title',
                       TaskModel::TABLE.'.project_id',
                       TaskModel::TABLE.'.color_id'
                   )
                   ->join(SubtaskModel::TABLE, 'id', 'subtask_id')
                   ->join(TaskModel::TABLE, 'id', 'task_id', SubtaskModel::TABLE)
                   ->eq(SubtaskTimeTrackingModel::TABLE.'.id', $id)
                   ->findOne();

     }

    /**
     * Create a time tracking event
     *
     * @access public
     * @param  array    $values   Form values
     * @return integer
     */
    public function create(array $values)
    {

        $this->prepare($values);
        $subtrackingid = $this->db->table(SubtaskTimeTrackingModel::TABLE)->persist($values);

        return (int) $subtrackingid;
    }

    /**
     * Update a time tracking event
     *
     * @access public
     * @param array $values
     * @return boolean
     */
     public function update(array $values)
     {
       $this->prepare($values);

       return $this->db->table(SubtaskTimeTrackingModel::TABLE)->eq('id', $values['id'])->update($values);
     }


     /**
      * remove an entry
      *
      * @access public
      * @param int $id
      * @return boolran
      */
      public function remove($id)
      {
        return $this->db->table(SubtaskTimeTrackingModel::TABLE)->eq('id', $id)->remove();

      }

    /**
     * Prepare data
     *
     * @access public
     * @param  array    $values    Form values
     */
    public function prepare(array &$values)
    {
        if ($this->userSession->isLogged()) {
            $values['user_id'] = $this->userSession->getId();
        }

        $values["subtask_id"] = $values["opposite_subtask_id"];

        $this->helper->model->removeFields($values, array('project_id', 'task_id', 'opposite_subtask_id', 'subtask', 'add_another',  'old_time_spent', 'old_opposite_subtask_id'));

        // Calculate end time
        $values = $this->dateParser->convert($values, array('start'), true);
        $values["end"] = $values["start"] + ($values['time_spent']*60*60);
    }
}