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