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

1.1       www         1: # The LearningOnline Network
1.2       www         2: # Announce
1.1       www         3: #
1.73    ! bisitz      4: # $Id: lonannounce.pm,v 1.72 2008/09/25 01:39:29 raeburn 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.63      albertel   40: use LONCAPA qw(:match);
1.71      raeburn    41: use DateTime;
                     42: use DateTime::TimeZone;
1.3       www        43: 
1.12      www        44: my %todayhash;
1.16      www        45: my %showedcheck;
1.12      www        46: 
1.10      www        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);
1.55      albertel   56:     my $help=&Apache::loncommon::help_open_menu('Calendar Add Announcement','Calendar_Add_Announcement',274,'Communication Tools');
1.73    ! bisitz     57:     my %lt=&Apache::lonlocal::texthash('annon' => 'Course Announcements',
        !            58:                                        'post' => 'Post Announcement',
1.36      www        59: 				       'start' => 'Starting date',
                     60: 				       'end' => 'Ending date',
1.37      www        61: 				       'incrss' => 'Include in course RSS newsfeed');
1.36      www        62: 
1.10      www        63:     $r->print(<<ENDFORM);
1.73    ! bisitz     64: <h2>$lt{'annon'} $help</h2>
1.10      www        65: <form name="anno" method="post">
1.36      www        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>
1.10      www        69: <textarea name="msg" rows="4" cols="60">$text</textarea>
1.36      www        70: <br />
1.46      albertel   71: <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label>
1.36      www        72: <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr />
1.10      www        73: ENDFORM
                     74: }
                     75: 
1.3       www        76: sub readcalendar {
                     77:     my $courseid=shift;
1.34      albertel   78:     my $coursenum=$env{'course.'.$courseid.'.num'};
                     79:     my $coursedom=$env{'course.'.$courseid.'.domain'};
1.69      raeburn    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: 
1.3       www        90:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
                     91:     my %returnhash=();
1.42      albertel   92:     foreach my $item (keys(%thiscal)) {
                     93:         unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) {
1.64      albertel   94: 	    my ($start,$end)=split('_',$item);
                     95: 	    $returnhash{join("\0",$courseid,$start,$end)}=$thiscal{$item};
1.3       www        96:         }
                     97:     }
1.61      albertel   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:     }
1.68      albertel  105: 
                    106:     my $resourcedata=
1.65      albertel  107: 	&Apache::lonnet::get_courseresdata($coursenum,$coursedom);
1.69      raeburn   108:     if (ref($resourcedata) ne 'HASH') {
                    109:         return %returnhash;
                    110:     } 
1.68      albertel  111:     foreach my $thiskey (keys(%$resourcedata)) {
                    112: 	if ($resourcedata->{$thiskey.'.type'}=~/^date/) {
1.53      www       113: 	    my ($course,$middle,$part,$name)=
1.68      albertel  114: 		($thiskey=~/^(\Q$courseid\E)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/);
                    115: 
1.56      albertel  116: 	    my %data = ( 'section' => &mt('All Students'));
1.53      www       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; }
1.56      albertel  125: 		$data{'section'}=&mt('Group/Section').': '.$1;
1.53      www       126: 		$middle=~s/^\[(.*)\]\.//;
                    127: 	    }
                    128: 	    $middle=~s/\.$//;
1.56      albertel  129: 	    $data{'realm'}=&mt('All Resources');
1.53      www       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);
1.68      albertel  141: 		    if ($res && $res->randomout()) {
                    142: 			next;
                    143: 		    }
1.26      www       144: 		}
1.56      albertel  145: 		$data{'realm'}=&mt('Folder/Map');
                    146: 		$data{'url'} = $map_url;
