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

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

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