Annotation of loncom/interface/slotrequest.pm, revision 1.7
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Handler for requesting to have slots added to a students record
3: #
1.7 ! albertel 4: # $Id: slotrequest.pm,v 1.6 2005/08/09 15:04:50 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:
1.4 albertel 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'});
1.7 ! albertel 128: if ($slots{$slot_name}->{'endtime'} > time &&
1.4 albertel 129: $slots{$slot_name}->{'startreserve'} < time) {
1.7 ! albertel 130: # between start of reservation times and end of slot
1.4 albertel 131: return($slot_name, $slots{$slot_name});
132: }
133: }
134: return (undef,undef);
135: }
136:
1.5 albertel 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:
1.2 albertel 161: sub make_reservation {
162: my ($slot_name,$slot,$symb)=@_;
1.3 albertel 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:
1.2 albertel 184: my $max=$slot->{'maxspace'};
1.3 albertel 185: if (!defined($max)) { $max=99999; }
1.2 albertel 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;
1.3 albertel 196: &Apache::lonxml::debug("wanted $wanted<br />");
1.7 ! albertel 197: if (scalar(@ids) >= $max) {
1.2 albertel 198: # full up
1.7 ! albertel 199: return undef;
1.2 albertel 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},
1.3 albertel 209: $cdom, $cnum);
210:
1.2 albertel 211: if ($success eq 'ok') {
1.3 albertel 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");
1.2 albertel 222: return $wanted;
223: }
1.3 albertel 224:
1.2 albertel 225: # someone else got it
1.3 albertel 226: return undef;
227: }
228:
1.5 albertel 229: sub release_slot {
1.6 albertel 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:
1.5 albertel 235: # get parameter string, check for existance, rebuild string with the slot
1.6 albertel 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);
1.5 albertel 246:
247: # get slot reservations, check if user has one, if so remove reservation
1.6 albertel 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: }
1.5 albertel 257: # store new parameter string
1.6 albertel 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>");
1.7 ! albertel 266: if (!$inhibit_return_link) {
1.6 albertel 267: $r->print('<p><a href="/adm/flip?postdata=return:">'.
268: &mt('Return to last resource').'</a></p>');
269: }
270: return 1;
1.5 albertel 271: }
272:
1.3 albertel 273: sub get_slot {
274: my ($r,$symb)=@_;
275:
1.5 albertel 276: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
277: if ($slot_name) {
278: my %slot=&Apache::lonnet::get_slot($slot_name);
1.6 albertel 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>");
1.7 ! albertel 283: if ($slot_name ne $env{'form.slotname'}) {
! 284: $r->print(<<STUFF);
1.6 albertel 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
1.7 ! albertel 291: $r->print("<p>You can either ");
! 292: $r->print(<<STUFF);
1.6 albertel 293: <input type="submit" name="change" value="Change" />
294: STUFF
1.7 ! albertel 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);
1.6 albertel 300: </form>
301: STUFF
1.7 ! albertel 302: } else {
! 303: $r->print('<p><a href="/adm/flip?postdata=return:">'.
! 304: &mt('Return to last resource').'</a></p>');
! 305: }
1.5 albertel 306: return;
307: }
1.3 albertel 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);
1.7 ! albertel 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: }
1.3 albertel 324: }
325:
1.7 ! albertel 326: my %lt=('request'=>"Availibility list",
1.3 albertel 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) {
1.5 albertel 359: # all open slot to be schedulable
360: #return 0;
1.3 albertel 361: }
1.5 albertel 362: &Apache::lonxml::debug("$slot_name starttime good");
1.3 albertel 363: #already ended
364: if ($slot->{'endtime'} < time) {
365: return 0;
366: }
1.5 albertel 367: &Apache::lonxml::debug("$slot_name endtime good");
1.3 albertel 368: # not allowed to pick this one
369: if (defined($slot->{'type'})
370: && $slot->{'type'} ne 'schedulable_student') {
371: return 0;
372: }
1.5 albertel 373: &Apache::lonxml::debug("$slot_name type good");
1.3 albertel 374: # not allowed for this resource
375: if (defined($slot->{'symb'})
376: && $slot->{'symb'} ne $symb) {
377: return 0;
378: }
1.5 albertel 379: &Apache::lonxml::debug("$slot_name symb good");
1.3 albertel 380: return 1;
1.2 albertel 381: }
382:
1.3 albertel 383: sub get_description {
384: my ($slot_name,$slot)=@_;
385: my $description=$slot->{'description'};
386: if (!defined($description)) {
1.4 albertel 387: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3 albertel 388: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
389: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
390: }
391: return $description;
392: }
1.2 albertel 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);
1.3 albertel 399: my $available;
1.2 albertel 400: $r->print('<table border="1">');
1.5 albertel 401: &Apache::lonxml::debug("Checking Slots");
402: my ($got_slot)=&check_for_reservation($symb);
1.2 albertel 403: foreach my $slot (sort
404: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
405: (keys(%slots))) {
1.5 albertel 406:
407: &Apache::lonxml::debug("Checking Slot $slot");
1.3 albertel 408: next if (!&allowed_slot($slot,$slots{$slot}));
409:
410: $available++;
411:
412: my $description=&get_description($slot,$slots{$slot});
1.2 albertel 413:
414: my $form=&mt('Unavailable');
1.7 ! albertel 415: if (($slot eq $got_slot) ||
! 416: &space_available($slot,$slots{$slot},$symb)) {
1.5 albertel 417: my $text=&mt('Select');
418: my $command='get';
419: if ($slot eq $got_slot) {
420: $text=&mt('Free Reservation');
421: $command='release';
422: }
1.3 albertel 423: my $escsymb=&Apache::lonnet::escape($symb);
1.2 albertel 424: $form=<<STUFF;
1.3 albertel 425: <form method="POST" action="/adm/slotrequest">
1.5 albertel 426: <input type="submit" name="Select" value="$text" />
1.3 albertel 427: <input type="hidden" name="symb" value="$escsymb" />
428: <input type="hidden" name="slotname" value="$slot" />
1.5 albertel 429: <input type="hidden" name="command" value="$command" />
1.2 albertel 430: </form>
431: STUFF
432: }
433: $r->print(<<STUFF);
434: <tr>
435: <td>$form</td>
436: <td>$description</td>
437: </tr>
438: STUFF
439: }
1.3 albertel 440:
441: if (!$available) {
1.5 albertel 442: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
1.3 albertel 443: &mt('Return to last resource').'</a></td></tr>');
444: }
1.2 albertel 445: $r->print('</table>');
446: }
447:
1.5 albertel 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:
1.1 albertel 486: sub handler {
487: my $r=shift;
488:
489: &start_page($r);
1.2 albertel 490: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
1.1 albertel 491: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.2 albertel 492: if ($res !~ /\.task$/) {
1.1 albertel 493: &fail($r,'not_valid');
494: return OK;
495: }
1.5 albertel 496:
497: if ($env{'form.command'} eq 'showslots') {
498: &show_table($r,$symb);
499: } elsif ($env{'form.requestattempt'}) {
1.2 albertel 500: &show_choices($r,$symb);
1.5 albertel 501: } elsif ($env{'form.command'} eq 'release') {
502: &release_slot($r,$symb);
1.3 albertel 503: } elsif ($env{'form.command'} eq 'get') {
504: &get_slot($r,$symb);
1.6 albertel 505: } elsif ($env{'form.command'} eq 'change') {
506: &release_slot($r,$symb,$env{'form.releaseslot'},1);
507: &get_slot($r,$symb);
1.2 albertel 508: }
1.1 albertel 509: &end_page($r);
510: return OK;
511: }
1.3 albertel 512:
513: 1;
514: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>