1.53      www       147: 	    } elsif ($middle) {
                    148: 		if (!$can_see_hidden && !$navmap) {
                    149: 		    next;
1.26      www       150: 		}
1.53      www       151: 		if (!$can_see_hidden) {
                    152: 		    my $res = $navmap->getBySymb($middle);
1.68      albertel  153: 		    if ($res && $res->randomout()) {
                    154: 			next;
                    155: 		    }
1.53      www       156: 		}
1.56      albertel  157: 		$data{'realm'} = &mt('Resource');
                    158: 		$data{'symb'} = $middle;
1.53      www       159: 	    }
1.56      albertel  160: 	    $data{'datetype'} = $name;
1.53      www       161: 	    if ($name eq 'duedate') { 
1.56      albertel  162: 		$data{'datetype'} = &mt('Due'); 
1.28      www       163: # see if accidentally answerdate is before duedate
1.53      www       164: 		my $answerkey=$thiskey;
                    165: 		$answerkey=~s/duedate$/answerdate/;
1.68      albertel  166: 		if ($resourcedata->{$thiskey}>$resourcedata->{$answerkey}) {
1.56      albertel  167: 		    $data{'datetype'} = &mt('Due and Answer Available');
1.28      www       168: 		}
1.53      www       169: 	    }
1.56      albertel  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: 	    }
1.53      www       177: 	    if ($name eq 'answerdate') {
1.28      www       178: # see if accidentally answerdate is before duedate
1.53      www       179: 		my $duekey=$thiskey;
                    180: 		$duekey=~s/answerdate$/duedate/;
1.68      albertel  181: 		if ($resourcedata->{$duekey}>$resourcedata->{$thiskey}) {
1.28      www       182: # forget it
1.53      www       183: 		    next;
                    184: 		} 
1.56      albertel  185: 		$data{'datetype'}=&mt('Answer Available'); 
1.53      www       186: 	    }
1.64      albertel  187: 	    $returnhash{join("\0",$courseid,
1.68      albertel  188: 			     $resourcedata->{$thiskey},
                    189: 			     $resourcedata->{$thiskey})}=\%data;
1.26      www       190: 	}
                    191:     }
1.3       www       192:     return %returnhash;
                    193: }
                    194: 
                    195: sub emptycell {
1.49      albertel  196:     return '<td class="LC_calendar_day_empty">&nbsp;</td>';
1.3       www       197: }
                    198: 
                    199: sub normalcell {
1.56      albertel  200:     my ($day,$month,$year,$items_ref)=@_;
1.51      albertel  201:     my $output;
1.56      albertel  202:     my @items=&order($items_ref);
1.40      albertel  203:     foreach my $item (@items) {
                    204:         if ($item) {
1.56      albertel  205: 	    my ($courseid,$start,$end,$msg)=@$item;
                    206: 	    my $internalflag= (ref($msg)) ? 1 : 0;
                    207: 	    $msg = &display_msg($msg);
1.34      albertel  208:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
1.32      www       209: 		'\n'.&Apache::lonlocal::locallocaltime($start);
1.26      www       210: 	    if ($start!=$end) {
                    211: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
                    212: 	    }
1.32      www       213: 	    $fullmsg.=':\n'.$msg;
1.56      albertel  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 />'; }
1.34      albertel  219:             if ($courseid eq $env{'request.course.id'}) {
                    220:               if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'}))
1.19      www       221:                && (!$showedcheck{$start.'_'.$end})
1.34      albertel  222: 	       && ($env{'form.pickdate'} ne 'yes')
1.26      www       223: 	       && (!$internalflag)) {
1.5       www       224:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
                    225: 		   $end.'">';
1.16      www       226:                $showedcheck{$start.'_'.$end}=1;
1.5       www       227: 	      }
                    228: 	    }
                    229:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.51      albertel  230: 	       $short_msg.'</a>';
1.4       www       231:        }
                    232:     }
1.49      albertel  233:     return '<td class="LC_calendar_day'.
1.12      www       234: 	((($day eq $todayhash{'day'}) &&
                    235:           ($month eq $todayhash{'month'}) &&
1.49      albertel  236:           ($year eq $todayhash{'year'}))?'_current':'').
                    237:            '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>';
1.3       www       238: }
                    239: 
