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.88 2009/02/02 18:20:15 bisitz 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 [_1] has no reservations.',
349: '<tt>'.$slot_name.'</tt>').'</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>'.&mt('Slot [_1] marked as deleted.','<tt>'.$slot_name.'</tt>').'</p>');
561: } else {
562: $r->print('<p><span class="LC_error">'.&mt('An error occurred when attempting to delete slot: [_1]','<tt>'.$slot_name.'</tt>')." ($ret)</span></p>");
563: }
564: } else {
565: if (%consumed) {
566: $r->print('<p>'.&mt('Slot [_1] has active reservations.','<tt>'.$slot_name.'</tt>').'</p>');
567: } else {
568: $r->print('<p>'.&mt('Slot [_1] does not exist.','<tt>'.$slot_name.'</tt>').'</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>'.&mt('Already have a reservation: [_1].',$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>'
610: .&mt('You can either [_1]Change[_2] your reservation from [_3] to [_4] or'
611: ,'<input type="submit" name="change" value="'
612: ,'" />'
613: ,'<b>'.$description1.'</b>'
614: ,'<b>'.$description2.'</b>')
615: .'<br /></p>'
616: );
617: &return_link($r);
618: $r->print(<<STUFF);
619: </form>
620: STUFF
621: } else {
622: &return_link($r);
623: }
624: return 0;
625: }
626:
627: my $reserved=&make_reservation($env{'form.slotname'},
628: \%slot,$symb);
629: my $description=&get_description($env{'form.slotname'},\%slot);
630: if (defined($reserved)) {
631: my $retvalue = 0;
632: if ($slot_name =~ /^error: (.*)/) {
633: $r->print('<p><span class="LC_error">'
634: .&mt('An error occurred while attempting to make a reservation. ([_1])',$1)
635: .'</span></p>');
636: } elsif ($reserved > -1) {
637: $r->print('<p>'.&mt('Success: [_1]',$description).'</p>');
638: $retvalue = 1;
639: } elsif ($reserved < 0) {
640: $r->print('<p>'.&mt('Already reserved: [_1]',$description).'</p>');
641: }
642: if (!$inhibit_return_link) { &return_link($r); }
643: return 1;
644: }
645:
646: my %lt=('request'=>"Availibility list",
647: 'try' =>'Try again?',
648: 'or' => 'or');
649: %lt=&Apache::lonlocal::texthash(%lt);
650:
651: my $extra_input;
652: if ($conflictable_slot) {
653: $extra_input='<input type="hidden" name="releaseslot" value="'.$env{'form.slotname'}.'" />';
654: }
655:
656: $r->print('<p>'.&mt('[_1]Failed[_2] to reserve a slot for [_3].','<span class="LC_warning">','</span>',$description).'</p>');
657: $r->print(<<STUFF);
658: <p>
659: <form method="post" action="/adm/slotrequest">
660: <input type="submit" name="Try Again" value="$lt{'try'}" />
661: <input type="hidden" name="symb" value="$env{'form.symb'}" />
662: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
663: <input type="hidden" name="command" value="$env{'form.command'}" />
664: $extra_input
665: </form>
666: </p>
667: <p>
668: $lt{'or'}
669: <form method="post" action="/adm/slotrequest">
670: <input type="hidden" name="symb" value="$env{'form.symb'}" />
671: <input type="submit" name="requestattempt" value="$lt{'request'}" />
672: </form>
673: STUFF
674:
675: if (!$inhibit_return_link) {
676: $r->print(&mt('or').'</p>').&return_link($r);
677: } else {
678: $r->print('</p>');
679: }
680: return 0;
681: }
682:
683: sub allowed_slot {
684: my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
685:
686: #already started
687: if ($slot->{'starttime'} < time) {
688: return 0;
689: }
690: &Apache::lonxml::debug("$slot_name starttime good");
691:
692: #already ended
693: if ($slot->{'endtime'} < time) {
694: return 0;
695: }
696: &Apache::lonxml::debug("$slot_name endtime good");
697:
698: # not allowed to pick this one
699: if (defined($slot->{'type'})
700: && $slot->{'type'} ne 'schedulable_student') {
701: return 0;
702: }
703: &Apache::lonxml::debug("$slot_name type good");
704:
705: # reserve time not yet started
706: if ($slot->{'startreserve'} > time) {
707: return 0;
708: }
709: &Apache::lonxml::debug("$slot_name reserve good");
710:
711: my $userallowed=0;
712: # its for a different set of users
713: if (defined($slot->{'allowedsections'})) {
714: if (!defined($env{'request.role.sec'})
715: && grep(/^No section assigned$/,
716: split(',',$slot->{'allowedsections'}))) {
717: $userallowed=1;
718: }
719: if (defined($env{'request.role.sec'})
720: && grep(/^\Q$env{'request.role.sec'}\E$/,
721: split(',',$slot->{'allowedsections'}))) {
722: $userallowed=1;
723: }
724: if (defined($env{'request.course.groups'})) {
725: my @groups = split(/:/,$env{'request.course.groups'});
726: my @allowed_sec = split(',',$slot->{'allowedsections'});
727: foreach my $group (@groups) {
728: if (grep {$_ eq $group} (@allowed_sec)) {
729: $userallowed=1;
730: last;
731: }
732: }
733: }
734: }
735: &Apache::lonxml::debug("$slot_name sections is $userallowed");
736:
737: # its for a different set of users
738: if (defined($slot->{'allowedusers'})
739: && grep(/^\Q$env{'user.name'}:$env{'user.domain'}\E$/,
740: split(',',$slot->{'allowedusers'}))) {
741: $userallowed=1;
742: }
743:
744: if (!defined($slot->{'allowedusers'})
745: && !defined($slot->{'allowedsections'})) {
746: $userallowed=1;
747: }
748:
749: &Apache::lonxml::debug("$slot_name user is $userallowed");
750: return 0 if (!$userallowed);
751:
752: # not allowed for this resource
753: if (defined($slot->{'symb'})
754: && $slot->{'symb'} ne $symb) {
755: return 0;
756: }
757:
758: my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
759: $consumed_uniqueperiods);
760: if ($conflict =~ /^error: /) {
761: return 0;
762: } elsif ($conflict ne '') {
763: if ($slots->{$conflict}{'starttime'} < time) {
764: return 0;
765: }
766: }
767: &Apache::lonxml::debug("$slot_name symb good");
768: return 1;
769: }
770:
771: sub get_description {
772: my ($slot_name,$slot)=@_;
773: my $description=$slot->{'description'};
774: if (!defined($description)) {
775: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
776: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
777: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
778: }
779: return $description;
780: }
781:
782: sub show_choices {
783: my ($r,$symb)=@_;
784:
785: my ($cnum,$cdom)=&get_course();
786: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
787: my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
788: if (ref($consumed_uniqueperiods) eq 'HASH') {
789: if (&Apache::lonnet::error(%$consumed_uniqueperiods)) {
790: $r->print('<span class="LC_error">'.
791: &mt('An error occurred determining slot availability').
792: '</span>');
793: return;
794: }
795: } elsif ($consumed_uniqueperiods =~ /^error: /) {
796: $r->print('<span class="LC_error">'.
797: &mt('An error occurred determining slot availability').
798: '</span>');
799: return;
800: }
801: my $available;
802: &Apache::lonxml::debug("Checking Slots");
803: my @got_slots=&check_for_reservation($symb,'allslots');
804: if ($got_slots[0] =~ /^error: /) {
805: $r->print('<span class="LC_error">'.
806: &mt('An error occurred determining slot availability').
807: '</span>');
808: return;
809: }
810: $r->print('<table border="1">');
811: foreach my $slot (sort
812: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
813: (keys(%slots))) {
814:
815: &Apache::lonxml::debug("Checking Slot $slot");
816: next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
817: $consumed_uniqueperiods));
818:
819: $available++;
820:
821: my $description=&get_description($slot,$slots{$slot});
822:
823: my $form=&mt('Unavailable');
824: if ((grep(/^\Q$slot\E$/,@got_slots)) ||
825: &space_available($slot,$slots{$slot},$symb)) {
826: my $text=&mt('Select');
827: my $command='get';
828: if (grep(/^\Q$slot\E$/,@got_slots)) {
829: $text=&mt('Drop Reservation');
830: $command='release';
831: } else {
832: my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
833: \%slots,
834: $consumed_uniqueperiods);
835: if ($conflict) {
836: if ($conflict =~ /^error: /) {
837: $r->print('<tr><td><span class="LC_error" colspan="2">'
838: .&mt('Slot: [_1] has unknown status.',$description)
839: .'</span></td></tr>');
840: } else {
841: $text=&mt('Change Reservation');
842: $command='get';
843: }
844: }
845: }
846: my $escsymb=&escape($symb);
847: $form=<<STUFF;
848: <form method="post" action="/adm/slotrequest">
849: <input type="submit" name="Select" value="$text" />
850: <input type="hidden" name="symb" value="$escsymb" />
851: <input type="hidden" name="slotname" value="$slot" />
852: <input type="hidden" name="command" value="$command" />
853: </form>
854: STUFF
855: }
856: $r->print(<<STUFF);
857: <tr>
858: <td>$form</td>
859: <td>$description</td>
860: </tr>
861: STUFF
862: }
863:
864: if (!$available) {
865: $r->print('<tr><td>'.&mt('No available times.').
866: ' <a href="/adm/flip?postdata=return:">'.
867: &mt('Return to last resource').'</a></td></tr>');
868: }
869: $r->print('</table>');
870: }
871:
872: sub to_show {
873: my ($slotname,$slot,$when,$deleted,$name) = @_;
874: my $time=time;
875: my $week=60*60*24*7;
876:
877: if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
878: return 0;
879: }
880:
881: if ($name && $name->{'value'} =~ /\w/) {
882: if ($name->{'type'} eq 'substring') {
883: if ($slotname !~ /\Q$name->{'value'}\E/) {
884: return 0;
885: }
886: }
887: if ($name->{'type'} eq 'exact') {
888: if ($slotname eq $name->{'value'}) {
889: return 0;
890: }
891: }
892: }
893:
894: if ($when eq 'any') {
895: return 1;
896: } elsif ($when eq 'now') {
897: if ($time > $slot->{'starttime'} &&
898: $time < $slot->{'endtime'}) {
899: return 1;
900: }
901: return 0;
902: } elsif ($when eq 'nextweek') {
903: if ( ($time < $slot->{'starttime'} &&
904: ($time+$week) > $slot->{'starttime'})
905: ||
906: ($time < $slot->{'endtime'} &&
907: ($time+$week) > $slot->{'endtime'}) ) {
908: return 1;
909: }
910: return 0;
911: } elsif ($when eq 'lastweek') {
912: if ( ($time > $slot->{'starttime'} &&
913: ($time-$week) < $slot->{'starttime'})
914: ||
915: ($time > $slot->{'endtime'} &&
916: ($time-$week) < $slot->{'endtime'}) ) {
917: return 1;
918: }
919: return 0;
920: } elsif ($when eq 'willopen') {
921: if ($time < $slot->{'starttime'}) {
922: return 1;
923: }
924: return 0;
925: } elsif ($when eq 'wereopen') {
926: if ($time > $slot->{'endtime'}) {
927: return 1;
928: }
929: return 0;
930: }
931:
932: return 1;
933: }
934:
935: sub remove_link {
936: my ($slotname,$entry,$uname,$udom,$symb) = @_;
937:
938: my $remove = &mt('Remove');
939:
940: if ($entry eq 'remove all') {
941: $remove = &mt('Remove All');
942: undef($uname);
943: undef($udom);
944: }
945:
946: $slotname = &escape($slotname);
947: $entry = &escape($entry);
948: $uname = &escape($uname);
949: $udom = &escape($udom);
950: $symb = &escape($symb);
951:
952: return <<"END_LINK";
953: <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
954: >($remove)</a>
955: END_LINK
956:
957: }
958:
959: sub show_table {
960: my ($r,$mgr)=@_;
961:
962: my ($cnum,$cdom)=&get_course();
963: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
964: if ( (keys(%slots))[0] =~ /^error: 2 /) {
965: undef(%slots);
966: }
967: my $available;
968: if ($mgr eq 'F') {
969: # FIXME: This line should be deleted once Slots uses breadcrumbs
970: $r->print(&Apache::loncommon::help_open_topic('Slot About', 'Help on slots'));
971:
972: $r->print('<div>');
973: $r->print('<form method="post" action="/adm/slotrequest">
974: <input type="hidden" name="command" value="uploadstart" />
975: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
976: </form>');
977: $r->print(&Apache::loncommon::help_open_topic('Slot CommaDelimited'));
978: $r->print('<form method="post" action="/adm/helper/newslot.helper">
979: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
980: </form>');
981: $r->print(&Apache::loncommon::help_open_topic('Slot AddInterface'));
982: $r->print('</div>');
983: }
984:
985: my %Saveable_Parameters = ('show' => 'array',
986: 'when' => 'scalar',
987: 'order' => 'scalar',
988: 'deleted' => 'scalar',
989: 'name_filter_type' => 'scalar',
990: 'name_filter_value' => 'scalar',
991: );
992: &Apache::loncommon::store_course_settings('slotrequest',
993: \%Saveable_Parameters);
994: &Apache::loncommon::restore_course_settings('slotrequest',
995: \%Saveable_Parameters);
996: &Apache::grades::init_perm();
997: my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
998: &Apache::grades::reset_perm();
999:
1000: # what to display filtering
1001: my %show_fields=&Apache::lonlocal::texthash(
1002: 'name' => 'Slot Name',
1003: 'description' => 'Description',
1004: 'type' => 'Type',
1005: 'starttime' => 'Start time',
1006: 'endtime' => 'End Time',
1007: 'startreserve' => 'Time students can start reserving',
1008: 'secret' => 'Secret Word',
1009: 'space' => '# of students/max',
1010: 'ip' => 'IP or DNS restrictions',
1011: 'symb' => 'Resource slot is restricted to.',
1012: 'allowedsections' => 'Sections slot is restricted to.',
1013: 'allowedusers' => 'Users slot is restricted to.',
1014: 'uniqueperiod' => 'Period of time slot is unique',
1015: 'scheduled' => 'Scheduled Students',
1016: 'proctor' => 'List of proctors');
1017: my @show_order=('name','description','type','starttime','endtime',
1018: 'startreserve','secret','space','ip','symb',
1019: 'allowedsections','allowedusers','uniqueperiod',
1020: 'scheduled','proctor');
1021: my @show =
1022: (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1023: : keys(%show_fields);
1024: my %show = map { $_ => 1 } (@show);
1025:
1026: #when filtering setup
1027: my %when_fields=&Apache::lonlocal::texthash(
1028: 'now' => 'Open now',
1029: 'nextweek' => 'Open within the next week',
1030: 'lastweek' => 'Were open last week',
1031: 'willopen' => 'Will open later',
1032: 'wereopen' => 'Were open',
1033: 'any' => 'Anytime',
1034: );
1035: my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1036: $when_fields{'select_form_order'} = \@when_order;
1037: my $when = (exists($env{'form.when'})) ? $env{'form.when'}
1038: : 'now';
1039:
1040: #display of students setup
1041: my %stu_display_fields=
1042: &Apache::lonlocal::texthash('username' => 'User name',
1043: 'fullname' => 'Full name',
1044: );
1045: my @stu_display_order=('fullname','username');
1046: my @stu_display =
1047: (exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
1048: : keys(%stu_display_fields);
1049: my %stu_display = map { $_ => 1 } (@stu_display);
1050:
1051: #name filtering setup
1052: my %name_filter_type_fields=
1053: &Apache::lonlocal::texthash('substring' => 'Substring',
1054: 'exact' => 'Exact',
1055: #'reg' => 'Regular Expression',
1056: );
1057: my @name_filter_type_order=('substring','exact');
1058:
1059: $name_filter_type_fields{'select_form_order'} = \@name_filter_type_order;
1060: my $name_filter_type =
1061: (exists($env{'form.name_filter_type'})) ? $env{'form.name_filter_type'}
1062: : 'substring';
1063: my $name_filter = {'type' => $name_filter_type,
1064: 'value' => $env{'form.name_filter_value'},};
1065:
1066:
1067: #deleted slot filtering
1068: #default to hide if no value
1069: $env{'form.deleted'} ||= 'hide';
1070: my $hide_radio =
1071: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
1072: my $show_radio =
1073: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
1074:
1075: $r->print('<form method="post" action="/adm/slotrequest">
1076: <input type="hidden" name="command" value="showslots" />');
1077: $r->print('<div>');
1078: $r->print('<table class="inline">
1079: <tr><th>'.&mt('Show').'</th>
1080: <th>'.&mt('Student Display').'</th>
1081: <th>'.&mt('Open').'</th>
1082: <th>'.&mt('Slot Name Filter').'</th>
1083: <th>'.&mt('Options').'</th>
1084: </tr>
1085: <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
1086: '</td>
1087: <td>
1088: '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
1089: 6,\%stu_display_fields,
1090: \@stu_display_order).'
1091: </td>
1092: <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
1093: '</td>
1094: <td>'.&Apache::loncommon::select_form($name_filter_type,
1095: 'name_filter_type',
1096: %name_filter_type_fields).
1097: '<br />'.
1098: &Apache::lonhtmlcommon::textbox('name_filter_value',
1099: $env{'form.name_filter_value'},
1100: 15).
1101: '</td>
1102: <td>
1103: <table>
1104: <tr>
1105: <td rowspan="2">Deleted slots:</td>
1106: <td><label>'.$show_radio.'Show</label></td>
1107: </tr>
1108: <tr>
1109: <td><label>'.$hide_radio.'Hide</label></td>
1110: </tr>
1111: </table>
1112: </td>
1113: </tr>
1114: </table>');
1115: $r->print('</div>');
1116: $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1117: my $linkstart='<a href="/adm/slotrequest?command=showslots&order=';
1118: $r->print(&Apache::loncommon::start_data_table().
1119: &Apache::loncommon::start_data_table_header_row().'
1120: <th></th>');
1121: foreach my $which (@show_order) {
1122: if ($which ne 'proctor' && exists($show{$which})) {
1123: $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1124: }
1125: }
1126: $r->print(&Apache::loncommon::end_data_table_header_row());
1127:
1128: my %name_cache;
1129: my $slotsort = sub {
1130: if ($env{'form.order'}=~/^(type|description|endtime|startreserve|ip|symb|allowedsections|allowedusers)$/) {
1131: if (lc($slots{$a}->{$env{'form.order'}})
1132: ne lc($slots{$b}->{$env{'form.order'}})) {
1133: return (lc($slots{$a}->{$env{'form.order'}})
1134: cmp lc($slots{$b}->{$env{'form.order'}}));
1135: }
1136: } elsif ($env{'form.order'} eq 'space') {
1137: if ($slots{$a}{'maxspace'} ne $slots{$b}{'maxspace'}) {
1138: return ($slots{$a}{'maxspace'} cmp $slots{$b}{'maxspace'});
1139: }
1140: } elsif ($env{'form.order'} eq 'name') {
1141: if (lc($a) cmp lc($b)) {
1142: return lc($a) cmp lc($b);
1143: }
1144: } elsif ($env{'form.order'} eq 'uniqueperiod') {
1145:
1146: if ($slots{$a}->{'uniqueperiod'}[0]
1147: ne $slots{$b}->{'uniqueperiod'}[0]) {
1148: return ($slots{$a}->{'uniqueperiod'}[0]
1149: cmp $slots{$b}->{'uniqueperiod'}[0]);
1150: }
1151: if ($slots{$a}->{'uniqueperiod'}[1]
1152: ne $slots{$b}->{'uniqueperiod'}[1]) {
1153: return ($slots{$a}->{'uniqueperiod'}[1]
1154: cmp $slots{$b}->{'uniqueperiod'}[1]);
1155: }
1156: }
1157: return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
1158: };
1159:
1160: my %consumed;
1161: if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
1162: %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum);
1163: my ($tmp)=%consumed;
1164: if ($tmp =~ /^error: /) { undef(%consumed); }
1165: }
1166:
1167: foreach my $slot (sort $slotsort (keys(%slots))) {
1168: if (!&to_show($slot,$slots{$slot},$when,
1169: $env{'form.deleted'},$name_filter)) { next; }
1170: if (defined($slots{$slot}->{'type'})
1171: && $slots{$slot}->{'type'} ne 'schedulable_student') {
1172: #next;
1173: }
1174: my $description=&get_description($slot,$slots{$slot});
1175: my ($id_count,$ids);
1176:
1177: if (exists($show{'scheduled'}) || exists($show{'space'}) ) {
1178: my $re_str = "$slot\0";
1179: my @this_slot = grep(/^\Q$re_str\E/,keys(%consumed));
1180: $id_count = scalar(@this_slot);
1181: if (exists($show{'scheduled'})) {
1182: foreach my $entry (sort { $consumed{$a}{name} cmp
1183: $consumed{$b}{name} }
1184: (@this_slot)) {
1185: my (undef,$id)=split("\0",$entry);
1186: my ($uname,$udom) = split(':',$consumed{$entry}{'name'});
1187: $ids.= '<span class="LC_nobreak">';
1188: foreach my $item (@stu_display_order) {
1189: if ($stu_display{$item}) {
1190: if ($item eq 'fullname') {
1191: $ids.=$fullname->{"$uname:$udom"}.' ';
1192: } elsif ($item eq 'username') {
1193: $ids.="<tt>$uname:$udom</tt> ";
1194: }
1195: }
1196: }
1197: $ids.=&remove_link($slot,$entry,$uname,$udom,
1198: $consumed{$entry}{'symb'}).'</span><br />';
1199: }
1200: }
1201: }
1202:
1203: my $start=($slots{$slot}->{'starttime'}?
1204: &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
1205: my $end=($slots{$slot}->{'endtime'}?
1206: &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1207: my $start_reserve=($slots{$slot}->{'startreserve'}?
1208: &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
1209:
1210: my $unique;
1211: if (ref($slots{$slot}{'uniqueperiod'})) {
1212: $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).', '.
1213: localtime($slots{$slot}{'uniqueperiod'}[1]);
1214: }
1215:
1216: my $title;
1217: if (exists($slots{$slot}{'symb'})) {
1218: my (undef,undef,$res)=
1219: &Apache::lonnet::decode_symb($slots{$slot}{'symb'});
1220: $res = &Apache::lonnet::clutter($res);
1221: $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
1222: $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
1223: }
1224:
1225: my $allowedsections;
1226: if (exists($show{'allowedsections'})) {
1227: $allowedsections =
1228: join(', ',sort(split(/\s*,\s*/,
1229: $slots{$slot}->{'allowedsections'})));
1230: }
1231:
1232: my @allowedusers;
1233: if (exists($show{'allowedusers'})) {
1234: @allowedusers= map {
1235: my ($uname,$udom)=split(/:/,$_);
1236: my $fullname=$name_cache{$_};
1237: if (!defined($fullname)) {
1238: $fullname = &Apache::loncommon::plainname($uname,$udom);
1239: $fullname =~s/\s/ /g;
1240: $name_cache{$_} = $fullname;
1241: }
1242: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
1243: } (sort(split(/\s*,\s*/,$slots{$slot}->{'allowedusers'})));
1244: }
1245: my $allowedusers=join(', ',@allowedusers);
1246:
1247: my @proctors;
1248: my $rowspan=1;
1249: my $colspan=1;
1250: if (exists($show{'proctor'})) {
1251: $rowspan=2;
1252: @proctors= map {
1253: my ($uname,$udom)=split(/:/,$_);
1254: my $fullname=$name_cache{$_};
1255: if (!defined($fullname)) {
1256: $fullname = &Apache::loncommon::plainname($uname,$udom);
1257: $fullname =~s/\s/ /g;
1258: $name_cache{$_} = $fullname;
1259: }
1260: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
1261: } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
1262: }
1263: my $proctors=join(', ',@proctors);
1264:
1265: my $edit=(<<"EDITLINK");
1266: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
1267: EDITLINK
1268:
1269: my $delete=(<<"DELETELINK");
1270: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
1271: DELETELINK
1272:
1273: my $remove_all=&remove_link($slot,'remove all').'<br />';
1274:
1275: if ($ids ne '') { undef($delete); }
1276: if ($slots{$slot}{'type'} ne 'schedulable_student'
1277: || $ids eq '') {
1278: undef($remove_all);
1279: }
1280:
1281: my $row_start=&Apache::loncommon::start_data_table_row();
1282: my $row_end=&Apache::loncommon::end_data_table_row();
1283: $r->print($row_start.
1284: "\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1285: if (exists($show{'name'})) {
1286: $colspan++;$r->print("<td>$slot</td>");
1287: }
1288: if (exists($show{'description'})) {
1289: $colspan++;$r->print("<td>$description</td>\n");
1290: }
1291: if (exists($show{'type'})) {
1292: $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
1293: }
1294: if (exists($show{'starttime'})) {
1295: $colspan++;$r->print("<td>$start</td>\n");
1296: }
1297: if (exists($show{'endtime'})) {
1298: $colspan++;$r->print("<td>$end</td>\n");
1299: }
1300: if (exists($show{'startreserve'})) {
1301: $colspan++;$r->print("<td>$start_reserve</td>\n");
1302: }
1303: if (exists($show{'secret'})) {
1304: $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
1305: }
1306: if (exists($show{'space'})) {
1307: my $display = $id_count;
1308: if ($slots{$slot}{'maxspace'}>0) {
1309: $display.='/'.$slots{$slot}{'maxspace'};
1310: if ($slots{$slot}{'maxspace'} <= $id_count) {
1311: $display = '<strong>'.$display.' (full) </strong>';
1312: }
1313: }
1314: $colspan++;$r->print("<td>$display</td>\n");
1315: }
1316: if (exists($show{'ip'})) {
1317: $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
1318: }
1319: if (exists($show{'symb'})) {
1320: $colspan++;$r->print("<td>$title</td>\n");
1321: }
1322: if (exists($show{'allowedsections'})) {
1323: $colspan++;$r->print("<td>$allowedsections</td>\n");
1324: }
1325: if (exists($show{'allowedusers'})) {
1326: $colspan++;$r->print("<td>$allowedusers</td>\n");
1327: }
1328: if (exists($show{'uniqueperiod'})) {
1329: $colspan++;$r->print("<td>$unique</td>\n");
1330: }
1331: if (exists($show{'scheduled'})) {
1332: $colspan++;$r->print("<td>$remove_all $ids</td>\n");
1333: }
1334: $r->print("$row_end\n");
1335: if (exists($show{'proctor'})) {
1336: $r->print(<<STUFF);
1337: $row_start
1338: <td colspan="$colspan">$proctors</td>
1339: $row_end
1340: STUFF
1341: }
1342: }
1343: $r->print('</table></form>');
1344: }
1345:
1346: sub upload_start {
1347: my ($r)=@_;
1348: $r->print(&Apache::grades::checkforfile_js());
1349: my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1350: $result.=' <b>'.
1351: &mt('Specify a file containing the slot definitions.').
1352: '</b></td></tr>'."\n";
1353: $result.='<tr bgcolor="#ffffe6"><td>'."\n";
1354: my $upfile_select=&Apache::loncommon::upfile_select_html();
1355: my $ignore=&mt('Ignore First Line');
1356: $result.=<<ENDUPFORM;
1357: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1358: <input type="hidden" name="command" value="csvuploadmap" />
1359: $upfile_select
1360: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
1361: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
1362: </form>
1363: ENDUPFORM
1364: $result.='</td></tr></table>'."\n";
1365: $result.='</td></tr></table>'."\n";
1366: $r->print($result);
1367: }
1368:
1369: sub csvuploadmap_header {
1370: my ($r,$datatoken,$distotal)= @_;
1371: my $javascript;
1372: if ($env{'form.upfile_associate'} eq 'reverse') {
1373: $javascript=&csvupload_javascript_reverse_associate();
1374: } else {
1375: $javascript=&csvupload_javascript_forward_associate();
1376: }
1377:
1378: my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1379: my $ignore=&mt('Ignore First Line');
1380: my $help_field = &Apache::loncommon::help_open_topic('Slot SelectingField');
1381:
1382: $r->print(<<ENDPICK);
1383: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1384: <h3>Identify fields $help_field</h3>
1385: Total number of records found in file: $distotal <hr />
1386: Enter as many fields as you can. The system will inform you and bring you back
1387: to this page if the data selected is insufficient to create the slots.<hr />
1388: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1389: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
1390: <input type="hidden" name="associate" value="" />
1391: <input type="hidden" name="datatoken" value="$datatoken" />
1392: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
1393: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
1394: <input type="hidden" name="upfile_associate"
1395: value="$env{'form.upfile_associate'}" />
1396: <input type="hidden" name="command" value="csvuploadassign" />
1397: <hr />
1398: <script type="text/javascript" language="Javascript">
1399: $javascript
1400: </script>
1401: ENDPICK
1402: return '';
1403:
1404: }
1405:
1406: sub csvuploadmap_footer {
1407: my ($request,$i,$keyfields) =@_;
1408: my $buttontext = &mt('Create Slots');
1409: $request->print(<<ENDPICK);
1410: </table>
1411: <input type="hidden" name="nfields" value="$i" />
1412: <input type="hidden" name="keyfields" value="$keyfields" />
1413: <input type="button" onClick="javascript:verify(this.form)" value="$buttontext" /><br />
1414: </form>
1415: ENDPICK
1416: }
1417:
1418: sub csvupload_javascript_reverse_associate {
1419: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1420: return(<<ENDPICK);
1421: function verify(vf) {
1422: var foundstart=0;
1423: var foundend=0;
1424: var foundname=0;
1425: var foundtype=0;
1426: for (i=0;i<=vf.nfields.value;i++) {
1427: tw=eval('vf.f'+i+'.selectedIndex');
1428: if (i==0 && tw!=0) { foundname=1; }
1429: if (i==1 && tw!=0) { foundtype=1; }
1430: if (i==2 && tw!=0) { foundstat=1; }
1431: if (i==3 && tw!=0) { foundend=1; }
1432: }
1433: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1434: alert('$error1');
1435: return;
1436: }
1437: vf.submit();
1438: }
1439: function flip(vf,tf) {
1440: }
1441: ENDPICK
1442: }
1443:
1444: sub csvupload_javascript_forward_associate {
1445: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1446: return(<<ENDPICK);
1447: function verify(vf) {
1448: var foundstart=0;
1449: var foundend=0;
1450: var foundname=0;
1451: var foundtype=0;
1452: for (i=0;i<=vf.nfields.value;i++) {
1453: tw=eval('vf.f'+i+'.selectedIndex');
1454: if (tw==1) { foundname=1; }
1455: if (tw==2) { foundtype=1; }
1456: if (tw==3) { foundstat=1; }
1457: if (tw==4) { foundend=1; }
1458: }
1459: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1460: alert('$error1');
1461: return;
1462: }
1463: vf.submit();
1464: }
1465: function flip(vf,tf) {
1466: }
1467: ENDPICK
1468: }
1469:
1470: sub csv_upload_map {
1471: my ($r)= @_;
1472:
1473: my $datatoken;
1474: if (!$env{'form.datatoken'}) {
1475: $datatoken=&Apache::loncommon::upfile_store($r);
1476: } else {
1477: $datatoken=$env{'form.datatoken'};
1478: &Apache::loncommon::load_tmp_file($r);
1479: }
1480: my @records=&Apache::loncommon::upfile_record_sep();
1481: if ($env{'form.noFirstLine'}) { shift(@records); }
1482: &csvuploadmap_header($r,$datatoken,$#records+1);
1483: my ($i,$keyfields);
1484: if (@records) {
1485: my @fields=&csvupload_fields();
1486:
1487: if ($env{'form.upfile_associate'} eq 'reverse') {
1488: &Apache::loncommon::csv_print_samples($r,\@records);
1489: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
1490: \@fields);
1491: foreach (@fields) { $keyfields.=$_->[0].','; }
1492: chop($keyfields);
1493: } else {
1494: unshift(@fields,['none','']);
1495: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
1496: \@fields);
1497: my %sone=&Apache::loncommon::record_sep($records[0]);
1498: $keyfields=join(',',sort(keys(%sone)));
1499: }
1500: }
1501: &csvuploadmap_footer($r,$i,$keyfields);
1502:
1503: return '';
1504: }
1505:
1506: sub csvupload_fields {
1507: return (['name','Slot name'],
1508: ['type','Type of slot'],
1509: ['starttime','Start Time of slot'],
1510: ['endtime','End Time of slot'],
1511: ['startreserve','Reservation Start Time'],
1512: ['ip','IP or DNS restriction'],
1513: ['proctor','List of proctor ids'],
1514: ['description','Slot Description'],
1515: ['maxspace','Maximum number of reservations'],
1516: ['symb','Resource Restriction'],
1517: ['uniqueperiod','Date range of slot exclusion'],
1518: ['secret','Secret word proctor uses to validate'],
1519: ['allowedsections','Sections slot is restricted to'],
1520: ['allowedusers','Users slot is restricted to'],
1521: );
1522: }
1523:
1524: sub csv_upload_assign {
1525: my ($r,$mgr)= @_;
1526: &Apache::loncommon::load_tmp_file($r);
1527: my @slotdata = &Apache::loncommon::upfile_record_sep();
1528: if ($env{'form.noFirstLine'}) { shift(@slotdata); }
1529: my %fields=&Apache::grades::get_fields();
1530: $r->print('<h3>'.&mt('Creating Slots').'</h3>');
1531: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
1532: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1533: my $countdone=0;
1534: my @errors;
1535: foreach my $slot (@slotdata) {
1536: my %slot;
1537: my %entries=&Apache::loncommon::record_sep($slot);
1538: my $domain;
1539: my $name=$entries{$fields{'name'}};
1540: if ($name=~/^\s*$/) {
1541: push(@errors,"Did not create slot with no name");
1542: next;
1543: }
1544: if ($name=~/\s/) {
1545: push(@errors,"$name not created -- Name must not contain spaces");
1546: next;
1547: }
1548: if ($name=~/\W/) {
1549: push(@errors,"$name not created -- Name must contain only letters, numbers and _");
1550: next;
1551: }
1552: if ($entries{$fields{'type'}}) {
1553: $slot{'type'}=$entries{$fields{'type'}};
1554: } else {
1555: $slot{'type'}='preassigned';
1556: }
1557: if ($slot{'type'} ne 'preassigned' &&
1558: $slot{'type'} ne 'schedulable_student') {
1559: push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
1560: next;
1561: }
1562: if ($entries{$fields{'starttime'}}) {
1563: $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
1564: }
1565: if ($entries{$fields{'endtime'}}) {
1566: $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1567: }
1568:
1569: # start/endtime must be defined and greater than zero
1570: if (!$slot{'starttime'}) {
1571: push(@errors,"$name not created -- Invalid start time");
1572: next;
1573: }
1574: if (!$slot{'endtime'}) {
1575: push(@errors,"$name not created -- Invalid end time");
1576: next;
1577: }
1578: if ($slot{'starttime'} > $slot{'endtime'}) {
1579: push(@errors,"$name not created -- Slot starts after it ends");
1580: next;
1581: }
1582:
1583: if ($entries{$fields{'startreserve'}}) {
1584: $slot{'startreserve'}=
1585: &UnixDate($entries{$fields{'startreserve'}},"%s");
1586: }
1587: if (defined($slot{'startreserve'})
1588: && $slot{'startreserve'} > $slot{'starttime'}) {
1589: push(@errors,"$name not created -- Slot's reservation start time is after the slot's start time.");
1590: next;
1591: }
1592:
1593: foreach my $key ('ip','proctor','description','maxspace',
1594: 'secret','symb') {
1595: if ($entries{$fields{$key}}) {
1596: $slot{$key}=$entries{$fields{$key}};
1597: }
1598: }
1599:
1600: if ($entries{$fields{'uniqueperiod'}}) {
1601: my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
1602: my @times=(&UnixDate($start,"%s"),
1603: &UnixDate($end,"%s"));
1604: $slot{'uniqueperiod'}=\@times;
1605: }
1606: if (defined($slot{'uniqueperiod'})
1607: && $slot{'uniqueperiod'}[0] > $slot{'uniqueperiod'}[1]) {
1608: push(@errors,"$name not created -- Slot's unique period start time is later than the unique period's end time.");
1609: next;
1610: }
1611:
1612: &Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
1613: $r->print('.');
1614: $r->rflush();
1615: $countdone++;
1616: }
1617: $r->print('<p>'.&mt('Created [quant,_1,slot]',$countdone)."\n".'</p>');
1618: foreach my $error (@errors) {
1619: $r->print('<p><span class="LC_warning">'.$error.'</span></p>'."\n");
1620: }
1621: &show_table($r,$mgr);
1622: return '';
1623: }
1624:
1625: sub handler {
1626: my $r=shift;
1627:
1628: &Apache::loncommon::content_type($r,'text/html');
1629: &Apache::loncommon::no_cache($r);
1630: if ($r->header_only()) {
1631: $r->send_http_header();
1632: return OK;
1633: }
1634:
1635: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1636:
1637: my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1638: my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1639: my $title='Requesting Another Worktime';
1640: if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
1641: $title = 'Managing Slots';
1642: }
1643: &start_page($r,$title);
1644:
1645: if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1646: &show_table($r,$mgr);
1647: } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
1648: &remove_registration($r);
1649: } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1650: if ($env{'form.entry'} eq 'remove all') {
1651: &release_all_slot($r,$mgr);
1652: } else {
1653: &release_slot($r,undef,undef,undef,$mgr);
1654: }
1655: } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
1656: &delete_slot($r);
1657: } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1658: &upload_start($r);
1659: } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1660: &csv_upload_map($r);
1661: } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
1662: if ($env{'form.associate'} ne 'Reverse Association') {
1663: &csv_upload_assign($r,$mgr);
1664: } else {
1665: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
1666: $env{'form.upfile_associate'} = 'reverse';
1667: } else {
1668: $env{'form.upfile_associate'} = 'forward';
1669: }
1670: &csv_upload_map($r);
1671: }
1672: } else {
1673: my $symb=&unescape($env{'form.symb'});
1674: if (!defined($symb)) {
1675: &fail($r,'not_valid');
1676: return OK;
1677: }
1678: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1679: my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1680: if ($useslots ne 'resource'
1681: && $useslots ne 'map'
1682: && $useslots ne 'map_map') {
1683: &fail($r,'not_available');
1684: return OK;
1685: }
1686: $env{'request.symb'}=$symb;
1687: my $type = ($res =~ /\.task$/) ? 'Task'
1688: : 'problem';
1689: my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1690: if ($status eq 'CAN_ANSWER' ||
1691: $status eq 'NEEDS_CHECKIN' ||
1692: $status eq 'WAITING_FOR_GRADE') {
1693: &fail($r,'not_allowed');
1694: return OK;
1695: }
1696: if ($env{'form.requestattempt'}) {
1697: &show_choices($r,$symb);
1698: } elsif ($env{'form.command'} eq 'release') {
1699: &release_slot($r,$symb);
1700: } elsif ($env{'form.command'} eq 'get') {
1701: &get_slot($r,$symb);
1702: } elsif ($env{'form.command'} eq 'change') {
1703: if (&get_slot($r,$symb,$env{'form.releaseslot'},1)) {
1704: &release_slot($r,$symb,$env{'form.releaseslot'});
1705: }
1706: } else {
1707: $r->print('<p>'.&mt('Unknown command: [_1]',$env{'form.command'}).'</p>');
1708: }
1709: }
1710: &end_page($r);
1711: return OK;
1712: }
1713:
1714: 1;
1715: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>