File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.2: download - view: text, annotated - select for diffs
Tue May 31 21:13:01 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- can display the slots now
- code for making a reservation written but untested

    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.2 2005/05/31 21:13:01 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 make_reservation {
  109:     my ($slot_name,$slot,$symb)=@_;
  110:     my $max=$slot->{'maxspace'};
  111: 
  112:     if (!defined($max)) { return 1; }
  113: 
  114:     my (@ids)=&get_reservation_ids($slot_name);
  115: 
  116:     # FIXME we could end up having holes... 
  117:     my $last=0;
  118:     foreach my $id (@ids) {
  119: 	my $num=(split('\0',$id))[1];
  120: 	if ($num > $last) { $last=$num; }
  121:     }
  122:     
  123:     my $wanted=$last+1;
  124:     if ($wanted >= $max) {
  125: 	# full up
  126: 	return -1;
  127:     }
  128:     
  129:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  130: 		     'timestamp' => time,
  131: 		     'symb'      => $symb);
  132: 
  133:     my ($cnum,$cdom)=&get_course();
  134:     my $success=&Apache::lonnet::newput('slot_reservations',
  135: 					{"$slot_name\0$wanted" =>
  136: 					     \%reservation},
  137: 					$cdom,$cnum);
  138:     if ($success eq 'ok') {
  139: 	return $wanted;
  140:     }
  141:     # someone else got it
  142:     return -1;
  143: }
  144: 
  145: 
  146: sub show_choices {
  147:     my ($r,$symb)=@_;
  148: 
  149:     my ($cnum,$cdom)=&get_course();
  150:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  151:     $r->print('<table border="1">');
  152:     foreach my $slot (sort 
  153: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  154: 		      (keys(%slots)))  {
  155: 	my $description=$slots{$slot}->{'description'};
  156: 	if (!defined($description)) {
  157: 	    $description=&mt('[_1] From [_2] to [_3]',$slot,
  158: 			     &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}),
  159: 			     &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}));
  160: 	}
  161: 
  162: 	my $form=&mt('Unavailable');
  163: 	if (&space_available($slot,$slots{$slot})) {
  164: 	    $form=<<STUFF;
  165:    <form>
  166:      <input type="submit" name="Select" value="Select" />
  167:      <!-- FIXME needs to send data -->
  168:    </form>
  169: STUFF
  170: 	}
  171: 	$r->print(<<STUFF);
  172: <tr>
  173:  <td>$form</td>
  174:  <td>$description</td>
  175: </tr>
  176: STUFF
  177:     }
  178:     $r->print('</table>');
  179: }
  180: 
  181: sub handler {
  182:     my $r=shift;
  183: 
  184:     &start_page($r);
  185:     my $symb=&Apache::lonnet::unescape($env{'form.symb'});
  186:     my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
  187:     if ($res !~ /\.task$/) {
  188: 	&fail($r,'not_valid');
  189: 	return OK;
  190:     }
  191:     
  192:     if ($env{'form.requestattempt'}) {
  193: 	&show_choices($r,$symb);
  194:     }
  195: 
  196:     &end_page($r);
  197:     return OK;
  198: }

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