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

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

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