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