File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.50: download - view: text, annotated - select for diffs
Tue Mar 7 21:37:29 2006 UTC (18 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- respect the allowedsections and allowedusers slot attributes

    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.50 2006/03/07 21:37:29 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 Apache::lonnavmaps();
   38: use Date::Manip;
   39: 
   40: sub fail {
   41:     my ($r,$code)=@_;
   42:     if ($code eq 'not_valid') {
   43: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
   44: 
   45:     } elsif ($code eq 'not_allowed') {
   46: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
   47:     } else {
   48: 	$r->print('<p>'.&mt('Failed.').'</p>');
   49:     }
   50:     
   51:     &return_link($r);
   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:     if (&network_error(%consumed)) { 
   93: 	return 'error: Unable to determine current status';
   94:     }
   95:     my ($tmp)=%consumed;
   96:     if ($tmp=~/^error: 2 / ) {
   97: 	return 0;
   98:     }
   99:     return keys(%consumed);
  100: }
  101: 
  102: sub space_available {
  103:     my ($slot_name,$slot)=@_;
  104:     my $max=$slot->{'maxspace'};
  105: 
  106:     if (!defined($max)) { return 1; }
  107: 
  108:     my $consumed=scalar(&get_reservation_ids($slot_name));
  109:     if ($consumed < $max) {
  110: 	return 1
  111:     }
  112:     return 0;
  113: }
  114: 
  115: sub check_for_reservation {
  116:     my ($symb,$mode)=@_;
  117:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  118: 				       $env{'user.domain'}, $env{'user.name'});
  119: 
  120:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  121: 				    $env{'user.domain'}, $env{'user.name'});
  122:     my @slots = (split(/:/,$student), split(/:/, $course));
  123: 
  124:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  125: 
  126:     my ($cnum,$cdom)=&get_course();
  127:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  128: 
  129:     if (&network_error($student) || &network_error($course)  ||
  130: 	&network_error(%slots)) {
  131: 	return 'error: Unable to determine current status';
  132:     }    
  133:     my @got;
  134:     foreach my $slot_name (sort {
  135: 	if (ref($slots{$a}) && ref($slots{$b})) {
  136: 	    return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
  137: 	}
  138: 	if (ref($slots{$a})) { return -1;}
  139: 	if (ref($slots{$b})) { return 1;}
  140: 	return 0;
  141:     } @slots) {
  142: 	next if (!defined($slots{$slot_name}) ||
  143: 		 !ref($slots{$slot_name}));
  144: 	&Apache::lonxml::debug(time." $slot_name ".
  145: 			       $slots{$slot_name}->{'starttime'}." -- ".
  146: 			       $slots{$slot_name}->{'startreserve'});
  147: 	if ($slots{$slot_name}->{'endtime'} > time &&
  148: 	    $slots{$slot_name}->{'startreserve'} < time) {
  149: 	    # between start of reservation times and end of slot
  150: 	    if ($mode eq 'allslots') {
  151: 		push(@got,$slot_name);
  152: 	    } else {
  153: 		return($slot_name, $slots{$slot_name});
  154: 	    }
  155: 	}
  156:     }
  157:     if ($mode eq 'allslots' && @got) {
  158: 	return @got;
  159:     }
  160:     return (undef,undef);
  161: }
  162: 
  163: sub get_consumed_uniqueperiods {
  164:     my ($slots) = @_;
  165:     my $navmap=Apache::lonnavmaps::navmap->new;
  166:     my @problems = $navmap->retrieveResources(undef,
  167: 					      sub { $_[0]->is_problem() },1,0);
  168:     my %used_slots;
  169:     foreach my $problem (@problems) {
  170: 	my $symb = $problem->symb();
  171: 	my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
  172: 					   $symb, $env{'user.domain'},
  173: 					   $env{'user.name'});
  174: 	my $course =  &Apache::lonnet::EXT("resource.0.available",
  175: 					   $symb, $env{'user.domain'},
  176: 					   $env{'user.name'});
  177: 	if (&network_error($student) || &network_error($course)) {
  178: 	    return 'error: Unable to determine current status';
  179: 	}
  180: 	foreach my $slot (split(/:/,$student), split(/:/, $course)) {
  181: 	    $used_slots{$slot}=1;
  182: 	}
  183:     }
  184: 
  185:     if (!ref($slots)) {
  186: 	my ($cnum,$cdom)=&get_course();
  187: 	my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
  188: 	if (&network_error(%slots)) {
  189: 	    return 'error: Unable to determine current status';
  190: 	}
  191: 	$slots = \%slots;
  192:     }
  193: 
  194:     my %consumed_uniqueperiods;
  195:     foreach my $slot_name (keys(%used_slots)) {
  196: 	next if (!defined($slots->{$slot_name}) ||
  197: 		 !ref($slots->{$slot_name}));
  198: 	
  199:         next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
  200: 		 !ref($slots->{$slot_name}{'uniqueperiod'}));
  201: 	$consumed_uniqueperiods{$slot_name} = 
  202: 	    $slots->{$slot_name}{'uniqueperiod'};
  203:     }
  204:     return \%consumed_uniqueperiods;
  205: }
  206: 
  207: sub check_for_conflict {
  208:     my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
  209: 
  210:     if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
  211: 
  212:     if (!ref($consumed_uniqueperiods)) {
  213: 	$consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
  214: 	if (&network_error(%$consumed_uniqueperiods)) {
  215: 	    return 'error: Unable to determine current status';
  216: 	}
  217:     }
  218:     
  219:     my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
  220:     foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
  221: 	my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
  222: 	if (!
  223: 	    ($start < $new_uniq_start &&  $end < $new_uniq_start) ||
  224: 	    ($start > $new_uniq_end   &&  $end > $new_uniq_end  )) {
  225: 	    return $slot_name;
  226: 	}
  227:     }
  228:     return undef;
  229: 
  230: }
  231: 
  232: sub network_error {
  233:     my ($result) = @_;
  234:     if ($result =~ /^(con_lost|no_such_host|error: [^2])/) {
  235: 	return 1;
  236:     }
  237:     return 0;
  238: }
  239: 
  240: sub make_reservation {
  241:     my ($slot_name,$slot,$symb)=@_;
  242: 
  243:     my ($cnum,$cdom)=&get_course();
  244: 
  245:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  246: 				   $env{'user.domain'},$env{'user.name'});
  247:     &Apache::lonxml::debug("value is  $value<br />");
  248:     if (&network_error($value)) { 
  249: 	return 'error: Unable to determine current status';
  250:     }
  251: 
  252:     foreach my $other_slot (split(/:/, $value)) {
  253: 	if ($other_slot eq $slot_name) {
  254: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  255: 					       $cnum, "^$slot_name\0");   
  256: 	    if (&network_error($value)) { 
  257: 		return 'error: Unable to determine current status';
  258: 	    }
  259: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  260: 	    foreach my $key (keys(%consumed)) {
  261: 		if ($consumed{$key}->{'name'} eq $me) {
  262: 		    my $num=(split('\0',$key))[1];
  263: 		    return -$num;
  264: 		}
  265: 	    }
  266: 	}
  267:     }
  268: 
  269:     my $max=$slot->{'maxspace'};
  270:     if (!defined($max)) { $max=99999; }
  271: 
  272:     my (@ids)=&get_reservation_ids($slot_name);
  273:     if (&network_error(@ids)) { 
  274: 	return 'error: Unable to determine current status';
  275:     }
  276:     my $last=0;
  277:     foreach my $id (@ids) {
  278: 	my $num=(split('\0',$id))[1];
  279: 	if ($num > $last) { $last=$num; }
  280:     }
  281:     
  282:     my $wanted=$last+1;
  283:     &Apache::lonxml::debug("wanted $wanted<br />");
  284:     if (scalar(@ids) >= $max) {
  285: 	# full up
  286: 	return undef;
  287:     }
  288:     
  289:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  290: 		     'timestamp' => time,
  291: 		     'symb'      => $symb);
  292: 
  293:     my $success=&Apache::lonnet::newput('slot_reservations',
  294: 					{"$slot_name\0$wanted" =>
  295: 					     \%reservation},
  296: 					$cdom, $cnum);
  297: 
  298:     if ($success eq 'ok') {
  299: 	my $new_value=$slot_name;
  300: 	if ($value) {
  301: 	    $new_value=$value.':'.$new_value;
  302: 	}
  303: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  304: 						      '0_availablestudent',
  305: 						       1, $new_value, 'string',
  306: 						       $env{'user.name'},
  307: 					               $env{'user.domain'});
  308: 	&Apache::lonxml::debug("hrrm $result");
  309: 	return $wanted;
  310:     }
  311: 
  312:     # someone else got it
  313:     return undef;
  314: }
  315: 
  316: sub remove_registration {
  317:     my ($r) = @_;
  318:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
  319: 					     $env{'form.udom'});
  320: 
  321:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
  322: 
  323:     my $hidden_input;
  324:     foreach my $parm ('uname','udom','slotname','entry','symb') {
  325: 	$hidden_input .=
  326: 	    '<input type="hidden" name="'.$parm.'" value="'
  327: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
  328:     }
  329:     $r->print(<<"END_CONFIRM");
  330: <p> Remove $name from slot $env{'form.slotname'} for $title</p>
  331: <form action="/adm/slotrequest" method="POST">
  332:     <input type="hidden" name="command" value="release" />
  333:     $hidden_input
  334:     <input type="submit" name="Yes" value="yes" />
  335: </form>
  336: <form action="/adm/slotrequest" method="POST">
  337:     <input type="hidden" name="command" value="showslots" />
  338:     <input type="submit" name="No" value="no" />
  339: </form>
  340: END_CONFIRM
  341: 
  342: }
  343: 
  344: sub release_slot {
  345:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
  346: 
  347:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  348:     my ($cnum,$cdom)=&get_course();
  349: 
  350:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
  351:     if ($mgr eq 'F' 
  352: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
  353: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
  354:     }
  355: 
  356:     if ($mgr eq 'F' 
  357: 	&& defined($env{'form.symb'})) {
  358: 	$symb = $env{'form.symb'};
  359:     }
  360:     my %slot=&Apache::lonnet::get_slot($slot_name);
  361:     my $description=&get_description($env{'form.slotname'},\%slot);
  362: 
  363:     if ($mgr ne 'F') {
  364: 	if ($slot{'starttime'} < time) {
  365: 	    $r->print("<p>Not allowed to release Reservation: $description, as it has already ended.  </p>");
  366: 	    &return_link($r);
  367: 	    return 0;
  368: 	}
  369:     }
  370:     # get parameter string, check for existance, rebuild string with the slot
  371:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  372: 					       $symb,$udom,$uname));
  373: 
  374:     my @new_slots;
  375:     foreach my $exist_slot (@slots) {
  376: 	if ($exist_slot eq $slot_name) { next; }
  377: 	push(@new_slots,$exist_slot);
  378:     }
  379:     my $new_param = join(':',@new_slots);
  380: 
  381:     # get slot reservations, check if user has one, if so remove reservation
  382:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  383: 				       "^$slot_name\0");
  384:     foreach my $entry (keys(%consumed)) {
  385: 	if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) {
  386: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  387: 				 $cdom,$cnum);
  388: 	}
  389:     }
  390: 
  391:     # store new parameter string
  392:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  393: 						      '0_availablestudent',
  394: 						      1, $new_param, 'string',
  395: 						      $uname,$udom);
  396:     $r->print("<p>Released Reservation: $description</p>");
  397:     if ($mgr eq 'F') {
  398: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  399: 		  &mt('Return to slot list').'</a></p>');
  400:     }
  401:     if (!$inhibit_return_link) { &return_link($r);  }
  402:     return 1;
  403: }
  404: 
  405: sub delete_slot {
  406:     my ($r)=@_;
  407: 
  408:     my $slot_name = $env{'form.slotname'};
  409:     my %slot=&Apache::lonnet::get_slot($slot_name);
  410: 
  411:     my ($cnum,$cdom)=&get_course();
  412:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  413: 				       "^$slot_name\0");
  414:     my ($tmp) = %consumed;
  415:     if ($tmp =~ /error: 2/) { undef(%consumed); }
  416: 
  417:     if (%slot && !%consumed) {
  418: 	$slot{'type'} = 'deleted';
  419: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
  420: 					$cdom, $cnum);
  421: 	if ($ret eq 'ok') {
  422: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
  423: 	} else {
  424: 	    $r->print("<p> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</p>");
  425: 	}
  426:     } else {
  427: 	if (%consumed) {
  428: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
  429: 	} else {
  430: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
  431: 	}
  432:     }
  433:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
  434: 	      &mt('Return to slot list').'</a></p>');
  435:     &return_link($r);
  436: }
  437: 
  438: sub return_link {
  439:     my ($r) = @_;
  440:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  441: 	      &mt('Return to last resource').'</a></p>');
  442: }
  443: 
  444: sub get_slot {
  445:     my ($r,$symb)=@_;
  446: 
  447:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  448:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
  449: 
  450:     if ($slot_name =~ /^error: (.*)/) {
  451: 	$r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
  452: 	&return_link($r);
  453: 	return;
  454:     }
  455:     if ($slot_name) {
  456: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  457: 	my $description1=&get_description($slot_name,\%slot);
  458: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  459: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  460: 	$r->print("<p>Already have a reservation: $description1</p>");
  461: 	if ($slot_name ne $env{'form.slotname'}) {
  462: 	    $r->print(<<STUFF);
  463: <form method="POST" action="/adm/slotrequest">
  464:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  465:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  466:    <input type="hidden" name="releaseslot" value="$slot_name" />
  467:    <input type="hidden" name="command" value="change" />
  468: STUFF
  469:             $r->print("<p>You can either ");
  470: 	    $r->print(<<STUFF);
  471:    <input type="submit" name="change" value="Change" />
  472: STUFF
  473: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  474: 		      $description2.
  475: 		      '</b> <br />or </p>');
  476: 	    &return_link($r);
  477: 	    $r->print(<<STUFF);
  478: </form>
  479: STUFF
  480:         } else {
  481: 	    &return_link($r);
  482: 	}
  483: 	return;
  484:     }
  485: 
  486:     my $reserved=&make_reservation($env{'form.slotname'},
  487: 				   \%slot,$symb);
  488:     my $description=&get_description($env{'form.slotname'},\%slot);
  489:     if (defined($reserved)) {
  490: 	if ($slot_name =~ /^error: (.*)/) {
  491: 	    $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
  492: 	} elsif ($reserved > -1) {
  493: 	    $r->print("<p>Success: $description</p>");
  494: 	} elsif ($reserved < 0) {
  495: 	    $r->print("<p>Already reserved: $description</p>");
  496: 	}
  497: 	&return_link($r);
  498: 	return;
  499:     }
  500: 
  501:     my %lt=('request'=>"Availibility list",
  502: 	    'try'    =>'Try again');
  503:     %lt=&Apache::lonlocal::texthash(%lt);
  504: 
  505:     $r->print(<<STUFF);
  506: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  507: <p>
  508: <form method="POST" action="/adm/slotrequest">
  509:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  510:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  511:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  512:    <input type="hidden" name="command" value="get" />
  513: </form>
  514: ?
  515: </p>
  516: <p>
  517: or
  518: <form method="POST" action="/adm/slotrequest">
  519:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  520:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  521: </form>
  522: </p>
  523: or
  524: STUFF
  525: 
  526:     &return_link($r);
  527:     return;
  528: }
  529: 
  530: sub allowed_slot {
  531:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
  532: 
  533:     #already started
  534:     if ($slot->{'starttime'} < time) {
  535: 	# all open slot to be schedulable
  536: 	#return 0;
  537:     }
  538:     &Apache::lonxml::debug("$slot_name starttime good");
  539: 
  540:     #already ended
  541:     if ($slot->{'endtime'} < time) {
  542: 	return 0;
  543:     }
  544:     &Apache::lonxml::debug("$slot_name endtime good");
  545: 
  546:     # not allowed to pick this one
  547:     if (defined($slot->{'type'})
  548: 	&& $slot->{'type'} ne 'schedulable_student') {
  549: 	return 0;
  550:     }
  551:     &Apache::lonxml::debug("$slot_name type good");
  552: 
  553:     my $userallowed=0;
  554:     # its for a different set of users
  555:     if (defined($slot->{'allowedsections'})) {
  556: 	if (!defined($env{'request.role.sec'})
  557: 	    && grep(/^No section assigned$/,
  558: 		    split(',',$slot->{'allowedsections'}))) {
  559: 	    $userallowed=1;
  560: 	}
  561: 	if (defined($env{'request.role.sec'})
  562: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
  563: 		    split(',',$slot->{'allowedsections'}))) {
  564: 	    $userallowed=1;
  565: 	}
  566:     }
  567:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
  568: 
  569:     # its for a different set of users
  570:     if (defined($slot->{'allowedusers'})
  571: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
  572: 		split(',',$slot->{'allowedusers'}))) {
  573: 	$userallowed=1;
  574:     }
  575:     &Apache::lonxml::debug("$slot_name user is $userallowed");
  576:     return 0 if (!$userallowed);
  577: 
  578:     # not allowed for this resource
  579:     if (defined($slot->{'symb'})
  580: 	&& $slot->{'symb'} ne $symb) {
  581: 	return 0;
  582:     }
  583: 
  584:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
  585: 				       $consumed_uniqueperiods);
  586:     if ($conflict) {
  587: 	if ($slots->{$conflict}{'starttime'} < time) {
  588: 	    return 0;
  589: 	}
  590:     }
  591:     &Apache::lonxml::debug("$slot_name symb good");
  592:     return 1;
  593: }
  594: 
  595: sub get_description {
  596:     my ($slot_name,$slot)=@_;
  597:     my $description=$slot->{'description'};
  598:     if (!defined($description)) {
  599: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  600: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  601: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  602:     }
  603:     return $description;
  604: }
  605: 
  606: sub show_choices {
  607:     my ($r,$symb)=@_;
  608: 
  609:     my ($cnum,$cdom)=&get_course();
  610:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  611:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
  612:     my $available;
  613:     $r->print('<table border="1">');
  614:     &Apache::lonxml::debug("Checking Slots");
  615:     my @got_slots=&check_for_reservation($symb,'allslots');
  616:     foreach my $slot (sort 
  617: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  618: 		      (keys(%slots)))  {
  619: 
  620: 	&Apache::lonxml::debug("Checking Slot $slot");
  621: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
  622: 				$consumed_uniqueperiods));
  623: 
  624: 	$available++;
  625: 
  626: 	my $description=&get_description($slot,$slots{$slot});
  627: 
  628: 	my $form=&mt('Unavailable');
  629: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
  630: 	    &space_available($slot,$slots{$slot},$symb)) {
  631: 	    my $text=&mt('Select');
  632: 	    my $command='get';
  633: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
  634: 		$text=&mt('Free Reservation');
  635: 		$command='release';
  636: 	    } else {
  637: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
  638: 						   \%slots,
  639: 						   $consumed_uniqueperiods);
  640: 		if ($conflict) {
  641: 		    $text=&mt('Change Reservation');
  642: 		    $command='get';
  643: 		}
  644: 	    }
  645: 	    my $escsymb=&Apache::lonnet::escape($symb);
  646: 	    $form=<<STUFF;
  647:    <form method="POST" action="/adm/slotrequest">
  648:      <input type="submit" name="Select" value="$text" />
  649:      <input type="hidden" name="symb" value="$escsymb" />
  650:      <input type="hidden" name="slotname" value="$slot" />
  651:      <input type="hidden" name="command" value="$command" />
  652:    </form>
  653: STUFF
  654: 	}
  655: 	$r->print(<<STUFF);
  656: <tr>
  657:  <td>$form</td>
  658:  <td>$description</td>
  659: </tr>
  660: STUFF
  661:     }
  662: 
  663:     if (!$available) {
  664: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  665: 		  &mt('Return to last resource').'</a></td></tr>');
  666:     }
  667:     $r->print('</table>');
  668: }
  669: 
  670: sub to_show {
  671:     my ($slot,$when,$deleted) = @_;
  672:     my $time=time;
  673:     my $week=60*60*24*7;
  674:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
  675: 	return 0;
  676:     }
  677:     if ($when eq 'any') {
  678: 	return 1;
  679:     } elsif ($when eq 'now') {
  680: 	if ($time > $slot->{'starttime'} &&
  681: 	    $time < $slot->{'endtime'}) {
  682: 	    return 1;
  683: 	}
  684: 	return 0;
  685:     } elsif ($when eq 'nextweek') {
  686: 	if ( ($time        < $slot->{'starttime'} &&
  687: 	      ($time+$week) > $slot->{'starttime'})
  688: 	     ||
  689: 	     ($time        < $slot->{'endtime'} &&
  690: 	      ($time+$week) > $slot->{'endtime'}) ) {
  691: 	    return 1;
  692: 	}
  693: 	return 0;
  694:     } elsif ($when eq 'lastweek') {
  695: 	if ( ($time        > $slot->{'starttime'} &&
  696: 	      ($time-$week) < $slot->{'starttime'})
  697: 	     ||
  698: 	     ($time        > $slot->{'endtime'} &&
  699: 	      ($time-$week) < $slot->{'endtime'}) ) {
  700: 	    return 1;
  701: 	}
  702: 	return 0;
  703:     } elsif ($when eq 'willopen') {
  704: 	if ($time < $slot->{'starttime'}) {
  705: 	    return 1;
  706: 	}
  707: 	return 0;
  708:     } elsif ($when eq 'wereopen') {
  709: 	if ($time > $slot->{'endtime'}) {
  710: 	    return 1;
  711: 	}
  712: 	return 0;
  713:     }
  714:     
  715:     return 1;
  716: }
  717: 
  718: sub remove_link {
  719:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
  720: 
  721:     $slotname  = &Apache::lonnet::escape($slotname);
  722:     $entry     = &Apache::lonnet::escape($entry);
  723:     $uname     = &Apache::lonnet::escape($uname);
  724:     $udom      = &Apache::lonnet::escape($udom);
  725:     $symb      = &Apache::lonnet::escape($symb);
  726: 
  727:     my $remove= &mt('Remove');
  728: 
  729:     return <<"END_LINK";
  730:  <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
  731:    >($remove)</a>
  732: END_LINK
  733: 
  734: }
  735: 
  736: sub show_table {
  737:     my ($r,$mgr)=@_;
  738: 
  739:     my ($cnum,$cdom)=&get_course();
  740:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  741:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
  742: 	undef(%slots);
  743:     } 
  744:     my $available;
  745:     if ($mgr eq 'F') {
  746: 	$r->print('<div>');
  747: 	$r->print('<form method="POST" action="/adm/slotrequest">
  748: <input type="hidden" name="command" value="uploadstart" />
  749: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
  750: </form>');
  751: 	$r->print('<form method="POST" action="/adm/helper/newslot.helper">
  752: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
  753: </form>');
  754: 	$r->print('</div>');
  755:     }
  756:     
  757:     my %Saveable_Parameters = ('show'    => 'array',
  758: 			       'when'    => 'scalar',
  759: 			       'order'   => 'scalar',
  760: 			       'deleted' => 'scalar',
  761: 			       );
  762:     &Apache::loncommon::store_course_settings('slotrequest',
  763: 					      \%Saveable_Parameters);
  764:     &Apache::loncommon::restore_course_settings('slotrequest',
  765: 						\%Saveable_Parameters);
  766:     &Apache::grades::init_perm();
  767:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
  768:     &Apache::grades::reset_perm();
  769: 
  770:     my %show_fields=&Apache::lonlocal::texthash(
  771: 	     'name'            => 'Slot Name',
  772: 	     'description'     => 'Description',
  773: 	     'type'            => 'Type',
  774: 	     'starttime'       => 'Start time',
  775: 	     'endtime'         => 'End Time',
  776:              'startreserve'    => 'Time students can start reserving',
  777: 	     'secret'          => 'Secret Word',
  778: 	     'maxspace'        => 'Maximum # of students',
  779: 	     'ip'              => 'IP or DNS restrictions',
  780: 	     'symb'            => 'Resource slot is restricted to.',
  781: 	     'allowedsections' => 'Sections slot is restricted to.',
  782: 	     'allowedusers'    => 'Users slot is restricted to.',
  783: 	     'uniqueperiod'    => 'Period of time slot is unique',
  784: 	     'scheduled'       => 'Scheduled Students',
  785: 	     'proctor'         => 'List of proctors');
  786:     my @show_order=('name','description','type','starttime','endtime',
  787: 		    'startreserve','secret','maxspace','ip','symb',
  788: 		    'allowedsections','allowedusers','uniqueperiod',
  789: 		    'scheduled','proctor');
  790:     my @show = 
  791: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
  792: 	                            : keys(%show_fields);
  793:     my %show =  map { $_ => 1 } (@show);
  794: 
  795:     my %when_fields=&Apache::lonlocal::texthash(
  796: 	     'now'      => 'Open now',
  797: 	     'nextweek' => 'Open within the next week',
  798: 	     'lastweek' => 'Were open last week',
  799: 	     'willopen' => 'Will open later',
  800: 	     'wereopen' => 'Were open',
  801: 	     'any'      => 'Anytime',
  802: 						);
  803:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
  804:     $when_fields{'select_form_order'} = \@when_order;
  805:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
  806:                                             : 'now';
  807: 
  808:     my %stu_display_fields=
  809: 	&Apache::lonlocal::texthash('username' => 'User name',
  810: 				    'fullname' => 'Full name',
  811: 				    );
  812:     my @stu_display_order=('fullname','username');
  813:     my @stu_display = 
  814: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
  815: 	                                  : keys(%stu_display_fields);
  816:     my %stu_display =  map { $_ => 1 } (@stu_display);
  817: 
  818:     my $hide_radio = 
  819: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
  820:     my $show_radio = 
  821: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
  822: 	
  823:     $r->print('<form method="POST" action="/adm/slotrequest">
  824: <input type="hidden" name="command" value="showslots" />');
  825:     $r->print('<div>');
  826:     $r->print('<table class="inline">
  827:       <tr><th>'.&mt('Show').'</th>
  828:           <th>'.&mt('Student Display').'</th>
  829:           <th>'.&mt('Open').'</th>
  830:           <th>'.&mt('Options').'</th>
  831:       </tr>
  832:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
  833: 	      '</td>
  834:            <td>
  835:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
  836: 						    6,\%stu_display_fields,
  837: 						    \@stu_display_order).'
  838:            </td>
  839:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
  840:           '</td>
  841:            <td>
  842:             <table>
  843:               <tr>
  844:                 <td rowspan="2">Deleted slots:</td>
  845:                 <td><label>'.$show_radio.'Show</label></td>
  846:               </tr>
  847:               <tr>
  848:                 <td><label>'.$hide_radio.'Hide</label></td>
  849:               </tr>
  850:             </table>
  851: 	  </td>
  852:        </tr>
  853:     </table>');
  854:     $r->print('</div>');
  855:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
  856:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
  857:     $r->print('<table class="thinborder">
  858: <tr>
  859:   <th></th>');
  860:     foreach my $which (@show_order) {
  861: 	if ($which ne 'proctor' && exists($show{$which})) {
  862: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
  863: 	}
  864:     }
  865: 
  866:     my %name_cache;
  867:     my $slotsort = sub {
  868: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb|allowedsections|allowedusers)$/) {
  869: 	    if (lc($slots{$a}->{$env{'form.order'}})
  870: 		ne lc($slots{$b}->{$env{'form.order'}})) {
  871: 		return (lc($slots{$a}->{$env{'form.order'}}) 
  872: 			cmp lc($slots{$b}->{$env{'form.order'}}));
  873: 	    }
  874: 	} elsif ($env{'form.order'} eq 'name') {
  875: 	    if (lc($a) cmp lc($b)) {
  876: 		return lc($a) cmp lc($b);
  877: 	    }
  878: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
  879: 	    
  880: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
  881: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
  882: 		return ($slots{$a}->{'uniqueperiod'}[0]
  883: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
  884: 	    }
  885: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
  886: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
  887: 		return ($slots{$a}->{'uniqueperiod'}[1]
  888: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
  889: 	    }
  890: 	}
  891: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
  892:     };
  893:     foreach my $slot (sort $slotsort (keys(%slots)))  {
  894: 	if (!&to_show($slots{$slot},$when,$env{'form.deleted'})) { next; }
  895: 	if (defined($slots{$slot}->{'type'})
  896: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
  897: 	    #next;
  898: 	}
  899: 	my $description=&get_description($slot,$slots{$slot});
  900: 	my $ids;
  901: 	if (exists($show{'scheduled'})) {
  902: 	    my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  903: 					       "^$slot\0");
  904: 	    my ($tmp)=%consumed;
  905: 	    if ($tmp !~ /^error: /) {
  906: 		foreach my $entry (sort(keys(%consumed))) {
  907: 		    my (undef,$id)=split("\0",$entry);
  908: 		    my ($uname,$udom) = split('@',$consumed{$entry}{'name'});
  909: 		    $ids.= '<nobr>';
  910: 		    foreach my $item (@stu_display_order) {
  911: 			if ($stu_display{$item}) {
  912: 			    if ($item eq 'fullname') {
  913: 				$ids.=$fullname->{"$uname:$udom"}.' ';
  914: 			    } elsif ($item eq 'username') {
  915: 				$ids.="<tt>$uname\@$udom</tt> ";
  916: 			    }
  917: 			}
  918: 		    }
  919: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
  920: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
  921: 		}
  922: 	    }
  923: 	}
  924: 
  925: 	my $start=($slots{$slot}->{'starttime'}?
  926: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
  927: 	my $end=($slots{$slot}->{'endtime'}?
  928: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
  929: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
  930: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
  931: 	
  932: 	my $unique;
  933: 	if (ref($slots{$slot}{'uniqueperiod'})) {
  934: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
  935: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
  936: 	}
  937: 
  938: 	my $title;
  939: 	if (exists($slots{$slot}{'symb'})) {
  940: 	    my (undef,undef,$res)=
  941: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
  942: 	    $res =   &Apache::lonnet::clutter($res);
  943: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
  944: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
  945: 	}
  946: 
  947: 	my $allowedsections;
  948: 	if (exists($show{'allowedsections'})) {
  949: 	    $allowedsections = 
  950: 		join(', ',sort(split(/\s*,\s*/,
  951: 				     $slots{$slot}->{'allowedsections'})));
  952: 	}
  953: 
  954: 	my @allowedusers;
  955: 	if (exists($show{'allowedusers'})) {
  956: 	    @allowedusers= map {
  957: 		my ($uname,$udom)=split(/:/,$_);
  958: 		my $fullname=$name_cache{$_};
  959: 		if (!defined($fullname)) {
  960: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
  961: 		    $fullname =~s/\s/&nbsp;/g;
  962: 		    $name_cache{$_} = $fullname;
  963: 		}
  964: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
  965: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
  966: 	}
  967: 	my $allowedusers=join(', ',@allowedusers);
  968: 	
  969: 	my @proctors;
  970: 	my $rowspan=1;
  971: 	my $colspan=1;
  972: 	if (exists($show{'proctor'})) {
  973: 	    $rowspan=2;
  974: 	    @proctors= map {
  975: 		my ($uname,$udom)=split(/@/,$_);
  976: 		my $fullname=$name_cache{$_};
  977: 		if (!defined($fullname)) {
  978: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
  979: 		    $fullname =~s/\s/&nbsp;/g;
  980: 		    $name_cache{$_} = $fullname;
  981: 		}
  982: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
  983: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
  984: 	}
  985: 	my $proctors=join(', ',@proctors);
  986: 
  987: 	my $edit=(<<"EDITLINK");
  988: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
  989: EDITLINK
  990: 
  991: 	my $delete=(<<"DELETELINK");
  992: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
  993: DELETELINK
  994:         if ($ids ne '') { undef($delete); }
  995: 
  996:         $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
  997: 	if (exists($show{'name'})) {
  998: 	    $colspan++;$r->print("<td>$slot</td>");
  999: 	}
 1000: 	if (exists($show{'description'})) {
 1001: 	    $colspan++;$r->print("<td>$description</td>\n");
 1002: 	}
 1003: 	if (exists($show{'type'})) {
 1004: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
 1005: 	}
 1006: 	if (exists($show{'starttime'})) {
 1007: 	    $colspan++;$r->print("<td>$start</td>\n");
 1008: 	}
 1009: 	if (exists($show{'endtime'})) {
 1010: 	    $colspan++;$r->print("<td>$end</td>\n");
 1011: 	}
 1012: 	if (exists($show{'startreserve'})) {
 1013: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
 1014: 	}
 1015: 	if (exists($show{'secret'})) {
 1016: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
 1017: 	}
 1018: 	if (exists($show{'maxspace'})) {
 1019: 	    $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
 1020: 	}
 1021: 	if (exists($show{'ip'})) {
 1022: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
 1023: 	}
 1024: 	if (exists($show{'symb'})) {
 1025: 	    $colspan++;$r->print("<td>$title</td>\n");
 1026: 	}
 1027: 	if (exists($show{'allowedsections'})) {
 1028: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
 1029: 	}
 1030: 	if (exists($show{'allowedusers'})) {
 1031: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
 1032: 	}
 1033: 	if (exists($show{'scheduled'})) {
 1034: 	    $colspan++;$r->print("<td>$ids</td>\n</tr>\n");
 1035: 	}
 1036: 	if (exists($show{'proctor'})) {
 1037: 	    $r->print(<<STUFF);
 1038: <tr>
 1039:  <td colspan="$colspan">$proctors</td>
 1040: </tr>
 1041: STUFF
 1042:         }
 1043:     }
 1044:     $r->print('</table>');
 1045: }
 1046: 
 1047: sub upload_start {
 1048:     my ($r)=@_;    
 1049:     $r->print(&Apache::grades::checkforfile_js());
 1050:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
 1051:     $result.='&nbsp;<b>'.
 1052: 	&mt('Specify a file containing the slot definitions.').
 1053: 	'</b></td></tr>'."\n";
 1054:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
 1055:     my $upfile_select=&Apache::loncommon::upfile_select_html();
 1056:     my $ignore=&mt('Ignore First Line');
 1057:     $result.=<<ENDUPFORM;
 1058: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1059: <input type="hidden" name="command" value="csvuploadmap" />
 1060: $upfile_select
 1061: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
 1062: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
 1063: </form>
 1064: ENDUPFORM
 1065:     $result.='</td></tr></table>'."\n";
 1066:     $result.='</td></tr></table>'."\n";
 1067:     $r->print($result);
 1068: }
 1069: 
 1070: sub csvuploadmap_header {
 1071:     my ($r,$datatoken,$distotal)= @_;
 1072:     my $javascript;
 1073:     if ($env{'form.upfile_associate'} eq 'reverse') {
 1074: 	$javascript=&csvupload_javascript_reverse_associate();
 1075:     } else {
 1076: 	$javascript=&csvupload_javascript_forward_associate();
 1077:     }
 1078: 
 1079:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
 1080:     my $ignore=&mt('Ignore First Line');
 1081:     $r->print(<<ENDPICK);
 1082: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
 1083: <h3>Identify fields</h3>
 1084: Total number of records found in file: $distotal <hr />
 1085: Enter as many fields as you can. The system will inform you and bring you back
 1086: to this page if the data selected is insufficient to create the slots.<hr />
 1087: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
 1088: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
 1089: <input type="hidden" name="associate"  value="" />
 1090: <input type="hidden" name="datatoken"  value="$datatoken" />
 1091: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
 1092: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
 1093: <input type="hidden" name="upfile_associate" 
 1094:                                        value="$env{'form.upfile_associate'}" />
 1095: <input type="hidden" name="command"    value="csvuploadassign" />
 1096: <hr />
 1097: <script type="text/javascript" language="Javascript">
 1098: $javascript
 1099: </script>
 1100: ENDPICK
 1101:     return '';
 1102: 
 1103: }
 1104: 
 1105: sub csvuploadmap_footer {
 1106:     my ($request,$i,$keyfields) =@_;
 1107:     $request->print(<<ENDPICK);
 1108: </table>
 1109: <input type="hidden" name="nfields" value="$i" />
 1110: <input type="hidden" name="keyfields" value="$keyfields" />
 1111: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
 1112: </form>
 1113: ENDPICK
 1114: }
 1115: 
 1116: sub csvupload_javascript_reverse_associate {
 1117:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1118:     return(<<ENDPICK);
 1119:   function verify(vf) {
 1120:     var foundstart=0;
 1121:     var foundend=0;
 1122:     var foundname=0;
 1123:     var foundtype=0;
 1124:     for (i=0;i<=vf.nfields.value;i++) {
 1125:       tw=eval('vf.f'+i+'.selectedIndex');
 1126:       if (i==0 && tw!=0) { foundname=1; }
 1127:       if (i==1 && tw!=0) { foundtype=1; }
 1128:       if (i==2 && tw!=0) { foundstat=1; }
 1129:       if (i==3 && tw!=0) { foundend=1; }
 1130:     }
 1131:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1132: 	alert('$error1');
 1133: 	return;
 1134:     }
 1135:     vf.submit();
 1136:   }
 1137:   function flip(vf,tf) {
 1138:   }
 1139: ENDPICK
 1140: }
 1141: 
 1142: sub csvupload_javascript_forward_associate {
 1143:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
 1144:   return(<<ENDPICK);
 1145:   function verify(vf) {
 1146:     var foundstart=0;
 1147:     var foundend=0;
 1148:     var foundname=0;
 1149:     var foundtype=0;
 1150:     for (i=0;i<=vf.nfields.value;i++) {
 1151:       tw=eval('vf.f'+i+'.selectedIndex');
 1152:       if (tw==1) { foundname=1; }
 1153:       if (tw==2) { foundtype=1; }
 1154:       if (tw==3) { foundstat=1; }
 1155:       if (tw==4) { foundend=1; }
 1156:     }
 1157:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
 1158: 	alert('$error1');
 1159: 	return;
 1160:     }
 1161:     vf.submit();
 1162:   }
 1163:   function flip(vf,tf) {
 1164:   }
 1165: ENDPICK
 1166: }
 1167: 
 1168: sub csv_upload_map {
 1169:     my ($r)= @_;
 1170: 
 1171:     my $datatoken;
 1172:     if (!$env{'form.datatoken'}) {
 1173: 	$datatoken=&Apache::loncommon::upfile_store($r);
 1174:     } else {
 1175: 	$datatoken=$env{'form.datatoken'};
 1176: 	&Apache::loncommon::load_tmp_file($r);
 1177:     }
 1178:     my @records=&Apache::loncommon::upfile_record_sep();
 1179:     if ($env{'form.noFirstLine'}) { shift(@records); }
 1180:     &csvuploadmap_header($r,$datatoken,$#records+1);
 1181:     my ($i,$keyfields);
 1182:     if (@records) {
 1183: 	my @fields=&csvupload_fields();
 1184: 
 1185: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
 1186: 	    &Apache::loncommon::csv_print_samples($r,\@records);
 1187: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
 1188: 							  \@fields);
 1189: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
 1190: 	    chop($keyfields);
 1191: 	} else {
 1192: 	    unshift(@fields,['none','']);
 1193: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
 1194: 							    \@fields);
 1195: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
 1196: 	    $keyfields=join(',',sort(keys(%sone)));
 1197: 	}
 1198:     }
 1199:     &csvuploadmap_footer($r,$i,$keyfields);
 1200: 
 1201:     return '';
 1202: }
 1203: 
 1204: sub csvupload_fields {
 1205:     return (['name','Slot name'],
 1206: 	    ['type','Type of slot'],
 1207: 	    ['starttime','Start Time of slot'],
 1208: 	    ['endtime','End Time of slot'],
 1209: 	    ['startreserve','Reservation Start Time'],
 1210: 	    ['ip','IP or DNS restriction'],
 1211: 	    ['proctor','List of proctor ids'],
 1212: 	    ['description','Slot Description'],
 1213: 	    ['maxspace','Maximum number of reservations'],
 1214: 	    ['symb','Resource Restriction'],
 1215: 	    ['uniqueperiod','Date range of slot exclusion'],
 1216: 	    ['secret','Secret word proctor uses to validate'],
 1217: 	    ['allowedsections','Sections slot is restricted to'],
 1218: 	    ['allowedusers','Users slot is restricted to'],
 1219: 	    );
 1220: }
 1221: 
 1222: sub csv_upload_assign {
 1223:     my ($r,$mgr)= @_;
 1224:     &Apache::loncommon::load_tmp_file($r);
 1225:     my @slotdata = &Apache::loncommon::upfile_record_sep();
 1226:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
 1227:     my %fields=&Apache::grades::get_fields();
 1228:     $r->print('<h3>Creating Slots</h3>');
 1229:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
 1230:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 1231:     my $countdone=0;
 1232:     my @errors;
 1233:     foreach my $slot (@slotdata) {
 1234: 	my %slot;
 1235: 	my %entries=&Apache::loncommon::record_sep($slot);
 1236: 	my $domain;
 1237: 	my $name=$entries{$fields{'name'}};
 1238: 	if ($name=~/^\s*$/) {
 1239: 	    push(@errors,"Did not create slot with no name");
 1240: 	    next;
 1241: 	}
 1242: 	if ($name=~/\s/) { 
 1243: 	    push(@errors,"$name not created -- Name must not contain spaces");
 1244: 	    next;
 1245: 	}
 1246: 	if ($name=~/\W/) { 
 1247: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
 1248: 	    next;
 1249: 	}
 1250: 	if ($entries{$fields{'type'}}) {
 1251: 	    $slot{'type'}=$entries{$fields{'type'}};
 1252: 	} else {
 1253: 	    $slot{'type'}='preassigned';
 1254: 	}
 1255: 	if ($slot{'type'} ne 'preassigned' &&
 1256: 	    $slot{'type'} ne 'schedulable_student') {
 1257: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
 1258: 	    next;
 1259: 	}
 1260: 	if ($entries{$fields{'starttime'}}) {
 1261: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
 1262: 	}
 1263: 	if ($entries{$fields{'endtime'}}) {
 1264: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
 1265: 	}
 1266: 	if ($entries{$fields{'startreserve'}}) {
 1267: 	    $slot{'startreserve'}=
 1268: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
 1269: 	}
 1270: 	foreach my $key ('ip','proctor','description','maxspace',
 1271: 			 'secret','symb') {
 1272: 	    if ($entries{$fields{$key}}) {
 1273: 		$slot{$key}=$entries{$fields{$key}};
 1274: 	    }
 1275: 	}
 1276: 	if ($entries{$fields{'uniqueperiod'}}) {
 1277: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
 1278: 	    my @times=(&UnixDate($start,"%s"),
 1279: 		       &UnixDate($end,"%s"));
 1280: 	    $slot{'uniqueperiod'}=\@times;
 1281: 	}
 1282: 
 1283: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
 1284: 	$r->print('.');
 1285: 	$r->rflush();
 1286: 	$countdone++;
 1287:     }
 1288:     $r->print("<p>Created $countdone slots\n</p>");
 1289:     foreach my $error (@errors) {
 1290: 	$r->print("<p>$error\n</p>");
 1291:     }
 1292:     &show_table($r,$mgr);
 1293:     return '';
 1294: }
 1295: 
 1296: sub handler {
 1297:     my $r=shift;
 1298: 
 1299:     &Apache::loncommon::content_type($r,'text/html');
 1300:     &Apache::loncommon::no_cache($r);
 1301:     if ($r->header_only()) {
 1302: 	$r->send_http_header();
 1303: 	return OK;
 1304:     }
 1305: 
 1306:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
 1307:     
 1308:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
 1309:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
 1310:     my $title='Requesting Another Worktime';
 1311:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
 1312: 	$title = 'Managing Slots';
 1313:     }
 1314:     &start_page($r,$title);
 1315: 
 1316:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
 1317: 	&show_table($r,$mgr);
 1318:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
 1319: 	&remove_registration($r);
 1320:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
 1321: 	&release_slot($r,undef,undef,undef,$mgr);
 1322:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
 1323: 	&delete_slot($r);
 1324:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
 1325: 	&upload_start($r);
 1326:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
 1327: 	&csv_upload_map($r);
 1328:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
 1329: 	if ($env{'form.associate'} ne 'Reverse Association') {
 1330: 	    &csv_upload_assign($r,$mgr);
 1331: 	} else {
 1332: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
 1333: 		$env{'form.upfile_associate'} = 'reverse';
 1334: 	    } else {
 1335: 		$env{'form.upfile_associate'} = 'forward';
 1336: 	    }
 1337: 	    &csv_upload_map($r);
 1338: 	}
 1339:     } else {
 1340: 	my $symb=&Apache::lonnet::unescape($env{'form.symb'});
 1341: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
 1342: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
 1343: 	if ($useslots ne 'resource') {
 1344: 	    &fail($r,'not_valid');
 1345: 	    return OK;
 1346: 	}
 1347: 	$env{'request.symb'}=$symb;
 1348: 	my $type = ($res =~ /\.task$/) ? 'Task'
 1349: 	                               : 'problem';
 1350: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
 1351: 	if ($status eq 'CAN_ANSWER' ||
 1352: 	    $status eq 'NEEDS_CHECKIN' ||
 1353: 	    $status eq 'WAITING_FOR_GRADE') {
 1354: 	    &fail($r,'not_allowed');
 1355: 	    return OK;
 1356: 	}
 1357: 	if ($env{'form.requestattempt'}) {
 1358: 	    &show_choices($r,$symb);
 1359: 	} elsif ($env{'form.command'} eq 'release') {
 1360: 	    &release_slot($r,$symb);
 1361: 	} elsif ($env{'form.command'} eq 'get') {
 1362: 	    &get_slot($r,$symb);
 1363: 	} elsif ($env{'form.command'} eq 'change') {
 1364: 	    if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
 1365: 		&get_slot($r,$symb);
 1366: 	    }
 1367: 	} else {
 1368: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
 1369: 	}
 1370:     }
 1371:     &end_page($r);
 1372:     return OK;
 1373: }
 1374: 
 1375: 1;
 1376: __END__

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