MDL-39173 Assign - Enable comments for blind marking assignments (anonymous ones)
[moodle.git] / mod / assign / submission / comments / lib.php
CommitLineData
bbd0e548
DW
1<?PHP
2// This file is part of 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 * This file contains the moodle hooks for the submission comments plugin
19 *
20 * @package assignsubmission_comments
21 * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24defined('MOODLE_INTERNAL') || die();
25
26/**
27 *
28 * Callback method for data validation---- required method for AJAXmoodle based comment API
29 *
30 * @param stdClass $options
31 * @return bool
32 */
33function assignsubmission_comments_comment_validate(stdClass $options) {
e00b5c45
DW
34 global $USER, $CFG, $DB;
35
36 if ($options->commentarea != 'submission_comments' &&
37 $options->commentarea != 'submission_comments_upgrade') {
38 throw new comment_exception('invalidcommentarea');
39 }
40 if (!$submission = $DB->get_record('assign_submission', array('id'=>$options->itemid))) {
41 throw new comment_exception('invalidcommentitemid');
42 }
43 $context = $options->context;
44
45 require_once($CFG->dirroot . '/mod/assign/locallib.php');
46 $assignment = new assign($context, null, null);
47
48 if ($assignment->get_instance()->id != $submission->assignment) {
49 throw new comment_exception('invalidcontext');
50 }
51 if (!has_capability('mod/assign:grade', $context)) {
52 if (!has_capability('mod/assign:submit', $context)) {
53 throw new comment_exception('nopermissiontocomment');
54 } else if ($assignment->get_instance()->teamsubmission) {
55 $group = $assignment->get_submission_group($USER->id);
56 $groupid = 0;
57 if ($group) {
58 $groupid = $group->id;
59 }
60 if ($groupid != $submission->groupid) {
61 throw new comment_exception('nopermissiontocomment');
62 }
63 } else if ($submission->userid != $USER->id) {
64 throw new comment_exception('nopermissiontocomment');
65 }
66 }
bbd0e548
DW
67
68 return true;
69}
70
71/**
72 * Permission control method for submission plugin ---- required method for AJAXmoodle based comment API
73 *
74 * @param stdClass $options
75 * @return array
76 */
77function assignsubmission_comments_comment_permissions(stdClass $options) {
e00b5c45
DW
78 global $USER, $CFG, $DB;
79
80 if ($options->commentarea != 'submission_comments' &&
81 $options->commentarea != 'submission_comments_upgrade') {
82 throw new comment_exception('invalidcommentarea');
83 }
84 if (!$submission = $DB->get_record('assign_submission', array('id'=>$options->itemid))) {
85 throw new comment_exception('invalidcommentitemid');
86 }
87 $context = $options->context;
88
89 require_once($CFG->dirroot . '/mod/assign/locallib.php');
90 $assignment = new assign($context, null, null);
91
92 if ($assignment->get_instance()->id != $submission->assignment) {
93 throw new comment_exception('invalidcontext');
94 }
3e1b63f1
DW
95
96 if ($assignment->get_instance()->teamsubmission &&
97 !$assignment->can_view_group_submission($submission->groupid)) {
98 return array('post' => false, 'view' => false);
99 }
100
101 if (!$assignment->get_instance()->teamsubmission &&
102 !$assignment->can_view_submission($submission->userid)) {
103 return array('post' => false, 'view' => false);
e00b5c45 104 }
bbd0e548
DW
105
106 return array('post' => true, 'view' => true);
107}
108
03995800
SC
109/**
110 * Callback called by comment::get_comments() and comment::add(). Gives an opportunity to enforce blind-marking.
111 *
112 * @param array $comments
113 * @param stdClass $options
114 * @return array
115 * @throws comment_exception
116 */
117function assignsubmission_comments_comment_display($comments, $options) {
118 global $CFG, $DB, $USER, $OUTPUT;
119
120 if ($options->commentarea != 'submission_comments' &&
121 $options->commentarea != 'submission_comments_upgrade') {
122 throw new comment_exception('invalidcommentarea');
123 }
124 if (!$submission = $DB->get_record('assign_submission', array('id'=>$options->itemid))) {
125 throw new comment_exception('invalidcommentitemid');
126 }
127 $context = $options->context;
128 $cm = $options->cm;
129 $course = $options->courseid;
130
131 require_once($CFG->dirroot . '/mod/assign/locallib.php');
132 $assignment = new assign($context, $cm, $course);
133
134 if ($assignment->get_instance()->id != $submission->assignment) {
135 throw new comment_exception('invalidcontext');
136 }
137
138 if ($assignment->is_blind_marking() && !empty($comments)) {
139 // Blind marking is being used, may need to map unique anonymous ids to the comments.
140 $usermappings = array();
141 $hiddenuserstr = trim(get_string('hiddenuser', 'assign'));
142 $guestuser = guest_user();
143
144 foreach ($comments as $comment) {
145 if (($USER->id != $comment->userid) && !has_capability('mod/assign:grade', $assignment->get_context(), $comment->userid)) {
146 // Anonymize if the logged in user is not the commenter and the commenter does not have the grading capability.
147 if (empty($usermappings[$comment->userid])) {
148 // The blind-marking information for this commenter has not been generated; do so now.
149 $anonid = $assignment->get_uniqueid_for_user($comment->userid);
150 $commenter = new stdClass();
151 $commenter->firstname = $hiddenuserstr;
152 $commenter->lastname = $anonid;
153 $commenter->picture = 0;
154 $commenter->id = $guestuser->id;
155 $commenter->email = $guestuser->email;
156 $commenter->imagealt = $guestuser->imagealt;
157
158 // Temporarily store blind-marking information for use in later comments if necessary.
159 $usermappings[$comment->userid]->fullname = fullname($commenter);
160 $usermappings[$comment->userid]->avatar = $OUTPUT->user_picture($commenter,
161 array('size'=>18, 'link' => false));
162 }
163
164 // Set blind-marking information for this comment.
165 $comment->fullname = $usermappings[$comment->userid]->fullname;
166 $comment->avatar = $usermappings[$comment->userid]->avatar;
167 $comment->profileurl = null;
168 }
169 }
170 }
171
172 return $comments;
173}
174
bbd0e548
DW
175/**
176 * Callback to force the userid for all comments to be the userid of the submission and NOT the global $USER->id. This
177 * is required by the upgrade code. Note the comment area is used to identify upgrades.
178 *
179 * @param stdClass $comment
180 * @param stdClass $param
181 */
182function assignsubmission_comments_comment_add(stdClass $comment, stdClass $param) {
183
184 global $DB;
185 if ($comment->commentarea == 'submission_comments_upgrade') {
186 $submissionid = $comment->itemid;
187 $submission = $DB->get_record('assign_submission', array('id' => $submissionid));
188
189 $comment->userid = $submission->userid;
190 $comment->commentarea = 'submission_comments';
191 }
192}
193