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

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

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