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