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