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

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

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