![]() ![]() | ![]() |
iCalendar working, at least with Apple iCal
# The LearningOnline Network # Announce # # $Id: lonannounce.pm,v 1.53 2006/05/12 15:53:10 www Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # package Apache::lonannounce; use strict; use Apache::Constants qw(:common); use Apache::loncommon; use Apache::lonhtmlcommon(); use Apache::lonlocal; use Apache::lonnavmaps(); use Apache::lonrss(); use Apache::lonnet; use HTML::Entities(); my %todayhash; my %showedcheck; sub editfield { my ($r,$start,$end,$text)=@_; # Deal with date forms my $startdateform = &Apache::lonhtmlcommon::date_setter('anno', 'startdate', $start); my $enddateform = &Apache::lonhtmlcommon::date_setter('anno', 'enddate', $end); my $help=&Apache::loncommon::help_open_menu('','Calendar Add Announcement','Calendar_Add_Announcement','',274,'Communication Tools'); my %lt=&Apache::lonlocal::texthash('post' => 'Post Announcement', 'start' => 'Starting date', 'end' => 'Ending date', 'incrss' => 'Include in course RSS newsfeed'); $r->print(<<ENDFORM); $help <form name="anno" method="post"> <input type="hidden" value='' name="action" /> <table><tr><td>$lt{'start'}:</td><td>$startdateform</td></tr> <tr><td>$lt{'end'}:</td><td>$enddateform</td></tr></table> <textarea name="msg" rows="4" cols="60">$text</textarea> <br /> <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label> <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr /> ENDFORM } sub readcalendar { my $courseid=shift; my $coursenum=$env{'course.'.$courseid.'.num'}; my $coursedom=$env{'course.'.$courseid.'.domain'}; my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum); my %returnhash=(); foreach my $item (keys(%thiscal)) { unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) { $returnhash{$courseid.'@'.$item}=$thiscal{$item}; } } my $can_see_hidden = $env{'request.role.adv'}; my $navmap = Apache::lonnavmaps::navmap->new(); my %resourcedata= &Apache::lonnet::dump('resourcedata',$coursedom,$coursenum); foreach my $thiskey (sort keys %resourcedata) { if ($resourcedata{$thiskey.'.type'}=~/^date/) { my ($course,$middle,$part,$name)= ($thiskey=~/^(\w+)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/); my $section=&mt('All Students'); if ($middle=~/^\[(.*)\]\./) { my $sec=$1; # if we have a section don't show ones that aren't ours if ($env{'request.course.sec'} && $env{'request.course.sec'} ne $sec) { next; } # if a student without a section don't show any section ones if (!$env{'request.role.adv'} && !$env{'request.course.sec'}) { next; } $section=&mt('Group/Section').': '.$1; $middle=~s/^\[(.*)\]\.//; } $middle=~s/\.$//; my $realm=&mt('All Resources'); if ($middle eq '___(all)') { if (!$can_see_hidden && !$navmap) { next; } } elsif ($middle=~/^(.+)\_\_\_\(all\)$/) { my $map_url=$1; if (!$can_see_hidden && !$navmap) { next; } if (!$can_see_hidden) { my $res = $navmap->getResourceByUrl($map_url); if ($res && $res->randomout()) { next; } } $realm=&mt('Folder/Map').': '.&Apache::lonnet::gettitle($map_url); } elsif ($middle) { if (!$can_see_hidden && !$navmap) { next; } if (!$can_see_hidden) { my $res = $navmap->getBySymb($middle); if ($res && $res->randomout()) { next; } } $realm=&mt('Resource').': '.&Apache::lonnet::gettitle($middle); } my $datetype=''; if ($name eq 'duedate') { $datetype=&mt('Due'); # see if accidentally answerdate is before duedate my $answerkey=$thiskey; $answerkey=~s/duedate$/answerdate/; if ($resourcedata{$thiskey}>$resourcedata{$answerkey}) { $datetype=&mt('Due and Answer Available'); } } if ($name eq 'opendate') { $datetype=&mt('Opening'); } if ($name eq 'answerdate') { # see if accidentally answerdate is before duedate my $duekey=$thiskey; $duekey=~s/answerdate$/duedate/; if ($resourcedata{$duekey}>$resourcedata{$thiskey}) { # forget it next; } $datetype=&mt('Answer Available'); } $returnhash{$courseid.'@'.$resourcedata{$thiskey}.'_'. $resourcedata{$thiskey}}= 'INTERNAL:'.$datetype.': '.$realm.' ('.$section.')'; } } return %returnhash; } sub emptycell { return '<td class="LC_calendar_day_empty"> </td>'; } sub normalcell { my ($day,$month,$year,$text)=@_; my $output; my @items=&order($text); foreach my $item (@items) { if ($item) { my $internalflag=0; my ($courseid,$start,$end,$msg)=split(/\@/,$item,4); if ($msg=~/INTERNAL\:/) { $msg=~s/INTERNAL\://gs; $internalflag=1; } my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}. '\n'.&Apache::lonlocal::locallocaltime($start); if ($start!=$end) { $fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end); } $fullmsg.=':\n'.$msg; if ($courseid eq $env{'request.course.id'}) { if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'})) && (!$showedcheck{$start.'_'.$end}) && ($env{'form.pickdate'} ne 'yes') && (!$internalflag)) { $output.='<input type="checkbox" name="remove_'.$start.'_'. $end.'">'; $showedcheck{$start.'_'.$end}=1; } } $fullmsg=~s/[\n\r]/\\n/gs; $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\''); $fullmsg=~s/&/\\&/g; my $short_msg = substr($msg,0,20).((length($msg) > 20)?'...':''); if (defined($output)) { $output.='<br />'; } $output.='<a href="javascript:alert('."'$fullmsg'".')">'. $short_msg.'</a>'; } } return '<td class="LC_calendar_day'. ((($day eq $todayhash{'day'}) && ($month eq $todayhash{'month'}) && ($year eq $todayhash{'year'}))?'_current':''). '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>'; } sub plaincell { my ($text)=@_; my $output; my @items=&order($text); foreach my $item (@items) { if ($item) { my ($courseid,$start,$end,$msg)=split(/\@/,$item,4); my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}. '\n'.&Apache::lonlocal::locallocaltime($start); if ($start!=$end) { $fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end); } $msg=~s/INTERNAL\://gs; $fullmsg.=':\n'.$msg; $fullmsg=~s/[\n\r]/\\n/gs; $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\''); $fullmsg=~s/&/\\&/g; my $short_msg = substr($msg,0,80).((length($msg) > 80)?'...':''); if (defined($output)) { $output.='<br />'; } $output.='<a href="javascript:alert('."'$fullmsg'".')">'. $short_msg.'</a>'; } } return $output; } sub listcell { my ($text)=@_; my $output=''; my @items=&order($text); foreach my $item (@items) { if ($item) { my ($courseid,$start,$end,$msg)=split(/\@/,$item,4); $msg=~s/INTERNAL\://gs; my $fullmsg=&Apache::lonlocal::locallocaltime($start); if ($start!=$end) { $fullmsg.=&mt(' to '). &Apache::lonlocal::locallocaltime($end); } $fullmsg.=':<br /><b>'. $msg.'</b>'; $output.='<li>'.$fullmsg.'</li>'; } } return $output; } sub order { my ($text)=@_; my @items = split(/___&&&___/,$text); sort { my (undef,$astart,$aend)=split(/\@/,$a); my (undef,$bstart,$bend)=split(/\@/,$b); if ($astart != $bstart) { return $astart <=> $bstart; } return $aend <=> $bend; } @items; } sub nextday { my %th=@_; $th{'day'}++; return (&Apache::loncommon::maketime(%th),$th{'month'}); } sub showday { my ($tk,$mode,%allcal)=@_; my %th=&Apache::loncommon::timehash($tk); my ($nextday,$nextmonth)=&nextday(%th); my $outp=''; if ($mode) { my $oneday=24*3600; $tk-=$oneday; $nextday+=$oneday; } foreach my $item (keys(%allcal)) { my ($course,$startdate,$enddate)=($item=~/^(\w+)\@(\d+)\_(\d+)$/); if (($startdate<$nextday) && ($enddate>=$tk)) { $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'. $allcal{$item}; } } unless ($mode) { return ($nextday,$nextmonth,&normalcell( $th{'day'},$th{'month'},$th{'year'},$outp)); } elsif ($outp) { if ($mode==1) { return '<br />'.&plaincell($outp); } else { return '<ul>'.&listcell($outp).'</ul>'; } } else { return ''; } } sub picklink { my ($text,$day,$month,$year)=@_; if ($env{'form.pickdate'} eq 'yes') { return '<a href="javascript:dialin('.$day.','.$month.','.$year.')">'. $text.'</a>'; } else { return $text; } } sub dialscript { return (<<ENDDIA); <script language="Javascript"> function dialin(day,month,year) { opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year; var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month; var i; for (i=0;i<slct.length;i++) { if (slct.options[i].value==month) { slct.selectedIndex=i; } } opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day; opener.$env{'form.element'}\_checkday(); self.close(); } </script> ENDDIA } # ----------------------------------------------------- Summarize all calendars sub get_all_calendars { my %allcal=(); foreach my $course (sort(&Apache::loncommon::findallcourses())) { %allcal=(%allcal,&readcalendar($course)); } return %allcal; } sub output_ics_file { my ($r)=@_; # RFC 2445 wants CRLF my $crlf="\015\012"; # Header $r->print("BEGIN:VCALENDAR$crlf"); $r->print("VERSION:2.0$crlf"); $r->print("PRODID:-//LONCAPA//LONCAPA Calendar Output//EN$crlf"); my %allcal=&get_all_calendars(); foreach my $event (keys(%allcal)) { my ($courseid,$startdate,$enddate)=($event=~/^(\w+)\@(\d+)\_(\d+)$/); my $uid=$event; $uid=~s/[\W\_]/-/gs; $uid.='@loncapa'; my $summary=$allcal{$event}; $summary=~s/^INTERNAL\://; $summary=~s/\s+/ /gs; $summary=$env{'course.'.$courseid.'.description'}.': '.$summary; $r->print("BEGIN:VEVENT$crlf"); $r->print("DTSTART:".&Apache::loncommon::utc_string($startdate).$crlf); $r->print("DTEND:".&Apache::loncommon::utc_string($enddate).$crlf); $r->print("SUMMARY:$summary$crlf"); $r->print("UID:$uid$crlf"); $r->print("END:VEVENT$crlf"); } # Footer $r->print("END:VCALENDAR$crlf"); } sub handler { my $r = shift; if ($r->uri=~/\.(ics|ical)$/) { &Apache::loncommon::content_type($r,'text/calendar'); &output_ics_file($r); return OK; } &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; return OK if $r->header_only; # ---------------------------------------------------------- Get time right now my $today=time; %todayhash=&Apache::loncommon::timehash($today); # ----------------------------------------------------------------- Check marks undef(%showedcheck); # ---------------------------------------------------------- Get month and year &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, ['month','year','pickdate','formname','element']); # --------------------------------------------------- Decide what month to show my $year=$todayhash{'year'}; if ($env{'form.year'}) { $year=$env{'form.year'}; } my $month=$todayhash{'month'}; if ($env{'form.month'}) { $month=$env{'form.month'}; } # ---------------------------------------------- See if we are in pickdate mode my $pickdatemode=($env{'form.pickdate'} eq 'yes'); my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}. '&element='.$env{'form.element'}; # --------------------------------------------- Find out first day of the month my %firstday=&Apache::loncommon::timehash( &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month, 'year' => $year, 'hours' => 0, 'minutes' => 0, 'seconds' => 0, 'dlsav' => -1 )); my $weekday=$firstday{'weekday'}; # ------------------------------------------------------------ Print the screen my $js = <<ENDDOCUMENT; <script type="text/javascript"> function trysubmit() { document.anno.action.value="new"; document.anno.submit(); } function removesub() { document.anno.action.value="del"; document.anno.submit(); } </script> ENDDOCUMENT if ($pickdatemode) { # no big header in pickdate mode $r->print(&Apache::loncommon::start_page("Pick a Date",$js, {'only_body' => 1,}). &dialscript(). '<font size="1">'); } else { $r->print(&Apache::loncommon::start_page("Announcements and Calendar", $js)); } # does this user have privileges to post, etc? my $allowed=0; if ($env{'request.course.id'}) { $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'}); } # does this user have privileges to post to servers? my $serverpost=0; if ($env{'request.role.domain'}) { $serverpost=&Apache::lonnet::allowed('psa', $env{'request.role.domain'}); } else { $serverpost=&Apache::lonnet::allowed('psa','/'); } # -------------------------------- BUT: do no fancy stuff when in pickdate mode if ($pickdatemode) { $serverpost=0; $allowed=0; } # ------------------------------------------------------------ Process commands if ($serverpost) { if ($env{'form.serveraction'}) { foreach my $key (keys(%env)) { if ($key=~/^form\.postto\_(\w+)/) { $r->print( '<br />Posting '.$1.': '.&Apache::lonnet::postannounce ($1,$env{'form.serverannnounce'})); } } } $r->print(<<SERVERANNOUNCE); <form name="serveranno" method="post"> <h3>Post Server Announcements</h3> Post announcements to the system login and roles screen<br /> <i>(leave blank to delete announcement)</i><br /> <textarea name="serverannnounce" cols="60" rows="5"></textarea><br /> Check machines:<br /> SERVERANNOUNCE # list servers foreach my $host (sort(keys(%Apache::lonnet::hostname))) { if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$host})) { $r->print ('<br /><input type="checkbox" name="postto_'.$host.'" /> '. $host.' <tt>'.$Apache::lonnet::hostname{$host}.'</tt> '. '<a href="http://'.$Apache::lonnet::hostname{$host}. '/announcement.txt" target="annowin">current</a>'); } } $r->print( '<br /><input type="submit" name="serveraction" value="Post"></form><hr />'); } if ($allowed) { my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'}; my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'}; # ----------------------------------------------------- Store new submitted one if ($env{'form.action'} eq 'new') { my $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate'); my $enddate = &Apache::lonhtmlcommon::get_date_from_form('enddate'); unless ($startdate=~/^\d+$/) { $startdate=time; } unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; } if ($startdate>$enddate) { my $buffer=$startdate; $startdate=$enddate; $enddate=$buffer; } &Apache::lonnet::put('calendar',{ $startdate.'_'.$enddate => $env{'form.msg'} },$coursedom,$coursenum); if ($env{'form.rsspost'}) { &Apache::lonrss::addentry($coursenum,$coursedom,'Course_Announcements', &mt('Event from [_1] to [_2]', &Apache::lonlocal::locallocaltime($startdate), &Apache::lonlocal::locallocaltime($enddate)), $env{'form.msg'},'/adm/announcements','public'); } } # ---------------------------------------------------------------- Remove items if ($env{'form.action'} eq 'del') { my @delwhich=(); foreach my $key (keys(%env)) { if ($key=~/^form\.remove\_(.+)$/) { push(@delwhich,$1); } } &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum); } # -------------------------------------------------------- Form to post new one my %tomorrowhash=%todayhash; $tomorrowhash{'day'}++; my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash); &editfield($r,$today,$tomorrow,''); } # ----------------------------------------------------- Summarize all calendars my %allcal=&get_all_calendars(); # ------------------------------- Initialize table and forward backward buttons my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year); if ($pm<1) { ($pm,$py)=(12,$year-1); } if ($fm>12){ ($fm,$fy)=(1,$year+1); } $r->print('<h1>'.('',&mt('January'),&mt('February'),&mt('March'), &mt('April'),&mt('May'), &mt('June'),&mt('July'),&mt('August'), &mt('September'),&mt('October'), &mt('November'),&mt('December'))[$month].' '. $year.'</h1>'); # Reached the end of times, give up if (($year<1970) || ($year>2037)) { $r->print('<h3>No calendar available for this date.</h3>'. '<a href="/adm/announcements?month='.$todayhash{'month'}. '&year='.$todayhash{'year'}.'">Current Month</a>'. &Apache::loncommon::end_page()); return OK; } my $class = "LC_calendar"; if ($env{'form.pickdate'} eq 'yes') { $class .= " LC_calendar_pickdate"; } $r->print( '<a href="/adm/announcements?month='.$pm.'&year='.$py. ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '. '<a href="/adm/announcements?month='.$fm.'&year='.$fy. ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'. ' <a href="/adm/announcements?month='.$todayhash{'month'}. '&year='.$todayhash{'year'}. ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'. '<table class="'.$class.'" cols="7" rows="5"><tr> <th>'.&mt('Sun').'</th> <th>'.&mt('Mon').'</th> <th>'.&mt('Tue').'</th> <th>'.&mt('Wed').'</th> <th>'.&mt('Thu').'</th> <th>'.&mt('Fri').'</th> <th>'.&mt('Sat').'</th></tr>'); my $tk=&Apache::loncommon::maketime(%firstday); my $outp; my $nm; # ---------------------------------------------------------------- Actual table $r->print('<tr>'); for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); } for (my $i=$weekday;$i<=6;$i++) { ($tk,$nm,$outp)=&showday($tk,0,%allcal); $r->print($outp); } $r->print('</tr>'); for (my $k=0;$k<=4;$k++) { $r->print('<tr>'); for (my $i=0;$i<=6;$i++) { ($tk,$nm,$outp)=&showday($tk,0,%allcal); if ($month!=$nm) { $outp=&emptycell; } $r->print($outp); } $r->print('</tr>'); } # ------------------------------------------------------------------- End table $r->print('</table>'); # ----------------------------------------------------------------- Check marks undef(%showedcheck); # --------------------------------------------------------------- Remove button if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries">'. &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); } $r->print('<p>'. '<a href="/adm/announcements?month='.$pm.'&year='.$py. ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '. '<a href="/adm/announcements?month='.$fm.'&year='.$fy. ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'. ' <a href="/adm/announcements?month='.$todayhash{'month'}. '&year='.$todayhash{'year'}. ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'. ($pickdatemode?'</font>':'').&Apache::loncommon::end_page()); $r->print('<a href="/adm/announcements.ics">'.&mt('Download your Calendar as iCalendar File').'</a>'); return OK; } 1; __END__