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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for requesting to have slots added to a students record
                      3: #
1.81    ! raeburn     4: # $Id: slotrequest.pm,v 1.80 2007/09/21 22:37:23 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.48      albertel   37: use Apache::lonnavmaps();
1.27      albertel   38: use Date::Manip;
1.63      www        39: use lib '/home/httpd/lib/perl/';
                     40: use LONCAPA;
1.1       albertel   41: 
                     42: sub fail {
                     43:     my ($r,$code)=@_;
1.2       albertel   44:     if ($code eq 'not_valid') {
1.8       albertel   45: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
1.61      albertel   46:     } elsif ($code eq 'not_available') {
                     47: 	$r->print('<p>'.&mt('No slots are available.').'</p>');
1.8       albertel   48:     } elsif ($code eq 'not_allowed') {
                     49: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
                     50:     } else {
                     51: 	$r->print('<p>'.&mt('Failed.').'</p>');
1.2       albertel   52:     }
1.8       albertel   53:     
1.42      albertel   54:     &return_link($r);
1.1       albertel   55:     &end_page($r);
                     56: }
                     57: 
                     58: sub start_page {
1.28      albertel   59:     my ($r,$title)=@_;
1.52      albertel   60:     $r->print(&Apache::loncommon::start_page($title));
1.1       albertel   61: }
                     62: 
                     63: sub end_page {
                     64:     my ($r)=@_;
1.52      albertel   65:     $r->print(&Apache::loncommon::end_page());
1.1       albertel   66: }
                     67: 
1.2       albertel   68: =pod
                     69: 
                     70:  slot_reservations db
                     71:    - keys are 
                     72:     - slotname\0id -> value is an hashref of
                     73:                          name -> user@domain of holder
                     74:                          timestamp -> timestamp of reservation
                     75:                          symb -> symb of resource that it is reserved for
                     76: 
                     77: =cut
                     78: 
                     79: sub get_course {
1.69      albertel   80:     (undef,my $courseid)=&Apache::lonnet::whichuser();
1.2       albertel   81:     my $cdom=$env{'course.'.$courseid.'.domain'};
                     82:     my $cnum=$env{'course.'.$courseid.'.num'};
                     83:     return ($cnum,$cdom);
                     84: }
                     85: 
                     86: sub get_reservation_ids {
                     87:     my ($slot_name)=@_;
                     88:     
                     89:     my ($cnum,$cdom)=&get_course();
                     90: 
                     91:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                     92: 				       "^$slot_name\0");
1.67      albertel   93:     if (&Apache::lonnet::error(%consumed)) { 
1.40      albertel   94: 	return 'error: Unable to determine current status';
                     95:     }
1.2       albertel   96:     my ($tmp)=%consumed;
                     97:     if ($tmp=~/^error: 2 / ) {
                     98: 	return 0;
                     99:     }
                    100:     return keys(%consumed);
                    101: }
                    102: 
                    103: sub space_available {
                    104:     my ($slot_name,$slot)=@_;
                    105:     my $max=$slot->{'maxspace'};
                    106: 
                    107:     if (!defined($max)) { return 1; }
                    108: 
                    109:     my $consumed=scalar(&get_reservation_ids($slot_name));
                    110:     if ($consumed < $max) {
                    111: 	return 1
                    112:     }
                    113:     return 0;
                    114: }
1.3       albertel  115: 
1.4       albertel  116: sub check_for_reservation {
1.43      albertel  117:     my ($symb,$mode)=@_;
1.4       albertel  118:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
                    119: 				       $env{'user.domain'}, $env{'user.name'});
                    120: 
                    121:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
                    122: 				    $env{'user.domain'}, $env{'user.name'});
                    123:     my @slots = (split(/:/,$student), split(/:/, $course));
                    124: 
                    125:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
                    126: 
                    127:     my ($cnum,$cdom)=&get_course();
                    128:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
                    129: 
1.67      albertel  130:     if (&Apache::lonnet::error($student) 
                    131: 	|| &Apache::lonnet::error($course)
                    132: 	|| &Apache::lonnet::error(%slots)) {
1.41      albertel  133: 	return 'error: Unable to determine current status';
                    134:     }    
1.43      albertel  135:     my @got;
                    136:     foreach my $slot_name (sort {
                    137: 	if (ref($slots{$a}) && ref($slots{$b})) {
                    138: 	    return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
                    139: 	}
                    140: 	if (ref($slots{$a})) { return -1;}
                    141: 	if (ref($slots{$b})) { return 1;}
                    142: 	return 0;
                    143:     } @slots) {
1.4       albertel  144: 	next if (!defined($slots{$slot_name}) ||
                    145: 		 !ref($slots{$slot_name}));
                    146: 	&Apache::lonxml::debug(time." $slot_name ".
                    147: 			       $slots{$slot_name}->{'starttime'}." -- ".
                    148: 			       $slots{$slot_name}->{'startreserve'});
1.7       albertel  149: 	if ($slots{$slot_name}->{'endtime'} > time &&
1.4       albertel  150: 	    $slots{$slot_name}->{'startreserve'} < time) {
1.7       albertel  151: 	    # between start of reservation times and end of slot
1.43      albertel  152: 	    if ($mode eq 'allslots') {
                    153: 		push(@got,$slot_name);
                    154: 	    } else {
                    155: 		return($slot_name, $slots{$slot_name});
                    156: 	    }
1.4       albertel  157: 	}
                    158:     }
1.43      albertel  159:     if ($mode eq 'allslots' && @got) {
                    160: 	return @got;
                    161:     }
1.4       albertel  162:     return (undef,undef);
                    163: }
                    164: 
1.48      albertel  165: sub get_consumed_uniqueperiods {
                    166:     my ($slots) = @_;
                    167:     my $navmap=Apache::lonnavmaps::navmap->new;
                    168:     my @problems = $navmap->retrieveResources(undef,
                    169: 					      sub { $_[0]->is_problem() },1,0);
                    170:     my %used_slots;
                    171:     foreach my $problem (@problems) {
                    172: 	my $symb = $problem->symb();
                    173: 	my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
                    174: 					   $symb, $env{'user.domain'},
                    175: 					   $env{'user.name'});
                    176: 	my $course =  &Apache::lonnet::EXT("resource.0.available",
                    177: 					   $symb, $env{'user.domain'},
                    178: 					   $env{'user.name'});
1.67      albertel  179: 	if (&Apache::lonnet::error($student) 
                    180: 	    || &Apache::lonnet::error($course)) {
1.48      albertel  181: 	    return 'error: Unable to determine current status';
                    182: 	}
                    183: 	foreach my $slot (split(/:/,$student), split(/:/, $course)) {
                    184: 	    $used_slots{$slot}=1;
                    185: 	}
                    186:     }
1.43      albertel  187: 
                    188:     if (!ref($slots)) {
1.48      albertel  189: 	my ($cnum,$cdom)=&get_course();
                    190: 	my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
1.67      albertel  191: 	if (&Apache::lonnet::error(%slots)) {
1.48      albertel  192: 	    return 'error: Unable to determine current status';
                    193: 	}
1.43      albertel  194: 	$slots = \%slots;
                    195:     }
1.41      albertel  196: 
1.48      albertel  197:     my %consumed_uniqueperiods;
                    198:     foreach my $slot_name (keys(%used_slots)) {
1.43      albertel  199: 	next if (!defined($slots->{$slot_name}) ||
                    200: 		 !ref($slots->{$slot_name}));
1.48      albertel  201: 	
1.43      albertel  202:         next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
                    203: 		 !ref($slots->{$slot_name}{'uniqueperiod'}));
1.48      albertel  204: 	$consumed_uniqueperiods{$slot_name} = 
                    205: 	    $slots->{$slot_name}{'uniqueperiod'};
                    206:     }
                    207:     return \%consumed_uniqueperiods;
                    208: }
                    209: 
                    210: sub check_for_conflict {
                    211:     my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
                    212: 
                    213:     if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
                    214: 
                    215:     if (!ref($consumed_uniqueperiods)) {
                    216: 	$consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
1.67      albertel  217: 	if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
1.48      albertel  218: 	    return 'error: Unable to determine current status';
                    219: 	}
                    220:     }
                    221:     
                    222:     my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
                    223:     foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
                    224: 	my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
1.43      albertel  225: 	if (!
                    226: 	    ($start < $new_uniq_start &&  $end < $new_uniq_start) ||
                    227: 	    ($start > $new_uniq_end   &&  $end > $new_uniq_end  )) {
1.5       albertel  228: 	    return $slot_name;
                    229: 	}
                    230:     }
                    231:     return undef;
                    232: 
                    233: }
                    234: 
1.2       albertel  235: sub make_reservation {
                    236:     my ($slot_name,$slot,$symb)=@_;
1.3       albertel  237: 
                    238:     my ($cnum,$cdom)=&get_course();
                    239: 
                    240:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
                    241: 				   $env{'user.domain'},$env{'user.name'});
                    242:     &Apache::lonxml::debug("value is  $value<br />");
