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

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

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