File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.73: download - view: text, annotated - select for diffs
Mon Oct 13 15:33:02 2008 UTC (15 years, 8 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Server announcement and calendar:
  - Localization: Added missing &mt() calls
  - Added headline to calendar
  - Standard headline usage
  - Use standard warning style for out-of-date warning
  - Moved <br /> from start of list to end of list

    1: # The LearningOnline Network
    2: # Announce
    3: #
    4: # $Id: lonannounce.pm,v 1.73 2008/10/13 15:33:02 bisitz Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   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.
   14: #
   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: 
   29: package Apache::lonannounce;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::loncommon;
   34: use Apache::lonhtmlcommon();
   35: use Apache::lonlocal;
   36: use Apache::lonnavmaps();
   37: use Apache::lonrss();
   38: use Apache::lonnet;
   39: use HTML::Entities();
   40: use LONCAPA qw(:match);
   41: use DateTime;
   42: use DateTime::TimeZone;
   43: 
   44: my %todayhash;
   45: my %showedcheck;
   46: 
   47: sub editfield {
   48:     my ($r,$start,$end,$text)=@_;
   49:     # Deal with date forms
   50:     my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
   51:                                                             'startdate',
   52:                                                             $start);
   53:     my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
   54:                                                           'enddate',
   55:                                                           $end);
   56:     my $help=&Apache::loncommon::help_open_menu('Calendar Add Announcement','Calendar_Add_Announcement',274,'Communication Tools');
   57:     my %lt=&Apache::lonlocal::texthash('annon' => 'Course Announcements',
   58:                                        'post' => 'Post Announcement',
   59: 				       'start' => 'Starting date',
   60: 				       'end' => 'Ending date',
   61: 				       'incrss' => 'Include in course RSS newsfeed');
   62: 
   63:     $r->print(<<ENDFORM);
   64: <h2>$lt{'annon'} $help</h2>
   65: <form name="anno" method="post">
   66: <input type="hidden" value='' name="action" />
   67: <table><tr><td>$lt{'start'}:</td><td>$startdateform</td></tr>
   68: <tr><td>$lt{'end'}:</td><td>$enddateform</td></tr></table>
   69: <textarea name="msg" rows="4" cols="60">$text</textarea>
   70: <br />
   71: <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label>
   72: <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr />
   73: ENDFORM
   74: }
   75: 
   76: sub readcalendar {
   77:     my $courseid=shift;
   78:     my $coursenum=$env{'course.'.$courseid.'.num'};
   79:     my $coursedom=$env{'course.'.$courseid.'.domain'};
   80:     if ($coursenum eq '' || $coursedom eq '') {
   81:         my %courseinfo=&Apache::lonnet::coursedescription($courseid);
   82:         if ($coursenum eq '' && exists($courseinfo{'num'})) {
   83:             $coursenum = $courseinfo{'num'};
   84:         }
   85:         if ($coursedom eq '' && exists($courseinfo{'domain'})) {
   86:             $coursedom = $courseinfo{'domain'};
   87:         }
   88:     }
   89: 
   90:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
   91:     my %returnhash=();
   92:     foreach my $item (keys(%thiscal)) {
   93:         unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) {
   94: 	    my ($start,$end)=split('_',$item);
   95: 	    $returnhash{join("\0",$courseid,$start,$end)}=$thiscal{$item};
   96:         }
   97:     }
   98:     my $can_see_hidden = ($env{'request.role.adv'} &&
   99: 			  ($courseid eq $env{'request.course.id'}));
  100:     
  101:     my $navmap;
  102:     if ($courseid eq $env{'request.course.id'}) {
  103: 	$navmap = Apache::lonnavmaps::navmap->new();
  104:     }
  105: 
  106:     my $resourcedata=
  107: 	&Apache::lonnet::get_courseresdata($coursenum,$coursedom);
  108:     if (ref($resourcedata) ne 'HASH') {
  109:         return %returnhash;
  110:     } 
  111:     foreach my $thiskey (keys(%$resourcedata)) {
  112: 	if ($resourcedata->{$thiskey.'.type'}=~/^date/) {
  113: 	    my ($course,$middle,$part,$name)=
  114: 		($thiskey=~/^(\Q$courseid\E)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/);
  115: 
  116: 	    my %data = ( 'section' => &mt('All Students'));
  117: 	    if ($middle=~/^\[(.*)\]\./) {
  118: 		my $sec=$1;
  119: 		# if we have a section don't show ones that aren't ours
  120: 		if ($env{'request.course.sec'} &&
  121: 		    $env{'request.course.sec'} ne $sec) { next; }
  122: 		# if a student without a section don't show any section ones
  123: 		if (!$env{'request.role.adv'} &&
  124: 		    !$env{'request.course.sec'}) { next; }
  125: 		$data{'section'}=&mt('Group/Section').': '.$1;
  126: 		$middle=~s/^\[(.*)\]\.//;
  127: 	    }
  128: 	    $middle=~s/\.$//;
  129: 	    $data{'realm'}=&mt('All Resources');
  130: 	    if ($middle eq '___(all)') {
  131: 		if (!$can_see_hidden && !$navmap) {
  132: 		    next;
  133: 		}
  134: 	    } elsif ($middle=~/^(.+)\_\_\_\(all\)$/) {
  135: 		my $map_url=$1;
  136: 		if (!$can_see_hidden && !$navmap) {
  137: 		    next;
  138: 		}
  139: 		if (!$can_see_hidden) {
  140: 		    my $res = $navmap->getResourceByUrl($map_url);
  141: 		    if ($res && $res->randomout()) {
  142: 			next;
  143: 		    }
  144: 		}
  145: 		$data{'realm'}=&mt('Folder/Map');
  146: 		$data{'url'} = $map_url;
  147: 	    } elsif ($middle) {
  148: 		if (!$can_see_hidden && !$navmap) {
  149: 		    next;
  150: 		}
  151: 		if (!$can_see_hidden) {
  152: 		    my $res = $navmap->getBySymb($middle);
  153: 		    if ($res && $res->randomout()) {
  154: 			next;
  155: 		    }
  156: 		}
  157: 		$data{'realm'} = &mt('Resource');
  158: 		$data{'symb'} = $middle;
  159: 	    }
  160: 	    $data{'datetype'} = $name;
  161: 	    if ($name eq 'duedate') { 
  162: 		$data{'datetype'} = &mt('Due'); 
  163: # see if accidentally answerdate is before duedate
  164: 		my $answerkey=$thiskey;
  165: 		$answerkey=~s/duedate$/answerdate/;
  166: 		if ($resourcedata->{$thiskey}>$resourcedata->{$answerkey}) {
  167: 		    $data{'datetype'} = &mt('Due and Answer Available');
  168: 		}
  169: 	    }
  170: 	    if ($name eq 'opendate' 
  171: 		|| $name eq 'contentopen' ) {
  172: 		$data{'datetype'}=&mt('Opening');
  173: 	    }
  174: 	    if ($name eq 'contentclose') {
  175: 		$data{'datetype'}=&mt('Closing');
  176: 	    }
  177: 	    if ($name eq 'answerdate') {
  178: # see if accidentally answerdate is before duedate
  179: 		my $duekey=$thiskey;
  180: 		$duekey=~s/answerdate$/duedate/;
  181: 		if ($resourcedata->{$duekey}>$resourcedata->{$thiskey}) {
  182: # forget it
  183: 		    next;
  184: 		} 
  185: 		$data{'datetype'}=&mt('Answer Available'); 
  186: 	    }
  187: 	    $returnhash{join("\0",$courseid,
  188: 			     $resourcedata->{$thiskey},
  189: 			     $resourcedata->{$thiskey})}=\%data;
  190: 	}
  191:     }
  192:     return %returnhash;
  193: }
  194: 
  195: sub emptycell {
  196:     return '<td class="LC_calendar_day_empty">&nbsp;</td>';
  197: }
  198: 
  199: sub normalcell {
  200:     my ($day,$month,$year,$items_ref)=@_;
  201:     my $output;
  202:     my @items=&order($items_ref);
  203:     foreach my $item (@items) {
  204:         if ($item) {
  205: 	    my ($courseid,$start,$end,$msg)=@$item;
  206: 	    my $internalflag= (ref($msg)) ? 1 : 0;
  207: 	    $msg = &display_msg($msg);
  208:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
  209: 		'\n'.&Apache::lonlocal::locallocaltime($start);
  210: 	    if ($start!=$end) {
  211: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
  212: 	    }
  213: 	    $fullmsg.=':\n'.$msg;
  214: 	    $fullmsg=~s/[\n\r]/\\n/gs;
  215:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
  216:             $fullmsg=~s/&/\\&/g;
  217: 	    my $short_msg = substr($msg,0,20).((length($msg) > 20)?'...':'');
  218: 	    if (defined($output)) { $output.='<br />'; }
  219:             if ($courseid eq $env{'request.course.id'}) {
  220:               if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'}))
  221:                && (!$showedcheck{$start.'_'.$end})
  222: 	       && ($env{'form.pickdate'} ne 'yes')
  223: 	       && (!$internalflag)) {
  224:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
  225: 		   $end.'">';
  226:                $showedcheck{$start.'_'.$end}=1;
  227: 	      }
  228: 	    }
  229:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
  230: 	       $short_msg.'</a>';
  231:        }
  232:     }
  233:     return '<td class="LC_calendar_day'.
  234: 	((($day eq $todayhash{'day'}) &&
  235:           ($month eq $todayhash{'month'}) &&
  236:           ($year eq $todayhash{'year'}))?'_current':'').
  237:            '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>';
  238: }
  239: 
  240: sub plaincell {
  241:     my ($items_ref)=@_;
  242:     my $output;
  243:     my @items=&order($items_ref);
  244:     foreach my $item (@items) {
  245:         if (ref($item)) {
  246: 	    my ($courseid,$start,$end,$msg)=@$item;
  247:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
  248: 		'\n'.&Apache::lonlocal::locallocaltime($start);
  249: 	    if ($start!=$end) {
  250: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
  251: 	    }
  252: 	    $msg = &display_msg($msg);
  253: 	    $fullmsg.=':\n'.$msg;
  254:  	    $fullmsg=~s/[\n\r]/\\n/gs;
  255:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
  256:             $fullmsg=~s/&/\\&/g;
  257: 	    my $short_msg = substr($msg,0,80).((length($msg) > 80)?'...':'');
  258: 	    if (defined($output)) { $output.='<br />'; }
  259:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
  260: 	       $short_msg.'</a>';
  261:        }
  262:     }
  263:     return $output;
  264: }
  265: 
  266: sub listcell {
  267:     my ($items_ref)=@_;
  268:     my $output='';
  269:     my @items=&order($items_ref);
  270:     foreach my $item (@items) {
  271:         if (ref($item)) {
  272: 	    my ($courseid,$start,$end,$msg)=@$item;
  273: 	    my $fullmsg=&Apache::lonlocal::locallocaltime($start);
  274: 	    if ($start!=$end) {
  275: 		$fullmsg.=&mt(' to ').
  276: 		    &Apache::lonlocal::locallocaltime($end);
  277: 	    }
  278:             $fullmsg.=':<br /><b>'.&display_msg($msg).'</b>';
  279:             $output.='<li>'.$fullmsg.'</li>';
  280:        }
  281:     }
  282:     return $output;
  283: }
  284: 
  285: sub order {
  286:     my ($items)=@_;
  287:     return sort {
  288: 	my ($astart,$aend)=$a->[1,2];
  289: 	my ($bstart,$bend)=$b->[1,2];
  290: 	if ($astart != $bstart) {
  291: 	    return $astart <=> $bstart;
  292: 	}
  293: 	return $aend <=> $bend;
  294:     } @$items;
  295: }
  296: 
  297: sub nextday {
  298:     my ($tk,%th)=@_;
  299:     my ($incmonth,$incyear);
  300:     if ($th{'day'} > 27) {
  301:         if ($th{'month'} == 2) {
  302:             if ($th{'day'} == 29) { 
  303:                 $incmonth = 1;
  304:             } elsif ($th{'day'} == 28) {
  305:                 if (!&is_leap_year($tk)) {
  306:                    $incmonth = 1;
  307:                 }
  308:             }
  309:         } elsif (($th{'month'} == 4) || ($th{'month'} == 6) || 
  310:                  ($th{'month'} == 9) || ($th{'month'} == 11)) {
  311:             if ($th{'day'} == 30) {
  312:                 $incmonth = 1;
  313:             }
  314:         } elsif ($th{'day'} == 31) {
  315:             if ($th{'month'} == 12) {
  316:                 $incyear = 1;
  317:             } else {
  318:                 $incmonth = 1;
  319:             }
  320:         }
  321:         if ($incyear) {
  322:             $th{'day'} = 1;
  323:             $th{'month'} = 1;
  324:             $th{'year'}++;
  325:         } elsif ($incmonth) {
  326:             $th{'day'} = 1;
  327:             $th{'month'}++;
  328:         } else {
  329:             $th{'day'}++;
  330:         }
  331:     } else {
  332:         $th{'day'}++;
  333:     }
  334:     return (&Apache::loncommon::maketime(%th),$th{'month'});
  335: }
  336: 
  337: sub is_leap_year {
  338:     my ($thistime) = @_;
  339:     my ($is_leap,$timezone,$dt);
  340:     $timezone = &Apache::lonlocal::gettimezone();
  341:     eval {
  342:         $dt = DateTime->from_epoch(epoch => $thistime)
  343:                       ->set_time_zone($timezone);
  344:     };
  345:     if (!$@) {
  346:         $is_leap = $dt->is_leap_year;
  347:     }
  348:     return $is_leap;
  349: }
  350: 
  351: sub display_msg {
  352:     my ($msg) = @_;
  353: 
  354:     # if it's not a ref, it's an instructor provided message
  355:     return $msg if (!ref($msg));
  356: 
  357:     my $output = $msg->{'datetype'}. ': '.$msg->{'realm'};
  358:     if (exists($msg->{'url'})) {
  359: 	my $displayurl=&Apache::lonnet::gettitle($msg->{'url'});
  360: 	if ($msg->{'url'}!~/\Q$displayurl\E$/) {
  361: 	    $output .= ' - '.$displayurl;
  362: 	}
  363:     }
  364:     if (exists($msg->{'symb'})) {
  365: 	my $displaysymb=&Apache::lonnet::gettitle($msg->{'symb'});
  366: 	if ($msg->{'symb'}!~/\Q$displaysymb\E$/) {
  367: 	    $output .= ' - '.$displaysymb;
  368: 	}
  369:     }
  370:     $output .= ' ('.$msg->{'section'}.') ';
  371:     return $output;
  372: }
  373: 
  374: sub showday {
  375:     my ($tk,$mode,%allcal)=@_;
  376:     my %th=&Apache::loncommon::timehash($tk);
  377:     my ($nextday,$nextmonth)=&nextday($tk,%th);
  378:     my @outp;
  379:     if ($mode) {
  380: 	my $oneday=24*3600;
  381: 	$tk-=$oneday;
  382: 	$nextday+=$oneday;
  383:     }
  384:     foreach my $item (keys(%allcal)) {
  385: 	my ($courseid,$startdate,$enddate)= split("\0",$item);
  386: 	if (($startdate<$nextday) && ($enddate>=$tk))  {
  387: 	    push(@outp,[$courseid,$startdate,$enddate,$allcal{$item}]);
  388:         }
  389:     }
  390:     unless ($mode) {
  391:        return ($nextday,$nextmonth,&normalcell(
  392:                $th{'day'},$th{'month'},$th{'year'},\@outp));
  393:    } elsif (@outp) {
  394:        if ($mode==1) {
  395:           return '<br />'.&plaincell(\@outp);
  396:       } else {
  397:           return '<ul>'.&listcell(\@outp).'</ul>';
  398:       }
  399:    } else {
  400:        return '';
  401:    }
  402: }
  403: 
  404: sub picklink {
  405:     my ($text,$day,$month,$year)=@_;
  406:     if ($env{'form.pickdate'} eq 'yes') {
  407: 	return '<a href="javascript:dialin('.$day.','.$month.','.$year.')">'.
  408: 	    $text.'</a>';
  409:     } else {
  410: 	return $text;
  411:     }
  412: }
  413: 
  414: sub dialscript {
  415:     return (<<ENDDIA);
  416: <script language="Javascript">
  417: function dialin(day,month,year) {
  418: 	opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
  419:     var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
  420:     var i;
  421:     for (i=0;i<slct.length;i++) {
  422:         if (slct.options[i].value==month) { slct.selectedIndex=i; }
  423:     }
  424:     opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
  425:     opener.$env{'form.element'}\_checkday();
  426:     self.close();
  427: }
  428: </script>
  429: ENDDIA
  430: }
  431: # ----------------------------------------------------- Summarize all calendars
  432: sub get_all_calendars {
  433:     my %allcal=();
  434:     my %courses = &Apache::loncommon::findallcourses();
  435:     foreach my $course (sort(keys(%courses))) {
  436: 	%allcal=(%allcal,&readcalendar($course));
  437:     }
  438:     return %allcal;
  439: }
  440: 
  441: sub output_ics_file {
  442:     my ($r)=@_;
  443: # RFC 2445 wants CRLF
  444:     my $crlf="\015\012";
  445: # Header
  446:     $r->print("BEGIN:VCALENDAR$crlf");
  447:     $r->print("VERSION:2.0$crlf");
  448:     $r->print("PRODID:-//LONCAPA//LONCAPA Calendar Output//EN$crlf");
  449:     my %allcal=&get_all_calendars();
  450:     foreach my $event (keys(%allcal)) {
  451: 	my ($courseid,$startdate,$enddate)= split('\0',$event);
  452: 	my $uid=$event;
  453: 	$uid=~s/[\W\_]/-/gs;
  454: 	$uid.='@loncapa';
  455: 	my $summary=&display_msg($allcal{$event});
  456: 	$summary=~s/\s+/ /gs;
  457:         $summary=$env{'course.'.$courseid.'.description'}.': '.$summary;
  458: 	$r->print("BEGIN:VEVENT$crlf");
  459: 	$r->print("DTSTART:".&Apache::loncommon::utc_string($startdate).$crlf);
  460: 	$r->print("DTEND:".&Apache::loncommon::utc_string($enddate).$crlf);
  461: 	$r->print("SUMMARY:$summary$crlf");
  462: 	$r->print("UID:$uid$crlf");
  463: 	$r->print("END:VEVENT$crlf");
  464:     }
  465: # Footer
  466:     $r->print("END:VCALENDAR$crlf");
  467: }
  468: 
  469: sub show_timezone {
  470:     my $tzone = &Apache::lonlocal::gettimezone();
  471:     my $dt = DateTime->now();
  472:     my $tz = DateTime::TimeZone->new( name => $tzone );
  473:     return &mt('([_1] time zone)',$tz->short_name_for_datetime($dt));
  474: }
  475: 
  476: sub handler {
  477:     my $r = shift;
  478:     if ($r->uri=~/\.(ics|ical)$/) {
  479:         &Apache::loncommon::content_type($r,'text/calendar');
  480: 	&output_ics_file($r);
  481: 	return OK;
  482:     }
  483:     &Apache::loncommon::content_type($r,'text/html');
  484:     $r->send_http_header;
  485:     return OK if $r->header_only;
  486: 
  487: # ---------------------------------------------------------- Get time right now
  488:     my $today=time;
  489:     %todayhash=&Apache::loncommon::timehash($today);
  490: # ----------------------------------------------------------------- Check marks
  491:     undef(%showedcheck);
  492: # ---------------------------------------------------------- Get month and year
  493:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  494:                              ['month','year','pickdate','formname','element']);
  495: # --------------------------------------------------- Decide what month to show
  496:     my $year=$todayhash{'year'};
  497:     if ($env{'form.year'}) { $year=$env{'form.year'}; }
  498:     my $month=$todayhash{'month'};
  499:     if ($env{'form.month'}) { $month=$env{'form.month'}; }
  500: 
  501: # ---------------------------------------------- See if we are in pickdate mode
  502:     my $pickdatemode=($env{'form.pickdate'} eq 'yes');
  503:     my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}.
  504: 	'&element='.$env{'form.element'};
  505: # --------------------------------------------- Find out first day of the month
  506: 
  507:     my $tk = &Apache::loncommon::maketime( 'day' => 1,
  508:                                            'month'=> $month,
  509:                                            'year' => $year, 
  510:                                            'hour' => 0,
  511: 				           'minute' => 0, 
  512:                                            'second' => 0);
  513:     my %firstday = &Apache::loncommon::timehash($tk);
  514:     my $weekday=$firstday{'weekday'};
  515: 
  516: # ------------------------------------------------------------ Print the screen
  517:     my $js = <<ENDDOCUMENT;
  518: <script type="text/javascript">
  519: 
  520:     function trysubmit() {
  521:         document.anno.action.value="new";
  522: 	document.anno.submit();
  523:     }
  524: 
  525:     function removesub() {
  526:         document.anno.action.value="del";
  527: 	document.anno.submit();
  528:     }
  529: </script>
  530: ENDDOCUMENT
  531: 
  532:     if ($pickdatemode) {
  533: # no big header in pickdate mode
  534: 	$r->print(&Apache::loncommon::start_page("Pick a Date",$js,
  535: 						 {'only_body' => 1,}).
  536: 		  &dialscript().
  537: 		  '<font size="1">');
  538:     } else {
  539:        $r->print(&Apache::loncommon::start_page("Announcements and Calendar",
  540: 						$js));
  541:     }
  542: # does this user have privileges to post, etc?
  543:     my $allowed=0;
  544:     if ($env{'request.course.id'}) {
  545:        $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
  546:     }
  547: # does this user have privileges to post to servers?
  548:     my $serverpost=0;
  549:     if ($env{'request.role.domain'}) {
  550: 	$serverpost=&Apache::lonnet::allowed('psa',
  551: 					     $env{'request.role.domain'});
  552:     } else {
  553: 	$serverpost=&Apache::lonnet::allowed('psa','/');
  554:     }
  555: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
  556:     if ($pickdatemode) { 
  557: 	$serverpost=0; 
  558: 	$allowed=0;
  559:     }
  560: # ------------------------------------------------------------ Process commands
  561:     if ($serverpost) {
  562: 	if ($env{'form.serveraction'}) {
  563: 	    foreach my $key (keys(%env)) {
  564: 		if ($key=~/^form\.postto\_(\w+)/) {
  565: 		    $r->print( 
  566: 			'<br />Posting '.$1.': '.&Apache::lonnet::postannounce
  567: 			($1,$env{'form.serverannnounce'}));
  568: 		}
  569: 	    }
  570: 	}
  571: 	$r->print('<form name="serveranno" method="post">'
  572:                  .'<h2>'.&mt('Post Server Announcements').'</h2>'
  573:                  .&mt('Post announcements to the system login and roles screen').'<br />'
  574:                  .'<i>'.&mt('(leave blank to delete announcement)').'</i><br />'
  575:                  .'<textarea name="serverannnounce" cols="60" rows="5"></textarea><br />'
  576:                  .&mt('Check machines:').'<br />'
  577:         );
  578: # list servers
  579:     my %hostname = &Apache::lonnet::all_hostnames();
  580:     foreach my $host (sort(keys(%hostname))) {
  581: 	if (&Apache::lonnet::allowed('psa',
  582: 				     &Apache::lonnet::host_domain($host))) {
  583: 	    $r->print ('<label><input type="checkbox" name="postto_'.$host.'" /> '.
  584: 		       $host.' <tt>'.$hostname{$host}.'</tt> '.
  585: 		       '</label><a href="http://'.$hostname{$host}.
  586: 		       '/announcement.txt?time='.time.'" target="annowin">'.
  587:                        &mt('Current Announcement').'</a><br />');
  588: 	}
  589:     }
  590:     $r->print(
  591:   '<br /><input type="submit" name="serveraction" value="'.&mt('Post').'"></form><hr />');
  592:     }
  593:     if ($allowed) {
  594:         my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
  595:         my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  596: # ----------------------------------------------------- Store new submitted one
  597:         if ($env{'form.action'} eq 'new') {
  598: 	    my $startdate = 
  599: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
  600: 	    my $enddate   = 
  601: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
  602: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
  603:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
  604:             if ($startdate>$enddate) {
  605: 		my $buffer=$startdate;
  606: 		$startdate=$enddate;
  607: 		$enddate=$buffer;
  608:             }
  609: 	    &Apache::lonnet::put('calendar',{ 
  610: 		$startdate.'_'.$enddate => 
  611: 		    $env{'form.msg'} },$coursedom,$coursenum);
  612: 	    if ($env{'form.rsspost'}) {
  613:                &Apache::lonrss::addentry($coursenum,$coursedom,'Course_Announcements',
  614: 					 &mt('Event from [_1] to [_2]',
  615: 					     &Apache::lonlocal::locallocaltime($startdate),
  616: 					     &Apache::lonlocal::locallocaltime($enddate)),
  617: 					 $env{'form.msg'},'/adm/announcements','public');
  618: 	   }
  619:         }
  620: # ---------------------------------------------------------------- Remove items
  621:         if ($env{'form.action'} eq 'del') {
  622: 	    my @delwhich=();
  623:             foreach my $key (keys(%env)) {
  624: 		if ($key=~/^form\.remove\_(.+)$/) {
  625: 		    push(@delwhich,$1);
  626:                 }
  627:             }
  628:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
  629:         }
  630: # -------------------------------------------------------- Form to post new one
  631:         my %tomorrowhash=%todayhash;
  632:         $tomorrowhash{'day'}++;
  633:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
  634:         
  635:         &editfield($r,$today,$tomorrow,'');
  636:     }
  637: # ----------------------------------------------------- Summarize all calendars
  638:     my %allcal=&get_all_calendars();
  639: # ------------------------------- Initialize table and forward backward buttons
  640:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
  641:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
  642:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
  643: 
  644:     $r->print('<h2>'.&mt('Calendar').'</h2>'
  645:              .'<h3>'.('',&mt('January'),&mt('February'),&mt('March'),
  646: 		      &mt('April'),&mt('May'),
  647: 		      &mt('June'),&mt('July'),&mt('August'),
  648:                       &mt('September'),&mt('October'),
  649:                       &mt('November'),&mt('December'))[$month].' '.
  650: 	              $year.' '.&show_timezone().'</h3>');
  651: # Reached the end of times, give up
  652:     if (($year<1970) || ($year>2037)) {
  653: 	$r->print('<p class="LC_warning">'
  654:                  .&mt('No calendar available for this date.')
  655:                  .'</p>'
  656:                  .'<a href="/adm/announcements?month='.$todayhash{'month'}
  657:                  .'&year='.$todayhash{'year'}.'">'.&mt('Current Month').'</a>'
  658: 		 .&Apache::loncommon::end_page());
  659: 	return OK;
  660:     }
  661: 
  662:     my $class = "LC_calendar";
  663:     if ($env{'form.pickdate'} eq 'yes') {
  664: 	$class .= " LC_calendar_pickdate";
  665:     }
  666: # ------------------------------------------------ Determine first day of a week
  667:     my $datelocale =  &Apache::lonlocal::getdatelocale();
  668:     my $days_in_week = 7;
  669:     my $startweek = 0;
  670:     if (ref($datelocale)) {
  671:         $startweek = $datelocale->first_day_of_week();
  672:         if ($startweek == $days_in_week)  { $startweek = 0; }
  673:     }
  674:     my @days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  675:     my @localdays;
  676:     if ($startweek == 0) {
  677:         @localdays = @days;
  678:     } else {
  679:         my $endday = $days_in_week - $startweek;
  680:         for (my $i=0; $i<$days_in_week; $i++) {
  681:             if ($i < $endday) {
  682:                 $localdays[$i] = $days[$i+$startweek];
  683:             } else {
  684:                 $localdays[$i] = $days[$i-$endday];
  685:             }
  686:         }
  687:     }
  688: 
  689: # ----------------------------------------------------------- Weekday in locale
  690:     my $loc_weekday = $weekday - $startweek;
  691:     if ($loc_weekday < 0) {
  692:         $loc_weekday += $days_in_week; 
  693:     }
  694: 
  695:     $r->print(
  696:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
  697:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
  698:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
  699:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
  700:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  701:  '&year='.$todayhash{'year'}.
  702:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'.
  703:         '<table class="'.$class.'" cols="7" rows="5"><tr>');
  704:     for (my $i=0; $i<@localdays; $i++) {
  705:         $r->print('<th>'.&mt($localdays[$i]).'</th>');
  706:     }
  707:     $r->print('</tr>');
  708: 
  709:     my $outp;
  710:     my $nm;
  711: 
  712: # ---------------------------------------------------------------- Actual table
  713:     $r->print('<tr>');
  714:     for (my $i=0;$i<$loc_weekday;$i++) { $r->print(&emptycell); }
  715:     for (my $i=$loc_weekday;$i<=6;$i++) {
  716:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  717:         $r->print($outp);
  718:     }
  719:     $r->print('</tr>');
  720: 
  721:     my $lastrow = 0;
  722:     my $lastday = 0;
  723:     for (my $k=0;$k<=4;$k++) {
  724:         if (!$lastrow) {
  725:             $r->print('<tr>');
  726:             for (my $i=0;$i<=6;$i++) {
  727:                 if ($lastday) {
  728:                     $outp = &emptycell();
  729:                 } else {
  730:                     my $currtk = $tk;
  731:                     ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  732:                     if ($month!=$nm) { $lastday = 1; }
  733:                 }
  734:                 $r->print($outp);
  735:             }
  736:             if ($lastday) {
  737:                 $lastrow = 1;
  738:             }
  739:             $r->print('</tr>');
  740:         }
  741:     }
  742: # ------------------------------------------------------------------- End table
  743:     $r->print('</table>');
  744: # ----------------------------------------------------------------- Check marks
  745:     undef(%showedcheck);
  746: # --------------------------------------------------------------- Remove button
  747:     if ($allowed) { $r->print('<br /><input type="button" onClick="removesub()" value="'.&mt('Remove Checked Entries').'">'.
  748: 			      &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
  749:     $r->print('<p>'.
  750:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
  751:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
  752:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
  753:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
  754:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  755:  '&year='.$todayhash{'year'}.
  756:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
  757:  ($pickdatemode?'</font>':'').&Apache::loncommon::end_page());
  758:     $r->print('<a href="/adm/announcements.ics">'.&mt('Download your Calendar as iCalendar File').'</a>');
  759:     return OK;
  760: } 
  761: 
  762: 1;
  763: __END__

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