1.59      albertel  243: 
1.80      albertel  244:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",$symb,
                    245: 					 $env{'user.domain'},$env{'user.name'});
1.59      albertel  246:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
                    247: 
1.67      albertel  248:     if (&Apache::lonnet::error($value) 
                    249: 	|| &Apache::lonnet::error($use_slots)) { 
1.40      albertel  250: 	return 'error: Unable to determine current status';
                    251:     }
                    252: 
1.59      albertel  253:     my $parm_symb  = $symb;
                    254:     my $parm_level = 1;
1.66      albertel  255:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
1.59      albertel  256: 	my ($map) = &Apache::lonnet::decode_symb($symb);
                    257: 	$parm_symb = &Apache::lonnet::symbread($map);
                    258: 	$parm_level = 2;
                    259:     }
                    260: 
1.3       albertel  261:     foreach my $other_slot (split(/:/, $value)) {
                    262: 	if ($other_slot eq $slot_name) {
                    263: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
                    264: 					       $cnum, "^$slot_name\0");   
1.67      albertel  265: 	    if (&Apache::lonnet::error($value)) { 
1.40      albertel  266: 		return 'error: Unable to determine current status';
                    267: 	    }
1.57      albertel  268: 	    my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.3       albertel  269: 	    foreach my $key (keys(%consumed)) {
                    270: 		if ($consumed{$key}->{'name'} eq $me) {
                    271: 		    my $num=(split('\0',$key))[1];
                    272: 		    return -$num;
                    273: 		}
                    274: 	    }
                    275: 	}
                    276:     }
                    277: 
1.2       albertel  278:     my $max=$slot->{'maxspace'};
1.3       albertel  279:     if (!defined($max)) { $max=99999; }
1.2       albertel  280: 
                    281:     my (@ids)=&get_reservation_ids($slot_name);
1.67      albertel  282:     if (&Apache::lonnet::error(@ids)) { 
1.40      albertel  283: 	return 'error: Unable to determine current status';
                    284:     }
1.2       albertel  285:     my $last=0;
                    286:     foreach my $id (@ids) {
                    287: 	my $num=(split('\0',$id))[1];
                    288: 	if ($num > $last) { $last=$num; }
                    289:     }
                    290:     
                    291:     my $wanted=$last+1;
1.3       albertel  292:     &Apache::lonxml::debug("wanted $wanted<br />");
1.7       albertel  293:     if (scalar(@ids) >= $max) {
1.2       albertel  294: 	# full up
1.7       albertel  295: 	return undef;
1.2       albertel  296:     }
                    297:     
1.57      albertel  298:     my %reservation=('name'      => $env{'user.name'}.':'.$env{'user.domain'},
1.2       albertel  299: 		     'timestamp' => time,
1.59      albertel  300: 		     'symb'      => $parm_symb);
1.2       albertel  301: 
                    302:     my $success=&Apache::lonnet::newput('slot_reservations',
                    303: 					{"$slot_name\0$wanted" =>
                    304: 					     \%reservation},
1.3       albertel  305: 					$cdom, $cnum);
                    306: 
1.2       albertel  307:     if ($success eq 'ok') {
1.3       albertel  308: 	my $new_value=$slot_name;
                    309: 	if ($value) {
                    310: 	    $new_value=$value.':'.$new_value;
                    311: 	}
                    312: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
                    313: 						      '0_availablestudent',
1.59      albertel  314: 						       $parm_level, $new_value,
                    315: 						       'string',
1.3       albertel  316: 						       $env{'user.name'},
                    317: 					               $env{'user.domain'});
                    318: 	&Apache::lonxml::debug("hrrm $result");
1.2       albertel  319: 	return $wanted;
                    320:     }
1.3       albertel  321: 
1.2       albertel  322:     # someone else got it
1.3       albertel  323:     return undef;
                    324: }
                    325: 
1.33      albertel  326: sub remove_registration {
                    327:     my ($r) = @_;
1.55      albertel  328:     if ($env{'form.entry'} ne 'remove all') {
                    329: 	return &remove_registration_user($r);
                    330:     }
                    331:     my $slot_name = $env{'form.slotname'};
                    332:     my %slot=&Apache::lonnet::get_slot($slot_name);
                    333: 
                    334:     my ($cnum,$cdom)=&get_course();
                    335:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    336: 				       "^$slot_name\0");
1.67      albertel  337:     if (&Apache::lonnet::error(%consumed)) {
1.78      albertel  338: 	$r->print("<p><span class=\"LC_error\">".&mt('A network error has occured.').'</span></p>');
1.55      albertel  339: 	return;
                    340:     }
                    341:     if (!%consumed) {
                    342: 	$r->print("<p>".&mt('Slot <tt>[_1]</tt> has no reservations.',
                    343: 			    $slot_name)."</p>");
                    344: 	return;
                    345:     }
                    346: 
                    347:     my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed)));
                    348:     my $names = join(' ',@names);
                    349: 
                    350:     my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name);
                    351:     &remove_registration_confirmation($r,$msg,['entry','slotname']);
                    352: }
                    353: 
                    354: sub remove_registration_user {
                    355:     my ($r) = @_;
                    356:     
                    357:     my $slot_name = $env{'form.slotname'};
                    358: 
1.33      albertel  359:     my $name = &Apache::loncommon::plainname($env{'form.uname'},
                    360: 					     $env{'form.udom'});
                    361: 
                    362:     my $title = &Apache::lonnet::gettitle($env{'form.symb'});
                    363: 
1.55      albertel  364:     my $msg = &mt('Remove [_1] from slot [_2] for [_3]',
                    365: 		  $name,$slot_name,$title);
                    366:     
                    367:     &remove_registration_confirmation($r,$msg,['uname','udom','slotname',
                    368: 					       'entry','symb']);
                    369: }
                    370: 
                    371: sub remove_registration_confirmation {
                    372:     my ($r,$msg,$inputs) =@_;
                    373: 
1.33      albertel  374:     my $hidden_input;
1.55      albertel  375:     foreach my $parm (@{$inputs}) {
1.33      albertel  376: 	$hidden_input .=
                    377: 	    '<input type="hidden" name="'.$parm.'" value="'
                    378: 	    .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
                    379:     }
1.55      albertel  380:     my %lt = &Apache::lonlocal::texthash('yes' => 'Yes',
                    381: 					 'no'  => 'No',);
1.33      albertel  382:     $r->print(<<"END_CONFIRM");
1.55      albertel  383: <p> $msg </p>
1.64      albertel  384: <form action="/adm/slotrequest" method="post">
1.33      albertel  385:     <input type="hidden" name="command" value="release" />
1.55      albertel  386:     <input type="hidden" name="button" value="yes" />
1.33      albertel  387:     $hidden_input
1.55      albertel  388:     <input type="submit" value="$lt{'yes'}" />
1.33      albertel  389: </form>
1.64      albertel  390: <form action="/adm/slotrequest" method="post">
1.33      albertel  391:     <input type="hidden" name="command" value="showslots" />
1.55      albertel  392:     <input type="submit" value="$lt{'no'}" />
1.33      albertel  393: </form>
                    394: END_CONFIRM
                    395: 
                    396: }
                    397: 
1.55      albertel  398: sub release_all_slot {
                    399:     my ($r,$mgr)=@_;
                    400:     
                    401:     my $slot_name = $env{'form.slotname'};
                    402: 
                    403:     my ($cnum,$cdom)=&get_course();
                    404: 
                    405:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    406: 				       "^$slot_name\0");
                    407:     
                    408:     $r->print('<p>'.&mt('Releasing reservations').'</p>');
                    409: 
                    410:     foreach my $entry (sort { $consumed{$a}{'name'} cmp 
                    411: 				  $consumed{$b}{'name'} } (keys(%consumed))) {
1.57      albertel  412: 	my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.55      albertel  413: 	my ($result,$msg) =
                    414: 	    &release_reservation($slot_name,$uname,$udom,
                    415: 				 $consumed{$entry}{'symb'},$mgr);
                    416: 	$r->print("<p>$msg</p>");
                    417: 	$r->rflush();
                    418:     }
                    419:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    420: 	      &mt('Return to slot list').'</a></p>');
                    421:     &return_link($r);
                    422: }
                    423: 
