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