1.11      www       240: sub plaincell {
1.56      albertel  241:     my ($items_ref)=@_;
1.51      albertel  242:     my $output;
1.56      albertel  243:     my @items=&order($items_ref);
1.40      albertel  244:     foreach my $item (@items) {
1.56      albertel  245:         if (ref($item)) {
                    246: 	    my ($courseid,$start,$end,$msg)=@$item;
1.34      albertel  247:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
1.32      www       248: 		'\n'.&Apache::lonlocal::locallocaltime($start);
1.26      www       249: 	    if ($start!=$end) {
                    250: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
                    251: 	    }
1.56      albertel  252: 	    $msg = &display_msg($msg);
1.32      www       253: 	    $fullmsg.=':\n'.$msg;
                    254:  	    $fullmsg=~s/[\n\r]/\\n/gs;
1.15      www       255:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
                    256:             $fullmsg=~s/&/\\&/g;
1.51      albertel  257: 	    my $short_msg = substr($msg,0,80).((length($msg) > 80)?'...':'');
                    258: 	    if (defined($output)) { $output.='<br />'; }
1.11      www       259:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.51      albertel  260: 	       $short_msg.'</a>';
1.11      www       261:        }
                    262:     }
                    263:     return $output;
                    264: }
                    265: 
                    266: sub listcell {
1.56      albertel  267:     my ($items_ref)=@_;
1.11      www       268:     my $output='';
1.56      albertel  269:     my @items=&order($items_ref);
1.40      albertel  270:     foreach my $item (@items) {
1.56      albertel  271:         if (ref($item)) {
                    272: 	    my ($courseid,$start,$end,$msg)=@$item;
                    273: 	    my $fullmsg=&Apache::lonlocal::locallocaltime($start);
1.26      www       274: 	    if ($start!=$end) {
                    275: 		$fullmsg.=&mt(' to ').
                    276: 		    &Apache::lonlocal::locallocaltime($end);
                    277: 	    }
1.56      albertel  278:             $fullmsg.=':<br /><b>'.&display_msg($msg).'</b>';
1.11      www       279:             $output.='<li>'.$fullmsg.'</li>';
                    280:        }
                    281:     }
                    282:     return $output;
                    283: }
                    284: 
1.40      albertel  285: sub order {
1.56      albertel  286:     my ($items)=@_;
                    287:     return sort {
                    288: 	my ($astart,$aend)=$a->[1,2];
                    289: 	my ($bstart,$bend)=$b->[1,2];
1.40      albertel  290: 	if ($astart != $bstart) {
                    291: 	    return $astart <=> $bstart;
                    292: 	}
                    293: 	return $aend <=> $bend;
1.56      albertel  294:     } @$items;
1.40      albertel  295: }
                    296: 
1.3       www       297: sub nextday {
1.71      raeburn   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:     }
1.3       www       334:     return (&Apache::loncommon::maketime(%th),$th{'month'});
                    335: }
                    336: 
1.71      raeburn   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: 
1.56      albertel  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'})) {
1.59      www       359: 	my $displayurl=&Apache::lonnet::gettitle($msg->{'url'});
                    360: 	if ($msg->{'url'}!~/\Q$displayurl\E$/) {
                    361: 	    $output .= ' - '.$displayurl;
                    362: 	}
1.56      albertel  363:     }
                    364:     if (exists($msg->{'symb'})) {
1.59      www       365: 	my $displaysymb=&Apache::lonnet::gettitle($msg->{'symb'});
                    366: 	if ($msg->{'symb'}!~/\Q$displaysymb\E$/) {
                    367: 	    $output .= ' - '.$displaysymb;
                    368: 	}
1.56      albertel  369:     }
                    370:     $output .= ' ('.$msg->{'section'}.') ';
                    371:     return $output;
                    372: }
                    373: 
