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