xref: /plugin/bez/ctl/start.php (revision bc2653b00a718c1ef68486f970cffe35acb167aa)
1<?php
2/** @var action_plugin_bez $this */
3
4use \dokuwiki\plugin\bez;
5
6if ($this->model->get_level() < BEZ_AUTH_USER) {
7    throw new bez\meta\PermissionDeniedException();
8}
9
10//activity
11
12class Timeline {
13    protected $timeline = array();
14
15    protected function datetime($iso8601) {
16        $timestamp = strtotime($iso8601);
17        $date = date('Y-m-d', $timestamp);
18        $time = date('H:i', $timestamp);
19
20        return array($date, $time);
21    }
22
23    public function push($datetime, $type, $author, $entity) {
24        list($date, $time) = $this->datetime($datetime);
25
26        if (!isset($this->timeline[$date])) $timeline[$date] = array();
27        $this->timeline[$date][] = array('time' => $time,
28                                   'type' => $type,
29                                   'author' => $author,
30                                   'entity' => $entity);
31    }
32
33    public function get_assoc() {
34        //sort dates, iso8601 can be compared as strings
35        krsort($this->timeline);
36
37        //sort times
38        foreach ($this->timeline as &$elm) {
39            usort($elm, function ($a, $b) {
40                return -1 * strcmp($a['time'], $b['time']);
41            });
42        }
43        return $this->timeline;
44    }
45}
46
47$month_earlier = date('c', strtotime('-1 month'));
48$filter = array();
49$filter['create_date'] = array('>=', $month_earlier, array('date'));
50$threads = $this->model->threadFactory->get_all($filter, 'create_date DESC');
51$thread_comments = $this->model->thread_commentFactory->get_all($filter, 'create_date DESC');
52$tasks = $this->model->taskFactory->get_all($filter, 'create_date DESC');
53$task_comments = $this->model->task_commentFactory->get_all($filter, 'create_date DESC');
54
55$timeline = new Timeline();
56foreach ($threads as $thread) {
57    if ($thread->acl_of('id') < BEZ_PERMISSION_VIEW) continue;
58
59    $project = '';
60    if ($thread->type == 'project') {
61        $project = '_project';
62    }
63
64    if ($thread->state == 'proposal') {
65        $timeline->push($thread->create_date, 'thread_proposal' . $project, $thread->original_poster, $thread);
66    } else {
67        $timeline->push($thread->create_date, 'thread_opened' . $project, $thread->coordinator, $thread);
68    }
69
70    if ($thread->state == 'done') {
71        $timeline->push($thread->last_activity_date, 'thread_done' . $project, $thread->coordinator, $thread);
72    } elseif ($thread->state == 'closed') {
73        $timeline->push($thread->last_activity_date, 'thread_closed' . $project, $thread->coordinator, $thread);
74    } elseif ($thread->state == 'rejected') {
75        $timeline->push($thread->last_activity_date, 'thread_rejected' . $project, $thread->coordinator, $thread);
76    }
77}
78
79foreach ($thread_comments as $thread_comment) {
80    if ($thread_comment->thread->acl_of('id') < BEZ_PERMISSION_VIEW) continue;
81
82    if ($thread_comment->type == 'comment') {
83        $timeline->push($thread_comment->create_date, 'thread_comment_added', $thread_comment->author, $thread_comment);
84    } else {
85        $timeline->push($thread_comment->create_date, 'thread_comment_cause_added', $thread_comment->author, $thread_comment);
86    }
87}
88
89foreach ($tasks as $task) {
90    if ($task->acl_of('id') < BEZ_PERMISSION_VIEW) continue;
91
92    $timeline->push($task->create_date, 'task_opened', $task->assignee, $task);
93
94    if ($task->state == 'done') {
95        $timeline->push($task->last_activity_date, 'task_done', $task->assignee, $task);
96    }
97}
98
99foreach ($task_comments as $task_comment) {
100    if ($task_comment->task->acl_of('id') < BEZ_PERMISSION_VIEW) continue;
101
102    $timeline->push($task_comment->create_date, 'task_comment_added', $task_comment->author, $task_comment);
103}
104
105$this->tpl->set('timeline', $timeline->get_assoc());
106
107$orderby = array('sort', 'priority DESC', 'create_date DESC');
108$filter = array('state' => 'proposal');
109$proposals = $this->model->threadFactory->get_all($filter, $orderby);
110$this->tpl->set('proposals', $proposals);
111$this->tpl->set('proposals_count', $this->model->threadFactory->count($filter));
112
113$orderby = array('sort', 'priority DESC', 'create_date DESC');
114$filter = array('state' => 'opened', 'coordinator' => $this->model->user_nick);
115$my_threads = $this->model->threadFactory->get_all($filter, $orderby);
116$this->tpl->set('my_threads', $my_threads);
117$this->tpl->set('my_threads_count', $this->model->threadFactory->count($filter));
118
119$orderby = array('priority DESC', 'plan_date');
120$filter = array('state' => 'opened', 'assignee' => $this->model->user_nick);
121$my_tasks = $this->model->taskFactory->get_all($filter, $orderby);
122$this->tpl->set('my_tasks', $my_tasks);
123$this->tpl->set('my_tasks_count', $this->model->taskFactory->count($filter));
124
125$orderby = array('sort', 'priority DESC', 'create_date DESC');
126$filter = array('state' => 'opened', 'original_poster' => $this->model->user_nick);
127$reported_threads = $this->model->threadFactory->get_all($filter, $orderby);
128$this->tpl->set('reported_threads', $reported_threads);
129$this->tpl->set('reported_threads_count', $this->model->threadFactory->count($filter));
130
131$orderby = array('priority DESC', 'plan_date');
132$filter = array('state' => 'opened', 'original_poster' => $this->model->user_nick);
133$reported_tasks = $this->model->taskFactory->get_all($filter, $orderby);
134$this->tpl->set('reported_tasks', $reported_tasks);
135$this->tpl->set('reported_tasks_count', $this->model->taskFactory->count($filter));
136
137