Annotation of loncom/interface/slotrequest.pm, revision 1.48.2.1
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Handler for requesting to have slots added to a students record
3: #
1.48.2.1! albertel 4: # $Id: slotrequest.pm,v 1.48 2006/02/26 19:40:14 albertel 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.1 albertel 39:
40: sub fail {
41: my ($r,$code)=@_;
1.2 albertel 42: if ($code eq 'not_valid') {
1.8 albertel 43: $r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
1.2 albertel 44:
1.8 albertel 45: } elsif ($code eq 'not_allowed') {
46: $r->print('<p>'.&mt('Not allowed to sign up or change reservations at this time.').'</p>');
47: } else {
48: $r->print('<p>'.&mt('Failed.').'</p>');
1.2 albertel 49: }
1.8 albertel 50:
1.42 albertel 51: &return_link($r);
1.1 albertel 52: &end_page($r);
53: }
54:
55: sub start_page {
1.28 albertel 56: my ($r,$title)=@_;
1.1 albertel 57: my $html=&Apache::lonxml::xmlbegin();
1.28 albertel 58: $r->print($html.'<head><title>'.&mt($title).'</title></head>');
59: $r->print(&Apache::loncommon::bodytag($title));
1.1 albertel 60: }
61:
62: sub end_page {
63: my ($r)=@_;
64: $r->print(&Apache::loncommon::endbodytag().'</html>');
65: }
66:
1.2 albertel 67: =pod
68:
69: slot_reservations db
70: - keys are
71: - slotname\0id -> value is an hashref of
72: name -> user@domain of holder
73: timestamp -> timestamp of reservation
74: symb -> symb of resource that it is reserved for
75:
76: =cut
77:
78: sub get_course {
79: (undef,my $courseid)=&Apache::lonxml::whichuser();
80: my $cdom=$env{'course.'.$courseid.'.domain'};
81: my $cnum=$env{'course.'.$courseid.'.num'};
82: return ($cnum,$cdom);
83: }
84:
85: sub get_reservation_ids {
86: my ($slot_name)=@_;
87:
88: my ($cnum,$cdom)=&get_course();
89:
90: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
91: "^$slot_name\0");
1.40 albertel 92: if (&network_error(%consumed)) {
93: return 'error: Unable to determine current status';
94: }
1.2 albertel 95: my ($tmp)=%consumed;
96: if ($tmp=~/^error: 2 / ) {
97: return 0;
98: }
99: return keys(%consumed);
100: }
101:
102: sub space_available {
103: my ($slot_name,$slot)=@_;
104: my $max=$slot->{'maxspace'};
105:
106: if (!defined($max)) { return 1; }
107:
108: my $consumed=scalar(&get_reservation_ids($slot_name));
109: if ($consumed < $max) {
110: return 1
111: }
112: return 0;
113: }
1.3 albertel 114:
1.4 albertel 115: sub check_for_reservation {
1.43 albertel 116: my ($symb,$mode)=@_;
1.4 albertel 117: my $student = &Apache::lonnet::EXT("resource.0.availablestudent", $symb,
118: $env{'user.domain'}, $env{'user.name'});
119:
120: my $course = &Apache::lonnet::EXT("resource.0.available", $symb,
121: $env{'user.domain'}, $env{'user.name'});
122: my @slots = (split(/:/,$student), split(/:/, $course));
123:
124: &Apache::lonxml::debug(" slot list is ".join(':',@slots));
125:
126: my ($cnum,$cdom)=&get_course();
127: my %slots=&Apache::lonnet::get('slots', [@slots], $cdom, $cnum);
128:
1.41 albertel 129: if (&network_error($student) || &network_error($course) ||
130: &network_error(%slots)) {
131: return 'error: Unable to determine current status';
132: }
1.43 albertel 133: my @got;
134: foreach my $slot_name (sort {
135: if (ref($slots{$a}) && ref($slots{$b})) {
136: return $slots{$a}{'starttime'} <=> $slots{$b}{'starttime'}
137: }
138: if (ref($slots{$a})) { return -1;}
139: if (ref($slots{$b})) { return 1;}
140: return 0;
141: } @slots) {
1.4 albertel 142: next if (!defined($slots{$slot_name}) ||
143: !ref($slots{$slot_name}));
144: &Apache::lonxml::debug(time." $slot_name ".
145: $slots{$slot_name}->{'starttime'}." -- ".
146: $slots{$slot_name}->{'startreserve'});
1.7 albertel 147: if ($slots{$slot_name}->{'endtime'} > time &&
1.4 albertel 148: $slots{$slot_name}->{'startreserve'} < time) {
1.7 albertel 149: # between start of reservation times and end of slot
1.43 albertel 150: if ($mode eq 'allslots') {
151: push(@got,$slot_name);
152: } else {
153: return($slot_name, $slots{$slot_name});
154: }
1.4 albertel 155: }
156: }
1.43 albertel 157: if ($mode eq 'allslots' && @got) {
158: return @got;
159: }
1.4 albertel 160: return (undef,undef);
161: }
162:
1.48 albertel 163: sub get_consumed_uniqueperiods {
164: my ($slots) = @_;
165: my $navmap=Apache::lonnavmaps::navmap->new;
166: my @problems = $navmap->retrieveResources(undef,
167: sub { $_[0]->is_problem() },1,0);
168: my %used_slots;
169: foreach my $problem (@problems) {
170: my $symb = $problem->symb();
171: my $student = &Apache::lonnet::EXT("resource.0.availablestudent",
172: $symb, $env{'user.domain'},
173: $env{'user.name'});
174: my $course = &Apache::lonnet::EXT("resource.0.available",
175: $symb, $env{'user.domain'},
176: $env{'user.name'});
177: if (&network_error($student) || &network_error($course)) {
178: return 'error: Unable to determine current status';
179: }
180: foreach my $slot (split(/:/,$student), split(/:/, $course)) {
181: $used_slots{$slot}=1;
182: }
183: }
1.43 albertel 184:
185: if (!ref($slots)) {
1.48 albertel 186: my ($cnum,$cdom)=&get_course();
187: my %slots=&Apache::lonnet::get('slots', [keys(%used_slots)], $cdom, $cnum);
188: if (&network_error(%slots)) {
189: return 'error: Unable to determine current status';
190: }
1.43 albertel 191: $slots = \%slots;
192: }
1.41 albertel 193:
1.48 albertel 194: my %consumed_uniqueperiods;
195: foreach my $slot_name (keys(%used_slots)) {
1.43 albertel 196: next if (!defined($slots->{$slot_name}) ||
197: !ref($slots->{$slot_name}));
1.48 albertel 198:
1.43 albertel 199: next if (!defined($slots->{$slot_name}{'uniqueperiod'}) ||
200: !ref($slots->{$slot_name}{'uniqueperiod'}));
1.48 albertel 201: $consumed_uniqueperiods{$slot_name} =
202: $slots->{$slot_name}{'uniqueperiod'};
203: }
204: return \%consumed_uniqueperiods;
205: }
206:
207: sub check_for_conflict {
208: my ($symb,$new_slot_name,$new_slot,$slots,$consumed_uniqueperiods)=@_;
209:
210: if (!defined($new_slot->{'uniqueperiod'})) { return undef; }
211:
212: if (!ref($consumed_uniqueperiods)) {
213: $consumed_uniqueperiods = &get_consumed_uniqueperiods($slots);
214: if (&network_error(%$consumed_uniqueperiods)) {
215: return 'error: Unable to determine current status';
216: }
217: }
218:
219: my ($new_uniq_start,$new_uniq_end) = @{$new_slot->{'uniqueperiod'}};
220: foreach my $slot_name (keys(%$consumed_uniqueperiods)) {
221: my ($start,$end)=@{$consumed_uniqueperiods->{$slot_name}};
1.43 albertel 222: if (!
223: ($start < $new_uniq_start && $end < $new_uniq_start) ||
224: ($start > $new_uniq_end && $end > $new_uniq_end )) {
1.5 albertel 225: return $slot_name;
226: }
227: }
228: return undef;
229:
230: }
231:
1.40 albertel 232: sub network_error {
233: my ($result) = @_;
234: if ($result =~ /^(con_lost|no_such_host|error: [^2])/) {
235: return 1;
236: }
237: return 0;
238: }
239:
1.2 albertel 240: sub make_reservation {
241: my ($slot_name,$slot,$symb)=@_;
1.3 albertel 242:
243: my ($cnum,$cdom)=&get_course();
244:
245: my $value=&Apache::lonnet::EXT("resource.0.availablestudent",$symb,
246: $env{'user.domain'},$env{'user.name'});
247: &Apache::lonxml::debug("value is $value<br />");
1.40 albertel 248: if (&network_error($value)) {
249: return 'error: Unable to determine current status';
250: }
251:
1.3 albertel 252: foreach my $other_slot (split(/:/, $value)) {
253: if ($other_slot eq $slot_name) {
254: my %consumed=&Apache::lonnet::dump('slot_reservations', $cdom,
255: $cnum, "^$slot_name\0");
1.40 albertel 256: if (&network_error($value)) {
257: return 'error: Unable to determine current status';
258: }
1.3 albertel 259: my $me=$env{'user.name'}.'@'.$env{'user.domain'};
260: foreach my $key (keys(%consumed)) {
261: if ($consumed{$key}->{'name'} eq $me) {
262: my $num=(split('\0',$key))[1];
263: return -$num;
264: }
265: }
266: }
267: }
268:
1.2 albertel 269: my $max=$slot->{'maxspace'};
1.3 albertel 270: if (!defined($max)) { $max=99999; }
1.2 albertel 271:
272: my (@ids)=&get_reservation_ids($slot_name);
1.40 albertel 273: if (&network_error(@ids)) {
274: return 'error: Unable to determine current status';
275: }
1.2 albertel 276: my $last=0;
277: foreach my $id (@ids) {
278: my $num=(split('\0',$id))[1];
279: if ($num > $last) { $last=$num; }
280: }
281:
282: my $wanted=$last+1;
1.3 albertel 283: &Apache::lonxml::debug("wanted $wanted<br />");
1.7 albertel 284: if (scalar(@ids) >= $max) {
1.2 albertel 285: # full up
1.7 albertel 286: return undef;
1.2 albertel 287: }
288:
289: my %reservation=('name' => $env{'user.name'}.'@'.$env{'user.domain'},
290: 'timestamp' => time,
291: 'symb' => $symb);
292:
293: my $success=&Apache::lonnet::newput('slot_reservations',
294: {"$slot_name\0$wanted" =>
295: \%reservation},
1.3 albertel 296: $cdom, $cnum);
297:
1.2 albertel 298: if ($success eq 'ok') {
1.3 albertel 299: my $new_value=$slot_name;
300: if ($value) {
301: $new_value=$value.':'.$new_value;
302: }
303: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
304: '0_availablestudent',
305: 1, $new_value, 'string',
306: $env{'user.name'},
307: $env{'user.domain'});
308: &Apache::lonxml::debug("hrrm $result");
1.2 albertel 309: return $wanted;
310: }
1.3 albertel 311:
1.2 albertel 312: # someone else got it
1.3 albertel 313: return undef;
314: }
315:
1.33 albertel 316: sub remove_registration {
317: my ($r) = @_;
318: my $name = &Apache::loncommon::plainname($env{'form.uname'},
319: $env{'form.udom'});
320:
321: my $title = &Apache::lonnet::gettitle($env{'form.symb'});
322:
323: my $hidden_input;
324: foreach my $parm ('uname','udom','slotname','entry','symb') {
325: $hidden_input .=
326: '<input type="hidden" name="'.$parm.'" value="'
327: .&HTML::Entities::encode($env{'form.'.$parm},'"<>&\'').'" />'."\n";
328: }
329: $r->print(<<"END_CONFIRM");
330: <p> Remove $name from slot $env{'form.slotname'} for $title</p>
331: <form action="/adm/slotrequest" method="POST">
332: <input type="hidden" name="command" value="release" />
333: $hidden_input
334: <input type="submit" name="Yes" value="yes" />
335: </form>
336: <form action="/adm/slotrequest" method="POST">
337: <input type="hidden" name="command" value="showslots" />
338: <input type="submit" name="No" value="no" />
339: </form>
340: END_CONFIRM
341:
342: }
343:
1.5 albertel 344: sub release_slot {
1.33 albertel 345: my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
1.6 albertel 346:
347: if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
348: my ($cnum,$cdom)=&get_course();
349:
1.33 albertel 350: my ($uname,$udom) = ($env{'user.name'}, $env{'user.domain'});
351: if ($mgr eq 'F'
352: && defined($env{'form.uname'}) && defined($env{'form.udom'})) {
353: ($uname,$udom) = ($env{'form.uname'}, $env{'form.udom'});
354: }
355:
356: if ($mgr eq 'F'
357: && defined($env{'form.symb'})) {
358: $symb = $env{'form.symb'};
359: }
1.39 albertel 360: my %slot=&Apache::lonnet::get_slot($slot_name);
361: my $description=&get_description($env{'form.slotname'},\%slot);
1.33 albertel 362:
1.39 albertel 363: if ($mgr ne 'F') {
1.43 albertel 364: if ($slot{'starttime'} < time) {
365: $r->print("<p>Not allowed to release Reservation: $description, as it has already ended. </p>");
1.42 albertel 366: &return_link($r);
1.39 albertel 367: return 0;
368: }
369: }
1.5 albertel 370: # get parameter string, check for existance, rebuild string with the slot
1.6 albertel 371: my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
1.33 albertel 372: $symb,$udom,$uname));
373:
1.6 albertel 374: my @new_slots;
375: foreach my $exist_slot (@slots) {
376: if ($exist_slot eq $slot_name) { next; }
377: push(@new_slots,$exist_slot);
378: }
379: my $new_param = join(':',@new_slots);
1.5 albertel 380:
381: # get slot reservations, check if user has one, if so remove reservation
1.6 albertel 382: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
383: "^$slot_name\0");
384: foreach my $entry (keys(%consumed)) {
1.33 albertel 385: if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) {
1.6 albertel 386: &Apache::lonnet::del('slot_reservations',[$entry],
387: $cdom,$cnum);
388: }
389: }
1.33 albertel 390:
1.5 albertel 391: # store new parameter string
1.6 albertel 392: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
393: '0_availablestudent',
394: 1, $new_param, 'string',
1.33 albertel 395: $uname,$udom);
1.6 albertel 396: $r->print("<p>Released Reservation: $description</p>");
1.33 albertel 397: if ($mgr eq 'F') {
398: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
399: &mt('Return to slot list').'</a></p>');
400: }
1.42 albertel 401: if (!$inhibit_return_link) { &return_link($r); }
1.6 albertel 402: return 1;
1.5 albertel 403: }
404:
1.34 albertel 405: sub delete_slot {
406: my ($r)=@_;
407:
408: my $slot_name = $env{'form.slotname'};
409: my %slot=&Apache::lonnet::get_slot($slot_name);
410:
411: my ($cnum,$cdom)=&get_course();
412: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
413: "^$slot_name\0");
1.38 albertel 414: my ($tmp) = %consumed;
415: if ($tmp =~ /error: 2/) { undef(%consumed); }
1.34 albertel 416:
417: if (%slot && !%consumed) {
418: $slot{'type'} = 'deleted';
419: my $ret = &Apache::lonnet::cput('slots', {$slot_name => \%slot},
420: $cdom, $cnum);
421: if ($ret eq 'ok') {
422: $r->print("<p>Slot <tt>$slot_name</tt> marked as deleted.</p>");
423: } else {
424: $r->print("<p> An error ($ret) occurse when attempting to delete Slot <tt>$slot_name</tt>.</p>");
425: }
426: } else {
427: if (%consumed) {
428: $r->print("<p>Slot <tt>$slot_name</tt> has active reservations.</p>");
429: } else {
430: $r->print("<p>Slot <tt>$slot_name</tt> does not exist.</p>");
431: }
432: }
433: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
434: &mt('Return to slot list').'</a></p>');
1.42 albertel 435: &return_link($r);
1.34 albertel 436: }
437:
1.40 albertel 438: sub return_link {
439: my ($r) = @_;
440: $r->print('<p><a href="/adm/flip?postdata=return:">'.
441: &mt('Return to last resource').'</a></p>');
442: }
443:
1.3 albertel 444: sub get_slot {
445: my ($r,$symb)=@_;
446:
1.43 albertel 447: my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
448: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'},\%slot);
1.40 albertel 449:
450: if ($slot_name =~ /^error: (.*)/) {
451: $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
452: &return_link($r);
453: return;
454: }
1.5 albertel 455: if ($slot_name) {
456: my %slot=&Apache::lonnet::get_slot($slot_name);
1.6 albertel 457: my $description1=&get_description($slot_name,\%slot);
458: %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
459: my $description2=&get_description($env{'form.slotname'},\%slot);
460: $r->print("<p>Already have a reservation: $description1</p>");
1.7 albertel 461: if ($slot_name ne $env{'form.slotname'}) {
462: $r->print(<<STUFF);
1.6 albertel 463: <form method="POST" action="/adm/slotrequest">
464: <input type="hidden" name="symb" value="$env{'form.symb'}" />
465: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
466: <input type="hidden" name="releaseslot" value="$slot_name" />
467: <input type="hidden" name="command" value="change" />
468: STUFF
1.7 albertel 469: $r->print("<p>You can either ");
470: $r->print(<<STUFF);
1.6 albertel 471: <input type="submit" name="change" value="Change" />
472: STUFF
1.7 albertel 473: $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
474: $description2.
1.40 albertel 475: '</b> <br />or </p>');
476: &return_link($r);
1.7 albertel 477: $r->print(<<STUFF);
1.6 albertel 478: </form>
479: STUFF
1.7 albertel 480: } else {
1.40 albertel 481: &return_link($r);
1.7 albertel 482: }
1.5 albertel 483: return;
484: }
1.45 albertel 485:
1.3 albertel 486: my $reserved=&make_reservation($env{'form.slotname'},
487: \%slot,$symb);
488: my $description=&get_description($env{'form.slotname'},\%slot);
1.7 albertel 489: if (defined($reserved)) {
1.40 albertel 490: if ($slot_name =~ /^error: (.*)/) {
491: $r->print("<p>An error occured while attempting to make a reservation. ($1)</p>");
492: } elsif ($reserved > -1) {
1.7 albertel 493: $r->print("<p>Success: $description</p>");
494: } elsif ($reserved < 0) {
495: $r->print("<p>Already reserved: $description</p>");
496: }
1.40 albertel 497: &return_link($r);
498: return;
1.3 albertel 499: }
500:
1.7 albertel 501: my %lt=('request'=>"Availibility list",
1.3 albertel 502: 'try' =>'Try again');
503: %lt=&Apache::lonlocal::texthash(%lt);
504:
505: $r->print(<<STUFF);
506: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
507: <p>
508: <form method="POST" action="/adm/slotrequest">
509: <input type="submit" name="Try Again" value="$lt{'try'}" />
510: <input type="hidden" name="symb" value="$env{'form.symb'}" />
511: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
512: <input type="hidden" name="command" value="get" />
513: </form>
514: ?
515: </p>
516: <p>
517: or
518: <form method="POST" action="/adm/slotrequest">
519: <input type="hidden" name="symb" value="$env{'form.symb'}" />
520: <input type="submit" name="requestattempt" value="$lt{'request'}" />
521: </form>
522: </p>
523: or
524: STUFF
1.42 albertel 525:
526: &return_link($r);
1.3 albertel 527: return;
528: }
529:
530: sub allowed_slot {
1.48 albertel 531: my ($slot_name,$slot,$symb,$slots,$consumed_uniqueperiods)=@_;
1.3 albertel 532: #already started
533: if ($slot->{'starttime'} < time) {
1.5 albertel 534: # all open slot to be schedulable
535: #return 0;
1.3 albertel 536: }
1.5 albertel 537: &Apache::lonxml::debug("$slot_name starttime good");
1.3 albertel 538: #already ended
539: if ($slot->{'endtime'} < time) {
540: return 0;
541: }
1.5 albertel 542: &Apache::lonxml::debug("$slot_name endtime good");
1.3 albertel 543: # not allowed to pick this one
544: if (defined($slot->{'type'})
545: && $slot->{'type'} ne 'schedulable_student') {
546: return 0;
547: }
1.5 albertel 548: &Apache::lonxml::debug("$slot_name type good");
1.3 albertel 549: # not allowed for this resource
550: if (defined($slot->{'symb'})
551: && $slot->{'symb'} ne $symb) {
552: return 0;
553: }
1.48.2.1! albertel 554: # reserve time not yet started
! 555: if ($slot->{'startreserve'} > time) {
! 556: return 0;
! 557: }
! 558: &Apache::lonxml::debug("$slot_name reserve good");
! 559:
1.48 albertel 560: my $conflict = &check_for_conflict($symb,$slot_name,$slot,$slots,
561: $consumed_uniqueperiods);
1.44 albertel 562: if ($conflict) {
563: if ($slots->{$conflict}{'starttime'} < time) {
564: return 0;
565: }
566: }
1.5 albertel 567: &Apache::lonxml::debug("$slot_name symb good");
1.3 albertel 568: return 1;
1.2 albertel 569: }
570:
1.3 albertel 571: sub get_description {
572: my ($slot_name,$slot)=@_;
573: my $description=$slot->{'description'};
574: if (!defined($description)) {
1.4 albertel 575: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3 albertel 576: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
577: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
578: }
579: return $description;
580: }
1.2 albertel 581:
582: sub show_choices {
583: my ($r,$symb)=@_;
584:
585: my ($cnum,$cdom)=&get_course();
586: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.48 albertel 587: my $consumed_uniqueperiods = &get_consumed_uniqueperiods(\%slots);
1.3 albertel 588: my $available;
1.2 albertel 589: $r->print('<table border="1">');
1.5 albertel 590: &Apache::lonxml::debug("Checking Slots");
1.43 albertel 591: my @got_slots=&check_for_reservation($symb,'allslots');
1.2 albertel 592: foreach my $slot (sort
593: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
594: (keys(%slots))) {
1.5 albertel 595:
596: &Apache::lonxml::debug("Checking Slot $slot");
1.48 albertel 597: next if (!&allowed_slot($slot,$slots{$slot},undef,\%slots,
598: $consumed_uniqueperiods));
1.3 albertel 599:
600: $available++;
601:
602: my $description=&get_description($slot,$slots{$slot});
1.2 albertel 603:
604: my $form=&mt('Unavailable');
1.43 albertel 605: if ((grep(/^\Q$slot\E$/,@got_slots)) ||
1.7 albertel 606: &space_available($slot,$slots{$slot},$symb)) {
1.5 albertel 607: my $text=&mt('Select');
608: my $command='get';
1.43 albertel 609: if (grep(/^\Q$slot\E$/,@got_slots)) {
1.5 albertel 610: $text=&mt('Free Reservation');
611: $command='release';
1.43 albertel 612: } else {
613: my $conflict = &check_for_conflict($symb,$slot,$slots{$slot},
1.48 albertel 614: \%slots,
615: $consumed_uniqueperiods);
1.43 albertel 616: if ($conflict) {
617: $text=&mt('Change Reservation');
618: $command='get';
619: }
1.5 albertel 620: }
1.3 albertel 621: my $escsymb=&Apache::lonnet::escape($symb);
1.2 albertel 622: $form=<<STUFF;
1.3 albertel 623: <form method="POST" action="/adm/slotrequest">
1.5 albertel 624: <input type="submit" name="Select" value="$text" />
1.3 albertel 625: <input type="hidden" name="symb" value="$escsymb" />
626: <input type="hidden" name="slotname" value="$slot" />
1.5 albertel 627: <input type="hidden" name="command" value="$command" />
1.2 albertel 628: </form>
629: STUFF
630: }
631: $r->print(<<STUFF);
632: <tr>
633: <td>$form</td>
634: <td>$description</td>
635: </tr>
636: STUFF
637: }
1.3 albertel 638:
639: if (!$available) {
1.5 albertel 640: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
1.3 albertel 641: &mt('Return to last resource').'</a></td></tr>');
642: }
1.2 albertel 643: $r->print('</table>');
644: }
645:
1.30 albertel 646: sub to_show {
1.35 albertel 647: my ($slot,$when,$deleted) = @_;
1.30 albertel 648: my $time=time;
649: my $week=60*60*24*7;
1.35 albertel 650: if ($deleted eq 'hide' && $slot->{'type'} eq 'deleted') {
651: return 0;
652: }
653: if ($when eq 'any') {
654: return 1;
655: } elsif ($when eq 'now') {
1.30 albertel 656: if ($time > $slot->{'starttime'} &&
657: $time < $slot->{'endtime'}) {
658: return 1;
659: }
660: return 0;
661: } elsif ($when eq 'nextweek') {
662: if ( ($time < $slot->{'starttime'} &&
663: ($time+$week) > $slot->{'starttime'})
664: ||
665: ($time < $slot->{'endtime'} &&
666: ($time+$week) > $slot->{'endtime'}) ) {
667: return 1;
668: }
669: return 0;
670: } elsif ($when eq 'lastweek') {
671: if ( ($time > $slot->{'starttime'} &&
672: ($time-$week) < $slot->{'starttime'})
673: ||
674: ($time > $slot->{'endtime'} &&
675: ($time-$week) < $slot->{'endtime'}) ) {
676: return 1;
677: }
678: return 0;
679: } elsif ($when eq 'willopen') {
680: if ($time < $slot->{'starttime'}) {
681: return 1;
682: }
683: return 0;
684: } elsif ($when eq 'wereopen') {
685: if ($time > $slot->{'endtime'}) {
686: return 1;
687: }
688: return 0;
689: }
690:
691: return 1;
692: }
693:
1.33 albertel 694: sub remove_link {
695: my ($slotname,$entry,$uname,$udom,$symb) = @_;
696:
697: $slotname = &Apache::lonnet::escape($slotname);
698: $entry = &Apache::lonnet::escape($entry);
699: $uname = &Apache::lonnet::escape($uname);
700: $udom = &Apache::lonnet::escape($udom);
701: $symb = &Apache::lonnet::escape($symb);
702:
703: my $remove= &mt('Remove');
704:
705: return <<"END_LINK";
706: <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
707: >($remove)</a>
708: END_LINK
709:
710: }
711:
1.5 albertel 712: sub show_table {
1.19 albertel 713: my ($r,$mgr)=@_;
1.5 albertel 714:
715: my ($cnum,$cdom)=&get_course();
716: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19 albertel 717: if ( (keys(%slots))[0] =~ /^error: 2 /) {
718: undef(%slots);
719: }
1.5 albertel 720: my $available;
1.14 albertel 721: if ($mgr eq 'F') {
1.30 albertel 722: $r->print('<div>');
1.14 albertel 723: $r->print('<form method="POST" action="/adm/slotrequest">
724: <input type="hidden" name="command" value="uploadstart" />
725: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
726: </form>');
1.28 albertel 727: $r->print('<form method="POST" action="/adm/helper/newslot.helper">
728: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
729: </form>');
1.30 albertel 730: $r->print('</div>');
1.14 albertel 731: }
1.29 albertel 732:
1.35 albertel 733: my %Saveable_Parameters = ('show' => 'array',
734: 'when' => 'scalar',
735: 'order' => 'scalar',
736: 'deleted' => 'scalar',
737: );
1.46 albertel 738: &Apache::loncommon::store_course_settings('slotrequest',
739: \%Saveable_Parameters);
740: &Apache::loncommon::restore_course_settings('slotrequest',
741: \%Saveable_Parameters);
742: &Apache::grades::init_perm();
743: my ($classlist,$section,$fullname)=&Apache::grades::getclasslist('all');
744: &Apache::grades::reset_perm();
1.29 albertel 745:
1.30 albertel 746: my %show_fields=&Apache::lonlocal::texthash(
1.29 albertel 747: 'name' => 'Slot Name',
748: 'description' => 'Description',
749: 'type' => 'Type',
750: 'starttime' => 'Start time',
751: 'endtime' => 'End Time',
752: 'startreserve' => 'Time students can start reserving',
753: 'secret' => 'Secret Word',
1.37 raeburn 754: 'maxspace' => 'Maximum # of students',
1.29 albertel 755: 'ip' => 'IP or DNS restrictions',
756: 'symb' => 'Resource slot is restricted to.',
757: 'uniqueperiod' => 'Period of time slot is unique',
1.47 albertel 758: 'scheduled' => 'Scheduled Students',
1.29 albertel 759: 'proctor' => 'List of proctors');
1.30 albertel 760: my @show_order=('name','description','type','starttime','endtime',
1.29 albertel 761: 'startreserve','secret','maxspace','ip','symb',
1.47 albertel 762: 'uniqueperiod','scheduled','proctor');
1.30 albertel 763: my @show =
1.29 albertel 764: (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30 albertel 765: : keys(%show_fields);
766: my %show = map { $_ => 1 } (@show);
767:
768: my %when_fields=&Apache::lonlocal::texthash(
1.35 albertel 769: 'now' => 'Open now',
1.30 albertel 770: 'nextweek' => 'Open within the next week',
771: 'lastweek' => 'Were open last week',
772: 'willopen' => 'Will open later',
1.35 albertel 773: 'wereopen' => 'Were open',
774: 'any' => 'Anytime',
775: );
776: my @when_order=('any','now','nextweek','lastweek','willopen','wereopen');
1.30 albertel 777: $when_fields{'select_form_order'} = \@when_order;
778: my $when = (exists($env{'form.when'})) ? $env{'form.when'}
779: : 'now';
1.29 albertel 780:
1.46 albertel 781: my %stu_display_fields=
782: &Apache::lonlocal::texthash('username' => 'User name',
783: 'fullname' => 'Full name',
784: );
785: my @stu_display_order=('fullname','username');
786: my @stu_display =
787: (exists($env{'form.studisplay'})) ? &Apache::loncommon::get_env_multiple('form.studisplay')
788: : keys(%stu_display_fields);
789: my %stu_display = map { $_ => 1 } (@stu_display);
790:
1.35 albertel 791: my $hide_radio =
792: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'hide');
793: my $show_radio =
794: &Apache::lonhtmlcommon::radio('deleted',$env{'form.deleted'},'show');
795:
1.29 albertel 796: $r->print('<form method="POST" action="/adm/slotrequest">
1.30 albertel 797: <input type="hidden" name="command" value="showslots" />');
798: $r->print('<div>');
1.35 albertel 799: $r->print('<table class="inline">
800: <tr><th>'.&mt('Show').'</th>
1.46 albertel 801: <th>'.&mt('Student Display').'</th>
1.35 albertel 802: <th>'.&mt('Open').'</th>
803: <th>'.&mt('Options').'</th>
804: </tr>
805: <tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
806: '</td>
1.46 albertel 807: <td>
808: '.&Apache::loncommon::multiple_select_form('studisplay',\@stu_display,
809: 6,\%stu_display_fields,
810: \@stu_display_order).'
811: </td>
1.35 albertel 812: <td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
813: '</td>
814: <td>
815: <table>
816: <tr>
817: <td rowspan="2">Deleted slots:</td>
818: <td><label>'.$show_radio.'Show</label></td>
819: </tr>
820: <tr>
821: <td><label>'.$hide_radio.'Hide</label></td>
822: </tr>
823: </table>
824: </td>
825: </tr>
826: </table>');
1.30 albertel 827: $r->print('</div>');
828: $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21 albertel 829: my $linkstart='<a href="/adm/slotrequest?command=showslots&order=';
1.30 albertel 830: $r->print('<table class="thinborder">
1.10 albertel 831: <tr>
1.29 albertel 832: <th></th>');
1.30 albertel 833: foreach my $which (@show_order) {
834: if ($which ne 'proctor' && exists($show{$which})) {
835: $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
1.29 albertel 836: }
837: }
838:
1.21 albertel 839: my %name_cache;
840: my $slotsort = sub {
1.29 albertel 841: if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
1.21 albertel 842: if (lc($slots{$a}->{$env{'form.order'}})
843: ne lc($slots{$b}->{$env{'form.order'}})) {
844: return (lc($slots{$a}->{$env{'form.order'}})
845: cmp lc($slots{$b}->{$env{'form.order'}}));
846: }
1.23 albertel 847: } elsif ($env{'form.order'} eq 'name') {
848: if (lc($a) cmp lc($b)) {
849: return lc($a) cmp lc($b);
850: }
1.29 albertel 851: } elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21 albertel 852:
853: if ($slots{$a}->{'uniqueperiod'}[0]
854: ne $slots{$b}->{'uniqueperiod'}[0]) {
855: return ($slots{$a}->{'uniqueperiod'}[0]
856: cmp $slots{$b}->{'uniqueperiod'}[0]);
857: }
858: if ($slots{$a}->{'uniqueperiod'}[1]
859: ne $slots{$b}->{'uniqueperiod'}[1]) {
860: return ($slots{$a}->{'uniqueperiod'}[1]
861: cmp $slots{$b}->{'uniqueperiod'}[1]);
862: }
863: }
864: return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
865: };
866: foreach my $slot (sort $slotsort (keys(%slots))) {
1.35 albertel 867: if (!&to_show($slots{$slot},$when,$env{'form.deleted'})) { next; }
1.5 albertel 868: if (defined($slots{$slot}->{'type'})
869: && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13 albertel 870: #next;
1.5 albertel 871: }
872: my $description=&get_description($slot,$slots{$slot});
873: my $ids;
1.47 albertel 874: if (exists($show{'scheduled'})) {
875: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
876: "^$slot\0");
877: my ($tmp)=%consumed;
878: if ($tmp !~ /^error: /) {
879: foreach my $entry (sort(keys(%consumed))) {
880: my (undef,$id)=split("\0",$entry);
881: my ($uname,$udom) = split('@',$consumed{$entry}{'name'});
882: $ids.= '<nobr>';
883: foreach my $item (@stu_display_order) {
884: if ($stu_display{$item}) {
885: if ($item eq 'fullname') {
886: $ids.=$fullname->{"$uname:$udom"}.' ';
887: } elsif ($item eq 'username') {
888: $ids.="<tt>$uname\@$udom</tt> ";
889: }
1.46 albertel 890: }
891: }
1.47 albertel 892: $ids.=&remove_link($slot,$entry,$uname,$udom,
893: $consumed{$entry}{'symb'}).'</nobr><br />';
1.46 albertel 894: }
1.38 albertel 895: }
1.5 albertel 896: }
1.33 albertel 897:
1.24 albertel 898: my $start=($slots{$slot}->{'starttime'}?
899: &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
900: my $end=($slots{$slot}->{'endtime'}?
901: &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
1.28 albertel 902: my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24 albertel 903: &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
904:
1.14 albertel 905: my $unique;
906: if (ref($slots{$slot}{'uniqueperiod'})) {
907: $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
908: localtime($slots{$slot}{'uniqueperiod'}[1]);
909: }
1.33 albertel 910:
1.29 albertel 911: my $title;
912: if (exists($slots{$slot}{'symb'})) {
913: my (undef,undef,$res)=
914: &Apache::lonnet::decode_symb($slots{$slot}{'symb'});
915: $res = &Apache::lonnet::clutter($res);
916: $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
917: $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
918: }
1.33 albertel 919:
1.29 albertel 920: my @proctors;
921: my $rowspan=1;
922: my $colspan=1;
1.30 albertel 923: if (exists($show{'proctor'})) {
1.29 albertel 924: $rowspan=2;
925: @proctors= map {
926: my ($uname,$udom)=split(/@/,$_);
927: my $fullname=$name_cache{$_};
928: if (!defined($fullname)) {
929: &Apache::lonnet::logthis("Gettign $uname $udom");
930: $fullname = &Apache::loncommon::plainname($uname,$udom);
931: $fullname =~s/\s/ /g;
932: $name_cache{$_} = $fullname;
933: }
934: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
935: } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
936: }
1.20 albertel 937: my $proctors=join(', ',@proctors);
1.14 albertel 938:
1.34 albertel 939: my $edit=(<<"EDITLINK");
1.31 albertel 940: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
941: EDITLINK
1.34 albertel 942:
943: my $delete=(<<"DELETELINK");
944: <a href="/adm/slotrequest?command=delete&slotname=$slot">Delete</a>
945: DELETELINK
946: if ($ids ne '') { undef($delete); }
947:
948: $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit $delete</td>\n");
1.30 albertel 949: if (exists($show{'name'})) {
1.29 albertel 950: $colspan++;$r->print("<td>$slot</td>");
951: }
1.33 albertel 952: if (exists($show{'description'})) {
953: $colspan++;$r->print("<td>$description</td>\n");
954: }
1.30 albertel 955: if (exists($show{'type'})) {
1.29 albertel 956: $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
957: }
1.30 albertel 958: if (exists($show{'starttime'})) {
1.29 albertel 959: $colspan++;$r->print("<td>$start</td>\n");
960: }
1.30 albertel 961: if (exists($show{'endtime'})) {
1.29 albertel 962: $colspan++;$r->print("<td>$end</td>\n");
963: }
1.30 albertel 964: if (exists($show{'startreserve'})) {
1.29 albertel 965: $colspan++;$r->print("<td>$start_reserve</td>\n");
966: }
1.30 albertel 967: if (exists($show{'secret'})) {
1.29 albertel 968: $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
969: }
1.30 albertel 970: if (exists($show{'maxspace'})) {
1.29 albertel 971: $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
972: }
1.30 albertel 973: if (exists($show{'ip'})) {
1.29 albertel 974: $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
975: }
1.30 albertel 976: if (exists($show{'symb'})) {
1.29 albertel 977: $colspan++;$r->print("<td>$title</td>\n");
978: }
1.30 albertel 979: if (exists($show{'uniqueperiod'})) {
1.29 albertel 980: $colspan++;$r->print("<td>$unique</td>\n");
981: }
1.47 albertel 982: if (exists($show{'scheduled'})) {
983: $colspan++;$r->print("<td>$ids</td>\n</tr>\n");
984: }
1.30 albertel 985: if (exists($show{'proctor'})) {
1.29 albertel 986: $r->print(<<STUFF);
1.21 albertel 987: <tr>
1.29 albertel 988: <td colspan="$colspan">$proctors</td>
1.21 albertel 989: </tr>
1.5 albertel 990: STUFF
1.29 albertel 991: }
1.5 albertel 992: }
993: $r->print('</table>');
994: }
995:
1.14 albertel 996: sub upload_start {
1.19 albertel 997: my ($r)=@_;
1.14 albertel 998: $r->print(&Apache::grades::checkforfile_js());
999: my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
1000: $result.=' <b>'.
1001: &mt('Specify a file containing the slot definitions.').
1002: '</b></td></tr>'."\n";
1003: $result.='<tr bgcolor=#ffffe6><td>'."\n";
1004: my $upfile_select=&Apache::loncommon::upfile_select_html();
1005: my $ignore=&mt('Ignore First Line');
1006: $result.=<<ENDUPFORM;
1007: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1008: <input type="hidden" name="command" value="csvuploadmap" />
1009: $upfile_select
1010: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
1011: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
1012: </form>
1013: ENDUPFORM
1014: $result.='</td></tr></table>'."\n";
1015: $result.='</td></tr></table>'."\n";
1016: $r->print($result);
1017: }
1018:
1019: sub csvuploadmap_header {
1.19 albertel 1020: my ($r,$datatoken,$distotal)= @_;
1.14 albertel 1021: my $javascript;
1022: if ($env{'form.upfile_associate'} eq 'reverse') {
1023: $javascript=&csvupload_javascript_reverse_associate();
1024: } else {
1025: $javascript=&csvupload_javascript_forward_associate();
1026: }
1027:
1028: my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
1029: my $ignore=&mt('Ignore First Line');
1030: $r->print(<<ENDPICK);
1031: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
1032: <h3>Identify fields</h3>
1033: Total number of records found in file: $distotal <hr />
1034: Enter as many fields as you can. The system will inform you and bring you back
1035: to this page if the data selected is insufficient to create the slots.<hr />
1036: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
1037: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
1038: <input type="hidden" name="associate" value="" />
1039: <input type="hidden" name="datatoken" value="$datatoken" />
1040: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
1041: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
1042: <input type="hidden" name="upfile_associate"
1043: value="$env{'form.upfile_associate'}" />
1044: <input type="hidden" name="command" value="csvuploadassign" />
1045: <hr />
1046: <script type="text/javascript" language="Javascript">
1047: $javascript
1048: </script>
1049: ENDPICK
1050: return '';
1051:
1052: }
1053:
1054: sub csvuploadmap_footer {
1055: my ($request,$i,$keyfields) =@_;
1056: $request->print(<<ENDPICK);
1057: </table>
1058: <input type="hidden" name="nfields" value="$i" />
1059: <input type="hidden" name="keyfields" value="$keyfields" />
1060: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
1061: </form>
1062: ENDPICK
1063: }
1064:
1065: sub csvupload_javascript_reverse_associate {
1066: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1067: return(<<ENDPICK);
1068: function verify(vf) {
1069: var foundstart=0;
1070: var foundend=0;
1071: var foundname=0;
1072: var foundtype=0;
1073: for (i=0;i<=vf.nfields.value;i++) {
1074: tw=eval('vf.f'+i+'.selectedIndex');
1075: if (i==0 && tw!=0) { foundname=1; }
1076: if (i==1 && tw!=0) { foundtype=1; }
1077: if (i==2 && tw!=0) { foundstat=1; }
1078: if (i==3 && tw!=0) { foundend=1; }
1079: }
1080: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1081: alert('$error1');
1082: return;
1083: }
1084: vf.submit();
1085: }
1086: function flip(vf,tf) {
1087: }
1088: ENDPICK
1089: }
1090:
1091: sub csvupload_javascript_forward_associate {
1092: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
1093: return(<<ENDPICK);
1094: function verify(vf) {
1095: var foundstart=0;
1096: var foundend=0;
1097: var foundname=0;
1098: var foundtype=0;
1099: for (i=0;i<=vf.nfields.value;i++) {
1100: tw=eval('vf.f'+i+'.selectedIndex');
1101: if (tw==1) { foundname=1; }
1102: if (tw==2) { foundtype=1; }
1103: if (tw==3) { foundstat=1; }
1104: if (tw==4) { foundend=1; }
1105: }
1106: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
1107: alert('$error1');
1108: return;
1109: }
1110: vf.submit();
1111: }
1112: function flip(vf,tf) {
1113: }
1114: ENDPICK
1115: }
1116:
1117: sub csv_upload_map {
1.19 albertel 1118: my ($r)= @_;
1.14 albertel 1119:
1120: my $datatoken;
1121: if (!$env{'form.datatoken'}) {
1122: $datatoken=&Apache::loncommon::upfile_store($r);
1123: } else {
1124: $datatoken=$env{'form.datatoken'};
1125: &Apache::loncommon::load_tmp_file($r);
1126: }
1127: my @records=&Apache::loncommon::upfile_record_sep();
1128: if ($env{'form.noFirstLine'}) { shift(@records); }
1.19 albertel 1129: &csvuploadmap_header($r,$datatoken,$#records+1);
1.14 albertel 1130: my ($i,$keyfields);
1131: if (@records) {
1132: my @fields=&csvupload_fields();
1133:
1134: if ($env{'form.upfile_associate'} eq 'reverse') {
1135: &Apache::loncommon::csv_print_samples($r,\@records);
1136: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
1137: \@fields);
1138: foreach (@fields) { $keyfields.=$_->[0].','; }
1139: chop($keyfields);
1140: } else {
1141: unshift(@fields,['none','']);
1142: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
1143: \@fields);
1144: my %sone=&Apache::loncommon::record_sep($records[0]);
1145: $keyfields=join(',',sort(keys(%sone)));
1146: }
1147: }
1148: &csvuploadmap_footer($r,$i,$keyfields);
1149:
1150: return '';
1151: }
1152:
1153: sub csvupload_fields {
1154: return (['name','Slot name'],
1155: ['type','Type of slot'],
1156: ['starttime','Start Time of slot'],
1157: ['endtime','End Time of slot'],
1.15 albertel 1158: ['startreserve','Reservation Start Time'],
1.14 albertel 1159: ['ip','IP or DNS restriction'],
1160: ['proctor','List of proctor ids'],
1161: ['description','Slot Description'],
1162: ['maxspace','Maximum number of reservations'],
1163: ['symb','Resource Restriction'],
1164: ['uniqueperiod','Date range of slot exclusion'],
1165: ['secret','Secret word proctor uses to validate']);
1166: }
1167:
1168: sub csv_upload_assign {
1.19 albertel 1169: my ($r,$mgr)= @_;
1.14 albertel 1170: &Apache::loncommon::load_tmp_file($r);
1171: my @slotdata = &Apache::loncommon::upfile_record_sep();
1172: if ($env{'form.noFirstLine'}) { shift(@slotdata); }
1173: my %fields=&Apache::grades::get_fields();
1174: $r->print('<h3>Creating Slots</h3>');
1175: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
1176: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1177: my $countdone=0;
1.31 albertel 1178: my @errors;
1.14 albertel 1179: foreach my $slot (@slotdata) {
1180: my %slot;
1181: my %entries=&Apache::loncommon::record_sep($slot);
1182: my $domain;
1183: my $name=$entries{$fields{'name'}};
1.31 albertel 1184: if ($name=~/^\s*$/) {
1185: push(@errors,"Did not create slot with no name");
1186: next;
1187: }
1188: if ($name=~/\s/) {
1189: push(@errors,"$name not created -- Name must not contain spaces");
1190: next;
1191: }
1192: if ($name=~/\W/) {
1193: push(@errors,"$name not created -- Name must contain only letters, numbers and _");
1194: next;
1195: }
1.14 albertel 1196: if ($entries{$fields{'type'}}) {
1197: $slot{'type'}=$entries{$fields{'type'}};
1198: } else {
1199: $slot{'type'}='preassigned';
1200: }
1.31 albertel 1201: if ($slot{'type'} ne 'preassigned' &&
1202: $slot{'type'} ne 'schedulable_student') {
1203: push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
1204: next;
1205: }
1.14 albertel 1206: if ($entries{$fields{'starttime'}}) {
1207: $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
1208: }
1209: if ($entries{$fields{'endtime'}}) {
1.16 albertel 1210: $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14 albertel 1211: }
1.23 albertel 1212: if ($entries{$fields{'startreserve'}}) {
1213: $slot{'startreserve'}=
1214: &UnixDate($entries{$fields{'startreserve'}},"%s");
1215: }
1.14 albertel 1216: foreach my $key ('ip','proctor','description','maxspace',
1217: 'secret','symb') {
1218: if ($entries{$fields{$key}}) {
1219: $slot{$key}=$entries{$fields{$key}};
1220: }
1221: }
1222: if ($entries{$fields{'uniqueperiod'}}) {
1223: my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
1224: my @times=(&UnixDate($start,"%s"),
1225: &UnixDate($end,"%s"));
1226: $slot{'uniqueperiod'}=\@times;
1227: }
1228:
1229: &Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
1230: $r->print('.');
1231: $r->rflush();
1232: $countdone++;
1233: }
1.31 albertel 1234: $r->print("<p>Created $countdone slots\n</p>");
1235: foreach my $error (@errors) {
1236: $r->print("<p>$error\n</p>");
1237: }
1.19 albertel 1238: &show_table($r,$mgr);
1.14 albertel 1239: return '';
1240: }
1241:
1.1 albertel 1242: sub handler {
1243: my $r=shift;
1244:
1.30 albertel 1245: &Apache::loncommon::content_type($r,'text/html');
1246: &Apache::loncommon::no_cache($r);
1247: if ($r->header_only()) {
1248: $r->send_http_header();
1249: return OK;
1250: }
1251:
1.8 albertel 1252: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28 albertel 1253:
1.12 albertel 1254: my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14 albertel 1255: my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28 albertel 1256: my $title='Requesting Another Worktime';
1257: if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
1258: $title = 'Managing Slots';
1259: }
1260: &start_page($r,$title);
1261:
1.8 albertel 1262: if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19 albertel 1263: &show_table($r,$mgr);
1.33 albertel 1264: } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
1265: &remove_registration($r);
1266: } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1267: &release_slot($r,undef,undef,undef,$mgr);
1.34 albertel 1268: } elsif ($env{'form.command'} eq 'delete' && $mgr eq 'F') {
1269: &delete_slot($r);
1.14 albertel 1270: } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19 albertel 1271: &upload_start($r);
1.14 albertel 1272: } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19 albertel 1273: &csv_upload_map($r);
1.14 albertel 1274: } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
1275: if ($env{'form.associate'} ne 'Reverse Association') {
1.19 albertel 1276: &csv_upload_assign($r,$mgr);
1.14 albertel 1277: } else {
1278: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
1279: $env{'form.upfile_associate'} = 'reverse';
1280: } else {
1281: $env{'form.upfile_associate'} = 'forward';
1282: }
1.19 albertel 1283: &csv_upload_map($r);
1.14 albertel 1284: }
1.8 albertel 1285: } else {
1.19 albertel 1286: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
1287: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1.36 albertel 1288: my $useslots = &Apache::lonnet::EXT("resource.0.useslots",$symb);
1289: if ($useslots ne 'resource') {
1.19 albertel 1290: &fail($r,'not_valid');
1291: return OK;
1292: }
1293: $env{'request.symb'}=$symb;
1.36 albertel 1294: my $type = ($res =~ /\.task$/) ? 'Task'
1295: : 'problem';
1296: my ($status) = &Apache::lonhomework::check_slot_access('0',$type);
1.11 albertel 1297: if ($status eq 'CAN_ANSWER' ||
1298: $status eq 'NEEDS_CHECKIN' ||
1299: $status eq 'WAITING_FOR_GRADE') {
1300: &fail($r,'not_allowed');
1301: return OK;
1302: }
1303: if ($env{'form.requestattempt'}) {
1304: &show_choices($r,$symb);
1305: } elsif ($env{'form.command'} eq 'release') {
1306: &release_slot($r,$symb);
1307: } elsif ($env{'form.command'} eq 'get') {
1308: &get_slot($r,$symb);
1309: } elsif ($env{'form.command'} eq 'change') {
1.39 albertel 1310: if (&release_slot($r,$symb,$env{'form.releaseslot'},1)) {
1311: &get_slot($r,$symb);
1312: }
1.11 albertel 1313: } else {
1314: $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
1315: }
1.2 albertel 1316: }
1.1 albertel 1317: &end_page($r);
1318: return OK;
1319: }
1.3 albertel 1320:
1321: 1;
1322: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>