Annotation of loncom/interface/lonannounce.pm, revision 1.59

1.1       www         1: # The LearningOnline Network
1.2       www         2: # Announce
1.1       www         3: #
1.59    ! www         4: # $Id: lonannounce.pm,v 1.58 2006/07/03 03:11:07 albertel Exp $
1.1       www         5: #
1.3       www         6: # Copyright Michigan State University Board of Trustees
1.1       www         7: #
1.3       www         8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
1.1       www         9: #
1.3       www        10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
1.1       www        14: #
1.3       www        15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
                     28: 
1.2       www        29: package Apache::lonannounce;
1.1       www        30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common);
1.3       www        33: use Apache::loncommon;
1.8       matthew    34: use Apache::lonhtmlcommon();
1.20      www        35: use Apache::lonlocal;
1.43      albertel   36: use Apache::lonnavmaps();
1.36      www        37: use Apache::lonrss();
1.34      albertel   38: use Apache::lonnet;
1.15      www        39: use HTML::Entities();
1.3       www        40: 
1.12      www        41: my %todayhash;
1.16      www        42: my %showedcheck;
1.12      www        43: 
1.10      www        44: sub editfield {
                     45:     my ($r,$start,$end,$text)=@_;
                     46:     # Deal with date forms
                     47:     my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
                     48:                                                             'startdate',
                     49:                                                             $start);
                     50:     my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
                     51:                                                           'enddate',
                     52:                                                           $end);
1.55      albertel   53:     my $help=&Apache::loncommon::help_open_menu('Calendar Add Announcement','Calendar_Add_Announcement',274,'Communication Tools');
1.36      www        54:     my %lt=&Apache::lonlocal::texthash('post' => 'Post Announcement',
                     55: 				       'start' => 'Starting date',
                     56: 				       'end' => 'Ending date',
1.37      www        57: 				       'incrss' => 'Include in course RSS newsfeed');
1.36      www        58: 
1.10      www        59:     $r->print(<<ENDFORM);
1.25      www        60: $help
1.10      www        61: <form name="anno" method="post">
1.36      www        62: <input type="hidden" value='' name="action" />
                     63: <table><tr><td>$lt{'start'}:</td><td>$startdateform</td></tr>
                     64: <tr><td>$lt{'end'}:</td><td>$enddateform</td></tr></table>
1.10      www        65: <textarea name="msg" rows="4" cols="60">$text</textarea>
1.36      www        66: <br />
1.46      albertel   67: <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label>
1.36      www        68: <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr />
1.10      www        69: ENDFORM
                     70: }
                     71: 
1.3       www        72: sub readcalendar {
                     73:     my $courseid=shift;
1.34      albertel   74:     my $coursenum=$env{'course.'.$courseid.'.num'};
                     75:     my $coursedom=$env{'course.'.$courseid.'.domain'};
1.3       www        76:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
                     77:     my %returnhash=();
1.42      albertel   78:     foreach my $item (keys(%thiscal)) {
                     79:         unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) {
                     80: 	   $returnhash{$courseid.'@'.$item}=$thiscal{$item};
1.3       www        81:         }
                     82:     }
