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

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

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