File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.4: download - view: text, annotated - select for diffs
Sat Jun 4 08:17:06 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_1_99_2, version_1_99_1, version_1_99_0, HEAD
- after requesting a new slot the 'request a new time' button disappears
- this requires a new slot info to be set 'startreserve'

    1: # The LearningOnline Network with CAPA
    2: # Handler for requesting to have slots added to a students record
    3: #
    4: # $Id: slotrequest.pm,v 1.4 2005/06/04 08:17:06 albertel Exp $
    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;
   37: 
   38: sub fail {
   39:     my ($r,$code)=@_;
   40:     if ($code eq 'not_valid') {
   41: 	$r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>'.$env{'form.symb'});
   42: 
   43:     }
   44:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
   45: 	      &mt('Return to last resource').'</a></p>');
   46:     &end_page($r);
   47: }
   48: 
   49: sub start_page {
   50:     my ($r)=@_;
   51:     my $html=&Apache::lonxml::xmlbegin();
   52:     $r->print($html.'<head><title>'.
   53: 	      &mt('Request another Worktime').'</title></head>');
   54:     $r->print(&Apache::loncommon::bodytag('Requesting another Worktime'));
   55: }
   56: 
   57: sub end_page {
   58:     my ($r)=@_;
   59:     $r->print(&Apache::loncommon::endbodytag().'</html>');
   60: }
   61: 
   62: =pod
   63: 
   64:  slot_reservations db
   65:    - keys are 
   66:     - slotname\0id -> value is an hashref of
   67:                          name -> user@domain of holder
   68:                          timestamp -> timestamp of reservation
   69:                          symb -> symb of resource that it is reserved for
   70: 
   71: =cut
   72: 
   73: sub get_course {
   74:     (undef,my $courseid)=&Apache::lonxml::whichuser();
   75:     my $cdom=$env{'course.'.$courseid.'.domain'};
   76:     my $cnum=$env{'course.'.$courseid.'.num'};
   77:     return ($cnum,$cdom);
   78: }
   79: 
   80: sub get_reservation_ids {
   81:     my ($slot_name)=@_;
   82:     
   83:     my ($cnum,$cdom)=&get_course();
   84: 
   85:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
   86: 				       "^$slot_name\0");
   87:     
   88:     my ($tmp)=%consumed;
   89:     if ($tmp=~/^error: 2 / ) {
   90: 	return 0;
   91:     }
   92:     return keys(%consumed);
   93: }
   94: 
   95: sub space_available {
   96:     my ($slot_name,$slot)=@_;
   97:     my $max=$slot->{'maxspace'};
   98: 
   99:     if (!defined($max)) { return 1; }
  100: 
  101:     my $consumed=scalar(&get_reservation_ids($slot_name));
  102:     if ($consumed < $max) {
  103: 	return 1
  104:     }
  105:     return 0;
  106: }
  107: 
  108: sub check_for_reservation {
  109:     my ($symb)=@_;
  110:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  111: 				       $env{'user.domain'}, $env{'user.name'});
  112: 
  113:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  114: 				    $env{'user.domain'}, $env{'user.name'});
  115:     my @slots = (split(/:/,$student), split(/:/, $course));
  116: 
  117:     &Apache::lonxml::debug(" slot list is ".join(':',@slots));
  118: 
  119:     my ($cnum,$cdom)=&get_course();
  120:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  121: 
  122:     foreach my $slot_name (@slots) {
  123: 	next if (!defined($slots{$slot_name}) ||
  124: 		 !ref($slots{$slot_name}));
  125: 	&Apache::lonxml::debug(time." $slot_name ".
  126: 			       $slots{$slot_name}->{'starttime'}." -- ".
  127: 			       $slots{$slot_name}->{'startreserve'});
  128: 	if ($slots{$slot_name}->{'starttime'} > time &&
  129: 	    $slots{$slot_name}->{'startreserve'} < time) {
  130: 	    # between start of reservation times and start of slot
  131: 	    return($slot_name, $slots{$slot_name});
  132: 	}
  133:     }
  134:     return (undef,undef);
  135: }
  136: 
  137: # FIXME - depends on the parameter for the resource to be correct
  138: #         tho prevent multiple reservations
  139: 
  140: sub make_reservation {
  141:     my ($slot_name,$slot,$symb)=@_;
  142: 
  143:     my ($cnum,$cdom)=&get_course();
  144: 
  145:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  146: 				   $env{'user.domain'},$env{'user.name'});
  147:     &Apache::lonxml::debug("value is  $value<br />");
  148:     foreach my $other_slot (split(/:/, $value)) {
  149: 	if ($other_slot eq $slot_name) {
  150: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  151: 					       $cnum, "^$slot_name\0");   
  152: 
  153: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  154: 	    foreach my $key (keys(%consumed)) {
  155: 		if ($consumed{$key}->{'name'} eq $me) {
  156: 		    my $num=(split('\0',$key))[1];
  157: 		    return -$num;
  158: 		}
  159: 	    }
  160: 	}
  161:     }
  162: 
  163:     my $max=$slot->{'maxspace'};
  164:     if (!defined($max)) { $max=99999; }
  165: 
  166:     my (@ids)=&get_reservation_ids($slot_name);
  167: 
  168:     # FIXME we could end up having holes... 
  169:     my $last=0;
  170:     foreach my $id (@ids) {
  171: 	my $num=(split('\0',$id))[1];
  172: 	if ($num > $last) { $last=$num; }
  173:     }
  174:     
  175:     my $wanted=$last+1;
  176:     &Apache::lonxml::debug("wanted $wanted<br />");
  177:     if ($wanted >= $max) {
  178: 	# full up
  179: 	return -1;
  180:     }
  181:     
  182:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  183: 		     'timestamp' => time,
  184: 		     'symb'      => $symb);
  185: 
  186:     my $success=&Apache::lonnet::newput('slot_reservations',
  187: 					{"$slot_name\0$wanted" =>
  188: 					     \%reservation},
  189: 					$cdom, $cnum);
  190: 
  191:     if ($success eq 'ok') {
  192: 	#FIXME need to set the parm
  193: 	my $new_value=$slot_name;
  194: 	if ($value) {
  195: 	    $new_value=$value.':'.$new_value;
  196: 	}
  197: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  198: 						      '0_availablestudent',
  199: 						       1, $new_value, 'string',
  200: 						       $env{'user.name'},
  201: 					               $env{'user.domain'});
  202: 	&Apache::lonxml::debug("hrrm $result");
  203: 	return $wanted;
  204:     }
  205: 
  206:     # someone else got it
  207:     return undef;
  208: }
  209: 
  210: sub get_slot {
  211:     my ($r,$symb)=@_;
  212: 
  213:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  214:     my $reserved=&make_reservation($env{'form.slotname'},
  215: 				   \%slot,$symb);
  216:     my $description=&get_description($env{'form.slotname'},\%slot);
  217:     if ($reserved > -1) {
  218: 	$r->print("<p>Success: $description</p>");
  219: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
  220: 		  &mt('Return to last resource').'</a></p>');
  221: 	return;
  222:     } elsif ($reserved < 0) {
  223: 	$r->print("<p>Already reserved: $description</p>");
  224: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
  225: 		  &mt('Return to last resource').'</a></p>');
  226: 	return;
  227:     }
  228: 
  229:     my %lt=('request'=>"Request another attempt",
  230: 	    'try'    =>'Try again');
  231:     %lt=&Apache::lonlocal::texthash(%lt);
  232: 
  233:     $r->print(<<STUFF);
  234: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  235: <p>
  236: <form method="POST" action="/adm/slotrequest">
  237:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  238:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  239:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  240:    <input type="hidden" name="command" value="get" />
  241: </form>
  242: ?
  243: </p>
  244: <p>
  245: or
  246: <form method="POST" action="/adm/slotrequest">
  247:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  248:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  249: </form>
  250: </p>
  251: or
  252: STUFF
  253:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  254: 	      &mt('Return to last resource').'</a></p>');
  255:     return;
  256: }
  257: 
  258: sub allowed_slot {
  259:     my ($slot_name,$slot,$symb)=@_;
  260:     #already started
  261:     if ($slot->{'starttime'} < time) {
  262: 	return 0;
  263:     }
  264: 
  265:     #already ended
  266:     if ($slot->{'endtime'} < time) {
  267: 	return 0;
  268:     }
  269: 
  270:     # not allowed to pick this one
  271:     if (defined($slot->{'type'})
  272: 	&& $slot->{'type'} ne 'schedulable_student') {
  273: 	return 0;
  274:     }
  275: 
  276:     # not allowed for this resource
  277:     if (defined($slot->{'symb'})
  278: 	&& $slot->{'symb'} ne $symb) {
  279: 	return 0;
  280:     }
  281: 
  282:     return 1;
  283: }
  284: 
  285: sub get_description {
  286:     my ($slot_name,$slot)=@_;
  287:     my $description=$slot->{'description'};
  288:     if (!defined($description)) {
  289: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  290: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  291: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  292:     }
  293:     return $description;
  294: }
  295: 
  296: sub show_choices {
  297:     my ($r,$symb)=@_;
  298: 
  299:     my ($cnum,$cdom)=&get_course();
  300:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  301:     my $available;
  302:     $r->print('<table border="1">');
  303:     foreach my $slot (sort 
  304: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  305: 		      (keys(%slots)))  {
  306: 	
  307: 	next if (!&allowed_slot($slot,$slots{$slot}));
  308: 
  309: 	$available++;
  310: 
  311: 	my $description=&get_description($slot,$slots{$slot});
  312: 
  313: 	my $form=&mt('Unavailable');
  314: 	if (&space_available($slot,$slots{$slot},$symb)) {
  315: 	    my $escsymb=&Apache::lonnet::escape($symb);
  316: 	    $form=<<STUFF;
  317:    <form method="POST" action="/adm/slotrequest">
  318:      <input type="submit" name="Select" value="Select" />
  319:      <input type="hidden" name="symb" value="$escsymb" />
  320:      <input type="hidden" name="slotname" value="$slot" />
  321:      <input type="hidden" name="command" value="get" />
  322:    </form>
  323: STUFF
  324: 	}
  325: 	$r->print(<<STUFF);
  326: <tr>
  327:  <td>$form</td>
  328:  <td>$description</td>
  329: </tr>
  330: STUFF
  331:     }
  332: 
  333:     if (!$available) {
  334: 	$r->print('<tr><td>No avaliable times. <a href="/adm/flip?postdata=return:">'.
  335: 		  &mt('Return to last resource').'</a></td></tr>');
  336:     }
  337:     $r->print('</table>');
  338: }
  339: 
  340: sub handler {
  341:     my $r=shift;
  342: 
  343:     &start_page($r);
  344:     my $symb=&Apache::lonnet::unescape($env{'form.symb'});
  345:     my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
  346:     if ($res !~ /\.task$/) {
  347: 	&fail($r,'not_valid');
  348: 	return OK;
  349:     }
  350:     
  351:     if ($env{'form.requestattempt'}) {
  352: 	&show_choices($r,$symb);
  353:     } elsif ($env{'form.command'} eq 'get') {
  354: 	&get_slot($r,$symb);
  355:     }
  356:     &end_page($r);
  357:     return OK;
  358: }
  359: 
  360: 1;
  361: __END__

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