File:  [LON-CAPA] / loncom / interface / lonannounce.pm
Revision 1.27: download - view: text, annotated - select for diffs
Tue Feb 17 01:46:34 2004 UTC (20 years, 3 months ago) by www
Branches: MAIN
CVS tags: version_1_1_99_0, HEAD
Show one day around current time on Syllabus and Roles

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

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