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.33 2005/11/21 18:55:41 albertel 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 get_slot {
320: my ($r,$symb)=@_;
321:
322: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
323: if ($slot_name) {
324: my %slot=&Apache::lonnet::get_slot($slot_name);
325: my $description1=&get_description($slot_name,\%slot);
326: %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
327: my $description2=&get_description($env{'form.slotname'},\%slot);
328: $r->print("<p>Already have a reservation: $description1</p>");
329: if ($slot_name ne $env{'form.slotname'}) {
330: $r->print(<<STUFF);
331: <form method="POST" action="/adm/slotrequest">
332: <input type="hidden" name="symb" value="$env{'form.symb'}" />
333: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
334: <input type="hidden" name="releaseslot" value="$slot_name" />
335: <input type="hidden" name="command" value="change" />
336: STUFF
337: $r->print("<p>You can either ");
338: $r->print(<<STUFF);
339: <input type="submit" name="change" value="Change" />
340: STUFF
341: $r->print(' your reservation from <b>'.$description1.'</b> to <b>'.
342: $description2.
343: '</b> <br />or <a href="/adm/flip?postdata=return:">'.
344: &mt('Return to last resource').'</a></p>');
345: $r->print(<<STUFF);
346: </form>
347: STUFF
348: } else {
349: $r->print('<p><a href="/adm/flip?postdata=return:">'.
350: &mt('Return to last resource').'</a></p>');
351: }
352: return;
353: }
354: my %slot=&Apache::lonnet::get_slot($env{'form.slotname'});
355: my $reserved=&make_reservation($env{'form.slotname'},
356: \%slot,$symb);
357: my $description=&get_description($env{'form.slotname'},\%slot);
358: if (defined($reserved)) {
359: if ($reserved > -1) {
360: $r->print("<p>Success: $description</p>");
361: $r->print('<p><a href="/adm/flip?postdata=return:">'.
362: &mt('Return to last resource').'</a></p>');
363: return;
364: } elsif ($reserved < 0) {
365: $r->print("<p>Already reserved: $description</p>");
366: $r->print('<p><a href="/adm/flip?postdata=return:">'.
367: &mt('Return to last resource').'</a></p>');
368: return;
369: }
370: }
371:
372: my %lt=('request'=>"Availibility list",
373: 'try' =>'Try again');
374: %lt=&Apache::lonlocal::texthash(%lt);
375:
376: $r->print(<<STUFF);
377: <p> <font color="red">Failed</font> to reserve a spot for $description. </p>
378: <p>
379: <form method="POST" action="/adm/slotrequest">
380: <input type="submit" name="Try Again" value="$lt{'try'}" />
381: <input type="hidden" name="symb" value="$env{'form.symb'}" />
382: <input type="hidden" name="slotname" value="$env{'form.slotname'}" />
383: <input type="hidden" name="command" value="get" />
384: </form>
385: ?
386: </p>
387: <p>
388: or
389: <form method="POST" action="/adm/slotrequest">
390: <input type="hidden" name="symb" value="$env{'form.symb'}" />
391: <input type="submit" name="requestattempt" value="$lt{'request'}" />
392: </form>
393: </p>
394: or
395: STUFF
396: $r->print('<p><a href="/adm/flip?postdata=return:">'.
397: &mt('Return to last resource').'</a></p>');
398: return;
399: }
400:
401: sub allowed_slot {
402: my ($slot_name,$slot,$symb)=@_;
403: #already started
404: if ($slot->{'starttime'} < time) {
405: # all open slot to be schedulable
406: #return 0;
407: }
408: &Apache::lonxml::debug("$slot_name starttime good");
409: #already ended
410: if ($slot->{'endtime'} < time) {
411: return 0;
412: }
413: &Apache::lonxml::debug("$slot_name endtime good");
414: # not allowed to pick this one
415: if (defined($slot->{'type'})
416: && $slot->{'type'} ne 'schedulable_student') {
417: return 0;
418: }
419: &Apache::lonxml::debug("$slot_name type good");
420: # not allowed for this resource
421: if (defined($slot->{'symb'})
422: && $slot->{'symb'} ne $symb) {
423: return 0;
424: }
425: &Apache::lonxml::debug("$slot_name symb good");
426: return 1;
427: }
428:
429: sub get_description {
430: my ($slot_name,$slot)=@_;
431: my $description=$slot->{'description'};
432: if (!defined($description)) {
433: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
434: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
435: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
436: }
437: return $description;
438: }
439:
440: sub show_choices {
441: my ($r,$symb)=@_;
442:
443: my ($cnum,$cdom)=&get_course();
444: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
445: my $available;
446: $r->print('<table border="1">');
447: &Apache::lonxml::debug("Checking Slots");
448: my ($got_slot)=&check_for_reservation($symb);
449: foreach my $slot (sort
450: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
451: (keys(%slots))) {
452:
453: &Apache::lonxml::debug("Checking Slot $slot");
454: next if (!&allowed_slot($slot,$slots{$slot}));
455:
456: $available++;
457:
458: my $description=&get_description($slot,$slots{$slot});
459:
460: my $form=&mt('Unavailable');
461: if (($slot eq $got_slot) ||
462: &space_available($slot,$slots{$slot},$symb)) {
463: my $text=&mt('Select');
464: my $command='get';
465: if ($slot eq $got_slot) {
466: $text=&mt('Free Reservation');
467: $command='release';
468: }
469: my $escsymb=&Apache::lonnet::escape($symb);
470: $form=<<STUFF;
471: <form method="POST" action="/adm/slotrequest">
472: <input type="submit" name="Select" value="$text" />
473: <input type="hidden" name="symb" value="$escsymb" />
474: <input type="hidden" name="slotname" value="$slot" />
475: <input type="hidden" name="command" value="$command" />
476: </form>
477: STUFF
478: }
479: $r->print(<<STUFF);
480: <tr>
481: <td>$form</td>
482: <td>$description</td>
483: </tr>
484: STUFF
485: }
486:
487: if (!$available) {
488: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
489: &mt('Return to last resource').'</a></td></tr>');
490: }
491: $r->print('</table>');
492: }
493:
494: sub to_show {
495: my ($when,$slot) = @_;
496: my $time=time;
497: my $week=60*60*24*7;
498: if ($when eq 'now') {
499: if ($time > $slot->{'starttime'} &&
500: $time < $slot->{'endtime'}) {
501: return 1;
502: }
503: return 0;
504: } elsif ($when eq 'nextweek') {
505: if ( ($time < $slot->{'starttime'} &&
506: ($time+$week) > $slot->{'starttime'})
507: ||
508: ($time < $slot->{'endtime'} &&
509: ($time+$week) > $slot->{'endtime'}) ) {
510: return 1;
511: }
512: return 0;
513: } elsif ($when eq 'lastweek') {
514: if ( ($time > $slot->{'starttime'} &&
515: ($time-$week) < $slot->{'starttime'})
516: ||
517: ($time > $slot->{'endtime'} &&
518: ($time-$week) < $slot->{'endtime'}) ) {
519: return 1;
520: }
521: return 0;
522: } elsif ($when eq 'willopen') {
523: if ($time < $slot->{'starttime'}) {
524: return 1;
525: }
526: return 0;
527: } elsif ($when eq 'wereopen') {
528: if ($time > $slot->{'endtime'}) {
529: return 1;
530: }
531: return 0;
532: }
533:
534: return 1;
535: }
536:
537: sub remove_link {
538: my ($slotname,$entry,$uname,$udom,$symb) = @_;
539:
540: $slotname = &Apache::lonnet::escape($slotname);
541: $entry = &Apache::lonnet::escape($entry);
542: $uname = &Apache::lonnet::escape($uname);
543: $udom = &Apache::lonnet::escape($udom);
544: $symb = &Apache::lonnet::escape($symb);
545:
546: my $remove= &mt('Remove');
547:
548: return <<"END_LINK";
549: <a href="/adm/slotrequest?command=remove_registration&slotname=$slotname&entry=$entry&uname=$uname&udom=$udom&symb=$symb"
550: >($remove)</a>
551: END_LINK
552:
553: }
554:
555: sub show_table {
556: my ($r,$mgr)=@_;
557:
558: my ($cnum,$cdom)=&get_course();
559: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
560: if ( (keys(%slots))[0] =~ /^error: 2 /) {
561: undef(%slots);
562: }
563: my $available;
564: if ($mgr eq 'F') {
565: $r->print('<div>');
566: $r->print('<form method="POST" action="/adm/slotrequest">
567: <input type="hidden" name="command" value="uploadstart" />
568: <input type="submit" name="start" value="'.&mt('Upload Slot List').'" />
569: </form>');
570: $r->print('<form method="POST" action="/adm/helper/newslot.helper">
571: <input type="submit" name="newslot" value="'.&mt('Create a New Slot').'" />
572: </form>');
573: $r->print('</div>');
574: }
575:
576: my %Saveable_Parameters = ('show' => 'array',
577: 'when' => 'scalar',
578: 'order' => 'scalar');
579:
580: &Apache::loncommon::store_course_settings('slotrequest',\%Saveable_Parameters);
581: &Apache::loncommon::restore_course_settings('slotrequest',\%Saveable_Parameters);
582:
583: my %show_fields=&Apache::lonlocal::texthash(
584: 'name' => 'Slot Name',
585: 'description' => 'Description',
586: 'type' => 'Type',
587: 'starttime' => 'Start time',
588: 'endtime' => 'End Time',
589: 'startreserve' => 'Time students can start reserving',
590: 'secret' => 'Secret Word',
591: 'maxspace' => 'Maxium # of students',
592: 'ip' => 'IP or DNS restrictions',
593: 'symb' => 'Resource slot is restricted to.',
594: 'uniqueperiod' => 'Period of time slot is unique',
595: 'proctor' => 'List of proctors');
596: my @show_order=('name','description','type','starttime','endtime',
597: 'startreserve','secret','maxspace','ip','symb',
598: 'uniqueperiod','proctor');
599: my @show =
600: (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
601: : keys(%show_fields);
602: my %show = map { $_ => 1 } (@show);
603:
604: my %when_fields=&Apache::lonlocal::texthash(
605: 'now' => 'Open now',
606: 'nextweek' => 'Open within the next week',
607: 'lastweek' => 'Were open last week',
608: 'willopen' => 'Will open later',
609: 'wereopen' => 'Were open');
610: my @when_order=('now','nextweek','lastweek','willopen','wereopen');
611: $when_fields{'select_form_order'} = \@when_order;
612: my $when = (exists($env{'form.when'})) ? $env{'form.when'}
613: : 'now';
614:
615: $r->print('<form method="POST" action="/adm/slotrequest">
616: <input type="hidden" name="command" value="showslots" />');
617: $r->print('<div>');
618: $r->print('<table class="inline"><tr><th>'.&mt('Show').'</th><th>'.&mt('Open').'</th></tr><tr><td>'.&Apache::loncommon::multiple_select_form('show',\@show,6,\%show_fields,\@show_order).
619: '</td><td>'.&Apache::loncommon::select_form($when,'when',%when_fields).
620: '</td></tr></table>');
621: $r->print('</div>');
622: $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
623: my $linkstart='<a href="/adm/slotrequest?command=showslots&order=';
624: $r->print('<table class="thinborder">
625: <tr>
626: <th></th>');
627: foreach my $which (@show_order) {
628: if ($which ne 'proctor' && exists($show{$which})) {
629: $r->print('<th>'.$linkstart.$which.'">'.$show_fields{$which}.'</a></th>');
630: }
631: }
632: $r->print('<th>Scheduled Students</th></tr>');
633:
634: my %name_cache;
635: my $slotsort = sub {
636: if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
637: if (lc($slots{$a}->{$env{'form.order'}})
638: ne lc($slots{$b}->{$env{'form.order'}})) {
639: return (lc($slots{$a}->{$env{'form.order'}})
640: cmp lc($slots{$b}->{$env{'form.order'}}));
641: }
642: } elsif ($env{'form.order'} eq 'name') {
643: if (lc($a) cmp lc($b)) {
644: return lc($a) cmp lc($b);
645: }
646: } elsif ($env{'form.order'} eq 'uniqueperiod') {
647:
648: if ($slots{$a}->{'uniqueperiod'}[0]
649: ne $slots{$b}->{'uniqueperiod'}[0]) {
650: return ($slots{$a}->{'uniqueperiod'}[0]
651: cmp $slots{$b}->{'uniqueperiod'}[0]);
652: }
653: if ($slots{$a}->{'uniqueperiod'}[1]
654: ne $slots{$b}->{'uniqueperiod'}[1]) {
655: return ($slots{$a}->{'uniqueperiod'}[1]
656: cmp $slots{$b}->{'uniqueperiod'}[1]);
657: }
658: }
659: return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'};
660: };
661: foreach my $slot (sort $slotsort (keys(%slots))) {
662: if (!&to_show($when,$slots{$slot})) { next; }
663: if (defined($slots{$slot}->{'type'})
664: && $slots{$slot}->{'type'} ne 'schedulable_student') {
665: #next;
666: }
667: my $description=&get_description($slot,$slots{$slot});
668: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
669: "^$slot\0");
670: my $ids;
671: foreach my $entry (sort(keys(%consumed))) {
672: my (undef,$id)=split("\0",$entry);
673: my ($uname,$udom) = split('@',$consumed{$entry}{'name'});
674: my $name = &Apache::loncommon::plainname($uname,$udom);
675: $ids.= '<nobr>'.$name.&remove_link($slot,$entry,$uname,$udom,
676: $consumed{$entry}{'symb'})
677: .'</nobr><br />';
678: }
679:
680: my $start=($slots{$slot}->{'starttime'}?
681: &Apache::lonlocal::locallocaltime($slots{$slot}->{'starttime'}):'');
682: my $end=($slots{$slot}->{'endtime'}?
683: &Apache::lonlocal::locallocaltime($slots{$slot}->{'endtime'}):'');
684: my $start_reserve=($slots{$slot}->{'startreserve'}?
685: &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
686:
687: my $unique;
688: if (ref($slots{$slot}{'uniqueperiod'})) {
689: $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
690: localtime($slots{$slot}{'uniqueperiod'}[1]);
691: }
692:
693: my $title;
694: if (exists($slots{$slot}{'symb'})) {
695: my (undef,undef,$res)=
696: &Apache::lonnet::decode_symb($slots{$slot}{'symb'});
697: $res = &Apache::lonnet::clutter($res);
698: $title = &Apache::lonnet::gettitle($slots{$slot}{'symb'});
699: $title='<a href="'.$res.'?symb='.$slots{$slot}{'symb'}.'">'.$title.'</a>';
700: }
701:
702: my @proctors;
703: my $rowspan=1;
704: my $colspan=1;
705: if (exists($show{'proctor'})) {
706: $rowspan=2;
707: @proctors= map {
708: my ($uname,$udom)=split(/@/,$_);
709: my $fullname=$name_cache{$_};
710: if (!defined($fullname)) {
711: &Apache::lonnet::logthis("Gettign $uname $udom");
712: $fullname = &Apache::loncommon::plainname($uname,$udom);
713: $fullname =~s/\s/ /g;
714: $name_cache{$_} = $fullname;
715: }
716: &Apache::loncommon::aboutmewrapper($fullname,$uname,$udom);
717: } (sort(split(/\s*,\s*/,$slots{$slot}->{'proctor'})));
718: }
719: my $proctors=join(', ',@proctors);
720:
721: my $edit=(<<EDITLINK);
722: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
723: EDITLINK
724:
725: $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit</td>\n");
726: if (exists($show{'name'})) {
727: $colspan++;$r->print("<td>$slot</td>");
728: }
729: if (exists($show{'description'})) {
730: $colspan++;$r->print("<td>$description</td>\n");
731: }
732: if (exists($show{'type'})) {
733: $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
734: }
735: if (exists($show{'starttime'})) {
736: $colspan++;$r->print("<td>$start</td>\n");
737: }
738: if (exists($show{'endtime'})) {
739: $colspan++;$r->print("<td>$end</td>\n");
740: }
741: if (exists($show{'startreserve'})) {
742: $colspan++;$r->print("<td>$start_reserve</td>\n");
743: }
744: if (exists($show{'secret'})) {
745: $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
746: }
747: if (exists($show{'maxspace'})) {
748: $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
749: }
750: if (exists($show{'ip'})) {
751: $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
752: }
753: if (exists($show{'symb'})) {
754: $colspan++;$r->print("<td>$title</td>\n");
755: }
756: if (exists($show{'uniqueperiod'})) {
757: $colspan++;$r->print("<td>$unique</td>\n");
758: }
759: $colspan++;$r->print("<td>$ids</td>\n</tr>\n");
760: if (exists($show{'proctor'})) {
761: $r->print(<<STUFF);
762: <tr>
763: <td colspan="$colspan">$proctors</td>
764: </tr>
765: STUFF
766: }
767: }
768: $r->print('</table>');
769: }
770:
771: sub upload_start {
772: my ($r)=@_;
773: $r->print(&Apache::grades::checkforfile_js());
774: my $result.='<table width=100% border=0><tr bgcolor="#e6ffff"><td>'."\n";
775: $result.=' <b>'.
776: &mt('Specify a file containing the slot definitions.').
777: '</b></td></tr>'."\n";
778: $result.='<tr bgcolor=#ffffe6><td>'."\n";
779: my $upfile_select=&Apache::loncommon::upfile_select_html();
780: my $ignore=&mt('Ignore First Line');
781: $result.=<<ENDUPFORM;
782: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
783: <input type="hidden" name="command" value="csvuploadmap" />
784: $upfile_select
785: <br /><input type="button" onClick="javascript:checkUpload(this.form);" value="Upload Data" />
786: <label><input type="checkbox" name="noFirstLine" />$ignore</label>
787: </form>
788: ENDUPFORM
789: $result.='</td></tr></table>'."\n";
790: $result.='</td></tr></table>'."\n";
791: $r->print($result);
792: }
793:
794: sub csvuploadmap_header {
795: my ($r,$datatoken,$distotal)= @_;
796: my $javascript;
797: if ($env{'form.upfile_associate'} eq 'reverse') {
798: $javascript=&csvupload_javascript_reverse_associate();
799: } else {
800: $javascript=&csvupload_javascript_forward_associate();
801: }
802:
803: my $checked=(($env{'form.noFirstLine'})?' checked="checked"':'');
804: my $ignore=&mt('Ignore First Line');
805: $r->print(<<ENDPICK);
806: <form method="post" enctype="multipart/form-data" action="/adm/slotrequest" name="slotupload">
807: <h3>Identify fields</h3>
808: Total number of records found in file: $distotal <hr />
809: Enter as many fields as you can. The system will inform you and bring you back
810: to this page if the data selected is insufficient to create the slots.<hr />
811: <input type="button" value="Reverse Association" onClick="javascript:this.form.associate.value='Reverse Association';submit(this.form);" />
812: <label><input type="checkbox" name="noFirstLine" $checked />$ignore</label>
813: <input type="hidden" name="associate" value="" />
814: <input type="hidden" name="datatoken" value="$datatoken" />
815: <input type="hidden" name="fileupload" value="$env{'form.fileupload'}" />
816: <input type="hidden" name="upfiletype" value="$env{'form.upfiletype'}" />
817: <input type="hidden" name="upfile_associate"
818: value="$env{'form.upfile_associate'}" />
819: <input type="hidden" name="command" value="csvuploadassign" />
820: <hr />
821: <script type="text/javascript" language="Javascript">
822: $javascript
823: </script>
824: ENDPICK
825: return '';
826:
827: }
828:
829: sub csvuploadmap_footer {
830: my ($request,$i,$keyfields) =@_;
831: $request->print(<<ENDPICK);
832: </table>
833: <input type="hidden" name="nfields" value="$i" />
834: <input type="hidden" name="keyfields" value="$keyfields" />
835: <input type="button" onClick="javascript:verify(this.form)" value="Create Slots" /><br />
836: </form>
837: ENDPICK
838: }
839:
840: sub csvupload_javascript_reverse_associate {
841: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
842: return(<<ENDPICK);
843: function verify(vf) {
844: var foundstart=0;
845: var foundend=0;
846: var foundname=0;
847: var foundtype=0;
848: for (i=0;i<=vf.nfields.value;i++) {
849: tw=eval('vf.f'+i+'.selectedIndex');
850: if (i==0 && tw!=0) { foundname=1; }
851: if (i==1 && tw!=0) { foundtype=1; }
852: if (i==2 && tw!=0) { foundstat=1; }
853: if (i==3 && tw!=0) { foundend=1; }
854: }
855: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
856: alert('$error1');
857: return;
858: }
859: vf.submit();
860: }
861: function flip(vf,tf) {
862: }
863: ENDPICK
864: }
865:
866: sub csvupload_javascript_forward_associate {
867: my $error1=&mt('You need to specify the name, starttime, endtime and a type');
868: return(<<ENDPICK);
869: function verify(vf) {
870: var foundstart=0;
871: var foundend=0;
872: var foundname=0;
873: var foundtype=0;
874: for (i=0;i<=vf.nfields.value;i++) {
875: tw=eval('vf.f'+i+'.selectedIndex');
876: if (tw==1) { foundname=1; }
877: if (tw==2) { foundtype=1; }
878: if (tw==3) { foundstat=1; }
879: if (tw==4) { foundend=1; }
880: }
881: if (foundstart==0 && foundend==0 && foundtype==0 && foundname==0) {
882: alert('$error1');
883: return;
884: }
885: vf.submit();
886: }
887: function flip(vf,tf) {
888: }
889: ENDPICK
890: }
891:
892: sub csv_upload_map {
893: my ($r)= @_;
894:
895: my $datatoken;
896: if (!$env{'form.datatoken'}) {
897: $datatoken=&Apache::loncommon::upfile_store($r);
898: } else {
899: $datatoken=$env{'form.datatoken'};
900: &Apache::loncommon::load_tmp_file($r);
901: }
902: my @records=&Apache::loncommon::upfile_record_sep();
903: if ($env{'form.noFirstLine'}) { shift(@records); }
904: &csvuploadmap_header($r,$datatoken,$#records+1);
905: my ($i,$keyfields);
906: if (@records) {
907: my @fields=&csvupload_fields();
908:
909: if ($env{'form.upfile_associate'} eq 'reverse') {
910: &Apache::loncommon::csv_print_samples($r,\@records);
911: $i=&Apache::loncommon::csv_print_select_table($r,\@records,
912: \@fields);
913: foreach (@fields) { $keyfields.=$_->[0].','; }
914: chop($keyfields);
915: } else {
916: unshift(@fields,['none','']);
917: $i=&Apache::loncommon::csv_samples_select_table($r,\@records,
918: \@fields);
919: my %sone=&Apache::loncommon::record_sep($records[0]);
920: $keyfields=join(',',sort(keys(%sone)));
921: }
922: }
923: &csvuploadmap_footer($r,$i,$keyfields);
924:
925: return '';
926: }
927:
928: sub csvupload_fields {
929: return (['name','Slot name'],
930: ['type','Type of slot'],
931: ['starttime','Start Time of slot'],
932: ['endtime','End Time of slot'],
933: ['startreserve','Reservation Start Time'],
934: ['ip','IP or DNS restriction'],
935: ['proctor','List of proctor ids'],
936: ['description','Slot Description'],
937: ['maxspace','Maximum number of reservations'],
938: ['symb','Resource Restriction'],
939: ['uniqueperiod','Date range of slot exclusion'],
940: ['secret','Secret word proctor uses to validate']);
941: }
942:
943: sub csv_upload_assign {
944: my ($r,$mgr)= @_;
945: &Apache::loncommon::load_tmp_file($r);
946: my @slotdata = &Apache::loncommon::upfile_record_sep();
947: if ($env{'form.noFirstLine'}) { shift(@slotdata); }
948: my %fields=&Apache::grades::get_fields();
949: $r->print('<h3>Creating Slots</h3>');
950: my $cname=$env{'course.'.$env{'request.course.id'}.'.num'};
951: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
952: my $countdone=0;
953: my @errors;
954: foreach my $slot (@slotdata) {
955: my %slot;
956: my %entries=&Apache::loncommon::record_sep($slot);
957: my $domain;
958: my $name=$entries{$fields{'name'}};
959: if ($name=~/^\s*$/) {
960: push(@errors,"Did not create slot with no name");
961: next;
962: }
963: if ($name=~/\s/) {
964: push(@errors,"$name not created -- Name must not contain spaces");
965: next;
966: }
967: if ($name=~/\W/) {
968: push(@errors,"$name not created -- Name must contain only letters, numbers and _");
969: next;
970: }
971: if ($entries{$fields{'type'}}) {
972: $slot{'type'}=$entries{$fields{'type'}};
973: } else {
974: $slot{'type'}='preassigned';
975: }
976: if ($slot{'type'} ne 'preassigned' &&
977: $slot{'type'} ne 'schedulable_student') {
978: push(@errors,"$name not created -- invalid type ($slot{'type'}) must be either preassigned or schedulable_student");
979: next;
980: }
981: if ($entries{$fields{'starttime'}}) {
982: $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
983: }
984: if ($entries{$fields{'endtime'}}) {
985: $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
986: }
987: if ($entries{$fields{'startreserve'}}) {
988: $slot{'startreserve'}=
989: &UnixDate($entries{$fields{'startreserve'}},"%s");
990: }
991: foreach my $key ('ip','proctor','description','maxspace',
992: 'secret','symb') {
993: if ($entries{$fields{$key}}) {
994: $slot{$key}=$entries{$fields{$key}};
995: }
996: }
997: if ($entries{$fields{'uniqueperiod'}}) {
998: my ($start,$end)=split(',',$entries{$fields{'uniqueperiod'}});
999: my @times=(&UnixDate($start,"%s"),
1000: &UnixDate($end,"%s"));
1001: $slot{'uniqueperiod'}=\@times;
1002: }
1003:
1004: &Apache::lonnet::cput('slots',{$name=>\%slot},$cdom,$cname);
1005: $r->print('.');
1006: $r->rflush();
1007: $countdone++;
1008: }
1009: $r->print("<p>Created $countdone slots\n</p>");
1010: foreach my $error (@errors) {
1011: $r->print("<p>$error\n</p>");
1012: }
1013: &show_table($r,$mgr);
1014: return '';
1015: }
1016:
1017: sub handler {
1018: my $r=shift;
1019:
1020: &Apache::loncommon::content_type($r,'text/html');
1021: &Apache::loncommon::no_cache($r);
1022: if ($r->header_only()) {
1023: $r->send_http_header();
1024: return OK;
1025: }
1026:
1027: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1028:
1029: my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1030: my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1031: my $title='Requesting Another Worktime';
1032: if ($env{'form.command'} =~ /^(showslots|uploadstart|csvuploadmap|csvuploadassign)$/ && $vgr eq 'F') {
1033: $title = 'Managing Slots';
1034: }
1035: &start_page($r,$title);
1036:
1037: if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1038: &show_table($r,$mgr);
1039: } elsif ($env{'form.command'} eq 'remove_registration' && $mgr eq 'F') {
1040: &remove_registration($r);
1041: } elsif ($env{'form.command'} eq 'release' && $mgr eq 'F') {
1042: &release_slot($r,undef,undef,undef,$mgr);
1043: } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1044: &upload_start($r);
1045: } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1046: &csv_upload_map($r);
1047: } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
1048: if ($env{'form.associate'} ne 'Reverse Association') {
1049: &csv_upload_assign($r,$mgr);
1050: } else {
1051: if ( $env{'form.upfile_associate'} ne 'reverse' ) {
1052: $env{'form.upfile_associate'} = 'reverse';
1053: } else {
1054: $env{'form.upfile_associate'} = 'forward';
1055: }
1056: &csv_upload_map($r);
1057: }
1058: } else {
1059: my $symb=&Apache::lonnet::unescape($env{'form.symb'});
1060: my (undef,undef,$res)=&Apache::lonnet::decode_symb($symb);
1061: if ($res !~ /\.task$/) {
1062: &fail($r,'not_valid');
1063: return OK;
1064: }
1065: $env{'request.symb'}=$symb;
1066: my ($status) = &Apache::lonhomework::check_task_access('0');
1067: if ($status eq 'CAN_ANSWER' ||
1068: $status eq 'NEEDS_CHECKIN' ||
1069: $status eq 'WAITING_FOR_GRADE') {
1070: &fail($r,'not_allowed');
1071: return OK;
1072: }
1073: if ($env{'form.requestattempt'}) {
1074: &show_choices($r,$symb);
1075: } elsif ($env{'form.command'} eq 'release') {
1076: &release_slot($r,$symb);
1077: } elsif ($env{'form.command'} eq 'get') {
1078: &get_slot($r,$symb);
1079: } elsif ($env{'form.command'} eq 'change') {
1080: &release_slot($r,$symb,$env{'form.releaseslot'},1);
1081: &get_slot($r,$symb);
1082: } else {
1083: $r->print("<p>Unknown command: ".$env{'form.command'}."</p>");
1084: }
1085: }
1086: &end_page($r);
1087: return OK;
1088: }
1089:
1090: 1;
1091: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>