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

1.1       www         1: # The LearningOnline Network
1.2       www         2: # Announce
1.1       www         3: #
1.9     ! www         4: # $Id: lonannounce.pm,v 1.8 2003/04/30 15:49:45 matthew 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.3       www        35: 
                     36: sub readcalendar {
                     37:     my $courseid=shift;
                     38:     my $coursenum=$ENV{'course.'.$courseid.'.num'};
                     39:     my $coursedom=$ENV{'course.'.$courseid.'.domain'};
                     40:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
                     41:     my %returnhash=();
                     42:     foreach (keys %thiscal) {
                     43:         unless (($_=~/^error\:/) || ($thiscal{$_}=~/^error\:/)) {
                     44: 	   $returnhash{$courseid.'@'.$_}=$thiscal{$_};
                     45:         }
                     46:     }
                     47:     return %returnhash;
                     48: }
                     49: 
                     50: sub emptycell {
                     51:     return '<td bgcolor="#AAAAAA">&nbsp;</td>';
                     52: }
                     53: 
                     54: sub normalcell {
                     55:     my ($day,$text)=@_;
1.4       www        56:     my $output='';
                     57:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
                     58:         if ($_) {
1.5       www        59: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
                     60:             my $msg=join('@',@msg);
1.4       www        61:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.': '.$msg;
1.5       www        62:             if ($courseid eq $ENV{'request.course.id'}) {
                     63:               if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
                     64:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
                     65: 		   $end.'">';
                     66: 	      }
                     67: 	    }
                     68:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
1.4       www        69: 	       substr($msg,0,20).'...</a><br>';
                     70:        }
                     71:     }
                     72:     return '<td><b>'.$day.'</b><br>'.$output.'</td>';
1.3       www        73: }
                     74: 
                     75: sub nextday {
                     76:     my %th=@_;
                     77:     $th{'day'}++;
                     78:     return (&Apache::loncommon::maketime(%th),$th{'month'});
                     79: }
                     80: 
                     81: sub showday {
                     82:     my ($tk,%allcal)=@_;
                     83:     my %th=&Apache::loncommon::timehash($tk);
                     84:     my ($nextday,$nextmonth)=&nextday(%th);
                     85:     my $outp='';
1.4       www        86:     my $oneday=24*3600;
1.3       www        87:     foreach (keys %allcal) {
                     88: 	my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
1.4       www        89:         if (($startdate<$nextday) && ($enddate>$tk))  {
1.5       www        90: 	    $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
                     91:             $allcal{$_};
1.3       www        92:         }
                     93:     }
                     94:     return ($nextday,$nextmonth,&normalcell($th{'day'},$outp));
                     95: }
1.1       www        96: 
                     97: sub handler {
                     98:     my $r = shift;
                     99:     $r->content_type('text/html');
                    100:     $r->send_http_header;
                    101:     return OK if $r->header_only;
                    102: 
1.3       www       103: # ---------------------------------------------------------- Get time right now
                    104:     my $today=time;
                    105:     my %todayhash=&Apache::loncommon::timehash($today);
                    106: 
                    107: # ---------------------------------------------------------- Get month and year
                    108:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    109:                                             ['month','year']);
                    110: # --------------------------------------------------- Decide what month to show
                    111:     my $year=$todayhash{'year'};
                    112:     if ($ENV{'form.year'}) { $year=$ENV{'form.year'}; }
                    113:     my $month=$todayhash{'month'};
                    114:     if ($ENV{'form.month'}) { $month=$ENV{'form.month'}; }
                    115: # --------------------------------------------- Find out first day of the month
                    116: 
                    117:     my %firstday=&Apache::loncommon::timehash(
                    118:        &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
                    119:                                      'year' => $year, 'hours' => 0,
                    120: 				     'minutes' => 0, 'seconds' => 0,
                    121:                                      'dlsav' => $todayhash{'dlsav'} ));
                    122:     my $weekday=$firstday{'weekday'};
                    123: # ------------------------------------------------------------ Print the screen
1.9     ! www       124: 
1.1       www       125:     $r->print(<<ENDDOCUMENT);
                    126: <html>
                    127: <head>
                    128: <title>The LearningOnline Network with CAPA</title>
1.3       www       129: <script>
                    130: 
                    131:     function trysubmit() {
                    132:         document.anno.action.value="new";
                    133: 	document.anno.submit();
                    134:     }
                    135: 
                    136:     function removesub() {
                    137:         document.anno.action.value="del";
                    138: 	document.anno.submit();
                    139:     }
                    140: </script>
1.1       www       141: </head>
                    142: ENDDOCUMENT
