/**
 *
 * retrieves the messages for user $usr (or logged in user as default). To
 * view the messages the user has sent, use $role='sender'. Type should always
 * be 2 (usermessage). Limit and offset can be used for paginated views
 * <br/>
 * example result:                          <br/>
 * array (                                  <br/>
 *   0 =>                                   <br/>
 *   stdClass::__set_state(array(           <br/>
 *      'id' => '23',                       <br/>
 *      'type' => '2',                      <br/>
 *      'ctime' => '2013-09-12 10:12:26',   <br/>
 *      'subject' => 'some subject',        <br/>
 *      'message' => 'some body',           <br/>
 *      'parent' => NULL,                   <br/>
 *      'userids' =>                        <br/>
 *     array (                              <br/>
 *       0 => '1',                          <br/>
 *       1 => '2',                          <br/>
 *     ),                                   <br/>
 *      'fromid' => '1',                    <br/>
 *   ))                                     <br/>
 * )                                        <br/>
 *
 * @param int $usr the id of the user
 * @param string $role the role can be <b>sender</b> or <b>recipient</b>
 * @param int $type so far it's always <b>2</b> for usermessage
 * @param string $sortby should be <b>null</b> or <b>ctime ASC</b> or <b>ctime DESC</b>
 * @param int $limit limits the number of results - for paginations for example
 * @param int $offset offset of the first row to return
 * @return array
 */
function get_messages_mr($usr = null, $role = 'recipient', $type = null, $sortby = null, $limit = 20, $offset = 0)
{
    $messageids = get_message_ids_mr($usr, $role, $type, $sortby, $limit, $offset);
    if (null === $usr) {
        global $USER;
        $usr = $USER->get('id');
    }
    foreach ($messageids as $msgid) {
        $msg = get_message_mr($usr, $msgid);
        if (null !== $msg) {
            $messages[] = $msg;
        }
    }
    return $messages;
}
Esempio n. 2
0
 public static function render_instance(BlockInstance $instance, $editing = false)
 {
     global $USER, $THEME;
     $configdata = $instance->get('configdata');
     $desiredtypes = array();
     foreach ($configdata as $k => $v) {
         if (!empty($v) && $k != 'maxitems') {
             $type = preg_replace('/[^a-z]+/', '', $k);
             $desiredtypes[$type] = $type;
         }
     }
     if ($USER->get('admin') && !empty($desiredtypes['adminmessages'])) {
         unset($desiredtypes['adminmessages']);
         $desiredtypes += get_column('activity_type', 'name', 'admin', 1);
     }
     $maxitems = isset($configdata['maxitems']) ? $configdata['maxitems'] : 5;
     // check if multirecipientnotification plugin is active or if we proceed here
     if (record_exists('artefact_installed', 'name', 'multirecipientnotification', 'active', '1')) {
         global $USER;
         $userid = $USER->get('id');
         safe_require('artefact', 'multirecipientnotification');
         $activitylist = activitylistin(join(',', $desiredtypes), $maxitems);
         $records = array();
         foreach ($activitylist->msgidrecords as $msgidrecord) {
             if ($msgidrecord->msgtable == 'notification_internal_activity') {
                 $sql = "\n                        SELECT n.id, n.subject, n.message, n.url, n.urltext, n.read, t.name AS type\n                        FROM {notification_internal_activity} n JOIN {activity_type} t ON n.type = t.id\n                        WHERE n.id = ?";
                 $notificationrecords = get_records_sql_array($sql, array($msgidrecord->id));
                 if (count($notificationrecords) === 1) {
                     $record = $notificationrecords[0];
                     $record->msgtable = $msgidrecord->msgtable;
                     $records[] = $record;
                 }
             } else {
                 $record = get_message_mr($userid, $msgidrecord->id);
                 if (null === $record) {
                     continue;
                 }
                 $record->url = 'artefact/multirecipientnotification/sendmessage.php?replyto=' . $msgidrecord->id . '&returnto=outbox';
                 if (count($record->userids) > 1) {
                     $record->urltext = get_string('replyurltext', 'artefact.multirecipientnotification');
                 } else {
                     $record->urltext = get_string('returnurltext', 'artefact.multirecipientnotification');
                 }
                 $record->msgtable = $msgidrecord->msgtable;
                 $records[] = $record;
             }
         }
         $showmore = $activitylist->count > $maxitems;
         // use a different template
         $smartytemplate = 'blocktype:inbox:inboxmr.tpl';
     } else {
         $records = array();
         if ($desiredtypes) {
             $sql = "\n                    SELECT n.id, n.subject, n.message, n.url, n.urltext, n.read, t.name AS type\n                    FROM {notification_internal_activity} n JOIN {activity_type} t ON n.type = t.id\n                    WHERE n.usr = ?\n                    AND t.name IN (" . join(',', array_map('db_quote', $desiredtypes)) . ")\n                    ORDER BY n.ctime DESC\n                    LIMIT ?;";
             $records = get_records_sql_array($sql, array($USER->get('id'), $maxitems + 1));
         }
         // Hack to decide whether to show the More... link
         if ($showmore = count($records) > $maxitems) {
             unset($records[$maxitems]);
         }
         $smartytemplate = 'blocktype:inbox:inbox.tpl';
     }
     if ($records) {
         require_once 'activity.php';
         foreach ($records as &$r) {
             $section = empty($r->plugintype) ? 'activity' : "{$r->plugintype}.{$r->pluginname}";
             $r->strtype = get_string('type' . $r->type, $section);
             $r->message = format_notification_whitespace($r->message, $r->type);
         }
     }
     $smarty = smarty_core();
     if ($showmore) {
         $smarty->assign('desiredtypes', implode(',', $desiredtypes));
     }
     $smarty->assign('blockid', 'blockinstance_' . $instance->get('id'));
     $smarty->assign('items', $records);
     $smarty->assign('readicon', $THEME->get_url('images/readusermessage.png'));
     return $smarty->fetch($smartytemplate);
 }