1.3       www       374: sub showday {
1.11      www       375:     my ($tk,$mode,%allcal)=@_;
1.3       www       376:     my %th=&Apache::loncommon::timehash($tk);
1.71      raeburn   377:     my ($nextday,$nextmonth)=&nextday($tk,%th);
1.56      albertel  378:     my @outp;
1.27      www       379:     if ($mode) {
                    380: 	my $oneday=24*3600;
                    381: 	$tk-=$oneday;
                    382: 	$nextday+=$oneday;
                    383:     }
1.40      albertel  384:     foreach my $item (keys(%allcal)) {
1.64      albertel  385: 	my ($courseid,$startdate,$enddate)= split("\0",$item);
                    386: 	if (($startdate<$nextday) && ($enddate>=$tk))  {
                    387: 	    push(@outp,[$courseid,$startdate,$enddate,$allcal{$item}]);
1.3       www       388:         }
                    389:     }
1.11      www       390:     unless ($mode) {
1.12      www       391:        return ($nextday,$nextmonth,&normalcell(
1.56      albertel  392:                $th{'day'},$th{'month'},$th{'year'},\@outp));
                    393:    } elsif (@outp) {
1.11      www       394:        if ($mode==1) {
1.56      albertel  395:           return '<br />'.&plaincell(\@outp);
1.11      www       396:       } else {
1.56      albertel  397:           return '<ul>'.&listcell(\@outp).'</ul>';
1.11      www       398:       }
                    399:    } else {
                    400:        return '';
                    401:    }
1.3       www       402: }
1.1       www       403: 
1.19      www       404: sub picklink {
                    405:     my ($text,$day,$month,$year)=@_;
1.34      albertel  406:     if ($env{'form.pickdate'} eq 'yes') {
1.19      www       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) {
1.34      albertel  418: 	opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
                    419:     var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
1.19      www       420:     var i;
                    421:     for (i=0;i<slct.length;i++) {
                    422:         if (slct.options[i].value==month) { slct.selectedIndex=i; }
                    423:     }
1.34      albertel  424:     opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
                    425:     opener.$env{'form.element'}\_checkday();
1.19      www       426:     self.close();
                    427: }
                    428: </script>
                    429: ENDDIA
                    430: }
1.52      www       431: # ----------------------------------------------------- Summarize all calendars
                    432: sub get_all_calendars {
                    433:     my %allcal=();
1.62      raeburn   434:     my %courses = &Apache::loncommon::findallcourses();
                    435:     foreach my $course (sort(keys(%courses))) {
1.52      www       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)) {
1.64      albertel  451: 	my ($courseid,$startdate,$enddate)= split('\0',$event);
1.53      www       452: 	my $uid=$event;
                    453: 	$uid=~s/[\W\_]/-/gs;
                    454: 	$uid.='@loncapa';
1.56      albertel  455: 	my $summary=&display_msg($allcal{$event});
1.53      www       456: 	$summary=~s/\s+/ /gs;
                    457:         $summary=$env{'course.'.$courseid.'.description'}.': '.$summary;
1.52      www       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);
1.53      www       461: 	$r->print("SUMMARY:$summary$crlf");
                    462: 	$r->print("UID:$uid$crlf");
1.52      www       463: 	$r->print("END:VEVENT$crlf");
                    464:     }
                    465: # Footer
                    466:     $r->print("END:VCALENDAR$crlf");
                    467: }
1.19      www       468: 
1.71      raeburn   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: 
1.1       www       476: sub handler {
                    477:     my $r = shift;
1.52      www       478:     if ($r->uri=~/\.(ics|ical)$/) {
                    479:         &Apache::loncommon::content_type($r,'text/calendar');
                    480: 	&output_ics_file($r);
                    481: 	return OK;
                    482:     }
1.21      www       483:     &Apache::loncommon::content_type($r,'text/html');
1.1       www       484:     $r->send_http_header;
                    485:     return OK if $r->header_only;
                    486: 
1.3       www       487: # ---------------------------------------------------------- Get time right now
                    488:     my $today=time;
1.12      www       489:     %todayhash=&Apache::loncommon::timehash($today);
1.16      www       490: # ----------------------------------------------------------------- Check marks
1.49      albertel  491:     undef(%showedcheck);
1.3       www       492: # ---------------------------------------------------------- Get month and year
                    493:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.19      www       494:                              ['month','year','pickdate','formname','element']);
1.3       www       495: # --------------------------------------------------- Decide what month to show
                    496:     my $year=$todayhash{'year'};
1.34      albertel  497:     if ($env{'form.year'}) { $year=$env{'form.year'}; }
1.3       www       498:     my $month=$todayhash{'month'};
1.34      albertel  499:     if ($env{'form.month'}) { $month=$env{'form.month'}; }
1.18      www       500: 
                    501: # ---------------------------------------------- See if we are in pickdate mode