1.53      www        83:     my $can_see_hidden = $env{'request.role.adv'};
1.54      albertel   84:     my $navmap;# = Apache::lonnavmaps::navmap->new();
1.53      www        85:     my %resourcedata=
                     86: 	&Apache::lonnet::dump('resourcedata',$coursedom,$coursenum);
                     87:     foreach my $thiskey (sort keys %resourcedata) {
                     88: 	if ($resourcedata{$thiskey.'.type'}=~/^date/) {
                     89: 	    my ($course,$middle,$part,$name)=
                     90: 		($thiskey=~/^(\w+)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/);
1.56      albertel   91: 	    my %data = ( 'section' => &mt('All Students'));
1.53      www        92: 	    if ($middle=~/^\[(.*)\]\./) {
                     93: 		my $sec=$1;
                     94: 		# if we have a section don't show ones that aren't ours
                     95: 		if ($env{'request.course.sec'} &&
                     96: 		    $env{'request.course.sec'} ne $sec) { next; }
                     97: 		# if a student without a section don't show any section ones
                     98: 		if (!$env{'request.role.adv'} &&
                     99: 		    !$env{'request.course.sec'}) { next; }
1.56      albertel  100: 		$data{'section'}=&mt('Group/Section').': '.$1;
1.53      www       101: 		$middle=~s/^\[(.*)\]\.//;
                    102: 	    }
                    103: 	    $middle=~s/\.$//;
1.56      albertel  104: 	    $data{'realm'}=&mt('All Resources');
1.53      www       105: 	    if ($middle eq '___(all)') {
                    106: 		if (!$can_see_hidden && !$navmap) {
                    107: 		    next;
                    108: 		}
                    109: 	    } elsif ($middle=~/^(.+)\_\_\_\(all\)$/) {
                    110: 		my $map_url=$1;
                    111: 		if (!$can_see_hidden && !$navmap) {
                    112: 		    next;
                    113: 		}
                    114: 		if (!$can_see_hidden) {
                    115: 		    my $res = $navmap->getResourceByUrl($map_url);
                    116: 		    if ($res && $res->randomout()) { next; }
1.26      www       117: 		}
1.56      albertel  118: 		$data{'realm'}=&mt('Folder/Map');
                    119: 		$data{'url'} = $map_url;
1.53      www       120: 	    } elsif ($middle) {
                    121: 		if (!$can_see_hidden && !$navmap) {
                    122: 		    next;
1.26      www       123: 		}
1.53      www       124: 		if (!$can_see_hidden) {
                    125: 		    my $res = $navmap->getBySymb($middle);
                    126: 		    if ($res && $res->randomout()) { next; }
                    127: 		}
1.56      albertel  128: 		$data{'realm'} = &mt('Resource');
                    129: 		$data{'symb'} = $middle;
1.53      www       130: 	    }
1.56      albertel  131: 	    $data{'datetype'} = $name;
1.53      www       132: 	    if ($name eq 'duedate') { 
1.56      albertel  133: 		$data{'datetype'} = &mt('Due'); 
1.28      www       134: # see if accidentally answerdate is before duedate
1.53      www       135: 		my $answerkey=$thiskey;
                    136: 		$answerkey=~s/duedate$/answerdate/;
                    137: 		if ($resourcedata{$thiskey}>$resourcedata{$answerkey}) {
1.56      albertel  138: 		    $data{'datetype'} = &mt('Due and Answer Available');
1.28      www       139: 		}
1.53      www       140: 	    }
1.56      albertel  141: 	    if ($name eq 'opendate' 
                    142: 		|| $name eq 'contentopen' ) {
                    143: 		$data{'datetype'}=&mt('Opening');
                    144: 	    }
                    145: 	    if ($name eq 'contentclose') {
                    146: 		$data{'datetype'}=&mt('Closing');
                    147: 	    }
1.53      www       148: 	    if ($name eq 'answerdate') {
1.28      www       149: # see if accidentally answerdate is before duedate
1.53      www       150: 		my $duekey=$thiskey;
                    151: 		$duekey=~s/answerdate$/duedate/;
                    152: 		if ($resourcedata{$duekey}>$resourcedata{$thiskey}) {
1.28      www       153: # forget it
1.53      www       154: 		    next;
                    155: 		} 
1.56      albertel  156: 		$data{'datetype'}=&mt('Answer Available'); 
1.53      www       157: 	    }
                    158: 	    $returnhash{$courseid.'@'.$resourcedata{$thiskey}.'_'.
1.56      albertel  159: 			    $resourcedata{$thiskey}}=\%data;
1.26      www       160: 	}
                    161:     }
1.3       www       162:     return %returnhash;
                    163: }
                    164: 
                    165: sub emptycell {
1.49      albertel  166:     return '<td class="LC_calendar_day_empty">&nbsp;</td>';
1.3       www       167: }
                    168: 
                    169: sub normalcell {
1.56      albertel  170:     my ($day,$month,$year,$items_ref)=@_;
1.51      albertel  171:     my $output;
1.56      albertel  172:     my @items=&order($items_ref);
1.40      albertel  173:     foreach my $item (@items) {
                    174:         if ($item) {
1.56      albertel  175: 	    my ($courseid,$start,$end,$msg)=@$item;
                    176: 	    my $internalflag= (ref($msg)) ? 1 : 0;
                    177: 	    $msg = &display_msg($msg);
1.34      albertel  178:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
1.32      www       179: 		'\n'.&Apache::lonlocal::locallocaltime($start);
1.26      www       180: 	    if ($start!=$end) {
                    181: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
                    182: 	    }
1.32      www       183: 	    $fullmsg.=':\n'.$msg;
1.56      albertel  184: 	    $fullmsg=~s/[\n\r]/\\n/gs;
                    185:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
                    186:             $fullmsg=~s/&/\\&/g;
                    187: 	    my $short_msg = substr($msg,0,20).((length($msg) > 20)?'...':'');
                    188: 	    if (defined($output)) { $output.='<br />'; }
1.34      albertel  189:             if ($courseid eq $env{'request.course.id'}) {
                    190:               if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'}))
1.19      www       191:                && (!$showedcheck{$start.'_'.$end})
1.34      albertel  192: 	       && ($env{'form.pickdate'} ne 'yes')
1.26      www       193: 	       && (!$internalflag)) {
1.5       www       194:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
                    195: 		   $end.'">';
1.16      www       196:                $showedcheck{$start.'_'.$end}=1;
1.5       www       197: 	      }
                    198: 	    }
                    199:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.51      albertel  200: 	       $short_msg.'</a>';
1.4       www       201:        }
                    202:     }
