summaryrefslogtreecommitdiff
path: root/plugins/TimeMachine/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/TimeMachine/Controller')
-rw-r--r--plugins/TimeMachine/Controller/AnalyticsTimesController.php150
-rw-r--r--plugins/TimeMachine/Controller/SubtaskController.php100
2 files changed, 250 insertions, 0 deletions
diff --git a/plugins/TimeMachine/Controller/AnalyticsTimesController.php b/plugins/TimeMachine/Controller/AnalyticsTimesController.php
new file mode 100644
index 00000000..081d3be0
--- /dev/null
+++ b/plugins/TimeMachine/Controller/AnalyticsTimesController.php
@@ -0,0 +1,150 @@
+<?php
+
+namespace Kanboard\Plugin\TimeMachine\Controller;
+
+use Kanboard\Controller\BaseController;
+use Kanboard\Filter\TaskProjectFilter;
+use Kanboard\Model\TaskModel;
+
+
+/**
+ * @plugin TimeMachine
+ *
+ * @package Kanboard\Plugin\TimeMachine\Controller
+ * @author yvalentin
+ */
+class AnalyticsTimesController extends BaseController {
+
+ /**
+ * Show comparison between actual and estimated hours chart by Swimlane
+ *
+ * @access public
+ */
+ public function timeComparisonBySwimlane()
+ {
+ $project = $this->getProject();
+ $paginator = $this->getPaginator('timeComparisonBySwimlane', $project);
+
+ list($metrics, $swimlanes) = $this->helper->analyticsTimes->buildBySwimlane($project['id']);
+ $this->response->html(
+ $this->helper->layout->analytic('TimeMachine:analytic/time_comparison_by_swimlane',
+ array(
+ 'project' => $project,
+ 'swimlanes' => $swimlanes,
+ 'paginator' => $paginator,
+ 'metrics' => $metrics,
+ 'title' => t('Estimated vs actual time / Swimlanes'),
+ )
+ )
+ );
+ }
+
+ /**
+ * Show comparison between actual and estimated hours chart by Categories
+ *
+ * @access public
+ */
+ public function timeComparisonByCategories()
+ {
+ $project = $this->getProject();
+ $paginator = $this->getPaginator('timeComparisonByCategories', $project);
+
+ list($metrics, $categories) = $this->helper->analyticsTimes->buildByCategories($project['id']);
+ $this->response->html(
+ $this->helper->layout->analytic('TimeMachine:analytic/time_comparison_by_categories',
+ array(
+ 'project' => $project,
+ 'categories' => $categories,
+ 'paginator' => $paginator,
+ 'metrics' => $metrics,
+ 'title' => t('Estimated vs actual time / Categories'),
+ )
+ )
+ );
+ }
+
+ public function timeComparisonByDates()
+ {
+ $project = $this->getProject();
+ list($from, $to) = $this->getDates();
+ $paginatorParams = [
+ 'from' => $from->format('Y-m-d'),
+ 'to' => $to->format('Y-m-d')
+ ];
+ $paginator = $this->getPaginator('timeComparisonByDates', $project, $paginatorParams);
+
+ list($metrics, $categories, $swimlanes) = $this->helper->analyticsTimes->buildByDates($project['id'], $from, $to);
+
+ $this->response->html(
+ $this->helper->layout->analytic('TimeMachine:analytic/spent_time_by_dates',
+ array(
+ 'project' => $project,
+ 'categories' => $categories,
+ 'swimlanes' => $swimlanes,
+ 'from' => $from,
+ 'to' => $to,
+ 'userFormat' => $this->dateParser->getUserDateFormat(),
+ 'paginator' => $paginator,
+ 'metrics' => $metrics,
+ 'title' => t('Spent time / Dates'),
+ )
+ )
+ );
+
+ }
+
+ /**
+ * Get dates from/to from form request
+ *
+ * @return array
+ */
+ private function getDates()
+ {
+ $values = $this->request->getValues();
+
+ $from = $this->request->getStringParam('from', date('Y-m-d', strtotime('first day of this month')));
+ $to = $this->request->getStringParam('to', date('Y-m-d', strtotime('last day of this month')));
+
+ if (! empty($values)) {
+ $from = $this->dateParser->getIsoDate($values['from']);
+ $to = $this->dateParser->getIsoDate($values['to']);
+ }
+
+ // Return \DateTime
+ $from = (new \DateTime($from));
+ $to = (new \DateTime($to));
+ $to->setTime('23', '59', '59');
+
+ return array($from, $to);
+ }
+
+ /**
+ * @param $action
+ * @param $project
+ * @param array $params
+ *
+ * @return \Kanboard\Core\Paginator
+ */
+ private function getPaginator($action, $project, array $params = [])
+ {
+ $urlParams = array('plugin' => 'TimeMachine', 'project_id' => $project['id']);
+ if(!empty($params)) {
+ foreach ($params as $key => $param) {
+ $urlParams[$key] = $param;
+ }
+ }
+ return $this->paginator
+ ->setUrl(
+ 'AnalyticsTimesController',
+ $action,
+ $urlParams
+ )
+ ->setMax(30)
+ ->setOrder(TaskModel::TABLE.'.id')
+ ->setQuery($this->taskQuery
+ ->withFilter(new TaskProjectFilter($project['id']))
+ ->getQuery()
+ )
+ ->calculate();
+ }
+}
diff --git a/plugins/TimeMachine/Controller/SubtaskController.php b/plugins/TimeMachine/Controller/SubtaskController.php
new file mode 100644
index 00000000..423a0d88
--- /dev/null
+++ b/plugins/TimeMachine/Controller/SubtaskController.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Kanboard\Plugin\TimeMachine\Controller;
+
+use Kanboard\Controller\BaseController;
+use Kanboard\Core\Controller\AccessForbiddenException;
+use Kanboard\Core\Controller\PageNotFoundException;
+
+/**
+ * Subtask controller
+ *
+ * @package Kanboard\Plugin\TimeMachine\Controller
+ * @author yvalentin
+ */
+class SubtaskController extends BaseController
+{
+
+ /**
+ * Edit form with subTaskTimeTracking
+ *
+ * @access public
+ * @param array $values
+ * @param array $errors
+ * @throws AccessForbiddenException
+ * @throws PageNotFoundException
+ */
+ public function edit(array $values = [], array $valuesSttts = [], array $errors = [], array $errorsSttts = [])
+ {
+ $task = $this->getTask();
+ $subtask = $this->getSubtask($task);
+ $sttts = $this->subtaskTimeTrackingModel->getBySubTaskId($subtask['id']);
+
+ $this->response->html($this->template->render('subtask/edit', array(
+ 'values' => empty($values) ? $subtask : $values,
+ 'errors' => $errors,
+ 'errorsSttts' => $errorsSttts,
+ 'users_list' => $this->projectUserRoleModel->getAssignableUsersList($task['project_id']),
+ 'status_list' => $this->subtaskModel->getStatusList(),
+ 'sttts' => empty($valuesSttts) ? $sttts : $valuesSttts,
+ 'subtask' => $subtask,
+ 'task' => $task,
+ )));
+ }
+
+ /**
+ * Update and validate a subtask with subTaskTimeTracking
+ *
+ * @access public
+ */
+ public function update()
+ {
+ $task = $this->getTask();
+ $subtask = $this->getSubtask($task);
+
+ $values = $this->request->getValues();
+ $values['id'] = $subtask['id'];
+ $values['task_id'] = $task['id'];
+
+ // get subTaskTimeTracking values
+ $valuesSttts = $this->getSubTaskTimeTracking($values);
+
+ list($valid, $errors) = $this->subtaskValidator->validateModification($values);
+ list($validSttts, $errorsSttts) = $this->helper->subTaskTimeTrackingValidator->validates($valuesSttts);
+
+ if ($valid && !in_array(false, $validSttts)) {
+ if ($this->subtaskModel->update($values)
+ && $this->subtaskTimeTrackingModel->updates($valuesSttts, $subtask['id'], $task['id'])) {
+ $this->flash->success(t('Sub-task updated successfully.'));
+ } else {
+ $this->flash->failure(t('Unable to update your sub-task.'));
+ }
+
+ return $this->response->redirect($this->helper->url->to('TaskViewController', 'show', array('project_id' => $task['project_id'], 'task_id' => $task['id'])), true);
+ }
+
+ return $this->edit($values, $valuesSttts, $errors, $errorsSttts);
+ }
+
+ /**
+ * Get Sub Task Time Tracking from form values
+ *
+ * @param array $values
+ *
+ * @return array
+ */
+ private function getSubTaskTimeTracking(array &$values)
+ {
+ $sttts = [];
+ foreach ($values as $key => $value) {
+ if(strpos($key,'-') !== false) {
+ list($column, $id) = explode('-', $key);
+ $sttts[$id][$column] = $value;
+ $sttts[$id]['id'] = $id;
+ unset($values[$key]);
+ }
+ }
+
+ return $sttts;
+ }
+}