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