1.34      albertel  502:     my $pickdatemode=($env{'form.pickdate'} eq 'yes');
                    503:     my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}.
                    504: 	'&element='.$env{'form.element'};
1.3       www       505: # --------------------------------------------- Find out first day of the month
                    506: 
1.71      raeburn   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);
1.3       www       514:     my $weekday=$firstday{'weekday'};
1.71      raeburn   515: 
1.3       www       516: # ------------------------------------------------------------ Print the screen
1.47      albertel  517:     my $js = <<ENDDOCUMENT;
                    518: <script type="text/javascript">
1.3       www       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>
1.1       www       530: ENDDOCUMENT
1.47      albertel  531: 
1.18      www       532:     if ($pickdatemode) {
                    533: # no big header in pickdate mode
1.47      albertel  534: 	$r->print(&Apache::loncommon::start_page("Pick a Date",$js,
                    535: 						 {'only_body' => 1,}).
1.19      www       536: 		  &dialscript().
1.18      www       537: 		  '<font size="1">');
                    538:     } else {
1.47      albertel  539:        $r->print(&Apache::loncommon::start_page("Announcements and Calendar",
                    540: 						$js));
1.18      www       541:     }
1.3       www       542: # does this user have privileges to post, etc?
                    543:     my $allowed=0;
1.34      albertel  544:     if ($env{'request.course.id'}) {
                    545:        $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
1.3       www       546:     }
1.17      www       547: # does this user have privileges to post to servers?
                    548:     my $serverpost=0;
1.34      albertel  549:     if ($env{'request.role.domain'}) {
1.17      www       550: 	$serverpost=&Apache::lonnet::allowed('psa',
1.34      albertel  551: 					     $env{'request.role.domain'});
1.17      www       552:     } else {
                    553: 	$serverpost=&Apache::lonnet::allowed('psa','/');
                    554:     }
1.18      www       555: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
                    556:     if ($pickdatemode) { 
                    557: 	$serverpost=0; 
                    558: 	$allowed=0;
                    559:     }
1.17      www       560: # ------------------------------------------------------------ Process commands
                    561:     if ($serverpost) {
1.34      albertel  562: 	if ($env{'form.serveraction'}) {
1.42      albertel  563: 	    foreach my $key (keys(%env)) {
                    564: 		if ($key=~/^form\.postto\_(\w+)/) {
1.17      www       565: 		    $r->print( 
                    566: 			'<br />Posting '.$1.': '.&Apache::lonnet::postannounce
1.34      albertel  567: 			($1,$env{'form.serverannnounce'}));
1.17      www       568: 		}
                    569: 	    }
                    570: 	}
1.73    ! bisitz    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:         );
1.17      www       578: # list servers
1.66      albertel  579:     my %hostname = &Apache::lonnet::all_hostnames();
                    580:     foreach my $host (sort(keys(%hostname))) {
1.67      albertel  581: 	if (&Apache::lonnet::allowed('psa',
                    582: 				     &Apache::lonnet::host_domain($host))) {
1.73    ! bisitz    583: 	    $r->print ('<label><input type="checkbox" name="postto_'.$host.'" /> '.
1.66      albertel  584: 		       $host.' <tt>'.$hostname{$host}.'</tt> '.
                    585: 		       '</label><a href="http://'.$hostname{$host}.
1.73    ! bisitz    586: 		       '/announcement.txt?time='.time.'" target="annowin">'.
        !           587:                        &mt('Current Announcement').'</a><br />');
1.17      www       588: 	}
                    589:     }
                    590:     $r->print(
1.73    ! bisitz    591:   '<br /><input type="submit" name="serveraction" value="'.&mt('Post').'"></form><hr />');
1.17      www       592:     }
1.3       www       593:     if ($allowed) {
1.34      albertel  594:         my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
                    595:         my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.3       www       596: # ----------------------------------------------------- Store new submitted one
1.34      albertel  597:         if ($env{'form.action'} eq 'new') {
1.9       www       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:             }
1.3       www       609: 	    &Apache::lonnet::put('calendar',{ 
1.9       www       610: 		$startdate.'_'.$enddate => 
1.34      albertel  611: 		    $env{'form.msg'} },$coursedom,$coursenum);
1.36      www       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: 	   }
1.3       www       619:         }
                    620: # ---------------------------------------------------------------- Remove items
