36
36
* @package mod_moodleoverflow
37
37
* @copyright 2023 Tamaro Walter
38
38
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
+ *
40
+ * @covers \userstats_table
39
41
*/
40
42
class userstats_test extends \advanced_testcase {
41
43
@@ -107,11 +109,7 @@ public function test_upvote() {
107
109
108
110
// Create the user statistics table for this course and save it in $data.
109
111
$ data = $ this ->create_statstable ();
110
- foreach ($ data as $ student ) {
111
- if ($ student ->id == $ this ->user2 ->id ) {
112
- $ upvotes = $ student ->receivedupvotes ;
113
- }
114
- }
112
+ $ upvotes = $ this ->get_specific_userstats ($ data , $ this ->user2 , 'receivedupvotes ' );
115
113
$ this ->assertEquals (1 , $ upvotes );
116
114
}
117
115
@@ -125,11 +123,7 @@ public function test_downvote() {
125
123
126
124
// Create the user statistics table for this course and save it in $data.
127
125
$ data = $ this ->create_statstable ();
128
- foreach ($ data as $ student ) {
129
- if ($ student ->id == $ this ->user1 ->id ) {
130
- $ downvotes = $ student ->receiveddownvotes ;
131
- }
132
- }
126
+ $ downvotes = $ this ->get_specific_userstats ($ data , $ this ->user1 , 'receiveddownvotes ' );
133
127
$ this ->assertEquals (1 , $ downvotes );
134
128
}
135
129
@@ -146,11 +140,7 @@ public function test_activity() {
146
140
// Activity = 5.
147
141
// Create the user statistics table for this course and save it in $data.
148
142
$ data = $ this ->create_statstable ();
149
- foreach ($ data as $ student ) {
150
- if ($ student ->id == $ this ->user1 ->id ) {
151
- $ activity = $ student ->activity ;
152
- }
153
- }
143
+ $ activity = $ this ->get_specific_userstats ($ data , $ this ->user1 , 'forumactivity ' );
154
144
$ this ->assertEquals (5 , $ activity );
155
145
156
146
}
@@ -165,18 +155,115 @@ public function test_reputation() {
165
155
$ this ->create_downvote ($ this ->user1 , $ this ->discussion2 [1 ], $ this ->post2 );
166
156
$ this ->create_solution ($ this ->teacher , $ this ->discussion1 [1 ], $ this ->answer1 );
167
157
168
- // Calculate the reputation of user2.
169
- $ reputation = \mod_moodleoverflow \ratings::moodleoverflow_get_reputation ($ this ->moodleoverflow ->id , $ this ->user2 ->id );
158
+ // Calculate the forum reputation of user2.
159
+ $ reputation = \mod_moodleoverflow \ratings::moodleoverflow_get_reputation_instance ($ this ->moodleoverflow ->id ,
160
+ $ this ->user2 ->id );
170
161
// Create the user statistics table for this course and save it in $data.
171
162
$ data = $ this ->create_statstable ();
172
- foreach ($ data as $ student ) {
173
- if ($ student ->id == $ this ->user2 ->id ) {
174
- $ reputation2 = $ student ->reputation ;
175
- }
176
- }
163
+ $ reputation2 = $ this ->get_specific_userstats ($ data , $ this ->user2 , 'forumreputation ' );
177
164
$ this ->assertEquals ($ reputation , $ reputation2 );
178
165
}
179
166
167
+ /**
168
+ * Test, if userstats are calculated correctly if the moodleoverflow is partially anonymous.
169
+ * @covers \userstats_table
170
+ */
171
+ public function test_partial_anonymous () {
172
+ global $ DB ;
173
+ // Test case: Only topic startes are anonymous.
174
+ $ this ->make_anonymous (1 );
175
+
176
+ // Get the current userstats to compare later.
177
+ $ olduserstats = $ this ->create_statstable ();
178
+ $ oldupvotesuser1 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user1 , 'receivedupvotes ' );
179
+ $ oldactivityuser1 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user1 , 'forumactivity ' );
180
+
181
+ $ oldupvotesuser2 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user2 , 'receivedupvotes ' );
182
+ $ oldactivityuser2 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user2 , 'forumactivity ' );
183
+
184
+ // User1 starts a new discussion, the forum activity shouldn't change.
185
+ $ discussion = $ this ->generator ->post_to_forum ($ this ->moodleoverflow , $ this ->user1 );
186
+ $ starterpost = $ DB ->get_record ('moodleoverflow_posts ' , array ('id ' => $ discussion [0 ]->firstpost ), '* ' );
187
+ $ newuserstats = $ this ->create_statstable ();
188
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
189
+ $ this ->assertEquals ($ oldactivityuser1 , $ newactivityuser1 );
190
+
191
+ // User2 now gives an answer to user1, his activity should change.
192
+ $ answeruser2 = $ this ->generator ->reply_to_post ($ discussion [1 ], $ this ->user2 , true );
193
+ $ newuserstats = $ this ->create_statstable ();
194
+ $ newactivityuser2 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user2 , 'forumactivity ' );
195
+ $ this ->assertEquals ($ oldactivityuser2 + 1 , $ newactivityuser2 );
196
+ $ oldactivityuser2 = $ newactivityuser2 ; // Update it for further comparisons.
197
+
198
+ // User1 rates the answer from user2 as helpful an gives it an upvote.
199
+ // The activity of user1 should only change when he gives an upvote.
200
+ // The received upvotes from user2 should change.
201
+ $ this ->create_helpful ($ this ->user1 , $ discussion [1 ], $ answeruser2 );
202
+ $ newuserstats = $ this ->create_statstable ();
203
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
204
+ $ this ->assertEquals ($ oldactivityuser1 , $ newactivityuser1 );
205
+
206
+ $ this ->create_upvote ($ this ->user1 , $ discussion [1 ], $ answeruser2 );
207
+ $ newuserstats = $ this ->create_statstable ();
208
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
209
+ $ newupvotesuser2 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user2 , 'receivedupvotes ' );
210
+ $ this ->assertEquals ($ oldactivityuser1 + 1 , $ newactivityuser1 );
211
+ $ this ->assertEquals ($ oldupvotesuser2 + 1 , $ newupvotesuser2 );
212
+
213
+ // User2 gives the discussion starter post an upvote.
214
+ // Activity of User2 should change, the receivedupvotes from user1 shouln't change.
215
+ $ this ->create_upvote ($ this ->user2 , $ discussion [1 ], $ starterpost );
216
+ $ newuserstats = $ this ->create_statstable ();
217
+ $ newactivityuser2 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user2 , 'forumactivity ' );
218
+ $ newupvotesuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'receivedupvotes ' );
219
+ $ this ->assertEquals ($ oldactivityuser2 + 1 , $ newactivityuser2 );
220
+ $ this ->assertEquals ($ oldupvotesuser1 , $ newupvotesuser1 );
221
+ }
222
+
223
+ /**
224
+ * Test, if userstats are calculated correctly if the moodleoverflow is partially anonymous.
225
+ * @covers \userstats_table
226
+ */
227
+ public function test_total_anonymous () {
228
+ // Test case: Only topic startes are anonymous.
229
+ $ this ->make_anonymous (2 );
230
+
231
+ // Get the current userstats to compare later.
232
+ $ olduserstats = $ this ->create_statstable ();
233
+ $ oldupvotesuser1 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user1 , 'receivedupvotes ' );
234
+ $ oldactivityuser1 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user1 , 'forumactivity ' );
235
+
236
+ $ oldupvotesuser2 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user2 , 'receivedupvotes ' );
237
+ $ oldactivityuser2 = $ this ->get_specific_userstats ($ olduserstats , $ this ->user2 , 'forumactivity ' );
238
+
239
+ // User1 starts a new discussion, the forum activity shouldn't change.
240
+ $ discussion = $ this ->generator ->post_to_forum ($ this ->moodleoverflow , $ this ->user1 );
241
+ $ newuserstats = $ this ->create_statstable ();
242
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
243
+ $ this ->assertEquals ($ oldactivityuser1 , $ newactivityuser1 );
244
+
245
+ // User2 now gives an answer to user1, his activity shouldn't change.
246
+ $ answeruser2 = $ this ->generator ->reply_to_post ($ discussion [1 ], $ this ->user2 , true );
247
+ $ newuserstats = $ this ->create_statstable ();
248
+ $ newactivityuser2 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user2 , 'forumactivity ' );
249
+ $ this ->assertEquals ($ oldactivityuser2 , $ newactivityuser2 );
250
+
251
+ // User1 rates the answer from user2 as helpful an gives it an upvote.
252
+ // The activity of user1 should only change when he gives an upvote.
253
+ // User2 received upvotes should not change.
254
+ $ this ->create_helpful ($ this ->user1 , $ discussion [1 ], $ answeruser2 );
255
+ $ newuserstats = $ this ->create_statstable ();
256
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
257
+ $ this ->assertEquals ($ oldactivityuser1 , $ newactivityuser1 );
258
+
259
+ $ this ->create_upvote ($ this ->user1 , $ discussion [1 ], $ answeruser2 );
260
+ $ newuserstats = $ this ->create_statstable ();
261
+ $ newactivityuser1 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user1 , 'forumactivity ' );
262
+ $ newupvotesuser2 = $ this ->get_specific_userstats ($ newuserstats , $ this ->user2 , 'receivedupvotes ' );
263
+ $ this ->assertEquals ($ oldactivityuser1 + 1 , $ newactivityuser1 );
264
+ $ this ->assertEquals ($ oldupvotesuser2 , $ newupvotesuser2 );
265
+ }
266
+
180
267
// Helper functions.
181
268
182
269
/**
@@ -212,6 +299,23 @@ private function helper_course_set_up() {
212
299
$ this ->answer2 = $ this ->generator ->reply_to_post ($ this ->discussion2 [1 ], $ this ->user1 , true );
213
300
}
214
301
302
+ /**
303
+ * Makes the existing moodleoverflow anonymous.
304
+ * There are 2 types of anonymous moodleoverflows:
305
+ * anonymous = 1, the topic starter is anonymous
306
+ * anonymous = 2, all users are anonym
307
+ *
308
+ * @param int $anonymoussetting
309
+ */
310
+ private function make_anonymous ($ anonymoussetting ) {
311
+ global $ DB ;
312
+ if ($ anonymoussetting == 1 || $ anonymoussetting == 2 ) {
313
+ $ this ->moodleoverflow ->anonymous = $ anonymoussetting ;
314
+ $ DB ->update_record ('moodleoverflow ' , $ this ->moodleoverflow );
315
+ } else {
316
+ throw new \Exception ('invalid parameter, anonymoussetting should be 1 or 2 ' );
317
+ }
318
+ }
215
319
216
320
/**
217
321
* Create a usertable and return it.
@@ -284,7 +388,7 @@ private function create_helpful($author, $discussion, $post) {
284
388
'discussionid ' => $ discussion ->id ,
285
389
'userid ' => $ author ->id ,
286
390
'postid ' => $ post ->id ,
287
- 'rating ' => 3 ,
391
+ 'rating ' => 4 ,
288
392
'firstrated ' => time (),
289
393
'lastchanged ' => time ()
290
394
];
@@ -306,10 +410,42 @@ private function create_solution($author, $discussion, $post) {
306
410
'discussionid ' => $ discussion ->id ,
307
411
'userid ' => $ author ->id ,
308
412
'postid ' => $ post ->id ,
309
- 'rating ' => 4 ,
413
+ 'rating ' => 3 ,
310
414
'firstrated ' => time (),
311
415
'lastchanged ' => time ()
312
416
];
313
417
return $ this ->generator ->create_rating ($ record );
314
418
}
419
+
420
+ /**
421
+ * Return a specific value from the userstatstable.
422
+ *
423
+ * @param array $statstable
424
+ * @param object $user
425
+ * @param string $stats // A key that specifies which value should be returned.
426
+ */
427
+ private function get_specific_userstats ($ statstable , $ user , $ stats ) {
428
+ foreach ($ statstable as $ student ) {
429
+ if ($ student ->id == $ user ->id ) {
430
+ switch ($ stats ) {
431
+ case 'receivedupvotes ' :
432
+ $ result = $ student ->receivedupvotes ;
433
+ break ;
434
+ case 'receiveddownvotes ' :
435
+ $ result = $ student ->receiveddownvotes ;
436
+ break ;
437
+ case 'forumactivity ' :
438
+ $ result = $ student ->forumactivity ;
439
+ break ;
440
+ case 'forumreputation ' :
441
+ $ result = $ student ->forumreputation ;
442
+ break ;
443
+ default :
444
+ throw new \Exception ('parameter unknown ' );
445
+ break ;
446
+ }
447
+ }
448
+ }
449
+ return $ result ;
450
+ }
315
451
}
0 commit comments