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

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

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