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

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

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