1
+ <?php if (!defined ('APPLICATION ' )) exit ();
2
+
3
+ class VotingPlugin extends Gdn_Plugin {
4
+
5
+ /**
6
+ * Add JS & CSS to the page.
7
+ */
8
+ public function AddJsCss ($ Sender ) {
9
+ $ Sender ->AddCSSFile ('voting.css ' , 'plugins/Voting ' );
10
+ $ Sender ->AddJSFile ('voting.js ' , 'plugins/Voting ' );
11
+ }
12
+
13
+ public function addVotingBox ($ sender , $ args ) {
14
+ $ session = Gdn::Session ();
15
+ $ object = $ args ['Object ' ];
16
+ $ VoteType = $ args ['Type ' ] == 'Discussion ' ? 'votediscussion ' : 'votecomment ' ;
17
+ $ id = $ args ['Type ' ] == 'Discussion ' ? val ('DiscussionID ' , $ object ) : val ('CommentID ' , $ object );
18
+ $ score = val ('Score ' , $ object );
19
+ $ cssClass = '' ;
20
+ $ voteUpUrl = '/discussion/ ' .$ VoteType .'/ ' .$ id .'/voteup/ ' .$ session ->TransientKey ().'/ ' ;
21
+ $ voteDownUrl = '/discussion/ ' .$ VoteType .'/ ' .$ id .'/votedown/ ' .$ session ->TransientKey ().'/ ' ;
22
+ if (!$ session ->IsValid ()) {
23
+ $ voteUpUrl = Gdn::Authenticator ()->SignInUrl ($ sender ->SelfUrl );
24
+ $ voteDownUrl = $ voteUpUrl ;
25
+ $ cssClass = ' SignInPopup ' ;
26
+ }
27
+
28
+ if ($ args ['Type ' ] == 'Discussion ' ) {
29
+ $ discussionModel = new DiscussionModel ();
30
+ $ currentUserVote = $ discussionModel ->GetUserScore ($ id , $ session ->UserID );
31
+ } else {
32
+ $ commentModel = new CommentModel ();
33
+ $ currentUserVote = $ commentModel ->GetUserScore ($ id , $ session ->UserID );
34
+ }
35
+ $ cssClassVoteUp = $ cssClassVoteDown = '' ;
36
+ if ($ currentUserVote > 0 ) {
37
+ $ cssClassVoteUp = ' Voted ' ;
38
+ } else if ($ currentUserVote < 0 ){
39
+ $ cssClassVoteDown = ' Voted ' ;
40
+ }
41
+
42
+ echo '<span class="Voter"> ' ;
43
+ echo Anchor (Wrap ('Vote Up ' , 'span ' , array ('class ' => 'ArrowSprite SpriteUp ' .$ cssClassVoteUp , 'rel ' => 'nofollow ' )), $ voteUpUrl , 'VoteUp ' .$ cssClass );
44
+ echo Wrap (StringIsNullOrEmpty ($ score ) ? '0 ' : Gdn_Format::BigNumber ($ score ), 'span ' , array ('class ' => 'CountVoices ' ));
45
+ echo Anchor (Wrap ('Vote Down ' , 'span ' , array ('class ' => 'ArrowSprite SpriteDown ' .$ cssClassVoteDown , 'rel ' => 'nofollow ' )), $ voteDownUrl , 'VoteDown ' .$ cssClass );
46
+ echo '</span> | ' ;
47
+
48
+ }
49
+
50
+
51
+ public function discussionController_BeforeInlineDiscussionOptions_handler ($ sender , $ args ) {
52
+ $ this ->addVotingBox ($ sender , $ args );
53
+ }
54
+
55
+ public function discussionController_BeforeInlineCommentOptions_handler ($ sender , $ args ) {
56
+ $ this ->addVotingBox ($ sender , $ args );
57
+ }
58
+
59
+ public function postController_BeforeInlineCommentOptions_handler ($ sender , $ args ) {
60
+ $ this ->addVotingBox ($ sender , $ args );
61
+ }
62
+
63
+
64
+ /**
65
+ * Add the files to discussions page
66
+ */
67
+ public function discussionController_render_Before ($ sender ) {
68
+ $ this ->AddJsCss ($ sender );
69
+ }
70
+
71
+
72
+ /**
73
+ * Increment/decrement comment scores
74
+ */
75
+ public function discussionController_VoteComment_create ($ sender ) {
76
+ $ CommentID = GetValue (0 , $ sender ->RequestArgs , 0 );
77
+ $ VoteType = GetValue (1 , $ sender ->RequestArgs );
78
+ $ TransientKey = GetValue (2 , $ sender ->RequestArgs );
79
+ $ Session = Gdn::Session ();
80
+ $ FinalVote = 0 ;
81
+ $ Total = 0 ;
82
+ if ($ Session ->IsValid () && $ Session ->ValidateTransientKey ($ TransientKey ) && $ CommentID > 0 ) {
83
+ $ CommentModel = new CommentModel ();
84
+ $ OldUserVote = $ CommentModel ->GetUserScore ($ CommentID , $ Session ->UserID );
85
+ switch ($ VoteType ) {
86
+ case 'voteup ' :
87
+ $ NewUserVote = 1 ;
88
+ break ;
89
+ case 'votedown ' :
90
+ $ NewUserVote = -1 ;
91
+ break ;
92
+ default :
93
+ $ NewUserVote = 0 ;
94
+ }
95
+ $ FinalVote = intval ($ OldUserVote ) + intval ($ NewUserVote );
96
+ if ($ FinalVote == 2 || $ FinalVote == -2 ) {
97
+ // user cancelled a voice
98
+ $ FinalVote = 0 ;
99
+ } else {
100
+ $ FinalVote = $ NewUserVote ;
101
+ }
102
+
103
+ $ Total = $ CommentModel ->SetUserScore ($ CommentID , $ Session ->UserID , $ FinalVote );
104
+ }
105
+ $ sender ->DeliveryType (DELIVERY_TYPE_BOOL );
106
+ $ sender ->SetJson ('TotalScore ' , $ Total );
107
+ $ sender ->SetJson ('FinalVote ' , $ FinalVote );
108
+ $ sender ->SetJson ('VoteUpCssClass ' , $ FinalVote > 0 ? 'Voted ' :'' );
109
+ $ sender ->SetJson ('VoteDownCssClass ' , $ FinalVote < 0 ? 'Voted ' :'' );
110
+ $ sender ->Render ();
111
+ }
112
+
113
+ /**
114
+ * Increment/decrement discussion scores
115
+ */
116
+ public function discussionController_VoteDiscussion_create ($ sender ) {
117
+ $ DiscussionID = GetValue (0 , $ sender ->RequestArgs , 0 );
118
+ $ TransientKey = GetValue (1 , $ sender ->RequestArgs );
119
+ $ VoteType = FALSE ;
120
+ if ($ TransientKey == 'voteup ' || $ TransientKey == 'votedown ' ) {
121
+ $ VoteType = $ TransientKey ;
122
+ $ TransientKey = GetValue (2 , $ sender ->RequestArgs );
123
+ }
124
+ $ Session = Gdn::Session ();
125
+ $ NewUserVote = 0 ;
126
+ $ Total = 0 ;
127
+ if ($ Session ->IsValid () && $ Session ->ValidateTransientKey ($ TransientKey ) && $ DiscussionID > 0 ) {
128
+ $ DiscussionModel = new DiscussionModel ();
129
+ $ OldUserVote = $ DiscussionModel ->GetUserScore ($ DiscussionID , $ Session ->UserID );
130
+
131
+ switch ($ VoteType ) {
132
+ case 'voteup ' :
133
+ $ NewUserVote = 1 ;
134
+ break ;
135
+ case 'votedown ' :
136
+ $ NewUserVote = -1 ;
137
+ break ;
138
+ default :
139
+ $ NewUserVote = 0 ;
140
+ }
141
+
142
+ $ FinalVote = intval ($ OldUserVote ) + intval ($ NewUserVote );
143
+ if ($ FinalVote == 2 || $ FinalVote == -2 ) {
144
+ // user cancelled a voice
145
+ $ FinalVote = 0 ;
146
+ } else {
147
+ $ FinalVote = $ NewUserVote ;
148
+ }
149
+ $ Total = $ DiscussionModel ->SetUserScore ($ DiscussionID , $ Session ->UserID , $ FinalVote );
150
+ }
151
+ $ sender ->DeliveryType (DELIVERY_TYPE_BOOL );
152
+ $ sender ->SetJson ('TotalScore ' , $ Total );
153
+ $ sender ->SetJson ('FinalVote ' , $ FinalVote );
154
+ $ sender ->SetJson ('VoteUpCssClass ' , $ FinalVote > 0 ? 'Voted ' :'' );
155
+ $ sender ->SetJson ('VoteDownCssClass ' , $ FinalVote < 0 ? 'Voted ' :'' );
156
+ $ sender ->Render ();
157
+ }
158
+
159
+ /**
160
+ * Grab the score field whenever the discussions are queried.
161
+ */
162
+ public function DiscussionModel_AfterDiscussionSummaryQuery_Handler ($ Sender ) {
163
+ $ Sender ->SQL ->Select ('d.Score ' );
164
+ }
165
+
166
+ /**
167
+ * Add voting css to post controller.
168
+ */
169
+ public function PostController_Render_Before ($ Sender ) {
170
+ $ this ->AddJsCss ($ Sender );
171
+ }
172
+
173
+ public function Setup () {
174
+ }
175
+
176
+ public function OnDisable () {
177
+ }
178
+
179
+ public function dashboardNavModule_init_handler ($ sender ) {
180
+ /** @var DashboardNavModule $nav */
181
+ $ nav = $ sender ;
182
+ $ sort = -1 ;
183
+ $ nav ->addGroupToSection ('Moderation ' , t ('Voting ' ), 'voting ' , '' , ['after ' =>'site ' ])
184
+ ->addLinkToSectionIf ('Garden.Settings.Manage ' , 'Moderation ' , t ('Discussions ' ), '/voting/discussions ' ,
185
+ 'voting.discussions ' , '' , $ sort )
186
+ ->addLinkToSectionIf ('Garden.Settings.Manage ' , 'Moderation ' , t ('Comments ' ), '/voting/comments ' ,
187
+ 'voting.comments ' , '' , $ sort );
188
+
189
+ }
190
+ }
0 commit comments