Skip to content

Commit 872bc1b

Browse files
author
Laur0r
authored
Merge pull request #81 from learnweb/feature/anonymous
Add support for anonymized moodleoverflow instances
2 parents 69b45a5 + 9f97824 commit 872bc1b

18 files changed

+254
-95
lines changed

amd/build/functions.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/build/functions.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amd/src/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/conf
8181
// Update user reputation.
8282
templates.replaceNode($('.user-details,.author').find('a[href*="id=' + userid + '"]')
8383
.siblings('span'), '<span>' + response.raterreputation + '</span>', "");
84-
if (userid !== response.ownerid) {
84+
if (response.ownerid && userid !== response.ownerid) {
8585
templates.replaceNode($('.user-details,.author').find('a[href*="id=' + response.ownerid + '"]')
8686
.siblings('span'), '<span>' + response.ownerreputation + '</span>', "");
8787
}

classes/anonymous.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
// This file is part of a plugin for Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Moodleoverflow anonymous related class.
19+
*
20+
* @package mod_moodleoverflow
21+
* @copyright 2021 Justus Dieckmann WWU
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace mod_moodleoverflow;
26+
27+
defined('MOODLE_INTERNAL') || die();
28+
29+
/**
30+
* Class for Moodleoverflow anonymity
31+
*
32+
* @package mod_moodleoverflow
33+
* @copyright 2021 Justus Dieckmann WWU
34+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35+
*/
36+
class anonymous {
37+
38+
const NOT_ANONYMOUS = 0;
39+
const QUESTION_ANONYMOUS = 1;
40+
const EVERYTHING_ANONYMOUS = 2;
41+
42+
public static function is_post_anonymous($post, $moodleoverflow, $postinguserid): bool {
43+
if ($postinguserid == 0) {
44+
return true;
45+
}
46+
47+
if ($moodleoverflow->anonymous == self::EVERYTHING_ANONYMOUS) {
48+
return true;
49+
}
50+
51+
if ($moodleoverflow->anonymous == self::QUESTION_ANONYMOUS) {
52+
return $post->parent == 0;
53+
}
54+
55+
return false;
56+
}
57+
58+
}

classes/output/moodleoverflow_email.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
namespace mod_moodleoverflow\output;
2626

27+
use mod_moodleoverflow\anonymous;
28+
2729
defined('MOODLE_INTERNAL') || die();
2830

2931
/**
@@ -357,7 +359,11 @@ public function get_discussionname() {
357359
* @return string
358360
*/
359361
public function get_author_fullname() {
360-
return fullname($this->author, $this->viewfullnames);
362+
if (anonymous::is_post_anonymous($this->post, $this->moodleoverflow, $this->author->id)) {
363+
return get_string('privacy:anonym_user_name', 'mod_moodleoverflow');
364+
} else {
365+
return fullname($this->author, $this->viewfullnames);
366+
}
361367
}
362368

363369
/**
@@ -515,6 +521,10 @@ public function get_moodleoverflowviewlink() {
515521
* @return string
516522
*/
517523
public function get_authorlink() {
524+
if (anonymous::is_post_anonymous($this->post, $this->moodleoverflow, $this->author->id)) {
525+
return null;
526+
}
527+
518528
$link = new \moodle_url(
519529
'/user/view.php', array(
520530
'id' => $this->post->userid,
@@ -532,6 +542,9 @@ public function get_authorlink() {
532542
*/
533543
public function get_author_picture() {
534544
global $OUTPUT;
545+
if (anonymous::is_post_anonymous($this->post, $this->moodleoverflow, $this->author->id)) {
546+
return '';
547+
}
535548

536549
return $OUTPUT->user_picture($this->author, array('courseid' => $this->course->id));
537550
}
@@ -542,6 +555,10 @@ public function get_author_picture() {
542555
* @return string
543556
*/
544557
public function get_group_picture() {
558+
if (anonymous::is_post_anonymous($this->post, $this->moodleoverflow, $this->author->id)) {
559+
return '';
560+
}
561+
545562
if (isset($this->userfrom->groups)) {
546563
$groups = $this->userfrom->groups[$this->moodleoverflow->id];
547564
} else {

classes/ratings.php

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -465,51 +465,60 @@ private static function moodleoverflow_get_reputation_instance($moodleoverflowid
465465
print_error('invalidmoodleoverflowid', 'moodleoverflow');
466466
}
467467

468-
// Get all posts of this user in this module.
469-
// Do not count votes for own posts.
470-
$sql = "SELECT r.id, r.postid as post, r.rating
468+
// Initiate a variable.
469+
$reputation = 0;
470+
471+
if ($moodleoverflow->anonymous != anonymous::EVERYTHING_ANONYMOUS) {
472+
473+
// Get all posts of this user in this module.
474+
// Do not count votes for own posts.
475+
$sql = "SELECT r.id, r.postid as post, r.rating
471476
FROM {moodleoverflow_posts} p
472477
JOIN {moodleoverflow_ratings} r ON p.id = r.postid
473-
WHERE p.userid = ? AND NOT r.userid = ? AND r.moodleoverflowid = ?
474-
ORDER BY r.postid ASC";
475-
$params = array($userid, $userid, $moodleoverflowid);
476-
$records = $DB->get_records_sql($sql, $params);
478+
WHERE p.userid = ? AND NOT r.userid = ? AND r.moodleoverflowid = ? ";
477479

478-
// Check if there are results.
479-
$records = (isset($records)) ? $records : array();
480+
if ($moodleoverflow->anonymous == anonymous::QUESTION_ANONYMOUS) {
481+
$sql .= " AND p.parent <> 0 ";
482+
}
480483

481-
// Initiate a variable.
482-
$reputation = 0;
484+
$sql .= "ORDER BY r.postid ASC";
483485

484-
// Iterate through all ratings.
485-
foreach ($records as $record) {
486+
$params = array($userid, $userid, $moodleoverflowid);
487+
$records = $DB->get_records_sql($sql, $params);
486488

487-
// The rating is a downvote.
488-
if ($record->rating == RATING_DOWNVOTE) {
489-
$reputation += get_config('moodleoverflow', 'votescaledownvote');
490-
continue;
491-
}
489+
// Check if there are results.
490+
$records = (isset($records)) ? $records : array();
492491

493-
// The rating is an upvote.
494-
if ($record->rating == RATING_UPVOTE) {
495-
$reputation += get_config('moodleoverflow', 'votescaleupvote');
496-
continue;
497-
}
492+
// Iterate through all ratings.
493+
foreach ($records as $record) {
498494

499-
// The post has been marked as helpful by the question owner.
500-
if ($record->rating == RATING_HELPFUL) {
501-
$reputation += get_config('moodleoverflow', 'votescalehelpful');
502-
continue;
503-
}
495+
// The rating is a downvote.
496+
if ($record->rating == RATING_DOWNVOTE) {
497+
$reputation += get_config('moodleoverflow', 'votescaledownvote');
498+
continue;
499+
}
504500

505-
// The post has been marked as solved by a teacher.
506-
if ($record->rating == RATING_SOLVED) {
507-
$reputation += get_config('moodleoverflow', 'votescalesolved');
501+
// The rating is an upvote.
502+
if ($record->rating == RATING_UPVOTE) {
503+
$reputation += get_config('moodleoverflow', 'votescaleupvote');
504+
continue;
505+
}
506+
507+
// The post has been marked as helpful by the question owner.
508+
if ($record->rating == RATING_HELPFUL) {
509+
$reputation += get_config('moodleoverflow', 'votescalehelpful');
510+
continue;
511+
}
512+
513+
// The post has been marked as solved by a teacher.
514+
if ($record->rating == RATING_SOLVED) {
515+
$reputation += get_config('moodleoverflow', 'votescalesolved');
516+
continue;
517+
}
518+
519+
// Another rating should not exist.
508520
continue;
509521
}
510-
511-
// Another rating should not exist.
512-
continue;
513522
}
514523

515524
// Get votes this user made.
@@ -806,4 +815,4 @@ private static function moodleoverflow_user_can_rate($moodleoverflow, $cm = null
806815
return has_capability('mod/moodleoverflow:ratepost', $modulecontext, $userid);
807816
}
808817

809-
}
818+
}

db/install.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<FIELD NAME="grademaxgrade" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
2424
<FIELD NAME="gradescalefactor" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
2525
<FIELD NAME="gradecat" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
26+
<FIELD NAME="anonymous" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
2627
</FIELDS>
2728
<KEYS>
2829
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

db/upgrade.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,22 @@ function xmldb_moodleoverflow_upgrade($oldversion) {
138138
upgrade_mod_savepoint(true, 2019052600, 'moodleoverflow');
139139
}
140140

141+
if ($oldversion < 2021060800) {
142+
143+
// Define table moodleoverflow to be edited
144+
$table = new xmldb_table('moodleoverflow');
145+
146+
// Define field anonymous to be added to moodleoverflow.
147+
$field = new xmldb_field('anonymous', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, 0, 'gradecat');
148+
149+
// Conditionally launch add field grademaxgrade.
150+
if (!$dbman->field_exists($table, $field)) {
151+
$dbman->add_field($table, $field);
152+
}
153+
154+
// Moodleoverflow savepoint reached.
155+
upgrade_mod_savepoint(true, 2021060800, 'moodleoverflow');
156+
}
157+
141158
return true;
142159
}

0 commit comments

Comments
 (0)