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