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

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # Handler for requesting to have slots added to a students record
                      3: #
1.3     ! albertel    4: # $Id: slotrequest.pm,v 1.2 2005/05/31 21:13:01 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;
                     37: 
                     38: sub fail {
                     39:     my ($r,$code)=@_;
1.2       albertel   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>');
1.1       albertel   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: 
1.2       albertel   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: }
1.3     ! albertel  107: 
        !           108: # FIXME - depends on the parameter for the resource to be correct
        !           109: #         tho prevent multiple reservations
        !           110: 
1.2       albertel  111: sub make_reservation {
                    112:     my ($slot_name,$slot,$symb)=@_;
1.3     ! albertel  113: 
        !           114:     my ($cnum,$cdom)=&get_course();
        !           115: 
        !           116:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
        !           117: 				   $env{'user.domain'},$env{'user.name'});
        !           118:     &Apache::lonxml::debug("value is  $value<br />");
        !           119:     foreach my $other_slot (split(/:/, $value)) {
        !           120: 	if ($other_slot eq $slot_name) {
        !           121: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
        !           122: 					       $cnum, "^$slot_name\0");   
        !           123: 
        !           124: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
        !           125: 	    foreach my $key (keys(%consumed)) {
        !           126: 		if ($consumed{$key}->{'name'} eq $me) {
        !           127: 		    my $num=(split('\0',$key))[1];
        !           128: 		    return -$num;
        !           129: 		}
        !           130: 	    }
        !           131: 	}
        !           132:     }
        !           133: 
1.2       albertel  134:     my $max=$slot->{'maxspace'};
1.3     ! albertel  135:     if (!defined($max)) { $max=99999; }
1.2       albertel  136: 
                    137:     my (@ids)=&get_reservation_ids($slot_name);
                    138: 
                    139:     # FIXME we could end up having holes... 
                    140:     my $last=0;
                    141:     foreach my $id (@ids) {
                    142: 	my $num=(split('\0',$id))[1];
                    143: 	if ($num > $last) { $last=$num; }
                    144:     }
                    145:     
                    146:     my $wanted=$last+1;
1.3     ! albertel  147:     &Apache::lonxml::debug("wanted $wanted<br />");
1.2       albertel  148:     if ($wanted >= $max) {
                    149: 	# full up
                    150: 	return -1;
                    151:     }
                    152:     
                    153:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
                    154: 		     'timestamp' => time,
                    155: 		     'symb'      => $symb);
                    156: 
                    157:     my $success=&Apache::lonnet::newput('slot_reservations',
                    158: 					{"$slot_name\0$wanted" =>
                    159: 					     \%reservation},
1.3     ! albertel  160: 					$cdom, $cnum);
        !           161: 
1.2       albertel  162:     if ($success eq 'ok') {
1.3     ! albertel  163: 	#FIXME need to set the parm
        !           164: 	my $new_value=$slot_name;
        !           165: 	if ($value) {
        !           166: 	    $new_value=$value.':'.$new_value;
        !           167: 	}
        !           168: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
        !           169: 						      '0_availablestudent',
        !           170: 						       1, $new_value, 'string',
        !           171: 						       $env{'user.name'},
        !           172: 					               $env{'user.domain'});
        !           173: 	&Apache::lonxml::debug("hrrm $result");
1.2       albertel  174: 	return $wanted;
                    175:     }
