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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for requesting to have slots added to a students record
                      3: #
1.87    ! raeburn     4: # $Id: slotrequest.pm,v 1.86 2009/01/23 21:34:47 raeburn 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) {
1.87    ! raeburn   348: 	$r->print('<p>'.&mt('Slot [_1] has no reservations.',
        !           349: 			    '<tt>'.$slot_name.'</tt>').'</p>');
1.55      albertel  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') {
1.87    ! raeburn   560: 	    $r->print('<p>'.&mt('Slot [_1] marked as deleted.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  561: 	} else {
1.87    ! raeburn   562: 	    $r->print('<p><span class="LC_error">'.&mt('An error occurred when attempting to delete slot: [_1]','<tt>'.$slot_name.'</tt>')." ($ret)</span></p>");
1.34      albertel  563: 	}
                    564:     } else {
                    565: 	if (%consumed) {
1.87    ! raeburn   566: 	    $r->print('<p>'.&mt('Slot [_1] has active reservations.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  567: 	} else {
1.87    ! raeburn   568: 	    $r->print('<p>'.&mt('Slot [_1] does not exist.','<tt>'.$slot_name.'</tt>').'</p>');
1.34      albertel  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);
1.87    ! raeburn   600: 	$r->print('<p>'.&mt('Already have a reservation: [_1].',$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.87    ! raeburn   609:             $r->print('<p>'.&mt('You can either [_1] your reservation from [2] to [_3] or [_4]','<input type="submit" name="change" value="'.&mt('Change').'" />','<b>'.$description1.'</b>','<b>'.$description2.'</b><br />','</p>'));
1.40      albertel  610: 	    &return_link($r);
1.7       albertel  611: 	    $r->print(<<STUFF);
1.6       albertel  612: </form>
                    613: STUFF
1.7       albertel  614:         } else {
1.40      albertel  615: 	    &return_link($r);
1.7       albertel  616: 	}
1.75      albertel  617: 	return 0;
1.5       albertel  618:     }
1.45      albertel  619: 
1.3       albertel  620:     my $reserved=&make_reservation($env{'form.slotname'},
                    621: 				   \%slot,$symb);
                    622:     my $description=&get_description($env{'form.slotname'},\%slot);
1.7       albertel  623:     if (defined($reserved)) {
1.75      albertel  624: 	my $retvalue = 0;
1.40      albertel  625: 	if ($slot_name =~ /^error: (.*)/) {
1.82      bisitz    626: 	    $r->print('<p><span class="LC_error">'
                    627:                      .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
                    628:                      .'</span></p>');
1.40      albertel  629: 	} elsif ($reserved > -1) {
1.87    ! raeburn   630: 	    $r->print('<p>'.&mt('Success: [_1]',$description).'</p>');
1.75      albertel  631: 	    $retvalue = 1;
1.7       albertel  632: 	} elsif ($reserved < 0) {
1.87    ! raeburn   633: 	    $r->print('<p>'.&mt('Already reserved: [_1]',$description).'</p>');
1.7       albertel  634: 	}
1.75      albertel  635: 	if (!$inhibit_return_link) { &return_link($r); }
                    636: 	return 1;
1.3       albertel  637:     }
                    638: 
1.7       albertel  639:     my %lt=('request'=>"Availibility list",
1.87    ! raeburn   640: 	    'try'    =>'Try again?',
        !           641:             'or'     => 'or');
1.3       albertel  642:     %lt=&Apache::lonlocal::texthash(%lt);
                    643: 
1.75      albertel  644:     my $extra_input;
                    645:     if ($conflictable_slot) {
                    646: 	$extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
                    647:     }
                    648: 
1.87    ! raeburn   649:     $r->print('<p>'.&mt('[_1]Failed[_2] to reserve a slot for [_3].','<span class="LC_warning">','</span>',$description).'</p>');
1.3       albertel  650:     $r->print(<<STUFF);
                    651: <p>
1.64      albertel  652: <form method="post" action="/adm/slotrequest">
1.3       albertel  653:    <input type="submit" name="Try Again" value="$lt{'try'}" />
                    654:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    655:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
1.75      albertel  656:    <input type="hidden" name="command" value="$env{'form.command'}" />
                    657:    $extra_input
1.3       albertel  658: </form>
                    659: </p>
                    660: <p>
1.87    ! raeburn   661: $lt{'or'}
1.64      albertel  662: <form method="post" action="/adm/slotrequest">
1.3       albertel  663:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
                    664:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
                    665: </form>
                    666: STUFF
1.42      albertel  667: 
1.87    ! raeburn   668:     if (!$inhibit_return_link) { 
        !           669:         $r->print(&mt('or').'</p>').&return_link($r);
        !           670:     } else {
        !           671:         $r->print('</p>');
        !           672:     }
1.75      albertel  673:     return 0;
1.3       albertel  674: }
                    675: 
                    676: sub allowed_slot {
1.48      albertel  677:     my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
1.49      albertel  678: 
1.3       albertel  679:     #already started
                    680:     if ($slot->{'starttime'} < time) {
1.76      albertel  681: 	return 0;
1.3       albertel  682:     }
1.5       albertel  683:     &Apache::lonxml::debug("$slot_name starttime good");
1.49      albertel  684: 
1.3       albertel  685:     #already ended
                    686:     if ($slot->{'endtime'} < time) {
                    687: 	return 0;
                    688:     }
1.5       albertel  689:     &Apache::lonxml::debug("$slot_name endtime good");
1.49      albertel  690: 
1.3       albertel  691:     # not allowed to pick this one
                    692:     if (defined($slot->{'type'})
                    693: 	&& $slot->{'type'} ne 'schedulable_student') {
                    694: 	return 0;
                    695:     }
1.5       albertel  696:     &Apache::lonxml::debug("$slot_name type good");
1.49      albertel  697: 
1.53      albertel  698:     # reserve time not yet started
                    699:     if ($slot->{'startreserve'} > time) {
                    700: 	return 0;
                    701:     }
                    702:     &Apache::lonxml::debug("$slot_name reserve good");
                    703: 
1.50      albertel  704:     my $userallowed=0;
1.49      albertel  705:     # its for a different set of users
1.50      albertel  706:     if (defined($slot->{'allowedsections'})) {
                    707: 	if (!defined($env{'request.role.sec'})
                    708: 	    && grep(/^No section assigned$/,
                    709: 		    split(',',$slot->{'allowedsections'}))) {
                    710: 	    $userallowed=1;
                    711: 	}
                    712: 	if (defined($env{'request.role.sec'})
                    713: 	    && grep(/^\Q$env{'request.role.sec'}\E$/,
                    714: 		    split(',',$slot->{'allowedsections'}))) {
                    715: 	    $userallowed=1;
                    716: 	}
1.68      albertel  717: 	if (defined($env{'request.course.groups'})) {
                    718: 	    my @groups = split(/:/,$env{'request.course.groups'});
                    719: 	    my @allowed_sec = split(',',$slot->{'allowedsections'});
                    720: 	    foreach my $group (@groups) {
                    721: 		if (grep {$_ eq $group} (@allowed_sec)) {
                    722: 		    $userallowed=1;
                    723: 		    last;
                    724: 		}
                    725: 	    }
                    726: 	}
1.49      albertel  727:     }
1.50      albertel  728:     &Apache::lonxml::debug("$slot_name sections is $userallowed");
1.49      albertel  729: 
                    730:     # its for a different set of users
1.50      albertel  731:     if (defined($slot->{'allowedusers'})
                    732: 	&& grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
                    733: 		split(',',$slot->{'allowedusers'}))) {
                    734: 	$userallowed=1;
1.49      albertel  735:     }
1.51      albertel  736: 
                    737:     if (!defined($slot->{'allowedusers'})
                    738: 	&& !defined($slot->{'allowedsections'})) {
                    739: 	$userallowed=1;
                    740:     }
                    741: 
1.50      albertel  742:     &Apache::lonxml::debug("$slot_name user is $userallowed");
                    743:     return 0 if (!$userallowed);
1.49      albertel  744: 
1.3       albertel  745:     # not allowed for this resource
                    746:     if (defined($slot->{'symb'})
                    747: 	&& $slot->{'symb'} ne $symb) {
                    748: 	return 0;
                    749:     }
1.50      albertel  750: 
1.48      albertel  751:     my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
                    752: 				       $consumed_uniqueperiods);
1.85      raeburn   753:     if ($conflict =~ /^error: /) {
                    754:         return 0;
1.86      raeburn   755:     } elsif ($conflict ne '') {
1.44      albertel  756: 	if ($slots->{$conflict}{'starttime'} < time) {
                    757: 	    return 0;
                    758: 	}
                    759:     }
1.5       albertel  760:     &Apache::lonxml::debug("$slot_name symb good");
1.3       albertel  761:     return 1;
1.2       albertel  762: }
                    763: 
