File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.19: download - view: text, annotated - select for diffs
Wed Sep 3 01:53:46 2003 UTC (20 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Use calendar function to more intuitively pick dates.

    1: # The LearningOnline Network
    2: # Announce
    3: #
    4: # $Id: lonannounce.pm,v 1.19 2003/09/03 01:53:46 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 HTML::Entities();
   36: 
   37: my %todayhash;
   38: my %showedcheck;
   39: 
   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: 
   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 {
   79:     my ($day,$month,$year,$text)=@_;
   80:     my $output='';
   81:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
   82:         if ($_) {
   83: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
   84:             my $msg=join('@',@msg);
   85:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
   86: 		', '.localtime($start).' - '.localtime($end).': '.$msg;
   87:             if ($courseid eq $ENV{'request.course.id'}) {
   88:               if ((&Apache::lonnet::allowed('srm',$ENV{'request.course.id'}))
   89:                && (!$showedcheck{$start.'_'.$end})
   90: 	       && ($ENV{'form.pickdate'} ne 'yes')) {
   91:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
   92: 		   $end.'">';
   93:                $showedcheck{$start.'_'.$end}=1;
   94: 	      }
   95: 	    }
   96:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
   97:             $fullmsg=~s/&/\\&/g;
   98:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
   99: 	       substr($msg,0,20).'...</a><br />';
  100:        }
  101:     }
  102:     return '<td'.
  103: 	((($day eq $todayhash{'day'}) &&
  104:           ($month eq $todayhash{'month'}) &&
  105:           ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
  106:            '>'.&tfont('<b>'.&picklink($day,$day,$month,$year).'</b><br>'.$output).'</td>';
  107: }
  108: 
  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;
  118:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
  119:             $fullmsg=~s/&/\\&/g;
  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: 
  142: sub nextday {
  143:     my %th=@_;
  144:     $th{'day'}++;
  145:     return (&Apache::loncommon::maketime(%th),$th{'month'});
  146: }
  147: 
  148: sub showday {
  149:     my ($tk,$mode,%allcal)=@_;
  150:     my %th=&Apache::loncommon::timehash($tk);
  151:     my ($nextday,$nextmonth)=&nextday(%th);
  152:     my $outp='';
  153:     my $oneday=24*3600;
  154:     foreach (keys %allcal) {
  155: 	my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
  156:         if (($startdate<$nextday) && ($enddate>$tk))  {
  157: 	    $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
  158:             $allcal{$_};
  159:         }
  160:     }
  161:     unless ($mode) {
  162:        return ($nextday,$nextmonth,&normalcell(
  163:                $th{'day'},$th{'month'},$th{'year'},$outp));
  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:    }
  173: }
  174: 
  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: 
  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: 
  218: # ---------------------------------------------------------- Get time right now
  219:     my $today=time;
  220:     %todayhash=&Apache::loncommon::timehash($today);
  221: # ----------------------------------------------------------------- Check marks
  222:     %showedcheck=();
  223:     undef %showedcheck;
  224: # ---------------------------------------------------------- Get month and year
  225:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  226:                              ['month','year','pickdate','formname','element']);
  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'}; }
  232: 
  233: # ---------------------------------------------- See if we are in pickdate mode
  234:     my $pickdatemode=($ENV{'form.pickdate'} eq 'yes');
  235:     my $pickinfo='&pickdate=yes&formname='.$ENV{'form.formname'}.
  236: 	'&element='.$ENV{'form.element'};
  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
  246: 
  247:     $r->print(<<ENDDOCUMENT);
  248: <html>
  249: <head>
  250: <title>The LearningOnline Network with CAPA</title>
  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>
  263: </head>
  264: ENDDOCUMENT
  265:     if ($pickdatemode) {
  266: # no big header in pickdate mode
  267: 	$r->print(&Apache::loncommon::bodytag("Pick a Date",'','',1).
  268: 		  &dialscript().
  269: 		  '<font size="1">');
  270:     } else {
  271:        $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
  272:     }
  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:     }
  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:     }
  286: # -------------------------------- BUT: do no fancy stuff when in pickdate mode
  287:     if ($pickdatemode) { 
  288: 	$serverpost=0; 
  289: 	$allowed=0;
  290:     }
  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:     }
  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') {
  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:             }
  338: 	    &Apache::lonnet::put('calendar',{ 
  339: 		$startdate.'_'.$enddate => 
  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:         
  357:         &editfield($r,$today,$tomorrow,'');
  358:     }
  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
  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); }
  369: 
  370:     $r->print('<h1>'.('','January','February','March','April','May',
  371: 		      'June','July','August','September','October',
  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(
  381:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
  382:  ($pickdatemode?$pickinfo:'').'">Previous Month</a> '.
  383:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
  384:  ($pickdatemode?$pickinfo:'').'">Next Month</a>'.
  385:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  386:  '&year='.$todayhash{'year'}.
  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>');
  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++) { 
  412:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  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++) {
  420:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  421:             if ($month!=$nm) { $outp=&emptycell; }
  422:             $r->print($outp);
  423:         }
  424:         $r->print('</tr>');
  425:     }
  426: # ------------------------------------------------------------------- End table
  427:     $r->print('</table>');
  428: # ----------------------------------------------------------------- Check marks
  429:     %showedcheck=();
  430:     undef %showedcheck;
  431: # --------------------------------------------------------------- Remove button
  432:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
  433:     $r->print('<p>'.
  434:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.
  435:  ($pickdatemode?$pickinfo:'').'">Previous Month</a> '.
  436:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.
  437:  ($pickdatemode?$pickinfo:'').'">Next Month</a>'.
  438:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  439:  '&year='.$todayhash{'year'}.
  440:  ($pickdatemode?$pickinfo:'').'">Current Month</a></p>'.
  441:  ($pickdatemode?'</font>':'').
  442:  '</body></html>');
  443:     return OK;
  444: } 
  445: 
  446: 1;
  447: __END__

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