File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.26: download - view: text, annotated - select for diffs
Tue Feb 17 01:36:14 2004 UTC (20 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Show due dates, etc, on calendar

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

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