--- loncom/interface/lonmsg.pm 2005/06/06 20:31:24 1.147 +++ loncom/interface/lonmsg.pm 2005/11/28 18:32:39 1.157 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Routines for messaging # -# $Id: lonmsg.pm,v 1.147 2005/06/06 20:31:24 www Exp $ +# $Id: lonmsg.pm,v 1.157 2005/11/28 18:32:39 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -114,6 +114,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 +126,7 @@ my $interdis; sub packagemsg { my ($subject,$message,$citation,$baseurl,$attachmenturl, - $recuser,$recdomain)=@_; + $recuser,$recdomain,$msgid)=@_; $message =&HTML::Entities::encode($message,'<>&"'); $citation=&HTML::Entities::encode($citation,'<>&"'); $subject =&HTML::Entities::encode($subject,'<>&"'); @@ -139,10 +141,12 @@ sub packagemsg { $msgcount++; my $partsubj=$subject; $partsubj=&Apache::lonnet::escape($partsubj); - my $msgid=&Apache::lonnet::escape( + unless(defined($msgid)) { + $msgid=&Apache::lonnet::escape( $now.':'.$partsubj.':'.$env{'user.name'}.':'. $env{'user.domain'}.':'.$msgcount.':'. $env{'request.course.id'}.':'.$$); + } my $result=''.$env{'user.name'}.''. ''.$env{'user.domain'}.''. ''.$subject.''. @@ -159,10 +163,17 @@ sub packagemsg { ''.$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++) { + $result .= ''.$$recuser[$i].''. + ''.$$recdomain[$i].''; + } + } else { + $result .= ''.$recuser.''. + ''.$recdomain.''; + } + $result .= ''.$message.''; if (defined($citation)) { $result.=''.$citation.''; } @@ -186,7 +197,11 @@ 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); + } else { + $content{$entry}=$value; + } } } if ($content{'attachmenturl'}) { @@ -337,7 +352,20 @@ sub del_url_author_res_msg { } return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author); } +# =================================== Clear out all author messages in URL path +sub clear_author_res_msg { + my $url=shift; + $url=&Apache::lonnet::declutter($url); + my ($domain,$author)=($url=~/^(\w+)\/(\w+)\//); + my @delmsgs=(); + foreach (&Apache::lonnet::getkeys('nohist_res_msgs',$domain,$author)) { + if ($_=~/^\Q$url\E/) { + push (@delmsgs,$_); + } + } + return &Apache::lonnet::del('nohist_res_msgs',\@delmsgs,$domain,$author); +} # ================= Return hash with URLs for which there is a resource message sub all_url_author_res_msg { @@ -459,7 +487,7 @@ sub user_crit_received { sub user_normal_msg_raw { my ($user,$domain,$subject,$message,$citation,$baseurl,$attachmenturl, - $toperm)=@_; + $toperm,$newid)=@_; # Check if allowed missing my $status=''; my $msgid='undefined'; @@ -477,15 +505,16 @@ sub user_normal_msg_raw { # 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 + unless (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { + $status .= &store_sent_mail($msgid,$message); + } } else { $status='no_host'; } + if (defined($newid)) { + $$newid = $msgid; + } # Notifications my %userenv = &Apache::lonnet::get('environment',['notification', 'permanentemail'], @@ -535,6 +564,15 @@ sub user_normal_msg { 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 @@ -564,13 +602,14 @@ sub scrollbuttons { my ($start,$maxdis,$first,$finish,$total)=@_; unless ($total>0) { return ''; } $start++; $maxdis++;$first++;$finish++; - return + return + &mt('Page').': '. ''. ''. ' of '.$maxdis. ''. '
'. - &mt('Messages [_1] through [_2] of [_3]',$first,$finish,$total).''; + &mt('Showing messages [_1] through [_2] of [_3]',$first,$finish,$total).''; } # =============================================================== Folder suffix @@ -596,6 +635,9 @@ sub statuschange { 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 @@ -768,8 +810,9 @@ sub sortedmessages { my $msgid=&Apache::lonnet::escape($_); my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid)= &Apache::lonmsg::unpackmsgid($msgid,$folder); + my $description = &get_course_desc($fromcid); my @temp1 = ($sendtime,$shortsubj,$fromname,$fromdomain,$status, - $msgid); + $msgid,$description); # Check whether message was sent during blocking period. if ($sendtime >= $startblock && ($sendtime <= $endblock && $endblock > 0) ) { my $escid = &Apache::lonnet::unescape($msgid); @@ -805,6 +848,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; } @@ -814,6 +863,18 @@ sub sortedmessages { return @temp; } +sub get_course_desc { + my ($fromcid) = @_; + my $description; + if (defined($env{'course.'.$fromcid.'.description'})) { + $description = $env{'course.'.$fromcid.'.description'}; + } else { + my %courseinfo=&Apache::lonnet::coursedescription($fromcid); + $description = $courseinfo{'description'}; + } + return $description; +} + # ======================================================== Display new messages @@ -822,6 +883,7 @@ sub disnew { my %lt=&Apache::lonlocal::texthash( 'nm' => 'New Messages', 'su' => 'Subject', + 'co' => 'Course', 'da' => 'Date', 'us' => 'Username', 'op' => 'Open', @@ -843,6 +905,7 @@ sub disnew { my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)= &Apache::lonmsg::unpackmsgid($_); if (defined($sendtime) && $sendtime!~/error/) { + my $description = &get_course_desc($fromcid); my $numsendtime = $sendtime; $sendtime = &Apache::lonlocal::locallocaltime($sendtime); if ($status eq 'new') { @@ -855,7 +918,8 @@ sub disnew { sendtime => $sendtime, shortsub => &Apache::lonnet::unescape($shortsubj), from => $fromname, - fromdom => $fromdom + fromdom => $fromdom, + course => $description } } } @@ -865,14 +929,15 @@ sub disnew { $r->print(<$lt{'nm'} - + TABLEHEAD foreach my $msg (@newmsgs) { $r->print(<<"ENDLINK"); - + ENDLINK - foreach ('sendtime','from','fromdom','shortsub') { + foreach ('sendtime','from','fromdom','shortsub','course') { $r->print(""); } $r->print(""); @@ -986,6 +1051,12 @@ ENDDISHEADER $r->print(''.&mt('Subject').''); } $r->print(''); } else { @@ -993,16 +1064,16 @@ ENDDISHEADER } $r->print("\n"); for (my $n=$firstdis;$n<=$lastdis;$n++) { - my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID)= @{$temp[$n]}; + my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$origID,$description)= @{$temp[$n]}; if (($status ne 'deleted') && defined($sendtime) && $sendtime!~/error/) { if ($status eq 'new') { - $r->print(''); + $r->print(''); } elsif ($status eq 'read') { - $r->print(''); + $r->print(''); } elsif ($status eq 'replied') { - $r->print(''); + $r->print(''); } else { - $r->print(''); + $r->print(''); } $r->print('\n"); + $description.''."\n"); } elsif ($status eq 'deleted') { # purge &movemsg(&Apache::lonnet::unescape($origID),$folder,'trash'); @@ -1095,8 +1166,9 @@ sub compout { '' . $crithelp . '