1.49      albertel  203:     return '<td class="LC_calendar_day'.
1.12      www       204: 	((($day eq $todayhash{'day'}) &&
                    205:           ($month eq $todayhash{'month'}) &&
1.49      albertel  206:           ($year eq $todayhash{'year'}))?'_current':'').
                    207:            '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>';
1.3       www       208: }
                    209: 
1.11      www       210: sub plaincell {
1.56      albertel  211:     my ($items_ref)=@_;
1.51      albertel  212:     my $output;
1.56      albertel  213:     my @items=&order($items_ref);
1.40      albertel  214:     foreach my $item (@items) {
1.56      albertel  215:         if (ref($item)) {
                    216: 	    my ($courseid,$start,$end,$msg)=@$item;
1.34      albertel  217:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
1.32      www       218: 		'\n'.&Apache::lonlocal::locallocaltime($start);
1.26      www       219: 	    if ($start!=$end) {
                    220: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
                    221: 	    }
1.56      albertel  222: 	    $msg = &display_msg($msg);
1.32      www       223: 	    $fullmsg.=':\n'.$msg;
                    224:  	    $fullmsg=~s/[\n\r]/\\n/gs;
1.15      www       225:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
                    226:             $fullmsg=~s/&/\\&/g;
1.51      albertel  227: 	    my $short_msg = substr($msg,0,80).((length($msg) > 80)?'...':'');
                    228: 	    if (defined($output)) { $output.='<br />'; }
1.11      www       229:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.51      albertel  230: 	       $short_msg.'</a>';
1.11      www       231:        }
                    232:     }
                    233:     return $output;
                    234: }
                    235: 
                    236: sub listcell {
1.56      albertel  237:     my ($items_ref)=@_;
1.11      www       238:     my $output='';
1.56      albertel  239:     my @items=&order($items_ref);
1.40      albertel  240:     foreach my $item (@items) {
1.56      albertel  241:         if (ref($item)) {
                    242: 	    my ($courseid,$start,$end,$msg)=@$item;
                    243: 	    my $fullmsg=&Apache::lonlocal::locallocaltime($start);
1.26      www       244: 	    if ($start!=$end) {
                    245: 		$fullmsg.=&mt(' to ').
                    246: 		    &Apache::lonlocal::locallocaltime($end);
                    247: 	    }
1.56      albertel  248:             $fullmsg.=':<br /><b>'.&display_msg($msg).'</b>';
1.11      www       249:             $output.='<li>'.$fullmsg.'</li>';
                    250:        }
                    251:     }
                    252:     return $output;
                    253: }
                    254: 
1.40      albertel  255: sub order {
1.56      albertel  256:     my ($items)=@_;
                    257:     return sort {
                    258: 	my ($astart,$aend)=$a->[1,2];
                    259: 	my ($bstart,$bend)=$b->[1,2];
1.40      albertel  260: 	if ($astart != $bstart) {
                    261: 	    return $astart <=> $bstart;
                    262: 	}
                    263: 	return $aend <=> $bend;
1.56      albertel  264:     } @$items;
1.40      albertel  265: }
                    266: 
1.3       www       267: sub nextday {
                    268:     my %th=@_;
                    269:     $th{'day'}++;
                    270:     return (&Apache::loncommon::maketime(%th),$th{'month'});
                    271: }
                    272: 
