Annotation of loncom/homework/lonhomework.pm, revision 1.250
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.250 ! albertel 4: # $Id: lonhomework.pm,v 1.249 2006/06/08 22:33:57 albertel Exp $
1.63 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/
1.159 www 27:
1.1 albertel 28:
29: package Apache::lonhomework;
30: use strict;
1.73 albertel 31: use Apache::style();
32: use Apache::lonxml();
1.204 albertel 33: use Apache::lonnet;
1.73 albertel 34: use Apache::lonplot();
35: use Apache::inputtags();
36: use Apache::structuretags();
37: use Apache::randomlabel();
38: use Apache::response();
39: use Apache::hint();
40: use Apache::outputtags();
1.83 albertel 41: use Apache::caparesponse();
42: use Apache::radiobuttonresponse();
43: use Apache::optionresponse();
44: use Apache::imageresponse();
45: use Apache::essayresponse();
46: use Apache::externalresponse();
1.106 albertel 47: use Apache::rankresponse();
1.107 albertel 48: use Apache::matchresponse();
1.137 albertel 49: use Apache::chemresponse();
1.169 albertel 50: use Apache::drawimage();
1.26 www 51: use Apache::Constants qw(:common);
1.73 albertel 52: use HTML::Entities();
1.83 albertel 53: use Apache::loncommon();
1.146 albertel 54: use Apache::lonlocal;
1.179 albertel 55: use Time::HiRes qw( gettimeofday tv_interval );
1.188 foxr 56: use Apache::lonnet();
57:
1.189 albertel 58: # FIXME - improve commenting
1.188 foxr 59:
1.43 albertel 60:
1.69 harris41 61: BEGIN {
1.145 albertel 62: &Apache::lonxml::register_insert();
1.43 albertel 63: }
64:
1.188 foxr 65:
66: #
1.189 albertel 67: # Decides what targets to render for.
1.188 foxr 68: # Implicit inputs:
69: # Various session environment variables:
1.189 albertel 70: # request.state - published - is a /res/ resource
71: # uploaded - is a /uploaded/ resource
72: # contruct - is a /priv/ resource
73: # form.grade_target - a form parameter requesting a specific target
1.5 albertel 74: sub get_target {
1.204 albertel 75: &Apache::lonxml::debug("request.state = $env{'request.state'}");
76: if( defined($env{'form.grade_target'})) {
77: &Apache::lonxml::debug("form.grade_target= $env{'form.grade_target'}");
1.188 foxr 78: } else {
1.189 albertel 79: &Apache::lonxml::debug("form.grade_target <undefined>");
1.188 foxr 80: }
1.204 albertel 81: if (($env{'request.state'} eq "published") ||
82: ($env{'request.state'} eq "uploaded")) {
83: if ( defined($env{'form.grade_target'} )
84: && ($env{'form.grade_target'} eq 'tex')) {
85: return ($env{'form.grade_target'});
86: } elsif ( defined($env{'form.grade_target'} )
1.145 albertel 87: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.207 albertel 88: return ($env{'form.grade_target'});
1.244 albertel 89: } elsif ( $env{'form.grade_target'} eq 'webgrade'
90: && ($Apache::lonhomework::queuegrade eq 'F' )) {
91: return ($env{'form.grade_target'});
1.207 albertel 92: }
93: if ($env{'form.webgrade'} &&
1.244 albertel 94: ($Apache::lonhomework::modifygrades eq 'F'
95: || $Apache::lonhomework::queuegrade eq 'F' )) {
1.207 albertel 96: return ('grade','webgrade');
1.145 albertel 97: }
1.204 albertel 98: if ( defined($env{'form.submitted'}) &&
99: ( !defined($env{'form.resetdata'})) &&
100: ( !defined($env{'form.newrandomization'}))) {
1.217 albertel 101: return ('grade', 'web');
1.145 albertel 102: } else {
1.217 albertel 103: return ('web');
1.145 albertel 104: }
1.204 albertel 105: } elsif ($env{'request.state'} eq "construct") {
106: if ( defined($env{'form.grade_target'}) ) {
107: return ($env{'form.grade_target'});
1.145 albertel 108: }
1.204 albertel 109: if ( defined($env{'form.preview'})) {
110: if ( defined($env{'form.submitted'})) {
1.145 albertel 111: return ('grade', 'web');
112: } else {
113: return ('web');
114: }
115: } else {
1.204 albertel 116: if ( $env{'form.problemmode'} eq &mt('View') ||
117: $env{'form.problemmode'} eq &mt('Discard Edits and View')) {
118: if ( defined($env{'form.submitted'}) &&
119: (!defined($env{'form.resetdata'})) &&
120: (!defined($env{'form.newrandomization'}))) {
1.145 albertel 121: return ('grade', 'web','answer');
122: } else {
123: return ('web','answer');
124: }
1.204 albertel 125: } elsif ( $env{'form.problemmode'} eq &mt('Edit') ||
126: $env{'form.problemmode'} eq 'Edit') {
127: if ( $env{'form.submitted'} eq 'edit' ) {
128: if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
1.145 albertel 129: return ('modified','web','answer');
130: } else {
1.211 albertel 131: return ('modified','no_output_web','edit');
1.145 albertel 132: }
133: } else {
1.211 albertel 134: return ('no_output_web','edit');
1.145 albertel 135: }
136: } else {
137: return ('web');
138: }
139: }
1.15 albertel 140: }
1.145 albertel 141: return ();
1.5 albertel 142: }
143:
1.3 albertel 144: sub setup_vars {
1.145 albertel 145: my ($target) = @_;
146: return ';'
1.11 albertel 147: # return ';$external::target='.$target.';';
1.2 albertel 148: }
149:
1.36 albertel 150: sub createmenu {
1.145 albertel 151: my ($which,$request)=@_;
152: if ($which eq 'grade') {
153: $request->print('<script language="JavaScript">
1.91 albertel 154: hwkmenu=window.open("/res/adm/pages/homeworkmenu.html","homeworkremote",
1.52 albertel 155: "height=350,width=150,menubar=no");
156: </script>');
1.145 albertel 157: }
1.36 albertel 158: }
159:
1.200 albertel 160: sub proctor_checked_in {
1.226 albertel 161: my ($slot_name,$slot,$type)=@_;
162: my @possible_proctors=split(",",$slot->{'proctor'});
163:
1.248 albertel 164: return 1 if (!@possible_proctors);
165:
1.226 albertel 166: my $key;
167: if ($type eq 'Task') {
1.230 albertel 168: my $version=$Apache::lonhomework::history{'resource.0.version'};
169: $key ="resource.$version.0.checkedin";
1.226 albertel 170: } elsif ($type eq 'problem') {
171: $key ='resource.0.checkedin';
172: }
1.249 albertel 173: # backward compatability, used to be username@domain,
174: # now is username:domain
175: my $who = $Apache::lonhomework::history{$key};
176: if ($who !~ /:/) {
177: $who =~ tr/@/:/;
178: }
1.226 albertel 179: foreach my $possible (@possible_proctors) {
1.249 albertel 180: if ($who eq $possible
1.226 albertel 181: && $Apache::lonhomework::history{$key.'.slot'} eq $slot_name) {
1.202 albertel 182: return 1;
183: }
184: }
1.226 albertel 185:
1.200 albertel 186: return 0;
187: }
188:
1.52 albertel 189: $Apache::lonxml::browse='';
1.152 albertel 190: sub check_ip_acc {
191: my ($acc)=@_;
1.218 albertel 192: &Apache::lonxml::debug("acc is $acc");
193: if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
194: return 1;
195: }
1.152 albertel 196: my $allowed=0;
197: my $ip=$ENV{'REMOTE_ADDR'};
198: my $name;
199: foreach my $pattern (split(',',$acc)) {
1.233 albertel 200: $pattern =~ s/^\s*//;
201: $pattern =~ s/\s*$//;
1.152 albertel 202: if ($pattern =~ /\*$/) {
203: #35.8.*
204: $pattern=~s/\*//;
205: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
206: } elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {
207: #35.8.3.[34-56]
208: my $low=$2;
209: my $high=$3;
210: $pattern=$1;
211: if ($ip =~ /^\Q$pattern\E/) {
212: my $last=(split(/\./,$ip))[3];
213: if ($last <=$high && $last >=$low) { $allowed=1; }
214: }
215: } elsif ($pattern =~ /^\*/) {
216: #*.msu.edu
217: $pattern=~s/\*//;
218: if (!defined($name)) {
219: use Socket;
220: my $netaddr=inet_aton($ip);
221: ($name)=gethostbyaddr($netaddr,AF_INET);
1.158 albertel 222: }
1.152 albertel 223: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
224: } elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
225: #127.0.0.1
226: if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
227: } else {
228: #some.name.com
229: if (!defined($name)) {
230: use Socket;
231: my $netaddr=inet_aton($ip);
232: ($name)=gethostbyaddr($netaddr,AF_INET);
233: }
234: if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
235: }
236: if ($allowed) { last; }
237: }
238: return $allowed;
239: }
1.198 albertel 240:
1.226 albertel 241: sub check_slot_access {
242: my ($id,$type)=@_;
243:
1.207 albertel 244: # does it pass normal muster
1.226 albertel 245: my ($status,$datemsg)=&check_access($id);
246:
247: my $useslots = &Apache::lonnet::EXT("resource.$id.useslots");
1.245 albertel 248: if ($useslots ne 'resource' && $useslots ne 'map') {
1.226 albertel 249: return ($status,$datemsg);
250: }
251:
1.200 albertel 252: if ($status eq 'SHOW_ANSWER' ||
253: $status eq 'CLOSED' ||
254: $status eq 'INVALID_ACCESS' ||
255: $status eq 'UNAVAILABLE') {
256: return ($status,$datemsg);
257: }
1.204 albertel 258: if ($env{'request.state'} eq "construct") {
1.203 albertel 259: return ($status,$datemsg);
260: }
1.226 albertel 261:
262: if ($type eq 'Task') {
263: my $version=$Apache::lonhomework::history{'resource.version'};
1.230 albertel 264: if ($Apache::lonhomework::history{"resource.$version.0.checkedin"} &&
265: $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {
1.226 albertel 266: return ('SHOW_ANSWER');
267: }
1.207 albertel 268: }
1.226 albertel 269:
1.210 albertel 270: my @slots=
271: (split(':',&Apache::lonnet::EXT("resource.$id.availablestudent")),
272: split(':',&Apache::lonnet::EXT("resource.$id.available")));
273:
1.200 albertel 274: # if (!@slots) {
275: # return ($status,$datemsg);
276: # }
277: my $slotstatus='NOT_IN_A_SLOT';
1.206 albertel 278: my ($returned_slot,$slot_name);
1.210 albertel 279: foreach my $slot (@slots) {
1.241 albertel 280: $slot =~ s/(^\s*|\s*$)//g;
1.201 albertel 281: &Apache::lonxml::debug("getting $slot");
1.200 albertel 282: my %slot=&Apache::lonnet::get_slot($slot);
1.201 albertel 283: &Apache::lonhomework::showhash(%slot);
1.200 albertel 284: if ($slot{'starttime'} < time &&
285: $slot{'endtime'} > time &&
1.201 albertel 286: &check_ip_acc($slot{'ip'})) {
1.202 albertel 287: &Apache::lonxml::debug("$slot is good");
288: $slotstatus='NEEDS_CHECKIN';
289: $returned_slot=\%slot;
1.206 albertel 290: $slot_name=$slot;
1.200 albertel 291: last;
292: }
293: }
1.202 albertel 294: if ($slotstatus eq 'NEEDS_CHECKIN' &&
1.226 albertel 295: &proctor_checked_in($slot_name,$returned_slot,$type)) {
1.202 albertel 296: &Apache::lonxml::debug("protoctor checked in");
1.200 albertel 297: $slotstatus='CAN_ANSWER';
298: }
1.226 albertel 299:
1.235 albertel 300: my ($is_correct,$got_grade,$checkedin);
1.226 albertel 301: if ($type eq 'Task') {
1.230 albertel 302: my $version=$Apache::lonhomework::history{'resource.0.version'};
1.231 albertel 303: $got_grade =
304: ($Apache::lonhomework::history{"resource.$version.0.status"}
305: =~ /^(?:pass|fail)$/);
1.235 albertel 306: $is_correct =
307: ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass'
308: || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );
1.226 albertel 309: $checkedin =
1.230 albertel 310: $Apache::lonhomework::history{"resource.$version.0.checkedin"};
1.226 albertel 311: } elsif ($type eq 'problem') {
1.231 albertel 312: $got_grade = 1;
1.226 albertel 313: $checkedin = $Apache::lonhomework::history{"resource.0.checkedin"};
314: }
315:
1.235 albertel 316: &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)");
317:
1.243 albertel 318: # no slot is currently open, and has been checked in for this version
319: # but hasn't got a grade, therefore must be awaiting a grade
320: if (!defined($slot_name)
321: && $checkedin
1.236 albertel 322: && !$got_grade) {
323: return ('WAITING_FOR_GRADE');
324: }
325:
1.226 albertel 326: if ($slotstatus eq 'NOT_IN_A_SLOT'
327: && $checkedin ) {
328:
1.231 albertel 329: if ($got_grade) {
1.209 albertel 330: return ('SHOW_ANSWER');
331: } else {
332: return ('WAITING_FOR_GRADE');
333: }
1.226 albertel 334:
1.207 albertel 335: }
1.235 albertel 336: if ( $is_correct) {
337: return ('SHOW_ANSWER');
338: }
1.225 albertel 339: if ( $status eq 'CANNOT_ANSWER' &&
340: ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) {
341: return ($status,$datemsg);
342: }
343:
1.206 albertel 344: return ($slotstatus,$datemsg,$slot_name,$returned_slot);
1.198 albertel 345: }
1.200 albertel 346:
1.92 bowersj2 347: # JB, 9/24/2002: Any changes in this function may require a change
348: # in lonnavmaps::resource::getDateStatus.
1.53 www 349: sub check_access {
1.145 albertel 350: my ($id) = @_;
351: my $date ='';
352: my $status;
353: my $datemsg = '';
354: my $lastdate = '';
355: my $type;
356: my $passed;
357:
1.204 albertel 358: if ($env{'request.state'} eq "construct") {
359: if ($env{'form.problemstate'}) {
360: if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
361: if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct' &&
1.167 albertel 362: lc($Apache::lonhomework::problemstatus) eq 'no')) {
1.168 albertel 363: return ('CANNOT_ANSWER',
1.174 www 364: &mt('is in this state due to author settings.'));
1.167 albertel 365: }
1.165 albertel 366: } else {
1.204 albertel 367: return ($env{'form.problemstate'},
1.174 www 368: &mt('is in this state due to author settings.'));
1.165 albertel 369: }
370: }
1.145 albertel 371: &Apache::lonxml::debug("in construction ignoring dates");
372: $status='CAN_ANSWER';
1.146 albertel 373: $datemsg=&mt('is in under construction');
1.163 albertel 374: # return ($status,$datemsg);
1.145 albertel 375: }
376:
377: &Apache::lonxml::debug("checking for part :$id:");
378: &Apache::lonxml::debug("time:".time);
1.152 albertel 379:
1.212 albertel 380: my ($symb)=&Apache::lonxml::whichuser();
381: &Apache::lonxml::debug("symb:".$symb);
382: #if ($env{'request.state'} ne "construct" && $symb ne '') {
1.204 albertel 383: if ($env{'request.state'} ne "construct") {
1.163 albertel 384: my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
385: if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
386: $status='INVALID_ACCESS';
387: $date=&mt("can not be accessed from your location.");
1.145 albertel 388: return($status,$date);
389: }
1.163 albertel 390:
1.226 albertel 391: foreach my $temp ("opendate","duedate","answerdate") {
1.163 albertel 392: $lastdate = $date;
1.247 albertel 393: if ($temp eq 'duedate') {
394: $date = &due_date($id);
395: } else {
396: $date = &Apache::lonnet::EXT("resource.$id.$temp");
397: }
398:
1.163 albertel 399: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
400: if ($thistype =~ /^(con_lost|no_such_host)/ ||
401: $date =~ /^(con_lost|no_such_host)/) {
402: $status='UNAVAILABLE';
403: $date=&mt("may open later.");
404: return($status,$date);
405: }
406: if ($thistype eq 'date_interval') {
407: if ($temp eq 'opendate') {
408: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
409: }
410: if ($temp eq 'answerdate') {
411: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
412: }
1.145 albertel 413: }
1.163 albertel 414: &Apache::lonxml::debug("found :$date: for :$temp:");
415: if ($date eq '') {
416: $date = &mt("an unknown date"); $passed = 0;
417: } elsif ($date eq 'con_lost') {
418: $date = &mt("an indeterminate date"); $passed = 0;
419: } else {
420: if (time < $date) { $passed = 0; } else { $passed = 1; }
421: $date = localtime $date;
1.145 albertel 422: }
1.163 albertel 423: if (!$passed) { $type=$temp; last; }
1.145 albertel 424: }
1.163 albertel 425: &Apache::lonxml::debug("have :$type:$passed:");
426: if ($passed) {
427: $status='SHOW_ANSWER';
428: $datemsg=$date;
429: } elsif ($type eq 'opendate') {
430: $status='CLOSED';
431: $datemsg = &mt("will open on")." $date";
432: } elsif ($type eq 'duedate') {
433: $status='CAN_ANSWER';
434: $datemsg = &mt("is due at")." $date";
435: } elsif ($type eq 'answerdate') {
436: $status='CLOSED';
437: $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145 albertel 438: }
439: }
1.212 albertel 440: if ($status eq 'CAN_ANSWER' ||
441: (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) {
1.145 albertel 442: #check #tries, and if correct.
443: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
444: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
445: if ( $tries eq '' ) { $tries = '0'; }
1.164 albertel 446: if ( $maxtries eq '' &&
1.204 albertel 447: $env{'request.state'} ne 'construct') { $maxtries = '2'; }
1.164 albertel 448: if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145 albertel 449: # if (correct and show prob status) or excused then CANNOT_ANSWER
450: if(($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/
451: &&
452: lc($Apache::lonhomework::problemstatus) ne 'no')
453: ||
454: $Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
455: $status = 'CANNOT_ANSWER';
456: }
1.121 albertel 457: }
1.181 albertel 458: if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.177 albertel 459: my $interval=&Apache::lonnet::EXT("resource.$id.interval");
460: &Apache::lonxml::debug("looking for interval $interval");
461: if ($interval) {
462: my $first_access=&Apache::lonnet::get_first_access('map');
463: &Apache::lonxml::debug("looking for accesstime $first_access");
464: if (!$first_access) {
465: $status='NOT_YET_VIEWED';
1.247 albertel 466: my $due_date = &due_date($id);
467: $datemsg=&seconds_to_human_length($due_date-time);
1.177 albertel 468: }
469: }
470: }
1.247 albertel 471:
1.133 albertel 472: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
473: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
474: # return ('UNCHECKEDOUT','needs to be checked out');
475: #}
1.54 www 476:
477:
1.145 albertel 478: &Apache::lonxml::debug("sending back :$status:$datemsg:");
479: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
480: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
481: $status='CAN_ANSWER';
1.146 albertel 482: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 483: }
1.106 albertel 484:
1.145 albertel 485: return ($status,$datemsg);
1.20 albertel 486: }
1.247 albertel 487: # this should work exactly like the copy in lonnavmaps.pm
1.246 albertel 488: sub due_date {
1.250 ! albertel 489: my ($part_id,$symb,$udom,$uname)=@_;
1.246 albertel 490: my $date;
1.250 ! albertel 491: my $interval= &Apache::lonnet::EXT("resource.$part_id.interval",$symb,
! 492: $udom,$uname);
1.247 albertel 493: &Apache::lonxml::debug("looking for interval $part_id $symb $interval");
1.250 ! albertel 494: my $due_date= &Apache::lonnet::EXT("resource.$part_id.duedate",$symb,
! 495: $udom,$uname);
1.247 albertel 496: &Apache::lonxml::debug("looking for due_date $part_id $symb $due_date");
497: if (defined($interval)) {
1.246 albertel 498: my $first_access=&Apache::lonnet::get_first_access('map',$symb);
1.247 albertel 499: if (defined($first_access)) {
500: $interval = $first_access+$interval;
501: $date = ($interval < $due_date)? $interval : $due_date;
502: } else {
503: $date = $due_date;
504: }
505: } else {
506: $date = $due_date;
1.246 albertel 507: }
508: return $date
509: }
510:
1.192 albertel 511: sub seconds_to_human_length {
512: my ($length)=@_;
513:
514: my $seconds=$length%60; $length=int($length/60);
515: my $minutes=$length%60; $length=int($length/60);
516: my $hours=$length%24; $length=int($length/24);
517: my $days=$length;
518:
519: my $timestr;
520: if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
521: if ($hours > 0) { $timestr.=($timestr?", ":"").
522: &mt('[quant,_1,hour]',$hours); }
523: if ($minutes > 0) { $timestr.=($timestr?", ":"").
524: &mt('[quant,_1,minute]',$minutes); }
525: if ($seconds > 0) { $timestr.=($timestr?", ":"").
526: &mt('[quant,_1,second]',$seconds); }
527: return $timestr;
528: }
529:
1.41 albertel 530: sub showhash {
1.145 albertel 531: my (%hash) = @_;
532: &showhashsubset(\%hash,'.');
533: return '';
1.79 albertel 534: }
535:
1.106 albertel 536: sub showarray {
537: my ($array)=@_;
538: my $string="(";
539: foreach my $elm (@{ $array }) {
1.193 albertel 540: if (ref($elm) eq 'ARRAY') {
541: $string.=&showarray($elm);
542: } elsif (ref($elm) eq 'HASH') {
543: $string.= "HASH --- \n<br />";
544: $string.= &showhashsubset($elm,'.');
1.106 albertel 545: } else {
546: $string.="$elm,"
547: }
548: }
549: chop($string);
550: $string.=")";
551: return $string;
552: }
553:
1.79 albertel 554: sub showhashsubset {
1.145 albertel 555: my ($hash,$keyre) = @_;
556: my $resultkey;
557: foreach $resultkey (sort keys %$hash) {
1.193 albertel 558: if ($resultkey !~ /$keyre/) { next; }
559: if (ref($$hash{$resultkey}) eq 'ARRAY' ) {
560: &Apache::lonxml::debug("$resultkey ---- ".
561: &showarray($$hash{$resultkey}));
562: } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
563: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
564: &showhashsubset($$hash{$resultkey},'.');
565: } else {
566: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.145 albertel 567: }
568: }
569: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
570: return '';
1.41 albertel 571: }
572:
573: sub setuppermissions {
1.204 albertel 574: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
575: my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.145 albertel 576: if (! $viewgrades &&
1.204 albertel 577: exists($env{'request.course.sec'}) &&
578: $env{'request.course.sec'} !~ /^\s*$/) {
579: $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
580: '/'.$env{'request.course.sec'});
1.145 albertel 581: }
1.244 albertel 582: $Apache::lonhomework::viewgrades = $viewgrades;
583:
1.185 albertel 584: if ($Apache::lonhomework::browse eq 'F' &&
1.204 albertel 585: $env{'form.devalidatecourseresdata'} eq 'on') {
1.185 albertel 586: my (undef,$courseid) = &Apache::lonxml::whichuser();
1.204 albertel 587: &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
588: $env{"course.$courseid.domain"});
1.185 albertel 589: }
1.244 albertel 590:
1.205 albertel 591: my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
592: if (! $modifygrades &&
593: exists($env{'request.course.sec'}) &&
594: $env{'request.course.sec'} !~ /^\s*$/) {
595: $modifygrades =
596: &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
597: '/'.$env{'request.course.sec'});
598: }
599: $Apache::lonhomework::modifygrades = $modifygrades;
1.244 albertel 600:
601: my $queuegrade = &Apache::lonnet::allowed('mqg',$env{'request.course.id'});
602: if (! $queuegrade &&
603: exists($env{'request.course.sec'}) &&
604: $env{'request.course.sec'} !~ /^\s*$/) {
605: $queuegrade =
606: &Apache::lonnet::allowed('qgr',$env{'request.course.id'}.
607: '/'.$env{'request.course.sec'});
608: }
609: $Apache::lonhomework::queuegrade = $queuegrade;
1.205 albertel 610: return '';
1.41 albertel 611: }
612:
613: sub setupheader {
1.120 albertel 614: my $request=$_[0];
1.197 albertel 615: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 616: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
617: &Apache::loncommon::no_cache($request);
618: }
1.196 albertel 619: # $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
620: # 'lastrevisiondate'));
1.120 albertel 621: $request->send_http_header;
622: return OK if $request->header_only;
623: return ''
1.41 albertel 624: }
1.35 albertel 625:
1.47 albertel 626: sub handle_save_or_undo {
1.145 albertel 627: my ($request,$problem,$result) = @_;
628: my $file = &Apache::lonnet::filelocation("",$request->uri);
629: my $filebak =$file.".bak";
630: my $filetmp =$file.".tmp";
631: my $error=0;
1.156 albertel 632:
633: &Apache::lonnet::correct_line_ends($result);
1.52 albertel 634:
1.204 albertel 635: if ($env{'form.Undo'} eq &mt('undo')) {
1.145 albertel 636: my $error=0;
637: if (!copy($file,$filetmp)) { $error=1; }
638: if ((!$error) && (!copy($filebak,$file))) { $error=1; }
639: if ((!$error) && (!move($filetmp,$filebak))) { $error=1; }
640: if (!$error) {
1.171 albertel 641: &Apache::lonxml::info("<p><b>".&mt("Undid changes, Switched")." $filebak ".&mt("and")." $file</b></p>");
1.145 albertel 642: } else {
1.171 albertel 643: &Apache::lonxml::info("<p><font color=\"red\" size=\"+1\"><b>".&mt("Unable to undo, unable to switch")." $filebak ".&mt("and")." $file</b></font></p>");
1.145 albertel 644: $error=1;
645: }
1.52 albertel 646: } else {
1.145 albertel 647: my $fs=Apache::File->new(">$filebak");
648: if (defined($fs)) {
649: print $fs $$problem;
1.171 albertel 650: &Apache::lonxml::info("<b>".&mt("Making Backup to").
651: " $filebak</b>");
1.145 albertel 652: } else {
1.171 albertel 653: &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".&mt("Unable to make backup")." $filebak</b></font>");
1.145 albertel 654: $error=2;
655: }
656: my $fh=Apache::File->new(">$file");
657: if (defined($fh)) {
658: print $fh $$result;
1.171 albertel 659: &Apache::lonxml::info("<b>".&mt("Saving Modifications to").
660: " $file</b>");
1.145 albertel 661: } else {
1.171 albertel 662: &Apache::lonxml::info("<font color=\"red\" size=\"+1\"><b>".
663: &mt("Unable to write to")." $file</b></font>");
1.145 albertel 664: $error|=4;
665: }
1.52 albertel 666: }
1.145 albertel 667: return $error;
1.64 albertel 668: }
669:
1.101 albertel 670: sub analyze_header {
671: my ($request) = @_;
1.238 albertel 672: my $result =
1.240 albertel 673: &Apache::loncommon::start_page('Analyzing a problem',undef);
674:
1.238 albertel 675: $result .=
676: &Apache::lonxml::message_location().'
1.101 albertel 677: <form name="lonhomework" method="POST" action="'.
1.204 albertel 678: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 679: &Apache::structuretags::remember_problem_state().'
1.146 albertel 680: <input type="submit" name="problemmode" value="'.&mt("EditXML").'" />
681: <input type="submit" name="problemmode" value="'.&mt('Edit').'" />
1.101 albertel 682: <hr />
1.146 albertel 683: <input type="submit" name="submit" value="'.&mt("View").'" />
1.101 albertel 684: <hr />
685: </form>';
1.171 albertel 686: &Apache::lonxml::add_messages(\$result);
1.101 albertel 687: $request->print($result);
688: $request->rflush();
689: }
690:
1.109 albertel 691: sub analyze_footer {
692: my ($request) = @_;
1.237 albertel 693: $request->print(&Apache::loncommon::end_page());
1.109 albertel 694: $request->rflush();
695: }
696:
1.74 albertel 697: sub analyze {
1.101 albertel 698: my ($request,$file) = @_;
699: &Apache::lonxml::debug("Analyze");
700: my $result;
701: my %overall;
1.219 www 702: my %seedexample;
1.101 albertel 703: my %allparts;
1.204 albertel 704: my $rndseed=$env{'form.rndseed'};
1.101 albertel 705: &analyze_header($request);
1.114 albertel 706: my %prog_state=
1.146 albertel 707: &Apache::lonhtmlcommon::Create_PrgWin($request,&mt('Analyze Progress'),
708: &mt('Getting Problem Variants'),
1.204 albertel 709: $env{'form.numtoanalyze'},
1.175 albertel 710: 'inline',undef);
1.204 albertel 711: for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
1.114 albertel 712: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,
1.146 albertel 713: &mt('last problem'));
1.182 albertel 714: if (&Apache::loncommon::connection_aborted($request)) { return; }
1.219 www 715: my $thisseed=$i+$rndseed;
1.101 albertel 716: my $subresult=&Apache::lonnet::ssi($request->uri,
717: ('grade_target' => 'analyze'),
1.219 www 718: ('rndseed' => $thisseed));
1.101 albertel 719: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
720: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 721: my @parts;
722: if (defined(@{ $analyze{'parts'} })) {
723: @parts=@{ $analyze{'parts'} };
724: }
1.101 albertel 725: foreach my $part (@parts) {
726: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 727: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
728: $analyze{$part.'.type'} eq 'stringresponse' ||
729: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.219 www 730: my $concatanswer=join("\0",@{ $analyze{$part.'.answer'} });
731: if (($concatanswer eq '') || ($concatanswer=~/^\@/)) {
732: @{$analyze{$part.'.answer'}}=('<font color="red">'.&mt('Error').'</font>');
733: }
1.101 albertel 734: push( @{ $overall{$part.'.answer'} },
735: [@{ $analyze{$part.'.answer'} }]);
1.219 www 736: $seedexample{join("\0",@{ $analyze{$part.'.answer'}})}=$thisseed;
1.101 albertel 737: }
738: }
739: }
1.114 albertel 740: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,
1.146 albertel 741: &mt('Analyzing Results'));
1.175 albertel 742: $request->print('<hr />'.&mt('List of possible answers').': ');
1.134 albertel 743: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 744: if (defined(@{ $overall{$part.'.answer'} })) {
1.132 albertel 745: my $num_cols=scalar(@{ $overall{$part.'.answer'}->[0] });
1.221 albertel 746: $request->print('<table class="thinborder"><tr><th colspan="'.($num_cols+1).'">'.&mt('Part').' '.$part.'</th></tr>');
1.130 albertel 747: my %frequency;
1.109 albertel 748: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'} })) {
1.132 albertel 749: $frequency{join("\0",@{ $answer })}++;
1.130 albertel 750: }
1.219 www 751: $request->print('<tr><th colspan="'.($num_cols).'">'.&mt('Answer').'</th><th>'.&mt('Frequency').'<br />('
752: .&mt('click for example').')</th></tr>');
1.132 albertel 753: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
1.220 albertel 754: $request->print('<tr><td class="center">'.
755: join('</td><td class="center">',split("\0",$answer)).
756: '</td><td class="center"><a href="'.$request->uri.'?rndseed='.$seedexample{$answer}.'">'.$frequency{$answer}.
1.219 www 757: '</a></td></tr>');
1.109 albertel 758: }
759: $request->print('</table>');
760: } else {
1.162 albertel 761: $request->print('<p>'.&mt('Response').' '.$part.' '.
1.146 albertel 762: &mt('is not analyzable at this time').'</p>');
1.101 albertel 763: }
764: }
1.130 albertel 765: if (scalar(keys(%allparts)) == 0 ) {
1.215 raeburn 766: $request->print('<p>'.&mt('Found no analyzable responses in this problem, currently only Numerical, Formula and String response styles are supported.').'</p>');
1.130 albertel 767: }
1.114 albertel 768: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 769: &analyze_footer($request);
1.101 albertel 770: &Apache::lonhomework::showhash(%overall);
771: return $result;
1.74 albertel 772: }
773:
1.64 albertel 774: sub editxmlmode {
1.145 albertel 775: my ($request,$file) = @_;
776: my $result;
777: my $problem=&Apache::lonnet::getfile($file);
778: if ($problem eq -1) {
1.146 albertel 779: &Apache::lonxml::error("<b> ".&mt('Unable to find').
780: " <i>$file</i></b>");
1.145 albertel 781: $problem='';
782: }
1.204 albertel 783: if (defined($env{'form.editxmltext'}) || defined($env{'form.Undo'})) {
1.145 albertel 784: my $error=&handle_save_or_undo($request,\$problem,
1.204 albertel 785: \$env{'form.editxmltext'});
1.145 albertel 786: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
787: }
1.204 albertel 788: &Apache::lonhomework::showhashsubset(\%env,'^form');
789: if ( $env{'form.submit'} eq &mt('Submit Changes and View') ) {
790: &Apache::lonhomework::showhashsubset(\%env,'^form');
791: $env{'form.problemmode'}='View';
1.145 albertel 792: &renderpage($request,$file);
793: } else {
794: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1.160 www 795: my $xml_help = '<table><tr><td>'.
796: &Apache::loncommon::helpLatexCheatsheet("Problem_Editor_XML_Index",
797: "Problem Editing Help").
798: '</td><td>'.
1.178 albertel 799: &Apache::loncommon::help_open_menu('',undef,undef,undef,5,'Authoring').
800: '</td></tr></table>';
1.145 albertel 801: if ($cols > 80) { $cols = 80; }
802: if ($cols < 70) { $cols = 70; }
803: if ($rows < 20) { $rows = 20; }
1.238 albertel 804: my $start_page =
805: &Apache::loncommon::start_page("EditXML $file",
1.240 albertel 806: &Apache::edit::js_change_detection());
1.227 albertel 807:
1.238 albertel 808: $result.=$start_page.
1.213 albertel 809: &renderpage($request,$file,['no_output_web'],1).
810: &Apache::lonxml::message_location().'
1.227 albertel 811: <form '.&Apache::edit::form_change_detection().' name="lonhomework" method="POST" action="'.
1.204 albertel 812: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 813: &Apache::structuretags::remember_problem_state().'
1.146 albertel 814: <input type="hidden" name="problemmode" value="'.&mt('EditXML').'" />
1.170 matthew 815: <input type="submit" name="problemmode" accesskey="d" value="'.&mt('Discard Edits and View').'" />
1.232 albertel 816: <input type="submit" '.&Apache::edit::submit_ask_anyway().'name="problemmode" accesskey="e" value="'.&mt('Edit').'" />
1.228 albertel 817: <input type="submit" name="Undo" accesskey="u" value="'.&mt('undo').'" />
1.64 albertel 818: <hr />
1.170 matthew 819: <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
820: <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
1.64 albertel 821: <hr />
1.110 albertel 822: ' . $xml_help . '
1.227 albertel 823: <textarea '.&Apache::edit::element_change_detection().' style="width:100%" rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
1.229 albertel 824: &HTML::Entities::encode($problem,'<>&"').'</textarea><br />
825: <input type="submit" name="submit" accesskey="s" value="'.&mt('Submit Changes').'" />
826: <input type="submit" name="submit" accesskey="v" value="'.&mt('Submit Changes and View').'" />
1.238 albertel 827: </form>'.&Apache::loncommon::end_page();
1.171 albertel 828: &Apache::lonxml::add_messages(\$result);
1.145 albertel 829: $request->print($result);
830: }
831: return '';
1.47 albertel 832: }
1.189 albertel 833:
1.188 foxr 834: #
835: # Render the page in whatever target desired.
836: #
1.41 albertel 837: sub renderpage {
1.213 albertel 838: my ($request,$file,$targets,$return_string) = @_;
1.52 albertel 839:
1.211 albertel 840: my @targets = @{$targets || [&get_target()]};
1.204 albertel 841: &Apache::lonhomework::showhashsubset(\%env,'form.');
1.145 albertel 842: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.171 albertel 843: my $overall_result;
1.145 albertel 844: foreach my $target (@targets) {
1.183 albertel 845: # FIXME need to do something intelligent when a problem goes
846: # from viewable to not viewable due to map conditions
847: #&setuppermissions();
848: #if ( $Apache::lonhomework::browse ne '2'
849: # && $Apache::lonhomework::browse ne 'F' ) {
850: # $request->print(" You most likely shouldn't see me.");
851: #}
1.145 albertel 852: #my $t0 = [&gettimeofday()];
1.211 albertel 853: my $output=1;
854: if ($target eq 'no_output_web') {
855: $target = 'web'; $output=0;
856: }
1.145 albertel 857: my $problem=&Apache::lonnet::getfile($file);
1.222 albertel 858: my $result;
1.145 albertel 859: if ($problem eq -1) {
1.222 albertel 860: my $filename=(split('/',$file))[-1];
861: $result.="<b> ".&mt('Unable to find')." <i>$filename</i></b>";
1.145 albertel 862: $problem='';
863: }
1.52 albertel 864:
1.145 albertel 865: my %mystyle;
866: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
867: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
1.204 albertel 868: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
1.145 albertel 869:
870: &Apache::lonxml::debug("Should be parsing now");
1.222 albertel 871: $result .= &Apache::lonxml::xmlparse($request, $target, $problem,
872: &setup_vars($target),%mystyle);
1.145 albertel 873: undef($Apache::lonhomework::parsing_a_problem);
1.214 albertel 874: if (!$output) { $result = ''; }
1.145 albertel 875: #$request->print("Result follows:");
876: if ($target eq 'modified') {
877: &handle_save_or_undo($request,\$problem,\$result);
878: } else {
879: if ($target eq 'analyze') {
880: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
881: undef(%Apache::lonhomework::analyze);
882: }
883: #my $td=&tv_interval($t0);
884: #if ( $Apache::lonxml::debug) {
885: #$result =~ s:</body>::;
886: #$result.="<br />Spent $td seconds processing target $target\n</body>";
887: #}
1.171 albertel 888: # $request->print($result);
889: $overall_result.=$result;
890: # $request->rflush();
1.145 albertel 891: }
892: #$request->print(":Result ends");
893: #my $td=&tv_interval($t0);
1.52 albertel 894: }
1.213 albertel 895: if (!$return_string) {
896: &Apache::lonxml::add_messages(\$overall_result);
897: $request->print($overall_result);
898: $request->rflush();
899: } else {
900: return $overall_result;
901: }
1.41 albertel 902: }
903:
1.42 albertel 904: # with no arg it returns a HTML <option> list of the template titles
905: # with one arg it returns the filename associated with the arg passed
906: sub get_template_list {
1.145 albertel 907: my ($namewanted,$extension) = @_;
908: my $result;
909: my @allnames;
910: &Apache::lonxml::debug("Looking for :$extension:");
911: foreach my $file (</home/httpd/html/res/adm/includes/templates/*.$extension>) {
912: my $name=&Apache::lonnet::metadata($file,'title');
913: if ($namewanted && ($name eq $namewanted)) {
914: $result=$file;
915: last;
916: } else {
917: if ($name) { push (@allnames, $name); }
918: }
919: }
920: if (@allnames && !$result) {
1.146 albertel 921: $result="<option>".&mt("Select a")." $extension ".&mt('template')."</option>\n<option>".
1.145 albertel 922: join('</option><option>',sort(@allnames)).'</option>';
1.42 albertel 923: }
1.145 albertel 924: return $result;
1.42 albertel 925: }
926:
927: sub newproblem {
1.65 matthew 928: my ($request) = @_;
929: my $extension=$request->uri;
930: $extension=~s:^.*\.([\w]+)$:$1:;
931: &Apache::lonxml::debug("Looking for :$extension:");
1.131 albertel 932: my $templatelist=&get_template_list('',$extension);
1.204 albertel 933: if ($env{'form.template'} &&
934: $env{'form.template'} ne "Select a $extension template") {
1.65 matthew 935: use File::Copy;
1.204 albertel 936: my $file = &get_template_list($env{'form.template'},$extension);
1.65 matthew 937: my $dest = &Apache::lonnet::filelocation("",$request->uri);
938: copy($file,$dest);
939: &renderpage($request,$dest);
1.204 albertel 940: } elsif($env{'form.newfile'} && !$templatelist) {
1.131 albertel 941: # I don't like hard-coded filenames but for now, this will work.
942: use File::Copy;
943: my $templatefilename =
944: $request->dir_config('lonIncludes').'/templates/blank.problem';
945: &Apache::lonxml::debug("$templatefilename");
946: my $dest = &Apache::lonnet::filelocation("",$request->uri);
947: copy($templatefilename,$dest);
948: &renderpage($request,$dest);
1.85 albertel 949: } else {
1.176 albertel 950: my $url=&HTML::Entities::encode($request->uri,'<>&"');
951: my $shownurl=$url;
1.157 albertel 952: $shownurl=~s-^/~-/priv/-;
1.65 matthew 953: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 954: my $errormsg;
1.204 albertel 955: if ($env{'form.newfile'}) {
1.146 albertel 956: $errormsg='<p><font color="red">'.&mt('You did not select a template.').'</font></p>'."\n";
1.128 albertel 957: }
1.85 albertel 958: my $instructions;
1.240 albertel 959: my $start_page =
960: &Apache::loncommon::start_page("Create New $extension");
1.146 albertel 961: if ($templatelist) { $instructions=&mt(", select a template from the pull-down menu below.").'<br />'.&mt("Then");}
1.147 albertel 962: my %lt=&Apache::lonlocal::texthash( 'create' => 'Creating a new',
1.146 albertel 963: 'resource' => 'resource',
964: 'requested' => 'The requested file',
965: 'not exist' => 'currently does not exist',
966: 'createnew' => 'To create a new',
967: 'click' => 'click on the',
968: 'Create' => 'Create',
969: 'button' => 'button');
1.65 matthew 970: $request->print(<<ENDNEWPROBLEM);
1.240 albertel 971: $start_page
1.148 albertel 972: <h1>$lt{'create'} $extension $lt{'resource'}</h1>
1.128 albertel 973: $errormsg
1.157 albertel 974: $lt{'requested'} <tt>$shownurl</tt> $lt{'not exist'}.
1.105 www 975: <p>
1.146 albertel 976: <b>$lt{'createnew'} $extension$instructions $lt{'click'} "$lt{'Create'} $extension" $lt{'button'}.</b>
1.105 www 977: </p>
978: <p><form action="$url" method="POST">
1.42 albertel 979: ENDNEWPROBLEM
1.85 albertel 980: if (defined($templatelist)) {
981: $request->print("<select name=\"template\">$templatelist</select>");
982: }
1.146 albertel 983: $request->print("<br /><input type=\"submit\" name=\"newfile\" value=\"".&mt('Create')." $extension\" />");
1.238 albertel 984: $request->print("</form></p>".&Apache::loncommon::end_page());
1.65 matthew 985: }
986: return '';
1.42 albertel 987: }
988:
1.41 albertel 989: sub handler {
1.145 albertel 990: #my $t0 = [&gettimeofday()];
991: my $request=$_[0];
1.223 albertel 992: $Apache::lonxml::request=$request;
1.204 albertel 993: $Apache::lonxml::debug=$env{'user.debug'};
994: $env{'request.uri'}=$request->uri;
1.180 albertel 995: &setuppermissions();
1.145 albertel 996: # some times multiple problemmodes are submitted, need to select
997: # the last one
1.204 albertel 998: if ( defined($env{'form.problemmode'}) && ref($env{'form.problemmode'}) ) {
999: my $mode=$env{'form.problemmode'}->[-1];
1000: undef $env{'form.problemmode'};
1001: $env{'form.problemmode'}=$mode;
1.145 albertel 1002: }
1.193 albertel 1003:
1.145 albertel 1004: my $file=&Apache::lonnet::filelocation("",$request->uri);
1005:
1006: #check if we know where we are
1.204 albertel 1007: if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) {
1.145 albertel 1008: # if we are browsing we might not be able to know where we are
1.173 albertel 1009: if ($Apache::lonhomework::browse ne 'F' &&
1.204 albertel 1010: $env{'request.state'} ne "construct") {
1.145 albertel 1011: #should know where we are, so ask
1.190 albertel 1012: $request->internal_redirect('/adm/ambiguous'); return OK;
1.145 albertel 1013: }
1014: }
1.187 albertel 1015: if (&setupheader($request)) { return OK; }
1.244 albertel 1016: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
1.204 albertel 1017: &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
1.145 albertel 1018: my ($symb) = &Apache::lonxml::whichuser();
1019: &Apache::lonxml::debug('symb is '.$symb);
1.204 albertel 1020: if ($env{'request.state'} eq "construct") {
1.145 albertel 1021: if ( -e $file ) {
1022: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1023: ['problemmode']);
1.204 albertel 1024: if (!(defined $env{'form.problemmode'})) {
1.145 albertel 1025: #first visit to problem in construction space
1.204 albertel 1026: $env{'form.problemmode'}='View';
1.145 albertel 1027: &renderpage($request,$file);
1.204 albertel 1028: } elsif ($env{'form.problemmode'} eq &mt('EditXML') ||
1029: $env{'form.problemmode'} eq 'EditXML') {
1.145 albertel 1030: &editxmlmode($request,$file);
1.204 albertel 1031: } elsif ($env{'form.problemmode'} eq &mt('Calculate answers')) {
1.145 albertel 1032: &analyze($request,$file);
1033: } else {
1034: &renderpage($request,$file);
1035: }
1036: } else {
1037: # requested file doesn't exist in contruction space
1038: &newproblem($request);
1039: }
1040: } else {
1041: # just render the page normally outside of construction space
1042: &Apache::lonxml::debug("not construct");
1.52 albertel 1043: &renderpage($request,$file);
1.41 albertel 1044: }
1.145 albertel 1045: #my $td=&tv_interval($t0);
1046: #&Apache::lonxml::debug("Spent $td seconds processing");
1047: # always turn off debug messages
1048: $Apache::lonxml::debug=0;
1049: return OK;
1.52 albertel 1050:
1.1 albertel 1051: }
1052:
1053: 1;
1054: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>