Annotation of loncom/interface/lonannounce.pm, revision 1.18

1.1       www         1: # The LearningOnline Network
1.2       www         2: # Announce
1.1       www         3: #
1.18    ! www         4: # $Id: lonannounce.pm,v 1.17 2003/08/12 19:46:04 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.15      www        35: use HTML::Entities();
1.3       www        36: 
1.12      www        37: my %todayhash;
1.16      www        38: my %showedcheck;
1.12      www        39: 
1.10      www        40: sub editfield {
                     41:     my ($r,$start,$end,$text)=@_;
                     42:     # Deal with date forms
                     43:     my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
                     44:                                                             'startdate',
                     45:                                                             $start);
                     46:     my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
                     47:                                                           'enddate',
                     48:                                                           $end);
                     49: 
                     50:     $r->print(<<ENDFORM);
                     51: <form name="anno" method="post">
                     52: <input type="hidden" value=''          name="action"      >
                     53: <table><tr><td>Starting date:</td><td>$startdateform</td></tr>
                     54: <tr><td>Ending date:</td><td>$enddateform</td></tr></table>
                     55: <textarea name="msg" rows="4" cols="60">$text</textarea>
                     56: <input type="button" onClick="trysubmit()" value="Post Announcement"><hr>
                     57: ENDFORM
                     58: }
                     59: 
1.3       www        60: sub readcalendar {
                     61:     my $courseid=shift;
                     62:     my $coursenum=$ENV{'course.'.$courseid.'.num'};
                     63:     my $coursedom=$ENV{'course.'.$courseid.'.domain'};
                     64:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
                     65:     my %returnhash=();
                     66:     foreach (keys %thiscal) {
                     67:         unless (($_=~/^error\:/) || ($thiscal{$_}=~/^error\:/)) {
                     68: 	   $returnhash{$courseid.'@'.$_}=$thiscal{$_};
                     69:         }
                     70:     }
                     71:     return %returnhash;
                     72: }
                     73: 
                     74: sub emptycell {
                     75:     return '<td bgcolor="#AAAAAA">&nbsp;</td>';
                     76: }
                     77: 
                     78: sub normalcell {
1.12      www        79:     my ($day,$month,$year,$text)=@_;
1.4       www        80:     my $output='';
                     81:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
                     82:         if ($_) {
1.5       www        83: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
                     84:             my $msg=join('@',@msg);
1.11      www        85:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
                     86: 		', '.localtime($start).' - '.localtime($end).': '.$msg;
1.5       www        87:             if ($courseid eq $ENV{'request.course.id'}) {
1.16      www        88:               if ((&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))
                     89:                && (!$showedcheck{$start.'_'.$end})) {
1.5       www        90:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
                     91: 		   $end.'">';
1.16      www        92:                $showedcheck{$start.'_'.$end}=1;
1.5       www        93: 	      }
                     94: 	    }
1.15      www        95:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
                     96:             $fullmsg=~s/&/\\&/g;
1.5       www        97:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.11      www        98: 	       substr($msg,0,20).'...</a><br />';
1.4       www        99:        }
                    100:     }
1.12      www       101:     return '<td'.
                    102: 	((($day eq $todayhash{'day'}) &&
                    103:           ($month eq $todayhash{'month'}) &&
                    104:           ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
                    105:            '><b>'.$day.'</b><br>'.$output.'</td>';
1.3       www       106: }
                    107: 
1.11      www       108: sub plaincell {
                    109:     my ($text)=@_;
                    110:     my $output='';
                    111:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
                    112:         if ($_) {
                    113: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
                    114:             my $msg=join('@',@msg);
                    115:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
                    116: 		', '.localtime($start).' - '.localtime($end).': '.$msg;
1.15      www       117:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
                    118:             $fullmsg=~s/&/\\&/g;
1.11      www       119:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
                    120: 	       substr($msg,0,40).'...</a><br />';
                    121:        }
                    122:     }
                    123:     return $output;
                    124: }
                    125: 
                    126: sub listcell {
                    127:     my ($text)=@_;
                    128:     my $output='';
                    129:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
                    130:         if ($_) {
                    131: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
                    132:             my $msg=join('@',@msg);
                    133:             my $fullmsg=localtime($start).' to '.localtime($end).':<br /><b>'.
                    134:                $msg.'</b>';
                    135:             $output.='<li>'.$fullmsg.'</li>';
                    136:        }
                    137:     }
                    138:     return $output;
                    139: }
                    140: 
