File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.16: download - view: text, annotated - select for diffs
Thu Jul 10 10:27:04 2003 UTC (20 years, 10 months ago) by www
Branches: MAIN
CVS tags: version_1_0_3, version_1_0_2, version_1_0_1, version_1_0_0, version_0_99_5, version_0_99_4, HEAD
Bug #1486: remove confusing checkboxes from any entry but that on the first
day.

    1: # The LearningOnline Network
    2: # Announce
    3: #
    4: # $Id: lonannounce.pm,v 1.16 2003/07/10 10:27: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: 
  229:     if ($allowed) {
  230:         my $coursenum=$ENV{'course.'.$ENV{'request.course.id'}.'.num'};
  231:         my $coursedom=$ENV{'course.'.$ENV{'request.course.id'}.'.domain'};
  232: # ----------------------------------------------------- Store new submitted one
  233:         if ($ENV{'form.action'} eq 'new') {
  234: 	    my $startdate = 
  235: 		&Apache::lonhtmlcommon::get_date_from_form('startdate');
  236: 	    my $enddate   = 
  237: 		&Apache::lonhtmlcommon::get_date_from_form('enddate');
  238: 	    unless ($startdate=~/^\d+$/) { $startdate=time; }
  239:             unless ($enddate=~/^\d+$/) { $enddate=$startdate+1; }
  240:             if ($startdate>$enddate) {
  241: 		my $buffer=$startdate;
  242: 		$startdate=$enddate;
  243: 		$enddate=$buffer;
  244:             }
  245: 	    &Apache::lonnet::put('calendar',{ 
  246: 		$startdate.'_'.$enddate => 
  247: 		    $ENV{'form.msg'} },$coursedom,$coursenum);
  248:         }
  249: # ---------------------------------------------------------------- Remove items
  250:         if ($ENV{'form.action'} eq 'del') {
  251: 	    my @delwhich=();
  252:             foreach (keys %ENV) {
  253: 		if ($_=~/^form\.remove\_(.+)$/) {
  254: 		    push(@delwhich,$1);
  255:                 }
  256:             }
  257:             &Apache::lonnet::del('calendar',\@delwhich,$coursedom,$coursenum);
  258:         }
  259: # -------------------------------------------------------- Form to post new one
  260:         my %tomorrowhash=%todayhash;
  261:         $tomorrowhash{'day'}++;
  262:         my $tomorrow=&Apache::loncommon::maketime(%tomorrowhash);
  263:         
  264:         &editfield($r,$today,$tomorrow,'');
  265:     }
  266: # ----------------------------------------------------- Summarize all calendars
  267:     my %allcal=();
  268:     foreach (&Apache::loncommon::findallcourses()) {
  269: 	%allcal=(%allcal,&readcalendar($_));
  270:     }
  271: 
  272: # ------------------------------- Initialize table and forward backward buttons
  273:     my ($pm,$py,$fm,$fy)=($month-1,$year,$month+1,$year);
  274:     if ($pm<1) { ($pm,$py)=(12,$year-1); }
  275:     if ($fm>12){ ($fm,$fy)=(1,$year+1); }
  276: 
  277:     $r->print('<h1>'.('','January','February','March','April','May',
  278: 		      'June','July','August','September','October',
  279:                       'November','December')[$month].' '.$year.'</h1>');
  280: # Reached the end of times, give up
  281:     if (($year<1970) || ($year>2037)) {
  282: 	$r->print('<h3>No calendar available for this date.</h3>'.
  283:  '<a href="/adm/announcements?month='.$todayhash{'month'}.
  284:  '&year='.$todayhash{'year'}.'">Current Month</a></body></html>');
  285: 	return OK;
  286:     }
  287:     $r->print(
  288:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
  289:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
  290:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  291:  '&year='.$todayhash{'year'}.'">Current Month</a><p>'.
  292:         '<table border="2" cols="7" rows="5"><tr><th>Sun</th><th>Mon</th>'.
  293:         '<th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>');
  294: 
  295:     my $tk=&Apache::loncommon::maketime(%firstday);
  296:     my $outp;
  297:     my $nm;
  298: 
  299: # ---------------------------------------------------------------- Actual table
  300:     $r->print('<tr>');
  301:     for (my $i=0;$i<$weekday;$i++) { $r->print(&emptycell); }
  302:     for (my $i=$weekday;$i<=6;$i++) { 
  303:         ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  304:         $r->print($outp);
  305:     }
  306:     $r->print('</tr>');
  307: 
  308:     for (my $k=0;$k<=3;$k++) {
  309:         $r->print('<tr>');
  310:         for (my $i=0;$i<=6;$i++) {
  311:             ($tk,$nm,$outp)=&showday($tk,0,%allcal);
  312:             if ($month!=$nm) { $outp=&emptycell; }
  313:             $r->print($outp);
  314:         }
  315:         $r->print('</tr>');
  316:     }
  317: # ------------------------------------------------------------------- End table
  318:     $r->print('</table>');
  319: # ----------------------------------------------------------------- Check marks
  320:     %showedcheck=();
  321:     undef %showedcheck;
  322: # --------------------------------------------------------------- Remove button
  323:     if ($allowed) { $r->print('<input type="button" onClick="removesub()" value="Remove Checked Entries"></form>'); }
  324:     $r->print('<p>'.
  325:  '<a href="/adm/announcements?month='.$pm.'&year='.$py.'">Previous Month</a> '.
  326:  '<a href="/adm/announcements?month='.$fm.'&year='.$fy.'">Next Month</a>'.
  327:  '&nbsp;&nbsp;&nbsp;<a href="/adm/announcements?month='.$todayhash{'month'}.
  328:  '&year='.$todayhash{'year'}.'">Current Month</a></p>'.
  329:  '</body></html>');
  330:     return OK;
  331: } 
  332: 
  333: 1;
  334: __END__

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