forked from humhub/polls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Events.php
180 lines (150 loc) · 5.58 KB
/
Events.php
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
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\polls;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\polls\models\Poll;
use Yii;
use humhub\modules\polls\models\PollAnswer;
use humhub\modules\polls\models\PollAnswerUser;
/**
* Description of Events
*
* @author luke
*/
class Events
{
public static function onWallEntryControlsInit($event)
{
$object = $event->sender->object;
if(!$object instanceof Poll) {
return;
}
if($object->content->canWrite()) {
$event->sender->addWidget(\humhub\modules\polls\widgets\CloseButton::className(), [
'poll' => $object
]);
}
if($object->isResetAllowed()) {
$event->sender->addWidget(\humhub\modules\polls\widgets\ResetButton::className(), [
'poll' => $object
]);
}
}
/**
* On build of a Space Navigation, check if this module is enabled.
* When enabled add a menu item
*
* @param type $event
*/
public static function onSpaceMenuInit($event)
{
$space = $event->sender->space;
// Is Module enabled on this workspace?
if ($space->isModuleEnabled('polls')) {
$event->sender->addItem(array(
'label' => Yii::t('PollsModule.base', 'Polls'),
'group' => 'modules',
'url' => $space->createUrl('/polls/poll/show'),
'icon' => '<i class="fa fa-question-circle"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'polls'),
));
}
}
/**
* On User delete, delete all poll answers by this user
*
* @param type $event
*/
public static function onUserDelete($event)
{
foreach (PollAnswerUser::findAll(array('created_by' => $event->sender->id)) as $answer) {
$answer->delete();
}
return true;
}
/**
* Callback to validate module database records.
*
* @param Event $event
*/
public static function onIntegrityCheck($event)
{
$integrityController = $event->sender;
$integrityController->showTestHeadline("Polls Module - Answers (" . PollAnswer::find()->count() . " entries)");
foreach (PollAnswer::find()->joinWith('poll')->all() as $answer) {
if ($answer->poll === null) {
if ($integrityController->showFix("Deleting poll answer id " . $answer->id . " without existing poll!")) {
$answer->delete();
}
}
}
$integrityController->showTestHeadline("Polls Module - Answers User (" . PollAnswerUser::find()->count() . " entries)");
foreach (PollAnswerUser::find()->joinWith(['poll', 'user'])->all() as $answerUser) {
if ($answerUser->poll === null) {
if ($integrityController->showFix("Deleting poll answer id " . $answerUser->id . " without existing poll!")) {
$answerUser->delete();
}
}
if ($answerUser->user === null) {
if ($integrityController->showFix("Deleting poll answer id " . $answerUser->id . " without existing user!")) {
$answerUser->delete();
}
}
}
}
/**
* Create installer sample data
*
* @param \yii\base\Event $event
*/
public static function onSampleDataInstall($event)
{
$space = Space::find()->where(['id' => 1])->one();
// activate module at space
if (!$space->isModuleEnabled("polls")) {
$space->enableModule("polls");
}
// Switch Identity
$user = User::find()->where(['id' => 1])->one();
Yii::$app->user->switchIdentity($user);
$poll = new Poll();
$poll->scenario = Poll::SCENARIO_CREATE;
$poll->question = Yii::t('PollsModule.events', "Right now, we are in the planning stages for our next meetup and we would like to know from you, where you would like to go?");
$poll->newAnswers = [
Yii::t('PollsModule.events', "To Daniel"),
Yii::t('PollsModule.events', "Club A Steakhouse"),
Yii::t('PollsModule.events', "Pisillo Italian Panini")
];
$poll->content->container = $space;
$poll->allow_multiple = Yii::$app->request->post('allowMultiple', 0);
$poll->save();
// load users
$user2 = User::find()->where(['id' => 2])->one();
$user3 = User::find()->where(['id' => 3])->one();
// Switch Identity
Yii::$app->user->switchIdentity($user2);
// vote
$poll->vote([2]);
$comment = new \humhub\modules\comment\models\Comment();
$comment->message = Yii::t('PollsModule.events', "Why don't we go to Bemelmans Bar?");
$comment->object_model = $poll->className();
$comment->object_id = $poll->getPrimaryKey();
$comment->save();
// Switch Identity
Yii::$app->user->switchIdentity($user3);
// vote
$poll->vote([3]);
$comment = new \humhub\modules\comment\models\Comment();
$comment->message = Yii::t('PollsModule.events', "Again? ;Weary;");
$comment->object_model = $poll->className();
$comment->object_id = $poll->getPrimaryKey();
$comment->save();
// Switch Identity
Yii::$app->user->switchIdentity($user);
}
}