File:  [LON-CAPA] / loncom / interface / slotrequest.pm
Revision 1.10: download - view: text, annotated - select for diffs
Tue Sep 6 20:53:36 2005 UTC (18 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- add moe info to the slot screen

    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.10 2005/09/06 20:53:36 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>');
   42: 
   43:     } elsif ($code eq 'not_allowed') {
   44: 	$r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
   45:     } else {
   46: 	$r->print('<p>'.&mt('Failed.').'</p>');
   47:     }
   48:     
   49:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
   50: 	      &mt('Return to last resource').'</a></p>');
   51:     &end_page($r);
   52: }
   53: 
   54: sub start_page {
   55:     my ($r)=@_;
   56:     my $html=&Apache::lonxml::xmlbegin();
   57:     $r->print($html.'<head><title>'.
   58: 	      &mt('Request another Worktime').'</title></head>');
   59:     $r->print(&Apache::loncommon::bodytag('Requesting another Worktime'));
   60: }
   61: 
   62: sub end_page {
   63:     my ($r)=@_;
   64:     $r->print(&Apache::loncommon::endbodytag().'</html>');
   65: }
   66: 
   67: =pod
   68: 
   69:  slot_reservations db
   70:    - keys are 
   71:     - slotname\0id -> value is an hashref of
   72:                          name -> user@domain of holder
   73:                          timestamp -> timestamp of reservation
   74:                          symb -> symb of resource that it is reserved for
   75: 
   76: =cut
   77: 
   78: sub get_course {
   79:     (undef,my $courseid)=&Apache::lonxml::whichuser();
   80:     my $cdom=$env{'course.'.$courseid.'.domain'};
   81:     my $cnum=$env{'course.'.$courseid.'.num'};
   82:     return ($cnum,$cdom);
   83: }
   84: 
   85: sub get_reservation_ids {
   86:     my ($slot_name)=@_;
   87:     
   88:     my ($cnum,$cdom)=&get_course();
   89: 
   90:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
   91: 				       "^$slot_name\0");
   92:     
   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: }
  112: 
  113: sub check_for_reservation {
  114:     my ($symb)=@_;
  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: 
  127:     foreach my $slot_name (@slots) {
  128: 	next if (!defined($slots{$slot_name}) ||
  129: 		 !ref($slots{$slot_name}));
  130: 	&Apache::lonxml::debug(time." $slot_name ".
  131: 			       $slots{$slot_name}->{'starttime'}." -- ".
  132: 			       $slots{$slot_name}->{'startreserve'});
  133: 	if ($slots{$slot_name}->{'endtime'} > time &&
  134: 	    $slots{$slot_name}->{'startreserve'} < time) {
  135: 	    # between start of reservation times and end of slot
  136: 	    return($slot_name, $slots{$slot_name});
  137: 	}
  138:     }
  139:     return (undef,undef);
  140: }
  141: 
  142: sub check_for_conflict {
  143:     my ($symb,$new_slot_name)=@_;
  144:     my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
  145: 				       $env{'user.domain'}, $env{'user.name'});
  146:     my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
  147: 				      $env{'user.domain'}, $env{'user.name'});
  148:     my @slots = (split(/:/,$student), split(/:/, $course));
  149:     my ($cnum,$cdom)=&get_course();
  150:     my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
  151:     foreach my $slot_name (@slots) {
  152: 	next if (!defined($slots{$slot_name}) ||
  153: 		 !ref($slots{$slot_name}));
  154: 
  155:         next if (!defined($slots{$slot_name}->{'uniqueperiod'}) ||
  156: 		 !ref($slots{$slot_name}->{'uniqueperiod'}));
  157: 	my ($start,$end)=@{$slots{$slot_name}->{'uniqueperiod'}};
  158: 	if ($start<time && time < $end) {
  159: 	    return $slot_name;
  160: 	}
  161:     }
  162:     return undef;
  163: 
  164: }
  165: 
  166: sub make_reservation {
  167:     my ($slot_name,$slot,$symb)=@_;
  168: 
  169:     my ($cnum,$cdom)=&get_course();
  170: 
  171:     my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
  172: 				   $env{'user.domain'},$env{'user.name'});
  173:     &Apache::lonxml::debug("value is  $value<br />");
  174:     foreach my $other_slot (split(/:/, $value)) {
  175: 	if ($other_slot eq $slot_name) {
  176: 	    my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
  177: 					       $cnum, "^$slot_name\0");   
  178: 
  179: 	    my $me=$env{'user.name'}.'@'.$env{'user.domain'};
  180: 	    foreach my $key (keys(%consumed)) {
  181: 		if ($consumed{$key}->{'name'} eq $me) {
  182: 		    my $num=(split('\0',$key))[1];
  183: 		    return -$num;
  184: 		}
  185: 	    }
  186: 	}
  187:     }
  188: 
  189:     my $max=$slot->{'maxspace'};
  190:     if (!defined($max)) { $max=99999; }
  191: 
  192:     my (@ids)=&get_reservation_ids($slot_name);
  193: 
  194:     my $last=0;
  195:     foreach my $id (@ids) {
  196: 	my $num=(split('\0',$id))[1];
  197: 	if ($num > $last) { $last=$num; }
  198:     }
  199:     
  200:     my $wanted=$last+1;
  201:     &Apache::lonxml::debug("wanted $wanted<br />");
  202:     if (scalar(@ids) >= $max) {
  203: 	# full up
  204: 	return undef;
  205:     }
  206:     
  207:     my %reservation=('name'      => $env{'user.name'}.'@'.$env{'user.domain'},
  208: 		     'timestamp' => time,
  209: 		     'symb'      => $symb);
  210: 
  211:     my $success=&Apache::lonnet::newput('slot_reservations',
  212: 					{"$slot_name\0$wanted" =>
  213: 					     \%reservation},
  214: 					$cdom, $cnum);
  215: 
  216:     if ($success eq 'ok') {
  217: 	my $new_value=$slot_name;
  218: 	if ($value) {
  219: 	    $new_value=$value.':'.$new_value;
  220: 	}
  221: 	my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  222: 						      '0_availablestudent',
  223: 						       1, $new_value, 'string',
  224: 						       $env{'user.name'},
  225: 					               $env{'user.domain'});
  226: 	&Apache::lonxml::debug("hrrm $result");
  227: 	return $wanted;
  228:     }
  229: 
  230:     # someone else got it
  231:     return undef;
  232: }
  233: 
  234: sub release_slot {
  235:     my ($r,$symb,$slot_name,$inhibit_return_link)=@_;
  236: 
  237:     if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
  238:     my ($cnum,$cdom)=&get_course();
  239: 
  240:     # get parameter string, check for existance, rebuild string with the slot
  241: 				       
  242:     my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
  243: 					       $symb,$env{'user.domain'},
  244: 					       $env{'user.name'}));
  245:     my @new_slots;
  246:     foreach my $exist_slot (@slots) {
  247: 	if ($exist_slot eq $slot_name) { next; }
  248: 	push(@new_slots,$exist_slot);
  249:     }
  250:     my $new_param = join(':',@new_slots);
  251: 
  252:     # get slot reservations, check if user has one, if so remove reservation
  253:     my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  254: 				       "^$slot_name\0");
  255:     foreach my $entry (keys(%consumed)) {
  256: 	if ( $consumed{$entry}->{'name'} eq 
  257: 	     ($env{'user.name'}.'@'.$env{'user.domain'}) ) {
  258: 	    &Apache::lonnet::del('slot_reservations',[$entry],
  259: 				 $cdom,$cnum);
  260: 	}
  261:     }
  262:     # store new parameter string
  263:     my $result=&Apache::lonparmset::storeparm_by_symb($symb,
  264: 						      '0_availablestudent',
  265: 						      1, $new_param, 'string',
  266: 						      $env{'user.name'},
  267: 						      $env{'user.domain'});
  268:     my %slot=&Apache::lonnet::get_slot($slot_name);
  269:     my $description=&get_description($env{'form.slotname'},\%slot);
  270:     $r->print("<p>Released Reservation: $description</p>");
  271:     if (!$inhibit_return_link) {
  272: 	$r->print('<p><a href="/adm/flip?postdata=return:">'.
  273: 		  &mt('Return to last resource').'</a></p>');
  274:     }
  275:     return 1;
  276: }
  277: 
  278: sub get_slot {
  279:     my ($r,$symb)=@_;
  280: 
  281:     my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
  282:     if ($slot_name) {
  283: 	my %slot=&Apache::lonnet::get_slot($slot_name);
  284: 	my $description1=&get_description($slot_name,\%slot);
  285: 	%slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  286: 	my $description2=&get_description($env{'form.slotname'},\%slot);
  287: 	$r->print("<p>Already have a reservation: $description1</p>");
  288: 	if ($slot_name ne $env{'form.slotname'}) {
  289: 	    $r->print(<<STUFF);
  290: <form method="POST" action="/adm/slotrequest">
  291:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  292:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  293:    <input type="hidden" name="releaseslot" value="$slot_name" />
  294:    <input type="hidden" name="command" value="change" />
  295: STUFF
  296:             $r->print("<p>You can either ");
  297: 	    $r->print(<<STUFF);
  298:    <input type="submit" name="change" value="Change" />
  299: STUFF
  300: 	    $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
  301: 		      $description2.
  302: 		      '</b> <br />or <a href="/adm/flip?postdata=return:">'.
  303: 		      &mt('Return to last resource').'</a></p>');
  304: 	    $r->print(<<STUFF);
  305: </form>
  306: STUFF
  307:         } else {
  308: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  309: 		      &mt('Return to last resource').'</a></p>');
  310: 	}
  311: 	return;
  312:     }
  313:     my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
  314:     my $reserved=&make_reservation($env{'form.slotname'},
  315: 				   \%slot,$symb);
  316:     my $description=&get_description($env{'form.slotname'},\%slot);
  317:     if (defined($reserved)) {
  318: 	if ($reserved > -1) {
  319: 	    $r->print("<p>Success: $description</p>");
  320: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  321: 		      &mt('Return to last resource').'</a></p>');
  322: 	    return;
  323: 	} elsif ($reserved < 0) {
  324: 	    $r->print("<p>Already reserved: $description</p>");
  325: 	    $r->print('<p><a href="/adm/flip?postdata=return:">'.
  326: 		      &mt('Return to last resource').'</a></p>');
  327: 	    return;
  328: 	}
  329:     }
  330: 
  331:     my %lt=('request'=>"Availibility list",
  332: 	    'try'    =>'Try again');
  333:     %lt=&Apache::lonlocal::texthash(%lt);
  334: 
  335:     $r->print(<<STUFF);
  336: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
  337: <p>
  338: <form method="POST" action="/adm/slotrequest">
  339:    <input type="submit" name="Try Again" value="$lt{'try'}" />
  340:    <input type="hidden" name="symb" value="$env{'form.symb'}" />
  341:    <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
  342:    <input type="hidden" name="command" value="get" />
  343: </form>
  344: ?
  345: </p>
  346: <p>
  347: or
  348: <form method="POST" action="/adm/slotrequest">
  349:     <input type="hidden" name="symb" value="$env{'form.symb'}" />
  350:     <input type="submit" name="requestattempt" value="$lt{'request'}" />
  351: </form>
  352: </p>
  353: or
  354: STUFF
  355:     $r->print('<p><a href="/adm/flip?postdata=return:">'.
  356: 	      &mt('Return to last resource').'</a></p>');
  357:     return;
  358: }
  359: 
  360: sub allowed_slot {
  361:     my ($slot_name,$slot,$symb)=@_;
  362:     #already started
  363:     if ($slot->{'starttime'} < time) {
  364: 	# all open slot to be schedulable
  365: 	#return 0;
  366:     }
  367:     &Apache::lonxml::debug("$slot_name starttime good");
  368:     #already ended
  369:     if ($slot->{'endtime'} < time) {
  370: 	return 0;
  371:     }
  372:     &Apache::lonxml::debug("$slot_name endtime good");
  373:     # not allowed to pick this one
  374:     if (defined($slot->{'type'})
  375: 	&& $slot->{'type'} ne 'schedulable_student') {
  376: 	return 0;
  377:     }
  378:     &Apache::lonxml::debug("$slot_name type good");
  379:     # not allowed for this resource
  380:     if (defined($slot->{'symb'})
  381: 	&& $slot->{'symb'} ne $symb) {
  382: 	return 0;
  383:     }
  384:     &Apache::lonxml::debug("$slot_name symb good");
  385:     return 1;
  386: }
  387: 
  388: sub get_description {
  389:     my ($slot_name,$slot)=@_;
  390:     my $description=$slot->{'description'};
  391:     if (!defined($description)) {
  392: 	$description=&mt('[_1] From [_2] to [_3]',$slot_name,
  393: 			 &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
  394: 			 &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
  395:     }
  396:     return $description;
  397: }
  398: 
  399: sub show_choices {
  400:     my ($r,$symb)=@_;
  401: 
  402:     my ($cnum,$cdom)=&get_course();
  403:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  404:     my $available;
  405:     $r->print('<table border="1">');
  406:     &Apache::lonxml::debug("Checking Slots");
  407:     my ($got_slot)=&check_for_reservation($symb);
  408:     foreach my $slot (sort 
  409: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  410: 		      (keys(%slots)))  {
  411: 
  412: 	&Apache::lonxml::debug("Checking Slot $slot");
  413: 	next if (!&allowed_slot($slot,$slots{$slot}));
  414: 
  415: 	$available++;
  416: 
  417: 	my $description=&get_description($slot,$slots{$slot});
  418: 
  419: 	my $form=&mt('Unavailable');
  420: 	if (($slot eq $got_slot) ||
  421: 	    &space_available($slot,$slots{$slot},$symb)) {
  422: 	    my $text=&mt('Select');
  423: 	    my $command='get';
  424: 	    if ($slot eq $got_slot) {
  425: 		$text=&mt('Free Reservation');
  426: 		$command='release';
  427: 	    }
  428: 	    my $escsymb=&Apache::lonnet::escape($symb);
  429: 	    $form=<<STUFF;
  430:    <form method="POST" action="/adm/slotrequest">
  431:      <input type="submit" name="Select" value="$text" />
  432:      <input type="hidden" name="symb" value="$escsymb" />
  433:      <input type="hidden" name="slotname" value="$slot" />
  434:      <input type="hidden" name="command" value="$command" />
  435:    </form>
  436: STUFF
  437: 	}
  438: 	$r->print(<<STUFF);
  439: <tr>
  440:  <td>$form</td>
  441:  <td>$description</td>
  442: </tr>
  443: STUFF
  444:     }
  445: 
  446:     if (!$available) {
  447: 	$r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
  448: 		  &mt('Return to last resource').'</a></td></tr>');
  449:     }
  450:     $r->print('</table>');
  451: }
  452: 
  453: sub show_table {
  454:     my ($r,$symb)=@_;
  455: 
  456:     my ($cnum,$cdom)=&get_course();
  457:     my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
  458:     my $available;
  459:     $r->print('<table border="1">
  460: <tr>
  461:   <th>Slot name</th>
  462:   <th>Type</th>
  463:   <th>Description</th>
  464:   <th>Start Time</th>
  465:   <th>End Time</th>
  466:   <th>Max space</th>
  467:   <th>Scheduled Students</th>
  468:   <th>Proctors</th>
  469:   <th>Unique Period</th>
  470: </tr>');
  471:     foreach my $slot (sort 
  472: 		      { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
  473: 		      (keys(%slots)))  {
  474: 	if (defined($slots{$slot}->{'type'})
  475: 	    && $slots{$slot}->{'type'} ne 'schedulable_student') {
  476: 	    next;
  477: 	}
  478: 	my $description=&get_description($slot,$slots{$slot});
  479: 	my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
  480: 					   "^$slot\0");
  481: 	my $ids;
  482: 	foreach my $entry (sort(keys(%consumed))) {
  483: 	    my (undef,$id)=split("\0",$entry);
  484: 	    $ids.= $id.'-> '.$consumed{$entry}->{'name'}.'<br />';
  485: 	}
  486: 	my $start=localtime($slots{$slot}->{'starttime'});
  487: 	my $end=localtime($slots{$slot}->{'endtime'});
  488: 	$r->print(<<STUFF);
  489: <tr>
  490:  <td>$slot</td>
  491:  <td>$slots{$slot}->{'type'}</td>
  492:  <td>$description</td>
  493:  <td>$start</td>
  494:  <td>$end</td>
  495:  <td>$slots{$slot}->{'maxspace'}</td>
  496:  <td>$ids</td>
  497:  <td>$slots{$slot}->{'proctor'}</td>
  498:  <td>$slots{$slot}->{'uniqueperiod'}</td>
  499: </tr>
  500: STUFF
  501:     }
  502:     $r->print('</table>');
  503: }
  504: 
  505: sub handler {
  506:     my $r=shift;
  507: 
  508:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
  509:     &start_page($r);
  510:     my $symb=&Apache::lonnet::unescape($env{'form.symb'});
  511:     my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
  512:     if ($res !~ /\.task$/) {
  513: 	&fail($r,'not_valid');
  514: 	return OK;
  515:     }
  516:     $env{'request.symb'}=$symb;
  517:     my ($status) = &Apache::lonhomework::check_task_access('0');
  518:     if ($status eq 'CAN_ANSWER' ||
  519: 	$status eq 'NEEDS_CHECKIN' ||
  520: 	$status eq 'WAITING_FOR_GRADE') {
  521: 	&fail($r,'not_allowed');
  522: 	return OK;
  523:     }
  524:     my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
  525:     if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
  526: 	&show_table($r,$symb);
  527:     } elsif ($env{'form.requestattempt'}) {
  528: 	&show_choices($r,$symb);
  529:     } elsif ($env{'form.command'} eq 'release') {
  530: 	&release_slot($r,$symb);
  531:     } elsif ($env{'form.command'} eq 'get') {
  532: 	&get_slot($r,$symb);
  533:     } elsif ($env{'form.command'} eq 'change') {
  534:        	&release_slot($r,$symb,$env{'form.releaseslot'},1);
  535: 	&get_slot($r,$symb);
  536:     } else {
  537: 	$r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
  538:     }
  539:     &end_page($r);
  540:     return OK;
  541: }
  542: 
  543: 1;
  544: __END__

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