This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEvents.php
executable file
·148 lines (126 loc) · 4.73 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
<?php
/**
* Connected Communities Initiative
* Copyright (C) 2016 Queensland University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace humhub\modules\questionanswer;
use humhub\modules\content\models\Content;
use humhub\modules\karma\models\Karma;
use humhub\modules\questionanswer\models\Answer;
use humhub\modules\questionanswer\models\Question;
use humhub\modules\questionanswer\models\QuestionVotes;
use humhub\modules\user\models\forms\AccountRegister;
use humhub\modules\email_whitelist\models\EmailWhitelist;
use Yii;
use yii\helpers\Url;
class Events extends \yii\base\BaseObject
{
/**
* Defines what to do if admin menu is initialized.
*
* @param type $event
*/
public static function onAdminMenuInit($event)
{
$event->sender->addItem(array(
'label' => "Q&A",
'url' => Url::to(['/questionanswer/setting']),
'group' => 'manage',
'icon' => '<i class="fa fa-stack-exchange"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'questionanswer' && Yii::$app->controller->id == 'setting'),
'sortOrder' => 510,
));
}
public static function onTopMenuInit($event)
{
$event->sender->addItem(array(
'label' => "Q&A",
'icon' => '<i class="fa fa-stack-exchange"></i>',
'url' => Url::to(['/questionanswer/question/index']),
'sortOrder' => 350,
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'questionanswer'),
));
}
public static function onSpaceMenuInit($event)
{
if ($event->sender->space !== null && $event->sender->space->isModuleEnabled('questionanswer')) {
$event->sender->addItem(array(
'label' => "Q&A",
'group' => 'modules',
'url' => $event->sender->space->createUrl('//questionanswer/question/index'),
'icon' => '<i class="fa fa-stack-exchange"></i>',
'isActive' => (Yii::$app->controller->module && Yii::$app->controller->module->id == 'questionanswer'),
));
}
}
/**
* A question has been created
* @param type $event
*/
public static function onQuestionAfterSave($event)
{
if(isset(Yii::$app->modules['karma'])) {
Karma::addKarma('asked', $event->sender->user->id, $event->sender, true);
}
}
/**
* An answer has been created
* @param type $event
*/
public static function onAnswerAfterSave($event)
{
if(isset(Yii::$app->modules['karma'])) {
Karma::addKarma('answered', $event->sender->user->id, $event->sender, true);
}
}
/**
* A question has been voted on
* This method will determine what type
* of vote has been cast and what karma to give.
*
* Key Votes:
* - up vote on question
* - up vote on answer
* - marked as best answer
*
* @param type $event
*/
public static function onQuestionVoteAfterSave($event)
{
switch($event->sender->vote_type) {
case "up":
// Only vote on questions and answers
switch($event->sender->vote_on) {
case "question":
if(isset(Yii::$app->modules['karma'])) {
Karma::addKarma('question_up_vote', $event->sender->question->created_by, $event->sender->question);
}
break;
case "answer":
if(isset(Yii::$app->modules['karma'])) {
Karma::addKarma('answer_up_vote', $event->sender->answer->created_by, $event->sender->answer);
}
break;
}
break;
case "accepted_answer":
if(isset(Yii::$app->modules['karma'])) {
Karma::addKarma('accepted_answer', $event->sender->answer->created_by, $event->sender->answer);
}
break;
}
}
}