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

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

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