1.56      albertel  273: sub display_msg {
                    274:     my ($msg) = @_;
                    275: 
                    276:     # if it's not a ref, it's an instructor provided message
                    277:     return $msg if (!ref($msg));
                    278: 
                    279:     my $output = $msg->{'datetype'}. ': '.$msg->{'realm'};
                    280:     if (exists($msg->{'url'})) {
1.59    ! www       281: 	my $displayurl=&Apache::lonnet::gettitle($msg->{'url'});
        !           282: 	if ($msg->{'url'}!~/\Q$displayurl\E$/) {
        !           283: 	    $output .= ' - '.$displayurl;
        !           284: 	}
1.56      albertel  285:     }
                    286:     if (exists($msg->{'symb'})) {
1.59    ! www       287: 	my $displaysymb=&Apache::lonnet::gettitle($msg->{'symb'});
        !           288: 	if ($msg->{'symb'}!~/\Q$displaysymb\E$/) {
        !           289: 	    $output .= ' - '.$displaysymb;
        !           290: 	}
1.56      albertel  291:     }
                    292:     $output .= ' ('.$msg->{'section'}.') ';
                    293:     return $output;
                    294: }
                    295: 
1.3       www       296: sub showday {
1.11      www       297:     my ($tk,$mode,%allcal)=@_;
1.3       www       298:     my %th=&Apache::loncommon::timehash($tk);
                    299:     my ($nextday,$nextmonth)=&nextday(%th);
1.56      albertel  300:     my @outp;
1.27      www       301:     if ($mode) {
                    302: 	my $oneday=24*3600;
                    303: 	$tk-=$oneday;
                    304: 	$nextday+=$oneday;
                    305:     }
1.40      albertel  306:     foreach my $item (keys(%allcal)) {
                    307: 	my ($course,$startdate,$enddate)=($item=~/^(\w+)\@(\d+)\_(\d+)$/);
1.39      albertel  308:         if (($startdate<$nextday) && ($enddate>=$tk))  {
1.56      albertel  309: 	    push(@outp,[$course,$startdate,$enddate,$allcal{$item}]);
1.3       www       310:         }
                    311:     }
1.11      www       312:     unless ($mode) {
1.12      www       313:        return ($nextday,$nextmonth,&normalcell(
1.56      albertel  314:                $th{'day'},$th{'month'},$th{'year'},\@outp));
                    315:    } elsif (@outp) {
1.11      www       316:        if ($mode==1) {
1.56      albertel  317:           return '<br />'.&plaincell(\@outp);
1.11      www       318:       } else {
1.56      albertel  319:           return '<ul>'.&listcell(\@outp).'</ul>';
1.11      www       320:       }
                    321:    } else {
                    322:        return '';
                    323:    }
1.3       www       324: }
1.1       www       325: 
1.19      www       326: sub picklink {
                    327:     my ($text,$day,$month,$year)=@_;
1.34      albertel  328:     if ($env{'form.pickdate'} eq 'yes') {
1.19      www       329: 	return '<a href="javascript:dialin('.$day.','.$month.','.$year.')">'.
                    330: 	    $text.'</a>';
                    331:     } else {
                    332: 	return $text;
                    333:     }
                    334: }
                    335: 
                    336: sub dialscript {
                    337:     return (<<ENDDIA);
                    338: <script language="Javascript">
                    339: function dialin(day,month,year) {
1.34      albertel  340: 	opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
                    341:     var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
1.19      www       342:     var i;
                    343:     for (i=0;i<slct.length;i++) {
                    344:         if (slct.options[i].value==month) { slct.selectedIndex=i; }
                    345:     }
1.34      albertel  346:     opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
                    347:     opener.$env{'form.element'}\_checkday();
1.19      www       348:     self.close();
                    349: }
                    350: </script>
                    351: ENDDIA
                    352: }