1.6       www       143:     $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
1.3       www       144: # does this user have privileges to post, etc?
                    145:     my $allowed=0;
                    146:     if ($ENV{'request.course.id'}) {
                    147:        $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
                    148:     }
                    149: 
                    150:     if ($allowed) {
                    151:         my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
                    152:         my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
                    153: # ----------------------------------------------------- Store new submitted one
                    154:         if ($ENV{'form.action'} eq 'new') {
1.9     ! www       155: 	    my $startdate = 
        !           156: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
        !           157: 	    my $enddate   = 
        !           158: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
        !           159: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
        !           160:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
        !           161:             if ($startdate>$enddate) {
        !           162: 		my $buffer=$startdate;
        !           163: 		$startdate=$enddate;
        !           164: 		$enddate=$buffer;
        !           165:             }
1.3       www       166: 	    &Apache::lonnet::put('calendar',{ 
1.9     ! www       167: 		$startdate.'_'.$enddate => 
1.3       www       168: 		    $ENV{'form.msg'} },$coursedom,$coursenum);
                    169:         }
                    170: # ---------------------------------------------------------------- Remove items
                    171:         if ($ENV{'form.action'} eq 'del') {
                    172: 	    my @delwhich=();
                    173:             foreach (keys %ENV) {
                    174: 		if ($_=~/^form\.remove\_(.+)$/) {
                    175: 		    push(@delwhich,$1);
                    176:                 }
                    177:             }
                    178:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
                    179:         }
                    180: # -------------------------------------------------------- Form to post new one
                    181:         my %tomorrowhash=%todayhash;
                    182:         $tomorrowhash{'day'}++;
                    183:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
                    184:         
1.9     ! www       185:     # Deal with date forms
        !           186: 	my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
        !           187:                                                             'startdate',
        !           188:                                                             $today);
        !           189: 	my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
        !           190:                                                           'enddate',
        !           191:                                                           $tomorrow);
        !           192:     #
        !           193: 
1.3       www       194: 	$r->print(<<ENDFORM);
                    195: <form name="anno" method="post">
                    196: <input type="hidden" value=''          name="action"      >
1.9     ! www       197: <table><tr><td>Starting date:</td><td>$startdateform</td></tr>
        !           198: <tr><td>Ending date:</td><td>$enddateform</td></tr></table>
1.3       www       199: <textarea name="msg" rows="4" cols="60">
                    200: </textarea>
                    201: <input type="button" onClick="trysubmit()" value="Post Announcement"><hr>
                    202: ENDFORM
                    203:     }
1.5       www       204: # ----------------------------------------------------- Summarize all calendars
                    205:     my %allcal=();
                    206:     foreach (&Apache::loncommon::findallcourses()) {
                    207: 	%allcal=(%allcal,&readcalendar($_));
                    208:     }
                    209: 
                    210: # ------------------------------- Initialize table and forward backward buttons
1.3       www       211:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
                    212:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
                    213:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
                    214:     $r->print('<h1>'.('','January','February','March','April','May',
                    215: 		      'June','July','August','September','October',
                    216:                       'November','December')[$month].' '.$year.'</h1>'.
                    217:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
                    218:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a><p>'.
                    219:         '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
1.7       matthew   220:         '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
1.3       www       221: 
                    222:     my $tk=&Apache::loncommon::maketime(%firstday);
                    223:     my $outp;
                    224:     my $nm;
                    225: 
                    226: # ---------------------------------------------------------------- Actual table
                    227:     $r->print('<tr>');
                    228:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
                    229:     for (my $i=$weekday;$i<=6;$i++) { 
                    230:         ($tk,$nm,$outp)=&showday($tk,%allcal);
                    231:         $r->print($outp);
                    232:     }
                    233:     $r->print('</tr>');
                    234: 
                    235:     for (my $k=0;$k<=3;$k++) {
                    236:         $r->print('<tr>');
                    237:         for (my $i=0;$i<=6;$i++) {
                    238:             ($tk,$nm,$outp)=&showday($tk,%allcal);
                    239:             if ($month!=$nm) { $outp=&emptycell; }
                    240:             $r->print($outp);
                    241:         }
                    242:         $r->print('</tr>');
                    243:     }
                    244: # ------------------------------------------------------------------- End table
1.7       matthew   245:     $r->print('</table>');
1.9     ! www       246:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
1.7       matthew   247:     $r->print('<p>'.
1.3       www       248:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
                    249:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a><p>'.
                    250:  '</body></html>');
1.1       www       251:     return OK;
                    252: } 
                    253: 
                    254: 1;
                    255: __END__

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