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