1.52      www       353: # ----------------------------------------------------- Summarize all calendars
                    354: sub get_all_calendars {
                    355:     my %allcal=();
                    356:     foreach my $course (sort(&Apache::loncommon::findallcourses())) {
                    357: 	%allcal=(%allcal,&readcalendar($course));
                    358:     }
                    359:     return %allcal;
                    360: }
                    361: 
                    362: sub output_ics_file {
                    363:     my ($r)=@_;
                    364: # RFC 2445 wants CRLF
                    365:     my $crlf="\015\012";
                    366: # Header
                    367:     $r->print("BEGIN:VCALENDAR$crlf");
                    368:     $r->print("VERSION:2.0$crlf");
                    369:     $r->print("PRODID:-//LONCAPA//LONCAPA Calendar Output//EN$crlf");
                    370:     my %allcal=&get_all_calendars();
                    371:     foreach my $event (keys(%allcal)) {
1.53      www       372: 	my ($courseid,$startdate,$enddate)=($event=~/^(\w+)\@(\d+)\_(\d+)$/);
                    373: 	my $uid=$event;
                    374: 	$uid=~s/[\W\_]/-/gs;
                    375: 	$uid.='@loncapa';
1.56      albertel  376: 	my $summary=&display_msg($allcal{$event});
1.53      www       377: 	$summary=~s/\s+/ /gs;
                    378:         $summary=$env{'course.'.$courseid.'.description'}.': '.$summary;
1.52      www       379: 	$r->print("BEGIN:VEVENT$crlf");
                    380: 	$r->print("DTSTART:".&Apache::loncommon::utc_string($startdate).$crlf);
                    381: 	$r->print("DTEND:".&Apache::loncommon::utc_string($enddate).$crlf);
1.53      www       382: 	$r->print("SUMMARY:$summary$crlf");
                    383: 	$r->print("UID:$uid$crlf");
1.52      www       384: 	$r->print("END:VEVENT$crlf");
                    385:     }
                    386: # Footer
                    387:     $r->print("END:VCALENDAR$crlf");
                    388: }
1.19      www       389: 
1.1       www       390: sub handler {
                    391:     my $r = shift;
1.52      www       392:     if ($r->uri=~/\.(ics|ical)$/) {
                    393:         &Apache::loncommon::content_type($r,'text/calendar');
                    394: 	&output_ics_file($r);
                    395: 	return OK;
                    396:     }
1.21      www       397:     &Apache::loncommon::content_type($r,'text/html');
1.1       www       398:     $r->send_http_header;
                    399:     return OK if $r->header_only;
                    400: 
1.3       www       401: # ---------------------------------------------------------- Get time right now
                    402:     my $today=time;
1.12      www       403:     %todayhash=&Apache::loncommon::timehash($today);
1.16      www       404: # ----------------------------------------------------------------- Check marks
1.49      albertel  405:     undef(%showedcheck);
1.3       www       406: # ---------------------------------------------------------- Get month and year
                    407:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.19      www       408:                              ['month','year','pickdate','formname','element']);
1.3       www       409: # --------------------------------------------------- Decide what month to show
                    410:     my $year=$todayhash{'year'};
1.34      albertel  411:     if ($env{'form.year'}) { $year=$env{'form.year'}; }
1.3       www       412:     my $month=$todayhash{'month'};
1.34      albertel  413:     if ($env{'form.month'}) { $month=$env{'form.month'}; }
1.18      www       414: 
                    415: # ---------------------------------------------- See if we are in pickdate mode
1.34      albertel  416:     my $pickdatemode=($env{'form.pickdate'} eq 'yes');
                    417:     my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}.
                    418: 	'&element='.$env{'form.element'};
1.3       www       419: # --------------------------------------------- Find out first day of the month
                    420: 
                    421:     my %firstday=&Apache::loncommon::timehash(
                    422:        &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
                    423:                                      'year' => $year, 'hours' => 0,
                    424: 				     'minutes' => 0, 'seconds' => 0,
1.30      www       425:                                      'dlsav' => -1 ));
1.3       www       426:     my $weekday=$firstday{'weekday'};
                    427: # ------------------------------------------------------------ Print the screen
1.47      albertel  428:     my $js = <<ENDDOCUMENT;
                    429: <script type="text/javascript">
1.3       www       430: 
                    431:     function trysubmit() {
                    432:         document.anno.action.value="new";
                    433: 	document.anno.submit();
                    434:     }
                    435: 
                    436:     function removesub() {
                    437:         document.anno.action.value="del";
                    438: 	document.anno.submit();
                    439:     }
                    440: </script>
1.1       www       441: ENDDOCUMENT
1.47      albertel  442: 
1.18      www       443:     if ($pickdatemode) {
                    444: # no big header in pickdate mode
1.47      albertel  445: 	$r->print(&Apache::loncommon::start_page("Pick a Date",$js,
                    446: 						 {'only_body' => 1,}).
1.19      www       447: 		  &dialscript().
1.18      www       448: 		  '<font size="1">');
                    449:     } else {
1.47      albertel  450:        $r->print(&Apache::loncommon::start_page("Announcements and Calendar",
                    451: 						$js));
1.18      www       452:     }
1.3       www       453: # does this user have privileges to post, etc?
                    454:     my $allowed=0;