1.3       albertel  764: sub get_description {
                    765:     my ($slot_name,$slot)=@_;
                    766:     my $description=$slot->{'description'};
                    767:     if (!defined($description)) {
1.4       albertel  768: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3       albertel  769: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
                    770: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
                    771:     }
                    772:     return $description;
                    773: }
1.2       albertel  774: 
                    775: sub show_choices {
                    776:     my ($r,$symb)=@_;
                    777: 
                    778:     my ($cnum,$cdom)=&get_course();
                    779:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.48      albertel  780:     my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
1.85      raeburn   781:     if (ref($consumed_uniqueperiods) eq 'HASH') {
                    782:         if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
                    783:             $r->print('<span class="LC_error">'.
                    784:                       &mt('An error occurred determining slot availability').
                    785:                       '</span>');
                    786:             return;
                    787:         }
                    788:     } elsif ($consumed_uniqueperiods =~ /^error: /) {
                    789:         $r->print('<span class="LC_error">'.
                    790:                   &mt('An error occurred determining slot availability').
                    791:                   '</span>');
                    792:         return;
                    793:     }
1.3       albertel  794:     my $available;
1.5       albertel  795:     &Apache::lonxml::debug("Checking Slots");
1.43      albertel  796:     my @got_slots=&check_for_reservation($symb,'allslots');
1.85      raeburn   797:     if ($got_slots[0] =~ /^error: /) {
                    798:         $r->print('<span class="LC_error">'.
                    799:                   &mt('An error occurred determining slot availability').
                    800:                   '</span>');
                    801:         return;
                    802:     }
                    803:     $r->print('<table border="1">');
1.2       albertel  804:     foreach my $slot (sort 
                    805: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
                    806: 		      (keys(%slots)))  {
1.5       albertel  807: 
                    808: 	&Apache::lonxml::debug("Checking Slot $slot");
1.48      albertel  809: 	next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
                    810: 				$consumed_uniqueperiods));
1.3       albertel  811: 
                    812: 	$available++;
                    813: 
                    814: 	my $description=&get_description($slot,$slots{$slot});
1.2       albertel  815: 
                    816: 	my $form=&mt('Unavailable');
1.43      albertel  817: 	if ((grep(/^\Q$slot\E$/,@got_slots)) ||
1.7       albertel  818: 	    &space_available($slot,$slots{$slot},$symb)) {
1.5       albertel  819: 	    my $text=&mt('Select');
                    820: 	    my $command='get';
1.43      albertel  821: 	    if (grep(/^\Q$slot\E$/,@got_slots)) {
1.70      albertel  822: 		$text=&mt('Drop Reservation');
1.5       albertel  823: 		$command='release';
1.43      albertel  824: 	    } else {
                    825: 		my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
1.48      albertel  826: 						   \%slots,
                    827: 						   $consumed_uniqueperiods);
1.85      raeburn   828:                 if ($conflict) {
                    829:                     if ($conflict =~ /^error: /) {
                    830:                         $r->print('<tr><td><span class="LC_error" colspan="2">'
                    831:                                   .&mt('Slot: [_1] has unknown status.',$description)
                    832:                                   .'</span></td></tr>');
                    833:                     } else {
                    834: 		        $text=&mt('Change Reservation');
                    835: 		        $command='get';
                    836: 		    }
                    837:                 }
1.5       albertel  838: 	    }
1.63      www       839: 	    my $escsymb=&escape($symb);
1.2       albertel  840: 	    $form=<<STUFF;
1.64      albertel  841:    <form method="post" action="/adm/slotrequest">
1.5       albertel  842:      <input type="submit" name="Select" value="$text" />
1.3       albertel  843:      <input type="hidden" name="symb" value="$escsymb" />
                    844:      <input type="hidden" name="slotname" value="$slot" />
1.5       albertel  845:      <input type="hidden" name="command" value="$command" />
1.2       albertel  846:    </form>
                    847: STUFF
                    848: 	}
                    849: 	$r->print(<<STUFF);
                    850: <tr>
                    851:  <td>$form</td>
                    852:  <td>$description</td>
                    853: </tr>
                    854: STUFF
                    855:     }
1.3       albertel  856: 
                    857:     if (!$available) {
1.87    ! raeburn   858: 	$r->print('<tr><td>'.&mt('No available times.').
        !           859:                   ' <a href="/adm/flip?postdata=return:">'.
1.3       albertel  860: 		  &mt('Return to last resource').'</a></td></tr>');
                    861:     }
1.2       albertel  862:     $r->print('</table>');
                    863: }
                    864: 
1.30      albertel  865: sub to_show {
1.54      albertel  866:     my ($slotname,$slot,$when,$deleted,$name) = @_;
1.30      albertel  867:     my $time=time;
                    868:     my $week=60*60*24*7;
1.54      albertel  869: 
1.35      albertel  870:     if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
                    871: 	return 0;
                    872:     }
