[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/competency/ -> lib.php (source)

   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   * Competency lib.
  19   *
  20   * @package    core_competency
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core_competency\api;
  28  use core_competency\plan;
  29  use core_competency\url;
  30  use core_competency\user_competency;
  31  use core_competency\user_evidence;
  32  
  33  /**
  34   * Hook when a comment is added.
  35   *
  36   * @param  stdClass $comment The comment.
  37   * @param  stdClass $params The parameters.
  38   * @return array
  39   */
  40  function core_competency_comment_add($comment, $params) {
  41      global $USER;
  42  
  43      if (!get_config('core_competency', 'enabled')) {
  44          return;
  45      }
  46  
  47      if ($params->commentarea == 'user_competency') {
  48          $uc = new user_competency($params->itemid);
  49  
  50          // Message both the user and the reviewer, except when they are the author of the message.
  51          $recipients = array($uc->get_userid());
  52          if ($uc->get_reviewerid()) {
  53              $recipients[] = $uc->get_reviewerid();
  54          }
  55          $recipients = array_diff($recipients, array($comment->userid));
  56          if (empty($recipients)) {
  57              return;
  58          }
  59  
  60          // Get the sender.
  61          $user = $USER;
  62          if ($USER->id != $comment->userid) {
  63              $user = core_user::get_user($comment->userid);
  64          }
  65          $fullname = fullname($user);
  66  
  67          // Get the competency.
  68          $competency = $uc->get_competency();
  69          $competencyname = format_string($competency->get_shortname(), true, array('context' => $competency->get_context()));
  70  
  71          // We want to send a message for one plan, trying to find an active one first, or the last modified one.
  72          $plan = null;
  73          $plans = $uc->get_plans();
  74          foreach ($plans as $candidate) {
  75              if ($candidate->get_status() == plan::STATUS_ACTIVE) {
  76                  $plan = $candidate;
  77                  break;
  78  
  79              } else if (!empty($plan) && $plan->get_timemodified() < $candidate->get_timemodified()) {
  80                  $plan = $candidate;
  81  
  82              } else if (empty($plan)) {
  83                  $plan = $candidate;
  84              }
  85          }
  86  
  87          // Urls.
  88          // TODO MDL-52749 Replace the link to the plan with the user competency page.
  89          if (empty($plan)) {
  90              $urlname = get_string('userplans', 'core_competency');
  91              $url = url::plans($uc->get_userid());
  92          } else {
  93              $urlname = $competencyname;
  94              $url = url::user_competency_in_plan($uc->get_userid(), $uc->get_competencyid(), $plan->get_id());
  95          }
  96  
  97          // Construct the message content.
  98          $fullmessagehtml = get_string('usercommentedonacompetencyhtml', 'core_competency', array(
  99              'fullname' => $fullname,
 100              'competency' => $competencyname,
 101              'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
 102              'url' => $url->out(true),
 103              'urlname' => $urlname,
 104          ));
 105          if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
 106              $format = FORMAT_MOODLE;
 107              $fullmessage = get_string('usercommentedonacompetency', 'core_competency', array(
 108                  'fullname' => $fullname,
 109                  'competency' => $competencyname,
 110                  'comment' => $comment->content,
 111                  'url' => $url->out(false),
 112              ));
 113          } else {
 114              $format = FORMAT_HTML;
 115              $fullmessage = $fullmessagehtml;
 116          }
 117  
 118          $message = new \core\message\message();
 119          $message->component = 'moodle';
 120          $message->name = 'competencyusercompcomment';
 121          $message->notification = 1;
 122          $message->userfrom = core_user::get_noreply_user();
 123          $message->subject = get_string('usercommentedonacompetencysubject', 'core_competency', $fullname);
 124          $message->fullmessage = $fullmessage;
 125          $message->fullmessageformat = $format;
 126          $message->fullmessagehtml = $fullmessagehtml;
 127          $message->smallmessage = get_string('usercommentedonacompetencysmall', 'core_competency', array(
 128              'fullname' => $fullname,
 129              'competency' => $competencyname,
 130          ));
 131          $message->contexturl = $url->out(false);
 132          $message->contexturlname = $urlname;
 133  
 134          // Message each recipient.
 135          foreach ($recipients as $recipient) {
 136              $msgcopy = clone($message);
 137              $msgcopy->userto = $recipient;
 138              message_send($msgcopy);
 139          }
 140  
 141      } else if ($params->commentarea == 'plan') {
 142          $plan = new plan($params->itemid);
 143  
 144          // Message both the user and the reviewer, except when they are the author of the message.
 145          $recipients = array($plan->get_userid());
 146          if ($plan->get_reviewerid()) {
 147              $recipients[] = $plan->get_reviewerid();
 148          }
 149          $recipients = array_diff($recipients, array($comment->userid));
 150          if (empty($recipients)) {
 151              return;
 152          }
 153  
 154          // Get the sender.
 155          $user = $USER;
 156          if ($USER->id != $comment->userid) {
 157              $user = core_user::get_user($comment->userid);
 158          }
 159  
 160          $fullname = fullname($user);
 161          $planname = format_string($plan->get_name(), true, array('context' => $plan->get_context()));
 162          $urlname = $planname;
 163          $url = url::plan($plan->get_id());
 164  
 165          // Construct the message content.
 166          $fullmessagehtml = get_string('usercommentedonaplanhtml', 'core_competency', array(
 167              'fullname' => $fullname,
 168              'plan' => $planname,
 169              'comment' => format_text($comment->content, $comment->format, array('context' => $params->context->id)),
 170              'url' => $url->out(true),
 171              'urlname' => $urlname,
 172          ));
 173          if ($comment->format == FORMAT_PLAIN || $comment->format == FORMAT_MOODLE) {
 174              $format = FORMAT_MOODLE;
 175              $fullmessage = get_string('usercommentedonaplan', 'core_competency', array(
 176                  'fullname' => $fullname,
 177                  'plan' => $planname,
 178                  'comment' => $comment->content,
 179                  'url' => $url->out(false),
 180              ));
 181          } else {
 182              $format = FORMAT_HTML;
 183              $fullmessage = $fullmessagehtml;
 184          }
 185  
 186          $message = new \core\message\message();
 187          $message->component = 'moodle';
 188          $message->name = 'competencyplancomment';
 189          $message->notification = 1;
 190          $message->userfrom = core_user::get_noreply_user();
 191          $message->subject = get_string('usercommentedonaplansubject', 'core_competency', $fullname);
 192          $message->fullmessage = $fullmessage;
 193          $message->fullmessageformat = $format;
 194          $message->fullmessagehtml = $fullmessagehtml;
 195          $message->smallmessage = get_string('usercommentedonaplansmall', 'core_competency', array(
 196              'fullname' => $fullname,
 197              'plan' => $planname,
 198          ));
 199          $message->contexturl = $url->out(false);
 200          $message->contexturlname = $urlname;
 201  
 202          // Message each recipient.
 203          foreach ($recipients as $recipient) {
 204              $msgcopy = clone($message);
 205              $msgcopy->userto = $recipient;
 206              message_send($msgcopy);
 207          }
 208      }
 209  }
 210  
 211  /**
 212   * Return the permissions of for the comments.
 213   *
 214   * @param  stdClass $params The parameters.
 215   * @return array
 216   */
 217  function core_competency_comment_permissions($params) {
 218      if (!get_config('core_competency', 'enabled')) {
 219          return array('post' => false, 'view' => false);
 220      }
 221  
 222      if ($params->commentarea == 'user_competency') {
 223          $uc = new user_competency($params->itemid);
 224          if ($uc->can_read()) {
 225              return array('post' => $uc->can_comment(), 'view' => $uc->can_read_comments());
 226          }
 227      } else if ($params->commentarea == 'plan') {
 228          $plan = new plan($params->itemid);
 229          if ($plan->can_read()) {
 230              return array('post' => $plan->can_comment(), 'view' => $plan->can_read_comments());
 231          }
 232      }
 233  
 234      return array('post' => false, 'view' => false);
 235  }
 236  
 237  /**
 238   * Validates comments.
 239   *
 240   * @param  stdClass $params The parameters.
 241   * @return bool
 242   */
 243  function core_competency_comment_validate($params) {
 244      if (!get_config('core_competency', 'enabled')) {
 245          return false;
 246      }
 247  
 248      if ($params->commentarea == 'user_competency') {
 249          if (!user_competency::record_exists($params->itemid)) {
 250              return false;
 251          }
 252          return true;
 253      } else if ($params->commentarea == 'plan') {
 254          if (!plan::record_exists($params->itemid)) {
 255              return false;
 256          }
 257          return true;
 258      }
 259      return false;
 260  }
 261  
 262  /**
 263   * File serving.
 264   *
 265   * @param stdClass $course The course object.
 266   * @param stdClass $cm The cm object.
 267   * @param context $context The context object.
 268   * @param string $filearea The file area.
 269   * @param array $args List of arguments.
 270   * @param bool $forcedownload Whether or not to force the download of the file.
 271   * @param array $options Array of options.
 272   * @return void|false
 273   */
 274  function core_competency_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = array()) {
 275      global $CFG;
 276  
 277      if (!get_config('core_competency', 'enabled')) {
 278          return false;
 279      }
 280  
 281      $fs = get_file_storage();
 282      $file = null;
 283  
 284      $itemid = array_shift($args);
 285      $filename = array_shift($args);
 286      $filepath = $args ? '/' .implode('/', $args) . '/' : '/';
 287  
 288      if ($filearea == 'userevidence' && $context->contextlevel == CONTEXT_USER) {
 289          if (user_evidence::can_read_user($context->instanceid)) {
 290              $file = $fs->get_file($context->id, 'core_competency', $filearea, $itemid, $filepath, $filename);
 291          }
 292      }
 293  
 294      if (!$file) {
 295          return false;
 296      }
 297  
 298      send_stored_file($file, null, 0, $forcedownload);
 299  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1