1.3       www       141: sub nextday {
                    142:     my %th=@_;
                    143:     $th{'day'}++;
                    144:     return (&Apache::loncommon::maketime(%th),$th{'month'});
                    145: }
                    146: 
                    147: sub showday {
1.11      www       148:     my ($tk,$mode,%allcal)=@_;
1.3       www       149:     my %th=&Apache::loncommon::timehash($tk);
                    150:     my ($nextday,$nextmonth)=&nextday(%th);
                    151:     my $outp='';
1.4       www       152:     my $oneday=24*3600;
1.3       www       153:     foreach (keys %allcal) {
                    154: 	my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
1.4       www       155:         if (($startdate<$nextday) && ($enddate>$tk))  {
1.5       www       156: 	    $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
                    157:             $allcal{$_};
1.3       www       158:         }
                    159:     }
1.11      www       160:     unless ($mode) {
1.12      www       161:        return ($nextday,$nextmonth,&normalcell(
                    162:                $th{'day'},$th{'month'},$th{'year'},$outp));
1.11      www       163:    } elsif ($outp) {
                    164:        if ($mode==1) {
                    165:           return '<br />'.&plaincell($outp);
                    166:       } else {
                    167:           return '<ul>'.&listcell($outp).'</ul>';
                    168:       }
                    169:    } else {
                    170:        return '';
                    171:    }
1.3       www       172: }
1.1       www       173: 
                    174: sub handler {
                    175:     my $r = shift;
                    176:     $r->content_type('text/html');
                    177:     $r->send_http_header;
                    178:     return OK if $r->header_only;
                    179: 
1.3       www       180: # ---------------------------------------------------------- Get time right now
                    181:     my $today=time;
1.12      www       182:     %todayhash=&Apache::loncommon::timehash($today);
1.16      www       183: # ----------------------------------------------------------------- Check marks
                    184:     %showedcheck=();
                    185:     undef %showedcheck;
1.3       www       186: # ---------------------------------------------------------- Get month and year
                    187:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.18    ! www       188:                                             ['month','year','pickdate']);
1.3       www       189: # --------------------------------------------------- Decide what month to show
                    190:     my $year=$todayhash{'year'};
                    191:     if ($ENV{'form.year'}) { $year=$ENV{'form.year'}; }
                    192:     my $month=$todayhash{'month'};
                    193:     if ($ENV{'form.month'}) { $month=$ENV{'form.month'}; }
1.18    ! www       194: 
        !           195: # ---------------------------------------------- See if we are in pickdate mode
        !           196:     my $pickdatemode=($ENV{'form.pickdate'} eq 'yes');
        !           197: 
1.3       www       198: # --------------------------------------------- Find out first day of the month
                    199: 
                    200:     my %firstday=&Apache::loncommon::timehash(
                    201:        &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
                    202:                                      'year' => $year, 'hours' => 0,
                    203: 				     'minutes' => 0, 'seconds' => 0,
                    204:                                      'dlsav' => $todayhash{'dlsav'} ));
                    205:     my $weekday=$firstday{'weekday'};
                    206: # ------------------------------------------------------------ Print the screen
1.9       www       207: 
1.1       www       208:     $r->print(<<ENDDOCUMENT);
                    209: <html>
                    210: <head>
                    211: <title>The LearningOnline Network with CAPA</title>
1.3       www       212: <script>
                    213: 
                    214:     function trysubmit() {
                    215:         document.anno.action.value="new";
                    216: 	document.anno.submit();
                    217:     }
                    218: 
                    219:     function removesub() {
                    220:         document.anno.action.value="del";
                    221: 	document.anno.submit();
                    222:     }
                    223: </script>