'; - } +&mt('Send copy to permanent email address (if known)').'

'. +'

'; } my %message; my %content; my $defdom=$env{'user.domain'}; @@ -1215,6 +1287,7 @@ ENDUPLOAD &discourse; } $r->print(''. + &Apache::lonfeedback::generate_preview_button('compemail','message'). &Apache::lonhtmlcommon::htmlareaselectactive('message')); } @@ -1777,7 +1850,7 @@ sub displaymessage { ''. ''. + '">'.&mt('Delete').''. ''); @@ -1792,20 +1865,28 @@ sub displaymessage { $r->print('
 $lt{'da'}$lt{'us'}$lt{'do'}$lt{'su'}
$lt{'da'}$lt{'us'}$lt{'do'}$lt{'su'}$lt{'co'}
$lt{'op'}$msg->{$_}
'); + if ($env{'form.sortedby'} eq "revcourse") { + $r->print(''.&mt('Course').''); + } else { + $r->print(''.&mt('Course').''); + } + $r->print(''); if ($env{'form.sortedby'} eq "revstatus") { $r->print(''.&mt('Status').'
'.&mt('Open').''. @@ -1011,7 +1082,7 @@ ENDDISHEADER ''.&Apache::lonlocal::locallocaltime($sendtime).''. $fromname.''.$fromdomain.''. &Apache::lonnet::unescape($shortsubj).''. - $status."
'.$status.'
'.&mt('Mark Unread').'Delete'.&mt('Back to Folder Display').'
'); if ($env{'user.adv'}) { $r->print(''); - + my $symb=&Apache::lonnet::symbread($content{'baseurl'}); if (&Apache::lonnet::allowed('vgr',$env{'request.course.id'})) { $r->print(''); } - if (&Apache::lonnet::allowed('opa',$env{'request.course.id'})) { - my $symb=&Apache::lonnet::symbread($content{'baseurl'}); + if (&Apache::lonnet::allowed('opa',$env{'request.course.id'}) && $symb) { $r->print(''); } - if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'})) { - my $symb=&Apache::lonnet::symbread($content{'baseurl'}); + if (&Apache::lonnet::allowed('mgr',$env{'request.course.id'}) && $symb) { $r->print(''); } $r->print('
'.&mt('Currently available actions (will open extra window)').':'.&Apache::loncommon::track_student_link(&mt('View recent activity'),$content{'sendername'},$content{'senderdomain'},'check').''.&Apache::loncommon::pprmlink(&mt('Set/Change parameters'),$content{'sendername'},$content{'senderdomain'},$symb,'check').''.&Apache::loncommon::pgrdlink(&mt('Set/Change grades'),$content{'sendername'},$content{'senderdomain'},$symb,'check').'
'); } + my $tolist; + my @recipients = (); + for (my $i=0; $i<@{$content{'recuser'}}; $i++) { + $recipients[$i] = &Apache::loncommon::aboutmewrapper( + &Apache::loncommon::plainname($content{'recuser'}[$i], + $content{'recdomain'}[$i]), + $content{'recuser'}[$i],$content{'recdomain'}[$i]). + ' ('.$content{'recuser'}[$i].' at '.$content{'recdomain'}[$i].') '; + } + $tolist = join(', ',@recipients); $r->print('
'.&mt('Subject').': '.$content{'subject'}. ($folder ne 'sent'?'
'.&mt('From').': '. &Apache::loncommon::aboutmewrapper( @@ -1813,11 +1894,7 @@ sub displaymessage { $content{'sendername'},$content{'senderdomain'}).' ('. $content{'sendername'}.' at '. $content{'senderdomain'}.') ':'
'.&mt('To').': '. - &Apache::loncommon::aboutmewrapper( - &Apache::loncommon::plainname($content{'recuser'},$content{'recdomain'}), - $content{'recuser'},$content{'recdomain'}).' ('. - $content{'recuser'}.' at '. - $content{'recdomain'}.') '). + $tolist). ($content{'courseid'}?'
'.&mt('Course').': '.$courseinfo{'description'}. ($content{'coursesec'}?' ('.&mt('Group/Section').': '.$content{'coursesec'}.')':''):''). '
'.&mt('Time').': '.$content{'time'}. @@ -1932,6 +2009,8 @@ sub sendoffmail { my ($r,$folder)=@_; my $suffix=&foldersuffix($folder); my $sendstatus=''; + my %broadcast_status; + my $numbroadcast = 0; if ($env{'form.send'}) { &printheader($r,'','Messages being sent.'); $r->rflush(); @@ -1976,18 +2055,22 @@ sub sendoffmail { $toaddr{$auname.':'.$audom}=''; } } + + my $basicmsg; + my $msgtype; + if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && + (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { + $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); + $msgtype = '(critical)'; + } else { + $basicmsg=&Apache::lonfeedback::clear_out_html($env{'form.message'}); + } foreach (keys %toaddr) { my ($recuname,$recdomain)=split(/\:/,$_); - my $msgtxt; - if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && - (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { - $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'},1); - } else { - $msgtxt=&Apache::lonfeedback::clear_out_html($env{'form.message'}); - } + my $msgtxt = $basicmsg; if ($toaddr{$_}) { $msgtxt.='
'.$toaddr{$_}; } - my $thismsg; + my $thismsg; if ((($env{'form.critmsg'}) || ($env{'form.sendbck'})) && (&Apache::lonnet::allowed('srm',$env{'request.course.id'}))) { $r->print(&mt('Sending critical message').' '.$recuname.'@'.$recdomain.': '); @@ -2001,17 +2084,52 @@ sub sendoffmail { &Apache::lonfeedback::clear_out_html($env{'form.subject'}), $msgtxt, $content{'citation'},undef,undef,$env{'form.permanent'}); - if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { - &user_normal_msg_raw( - $env{'course.'.$env{'request.course.id'}.'.num'}, - $env{'course.'.$env{'request.course.id'}.'.domain'}, - 'Broadcast ['.$recuname.':'.$recdomain.']', - $msgtxt); - } + } + if (($env{'request.course.id'}) && + ($env{'form.sendmode'} eq 'group')) { + $broadcast_status{$recuname.':'.$recdomain} = $thismsg; + if ($thismsg eq 'ok') { + $numbroadcast ++; + } } $r->print($thismsg.'
'); $sendstatus.=' '.$thismsg; } + if (($env{'request.course.id'}) && ($env{'form.sendmode'} eq 'group')) { + my $subj_prefix; + if ($msgtype eq 'critical') { + $subj_prefix = 'Critical broadcast'; + } else { + $subj_prefix = 'Broadcast'; + } + my ($broadmsgid,$broadresult); + if ($numbroadcast) { + $broadresult = &user_normal_msg_raw( + $env{'course.'.$env{'request.course.id'}.'.num'}, + $env{'course.'.$env{'request.course.id'}.'.domain'}, $subj_prefix.' to: '.$env{'course.'.$env{'request.course.id'}.'.description'}. + ' ('.$numbroadcast.' sent)',$basicmsg,undef,undef,undef, + undef,\$broadmsgid); + } + if ($broadresult eq 'ok') { + my $record_sent; + my @recusers = (); + my @recudoms = (); + foreach my $recipient (sort(keys(%toaddr))) { + if ($broadcast_status{$recipient} eq 'ok') { + my ($uname,$udom) = split/:/,$recipient; + push(@recusers,$uname); + push(@recudoms,$udom); + } + } + if (@recusers) { + my $broadmessage; + ($broadmsgid,$broadmessage)=&packagemsg(&Apache::lonfeedback::clear_out_html($env{'form.subject'}),$basicmsg,undef,undef,undef,\@recusers,\@recudoms,$broadmsgid); + $record_sent = &store_sent_mail($broadmsgid,$broadmessage); + } + } else { + &Apache::lonnet::logthis('Failed to create record of broadcast in '.$env{'course.'.$env{'request.course.id'}.'.num'}.' at '.$env{'course.'.$env{'request.course.id'}.'.domain'}.' - no msgid generated'); + } + } } else { &printheader($r,'','No messages sent.'); } @@ -2177,6 +2295,13 @@ sub handler { if ($env{'form.storebasecomment'}) { &storecomment($r); } + if (($env{'form.rsspost'}) && ($env{'request.course.id'})) { + &Apache::lonrss::addentry($env{'course.'.$env{'request.course.id'}.'.num'}, + $env{'course.'.$env{'request.course.id'}.'.domain'}, + 'Course_Announcements', + $env{'form.subject'}, + $env{'form.message'},'/adm/communicate','public'); + } &disall($r,($folder?$folder:$dismode)); } elsif ($env{'form.newfolder'}) { &printheader($r,'','New Folder');