1.3     ! albertel  176: 
1.2       albertel  177:     # someone else got it
1.3     ! albertel  178:     return undef;
        !           179: }
        !           180: 
        !           181: sub get_slot {
        !           182:     my ($r,$symb)=@_;
        !           183: 
        !           184:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
        !           185:     my $reserved=&make_reservation($env{'form.slotname'},
        !           186: 				   \%slot,$symb);
        !           187:     my $description=&get_description($env{'form.slotname'},\%slot);
        !           188:     if ($reserved > -1) {
        !           189: 	$r->print("<p>Success: $description</p>");
        !           190: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
        !           191: 		  &mt('Return to last resource').'</a></p>');
        !           192: 	return;
        !           193:     } elsif ($reserved < 0) {
        !           194: 	$r->print("<p>Already reserved: $description</p>");
        !           195: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
        !           196: 		  &mt('Return to last resource').'</a></p>');
        !           197: 	return;
        !           198:     }
        !           199: 
        !           200:     my %lt=('request'=>"Request another attempt",
        !           201: 	    'try'    =>'Try again');
        !           202:     %lt=&Apache::lonlocal::texthash(%lt);
        !           203: 
        !           204:     $r->print(<<STUFF);
        !           205: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
        !           206: <p>
        !           207: <form method="POST" action="/adm/slotrequest">
        !           208:    <input type="submit" name="Try Again" value="$lt{'try'}" />
        !           209:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
        !           210:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
        !           211:    <input type="hidden" name="command" value="get" />
        !           212: </form>
        !           213: ?
        !           214: </p>
        !           215: <p>
        !           216: or
        !           217: <form method="POST" action="/adm/slotrequest">
        !           218:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
        !           219:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
        !           220: </form>
        !           221: </p>
        !           222: or
        !           223: STUFF
        !           224:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
        !           225: 	      &mt('Return to last resource').'</a></p>');
        !           226:     return;
        !           227: }
        !           228: 
        !           229: sub allowed_slot {
        !           230:     my ($slot_name,$slot,$symb)=@_;
        !           231:     #already started
        !           232:     if ($slot->{'starttime'} < time) {
        !           233: 	return 0;
        !           234:     }
        !           235: 
        !           236:     #already ended
        !           237:     if ($slot->{'endtime'} < time) {
        !           238: 	return 0;
        !           239:     }
        !           240: 
        !           241:     # not allowed to pick this one
        !           242:     if (defined($slot->{'type'})
        !           243: 	&& $slot->{'type'} ne 'schedulable_student') {
        !           244: 	return 0;
        !           245:     }
        !           246: 
        !           247:     # not allowed for this resource
        !           248:     if (defined($slot->{'symb'})
        !           249: 	&& $slot->{'symb'} ne $symb) {
        !           250: 	return 0;
        !           251:     }
        !           252: 
        !           253:     return 1;
1.2       albertel  254: }
                    255: 
1.3     ! albertel  256: sub get_description {
        !           257:     my ($slot_name,$slot)=@_;
        !           258:     my $description=$slot->{'description'};
        !           259:     if (!defined($description)) {
        !           260: 	$description=&mt('[_1] From [_2] to [_3]',$slot,
        !           261: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
        !           262: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
        !           263:     }
        !           264:     return $description;
        !           265: }
1.2       albertel  266: 
                    267: sub show_choices {
                    268:     my ($r,$symb)=@_;
                    269: 
                    270:     my ($cnum,$cdom)=&get_course();
                    271:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.3     ! albertel  272:     my $available;
1.2       albertel  273:     $r->print('<table border="1">');
                    274:     foreach my $slot (sort 
                    275: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
                    276: 		      (keys(%slots)))  {
1.3     ! albertel  277: 	
        !           278: 	next if (!&allowed_slot($slot,$slots{$slot}));
        !           279: 
        !           280: 	$available++;
        !           281: 
        !           282: 	my $description=&get_description($slot,$slots{$slot});
1.2       albertel  283: 
                    284: 	my $form=&mt('Unavailable');
1.3     ! albertel  285: 	if (&space_available($slot,$slots{$slot},$symb)) {
        !           286: 	    my $escsymb=&Apache::lonnet::escape($symb);
1.2       albertel  287: 	    $form=<<STUFF;
1.3     ! albertel  288:    <form method="POST" action="/adm/slotrequest">
1.2       albertel  289:      <input type="submit" name="Select" value="Select" />
1.3     ! albertel  290:      <input type="hidden" name="symb" value="$escsymb" />
        !           291:      <input type="hidden" name="slotname" value="$slot" />
        !           292:      <input type="hidden" name="command" value="get" />
1.2       albertel  293:    </form>
                    294: STUFF
                    295: 	}
                    296: 	$r->print(<<STUFF);
                    297: <tr>
                    298:  <td>$form</td>
                    299:  <td>$description</td>
                    300: </tr>
                    301: STUFF
                    302:     }
1.3     ! albertel  303: 
        !           304:     if (!$available) {
        !           305: 	$r->print('<tr><td>No avaliable times. <a href="/adm/flip?postdata=return:">'.
        !           306: 		  &mt('Return to last resource').'</a></td></tr>');
        !           307:     }
1.2       albertel  308:     $r->print('</table>');
                    309: }
                    310: 
1.1       albertel  311: sub handler {
                    312:     my $r=shift;
                    313: 
                    314:     &start_page($r);
1.2       albertel  315:     my $symb=&Apache::lonnet::unescape($env{'form.symb'});
1.1       albertel  316:     my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.2       albertel  317:     if ($res !~ /\.task$/) {
1.1       albertel  318: 	&fail($r,'not_valid');
                    319: 	return OK;
                    320:     }
1.2       albertel  321:     
                    322:     if ($env{'form.requestattempt'}) {
                    323: 	&show_choices($r,$symb);
1.3     ! albertel  324:     } elsif ($env{'form.command'} eq 'get') {
        !           325: 	&get_slot($r,$symb);
1.2       albertel  326:     }
1.1       albertel  327:     &end_page($r);
                    328:     return OK;
                    329: }
1.3     ! albertel  330: 
        !           331: 1;
        !           332: __END__

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