File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.22: download - view: text, annotated - select for diffs
Sat Oct 4 20:49:40 2003 UTC (20 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Internationalization, and in particular, lonlocal:locallocaltime and
Japanese dates.

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

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