File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.17: download - view: text, annotated - select for diffs
Tue Aug 12 19:46:04 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Bug #815: Frontpage announcements can now be entered by
Superusers and Domain Coordinators
on a per-machine-base from "CLDR (Calendar/Announce)".

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

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