Example #1
0
 public function export_for_template(\renderer_base $output)
 {
     global $USER;
     $context = clone $this->notification;
     if ($context->useridto == $USER->id && $context->timeusertodeleted) {
         $context->deleted = true;
     } else {
         $context->deleted = false;
     }
     $context->timecreatedpretty = get_string('ago', 'message', format_time(time() - $context->timecreated));
     $context->text = message_format_message_text($context);
     $context->read = $context->timeread ? true : false;
     $context->shortenedsubject = shorten_text($context->subject, 125);
     if (!empty($context->component) && substr($context->component, 0, 4) == 'mod_') {
         $iconurl = $output->pix_url('icon', $context->component);
     } else {
         $iconurl = $output->pix_url('i/marker', 'core');
     }
     $context->iconurl = $iconurl->out();
     return $context;
 }
Example #2
0
 /**
  * Get messages function implementation.
  *
  * @since  2.8
  * @throws invalid_parameter_exception
  * @throws moodle_exception
  * @param  int      $useridto       the user id who received the message
  * @param  int      $useridfrom     the user id who send the message. -10 or -20 for no-reply or support user
  * @param  string   $type           type of message to return, expected values: notifications, conversations and both
  * @param  bool     $read           true for retreiving read messages, false for unread
  * @param  bool     $newestfirst    true for ordering by newest first, false for oldest first
  * @param  int      $limitfrom      limit from
  * @param  int      $limitnum       limit num
  * @return external_description
  */
 public static function get_messages($useridto, $useridfrom = 0, $type = 'both', $read = true, $newestfirst = true, $limitfrom = 0, $limitnum = 0)
 {
     global $CFG, $USER;
     $warnings = array();
     $params = array('useridto' => $useridto, 'useridfrom' => $useridfrom, 'type' => $type, 'read' => $read, 'newestfirst' => $newestfirst, 'limitfrom' => $limitfrom, 'limitnum' => $limitnum);
     $params = self::validate_parameters(self::get_messages_parameters(), $params);
     $context = context_system::instance();
     self::validate_context($context);
     $useridto = $params['useridto'];
     $useridfrom = $params['useridfrom'];
     $type = $params['type'];
     $read = $params['read'];
     $newestfirst = $params['newestfirst'];
     $limitfrom = $params['limitfrom'];
     $limitnum = $params['limitnum'];
     $allowedvalues = array('notifications', 'conversations', 'both');
     if (!in_array($type, $allowedvalues)) {
         throw new invalid_parameter_exception('Invalid value for type parameter (value: ' . $type . '),' . 'allowed values are: ' . implode(',', $allowedvalues));
     }
     // Check if private messaging between users is allowed.
     if (empty($CFG->messaging)) {
         // If we are retreiving only conversations, and messaging is disabled, throw an exception.
         if ($type == "conversations") {
             throw new moodle_exception('disabled', 'message');
         }
         if ($type == "both") {
             $warning = array();
             $warning['item'] = 'message';
             $warning['itemid'] = $USER->id;
             $warning['warningcode'] = '1';
             $warning['message'] = 'Private messages (conversations) are not enabled in this site.
                 Only notifications will be returned';
             $warnings[] = $warning;
         }
     }
     if (!empty($useridto)) {
         if (core_user::is_real_user($useridto)) {
             $userto = core_user::get_user($useridto, '*', MUST_EXIST);
         } else {
             throw new moodle_exception('invaliduser');
         }
     }
     if (!empty($useridfrom)) {
         // We use get_user here because the from user can be the noreply or support user.
         $userfrom = core_user::get_user($useridfrom, '*', MUST_EXIST);
     }
     // Check if the current user is the sender/receiver or just a privileged user.
     if ($useridto != $USER->id and $useridfrom != $USER->id and !has_capability('moodle/site:readallmessages', $context)) {
         throw new moodle_exception('accessdenied', 'admin');
     }
     // Which type of messages to retrieve.
     $notifications = -1;
     if ($type != 'both') {
         $notifications = $type == 'notifications' ? 1 : 0;
     }
     $orderdirection = $newestfirst ? 'DESC' : 'ASC';
     $sort = "mr.timecreated {$orderdirection}";
     if ($messages = message_get_messages($useridto, $useridfrom, $notifications, $read, $sort, $limitfrom, $limitnum)) {
         $canviewfullname = has_capability('moodle/site:viewfullnames', $context);
         // In some cases, we don't need to get the to/from user objects from the sql query.
         $userfromfullname = '';
         $usertofullname = '';
         // In this case, the useridto field is not empty, so we can get the user destinatary fullname from there.
         if (!empty($useridto)) {
             $usertofullname = fullname($userto, $canviewfullname);
             // The user from may or may not be filled.
             if (!empty($useridfrom)) {
                 $userfromfullname = fullname($userfrom, $canviewfullname);
             }
         } else {
             // If the useridto field is empty, the useridfrom must be filled.
             $userfromfullname = fullname($userfrom, $canviewfullname);
         }
         foreach ($messages as $mid => $message) {
             // Do not return deleted messages.
             if ($useridto == $USER->id and $message->timeusertodeleted or $useridfrom == $USER->id and $message->timeuserfromdeleted) {
                 unset($messages[$mid]);
                 continue;
             }
             // We need to get the user from the query.
             if (empty($userfromfullname)) {
                 // Check for non-reply and support users.
                 if (core_user::is_real_user($message->useridfrom)) {
                     $user = new stdClass();
                     $user = username_load_fields_from_object($user, $message, 'userfrom');
                     $message->userfromfullname = fullname($user, $canviewfullname);
                 } else {
                     $user = core_user::get_user($message->useridfrom);
                     $message->userfromfullname = fullname($user, $canviewfullname);
                 }
             } else {
                 $message->userfromfullname = $userfromfullname;
             }
             // We need to get the user from the query.
             if (empty($usertofullname)) {
                 $user = new stdClass();
                 $user = username_load_fields_from_object($user, $message, 'userto');
                 $message->usertofullname = fullname($user, $canviewfullname);
             } else {
                 $message->usertofullname = $usertofullname;
             }
             // This field is only available in the message_read table.
             if (!isset($message->timeread)) {
                 $message->timeread = 0;
             }
             $message->text = message_format_message_text($message);
             $messages[$mid] = (array) $message;
         }
     }
     $results = array('messages' => $messages, 'warnings' => $warnings);
     return $results;
 }
