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