File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.29: download - view: text, annotated - select for diffs
Tue Nov 8 03:13:20 2005 UTC (18 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- can pick what parts of a slot to see

    1: # The LearningOnline Network with CAPA
    2: # Handler for requesting to have slots added to a students record
    3: #
    4: # $Id: slotrequest.pm,v 1.29 2005/11/08 03:13:20 albertel 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: 
   30: package Apache::slotrequest;
   31: 
   32: use strict;
   33: use Apache::Constants qw(:common :http :methods);
   34: use Apache::loncommon();
   35: use Apache::lonlocal;
   36: use Apache::lonnet;
   37: use Date::Manip;
   38: 
   39: sub fail {
   40:     my ($r,$code)=@_;
   41:     if ($code eq 'not_valid') {
   42: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
   43: 
   44:     } elsif ($code eq 'not_allowed') {
   45: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
   46:     } else {
   47: 	$r->print('<p>'.&mt('Failed.').'</p>');
   48:     }
   49:     
   50:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
   51: 	      &mt('Return to last resource').'</a></p>');
   52:     &end_page($r);
   53: }
   54: 
   55: sub start_page {
   56:     my ($r,$title)=@_;
   57:     my $html=&Apache::lonxml::xmlbegin();
   58:     $r->print($html.'<head><title>'.&mt($title).'</title></head>');
   59:     $r->print(&Apache::loncommon::bodytag($title));
   60: }
   61: 
   62: sub end_page {
   63:     my ($r)=@_;
   64:     $r->print(&Apache::loncommon::endbodytag().'</html>');
   65: }
   66: 
   67: =pod
   68: 
   69:  slot_reservations db
   70:    - keys are 
   71:     - slotname\0id -> value is an hashref of
   72:                          name -> user@domain of holder
   73:                          timestamp -> timestamp of reservation
   74:                          symb -> symb of resource that it is reserved for
   75: 
   76: =cut
   77: 
   78: sub get_course {
   79:     (undef,my $courseid)=&Apache::lonxml::whichuser();
   80:     my $cdom=$env{'course.'.$courseid.'.domain'};
   81:     my $cnum=$env{'course.'.$courseid.'.num'};
   82:     return ($cnum,$cdom);
   83: }
   84: 
   85: sub get_reservation_ids {
   86:     my ($slot_name)=@_;
   87:     
   88:     my ($cnum,$cdom)=&get_course();
   89: 
   90:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
   91: 				       "^$slot_name\0");
   92:     
   93:     my ($tmp)=%consumed;
   94:     if ($tmp=~/^error: 2 / ) {
   95: 	return 0;
   96:     }
   97:     return keys(%consumed);
   98: }
   99: 
  100: sub space_available {
  101:     my ($slot_name,$slot)=@_;
  102:     my $max=$slot->{'maxspace'};
  103: 
  104:     if (!defined($max)) { return 1; }
  105: 
  106:     my $consumed=scalar(&get_reservation_ids($slot_name));
  107:     if ($consumed < $max) {
  108: 	return 1
  109:     }
  110:     return 0;
  111: }
  112: 
  113: sub check_for_reservation {
  114:     my ($symb)=@_;
  115:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  116: 				       $env{'user.domain'}, $env{'user.name'});
  117: 
  118:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  119: 				    $env{'user.domain'}, $env{'user.name'});
  120:     my @slots = (split(/:/,$student), split(/:/, $course));
  121: 
  122:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  123: 
  124:     my ($cnum,$cdom)=&get_course();
  125:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  126: 
  127:     foreach my $slot_name (@slots) {
  128: 	next if (!defined($slots{$slot_name}) ||
  129: 		 !ref($slots{$slot_name}));
  130: 	&Apache::lonxml::debug(time." $slot_name ".
  131: 			       $slots{$slot_name}->{'starttime'}." -- ".
  132: 			       $slots{$slot_name}->{'startreserve'});
  133: 	if ($slots{$slot_name}->{'endtime'} > time &&
  134: 	    $slots{$slot_name}->{'startreserve'} < time) {
  135: 	    # between start of reservation times and end of slot
  136: 	    return($slot_name, $slots{$slot_name});
  137: 	}
  138:     }
  139:     return (undef,undef);
  140: }
  141: 
  142: sub check_for_conflict {
  143:     my ($symb,$new_slot_name)=@_;
  144:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  145: 				       $env{'user.domain'}, $env{'user.name'});
  146:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  147: 				      $env{'user.domain'}, $env{'user.name'});
  148:     my @slots = (split(/:/,$student), split(/:/, $course));
  149:     my ($cnum,$cdom)=&get_course();
  150:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  151:     foreach my $slot_name (@slots) {
  152: 	next if (!defined($slots{$slot_name}) ||
  153: 		 !ref($slots{$slot_name}));
  154: 
  155:         next if (!defined($slots{$slot_name}->{'uniqueperiod'}) ||
  156: 		 !ref($slots{$slot_name}->{'uniqueperiod'}));
  157: 	my ($start,$end)=@{$slots{$slot_name}->{'uniqueperiod'}};
  158: 	if ($start<time && time < $end) {
  159: 	    return $slot_name;
  160: 	}
  161:     }
  162:     return undef;
  163: 
  164: }
  165: 
  166: sub make_reservation {
  167:     my ($slot_name,$slot,$symb)=@_;
  168: 
  169:     my ($cnum,$cdom)=&get_course();
  170: 
  171:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  172: 				   $env{'user.domain'},$env{'user.name'});
  173:     &Apache::lonxml::debug("value is  $value<br />");
  174:     foreach my $other_slot (split(/:/, $value)) {
  175: 	if ($other_slot eq $slot_name) {
  176: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  177: 					       $cnum, "^$slot_name\0");   
  178: 
  179: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  180: 	    foreach my $key (keys(%consumed)) {
  181: 		if ($consumed{$key}->{'name'} eq $me) {
  182: 		    my $num=(split('\0',$key))[1];
  183: 		    return -$num;
  184: 		}
  185: 	    }
  186: 	}
  187:     }
  188: 
  189:     my $max=$slot->{'maxspace'};
  190:     if (!defined($max)) { $max=99999; }
  191: 
  192:     my (@ids)=&get_reservation_ids($slot_name);
  193: 
  194:     my $last=0;
  195:     foreach my $id (@ids) {
  196: 	my $num=(split('\0',$id))[1];
  197: 	if ($num > $last) { $last=$num; }
  198:     }
  199:     
  200:     my $wanted=$last+1;
  201:     &Apache::lonxml::debug("wanted $wanted<br />");
  202:     if (scalar(@ids) >= $max) {
  203: 	# full up
  204: 	return undef;
  205:     }
  206:     
  207:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  208: 		     'timestamp' => time,
  209: 		     'symb'      => $symb);
  210: 
  211:     my $success=&Apache::lonnet::newput('slot_reservations',
  212: 					{"$slot_name\0$wanted" =>
  213: 					     \%reservation},
  214: 					$cdom, $cnum);
  215: 
  216:     if ($success eq 'ok') {
  217: 	my $new_value=$slot_name;
  218: 	if ($value) {
  219: 	    $new_value=$value.':'.$new_value;
  220: 	}
  221: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  222: 						      '0_availablestudent',
  223: 						       1, $new_value, 'string',
  224: 						       $env{'user.name'},
  225: 					               $env{'user.domain'});
  226: 	&Apache::lonxml::debug("hrrm $result");
  227: 	return $wanted;
  228:     }
  229: 
  230:     # someone else got it
  231:     return undef;
  232: }
  233: 
  234: sub release_slot {
  235:     my ($r,$symb,$slot_name,$inhibit_return_link)=@_;
  236: 
  237:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  238:     my ($cnum,$cdom)=&get_course();
  239: 
  240:     # get parameter string, check for existance, rebuild string with the slot
  241: 				       
  242:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  243: 					       $symb,$env{'user.domain'},
  244: 					       $env{'user.name'}));
  245:     my @new_slots;
  246:     foreach my $exist_slot (@slots) {
  247: 	if ($exist_slot eq $slot_name) { next; }
  248: 	push(@new_slots,$exist_slot);
  249:     }
  250:     my $new_param = join(':',@new_slots);
  251: 
  252:     # get slot reservations, check if user has one, if so remove reservation
  253:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  254: 				       "^$slot_name\0");
  255:     foreach my $entry (keys(%consumed)) {
  256: 	if ( $consumed{$entry}->{'name'} eq 
  257: 	     ($env{'user.name'}.'@'.$env{'user.domain'}) ) {
  258: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  259: 				 $cdom,$cnum);
  260: 	}
  261:     }
  262:     # store new parameter string
  263:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  264: 						      '0_availablestudent',
  265: 						      1, $new_param, 'string',
  266: 						      $env{'user.name'},
  267: 						      $env{'user.domain'});
  268:     my %slot=&Apache::lonnet::get_slot($slot_name);
  269:     my $description=&get_description($env{'form.slotname'},\%slot);
  270:     $r->print("<p>Released Reservation: $description</p>");
  271:     if (!$inhibit_return_link) {
  272: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
  273: 		  &mt('Return to last resource').'</a></p>');
  274:     }
  275:     return 1;
  276: }
  277: 
  278: sub get_slot {
  279:     my ($r,$symb)=@_;
  280: 
  281:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
  282:     if ($slot_name) {
  283: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  284: 	my $description1=&get_description($slot_name,\%slot);
  285: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  286: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  287: 	$r->print("<p>Already have a reservation: $description1</p>");
  288: 	if ($slot_name ne $env{'form.slotname'}) {
  289: 	    $r->print(<<STUFF);
  290: <form method="POST" action="/adm/slotrequest">
  291:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  292:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  293:    <input type="hidden" name="releaseslot" value="$slot_name" />
  294:    <input type="hidden" name="command" value="change" />
  295: STUFF
  296:             $r->print("<p>You can either ");
  297: 	    $r->print(<<STUFF);
  298:    <input type="submit" name="change" value="Change" />
  299: STUFF
  300: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  301: 		      $description2.
  302: 		      '</b> <br />or <a href="/adm/flip?postdata=return:">'.
  303: 		      &mt('Return to last resource').'</a></p>');
  304: 	    $r->print(<<STUFF);
  305: </form>
  306: STUFF
  307:         } else {
  308: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  309: 		      &mt('Return to last resource').'</a></p>');
  310: 	}
  311: 	return;
  312:     }
  313:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  314:     my $reserved=&make_reservation($env{'form.slotname'},
  315: 				   \%slot,$symb);
  316:     my $description=&get_description($env{'form.slotname'},\%slot);
  317:     if (defined($reserved)) {
  318: 	if ($reserved > -1) {
  319: 	    $r->print("<p>Success: $description</p>");
  320: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  321: 		      &mt('Return to last resource').'</a></p>');
  322: 	    return;
  323: 	} elsif ($reserved < 0) {
  324: 	    $r->print("<p>Already reserved: $description</p>");
  325: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  326: 		      &mt('Return to last resource').'</a></p>');
  327: 	    return;
  328: 	}
  329:     }
  330: 
  331:     my %lt=('request'=>"Availibility list",
  332: 	    'try'    =>'Try again');
  333:     %lt=&Apache::lonlocal::texthash(%lt);
  334: 
  335:     $r->print(<<STUFF);
  336: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  337: <p>
  338: <form method="POST" action="/adm/slotrequest">
  339:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  340:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  341:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  342:    <input type="hidden" name="command" value="get" />
  343: </form>
  344: ?
  345: </p>
  346: <p>
  347: or
  348: <form method="POST" action="/adm/slotrequest">
  349:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  350:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  351: </form>
  352: </p>
  353: or
  354: STUFF
  355:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  356: 	      &mt('Return to last resource').'</a></p>');
  357:     return;
  358: }
  359: 
  360: sub allowed_slot {
  361:     my ($slot_name,$slot,$symb)=@_;
  362:     #already started
  363:     if ($slot->{'starttime'} < time) {
  364: 	# all open slot to be schedulable
  365: 	#return 0;
  366:     }
  367:     &Apache::lonxml::debug("$slot_name starttime good");
  368:     #already ended
  369:     if ($slot->{'endtime'} < time) {
  370: 	return 0;
  371:     }
  372:     &Apache::lonxml::debug("$slot_name endtime good");
  373:     # not allowed to pick this one
  374:     if (defined($slot->{'type'})
  375: 	&& $slot->{'type'} ne 'schedulable_student') {
  376: 	return 0;
  377:     }
  378:     &Apache::lonxml::debug("$slot_name type good");
  379:     # not allowed for this resource
  380:     if (defined($slot->{'symb'})
  381: 	&& $slot->{'symb'} ne $symb) {
  382: 	return 0;
  383:     }
  384:     &Apache::lonxml::debug("$slot_name symb good");
  385:     return 1;
  386: }
  387: 
  388: sub get_description {
  389:     my ($slot_name,$slot)=@_;
  390:     my $description=$slot->{'description'};
  391:     if (!defined($description)) {
  392: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  393: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  394: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  395:     }
  396:     return $description;
  397: }
  398: 
  399: sub show_choices {
  400:     my ($r,$symb)=@_;
  401: 
  402:     my ($cnum,$cdom)=&get_course();
  403:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  404:     my $available;
  405:     $r->print('<table border="1">');
  406:     &Apache::lonxml::debug("Checking Slots");
  407:     my ($got_slot)=&check_for_reservation($symb);
  408:     foreach my $slot (sort 
  409: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  410: 		      (keys(%slots)))  {
  411: 
  412: 	&Apache::lonxml::debug("Checking Slot $slot");
  413: 	next if (!&allowed_slot($slot,$slots{$slot}));
  414: 
  415: 	$available++;
  416: 
  417: 	my $description=&get_description($slot,$slots{$slot});
  418: 
  419: 	my $form=&mt('Unavailable');
  420: 	if (($slot eq $got_slot) ||
  421: 	    &space_available($slot,$slots{$slot},$symb)) {
  422: 	    my $text=&mt('Select');
  423: 	    my $command='get';
  424: 	    if ($slot eq $got_slot) {
  425: 		$text=&mt('Free Reservation');
  426: 		$command='release';
  427: 	    }
  428: 	    my $escsymb=&Apache::lonnet::escape($symb);
  429: 	    $form=<<STUFF;
  430:    <form method="POST" action="/adm/slotrequest">
  431:      <input type="submit" name="Select" value="$text" />
  432:      <input type="hidden" name="symb" value="$escsymb" />
  433:      <input type="hidden" name="slotname" value="$slot" />
  434:      <input type="hidden" name="command" value="$command" />
  435:    </form>
  436: STUFF
  437: 	}
  438: 	$r->print(<<STUFF);
  439: <tr>
  440:  <td>$form</td>
  441:  <td>$description</td>
  442: </tr>
  443: STUFF
  444:     }
  445: 
  446:     if (!$available) {
  447: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  448: 		  &mt('Return to last resource').'</a></td></tr>');
  449:     }
  450:     $r->print('</table>');
  451: }
  452: 
  453: sub show_table {
  454:     my ($r,$mgr)=@_;
  455: 
  456:     my ($cnum,$cdom)=&get_course();
  457:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  458:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  459: 	undef(%slots);
  460:     } 
  461:     my $available;
  462:     if ($mgr eq 'F') {
  463: 	$r->print('<form method="POST" action="/adm/slotrequest">
  464: <input type="hidden" name="command" value="uploadstart" />
  465: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  466: </form>');
  467: 	$r->print('<form method="POST" action="/adm/helper/newslot.helper">
  468: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  469: </form>');
  470:     }
  471:     
  472:     my %Saveable_Parameters = ('show' => 'array');
  473: 
  474:     &Apache::loncommon::store_course_settings('chart',\%Saveable_Parameters);
  475:     &Apache::loncommon::restore_course_settings('chart',\%Saveable_Parameters);
  476: 
  477:     my %fields=&Apache::lonlocal::texthash(
  478: 	     'name'         => 'Slot Name',
  479: 	     'description'  => 'Description',
  480: 	     'type'         => 'Type',
  481: 	     'starttime'    => 'Start time',
  482: 	     'endtime'      => 'End Time',
  483:              'startreserve' => 'Time students can start reserving',
  484: 	     'secret'       => 'Secret Word',
  485: 	     'maxspace'     => 'Maxium # of students',
  486: 	     'ip'           => 'IP or DNS restrictions',
  487: 	     'symb'         => 'Resource slot is restricted to.',
  488: 	     'uniqueperiod' => 'Period of time slot is unique',
  489: 	     'proctor'      => 'List of proctors');
  490:     my @order=('name','description','type','starttime','endtime',
  491: 	       'startreserve','secret','maxspace','ip','symb',
  492: 	       'uniqueperiod','proctor');
  493:     my @sel = 
  494: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  495: 	                            : keys(%fields);
  496:     my %sel =  map { $_ => 1 } (@sel);
  497: 
  498:     $r->print('<form method="POST" action="/adm/slotrequest">
  499: <input type="hidden" name="command" value="showslots" />
  500: <input type="submit" name="start" value="'.&mt('Change').'" />');
  501:     $r->print('<table><tr><td>Show: '.&Apache::loncommon::multiple_select_form('show',\@sel,6,\%fields,\@order).
  502: 	      '</td></tr></table>');
  503:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
  504:     $r->print('<table border="1">
  505: <tr>
  506:   <th></th>');
  507:     foreach my $which (@order) {
  508: 	if ($which ne 'proctor' && exists($sel{$which})) {
  509: 	    $r->print('<th>'.$linkstart.$which.'">'.$fields{$which}.'</a></th>');
  510: 	}
  511:     }
  512:     $r->print('<th>Scheduled Students</th></tr>');
  513: 
  514:     my %name_cache;
  515:     my $slotsort = sub {
  516: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
  517: 	    if (lc($slots{$a}->{$env{'form.order'}})
  518: 		ne lc($slots{$b}->{$env{'form.order'}})) {
  519: 		return (lc($slots{$a}->{$env{'form.order'}}) 
  520: 			cmp lc($slots{$b}->{$env{'form.order'}}));
  521: 	    }
  522: 	} elsif ($env{'form.order'} eq 'name') {
  523: 	    if (lc($a) cmp lc($b)) {
  524: 		return lc($a) cmp lc($b);
  525: 	    }
  526: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
  527: 	    
  528: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
  529: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
  530: 		return ($slots{$a}->{'uniqueperiod'}[0]
  531: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
  532: 	    }
  533: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
  534: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
  535: 		return ($slots{$a}->{'uniqueperiod'}[1]
  536: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
  537: 	    }
  538: 	}
  539: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
  540:     };
  541:     foreach my $slot (sort $slotsort (keys(%slots)))  {
  542: 	if (defined($slots{$slot}->{'type'})
  543: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
  544: 	    #next;
  545: 	}
  546: 	my $description=&get_description($slot,$slots{$slot});
  547: 	my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  548: 					   "^$slot\0");
  549: 	my $ids;
  550: 	foreach my $entry (sort(keys(%consumed))) {
  551: 	    my (undef,$id)=split("\0",$entry);
  552: 	    $ids.= $id.'-> '.$consumed{$entry}->{'name'}.'<br />';
  553: 	}
  554: 	my $start=($slots{$slot}->{'starttime'}?
  555: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
  556: 	my $end=($slots{$slot}->{'endtime'}?
  557: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
  558: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
  559: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
  560: 	
  561: 	my $unique;
  562: 	if (ref($slots{$slot}{'uniqueperiod'})) {
  563: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
  564: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
  565: 	}
  566: 	my $title;
  567: 	if (exists($slots{$slot}{'symb'})) {
  568: 	    my (undef,undef,$res)=
  569: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
  570: 	    $res =   &Apache::lonnet::clutter($res);
  571: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
  572: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
  573: 	}
  574: 	my @proctors;
  575: 	my $rowspan=1;
  576: 	my $colspan=1;
  577: 	if (exists($sel{'proctor'})) {
  578: 	    $rowspan=2;
  579: 	    @proctors= map {
  580: 		my ($uname,$udom)=split(/@/,$_);
  581: 		my $fullname=$name_cache{$_};
  582: 		if (!defined($fullname)) {
  583: 		    &Apache::lonnet::logthis("Gettign $uname $udom");
  584: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
  585: 		    $fullname =~s/\s/&nbsp;/g;
  586: 		    $name_cache{$_} = $fullname;
  587: 		}
  588: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
  589: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
  590: 	}
  591: 	my $proctors=join(', ',@proctors);
  592: 
  593: 	my $edit=(<<EDITFORM);
  594: <form method="POST" action="/adm/helper/newslot.helper">
  595:   <input type="hidden" name="name" value="$slot" />
  596:   <input type="submit" name="Edit" value="Edit" />
  597: </form>
  598: EDITFORM
  599:  
  600:         $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit</td>\n");
  601: 	if (exists($sel{'name'})) {
  602: 	    $colspan++;$r->print("<td>$slot</td>");
  603: 	}
  604: 	if (exists($sel{'type'})) {
  605: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
  606: 	}
  607: 	if (exists($sel{'description'})) {
  608: 	    $colspan++;$r->print("<td>$description</td>\n");
  609: 	}
  610: 	if (exists($sel{'starttime'})) {
  611: 	    $colspan++;$r->print("<td>$start</td>\n");
  612: 	}
  613: 	if (exists($sel{'endtime'})) {
  614: 	    $colspan++;$r->print("<td>$end</td>\n");
  615: 	}
  616: 	if (exists($sel{'startreserve'})) {
  617: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
  618: 	}
  619: 	if (exists($sel{'secret'})) {
  620: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
  621: 	}
  622: 	if (exists($sel{'maxspace'})) {
  623: 	    $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
  624: 	}
  625: 	if (exists($sel{'ip'})) {
  626: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
  627: 	}
  628: 	if (exists($sel{'symb'})) {
  629: 	    $colspan++;$r->print("<td>$title</td>\n");
  630: 	}
  631: 	if (exists($sel{'uniqueperiod'})) {
  632: 	    $colspan++;$r->print("<td>$unique</td>\n");
  633: 	}
  634: 	$colspan++;$r->print("<td>$ids</td>\n</tr>\n");
  635: 	if (exists($sel{'proctor'})) {
  636: 	    $r->print(<<STUFF);
  637: <tr>
  638:  <td colspan="$colspan">$proctors</td>
  639: </tr>
  640: STUFF
  641:         }
  642:     }
  643:     $r->print('</table>');
  644: }
  645: 
  646: sub upload_start {
  647:     my ($r)=@_;    
  648:     $r->print(&Apache::grades::checkforfile_js());
  649:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
  650:     $result.='&nbsp;<b>'.
  651: 	&mt('Specify a file containing the slot definitions.').
  652: 	'</b></td></tr>'."\n";
  653:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
  654:     my $upfile_select=&Apache::loncommon::upfile_select_html();
  655:     my $ignore=&mt('Ignore First Line');
  656:     $result.=<<ENDUPFORM;
  657: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
  658: <input type="hidden" name="command" value="csvuploadmap" />
  659: $upfile_select
  660: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
  661: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
  662: </form>
  663: ENDUPFORM
  664:     $result.='</td></tr></table>'."\n";
  665:     $result.='</td></tr></table>'."\n";
  666:     $r->print($result);
  667: }
  668: 
  669: sub csvuploadmap_header {
  670:     my ($r,$datatoken,$distotal)= @_;
  671:     my $javascript;
  672:     if ($env{'form.upfile_associate'} eq 'reverse') {
  673: 	$javascript=&csvupload_javascript_reverse_associate();
  674:     } else {
  675: 	$javascript=&csvupload_javascript_forward_associate();
  676:     }
  677: 
  678:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
  679:     my $ignore=&mt('Ignore First Line');
  680:     $r->print(<<ENDPICK);
  681: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
  682: <h3>Identify fields</h3>
  683: Total number of records found in file: $distotal <hr />
  684: Enter as many fields as you can. The system will inform you and bring you back
  685: to this page if the data selected is insufficient to create the slots.<hr />
  686: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
  687: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
  688: <input type="hidden" name="associate"  value="" />
  689: <input type="hidden" name="datatoken"  value="$datatoken" />
  690: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
  691: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
  692: <input type="hidden" name="upfile_associate" 
  693:                                        value="$env{'form.upfile_associate'}" />
  694: <input type="hidden" name="command"    value="csvuploadassign" />
  695: <hr />
  696: <script type="text/javascript" language="Javascript">
  697: $javascript
  698: </script>
  699: ENDPICK
  700:     return '';
  701: 
  702: }
  703: 
  704: sub csvuploadmap_footer {
  705:     my ($request,$i,$keyfields) =@_;
  706:     $request->print(<<ENDPICK);
  707: </table>
  708: <input type="hidden" name="nfields" value="$i" />
  709: <input type="hidden" name="keyfields" value="$keyfields" />
  710: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
  711: </form>
  712: ENDPICK
  713: }
  714: 
  715: sub csvupload_javascript_reverse_associate {
  716:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
  717:     return(<<ENDPICK);
  718:   function verify(vf) {
  719:     var foundstart=0;
  720:     var foundend=0;
  721:     var foundname=0;
  722:     var foundtype=0;
  723:     for (i=0;i<=vf.nfields.value;i++) {
  724:       tw=eval('vf.f'+i+'.selectedIndex');
  725:       if (i==0 && tw!=0) { foundname=1; }
  726:       if (i==1 && tw!=0) { foundtype=1; }
  727:       if (i==2 && tw!=0) { foundstat=1; }
  728:       if (i==3 && tw!=0) { foundend=1; }
  729:     }
  730:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
  731: 	alert('$error1');
  732: 	return;
  733:     }
  734:     vf.submit();
  735:   }
  736:   function flip(vf,tf) {
  737:   }
  738: ENDPICK
  739: }
  740: 
  741: sub csvupload_javascript_forward_associate {
  742:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
  743:   return(<<ENDPICK);
  744:   function verify(vf) {
  745:     var foundstart=0;
  746:     var foundend=0;
  747:     var foundname=0;
  748:     var foundtype=0;
  749:     for (i=0;i<=vf.nfields.value;i++) {
  750:       tw=eval('vf.f'+i+'.selectedIndex');
  751:       if (tw==1) { foundname=1; }
  752:       if (tw==2) { foundtype=1; }
  753:       if (tw==3) { foundstat=1; }
  754:       if (tw==4) { foundend=1; }
  755:     }
  756:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
  757: 	alert('$error1');
  758: 	return;
  759:     }
  760:     vf.submit();
  761:   }
  762:   function flip(vf,tf) {
  763:   }
  764: ENDPICK
  765: }
  766: 
  767: sub csv_upload_map {
  768:     my ($r)= @_;
  769: 
  770:     my $datatoken;
  771:     if (!$env{'form.datatoken'}) {
  772: 	$datatoken=&Apache::loncommon::upfile_store($r);
  773:     } else {
  774: 	$datatoken=$env{'form.datatoken'};
  775: 	&Apache::loncommon::load_tmp_file($r);
  776:     }
  777:     my @records=&Apache::loncommon::upfile_record_sep();
  778:     if ($env{'form.noFirstLine'}) { shift(@records); }
  779:     &csvuploadmap_header($r,$datatoken,$#records+1);
  780:     my ($i,$keyfields);
  781:     if (@records) {
  782: 	my @fields=&csvupload_fields();
  783: 
  784: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
  785: 	    &Apache::loncommon::csv_print_samples($r,\@records);
  786: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
  787: 							  \@fields);
  788: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
  789: 	    chop($keyfields);
  790: 	} else {
  791: 	    unshift(@fields,['none','']);
  792: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
  793: 							    \@fields);
  794: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
  795: 	    $keyfields=join(',',sort(keys(%sone)));
  796: 	}
  797:     }
  798:     &csvuploadmap_footer($r,$i,$keyfields);
  799: 
  800:     return '';
  801: }
  802: 
  803: sub csvupload_fields {
  804:     return (['name','Slot name'],
  805: 	    ['type','Type of slot'],
  806: 	    ['starttime','Start Time of slot'],
  807: 	    ['endtime','End Time of slot'],
  808: 	    ['startreserve','Reservation Start Time'],
  809: 	    ['ip','IP or DNS restriction'],
  810: 	    ['proctor','List of proctor ids'],
  811: 	    ['description','Slot Description'],
  812: 	    ['maxspace','Maximum number of reservations'],
  813: 	    ['symb','Resource Restriction'],
  814: 	    ['uniqueperiod','Date range of slot exclusion'],
  815: 	    ['secret','Secret word proctor uses to validate']);
  816: }
  817: 
  818: sub csv_upload_assign {
  819:     my ($r,$mgr)= @_;
  820:     &Apache::loncommon::load_tmp_file($r);
  821:     my @slotdata = &Apache::loncommon::upfile_record_sep();
  822:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
  823:     my %fields=&Apache::grades::get_fields();
  824:     $r->print('<h3>Creating Slots</h3>');
  825:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
  826:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
  827:     my $countdone=0;
  828:     foreach my $slot (@slotdata) {
  829: 	my %slot;
  830: 	my %entries=&Apache::loncommon::record_sep($slot);
  831: 	my $domain;
  832: 	my $name=$entries{$fields{'name'}};
  833: 	if ($entries{$fields{'type'}}) {
  834: 	    $slot{'type'}=$entries{$fields{'type'}};
  835: 	} else {
  836: 	    $slot{'type'}='preassigned';
  837: 	}
  838: 	if ($entries{$fields{'starttime'}}) {
  839: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
  840: 	}
  841: 	if ($entries{$fields{'endtime'}}) {
  842: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
  843: 	}
  844: 	if ($entries{$fields{'startreserve'}}) {
  845: 	    $slot{'startreserve'}=
  846: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
  847: 	}
  848: 	foreach my $key ('ip','proctor','description','maxspace',
  849: 			 'secret','symb') {
  850: 	    if ($entries{$fields{$key}}) {
  851: 		$slot{$key}=$entries{$fields{$key}};
  852: 	    }
  853: 	}
  854: 	if ($entries{$fields{'uniqueperiod'}}) {
  855: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
  856: 	    my @times=(&UnixDate($start,"%s"),
  857: 		       &UnixDate($end,"%s"));
  858: 	    $slot{'uniqueperiod'}=\@times;
  859: 	}
  860: 
  861: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
  862: 	$r->print('.');
  863: 	$r->rflush();
  864: 	$countdone++;
  865:     }
  866:     $r->print("<br />Created $countdone slots\n");
  867:     $r->print("<br />\n");
  868:     &show_table($r,$mgr);
  869:     return '';
  870: }
  871: 
  872: sub handler {
  873:     my $r=shift;
  874: 
  875:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
  876:     
  877:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
  878:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
  879:     my $title='Requesting Another Worktime';
  880:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
  881: 	$title = 'Managing Slots';
  882:     }
  883:     &start_page($r,$title);
  884: 
  885:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
  886: 	&show_table($r,$mgr);
  887:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
  888: 	&upload_start($r);
  889:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
  890: 	&csv_upload_map($r);
  891:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
  892: 	if ($env{'form.associate'} ne 'Reverse Association') {
  893: 	    &csv_upload_assign($r,$mgr);
  894: 	} else {
  895: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
  896: 		$env{'form.upfile_associate'} = 'reverse';
  897: 	    } else {
  898: 		$env{'form.upfile_associate'} = 'forward';
  899: 	    }
  900: 	    &csv_upload_map($r);
  901: 	}
  902:     } else {
  903: 	my $symb=&Apache::lonnet::unescape($env{'form.symb'});
  904: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
  905: 	if ($res !~ /\.task$/) {
  906: 	    &fail($r,'not_valid');
  907: 	    return OK;
  908: 	}
  909: 	$env{'request.symb'}=$symb;
  910: 	my ($status) = &Apache::lonhomework::check_task_access('0');
  911: 	if ($status eq 'CAN_ANSWER' ||
  912: 	    $status eq 'NEEDS_CHECKIN' ||
  913: 	    $status eq 'WAITING_FOR_GRADE') {
  914: 	    &fail($r,'not_allowed');
  915: 	    return OK;
  916: 	}
  917: 	if ($env{'form.requestattempt'}) {
  918: 	    &show_choices($r,$symb);
  919: 	} elsif ($env{'form.command'} eq 'release') {
  920: 	    &release_slot($r,$symb);
  921: 	} elsif ($env{'form.command'} eq 'get') {
  922: 	    &get_slot($r,$symb);
  923: 	} elsif ($env{'form.command'} eq 'change') {
  924: 	    &release_slot($r,$symb,$env{'form.releaseslot'},1);
  925: 	    &get_slot($r,$symb);
  926: 	} else {
  927: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
  928: 	}
  929:     }
  930:     &end_page($r);
  931:     return OK;
  932: }
  933: 
  934: 1;
  935: __END__

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