Skip to content

Commit 0645f04

Browse files
authored
Merge pull request #147 from learnweb/fix/anonymoususerstats
Fix/anonymoususerstats
2 parents 5357dec + c2cce45 commit 0645f04

8 files changed

+313
-110
lines changed

Diff for: classes/ratings.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public static function moodleoverflow_discussion_is_solved($discussionid, $teach
477477
*
478478
* @return int
479479
*/
480-
private static function moodleoverflow_get_reputation_instance($moodleoverflowid, $userid = null) {
480+
public static function moodleoverflow_get_reputation_instance($moodleoverflowid, $userid = null) {
481481
global $DB, $USER;
482482

483483
// Get the user id.
@@ -574,7 +574,7 @@ private static function moodleoverflow_get_reputation_instance($moodleoverflowid
574574
*
575575
* @return int
576576
*/
577-
private static function moodleoverflow_get_reputation_course($courseid, $userid = null) {
577+
public static function moodleoverflow_get_reputation_course($courseid, $userid = null) {
578578
global $USER, $DB;
579579

580580
// Get the userid.

Diff for: classes/tables/userstats_table.php

+143-81
Large diffs are not rendered by default.

Diff for: lang/en/moodleoverflow.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,10 @@
206206
// Strings for the userstats feature.
207207
$string['userstatsupvotes'] = 'Received upvotes';
208208
$string['userstatsdownvotes'] = 'Received downvotes';
209-
$string['userstatsactivity'] = 'Amount of activity';
210-
$string['userstatsreputation'] = 'User reputation';
209+
$string['userstatsforumactivity'] = 'Activity (this forum)';
210+
$string['userstatsforumreputation'] = 'Reputation (this forum)';
211+
$string['userstatscourseactivity'] = 'Activity (coursewide)';
212+
$string['userstatscoursereputation'] = 'Reputation (coursewide)';
211213
$string['helpamountofactivity'] = 'Each activity like writing a post, starting a discussion or giving a rating gives 1 point';
212214
$string['showuserstats'] = 'Show cumulative user statistics';
213215
$string['configshowuserstats'] = 'Allow teachers in courses to see statistics summarizing the activity of users in Moodleoverflows.';

Diff for: styles.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@
529529
}
530530

531531

532-
.moodleoverflow-statistics-table .header.c3 .helpactivityclass {
532+
.moodleoverflow-statistics-table .header .helpactivityclass {
533533
padding: 0;
534534
margin-left: 8px;
535535
}

Diff for: tests/privacy_provider_test.php

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @group mod_moodleoverflow
4040
* @group mod_moodleoverflow_privacy
4141
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42+
* @covers \provider
4243
*/
4344
class privacy_provider_test extends \core_privacy\tests\provider_testcase {
4445
/**

Diff for: tests/readtracking_test.php

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @package mod_moodleoverflow
3737
* @copyright 2017 Kennet Winter <[email protected]>
3838
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
* @covers \readtracking
3940
*/
4041
class readtracking_test extends advanced_testcase {
4142

Diff for: tests/subscriptions_test.php

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* @package mod_moodleoverflow
3737
* @copyright 2017 Kennet Winter <[email protected]>
3838
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
* @covers \subscriptions
3940
*/
4041
class subscriptions_test extends advanced_testcase {
4142

Diff for: tests/userstats_test.php

+160-24
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* @package mod_moodleoverflow
3737
* @copyright 2023 Tamaro Walter
3838
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
*
40+
* @covers \userstats_table
3941
*/
4042
class userstats_test extends \advanced_testcase {
4143

@@ -107,11 +109,7 @@ public function test_upvote() {
107109

108110
// Create the user statistics table for this course and save it in $data.
109111
$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');
115113
$this->assertEquals(1, $upvotes);
116114
}
117115

@@ -125,11 +123,7 @@ public function test_downvote() {
125123

126124
// Create the user statistics table for this course and save it in $data.
127125
$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');
133127
$this->assertEquals(1, $downvotes);
134128
}
135129

@@ -146,11 +140,7 @@ public function test_activity() {
146140
// Activity = 5.
147141
// Create the user statistics table for this course and save it in $data.
148142
$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');
154144
$this->assertEquals(5, $activity);
155145

156146
}
@@ -165,18 +155,115 @@ public function test_reputation() {
165155
$this->create_downvote($this->user1, $this->discussion2[1], $this->post2);
166156
$this->create_solution($this->teacher, $this->discussion1[1], $this->answer1);
167157

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);
170161
// Create the user statistics table for this course and save it in $data.
171162
$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');
177164
$this->assertEquals($reputation, $reputation2);
178165
}
179166

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+
180267
// Helper functions.
181268

182269
/**
@@ -212,6 +299,23 @@ private function helper_course_set_up() {
212299
$this->answer2 = $this->generator->reply_to_post($this->discussion2[1], $this->user1, true);
213300
}
214301

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+
}
215319

216320
/**
217321
* Create a usertable and return it.
@@ -284,7 +388,7 @@ private function create_helpful($author, $discussion, $post) {
284388
'discussionid' => $discussion->id,
285389
'userid' => $author->id,
286390
'postid' => $post->id,
287-
'rating' => 3,
391+
'rating' => 4,
288392
'firstrated' => time(),
289393
'lastchanged' => time()
290394
];
@@ -306,10 +410,42 @@ private function create_solution($author, $discussion, $post) {
306410
'discussionid' => $discussion->id,
307411
'userid' => $author->id,
308412
'postid' => $post->id,
309-
'rating' => 4,
413+
'rating' => 3,
310414
'firstrated' => time(),
311415
'lastchanged' => time()
312416
];
313417
return $this->generator->create_rating($record);
314418
}
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+
}
315451
}

0 commit comments

Comments
 (0)