1.1       www       224: </head>
                    225: ENDDOCUMENT
1.18    ! www       226:     if ($pickdatemode) {
        !           227: # no big header in pickdate mode
        !           228: 	$r->print(&Apache::loncommon::bodytag("Pick a Date",'','',1).
        !           229: 		  '<font size="1">');
        !           230:     } else {
        !           231:        $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
        !           232:     }
1.3       www       233: # does this user have privileges to post, etc?
                    234:     my $allowed=0;
                    235:     if ($ENV{'request.course.id'}) {
                    236:        $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
                    237:     }
1.17      www       238: # does this user have privileges to post to servers?
                    239:     my $serverpost=0;
                    240:     if ($ENV{'request.role.domain'}) {
                    241: 	$serverpost=&Apache::lonnet::allowed('psa',
                    242: 					     $ENV{'request.role.domain'});
                    243:     } else {
                    244: 	$serverpost=&Apache::lonnet::allowed('psa','/');
                    245:     }
1.18    ! www       246: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
        !           247:     if ($pickdatemode) { 
        !           248: 	$serverpost=0; 
        !           249: 	$allowed=0;
        !           250:     }
1.17      www       251: # ------------------------------------------------------------ Process commands
                    252:     if ($serverpost) {
                    253: 	if ($ENV{'form.serveraction'}) {
                    254: 	    foreach (keys %ENV) {
                    255: 		if ($_=~/^form\.postto\_(\w+)/) {
                    256: 		    $r->print( 
                    257: 			'<br />Posting '.$1.': '.&Apache::lonnet::postannounce
                    258: 			($1,$ENV{'form.serverannnounce'}));
                    259: 		}
                    260: 	    }
                    261: 	}
                    262: 	$r->print(<<SERVERANNOUNCE);
                    263: <form name="serveranno" method="post">
                    264: <h3>Post Server Announcements</h3>
                    265: Post announcements to the system login and roles screen<br />
                    266: <i>(leave blank to delete announcement)</i><br />
                    267: <textarea name="serverannnounce" cols="60" rows="5"></textarea><br />
                    268: Check machines:<br />
                    269: SERVERANNOUNCE
                    270: # list servers
                    271:     foreach (sort keys %Apache::lonnet::hostname) {
                    272: 	if (&Apache::lonnet::allowed('psa',$Apache::lonnet::hostdom{$_})) {
                    273: 	    $r->print ('<br /><input type="checkbox" name="postto_'.$_.'" /> '.
                    274: 		       $_.' <tt>'.$Apache::lonnet::hostname{$_}.'</tt> '.
                    275: 		       '<a href="http://'.$Apache::lonnet::hostname{$_}.
                    276: 		       '/announcement.txt" target="annowin">current</a>');
                    277: 	}
                    278:     }
                    279:     $r->print(
                    280:   '<br /><input type="submit" name="serveraction" value="Post"></form><hr />');
                    281:     }
