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

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

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