1: # The LearningOnline Network
2: # Announce
3: #
4: # $Id: lonannounce.pm,v 1.47 2006/03/21 20:45:35 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 bgcolor="#AAAAAA"> </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: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
193: substr($msg,0,20).'...</a><br />';
194: }
195: }
196: return '<td valign="top"'.
197: ((($day eq $todayhash{'day'}) &&
198: ($month eq $todayhash{'month'}) &&
199: ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
200: '>'.&tfont('<b>'.&picklink($day,$day,$month,$year).'</b><br />'.$output).'</td>';
201: }
202:
203: sub plaincell {
204: my ($text)=@_;
205: my $output='';
206: my @items=&order($text);
207: foreach my $item (@items) {
208: if ($item) {
209: my ($courseid,$start,$end,$msg)=split(/\@/,$item,4);
210: my $fullmsg=&mt('Calendar Announcement for ').$env{'course.'.$courseid.'.description'}.
211: '\n'.&Apache::lonlocal::locallocaltime($start);
212: if ($start!=$end) {
213: $fullmsg.=' - '.&Apache::lonlocal::locallocaltime($end);
214: }
215: $msg=~s/INTERNAL\://gs;
216: $fullmsg.=':\n'.$msg;
217: $fullmsg=~s/[\n\r]/\\n/gs;
218: $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
219: $fullmsg=~s/&/\\&/g;
220: $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
221: substr($msg,0,80).'...</a><br />';
222: }
223: }
224: return $output;
225: }
226:
227: sub listcell {
228: my ($text)=@_;
229: my $output='';
230: my @items=&order($text);
231: foreach my $item (@items) {
232: if ($item) {
233: my ($courseid,$start,$end,$msg)=split(/\@/,$item,4);
234: $msg=~s/INTERNAL\://gs;
235: my $fullmsg=&Apache::lonlocal::locallocaltime($start);
236: if ($start!=$end) {
237: $fullmsg.=&mt(' to ').
238: &Apache::lonlocal::locallocaltime($end);
239: }
240: $fullmsg.=':<br /><b>'.
241: $msg.'</b>';
242: $output.='<li>'.$fullmsg.'</li>';
243: }
244: }
245: return $output;
246: }
247:
248: sub order {
249: my ($text)=@_;
250: my @items = split(/___&&&___/,$text);
251: sort {
252: my (undef,$astart,$aend)=split(/\@/,$a);
253: my (undef,$bstart,$bend)=split(/\@/,$b);
254: if ($astart != $bstart) {
255: return $astart <=> $bstart;
256: }
257: return $aend <=> $bend;
258: } @items;
259: }
260:
261: sub nextday {
262: my %th=@_;
263: $th{'day'}++;
264: return (&Apache::loncommon::maketime(%th),$th{'month'});
265: }
266:
267: sub showday {
268: my ($tk,$mode,%allcal)=@_;
269: my %th=&Apache::loncommon::timehash($tk);
270: my ($nextday,$nextmonth)=&nextday(%th);
271: my $outp='';
272: if ($mode) {
273: my $oneday=24*3600;
274: $tk-=$oneday;
275: $nextday+=$oneday;
276: }
277: foreach my $item (keys(%allcal)) {
278: my ($course,$startdate,$enddate)=($item=~/^(\w+)\@(\d+)\_(\d+)$/);
279: if (($startdate<$nextday) && ($enddate>=$tk)) {
280: $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
281: $allcal{$item};
282: }
283: }
284: unless ($mode) {
285: return ($nextday,$nextmonth,&normalcell(
286: $th{'day'},$th{'month'},$th{'year'},$outp));
287: } elsif ($outp) {
288: if ($mode==1) {
289: return '<br />'.&plaincell($outp);
290: } else {
291: return '<ul>'.&listcell($outp).'</ul>';
292: }
293: } else {
294: return '';
295: }
296: }
297:
298: sub tfont {
299: my $text=shift;
300: if ($env{'form.pickdate'} eq 'yes') {
301: return '<font size="1">'.$text.'</font>';
302: } else {
303: return $text;
304: }
305: }
306:
307: sub picklink {
308: my ($text,$day,$month,$year)=@_;
309: if ($env{'form.pickdate'} eq 'yes') {
310: return '<a href="javascript:dialin('.$day.','.$month.','.$year.')">'.
311: $text.'</a>';
312: } else {
313: return $text;
314: }
315: }
316:
317: sub dialscript {
318: return (<<ENDDIA);
319: <script language="Javascript">
320: function dialin(day,month,year) {
321: opener.document.$env{'form.formname'}.$env{'form.element'}\_year.value=year;
322: var slct=opener.document.$env{'form.formname'}.$env{'form.element'}\_month;
323: var i;
324: for (i=0;i<slct.length;i++) {
325: if (slct.options[i].value==month) { slct.selectedIndex=i; }
326: }
327: opener.document.$env{'form.formname'}.$env{'form.element'}\_day.value=day;
328: opener.$env{'form.element'}\_checkday();
329: self.close();
330: }
331: </script>
332: ENDDIA
333: }
334:
335: sub handler {
336: my $r = shift;
337: &Apache::loncommon::content_type($r,'text/html');
338: $r->send_http_header;
339: return OK if $r->header_only;
340:
341: # ---------------------------------------------------------- Get time right now
342: my $today=time;
343: %todayhash=&Apache::loncommon::timehash($today);
344: # ----------------------------------------------------------------- Check marks
345: %showedcheck=();
346: undef %showedcheck;
347: # ---------------------------------------------------------- Get month and year
348: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
349: ['month','year','pickdate','formname','element']);
350: # --------------------------------------------------- Decide what month to show
351: my $year=$todayhash{'year'};
352: if ($env{'form.year'}) { $year=$env{'form.year'}; }
353: my $month=$todayhash{'month'};
354: if ($env{'form.month'}) { $month=$env{'form.month'}; }
355:
356: # ---------------------------------------------- See if we are in pickdate mode
357: my $pickdatemode=($env{'form.pickdate'} eq 'yes');
358: my $pickinfo='&pickdate=yes&formname='.$env{'form.formname'}.
359: '&element='.$env{'form.element'};
360: # --------------------------------------------- Find out first day of the month
361:
362: my %firstday=&Apache::loncommon::timehash(
363: &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
364: 'year' => $year, 'hours' => 0,
365: 'minutes' => 0, 'seconds' => 0,
366: 'dlsav' => -1 ));
367: my $weekday=$firstday{'weekday'};
368: # ------------------------------------------------------------ Print the screen
369: my $html=&Apache::lonxml::xmlbegin();
370: my $js = <<ENDDOCUMENT;
371: <script type="text/javascript">
372:
373: function trysubmit() {
374: document.anno.action.value="new";
375: document.anno.submit();
376: }
377:
378: function removesub() {
379: document.anno.action.value="del";
380: document.anno.submit();
381: }
382: </script>
383: ENDDOCUMENT
384:
385: if ($pickdatemode) {
386: # no big header in pickdate mode
387: $r->print(&Apache::loncommon::start_page("Pick a Date",$js,
388: {'only_body' => 1,}).
389: &dialscript().
390: '<font size="1">');
391: } else {
392: $r->print(&Apache::loncommon::start_page("Announcements and Calendar",
393: $js));
394: }
395: # does this user have privileges to post, etc?
396: my $allowed=0;
397: if ($env{'request.course.id'}) {
398: $allowed=&Apache::lonnet::allowed('srm',$env{'request.course.id'});
399: }
400: # does this user have privileges to post to servers?
401: my $serverpost=0;
402: if ($env{'request.role.domain'}) {
403: $serverpost=&Apache::lonnet::allowed('psa',
404: $env{'request.role.domain'});
405: } else {
406: $serverpost=&Apache::lonnet::allowed('psa','/');
407: }
408: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
409: if ($pickdatemode) {
410: $serverpost=0;
411: $allowed=0;
412: }
413: # ------------------------------------------------------------ Process commands
414: if ($serverpost) {
415: if ($env{'form.serveraction'}) {
416: foreach my $key (keys(%env)) {
417: if ($key=~/^form\.postto\_(\w+)/) {
418: $r->print(
419: '<br />Posting '.$1.': '.&Apache::lonnet::postannounce
420: ($1,$env{'form.serverannnounce'}));
421: }
422: }
423: }
424: $r->print(<<SERVERANNOUNCE);
425: <form name="serveranno" method="post">
426: <h3>Post Server Announcements</h3>
427: Post announcements to the system login and roles screen<br />
428: <i>(leave blank to delete announcement)</i><br />
429: <textarea name="serverannnounce" cols="60" rows="5"></textarea><br />
430: Check machines:<br />
431: SERVERANNOUNCE
432: # list servers
433: foreach my $host (sort(keys(%Apache::lonnet::hostname))) {
434: if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$host})) {
435: $r->print ('<br /><input type="checkbox" name="postto_'.$host.'" /> '.
436: $host.' <tt>'.$Apache::lonnet::hostname{$host}.'</tt> '.
437: '<a href="http://'.$Apache::lonnet::hostname{$host}.
438: '/announcement.txt" target="annowin">current</a>');
439: }
440: }
441: $r->print(
442: '<br /><input type="submit" name="serveraction" value="Post"></form><hr />');
443: }
444: if ($allowed) {
445: my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
446: my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
447: # ----------------------------------------------------- Store new submitted one
448: if ($env{'form.action'} eq 'new') {
449: my $startdate =
450: &Apache::lonhtmlcommon::get_date_from_form('startdate');
451: my $enddate =
452: &Apache::lonhtmlcommon::get_date_from_form('enddate');
453: unless ($startdate=~/^\d+$/) { $startdate=time; }
454: unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
455: if ($startdate>$enddate) {
456: my $buffer=$startdate;
457: $startdate=$enddate;
458: $enddate=$buffer;
459: }
460: &Apache::lonnet::put('calendar',{
461: $startdate.'_'.$enddate =>
462: $env{'form.msg'} },$coursedom,$coursenum);
463: if ($env{'form.rsspost'}) {
464: &Apache::lonrss::addentry($coursenum,$coursedom,'Course_Announcements',
465: &mt('Event from [_1] to [_2]',
466: &Apache::lonlocal::locallocaltime($startdate),
467: &Apache::lonlocal::locallocaltime($enddate)),
468: $env{'form.msg'},'/adm/announcements','public');
469: }
470: }
471: # ---------------------------------------------------------------- Remove items
472: if ($env{'form.action'} eq 'del') {
473: my @delwhich=();
474: foreach my $key (keys(%env)) {
475: if ($key=~/^form\.remove\_(.+)$/) {
476: push(@delwhich,$1);
477: }
478: }
479: &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
480: }
481: # -------------------------------------------------------- Form to post new one
482: my %tomorrowhash=%todayhash;
483: $tomorrowhash{'day'}++;
484: my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
485:
486: &editfield($r,$today,$tomorrow,'');
487: }
488: # ----------------------------------------------------- Summarize all calendars
489: my %allcal=();
490: foreach my $course (&Apache::loncommon::findallcourses()) {
491: %allcal=(%allcal,&readcalendar($course));
492: }
493:
494: # ------------------------------- Initialize table and forward backward buttons
495: my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
496: if ($pm<1) { ($pm,$py)=(12,$year-1); }
497: if ($fm>12){ ($fm,$fy)=(1,$year+1); }
498:
499: $r->print('<h1>'.('',&mt('January'),&mt('February'),&mt('March'),
500: &mt('April'),&mt('May'),
501: &mt('June'),&mt('July'),&mt('August'),
502: &mt('September'),&mt('October'),
503: &mt('November'),&mt('December'))[$month].' '.
504: $year.'</h1>');
505: # Reached the end of times, give up
506: if (($year<1970) || ($year>2037)) {
507: $r->print('<h3>No calendar available for this date.</h3>'.
508: '<a href="/adm/announcements?month='.$todayhash{'month'}.
509: '&year='.$todayhash{'year'}.'">Current Month</a>'.
510: &Apache::loncommon::end_page());
511: return OK;
512: }
513: $r->print(
514: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
515: ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
516: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
517: ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
518: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
519: '&year='.$todayhash{'year'}.
520: ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a><p>'.
521: '<table border="2" cols="7" rows="5"><tr><th>'.
522: &tfont(&mt('Sun'))
523: .'</th><th>'.
524: &tfont(&mt('Mon'))
525: .'</th><th>'.
526: &tfont(&mt('Tue'))
527: .'</th><th>'.
528: &tfont(&mt('Wed'))
529: .'</th><th>'.
530: &tfont(&mt('Thu'))
531: .'</th><th>'.
532: &tfont(&mt('Fri'))
533: .'</th><th>'.
534: &tfont(&mt('Sat'))
535: .'</th></tr>');
536:
537: my $tk=&Apache::loncommon::maketime(%firstday);
538: my $outp;
539: my $nm;
540:
541: # ---------------------------------------------------------------- Actual table
542: $r->print('<tr>');
543: for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
544: for (my $i=$weekday;$i<=6;$i++) {
545: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
546: $r->print($outp);
547: }
548: $r->print('</tr>');
549:
550: for (my $k=0;$k<=4;$k++) {
551: $r->print('<tr>');
552: for (my $i=0;$i<=6;$i++) {
553: ($tk,$nm,$outp)=&showday($tk,0,%allcal);
554: if ($month!=$nm) { $outp=&emptycell; }
555: $r->print($outp);
556: }
557: $r->print('</tr>');
558: }
559: # ------------------------------------------------------------------- End table
560: $r->print('</table>');
561: # ----------------------------------------------------------------- Check marks
562: %showedcheck=();
563: undef %showedcheck;
564: # --------------------------------------------------------------- Remove button
565: if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries">'.
566: &Apache::loncommon::help_open_topic('Calendar_Remove_Announcement').'</form>'); }
567: $r->print('<p>'.
568: '<a href="/adm/announcements?month='.$pm.'&year='.$py.
569: ($pickdatemode?$pickinfo:'').'">'.&mt('Previous Month').'</a> '.
570: '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
571: ($pickdatemode?$pickinfo:'').'">'.&mt('Next Month').'</a>'.
572: ' <a href="/adm/announcements?month='.$todayhash{'month'}.
573: '&year='.$todayhash{'year'}.
574: ($pickdatemode?$pickinfo:'').'">'.&mt('Current Month').'</a></p>'.
575: ($pickdatemode?'</font>':'').&Apache::loncommon::end_page());
576: return OK;
577: }
578:
579: 1;
580: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>