/** * Serves the message attachments. Implements needed access control ;-) * * @param object $course * @param object $cm * @param object $context * @param string $filearea * @param array $args * @param bool $forcedownload * @return bool false if file not found, does not return if found - justsend the file */ function block_jmail_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) { global $SCRIPT; if ($context->contextlevel != CONTEXT_BLOCK) { //send_file_not_found(); } require_course_login($course); $coursecontext = block_jmail_get_context(CONTEXT_COURSE, $course->id, MUST_EXIST); // The mailbox constructor does the permission validation if (!($mailbox = new block_jmail_mailbox($course, $coursecontext, $context))) { return; } $messageid = (int) array_shift($args); $message = block_jmail_message::get_from_id($messageid); // We check if we are the senders or the receivers if (!$message) { send_file_not_found(); } $pendingaprobal = !$message->approved and has_capability('block/jmail:approvemessages', $context); if (!$message->is_mine() and !$pendingaprobal) { send_file_not_found(); } $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$context->id}/block_jmail/{$filearea}/{$messageid}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } $forcedownload = true; send_stored_file($file, 60 * 60, 0, $forcedownload); }
public function get_content() { global $CFG, $DB; if (empty($this->instance)) { $this->content = ''; return $this->content; } if ($this->content !== null) { return $this->content; } // Only for logged and non guest users if (!isloggedin() or isguestuser()) { return ''; } require_once dirname(__FILE__) . '/block_jmail_mailbox.class.php'; $this->content = new stdClass(); $this->content->items = array(); $this->content->icons = array(); $this->content->footer = ''; $currentcontext = $this->page->context; $renderer = $this->page->get_renderer('block_jmail'); $newmailicon = $renderer->plugin_icon('new'); $mycourses = array(); if ($this->page->course->id == SITEID) { if (!empty($CFG->block_jmail_enable_globalinbox)) { $this->content->footer = $renderer->global_inbox(); } if ($mycourses = block_jmail_mailbox::get_my_mailboxes()) { foreach ($mycourses as $course) { if ($mailbox = new block_jmail_mailbox($course)) { if ($unreadmails = $mailbox->count_unread_messages()) { $this->content->items[] = $renderer->unread_messages($mailbox); $this->content->icons[] = $newmailicon; } } } } } else { $this->content->footer = $renderer->course_inbox($this->page->course); $context = block_jmail_get_context(CONTEXT_COURSE, $this->page->course->id); if ($mailbox = new block_jmail_mailbox($this->page->course, $context)) { if ($unreadmails = $mailbox->count_unread_messages()) { $mailbox->pagesize = 5; if ($messages = $mailbox->get_message_headers('unread', 0, 'date', 'desc', '')) { foreach ($messages[1] as $m) { $strfrom = get_string('from', 'block_jmail'); $messagetext = shorten_text($m->subject, 25) . " (<i>{$strfrom}: " . shorten_text($m->from, 15) . ")</i>"; $this->content->items[] = $messagetext; $this->content->icons[] = ''; } } $this->content->items[] = get_string('unreadmessages', 'block_jmail', $unreadmails); $this->content->icons[] = $newmailicon; } } } if (!count($this->content->items)) { $this->content->items[] = get_string('nonewmessages', 'block_jmail'); } return $this->content; }
public static function get_my_mailboxes() { global $DB, $USER, $CFG; $mailboxes = array(); if (has_capability('moodle/site:config', block_jmail_get_context(CONTEXT_SYSTEM))) { $mycourses = $DB->get_records_sql("SELECT DISTINCT(c.id), c.shortname FROM {course} c LEFT JOIN {block_jmail_sent} s ON s.userid = :userid LEFT JOIN {block_jmail} j ON j.id = s.messageid", array('userid' => $USER->id)); } else { $mycourses = enrol_get_my_courses(); } if ($mycourses) { foreach ($mycourses as $c) { context_helper::preload_from_record($c); if (!($context = block_jmail_get_context(CONTEXT_COURSE, $c->id))) { continue; } if (!($instances = $DB->get_records('block_instances', array('blockname' => 'jmail', 'parentcontextid' => $context->id)))) { continue; } $instance = array_shift($instances); if ($c->id != SITEID) { $blockcontext = block_jmail_get_context(CONTEXT_BLOCK, $instance->id, MUST_EXIST); if (!has_capability('block/jmail:viewmailbox', $blockcontext)) { continue; } } else { if (empty($CFG->block_jmail_enable_globalinbox)) { continue; } } if ($invisible = $DB->get_records('block_positions', array('blockinstanceid' => $instance->id, 'visible' => 0))) { continue; } $mailbox = new stdClass(); $mailbox->id = $c->id; $mailbox->shortname = format_string($c->shortname); $mailboxes[$c->id] = $mailbox; } } return $mailboxes; }
$editor = ''; if ($CFG->version >= 2014051200) { if ($USER->preference) { if (!empty($USER->preference['htmleditor'])) { $editor = $USER->preference['htmleditor']; } } } if (!$editor) { require_once $CFG->libdir . '/editorlib.php'; $editors = array_keys(editors_get_enabled()); if (count($editors) > 1) { $editor = array_shift($editors); } } $usercontext = block_jmail_get_context(CONTEXT_USER, $USER->id); $privatefiles = has_capability('moodle/user:manageownfiles', $usercontext); $jmailcfg = array('wwwroot' => $CFG->wwwroot, 'courseid' => $course->id, 'sesskey' => sesskey(), 'pagesize' => $mailbox->pagesize, 'cansend' => $mailbox->cansend, 'canapprovemessages' => $mailbox->canapprovemessages, 'canmanagelabels' => $mailbox->canmanagelabels, 'canmanagepreferences' => $mailbox->canmanagepreferences, 'userid' => $USER->id, 'globalinbox' => $mailbox->globalinbox, 'approvemode' => !empty($mailbox->config->approvemode) ? $mailbox->config->approvemode : false, 'usertoid' => $usertoid, 'usertoname' => $usertoname, 'version' => $CFG->version, 'editor' => $editor, 'privatefiles' => $privatefiles); //$PAGE->requires->js('/lib/editor/tinymce/tiny_mce/3.4.2/tiny_mce.js'); //MDL-34741 switch to YUI2 to 2in3 (2.4 and above) if ($CFG->version < 2012120300) { $PAGE->requires->yui2_lib(array('event', 'dragdrop', 'element', 'animation', 'resize', 'layout', 'widget', 'button', 'editor', 'get', 'connection', 'datasource', 'datatable', 'container', 'utilities', 'menu', 'json', 'paginator')); } //MDL-34741 switch to YUI2 to 2in3 (2.4 and above) if ($CFG->version < 2012120300) { // 2.1, 2.2, 2.3 $PAGE->requires->js_init_call('M.block_jmail.init', array($jmailcfg), true, $module); } else { // 2.4 and onwards.. $PAGE->requires->js_init_call('M.block_jmail.initYAHOO', array($jmailcfg), true, $module); }
*/ require_once dirname(__FILE__) . '/../../config.php'; require_once $CFG->dirroot . '/blocks/jmail/block_jmail_mailbox.class.php'; require_once $CFG->dirroot . '/blocks/jmail/message_form.php'; $id = required_param('id', PARAM_INT); $messageid = required_param('messageid', PARAM_INT); $mode = required_param('mode', PARAM_ALPHA); $PAGE->set_url('/blocks/jmail/message.php', array('id' => $id, 'messageid' => $messageid)); if (!($course = $DB->get_record('course', array('id' => $id)))) { throw new moodle_exception('invalidcourseid', 'error'); } if (!($block = $DB->get_record('block', array('name' => 'jmail', 'visible' => 1)))) { throw new moodle_exception('invalidcourseid', 'error'); } require_login($course->id); $context = block_jmail_get_context(CONTEXT_COURSE, $course->id, MUST_EXIST); $PAGE->set_context($context); if (!($mailbox = new block_jmail_mailbox($course, $context))) { throw new moodle_exception('Invalid mailbox'); } $blockcontext = $mailbox->blockcontext; // TODO, check block disabled or instance not visible? $message = block_jmail_message::get_from_id($messageid); if (!$message or !$message->is_mine()) { $messageid = 0; } if ($messageid and $message->courseid != $course->id) { throw new moodle_exception('invalidcourseid', 'error'); } if ($mailbox->cansend) { $mform = new block_jmail_message_form(null, array('course' => $course, 'context' => $blockcontext));
/** * Save a message and destinataries * @param array $destinataries Destinataries for the message * @param array $attachments File attachments * @return mixed Message id or false if something fails */ public function save($destinataries, $attachments, $editoritemid) { global $USER, $DB, $CFG; $message = new stdClass(); $message->sender = $USER->id; $message->courseid = $this->courseid; $message->subject = $this->subject; $message->body = $this->body; //$post->message = file_save_draft_area_files($post->itemid, $context->id, 'mod_forum', 'post', $post->id, array('subdirs'=>true), $post->message); $message->attachment = 0; $message->approved = $this->approved; $message->timesent = $this->timesent; $message->timecreated = $this->timecreated; if ($messageid = $DB->insert_record('block_jmail', $message)) { foreach ($destinataries as $d) { if (!$d->userid) { continue; } $to = new stdClass(); $to->userid = $d->userid; $to->messageid = $messageid; $to->type = $d->type; $to->mread = 0; $to->answered = 0; $to->deleted = 0; $to->labeled = 0; $DB->insert_record('block_jmail_sent', $to); } $this->id = $messageid; $context = block_jmail_get_context(CONTEXT_COURSE, $this->courseid); // We need the block instance for saving the attachments if ($instance = $DB->get_record('block_instances', array('blockname' => 'jmail', 'parentcontextid' => $context->id))) { // attachments $context = block_jmail_get_context(CONTEXT_BLOCK, $instance->id); require_once $CFG->libdir . '/filelib.php'; $message->body = file_save_draft_area_files($editoritemid, $context->id, 'block_jmail', 'body', $messageid, array('subdirs' => true), $message->body); $DB->set_field('block_jmail', 'body', $message->body, array('id' => $messageid)); $info = file_get_draft_area_info($attachments); $present = $info['filecount'] > 0 ? '1' : ''; file_save_draft_area_files($attachments, $context->id, 'block_jmail', 'attachment', $messageid); $DB->set_field('block_jmail', 'attachment', $present, array('id' => $messageid)); } return $messageid; } return false; }