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.7 2005/08/09 15:38:13 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}->{'endtime'} > time &&
129: $slots{$slot_name}->{'startreserve'} < time) {
130: # between start of reservation times and end 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: sub make_reservation {
162: my ($slot_name,$slot,$symb)=@_;
163:
164: my ($cnum,$cdom)=&get_course();
165:
166: my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
167: $env{'user.domain'},$env{'user.name'});
168: &Apache::lonxml::debug("value is $value<br />");
169: foreach my $other_slot (split(/:/, $value)) {
170: if ($other_slot eq $slot_name) {
171: my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
172: $cnum, "^$slot_name\0");
173:
174: my $me=$env{'user.name'}.'@'.$env{'user.domain'};
175: foreach my $key (keys(%consumed)) {
176: if ($consumed{$key}->{'name'} eq $me) {
177: my $num=(split('\0',$key))[1];
178: return -$num;
179: }
180: }
181: }
182: }
183:
184: my $max=$slot->{'maxspace'};
185: if (!defined($max)) { $max=99999; }
186:
187: my (@ids)=&get_reservation_ids($slot_name);
188:
189: my $last=0;
190: foreach my $id (@ids) {
191: my $num=(split('\0',$id))[1];
192: if ($num > $last) { $last=$num; }
193: }
194:
195: my $wanted=$last+1;
196: &Apache::lonxml::debug("wanted $wanted<br />");
197: if (scalar(@ids) >= $max) {
198: # full up
199: return undef;
200: }
201:
202: my %reservation=('name' => $env{'user.name'}.'@'.$env{'user.domain'},
203: 'timestamp' => time,
204: 'symb' => $symb);
205:
206: my $success=&Apache::lonnet::newput('slot_reservations',
207: {"$slot_name\0$wanted" =>
208: \%reservation},
209: $cdom, $cnum);
210:
211: if ($success eq 'ok') {
212: my $new_value=$slot_name;
213: if ($value) {
214: $new_value=$value.':'.$new_value;
215: }
216: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
217: '0_availablestudent',
218: 1, $new_value, 'string',
219: $env{'user.name'},
220: $env{'user.domain'});
221: &Apache::lonxml::debug("hrrm $result");
222: return $wanted;
223: }
224:
225: # someone else got it
226: return undef;
227: }
228:
229: sub release_slot {
230: my ($r,$symb,$slot_name,$inhibit_return_link)=@_;
231:
232: if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
233: my ($cnum,$cdom)=&get_course();
234:
235: # get parameter string, check for existance, rebuild string with the slot
236:
237: my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
238: $symb,$env{'user.domain'},
239: $env{'user.name'}));
240: my @new_slots;
241: foreach my $exist_slot (@slots) {
242: if ($exist_slot eq $slot_name) { next; }
243: push(@new_slots,$exist_slot);
244: }
245: my $new_param = join(':',@new_slots);
246:
247: # get slot reservations, check if user has one, if so remove reservation
248: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
249: "^$slot_name\0");
250: foreach my $entry (keys(%consumed)) {
251: if ( $consumed{$entry}->{'name'} eq
252: ($env{'user.name'}.'@'.$env{'user.domain'}) ) {
253: &Apache::lonnet::del('slot_reservations',[$entry],
254: $cdom,$cnum);
255: }
256: }
257: # store new parameter string
258: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
259: '0_availablestudent',
260: 1, $new_param, 'string',
261: $env{'user.name'},
262: $env{'user.domain'});
263: my %slot=&Apache::lonnet::get_slot($slot_name);
264: my $description=&get_description($env{'form.slotname'},\%slot);
265: $r->print("<p>Released Reservation: $description</p>");
266: if (!$inhibit_return_link) {
267: $r->print('<p><a href="/adm/flip?postdata=return:">'.
268: &mt('Return to last resource').'</a></p>');
269: }
270: return 1;
271: }
272:
273: sub get_slot {
274: my ($r,$symb)=@_;
275:
276: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
277: if ($slot_name) {
278: my %slot=&Apache::lonnet::get_slot($slot_name);
279: my $description1=&get_description($slot_name,\%slot);
280: %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
281: my $description2=&get_description($env{'form.slotname'},\%slot);
282: $r->print("<p>Already have a reservation: $description1</p>");
283: if ($slot_name ne $env{'form.slotname'}) {
284: $r->print(<<STUFF);
285: <form method="POST" action="/adm/slotrequest">
286: <input type="hidden" name="symb" value="$env{'form.symb'}" />
287: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
288: <input type="hidden" name="releaseslot" value="$slot_name" />
289: <input type="hidden" name="command" value="change" />
290: STUFF
291: $r->print("<p>You can either ");
292: $r->print(<<STUFF);
293: <input type="submit" name="change" value="Change" />
294: STUFF
295: $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
296: $description2.
297: '</b> <br />or <a href="/adm/flip?postdata=return:">'.
298: &mt('Return to last resource').'</a></p>');
299: $r->print(<<STUFF);
300: </form>
301: STUFF
302: } else {
303: $r->print('<p><a href="/adm/flip?postdata=return:">'.
304: &mt('Return to last resource').'</a></p>');
305: }
306: return;
307: }
308: my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
309: my $reserved=&make_reservation($env{'form.slotname'},
310: \%slot,$symb);
311: my $description=&get_description($env{'form.slotname'},\%slot);
312: if (defined($reserved)) {
313: if ($reserved > -1) {
314: $r->print("<p>Success: $description</p>");
315: $r->print('<p><a href="/adm/flip?postdata=return:">'.
316: &mt('Return to last resource').'</a></p>');
317: return;
318: } elsif ($reserved < 0) {
319: $r->print("<p>Already reserved: $description</p>");
320: $r->print('<p><a href="/adm/flip?postdata=return:">'.
321: &mt('Return to last resource').'</a></p>');
322: return;
323: }
324: }
325:
326: my %lt=('request'=>"Availibility list",
327: 'try' =>'Try again');
328: %lt=&Apache::lonlocal::texthash(%lt);
329:
330: $r->print(<<STUFF);
331: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
332: <p>
333: <form method="POST" action="/adm/slotrequest">
334: <input type="submit" name="Try Again" value="$lt{'try'}" />
335: <input type="hidden" name="symb" value="$env{'form.symb'}" />
336: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
337: <input type="hidden" name="command" value="get" />
338: </form>
339: ?
340: </p>
341: <p>
342: or
343: <form method="POST" action="/adm/slotrequest">
344: <input type="hidden" name="symb" value="$env{'form.symb'}" />
345: <input type="submit" name="requestattempt" value="$lt{'request'}" />
346: </form>
347: </p>
348: or
349: STUFF
350: $r->print('<p><a href="/adm/flip?postdata=return:">'.
351: &mt('Return to last resource').'</a></p>');
352: return;
353: }
354:
355: sub allowed_slot {
356: my ($slot_name,$slot,$symb)=@_;
357: #already started
358: if ($slot->{'starttime'} < time) {
359: # all open slot to be schedulable
360: #return 0;
361: }
362: &Apache::lonxml::debug("$slot_name starttime good");
363: #already ended
364: if ($slot->{'endtime'} < time) {
365: return 0;
366: }
367: &Apache::lonxml::debug("$slot_name endtime good");
368: # not allowed to pick this one
369: if (defined($slot->{'type'})
370: && $slot->{'type'} ne 'schedulable_student') {
371: return 0;
372: }
373: &Apache::lonxml::debug("$slot_name type good");
374: # not allowed for this resource
375: if (defined($slot->{'symb'})
376: && $slot->{'symb'} ne $symb) {
377: return 0;
378: }
379: &Apache::lonxml::debug("$slot_name symb good");
380: return 1;
381: }
382:
383: sub get_description {
384: my ($slot_name,$slot)=@_;
385: my $description=$slot->{'description'};
386: if (!defined($description)) {
387: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
388: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
389: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
390: }
391: return $description;
392: }
393:
394: sub show_choices {
395: my ($r,$symb)=@_;
396:
397: my ($cnum,$cdom)=&get_course();
398: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
399: my $available;
400: $r->print('<table border="1">');
401: &Apache::lonxml::debug("Checking Slots");
402: my ($got_slot)=&check_for_reservation($symb);
403: foreach my $slot (sort
404: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
405: (keys(%slots))) {
406:
407: &Apache::lonxml::debug("Checking Slot $slot");
408: next if (!&allowed_slot($slot,$slots{$slot}));
409:
410: $available++;
411:
412: my $description=&get_description($slot,$slots{$slot});
413:
414: my $form=&mt('Unavailable');
415: if (($slot eq $got_slot) ||
416: &space_available($slot,$slots{$slot},$symb)) {
417: my $text=&mt('Select');
418: my $command='get';
419: if ($slot eq $got_slot) {
420: $text=&mt('Free Reservation');
421: $command='release';
422: }
423: my $escsymb=&Apache::lonnet::escape($symb);
424: $form=<<STUFF;
425: <form method="POST" action="/adm/slotrequest">
426: <input type="submit" name="Select" value="$text" />
427: <input type="hidden" name="symb" value="$escsymb" />
428: <input type="hidden" name="slotname" value="$slot" />
429: <input type="hidden" name="command" value="$command" />
430: </form>
431: STUFF
432: }
433: $r->print(<<STUFF);
434: <tr>
435: <td>$form</td>
436: <td>$description</td>
437: </tr>
438: STUFF
439: }
440:
441: if (!$available) {
442: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
443: &mt('Return to last resource').'</a></td></tr>');
444: }
445: $r->print('</table>');
446: }
447:
448: sub show_table {
449: my ($r,$symb)=@_;
450:
451: my ($cnum,$cdom)=&get_course();
452: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
453: my $available;
454: $r->print('<table border="1">');
455: foreach my $slot (sort
456: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
457: (keys(%slots))) {
458: if (defined($slots{$slot}->{'type'})
459: && $slots{$slot}->{'type'} ne 'schedulable_student') {
460: next;
461: }
462: my $description=&get_description($slot,$slots{$slot});
463: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
464: "^$slot\0");
465: my $ids;
466: foreach my $entry (sort(keys(%consumed))) {
467: my (undef,$id)=split("\0",$entry);
468: $ids.= $id.'-> '.$consumed{$entry}->{'name'}.'<br />';
469: }
470: my $start=localtime($slots{$slot}->{'starttime'});
471: my $end=localtime($slots{$slot}->{'endtime'});
472: $r->print(<<STUFF);
473: <tr>
474: <td>$slot</td>
475: <td>$description</td>
476: <td>$start</td>
477: <td>$end</td>
478: <td>$slots{$slot}->{'maxspace'}</td>
479: <td>$ids</td>
480: </tr>
481: STUFF
482: }
483: $r->print('</table>');
484: }
485:
486: sub handler {
487: my $r=shift;
488:
489: &start_page($r);
490: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
491: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
492: if ($res !~ /\.task$/) {
493: &fail($r,'not_valid');
494: return OK;
495: }
496:
497: if ($env{'form.command'} eq 'showslots') {
498: &show_table($r,$symb);
499: } elsif ($env{'form.requestattempt'}) {
500: &show_choices($r,$symb);
501: } elsif ($env{'form.command'} eq 'release') {
502: &release_slot($r,$symb);
503: } elsif ($env{'form.command'} eq 'get') {
504: &get_slot($r,$symb);
505: } elsif ($env{'form.command'} eq 'change') {
506: &release_slot($r,$symb,$env{'form.releaseslot'},1);
507: &get_slot($r,$symb);
508: }
509: &end_page($r);
510: return OK;
511: }
512:
513: 1;
514: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>