Annotation of loncom/interface/slotrequest.pm, revision 1.61
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Handler for requesting to have slots added to a students record
3: #
1.61 ! albertel 4: # $Id: slotrequest.pm,v 1.60 2006/05/12 01:20:31 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;
1.48 albertel 37: use Apache::lonnavmaps();
1.27 albertel 38: use Date::Manip;
1.1 albertel 39:
40: sub fail {
41: my ($r,$code)=@_;
1.2 albertel 42: if ($code eq 'not_valid') {
1.8 albertel 43: $r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
1.61 ! albertel 44: } elsif ($code eq 'not_available') {
! 45: $r->print('<p>'.&mt('No slots are available.').'</p>');
1.8 albertel 46: } elsif ($code eq 'not_allowed') {
47: $r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
48: } else {
49: $r->print('<p>'.&mt('Failed.').'</p>');
1.2 albertel 50: }
1.8 albertel 51:
1.42 albertel 52: &return_link($r);
1.1 albertel 53: &end_page($r);
54: }
55:
56: sub start_page {
1.28 albertel 57: my ($r,$title)=@_;
1.52 albertel 58: $r->print(&Apache::loncommon::start_page($title));
1.1 albertel 59: }
60:
61: sub end_page {
62: my ($r)=@_;
1.52 albertel 63: $r->print(&Apache::loncommon::end_page());
1.1 albertel 64: }
65:
1.2 albertel 66: =pod
67:
68: slot_reservations db
69: - keys are
70: - slotname\0id -> value is an hashref of
71: name -> user@domain of holder
72: timestamp -> timestamp of reservation
73: symb -> symb of resource that it is reserved for
74:
75: =cut
76:
77: sub get_course {
78: (undef,my $courseid)=&Apache::lonxml::whichuser();
79: my $cdom=$env{'course.'.$courseid.'.domain'};
80: my $cnum=$env{'course.'.$courseid.'.num'};
81: return ($cnum,$cdom);
82: }
83:
84: sub get_reservation_ids {
85: my ($slot_name)=@_;
86:
87: my ($cnum,$cdom)=&get_course();
88:
89: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
90: "^$slot_name\0");
1.40 albertel 91: if (&network_error(%consumed)) {
92: return 'error: Unable to determine current status';
93: }
1.2 albertel 94: my ($tmp)=%consumed;
95: if ($tmp=~/^error: 2 / ) {
96: return 0;
97: }
98: return keys(%consumed);
99: }
100:
101: sub space_available {
102: my ($slot_name,$slot)=@_;
103: my $max=$slot->{'maxspace'};
104:
105: if (!defined($max)) { return 1; }
106:
107: my $consumed=scalar(&get_reservation_ids($slot_name));
108: if ($consumed < $max) {
109: return 1
110: }
111: return 0;
112: }
1.3 albertel 113:
1.4 albertel 114: sub check_for_reservation {
1.43 albertel 115: my ($symb,$mode)=@_;
1.4 albertel 116: my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
117: $env{'user.domain'}, $env{'user.name'});
118:
119: my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
120: $env{'user.domain'}, $env{'user.name'});
121: my @slots = (split(/:/,$student), split(/:/, $course));
122:
123: &Apache::lonxml::debug(" slot list is ".join(':',@slots));
124:
125: my ($cnum,$cdom)=&get_course();
126: my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
127:
1.41 albertel 128: if (&network_error($student) || &network_error($course) ||
129: &network_error(%slots)) {
130: return 'error: Unable to determine current status';
131: }
1.43 albertel 132: my @got;
133: foreach my $slot_name (sort {
134: if (ref($slots{$a}) && ref($slots{$b})) {
135: return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
136: }
137: if (ref($slots{$a})) { return -1;}
138: if (ref($slots{$b})) { return 1;}
139: return 0;
140: } @slots) {
1.4 albertel 141: next if (!defined($slots{$slot_name}) ||
142: !ref($slots{$slot_name}));
143: &Apache::lonxml::debug(time." $slot_name ".
144: $slots{$slot_name}->{'starttime'}." -- ".
145: $slots{$slot_name}->{'startreserve'});
1.7 albertel 146: if ($slots{$slot_name}->{'endtime'} > time &&
1.4 albertel 147: $slots{$slot_name}->{'startreserve'} < time) {
1.7 albertel 148: # between start of reservation times and end of slot
1.43 albertel 149: if ($mode eq 'allslots') {
150: push(@got,$slot_name);
151: } else {
152: return($slot_name, $slots{$slot_name});
153: }
1.4 albertel 154: }
155: }
1.43 albertel 156: if ($mode eq 'allslots' && @got) {
157: return @got;
158: }
1.4 albertel 159: return (undef,undef);
160: }
161:
1.48 albertel 162: sub get_consumed_uniqueperiods {
163: my ($slots) = @_;
164: my $navmap=Apache::lonnavmaps::navmap->new;
165: my @problems = $navmap->retrieveResources(undef,
166: sub { $_[0]->is_problem() },1,0);
167: my %used_slots;
168: foreach my $problem (@problems) {
169: my $symb = $problem->symb();
170: my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
171: $symb, $env{'user.domain'},
172: $env{'user.name'});
173: my $course = &Apache::lonnet::EXT("resource.0.available",
174: $symb, $env{'user.domain'},
175: $env{'user.name'});
176: if (&network_error($student) || &network_error($course)) {
177: return 'error: Unable to determine current status';
178: }
179: foreach my $slot (split(/:/,$student), split(/:/, $course)) {
180: $used_slots{$slot}=1;
181: }
182: }
1.43 albertel 183:
184: if (!ref($slots)) {
1.48 albertel 185: my ($cnum,$cdom)=&get_course();
186: my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
187: if (&network_error(%slots)) {
188: return 'error: Unable to determine current status';
189: }
1.43 albertel 190: $slots = \%slots;
191: }
1.41 albertel 192:
1.48 albertel 193: my %consumed_uniqueperiods;
194: foreach my $slot_name (keys(%used_slots)) {
1.43 albertel 195: next if (!defined($slots->{$slot_name}) ||
196: !ref($slots->{$slot_name}));
1.48 albertel 197:
1.43 albertel 198: next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
199: !ref($slots->{$slot_name}{'uniqueperiod'}));
1.48 albertel 200: $consumed_uniqueperiods{$slot_name} =
201: $slots->{$slot_name}{'uniqueperiod'};
202: }
203: return \%consumed_uniqueperiods;
204: }
205:
206: sub check_for_conflict {
207: my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
208:
209: if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
210:
211: if (!ref($consumed_uniqueperiods)) {
212: $consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
213: if (&network_error(%$consumed_uniqueperiods)) {
214: return 'error: Unable to determine current status';
215: }
216: }
217:
218: my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
219: foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
220: my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
1.43 albertel 221: if (!
222: ($start < $new_uniq_start && $end < $new_uniq_start) ||
223: ($start > $new_uniq_end && $end > $new_uniq_end )) {
1.5 albertel 224: return $slot_name;
225: }
226: }
227: return undef;
228:
229: }
230:
1.40 albertel 231: sub network_error {
232: my ($result) = @_;
233: if ($result =~ /^(con_lost|no_such_host|error: [^2])/) {
234: return 1;
235: }
236: return 0;
237: }
238:
1.2 albertel 239: sub make_reservation {
240: my ($slot_name,$slot,$symb)=@_;
1.3 albertel 241:
242: my ($cnum,$cdom)=&get_course();
243:
244: my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
245: $env{'user.domain'},$env{'user.name'});
246: &Apache::lonxml::debug("value is $value<br />");
1.59 albertel 247:
248: my $use_slots = &Apache::lonnet::EXT("resource.0.useslots");
249: &Apache::lonxml::debug("use_slots is $use_slots<br />");
250:
251: if (&network_error($value) || &network_error($use_slots)) {
1.40 albertel 252: return 'error: Unable to determine current status';
253: }
254:
1.59 albertel 255: my $parm_symb = $symb;
256: my $parm_level = 1;
1.60 albertel 257: if ($use_slots eq 'map') {
1.59 albertel 258: my ($map) = &Apache::lonnet::decode_symb($symb);
259: $parm_symb = &Apache::lonnet::symbread($map);
260: $parm_level = 2;
261: }
262:
1.3 albertel 263: foreach my $other_slot (split(/:/, $value)) {
264: if ($other_slot eq $slot_name) {
265: my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
266: $cnum, "^$slot_name\0");
1.40 albertel 267: if (&network_error($value)) {
268: return 'error: Unable to determine current status';
269: }
1.57 albertel 270: my $me=$env{'user.name'}.':'.$env{'user.domain'};
1.3 albertel 271: foreach my $key (keys(%consumed)) {
272: if ($consumed{$key}->{'name'} eq $me) {
273: my $num=(split('\0',$key))[1];
274: return -$num;
275: }
276: }
277: }
278: }
279:
1.2 albertel 280: my $max=$slot->{'maxspace'};
1.3 albertel 281: if (!defined($max)) { $max=99999; }
1.2 albertel 282:
283: my (@ids)=&get_reservation_ids($slot_name);
1.40 albertel 284: if (&network_error(@ids)) {
285: return 'error: Unable to determine current status';
286: }
1.2 albertel 287: my $last=0;
288: foreach my $id (@ids) {
289: my $num=(split('\0',$id))[1];
290: if ($num > $last) { $last=$num; }
291: }
292:
293: my $wanted=$last+1;
1.3 albertel 294: &Apache::lonxml::debug("wanted $wanted<br />");
1.7 albertel 295: if (scalar(@ids) >= $max) {
1.2 albertel 296: # full up
1.7 albertel 297: return undef;
1.2 albertel 298: }
299:
1.57 albertel 300: my %reservation=('name' => $env{'user.name'}.':'.$env{'user.domain'},
1.2 albertel 301: 'timestamp' => time,
1.59 albertel 302: 'symb' => $parm_symb);
1.2 albertel 303:
304: my $success=&Apache::lonnet::newput('slot_reservations',
305: {"$slot_name\0$wanted" =>
306: \%reservation},
1.3 albertel 307: $cdom, $cnum);
308:
1.2 albertel 309: if ($success eq 'ok') {
1.3 albertel 310: my $new_value=$slot_name;
311: if ($value) {
312: $new_value=$value.':'.$new_value;
313: }
314: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
315: '0_availablestudent',
1.59 albertel 316: $parm_level, $new_value,
317: 'string',
1.3 albertel 318: $env{'user.name'},
319: $env{'user.domain'});
320: &Apache::lonxml::debug("hrrm $result");
1.2 albertel 321: return $wanted;
322: }
1.3 albertel 323:
1.2 albertel 324: # someone else got it
1.3 albertel 325: return undef;
326: }
327:
1.33 albertel 328: sub remove_registration {
329: my ($r) = @_;
1.55 albertel 330: if ($env{'form.entry'} ne 'remove all') {
331: return &remove_registration_user($r);
332: }
333: my $slot_name = $env{'form.slotname'};
334: my %slot=&Apache::lonnet::get_slot($slot_name);
335:
336: my ($cnum,$cdom)=&get_course();
337: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
338: "^$slot_name\0");
339: if (&network_error(%consumed)) {
340: $r->print("<p>".&mt('A network error has occured.').'</p>');
341: return;
342: }
343: if (!%consumed) {
344: $r->print("<p>".&mt('Slot <tt>[_1]</tt> has no reservations.',
345: $slot_name)."</p>");
346: return;
347: }
348:
349: my @names = map { $consumed{$_}{'name'} } (sort(keys(%consumed)));
350: my $names = join(' ',@names);
351:
352: my $msg = &mt('Remove all of [_1] from slot [_2]?',$names,$slot_name);
353: &remove_registration_confirmation($r,$msg,['entry','slotname']);
354: }
355:
356: sub remove_registration_user {
357: my ($r) = @_;
358:
359: my $slot_name = $env{'form.slotname'};
360:
1.33 albertel 361: my $name = &Apache::loncommon::plainname($env{'form.uname'},
362: $env{'form.udom'});
363:
364: my $title = &Apache::lonnet::gettitle($env{'form.symb'});
365:
1.55 albertel 366: my $msg = &mt('Remove [_1] from slot [_2] for [_3]',
367: $name,$slot_name,$title);
368:
369: &remove_registration_confirmation($r,$msg,['uname','udom','slotname',
370: 'entry','symb']);
371: }
372:
373: sub remove_registration_confirmation {
374: my ($r,$msg,$inputs) =@_;
375:
1.33 albertel 376: my $hidden_input;
1.55 albertel 377: foreach my $parm (@{$inputs}) {
1.33 albertel 378: $hidden_input .=
379: '<input type="hidden" name="'.$parm.'" value="'
380: .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
381: }
1.55 albertel 382: my %lt = &Apache::lonlocal::texthash('yes' => 'Yes',
383: 'no' => 'No',);
1.33 albertel 384: $r->print(<<"END_CONFIRM");
1.55 albertel 385: <p> $msg </p>
1.33 albertel 386: <form action="/adm/slotrequest" method="POST">
387: <input type="hidden" name="command" value="release" />
1.55 albertel 388: <input type="hidden" name="button" value="yes" />
1.33 albertel 389: $hidden_input
1.55 albertel 390: <input type="submit" value="$lt{'yes'}" />
1.33 albertel 391: </form>
392: <form action="/adm/slotrequest" method="POST">
393: <input type="hidden" name="command" value="showslots" />
1.55 albertel 394: <input type="submit" value="$lt{'no'}" />
1.33 albertel 395: </form>
396: END_CONFIRM
397:
398: }
399:
1.55 albertel 400: sub release_all_slot {
401: my ($r,$mgr)=@_;
402:
403: my $slot_name = $env{'form.slotname'};
404:
405: my ($cnum,$cdom)=&get_course();
406:
407: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
408: "^$slot_name\0");
409:
410: $r->print('<p>'.&mt('Releasing reservations').'</p>');
411:
412: foreach my $entry (sort { $consumed{$a}{'name'} cmp
413: $consumed{$b}{'name'} } (keys(%consumed))) {
1.57 albertel 414: my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.55 albertel 415: my ($result,$msg) =
416: &release_reservation($slot_name,$uname,$udom,
417: $consumed{$entry}{'symb'},$mgr);
418: $r->print("<p>$msg</p>");
419: $r->rflush();
420: }
421: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
422: &mt('Return to slot list').'</a></p>');
423: &return_link($r);
424: }
425:
1.5 albertel 426: sub release_slot {
1.33 albertel 427: my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
1.6 albertel 428:
429: if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
430:
1.33 albertel 431: my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
432: if ($mgr eq 'F'
433: && defined($env{'form.uname'}) && defined($env{'form.udom'})) {
434: ($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
435: }
436:
437: if ($mgr eq 'F'
438: && defined($env{'form.symb'})) {
439: $symb = $env{'form.symb'};
440: }
1.55 albertel 441:
442: my ($result,$msg) =
443: &release_reservation($slot_name,$uname,$udom,$symb,$mgr);
444: $r->print("<p>$msg</p>");
445:
446: if ($mgr eq 'F') {
447: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
448: &mt('Return to slot list').'</a></p>');
449: }
450:
451: if (!$inhibit_return_link) { &return_link($r); }
452: return $result;
453: }
454:
455: sub release_reservation {
456: my ($slot_name,$uname,$udom,$symb,$mgr) = @_;
1.39 albertel 457: my %slot=&Apache::lonnet::get_slot($slot_name);
1.55 albertel 458: my $description=&get_description($slot_name,\%slot);
1.33 albertel 459:
1.39 albertel 460: if ($mgr ne 'F') {
1.43 albertel 461: if ($slot{'starttime'} < time) {
1.55 albertel 462: return (0,&mt('Not allowed to release Reservation: [_1], as it has already ended.',$description));
1.39 albertel 463: }
464: }
1.5 albertel 465: # get parameter string, check for existance, rebuild string with the slot
1.6 albertel 466: my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
1.33 albertel 467: $symb,$udom,$uname));
468:
1.6 albertel 469: my @new_slots;
470: foreach my $exist_slot (@slots) {
471: if ($exist_slot eq $slot_name) { next; }
472: push(@new_slots,$exist_slot);
473: }
474: my $new_param = join(':',@new_slots);
1.5 albertel 475:
1.55 albertel 476: my ($cnum,$cdom)=&get_course();
477:
1.5 albertel 478: # get slot reservations, check if user has one, if so remove reservation
1.6 albertel 479: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
480: "^$slot_name\0");
481: foreach my $entry (keys(%consumed)) {
1.57 albertel 482: if ( $consumed{$entry}->{'name'} eq ($uname.':'.$udom) ) {
1.6 albertel 483: &Apache::lonnet::del('slot_reservations',[$entry],
484: $cdom,$cnum);
485: }
486: }
1.33 albertel 487:
1.59 albertel 488: my $use_slots = &Apache::lonnet::EXT("resource.0.useslots");
489: &Apache::lonxml::debug("use_slots is $use_slots<br />");
490:
491: if (&network_error($use_slots)) {
492: return (0,'error: Unable to determine current status');
493: }
494:
495: my $parm_level = 1;
1.60 albertel 496: if ($use_slots eq 'map') {
1.59 albertel 497: $parm_level = 2;
498: }
1.5 albertel 499: # store new parameter string
1.6 albertel 500: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
501: '0_availablestudent',
1.59 albertel 502: $parm_level, $new_param,
503: 'string', $uname, $udom);
1.55 albertel 504:
505: my $msg;
1.33 albertel 506: if ($mgr eq 'F') {
1.55 albertel 507: $msg = &mt('Released Reservation for user: [_1]',"$uname:$udom");
508: } else {
509: $msg = &mt('Released Reservation: [_1]',$description);
1.33 albertel 510: }
1.55 albertel 511: return (1,$msg);
1.5 albertel 512: }
513:
1.34 albertel 514: sub delete_slot {
515: my ($r)=@_;
516:
517: my $slot_name = $env{'form.slotname'};
518: my %slot=&Apache::lonnet::get_slot($slot_name);
519:
520: my ($cnum,$cdom)=&get_course();
521: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
522: "^$slot_name\0");
1.38 albertel 523: my ($tmp) = %consumed;
524: if ($tmp =~ /error: 2/) { undef(%consumed); }
1.34 albertel 525:
526: if (%slot && !%consumed) {
527: $slot{'type'} = 'deleted';
528: my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
529: $cdom, $cnum);
530: if ($ret eq 'ok') {
531: $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
532: } else {
533: $r->print("<p> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</p>");
534: }
535: } else {
536: if (%consumed) {
537: $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
538: } else {
539: $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
540: }
541: }
542: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
543: &mt('Return to slot list').'</a></p>');
1.42 albertel 544: &return_link($r);
1.34 albertel 545: }
546:
1.40 albertel 547: sub return_link {
548: my ($r) = @_;
549: $r->print('<p><a href="/adm/flip?postdata=return:">'.
550: &mt('Return to last resource').'</a></p>');
551: }
552:
1.3 albertel 553: sub get_slot {
554: my ($r,$symb)=@_;
555:
1.43 albertel 556: my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
557: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
1.40 albertel 558:
559: if ($slot_name =~ /^error: (.*)/) {
560: $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
561: &return_link($r);
562: return;
563: }
1.5 albertel 564: if ($slot_name) {
565: my %slot=&Apache::lonnet::get_slot($slot_name);
1.6 albertel 566: my $description1=&get_description($slot_name,\%slot);
567: %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
568: my $description2=&get_description($env{'form.slotname'},\%slot);
569: $r->print("<p>Already have a reservation: $description1</p>");
1.7 albertel 570: if ($slot_name ne $env{'form.slotname'}) {
571: $r->print(<<STUFF);
1.6 albertel 572: <form method="POST" action="/adm/slotrequest">
573: <input type="hidden" name="symb" value="$env{'form.symb'}" />
574: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
575: <input type="hidden" name="releaseslot" value="$slot_name" />
576: <input type="hidden" name="command" value="change" />
577: STUFF
1.7 albertel 578: $r->print("<p>You can either ");
579: $r->print(<<STUFF);
1.6 albertel 580: <input type="submit" name="change" value="Change" />
581: STUFF
1.7 albertel 582: $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
583: $description2.
1.40 albertel 584: '</b> <br />or </p>');
585: &return_link($r);
1.7 albertel 586: $r->print(<<STUFF);
1.6 albertel 587: </form>
588: STUFF
1.7 albertel 589: } else {
1.40 albertel 590: &return_link($r);
1.7 albertel 591: }
1.5 albertel 592: return;
593: }
1.45 albertel 594:
1.3 albertel 595: my $reserved=&make_reservation($env{'form.slotname'},
596: \%slot,$symb);
597: my $description=&get_description($env{'form.slotname'},\%slot);
1.7 albertel 598: if (defined($reserved)) {
1.40 albertel 599: if ($slot_name =~ /^error: (.*)/) {
600: $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
601: } elsif ($reserved > -1) {
1.7 albertel 602: $r->print("<p>Success: $description</p>");
603: } elsif ($reserved < 0) {
604: $r->print("<p>Already reserved: $description</p>");
605: }
1.40 albertel 606: &return_link($r);
607: return;
1.3 albertel 608: }
609:
1.7 albertel 610: my %lt=('request'=>"Availibility list",
1.3 albertel 611: 'try' =>'Try again');
612: %lt=&Apache::lonlocal::texthash(%lt);
613:
614: $r->print(<<STUFF);
615: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
616: <p>
617: <form method="POST" action="/adm/slotrequest">
618: <input type="submit" name="Try Again" value="$lt{'try'}" />
619: <input type="hidden" name="symb" value="$env{'form.symb'}" />
620: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
621: <input type="hidden" name="command" value="get" />
622: </form>
623: ?
624: </p>
625: <p>
626: or
627: <form method="POST" action="/adm/slotrequest">
628: <input type="hidden" name="symb" value="$env{'form.symb'}" />
629: <input type="submit" name="requestattempt" value="$lt{'request'}" />
630: </form>
631: </p>
632: or
633: STUFF
1.42 albertel 634:
635: &return_link($r);
1.3 albertel 636: return;
637: }
638:
639: sub allowed_slot {
1.48 albertel 640: my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
1.49 albertel 641:
1.3 albertel 642: #already started
643: if ($slot->{'starttime'} < time) {
1.5 albertel 644: # all open slot to be schedulable
645: #return 0;
1.3 albertel 646: }
1.5 albertel 647: &Apache::lonxml::debug("$slot_name starttime good");
1.49 albertel 648:
1.3 albertel 649: #already ended
650: if ($slot->{'endtime'} < time) {
651: return 0;
652: }
1.5 albertel 653: &Apache::lonxml::debug("$slot_name endtime good");
1.49 albertel 654:
1.3 albertel 655: # not allowed to pick this one
656: if (defined($slot->{'type'})
657: && $slot->{'type'} ne 'schedulable_student') {
658: return 0;
659: }
1.5 albertel 660: &Apache::lonxml::debug("$slot_name type good");
1.49 albertel 661:
1.53 albertel 662: # reserve time not yet started
663: if ($slot->{'startreserve'} > time) {
664: return 0;
665: }
666: &Apache::lonxml::debug("$slot_name reserve good");
667:
1.50 albertel 668: my $userallowed=0;
1.49 albertel 669: # its for a different set of users
1.50 albertel 670: if (defined($slot->{'allowedsections'})) {
671: if (!defined($env{'request.role.sec'})
672: && grep(/^No section assigned$/,
673: split(',',$slot->{'allowedsections'}))) {
674: $userallowed=1;
675: }
676: if (defined($env{'request.role.sec'})
677: && grep(/^\Q$env{'request.role.sec'}\E$/,
678: split(',',$slot->{'allowedsections'}))) {
679: $userallowed=1;
680: }
1.49 albertel 681: }
1.50 albertel 682: &Apache::lonxml::debug("$slot_name sections is $userallowed");
1.49 albertel 683:
684: # its for a different set of users
1.50 albertel 685: if (defined($slot->{'allowedusers'})
686: && grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
687: split(',',$slot->{'allowedusers'}))) {
688: $userallowed=1;
1.49 albertel 689: }
1.51 albertel 690:
691: if (!defined($slot->{'allowedusers'})
692: && !defined($slot->{'allowedsections'})) {
693: $userallowed=1;
694: }
695:
1.50 albertel 696: &Apache::lonxml::debug("$slot_name user is $userallowed");
697: return 0 if (!$userallowed);
1.49 albertel 698:
1.3 albertel 699: # not allowed for this resource
700: if (defined($slot->{'symb'})
701: && $slot->{'symb'} ne $symb) {
702: return 0;
703: }
1.50 albertel 704:
1.48 albertel 705: my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
706: $consumed_uniqueperiods);
1.44 albertel 707: if ($conflict) {
708: if ($slots->{$conflict}{'starttime'} < time) {
709: return 0;
710: }
711: }
1.5 albertel 712: &Apache::lonxml::debug("$slot_name symb good");
1.3 albertel 713: return 1;
1.2 albertel 714: }
715:
1.3 albertel 716: sub get_description {
717: my ($slot_name,$slot)=@_;
718: my $description=$slot->{'description'};
719: if (!defined($description)) {
1.4 albertel 720: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3 albertel 721: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
722: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
723: }
724: return $description;
725: }
1.2 albertel 726:
727: sub show_choices {
728: my ($r,$symb)=@_;
729:
730: my ($cnum,$cdom)=&get_course();
731: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.48 albertel 732: my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
1.3 albertel 733: my $available;
1.2 albertel 734: $r->print('<table border="1">');
1.5 albertel 735: &Apache::lonxml::debug("Checking Slots");
1.43 albertel 736: my @got_slots=&check_for_reservation($symb,'allslots');
1.2 albertel 737: foreach my $slot (sort
738: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
739: (keys(%slots))) {
1.5 albertel 740:
741: &Apache::lonxml::debug("Checking Slot $slot");
1.48 albertel 742: next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
743: $consumed_uniqueperiods));
1.3 albertel 744:
745: $available++;
746:
747: my $description=&get_description($slot,$slots{$slot});
1.2 albertel 748:
749: my $form=&mt('Unavailable');
1.43 albertel 750: if ((grep(/^\Q$slot\E$/,@got_slots)) ||
1.7 albertel 751: &space_available($slot,$slots{$slot},$symb)) {
1.5 albertel 752: my $text=&mt('Select');
753: my $command='get';
1.43 albertel 754: if (grep(/^\Q$slot\E$/,@got_slots)) {
1.5 albertel 755: $text=&mt('Free Reservation');
756: $command='release';
1.43 albertel 757: } else {
758: my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
1.48 albertel 759: \%slots,
760: $consumed_uniqueperiods);
1.43 albertel 761: if ($conflict) {
762: $text=&mt('Change Reservation');
763: $command='get';
764: }
1.5 albertel 765: }
1.3 albertel 766: my $escsymb=&Apache::lonnet::escape($symb);
1.2 albertel 767: $form=<<STUFF;
1.3 albertel 768: <form method="POST" action="/adm/slotrequest">
1.5 albertel 769: <input type="submit" name="Select" value="$text" />
1.3 albertel 770: <input type="hidden" name="symb" value="$escsymb" />
771: <input type="hidden" name="slotname" value="$slot" />
1.5 albertel 772: <input type="hidden" name="command" value="$command" />
1.2 albertel 773: </form>
774: STUFF
775: }
776: $r->print(<<STUFF);
777: <tr>
778: <td>$form</td>
779: <td>$description</td>
780: </tr>
781: STUFF
782: }
1.3 albertel 783:
784: if (!$available) {
1.5 albertel 785: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
1.3 albertel 786: &mt('Return to last resource').'</a></td></tr>');
787: }
1.2 albertel 788: $r->print('</table>');
789: }
790:
1.30 albertel 791: sub to_show {
1.54 albertel 792: my ($slotname,$slot,$when,$deleted,$name) = @_;
1.30 albertel 793: my $time=time;
794: my $week=60*60*24*7;
1.54 albertel 795:
1.35 albertel 796: if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
797: return 0;
798: }
1.54 albertel 799:
800: if ($name && $name->{'value'} =~ /\w/) {
801: if ($name->{'type'} eq 'substring') {
802: if ($slotname !~ /\Q$name->{'value'}\E/) {
803: return 0;
804: }
805: }
806: if ($name->{'type'} eq 'exact') {
807: if ($slotname eq $name->{'value'}) {
808: return 0;
809: }
810: }
811: }
812:
1.35 albertel 813: if ($when eq 'any') {
814: return 1;
815: } elsif ($when eq 'now') {
1.30 albertel 816: if ($time > $slot->{'starttime'} &&
817: $time < $slot->{'endtime'}) {
818: return 1;
819: }
820: return 0;
821: } elsif ($when eq 'nextweek') {
822: if ( ($time < $slot->{'starttime'} &&
823: ($time+$week) > $slot->{'starttime'})
824: ||
825: ($time < $slot->{'endtime'} &&
826: ($time+$week) > $slot->{'endtime'}) ) {
827: return 1;
828: }
829: return 0;
830: } elsif ($when eq 'lastweek') {
831: if ( ($time > $slot->{'starttime'} &&
832: ($time-$week) < $slot->{'starttime'})
833: ||
834: ($time > $slot->{'endtime'} &&
835: ($time-$week) < $slot->{'endtime'}) ) {
836: return 1;
837: }
838: return 0;
839: } elsif ($when eq 'willopen') {
840: if ($time < $slot->{'starttime'}) {
841: return 1;
842: }
843: return 0;
844: } elsif ($when eq 'wereopen') {
845: if ($time > $slot->{'endtime'}) {
846: return 1;
847: }
848: return 0;
849: }
850:
851: return 1;
852: }
853:
1.33 albertel 854: sub remove_link {
855: my ($slotname,$entry,$uname,$udom,$symb) = @_;
856:
1.55 albertel 857: my $remove = &mt('Remove');
858:
859: if ($entry eq 'remove all') {
860: $remove = &mt('Remove All');
861: undef($uname);
862: undef($udom);
863: }
864:
1.33 albertel 865: $slotname = &Apache::lonnet::escape($slotname);
866: $entry = &Apache::lonnet::escape($entry);
867: $uname = &Apache::lonnet::escape($uname);
868: $udom = &Apache::lonnet::escape($udom);
869: $symb = &Apache::lonnet::escape($symb);
870:
871: return <<"END_LINK";
872: <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
873: >($remove)</a>
874: END_LINK
875:
876: }
877:
1.5 albertel 878: sub show_table {
1.19 albertel 879: my ($r,$mgr)=@_;
1.5 albertel 880:
881: my ($cnum,$cdom)=&get_course();
882: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19 albertel 883: if ( (keys(%slots))[0] =~ /^error: 2 /) {
884: undef(%slots);
885: }
1.5 albertel 886: my $available;
1.14 albertel 887: if ($mgr eq 'F') {
1.30 albertel 888: $r->print('<div>');
1.14 albertel 889: $r->print('<form method="POST" action="/adm/slotrequest">
890: <input type="hidden" name="command" value="uploadstart" />
891: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
892: </form>');
1.28 albertel 893: $r->print('<form method="POST" action="/adm/helper/newslot.helper">
894: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
895: </form>');
1.30 albertel 896: $r->print('</div>');
1.14 albertel 897: }
1.29 albertel 898:
1.54 albertel 899: my %Saveable_Parameters = ('show' => 'array',
900: 'when' => 'scalar',
901: 'order' => 'scalar',
902: 'deleted' => 'scalar',
903: 'name_filter_type' => 'scalar',
904: 'name_filter_value' => 'scalar',
1.35 albertel 905: );
1.46 albertel 906: &Apache::loncommon::store_course_settings('slotrequest',
907: \%Saveable_Parameters);
908: &Apache::loncommon::restore_course_settings('slotrequest',
909: \%Saveable_Parameters);
910: &Apache::grades::init_perm();
911: my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
912: &Apache::grades::reset_perm();
1.29 albertel 913:
1.54 albertel 914: # what to display filtering
1.30 albertel 915: my %show_fields=&Apache::lonlocal::texthash(
1.49 albertel 916: 'name' => 'Slot Name',
917: 'description' => 'Description',
918: 'type' => 'Type',
919: 'starttime' => 'Start time',
920: 'endtime' => 'End Time',
921: 'startreserve' => 'Time students can start reserving',
922: 'secret' => 'Secret Word',
923: 'maxspace' => 'Maximum # of students',
924: 'ip' => 'IP or DNS restrictions',
925: 'symb' => 'Resource slot is restricted to.',
926: 'allowedsections' => 'Sections slot is restricted to.',
927: 'allowedusers' => 'Users slot is restricted to.',
928: 'uniqueperiod' => 'Period of time slot is unique',
929: 'scheduled' => 'Scheduled Students',
930: 'proctor' => 'List of proctors');
1.30 albertel 931: my @show_order=('name','description','type','starttime','endtime',
1.49 albertel 932: 'startreserve','secret','maxspace','ip','symb',
933: 'allowedsections','allowedusers','uniqueperiod',
934: 'scheduled','proctor');
1.30 albertel 935: my @show =
1.29 albertel 936: (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30 albertel 937: : keys(%show_fields);
938: my %show = map { $_ => 1 } (@show);
939:
1.54 albertel 940: #when filtering setup
1.30 albertel 941: my %when_fields=&Apache::lonlocal::texthash(
1.35 albertel 942: 'now' => 'Open now',
1.30 albertel 943: 'nextweek' => 'Open within the next week',
944: 'lastweek' => 'Were open last week',
945: 'willopen' => 'Will open later',
1.35 albertel 946: 'wereopen' => 'Were open',
947: 'any' => 'Anytime',
948: );
949: my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1.30 albertel 950: $when_fields{'select_form_order'} = \@when_order;
951: my $when = (exists($env{'form.when'})) ? $env{'form.when'}
952: : 'now';
1.29 albertel 953:
1.54 albertel 954: #display of students setup
1.46 albertel 955: my %stu_display_fields=
956: &Apache::lonlocal::texthash('username' => 'User name',
957: 'fullname' => 'Full name',
958: );
959: my @stu_display_order=('fullname','username');
960: my @stu_display =
961: (exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
962: : keys(%stu_display_fields);
963: my %stu_display = map { $_ => 1 } (@stu_display);
964:
1.54 albertel 965: #name filtering setup
966: my %name_filter_type_fields=
967: &Apache::lonlocal::texthash('substring' => 'Substring',
968: 'exact' => 'Exact',
969: #'reg' => 'Regular Expression',
970: );
971: my @name_filter_type_order=('substring','exact');
972:
973: $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
974: my $name_filter_type =
975: (exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
976: : 'substring';
977: my $name_filter = {'type' => $name_filter_type,
978: 'value' => $env{'form.name_filter_value'},};
979:
980: #deleted slot filtering
1.35 albertel 981: my $hide_radio =
982: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
983: my $show_radio =
984: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
985:
1.29 albertel 986: $r->print('<form method="POST" action="/adm/slotrequest">
1.30 albertel 987: <input type="hidden" name="command" value="showslots" />');
988: $r->print('<div>');
1.35 albertel 989: $r->print('<table class="inline">
990: <tr><th>'.&mt('Show').'</th>
1.46 albertel 991: <th>'.&mt('Student Display').'</th>
1.35 albertel 992: <th>'.&mt('Open').'</th>
1.54 albertel 993: <th>'.&mt('Slot Name Filter').'</th>
1.35 albertel 994: <th>'.&mt('Options').'</th>
995: </tr>
996: <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
997: '</td>
1.46 albertel 998: <td>
999: '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
1000: 6,\%stu_display_fields,
1001: \@stu_display_order).'
1002: </td>
1.35 albertel 1003: <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
1004: '</td>
1.54 albertel 1005: <td>'.&Apache::loncommon::select_form($name_filter_type,
1006: 'name_filter_type',
1007: %name_filter_type_fields).
1008: '<br />'.
1009: &Apache::lonhtmlcommon::textbox('name_filter_value',
1010: $env{'form.name_filter_value'},
1011: 15).
1012: '</td>
1.35 albertel 1013: <td>
1014: <table>
1015: <tr>
1016: <td rowspan="2">Deleted slots:</td>
1017: <td><label>'.$show_radio.'Show</label></td>
1018: </tr>
1019: <tr>
1020: <td><label>'.$hide_radio.'Hide</label></td>
1021: </tr>
1022: </table>
1023: </td>
1024: </tr>
1025: </table>');
1.30 albertel 1026: $r->print('</div>');
1027: $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21 albertel 1028: my $linkstart='<a href="/adm/slotrequest?command=showslots&order=';
1.30 albertel 1029: $r->print('<table class="thinborder">
1.10 albertel 1030: <tr>
1.29 albertel 1031: <th></th>');
1.30 albertel 1032: foreach my $which (@show_order) {
1033: if ($which ne 'proctor' && exists($show{$which})) {
1034: $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1.29 albertel 1035: }
1036: }
1037:
1.21 albertel 1038: my %name_cache;
1039: my $slotsort = sub {
1.49 albertel 1040: if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb|allowedsections|allowedusers)$/) {
1.21 albertel 1041: if (lc($slots{$a}->{$env{'form.order'}})
1042: ne lc($slots{$b}->{$env{'form.order'}})) {
1043: return (lc($slots{$a}->{$env{'form.order'}})
1044: cmp lc($slots{$b}->{$env{'form.order'}}));
1045: }
1.23 albertel 1046: } elsif ($env{'form.order'} eq 'name') {
1047: if (lc($a) cmp lc($b)) {
1048: return lc($a) cmp lc($b);
1049: }
1.29 albertel 1050: } elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21 albertel 1051:
1052: if ($slots{$a}->{'uniqueperiod'}[0]
1053: ne $slots{$b}->{'uniqueperiod'}[0]) {
1054: return ($slots{$a}->{'uniqueperiod'}[0]
1055: cmp $slots{$b}->{'uniqueperiod'}[0]);
1056: }
1057: if ($slots{$a}->{'uniqueperiod'}[1]
1058: ne $slots{$b}->{'uniqueperiod'}[1]) {
1059: return ($slots{$a}->{'uniqueperiod'}[1]
1060: cmp $slots{$b}->{'uniqueperiod'}[1]);
1061: }
1062: }
1063: return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
1064: };
1065: foreach my $slot (sort $slotsort (keys(%slots))) {
1.54 albertel 1066: if (!&to_show($slot,$slots{$slot},$when,
1067: $env{'form.deleted'},$name_filter)) { next; }
1.5 albertel 1068: if (defined($slots{$slot}->{'type'})
1069: && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13 albertel 1070: #next;
1.5 albertel 1071: }
1072: my $description=&get_description($slot,$slots{$slot});
1073: my $ids;
1.47 albertel 1074: if (exists($show{'scheduled'})) {
1075: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
1076: "^$slot\0");
1077: my ($tmp)=%consumed;
1078: if ($tmp !~ /^error: /) {
1.54 albertel 1079: foreach my $entry (sort { $consumed{$a}{name} cmp
1080: $consumed{$b}{name} }
1081: (keys(%consumed))) {
1.47 albertel 1082: my (undef,$id)=split("\0",$entry);
1.57 albertel 1083: my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1.47 albertel 1084: $ids.= '<nobr>';
1085: foreach my $item (@stu_display_order) {
1086: if ($stu_display{$item}) {
1087: if ($item eq 'fullname') {
1088: $ids.=$fullname->{"$uname:$udom"}.' ';
1089: } elsif ($item eq 'username') {
1.57 albertel 1090: $ids.="<tt>$uname:$udom</tt> ";
1.47 albertel 1091: }
1.46 albertel 1092: }
1093: }
1.47 albertel 1094: $ids.=&remove_link($slot,$entry,$uname,$udom,
1095: $consumed{$entry}{'symb'}).'</nobr><br />';
1.46 albertel 1096: }
1.38 albertel 1097: }
1.5 albertel 1098: }
1.33 albertel 1099:
1.24 albertel 1100: my $start=($slots{$slot}->{'starttime'}?
1101: &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
1102: my $end=($slots{$slot}->{'endtime'}?
1103: &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1.28 albertel 1104: my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24 albertel 1105: &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
1106:
1.14 albertel 1107: my $unique;
1108: if (ref($slots{$slot}{'uniqueperiod'})) {
1109: $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
1110: localtime($slots{$slot}{'uniqueperiod'}[1]);
1111: }
1.33 albertel 1112:
1.29 albertel 1113: my $title;
1114: if (exists($slots{$slot}{'symb'})) {
1115: my (undef,undef,$res)=
1116: &Apache::lonnet::decode_symb($slots{$slot}{'symb'});
1117: $res = &Apache::lonnet::clutter($res);
1118: $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
1119: $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
1120: }
1.33 albertel 1121:
1.49 albertel 1122: my $allowedsections;
1123: if (exists($show{'allowedsections'})) {
1124: $allowedsections =
1125: join(', ',sort(split(/\s*,\s*/,
1126: $slots{$slot}->{'allowedsections'})));
1127: }
1128:
1129: my @allowedusers;
1130: if (exists($show{'allowedusers'})) {
1131: @allowedusers= map {
1132: my ($uname,$udom)=split(/:/,$_);
1133: my $fullname=$name_cache{$_};
1134: if (!defined($fullname)) {
1135: $fullname = &Apache::loncommon::plainname($uname,$udom);
1136: $fullname =~s/\s/ /g;
1137: $name_cache{$_} = $fullname;
1138: }
1139: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
1140: } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
1141: }
1142: my $allowedusers=join(', ',@allowedusers);
1143:
1.29 albertel 1144: my @proctors;
1145: my $rowspan=1;
1146: my $colspan=1;
1.30 albertel 1147: if (exists($show{'proctor'})) {
1.29 albertel 1148: $rowspan=2;
1149: @proctors= map {
1150: my ($uname,$udom)=split(/@/,$_);
1151: my $fullname=$name_cache{$_};
1152: if (!defined($fullname)) {
1153: $fullname = &Apache::loncommon::plainname($uname,$udom);
1154: $fullname =~s/\s/ /g;
1155: $name_cache{$_} = $fullname;
1156: }
1157: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
1158: } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
1159: }
1.20 albertel 1160: my $proctors=join(', ',@proctors);
1.14 albertel 1161:
1.34 albertel 1162: my $edit=(<<"EDITLINK");
1.31 albertel 1163: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
1164: EDITLINK
1.34 albertel 1165:
1166: my $delete=(<<"DELETELINK");
1167: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
1168: DELETELINK
1.55 albertel 1169:
1.56 albertel 1170: my $remove_all=&remove_link($slot,'remove all').'<br />';
1.55 albertel 1171:
1.34 albertel 1172: if ($ids ne '') { undef($delete); }
1.56 albertel 1173: if ($slots{$slot}{'type'} ne 'schedulable_student'
1174: || $ids eq '') {
1.55 albertel 1175: undef($remove_all);
1176: }
1.34 albertel 1177:
1.56 albertel 1178: $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1.30 albertel 1179: if (exists($show{'name'})) {
1.29 albertel 1180: $colspan++;$r->print("<td>$slot</td>");
1181: }
1.33 albertel 1182: if (exists($show{'description'})) {
1183: $colspan++;$r->print("<td>$description</td>\n");
1184: }
1.30 albertel 1185: if (exists($show{'type'})) {
1.29 albertel 1186: $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
1187: }
1.30 albertel 1188: if (exists($show{'starttime'})) {
1.29 albertel 1189: $colspan++;$r->print("<td>$start</td>\n");
1190: }
1.30 albertel 1191: if (exists($show{'endtime'})) {
1.29 albertel 1192: $colspan++;$r->print("<td>$end</td>\n");
1193: }
1.30 albertel 1194: if (exists($show{'startreserve'})) {
1.29 albertel 1195: $colspan++;$r->print("<td>$start_reserve</td>\n");
1196: }
1.30 albertel 1197: if (exists($show{'secret'})) {
1.29 albertel 1198: $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
1199: }
1.30 albertel 1200: if (exists($show{'maxspace'})) {
1.29 albertel 1201: $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
1202: }
1.30 albertel 1203: if (exists($show{'ip'})) {
1.29 albertel 1204: $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
1205: }
1.30 albertel 1206: if (exists($show{'symb'})) {
1.29 albertel 1207: $colspan++;$r->print("<td>$title</td>\n");
1208: }
1.49 albertel 1209: if (exists($show{'allowedsections'})) {
1210: $colspan++;$r->print("<td>$allowedsections</td>\n");
1211: }
1212: if (exists($show{'allowedusers'})) {
1213: $colspan++;$r->print("<td>$allowedusers</td>\n");
1.29 albertel 1214: }
1.47 albertel 1215: if (exists($show{'scheduled'})) {
1.56 albertel 1216: $colspan++;$r->print("<td>$remove_all $ids</td>\n</tr>\n");
1.47 albertel 1217: }
1.30 albertel 1218: if (exists($show{'proctor'})) {
1.29 albertel 1219: $r->print(<<STUFF);
1.21 albertel 1220: <tr>
1.29 albertel 1221: <td colspan="$colspan">$proctors</td>
1.21 albertel 1222: </tr>
1.5 albertel 1223: STUFF
1.29 albertel 1224: }
1.5 albertel 1225: }
1226: $r->print('</table>');
1227: }
1228:
1.14 albertel 1229: sub upload_start {
1.19 albertel 1230: my ($r)=@_;
1.14 albertel 1231: $r->print(&Apache::grades::checkforfile_js());
1232: my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1233: $result.=' <b>'.
1234: &mt('Specify a file containing the slot definitions.').
1235: '</b></td></tr>'."\n";
1236: $result.='<tr bgcolor=#ffffe6><td>'."\n";
1237: my $upfile_select=&Apache::loncommon::upfile_select_html();
1238: my $ignore=&mt('Ignore First Line');
1239: $result.=<<ENDUPFORM;
1240: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1241: <input type="hidden" name="command" value="csvuploadmap" />
1242: $upfile_select
1243: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
1244: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
1245: </form>
1246: ENDUPFORM
1247: $result.='</td></tr></table>'."\n";
1248: $result.='</td></tr></table>'."\n";
1249: $r->print($result);
1250: }
1251:
1252: sub csvuploadmap_header {
1.19 albertel 1253: my ($r,$datatoken,$distotal)= @_;
1.14 albertel 1254: my $javascript;
1255: if ($env{'form.upfile_associate'} eq 'reverse') {
1256: $javascript=&csvupload_javascript_reverse_associate();
1257: } else {
1258: $javascript=&csvupload_javascript_forward_associate();
1259: }
1260:
1261: my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1262: my $ignore=&mt('Ignore First Line');
1263: $r->print(<<ENDPICK);
1264: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1265: <h3>Identify fields</h3>
1266: Total number of records found in file: $distotal <hr />
1267: Enter as many fields as you can. The system will inform you and bring you back
1268: to this page if the data selected is insufficient to create the slots.<hr />
1269: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1270: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
1271: <input type="hidden" name="associate" value="" />
1272: <input type="hidden" name="datatoken" value="$datatoken" />
1273: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
1274: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
1275: <input type="hidden" name="upfile_associate"
1276: value="$env{'form.upfile_associate'}" />
1277: <input type="hidden" name="command" value="csvuploadassign" />
1278: <hr />
1279: <script type="text/javascript" language="Javascript">
1280: $javascript
1281: </script>
1282: ENDPICK
1283: return '';
1284:
1285: }
1286:
1287: sub csvuploadmap_footer {
1288: my ($request,$i,$keyfields) =@_;
1289: $request->print(<<ENDPICK);
1290: </table>
1291: <input type="hidden" name="nfields" value="$i" />
1292: <input type="hidden" name="keyfields" value="$keyfields" />
1293: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
1294: </form>
1295: ENDPICK
1296: }
1297:
1298: sub csvupload_javascript_reverse_associate {
1299: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1300: return(<<ENDPICK);
1301: function verify(vf) {
1302: var foundstart=0;
1303: var foundend=0;
1304: var foundname=0;
1305: var foundtype=0;
1306: for (i=0;i<=vf.nfields.value;i++) {
1307: tw=eval('vf.f'+i+'.selectedIndex');
1308: if (i==0 && tw!=0) { foundname=1; }
1309: if (i==1 && tw!=0) { foundtype=1; }
1310: if (i==2 && tw!=0) { foundstat=1; }
1311: if (i==3 && tw!=0) { foundend=1; }
1312: }
1313: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1314: alert('$error1');
1315: return;
1316: }
1317: vf.submit();
1318: }
1319: function flip(vf,tf) {
1320: }
1321: ENDPICK
1322: }
1323:
1324: sub csvupload_javascript_forward_associate {
1325: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1326: return(<<ENDPICK);
1327: function verify(vf) {
1328: var foundstart=0;
1329: var foundend=0;
1330: var foundname=0;
1331: var foundtype=0;
1332: for (i=0;i<=vf.nfields.value;i++) {
1333: tw=eval('vf.f'+i+'.selectedIndex');
1334: if (tw==1) { foundname=1; }
1335: if (tw==2) { foundtype=1; }
1336: if (tw==3) { foundstat=1; }
1337: if (tw==4) { foundend=1; }
1338: }
1339: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1340: alert('$error1');
1341: return;
1342: }
1343: vf.submit();
1344: }
1345: function flip(vf,tf) {
1346: }
1347: ENDPICK
1348: }
1349:
1350: sub csv_upload_map {
1.19 albertel 1351: my ($r)= @_;
1.14 albertel 1352:
1353: my $datatoken;
1354: if (!$env{'form.datatoken'}) {
1355: $datatoken=&Apache::loncommon::upfile_store($r);
1356: } else {
1357: $datatoken=$env{'form.datatoken'};
1358: &Apache::loncommon::load_tmp_file($r);
1359: }
1360: my @records=&Apache::loncommon::upfile_record_sep();
1361: if ($env{'form.noFirstLine'}) { shift(@records); }
1.19 albertel 1362: &csvuploadmap_header($r,$datatoken,$#records+1);
1.14 albertel 1363: my ($i,$keyfields);
1364: if (@records) {
1365: my @fields=&csvupload_fields();
1366:
1367: if ($env{'form.upfile_associate'} eq 'reverse') {
1368: &Apache::loncommon::csv_print_samples($r,\@records);
1369: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
1370: \@fields);
1371: foreach (@fields) { $keyfields.=$_->[0].','; }
1372: chop($keyfields);
1373: } else {
1374: unshift(@fields,['none','']);
1375: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
1376: \@fields);
1377: my %sone=&Apache::loncommon::record_sep($records[0]);
1378: $keyfields=join(',',sort(keys(%sone)));
1379: }
1380: }
1381: &csvuploadmap_footer($r,$i,$keyfields);
1382:
1383: return '';
1384: }
1385:
1386: sub csvupload_fields {
1387: return (['name','Slot name'],
1388: ['type','Type of slot'],
1389: ['starttime','Start Time of slot'],
1390: ['endtime','End Time of slot'],
1.15 albertel 1391: ['startreserve','Reservation Start Time'],
1.14 albertel 1392: ['ip','IP or DNS restriction'],
1393: ['proctor','List of proctor ids'],
1394: ['description','Slot Description'],
1395: ['maxspace','Maximum number of reservations'],
1396: ['symb','Resource Restriction'],
1397: ['uniqueperiod','Date range of slot exclusion'],
1.49 albertel 1398: ['secret','Secret word proctor uses to validate'],
1399: ['allowedsections','Sections slot is restricted to'],
1400: ['allowedusers','Users slot is restricted to'],
1401: );
1.14 albertel 1402: }
1403:
1404: sub csv_upload_assign {
1.19 albertel 1405: my ($r,$mgr)= @_;
1.14 albertel 1406: &Apache::loncommon::load_tmp_file($r);
1407: my @slotdata = &Apache::loncommon::upfile_record_sep();
1408: if ($env{'form.noFirstLine'}) { shift(@slotdata); }
1409: my %fields=&Apache::grades::get_fields();
1410: $r->print('<h3>Creating Slots</h3>');
1411: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
1412: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1413: my $countdone=0;
1.31 albertel 1414: my @errors;
1.14 albertel 1415: foreach my $slot (@slotdata) {
1416: my %slot;
1417: my %entries=&Apache::loncommon::record_sep($slot);
1418: my $domain;
1419: my $name=$entries{$fields{'name'}};
1.31 albertel 1420: if ($name=~/^\s*$/) {
1421: push(@errors,"Did not create slot with no name");
1422: next;
1423: }
1424: if ($name=~/\s/) {
1425: push(@errors,"$name not created -- Name must not contain spaces");
1426: next;
1427: }
1428: if ($name=~/\W/) {
1429: push(@errors,"$name not created -- Name must contain only letters, numbers and _");
1430: next;
1431: }
1.14 albertel 1432: if ($entries{$fields{'type'}}) {
1433: $slot{'type'}=$entries{$fields{'type'}};
1434: } else {
1435: $slot{'type'}='preassigned';
1436: }
1.31 albertel 1437: if ($slot{'type'} ne 'preassigned' &&
1438: $slot{'type'} ne 'schedulable_student') {
1439: push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
1440: next;
1441: }
1.14 albertel 1442: if ($entries{$fields{'starttime'}}) {
1443: $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
1444: }
1445: if ($entries{$fields{'endtime'}}) {
1.16 albertel 1446: $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14 albertel 1447: }
1.58 albertel 1448:
1449: # start/endtime must be defined and greater than zero
1450: if (!$slot{'starttime'}) {
1451: push(@errors,"$name not created -- Invalid start time");
1452: next;
1453: }
1454: if (!$slot{'endtime'}) {
1455: push(@errors,"$name not created -- Invalid end time");
1456: next;
1457: }
1458: if ($slot{'starttime'} > $slot{'endtime'}) {
1459: push(@errors,"$name not created -- Slot starts after it ends");
1460: next;
1461: }
1462:
1.23 albertel 1463: if ($entries{$fields{'startreserve'}}) {
1464: $slot{'startreserve'}=
1465: &UnixDate($entries{$fields{'startreserve'}},"%s");
1466: }
1.58 albertel 1467: if (defined($slot{'startreserve'})
1468: && $slot{'startreserve'} > $slot{'starttime'}) {
1469: push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
1470: next;
1471: }
1472:
1.14 albertel 1473: foreach my $key ('ip','proctor','description','maxspace',
1474: 'secret','symb') {
1475: if ($entries{$fields{$key}}) {
1476: $slot{$key}=$entries{$fields{$key}};
1477: }
1478: }
1.58 albertel 1479:
1.14 albertel 1480: if ($entries{$fields{'uniqueperiod'}}) {
1481: my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
1482: my @times=(&UnixDate($start,"%s"),
1483: &UnixDate($end,"%s"));
1484: $slot{'uniqueperiod'}=\@times;
1485: }
1.58 albertel 1486: if (defined($slot{'uniqueperiod'})
1487: && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
1488: push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
1489: next;
1490: }
1.14 albertel 1491:
1492: &Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
1493: $r->print('.');
1494: $r->rflush();
1495: $countdone++;
1496: }
1.31 albertel 1497: $r->print("<p>Created $countdone slots\n</p>");
1498: foreach my $error (@errors) {
1499: $r->print("<p>$error\n</p>");
1500: }
1.19 albertel 1501: &show_table($r,$mgr);
1.14 albertel 1502: return '';
1503: }
1504:
1.1 albertel 1505: sub handler {
1506: my $r=shift;
1507:
1.30 albertel 1508: &Apache::loncommon::content_type($r,'text/html');
1509: &Apache::loncommon::no_cache($r);
1510: if ($r->header_only()) {
1511: $r->send_http_header();
1512: return OK;
1513: }
1514:
1.8 albertel 1515: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28 albertel 1516:
1.12 albertel 1517: my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14 albertel 1518: my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28 albertel 1519: my $title='Requesting Another Worktime';
1520: if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
1521: $title = 'Managing Slots';
1522: }
1523: &start_page($r,$title);
1524:
1.8 albertel 1525: if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19 albertel 1526: &show_table($r,$mgr);
1.33 albertel 1527: } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
1528: &remove_registration($r);
1529: } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1.55 albertel 1530: if ($env{'form.entry'} eq 'remove all') {
1531: &release_all_slot($r,$mgr);
1532: } else {
1533: &release_slot($r,undef,undef,undef,$mgr);
1534: }
1.34 albertel 1535: } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
1536: &delete_slot($r);
1.14 albertel 1537: } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19 albertel 1538: &upload_start($r);
1.14 albertel 1539: } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19 albertel 1540: &csv_upload_map($r);
1.14 albertel 1541: } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
1542: if ($env{'form.associate'} ne 'Reverse Association') {
1.19 albertel 1543: &csv_upload_assign($r,$mgr);
1.14 albertel 1544: } else {
1545: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
1546: $env{'form.upfile_associate'} = 'reverse';
1547: } else {
1548: $env{'form.upfile_associate'} = 'forward';
1549: }
1.19 albertel 1550: &csv_upload_map($r);
1.14 albertel 1551: }
1.8 albertel 1552: } else {
1.19 albertel 1553: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
1.61 ! albertel 1554: if (!defined($symb)) {
! 1555: &fail($r,'not_valid');
! 1556: return OK;
! 1557: }
1.19 albertel 1558: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.36 albertel 1559: my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1.60 albertel 1560: if ($useslots ne 'resource' && $useslots ne 'map') {
1.61 ! albertel 1561: &fail($r,'not_available');
1.19 albertel 1562: return OK;
1563: }
1564: $env{'request.symb'}=$symb;
1.36 albertel 1565: my $type = ($res =~ /\.task$/) ? 'Task'
1566: : 'problem';
1567: my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1.11 albertel 1568: if ($status eq 'CAN_ANSWER' ||
1569: $status eq 'NEEDS_CHECKIN' ||
1570: $status eq 'WAITING_FOR_GRADE') {
1571: &fail($r,'not_allowed');
1572: return OK;
1573: }
1574: if ($env{'form.requestattempt'}) {
1575: &show_choices($r,$symb);
1576: } elsif ($env{'form.command'} eq 'release') {
1577: &release_slot($r,$symb);
1578: } elsif ($env{'form.command'} eq 'get') {
1579: &get_slot($r,$symb);
1580: } elsif ($env{'form.command'} eq 'change') {
1.39 albertel 1581: if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
1582: &get_slot($r,$symb);
1583: }
1.11 albertel 1584: } else {
1585: $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
1586: }
1.2 albertel 1587: }
1.1 albertel 1588: &end_page($r);
1589: return OK;
1590: }
1.3 albertel 1591:
1592: 1;
1593: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>