VideoconfController.php 20.2 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?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;

use elitedivision\amos\admin\models\UserProfile;
use elitedivision\amos\core\user\User;
use elitedivision\amos\core\utilities\Email;
use elitedivision\amos\notificationmanager\models\NotificationsRead;
use elitedivision\amos\videoconference\models\base\UserProfileForm;
use elitedivision\amos\videoconference\models\VideoconfUsersMm;
use elitedivision\amos\videoconference\utils\EmailUtil;
use Yii;
use elitedivision\amos\videoconference\models\Videoconf;
use elitedivision\amos\videoconference\models\VideoconfSearch;
use elitedivision\amos\core\controllers\CrudController;
use elitedivision\amos\core\icons\AmosIcons;
use elitedivision\amos\core\helpers\Html;
use elitedivision\amos\core\helpers\T;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\data\ActiveDataProvider;
use elitedivision\amos\videoconference\AmosVideoconference;

/**
 * This is the class for controller "VideoconfController".
 */
class VideoconfController extends CrudController
{
    public $model_partecipanti;
    public $partecipanti;

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        $behaviors = ArrayHelper::merge(parent::behaviors(),
                [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'allow' => true,
                            'actions' => [
                                'meet',
                                'json-videoconf-data',
                            ],
                            'roles' => ['VIDEOCONF_READ']
                        ],
                    ]
                ],
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['post', 'get']
                    ]
                ]
        ]);
        return $behaviors;
    }

    /**
     * manda la mail agli utenti collegati alla videoconferenza
     */
    public function actionSendMail()
    {

        // trova la videoconferenza e gli utenti collegati
        $videoconfId = Yii::$app->request->get('id');

        $videoconference = Videoconf::findOne($videoconfId);
        if ($videoconference) {
            $collegati = $videoconference->getVideoconfUsersMms()->all();
            if (\is_array($collegati)) {
                foreach ($collegati as $u) {
                    $sent = EmailUtil::sendEmailPartecipant($videoconference, $u->user_id);
                }
            }
        }
    }

    public function init()
    {
        $this->setModelObj(new Videoconf());
        $this->setModelSearch(new VideoconfSearch());

        $this->setAvailableViews([
            'grid' => [
                'name' => 'grid',
                'label' => Yii::t('amoscore', '{iconaTabella}'.Html::tag('p', Yii::t('amoscore', 'Table')),
                    [
                    'iconaTabella' => AmosIcons::show('view-list-alt')
                ]),
                'url' => '?currentView=grid'
            ],
            /* 'list' => [
              'name' => 'list',
              'label' => Yii::t('amoscore', '{iconaLista}'.Html::tag('p',Yii::t('amoscore', 'List')), [
              'iconaLista' => AmosIcons::show('view-list')
              ]),
              'url' => '?currentView=list'
              ],
              'icon' => [
              'name' => 'icon',
              'label' => Yii::t('amoscore', '{iconaElenco}'.Html::tag('p',Yii::t('amoscore', 'Icons')), [
              'iconaElenco' => AmosIcons::show('grid')
              ]),
              'url' => '?currentView=icon'
              ],
              'map' => [
              'name' => 'map',
              'label' => Yii::t('amoscore', '{iconaMappa}'.Html::tag('p',Yii::t('amoscore', 'Map')), [
              'iconaMappa' => AmosIcons::show('map')
              ]),
              'url' => '?currentView=map'
              ],
              'calendar' => [
              'name' => 'calendar',
              'intestazione' => '', //codice HTML per l'intestazione che verrà caricato prima del calendario,
              //per esempio si può inserire una funzione $model->getHtmlIntestazione() creata ad hoc
              'label' => Yii::t('amoscore', '{iconaCalendario}'.Html::tag('p',Yii::t('amoscore', 'Calendar')), [
              'iconaMappa' => AmosIcons::show('calendar')
              ]),
              'url' => '?currentView=calendar'
              ], */
        ]);

        parent::init();
    }

    public function actionTest()
    {
        //$videoconference = new \elitedivision\amos\videoconference\models\Videoconf();
        $videoconference = \elitedivision\amos\videoconference\models\Videoconf::findOne(1);
        // pr($videoconference->toArray(), 'videoconference');//exit;
        $users           = $videoconference->getVideoconfUsersMms()->all();
        // $users = $videoconference->getVideoconfUsers();

        foreach ($users as $u) {
            pr($u->toArray(), '$u relazione'); //exit;
            $userProfile = \elitedivision\amos\admin\models\UserProfile::findOne($u->user_id);
            // pr($userProfile->toArray(), '$userProfile');//exit;
            $user        = $userProfile->getUser()->one();
            // pr($user->toArray(), '$user');//exit;
            $userEmail   = $userProfile->getUser()->one()->email;
            //  print "Email: $userEmail;<br />";
        }
        //pr($users->toArray(), '$users');exit;/****/
    }

    /**
     * Lists all Videoconf models.
     * @return mixed
     */
    public function actionIndex($layout = null)
    {
        Url::remember();

        //label pulsante "nuovo"
        Yii::$app->view->params['createNewBtnParams'] = [
            'createNewBtnLabel' => AmosVideoconference::t('amosvideoconference', 'Nuova Videoconferenza'),
        ];

        $filter = ArrayHelper::merge(Yii::$app->request->getQueryParams(), [
        ]);
        $this->setDataProvider($this->getModelSearch()->searchAll(Yii::$app->request->getQueryParams()));
        return parent::actionIndex();
    }

    /**
     * Displays a single Videoconference.
     * @param integer $id
     * @return mixed
     */
    public function actionMeet($id_room)
    {
        $record = Videoconf::findOne(['id_room_videoconference' => $id_room]);
        if (is_object($record) && $record->id) {
            $id    = $record->id;
            $model = $this->findModel($id);
        }
        return $this->render('meet', ['model' => $model]);
    }

    /**
     * @param $id
     * @return string
     * @throws NotFoundHttpException
     */
    public function actionJsonVideoconfData($id)
    {
        $model = $this->findModel($id);

        $jitsiDomain           = Yii::$app->getModule('videoconference')->jitsiDomain ?: null;

        $loggedUser        = \Yii::$app->getUser()->identity;
        $loggedUserProfile = $loggedUser->getProfile();

        //pr($loggedUserProfile);
        $data = [
            "domain" => $jitsiDomain,
            "displayName" => $loggedUserProfile->nome." ".$loggedUserProfile->cognome,
            "roomName" => $model->id_room_videoconference,
            "titolo" => $model->title,
            "avatar" => $loggedUserProfile->getAvatarWebUrl()
        ];
        return json_encode($data);
    }

    /**
     * Displays a single Videoconf model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('view', ['model' => $model]);
        }
    }

    /**
     * Creates a new Videoconf model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $this->layout = "@vendor/elitedivision/amos-core/views/layouts/form";
        $model        = new Videoconf;

        //carico i partecipanti ottenendo le variabili $this->model_partecipanti e $this->partecipanti popolate
        $this->loadPartecipanti($id = null);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            //creo una transazione in modo che se non salvasse correttamente i partecipanti, non si creerebbe un record video
            $transaction = \Yii::$app->db->beginTransaction();

            $scope = $this->getScopeCommunity();
            if ($scope) {
                $model->community_id = $scope;
            }

            if ($model->save()) {

                $this->model_partecipanti->videoconf_id = $model->id;

                //aggiorno i partecipanti della conferenza con quelli in post
                $this->setPartecipanti(Yii::$app->request->post()[$this->model_partecipanti->getModelName()]['ids']);

                //salvo i partecipanti e creo una notification (se non già inviata) per inviare successivamente la mail
                $saved_partecipanti = $this->model_partecipanti->saveUser2Videoconference();

                if ($saved_partecipanti) {
                    $transaction->commit();

                    //Notifica i partecipanti ancora da avvisare
                    $notifiche = $model->getNotification2Notify();
                    $this->notifyUsers($notifiche);

                    Yii::$app->getSession()->addFlash('success', Yii::t('amoscore', 'Item created'));
                    return $this->redirect(['index']);
                } else {
                    $transaction->rollBack();

                    Yii::$app->getSession()->addFlash('danger',
                        Yii::t('amoscore', 'Item not created, check partecipants'));
                    return $this->render('create',
                            [
                            'model' => $model,
                            'model_partecipanti' => $this->model_partecipanti,
                            'partecipanti' => $this->partecipanti,
                    ]);
                }
            } else {
                $transaction->rollBack();
                Yii::$app->getSession()->addFlash('danger', Yii::t('amoscore', 'Item not created, check data'));
                return $this->render('create',
                        [
                        'model' => $model,
                        'model_partecipanti' => $this->model_partecipanti,
                        'partecipanti' => $this->partecipanti,
                ]);
            }
        } else {
            return $this->render('create',
                    [
                    'model' => $model,
                    'model_partecipanti' => $this->model_partecipanti,
                    'partecipanti' => $this->partecipanti,
            ]);
        }
    }

    /**
     * Updates an existing Videoconf model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $this->layout = "@vendor/elitedivision/amos-core/views/layouts/form";
        $model        = $this->findModel($id);

        //carico i partecipanti ottenendo le variabili $this->model_partecipanti e $this->partecipanti popolate
        $this->loadPartecipanti($id);

        /* pr(Yii::$app->request->post(), "post"); */
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            //aggiorno i partecipanti della conferenza con quelli in post
            $this->setPartecipanti(Yii::$app->request->post()[$this->model_partecipanti->getModelName()]['ids']);

            if ($model->save()) {
                //salvo i partecipanti e creo una notification (se non già inviata) per inviare successivamente la mail
                $saved_partecipanti = $this->model_partecipanti->saveUser2Videoconference();

                //Notifica i partecipanti ancora da avvisare
                $notifiche = $model->getNotification2Notify();
                $this->notifyUsers($notifiche);

                Yii::$app->getSession()->addFlash('success', Yii::t('amoscore', 'Item updated'));
                return $this->redirect(['index']);
            } else {
                Yii::$app->getSession()->addFlash('danger', Yii::t('amoscore', 'Item not updated, check data'));
                return $this->render('update',
                        [
                        'model' => $model,
                        'model_partecipanti' => $this->model_partecipanti,
                        'partecipanti' => $this->partecipanti,
                ]);
            }
        } else {
            $model->sheduledVideoconfCheckBox = !empty($model->begin_date_hour);
            return $this->render('update',
                    [
                    'model' => $model,
                    'model_partecipanti' => $this->model_partecipanti,
                    'partecipanti' => $this->partecipanti,
            ]);
        }
    }

    /**
     * Deletes an existing Videoconf model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if ($model) {
//si può sostituire il  delete() con forceDelete() in caso di SOFT DELETE attiva 
//In caso di soft delete attiva e usando la funzione delete() non sarà bloccata
//la cancellazione del record in presenza di foreign key quindi 
//il record sarà cancelleto comunque anche in presenza di tabelle collegate a questo record
//e non saranno cancellate le dipendenze e non avremo nemmeno evidenza della loro presenza
//In caso di soft delete attiva è consigliato modificare la funzione oppure utilizzare il forceDelete() che non andrà 
//mai a buon fine in caso di dipendenze presenti sul record da cancellare
            $model->delete();
            if (!empty($model->deleted_at)) {
                //cancello tutti partecipanti eventualmente presenti
                VideoconfUsersMm::deleteAll(['videoconf_id' => $id]);

                Yii::$app->getSession()->addFlash('success', Yii::t('amoscore', 'Item deleted'));
            } else {
                Yii::$app->getSession()->addFlash('danger', Yii::t('amoscore', 'Item not deleted because of dependency'));
            }
        } else {
            Yii::$app->getSession()->addFlash('danger', Yii::t('amoscore', 'Item not found'));
        }
        return $this->redirect(['index']);
    }

    public function loadPartecipanti($videoconf_id)
    {
        $this->model_partecipanti               = new UserProfileForm();
        $this->model_partecipanti->videoconf_id = $videoconf_id;
        //$this->model_partecipanti->load(\Yii::$app->request->post());
        /* if ($this->model_partecipanti->load(\Yii::$app->request->post())) {
          if ($this->model_partecipanti->validate()) {
          //TODO
          //$model->saveFavorites();
          //return $this->redirect(['index']);
          }
          } */
        //carica i partecipanti (da DB) per la conferenza settata: popola $this->ids
        $this->model_partecipanti->loadUsers();

        $this->partecipanti = UserProfileForm::getAvailableUsers();
    }

    /**
     * @param $ids
     * @param bool $videoconf_id
     * Setta gli id degli user_profile ricevuti
     */
    public function setPartecipanti($ids, $videoconf_id = false)
    {
        if (!$this->model_partecipanti) {
            $this->model_partecipanti = new UserProfileForm();
        }
        $this->model_partecipanti->ids = $ids;
    }

    public function notifyPartecipantiProgrammed($videoconf_id)
    {
        $VideoconfRecord = Videoconf::findOne(['id' => $videoconf_id]);

        if (is_object($VideoconfRecord) && $VideoconfRecord->id) {
            $a = $VideoconfRecord->getNotification2Notify(/* $VideoconfRecord->id */);
        }
    }

    /**
     * @param $notificaRecord
     * @return bool
     * dato un record notification, crea un corrispondente record NotificationRead
     */
    protected function createNotificationRead($notificaRecord)
    {
        if (is_object($notificaRecord) && $notificaRecord->id) {
            $notiRead                  = new NotificationsRead();
            $notiRead->user_id         = $notificaRecord->user_id;
            $notiRead->notification_id = $notificaRecord->id;
            //pr($notiRead->toArray(), "SALVEREI notification READ");
            $notiRead->save(false);
        }

        return false;
    }

    /**
     * @param $notifiche Array Records notification
     * Dato un array contentente i record notification da notificare: invia la mail di notifica agli utenti settati nel record in questione
     */
    protected function notifyUsers($notifiche)
    {
        foreach ($notifiche as $k_n => $notificaRecord) {
            //pr($notificaRecord->toArray(), "---- notificare");

            $model_record = $notificaRecord->class_name;
            $id_record    = $notificaRecord->content_id;
            $id_user      = $notificaRecord->user_id;

            //$userProfileModel = AmosAdmin::instance()->createModel('UserProfile');
            $userRecord = User::findOne($id_user);

            //risalgo al record videoconferenza salvato nella notifica
            $videoconfRecord = $model_record::findOne($id_record);

            //invio email
            $send = EmailUtil::sendEmailPartecipant($videoconfRecord, $userRecord);

            //se la email è stata inviata, scrivo in notificationRead in modo da settare la notifica come 'letta'
            if ($send) {
                $this->createNotificationRead($notificaRecord);
            }
        }
    }

    /**
     * Create a video conference between to users
     *
     * @param $user_profile_id_sender
     * @param $user_profile_id_receiver
     * @return null|string
     */
    public function actionCreateVideoConferenceAjax($user_profile_id_sender, $user_profile_id_receiver)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        $profile                     = UserProfile::find()->andWhere(['user_id' => $user_profile_id_sender])->one();
        $videoconfList               = Videoconf::find()
                ->innerJoinWith('videoconfUsersMms')
                ->andWhere(['videoconf.created_by' => $profile->user_id])
                ->andWhere(['status' => Videoconf::STATUS_RUNNING])
                ->andWhere(['videoconf_users_mm.user_profile_id' => $user_profile_id_receiver])->all();

        $model = null;
        /** @var  $videoconf Videoconf */
        foreach ($videoconfList as $videoconf) {
            if ($videoconf->isVideoConferenceRoomActive() && count($videoconf->videoconfUsersMms) == 2) {
                $model = $videoconf;
            }
        }
        // if there is a videconference is between the two users  already active
        if (!empty($model)) {
            return $model->id_room_videoconference;
        } else {
            // create a new videoconference between two users
            $model        = new Videoconf();
            $model->title = "Videoconference ".$user_profile_id_sender."-".$user_profile_id_receiver;
            if ($model->save()) {
                $partecipante1                  = new VideoconfUsersMm();
                $partecipante1->videoconf_id    = $model->id;
                $partecipante1->user_profile_id = $user_profile_id_sender;

                $partecipante2                  = new VideoconfUsersMm();
                $partecipante2->videoconf_id    = $model->id;
                $partecipante2->user_profile_id = $user_profile_id_receiver;

                if ($partecipante1->save() && $partecipante2->save()) {
                    return $model->id_room_videoconference;
                }
            }
        }

        return null;
    }

    /**
     * @return null
     */
    public function getScopeCommunity()
    {
        $moduleCwh = Yii::$app->getModule('cwh');
        if (!is_null($moduleCwh)) {
            $scope = $moduleCwh->getCwhScope();
            if (isset($scope['community'])) {
                return $scope['community'];
            }
        }
        return null;
    }
}