Esempio n. 3
0
    if (null === $message) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }
    if ($message[0]->usr != $USER->id) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }
    $subject = $message[0]->subject;
    $prefix = trim(get_string('replysubjectprefix', 'module.multirecipientnotification'));
    if (strpos($subject, $prefix) !== 0) {
        $subject = $prefix . ' ' . $subject;
    }
}
if (!is_null($replytoid)) {
    // Let us validate what we are going to reply first. The message should exist,
    // addressed to us and originated from the user we are replying to.
    $message = get_message_mr($USER->id, $replytoid);
    if (null === $message) {
        throw new AccessDeniedException(get_string('cantviewmessage', 'group'));
    }
    if (0 === count($users)) {
        foreach ($message->userids as $userrelid) {
            if ($USER->get('id') === $userrelid) {
                continue;
            }
            $deleted = get_field('usr', 'deleted', 'id', $userrelid);
            if ($deleted === '0' && can_send_message($USER->to_stdclass(), $userrelid) && $USER->id != $userrelid) {
                $users[] = $userrelid;
            } else {
                $SESSION->add_info_msg(get_string('removeduserfromlist', 'module.multirecipientnotification'));
            }
        }
Esempio n. 4
0
/**
 * creates a result-array with the number, limit, offset and notification-type(s)
 * of the returned htmlrepresentation of the notifications in the outbox, as well
 * as the html representation itself. The return array has the following format:
 *
 * array (
 *   'count' => '17',
 *   'limit' => 10,
 *   'offset' => 0,
 *   'type' => 'all',
 *   'tablerows' => '//html ...
 *   'pagination' => '// html
 *   'pagination_js' => '// javascript
 * )
 *
 * @global User $USER
 * @param type $type
 * @param type $limit
 * @param type $offset
 * @return array
 */
function activitylistout_html($type = 'all', $limit = 10, $offset = 0)
{
    global $USER;
    $userid = $USER->get('id');
    $typesql = '';
    if ($type != 'all') {
        // Treat as comma-separated list of activity type names
        $types = split(',', preg_replace('/[^a-z,]+/', '', $type));
        if ($types) {
            $typesql = ' at.name IN (' . join(',', array_map('db_quote', $types)) . ')';
            if (in_array('adminmessages', $types)) {
                $typesql = '(' . $typesql . ' OR at.admin = 1)';
            }
            $typesql = ' AND ' . $typesql;
        }
    }
    $notificationtargetcolumn = 'from';
    $notificationtargetrole = 'sender';
    $msgidquery = "\n        (\n        SELECT a.id, a.ctime, 'notification_internal_activity' AS msgtable\n        FROM {notification_internal_activity} AS a\n        INNER JOIN {activity_type} AS at ON a.type = at.id\n        WHERE a." . $notificationtargetcolumn . " = ?\n        " . $typesql . "\n        AND at.name != 'newpost'\n        )\n        UNION\n        (\n        SELECT a.id, a.ctime, 'artefact_multirecipient_notification' AS msgtable\n        FROM {artefact_multirecipient_notification} AS a\n        INNER JOIN {artefact_multirecipient_userrelation} AS b\n            ON a.id = b.notification\n        INNER JOIN {activity_type} AS at ON a.type = at.id\n        WHERE b.usr = ?\n        AND b.deleted = '0'\n        AND b.role = '" . $notificationtargetrole . "'\n        " . $typesql . "\n        )";
    $countquery = 'SELECT COUNT(*) FROM (' . $msgidquery . ') AS dummytable';
    $count = count_records_sql($countquery, array($userid, $userid));
    $pagination = build_pagination(array('id' => 'activitylist_pagination', 'url' => get_config('wwwroot') . 'artefact/multirecipientnotification/outbox.php?type=' . hsc($type), 'jsonscript' => 'artefact/multirecipientnotification/indexout.json.php', 'datatable' => 'activitylist', 'count' => $count, 'limit' => $limit, 'offset' => $offset, 'jumplinks' => 6, 'numbersincludeprevnext' => 2, 'setlimit' => true));
    $result = array('count' => $count, 'limit' => $limit, 'offset' => $offset, 'type' => $type, 'tablerows' => '', 'pagination' => $pagination['html'], 'pagination_js' => $pagination['javascript']);
    if ($count < 1) {
        return $result;
    }
    $records = array();
    $msgidquery .= "\n    ORDER BY ctime DESC";
    $msgidrecords = get_records_sql_array($msgidquery, array($userid, $userid), $offset, $limit);
    if (!is_array($msgidrecords)) {
        $msgidrecords = array();
    }
    foreach ($msgidrecords as $msgidrecord) {
        if ($msgidrecord->msgtable == 'notification_internal_activity') {
            $recordsarray = get_records_sql_array("SELECT a.*, at.name AS type, at.plugintype, at.pluginname\n                                      FROM {notification_internal_activity} a\n                                      INNER JOIN {activity_type} at ON a.type = at.id\n                                      WHERE a.id = ?", array($msgidrecord->id));
            if (1 !== count($recordsarray)) {
                log_warn('inconsistent message-id in notification_internal_activity, id: ' . $msgidrecord->id);
                continue;
            }
            $record = $recordsarray[0];
            $record->canreplyall = false;
            $record->canreply = false;
            $record->startnewthread = true;
            // read out receiver name
            if (isset($record->usr)) {
                $tousrarray = array('display' => display_name($record->usr), 'link' => null);
                if (!get_user($record->usr)->deleted) {
                    $tousrarray['link'] = profile_url($record->usr);
                    $record->canreply = true;
                }
                $record->tousr = array($tousrarray);
            } else {
                $record->tousr = array(array('display' => get_string('system'), 'link' => null));
            }
            // read out sender name
            if (isset($record->from)) {
                $record->fromusr = $record->from;
            } else {
                // we're in the outbox, so basically, this should hold for all messages
                $record->fromusr = $USER->get('id');
            }
            $record->date = format_date(strtotime($record->ctime), 'strfdaymonthyearshort');
            $section = empty($record->plugintype) ? 'activity' : "{$record->plugintype}.{$record->pluginname}";
            $record->strtype = get_string('type' . $record->type, $section);
            $record->message = format_notification_whitespace($record->message);
            // used to identify notification as internal for json-calls
            $record->table = 'notification_internal_activity';
            $records[] = $record;
        } else {
            if ($msgidrecord->msgtable === 'artefact_multirecipient_notification') {
                $record = get_message_mr($userid, $msgidrecord->id);
                if (null === $record) {
                    continue;
                }
                $record->strtype = $record->type;
                $record->date = format_date(strtotime($record->ctime), 'strfdaymonthyearshort');
                // We fill $record->tousr with an array per userentry, that holds the
                // display name of the user and the link to the users profile, if
                // applicable - we don't link to deleted users. Those will be summed
                // up in a single entry at the end of the list
                $deletedcount = 0;
                $record->canreply = false;
                $record->canreplyall = false;
                $record->startnewthread = false;
                for ($i = 0; $i < count($record->userids); $i++) {
                    $tousr = get_user($record->userids[$i]);
                    if ($tousr->deleted) {
                        $deletedcount++;
                    } else {
                        $record->tousr[] = array('display' => display_name($record->userids[$i]), 'link' => profile_url($record->userids[$i]));
                    }
                }
                if ($deletedcount > 0) {
                    $record->tousr[] = array('display' => $deletedcount . ' ' . get_string('deleteduser', 'artefact.multirecipientnotification'), 'link' => null);
                }
                if ($deletedcount < count($record->userids)) {
                    if (count($record->userids) - $deletedcount == 1) {
                        $record->canreply = true;
                    } else {
                        $record->canreplyall = true;
                    }
                }
                if (isset($record->fromid)) {
                    $record->fromusr = $record->fromid;
                } else {
                    $record->fromusr = 0;
                }
                $record->message = format_notification_whitespace($record->message);
                // used to identify notification as from this plugin for json-calls
                $record->table = 'artefact_multirecipient_notification';
                $records[] = $record;
            }
        }
    }
    $smarty = smarty_core();
    $smarty->assign('data', $records);
    $smarty->assign('USER', $USER);
    $smarty->assign('maxnamestrlength', PluginArtefactMultirecipientnotification::MAX_USERNAME_IN_LIST_LENGTH);
    $result['tablerows'] = $smarty->fetch('artefact:multirecipientnotification:activitylistout.tpl');
    return $result;
}