CronController.php 3.75 KB
Newer Older
Marcello Pivanti's avatar
Marcello Pivanti committed
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
<?php

/**
 * Art-ER Attrattività, ricerca e territorio dell’Emilia-Romagna
 * OPEN 2.0
 *
 * @package    Open20Package
 * @category   CategoryName
 * @author     Elite Division S.r.l.
 */
namespace elitedivision\amos\videoconference\controllers;
//namespace console\controllers;


use elitedivision\amos\admin\models\UserProfile;
use elitedivision\amos\videoconference\AmosVideoconference;
use elitedivision\amos\videoconference\models\Videoconf;
use elitedivision\amos\videoconference\models\VideoconfUsersMm;
use elitedivision\amos\videoconference\utils\EmailUtil;
use yii\console\Controller;
use yii\helpers\Console;
use Yii;

class CronController extends Controller
{

    public function actionStart_video_conference(){
        $programmedConferences = Videoconf::find()->andWhere(['status' => Videoconf::STATUS_FUTURE])->all();

        Console::stdout("*** CRON START VIDEO CONFERENCE ***" . "\n");
        $count = 0;
        /** @var  $videoConference Videoconf */
        foreach ($programmedConferences as $videoConference) {
            if($videoConference->isVideoConferenceActivable() && empty($videoConference->id_room_videoconference)) {
                $videoConference->setIdRoomVideoconference();
                if($videoConference->save(false)) {
                    Console::stdout("videoconferences started: ID:" . print_r($videoConference->id, true) . " title: " . print_r($videoConference->title, true) . "\n");
                    $count++;
                }
            }
        }

        Console::stdout("n. videoconferences started: " . print_r($count,true) . "\n");
        Console::stdout("*** END CRON ***" . "\n");
    }


    /**
     *
     */
    public function actionSend_email_reminder()
    {
        $module = \Yii::$app->getModule(AmosVideoconference::getModuleName());
        // find scheduled videoconferences which haven't been sent a reminder
        $programmedConferences = Videoconf::find()
            ->andWhere(['status' => Videoconf::STATUS_FUTURE])
            ->andWhere(['reminder_sent' => 0])->all();
        $now = new \DateTime();

        Console::stdout("*** CRON SEND EMAIL REMINDER VIDEOCONFERENCE ***" . "\n");
        $count = 0;
        /** @var  $videoconference Videoconf */
        foreach ($programmedConferences as $videoconference) {
            $notification_before_conference = !empty($videoconference->notification_before_conference) ? $videoconference->notification_before_conference : $module->minuteReminder;
            $intervalReminder =  new \DateInterval('PT' . trim($notification_before_conference) . 'M');
            $dateHourReminder = new \DateTime($videoconference->begin_date_hour);
            $dateHourReminder->sub($intervalReminder);

            if($videoconference->status == Videoconf::STATUS_FUTURE) {
                if($now >= $dateHourReminder) {
                    $collegati = $videoconference->getVideoconfUsersMms()->all();
                    /** @var  $u VideoconfUsersMm */
                    foreach ((array)$collegati as $u) {
                        $profile = UserProfile::findOne([$u->user_profile_id]);
                        $sent = EmailUtil::sendNotification($videoconference, $profile->user);
                        if($sent) {
                            Console::stdout( print_r($videoconference->id, true) . " - reminder sent to: user_profile_id:" . print_r($profile->id, true) . " email: " . print_r($profile->user->email, true) . "\n");
                            $count++;
                        }
                    }
                    $videoconference->reminder_sent = 1;
                    $videoconference->save(false);
                }
            }
        }

        Console::stdout("n. Email sent: " . print_r($count,true) . "\n");
        Console::stdout("*** END CRON ***" . "\n");
    }



}