Annotation of loncom/interface/slotrequest.pm, revision 1.43

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

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