File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.15: download - view: text, annotated - select for diffs
Wed Jun 18 20:06:29 2003 UTC (20 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_0_99_3, HEAD
Bug #1806: apostrophy in announcement produced JavaScript error. I love
escaping the escaped.

    1: # The LearningOnline Network
    2: # Announce
    3: #
    4: # $Id: lonannounce.pm,v 1.15 2003/06/18 20:06:29 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: 
   39: sub editfield {
   40:     my ($r,$start,$end,$text)=@_;
   41:     # Deal with date forms
   42:     my $startdateform = &Apache::lonhtmlcommon::date_setter('anno',
   43:                                                             'startdate',
   44:                                                             $start);
   45:     my $enddateform = &Apache::lonhtmlcommon::date_setter('anno',
   46:                                                           'enddate',
   47:                                                           $end);
   48: 
   49:     $r->print(<<ENDFORM);
   50: <form name="anno" method="post">
   51: <input type="hidden" value=''          name="action"      >
   52: <table><tr><td>Starting date:</td><td>$startdateform</td></tr>
   53: <tr><td>Ending date:</td><td>$enddateform</td></tr></table>
   54: <textarea name="msg" rows="4" cols="60">$text</textarea>
   55: <input type="button" onClick="trysubmit()" value="Post Announcement"><hr>
   56: ENDFORM
   57: }
   58: 
   59: sub readcalendar {
   60:     my $courseid=shift;
   61:     my $coursenum=$ENV{'course.'.$courseid.'.num'};
   62:     my $coursedom=$ENV{'course.'.$courseid.'.domain'};
   63:     my %thiscal=&Apache::lonnet::dump('calendar',$coursedom,$coursenum);
   64:     my %returnhash=();
   65:     foreach (keys %thiscal) {
   66:         unless (($_=~/^error\:/) || ($thiscal{$_}=~/^error\:/)) {
   67: 	   $returnhash{$courseid.'@'.$_}=$thiscal{$_};
   68:         }
   69:     }
   70:     return %returnhash;
   71: }
   72: 
   73: sub emptycell {
   74:     return '<td bgcolor="#AAAAAA">&nbsp;</td>';
   75: }
   76: 
   77: sub normalcell {
   78:     my ($day,$month,$year,$text)=@_;
   79:     my $output='';
   80:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
   81:         if ($_) {
   82: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
   83:             my $msg=join('@',@msg);
   84:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
   85: 		', '.localtime($start).' - '.localtime($end).': '.$msg;
   86:             if ($courseid eq $ENV{'request.course.id'}) {
   87:               if (&Apache::lonnet::allowed('srm',$ENV{'request.course.id'})) {
   88:                $output.='<input type="checkbox" name="remove_'.$start.'_'.
   89: 		   $end.'">';
   90: 	      }
   91: 	    }
   92:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
   93:             $fullmsg=~s/&/\\&/g;
   94:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
   95: 	       substr($msg,0,20).'...</a><br />';
   96:        }
   97:     }
   98:     return '<td'.
   99: 	((($day eq $todayhash{'day'}) &&
  100:           ($month eq $todayhash{'month'}) &&
  101:           ($year eq $todayhash{'year'}))?' bgcolor="#FFFF00"':'').
  102:            '><b>'.$day.'</b><br>'.$output.'</td>';
  103: }
  104: 
  105: sub plaincell {
  106:     my ($text)=@_;
  107:     my $output='';
  108:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
  109:         if ($_) {
  110: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
  111:             my $msg=join('@',@msg);
  112:             my $fullmsg=$ENV{'course.'.$courseid.'.description'}.
  113: 		', '.localtime($start).' - '.localtime($end).': '.$msg;
  114:             $fullmsg=&HTML::Entities::encode($fullmsg,'<>&"\'');
  115:             $fullmsg=~s/&/\\&/g;
  116:             $output.='<a href="javascript:alert('."'$fullmsg'".')">'.
  117: 	       substr($msg,0,40).'...</a><br />';
  118:        }
  119:     }
  120:     return $output;
  121: }
  122: 
  123: sub listcell {
  124:     my ($text)=@_;
  125:     my $output='';
  126:     foreach (split(/\_\_\_\&\&\&\_\_\_/,$text)) {
  127:         if ($_) {
  128: 	    my ($courseid,$start,$end,@msg)=split(/\@/,$_);
  129:             my $msg=join('@',@msg);
  130:             my $fullmsg=localtime($start).' to '.localtime($end).':<br /><b>'.
  131:                $msg.'</b>';
  132:             $output.='<li>'.$fullmsg.'</li>';
  133:        }
  134:     }
  135:     return $output;
  136: }
  137: 
  138: sub nextday {
  139:     my %th=@_;
  140:     $th{'day'}++;
  141:     return (&Apache::loncommon::maketime(%th),$th{'month'});
  142: }
  143: 
  144: sub showday {
  145:     my ($tk,$mode,%allcal)=@_;
  146:     my %th=&Apache::loncommon::timehash($tk);
  147:     my ($nextday,$nextmonth)=&nextday(%th);
  148:     my $outp='';
  149:     my $oneday=24*3600;
  150:     foreach (keys %allcal) {
  151: 	my ($course,$startdate,$enddate)=($_=~/^(\w+)\@(\d+)\_(\d+)$/);
  152:         if (($startdate<$nextday) && ($enddate>$tk))  {
  153: 	    $outp.='___&&&___'.$course.'@'.$startdate.'@'.$enddate.'@'.
  154:             $allcal{$_};
  155:         }
  156:     }
  157:     unless ($mode) {
  158:        return ($nextday,$nextmonth,&normalcell(
  159:                $th{'day'},$th{'month'},$th{'year'},$outp));
  160:    } elsif ($outp) {
  161:        if ($mode==1) {
  162:           return '<br />'.&plaincell($outp);
  163:       } else {
  164:           return '<ul>'.&listcell($outp).'</ul>';
  165:       }
  166:    } else {
  167:        return '';
  168:    }
  169: }
  170: 
  171: sub handler {
  172:     my $r = shift;
  173:     $r->content_type('text/html');
  174:     $r->send_http_header;
  175:     return OK if $r->header_only;
  176: 
  177: # ---------------------------------------------------------- Get time right now
  178:     my $today=time;
  179:     %todayhash=&Apache::loncommon::timehash($today);
  180: 
  181: # ---------------------------------------------------------- Get month and year
  182:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  183:                                             ['month','year']);
  184: # --------------------------------------------------- Decide what month to show
  185:     my $year=$todayhash{'year'};
  186:     if ($ENV{'form.year'}) { $year=$ENV{'form.year'}; }
  187:     my $month=$todayhash{'month'};
  188:     if ($ENV{'form.month'}) { $month=$ENV{'form.month'}; }
  189: # --------------------------------------------- Find out first day of the month
  190: 
  191:     my %firstday=&Apache::loncommon::timehash(
  192:        &Apache::loncommon::maketime( 'day' => 1, 'month'=> $month,
  193:                                      'year' => $year, 'hours' => 0,
  194: 				     'minutes' => 0, 'seconds' => 0,
  195:                                      'dlsav' => $todayhash{'dlsav'} ));
  196:     my $weekday=$firstday{'weekday'};
  197: # ------------------------------------------------------------ Print the screen
  198: 
  199:     $r->print(<<ENDDOCUMENT);
  200: <html>
  201: <head>
  202: <title>The LearningOnline Network with CAPA</title>
  203: <script>
  204: 
  205:     function trysubmit() {
  206:         document.anno.action.value="new";
  207: 	document.anno.submit();
  208:     }
  209: 
  210:     function removesub() {
  211:         document.anno.action.value="del";
  212: 	document.anno.submit();
  213:     }
  214: </script>
  215: </head>
  216: ENDDOCUMENT
  217:     $r->print(&Apache::loncommon::bodytag("Announcements and Calendar"));
  218: # does this user have privileges to post, etc?
  219:     my $allowed=0;
  220:     if ($ENV{'request.course.id'}) {
  221:        $allowed=&Apache::lonnet::allowed('srm',$ENV{'request.course.id'});
  222:     }
  223: 
  224:     if ($allowed) {
  225:         my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
  226:         my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
  227: # ----------------------------------------------------- Store new submitted one
  228:         if ($ENV{'form.action'} eq 'new') {
  229: 	    my $startdate = 
  230: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
  231: 	    my $enddate   = 
  232: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
  233: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
  234:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
  235:             if ($startdate>$enddate) {
  236: 		my $buffer=$startdate;
  237: 		$startdate=$enddate;
  238: 		$enddate=$buffer;
  239:             }
  240: 	    &Apache::lonnet::put('calendar',{ 
  241: 		$startdate.'_'.$enddate => 
  242: 		    $ENV{'form.msg'} },$coursedom,$coursenum);
  243:         }
  244: # ---------------------------------------------------------------- Remove items
  245:         if ($ENV{'form.action'} eq 'del') {
  246: 	    my @delwhich=();
  247:             foreach (keys %ENV) {
  248: 		if ($_=~/^form\.remove\_(.+)$/) {
  249: 		    push(@delwhich,$1);
  250:                 }
  251:             }
  252:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
  253:         }
  254: # -------------------------------------------------------- Form to post new one
  255:         my %tomorrowhash=%todayhash;
  256:         $tomorrowhash{'day'}++;
  257:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
  258:         
  259:         &editfield($r,$today,$tomorrow,'');
  260:     }
  261: # ----------------------------------------------------- Summarize all calendars
  262:     my %allcal=();
  263:     foreach (&Apache::loncommon::findallcourses()) {
  264: 	%allcal=(%allcal,&readcalendar($_));
  265:     }
  266: 
  267: # ------------------------------- Initialize table and forward backward buttons
  268:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
  269:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
  270:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
  271: 
  272:     $r->print('<h1>'.('','January','February','March','April','May',
  273: 		      'June','July','August','September','October',
  274:                       'November','December')[$month].' '.$year.'</h1>');
  275: # Reached the end of times, give up
  276:     if (($year<1970) || ($year>2037)) {
  277: 	$r->print('<h3>No calendar available for this date.</h3>'.
  278:  '<a href="/adm/announcements?month='.$todayhash{'month'}.
  279:  '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
  280: 	return OK;
  281:     }
  282:     $r->print(
  283:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
  284:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
  285:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  286:  '&year='.$todayhash{'year'}.'">Current Month</a><p>'.
  287:         '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
  288:         '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
  289: 
  290:     my $tk=&Apache::loncommon::maketime(%firstday);
  291:     my $outp;
  292:     my $nm;
  293: 
  294: # ---------------------------------------------------------------- Actual table
  295:     $r->print('<tr>');
  296:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
  297:     for (my $i=$weekday;$i<=6;$i++) { 
  298:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  299:         $r->print($outp);
  300:     }
  301:     $r->print('</tr>');
  302: 
  303:     for (my $k=0;$k<=3;$k++) {
  304:         $r->print('<tr>');
  305:         for (my $i=0;$i<=6;$i++) {
  306:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  307:             if ($month!=$nm) { $outp=&emptycell; }
  308:             $r->print($outp);
  309:         }
  310:         $r->print('</tr>');
  311:     }
  312: # ------------------------------------------------------------------- End table
  313:     $r->print('</table>');
  314:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
  315:     $r->print('<p>'.
  316:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
  317:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
  318:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  319:  '&year='.$todayhash{'year'}.'">Current Month</a></p>'.
  320:  '</body></html>');
  321:     return OK;
  322: } 
  323: 
  324: 1;
  325: __END__

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