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

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

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