1.5       albertel  424: sub release_slot {
1.33      albertel  425:     my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
1.6       albertel  426: 
                    427:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
                    428: 
1.33      albertel  429:     my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
                    430:     if ($mgr eq 'F' 
                    431: 	&& defined($env{'form.uname'}) && defined($env{'form.udom'})) {
                    432: 	($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
                    433:     }
                    434: 
                    435:     if ($mgr eq 'F' 
                    436: 	&& defined($env{'form.symb'})) {
1.71      albertel  437: 	$symb = &unescape($env{'form.symb'});
1.33      albertel  438:     }
1.55      albertel  439: 
                    440:     my ($result,$msg) =
                    441: 	&release_reservation($slot_name,$uname,$udom,$symb,$mgr);
                    442:     $r->print("<p>$msg</p>");
                    443:     
                    444:     if ($mgr eq 'F') {
                    445: 	$r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    446: 		  &mt('Return to slot list').'</a></p>');
                    447:     }
                    448: 
                    449:     if (!$inhibit_return_link) { &return_link($r);  }
                    450:     return $result;
                    451: }
                    452: 
                    453: sub release_reservation {
                    454:     my ($slot_name,$uname,$udom,$symb,$mgr) = @_;
1.39      albertel  455:     my %slot=&Apache::lonnet::get_slot($slot_name);
1.55      albertel  456:     my $description=&get_description($slot_name,\%slot);
1.33      albertel  457: 
1.39      albertel  458:     if ($mgr ne 'F') {
1.43      albertel  459: 	if ($slot{'starttime'} < time) {
1.55      albertel  460: 	    return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description));
1.39      albertel  461: 	}
                    462:     }
1.80      albertel  463: 
                    464:     # if the reservation symb is for a map get a resource in that map
                    465:     # to check slot parameters on
                    466:     my $navmap=Apache::lonnavmaps::navmap->new;
                    467:     my $passed_resource = $navmap->getBySymb($symb);
                    468:     if ($passed_resource->is_map()) {
                    469: 	my ($a_resource) = 
                    470: 	    $navmap->retrieveResources($passed_resource, 
                    471: 				       sub {$_[0]->is_problem()},0,1);
                    472: 	$symb = $a_resource->symb();
                    473:     }
                    474: 
1.5       albertel  475:     # get parameter string, check for existance, rebuild string with the slot
1.81    ! raeburn   476:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
        !           477:                                        $symb,$udom,$uname);
        !           478:     my @slots = split(/:/,$student);
1.33      albertel  479: 
1.6       albertel  480:     my @new_slots;
                    481:     foreach my $exist_slot (@slots) {
                    482: 	if ($exist_slot eq $slot_name) { next; }
                    483: 	push(@new_slots,$exist_slot);
                    484:     }
                    485:     my $new_param = join(':',@new_slots);
1.5       albertel  486: 
1.55      albertel  487:     my ($cnum,$cdom)=&get_course();
                    488: 
1.5       albertel  489:     # get slot reservations, check if user has one, if so remove reservation
1.6       albertel  490:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    491: 				       "^$slot_name\0");
                    492:     foreach my $entry (keys(%consumed)) {
1.57      albertel  493: 	if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
1.6       albertel  494: 	    &Apache::lonnet::del('slot_reservations',[$entry],
                    495: 				 $cdom,$cnum);
                    496: 	}
                    497:     }
1.33      albertel  498: 
1.80      albertel  499:     my $use_slots = &Apache::lonnet::EXT("resource.0.useslots",
                    500: 					 $symb,$udom,$uname);
1.59      albertel  501:     &Apache::lonxml::debug("use_slots is  $use_slots<br />");
                    502: 
1.67      albertel  503:     if (&Apache::lonnet::error($use_slots)) { 
1.59      albertel  504: 	return (0,'error: Unable to determine current status');
                    505:     }
                    506: 
                    507:     my $parm_level = 1;
1.66      albertel  508:     if ($use_slots eq 'map' || $use_slots eq 'map_map') {
1.59      albertel  509: 	$parm_level = 2;
                    510:     }
1.5       albertel  511:     # store new parameter string
1.6       albertel  512:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
                    513: 						      '0_availablestudent',
1.59      albertel  514: 						      $parm_level, $new_param,
                    515: 						      'string', $uname, $udom);
1.55      albertel  516: 
                    517:     my $msg;
1.33      albertel  518:     if ($mgr eq 'F') {
1.55      albertel  519: 	$msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
                    520:     } else {
                    521: 	$msg = &mt('Released Reservation: [_1]',$description);
1.33      albertel  522:     }
1.55      albertel  523:     return (1,$msg);
1.5       albertel  524: }
                    525: 
1.34      albertel  526: sub delete_slot {
                    527:     my ($r)=@_;
                    528: 
                    529:     my $slot_name = $env{'form.slotname'};
                    530:     my %slot=&Apache::lonnet::get_slot($slot_name);
                    531: 
                    532:     my ($cnum,$cdom)=&get_course();
                    533:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
                    534: 				       "^$slot_name\0");
1.38      albertel  535:     my ($tmp) = %consumed;
                    536:     if ($tmp =~ /error: 2/) { undef(%consumed); }
1.34      albertel  537: 
                    538:     if (%slot && !%consumed) {
                    539: 	$slot{'type'} = 'deleted';
                    540: 	my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
                    541: 					$cdom, $cnum);
                    542: 	if ($ret eq 'ok') {
                    543: 	    $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
                    544: 	} else {
1.78      albertel  545: 	    $r->print("<p><span class=\"LC_error\"> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</span></p>");
1.34      albertel  546: 	}
                    547:     } else {
                    548: 	if (%consumed) {
                    549: 	    $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
                    550: 	} else {
                    551: 	    $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
                    552: 	}
                    553:     }
                    554:     $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
                    555: 	      &mt('Return to slot list').'</a></p>');
1.42      albertel  556:     &return_link($r);
1.34      albertel  557: }
                    558: 
1.40      albertel  559: sub return_link {
                    560:     my ($r) = @_;
                    561:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
                    562: 	      &mt('Return to last resource').'</a></p>');
                    563: }
                    564: 
1.3       albertel  565: sub get_slot {
1.75      albertel  566:     my ($r,$symb,$conflictable_slot,$inhibit_return_link)=@_;
1.3       albertel  567: 
1.43      albertel  568:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
                    569:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
1.40      albertel  570: 
                    571:     if ($slot_name =~ /^error: (.*)/) {
1.78      albertel  572: 	$r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
1.40      albertel  573: 	&return_link($r);
1.75      albertel  574: 	return 0;
1.40      albertel  575:     }
1.75      albertel  576:     if ($slot_name && $slot_name ne $conflictable_slot) {
1.5       albertel  577: 	my %slot=&Apache::lonnet::get_slot($slot_name);
1.6       albertel  578: 	my $description1=&get_description($slot_name,\%slot);
                    579: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
                    580: 	my $description2=&get_description($env{'form.slotname'},\%slot);
                    581: 	$r->print("<p>Already have a reservation: $description1</p>");
1.7       albertel  582: 	if ($slot_name ne $env{'form.slotname'}) {
                    583: 	    $r->print(<<STUFF);
1.64      albertel  584: <form method="post" action="/adm/slotrequest">
1.6       albertel  585:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    586:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
                    587:    <input type="hidden" name="releaseslot" value="$slot_name" />
                    588:    <input type="hidden" name="command" value="change" />
                    589: STUFF
1.7       albertel  590:             $r->print("<p>You can either ");
                    591: 	    $r->print(<<STUFF);
1.6       albertel  592:    <input type="submit" name="change" value="Change" />
                    593: STUFF
1.7       albertel  594: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
                    595: 		      $description2.
1.40      albertel  596: 		      '</b> <br />or </p>');
                    597: 	    &return_link($r);
1.7       albertel  598: 	    $r->print(<<STUFF);
1.6       albertel  599: </form>
                    600: STUFF
1.7       albertel  601:         } else {
1.40      albertel  602: 	    &return_link($r);
1.7       albertel  603: 	}
1.75      albertel  604: 	return 0;
1.5       albertel  605:     }
1.45      albertel  606: 
1.3       albertel  607:     my $reserved=&make_reservation($env{'form.slotname'},
                    608: 				   \%slot,$symb);
                    609:     my $description=&get_description($env{'form.slotname'},\%slot);
1.7       albertel  610:     if (defined($reserved)) {
1.75      albertel  611: 	my $retvalue = 0;
1.40      albertel  612: 	if ($slot_name =~ /^error: (.*)/) {
1.78      albertel  613: 	    $r->print("<p><span class=\"LC_error\">An error occured while attempting to make a reservation. ($1)</span></p>");
1.40      albertel  614: 	} elsif ($reserved > -1) {
1.7       albertel  615: 	    $r->print("<p>Success: $description</p>");
1.75      albertel  616: 	    $retvalue = 1;
1.7       albertel  617: 	} elsif ($reserved < 0) {
                    618: 	    $r->print("<p>Already reserved: $description</p>");
                    619: 	}
1.75      albertel  620: 	if (!$inhibit_return_link) { &return_link($r); }
                    621: 	return 1;
1.3       albertel  622:     }
                    623: 
1.7       albertel  624:     my %lt=('request'=>"Availibility list",
1.3       albertel  625: 	    'try'    =>'Try again');
                    626:     %lt=&Apache::lonlocal::texthash(%lt);
                    627: 
1.75      albertel  628:     my $extra_input;
                    629:     if ($conflictable_slot) {
                    630: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
                    631:     }
                    632: 
1.3       albertel  633:     $r->print(<<STUFF);
1.78      albertel  634: <p> <span class="LC_warning">Failed</span> to reserve a spot for $description. </p>
1.3       albertel  635: <p>
1.64      albertel  636: <form method="post" action="/adm/slotrequest">
1.3       albertel  637:    <input type="submit" name="Try Again" value="$lt{'try'}" />
                    638:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    639:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