1.3       www       282:     if ($allowed) {
                    283:         my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    284:         my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    285: # ----------------------------------------------------- Store new submitted one
                    286:         if ($ENV{'form.action'} eq 'new') {
1.9       www       287: 	    my $startdate = 
                    288: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
                    289: 	    my $enddate   = 
                    290: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
                    291: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
                    292:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
                    293:             if ($startdate>$enddate) {
                    294: 		my $buffer=$startdate;
                    295: 		$startdate=$enddate;
                    296: 		$enddate=$buffer;
                    297:             }
1.3       www       298: 	    &Apache::lonnet::put('calendar',{ 
1.9       www       299: 		$startdate.'_'.$enddate => 
1.3       www       300: 		    $ENV{'form.msg'} },$coursedom,$coursenum);
                    301:         }
                    302: # ---------------------------------------------------------------- Remove items
                    303:         if ($ENV{'form.action'} eq 'del') {
                    304: 	    my @delwhich=();
                    305:             foreach (keys %ENV) {
                    306: 		if ($_=~/^form\.remove\_(.+)$/) {
                    307: 		    push(@delwhich,$1);
                    308:                 }
                    309:             }
                    310:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
                    311:         }
                    312: # -------------------------------------------------------- Form to post new one
                    313:         my %tomorrowhash=%todayhash;
                    314:         $tomorrowhash{'day'}++;
                    315:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
                    316:         
1.10      www       317:         &editfield($r,$today,$tomorrow,'');
1.3       www       318:     }
1.5       www       319: # ----------------------------------------------------- Summarize all calendars
                    320:     my %allcal=();
                    321:     foreach (&Apache::loncommon::findallcourses()) {
                    322: 	%allcal=(%allcal,&readcalendar($_));
                    323:     }
                    324: 
                    325: # ------------------------------- Initialize table and forward backward buttons
1.3       www       326:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
                    327:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
                    328:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
1.14      www       329: 
1.3       www       330:     $r->print('<h1>'.('','January','February','March','April','May',
                    331: 		      'June','July','August','September','October',
1.13      www       332:                       'November','December')[$month].' '.$year.'</h1>');
                    333: # Reached the end of times, give up
                    334:     if (($year<1970) || ($year>2037)) {
                    335: 	$r->print('<h3>No calendar available for this date.</h3>'.
                    336:  '<a href="/adm/announcements?month='.$todayhash{'month'}.
                    337:  '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
                    338: 	return OK;
                    339:     }
                    340:     $r->print(
1.18    ! www       341:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
        !           342:  ($pickdatemode?'&pickdate=yes':'').'">Previous Month</a> '.
        !           343:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
        !           344:  ($pickdatemode?'&pickdate=yes':'').'">Next Month</a>'.
1.12      www       345:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18    ! www       346:  '&year='.$todayhash{'year'}.
        !           347:  ($pickdatemode?'&pickdate=yes':'').'">Current Month</a><p>'.
1.3       www       348:         '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
1.7       matthew   349:         '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
1.3       www       350: 
                    351:     my $tk=&Apache::loncommon::maketime(%firstday);
                    352:     my $outp;
                    353:     my $nm;
                    354: 
                    355: # ---------------------------------------------------------------- Actual table
                    356:     $r->print('<tr>');
                    357:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
                    358:     for (my $i=$weekday;$i<=6;$i++) { 
1.11      www       359:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       360:         $r->print($outp);
                    361:     }
                    362:     $r->print('</tr>');
                    363: 
                    364:     for (my $k=0;$k<=3;$k++) {
                    365:         $r->print('<tr>');
                    366:         for (my $i=0;$i<=6;$i++) {
1.11      www       367:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       368:             if ($month!=$nm) { $outp=&emptycell; }
                    369:             $r->print($outp);
                    370:         }
                    371:         $r->print('</tr>');
                    372:     }
                    373: # ------------------------------------------------------------------- End table
1.7       matthew   374:     $r->print('</table>');
1.16      www       375: # ----------------------------------------------------------------- Check marks
                    376:     %showedcheck=();
                    377:     undef %showedcheck;
                    378: # --------------------------------------------------------------- Remove button
1.9       www       379:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
1.7       matthew   380:     $r->print('<p>'.
1.18    ! www       381:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
        !           382:  ($pickdatemode?'&pickdate=yes':'').'">Previous Month</a> '.
        !           383:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
        !           384:  ($pickdatemode?'&pickdate=yes':'').'">Next Month</a>'.
1.12      www       385:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
1.18    ! www       386:  '&year='.$todayhash{'year'}.
        !           387:  ($pickdatemode?'&pickdate=yes':'').'">Current Month</a></p>'.
        !           388:  ($pickdatemode?'</font>':'').
1.3       www       389:  '</body></html>');
1.1       www       390:     return OK;
                    391: } 
                    392: 
                    393: 1;
                    394: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>