1.54      albertel  873: 
                    874:     if ($name && $name->{'value'} =~ /\w/) {
                    875: 	if ($name->{'type'} eq 'substring') {
                    876: 	    if ($slotname !~ /\Q$name->{'value'}\E/) {
                    877: 		return 0;
                    878: 	    }
                    879: 	}
                    880: 	if ($name->{'type'} eq 'exact') {
                    881: 	    if ($slotname eq $name->{'value'}) {
                    882: 		return 0;
                    883: 	    }
                    884: 	}
                    885:     }
                    886: 
1.35      albertel  887:     if ($when eq 'any') {
                    888: 	return 1;
                    889:     } elsif ($when eq 'now') {
1.30      albertel  890: 	if ($time > $slot->{'starttime'} &&
                    891: 	    $time < $slot->{'endtime'}) {
                    892: 	    return 1;
                    893: 	}
                    894: 	return 0;
                    895:     } elsif ($when eq 'nextweek') {
                    896: 	if ( ($time        < $slot->{'starttime'} &&
                    897: 	      ($time+$week) > $slot->{'starttime'})
                    898: 	     ||
                    899: 	     ($time        < $slot->{'endtime'} &&
                    900: 	      ($time+$week) > $slot->{'endtime'}) ) {
                    901: 	    return 1;
                    902: 	}
                    903: 	return 0;
                    904:     } elsif ($when eq 'lastweek') {
                    905: 	if ( ($time        > $slot->{'starttime'} &&
                    906: 	      ($time-$week) < $slot->{'starttime'})
                    907: 	     ||
                    908: 	     ($time        > $slot->{'endtime'} &&
                    909: 	      ($time-$week) < $slot->{'endtime'}) ) {
                    910: 	    return 1;
                    911: 	}
                    912: 	return 0;
                    913:     } elsif ($when eq 'willopen') {
                    914: 	if ($time < $slot->{'starttime'}) {
                    915: 	    return 1;
                    916: 	}
                    917: 	return 0;
                    918:     } elsif ($when eq 'wereopen') {
                    919: 	if ($time > $slot->{'endtime'}) {
                    920: 	    return 1;
                    921: 	}
                    922: 	return 0;
                    923:     }
                    924:     
                    925:     return 1;
                    926: }
                    927: 
1.33      albertel  928: sub remove_link {
                    929:     my ($slotname,$entry,$uname,$udom,$symb) = @_;
                    930: 
1.55      albertel  931:     my $remove = &mt('Remove');
                    932: 
                    933:     if ($entry eq 'remove all') {
                    934: 	$remove = &mt('Remove All');
                    935: 	undef($uname);
                    936: 	undef($udom);
                    937:     }
                    938: 
1.63      www       939:     $slotname  = &escape($slotname);
                    940:     $entry     = &escape($entry);
                    941:     $uname     = &escape($uname);
                    942:     $udom      = &escape($udom);
                    943:     $symb      = &escape($symb);
1.33      albertel  944: 
                    945:     return <<"END_LINK";
1.64      albertel  946:  <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  947:    >($remove)</a>
                    948: END_LINK
                    949: 
                    950: }
                    951: 
