File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.82: download - view: text, annotated - select for diffs
Tue Mar 16 20:10:13 2010 UTC (14 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_1, version_2_9_0, version_2_8_99_1, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- fix typo in texthash key.

    1: # The LearningOnline Network
    2: # Announce
    3: #
    4: # $Id: lonannounce.pm,v 1.82 2010/03/16 20:10:13 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonannounce;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::loncommon;
   34: use Apache::lonhtmlcommon();
   35: use Apache::lonlocal;
   36: use Apache::lonnavmaps();
   37: use Apache::lonrss();
   38: use Apache::lonnet;
   39: use HTML::Entities();
   40: use LONCAPA qw(:match);
   41: use DateTime;
   42: use DateTime::TimeZone;
   43: 
   44: my %todayhash;
   45: my %showedcheck;
   46: 
   47: sub editfield {
   48:     my ($r,$start,$end,$text,$crstype)=@_;
   49:     # Deal with date forms
   50:     my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
   51:                                                             'startdate',
   52:                                                             $start);
   53:     my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
   54:                                                           'enddate',
   55:                                                           $end);
   56:     #my $help=&Apache::loncommon::help_open_menu('Calendar Add Announcement','Calendar_Add_Announcement',274,'Communication Tools');
   57:     my $help=&Apache::loncommon::help_open_topic('Calendar_Add_Announcement');
   58:     
   59:     my %lt=&Apache::lonlocal::texthash('annon' => 'Course Announcements',
   60:                                        'post' => 'Post Announcement',
   61: 				       'start' => 'Starting date',
   62: 				       'end' => 'Ending date',
   63: 				       'incrss' => 'Include in course RSS newsfeed');
   64:     if ($crstype eq 'Community') {
   65:         $lt{'annon'} = &mt('Community Announcements');
   66:         $lt{'incrss'} = &mt('Include in community RSS newsfeed');
   67:     }
   68: 
   69:     $r->print(<<ENDFORM);
   70: <h2>$lt{'annon'} $help</h2>
   71: <form name="anno" method="post">
   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>
   75: <textarea name="msg" rows="4" cols="60">$text</textarea>
   76: <br />
   77: <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label>
   78: <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr />
   79: ENDFORM
   80: }
   81: 
   82: sub readcalendar {
   83:     my $courseid=shift;
   84:     my $coursenum=$env{'course.'.$courseid.'.num'};
   85:     my $coursedom=$env{'course.'.$courseid.'.domain'};
   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: 
   96:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
   97:     my %returnhash=();
   98:     foreach my $item (keys(%thiscal)) {
   99:         unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) {
  100: 	    my ($start,$end)=split('_',$item);
  101: 	    $returnhash{join("\0",$courseid,$start,$end)}=$thiscal{$item};
  102:         }
  103:     }
  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:     }
  111: 
  112:     my $resourcedata=
  113: 	&Apache::lonnet::get_courseresdata($coursenum,$coursedom);
  114:     if (ref($resourcedata) ne 'HASH') {
  115:         return %returnhash;
  116:     } 
  117:     foreach my $thiskey (keys(%$resourcedata)) {
  118: 	if ($resourcedata->{$thiskey.'.type'}=~/^date/) {
  119: 	    my ($course,$middle,$part,$name)=
  120: 		($thiskey=~/^(\Q$courseid\E)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/);
  121: 
  122: 	    my %data = ( 'section' => &mt('All Students'));
  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; }
  131: 		$data{'section'}=&mt('Group/Section').': '.$1;
  132: 		$middle=~s/^\[(.*)\]\.//;
  133: 	    }
  134: 	    $middle=~s/\.$//;
  135: 	    $data{'realm'}=&mt('All Resources');
  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);
  147: 		    if ($res && $res->randomout()) {
  148: 			next;
  149: 		    }
  150: 		}
  151: 		$data{'realm'}=&mt('Folder/Map');
  152: 		$data{'url'} = $map_url;
  153: 	    } elsif ($middle) {
  154: 		if (!$can_see_hidden && !$navmap) {
  155: 		    next;
  156: 		}
  157: 		if (!$can_see_hidden) {
  158: 		    my $res = $navmap->getBySymb($middle);
  159: 		    if ($res && $res->randomout()) {
  160: 			next;
  161: 		    }
  162: 		}
  163: 		$data{'realm'} = &mt('Resource');
  164: 		$data{'symb'} = $middle;
  165: 	    }
  166: 	    $data{'datetype'} = $name;
  167: 	    if ($name eq 'duedate') { 
  168: 		$data{'datetype'} = &mt('Due'); 
  169: # see if accidentally answerdate is before duedate
  170: 		my $answerkey=$thiskey;
  171: 		$answerkey=~s/duedate$/answerdate/;
  172: 		if ($resourcedata->{$thiskey}>$resourcedata->{$answerkey}) {
  173: 		    $data{'datetype'} = &mt('Due and Answer Available');
  174: 		}
  175: 	    }
  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: 	    }
  183: 	    if ($name eq 'answerdate') {
  184: # see if accidentally answerdate is before duedate
  185: 		my $duekey=$thiskey;
  186: 		$duekey=~s/answerdate$/duedate/;
  187: 		if ($resourcedata->{$duekey}>$resourcedata->{$thiskey}) {
  188: # forget it
  189: 		    next;
  190: 		} 
  191: 		$data{'datetype'}=&mt('Answer Available'); 
  192: 	    }
  193: 	    $returnhash{join("\0",$courseid,
  194: 			     $resourcedata->{$thiskey},
  195: 			     $resourcedata->{$thiskey})}=\%data;
  196: 	}
  197:     }
  198:     return %returnhash;
  199: }
  200: 
  201: sub emptycell {
  202:     return '<td class="LC_calendar_day_empty">&nbsp;</td>';
  203: }
  204: 
  205: sub normalcell {
  206:     my ($day,$month,$year,$items_ref)=@_;
  207:     my $output;
  208:     my @items=&order($items_ref);
  209:     foreach my $item (@items) {
  210:         if ($item) {
  211: 	    my ($courseid,$start,$end,$msg)=@$item;
  212: 	    my $internalflag= (ref($msg)) ? 1 : 0;
  213: 	    $msg = &display_msg($msg);
  214:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
  215: 		'\n'.&Apache::lonlocal::locallocaltime($start);
  216: 	    if ($start!=$end) {
  217: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
  218: 	    }
  219: 	    $fullmsg.=':\n'.$msg;
  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 />'; }
  225:             if ($courseid eq $env{'request.course.id'}) {
  226:               if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'}))
  227:                && (!$showedcheck{$start.'_'.$end})
  228: 	       && ($env{'form.pickdate'} ne 'yes')
  229: 	       && (!$internalflag)) {
  230:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
  231: 		   $end.'">';
  232:                $showedcheck{$start.'_'.$end}=1;
  233: 	      }
  234: 	    }
  235:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
  236: 	       $short_msg.'</a>';
  237:        }
  238:     }
  239:     return '<td class="LC_calendar_day'.
  240: 	((($day eq $todayhash{'day'}) &&
  241:           ($month eq $todayhash{'month'}) &&
  242:           ($year eq $todayhash{'year'}))?'_current':'').
  243:            '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>';
  244: }
  245: 
  246: sub plaincell {
  247:     my ($items_ref)=@_;
  248:     my $output;
  249:     my @items=&order($items_ref);
  250:     foreach my $item (@items) {
  251:         if (ref($item)) {
  252: 	    my ($courseid,$start,$end,$msg)=@$item;
  253:             my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
  254: 		'\n'.&Apache::lonlocal::locallocaltime($start);
  255: 	    if ($start!=$end) {
  256: 		$fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
  257: 	    }
  258: 	    $msg = &display_msg($msg);
  259: 	    $fullmsg.=':\n'.$msg;
  260:  	    $fullmsg=~s/[\n\r]/\\n/gs;
  261:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
  262:             $fullmsg=~s/&/\\&/g;
  263: 	    my $short_msg = substr($msg,0,80).((length($msg) > 80)?'...':'');
  264: 	    if (defined($output)) { $output.='<br />'; }
  265:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
  266: 	       $short_msg.'</a>';
  267:        }
  268:     }
  269:     return $output;
  270: }
  271: 
  272: sub listcell {
  273:     my ($items_ref)=@_;
  274:     my $output='';
  275:     my @items=&order($items_ref);
  276:     foreach my $item (@items) {
  277:         if (ref($item)) {
  278: 	    my ($courseid,$start,$end,$msg)=@$item;
  279: 	    my $fullmsg=&Apache::lonlocal::locallocaltime($start);
  280: 	    if ($start!=$end) {
  281: 		$fullmsg.=&mt(' to ').
  282: 		    &Apache::lonlocal::locallocaltime($end);
  283: 	    }
  284:             $fullmsg.=':<br /><b>'.&display_msg($msg).'</b>';
  285:             $output.='<li>'.$fullmsg.'</li>';
  286:        }
  287:     }
  288:     return $output;
  289: }
  290: 
  291: sub order {
  292:     my ($items)=@_;
  293:     return sort {
  294: 	my ($astart,$aend)=$a->[1,2];
  295: 	my ($bstart,$bend)=$b->[1,2];
  296: 	if ($astart != $bstart) {
  297: 	    return $astart <=> $bstart;
  298: 	}
  299: 	return $aend <=> $bend;
  300:     } @$items;
  301: }
  302: 
  303: sub nextday {
  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:     }
  340:     return (&Apache::loncommon::maketime(%th),$th{'month'});
  341: }
  342: 
  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: 
  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'})) {
  365: 	my $displayurl=&Apache::lonnet::gettitle($msg->{'url'});
  366: 	if ($msg->{'url'}!~/\Q$displayurl\E$/) {
  367: 	    $output .= ' - '.$displayurl;
  368: 	}
  369:     }
  370:     if (exists($msg->{'symb'})) {
  371: 	my $displaysymb=&Apache::lonnet::gettitle($msg->{'symb'});
  372: 	if ($msg->{'symb'}!~/\Q$displaysymb\E$/) {
  373: 	    $output .= ' - '.$displaysymb;
  374: 	}
  375:     }
  376:     $output .= ' ('.$msg->{'section'}.') ';
  377:     return $output;
  378: }
  379: 
  380: sub showday {
  381:     my ($tk,$mode,%allcal)=@_;
  382:     my %th=&Apache::loncommon::timehash($tk);
  383:     my ($nextday,$nextmonth)=&nextday($tk,%th);
  384:     my @outp;
  385:     if ($mode) {
  386: 	my $oneday=24*3600;
  387: 	$tk-=$oneday;
  388: 	$nextday+=$oneday;
  389:     }
  390:     foreach my $item (keys(%allcal)) {
  391: 	my ($courseid,$startdate,$enddate)= split("\0",$item);
  392: 	if (($startdate<$nextday) && ($enddate>=$tk))  {
  393: 	    push(@outp,[$courseid,$startdate,$enddate,$allcal{$item}]);
  394:         }
  395:     }
  396:     unless ($mode) {
  397:        return ($nextday,$nextmonth,&normalcell(
  398:                $th{'day'},$th{'month'},$th{'year'},\@outp));
  399:    } elsif (@outp) {
  400:        if ($mode==1) {
  401:           return '<br />'.&plaincell(\@outp);
  402:       } else {
  403:           return '<ul>'.&listcell(\@outp).'</ul>';
  404:       }
  405:    } else {
  406:        return '';
  407:    }
  408: }
  409: 
  410: sub picklink {
  411:     my ($text,$day,$month,$year)=@_;
  412:     if ($env{'form.pickdate'} eq 'yes') {
  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);
  422: <script type="text/javascript" language="JavaScript">
  423: // <![CDATA[
  424: function dialin(day,month,year) {
  425: 	opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
  426:     var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
  427:     var i;
  428:     for (i=0;i<slct.length;i++) {
  429:         if (slct.options[i].value==month) { slct.selectedIndex=i; }
  430:     }
  431:     opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
  432:     opener.$env{'form.element'}\_checkday();
  433:     self.close();
  434: }
  435: // ]]>
  436: </script>
  437: ENDDIA
  438: }
  439: # ----------------------------------------------------- Summarize all calendars
  440: sub get_all_calendars {
  441:     my %allcal=();
  442:     my %courses = &Apache::loncommon::findallcourses();
  443:     foreach my $course (sort(keys(%courses))) {
  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)) {
  459: 	my ($courseid,$startdate,$enddate)= split('\0',$event);
  460: 	my $uid=$event;
  461: 	$uid=~s/[\W\_]/-/gs;
  462: 	$uid.='@loncapa';
  463: 	my $summary=&display_msg($allcal{$event});
  464: 	$summary=~s/\s+/ /gs;
  465:         $summary=$env{'course.'.$courseid.'.description'}.': '.$summary;
  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);
  469: 	$r->print("SUMMARY:$summary$crlf");
  470: 	$r->print("UID:$uid$crlf");
  471: 	$r->print("END:VEVENT$crlf");
  472:     }
  473: # Footer
  474:     $r->print("END:VCALENDAR$crlf");
  475: }
  476: 
  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: 
  484: sub handler {
  485:     my $r = shift;
  486:     if ($r->uri=~/\.(ics|ical)$/) {
  487:         &Apache::loncommon::content_type($r,'text/calendar');
  488: 	&output_ics_file($r);
  489: 	return OK;
  490:     }
  491:     &Apache::loncommon::content_type($r,'text/html');
  492:     $r->send_http_header;
  493:     return OK if $r->header_only;
  494: 
  495: # ---------------------------------------------------------- Get time right now
  496:     my $today=time;
  497:     %todayhash=&Apache::loncommon::timehash($today);
  498: # ----------------------------------------------------------------- Check marks
  499:     undef(%showedcheck);
  500: # ---------------------------------------------------------- Get month and year
  501:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  502:                              ['month','year','pickdate','formname','element']);
  503: # --------------------------------------------------- Decide what month to show
  504:     my $year=$todayhash{'year'};
  505:     if ($env{'form.year'}) { $year=$env{'form.year'}; }
  506:     my $month=$todayhash{'month'};
  507:     if ($env{'form.month'}) { $month=$env{'form.month'}; }
  508: 
  509: # ---------------------------------------------- See if we are in pickdate mode
  510:     my $pickdatemode=($env{'form.pickdate'} eq 'yes');
  511:     my $pickinfo='&amp;pickdate=yes&amp;formname='.$env{'form.formname'}.
  512: 	'&amp;element='.$env{'form.element'};
  513: # --------------------------------------------- Find out first day of the month
  514: 
  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);
  522:     my $weekday=$firstday{'weekday'};
  523: 
  524: # ------------------------------------------------------------ Print the screen
  525:     my $js = <<ENDDOCUMENT;
  526: <script type="text/javascript" language="JavaScript">
  527: // <![CDATA[
  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:     }
  538: // ]]>
  539: </script>
  540: ENDDOCUMENT
  541: 
  542:     if ($pickdatemode) {
  543: # no big header in pickdate mode
  544: 	$r->print(&Apache::loncommon::start_page("Pick a Date",$js,
  545: 						 {'only_body' => 1,}).
  546: 		  &dialscript().
  547: 		  '<font size="1">');
  548:     } else {
  549:         my $brcrum = [{href=>"/adm/announcements",text=>"Announcements and Calendar"}];
  550:         $r->print(&Apache::loncommon::start_page("Communication",$js,{'bread_crumbs' => $brcrum}));
  551:     }
  552: # does this user have privileges to post, etc?
  553:     my $allowed=0;
  554:     if ($env{'request.course.id'}) {
  555:        $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
  556:     }
  557: # does this user have privileges to post to servers?
  558:     my $serverpost=0;
  559:     if ($env{'request.role.domain'}) {
  560: 	$serverpost=&Apache::lonnet::allowed('psa',
  561: 					     $env{'request.role.domain'});
  562:     } else {
  563: 	$serverpost=&Apache::lonnet::allowed('psa','/');
  564:     }
  565: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
  566:     if ($pickdatemode) { 
  567: 	$serverpost=0; 
  568: 	$allowed=0;
  569:     }
  570: # ------------------------------------------------------------ Process commands
  571:     if ($serverpost) {
  572: 	if ($env{'form.serveraction'}) {
  573: 	    foreach my $key (keys(%env)) {
  574: 		if ($key=~/^form\.postto\_(\w+)/) {
  575: 		    $r->print( 
  576: 			'<br />Posting '.$1.': '.&Apache::lonnet::postannounce
  577: 			($1,$env{'form.serverannnounce'}));
  578: 		}
  579: 	    }
  580: 	}
  581: 	$r->print('<form name="serveranno" method="post">'
  582:                  .'<h2>'.&mt('Post Server Announcements').'</h2>'
  583:                  .&mt('Post announcements to the system login and roles screen').'<br />'
  584:                  .'<i>'.&mt('(leave blank to delete announcement)').'</i><br />'
  585:                  .'<textarea name="serverannnounce" cols="60" rows="5"></textarea><br />'
  586:                  .&mt('Check machines:').'<br />'
  587:         );
  588: # list servers
  589:     my %hostname = &Apache::lonnet::all_hostnames();
  590:     foreach my $host (sort(keys(%hostname))) {
  591: 	if (&Apache::lonnet::allowed('psa',
  592: 				     &Apache::lonnet::host_domain($host))) {
  593: 	    $r->print ('<label><input type="checkbox" name="postto_'.$host.'" /> '.
  594: 		       $host.' <tt>'.$hostname{$host}.'</tt> '.
  595: 		       '</label><a href="http://'.$hostname{$host}.
  596: 		       '/announcement.txt?time='.time.'" target="annowin">'.
  597:                        &mt('Current Announcement').'</a><br />');
  598: 	}
  599:     }
  600:     $r->print(
  601:   '<br /><input type="submit" name="serveraction" value="'.&mt('Post').'" /></form><hr />');
  602:     }
  603:     if ($allowed) {
  604:         my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
  605:         my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  606:         my $crstype = &Apache::loncommon::course_type();
  607: # ----------------------------------------------------- Store new submitted one
  608:         if ($env{'form.action'} eq 'new') {
  609: 	    my $startdate = 
  610: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
  611: 	    my $enddate   = 
  612: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
  613: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
  614:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
  615:             if ($startdate>$enddate) {
  616: 		my $buffer=$startdate;
  617: 		$startdate=$enddate;
  618: 		$enddate=$buffer;
  619:             }
  620: 	    &Apache::lonnet::put('calendar',{ 
  621: 		$startdate.'_'.$enddate => 
  622: 		    $env{'form.msg'} },$coursedom,$coursenum);
  623: 	    if ($env{'form.rsspost'}) {
  624:                my $feed;
  625:                if ($crstype eq 'Community') {
  626:                    $feed = 'Community_Announcements';
  627:                } else {
  628:                    $feed = 'Course_Announcements';
  629:                }
  630:                &Apache::lonrss::addentry($coursenum,$coursedom,$feed,
  631: 					 &mt('Event from [_1] to [_2]',
  632: 					     &Apache::lonlocal::locallocaltime($startdate),
  633: 					     &Apache::lonlocal::locallocaltime($enddate)),
  634: 					 $env{'form.msg'},'/adm/announcements','public');
  635: 	   }
  636:         }
  637: # ---------------------------------------------------------------- Remove items
  638:         if ($env{'form.action'} eq 'del') {
  639: 	    my @delwhich=();
  640:             foreach my $key (keys(%env)) {
  641: 		if ($key=~/^form\.remove\_(.+)$/) {
  642: 		    push(@delwhich,$1);
  643:                 }
  644:             }
  645:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
  646:         }
  647: # -------------------------------------------------------- Form to post new one
  648:         my %tomorrowhash=%todayhash;
  649:         $tomorrowhash{'day'}++;
  650:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
  651:         
  652:         &editfield($r,$today,$tomorrow,'',$crstype);
  653:     }
  654: # ----------------------------------------------------- Summarize all calendars
  655:     my %allcal=&get_all_calendars();
  656: # ------------------------------- Initialize table and forward backward buttons
  657:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
  658:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
  659:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
  660: 
  661:     $r->print('<h2>'.&mt('Calendar').'</h2>'
  662:              .'<h3>'.('',&mt('January'),&mt('February'),&mt('March'),
  663: 		      &mt('April'),&mt('May'),
  664: 		      &mt('June'),&mt('July'),&mt('August'),
  665:                       &mt('September'),&mt('October'),
  666:                       &mt('November'),&mt('December'))[$month].' '.
  667: 	              $year.' '.&show_timezone().'</h3>');
  668: # Reached the end of times, give up
  669:     if (($year<1970) || ($year>2037)) {
  670: 	$r->print('<p class="LC_warning">'
  671:                  .&mt('No calendar available for this date.')
  672:                  .'</p>'
  673:                  .'<a href="/adm/announcements?month='.$todayhash{'month'}
  674:                  .'&amp;year='.$todayhash{'year'}.'">'.&mt('Current Month').'</a>'
  675: 		 .&Apache::loncommon::end_page());
  676: 	return OK;
  677:     }
  678: 
  679:     my $class = "LC_calendar";
  680:     if ($env{'form.pickdate'} eq 'yes') {
  681: 	$class .= " LC_calendar_pickdate";
  682:     }
  683: # ------------------------------------------------ Determine first day of a week
  684:     my $datelocale =  &Apache::lonlocal::getdatelocale();
  685:     my $days_in_week = 7;
  686:     my $startweek = 0;
  687:     if (ref($datelocale)) {
  688:         $startweek = $datelocale->first_day_of_week();
  689:         if ($startweek == $days_in_week)  { $startweek = 0; }
  690:     }
  691:     my @days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  692:     my @localdays;
  693:     if ($startweek == 0) {
  694:         @localdays = @days;
  695:     } else {
  696:         my $endday = $days_in_week - $startweek;
  697:         for (my $i=0; $i<$days_in_week; $i++) {
  698:             if ($i < $endday) {
  699:                 $localdays[$i] = $days[$i+$startweek];
  700:             } else {
  701:                 $localdays[$i] = $days[$i-$endday];
  702:             }
  703:         }
  704:     }
  705: 
  706: # ----------------------------------------------------------- Weekday in locale
  707:     my $loc_weekday = $weekday - $startweek;
  708:     if ($loc_weekday < 0) {
  709:         $loc_weekday += $days_in_week; 
  710:     }
  711: 
  712:     $r->print(
  713:  '<a href="/adm/announcements?month='.$pm.'&amp;year='.$py.
  714:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
  715:  '<a href="/adm/announcements?month='.$fm.'&amp;year='.$fy.
  716:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
  717:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  718:  '&amp;year='.$todayhash{'year'}.
  719:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><div>'.
  720:         '<table class="'.$class.'"><tr>');
  721:     for (my $i=0; $i<@localdays; $i++) {
  722:         $r->print('<th>'.&mt($localdays[$i]).'</th>');
  723:     }
  724:     $r->print('</tr>');
  725: 
  726:     my $outp;
  727:     my $nm;
  728: 
  729: # ---------------------------------------------------------------- Actual table
  730:     $r->print('<tr>');
  731:     for (my $i=0;$i<$loc_weekday;$i++) { $r->print(&emptycell); }
  732:     for (my $i=$loc_weekday;$i<=6;$i++) {
  733:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  734:         $r->print($outp);
  735:     }
  736:     $r->print('</tr>');
  737: 
  738:     my $lastrow = 0;
  739:     my $lastday = 0;
  740:     for (my $k=0;$k<=4;$k++) {
  741:         if (!$lastrow) {
  742:             $r->print('<tr>');
  743:             for (my $i=0;$i<=6;$i++) {
  744:                 if ($lastday) {
  745:                     $outp = &emptycell();
  746:                 } else {
  747:                     my $currtk = $tk;
  748:                     ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  749:                     if ($month!=$nm) { $lastday = 1; }
  750:                 }
  751:                 $r->print($outp);
  752:             }
  753:             if ($lastday) {
  754:                 $lastrow = 1;
  755:             }
  756:             $r->print('</tr>');
  757:         }
  758:     }
  759: # ------------------------------------------------------------------- End table
  760:     $r->print('</table></div>');
  761: # ----------------------------------------------------------------- Check marks
  762:     undef(%showedcheck);
  763: # --------------------------------------------------------------- Remove button
  764:     if ($allowed) { $r->print('<br /><input type="button" onClick="removesub()" value="'.&mt('Remove Checked Entries').'">'.
  765: 			      &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
  766:     $r->print('<p>'.
  767:  '<a href="/adm/announcements?month='.$pm.'&amp;year='.$py.
  768:  ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
  769:  '<a href="/adm/announcements?month='.$fm.'&amp;year='.$fy.
  770:  ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
  771:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  772:  '&amp;year='.$todayhash{'year'}.
  773:  ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
  774:  ($pickdatemode?'</font>':'').
  775:  '<a href="/adm/announcements.ics">'.&mt('Download your Calendar as iCalendar File').'</a>'.
  776:     &Apache::loncommon::end_page());
  777:     return OK;
  778: } 
  779: 
  780: 1;
  781: __END__

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