Annotation of loncom/interface/slotrequest.pm, revision 1.33
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Handler for requesting to have slots added to a students record
3: #
1.33 ! albertel 4: # $Id: slotrequest.pm,v 1.32 2005/11/21 17:50:45 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.27 albertel 37: use Date::Manip;
1.1 albertel 38:
39: sub fail {
40: my ($r,$code)=@_;
1.2 albertel 41: if ($code eq 'not_valid') {
1.8 albertel 42: $r->print('<p>'.&mt('Unable to understand what resource you wanted to sign up for.').'</p>');
1.2 albertel 43:
1.8 albertel 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>');
1.2 albertel 48: }
1.8 albertel 49:
1.2 albertel 50: $r->print('<p><a href="/adm/flip?postdata=return:">'.
51: &mt('Return to last resource').'</a></p>');
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");
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: }
1.3 albertel 112:
1.4 albertel 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'});
1.7 albertel 133: if ($slots{$slot_name}->{'endtime'} > time &&
1.4 albertel 134: $slots{$slot_name}->{'startreserve'} < time) {
1.7 albertel 135: # between start of reservation times and end of slot
1.4 albertel 136: return($slot_name, $slots{$slot_name});
137: }
138: }
139: return (undef,undef);
140: }
141:
1.5 albertel 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:
1.2 albertel 166: sub make_reservation {
167: my ($slot_name,$slot,$symb)=@_;
1.3 albertel 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:
1.2 albertel 189: my $max=$slot->{'maxspace'};
1.3 albertel 190: if (!defined($max)) { $max=99999; }
1.2 albertel 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;
1.3 albertel 201: &Apache::lonxml::debug("wanted $wanted<br />");
1.7 albertel 202: if (scalar(@ids) >= $max) {
1.2 albertel 203: # full up
1.7 albertel 204: return undef;
1.2 albertel 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},
1.3 albertel 214: $cdom, $cnum);
215:
1.2 albertel 216: if ($success eq 'ok') {
1.3 albertel 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");
1.2 albertel 227: return $wanted;
228: }
1.3 albertel 229:
1.2 albertel 230: # someone else got it
1.3 albertel 231: return undef;
232: }
233:
1.33 ! albertel 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:
1.5 albertel 262: sub release_slot {
1.33 ! albertel 263: my ($r,$symb,$slot_name,$inhibit_return_link,$mgr)=@_;
1.6 albertel 264:
265: if ($slot_name eq '') { $slot_name=$env{'form.slotname'}; }
266: my ($cnum,$cdom)=&get_course();
267:
1.33 ! albertel 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:
1.5 albertel 279: # get parameter string, check for existance, rebuild string with the slot
1.6 albertel 280: my @slots = split(/:/,&Apache::lonnet::EXT("resource.0.availablestudent",
1.33 ! albertel 281: $symb,$udom,$uname));
! 282:
1.6 albertel 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);
1.5 albertel 289:
290: # get slot reservations, check if user has one, if so remove reservation
1.6 albertel 291: my %consumed=&Apache::lonnet::dump('slot_reservations',$cdom,$cnum,
292: "^$slot_name\0");
293: foreach my $entry (keys(%consumed)) {
1.33 ! albertel 294: if ( $consumed{$entry}->{'name'} eq ($uname.'@'.$udom) ) {
1.6 albertel 295: &Apache::lonnet::del('slot_reservations',[$entry],
296: $cdom,$cnum);
297: }
298: }
1.33 ! albertel 299:
1.5 albertel 300: # store new parameter string
1.6 albertel 301: my $result=&Apache::lonparmset::storeparm_by_symb($symb,
302: '0_availablestudent',
303: 1, $new_param, 'string',
1.33 ! albertel 304: $uname,$udom);
1.6 albertel 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>");
1.33 ! albertel 308: if ($mgr eq 'F') {
! 309: $r->print('<p><a href="/adm/slotrequest?command=showslots">'.
! 310: &mt('Return to slot list').'</a></p>');
! 311: }
1.7 albertel 312: if (!$inhibit_return_link) {
1.6 albertel 313: $r->print('<p><a href="/adm/flip?postdata=return:">'.
314: &mt('Return to last resource').'</a></p>');
315: }
316: return 1;
1.5 albertel 317: }
318:
1.3 albertel 319: sub get_slot {
320: my ($r,$symb)=@_;
321:
1.5 albertel 322: my $slot_name=&check_for_conflict($symb,$env{'form.slotname'});
323: if ($slot_name) {
324: my %slot=&Apache::lonnet::get_slot($slot_name);
1.6 albertel 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>");
1.7 albertel 329: if ($slot_name ne $env{'form.slotname'}) {
330: $r->print(<<STUFF);
1.6 albertel 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
1.7 albertel 337: $r->print("<p>You can either ");
338: $r->print(<<STUFF);
1.6 albertel 339: <input type="submit" name="change" value="Change" />
340: STUFF
1.7 albertel 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);
1.6 albertel 346: </form>
347: STUFF
1.7 albertel 348: } else {
349: $r->print('<p><a href="/adm/flip?postdata=return:">'.
350: &mt('Return to last resource').'</a></p>');
351: }
1.5 albertel 352: return;
353: }
1.3 albertel 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);
1.7 albertel 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: }
1.3 albertel 370: }
371:
1.7 albertel 372: my %lt=('request'=>"Availibility list",
1.3 albertel 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) {
1.5 albertel 405: # all open slot to be schedulable
406: #return 0;
1.3 albertel 407: }
1.5 albertel 408: &Apache::lonxml::debug("$slot_name starttime good");
1.3 albertel 409: #already ended
410: if ($slot->{'endtime'} < time) {
411: return 0;
412: }
1.5 albertel 413: &Apache::lonxml::debug("$slot_name endtime good");
1.3 albertel 414: # not allowed to pick this one
415: if (defined($slot->{'type'})
416: && $slot->{'type'} ne 'schedulable_student') {
417: return 0;
418: }
1.5 albertel 419: &Apache::lonxml::debug("$slot_name type good");
1.3 albertel 420: # not allowed for this resource
421: if (defined($slot->{'symb'})
422: && $slot->{'symb'} ne $symb) {
423: return 0;
424: }
1.5 albertel 425: &Apache::lonxml::debug("$slot_name symb good");
1.3 albertel 426: return 1;
1.2 albertel 427: }
428:
1.3 albertel 429: sub get_description {
430: my ($slot_name,$slot)=@_;
431: my $description=$slot->{'description'};
432: if (!defined($description)) {
1.4 albertel 433: $description=&mt('[_1] From [_2] to [_3]',$slot_name,
1.3 albertel 434: &Apache::lonlocal::locallocaltime($slot->{'starttime'}),
435: &Apache::lonlocal::locallocaltime($slot->{'endtime'}));
436: }
437: return $description;
438: }
1.2 albertel 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);
1.3 albertel 445: my $available;
1.2 albertel 446: $r->print('<table border="1">');
1.5 albertel 447: &Apache::lonxml::debug("Checking Slots");
448: my ($got_slot)=&check_for_reservation($symb);
1.2 albertel 449: foreach my $slot (sort
450: { return $slots{$a}->{'starttime'} <=> $slots{$b}->{'starttime'} }
451: (keys(%slots))) {
1.5 albertel 452:
453: &Apache::lonxml::debug("Checking Slot $slot");
1.3 albertel 454: next if (!&allowed_slot($slot,$slots{$slot}));
455:
456: $available++;
457:
458: my $description=&get_description($slot,$slots{$slot});
1.2 albertel 459:
460: my $form=&mt('Unavailable');
1.7 albertel 461: if (($slot eq $got_slot) ||
462: &space_available($slot,$slots{$slot},$symb)) {
1.5 albertel 463: my $text=&mt('Select');
464: my $command='get';
465: if ($slot eq $got_slot) {
466: $text=&mt('Free Reservation');
467: $command='release';
468: }
1.3 albertel 469: my $escsymb=&Apache::lonnet::escape($symb);
1.2 albertel 470: $form=<<STUFF;
1.3 albertel 471: <form method="POST" action="/adm/slotrequest">
1.5 albertel 472: <input type="submit" name="Select" value="$text" />
1.3 albertel 473: <input type="hidden" name="symb" value="$escsymb" />
474: <input type="hidden" name="slotname" value="$slot" />
1.5 albertel 475: <input type="hidden" name="command" value="$command" />
1.2 albertel 476: </form>
477: STUFF
478: }
479: $r->print(<<STUFF);
480: <tr>
481: <td>$form</td>
482: <td>$description</td>
483: </tr>
484: STUFF
485: }
1.3 albertel 486:
487: if (!$available) {
1.5 albertel 488: $r->print('<tr><td>No available times. <a href="/adm/flip?postdata=return:">'.
1.3 albertel 489: &mt('Return to last resource').'</a></td></tr>');
490: }
1.2 albertel 491: $r->print('</table>');
492: }
493:
1.30 albertel 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:
1.33 ! albertel 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:
1.5 albertel 555: sub show_table {
1.19 albertel 556: my ($r,$mgr)=@_;
1.5 albertel 557:
558: my ($cnum,$cdom)=&get_course();
559: my %slots=&Apache::lonnet::dump('slots',$cdom,$cnum);
1.19 albertel 560: if ( (keys(%slots))[0] =~ /^error: 2 /) {
561: undef(%slots);
562: }
1.5 albertel 563: my $available;
1.14 albertel 564: if ($mgr eq 'F') {
1.30 albertel 565: $r->print('<div>');
1.14 albertel 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>');
1.28 albertel 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>');
1.30 albertel 573: $r->print('</div>');
1.14 albertel 574: }
1.29 albertel 575:
1.30 albertel 576: my %Saveable_Parameters = ('show' => 'array',
577: 'when' => 'scalar',
578: 'order' => 'scalar');
1.29 albertel 579:
1.30 albertel 580: &Apache::loncommon::store_course_settings('slotrequest',\%Saveable_Parameters);
581: &Apache::loncommon::restore_course_settings('slotrequest',\%Saveable_Parameters);
1.29 albertel 582:
1.30 albertel 583: my %show_fields=&Apache::lonlocal::texthash(
1.29 albertel 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');
1.30 albertel 596: my @show_order=('name','description','type','starttime','endtime',
1.29 albertel 597: 'startreserve','secret','maxspace','ip','symb',
598: 'uniqueperiod','proctor');
1.30 albertel 599: my @show =
1.29 albertel 600: (exists($env{'form.show'})) ? &Apache::loncommon::get_env_multiple('form.show')
1.30 albertel 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';
1.29 albertel 614:
615: $r->print('<form method="POST" action="/adm/slotrequest">
1.30 albertel 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).
1.29 albertel 620: '</td></tr></table>');
1.30 albertel 621: $r->print('</div>');
622: $r->print('<p><input type="submit" name="start" value="'.&mt('Update Display').'" /></p>');
1.21 albertel 623: my $linkstart='<a href="/adm/slotrequest?command=showslots&order=';
1.30 albertel 624: $r->print('<table class="thinborder">
1.10 albertel 625: <tr>
1.29 albertel 626: <th></th>');
1.30 albertel 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>');
1.29 albertel 630: }
631: }
632: $r->print('<th>Scheduled Students</th></tr>');
633:
1.21 albertel 634: my %name_cache;
635: my $slotsort = sub {
1.29 albertel 636: if ($env{'form.order'}=~/^(type|description|endtime|startreserve|maxspace|ip|symb)$/) {
1.21 albertel 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: }
1.23 albertel 642: } elsif ($env{'form.order'} eq 'name') {
643: if (lc($a) cmp lc($b)) {
644: return lc($a) cmp lc($b);
645: }
1.29 albertel 646: } elsif ($env{'form.order'} eq 'uniqueperiod') {
1.21 albertel 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))) {
1.30 albertel 662: if (!&to_show($when,$slots{$slot})) { next; }
1.5 albertel 663: if (defined($slots{$slot}->{'type'})
664: && $slots{$slot}->{'type'} ne 'schedulable_student') {
1.13 albertel 665: #next;
1.5 albertel 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);
1.33 ! albertel 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 />';
1.5 albertel 678: }
1.33 ! albertel 679:
1.24 albertel 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'}):'');
1.28 albertel 684: my $start_reserve=($slots{$slot}->{'startreserve'}?
1.24 albertel 685: &Apache::lonlocal::locallocaltime($slots{$slot}->{'startreserve'}):'');
686:
1.14 albertel 687: my $unique;
688: if (ref($slots{$slot}{'uniqueperiod'})) {
689: $unique=localtime($slots{$slot}{'uniqueperiod'}[0]).','.
690: localtime($slots{$slot}{'uniqueperiod'}[1]);
691: }
1.33 ! albertel 692:
1.29 albertel 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: }
1.33 ! albertel 701:
1.29 albertel 702: my @proctors;
703: my $rowspan=1;
704: my $colspan=1;
1.30 albertel 705: if (exists($show{'proctor'})) {
1.29 albertel 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: }
1.20 albertel 719: my $proctors=join(', ',@proctors);
1.14 albertel 720:
1.31 albertel 721: my $edit=(<<EDITLINK);
722: <a href="/adm/helper/newslot.helper?name=$slot">Edit</a>
723: EDITLINK
1.29 albertel 724:
725: $r->print("<tr>\n<td rowspan=\"$rowspan\">$edit</td>\n");
1.30 albertel 726: if (exists($show{'name'})) {
1.29 albertel 727: $colspan++;$r->print("<td>$slot</td>");
728: }
1.33 ! albertel 729: if (exists($show{'description'})) {
! 730: $colspan++;$r->print("<td>$description</td>\n");
! 731: }
1.30 albertel 732: if (exists($show{'type'})) {
1.29 albertel 733: $colspan++;$r->print("<td>$slots{$slot}->{'type'}</td>\n");
734: }
1.30 albertel 735: if (exists($show{'starttime'})) {
1.29 albertel 736: $colspan++;$r->print("<td>$start</td>\n");
737: }
1.30 albertel 738: if (exists($show{'endtime'})) {
1.29 albertel 739: $colspan++;$r->print("<td>$end</td>\n");
740: }
1.30 albertel 741: if (exists($show{'startreserve'})) {
1.29 albertel 742: $colspan++;$r->print("<td>$start_reserve</td>\n");
743: }
1.30 albertel 744: if (exists($show{'secret'})) {
1.29 albertel 745: $colspan++;$r->print("<td>$slots{$slot}{'secret'}</td>\n");
746: }
1.30 albertel 747: if (exists($show{'maxspace'})) {
1.29 albertel 748: $colspan++;$r->print("<td>$slots{$slot}{'maxspace'}</td>\n");
749: }
1.30 albertel 750: if (exists($show{'ip'})) {
1.29 albertel 751: $colspan++;$r->print("<td>$slots{$slot}{'ip'}</td>\n");
752: }
1.30 albertel 753: if (exists($show{'symb'})) {
1.29 albertel 754: $colspan++;$r->print("<td>$title</td>\n");
755: }
1.30 albertel 756: if (exists($show{'uniqueperiod'})) {
1.29 albertel 757: $colspan++;$r->print("<td>$unique</td>\n");
758: }
759: $colspan++;$r->print("<td>$ids</td>\n</tr>\n");
1.30 albertel 760: if (exists($show{'proctor'})) {
1.29 albertel 761: $r->print(<<STUFF);
1.21 albertel 762: <tr>
1.29 albertel 763: <td colspan="$colspan">$proctors</td>
1.21 albertel 764: </tr>
1.5 albertel 765: STUFF
1.29 albertel 766: }
1.5 albertel 767: }
768: $r->print('</table>');
769: }
770:
1.14 albertel 771: sub upload_start {
1.19 albertel 772: my ($r)=@_;
1.14 albertel 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 {
1.19 albertel 795: my ($r,$datatoken,$distotal)= @_;
1.14 albertel 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 {
1.19 albertel 893: my ($r)= @_;
1.14 albertel 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); }
1.19 albertel 904: &csvuploadmap_header($r,$datatoken,$#records+1);
1.14 albertel 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'],
1.15 albertel 933: ['startreserve','Reservation Start Time'],
1.14 albertel 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 {
1.19 albertel 944: my ($r,$mgr)= @_;
1.14 albertel 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;
1.31 albertel 953: my @errors;
1.14 albertel 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'}};
1.31 albertel 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: }
1.14 albertel 971: if ($entries{$fields{'type'}}) {
972: $slot{'type'}=$entries{$fields{'type'}};
973: } else {
974: $slot{'type'}='preassigned';
975: }
1.31 albertel 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: }
1.14 albertel 981: if ($entries{$fields{'starttime'}}) {
982: $slot{'starttime'}=&UnixDate($entries{$fields{'starttime'}},"%s");
983: }
984: if ($entries{$fields{'endtime'}}) {
1.16 albertel 985: $slot{'endtime'}=&UnixDate($entries{$fields{'endtime'}},"%s");
1.14 albertel 986: }
1.23 albertel 987: if ($entries{$fields{'startreserve'}}) {
988: $slot{'startreserve'}=
989: &UnixDate($entries{$fields{'startreserve'}},"%s");
990: }
1.14 albertel 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: }
1.31 albertel 1009: $r->print("<p>Created $countdone slots\n</p>");
1010: foreach my $error (@errors) {
1011: $r->print("<p>$error\n</p>");
1012: }
1.19 albertel 1013: &show_table($r,$mgr);
1.14 albertel 1014: return '';
1015: }
1016:
1.1 albertel 1017: sub handler {
1018: my $r=shift;
1019:
1.30 albertel 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:
1.8 albertel 1027: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'});
1.28 albertel 1028:
1.12 albertel 1029: my $vgr=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.14 albertel 1030: my $mgr=&Apache::lonnet::allowed('mgr',$env{'request.course.id'});
1.28 albertel 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:
1.8 albertel 1037: if ($env{'form.command'} eq 'showslots' && $vgr eq 'F') {
1.19 albertel 1038: &show_table($r,$mgr);
1.33 ! albertel 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);
1.14 albertel 1043: } elsif ($env{'form.command'} eq 'uploadstart' && $mgr eq 'F') {
1.19 albertel 1044: &upload_start($r);
1.14 albertel 1045: } elsif ($env{'form.command'} eq 'csvuploadmap' && $mgr eq 'F') {
1.19 albertel 1046: &csv_upload_map($r);
1.14 albertel 1047: } elsif ($env{'form.command'} eq 'csvuploadassign' && $mgr eq 'F') {
1048: if ($env{'form.associate'} ne 'Reverse Association') {
1.19 albertel 1049: &csv_upload_assign($r,$mgr);
1.14 albertel 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: }
1.19 albertel 1056: &csv_upload_map($r);
1.14 albertel 1057: }
1.8 albertel 1058: } else {
1.19 albertel 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;
1.11 albertel 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: }
1.2 albertel 1085: }
1.1 albertel 1086: &end_page($r);
1087: return OK;
1088: }
1.3 albertel 1089:
1090: 1;
1091: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>