1.5       albertel  952: sub show_table {
1.19      albertel  953:     my ($r,$mgr)=@_;
1.5       albertel  954: 
                    955:     my ($cnum,$cdom)=&get_course();
                    956:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19      albertel  957:     if ( (keys(%slots))[0] =~ /^error: 2 /) {
                    958: 	undef(%slots);
                    959:     } 
1.5       albertel  960:     my $available;
1.14      albertel  961:     if ($mgr eq 'F') {
1.72      rezaferr  962:     # FIXME: This line should be deleted once Slots uses breadcrumbs
1.73      albertel  963:     $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
1.72      rezaferr  964: 
1.30      albertel  965: 	$r->print('<div>');
1.64      albertel  966: 	$r->print('<form method="post" action="/adm/slotrequest">
1.14      albertel  967: <input type="hidden" name="command" value="uploadstart" />
                    968: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
                    969: </form>');
1.72      rezaferr  970: 	$r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
1.64      albertel  971: 	$r->print('<form method="post" action="/adm/helper/newslot.helper">
1.28      albertel  972: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
                    973: </form>');
1.72      rezaferr  974: 	$r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
1.30      albertel  975: 	$r->print('</div>');
1.14      albertel  976:     }
1.29      albertel  977:     
1.54      albertel  978:     my %Saveable_Parameters = ('show'              => 'array',
                    979: 			       'when'              => 'scalar',
                    980: 			       'order'             => 'scalar',
                    981: 			       'deleted'           => 'scalar',
                    982: 			       'name_filter_type'  => 'scalar',
                    983: 			       'name_filter_value' => 'scalar',
1.35      albertel  984: 			       );
1.46      albertel  985:     &Apache::loncommon::store_course_settings('slotrequest',
                    986: 					      \%Saveable_Parameters);
                    987:     &Apache::loncommon::restore_course_settings('slotrequest',
                    988: 						\%Saveable_Parameters);
                    989:     &Apache::grades::init_perm();
                    990:     my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
                    991:     &Apache::grades::reset_perm();
1.29      albertel  992: 
1.54      albertel  993:     # what to display filtering
1.30      albertel  994:     my %show_fields=&Apache::lonlocal::texthash(
1.49      albertel  995: 	     'name'            => 'Slot Name',
                    996: 	     'description'     => 'Description',
                    997: 	     'type'            => 'Type',
                    998: 	     'starttime'       => 'Start time',
                    999: 	     'endtime'         => 'End Time',
                   1000:              'startreserve'    => 'Time students can start reserving',
                   1001: 	     'secret'          => 'Secret Word',
1.74      albertel 1002: 	     'space'           => '# of students/max',
1.49      albertel 1003: 	     'ip'              => 'IP or DNS restrictions',
                   1004: 	     'symb'            => 'Resource slot is restricted to.',
                   1005: 	     'allowedsections' => 'Sections slot is restricted to.',
                   1006: 	     'allowedusers'    => 'Users slot is restricted to.',
                   1007: 	     'uniqueperiod'    => 'Period of time slot is unique',
                   1008: 	     'scheduled'       => 'Scheduled Students',
                   1009: 	     'proctor'         => 'List of proctors');
1.30      albertel 1010:     my @show_order=('name','description','type','starttime','endtime',
1.74      albertel 1011: 		    'startreserve','secret','space','ip','symb',
1.49      albertel 1012: 		    'allowedsections','allowedusers','uniqueperiod',
                   1013: 		    'scheduled','proctor');
1.30      albertel 1014:     my @show = 
1.29      albertel 1015: 	(exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30      albertel 1016: 	                            : keys(%show_fields);
                   1017:     my %show =  map { $_ => 1 } (@show);
                   1018: 
1.54      albertel 1019:     #when filtering setup
1.30      albertel 1020:     my %when_fields=&Apache::lonlocal::texthash(
1.35      albertel 1021: 	     'now'      => 'Open now',
1.30      albertel 1022: 	     'nextweek' => 'Open within the next week',
                   1023: 	     'lastweek' => 'Were open last week',
                   1024: 	     'willopen' => 'Will open later',
1.35      albertel 1025: 	     'wereopen' => 'Were open',
                   1026: 	     'any'      => 'Anytime',
                   1027: 						);
                   1028:     my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1.30      albertel 1029:     $when_fields{'select_form_order'} = \@when_order;
                   1030:     my $when = 	(exists($env{'form.when'})) ? $env{'form.when'}
                   1031:                                             : 'now';
1.29      albertel 1032: 
1.54      albertel 1033:     #display of students setup
1.46      albertel 1034:     my %stu_display_fields=
                   1035: 	&Apache::lonlocal::texthash('username' => 'User name',
                   1036: 				    'fullname' => 'Full name',
                   1037: 				    );
                   1038:     my @stu_display_order=('fullname','username');
                   1039:     my @stu_display = 
                   1040: 	(exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
                   1041: 	                                  : keys(%stu_display_fields);
                   1042:     my %stu_display =  map { $_ => 1 } (@stu_display);
                   1043: 
1.54      albertel 1044:     #name filtering setup
                   1045:     my %name_filter_type_fields=
                   1046: 	&Apache::lonlocal::texthash('substring' => 'Substring',
                   1047: 				    'exact'     => 'Exact',
                   1048: 				    #'reg'       => 'Regular Expression',
                   1049: 				    );
                   1050:     my @name_filter_type_order=('substring','exact');
                   1051: 
                   1052:     $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
                   1053:     my $name_filter_type = 
                   1054: 	(exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
                   1055:                                                 : 'substring';
                   1056:     my $name_filter = {'type'  => $name_filter_type,
                   1057: 		       'value' => $env{'form.name_filter_value'},};
                   1058: 
1.64      albertel 1059:     
1.54      albertel 1060:     #deleted slot filtering
1.64      albertel 1061:     #default to hide if no value
                   1062:     $env{'form.deleted'} ||= 'hide';
1.35      albertel 1063:     my $hide_radio = 
                   1064: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
                   1065:     my $show_radio = 
                   1066: 	&Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
                   1067: 	
1.64      albertel 1068:     $r->print('<form method="post" action="/adm/slotrequest">
1.30      albertel 1069: <input type="hidden" name="command" value="showslots" />');
                   1070:     $r->print('<div>');
1.35      albertel 1071:     $r->print('<table class="inline">
                   1072:       <tr><th>'.&mt('Show').'</th>
1.46      albertel 1073:           <th>'.&mt('Student Display').'</th>
1.35      albertel 1074:           <th>'.&mt('Open').'</th>
1.54      albertel 1075:           <th>'.&mt('Slot Name Filter').'</th>
1.35      albertel 1076:           <th>'.&mt('Options').'</th>
                   1077:       </tr>
                   1078:       <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
                   1079: 	      '</td>
1.46      albertel 1080:            <td>
                   1081:          '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
                   1082: 						    6,\%stu_display_fields,
                   1083: 						    \@stu_display_order).'
                   1084:            </td>
1.35      albertel 1085:            <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
                   1086:           '</td>
1.54      albertel 1087:            <td>'.&Apache::loncommon::select_form($name_filter_type,
                   1088: 						 'name_filter_type',
                   1089: 						 %name_filter_type_fields).
                   1090: 	      '<br />'.
                   1091: 	      &Apache::lonhtmlcommon::textbox('name_filter_value',
                   1092: 					      $env{'form.name_filter_value'},
                   1093: 					      15).
                   1094:           '</td>
1.35      albertel 1095:            <td>
                   1096:             <table>
                   1097:               <tr>
                   1098:                 <td rowspan="2">Deleted slots:</td>
                   1099:                 <td><label>'.$show_radio.'Show</label></td>
                   1100:               </tr>
                   1101:               <tr>
                   1102:                 <td><label>'.$hide_radio.'Hide</label></td>
                   1103:               </tr>
                   1104:             </table>
                   1105: 	  </td>
                   1106:        </tr>
                   1107:     </table>');
1.30      albertel 1108:     $r->print('</div>');
                   1109:     $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21      albertel 1110:     my $linkstart='<a href="/adm/slotrequest?command=showslots&amp;order=';
1.65      albertel 1111:     $r->print(&Apache::loncommon::start_data_table().
                   1112: 	      &Apache::loncommon::start_data_table_header_row().'
                   1113: 	       <th></th>');
1.30      albertel 1114:     foreach my $which (@show_order) {
                   1115: 	if ($which ne 'proctor' && exists($show{$which})) {
                   1116: 	    $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1.29      albertel 1117: 	}
                   1118:     }
1.65      albertel 1119:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.29      albertel 1120: 
1.21      albertel 1121:     my %name_cache;
                   1122:     my $slotsort = sub {
1.74      albertel 1123: 	if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
1.21      albertel 1124: 	    if (lc($slots{$a}->{$env{'form.order'}})
                   1125: 		ne lc($slots{$b}->{$env{'form.order'}})) {
                   1126: 		return (lc($slots{$a}->{$env{'form.order'}}) 
                   1127: 			cmp lc($slots{$b}->{$env{'form.order'}}));
                   1128: 	    }
1.74      albertel 1129: 	} elsif ($env{'form.order'} eq 'space') {
                   1130: 	    if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
                   1131: 		return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
                   1132: 	    }
1.23      albertel 1133: 	} elsif ($env{'form.order'} eq 'name') {
                   1134: 	    if (lc($a) cmp lc($b)) {
                   1135: 		return lc($a) cmp lc($b);
                   1136: 	    }
1.29      albertel 1137: 	} elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21      albertel 1138: 	    
                   1139: 	    if ($slots{$a}->{'uniqueperiod'}[0] 
                   1140: 		ne $slots{$b}->{'uniqueperiod'}[0]) {
                   1141: 		return ($slots{$a}->{'uniqueperiod'}[0]
                   1142: 			cmp $slots{$b}->{'uniqueperiod'}[0]);
                   1143: 	    }
                   1144: 	    if ($slots{$a}->{'uniqueperiod'}[1] 
                   1145: 		ne $slots{$b}->{'uniqueperiod'}[1]) {
                   1146: 		return ($slots{$a}->{'uniqueperiod'}[1]
                   1147: 			cmp $slots{$b}->{'uniqueperiod'}[1]);
                   1148: 	    }
                   1149: 	}
                   1150: 	return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
                   1151:     };
1.74      albertel 1152: 
                   1153:     my %consumed;
                   1154:     if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
                   1155: 	%consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
                   1156: 	my ($tmp)=%consumed;
                   1157: 	if ($tmp =~ /^error: /) { undef(%consumed); }
                   1158:     }
                   1159: 
1.21      albertel 1160:     foreach my $slot (sort $slotsort (keys(%slots)))  {
1.54      albertel 1161: 	if (!&to_show($slot,$slots{$slot},$when,
                   1162: 		      $env{'form.deleted'},$name_filter)) { next; }
1.5       albertel 1163: 	if (defined($slots{$slot}->{'type'})
                   1164: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13      albertel 1165: 	    #next;
1.5       albertel 1166: 	}
                   1167: 	my $description=&get_description($slot,$slots{$slot});
1.74      albertel 1168: 	my ($id_count,$ids);
                   1169: 	    
                   1170: 	if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
1.79      albertel 1171: 	    my $re_str = "$slot\0";
                   1172: 	    my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
1.74      albertel 1173: 	    $id_count = scalar(@this_slot);
                   1174: 	    if (exists($show{'scheduled'})) {
1.54      albertel 1175: 		foreach my $entry (sort { $consumed{$a}{name} cmp 
                   1176: 					      $consumed{$b}{name} }
1.79      albertel 1177: 				   (@this_slot)) {
1.47      albertel 1178: 		    my (undef,$id)=split("\0",$entry);
1.57      albertel 1179: 		    my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.84      bisitz   1180: 		    $ids.= '<span class="LC_nobreak">';
1.47      albertel 1181: 		    foreach my $item (@stu_display_order) {
                   1182: 			if ($stu_display{$item}) {
                   1183: 			    if ($item eq 'fullname') {
                   1184: 				$ids.=$fullname->{"$uname:$udom"}.' ';
                   1185: 			    } elsif ($item eq 'username') {
1.57      albertel 1186: 				$ids.="<tt>$uname:$udom</tt> ";
1.47      albertel 1187: 			    }
1.46      albertel 1188: 			}
                   1189: 		    }
1.47      albertel 1190: 		    $ids.=&remove_link($slot,$entry,$uname,$udom,
1.84      bisitz   1191: 				       $consumed{$entry}{'symb'}).'</span><br />';
1.46      albertel 1192: 		}
1.38      albertel 1193: 	    }
1.5       albertel 1194: 	}
1.33      albertel 1195: 
1.24      albertel 1196: 	my $start=($slots{$slot}->{'starttime'}?
                   1197: 		   &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
                   1198: 	my $end=($slots{$slot}->{'endtime'}?
                   1199: 		 &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1.28      albertel 1200: 	my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24      albertel 1201: 			   &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
                   1202: 	
1.14      albertel 1203: 	my $unique;
                   1204: 	if (ref($slots{$slot}{'uniqueperiod'})) {
1.64      albertel 1205: 	    $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
1.14      albertel 1206: 		localtime($slots{$slot}{'uniqueperiod'}[1]);
                   1207: 	}
1.33      albertel 1208: 
1.29      albertel 1209: 	my $title;
                   1210: 	if (exists($slots{$slot}{'symb'})) {
                   1211: 	    my (undef,undef,$res)=
                   1212: 		&Apache::lonnet::decode_symb($slots{$slot}{'symb'});
                   1213: 	    $res =   &Apache::lonnet::clutter($res);
                   1214: 	    $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
                   1215: 	    $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
                   1216: 	}
1.33      albertel 1217: 
1.49      albertel 1218: 	my $allowedsections;
                   1219: 	if (exists($show{'allowedsections'})) {
                   1220: 	    $allowedsections = 
                   1221: 		join(', ',sort(split(/\s*,\s*/,
                   1222: 				     $slots{$slot}->{'allowedsections'})));
                   1223: 	}
                   1224: 
                   1225: 	my @allowedusers;
                   1226: 	if (exists($show{'allowedusers'})) {
                   1227: 	    @allowedusers= map {
                   1228: 		my ($uname,$udom)=split(/:/,$_);
                   1229: 		my $fullname=$name_cache{$_};
                   1230: 		if (!defined($fullname)) {
                   1231: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1232: 		    $fullname =~s/\s/&nbsp;/g;
                   1233: 		    $name_cache{$_} = $fullname;
                   1234: 		}
                   1235: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1236: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
                   1237: 	}
                   1238: 	my $allowedusers=join(', ',@allowedusers);
                   1239: 	
1.29      albertel 1240: 	my @proctors;
                   1241: 	my $rowspan=1;
                   1242: 	my $colspan=1;
1.30      albertel 1243: 	if (exists($show{'proctor'})) {
1.29      albertel 1244: 	    $rowspan=2;
                   1245: 	    @proctors= map {
1.62      albertel 1246: 		my ($uname,$udom)=split(/:/,$_);
1.29      albertel 1247: 		my $fullname=$name_cache{$_};
                   1248: 		if (!defined($fullname)) {
                   1249: 		    $fullname = &Apache::loncommon::plainname($uname,$udom);
                   1250: 		    $fullname =~s/\s/&nbsp;/g;
                   1251: 		    $name_cache{$_} = $fullname;
                   1252: 		}
                   1253: 		&Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
                   1254: 	    } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
                   1255: 	}
1.20      albertel 1256: 	my $proctors=join(', ',@proctors);
1.14      albertel 1257: 
1.34      albertel 1258: 	my $edit=(<<"EDITLINK");
1.31      albertel 1259: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
                   1260: EDITLINK
1.34      albertel 1261: 
                   1262: 	my $delete=(<<"DELETELINK");
1.64      albertel 1263: <a href="/adm/slotrequest?command=delete&amp;slotname=$slot">Delete</a>
1.34      albertel 1264: DELETELINK
1.55      albertel 1265: 
1.56      albertel 1266:         my $remove_all=&remove_link($slot,'remove all').'<br />';
1.55      albertel 1267: 
1.34      albertel 1268:         if ($ids ne '') { undef($delete); }
1.56      albertel 1269: 	if ($slots{$slot}{'type'} ne 'schedulable_student' 
                   1270: 	    || $ids eq '') { 
1.55      albertel 1271: 	    undef($remove_all);
                   1272: 	}
1.34      albertel 1273: 
1.65      albertel 1274: 	my $row_start=&Apache::loncommon::start_data_table_row();
                   1275: 	my $row_end=&Apache::loncommon::end_data_table_row();
                   1276:         $r->print($row_start.
                   1277: 		  "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1.30      albertel 1278: 	if (exists($show{'name'})) {
1.29      albertel 1279: 	    $colspan++;$r->print("<td>$slot</td>");
                   1280: 	}
1.33      albertel 1281: 	if (exists($show{'description'})) {
                   1282: 	    $colspan++;$r->print("<td>$description</td>\n");
                   1283: 	}
1.30      albertel 1284: 	if (exists($show{'type'})) {
1.29      albertel 1285: 	    $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
                   1286: 	}
1.30      albertel 1287: 	if (exists($show{'starttime'})) {
1.29      albertel 1288: 	    $colspan++;$r->print("<td>$start</td>\n");
                   1289: 	}
1.30      albertel 1290: 	if (exists($show{'endtime'})) {
1.29      albertel 1291: 	    $colspan++;$r->print("<td>$end</td>\n");
                   1292: 	}
1.30      albertel 1293: 	if (exists($show{'startreserve'})) {
1.29      albertel 1294: 	    $colspan++;$r->print("<td>$start_reserve</td>\n");
                   1295: 	}
1.30      albertel 1296: 	if (exists($show{'secret'})) {
1.29      albertel 1297: 	    $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
                   1298: 	}
1.74      albertel 1299: 	if (exists($show{'space'})) {
                   1300: 	    my $display = $id_count;
                   1301: 	    if ($slots{$slot}{'maxspace'}>0) {
                   1302: 		$display.='/'.$slots{$slot}{'maxspace'};
                   1303: 		if ($slots{$slot}{'maxspace'} <= $id_count) {
                   1304: 		    $display = '<strong>'.$display.' (full) </strong>';
                   1305: 		}
                   1306: 	    }
                   1307: 	    $colspan++;$r->print("<td>$display</td>\n");
1.29      albertel 1308: 	}
1.30      albertel 1309: 	if (exists($show{'ip'})) {
1.29      albertel 1310: 	    $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
                   1311: 	}
1.30      albertel 1312: 	if (exists($show{'symb'})) {
1.29      albertel 1313: 	    $colspan++;$r->print("<td>$title</td>\n");
                   1314: 	}
1.49      albertel 1315: 	if (exists($show{'allowedsections'})) {
                   1316: 	    $colspan++;$r->print("<td>$allowedsections</td>\n");
                   1317: 	}
                   1318: 	if (exists($show{'allowedusers'})) {
                   1319: 	    $colspan++;$r->print("<td>$allowedusers</td>\n");
1.29      albertel 1320: 	}
1.64      albertel 1321: 	if (exists($show{'uniqueperiod'})) {
                   1322: 	    $colspan++;$r->print("<td>$unique</td>\n");
                   1323: 	}
1.47      albertel 1324: 	if (exists($show{'scheduled'})) {
1.64      albertel 1325: 	    $colspan++;$r->print("<td>$remove_all $ids</td>\n");
1.47      albertel 1326: 	}
1.65      albertel 1327: 	$r->print("$row_end\n");
1.30      albertel 1328: 	if (exists($show{'proctor'})) {
1.29      albertel 1329: 	    $r->print(<<STUFF);
1.65      albertel 1330: $row_start
1.29      albertel 1331:  <td colspan="$colspan">$proctors</td>
1.65      albertel 1332: $row_end
1.5       albertel 1333: STUFF
1.29      albertel 1334:         }
1.5       albertel 1335:     }
1.64      albertel 1336:     $r->print('</table></form>');
1.5       albertel 1337: }
                   1338: 
1.14      albertel 1339: sub upload_start {
1.19      albertel 1340:     my ($r)=@_;    
1.14      albertel 1341:     $r->print(&Apache::grades::checkforfile_js());
                   1342:     my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
                   1343:     $result.='&nbsp;<b>'.
                   1344: 	&mt('Specify a file containing the slot definitions.').
                   1345: 	'</b></td></tr>'."\n";
1.87    ! raeburn  1346:     $result.='<tr bgcolor="#ffffe6"><td>'."\n";
1.14      albertel 1347:     my $upfile_select=&Apache::loncommon::upfile_select_html();
                   1348:     my $ignore=&mt('Ignore First Line');
                   1349:     $result.=<<ENDUPFORM;
                   1350: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
                   1351: <input type="hidden" name="command" value="csvuploadmap" />
                   1352: $upfile_select
                   1353: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
                   1354: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
                   1355: </form>
                   1356: ENDUPFORM
                   1357:     $result.='</td></tr></table>'."\n";
                   1358:     $result.='</td></tr></table>'."\n";
                   1359:     $r->print($result);
                   1360: }
                   1361: 
                   1362: sub csvuploadmap_header {
1.19      albertel 1363:     my ($r,$datatoken,$distotal)= @_;
1.14      albertel 1364:     my $javascript;
                   1365:     if ($env{'form.upfile_associate'} eq 'reverse') {
                   1366: 	$javascript=&csvupload_javascript_reverse_associate();
                   1367:     } else {
                   1368: 	$javascript=&csvupload_javascript_forward_associate();
                   1369:     }
                   1370: 
                   1371:     my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
                   1372:     my $ignore=&mt('Ignore First Line');
1.72      rezaferr 1373: 	my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
                   1374: 
1.14      albertel 1375:     $r->print(<<ENDPICK);
                   1376: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1.72      rezaferr 1377: <h3>Identify fields $help_field</h3>	
1.14      albertel 1378: Total number of records found in file: $distotal <hr />
                   1379: Enter as many fields as you can. The system will inform you and bring you back
                   1380: to this page if the data selected is insufficient to create the slots.<hr />
                   1381: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
                   1382: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
                   1383: <input type="hidden" name="associate"  value="" />
                   1384: <input type="hidden" name="datatoken"  value="$datatoken" />
                   1385: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
                   1386: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
                   1387: <input type="hidden" name="upfile_associate" 
                   1388:                                        value="$env{'form.upfile_associate'}" />
                   1389: <input type="hidden" name="command"    value="csvuploadassign" />
                   1390: <hr />
                   1391: <script type="text/javascript" language="Javascript">
                   1392: $javascript
                   1393: </script>
                   1394: ENDPICK
                   1395:     return '';
                   1396: 
                   1397: }
                   1398: 
                   1399: sub csvuploadmap_footer {
                   1400:     my ($request,$i,$keyfields) =@_;
1.87    ! raeburn  1401:     my $buttontext = &mt('Create Slots');
1.14      albertel 1402:     $request->print(<<ENDPICK);
                   1403: </table>
                   1404: <input type="hidden" name="nfields" value="$i" />
                   1405: <input type="hidden" name="keyfields" value="$keyfields" />
1.87    ! raeburn  1406: <input type="button" onClick="javascript:verify(this.form)" value="$buttontext" /><br />
1.14      albertel 1407: </form>
                   1408: ENDPICK
                   1409: }
                   1410: 
                   1411: sub csvupload_javascript_reverse_associate {
                   1412:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1413:     return(<<ENDPICK);
                   1414:   function verify(vf) {
                   1415:     var foundstart=0;
                   1416:     var foundend=0;
                   1417:     var foundname=0;
                   1418:     var foundtype=0;
                   1419:     for (i=0;i<=vf.nfields.value;i++) {
                   1420:       tw=eval('vf.f'+i+'.selectedIndex');
                   1421:       if (i==0 && tw!=0) { foundname=1; }
                   1422:       if (i==1 && tw!=0) { foundtype=1; }
                   1423:       if (i==2 && tw!=0) { foundstat=1; }
                   1424:       if (i==3 && tw!=0) { foundend=1; }
                   1425:     }
                   1426:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1427: 	alert('$error1');
                   1428: 	return;
                   1429:     }
                   1430:     vf.submit();
                   1431:   }
                   1432:   function flip(vf,tf) {
                   1433:   }
                   1434: ENDPICK
                   1435: }
                   1436: 
                   1437: sub csvupload_javascript_forward_associate {
                   1438:     my $error1=&mt('You need to specify the name, starttime, endtime and a type');
                   1439:   return(<<ENDPICK);
                   1440:   function verify(vf) {
                   1441:     var foundstart=0;
                   1442:     var foundend=0;
                   1443:     var foundname=0;
                   1444:     var foundtype=0;
                   1445:     for (i=0;i<=vf.nfields.value;i++) {
                   1446:       tw=eval('vf.f'+i+'.selectedIndex');
                   1447:       if (tw==1) { foundname=1; }
                   1448:       if (tw==2) { foundtype=1; }
                   1449:       if (tw==3) { foundstat=1; }
                   1450:       if (tw==4) { foundend=1; }
                   1451:     }
                   1452:     if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
                   1453: 	alert('$error1');
                   1454: 	return;
                   1455:     }
                   1456:     vf.submit();
                   1457:   }
                   1458:   function flip(vf,tf) {
                   1459:   }
                   1460: ENDPICK
                   1461: }
                   1462: 
                   1463: sub csv_upload_map {
1.19      albertel 1464:     my ($r)= @_;
1.14      albertel 1465: 
                   1466:     my $datatoken;
                   1467:     if (!$env{'form.datatoken'}) {
                   1468: 	$datatoken=&Apache::loncommon::upfile_store($r);
                   1469:     } else {
                   1470: 	$datatoken=$env{'form.datatoken'};
                   1471: 	&Apache::loncommon::load_tmp_file($r);
                   1472:     }
                   1473:     my @records=&Apache::loncommon::upfile_record_sep();
                   1474:     if ($env{'form.noFirstLine'}) { shift(@records); }
1.19      albertel 1475:     &csvuploadmap_header($r,$datatoken,$#records+1);
1.14      albertel 1476:     my ($i,$keyfields);
                   1477:     if (@records) {
                   1478: 	my @fields=&csvupload_fields();
                   1479: 
                   1480: 	if ($env{'form.upfile_associate'} eq 'reverse') {	
                   1481: 	    &Apache::loncommon::csv_print_samples($r,\@records);
                   1482: 	    $i=&Apache::loncommon::csv_print_select_table($r,\@records,
                   1483: 							  \@fields);
                   1484: 	    foreach (@fields) { $keyfields.=$_->[0].','; }
                   1485: 	    chop($keyfields);
                   1486: 	} else {
                   1487: 	    unshift(@fields,['none','']);
                   1488: 	    $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
                   1489: 							    \@fields);
                   1490: 	    my %sone=&Apache::loncommon::record_sep($records[0]);
                   1491: 	    $keyfields=join(',',sort(keys(%sone)));
                   1492: 	}
                   1493:     }
                   1494:     &csvuploadmap_footer($r,$i,$keyfields);
                   1495: 
                   1496:     return '';
                   1497: }
                   1498: 
                   1499: sub csvupload_fields {
                   1500:     return (['name','Slot name'],
                   1501: 	    ['type','Type of slot'],
                   1502: 	    ['starttime','Start Time of slot'],
                   1503: 	    ['endtime','End Time of slot'],
1.15      albertel 1504: 	    ['startreserve','Reservation Start Time'],
1.14      albertel 1505: 	    ['ip','IP or DNS restriction'],
                   1506: 	    ['proctor','List of proctor ids'],
                   1507: 	    ['description','Slot Description'],
                   1508: 	    ['maxspace','Maximum number of reservations'],
                   1509: 	    ['symb','Resource Restriction'],
                   1510: 	    ['uniqueperiod','Date range of slot exclusion'],
1.49      albertel 1511: 	    ['secret','Secret word proctor uses to validate'],
                   1512: 	    ['allowedsections','Sections slot is restricted to'],
                   1513: 	    ['allowedusers','Users slot is restricted to'],
                   1514: 	    );
1.14      albertel 1515: }
                   1516: 
                   1517: sub csv_upload_assign {
1.19      albertel 1518:     my ($r,$mgr)= @_;
1.14      albertel 1519:     &Apache::loncommon::load_tmp_file($r);
                   1520:     my @slotdata = &Apache::loncommon::upfile_record_sep();
                   1521:     if ($env{'form.noFirstLine'}) { shift(@slotdata); }
                   1522:     my %fields=&Apache::grades::get_fields();
1.87    ! raeburn  1523:     $r->print('<h3>'.&mt('Creating Slots').'</h3>');
1.14      albertel 1524:     my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
                   1525:     my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
                   1526:     my $countdone=0;
1.31      albertel 1527:     my @errors;
1.14      albertel 1528:     foreach my $slot (@slotdata) {
                   1529: 	my %slot;
                   1530: 	my %entries=&Apache::loncommon::record_sep($slot);
                   1531: 	my $domain;
                   1532: 	my $name=$entries{$fields{'name'}};
1.31      albertel 1533: 	if ($name=~/^\s*$/) {
                   1534: 	    push(@errors,"Did not create slot with no name");
                   1535: 	    next;
                   1536: 	}
                   1537: 	if ($name=~/\s/) { 
                   1538: 	    push(@errors,"$name not created -- Name must not contain spaces");
                   1539: 	    next;
                   1540: 	}
                   1541: 	if ($name=~/\W/) { 
                   1542: 	    push(@errors,"$name not created -- Name must contain only letters, numbers and _");
                   1543: 	    next;
                   1544: 	}
1.14      albertel 1545: 	if ($entries{$fields{'type'}}) {
                   1546: 	    $slot{'type'}=$entries{$fields{'type'}};
                   1547: 	} else {
                   1548: 	    $slot{'type'}='preassigned';
                   1549: 	}
1.31      albertel 1550: 	if ($slot{'type'} ne 'preassigned' &&
                   1551: 	    $slot{'type'} ne 'schedulable_student') {
                   1552: 	    push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
                   1553: 	    next;
                   1554: 	}
1.14      albertel 1555: 	if ($entries{$fields{'starttime'}}) {
                   1556: 	    $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
                   1557: 	}
                   1558: 	if ($entries{$fields{'endtime'}}) {
1.16      albertel 1559: 	    $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14      albertel 1560: 	}
1.58      albertel 1561: 
                   1562: 	# start/endtime must be defined and greater than zero
                   1563: 	if (!$slot{'starttime'}) {
                   1564: 	    push(@errors,"$name not created -- Invalid start time");
                   1565: 	    next;
                   1566: 	}
                   1567: 	if (!$slot{'endtime'}) {
                   1568: 	    push(@errors,"$name not created -- Invalid end time");
                   1569: 	    next;
                   1570: 	}
                   1571: 	if ($slot{'starttime'} > $slot{'endtime'}) {
                   1572: 	    push(@errors,"$name not created -- Slot starts after it ends");
                   1573: 	    next;
                   1574: 	}
                   1575: 
1.23      albertel 1576: 	if ($entries{$fields{'startreserve'}}) {
                   1577: 	    $slot{'startreserve'}=
                   1578: 		&UnixDate($entries{$fields{'startreserve'}},"%s");
                   1579: 	}
1.58      albertel 1580: 	if (defined($slot{'startreserve'})
                   1581: 	    && $slot{'startreserve'} > $slot{'starttime'}) {
                   1582: 	    push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
                   1583: 	    next;
                   1584: 	}
                   1585: 
1.14      albertel 1586: 	foreach my $key ('ip','proctor','description','maxspace',
                   1587: 			 'secret','symb') {
                   1588: 	    if ($entries{$fields{$key}}) {
                   1589: 		$slot{$key}=$entries{$fields{$key}};
                   1590: 	    }
                   1591: 	}
1.58      albertel 1592: 
1.14      albertel 1593: 	if ($entries{$fields{'uniqueperiod'}}) {
                   1594: 	    my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
                   1595: 	    my @times=(&UnixDate($start,"%s"),
                   1596: 		       &UnixDate($end,"%s"));
                   1597: 	    $slot{'uniqueperiod'}=\@times;
                   1598: 	}
1.58      albertel 1599: 	if (defined($slot{'uniqueperiod'})
                   1600: 	    && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
                   1601: 	    push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
                   1602: 	    next;
                   1603: 	}
1.14      albertel 1604: 
                   1605: 	&Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
                   1606: 	$r->print('.');
                   1607: 	$r->rflush();
                   1608: 	$countdone++;
                   1609:     }
1.87    ! raeburn  1610:     $r->print('<p>'.&mt('Created [quant,_1,slot]',$countdone)."\n".'</p>');
1.31      albertel 1611:     foreach my $error (@errors) {
1.87    ! raeburn  1612: 	$r->print('<p><span class="LC_warning">'.$error.'</span></p>'."\n");
1.31      albertel 1613:     }
1.19      albertel 1614:     &show_table($r,$mgr);
1.14      albertel 1615:     return '';
                   1616: }
                   1617: 
1.1       albertel 1618: sub handler {
                   1619:     my $r=shift;
                   1620: 
1.30      albertel 1621:     &Apache::loncommon::content_type($r,'text/html');
                   1622:     &Apache::loncommon::no_cache($r);
                   1623:     if ($r->header_only()) {
                   1624: 	$r->send_http_header();
                   1625: 	return OK;
                   1626:     }
                   1627: 
1.8       albertel 1628:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28      albertel 1629:     
1.12      albertel 1630:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14      albertel 1631:     my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28      albertel 1632:     my $title='Requesting Another Worktime';
                   1633:     if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
                   1634: 	$title = 'Managing Slots';
                   1635:     }
                   1636:     &start_page($r,$title);
                   1637: 
1.8       albertel 1638:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19      albertel 1639: 	&show_table($r,$mgr);
1.33      albertel 1640:     } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
                   1641: 	&remove_registration($r);
                   1642:     } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1.55      albertel 1643: 	if ($env{'form.entry'} eq 'remove all') {
                   1644: 	    &release_all_slot($r,$mgr);
                   1645: 	} else {
                   1646: 	    &release_slot($r,undef,undef,undef,$mgr);
                   1647: 	}
