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