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.5 2005/08/09 07:34:51 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: sub check_for_conflict {
138: my ($symb,$new_slot_name)=@_;
139: my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
140: $env{'user.domain'}, $env{'user.name'});
141: my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
142: $env{'user.domain'}, $env{'user.name'});
143: my @slots = (split(/:/,$student), split(/:/, $course));
144: my ($cnum,$cdom)=&get_course();
145: my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
146: foreach my $slot_name (@slots) {
147: next if (!defined($slots{$slot_name}) ||
148: !ref($slots{$slot_name}));
149:
150: next if (!defined($slots{$slot_name}->{'uniqueperiod'}) ||
151: !ref($slots{$slot_name}->{'uniqueperiod'}));
152: my ($start,$end)=@{$slots{$slot_name}->{'uniqueperiod'}};
153: if ($start<time && time < $end) {
154: return $slot_name;
155: }
156: }
157: return undef;
158:
159: }
160:
161: # FIXME - depends on the parameter for the resource to be correct
162: # to prevent multiple reservations
163:
164: sub make_reservation {
165: my ($slot_name,$slot,$symb)=@_;
166:
167: my ($cnum,$cdom)=&get_course();
168:
169: my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
170: $env{'user.domain'},$env{'user.name'});
171: &Apache::lonxml::debug("value is $value<br />");
172: foreach my $other_slot (split(/:/, $value)) {
173: if ($other_slot eq $slot_name) {
174: my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
175: $cnum, "^$slot_name\0");
176:
177: my $me=$env{'user.name'}.'@'.$env{'user.domain'};
178: foreach my $key (keys(%consumed)) {
179: if ($consumed{$key}->{'name'} eq $me) {
180: my $num=(split('\0',$key))[1];
181: return -$num;
182: }
183: }
184: }
185: }
186:
187: my $max=$slot->{'maxspace'};
188: if (!defined($max)) { $max=99999; }
189:
190: my (@ids)=&get_reservation_ids($slot_name);
191:
192: # FIXME we could end up having holes...
193: my $last=0;
194: foreach my $id (@ids) {
195: my $num=(split('\0',$id))[1];
196: if ($num > $last) { $last=$num; }
197: }
198:
199: my $wanted=$last+1;
200: &Apache::lonxml::debug("wanted $wanted<br />");
201: if ($wanted >= $max) {
202: # full up
203: return -1;
204: }
205:
206: my %reservation=('name' => $env{'user.name'}.'@'.$env{'user.domain'},
207: 'timestamp' => time,
208: 'symb' => $symb);
209:
210: my $success=&Apache::lonnet::newput('slot_reservations',
211: {"$slot_name\0$wanted" =>
212: \%reservation},
213: $cdom, $cnum);
214:
215: if ($success eq 'ok') {
216: #FIXME need to set the parm
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)=@_;
236: # get parameter string, check for existance, rebuild string with the slot
237:
238: # get slot reservations, check if user has one, if so remove reservation
239:
240: # store new parameter string
241: }
242:
243: sub get_slot {
244: my ($r,$symb)=@_;
245:
246: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
247: if ($slot_name) {
248: my %slot=&Apache::lonnet::get_slot($slot_name);
249: my $description=&get_description($env{'form.slotname'},\%slot);
250: $r->print("<p>Already have a reservation: $description</p>");
251: $r->print('<p><a href="/adm/flip?postdata=return:">'.
252: &mt('Return to last resource').'</a></p>');
253: # FIXME add button to free current reservation adn get new one
254: return;
255: }
256: my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
257: my $reserved=&make_reservation($env{'form.slotname'},
258: \%slot,$symb);
259: my $description=&get_description($env{'form.slotname'},\%slot);
260: if ($reserved > -1) {
261: $r->print("<p>Success: $description</p>");
262: $r->print('<p><a href="/adm/flip?postdata=return:">'.
263: &mt('Return to last resource').'</a></p>');
264: return;
265: } elsif ($reserved < 0) {
266: $r->print("<p>Already reserved: $description</p>");
267: $r->print('<p><a href="/adm/flip?postdata=return:">'.
268: &mt('Return to last resource').'</a></p>');
269: return;
270: }
271:
272: my %lt=('request'=>"Request another attempt",
273: 'try' =>'Try again');
274: %lt=&Apache::lonlocal::texthash(%lt);
275:
276: $r->print(<<STUFF);
277: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
278: <p>
279: <form method="POST" action="/adm/slotrequest">
280: <input type="submit" name="Try Again" value="$lt{'try'}" />
281: <input type="hidden" name="symb" value="$env{'form.symb'}" />
282: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
283: <input type="hidden" name="command" value="get" />
284: </form>
285: ?
286: </p>
287: <p>
288: or
289: <form method="POST" action="/adm/slotrequest">
290: <input type="hidden" name="symb" value="$env{'form.symb'}" />
291: <input type="submit" name="requestattempt" value="$lt{'request'}" />
292: </form>
293: </p>
294: or
295: STUFF
296: $r->print('<p><a href="/adm/flip?postdata=return:">'.
297: &mt('Return to last resource').'</a></p>');
298: return;
299: }
300:
301: sub allowed_slot {
302: my ($slot_name,$slot,$symb)=@_;
303: #already started
304: if ($slot->{'starttime'} < time) {
305: # all open slot to be schedulable
306: #return 0;
307: }
308: &Apache::lonxml::debug("$slot_name starttime good");
309: #already ended
310: if ($slot->{'endtime'} < time) {
311: return 0;
312: }
313: &Apache::lonxml::debug("$slot_name endtime good");
314: # not allowed to pick this one
315: if (defined($slot->{'type'})
316: && $slot->{'type'} ne 'schedulable_student') {
317: return 0;
318: }
319: &Apache::lonxml::debug("$slot_name type good");
320: # not allowed for this resource
321: if (defined($slot->{'symb'})
322: && $slot->{'symb'} ne $symb) {
323: return 0;
324: }
325: &Apache::lonxml::debug("$slot_name symb good");
326: return 1;
327: }
328:
329: sub get_description {
330: my ($slot_name,$slot)=@_;
331: my $description=$slot->{'description'};
332: if (!defined($description)) {
333: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
334: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
335: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
336: }
337: return $description;
338: }
339:
340: sub show_choices {
341: my ($r,$symb)=@_;
342:
343: my ($cnum,$cdom)=&get_course();
344: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
345: my $available;
346: $r->print('<table border="1">');
347: &Apache::lonxml::debug("Checking Slots");
348: my ($got_slot)=&check_for_reservation($symb);
349: foreach my $slot (sort
350: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
351: (keys(%slots))) {
352:
353: &Apache::lonxml::debug("Checking Slot $slot");
354: next if (!&allowed_slot($slot,$slots{$slot}));
355:
356: $available++;
357:
358: my $description=&get_description($slot,$slots{$slot});
359:
360: my $form=&mt('Unavailable');
361: if (&space_available($slot,$slots{$slot},$symb)) {
362: my $text=&mt('Select');
363: my $command='get';
364: if ($slot eq $got_slot) {
365: $text=&mt('Free Reservation');
366: $command='release';
367: }
368: my $escsymb=&Apache::lonnet::escape($symb);
369: $form=<<STUFF;
370: <form method="POST" action="/adm/slotrequest">
371: <input type="submit" name="Select" value="$text" />
372: <input type="hidden" name="symb" value="$escsymb" />
373: <input type="hidden" name="slotname" value="$slot" />
374: <input type="hidden" name="command" value="$command" />
375: </form>
376: STUFF
377: }
378: $r->print(<<STUFF);
379: <tr>
380: <td>$form</td>
381: <td>$description</td>
382: </tr>
383: STUFF
384: }
385:
386: if (!$available) {
387: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
388: &mt('Return to last resource').'</a></td></tr>');
389: }
390: $r->print('</table>');
391: }
392:
393: sub show_table {
394: my ($r,$symb)=@_;
395:
396: my ($cnum,$cdom)=&get_course();
397: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
398: my $available;
399: $r->print('<table border="1">');
400: foreach my $slot (sort
401: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
402: (keys(%slots))) {
403: if (defined($slots{$slot}->{'type'})
404: && $slots{$slot}->{'type'} ne 'schedulable_student') {
405: next;
406: }
407: my $description=&get_description($slot,$slots{$slot});
408: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
409: "^$slot\0");
410: my $ids;
411: foreach my $entry (sort(keys(%consumed))) {
412: my (undef,$id)=split("\0",$entry);
413: $ids.= $id.'-> '.$consumed{$entry}->{'name'}.'<br />';
414: }
415: my $start=localtime($slots{$slot}->{'starttime'});
416: my $end=localtime($slots{$slot}->{'endtime'});
417: $r->print(<<STUFF);
418: <tr>
419: <td>$slot</td>
420: <td>$description</td>
421: <td>$start</td>
422: <td>$end</td>
423: <td>$slots{$slot}->{'maxspace'}</td>
424: <td>$ids</td>
425: </tr>
426: STUFF
427: }
428: $r->print('</table>');
429: }
430:
431: sub handler {
432: my $r=shift;
433:
434: &start_page($r);
435: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
436: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
437: if ($res !~ /\.task$/) {
438: &fail($r,'not_valid');
439: return OK;
440: }
441:
442: if ($env{'form.command'} eq 'showslots') {
443: &show_table($r,$symb);
444: } elsif ($env{'form.requestattempt'}) {
445: &show_choices($r,$symb);
446: } elsif ($env{'form.command'} eq 'release') {
447: &release_slot($r,$symb);
448: } elsif ($env{'form.command'} eq 'get') {
449: &get_slot($r,$symb);
450: }
451: &end_page($r);
452: return OK;
453: }
454:
455: 1;
456: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>