1.75      albertel  640:    <input type="hidden" name="command" value="$env{'form.command'}" />
                    641:    $extra_input
1.3       albertel  642: </form>
                    643: ?
                    644: </p>
                    645: <p>
                    646: or
1.64      albertel  647: <form method="post" action="/adm/slotrequest">
1.3       albertel  648:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    649:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
                    650: </form>
                    651: </p>
                    652: or
                    653: STUFF
1.42      albertel  654: 
1.75      albertel  655:     if (!$inhibit_return_link) { &return_link($r); }
                    656:     return 0;
1.3       albertel  657: }
                    658: 
                    659: sub allowed_slot {
1.48      albertel  660:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
1.49      albertel  661: 
1.3       albertel  662:     #already started
                    663:     if ($slot->{'starttime'} < time) {
1.76      albertel  664: 	return 0;
1.3       albertel  665:     }
1.5       albertel  666:     &Apache::lonxml::debug("$slot_name starttime good");
1.49      albertel  667: 
1.3       albertel  668:     #already ended
                    669:     if ($slot->{'endtime'} < time) {
                    670: 	return 0;
                    671:     }
1.5       albertel  672:     &Apache::lonxml::debug("$slot_name endtime good");
1.49      albertel  673: 
1.3       albertel  674:     # not allowed to pick this one
                    675:     if (defined($slot->{'type'})
                    676: 	&& $slot->{'type'} ne 'schedulable_student') {
                    677: 	return 0;
                    678:     }
1.5       albertel  679:     &Apache::lonxml::debug("$slot_name type good");
1.49      albertel  680: 
1.53      albertel  681:     # reserve time not yet started
                    682:     if ($slot->{'startreserve'} > time) {
                    683: 	return 0;
                    684:     }
                    685:     &Apache::lonxml::debug("$slot_name reserve good");
                    686: 
1.50      albertel  687:     my $userallowed=0;
1.49      albertel  688:     # its for a different set of users
1.50      albertel  689:     if (defined($slot->{'allowedsections'})) {
                    690: 	if (!defined($env{'request.role.sec'})
                    691: 	    && grep(/^No section assigned$/,
                    692: 		    split(',',$slot->{'allowedsections'}))) {
                    693: 	    $userallowed=1;
                    694: 	}
                    695: 	if (defined($env{'request.role.sec'})
                    696: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
                    697: 		    split(',',$slot->{'allowedsections'}))) {
                    698: 	    $userallowed=1;
                    699: 	}
1.68      albertel  700: 	if (defined($env{'request.course.groups'})) {
                    701: 	    my @groups = split(/:/,$env{'request.course.groups'});
                    702: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
                    703: 	    foreach my $group (@groups) {
                    704: 		if (grep {$_ eq $group} (@allowed_sec)) {
                    705: 		    $userallowed=1;
                    706: 		    last;
                    707: 		}
                    708: 	    }
                    709: 	}
1.49      albertel  710:     }
1.50      albertel  711:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
1.49      albertel  712: 
                    713:     # its for a different set of users
1.50      albertel  714:     if (defined($slot->{'allowedusers'})
                    715: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
                    716: 		split(',',$slot->{'allowedusers'}))) {
                    717: 	$userallowed=1;
1.49      albertel  718:     }
1.51      albertel  719: 
                    720:     if (!defined($slot->{'allowedusers'})
                    721: 	&& !defined($slot->{'allowedsections'})) {
                    722: 	$userallowed=1;
                    723:     }
                    724: 
1.50      albertel  725:     &Apache::lonxml::debug("$slot_name user is $userallowed");
                    726:     return 0 if (!$userallowed);
1.49      albertel  727: 
1.3       albertel  728:     # not allowed for this resource
                    729:     if (defined($slot->{'symb'})
                    730: 	&& $slot->{'symb'} ne $symb) {
                    731: 	return 0;
                    732:     }
1.50      albertel  733: 
1.48      albertel  734:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
                    735: 				       $consumed_uniqueperiods);
1.44      albertel  736:     if ($conflict) {
                    737: 	if ($slots->{$conflict}{'starttime'} < time) {
                    738: 	    return 0;
                    739: 	}
                    740:     }
1.5       albertel  741:     &Apache::lonxml::debug("$slot_name symb good");
1.3       albertel  742:     return 1;
1.2       albertel  743: }
                    744: 
1.3       albertel  745: sub get_description {
                    746:     my ($slot_name,$slot)=@_;
                    747:     my $description=$slot->{'description'};
                    748:     if (!defined($description)) {
1.4       albertel  749: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3       albertel  750: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
                    751: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
                    752:     }
                    753:     return $description;
                    754: }
1.2       albertel  755: 
                    756: sub show_choices {
                    757:     my ($r,$symb)=@_;
                    758: 
                    759:     my ($cnum,$cdom)=&get_course();
                    760:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.48      albertel  761:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
1.3       albertel  762:     my $available;
1.2       albertel  763:     $r->print('<table border="1">');
1.5       albertel  764:     &Apache::lonxml::debug("Checking Slots");
1.43      albertel  765:     my @got_slots=&check_for_reservation($symb,'allslots');
1.2       albertel  766:     foreach my $slot (sort 
                    767: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
                    768: 		      (keys(%slots)))  {
1.5       albertel  769: 
                    770: 	&Apache::lonxml::debug("Checking Slot $slot");
1.48      albertel  771: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
                    772: 				$consumed_uniqueperiods));
1.3       albertel  773: 
                    774: 	$available++;
                    775: 
                    776: 	my $description=&get_description($slot,$slots{$slot});
1.2       albertel  777: 
                    778: 	my $form=&mt('Unavailable');
1.43      albertel  779: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
1.7       albertel  780: 	    &space_available($slot,$slots{$slot},$symb)) {
1.5       albertel  781: 	    my $text=&mt('Select');
                    782: 	    my $command='get';
1.43      albertel  783: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
1.70      albertel  784: 		$text=&mt('Drop Reservation');
1.5       albertel  785: 		$command='release';
1.43      albertel  786: 	    } else {
                    787: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
1.48      albertel  788: 						   \%slots,
                    789: 						   $consumed_uniqueperiods);
1.43      albertel  790: 		if ($conflict) {
                    791: 		    $text=&mt('Change Reservation');
                    792: 		    $command='get';
                    793: 		}
1.5       albertel  794: 	    }
1.63      www       795: 	    my $escsymb=&escape($symb);
1.2       albertel  796: 	    $form=<<STUFF;
1.64      albertel  797:    <form method="post" action="/adm/slotrequest">
1.5       albertel  798:      <input type="submit" name="Select" value="$text" />
1.3       albertel  799:      <input type="hidden" name="symb" value="$escsymb" />
                    800:      <input type="hidden" name="slotname" value="$slot" />
1.5       albertel  801:      <input type="hidden" name="command" value="$command" />
1.2       albertel  802:    </form>
                    803: STUFF
                    804: 	}
                    805: 	$r->print(<<STUFF);
                    806: <tr>
                    807:  <td>$form</td>
                    808:  <td>$description</td>
                    809: </tr>
                    810: STUFF
                    811:     }
1.3       albertel  812: 
                    813:     if (!$available) {
1.5       albertel  814: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
1.3       albertel  815: 		  &mt('Return to last resource').'</a></td></tr>');
                    816:     }
1.2       albertel  817:     $r->print('</table>');
                    818: }
                    819: 
1.30      albertel  820: sub to_show {
1.54      albertel  821:     my ($slotname,$slot,$when,$deleted,$name) = @_;
1.30      albertel  822:     my $time=time;
                    823:     my $week=60*60*24*7;
1.54      albertel  824: 
1.35      albertel  825:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
                    826: 	return 0;
                    827:     }
1.54      albertel  828: 
                    829:     if ($name && $name->{'value'} =~ /\w/) {
                    830: 	if ($name->{'type'} eq 'substring') {
                    831: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
                    832: 		return 0;
                    833: 	    }
                    834: 	}
                    835: 	if ($name->{'type'} eq 'exact') {
                    836: 	    if ($slotname eq $name->{'value'}) {
                    837: 		return 0;
                    838: 	    }
                    839: 	}
                    840:     }
                    841: 
