--- loncom/interface/lonmsg.pm 2006/01/10 02:11:17 1.171
+++ loncom/interface/lonmsg.pm 2006/04/11 14:17:23 1.181
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Routines for messaging
#
-# $Id: lonmsg.pm,v 1.171 2006/01/10 02:11:17 banghart Exp $
+# $Id: lonmsg.pm,v 1.181 2006/04/11 14:17:23 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -26,118 +26,27 @@
# http://www.lon-capa.org/
#
-
package Apache::lonmsg;
-=pod
-
-=head1 NAME
-
-Apache::lonmsg: supports internal messaging
-
-=head1 SYNOPSIS
-
-lonmsg provides routines for sending messages, receiving messages, and
-a handler to allow users to read, send, and delete messages.
-
-=head1 OVERVIEW
-
-=head2 Messaging Overview
-
-XLON-CAPA provides an internal messaging system similar to
-email, but customized for LON-CAPA's usage. LON-CAPA implements its
-own messaging system, rather then building on top of email, because of
-the features LON-CAPA messages can offer that conventional e-mail can
-not:
-
-=over 4
-
-=item * B: A message the recipient B
-acknowlegde receipt of before they are allowed to continue using the
-system, preventing a user from claiming they never got a message
-
-=item * B: LON-CAPA can reliably send reciepts informing the
-sender that it has been read; again, useful for preventing students
-from claiming they did not see a message. (While conventional e-mail
-has some reciept support, it's sporadic, e-mail client-specific, and
-generally the receiver can opt to not send one, making it useless in
-this case.)
-
-=item * B: LON-CAPA knows about the sender, such as where
-they are in a course. When a student mails an instructor asking for
-help on the problem, the instructor receives not just the student's
-question, but all submissions the student has made up to that point,
-the user's rendering of the problem, and the complete view the student
-saw of the resource, including discussion up to that point. Finally,
-the instructor is reading all of this inside of LON-CAPA, not their
-email program, so they have full access to LON-CAPA's grading
-interface, or other features they may wish to use in response to the
-student's query.
-
-=item * B: LON-CAPA can block display of e-mails that are
-sent to a student during an online exam. A course coordinator or
-instructor can set an open and close date/time for scheduled online
-exams in a course. If a user uses the LON-CAPA internal messaging
-system to display e-mails during the scheduled blocking event,
-display of all e-mail sent during the blocking period will be
-suppressed, and a message of explanation, including details of the
-currently active blocking periods will be displayed instead. A user
-who has a course coordinator or instructor role in a course will be
-unaffected by any blocking periods for the course, unless the user
-also has a student role in the course, AND has selected the student role.
-
-=back
-
-Users can ask LON-CAPA to forward messages to conventional e-mail
-addresses on their B screen, but generally, LON-CAPA messages
-are much more useful than traditional email can be made to be, even
-with HTML support.
-
-Right now, this document will cover just how to send a message, since
-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
-
-=cut
-
use strict;
use Apache::lonnet;
-use vars qw($msgcount);
use HTML::TokeParser();
-use Apache::Constants qw(:common);
-use Apache::loncommon();
-use Apache::lontexconvert();
-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;
-my $startdis;
-my $interdis;
+use Mail::Send;
+
+{
+ my $uniq;
+ sub get_uniq {
+ $uniq++;
+ return $uniq;
+ }
+}
# ===================================================================== Package
sub packagemsg {
my ($subject,$message,$citation,$baseurl,$attachmenturl,
- $recuser,$recdomain,$msgid,$type)=@_;
+ $recuser,$recdomain,$msgid,$type,$crsmsgid)=@_;
$message =&HTML::Entities::encode($message,'<>&"');
$citation=&HTML::Entities::encode($citation,'<>&"');
$subject =&HTML::Entities::encode($subject,'<>&"');
@@ -165,16 +74,24 @@ sub packagemsg {
$course_context = $env{'request.course.id'};
}
my $now=time;
- $msgcount++;
+ my $msgcount = &get_uniq();
unless(defined($msgid)) {
$msgid = &buildmsgid($now,$subject,$env{'user.name'},$env{'user.domain'},
$msgcount,$course_context,$$);
}
- my $result=''.$env{'user.name'}.''.
+ 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'}.''.
@@ -296,8 +213,8 @@ sub sendemail {
$msg->to($to);
$msg->subject('[LON-CAPA] '.$subject);
if (my $fh = $msg->open()) {
- print $fh $body;
- $fh->close;
+ print $fh $body;
+ $fh->close;
}
}
@@ -307,7 +224,7 @@ sub sendnotification {
my ($to,$touname,$toudom,$subj,$crit,$text)=@_;
my $sender=$env{'environment.firstname'}.' '.$env{'environment.lastname'};
unless ($sender=~/\w/) {
- $sender=$env{'user.name'}.'@'.$env{'user.domain'};
+ $sender=$env{'user.name'}.'@'.$env{'user.domain'};
}
my $critical=($crit?' critical':'');
$text=~s/\<\;/\This error occurred on machine ".
+ $Apache::lonnet::perlvar{'lonHostID'}."
";
my $msgid;
($msgid,$message)=&packagemsg($filename,$message);
return &Apache::lonnet::reply('put:'.$domain.':'.$author.
@@ -536,21 +455,24 @@ sub user_crit_received {
sub user_normal_msg_raw {
my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl,
- $toperm,$currid,$newid,$sentmessage)=@_;
+ $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,$currid);
+ ($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);
@@ -561,7 +483,12 @@ sub user_normal_msg_raw {
(&Apache::lonnet::allowed('srm',$env{'request.course.id'})
|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
'/'.$env{'request.course.sec'})))) {
- $status .= &store_sent_mail($msgid,$message);
+ (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';
@@ -570,7 +497,7 @@ sub user_normal_msg_raw {
$$newid = $msgid;
}
if (defined($sentmessage)) {
- $$sentmessage = $message;
+ $$sentmessage = $packed_message;
}
# Notifications
@@ -582,8 +509,8 @@ sub user_normal_msg_raw {
$text);
}
if ($toperm && $userenv{'permanentemail'}) {
- &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
- $text);
+ &sendnotification($userenv{'permanentemail'},$user,$domain,$subject,0,
+ $text);
}
&Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
$env{'user.home'},
@@ -610,14 +537,16 @@ sub user_normal_msg {
my $msgforward=$userenv{'msgforward'};
if ($msgforward) {
foreach (split(/\,/,$msgforward)) {
- my ($forwuser,$forwdomain)=split(/\:/,$_);
- $status.=
+ my ($forwuser,$forwdomain)=split(/\:/,$_);
+ $status.=
&user_normal_msg_raw($forwuser,$forwdomain,$subject,$message,
- $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage).' ';
+ $citation,$baseurl,$attachmenturl,$toperm,
+ undef,undef,$sentmessage).' ';
}
} else {
- $status=&user_normal_msg_raw($user,$domain,$subject,$message,
- $citation,$baseurl,$attachmenturl,$toperm,undef,undef,$sentmessage);
+ $status=&user_normal_msg_raw($user,$domain,$subject,$message,
+ $citation,$baseurl,$attachmenturl,$toperm,
+ undef,undef,$sentmessage);
}
return $status;
}
@@ -632,44 +561,6 @@ sub store_sent_mail {
return $status;
}
-# ============================================================ List all folders
-
-sub folderlist {
- my $folder=shift;
- my @allfolders=&Apache::lonnet::getkeys('email_folders');
- if ($allfolders[0]=~/^error:/) { @allfolders=(); }
- return '':'');
-}
-
-sub scrollbuttons {
- my ($start,$maxdis,$first,$finish,$total)=@_;
- unless ($total>0) { return ''; }
- $start++; $maxdis++;$first++;$finish++;
- return
- &mt('Page').': '.
- ''.
- ''.
- ' of '.$maxdis.
- ''.
- ' '.
- &mt('Showing messages [_1] through [_2] of [_3]',$first,$finish,$total).'';
-}
-
# =============================================================== Folder suffix
sub foldersuffix {
@@ -678,1785 +569,6 @@ sub foldersuffix {
return '_'.&Apache::lonnet::escape($folder);
}
-# =============================================================== Status Change
-
-sub statuschange {
- my ($msgid,$newstatus,$folder)=@_;
- my $suffix=&foldersuffix($folder);
- my %status=&Apache::lonnet::get('email_status'.$suffix,[$msgid]);
- if ($status{$msgid}=~/^error\:/) { $status{$msgid}=''; }
- unless ($status{$msgid}) { $status{$msgid}='new'; }
- unless (($status{$msgid} eq 'replied') ||
- ($status{$msgid} eq 'forwarded')) {
- &Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
- }
- if (($newstatus eq 'deleted') || ($newstatus eq 'new')) {
- &Apache::lonnet::put('email_status'.$suffix,{$msgid => $newstatus});
- }
- if ($newstatus eq 'deleted') {
- &movemsg(&Apache::lonnet::unescape($msgid),$folder,'trash');
- }
-}
-
-# ============================================================= Make new folder
-
-sub makefolder {
- my ($newfolder)=@_;
- if (($newfolder eq 'sent')
- || ($newfolder eq 'critical')
- || ($newfolder eq 'trash')
- || ($newfolder eq 'new')) { return; }
- &Apache::lonnet::put('email_folders',{$newfolder => time});
-}
-
-# ======================================================== Move between folders
-
-sub movemsg {
- my ($msgid,$srcfolder,$trgfolder)=@_;
- if ($srcfolder eq 'new') { $srcfolder=''; }
- my $srcsuffix=&foldersuffix($srcfolder);
- my $trgsuffix=&foldersuffix($trgfolder);
-
-# Copy message
- my %message=&Apache::lonnet::get('nohist_email'.$srcsuffix,[$msgid]);
- &Apache::lonnet::put('nohist_email'.$trgsuffix,{$msgid => $message{$msgid}});
-
-# Copy status
- unless ($trgfolder eq 'trash') {
- my %status=&Apache::lonnet::get('email_status'.$srcsuffix,[$msgid]);
- &Apache::lonnet::put('email_status'.$trgsuffix,{$msgid => $status{$msgid}});
- }
-# Delete orginals
- &Apache::lonnet::del('nohist_email'.$srcsuffix,[$msgid]);
- &Apache::lonnet::del('email_status'.$srcsuffix,[$msgid]);
-}
-
-# ======================================================= Display a course list
-
-sub discourse {
- my $r=shift;
- my $classlist = &Apache::loncoursedata::get_classlist();
- my $now=time;
- my %lt=&Apache::lonlocal::texthash('cfa' => 'Check All',
- 'cfs' => 'Check Section/Group',
- 'cfn' => 'Uncheck All');
- $r->print(<
-
-
-
-
-
-
-ENDDISHEADER
- my %coursepersonnel=&Apache::lonnet::get_course_adv_roles();
- $r->print('
');
- foreach my $role (sort keys %coursepersonnel) {
- foreach (split(/\,/,$coursepersonnel{$role})) {
- my ($puname,$pudom)=split(/\:/,$_);
- $r->print('
'.
- '
('.$_.'),
'.$role.'
');
- }
- }
- $r->print('
');
- my $sort = sub {
- my $aname=lc($classlist->{$a}[&Apache::loncoursedata::CL_FULLNAME()]);
- if (!$aname) { $aname=$a; }
- my $bname=lc($classlist->{$b}[&Apache::loncoursedata::CL_FULLNAME()]);
- if (!$bname) { $bname=$b; }
- return $aname cmp $bname;
- };
- foreach my $student (sort $sort (keys(%{$classlist}))) {
- my $info=$classlist->{$student};
- my ($sname,$sdom,$status,$fullname,$section) =
- (@{$info}[&Apache::loncoursedata::CL_SNAME(),
- &Apache::loncoursedata::CL_SDOM(),
- &Apache::loncoursedata::CL_STATUS(),
- &Apache::loncoursedata::CL_FULLNAME(),
- &Apache::loncoursedata::CL_SECTION()]);
- next if ($status ne 'Active');
- next if ($env{'request.course.sec'} &&
- $section ne $env{'request.course.sec'});
- my $key = 'send_to_&&&'.$section.'&&&_'.$student;
- if (! defined($fullname) || $fullname eq '') { $fullname = $sname; }
- $r->print('
'.$sname.'@'.$sdom.'
'.$section.
- '
');
- }
- $r->print('
');
-}
-
-# ==================================================== Display Critical Message
-
-sub discrit {
- my $r=shift;
- my $header = '
'.&mt('Critical Messages').'
'.
- '');
-}
-
-sub sortedmessages {
- my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
- my $suffix=&foldersuffix($folder);
- my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
- #unpack the varibles and repack into temp for sorting
- my @temp;
- my %descriptions;
- my %status_cache =
- &Apache::lonnet::get('email_status'.&foldersuffix($folder),\@messages);
- foreach (@messages) {
- my $msgid=&Apache::lonnet::escape($_);
- my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)=
- &Apache::lonmsg::unpackmsgid($msgid,$folder,undef,
- \%status_cache);
- my $description = &get_course_desc($fromcid,\%descriptions);
- my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status,
- $msgid,$description);
- # Check whether message was sent during blocking period.
- if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) {
- my $escid = &Apache::lonnet::unescape($msgid);
- $$blocked{$escid} = 'ON';
- $$numblocked ++;
- } else {
- push @temp ,\@temp1;
- }
- }
- #default sort
- @temp = sort {$a->[0] <=> $b->[0]} @temp;
- if ($env{'form.sortedby'} eq "date"){
- @temp = sort {$a->[0] <=> $b->[0]} @temp;
- }
- if ($env{'form.sortedby'} eq "revdate"){
- @temp = sort {$b->[0] <=> $a->[0]} @temp;
- }
- if ($env{'form.sortedby'} eq "user"){
- @temp = sort {lc($a->[2]) cmp lc($b->[2])} @temp;
- }
- if ($env{'form.sortedby'} eq "revuser"){
- @temp = sort {lc($b->[2]) cmp lc($a->[2])} @temp;
- }
- if ($env{'form.sortedby'} eq "domain"){
- @temp = sort {$a->[3] cmp $b->[3]} @temp;
- }
- if ($env{'form.sortedby'} eq "revdomain"){
- @temp = sort {$b->[3] cmp $a->[3]} @temp;
- }
- if ($env{'form.sortedby'} eq "subject"){
- @temp = sort {lc($a->[1]) cmp lc($b->[1])} @temp;
- }
- 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;
- }
- if ($env{'form.sortedby'} eq "revstatus"){
- @temp = sort {$b->[4] cmp $a->[4]} @temp;
- }
- 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
-
-
-sub disnew {
- my $r=shift;
- my %lt=&Apache::lonlocal::texthash(
- 'nm' => 'New Messages',
- 'su' => 'Subject',
- 'co' => 'Course',
- 'da' => 'Date',
- 'us' => 'Username',
- 'op' => 'Open',
- 'do' => 'Domain'
- );
- my @msgids = sort(&Apache::lonnet::getkeys('nohist_email'));
- my @newmsgs;
- my %setters = ();
- my $startblock = 0;
- my $endblock = 0;
- my %blocked = ();
- 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($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') {
- if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
- $blocked{$_} = 'ON';
- $numblocked ++;
- } else {
- push @newmsgs, {
- msgid => $msgid,
- sendtime => $sendtime,
- shortsub => &Apache::lonnet::unescape($shortsubj),
- from => $fromname,
- fromdom => $fromdom,
- course => $description
- }
- }
- }
- }
- }
- if ($#newmsgs >= 0) {
- $r->print(<$lt{'nm'}
-
 
-
$lt{'da'}
$lt{'us'}
$lt{'do'}
$lt{'su'}
$lt{'co'}
-TABLEHEAD
- foreach my $msg (@newmsgs) {
- $r->print(<<"ENDLINK");
-