1.34      albertel  621:         if ($env{'form.action'} eq 'del') {
1.3       www       622: 	    my @delwhich=();
1.42      albertel  623:             foreach my $key (keys(%env)) {
                    624: 		if ($key=~/^form\.remove\_(.+)$/) {
1.3       www       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:         
1.10      www       635:         &editfield($r,$today,$tomorrow,'');
1.3       www       636:     }
1.5       www       637: # ----------------------------------------------------- Summarize all calendars
1.52      www       638:     my %allcal=&get_all_calendars();
1.5       www       639: # ------------------------------- Initialize table and forward backward buttons
1.3       www       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); }
1.14      www       643: 
1.73    ! bisitz    644:     $r->print('<h2>'.&mt('Calendar').'</h2>'
        !           645:              .'<h3>'.('',&mt('January'),&mt('February'),&mt('March'),
1.20      www       646: 		      &mt('April'),&mt('May'),
                    647: 		      &mt('June'),&mt('July'),&mt('August'),
                    648:                       &mt('September'),&mt('October'),
                    649:                       &mt('November'),&mt('December'))[$month].' '.
1.73    ! bisitz    650: 	              $year.' '.&show_timezone().'</h3>');
1.13      www       651: # Reached the end of times, give up
                    652:     if (($year<1970) || ($year>2037)) {
1.73    ! bisitz    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());
1.13      www       659: 	return OK;
                    660:     }
1.49      albertel  661: 
                    662:     my $class = "LC_calendar";
                    663:     if ($env{'form.pickdate'} eq 'yes') {
                    664: 	$class .= " LC_calendar_pickdate";
                    665:     }
1.71      raeburn   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: 
1.13      www       695:     $r->print(
1.18      www       696:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
1.20      www       697:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
1.18      www       698:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
1.20      www       699:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
1.12      www       700:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18      www       701:  '&year='.$todayhash{'year'}.
1.20      www       702:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'.
1.71      raeburn   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>');
1.3       www       708: 
                    709:     my $outp;
                    710:     my $nm;
                    711: 
                    712: # ---------------------------------------------------------------- Actual table
                    713:     $r->print('<tr>');
1.71      raeburn   714:     for (my $i=0;$i<$loc_weekday;$i++) { $r->print(&emptycell); }
                    715:     for (my $i=$loc_weekday;$i<=6;$i++) {
1.11      www       716:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       717:         $r->print($outp);
                    718:     }
                    719:     $r->print('</tr>');
                    720: 
1.71      raeburn   721:     my $lastrow = 0;
                    722:     my $lastday = 0;
1.23      www       723:     for (my $k=0;$k<=4;$k++) {
1.71      raeburn   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>');
1.3       www       740:         }
                    741:     }
                    742: # ------------------------------------------------------------------- End table
1.7       matthew   743:     $r->print('</table>');
1.16      www       744: # ----------------------------------------------------------------- Check marks
1.49      albertel  745:     undef(%showedcheck);
1.16      www       746: # --------------------------------------------------------------- Remove button
1.73    ! bisitz    747:     if ($allowed) { $r->print('<br /><input type="button" onClick="removesub()" value="'.&mt('Remove Checked Entries').'">'.
1.24      www       748: 			      &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
1.7       matthew   749:     $r->print('<p>'.
1.18      www       750:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
1.20      www       751:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
1.18      www       752:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
1.20      www       753:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
1.12      www       754:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18      www       755:  '&year='.$todayhash{'year'}.
1.20      www       756:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
1.47      albertel  757:  ($pickdatemode?'</font>':'').&Apache::loncommon::end_page());
1.53      www       758:     $r->print('<a href="/adm/announcements.ics">'.&mt('Download your Calendar as iCalendar File').'</a>');
1.1       www       759:     return OK;
                    760: } 
                    761: 
                    762: 1;
                    763: __END__

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