1.35      albertel  842:     if ($when eq 'any') {
                    843: 	return 1;
                    844:     } elsif ($when eq 'now') {
1.30      albertel  845: 	if ($time > $slot->{'starttime'} &&
                    846: 	    $time < $slot->{'endtime'}) {
                    847: 	    return 1;
                    848: 	}
                    849: 	return 0;
                    850:     } elsif ($when eq 'nextweek') {
                    851: 	if ( ($time        < $slot->{'starttime'} &&
                    852: 	      ($time+$week) > $slot->{'starttime'})
                    853: 	     ||
                    854: 	     ($time        < $slot->{'endtime'} &&
                    855: 	      ($time+$week) > $slot->{'endtime'}) ) {
                    856: 	    return 1;
                    857: 	}
                    858: 	return 0;
                    859:     } elsif ($when eq 'lastweek') {
                    860: 	if ( ($time        > $slot->{'starttime'} &&
                    861: 	      ($time-$week) < $slot->{'starttime'})
                    862: 	     ||
                    863: 	     ($time        > $slot->{'endtime'} &&
                    864: 	      ($time-$week) < $slot->{'endtime'}) ) {
                    865: 	    return 1;
                    866: 	}
                    867: 	return 0;
                    868:     } elsif ($when eq 'willopen') {
                    869: 	if ($time < $slot->{'starttime'}) {
                    870: 	    return 1;
                    871: 	}
                    872: 	return 0;
                    873:     } elsif ($when eq 'wereopen') {
                    874: 	if ($time > $slot->{'endtime'}) {
                    875: 	    return 1;
                    876: 	}
                    877: 	return 0;
                    878:     }
                    879:     
                    880:     return 1;
                    881: }
                    882: 
1.33      albertel  883: sub remove_link {
                    884:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
                    885: 
1.55      albertel  886:     my $remove = &mt('Remove');
                    887: 
                    888:     if ($entry eq 'remove all') {
                    889: 	$remove = &mt('Remove All');
                    890: 	undef($uname);
                    891: 	undef($udom);
                    892:     }
                    893: 
1.63      www       894:     $slotname  = &escape($slotname);
                    895:     $entry     = &escape($entry);
                    896:     $uname     = &escape($uname);
                    897:     $udom      = &escape($udom);
                    898:     $symb      = &escape($symb);
1.33      albertel  899: 
                    900:     return <<"END_LINK";
1.64      albertel  901:  <a href="/adm/slotrequest?command=remove_registration&amp;slotname=$slotname&amp;entry=$entry&amp;uname=$uname&amp;udom=$udom&amp;symb=$symb"
1.33      albertel  902:    >($remove)</a>
                    903: END_LINK
                    904: 
                    905: }
                    906: 
1.5       albertel  907: sub show_table {
1.19      albertel  908:     my ($r,$mgr)=@_;
1.5       albertel  909: 
                    910:     my ($cnum,$cdom)=&get_course();
                    911:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19      albertel  912:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
                    913: 	undef(%slots);
                    914:     } 
1.5       albertel  915:     my $available;
1.14      albertel  916:     if ($mgr eq 'F') {
1.72      rezaferr  917:     # FIXME: This line should be deleted once Slots uses breadcrumbs
1.73      albertel  918:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
1.72      rezaferr  919: 
1.30      albertel  920: 	$r->print('<div>');
1.64      albertel  921: 	$r->print('<form method="post" action="/adm/slotrequest">
1.14      albertel  922: <input type="hidden" name="command" value="uploadstart" />
                    923: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
                    924: </form>');
1.72      rezaferr  925: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
1.64      albertel  926: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
1.28      albertel  927: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
                    928: </form>');
1.72      rezaferr  929: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
1.30      albertel  930: 	$r->print('</div>');
1.14      albertel  931:     }
1.29      albertel  932:     
1.54      albertel  933:     my %Saveable_Parameters = ('show'              => 'array',
                    934: 			       'when'              => 'scalar',
                    935: 			       'order'             => 'scalar',
                    936: 			       'deleted'           => 'scalar',
                    937: 			       'name_filter_type'  => 'scalar',
                    938: 			       'name_filter_value' => 'scalar',
1.35      albertel  939: 			       );
1.46      albertel  940:     &Apache::loncommon::store_course_settings('slotrequest',
                    941: 					      \%Saveable_Parameters);
                    942:     &Apache::loncommon::restore_course_settings('slotrequest',
                    943: 						\%Saveable_Parameters);
                    944:     &Apache::grades::init_perm();
                    945:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
                    946:     &Apache::grades::reset_perm();
1.29      albertel  947: 
1.54      albertel  948:     # what to display filtering
1.30      albertel  949:     my %show_fields=&Apache::lonlocal::texthash(
1.49      albertel  950: 	     'name'            => 'Slot Name',
                    951: 	     'description'     => 'Description',
                    952: 	     'type'            => 'Type',
                    953: 	     'starttime'       => 'Start time',
                    954: 	     'endtime'         => 'End Time',
                    955:              'startreserve'    => 'Time students can start reserving',
                    956: 	     'secret'          => 'Secret Word',
1.74      albertel  957: 	     'space'           => '# of students/max',
1.49      albertel  958: 	     'ip'              => 'IP or DNS restrictions',
                    959: 	     'symb'            => 'Resource slot is restricted to.',
                    960: 	     'allowedsections' => 'Sections slot is restricted to.',
                    961: 	     'allowedusers'    => 'Users slot is restricted to.',
                    962: 	     'uniqueperiod'    => 'Period of time slot is unique',
                    963: 	     'scheduled'       => 'Scheduled Students',
                    964: 	     'proctor'         => 'List of proctors');
1.30      albertel  965:     my @show_order=('name','description','type','starttime','endtime',
1.74      albertel  966: 		    'startreserve','secret','space','ip','symb',
1.49      albertel  967: 		    'allowedsections','allowedusers','uniqueperiod',
                    968: 		    'scheduled','proctor');
1.30      albertel  969:     my @show = 
1.29      albertel  970: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30      albertel  971: 	                            : keys(%show_fields);
                    972:     my %show =  map { $_ => 1 } (@show);
                    973: 
1.54      albertel  974:     #when filtering setup
1.30      albertel  975:     my %when_fields=&Apache::lonlocal::texthash(
1.35      albertel  976: 	     'now'      => 'Open now',
1.30      albertel  977: 	     'nextweek' => 'Open within the next week',
                    978: 	     'lastweek' => 'Were open last week',
                    979: 	     'willopen' => 'Will open later',
1.35      albertel  980: 	     'wereopen' => 'Were open',
                    981: 	     'any'      => 'Anytime',
                    982: 						);
                    983:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1.30      albertel  984:     $when_fields{'select_form_order'} = \@when_order;
                    985:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
                    986:                                             : 'now';
1.29      albertel  987: 
1.54      albertel  988:     #display of students setup
1.46      albertel  989:     my %stu_display_fields=
                    990: 	&Apache::lonlocal::texthash('username' => 'User name',
                    991: 				    'fullname' => 'Full name',
                    992: 				    );
                    993:     my @stu_display_order=('fullname','username');
                    994:     my @stu_display = 
                    995: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
                    996: 	                                  : keys(%stu_display_fields);
                    997:     my %stu_display =  map { $_ => 1 } (@stu_display);
                    998: 
1.54      albertel  999:     #name filtering setup
                   1000:     my %name_filter_type_fields=
                   1001: 	&Apache::lonlocal::texthash('substring' => 'Substring',
                   1002: 				    'exact'     => 'Exact',
                   1003: 				    #'reg'       => 'Regular Expression',
                   1004: 				    );
                   1005:     my @name_filter_type_order=('substring','exact');
                   1006: 
                   1007:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
                   1008:     my $name_filter_type = 
                   1009: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
                   1010:                                                 : 'substring';
                   1011:     my $name_filter = {'type'  => $name_filter_type,
                   1012: 		       'value' => $env{'form.name_filter_value'},};
                   1013: 
1.64      albertel 1014:     
1.54      albertel 1015:     #deleted slot filtering
1.64      albertel 1016:     #default to hide if no value
                   1017:     $env{'form.deleted'} ||= 'hide';
1.35      albertel 1018:     my $hide_radio = 
                   1019: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
                   1020:     my $show_radio = 
                   1021: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
                   1022: 	
1.64      albertel 1023:     $r->print('<form method="post" action="/adm/slotrequest">
1.30      albertel 1024: <input type="hidden" name="command" value="showslots" />');
                   1025:     $r->print('<div>');
1.35      albertel 1026:     $r->print('<table class="inline">
                   1027:       <tr><th>'.&mt('Show').'</th>
1.46      albertel 1028:           <th>'.&mt('Student Display').'</th>
1.35      albertel 1029:           <th>'.&mt('Open').'</th>
1.54      albertel 1030:           <th>'.&mt('Slot Name Filter').'</th>
1.35      albertel 1031:           <th>'.&mt('Options').'</th>
                   1032:       </tr>
                   1033:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
                   1034: 	      '</td>
1.46      albertel 1035:            <td>
                   1036:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
                   1037: 						    6,\%stu_display_fields,
                   1038: 						    \@stu_display_order).'
                   1039:            </td>
1.35      albertel 1040:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
                   1041:           '</td>
1.54      albertel 1042:            <td>'.&Apache::loncommon::select_form($name_filter_type,
                   1043: 						 'name_filter_type',
                   1044: 						 %name_filter_type_fields).
                   1045: 	      '<br />'.
                   1046: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
                   1047: 					      $env{'form.name_filter_value'},
                   1048: 					      15).
                   1049:           '</td>
