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

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

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