xref: /plugin/bez/cron/functions.php (revision d571026c0d66403aeb1c67008848d46279174a93)
1<?php
2
3$action = new action_plugin_bez_base();
4$action->createObjects(true);
5
6function send_inactive_issue() {
7    global $action;
8
9    $threads = $action->get_model()->threadFactory->get_all(array(
10        'last_activity_date' => array('<=', date('c', strtotime('-26 days'))),
11        'state' => 'opened'
12    ));
13
14    foreach ($threads as $thread) {
15        //send reminder once a month
16        $day_of_issue_last_activity = date('d', strtotime($thread->last_activity_date));
17        if ($day_of_issue_last_activity == date('d')) {
18            //send message to all
19            $thread->mail_notify_inactive($thread->get_participants('subscribent'));
20        }
21    }
22}
23
24function send_task_reminder() {
25    global $action;
26
27    $filters = array('state' => 'opened', 'plan_date' => array('OR', array()));
28
29    $days_before = $action->getConf('task_remaind_days_before');
30    $days_before = array_map('trim', explode(',', $days_before));
31
32    if (count($days_before) == 0) {
33        return;
34    }
35
36    foreach($days_before as $day) {
37        $filters['plan_date'][1][] = date('Y-m-d', strtotime("+$day day"));
38    }
39
40
41    $tasks = $action->get_model()->taskFactory->get_all($filters);
42
43    $now = new DateTime(date('Y-m-d'));
44    foreach ($tasks as $task) {
45        $plan_date = new DateTime($task->plan_date);
46        $task->mail_notify_remind($task->get_participants('subscribent'), $plan_date->diff($now)->format('%a'));
47    }
48}
49
50function send_weekly_message() {
51    global $action;
52    global $auth;
53
54    //email => array('user' => array('issues' => array(), 'tasks' => array()))
55    $msg = array();
56    $output = array();
57
58    $threads = $action->get_model()->threadFactory->get_all(array(
59          'type' => 'issue',
60          'priority' => array('OR', array('2', '1'))
61      ));
62
63    foreach ($threads as $thread) {
64        $key = $thread->coordinator;
65        if (!isset($msg[$key])) {
66            $msg[$key] = array(
67                'issues'          => array(),
68                'coming_tasks'    => array(),
69                'outdated_tasks'  => array()
70            );
71        }
72        $msg[$key]['issues'][] = $thread;
73    }
74
75    $tasks  = $action->get_model()->taskFactory->get_all(array(
76        'priority' => array('OR', array('2', '1'))
77    ));
78
79    foreach ($tasks as $task) {
80        $key = $task->assignee;
81        if (!isset($msg[$key])) {
82            $msg[$key] = array(
83                'issues'          => array(),
84                'coming_tasks'    => array(),
85                'outdated_tasks'  => array()
86            );
87        }
88
89        if ($task->priority == '1') {
90            $msg[$key]['coming_tasks'][] = $task;
91        } else {
92            $msg[$key]['outdated_tasks'][] = $task;
93        }
94    }
95
96    //outdated_tasks, coming_tasks, open_tasks
97
98
99    foreach ($msg as $user => $data) {
100        $udata = $auth->getUserData($user);
101
102        $issues = $data['issues'];
103        $outdated_tasks = $data['outdated_tasks'];
104        $coming_tasks = $data['coming_tasks'];
105
106
107        if (count($issues) + count($outdated_tasks) + count($coming_tasks) == 0)
108            continue;
109
110        $to = $udata['name'].' <'.$udata['mail'].'>';
111
112        $tpl = $action->get_tpl();
113        $tpl->set('issues', $issues);
114        $tpl->set('outdated_tasks', $outdated_tasks);
115        $tpl->set('coming_tasks', $coming_tasks);
116        $body = $action->bez_tpl_include('cron/weekly-message', true);
117
118        $mailer = new \dokuwiki\plugin\bez\meta\Mailer();
119        $rep = array();
120        $mailer->setBody('', $rep, NULL, $body, false);
121
122        $mailer->to($to);
123        $subject = 'Nadchodzące zadania';
124        $mailer->subject($subject);
125
126        $mailer->send();
127        $output[] = array($to, $subject, $body, array());
128    }
129
130    return $output;
131}
132