1.35      albertel 1050:            <td>
                   1051:             <table>
                   1052:               <tr>
                   1053:                 <td rowspan="2">Deleted slots:</td>
                   1054:                 <td><label>'.$show_radio.'Show</label></td>
                   1055:               </tr>
                   1056:               <tr>
                   1057:                 <td><label>'.$hide_radio.'Hide</label></td>
                   1058:               </tr>
                   1059:             </table>
                   1060: 	  </td>
                   1061:        </tr>
                   1062:     </table>');
1.30      albertel 1063:     $r->print('</div>');
                   1064:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21      albertel 1065:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
1.65      albertel 1066:     $r->print(&Apache::loncommon::start_data_table().
                   1067: 	      &Apache::loncommon::start_data_table_header_row().'
                   1068: 	       <th></th>');
1.30      albertel 1069:     foreach my $which (@show_order) {
                   1070: 	if ($which ne 'proctor' && exists($show{$which})) {
                   1071: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1.29      albertel 1072: 	}
                   1073:     }
1.65      albertel 1074:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.29      albertel 1075: 
1.21      albertel 1076:     my %name_cache;
                   1077:     my $slotsort = sub {
1.74      albertel 1078: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
1.21      albertel 1079: 	    if (lc($slots{$a}->{$env{'form.order'}})
                   1080: 		ne lc($slots{$b}->{$env{'form.order'}})) {
                   1081: 		return (lc($slots{$a}->{$env{'form.order'}}) 
                   1082: 			cmp lc($slots{$b}->{$env{'form.order'}}));
                   1083: 	    }
1.74      albertel 1084: 	} elsif ($env{'form.order'} eq 'space') {
                   1085: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
                   1086: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
                   1087: 	    }
1.23      albertel 1088: 	} elsif ($env{'form.order'} eq 'name') {
                   1089: 	    if (lc($a) cmp lc($b)) {
                   1090: 		return lc($a) cmp lc($b);
                   1091: 	    }
1.29      albertel 1092: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21      albertel 1093: 	    
                   1094: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
                   1095: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
                   1096: 		return ($slots{$a}->{'uniqueperiod'}[0]
                   1097: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
                   1098: 	    }
                   1099: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
                   1100: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
                   1101: 		return ($slots{$a}->{'uniqueperiod'}[1]
                   1102: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
                   1103: 	    }
                   1104: 	}
                   1105: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
                   1106:     };
1.74      albertel 1107: 
                   1108:     my %consumed;
                   1109:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
                   1110: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
                   1111: 	my ($tmp)=%consumed;
                   1112: 	if ($tmp =~ /^error: /) { undef(%consumed); }
                   1113:     }
                   1114: 
1.21      albertel 1115:     foreach my $slot (sort $slotsort (keys(%slots)))  {
1.54      albertel 1116: 	if (!&to_show($slot,$slots{$slot},$when,
                   1117: 		      $env{'form.deleted'},$name_filter)) { next; }
1.5       albertel 1118: 	if (defined($slots{$slot}->{'type'})
                   1119: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13      albertel 1120: 	    #next;
1.5       albertel 1121: 	}
                   1122: 	my $description=&get_description($slot,$slots{$slot});
1.74      albertel 1123: 	my ($id_count,$ids);
                   1124: 	    
                   1125: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
1.79      albertel 1126: 	    my $re_str = "$slot\0";
                   1127: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
1.74      albertel 1128: 	    $id_count = scalar(@this_slot);
                   1129: 	    if (exists($show{'scheduled'})) {
1.54      albertel 1130: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
                   1131: 					      $consumed{$b}{name} }
1.79      albertel 1132: 				   (@this_slot)) {
1.47      albertel 1133: 		    my (undef,$id)=split("\0",$entry);
1.57      albertel 1134: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.47      albertel 1135: 		    $ids.= '<nobr>';
                   1136: 		    foreach my $item (@stu_display_order) {
                   1137: 			if ($stu_display{$item}) {
                   1138: 			    if ($item eq 'fullname') {
                   1139: 				$ids.=$fullname->{"$uname:$udom"}.' ';
                   1140: 			    } elsif ($item eq 'username') {
1.57      albertel 1141: 				$ids.="<tt>$uname:$udom</tt> ";
1.47      albertel 1142: 			    }
1.46      albertel 1143: 			}
                   1144: 		    }
1.47      albertel 1145: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
                   1146: 				       $consumed{$entry}{'symb'}).'</nobr><br />';