1.34      albertel 1648:     } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
                   1649: 	&delete_slot($r);
1.14      albertel 1650:     } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19      albertel 1651: 	&upload_start($r);
1.14      albertel 1652:     } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19      albertel 1653: 	&csv_upload_map($r);
1.14      albertel 1654:     } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
                   1655: 	if ($env{'form.associate'} ne 'Reverse Association') {
1.19      albertel 1656: 	    &csv_upload_assign($r,$mgr);
1.14      albertel 1657: 	} else {
                   1658: 	    if ( $env{'form.upfile_associate'} ne 'reverse' ) {
                   1659: 		$env{'form.upfile_associate'} = 'reverse';
                   1660: 	    } else {
                   1661: 		$env{'form.upfile_associate'} = 'forward';
                   1662: 	    }
1.19      albertel 1663: 	    &csv_upload_map($r);
1.14      albertel 1664: 	}
1.8       albertel 1665:     } else {
1.63      www      1666: 	my $symb=&unescape($env{'form.symb'});
1.61      albertel 1667: 	if (!defined($symb)) {
                   1668: 	    &fail($r,'not_valid');
                   1669: 	    return OK;
                   1670: 	}
1.19      albertel 1671: 	my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.36      albertel 1672: 	my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1.66      albertel 1673: 	if ($useslots ne 'resource' 
                   1674: 	    && $useslots ne 'map' 
                   1675: 	    && $useslots ne 'map_map') {
1.61      albertel 1676: 	    &fail($r,'not_available');
1.19      albertel 1677: 	    return OK;
                   1678: 	}
                   1679: 	$env{'request.symb'}=$symb;
1.36      albertel 1680: 	my $type = ($res =~ /\.task$/) ? 'Task'
                   1681: 	                               : 'problem';
                   1682: 	my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1.11      albertel 1683: 	if ($status eq 'CAN_ANSWER' ||
                   1684: 	    $status eq 'NEEDS_CHECKIN' ||
                   1685: 	    $status eq 'WAITING_FOR_GRADE') {
                   1686: 	    &fail($r,'not_allowed');
                   1687: 	    return OK;
                   1688: 	}
                   1689: 	if ($env{'form.requestattempt'}) {
                   1690: 	    &show_choices($r,$symb);
                   1691: 	} elsif ($env{'form.command'} eq 'release') {
                   1692: 	    &release_slot($r,$symb);
                   1693: 	} elsif ($env{'form.command'} eq 'get') {
                   1694: 	    &get_slot($r,$symb);
                   1695: 	} elsif ($env{'form.command'} eq 'change') {
1.75      albertel 1696: 	    if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
                   1697: 		&release_slot($r,$symb,$env{'form.releaseslot'});
1.39      albertel 1698: 	    }
1.11      albertel 1699: 	} else {
1.87    ! raeburn  1700: 	    $r->print('<p>'.&mt('Unknown command: [_1]',$env{'form.command'}).'</p>');
1.11      albertel 1701: 	}
1.2       albertel 1702:     }
1.1       albertel 1703:     &end_page($r);
                   1704:     return OK;
                   1705: }
1.3       albertel 1706: 
                   1707: 1;
                   1708: __END__

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