Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

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