--- loncom/interface/lonmsgdisplay.pm 2006/10/04 20:26:48 1.41
+++ loncom/interface/lonmsgdisplay.pm 2006/12/07 21:06:57 1.48
@@ -1,7 +1,7 @@
# The LearningOnline Network with CAPA
# Routines for messaging display
#
-# $Id: lonmsgdisplay.pm,v 1.41 2006/10/04 20:26:48 albertel Exp $
+# $Id: lonmsgdisplay.pm,v 1.48 2006/12/07 21:06:57 albertel Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -138,24 +138,134 @@ my $interdis;
sub folderlist {
my $folder=shift;
- my @allfolders=&Apache::lonnet::getkeys('email_folders');
- if ($allfolders[0]=~/^error:/) { @allfolders=(); }
- return '
':'');
+ ''.
+ ($folder=~/^(new|critical)/?'':'');
+ return $output;
+}
+
+sub get_permanent_folders {
+ my %permfolders =
+ &Apache::lonlocal::texthash('' => 'INBOX',
+ 'trash' => 'TRASH',
+ 'new' => 'New Messages Only',
+ 'critical' => 'Critical',
+ 'sent' => 'Sent Messages',
+ );
+ return %permfolders;
}
sub scrollbuttons {
@@ -195,14 +305,123 @@ sub statuschange {
# ============================================================= 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});
+ my ($newfolder) = @_;
+ my %permfolders = &get_permanent_folders();
+ my %userfolders = &Apache::lonmsg::get_user_folders();
+ my ($outcome,$warning);
+ if (defined($userfolders{$newfolder})) {
+ return &mt('The folder name: "[_1]" is already in use for an existing folder.',$newfolder);
+ }
+ foreach my $perm (keys(%permfolders)) {
+ if ($permfolders{$perm} eq $newfolder) {
+ return &mt('The folder name: "[_1]" is already used for one of the folders automatically generated by the system.',$newfolder);
+ }
+ }
+ if (&get_msgfolder_lock() eq 'ok') {
+ my %counter_hash = &Apache::lonnet::get('email_folders',["\0".'idcount']);
+ my $lastcount = $counter_hash{"\0".'idcount'};
+ my $folder_id = $lastcount + 1;
+ while (defined($userfolders{$folder_id})) {
+ $folder_id ++;
+ }
+ my %folderinfo = ( id => $folder_id,
+ created => time, );
+ $outcome =
+ &Apache::lonnet::put('email_folders',{$newfolder => \%folderinfo,
+ "\0".'idcount' => $folder_id});
+ my $releaseresult = &release_msgfolder_lock();
+ if ($releaseresult ne 'ok') {
+ $warning = $releaseresult;
+ }
+ } else {
+ $outcome =
+ &mt('Error - could not obtain lock on email folders record.');
+ }
+ return ($outcome,$warning);
+}
+
+# ============================================================= Delete folder
+
+sub deletefolder {
+ my ($folder)=@_;
+ my %permfolders = &get_permanent_folders();
+ if (defined($permfolders{$folder})) {
+ return &mt('The folder [_1] may not be deleted',$folder);
+ }
+ my %userfolders = &Apache::lonmsg::get_user_folders();
+ if (!defined($userfolders{$folder})) {
+ return &mt('The folder [_1] does not exist so deletion is not required.',
+ $folder);
+ }
+ # check folder is empty;
+ my $suffix=&Apache::lonmsg::foldersuffix($folder);
+ my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
+ if (@messages > 0) {
+ return &mt('The folder [_1] contains messages so it may not be deleted.').
+ ' '.
+ &mt('Delete or move the messages to a different folder first.');
+ }
+ my $delresult = &Apache::lonnet::del('email_folders',[$folder]);
+ return $delresult;
+}
+
+sub renamefolder {
+ my ($folder) = @_;
+ my $newname = $env{'form.renamed'};
+ my %permfolders = &get_permanent_folders();
+ if ($env{'form.renamed'} eq '') {
+ return &mt('The folder "[_1]" may not be renamed to "[_2]" as the new name you requested is an invalid name.',$folder,$newname);
+ }
+ if (defined($permfolders{$newname})) {
+ return &mt('The folder "[_1]" may not be renamed to "[_2]" as the new name you requested is reserved for folders provided automatically by the system.',$folder,$newname);
+ }
+ my %userfolders = &Apache::lonmsg::get_user_folders();
+ if (defined($userfolders{$newname})) {
+ return &mt('The folder "[_1]" may not be renamed to "[_2]" because the new name you requested is already being used for an existing folder.',$folder,$newname);
+ }
+ if (!defined($userfolders{$folder})) {
+ return &mt('The folder "[_1]" could not be renamed to "[_2]" because the folder does not exist.',$folder,$newname);
+ }
+ my %folderinfo;
+ if (ref($userfolders{$folder}) eq 'HASH') {
+ %folderinfo = %{$userfolders{$folder}};
+ } else {
+ %folderinfo = ( id => $folder,
+ created => $userfolders{$folder},);
+ }
+ my $outcome =
+ &Apache::lonnet::put('email_folders',{$newname => \%folderinfo});
+ if ($outcome eq 'ok') {
+ $outcome = &Apache::lonnet::del('email_folders',[$folder]);
+ }
+ return $outcome;
}
+sub get_msgfolder_lock {
+ # get lock for mail folder counter.
+ my $lockhash = { "\0".'lock_counter' => time, };
+ my $tries = 0;
+ my $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
+ while (($gotlock ne 'ok') && $tries <3) {
+ $tries ++;
+ sleep(1);
+ $gotlock = &Apache::lonnet::newput('email_folders',$lockhash);
+ }
+ return $gotlock;
+}
+
+sub release_msgfolder_lock {
+ # remove lock
+ my @del_lock = ("\0".'lock_counter');
+ my $dellockoutcome=&Apache::lonnet::del('email_folders',\@del_lock);
+ if ($dellockoutcome ne 'ok') {
+ return (' '.&mt('Warning: failed to release lock for counter').' ');
+ } else {
+ return 'ok';
+ }
+}
+
+
# ======================================================== Move between folders
sub movemsg {
@@ -486,8 +705,8 @@ sub discrit {
'';
my %what=&Apache::lonnet::dump('critical');
my $result = '';
- foreach (sort keys %what) {
- my %content=&Apache::lonmsg::unpackagemsg($what{$_});
+ foreach my $key (sort(keys(%what))) {
+ my %content=&Apache::lonmsg::unpackagemsg($what{$key});
next if ($content{'senderdomain'} eq '');
$result.=''.&mt('From').': '.
&Apache::loncommon::aboutmewrapper(
@@ -500,8 +719,8 @@ $content{'sendername'}.':'.
''.
&mt('You have to confirm that you received this message. After confirmation, this message will be moved to your regular inbox').
' '.
- ''.
- ''.
+ '';
}
# Check to see if there were any messages.
@@ -520,6 +739,7 @@ sub sortedmessages {
my ($blocked,$startblock,$endblock,$numblocked,$folder) = @_;
my $suffix=&Apache::lonmsg::foldersuffix($folder);
my @messages = &Apache::lonnet::getkeys('nohist_email'.$suffix);
+
#unpack the varibles and repack into temp for sorting
my @temp;
my %descriptions;
@@ -623,7 +843,7 @@ sub get_course_desc {
if (defined($env{'course.'.$fromcid.'.description'})) {
$description = $env{'course.'.$fromcid.'.description'};
} else {
- my %courseinfo=&Apache::lonnet::coursedescription($fromcid); $description = $courseinfo{'description'};
+ my %courseinfo=&Apache::lonnet::coursedescription($fromcid);
$description = $courseinfo{'description'};
}
$$descriptions{$fromcid} = $description;
@@ -640,7 +860,7 @@ sub disnew {
my %lt=&Apache::lonlocal::texthash(
'nm' => 'New Messages',
'su' => 'Subject',
- 'co' => 'Course/Group',
+ 'co' => 'Course',
'da' => 'Date',
'us' => 'Username',
'op' => 'Open',
@@ -649,17 +869,15 @@ sub disnew {
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 ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
my %status_cache =
&Apache::lonnet::get('email_status',\@msgids);
my %descriptions;
- foreach (@msgids) {
- my $msgid=&escape($_);
+ foreach my $id (@msgids) {
+ my $msgid=&escape($id);
my ($sendtime,$shortsubj,$fromname,$fromdom,$status,$fromcid)=
&Apache::lonmsg::unpackmsgid($msgid,undef,undef,\%status_cache);
if (defined($sendtime) && $sendtime!~/error/) {
@@ -668,17 +886,17 @@ sub disnew {
$sendtime = &Apache::lonlocal::locallocaltime($sendtime);
if ($status eq 'new') {
if ($numsendtime >= $startblock && ($numsendtime <= $endblock && $endblock > 0) ) {
- $blocked{$_} = 'ON';
+ $blocked{$id} = 'ON';
$numblocked ++;
} else {
- push @newmsgs, {
+ push(@newmsgs, {
msgid => $msgid,
sendtime => $sendtime,
shortsub => $shortsubj,
from => $fromname,
fromdom => $fromdom,
- course => $description
- }
+ course => $description,
+ });
}
}
}
@@ -686,18 +904,18 @@ sub disnew {
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");
");
- $r->print(&mt('These').' '.$numblocked.' '.&mt('messages are not viewable because '));
- }
- $r->print(
-&mt('display of LON-CAPA messages sent to you by other students between').' '.$beginblock.' '.&mt('and').' '.$finishblock.' '.&mt('is currently being blocked because of online exams').'.');
- &build_block_table($r,$startblock,$endblock,\%setters);
+ $r->print('
'.&mt('You have [quant,_1,blocked unread message,blocked unread messages].',$numblocked).'
'."\n".
+ &mt('[quant,_1,message is,messages are] not viewable because display of LON-CAPA messages sent to you by other students between [_2] and [_3] is currently being blocked because of online exams.',$numblocked,$beginblock,$finishblock).' '."\n".
+ &Apache::loncommon::build_block_table($startblock,$endblock,
+ \%setters));
}
}
@@ -740,10 +952,8 @@ sub disfolder {
my ($r,$folder)=@_;
my %blocked = ();
my %setters = ();
- my $startblock;
- my $endblock;
my $numblocked = 0;
- &blockcheck(\%setters,\$startblock,\$endblock);
+ my ($startblock,$endblock) = &Apache::loncommon::blockcheck(\%setters,'com');
$r->print(<
function checkall() {
@@ -766,7 +976,7 @@ sub disfolder {
ENDDISHEADER
my $fsqs='&folder='.$folder;
- my @temp=sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
+ my @temp=&sortedmessages(\%blocked,$startblock,$endblock,\$numblocked,$folder);
my $totalnumber=$#temp+1;
unless ($totalnumber>0) {
$r->print('