1.34      albertel  455:     if ($env{'request.course.id'}) {
                    456:        $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
1.3       www       457:     }
1.17      www       458: # does this user have privileges to post to servers?
                    459:     my $serverpost=0;
1.34      albertel  460:     if ($env{'request.role.domain'}) {
1.17      www       461: 	$serverpost=&Apache::lonnet::allowed('psa',
1.34      albertel  462: 					     $env{'request.role.domain'});
1.17      www       463:     } else {
                    464: 	$serverpost=&Apache::lonnet::allowed('psa','/');
                    465:     }
1.18      www       466: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
                    467:     if ($pickdatemode) { 
                    468: 	$serverpost=0; 
                    469: 	$allowed=0;
                    470:     }
1.17      www       471: # ------------------------------------------------------------ Process commands
                    472:     if ($serverpost) {
1.34      albertel  473: 	if ($env{'form.serveraction'}) {
1.42      albertel  474: 	    foreach my $key (keys(%env)) {
                    475: 		if ($key=~/^form\.postto\_(\w+)/) {
1.17      www       476: 		    $r->print( 
                    477: 			'<br />Posting '.$1.': '.&Apache::lonnet::postannounce
1.34      albertel  478: 			($1,$env{'form.serverannnounce'}));
1.17      www       479: 		}
                    480: 	    }
                    481: 	}
                    482: 	$r->print(<<SERVERANNOUNCE);
                    483: <form name="serveranno" method="post">
                    484: <h3>Post Server Announcements</h3>
                    485: Post announcements to the system login and roles screen<br />
                    486: <i>(leave blank to delete announcement)</i><br />
                    487: <textarea name="serverannnounce" cols="60" rows="5"></textarea><br />
                    488: Check machines:<br />
                    489: SERVERANNOUNCE
                    490: # list servers
1.42      albertel  491:     foreach my $host (sort(keys(%Apache::lonnet::hostname))) {
                    492: 	if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$host})) {
                    493: 	    $r->print ('<br /><input type="checkbox" name="postto_'.$host.'" /> '.
                    494: 		       $host.' <tt>'.$Apache::lonnet::hostname{$host}.'</tt> '.
                    495: 		       '<a href="http://'.$Apache::lonnet::hostname{$host}.
1.17      www       496: 		       '/announcement.txt" target="annowin">current</a>');
                    497: 	}
                    498:     }
                    499:     $r->print(
                    500:   '<br /><input type="submit" name="serveraction" value="Post"></form><hr />');
                    501:     }
1.3       www       502:     if ($allowed) {
1.34      albertel  503:         my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
                    504:         my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.3       www       505: # ----------------------------------------------------- Store new submitted one
1.34      albertel  506:         if ($env{'form.action'} eq 'new') {
1.9       www       507: 	    my $startdate = 
                    508: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
                    509: 	    my $enddate   = 
                    510: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
                    511: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
                    512:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
                    513:             if ($startdate>$enddate) {
                    514: 		my $buffer=$startdate;
                    515: 		$startdate=$enddate;
                    516: 		$enddate=$buffer;
                    517:             }
1.3       www       518: 	    &Apache::lonnet::put('calendar',{ 
1.9       www       519: 		$startdate.'_'.$enddate => 
1.34      albertel  520: 		    $env{'form.msg'} },$coursedom,$coursenum);
1.36      www       521: 	    if ($env{'form.rsspost'}) {
                    522:                &Apache::lonrss::addentry($coursenum,$coursedom,'Course_Announcements',
                    523: 					 &mt('Event from [_1] to [_2]',
                    524: 					     &Apache::lonlocal::locallocaltime($startdate),
                    525: 					     &Apache::lonlocal::locallocaltime($enddate)),
                    526: 					 $env{'form.msg'},'/adm/announcements','public');
                    527: 	   }
1.3       www       528:         }
                    529: # ---------------------------------------------------------------- Remove items
1.34      albertel  530:         if ($env{'form.action'} eq 'del') {
1.3       www       531: 	    my @delwhich=();
1.42      albertel  532:             foreach my $key (keys(%env)) {
                    533: 		if ($key=~/^form\.remove\_(.+)$/) {
1.3       www       534: 		    push(@delwhich,$1);
                    535:                 }
                    536:             }
                    537:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
                    538:         }
                    539: # -------------------------------------------------------- Form to post new one
                    540:         my %tomorrowhash=%todayhash;
                    541:         $tomorrowhash{'day'}++;
                    542:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
                    543:         
1.10      www       544:         &editfield($r,$today,$tomorrow,'');
1.3       www       545:     }
1.5       www       546: # ----------------------------------------------------- Summarize all calendars
1.52      www       547:     my %allcal=&get_all_calendars();
1.5       www       548: # ------------------------------- Initialize table and forward backward buttons
1.3       www       549:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
                    550:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
                    551:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