1.46      albertel 1147: 		}
1.38      albertel 1148: 	    }
1.5       albertel 1149: 	}
1.33      albertel 1150: 
1.24      albertel 1151: 	my $start=($slots{$slot}->{'starttime'}?
                   1152: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
                   1153: 	my $end=($slots{$slot}->{'endtime'}?
                   1154: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1.28      albertel 1155: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24      albertel 1156: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
                   1157: 	
1.14      albertel 1158: 	my $unique;
                   1159: 	if (ref($slots{$slot}{'uniqueperiod'})) {
1.64      albertel 1160: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
1.14      albertel 1161: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
                   1162: 	}
1.33      albertel 1163: 
1.29      albertel 1164: 	my $title;
                   1165: 	if (exists($slots{$slot}{'symb'})) {
                   1166: 	    my (undef,undef,$res)=
                   1167: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
                   1168: 	    $res =   &Apache::lonnet::clutter($res);
                   1169: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
                   1170: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
                   1171: 	}
1.33      albertel 1172: 
1.49      albertel 1173: 	my $allowedsections;
                   1174: 	if (exists($show{'allowedsections'})) {
                   1175: 	    $allowedsections = 
                   1176: 		join(', ',sort(split(/\s*,\s*/,
                   1177: 				     $slots{$slot}->{'allowedsections'})));
                   1178: 	}
                   1179: 
                   1180: 	my @allowedusers;
                   1181: 	if (exists($show{'allowedusers'})) {
                   1182: 	    @allowedusers= map {
                   1183: 		my ($uname,$udom)=split(/:/,$_);
                   1184: 		my $fullname=$name_cache{$_};
                   1185: 		if (!defined($fullname)) {
                   1186: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1187: 		    $fullname =~s/\s/&nbsp;/g;
                   1188: 		    $name_cache{$_} = $fullname;
                   1189: 		}
                   1190: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1191: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
                   1192: 	}
                   1193: 	my $allowedusers=join(', ',@allowedusers);
                   1194: 	
1.29      albertel 1195: 	my @proctors;
                   1196: 	my $rowspan=1;
                   1197: 	my $colspan=1;
1.30      albertel 1198: 	if (exists($show{'proctor'})) {
1.29      albertel 1199: 	    $rowspan=2;
                   1200: 	    @proctors= map {
1.62      albertel 1201: 		my ($uname,$udom)=split(/:/,$_);
1.29      albertel 1202: 		my $fullname=$name_cache{$_};
                   1203: 		if (!defined($fullname)) {
                   1204: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1205: 		    $fullname =~s/\s/&nbsp;/g;
                   1206: 		    $name_cache{$_} = $fullname;
                   1207: 		}
                   1208: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1209: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
                   1210: 	}
1.20      albertel 1211: 	my $proctors=join(', ',@proctors);
1.14      albertel 1212: 
1.34      albertel 1213: 	my $edit=(<<"EDITLINK");
1.31      albertel 1214: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
                   1215: EDITLINK
1.34      albertel 1216: 
                   1217: 	my $delete=(<<"DELETELINK");
1.64      albertel 1218: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
1.34      albertel 1219: DELETELINK
1.55      albertel 1220: 
1.56      albertel 1221:         my $remove_all=&remove_link($slot,'remove all').'<br />';
1.55      albertel 1222: 
1.34      albertel 1223:         if ($ids ne '') { undef($delete); }
1.56      albertel 1224: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
                   1225: 	    || $ids eq '') { 
1.55      albertel 1226: 	    undef($remove_all);
                   1227: 	}
1.34      albertel 1228: 
1.65      albertel 1229: 	my $row_start=&Apache::loncommon::start_data_table_row();
                   1230: 	my $row_end=&Apache::loncommon::end_data_table_row();
                   1231:         $r->print($row_start.
                   1232: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1.30      albertel 1233: 	if (exists($show{'name'})) {
1.29      albertel 1234: 	    $colspan++;$r->print("<td>$slot</td>");
                   1235: 	}
1.33      albertel 1236: 	if (exists($show{'description'})) {
                   1237: 	    $colspan++;$r->print("<td>$description</td>\n");
                   1238: 	}
1.30      albertel 1239: 	if (exists($show{'type'})) {
1.29      albertel 1240: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
                   1241: 	}
1.30      albertel 1242: 	if (exists($show{'starttime'})) {
1.29      albertel 1243: 	    $colspan++;$r->print("<td>$start</td>\n");
                   1244: 	}
1.30      albertel 1245: 	if (exists($show{'endtime'})) {
1.29      albertel 1246: 	    $colspan++;$r->print("<td>$end</td>\n");
                   1247: 	}
1.30      albertel 1248: 	if (exists($show{'startreserve'})) {
1.29      albertel 1249: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
                   1250: 	}
1.30      albertel 1251: 	if (exists($show{'secret'})) {
1.29      albertel 1252: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
                   1253: 	}
1.74      albertel 1254: 	if (exists($show{'space'})) {
                   1255: 	    my $display = $id_count;
                   1256: 	    if ($slots{$slot}{'maxspace'}>0) {
                   1257: 		$display.='/'.$slots{$slot}{'maxspace'};
                   1258: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
                   1259: 		    $display = '<strong>'.$display.' (full) </strong>';
                   1260: 		}
                   1261: 	    }
                   1262: 	    $colspan++;$r->print("<td>$display</td>\n");
1.29      albertel 1263: 	}
1.30      albertel 1264: 	if (exists($show{'ip'})) {
1.29      albertel 1265: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
                   1266: 	}
1.30      albertel 1267: 	if (exists($show{'symb'})) {
1.29      albertel 1268: 	    $colspan++;$r->print("<td>$title</td>\n");
                   1269: 	}
1.49      albertel 1270: 	if (exists($show{'allowedsections'})) {
                   1271: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
                   1272: 	}
                   1273: 	if (exists($show{'allowedusers'})) {
                   1274: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
1.29      albertel 1275: 	}
1.64      albertel 1276: 	if (exists($show{'uniqueperiod'})) {
                   1277: 	    $colspan++;$r->print("<td>$unique</td>\n");
                   1278: 	}
1.47      albertel 1279: 	if (exists($show{'scheduled'})) {
1.64      albertel 1280: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
1.47      albertel 1281: 	}
1.65      albertel 1282: 	$r->print("$row_end\n");
1.30      albertel 1283: 	if (exists($show{'proctor'})) {
1.29      albertel 1284: 	    $r->print(<<STUFF);
1.65      albertel 1285: $row_start
1.29      albertel 1286:  <td colspan="$colspan">$proctors</td>
1.65      albertel 1287: $row_end
1.5       albertel 1288: STUFF
1.29      albertel 1289:         }
1.5       albertel 1290:     }
1.64      albertel 1291:     $r->print('</table></form>');
1.5       albertel 1292: }
                   1293: 
1.14      albertel 1294: sub upload_start {
1.19      albertel 1295:     my ($r)=@_;    
1.14      albertel 1296:     $r->print(&Apache::grades::checkforfile_js());
                   1297:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1298:     $result.='&nbsp;<b>'.
                   1299: 	&mt('Specify a file containing the slot definitions.').
                   1300: 	'</b></td></tr>'."\n";
                   1301:     $result.='<tr bgcolor=#ffffe6><td>'."\n";
                   1302:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1303:     my $ignore=&mt('Ignore First Line');
                   1304:     $result.=<<ENDUPFORM;
                   1305: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
                   1306: <input type="hidden" name="command" value="csvuploadmap" />
                   1307: $upfile_select
                   1308: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
                   1309: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
                   1310: </form>
                   1311: ENDUPFORM
                   1312:     $result.='</td></tr></table>'."\n";
                   1313:     $result.='</td></tr></table>'."\n";
                   1314:     $r->print($result);
                   1315: }
                   1316: 
                   1317: sub csvuploadmap_header {
1.19      albertel 1318:     my ($r,$datatoken,$distotal)= @_;
1.14      albertel 1319:     my $javascript;
                   1320:     if ($env{'form.upfile_associate'} eq 'reverse') {
                   1321: 	$javascript=&csvupload_javascript_reverse_associate();
                   1322:     } else {
                   1323: 	$javascript=&csvupload_javascript_forward_associate();
                   1324:     }
                   1325: 
                   1326:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
                   1327:     my $ignore=&mt('Ignore First Line');
1.72      rezaferr 1328: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
                   1329: 
1.14      albertel 1330:     $r->print(<<ENDPICK);
                   1331: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1.72      rezaferr 1332: <h3>Identify fields $help_field</h3>	
1.14      albertel 1333: Total number of records found in file: $distotal <hr />
                   1334: Enter as many fields as you can. The system will inform you and bring you back
                   1335: to this page if the data selected is insufficient to create the slots.<hr />
                   1336: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1337: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
                   1338: <input type="hidden" name="associate"  value="" />
                   1339: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1340: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
                   1341: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
                   1342: <input type="hidden" name="upfile_associate" 
                   1343:                                        value="$env{'form.upfile_associate'}" />
                   1344: <input type="hidden" name="command"    value="csvuploadassign" />
                   1345: <hr />
                   1346: <script type="text/javascript" language="Javascript">
                   1347: $javascript
                   1348: </script>
                   1349: ENDPICK
                   1350:     return '';
                   1351: 
                   1352: }
                   1353: 
                   1354: sub csvuploadmap_footer {
                   1355:     my ($request,$i,$keyfields) =@_;
                   1356:     $request->print(<<ENDPICK);
                   1357: </table>
                   1358: <input type="hidden" name="nfields" value="$i" />
                   1359: <input type="hidden" name="keyfields" value="$keyfields" />
                   1360: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
                   1361: </form>
                   1362: ENDPICK
                   1363: }
                   1364: 
                   1365: sub csvupload_javascript_reverse_associate {
                   1366:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1367:     return(<<ENDPICK);
                   1368:   function verify(vf) {
                   1369:     var foundstart=0;
                   1370:     var foundend=0;
                   1371:     var foundname=0;
                   1372:     var foundtype=0;
                   1373:     for (i=0;i<=vf.nfields.value;i++) {
                   1374:       tw=eval('vf.f'+i+'.selectedIndex');
                   1375:       if (i==0 && tw!=0) { foundname=1; }
                   1376:       if (i==1 && tw!=0) { foundtype=1; }
                   1377:       if (i==2 && tw!=0) { foundstat=1; }
                   1378:       if (i==3 && tw!=0) { foundend=1; }
                   1379:     }
                   1380:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1381: 	alert('$error1');
                   1382: 	return;
                   1383:     }
                   1384:     vf.submit();
                   1385:   }
                   1386:   function flip(vf,tf) {
                   1387:   }
                   1388: ENDPICK
                   1389: }
                   1390: 
                   1391: sub csvupload_javascript_forward_associate {
                   1392:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1393:   return(<<ENDPICK);
                   1394:   function verify(vf) {
                   1395:     var foundstart=0;
                   1396:     var foundend=0;
                   1397:     var foundname=0;
                   1398:     var foundtype=0;
                   1399:     for (i=0;i<=vf.nfields.value;i++) {
                   1400:       tw=eval('vf.f'+i+'.selectedIndex');
                   1401:       if (tw==1) { foundname=1; }
                   1402:       if (tw==2) { foundtype=1; }
                   1403:       if (tw==3) { foundstat=1; }
                   1404:       if (tw==4) { foundend=1; }
                   1405:     }
                   1406:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1407: 	alert('$error1');
                   1408: 	return;
                   1409:     }
                   1410:     vf.submit();
                   1411:   }
                   1412:   function flip(vf,tf) {
                   1413:   }
                   1414: ENDPICK
                   1415: }
                   1416: 
                   1417: sub csv_upload_map {
1.19      albertel 1418:     my ($r)= @_;
1.14      albertel 1419: 
                   1420:     my $datatoken;
                   1421:     if (!$env{'form.datatoken'}) {
                   1422: 	$datatoken=&Apache::loncommon::upfile_store($r);
                   1423:     } else {
                   1424: 	$datatoken=$env{'form.datatoken'};
                   1425: 	&Apache::loncommon::load_tmp_file($r);
                   1426:     }
                   1427:     my @records=&Apache::loncommon::upfile_record_sep();
                   1428:     if ($env{'form.noFirstLine'}) { shift(@records); }
1.19      albertel 1429:     &csvuploadmap_header($r,$datatoken,$#records+1);
1.14      albertel 1430:     my ($i,$keyfields);
                   1431:     if (@records) {
                   1432: 	my @fields=&csvupload_fields();
                   1433: 
                   1434: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
                   1435: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                   1436: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1437: 							  \@fields);
                   1438: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   1439: 	    chop($keyfields);
                   1440: 	} else {
                   1441: 	    unshift(@fields,['none','']);
                   1442: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1443: 							    \@fields);
                   1444: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   1445: 	    $keyfields=join(',',sort(keys(%sone)));
                   1446: 	}
                   1447:     }
                   1448:     &csvuploadmap_footer($r,$i,$keyfields);
                   1449: 
                   1450:     return '';
                   1451: }
                   1452: 
                   1453: sub csvupload_fields {
                   1454:     return (['name','Slot name'],
                   1455: 	    ['type','Type of slot'],
                   1456: 	    ['starttime','Start Time of slot'],
                   1457: 	    ['endtime','End Time of slot'],
1.15      albertel 1458: 	    ['startreserve','Reservation Start Time'],
1.14      albertel 1459: 	    ['ip','IP or DNS restriction'],
                   1460: 	    ['proctor','List of proctor ids'],
                   1461: 	    ['description','Slot Description'],
                   1462: 	    ['maxspace','Maximum number of reservations'],
                   1463: 	    ['symb','Resource Restriction'],
                   1464: 	    ['uniqueperiod','Date range of slot exclusion'],
1.49      albertel 1465: 	    ['secret','Secret word proctor uses to validate'],
                   1466: 	    ['allowedsections','Sections slot is restricted to'],
                   1467: 	    ['allowedusers','Users slot is restricted to'],
                   1468: 	    );
1.14      albertel 1469: }
                   1470: 
                   1471: sub csv_upload_assign {
1.19      albertel 1472:     my ($r,$mgr)= @_;
1.14      albertel 1473:     &Apache::loncommon::load_tmp_file($r);
                   1474:     my @slotdata = &Apache::loncommon::upfile_record_sep();
                   1475:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
                   1476:     my %fields=&Apache::grades::get_fields();
                   1477:     $r->print('<h3>Creating Slots</h3>');
                   1478:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1479:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1480:     my $countdone=0;
1.31      albertel 1481:     my @errors;
1.14      albertel 1482:     foreach my $slot (@slotdata) {
                   1483: 	my %slot;
                   1484: 	my %entries=&Apache::loncommon::record_sep($slot);
                   1485: 	my $domain;
                   1486: 	my $name=$entries{$fields{'name'}};
1.31      albertel 1487: 	if ($name=~/^\s*$/) {
                   1488: 	    push(@errors,"Did not create slot with no name");
                   1489: 	    next;
                   1490: 	}
                   1491: 	if ($name=~/\s/) { 
                   1492: 	    push(@errors,"$name not created -- Name must not contain spaces");
                   1493: 	    next;
                   1494: 	}
                   1495: 	if ($name=~/\W/) { 
                   1496: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
                   1497: 	    next;
                   1498: 	}
1.14      albertel 1499: 	if ($entries{$fields{'type'}}) {
                   1500: 	    $slot{'type'}=$entries{$fields{'type'}};
                   1501: 	} else {
                   1502: 	    $slot{'type'}='preassigned';
                   1503: 	}
1.31      albertel 1504: 	if ($slot{'type'} ne 'preassigned' &&
                   1505: 	    $slot{'type'} ne 'schedulable_student') {
                   1506: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
                   1507: 	    next;
                   1508: 	}
1.14      albertel 1509: 	if ($entries{$fields{'starttime'}}) {
                   1510: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
                   1511: 	}
                   1512: 	if ($entries{$fields{'endtime'}}) {
1.16      albertel 1513: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14      albertel 1514: 	}
1.58      albertel 1515: 
                   1516: 	# start/endtime must be defined and greater than zero
                   1517: 	if (!$slot{'starttime'}) {
                   1518: 	    push(@errors,"$name not created -- Invalid start time");
                   1519: 	    next;
                   1520: 	}
                   1521: 	if (!$slot{'endtime'}) {
                   1522: 	    push(@errors,"$name not created -- Invalid end time");
                   1523: 	    next;
                   1524: 	}
                   1525: 	if ($slot{'starttime'} > $slot{'endtime'}) {
                   1526: 	    push(@errors,"$name not created -- Slot starts after it ends");
                   1527: 	    next;
                   1528: 	}
                   1529: 
1.23      albertel 1530: 	if ($entries{$fields{'startreserve'}}) {
                   1531: 	    $slot{'startreserve'}=
                   1532: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
                   1533: 	}
1.58      albertel 1534: 	if (defined($slot{'startreserve'})
                   1535: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
                   1536: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
                   1537: 	    next;
                   1538: 	}
                   1539: 
1.14      albertel 1540: 	foreach my $key ('ip','proctor','description','maxspace',
                   1541: 			 'secret','symb') {
                   1542: 	    if ($entries{$fields{$key}}) {
                   1543: 		$slot{$key}=$entries{$fields{$key}};
                   1544: 	    }
                   1545: 	}
1.58      albertel 1546: 
1.14      albertel 1547: 	if ($entries{$fields{'uniqueperiod'}}) {
                   1548: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
                   1549: 	    my @times=(&UnixDate($start,"%s"),
                   1550: 		       &UnixDate($end,"%s"));
                   1551: 	    $slot{'uniqueperiod'}=\@times;
                   1552: 	}
1.58      albertel 1553: 	if (defined($slot{'uniqueperiod'})
                   1554: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
                   1555: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
                   1556: 	    next;
                   1557: 	}
1.14      albertel 1558: 
                   1559: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
                   1560: 	$r->print('.');
                   1561: 	$r->rflush();
                   1562: 	$countdone++;
                   1563:     }
1.31      albertel 1564:     $r->print("<p>Created $countdone slots\n</p>");
                   1565:     foreach my $error (@errors) {
1.78      albertel 1566: 	$r->print("<p><span class=\"LC_warning\">$error</span></p>\n");
1.31      albertel 1567:     }
1.19      albertel 1568:     &show_table($r,$mgr);
1.14      albertel 1569:     return '';
                   1570: }
                   1571: 
