1: # The LearningOnline Network
2: # Announce
3: #
4: # $Id: lonannounce.pm,v 1.50 2006/04/22 16:35:31 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
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.
14: #
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:
29: package Apache::lonannounce;
30:
31: use strict;
32: use Apache::Constants qw(:common);
33: use Apache::loncommon;
34: use Apache::lonhtmlcommon();
35: use Apache::lonlocal;
36: use Apache::lonnavmaps();
37: use Apache::lonrss();
38: use Apache::lonnet;
39: use HTML::Entities();
40:
41: my %todayhash;
42: my %showedcheck;
43:
44: sub editfield {
45: my ($r,$start,$end,$text)=@_;
46: # Deal with date forms
47: my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
48: 'startdate',
49: $start);
50: my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
51: 'enddate',
52: $end);
53: my $help=&Apache::loncommon::help_open_menu('','Calendar Add Announcement','Calendar_Add_Announcement','',274,'Communication Tools');
54: my %lt=&Apache::lonlocal::texthash('post' => 'Post Announcement',
55: 'start' => 'Starting date',
56: 'end' => 'Ending date',
57: 'incrss' => 'Include in course RSS newsfeed');
58:
59: $r->print(<<ENDFORM);
60: $help
61: <form name="anno" method="post">
62: <input type="hidden" value='' name="action" />
63: <table><tr><td>$lt{'start'}:</td><td>$startdateform</td></tr>
64: <tr><td>$lt{'end'}:</td><td>$enddateform</td></tr></table>
65: <textarea name="msg" rows="4" cols="60">$text</textarea>
66: <br />
67: <label><input type="checkbox" name="rsspost" /> $lt{'incrss'}</label>
68: <br /><input type="button" onClick="trysubmit()" value="$lt{'post'}" /><hr />
69: ENDFORM
70: }
71:
72: sub readcalendar {
73: my $courseid=shift;
74: my $coursenum=$env{'course.'.$courseid.'.num'};
75: my $coursedom=$env{'course.'.$courseid.'.domain'};
76: my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
77: my %returnhash=();
78: foreach my $item (keys(%thiscal)) {
79: unless (($item=~/^error\:/) || ($thiscal{$item}=~/^error\:/)) {
80: $returnhash{$courseid.'@'.$item}=$thiscal{$item};
81: }
82: }
83:
84: if ($courseid eq $env{'request.course.id'}) {
85: my $can_see_hidden = $env{'request.role.adv'};
86: my $navmap = Apache::lonnavmaps::navmap->new();
87: my %resourcedata=
88: &Apache::lonnet::dump('resourcedata',$coursedom,$coursenum);
89: foreach my $thiskey (sort keys %resourcedata) {
90: if ($resourcedata{$thiskey.'.type'}=~/^date/) {
91: my ($course,$middle,$part,$name)=
92: ($thiskey=~/^(\w+)\.(?:(.+)\.)*([\w\s]+)\.(\w+)$/);
93: my $section=&mt('All Students');
94: if ($middle=~/^\[(.*)\]\./) {
95: my $sec=$1;
96: # if we have a section don't show ones that aren't ours
97: if ($env{'request.course.sec'} &&
98: $env{'request.course.sec'} ne $sec) { next; }
99: # if a student without a section don't show any section ones
100: if (!$env{'request.role.adv'} &&
101: !$env{'request.course.sec'}) { next; }
102: $section=&mt('Group/Section').': '.$1;
103: $middle=~s/^\[(.*)\]\.//;
104: }
105: $middle=~s/\.$//;
106: my $realm=&mt('All Resources');
107: if ($middle=~/^(.+)\_\_\_\(all\)$/) {
108: my $map_url=$1;
109: if (!$can_see_hidden && !$navmap) {
110: next;
111: }
112: if (!$can_see_hidden) {
113: my $res = $navmap->getResourceByUrl($map_url);
114: if ($res && $res->randomout()) { next; }
115: }
116: $realm=&mt('Folder/Map').': '.&Apache::lonnet::gettitle($map_url);
117: } elsif ($middle) {
118: if (!$can_see_hidden && !$navmap) {
119: next;
120: }
121: if (!$can_see_hidden) {
122: my $res = $navmap->getBySymb($middle);
123: if ($res && $res->randomout()) { next; }
124: }
125: $realm=&mt('Resource').': '.&Apache::lonnet::gettitle($middle);
126: }
127: my $datetype='';
128: if ($name eq 'duedate') {
129: $datetype=&mt('Due');
130: # see if accidentally answerdate is before duedate
131: my $answerkey=$thiskey;
132: $answerkey=~s/duedate$/answerdate/;
133: if ($resourcedata{$thiskey}>$resourcedata{$answerkey}) {
134: $datetype='Due and Answer Available';
135: }
136: }
137: if ($name eq 'opendate') { $datetype=&mt('Opening'); }
138: if ($name eq 'answerdate') {
139: # see if accidentally answerdate is before duedate
140: my $duekey=$thiskey;
141: $duekey=~s/answerdate$/duedate/;
142: if ($resourcedata{$duekey}>$resourcedata{$thiskey}) {
143: # forget it
144: next;
145: }
146: $datetype=&mt('Answer Available');
147: }
148: $returnhash{$courseid.'@'.$resourcedata{$thiskey}.'_'.
149: $resourcedata{$thiskey}}=
150: 'INTERNAL:'.$datetype.': '.$realm.' ('.$section.')';
151: }
152: }
153: }
154: return %returnhash;
155: }
156:
157: sub emptycell {
158: return '<td class="LC_calendar_day_empty"> </td>';
159: }
160:
161: sub normalcell {
162: my ($day,$month,$year,$text)=@_;
163: my $output='';
164: my @items=&order($text);
165: foreach my $item (@items) {
166: if ($item) {
167: my $internalflag=0;
168: my ($courseid,$start,$end,$msg)=split(/\@/,$item,4);
169: if ($msg=~/INTERNAL\:/) {
170: $msg=~s/INTERNAL\://gs;
171: $internalflag=1;
172: }
173: my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
174: '\n'.&Apache::lonlocal::locallocaltime($start);
175: if ($start!=$end) {
176: $fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
177: }
178: $fullmsg.=':\n'.$msg;
179: if ($courseid eq $env{'request.course.id'}) {
180: if ((&Apache::lonnet::allowed('srm',$env{'request.course.id'}))
181: && (!$showedcheck{$start.'_'.$end})
182: && ($env{'form.pickdate'} ne 'yes')
183: && (!$internalflag)) {
184: $output.='<input type="checkbox" name="remove_'.$start.'_'.
185: $end.'">';
186: $showedcheck{$start.'_'.$end}=1;
187: }
188: }
189: $fullmsg=~s/[\n\r]/\\n/gs;
190: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
191: $fullmsg=~s/&/\\&/g;
192: my $short_msg = substr($msg,0,20).((length($msg) > 20)?'...':'');
193: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
194: $short_msg.'</a><br />';
195: }
196: }
197: return '<td class="LC_calendar_day'.
198: ((($day eq $todayhash{'day'}) &&
199: ($month eq $todayhash{'month'}) &&
200: ($year eq $todayhash{'year'}))?'_current':'').
201: '" ><b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output.'</td>';
202: }
203:
204: sub plaincell {
205: my ($text)=@_;
206: my $output='';
207: my @items=&order($text);
208: foreach my $item (@items) {
209: if ($item) {
210: my ($courseid,$start,$end,$msg)=split(/\@/,$item,4);
211: my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
212: '\n'.&Apache::lonlocal::locallocaltime($start);
213: if ($start!=$end) {
214: $fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
215: }
216: $msg=~s/INTERNAL\://gs;
217: $fullmsg.=':\n'.$msg;
218: $fullmsg=~s/[\n\r]/\\n/gs;
219: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
220: $fullmsg=~s/&/\\&/g;
221: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
222: substr($msg,0,80).'...</a><br />';
223: }
224: }
225: return $output;
226: }
227:
228: sub listcell {
229: my ($text)=@_;
230: my $output='';
231: my @items=&order($text);
232: foreach my $item (@items) {
233: if ($item) {
234: my ($courseid,$start,$end,$msg)=split(/\@/,$item,4);
235: $msg=~s/INTERNAL\://gs;
236: my $fullmsg=&Apache::lonlocal::locallocaltime($start);
237: if ($start!=$end) {
238: $fullmsg.=&mt(' to ').
239: &Apache::lonlocal::locallocaltime($end);
240: }
241: $fullmsg.=':<br /><b>'.
242: $msg.'</b>';
243: $output.='<li>'.$fullmsg.'</li>';
244: }
245: }
246: return $output;
247: }
248:
249: sub order {
250: my ($text)=@_;
251: my @items = split(/___&&&___/,$text);
252: sort {
253: my (undef,$astart,$aend)=split(/\@/,$a);
254: my (undef,$bstart,$bend)=split(/\@/,$b);
255: if ($astart != $bstart) {
256: return $astart <=> $bstart;
257: }
258: return $aend <=> $bend;
259: } @items;
260: }
261:
262: sub nextday {
263: my %th=@_;
264: $th{'day'}++;
265: return (&Apache::loncommon::maketime(%th),$th{'month'});
266: }
267:
268: sub showday {
269: my ($tk,$mode,%allcal)=@_;
270: my %th=&Apache::loncommon::timehash($tk);
271: my ($nextday,$nextmonth)=&nextday(%th);
272: my $outp='';
273: if ($mode) {
274: my $oneday=24*3600;
275: $tk-=$oneday;
276: $nextday+=$oneday;
277: }
278: foreach my $item (keys(%allcal)) {
279: my ($course,$startdate,$enddate)=($item=~/^(\w+)\@(\d+)\_(\d+)$/);
280: if (($startdate<$nextday) && ($enddate>=$tk)) {
281: $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
282: $allcal{$item};
283: }
284: }
285: unless ($mode) {
286: return ($nextday,$nextmonth,&normalcell(
287: $th{'day'},$th{'month'},$th{'year'},$outp));
288: } elsif ($outp) {
289: if ($mode==1) {
290: return '<br />'.&plaincell($outp);
291: } else {
292: return '<ul>'.&listcell($outp).'</ul>';
293: }
294: } else {
295: return '';
296: }
297: }
298:
299: sub picklink {
300: my ($text,$day,$month,$year)=@_;
301: if ($env{'form.pickdate'} eq 'yes') {
302: return '<a href="javascript:dialin('.$day.','.$month.','.$year.')">'.
303: $text.'</a>';
304: } else {
305: return $text;
306: }
307: }
308:
309: sub dialscript {
310: return (<<ENDDIA);
311: <script language="Javascript">
312: function dialin(day,month,year) {
313: opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
314: var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
315: var i;
316: for (i=0;i<slct.length;i++) {
317: if (slct.options[i].value==month) { slct.selectedIndex=i; }
318: }
319: opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
320: opener.$env{'form.element'}\_checkday();
321: self.close();
322: }
323: </script>
324: ENDDIA
325: }
326:
327: sub handler {
328: my $r = shift;
329: &Apache::loncommon::content_type($r,'text/html');
330: $r->send_http_header;
331: return OK if $r->header_only;
332:
333: # ---------------------------------------------------------- Get time right now
334: my $today=time;
335: %todayhash=&Apache::loncommon::timehash($today);
336: # ----------------------------------------------------------------- Check marks
337: undef(%showedcheck);
338: # ---------------------------------------------------------- Get month and year
339: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
340: ['month','year','pickdate','formname','element']);
341: # --------------------------------------------------- Decide what month to show
342: my $year=$todayhash{'year'};
343: if ($env{'form.year'}) { $year=$env{'form.year'}; }
344: my $month=$todayhash{'month'};
345: if ($env{'form.month'}) { $month=$env{'form.month'}; }
346:
347: # ---------------------------------------------- See if we are in pickdate mode
348: my $pickdatemode=($env{'form.pickdate'} eq 'yes');
349: my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}.
350: '&element='.$env{'form.element'};
351: # --------------------------------------------- Find out first day of the month
352:
353: my %firstday=&Apache::loncommon::timehash(
354: &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
355: 'year' => $year, 'hours' => 0,
356: 'minutes' => 0, 'seconds' => 0,
357: 'dlsav' => -1 ));
358: my $weekday=$firstday{'weekday'};
359: # ------------------------------------------------------------ Print the screen
360: my $js = <<ENDDOCUMENT;
361: <script type="text/javascript">
362:
363: function trysubmit() {
364: document.anno.action.value="new";
365: document.anno.submit();
366: }
367:
368: function removesub() {
369: document.anno.action.value="del";
370: document.anno.submit();
371: }
372: </script>
373: ENDDOCUMENT
374:
375: if ($pickdatemode) {
376: # no big header in pickdate mode
377: $r->print(&Apache::loncommon::start_page("Pick a Date",$js,
378: {'only_body' => 1,}).
379: &dialscript().
380: '<font size="1">');
381: } else {
382: $r->print(&Apache::loncommon::start_page("Announcements and Calendar",
383: $js));
384: }
385: # does this user have privileges to post, etc?
386: my $allowed=0;
387: if ($env{'request.course.id'}) {
388: $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
389: }
390: # does this user have privileges to post to servers?
391: my $serverpost=0;
392: if ($env{'request.role.domain'}) {
393: $serverpost=&Apache::lonnet::allowed('psa',
394: $env{'request.role.domain'});
395: } else {
396: $serverpost=&Apache::lonnet::allowed('psa','/');
397: }
398: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
399: if ($pickdatemode) {
400: $serverpost=0;
401: $allowed=0;
402: }
403: # ------------------------------------------------------------ Process commands
404: if ($serverpost) {
405: if ($env{'form.serveraction'}) {
406: foreach my $key (keys(%env)) {
407: if ($key=~/^form\.postto\_(\w+)/) {
408: $r->print(
409: '<br />Posting '.$1.': '.&Apache::lonnet::postannounce
410: ($1,$env{'form.serverannnounce'}));
411: }
412: }
413: }
414: $r->print(<<SERVERANNOUNCE);
415: <form name="serveranno" method="post">
416: <h3>Post Server Announcements</h3>
417: Post announcements to the system login and roles screen<br />
418: <i>(leave blank to delete announcement)</i><br />
419: <textarea name="serverannnounce" cols="60" rows="5"></textarea><br />
420: Check machines:<br />
421: SERVERANNOUNCE
422: # list servers
423: foreach my $host (sort(keys(%Apache::lonnet::hostname))) {
424: if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$host})) {
425: $r->print ('<br /><input type="checkbox" name="postto_'.$host.'" /> '.
426: $host.' <tt>'.$Apache::lonnet::hostname{$host}.'</tt> '.
427: '<a href="http://'.$Apache::lonnet::hostname{$host}.
428: '/announcement.txt" target="annowin">current</a>');
429: }
430: }
431: $r->print(
432: '<br /><input type="submit" name="serveraction" value="Post"></form><hr />');
433: }
434: if ($allowed) {
435: my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
436: my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
437: # ----------------------------------------------------- Store new submitted one
438: if ($env{'form.action'} eq 'new') {
439: my $startdate =
440: &Apache::lonhtmlcommon::get_date_from_form('startdate');
441: my $enddate =
442: &Apache::lonhtmlcommon::get_date_from_form('enddate');
443: unless ($startdate=~/^\d+$/) { $startdate=time; }
444: unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
445: if ($startdate>$enddate) {
446: my $buffer=$startdate;
447: $startdate=$enddate;
448: $enddate=$buffer;
449: }
450: &Apache::lonnet::put('calendar',{
451: $startdate.'_'.$enddate =>
452: $env{'form.msg'} },$coursedom,$coursenum);
453: if ($env{'form.rsspost'}) {
454: &Apache::lonrss::addentry($coursenum,$coursedom,'Course_Announcements',
455: &mt('Event from [_1] to [_2]',
456: &Apache::lonlocal::locallocaltime($startdate),
457: &Apache::lonlocal::locallocaltime($enddate)),
458: $env{'form.msg'},'/adm/announcements','public');
459: }
460: }
461: # ---------------------------------------------------------------- Remove items
462: if ($env{'form.action'} eq 'del') {
463: my @delwhich=();
464: foreach my $key (keys(%env)) {
465: if ($key=~/^form\.remove\_(.+)$/) {
466: push(@delwhich,$1);
467: }
468: }
469: &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
470: }
471: # -------------------------------------------------------- Form to post new one
472: my %tomorrowhash=%todayhash;
473: $tomorrowhash{'day'}++;
474: my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
475:
476: &editfield($r,$today,$tomorrow,'');
477: }
478: # ----------------------------------------------------- Summarize all calendars
479: my %allcal=();
480: foreach my $course (sort(&Apache::loncommon::findallcourses())) {
481: %allcal=(%allcal,&readcalendar($course));
482: }
483:
484: # ------------------------------- Initialize table and forward backward buttons
485: my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
486: if ($pm<1) { ($pm,$py)=(12,$year-1); }
487: if ($fm>12){ ($fm,$fy)=(1,$year+1); }
488:
489: $r->print('<h1>'.('',&mt('January'),&mt('February'),&mt('March'),
490: &mt('April'),&mt('May'),
491: &mt('June'),&mt('July'),&mt('August'),
492: &mt('September'),&mt('October'),
493: &mt('November'),&mt('December'))[$month].' '.
494: $year.'</h1>');
495: # Reached the end of times, give up
496: if (($year<1970) || ($year>2037)) {
497: $r->print('<h3>No calendar available for this date.</h3>'.
498: '<a href="/adm/announcements?month='.$todayhash{'month'}.
499: '&year='.$todayhash{'year'}.'">Current Month</a>'.
500: &Apache::loncommon::end_page());
501: return OK;
502: }
503:
504: my $class = "LC_calendar";
505: if ($env{'form.pickdate'} eq 'yes') {
506: $class .= " LC_calendar_pickdate";
507: }
508: $r->print(
509: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
510: ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
511: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
512: ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
513: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
514: '&year='.$todayhash{'year'}.
515: ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'.
516: '<table class="'.$class.'" cols="7" rows="5"><tr>
517: <th>'.&mt('Sun').'</th>
518: <th>'.&mt('Mon').'</th>
519: <th>'.&mt('Tue').'</th>
520: <th>'.&mt('Wed').'</th>
521: <th>'.&mt('Thu').'</th>
522: <th>'.&mt('Fri').'</th>
523: <th>'.&mt('Sat').'</th></tr>');
524:
525: my $tk=&Apache::loncommon::maketime(%firstday);
526: my $outp;
527: my $nm;
528:
529: # ---------------------------------------------------------------- Actual table
530: $r->print('<tr>');
531: for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
532: for (my $i=$weekday;$i<=6;$i++) {
533: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
534: $r->print($outp);
535: }
536: $r->print('</tr>');
537:
538: for (my $k=0;$k<=4;$k++) {
539: $r->print('<tr>');
540: for (my $i=0;$i<=6;$i++) {
541: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
542: if ($month!=$nm) { $outp=&emptycell; }
543: $r->print($outp);
544: }
545: $r->print('</tr>');
546: }
547: # ------------------------------------------------------------------- End table
548: $r->print('</table>');
549: # ----------------------------------------------------------------- Check marks
550: undef(%showedcheck);
551: # --------------------------------------------------------------- Remove button
552: if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries">'.
553: &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
554: $r->print('<p>'.
555: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
556: ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
557: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
558: ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
559: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
560: '&year='.$todayhash{'year'}.
561: ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
562: ($pickdatemode?'</font>':'').&Apache::loncommon::end_page());
563: return OK;
564: }
565:
566: 1;
567: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>