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

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

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