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