09666b0ca162e8164c5342934ed0623a98ed5684
[moodle.git] / mod / assign / submission / comments / lib.php
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/>.
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  */
24 defined('MOODLE_INTERNAL') || die();
26 /**
27  *
28  * Callback method for data validation---- required method for AJAXmoodle based comment API
29  *
30  * @param stdClass $options
31  * @return bool
32  */
33 function assignsubmission_comments_comment_validate(stdClass $options) {
34     global $USER, $CFG, $DB;
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;
45     require_once($CFG->dirroot . '/mod/assign/locallib.php');
46     $assignment = new assign($context, null, null);
48     if ($assignment->get_instance()->id != $submission->assignment) {
49         throw new comment_exception('invalidcontext');
50     }
51     $canview = false;
52     if ($submission->userid) {
53         $canview = $assignment->can_view_submission($submission->userid);
54     } else {
55         $canview = $assignment->can_view_group_submission($submission->groupid);
56     }
57     if (!$canview) {
58         throw new comment_exception('nopermissiontocomment');
59     }
61     return true;
62 }
64 /**
65  * Permission control method for submission plugin ---- required method for AJAXmoodle based comment API
66  *
67  * @param stdClass $options
68  * @return array
69  */
70 function assignsubmission_comments_comment_permissions(stdClass $options) {
71     global $USER, $CFG, $DB;
73     if ($options->commentarea != 'submission_comments' &&
74             $options->commentarea != 'submission_comments_upgrade') {
75         throw new comment_exception('invalidcommentarea');
76     }
77     if (!$submission = $DB->get_record('assign_submission', array('id'=>$options->itemid))) {
78         throw new comment_exception('invalidcommentitemid');
79     }
80     $context = $options->context;
82     require_once($CFG->dirroot . '/mod/assign/locallib.php');
83     $assignment = new assign($context, null, null);
85     if ($assignment->get_instance()->id != $submission->assignment) {
86         throw new comment_exception('invalidcontext');
87     }
89     if ($assignment->get_instance()->teamsubmission &&
90         !$assignment->can_view_group_submission($submission->groupid)) {
91         return array('post' => false, 'view' => false);
92     }
94     if (!$assignment->get_instance()->teamsubmission &&
95         !$assignment->can_view_submission($submission->userid)) {
96         return array('post' => false, 'view' => false);
97     }
99     return array('post' => true, 'view' => true);
102 /**
103  * Callback called by comment::get_comments() and comment::add(). Gives an opportunity to enforce blind-marking.
104  *
105  * @param array $comments
106  * @param stdClass $options
107  * @return array
108  * @throws comment_exception
109  */
110 function assignsubmission_comments_comment_display($comments, $options) {
111     global $CFG, $DB, $USER;
113     if ($options->commentarea != 'submission_comments' &&
114         $options->commentarea != 'submission_comments_upgrade') {
115         throw new comment_exception('invalidcommentarea');
116     }
117     if (!$submission = $DB->get_record('assign_submission', array('id'=>$options->itemid))) {
118         throw new comment_exception('invalidcommentitemid');
119     }
120     $context = $options->context;
121     $cm = $options->cm;
122     $course = $options->courseid;
124     require_once($CFG->dirroot . '/mod/assign/locallib.php');
125     $assignment = new assign($context, $cm, $course);
127     if ($assignment->get_instance()->id != $submission->assignment) {
128         throw new comment_exception('invalidcontext');
129     }
131     if ($assignment->is_blind_marking() && !empty($comments)) {
132         // Blind marking is being used, may need to map unique anonymous ids to the comments.
133         $usermappings = array();
134         $guestuser = guest_user();
136         // Check group users first.
137         $userinteam = false;
138         if ($assignment->get_instance()->teamsubmission && has_capability('mod/assign:submit', $context)) {
139             $assignment->set_course(get_course($course));
140             $userinteam = $assignment->can_edit_group_submission($submission->groupid);
141         }
143         foreach ($comments as $comment) {
145             if (has_capability('mod/assign:viewblinddetails', $context) && $USER->id != $comment->userid) {
146                 $anonid = $assignment->get_uniqueid_for_user($comment->userid);
147                 // Show participant information and the user's full name to users with the view blind details capability.
148                 $a = new stdClass();
149                 $a->participantnumber = $anonid;
150                 $a->participantfullname = $comment->fullname;
151                 $comment->fullname = get_string('blindmarkingviewfullname', 'assignsubmission_comments', $a);
152             } else if ($USER->id == $comment->userid || $submission->userid == $USER->id || $userinteam) { //@codingStandardsIgnoreLine
153                 // Do not anonymize the user details for this comment.
154             } else {
155                 // Anonymize the comments.
156                 if (empty($usermappings[$comment->userid])) {
157                     $anonid = $assignment->get_uniqueid_for_user($comment->userid);
158                     // The blind-marking information for this commenter has not been generated; do so now.
159                     $commenter = new stdClass();
160                     $commenter->firstname = get_string('blindmarkingname', 'assignsubmission_comments', $anonid);
161                     $commenter->lastname = '';
162                     $commenter->firstnamephonetic = '';
163                     $commenter->lastnamephonetic = '';
164                     $commenter->middlename = '';
165                     $commenter->alternatename = '';
166                     $commenter->picture = 0;
167                     $commenter->id = $guestuser->id;
168                     $commenter->email = $guestuser->email;
169                     $commenter->imagealt = $guestuser->imagealt;
171                     // Temporarily store blind-marking information for use in later comments if necessary.
172                     $usermappings[$comment->userid] = new stdClass();
173                     $usermappings[$comment->userid]->fullname = fullname($commenter);
174                     $usermappings[$comment->userid]->avatar = $assignment->get_renderer()->user_picture($commenter,
175                             array('size' => 18, 'link' => false));
176                 }
178                 // Set blind-marking information for this comment.
179                 $comment->fullname = $usermappings[$comment->userid]->fullname;
180                 $comment->avatar = $usermappings[$comment->userid]->avatar;
181                 $comment->profileurl = null;
182             }
183         }
184     }
186     return $comments;
189 /**
190  * Callback to force the userid for all comments to be the userid of the submission and NOT the global $USER->id. This
191  * is required by the upgrade code. Note the comment area is used to identify upgrades.
192  *
193  * @param stdClass $comment
194  * @param stdClass $param
195  */
196 function assignsubmission_comments_comment_add(stdClass $comment, stdClass $param) {
198     global $DB;
199     if ($comment->commentarea == 'submission_comments_upgrade') {
200         $submissionid = $comment->itemid;
201         $submission = $DB->get_record('assign_submission', array('id' => $submissionid));
203         $comment->userid = $submission->userid;
204         $comment->commentarea = 'submission_comments';
205     }