Example #3
0
/**
 * Format a message for display in the message history
 *
 * @param object $message the message object
 * @param string $format optional date format
 * @param string $keywords keywords to highlight
 * @param string $class CSS class to apply to the div around the message
 * @return string the formatted message
 */
function message_format_message($message, $format='', $keywords='', $class='other') {

    static $dateformat;

    //if we haven't previously set the date format or they've supplied a new one
    if ( empty($dateformat) || (!empty($format) && $dateformat != $format) ) {
        if ($format) {
            $dateformat = $format;
        } else {
            $dateformat = get_string('strftimedatetimeshort');
        }
    }
    $time = userdate($message->timecreated, $dateformat);

    $messagetext = message_format_message_text($message, false);

    if ($keywords) {
        $messagetext = highlight($keywords, $messagetext);
    }

    $messagetext .= message_format_contexturl($message);

    $messagetext = clean_text($messagetext, FORMAT_HTML);

    return <<<TEMPLATE
<div class='message $class'>
    <a name="m{$message->id}"></a>
    <span class="message-meta"><span class="time">$time</span></span>: <span class="text">$messagetext</span>
</div>
TEMPLATE;
}
Example #4
0
 /**
  * Helper function to return an array of messages.
  *
  * @param int $userid
  * @param array $messages
  * @return array
  */
 public static function create_messages($userid, $messages)
 {
     // Store the messages.
     $arrmessages = array();
     // We always view messages from oldest to newest, ensure we have it in that order.
     $lastmessage = end($messages);
     $firstmessage = reset($messages);
     if ($lastmessage->timecreated < $firstmessage->timecreated) {
         $messages = array_reverse($messages);
     }
     // Keeps track of the last day, month and year combo we were viewing.
     $day = '';
     $month = '';
     $year = '';
     foreach ($messages as $message) {
         // Check if we are now viewing a different block period.
         $displayblocktime = false;
         $date = usergetdate($message->timecreated);
         if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
             $day = $date['mday'];
             $month = $date['month'];
             $year = $date['year'];
             $displayblocktime = true;
         }
         // Store the message to pass to the renderable.
         $msg = new \stdClass();
         $msg->id = $message->id;
         $msg->text = message_format_message_text($message);
         $msg->currentuserid = $userid;
         $msg->useridfrom = $message->useridfrom;
         $msg->useridto = $message->useridto;
         $msg->displayblocktime = $displayblocktime;
         $msg->timecreated = $message->timecreated;
         $msg->timeread = $message->timeread;
         $arrmessages[] = $msg;
     }
     return $arrmessages;
 }