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

1.1       www         1: # The LearningOnline Network
1.2       www         2: # Announce
1.1       www         3: #
1.16    ! www         4: # $Id: lonannounce.pm,v 1.15 2003/06/18 20:06:29 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'},
                    188:                                             ['month','year']);
                    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'}; }
                    194: # --------------------------------------------- Find out first day of the month
                    195: 
                    196:     my %firstday=&Apache::loncommon::timehash(
                    197:        &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
                    198:                                      'year' => $year, 'hours' => 0,
                    199: 				     'minutes' => 0, 'seconds' => 0,
                    200:                                      'dlsav' => $todayhash{'dlsav'} ));
                    201:     my $weekday=$firstday{'weekday'};
                    202: # ------------------------------------------------------------ Print the screen
1.9       www       203: 
1.1       www       204:     $r->print(<<ENDDOCUMENT);
                    205: <html>
                    206: <head>
                    207: <title>The LearningOnline Network with CAPA</title>
1.3       www       208: <script>
                    209: 
                    210:     function trysubmit() {
                    211:         document.anno.action.value="new";
                    212: 	document.anno.submit();
                    213:     }
                    214: 
                    215:     function removesub() {
                    216:         document.anno.action.value="del";
                    217: 	document.anno.submit();
                    218:     }
                    219: </script>
1.1       www       220: </head>
                    221: ENDDOCUMENT
1.6       www       222:     $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
1.3       www       223: # does this user have privileges to post, etc?
                    224:     my $allowed=0;
                    225:     if ($ENV{'request.course.id'}) {
                    226:        $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
                    227:     }
                    228: 
                    229:     if ($allowed) {
                    230:         my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    231:         my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    232: # ----------------------------------------------------- Store new submitted one
                    233:         if ($ENV{'form.action'} eq 'new') {
1.9       www       234: 	    my $startdate = 
                    235: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
                    236: 	    my $enddate   = 
                    237: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
                    238: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
                    239:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
                    240:             if ($startdate>$enddate) {
                    241: 		my $buffer=$startdate;
                    242: 		$startdate=$enddate;
                    243: 		$enddate=$buffer;
                    244:             }
1.3       www       245: 	    &Apache::lonnet::put('calendar',{ 
1.9       www       246: 		$startdate.'_'.$enddate => 
1.3       www       247: 		    $ENV{'form.msg'} },$coursedom,$coursenum);
                    248:         }
                    249: # ---------------------------------------------------------------- Remove items
                    250:         if ($ENV{'form.action'} eq 'del') {
                    251: 	    my @delwhich=();
                    252:             foreach (keys %ENV) {
                    253: 		if ($_=~/^form\.remove\_(.+)$/) {
                    254: 		    push(@delwhich,$1);
                    255:                 }
                    256:             }
                    257:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
                    258:         }
                    259: # -------------------------------------------------------- Form to post new one
                    260:         my %tomorrowhash=%todayhash;
                    261:         $tomorrowhash{'day'}++;
                    262:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
                    263:         
1.10      www       264:         &editfield($r,$today,$tomorrow,'');
1.3       www       265:     }
1.5       www       266: # ----------------------------------------------------- Summarize all calendars
                    267:     my %allcal=();
                    268:     foreach (&Apache::loncommon::findallcourses()) {
                    269: 	%allcal=(%allcal,&readcalendar($_));
                    270:     }
                    271: 
                    272: # ------------------------------- Initialize table and forward backward buttons
1.3       www       273:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
                    274:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
                    275:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
1.14      www       276: 
1.3       www       277:     $r->print('<h1>'.('','January','February','March','April','May',
                    278: 		      'June','July','August','September','October',
1.13      www       279:                       'November','December')[$month].' '.$year.'</h1>');
                    280: # Reached the end of times, give up
                    281:     if (($year<1970) || ($year>2037)) {
                    282: 	$r->print('<h3>No calendar available for this date.</h3>'.
                    283:  '<a href="/adm/announcements?month='.$todayhash{'month'}.
                    284:  '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
                    285: 	return OK;
                    286:     }
                    287:     $r->print(
1.3       www       288:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
1.12      www       289:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
                    290:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
                    291:  '&year='.$todayhash{'year'}.'">Current Month</a><p>'.
1.3       www       292:         '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
1.7       matthew   293:         '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
1.3       www       294: 
                    295:     my $tk=&Apache::loncommon::maketime(%firstday);
                    296:     my $outp;
                    297:     my $nm;
                    298: 
                    299: # ---------------------------------------------------------------- Actual table
                    300:     $r->print('<tr>');
                    301:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
                    302:     for (my $i=$weekday;$i<=6;$i++) { 
1.11      www       303:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       304:         $r->print($outp);
                    305:     }
                    306:     $r->print('</tr>');
                    307: 
                    308:     for (my $k=0;$k<=3;$k++) {
                    309:         $r->print('<tr>');
                    310:         for (my $i=0;$i<=6;$i++) {
1.11      www       311:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
1.3       www       312:             if ($month!=$nm) { $outp=&emptycell; }
                    313:             $r->print($outp);
                    314:         }
                    315:         $r->print('</tr>');
                    316:     }
                    317: # ------------------------------------------------------------------- End table
1.7       matthew   318:     $r->print('</table>');
1.16    ! www       319: # ----------------------------------------------------------------- Check marks
        !           320:     %showedcheck=();
        !           321:     undef %showedcheck;
        !           322: # --------------------------------------------------------------- Remove button
1.9       www       323:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
1.7       matthew   324:     $r->print('<p>'.
1.3       www       325:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
1.12      www       326:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
                    327:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
                    328:  '&year='.$todayhash{'year'}.'">Current Month</a></p>'.
1.3       www       329:  '</body></html>');
1.1       www       330:     return OK;
                    331: } 
                    332: 
                    333: 1;
                    334: __END__

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