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

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

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