Skip to content

Commit

Permalink
Added button to send a message in user profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Gasset committed Apr 22, 2013
1 parent 42e32f5 commit 10e90bc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions create.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_once('create_form.php');

$courseid = optional_param('c', $SITE->id, PARAM_INT);
$recipient = optional_param('r', false, PARAM_INT);

// Setup page

Expand All @@ -36,6 +37,9 @@
if ($course->id != $SITE->id) {
require_sesskey();
$message = local_mail_message::create($USER->id, $course->id);
if (local_mail_valid_recipient($recipient)) {
$message->add_recipient('to', $recipient);
}
$params = array('m' => $message->id());
$url = new moodle_url('/local/mail/compose.php', $params);
redirect($url);
Expand Down
1 change: 1 addition & 0 deletions lang/ca/local_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
$string['forward'] = 'Reenvia';
$string['save'] = 'Desa';
$string['send'] = 'Envia';
$string['sendmessage'] = 'Envia un missatge';
$string['sentmail'] = 'Enviats';
$string['setlabels'] = 'Etiquetes';
$string['starred'] = 'Destacat';
Expand Down
1 change: 1 addition & 0 deletions lang/en/local_mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
$string['forward'] = 'Forward';
$string['save'] = 'Save';
$string['send'] = 'Send';
$string['sendmessage'] = 'Send a message';
$string['sentmail'] = 'Sent';
$string['setlabels'] = 'Labels';
$string['starred'] = 'Starred';
Expand Down
12 changes: 12 additions & 0 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ function local_mail_extends_navigation($root) {
$text = get_string('trash', 'local_mail');
$url = new moodle_url('/local/mail/view.php', array('t' => 'trash'));
$node->add(s($text), $url);

// User profile

if ($PAGE->url->compare(new moodle_url('/user/view.php'), URL_MATCH_BASE)) {
$userid = optional_param('id', false, PARAM_INT);
if (local_mail_valid_recipient($userid)) {
$vars = array('course' => $COURSE->id, 'recipient' => $userid);
$PAGE->requires->string_for_js('sendmessage', 'local_mail');
$PAGE->requires->js_init_code('M.local_mail = ' . json_encode($vars));
$PAGE->requires->js('/local/mail/user.js');
}
}
}

function local_mail_pluginfile($course, $cm, $context, $filearea, $args,
Expand Down
27 changes: 27 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,30 @@ function local_mail_get_my_courses() {
}
return $courses;
}

function local_mail_valid_recipient($recipient) {
global $COURSE, $USER;

if (!$recipient or $recipient == $USER->id) {
return false;
}

$context = context_course::instance($COURSE->id);

if (!is_enrolled($context, $recipient)) {
return false;
}

if ($COURSE->groupmode == SEPARATEGROUPS and
!has_capability('moodle/site:accessallgroups', $context)) {
$ugroups = groups_get_all_groups($COURSE->id, $USER->id,
$COURSE->defaultgroupingid, 'g.id');
$rgroups = groups_get_all_groups($COURSE->id, $recipient,
$COURSE->defaultgroupingid, 'g.id');
if (!array_intersect(array_keys($ugroups), array_keys($rgroups))) {
return false;
}
}

return true;
}
7 changes: 7 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -778,3 +778,10 @@
#local_mail_main_form {
min-height: 30em;
}

/* User profile */

.local_mail_sendmessage {
text-align: center;
margin: 1em;
}
17 changes: 17 additions & 0 deletions user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
YUI(M.yui.loader).use('node', function(Y) {
Y.on('domready', function() {
var node = Y.one('.userprofile');
if (node) {
var html = '<div class="local_mail_sendmessage">'
+ '<form action="' + M.cfg.wwwroot + '/local/mail/create.php" method="post">'
+ '<input type="hidden" name="c" value="' + M.local_mail.course +'" />'
+ '<input type="hidden" name="r" value="' + M.local_mail.recipient +'" />'
+ '<input type="hidden" name="sesskey" value="' + M.cfg.sesskey +'" />'
+ '<input type="submit" value="' + M.util.get_string('sendmessage', 'local_mail') + '" />'
+ '</form>'
+ '</div>';
var form = Y.Node.create(html);
node.append(form);
}
});
});

0 comments on commit 10e90bc

Please sign in to comment.