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

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

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