1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use common\models\Category; |
7
|
|
|
use common\models\Behavior; |
8
|
|
|
use common\interfaces\UserInterface; |
9
|
|
|
use common\interfaces\UserBehaviorInterface; |
10
|
|
|
use common\Interfaces\TimeInterface; |
11
|
|
|
use yii\di\Container; |
12
|
|
|
use yii\base\InvalidParamException; |
13
|
|
|
use yii\web\BadRequestHttpException; |
14
|
|
|
use yii\web\Controller; |
15
|
|
|
use yii\filters\VerbFilter; |
16
|
|
|
use common\components\AccessControl; |
17
|
|
|
use yii\helpers\ArrayHelper as AH; |
18
|
|
|
|
19
|
|
|
class CheckinController extends \yii\web\Controller |
20
|
|
|
{ |
21
|
|
|
public function behaviors() |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
|
|
'access' => [ |
25
|
|
|
'class' => AccessControl::class, |
26
|
|
|
'only' => ['index', 'view', 'questions', 'report'], |
27
|
|
|
'rules' => [ |
28
|
|
|
[ |
29
|
|
|
'actions' => ['index', 'view', 'questions', 'report'], |
30
|
|
|
'allow' => true, |
31
|
|
|
'roles' => ['@'], |
32
|
|
|
], |
33
|
|
|
], |
34
|
|
|
], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
public function actionIndex() { |
38
|
|
|
$form = Yii::$container->get(\site\models\CheckinForm::class); |
39
|
|
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
40
|
|
|
$form->compiled_behaviors = $form->compileBehaviors(); |
41
|
|
|
|
42
|
|
|
if(sizeof($form->compiled_behaviors) === 0) { |
43
|
|
|
return $this->redirect(['view']); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// we only store one data set per day, so wipe out previously saved ones |
47
|
|
|
$form->deleteToday(); |
48
|
|
|
$form->save(); |
49
|
|
|
|
50
|
|
|
return $this->redirect(['questions']); |
51
|
|
|
} else { |
52
|
|
|
$category = Yii::$container->get(\common\interfaces\CategoryInterface::class); |
53
|
|
|
$behavior = Yii::$container->get(\common\interfaces\BehaviorInterface::class); |
54
|
|
|
return $this->render('index', [ |
55
|
|
|
'categories' => $category::$categories, |
56
|
|
|
'model' => $form, |
57
|
|
|
'behaviorsList' => AH::index($behavior::$behaviors, null, "category_id") |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function actionQuestions() |
63
|
|
|
{ |
64
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
65
|
|
|
$date = Yii::$container->get(\common\interfaces\TimeInterface::class)->getLocalDate(); |
66
|
|
|
|
67
|
|
|
$user_behaviors = $user_behavior->getUserBehaviorsWithCategory($date); |
68
|
|
|
if(count($user_behaviors) === 0) { |
69
|
|
|
return $this->redirect(['view']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$form = Yii::$container->get(\site\models\QuestionForm::class); |
73
|
|
|
if ($form->load(Yii::$app->request->post()) && $form->validate()) { |
74
|
|
|
// we only store one data set per day so clear out any previously saved ones |
75
|
|
|
$form->deleteToday(); |
76
|
|
|
|
77
|
|
|
$behaviors = $user_behavior->findAll($form->getUserBehaviorIds()); |
78
|
|
|
if($result = $form->saveAnswers($behaviors)) { |
|
|
|
|
79
|
|
|
|
80
|
|
|
if(Yii::$app->user->identity->send_email) { |
|
|
|
|
81
|
|
|
Yii::$app->user->identity->sendEmailReport($date); |
|
|
|
|
82
|
|
|
Yii::$app->session->setFlash('success', 'Your check-in is complete. A notification has been sent to your report partners.'); |
|
|
|
|
83
|
|
|
} else { |
84
|
|
|
Yii::$app->session->setFlash('success', 'Your check-in is complete.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->redirect(['view']); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->render('questions', [ |
92
|
|
|
'model' => $form, |
93
|
|
|
'behaviors' => $user_behaviors |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function actionView(string $date = null) |
98
|
|
|
{ |
99
|
|
|
$time = Yii::$container->get(\common\interfaces\TimeInterface::class); |
100
|
|
|
if(is_null($date)) { |
101
|
|
|
$date = $time->getLocalDate(); |
102
|
|
|
} else if($dt = $time->parse($date)) { |
103
|
|
|
$date = $dt->format('Y-m-d'); |
104
|
|
|
} else { |
105
|
|
|
$date = $time->getLocalDate(); |
106
|
|
|
} |
107
|
|
|
$dt = $time->parse($date); |
108
|
|
|
list($start, $end) = $time->getUTCBookends($date); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$user = Yii::$container->get(\common\interfaces\UserInterface::class); |
111
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
112
|
|
|
$category = Yii::$container->get(\common\interfaces\CategoryInterface::class); |
113
|
|
|
$behavior = Yii::$container->get(\common\interfaces\BehaviorInterface::class); |
114
|
|
|
|
115
|
|
|
$form = Yii::$container->get(\site\models\CheckinForm::class); |
116
|
|
|
$form->setBehaviors($user->getUserBehaviors($date)); |
117
|
|
|
|
118
|
|
|
return $this->render('view', [ |
119
|
|
|
'model' => $form, |
120
|
|
|
'actual_date' => $date, |
121
|
|
|
'categories' => $category::$categories, |
122
|
|
|
'behaviorsList' => AH::index($behavior::$behaviors, 'name', "category_id"), |
123
|
|
|
'past_checkin_dates' => $user_behavior->getPastCheckinDates(), |
124
|
|
|
'answer_pie' => $user_behavior->getBehaviorsByCategory($dt), |
125
|
|
|
'questions' => $user->getUserQuestions($date), |
126
|
|
|
'isToday' => $time->getLocalDate() === $date, |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function actionReport() { |
131
|
|
|
$user_behavior = Yii::$container->get(\common\interfaces\UserBehaviorInterface::class); |
132
|
|
|
$user_rows = $user_behavior->getTopBehaviors(); |
133
|
|
|
$answer_pie = $user_behavior->getBehaviorsByCategory(); |
134
|
|
|
|
135
|
|
|
return $this->render('report', [ |
136
|
|
|
'top_behaviors' => $user_rows, |
137
|
|
|
'answer_pie' => $answer_pie, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|