1.1       albertel 1572: sub handler {
                   1573:     my $r=shift;
                   1574: 
1.30      albertel 1575:     &Apache::loncommon::content_type($r,'text/html');
                   1576:     &Apache::loncommon::no_cache($r);
                   1577:     if ($r->header_only()) {
                   1578: 	$r->send_http_header();
                   1579: 	return OK;
                   1580:     }
                   1581: 
1.8       albertel 1582:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28      albertel 1583:     
1.12      albertel 1584:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14      albertel 1585:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28      albertel 1586:     my $title='Requesting Another Worktime';
                   1587:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
                   1588: 	$title = 'Managing Slots';
                   1589:     }
                   1590:     &start_page($r,$title);
                   1591: 
1.8       albertel 1592:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19      albertel 1593: 	&show_table($r,$mgr);
1.33      albertel 1594:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
                   1595: 	&remove_registration($r);
                   1596:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1.55      albertel 1597: 	if ($env{'form.entry'} eq 'remove all') {
                   1598: 	    &release_all_slot($r,$mgr);
                   1599: 	} else {
                   1600: 	    &release_slot($r,undef,undef,undef,$mgr);
                   1601: 	}
1.34      albertel 1602:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
                   1603: 	&delete_slot($r);
1.14      albertel 1604:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19      albertel 1605: 	&upload_start($r);
1.14      albertel 1606:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19      albertel 1607: 	&csv_upload_map($r);
1.14      albertel 1608:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
                   1609: 	if ($env{'form.associate'} ne 'Reverse Association') {
1.19      albertel 1610: 	    &csv_upload_assign($r,$mgr);
1.14      albertel 1611: 	} else {
                   1612: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                   1613: 		$env{'form.upfile_associate'} = 'reverse';
                   1614: 	    } else {
                   1615: 		$env{'form.upfile_associate'} = 'forward';
                   1616: 	    }
1.19      albertel 1617: 	    &csv_upload_map($r);
1.14      albertel 1618: 	}
1.8       albertel 1619:     } else {
1.63      www      1620: 	my $symb=&unescape($env{'form.symb'});
1.61      albertel 1621: 	if (!defined($symb)) {
                   1622: 	    &fail($r,'not_valid');
                   1623: 	    return OK;
                   1624: 	}
1.19      albertel 1625: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.36      albertel 1626: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1.66      albertel 1627: 	if ($useslots ne 'resource' 
                   1628: 	    && $useslots ne 'map' 
                   1629: 	    && $useslots ne 'map_map') {
1.61      albertel 1630: 	    &fail($r,'not_available');
1.19      albertel 1631: 	    return OK;
                   1632: 	}
                   1633: 	$env{'request.symb'}=$symb;
1.36      albertel 1634: 	my $type = ($res =~ /\.task$/) ? 'Task'
                   1635: 	                               : 'problem';
                   1636: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1.11      albertel 1637: 	if ($status eq 'CAN_ANSWER' ||
                   1638: 	    $status eq 'NEEDS_CHECKIN' ||
                   1639: 	    $status eq 'WAITING_FOR_GRADE') {
                   1640: 	    &fail($r,'not_allowed');
                   1641: 	    return OK;
                   1642: 	}
                   1643: 	if ($env{'form.requestattempt'}) {
                   1644: 	    &show_choices($r,$symb);
                   1645: 	} elsif ($env{'form.command'} eq 'release') {
                   1646: 	    &release_slot($r,$symb);
                   1647: 	} elsif ($env{'form.command'} eq 'get') {
                   1648: 	    &get_slot($r,$symb);
                   1649: 	} elsif ($env{'form.command'} eq 'change') {
1.75      albertel 1650: 	    if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
                   1651: 		&release_slot($r,$symb,$env{'form.releaseslot'});
1.39      albertel 1652: 	    }
1.11      albertel 1653: 	} else {
                   1654: 	    $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
                   1655: 	}
1.2       albertel 1656:     }
1.1       albertel 1657:     &end_page($r);
                   1658:     return OK;
                   1659: }
1.3       albertel 1660: 
                   1661: 1;
                   1662: __END__

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