Annotation of loncom/homework/lonhomework.pm, revision 1.335
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.335 ! www 4: # $Id: lonhomework.pm,v 1.334 2011/12/20 22:46:06 raeburn 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.321 www 50: use Apache::functionplotresponse();
1.169 albertel 51: use Apache::drawimage();
1.26 www 52: use Apache::Constants qw(:common);
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.282 albertel 56: use HTML::Entities();
57: use File::Copy();
1.188 foxr 58:
1.189 albertel 59: # FIXME - improve commenting
1.188 foxr 60:
1.43 albertel 61:
1.69 harris41 62: BEGIN {
1.145 albertel 63: &Apache::lonxml::register_insert();
1.43 albertel 64: }
65:
1.188 foxr 66:
1.276 foxr 67: =pod
68:
69: =item set_bubble_lines()
70:
71: Called at analysis time to set the bubble lines
72: hash for the problem.. This should be called in the
73: end_problemtype tag in analysis mode.
74:
75: We fetch the hash of part id counters from lonxml
76: and push them into analyze:{part_id.bubble_lines}.
77:
78: =cut
79:
80: sub set_bubble_lines {
81: my %bubble_counters = &Apache::lonxml::get_bubble_line_hash();
82:
83: foreach my $key (keys(%bubble_counters)) {
84: $Apache::lonhomework::analyze{"$key.bubble_lines"} =
85: $bubble_counters{"$key"};
86: }
87: }
88:
1.301 jms 89: #
90: # Decides what targets to render for.
91: # Implicit inputs:
92: # Various session environment variables:
93: # request.state - published - is a /res/ resource
94: # uploaded - is a /uploaded/ resource
95: # contruct - is a /priv/ resource
96: # form.grade_target - a form parameter requesting a specific target
1.5 albertel 97: sub get_target {
1.204 albertel 98: &Apache::lonxml::debug("request.state = $env{'request.state'}");
99: if( defined($env{'form.grade_target'})) {
100: &Apache::lonxml::debug("form.grade_target= $env{'form.grade_target'}");
1.188 foxr 101: } else {
1.189 albertel 102: &Apache::lonxml::debug("form.grade_target <undefined>");
1.188 foxr 103: }
1.204 albertel 104: if (($env{'request.state'} eq "published") ||
105: ($env{'request.state'} eq "uploaded")) {
106: if ( defined($env{'form.grade_target'} )
107: && ($env{'form.grade_target'} eq 'tex')) {
108: return ($env{'form.grade_target'});
109: } elsif ( defined($env{'form.grade_target'} )
1.145 albertel 110: && ($Apache::lonhomework::viewgrades eq 'F' )) {
1.207 albertel 111: return ($env{'form.grade_target'});
1.244 albertel 112: } elsif ( $env{'form.grade_target'} eq 'webgrade'
113: && ($Apache::lonhomework::queuegrade eq 'F' )) {
114: return ($env{'form.grade_target'});
1.320 raeburn 115: } elsif ($env{'form.grade_target'} eq 'answer') {
116: if ($env{'form.answer_output_mode'} eq 'tex') {
117: return ($env{'form.grade_target'});
118: }
119: }
1.207 albertel 120: if ($env{'form.webgrade'} &&
1.244 albertel 121: ($Apache::lonhomework::modifygrades eq 'F'
122: || $Apache::lonhomework::queuegrade eq 'F' )) {
1.207 albertel 123: return ('grade','webgrade');
1.145 albertel 124: }
1.204 albertel 125: if ( defined($env{'form.submitted'}) &&
126: ( !defined($env{'form.newrandomization'}))) {
1.217 albertel 127: return ('grade', 'web');
1.145 albertel 128: } else {
1.217 albertel 129: return ('web');
1.145 albertel 130: }
1.204 albertel 131: } elsif ($env{'request.state'} eq "construct") {
1.323 www 132: #
133: # We are in construction space, editing and testing problems
134: #
1.204 albertel 135: if ( defined($env{'form.grade_target'}) ) {
136: return ($env{'form.grade_target'});
1.145 albertel 137: }
1.204 albertel 138: if ( defined($env{'form.preview'})) {
139: if ( defined($env{'form.submitted'})) {
1.323 www 140: #
141: # We are doing a problem preview
142: #
1.145 albertel 143: return ('grade', 'web');
144: } else {
145: return ('web');
146: }
147: } else {
1.267 albertel 148: if ($env{'form.problemstate'} eq 'WEB_GRADE') {
149: return ('grade','webgrade','answer');
1.324 www 150: } elsif ($env{'form.problemmode'} eq 'view') {
151: return ('grade','web','answer');
1.323 www 152: } elsif ($env{'form.problemmode'} eq 'saveview') {
153: return ('modified','web','answer');
154: } elsif ($env{'form.problemmode'} eq 'discard') {
155: return ('web','answer');
156: } elsif (($env{'form.problemmode'} eq 'saveedit') ||
157: ($env{'form.problemmode'} eq 'undo')) {
158: return ('modified','no_output_web','edit');
159: } elsif ($env{'form.problemmode'} eq 'edit') {
160: return ('no_output_web','edit');
1.145 albertel 161: } else {
162: return ('web');
163: }
1.323 www 164: }
165: #
166: # End of Construction Space
167: #
1.15 albertel 168: }
1.323 www 169: #
170: # Huh? We are nowhere, so do nothing.
171: #
1.145 albertel 172: return ();
1.5 albertel 173: }
174:
1.3 albertel 175: sub setup_vars {
1.145 albertel 176: my ($target) = @_;
177: return ';'
1.11 albertel 178: # return ';$external::target='.$target.';';
1.2 albertel 179: }
180:
1.200 albertel 181: sub proctor_checked_in {
1.226 albertel 182: my ($slot_name,$slot,$type)=@_;
183: my @possible_proctors=split(",",$slot->{'proctor'});
184:
1.248 albertel 185: return 1 if (!@possible_proctors);
186:
1.226 albertel 187: my $key;
188: if ($type eq 'Task') {
1.230 albertel 189: my $version=$Apache::lonhomework::history{'resource.0.version'};
190: $key ="resource.$version.0.checkedin";
1.226 albertel 191: } elsif ($type eq 'problem') {
192: $key ='resource.0.checkedin';
193: }
1.249 albertel 194: # backward compatability, used to be username@domain,
195: # now is username:domain
196: my $who = $Apache::lonhomework::history{$key};
197: if ($who !~ /:/) {
198: $who =~ tr/@/:/;
199: }
1.226 albertel 200: foreach my $possible (@possible_proctors) {
1.249 albertel 201: if ($who eq $possible
1.226 albertel 202: && $Apache::lonhomework::history{$key.'.slot'} eq $slot_name) {
1.202 albertel 203: return 1;
204: }
205: }
1.226 albertel 206:
1.200 albertel 207: return 0;
208: }
209:
1.226 albertel 210: sub check_slot_access {
211: my ($id,$type)=@_;
212:
1.207 albertel 213: # does it pass normal muster
1.226 albertel 214: my ($status,$datemsg)=&check_access($id);
215:
1.252 albertel 216: my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
1.251 albertel 217: if ($useslots ne 'resource' && $useslots ne 'map'
218: && $useslots ne 'map_map') {
1.226 albertel 219: return ($status,$datemsg);
220: }
221:
1.200 albertel 222: if ($status eq 'SHOW_ANSWER' ||
223: $status eq 'CLOSED' ||
224: $status eq 'INVALID_ACCESS' ||
225: $status eq 'UNAVAILABLE') {
226: return ($status,$datemsg);
227: }
1.204 albertel 228: if ($env{'request.state'} eq "construct") {
1.203 albertel 229: return ($status,$datemsg);
230: }
1.226 albertel 231:
232: if ($type eq 'Task') {
233: my $version=$Apache::lonhomework::history{'resource.version'};
1.230 albertel 234: if ($Apache::lonhomework::history{"resource.$version.0.checkedin"} &&
235: $Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass') {
1.226 albertel 236: return ('SHOW_ANSWER');
237: }
1.207 albertel 238: }
1.226 albertel 239:
1.288 raeburn 240: my $availablestudent = &Apache::lonnet::EXT("resource.0.availablestudent");
241: my $available = &Apache::lonnet::EXT("resource.0.available");
242: my @slots= (split(':',$availablestudent),split(':',$available));
1.210 albertel 243:
1.200 albertel 244: # if (!@slots) {
245: # return ($status,$datemsg);
246: # }
247: my $slotstatus='NOT_IN_A_SLOT';
1.206 albertel 248: my ($returned_slot,$slot_name);
1.334 raeburn 249: my $now = time;
250: my $num_usable_slots = 0;
1.210 albertel 251: foreach my $slot (@slots) {
1.241 albertel 252: $slot =~ s/(^\s*|\s*$)//g;
1.201 albertel 253: &Apache::lonxml::debug("getting $slot");
1.200 albertel 254: my %slot=&Apache::lonnet::get_slot($slot);
1.201 albertel 255: &Apache::lonhomework::showhash(%slot);
1.334 raeburn 256: next if ($slot{'endtime'} < $now);
257: $num_usable_slots ++;
258: if ($slot{'starttime'} < $now &&
259: $slot{'endtime'} > $now &&
1.297 raeburn 260: &Apache::loncommon::check_ip_acc($slot{'ip'})) {
1.202 albertel 261: &Apache::lonxml::debug("$slot is good");
262: $slotstatus='NEEDS_CHECKIN';
263: $returned_slot=\%slot;
1.206 albertel 264: $slot_name=$slot;
1.200 albertel 265: last;
1.334 raeburn 266: }
1.200 albertel 267: }
1.202 albertel 268: if ($slotstatus eq 'NEEDS_CHECKIN' &&
1.226 albertel 269: &proctor_checked_in($slot_name,$returned_slot,$type)) {
1.322 raeburn 270: &Apache::lonxml::debug("proctor checked in");
271: $slotstatus=$status;
1.200 albertel 272: }
1.226 albertel 273:
1.235 albertel 274: my ($is_correct,$got_grade,$checkedin);
1.226 albertel 275: if ($type eq 'Task') {
1.230 albertel 276: my $version=$Apache::lonhomework::history{'resource.0.version'};
1.231 albertel 277: $got_grade =
278: ($Apache::lonhomework::history{"resource.$version.0.status"}
279: =~ /^(?:pass|fail)$/);
1.235 albertel 280: $is_correct =
281: ($Apache::lonhomework::history{"resource.$version.0.status"} eq 'pass'
282: || $Apache::lonhomework::history{"resource.0.solved"} =~ /^correct_/ );
1.226 albertel 283: $checkedin =
1.230 albertel 284: $Apache::lonhomework::history{"resource.$version.0.checkedin"};
1.226 albertel 285: } elsif ($type eq 'problem') {
1.252 albertel 286: $got_grade = 1;
287: $checkedin = $Apache::lonhomework::history{"resource.0.checkedin"};
288: $is_correct =
289: ($Apache::lonhomework::history{"resource.0.solved"} =~/^correct_/);
1.226 albertel 290: }
291:
1.235 albertel 292: &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)");
293:
1.243 albertel 294: # no slot is currently open, and has been checked in for this version
295: # but hasn't got a grade, therefore must be awaiting a grade
296: if (!defined($slot_name)
297: && $checkedin
1.236 albertel 298: && !$got_grade) {
299: return ('WAITING_FOR_GRADE');
300: }
301:
1.309 raeburn 302: # Previously used slot is no longer open, and has been checked in for this version.
303: # However, the problem is not closed, and potentially, another slot might be
304: # used to gain access to it to work on it, until the due date is reached, and the
305: # problem then becomes CLOSED. Therefore return the slotstatus -
1.334 raeburn 306: # (which will be one of: NOT_IN_A_SLOT, RESERVABLE, RESERVABLE_LATER, or NOTRESERVABLE.
307: if (!defined($slot_name) && $type eq 'problem') {
308: if ($slotstatus eq 'NOT_IN_A_SLOT') {
309: if (!$num_usable_slots) {
310: if ($env{'request.course.id'}) {
311: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
312: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
313: my ($symb)=&Apache::lonnet::whichuser();
314: $slotstatus = 'NOTRESERVABLE';
315: my ($reservable_now_order,$reservable_now,$reservable_future_order,
316: $reservable_future) =
317: &Apache::loncommon::get_future_slots($cnum,$cdom,$now,$symb);
318: if ((ref($reservable_now_order) eq 'ARRAY') && (ref($reservable_now) eq 'HASH')) {
319: if (@{$reservable_now_order} > 0) {
320: $slotstatus = 'RESERVABLE';
321: $datemsg = $reservable_now->{$reservable_now_order->[-1]}{'endreserve'};
322: }
323: }
324: unless ($slotstatus eq 'RESERVABLE') {
325: if ((ref($reservable_future_order) eq 'ARRAY') && (ref($reservable_future) eq 'HASH')) {
326: if (@{$reservable_future_order} > 0) {
327: $slotstatus = 'RESERVABLE_LATER';
328: $datemsg = $reservable_future->{$reservable_future_order->[0]}{'startreserve'};
329: }
330: }
331: }
332: }
333: }
334: }
335: return ($slotstatus,$datemsg);
1.252 albertel 336: }
337:
1.226 albertel 338: if ($slotstatus eq 'NOT_IN_A_SLOT'
339: && $checkedin ) {
340:
1.231 albertel 341: if ($got_grade) {
1.209 albertel 342: return ('SHOW_ANSWER');
343: } else {
344: return ('WAITING_FOR_GRADE');
345: }
1.226 albertel 346:
1.207 albertel 347: }
1.255 albertel 348:
1.235 albertel 349: if ( $is_correct) {
1.255 albertel 350: if ($type eq 'problem') {
351: return ($status);
352: }
1.235 albertel 353: return ('SHOW_ANSWER');
354: }
1.255 albertel 355:
1.225 albertel 356: if ( $status eq 'CANNOT_ANSWER' &&
357: ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) {
358: return ($status,$datemsg);
359: }
360:
1.206 albertel 361: return ($slotstatus,$datemsg,$slot_name,$returned_slot);
1.198 albertel 362: }
1.200 albertel 363:
1.301 jms 364: # JB, 9/24/2002: Any changes in this function may require a change
365: # in lonnavmaps::resource::getDateStatus.
1.53 www 366: sub check_access {
1.145 albertel 367: my ($id) = @_;
368: my $date ='';
369: my $status;
370: my $datemsg = '';
371: my $lastdate = '';
372: my $type;
373: my $passed;
374:
1.204 albertel 375: if ($env{'request.state'} eq "construct") {
376: if ($env{'form.problemstate'}) {
377: if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
1.277 albertel 378: if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct'
379: && &hide_problem_status())) {
1.168 albertel 380: return ('CANNOT_ANSWER',
1.174 www 381: &mt('is in this state due to author settings.'));
1.167 albertel 382: }
1.165 albertel 383: } else {
1.204 albertel 384: return ($env{'form.problemstate'},
1.174 www 385: &mt('is in this state due to author settings.'));
1.165 albertel 386: }
387: }
1.145 albertel 388: &Apache::lonxml::debug("in construction ignoring dates");
389: $status='CAN_ANSWER';
1.146 albertel 390: $datemsg=&mt('is in under construction');
1.163 albertel 391: # return ($status,$datemsg);
1.145 albertel 392: }
393:
394: &Apache::lonxml::debug("checking for part :$id:");
395: &Apache::lonxml::debug("time:".time);
1.152 albertel 396:
1.261 albertel 397: my ($symb)=&Apache::lonnet::whichuser();
1.212 albertel 398: &Apache::lonxml::debug("symb:".$symb);
399: #if ($env{'request.state'} ne "construct" && $symb ne '') {
1.204 albertel 400: if ($env{'request.state'} ne "construct") {
1.288 raeburn 401: my $idacc = &Apache::lonnet::EXT("resource.$id.acc");
1.297 raeburn 402: my $allowed=&Apache::loncommon::check_ip_acc($idacc);
1.163 albertel 403: if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
404: $status='INVALID_ACCESS';
405: $date=&mt("can not be accessed from your location.");
1.145 albertel 406: return($status,$date);
407: }
1.327 raeburn 408: if ($env{'form.grade_imsexport'}) {
409: if (($env{'request.course.id'}) &&
410: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
411: return ('SHOW_ANSWER');
412: }
413: }
1.226 albertel 414: foreach my $temp ("opendate","duedate","answerdate") {
1.163 albertel 415: $lastdate = $date;
1.247 albertel 416: if ($temp eq 'duedate') {
417: $date = &due_date($id);
418: } else {
419: $date = &Apache::lonnet::EXT("resource.$id.$temp");
420: }
421:
1.163 albertel 422: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
423: if ($thistype =~ /^(con_lost|no_such_host)/ ||
424: $date =~ /^(con_lost|no_such_host)/) {
425: $status='UNAVAILABLE';
426: $date=&mt("may open later.");
427: return($status,$date);
428: }
429: if ($thistype eq 'date_interval') {
430: if ($temp eq 'opendate') {
431: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
432: }
433: if ($temp eq 'answerdate') {
434: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
435: }
1.145 albertel 436: }
1.163 albertel 437: &Apache::lonxml::debug("found :$date: for :$temp:");
438: if ($date eq '') {
439: $date = &mt("an unknown date"); $passed = 0;
440: } elsif ($date eq 'con_lost') {
441: $date = &mt("an indeterminate date"); $passed = 0;
442: } else {
443: if (time < $date) { $passed = 0; } else { $passed = 1; }
1.274 www 444: $date = &Apache::lonlocal::locallocaltime($date);
1.145 albertel 445: }
1.163 albertel 446: if (!$passed) { $type=$temp; last; }
1.145 albertel 447: }
1.163 albertel 448: &Apache::lonxml::debug("have :$type:$passed:");
449: if ($passed) {
450: $status='SHOW_ANSWER';
451: $datemsg=$date;
452: } elsif ($type eq 'opendate') {
453: $status='CLOSED';
454: $datemsg = &mt("will open on")." $date";
455: } elsif ($type eq 'duedate') {
456: $status='CAN_ANSWER';
457: $datemsg = &mt("is due at")." $date";
458: } elsif ($type eq 'answerdate') {
459: $status='CLOSED';
460: $datemsg = &mt("was due on")." $lastdate".&mt(", and answers will be available on")." $date";
1.145 albertel 461: }
462: }
1.212 albertel 463: if ($status eq 'CAN_ANSWER' ||
464: (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) {
1.145 albertel 465: #check #tries, and if correct.
466: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
467: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
468: if ( $tries eq '' ) { $tries = '0'; }
1.164 albertel 469: if ( $maxtries eq '' &&
1.204 albertel 470: $env{'request.state'} ne 'construct') { $maxtries = '2'; }
1.326 www 471: $Apache::lonhomework::results{'resource.'.$id.'.maxtries'}=$maxtries;
1.164 albertel 472: if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145 albertel 473: # if (correct and show prob status) or excused then CANNOT_ANSWER
1.331 raeburn 474: if ( ($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/)
475: && (&show_problem_status()) ) {
1.333 raeburn 476: if (($Apache::lonhomework::history{"resource.$id.awarded"} >= 1) ||
477: (&Apache::lonnet::EXT("resource.$id.retrypartial") !~/^1|on|yes$/i)) {
1.331 raeburn 478: $status = 'CANNOT_ANSWER';
479: }
480: } elsif ($Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
1.145 albertel 481: $status = 'CANNOT_ANSWER';
482: }
1.285 albertel 483: if ($status eq 'CANNOT_ANSWER'
484: && &show_answer_problem_status()) {
485: $status = 'SHOW_ANSWER';
486: }
1.121 albertel 487: }
1.181 albertel 488: if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.286 albertel 489: my @interval=&Apache::lonnet::EXT("resource.$id.interval");
490: &Apache::lonxml::debug("looking for interval @interval");
491: if ($interval[0]) {
492: my $first_access=&Apache::lonnet::get_first_access($interval[1]);
1.177 albertel 493: &Apache::lonxml::debug("looking for accesstime $first_access");
494: if (!$first_access) {
495: $status='NOT_YET_VIEWED';
1.247 albertel 496: my $due_date = &due_date($id);
1.256 albertel 497: my $seconds_left = $due_date - time;
1.286 albertel 498: if ($seconds_left > $interval[0] || $due_date eq '') {
499: $seconds_left = $interval[0];
1.256 albertel 500: }
501: $datemsg=&seconds_to_human_length($seconds_left);
1.177 albertel 502: }
503: }
504: }
1.247 albertel 505:
1.133 albertel 506: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
507: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
508: # return ('UNCHECKEDOUT','needs to be checked out');
509: #}
1.54 www 510:
511:
1.145 albertel 512: &Apache::lonxml::debug("sending back :$status:$datemsg:");
513: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
514: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
515: $status='CAN_ANSWER';
1.146 albertel 516: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 517: }
1.106 albertel 518:
1.145 albertel 519: return ($status,$datemsg);
1.20 albertel 520: }
1.301 jms 521: # this should work exactly like the copy in lonnavmaps.pm
1.246 albertel 522: sub due_date {
1.250 albertel 523: my ($part_id,$symb,$udom,$uname)=@_;
1.246 albertel 524: my $date;
1.286 albertel 525: my @interval= &Apache::lonnet::EXT("resource.$part_id.interval",$symb,
1.250 albertel 526: $udom,$uname);
1.286 albertel 527: &Apache::lonxml::debug("looking for interval $part_id $symb @interval");
1.250 albertel 528: my $due_date= &Apache::lonnet::EXT("resource.$part_id.duedate",$symb,
529: $udom,$uname);
1.247 albertel 530: &Apache::lonxml::debug("looking for due_date $part_id $symb $due_date");
1.286 albertel 531: if ($interval[0] =~ /\d+/) {
532: my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
533: &Apache::lonxml::debug("looking for first_access $first_access ($interval[1])");
1.247 albertel 534: if (defined($first_access)) {
1.286 albertel 535: my $interval = $first_access+$interval[0];
1.283 albertel 536: $date = (!$due_date || $interval < $due_date) ? $interval
537: : $due_date;
1.247 albertel 538: } else {
539: $date = $due_date;
540: }
541: } else {
542: $date = $due_date;
1.246 albertel 543: }
544: return $date
545: }
546:
1.192 albertel 547: sub seconds_to_human_length {
548: my ($length)=@_;
549:
550: my $seconds=$length%60; $length=int($length/60);
551: my $minutes=$length%60; $length=int($length/60);
552: my $hours=$length%24; $length=int($length/24);
553: my $days=$length;
554:
555: my $timestr;
556: if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
557: if ($hours > 0) { $timestr.=($timestr?", ":"").
558: &mt('[quant,_1,hour]',$hours); }
559: if ($minutes > 0) { $timestr.=($timestr?", ":"").
560: &mt('[quant,_1,minute]',$minutes); }
561: if ($seconds > 0) { $timestr.=($timestr?", ":"").
562: &mt('[quant,_1,second]',$seconds); }
563: return $timestr;
564: }
565:
1.41 albertel 566: sub showhash {
1.145 albertel 567: my (%hash) = @_;
568: &showhashsubset(\%hash,'.');
569: return '';
1.79 albertel 570: }
571:
1.106 albertel 572: sub showarray {
573: my ($array)=@_;
574: my $string="(";
575: foreach my $elm (@{ $array }) {
1.193 albertel 576: if (ref($elm) eq 'ARRAY') {
577: $string.=&showarray($elm);
578: } elsif (ref($elm) eq 'HASH') {
579: $string.= "HASH --- \n<br />";
580: $string.= &showhashsubset($elm,'.');
1.106 albertel 581: } else {
582: $string.="$elm,"
583: }
584: }
585: chop($string);
586: $string.=")";
587: return $string;
588: }
589:
1.79 albertel 590: sub showhashsubset {
1.145 albertel 591: my ($hash,$keyre) = @_;
592: my $resultkey;
593: foreach $resultkey (sort keys %$hash) {
1.193 albertel 594: if ($resultkey !~ /$keyre/) { next; }
595: if (ref($$hash{$resultkey}) eq 'ARRAY' ) {
596: &Apache::lonxml::debug("$resultkey ---- ".
597: &showarray($$hash{$resultkey}));
598: } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
599: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
600: &showhashsubset($$hash{$resultkey},'.');
601: } else {
602: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.145 albertel 603: }
604: }
605: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
606: return '';
1.41 albertel 607: }
608:
609: sub setuppermissions {
1.204 albertel 610: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
611: my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.145 albertel 612: if (! $viewgrades &&
1.204 albertel 613: exists($env{'request.course.sec'}) &&
614: $env{'request.course.sec'} !~ /^\s*$/) {
615: $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
616: '/'.$env{'request.course.sec'});
1.145 albertel 617: }
1.244 albertel 618: $Apache::lonhomework::viewgrades = $viewgrades;
619:
1.185 albertel 620: if ($Apache::lonhomework::browse eq 'F' &&
1.204 albertel 621: $env{'form.devalidatecourseresdata'} eq 'on') {
1.261 albertel 622: my (undef,$courseid) = &Apache::lonnet::whichuser();
1.204 albertel 623: &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
624: $env{"course.$courseid.domain"});
1.185 albertel 625: }
1.244 albertel 626:
1.205 albertel 627: my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
628: if (! $modifygrades &&
629: exists($env{'request.course.sec'}) &&
630: $env{'request.course.sec'} !~ /^\s*$/) {
631: $modifygrades =
632: &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
633: '/'.$env{'request.course.sec'});
634: }
635: $Apache::lonhomework::modifygrades = $modifygrades;
1.244 albertel 636:
637: my $queuegrade = &Apache::lonnet::allowed('mqg',$env{'request.course.id'});
638: if (! $queuegrade &&
639: exists($env{'request.course.sec'}) &&
640: $env{'request.course.sec'} !~ /^\s*$/) {
641: $queuegrade =
642: &Apache::lonnet::allowed('qgr',$env{'request.course.id'}.
643: '/'.$env{'request.course.sec'});
644: }
645: $Apache::lonhomework::queuegrade = $queuegrade;
1.205 albertel 646: return '';
1.41 albertel 647: }
648:
1.253 albertel 649: sub unset_permissions {
650: undef($Apache::lonhomework::queuegrade);
651: undef($Apache::lonhomework::modifygrades);
652: undef($Apache::lonhomework::viewgrades);
653: undef($Apache::lonhomework::browse);
654: }
655:
1.41 albertel 656: sub setupheader {
1.120 albertel 657: my $request=$_[0];
1.197 albertel 658: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 659: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
660: &Apache::loncommon::no_cache($request);
661: }
1.196 albertel 662: # $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
663: # 'lastrevisiondate'));
1.120 albertel 664: $request->send_http_header;
665: return OK if $request->header_only;
666: return ''
1.41 albertel 667: }
1.35 albertel 668:
1.47 albertel 669: sub handle_save_or_undo {
1.145 albertel 670: my ($request,$problem,$result) = @_;
1.323 www 671:
1.145 albertel 672: my $file = &Apache::lonnet::filelocation("",$request->uri);
673: my $filebak =$file.".bak";
674: my $filetmp =$file.".tmp";
675: my $error=0;
1.323 www 676: if (($env{'form.problemmode'} eq 'undo') || ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 677: my $error=0;
1.284 albertel 678: if (!&File::Copy::copy($file,$filetmp)) { $error=1; }
679: if ((!$error) && (!&File::Copy::copy($filebak,$file))) { $error=1; }
680: if ((!$error) && (!&File::Copy::move($filetmp,$filebak))) { $error=1; }
1.145 albertel 681: if (!$error) {
1.266 albertel 682: &Apache::lonxml::info("<p><b>".
683: &mt("Undid changes, Switched [_1] and [_2]",
684: '<span class="LC_filename">'.$filebak.
685: '</span>',
686: '<span class="LC_filename">'.$file.
687: '</span>')."</b></p>");
1.145 albertel 688: } else {
1.266 albertel 689: &Apache::lonxml::info("<p><span class=\"LC_error\">".
690: &mt("Unable to undo, unable to switch [_1] and [_2]",
691: '<span class="LC_filename">'.
692: $filebak.'</span>',
693: '<span class="LC_filename">'.
694: $file.'</span>')."</span></p>");
1.145 albertel 695: $error=1;
696: }
1.52 albertel 697: } else {
1.262 banghart 698: &Apache::lonnet::correct_line_ends($result);
1.323 www 699:
1.145 albertel 700: my $fs=Apache::File->new(">$filebak");
701: if (defined($fs)) {
702: print $fs $$problem;
703: } else {
1.266 albertel 704: &Apache::lonxml::info("<span class=\"LC_error\">".
705: &mt("Unable to make backup [_1]",
706: '<span class="LC_filename">'.
707: $filebak.'</span>')."</span>");
1.145 albertel 708: $error=2;
709: }
710: my $fh=Apache::File->new(">$file");
711: if (defined($fh)) {
712: print $fh $$result;
713: } else {
1.266 albertel 714: &Apache::lonxml::info('<span class="LC_error">'.
715: &mt("Unable to write to [_1]",
716: '<span class="LC_filename">'.
717: $file.'</span>').
718: '</span>');
1.145 albertel 719: $error|=4;
720: }
1.52 albertel 721: }
1.145 albertel 722: return $error;
1.64 albertel 723: }
724:
1.101 albertel 725: sub analyze_header {
726: my ($request) = @_;
1.289 raeburn 727: my $js = &Apache::structuretags::setmode_javascript();
1.312 bisitz 728:
729: # Breadcrumbs
1.330 raeburn 730: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.312 bisitz 731: 'text' => 'Construction Space'},
732: {'href' => '',
733: 'text' => 'Problem Testing'},
734: {'href' => '',
735: 'text' => 'Analyzing a problem'}];
736:
1.238 albertel 737: my $result =
1.312 bisitz 738: &Apache::loncommon::start_page('Analyzing a problem',
739: $js,
740: {'bread_crumbs' => $brcrum,})
1.311 bisitz 741: .&Apache::loncommon::head_subbox(
742: &Apache::loncommon::CSTR_pageheader());
1.238 albertel 743: $result .=
744: &Apache::lonxml::message_location().'
1.310 bisitz 745: <form name="lonhomework" method="post" action="'.
1.204 albertel 746: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.289 raeburn 747: '<input type="hidden" name="problemmode" value="'.
748: $env{'form.problemmode'}.'" />'.
1.179 albertel 749: &Apache::structuretags::remember_problem_state().'
1.278 albertel 750: <div class="LC_edit_problem_analyze_header">
1.289 raeburn 751: <input type="button" name="submitmode" value="'.&mt("EditXML").'" '.
752: 'onclick="javascript:setmode(this.form,'."'editxml'".')" />
753: <input type="button" name="submitmode" value="'.&mt('Edit').'" '.
754: 'onclick="javascript:setmode(this.form,'."'edit'".')" />
1.313 bisitz 755: <hr />
1.289 raeburn 756: <input type="button" name="submitmode" value="'.&mt("View").'" '.
757: 'onclick="javascript:setmode(this.form,'."'view'".')" />
1.313 bisitz 758: <hr />
1.278 albertel 759: </div>
1.101 albertel 760: </form>';
1.171 albertel 761: &Apache::lonxml::add_messages(\$result);
1.101 albertel 762: $request->print($result);
763: $request->rflush();
764: }
765:
1.109 albertel 766: sub analyze_footer {
767: my ($request) = @_;
1.237 albertel 768: $request->print(&Apache::loncommon::end_page());
1.109 albertel 769: $request->rflush();
770: }
771:
1.74 albertel 772: sub analyze {
1.101 albertel 773: my ($request,$file) = @_;
774: &Apache::lonxml::debug("Analyze");
775: my $result;
776: my %overall;
1.219 www 777: my %seedexample;
1.101 albertel 778: my %allparts;
1.204 albertel 779: my $rndseed=$env{'form.rndseed'};
1.101 albertel 780: &analyze_header($request);
1.114 albertel 781: my %prog_state=
1.335 ! www 782: &Apache::lonhtmlcommon::Create_PrgWin($request,$env{'form.numtoanalyze'});
1.204 albertel 783: for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
1.335 ! www 784: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,'last problem');
1.182 albertel 785: if (&Apache::loncommon::connection_aborted($request)) { return; }
1.219 www 786: my $thisseed=$i+$rndseed;
1.101 albertel 787: my $subresult=&Apache::lonnet::ssi($request->uri,
788: ('grade_target' => 'analyze'),
1.219 www 789: ('rndseed' => $thisseed));
1.101 albertel 790: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
791: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 792: my @parts;
793: if (defined(@{ $analyze{'parts'} })) {
794: @parts=@{ $analyze{'parts'} };
795: }
1.101 albertel 796: foreach my $part (@parts) {
797: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 798: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
799: $analyze{$part.'.type'} eq 'stringresponse' ||
800: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.263 albertel 801: foreach my $name (keys(%{ $analyze{$part.'.answer'} })) {
802: my $i=0;
803: foreach my $answer_part (@{ $analyze{$part.'.answer'}{$name} }) {
804: push( @{ $overall{$part.'.answer'}[$i] },
805: $answer_part);
806: my $concatanswer= join("\0",@{ $answer_part });
807: if (($concatanswer eq '') || ($concatanswer=~/^\@/)) {
1.266 albertel 808: $answer_part = ['<span class="LC_error">'.&mt('Error').'</span>'];
1.263 albertel 809: }
810: $seedexample{join("\0",$part,$i,@{$answer_part})}=
811: $thisseed;
812: $i++;
813: }
1.219 www 814: }
1.275 albertel 815: if (!keys(%{ $analyze{$part.'.answer'} })) {
816: my $answer_part =
817: ['<span class="LC_error">'.&mt('Error').'</span>'];
818: $seedexample{join("\0",$part,0,@{$answer_part})}=
819: $thisseed;
820: push( @{ $overall{$part.'.answer'}[0] },
821: $answer_part);
822: }
1.101 albertel 823: }
824: }
825: }
1.335 ! www 826: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,&mt('Analyzing Results'));
1.313 bisitz 827: $request->print('<hr />'
1.308 bisitz 828: .'<h3>'
829: .&mt('List of possible answers')
830: .'</h3>'
831: );
1.134 albertel 832: foreach my $part (sort(keys(%allparts))) {
1.109 albertel 833: if (defined(@{ $overall{$part.'.answer'} })) {
1.263 albertel 834: for (my $i=0;$i<scalar(@{ $overall{$part.'.answer'} });$i++) {
835: my $num_cols=scalar(@{ $overall{$part.'.answer'}[$i][0] });
1.308 bisitz 836: $request->print(&Apache::loncommon::start_data_table()
837: .&Apache::loncommon::start_data_table_header_row()
838: .'<th colspan="'.($num_cols+1).'">'
839: .&mt('Part').' '.$part
840: );
1.263 albertel 841: if (scalar(@{ $overall{$part.'.answer'} }) > 1) {
1.308 bisitz 842: $request->print(' '.&mt('Answer [_1]',$i+1));
1.263 albertel 843: }
1.308 bisitz 844: $request->print('</th>'
845: .&Apache::loncommon::end_data_table_header_row()
846: );
1.263 albertel 847: my %frequency;
848: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'}[$i] })) {
849: $frequency{join("\0",@{ $answer })}++;
850: }
1.308 bisitz 851: $request->print(&Apache::loncommon::start_data_table_header_row()
852: .'<th colspan="'.($num_cols).'">'.&mt('Answer').'</th>'
853: .'<th>'.&mt('Frequency').'<br />'
854: .'('.&mt('click for example').')</th>'
855: .&Apache::loncommon::end_data_table_header_row()
856: );
1.263 albertel 857: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
1.308 bisitz 858: $request->print(&Apache::loncommon::start_data_table_row()
859: .'<td>'
860: .join('</td><td>',split("\0",$answer))
861: .'</td>'
862: .'<td>'
863: .'<a href="'.$request->uri.'?rndseed='.$seedexample{join("\0",$part,$i,$answer)}.'">'.$frequency{$answer}.'</a>'
864: .'</td>'
865: .&Apache::loncommon::end_data_table_row()
866: );
1.263 albertel 867: }
1.308 bisitz 868: $request->print(&Apache::loncommon::end_data_table());
1.109 albertel 869: }
870: } else {
1.307 bisitz 871: $request->print('<p class="LC_warning">'
872: .&mt('Response [_1] is not analyzable at this time.',$part)
873: .'</p>'
874: );
1.101 albertel 875: }
876: }
1.130 albertel 877: if (scalar(keys(%allparts)) == 0 ) {
1.307 bisitz 878: $request->print('<p class="LC_warning">'
879: .&mt('Found no analyzable responses in this problem.'
880: .' Currently only Numerical, Formula and String response styles are supported.')
881: .'</p>'
882: );
1.130 albertel 883: }
1.114 albertel 884: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 885: &analyze_footer($request);
1.101 albertel 886: &Apache::lonhomework::showhash(%overall);
887: return $result;
1.74 albertel 888: }
889:
1.277 albertel 890: {
891: my $show_problem_status;
892: sub reset_show_problem_status {
893: undef($show_problem_status);
894: }
895:
896: sub set_show_problem_status {
897: my ($new_status) = @_;
898: $show_problem_status = lc($new_status);
899: }
900:
901: sub hide_problem_status {
902: return ($show_problem_status eq 'no'
903: || $show_problem_status eq 'no_feedback_ever');
904: }
905:
906: sub show_problem_status {
907: return ($show_problem_status eq 'yes'
1.285 albertel 908: || $show_problem_status eq 'answer'
1.277 albertel 909: || $show_problem_status eq '');
910: }
911:
912: sub show_some_problem_status {
913: return ($show_problem_status eq 'no');
914: }
915:
916: sub show_no_problem_status {
917: return ($show_problem_status eq 'no_feedback_ever');
918: }
1.285 albertel 919:
920: sub show_answer_problem_status {
921: return ($show_problem_status eq 'answer');
922: }
1.277 albertel 923: }
924:
1.64 albertel 925: sub editxmlmode {
1.145 albertel 926: my ($request,$file) = @_;
927: my $result;
928: my $problem=&Apache::lonnet::getfile($file);
929: if ($problem eq -1) {
1.305 bisitz 930: &Apache::lonxml::error(
1.328 bisitz 931: '<p class="LC_error">'
1.305 bisitz 932: .&mt('Unable to find [_1]',
933: '<span class="LC_filename">'.$file.'</span>')
1.328 bisitz 934: .'</p>');
1.305 bisitz 935:
1.145 albertel 936: $problem='';
937: }
1.323 www 938:
939: if (($env{'form.problemmode'} eq 'saveeditxml') ||
940: ($env{'form.problemmode'} eq 'saveviewxml') ||
941: ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 942: my $error=&handle_save_or_undo($request,\$problem,
1.204 albertel 943: \$env{'form.editxmltext'});
1.145 albertel 944: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
945: }
1.204 albertel 946: &Apache::lonhomework::showhashsubset(\%env,'^form');
1.323 www 947: if ($env{'form.problemmode'} eq 'saveviewxml') {
1.204 albertel 948: &Apache::lonhomework::showhashsubset(\%env,'^form');
1.289 raeburn 949: $env{'form.problemmode'}='view';
1.145 albertel 950: &renderpage($request,$file);
951: } else {
952: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
953: if ($cols > 80) { $cols = 80; }
954: if ($cols < 70) { $cols = 70; }
955: if ($rows < 20) { $rows = 20; }
1.269 albertel 956: my $js =
957: &Apache::edit::js_change_detection().
1.289 raeburn 958: &Apache::loncommon::resize_textarea_js().
1.296 raeburn 959: &Apache::structuretags::setmode_javascript().
1.298 foxr 960: &Apache::lonhtmlcommon::dragmath_js("EditMathPopup");
1.312 bisitz 961:
962: # Breadcrumbs
1.330 raeburn 963: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.312 bisitz 964: 'text' => 'Construction Space'},
965: {'href' => '',
966: 'text' => 'Problem Editing'}];
967:
1.238 albertel 968: my $start_page =
1.269 albertel 969: &Apache::loncommon::start_page(&mt("EditXML [_1]",$file),$js,
970: {'no_auto_mt_title' => 1,
1.318 droeschl 971: 'only_body' => 0,
1.269 albertel 972: 'add_entries' => {
973: 'onresize' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
974: 'onload' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
1.323 www 975: },
1.312 bisitz 976: 'bread_crumbs' => $brcrum,
1.323 www 977: });
1.311 bisitz 978:
979: $result=$start_page
980: .&Apache::loncommon::head_subbox(
981: &Apache::loncommon::CSTR_pageheader());
982: $result.=&renderpage($request,$file,['no_output_web'],1).
1.310 bisitz 983: '<form '.&Apache::edit::form_change_detection().' name="lonhomework" method="post" action="'.
1.204 albertel 984: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 985: &Apache::structuretags::remember_problem_state().'
1.278 albertel 986: <div class="LC_edit_problem_editxml_header">
1.280 albertel 987: <table class="LC_edit_problem_header_title"><tr><td>
1.317 bisitz 988: <h2>'.&mt('Problem Editing').' '.&Apache::loncommon::help_open_topic('Problem_Editor_XML_Index').'</h2>
1.280 albertel 989: </td><td align="right">
1.301 jms 990: '.&Apache::loncommon::helpLatexCheatsheet('Problem_LON-CAPA_Functions','Script Functions').'
1.280 albertel 991: </td></tr>
1.323 www 992: </table>';
993:
994: $result.='<input type="hidden" name="problemmode" value="saveedit" />'.
995: &Apache::structuretags::problem_edit_buttons('editxml');
996:
997: $result.='<hr style="clear:both;" />'.&Apache::lonxml::message_location().'</div>'.
998: '<textarea '.&Apache::edit::element_change_detection().
1.269 albertel 999: ' rows="'.$rows.'" cols="'.$cols.'" style="width:100%" '.
1000: ' name="editxmltext" id="LC_editxmltext">'.
1001: &HTML::Entities::encode($problem,'<>&"').'</textarea>
1002: <div id="LC_aftertextarea">
1003: </div>
1.238 albertel 1004: </form>'.&Apache::loncommon::end_page();
1.171 albertel 1005: &Apache::lonxml::add_messages(\$result);
1.145 albertel 1006: $request->print($result);
1007: }
1008: return '';
1.47 albertel 1009: }
1.189 albertel 1010:
1.301 jms 1011: #
1012: # Render the page in whatever target desired.
1013: #
1.41 albertel 1014: sub renderpage {
1.213 albertel 1015: my ($request,$file,$targets,$return_string) = @_;
1.52 albertel 1016:
1.211 albertel 1017: my @targets = @{$targets || [&get_target()]};
1.204 albertel 1018: &Apache::lonhomework::showhashsubset(\%env,'form.');
1.145 albertel 1019: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.268 albertel 1020:
1.171 albertel 1021: my $overall_result;
1.145 albertel 1022: foreach my $target (@targets) {
1.183 albertel 1023: # FIXME need to do something intelligent when a problem goes
1024: # from viewable to not viewable due to map conditions
1025: #&setuppermissions();
1026: #if ( $Apache::lonhomework::browse ne '2'
1027: # && $Apache::lonhomework::browse ne 'F' ) {
1028: # $request->print(" You most likely shouldn't see me.");
1029: #}
1.145 albertel 1030: #my $t0 = [&gettimeofday()];
1.211 albertel 1031: my $output=1;
1032: if ($target eq 'no_output_web') {
1033: $target = 'web'; $output=0;
1034: }
1.145 albertel 1035: my $problem=&Apache::lonnet::getfile($file);
1.222 albertel 1036: my $result;
1.145 albertel 1037: if ($problem eq -1) {
1.260 albertel 1038: $problem='';
1.222 albertel 1039: my $filename=(split('/',$file))[-1];
1.260 albertel 1040: my $error =
1.328 bisitz 1041: '<p class="LC_error">'
1042: .&mt('Unable to find [_1]',
1.305 bisitz 1043: '<span class="LC_filename">'.$filename.'</span>')
1.328 bisitz 1044: ."</p>";
1.260 albertel 1045: $result.=
1046: &Apache::loncommon::simple_error_page($request,'Not available',
1047: $error);
1048: return;
1.145 albertel 1049: }
1.52 albertel 1050:
1.145 albertel 1051: my %mystyle;
1052: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1053: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
1.204 albertel 1054: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
1.145 albertel 1055:
1056: &Apache::lonxml::debug("Should be parsing now");
1.222 albertel 1057: $result .= &Apache::lonxml::xmlparse($request, $target, $problem,
1058: &setup_vars($target),%mystyle);
1.273 albertel 1059: &finished_parsing();
1.214 albertel 1060: if (!$output) { $result = ''; }
1.145 albertel 1061: #$request->print("Result follows:");
1062: if ($target eq 'modified') {
1063: &handle_save_or_undo($request,\$problem,\$result);
1064: } else {
1065: if ($target eq 'analyze') {
1066: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1067: undef(%Apache::lonhomework::analyze);
1068: }
1069: #my $td=&tv_interval($t0);
1070: #if ( $Apache::lonxml::debug) {
1071: #$result =~ s:</body>::;
1072: #$result.="<br />Spent $td seconds processing target $target\n</body>";
1073: #}
1.171 albertel 1074: # $request->print($result);
1075: $overall_result.=$result;
1076: # $request->rflush();
1.145 albertel 1077: }
1078: #$request->print(":Result ends");
1079: #my $td=&tv_interval($t0);
1.52 albertel 1080: }
1.213 albertel 1081: if (!$return_string) {
1082: &Apache::lonxml::add_messages(\$overall_result);
1083: $request->print($overall_result);
1084: $request->rflush();
1085: } else {
1086: return $overall_result;
1087: }
1.41 albertel 1088: }
1089:
1.273 albertel 1090: sub finished_parsing {
1091: undef($Apache::lonhomework::parsing_a_problem);
1092: undef($Apache::lonhomework::parsing_a_task);
1093: }
1094:
1.42 albertel 1095: sub get_template_list {
1.282 albertel 1096: my ($extension) = @_;
1.145 albertel 1097: my $result;
1098: my @allnames;
1099: &Apache::lonxml::debug("Looking for :$extension:");
1.282 albertel 1100: my $glob_extension = $extension;
1101: if ($extension eq 'survey' || $extension eq 'exam') {
1102: $glob_extension = 'problem';
1103: }
1104: my @files = glob($Apache::lonnet::perlvar{'lonIncludes'}.
1105: '/templates/*.'.$glob_extension);
1.292 www 1106: @files = map {[$_,&mt(&Apache::lonnet::metadata($_, 'title')),
1107: (&Apache::lonnet::metadata($_, 'category')?&mt(&Apache::lonnet::metadata($_, 'category')):&mt('Miscellaneous')),
1108: &mt(&Apache::lonnet::metadata($_, 'help'))]} (@files);
1109: @files = sort {$a->[2].$a->[1] cmp $b->[2].$b->[1]} (@files);
1.287 raeburn 1110: my ($midpoint,$seconddiv,$numfiles);
1111: $numfiles = 0;
1112: foreach my $file (@files) {
1113: next if ($file->[1] !~ /\S/);
1114: $numfiles ++;
1115: }
1116: if ($numfiles > 0) {
1117: $result = '<div class="LC_left_float">';
1118: $midpoint = int($numfiles/2);
1119: if ($numfiles%2) {
1120: $midpoint ++;
1121: }
1122: }
1123: my $count = 0;
1.292 www 1124: my $currentcategory='';
1.314 bisitz 1125: my $first = 1;
1.329 raeburn 1126: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.282 albertel 1127: foreach my $file (@files) {
1128: next if ($file->[1] !~ /\S/);
1.292 www 1129: if ($file->[2] ne $currentcategory) {
1130: $currentcategory=$file->[2];
1131: if ((!$seconddiv) && ($count >= $midpoint)) {
1.314 bisitz 1132: $result .= '</div></div>'."\n".'<div class="LC_left_float">'."\n";
1.292 www 1133: $seconddiv = 1;
1.314 bisitz 1134: } elsif (!$first) {
1135: $result.='</div>'."\n";
1136: } else {
1137: $first = 0;
1.292 www 1138: }
1.314 bisitz 1139: $result.= '<div class="LC_Box">'."\n"
1140: .'<h3 class="LC_hcell">'.$currentcategory.'</h3>'."\n";
1.293 www 1141: $count++;
1.292 www 1142: }
1.282 albertel 1143: $result .=
1144: '<label><input type="radio" name="template" value="'.$file->[0].'" />'.
1.292 www 1145: $file->[1].'</label>';
1146: if ($file->[3]) {
1147: $result.=&Apache::loncommon::help_open_topic($file->[3]);
1148: }
1149: my $filename=$file->[0];
1.329 raeburn 1150: $filename=~s{^\Q$londocroot\E}{};
1.314 bisitz 1151: $result.=' <span class="LC_fontsize_small">'
1.332 www 1152: .&Apache::loncommon::modal_link($filename.'?inhibitmenu=yes',&mt('Example'),600,420,'sample')
1.314 bisitz 1153: .'</span><br />'."\n";
1.287 raeburn 1154: $count ++;
1155: }
1156: if ($numfiles > 0) {
1.314 bisitz 1157: $result .= '</div></div>'."\n".'<div class="LC_clear_float_footer"></div>'."\n";
1.42 albertel 1158: }
1.145 albertel 1159: return $result;
1.42 albertel 1160: }
1161:
1162: sub newproblem {
1.65 matthew 1163: my ($request) = @_;
1.282 albertel 1164:
1165: if ($env{'form.template'}) {
1166: my $file = $env{'form.template'};
1.65 matthew 1167: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1168: &File::Copy::copy($file,$dest);
1.65 matthew 1169: &renderpage($request,$dest);
1.282 albertel 1170: return;
1171: }
1172:
1173: my ($extension) = ($request->uri =~ m/\.(\w+)$/);
1174: &Apache::lonxml::debug("Looking for :$extension:");
1175: my $templatelist=&get_template_list($extension);
1176: if ($env{'form.newfile'} && !$templatelist) {
1177: # no templates found
1.131 albertel 1178: my $templatefilename =
1.258 albertel 1179: $request->dir_config('lonIncludes').'/templates/blank.'.$extension;
1.131 albertel 1180: &Apache::lonxml::debug("$templatefilename");
1181: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1182: &File::Copy::copy($templatefilename,$dest);
1.131 albertel 1183: &renderpage($request,$dest);
1.85 albertel 1184: } else {
1.176 albertel 1185: my $url=&HTML::Entities::encode($request->uri,'<>&"');
1.65 matthew 1186: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 1187: my $errormsg;
1.85 albertel 1188: my $instructions;
1.330 raeburn 1189: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.312 bisitz 1190: 'text' => 'Construction Space'},
1191: {'href' => '',
1192: 'text' => "Create New $extension"}];
1.240 albertel 1193: my $start_page =
1.312 bisitz 1194: &Apache::loncommon::start_page("Create New $extension",
1195: undef,
1196: {'bread_crumbs' => $brcrum,});
1.311 bisitz 1197: $request->print(
1198: $start_page
1199: .&Apache::loncommon::head_subbox(
1200: &Apache::loncommon::CSTR_pageheader())
1201: .'<h1>'.&mt("Creating a new $extension resource.")."</h1>
1.128 albertel 1202: $errormsg
1.258 albertel 1203: ".&mt("The requested file [_1] currently does not exist.",
1.329 raeburn 1204: '<span class="LC_filename">'.$url.'</span>').'
1.314 bisitz 1205: <p class="LC_info">
1206: '.&mt("To create a new $extension, select a template from the".
1207: " list below. Then click on the \"Create $extension\" button.").'
1208: </p><div><form action="'.$url.'" method="post">');
1.258 albertel 1209:
1.85 albertel 1210: if (defined($templatelist)) {
1.282 albertel 1211: $request->print($templatelist);
1.85 albertel 1212: }
1.282 albertel 1213: $request->print('<br /><input type="submit" name="newfile" value="'.
1214: &mt("Create $extension").'" />');
1.314 bisitz 1215: $request->print('</form></div>'.&Apache::loncommon::end_page());
1.65 matthew 1216: }
1.282 albertel 1217: return;
1.42 albertel 1218: }
1219:
1.268 albertel 1220: sub update_construct_style {
1221: if ($env{'request.state'} eq "construct"
1.289 raeburn 1222: && $env{'form.problemmode'} eq 'view'
1.268 albertel 1223: && defined($env{'form.submitted'})
1224: && !defined($env{'form.resetdata'})
1225: && !defined($env{'form.newrandomization'})) {
1226: if ((!$env{'form.style_file'} && $env{'construct.style'})
1227: ||$env{'form.clear_style_file'}) {
1.303 raeburn 1228: &Apache::lonnet::delenv('construct.style');
1.268 albertel 1229: } elsif ($env{'form.style_file'}
1230: && $env{'construct.style'} ne $env{'form.style_file'}) {
1.291 raeburn 1231: &Apache::lonnet::appenv({'construct.style' =>
1232: $env{'form.style_file'}});
1.268 albertel 1233: }
1234: }
1235: }
1236:
1237:
1.41 albertel 1238: sub handler {
1.145 albertel 1239: #my $t0 = [&gettimeofday()];
1240: my $request=$_[0];
1.223 albertel 1241: $Apache::lonxml::request=$request;
1.204 albertel 1242: $Apache::lonxml::debug=$env{'user.debug'};
1243: $env{'request.uri'}=$request->uri;
1.180 albertel 1244: &setuppermissions();
1.193 albertel 1245:
1.145 albertel 1246: my $file=&Apache::lonnet::filelocation("",$request->uri);
1247:
1248: #check if we know where we are
1.204 albertel 1249: if ($env{'request.course.fn'} && !&Apache::lonnet::symbread()) {
1.145 albertel 1250: # if we are browsing we might not be able to know where we are
1.173 albertel 1251: if ($Apache::lonhomework::browse ne 'F' &&
1.204 albertel 1252: $env{'request.state'} ne "construct") {
1.145 albertel 1253: #should know where we are, so ask
1.253 albertel 1254: &unset_permissions();
1255: $request->internal_redirect('/adm/ambiguous');
1256: return OK;
1.145 albertel 1257: }
1258: }
1.253 albertel 1259: if (&setupheader($request)) {
1260: &unset_permissions();
1261: return OK;
1262: }
1.244 albertel 1263: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
1.204 albertel 1264: &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
1.261 albertel 1265: my ($symb) = &Apache::lonnet::whichuser();
1.145 albertel 1266: &Apache::lonxml::debug('symb is '.$symb);
1.204 albertel 1267: if ($env{'request.state'} eq "construct") {
1.145 albertel 1268: if ( -e $file ) {
1269: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1270: ['problemmode']);
1.204 albertel 1271: if (!(defined $env{'form.problemmode'})) {
1.145 albertel 1272: #first visit to problem in construction space
1.289 raeburn 1273: $env{'form.problemmode'}= 'view';
1.145 albertel 1274: &renderpage($request,$file);
1.323 www 1275: } elsif (($env{'form.problemmode'} eq 'editxml') ||
1276: ($env{'form.problemmode'} eq 'saveeditxml') ||
1277: ($env{'form.problemmode'} eq 'saveviewxml') ||
1278: ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 1279: &editxmlmode($request,$file);
1.289 raeburn 1280: } elsif ($env{'form.problemmode'} eq 'calcanswers') {
1.145 albertel 1281: &analyze($request,$file);
1282: } else {
1.268 albertel 1283: &update_construct_style();
1.145 albertel 1284: &renderpage($request,$file);
1285: }
1286: } else {
1287: # requested file doesn't exist in contruction space
1288: &newproblem($request);
1289: }
1290: } else {
1291: # just render the page normally outside of construction space
1292: &Apache::lonxml::debug("not construct");
1.52 albertel 1293: &renderpage($request,$file);
1.41 albertel 1294: }
1.145 albertel 1295: #my $td=&tv_interval($t0);
1296: #&Apache::lonxml::debug("Spent $td seconds processing");
1297: # always turn off debug messages
1298: $Apache::lonxml::debug=0;
1.253 albertel 1299: &unset_permissions();
1.145 albertel 1300: return OK;
1.52 albertel 1301:
1.1 albertel 1302: }
1303:
1304: 1;
1305: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>