--- loncom/interface/lonmsg.pm 2005/07/08 10:39:49 1.152
+++ loncom/interface/lonmsg.pm 2006/03/21 20:54:35 1.179
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Routines for messaging
#
-# $Id: lonmsg.pm,v 1.152 2005/07/08 10:39:49 www Exp $
+# $Id: lonmsg.pm,v 1.179 2006/03/21 20:54:35 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -97,6 +97,17 @@ Right now, this document will cover just
it is likely you will not need to programmatically read messages,
since lonmsg already implements that functionality.
+The routines used to package messages and unpackage messages are not
+only used by lonmsg when creating/extracting messages for LON-CAPA's
+internal messaging system, but also by lonnotify.pm which is available
+for use by Domain Coordinators to broadcast standard e-mail to specified
+users in their domain. The XML packaging used in the two cases is very
+similar. The differences are the use of $uname and
+$udom in stored internal messages, compared
+with $email in stored
+Domain Coordinator e-mail for the storage of information about
+recipients of the message/e-mail.
+
=head1 FUNCTIONS
=over 4
@@ -114,6 +125,8 @@ use HTML::Entities();
use Mail::Send;
use Apache::lonlocal;
use Apache::loncommunicate;
+use Apache::lonfeedback;
+use Apache::lonrss();
# Querystring component with sorting type
my $sqs;
@@ -124,7 +137,7 @@ my $interdis;
sub packagemsg {
my ($subject,$message,$citation,$baseurl,$attachmenturl,
- $recuser,$recdomain)=@_;
+ $recuser,$recdomain,$msgid,$type,$crsmsgid)=@_;
$message =&HTML::Entities::encode($message,'<>&"');
$citation=&HTML::Entities::encode($citation,'<>&"');
$subject =&HTML::Entities::encode($subject,'<>&"');
@@ -134,20 +147,42 @@ sub packagemsg {
#remove machine specification
$attachmenturl =~ s|^http://[^/]+/|/|;
$attachmenturl =&HTML::Entities::encode($attachmenturl,'<>&"');
-
+ my $course_context;
+ if (defined($env{'form.replyid'})) {
+ my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid)=
+ split(/\:/,&Apache::lonnet::unescape($env{'form.replyid'}));
+ $course_context = $origcid;
+ }
+ foreach my $key (keys(%env)) {
+ if ($key=~/^form\.(rep)?rec\_(.*)$/) {
+ my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$origcid) =
+ split(/\:/,&Apache::lonnet::unescape($2));
+ $course_context = $origcid;
+ last;
+ }
+ }
+ unless(defined($course_context)) {
+ $course_context = $env{'request.course.id'};
+ }
my $now=time;
$msgcount++;
- my $partsubj=$subject;
- $partsubj=&Apache::lonnet::escape($partsubj);
- my $msgid=&Apache::lonnet::escape(
- $now.':'.$partsubj.':'.$env{'user.name'}.':'.
- $env{'user.domain'}.':'.$msgcount.':'.
- $env{'request.course.id'}.':'.$$);
- my $result=''.$env{'user.name'}.''.
+ unless(defined($msgid)) {
+ $msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
+ $msgcount,$course_context,$$);
+ }
+ my $result = ''.$env{'user.name'}.''.
''.$env{'user.domain'}.''.
''.$subject.''.
- ''.
- ''.$ENV{'SERVER_NAME'}.''.
+ '';
+ if (defined($crsmsgid)) {
+ $result.= ''.$course_context.''.
+ ''.$env{'request.course.sec'}.''.
+ ''.$msgid.''.
+ ''.$crsmsgid.''.
+ ''.$message.'';
+ return ($msgid,$result);
+ }
+ $result .= ''.$ENV{'SERVER_NAME'}.''.
''.$ENV{'HTTP_HOST'}.''.
''.$ENV{'REMOTE_ADDR'}.''.
''.$env{'browser.type'}.''.
@@ -155,14 +190,31 @@ sub packagemsg {
''.$env{'browser.version'}.''.
''.$env{'browser.mathml'}.''.
''.$ENV{'HTTP_USER_AGENT'}.''.
- ''.$env{'request.course.id'}.''.
+ ''.$course_context.''.
''.$env{'request.course.sec'}.''.
''.$env{'request.role'}.''.
''.$env{'request.filename'}.''.
- ''.$msgid.''.
- ''.$recuser.''.
- ''.$recdomain.''.
- ''.$message.'';
+ ''.$msgid.'';
+ if (ref($recuser) eq 'ARRAY') {
+ for (my $i=0; $i<@{$recuser}; $i++) {
+ if ($type eq 'dcmail') {
+ my ($username,$email) = split(/:/,$$recuser[$i]);
+ $username = &Apache::lonnet::unescape($username);
+ $email = &Apache::lonnet::unescape($email);
+ $username = &HTML::Entities::encode($username,'<>&"');
+ $email = &HTML::Entities::encode($email,'<>&"');
+ $result .= ''.
+ $email.'';
+ } else {
+ $result .= ''.$$recuser[$i].''.
+ ''.$$recdomain[$i].'';
+ }
+ }
+ } else {
+ $result .= ''.$recuser.''.
+ ''.$recdomain.'';
+ }
+ $result .= ''.$message.'';
if (defined($citation)) {
$result.=''.$citation.'';
}
@@ -186,9 +238,18 @@ sub unpackagemsg {
if ($token->[0] eq 'S') {
my $entry=$token->[1];
my $value=$parser->get_text('/'.$entry);
- $content{$entry}=$value;
+ if (($entry eq 'recuser') || ($entry eq 'recdomain')) {
+ push(@{$content{$entry}},$value);
+ } elsif ($entry eq 'recipient') {
+ my $username = $token->[2]{'username'};
+ $username = &HTML::Entities::decode($username,'<>&"');
+ $content{$entry}{$username} = $value;
+ } else {
+ $content{$entry}=$value;
+ }
}
}
+ if (!exists($content{'recuser'})) { $content{'recuser'} = []; }
if ($content{'attachmenturl'}) {
my ($fname)=($content{'attachmenturl'}=~m|/([^/]+)$|);
if ($notoken) {
@@ -206,15 +267,30 @@ sub unpackagemsg {
# ======================================================= Get info out of msgid
+sub buildmsgid {
+ my ($now,$subject,$uname,$udom,$msgcount,$course_context,$pid) = @_;
+ $subject=&Apache::lonnet::escape($subject);
+ return(&Apache::lonnet::escape($now.':'.$subject.':'.$uname.':'.
+ $udom.':'.$msgcount.':'.$course_context.':'.$pid));
+}
+
sub unpackmsgid {
- my ($msgid,$folder)=@_;
+ my ($msgid,$folder,$skipstatus,$status_cache)=@_;
$msgid=&Apache::lonnet::unescape($msgid);
- my $suffix=&foldersuffix($folder);
- my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid)=split(/\:/,
- &Apache::lonnet::unescape($msgid));
- my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
- if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
- unless ($status{$msgid}) { $status{$msgid}='new'; }
+ my ($sendtime,$shortsubj,$fromname,$fromdomain,$count,$fromcid,
+ $processid)=split(/\:/,&Apache::lonnet::unescape($msgid));
+ if (!defined($processid)) { $fromcid = ''; }
+ my %status=();
+ unless ($skipstatus) {
+ if (ref($status_cache)) {
+ $status{$msgid} = $status_cache->{$msgid};
+ } else {
+ my $suffix=&foldersuffix($folder);
+ %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
+ }
+ if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
+ unless ($status{$msgid}) { $status{$msgid}='new'; }
+ }
return ($sendtime,$shortsubj,$fromname,$fromdomain,$status{$msgid},$fromcid);
}
@@ -366,7 +442,7 @@ sub all_url_author_res_msg {
# ================================================== Critical message to a user
sub user_crit_msg_raw {
- my ($user,$domain,$subject,$message,$sendback,$toperm)=@_;
+ my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
# Check if allowed missing
my $status='';
my $msgid='undefined';
@@ -380,13 +456,9 @@ sub user_crit_msg_raw {
'put:'.$domain.':'.$user.':critical:'.
&Apache::lonnet::escape($msgid).'='.
&Apache::lonnet::escape($message),$homeserver);
- if ($env{'request.course.id'}) {
- &user_normal_msg_raw(
- $env{'course.'.$env{'request.course.id'}.'.num'},
- $env{'course.'.$env{'request.course.id'}.'.domain'},
- 'Critical ['.$user.':'.$domain.']',
- $message);
- }
+ if (defined($sentmessage)) {
+ $$sentmessage = $message;
+ }
} else {
$status='no_host';
}
@@ -424,7 +496,7 @@ sub user_crit_msg_raw {
=cut
sub user_crit_msg {
- my ($user,$domain,$subject,$message,$sendback,$toperm)=@_;
+ my ($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage)=@_;
my $status='';
my %userenv = &Apache::lonnet::get('environment',['msgforward'],
$domain,$user);
@@ -434,10 +506,10 @@ sub user_crit_msg {
my ($forwuser,$forwdomain)=split(/\:/,$_);
$status.=
&user_crit_msg_raw($forwuser,$forwdomain,$subject,$message,
- $sendback,$toperm).' ';
+ $sendback,$toperm,$sentmessage).' ';
}
} else {
- $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm);
+ $status=&user_crit_msg_raw($user,$domain,$subject,$message,$sendback,$toperm,$sentmessage);
}
return $status;
}
@@ -472,33 +544,51 @@ sub user_crit_received {
sub user_normal_msg_raw {
my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
- $toperm)=@_;
+ $toperm,$currid,$newid,$sentmessage,$crsmsgid)=@_;
# Check if allowed missing
- my $status='';
+ my ($status,$packed_message);
my $msgid='undefined';
my $text=$message;
unless (($message)&&($user)&&($domain)) { $status='empty'; };
my $homeserver=&Apache::lonnet::homeserver($user,$domain);
if ($homeserver ne 'no_host') {
- ($msgid,$message)=&packagemsg($subject,$message,$citation,$baseurl,
- $attachmenturl,$user,$domain);
+ ($msgid,$packed_message)=
+ &packagemsg($subject,$message,$citation,$baseurl,
+ $attachmenturl,$user,$domain,$currid,
+ undef,$crsmsgid);
+
# Store in user folder
$status=&Apache::lonnet::critical(
'put:'.$domain.':'.$user.':nohist_email:'.
&Apache::lonnet::escape($msgid).'='.
- &Apache::lonnet::escape($message),$homeserver);
+ &Apache::lonnet::escape($packed_message),$homeserver);
# Save new message received time
&Apache::lonnet::put
('email_status',{'recnewemail'=>time},$domain,$user);
-# Into sent-mail folder
- $status.=' '.&Apache::lonnet::critical(
- 'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
- ':nohist_email_sent:'.
- &Apache::lonnet::escape($msgid).'='.
- &Apache::lonnet::escape($message),$env{'user.home'});
+# Into sent-mail folder unless a broadcast message or critical message
+ unless (($env{'request.course.id'}) &&
+ (($env{'form.sendmode'} eq 'group') ||
+ (($env{'form.critmsg'}) || ($env{'form.sendbck'})) &&
+ (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
+ || &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
+ '/'.$env{'request.course.sec'})))) {
+ (undef, my $packed_message_no_citation)=
+ &packagemsg($subject,$message,undef ,$baseurl,
+ $attachmenturl,$user,$domain,$currid,
+ undef,$crsmsgid);
+
+ $status .= &store_sent_mail($msgid,$packed_message_no_citation);
+ }
} else {
$status='no_host';
}
+ if (defined($newid)) {
+ $$newid = $msgid;
+ }
+ if (defined($sentmessage)) {
+ $$sentmessage = $packed_message;
+ }
+
# Notifications
my %userenv = &Apache::lonnet::get('environment',['notification',
'permanentemail'],
@@ -529,25 +619,36 @@ sub user_normal_msg_raw {
sub user_normal_msg {
my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
- $toperm)=@_;
+ $toperm,$sentmessage)=@_;
my $status='';
my %userenv = &Apache::lonnet::get('environment',['msgforward'],
$domain,$user);
my $msgforward=$userenv{'msgforward'};
if ($msgforward) {
- foreach (split(/\,/,$msgforward)) {
- my ($forwuser,$forwdomain)=split(/\:/,$_);
- $status.=
- &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
- $citation,$baseurl,$attachmenturl,$toperm).' ';
- }
+ foreach (split(/\,/,$msgforward)) {
+ my ($forwuser,$forwdomain)=split(/\:/,$_);
+ $status.=
+ &user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
+ $citation,$baseurl,$attachmenturl,$toperm,
+ undef,undef,$sentmessage).' ';
+ }
} else {
$status=&user_normal_msg_raw($user,$domain,$subject,$message,
- $citation,$baseurl,$attachmenturl,$toperm);
+ $citation,$baseurl,$attachmenturl,$toperm,
+ undef,undef,$sentmessage);
}
return $status;
}
+sub store_sent_mail {
+ my ($msgid,$message) = @_;
+ my $status =' '.&Apache::lonnet::critical(
+ 'put:'.$env{'user.domain'}.':'.$env{'user.name'}.
+ ':nohist_email_sent:'.
+ &Apache::lonnet::escape($msgid).'='.
+ &Apache::lonnet::escape($message),$env{'user.home'});
+ return $status;
+}
# ============================================================ List all folders
@@ -673,7 +774,7 @@ sub discourse {
for (i=0; i= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
my $escid = &Apache::lonnet::unescape($msgid);
@@ -822,6 +928,12 @@ sub sortedmessages {
if ($env{'form.sortedby'} eq "revsubject"){
@temp = sort {lc($b->[1]) cmp lc($a->[1])} @temp;
}
+ if ($env{'form.sortedby'} eq "course"){
+ @temp = sort {lc($a->[6]) cmp lc($b->[6])} @temp;
+ }
+ if ($env{'form.sortedby'} eq "revcourse"){
+ @temp = sort {lc($b->[6]) cmp lc($a->[6])} @temp;
+ }
if ($env{'form.sortedby'} eq "status"){
@temp = sort {$a->[4] cmp $b->[4]} @temp;
}
@@ -831,6 +943,27 @@ sub sortedmessages {
return @temp;
}
+sub get_course_desc {
+ my ($fromcid,$descriptions) = @_;
+ my $description;
+ if (!$fromcid) {
+ return $description;
+ } else {
+ if (defined($$descriptions{$fromcid})) {
+ $description = $$descriptions{$fromcid};
+ } else {
+ if (defined($env{'course.'.$fromcid.'.description'})) {
+ $description = $env{'course.'.$fromcid.'.description'};
+ } else {
+ my %courseinfo=&Apache::lonnet::coursedescription($fromcid); $description = $courseinfo{'description'};
+ $description = $courseinfo{'description'};
+ }
+ $$descriptions{$fromcid} = $description;
+ }
+ return $description;
+ }
+}
+
# ======================================================== Display new messages
@@ -839,15 +972,13 @@ sub disnew {
my %lt=&Apache::lonlocal::texthash(
'nm' => 'New Messages',
'su' => 'Subject',
+ 'co' => 'Course',
'da' => 'Date',
'us' => 'Username',
'op' => 'Open',
'do' => 'Domain'
);
- my @msgids = sort split(/\&/,&Apache::lonnet::reply
- ('keys:'.$env{'user.domain'}.':'.
- $env{'user.name'}.':nohist_email',
- $env{'user.home'}));
+ my @msgids = sort(&Apache::lonnet::getkeys('nohist_email'));
my @newmsgs;
my %setters = ();
my $startblock = 0;
@@ -856,10 +987,15 @@ sub disnew {
my $numblocked = 0;
# Check for blocking of display because of scheduled online exams.
&blockcheck(\%setters,\$startblock,\$endblock);
+ my %status_cache =
+ &Apache::lonnet::get('email_status',\@msgids);
+ my %descriptions;
foreach (@msgids) {
+ my $msgid=&Apache::lonnet::escape($_);
my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)=
- &Apache::lonmsg::unpackmsgid($_);
+ &Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache);
if (defined($sendtime) && $sendtime!~/error/) {
+ my $description = &get_course_desc($fromcid,\%descriptions);
my $numsendtime = $sendtime;
$sendtime = &Apache::lonlocal::locallocaltime($sendtime);
if ($status eq 'new') {
@@ -868,11 +1004,12 @@ sub disnew {
$numblocked ++;
} else {
push @newmsgs, {
- msgid => $_,
+ msgid => $msgid,
sendtime => $sendtime,
shortsub => &Apache::lonnet::unescape($shortsubj),
from => $fromname,
- fromdom => $fromdom
+ fromdom => $fromdom,
+ course => $description
}
}
}
@@ -882,7 +1019,7 @@ sub disnew {
$r->print(<$lt{'nm'}
  |
-$lt{'da'} | $lt{'us'} | $lt{'do'} | $lt{'su'} |
+$lt{'da'} | $lt{'us'} | $lt{'do'} | $lt{'su'} | $lt{'co'} |
TABLEHEAD
foreach my $msg (@newmsgs) {
$r->print(<<"ENDLINK");
@@ -890,12 +1027,12 @@ TABLEHEAD
onMouseOut="javascript:style.backgroundColor='#FFBB77'">
$lt{'op'} |
ENDLINK
- foreach ('sendtime','from','fromdom','shortsub') {
+ foreach ('sendtime','from','fromdom','shortsub','course') {
$r->print("$msg->{$_} | ");
}
$r->print("");
}
- $r->print('
'.&Apache::loncommon::endbodytag().'