1.14      www       552: 
1.20      www       553:     $r->print('<h1>'.('',&mt('January'),&mt('February'),&mt('March'),
                    554: 		      &mt('April'),&mt('May'),
                    555: 		      &mt('June'),&mt('July'),&mt('August'),
                    556:                       &mt('September'),&mt('October'),
                    557:                       &mt('November'),&mt('December'))[$month].' '.
                    558: 	              $year.'</h1>');
1.13      www       559: # Reached the end of times, give up
                    560:     if (($year<1970) || ($year>2037)) {
                    561: 	$r->print('<h3>No calendar available for this date.</h3>'.
                    562:  '<a href="/adm/announcements?month='.$todayhash{'month'}.
1.47      albertel  563:  '&year='.$todayhash{'year'}.'">Current Month</a>'.
                    564: 		  &Apache::loncommon::end_page());
1.13      www       565: 	return OK;
                    566:     }
1.49      albertel  567: 
                    568:     my $class = "LC_calendar";
                    569:     if ($env{'form.pickdate'} eq 'yes') {
                    570: 	$class .= " LC_calendar_pickdate";
                    571:     }
1.13      www       572:     $r->print(
1.18      www       573:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
1.20      www       574:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
1.18      www       575:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
1.20      www       576:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
1.12      www       577:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18      www       578:  '&year='.$todayhash{'year'}.
1.20      www       579:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'.
1.49      albertel  580:         '<table class="'.$class.'" cols="7" rows="5"><tr>
                    581: <th>'.&mt('Sun').'</th>
                    582: <th>'.&mt('Mon').'</th>
                    583: <th>'.&mt('Tue').'</th>
                    584: <th>'.&mt('Wed').'</th>
                    585: <th>'.&mt('Thu').'</th>
                    586: <th>'.&mt('Fri').'</th>
                    587: <th>'.&mt('Sat').'</th></tr>');
1.3       www       588: 
                    589:     my $tk=&Apache::loncommon::maketime(%firstday);
                    590:     my $outp;
                    591:     my $nm;
                    592: 
                    593: # ---------------------------------------------------------------- Actual table
                    594:     $r->print('<tr>');
                    595:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
                    596:     for (my $i=$weekday;$i<=6;$i++) { 
1.11      www       597:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       598:         $r->print($outp);
                    599:     }
                    600:     $r->print('</tr>');
                    601: 
1.23      www       602:     for (my $k=0;$k<=4;$k++) {
1.3       www       603:         $r->print('<tr>');
                    604:         for (my $i=0;$i<=6;$i++) {
1.11      www       605:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       606:             if ($month!=$nm) { $outp=&emptycell; }
                    607:             $r->print($outp);
                    608:         }
                    609:         $r->print('</tr>');
                    610:     }
                    611: # ------------------------------------------------------------------- End table
1.7       matthew   612:     $r->print('</table>');
1.16      www       613: # ----------------------------------------------------------------- Check marks
1.49      albertel  614:     undef(%showedcheck);
1.16      www       615: # --------------------------------------------------------------- Remove button
1.24      www       616:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries">'.
                    617: 			      &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
1.7       matthew   618:     $r->print('<p>'.
1.18      www       619:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
1.20      www       620:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
1.18      www       621:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
1.20      www       622:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
1.12      www       623:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18      www       624:  '&year='.$todayhash{'year'}.
1.20      www       625:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
1.47      albertel  626:  ($pickdatemode?'</font>':'').&Apache::loncommon::end_page());
1.53      www       627:     $r->print('<a href="/adm/announcements.ics">'.&mt('Download your Calendar as iCalendar File').'</a>');
1.1       www       628:     return OK;
                    629: } 
                    630: 
                    631: 1;
                    632: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>