Annotation of loncom/homework/lonhomework.pm, revision 1.344.2.3
1.63 albertel 1: # The LearningOnline Network with CAPA
1.52 albertel 2: # The LON-CAPA Homework handler
1.63 albertel 3: #
1.344.2.3! raeburn 4: # $Id: lonhomework.pm,v 1.344.2.2 2015/04/20 01:22:58 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: #
1.338 raeburn 166: # End of Authoring Space
1.323 www 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 {
1.344.2.3! raeburn 211: my ($id,$type,$partlist)=@_;
1.226 albertel 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.344.2.3! raeburn 274: my ($is_correct,$got_grade,$checkinslot,$checkedin,$consumed_uniq);
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: $checkedin = $Apache::lonhomework::history{"resource.0.checkedin"};
1.344.2.3! raeburn 287: }
! 288: if ($checkedin) {
! 289: $checkinslot = $Apache::lonhomework::history{"$checkin.slot"};
! 290: my %slot=&Apache::lonnet::get_slot($checkinslot);
! 291: $consumed_uniq = $slot{'uniqueperiod'};
! 292: }
! 293: if ($type eq 'problem') {
! 294: if ((ref($partlist) eq 'ARRAY') && (@{$partlist} > 0)) {
! 295: my ($numcorrect,$numgraded) = (0,0);
! 296: foreach my $part (@{$partlist}) {
! 297: my $currtries = $Apache::lonhomework::history{"resource.$part.tries"};
! 298: my $maxtries = &Apache::lonnet::EXT("resource.$part.maxtries",$symb);
! 299: my $probstatus = &Apache::structuretags::get_problem_status($part);
! 300: my $earlyout;
! 301: unless (($probstatus eq 'no') ||
! 302: ($probstatus eq 'no_feedback_ever')) {
! 303: if ($Apache::lonhomework::history{"resource.$part.solved"} =~/^correct_/) {
! 304: $numcorrect ++;
! 305: } else {
! 306: $earlyout = 1;
! 307: }
! 308: }
! 309: if (($currtries == $maxtries) || ($is_correct)) {
! 310: $earlyout = 1;
! 311: } else {
! 312: $numgraded ++;
! 313: }
! 314: last if ($earlyout);
! 315: }
! 316: my $numparts = scalar(@{$partlist});
! 317: if ($numparts == $numcorrect) {
! 318: $is_correct = 1;
! 319: }
! 320: if ($numparts == $numgraded) {
! 321: $got_grade = 1;
! 322: }
! 323: } else {
! 324: my $currtries = $Apache::lonhomework::history{"resource.0.tries"};
! 325: my $maxtries = &Apache::lonnet::EXT("resource.0.maxtries",$symb);
! 326: my $probstatus = &Apache::structuretags::get_problem_status('0');
! 327: unless (($probstatus eq 'no') ||
! 328: ($probstatus eq 'no_feedback_ever')) {
! 329: $is_correct =
! 330: ($Apache::lonhomework::history{"resource.0.solved"} =~/^correct_/);
! 331: }
! 332: unless (($currtries == $maxtries) || ($is_correct)) {
! 333: $got_grade = 1;
! 334: }
! 335: }
1.226 albertel 336: }
337:
1.235 albertel 338: &Apache::lonxml::debug(" slot is $slotstatus checkedin ($checkedin) got_grade ($got_grade) is_correct ($is_correct)");
339:
1.243 albertel 340: # no slot is currently open, and has been checked in for this version
341: # but hasn't got a grade, therefore must be awaiting a grade
342: if (!defined($slot_name)
343: && $checkedin
1.236 albertel 344: && !$got_grade) {
345: return ('WAITING_FOR_GRADE');
346: }
347:
1.309 raeburn 348: # Previously used slot is no longer open, and has been checked in for this version.
349: # However, the problem is not closed, and potentially, another slot might be
350: # used to gain access to it to work on it, until the due date is reached, and the
351: # problem then becomes CLOSED. Therefore return the slotstatus -
1.334 raeburn 352: # (which will be one of: NOT_IN_A_SLOT, RESERVABLE, RESERVABLE_LATER, or NOTRESERVABLE.
353: if (!defined($slot_name) && $type eq 'problem') {
354: if ($slotstatus eq 'NOT_IN_A_SLOT') {
355: if (!$num_usable_slots) {
356: if ($env{'request.course.id'}) {
357: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
358: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
359: my ($symb)=&Apache::lonnet::whichuser();
360: $slotstatus = 'NOTRESERVABLE';
361: my ($reservable_now_order,$reservable_now,$reservable_future_order,
362: $reservable_future) =
363: &Apache::loncommon::get_future_slots($cnum,$cdom,$now,$symb);
364: if ((ref($reservable_now_order) eq 'ARRAY') && (ref($reservable_now) eq 'HASH')) {
365: if (@{$reservable_now_order} > 0) {
1.344.2.3! raeburn 366: if ((!$checkedin) || (ref($consumed_uniq) ne 'ARRAY')) {
! 367: $slotstatus = 'RESERVABLE';
! 368: $datemsg = $reservable_now->{$reservable_now_order->[-1]}{'endreserve'};
! 369: } else {
! 370: my ($uniqstart,$uniqend,$useslot);
! 371: if (ref($consumed_uniq) eq 'ARRAY') {
! 372: ($uniqstart,$uniqend)=@{$consumed_uniq};
! 373: }
! 374: foreach my $slot (reverse(@{$reservable_now_order})) {
! 375: if ($reservable_now->{$slot}{'uniqueperiod'} =~ /^(\d+)\,(\d+)$/) {
! 376: my ($new_uniq_start,$new_uniq_end) = ($1,$2);
! 377: next if (!
! 378: ($uniqstart < $new_uniq_start && $uniqend < $new_uniq_start) ||
! 379: ($uniqstart > $new_uniq_end && $uniqend > $new_uniq_end ));
! 380: }
! 381: $useslot = $slot;
! 382: last;
! 383: }
! 384: if ($useslot) {
! 385: $slotstatus = 'RESERVABLE';
! 386: $datemsg = $reservable_now->{$useslot}{'endreserve'};
! 387: }
! 388: }
1.334 raeburn 389: }
390: }
391: unless ($slotstatus eq 'RESERVABLE') {
392: if ((ref($reservable_future_order) eq 'ARRAY') && (ref($reservable_future) eq 'HASH')) {
393: if (@{$reservable_future_order} > 0) {
1.344.2.3! raeburn 394: if ((!$checkedin) || (ref($consumed_uniq) ne 'ARRAY')) {
! 395: $slotstatus = 'RESERVABLE_LATER';
! 396: $datemsg = $reservable_future->{$reservable_future_order->[0]}{'startreserve'};
! 397: } else {
! 398: my ($uniqstart,$uniqend,$useslot);
! 399: if (ref($consumed_uniq) eq 'ARRAY') {
! 400: ($uniqstart,$uniqend)=@{$consumed_uniq};
! 401: }
! 402: foreach my $slot (@{$reservable_future_order}) {
! 403: if ($reservable_future->{$slot}{'uniqueperiod'} =~ /^(\d+),(\d+)$/) {
! 404: my ($new_uniq_start,$new_uniq_end) = ($1,$2);
! 405: next if (!
! 406: ($uniqstart < $new_uniq_start && $uniqend < $new_uniq_start) ||
! 407: ($uniqstart > $new_uniq_end && $uniqend > $new_uniq_end ));
! 408: }
! 409: $useslot = $slot;
! 410: last;
! 411: }
! 412: if ($useslot) {
! 413: $slotstatus = 'RESERVABLE_LATER';
! 414: $datemsg = $reservable_future->{$useslot}{'startreserve'};
! 415: }
! 416: }
1.334 raeburn 417: }
418: }
419: }
420: }
421: }
422: }
423: return ($slotstatus,$datemsg);
1.252 albertel 424: }
425:
1.226 albertel 426: if ($slotstatus eq 'NOT_IN_A_SLOT'
427: && $checkedin ) {
428:
1.231 albertel 429: if ($got_grade) {
1.209 albertel 430: return ('SHOW_ANSWER');
431: } else {
432: return ('WAITING_FOR_GRADE');
433: }
1.226 albertel 434:
1.207 albertel 435: }
1.255 albertel 436:
1.235 albertel 437: if ( $is_correct) {
1.255 albertel 438: if ($type eq 'problem') {
439: return ($status);
440: }
1.235 albertel 441: return ('SHOW_ANSWER');
442: }
1.255 albertel 443:
1.225 albertel 444: if ( $status eq 'CANNOT_ANSWER' &&
445: ($slotstatus ne 'NEEDS_CHECKIN' && $slotstatus ne 'NOT_IN_A_SLOT')) {
446: return ($status,$datemsg);
447: }
448:
1.206 albertel 449: return ($slotstatus,$datemsg,$slot_name,$returned_slot);
1.198 albertel 450: }
1.200 albertel 451:
1.301 jms 452: # JB, 9/24/2002: Any changes in this function may require a change
453: # in lonnavmaps::resource::getDateStatus.
1.53 www 454: sub check_access {
1.145 albertel 455: my ($id) = @_;
456: my $date ='';
457: my $status;
458: my $datemsg = '';
459: my $lastdate = '';
460: my $type;
461: my $passed;
462:
1.204 albertel 463: if ($env{'request.state'} eq "construct") {
464: if ($env{'form.problemstate'}) {
465: if ($env{'form.problemstate'} =~ /^CANNOT_ANSWER/) {
1.277 albertel 466: if ( ! ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct'
467: && &hide_problem_status())) {
1.168 albertel 468: return ('CANNOT_ANSWER',
1.174 www 469: &mt('is in this state due to author settings.'));
1.167 albertel 470: }
1.165 albertel 471: } else {
1.204 albertel 472: return ($env{'form.problemstate'},
1.174 www 473: &mt('is in this state due to author settings.'));
1.165 albertel 474: }
475: }
1.145 albertel 476: &Apache::lonxml::debug("in construction ignoring dates");
477: $status='CAN_ANSWER';
1.146 albertel 478: $datemsg=&mt('is in under construction');
1.163 albertel 479: # return ($status,$datemsg);
1.145 albertel 480: }
481:
482: &Apache::lonxml::debug("checking for part :$id:");
483: &Apache::lonxml::debug("time:".time);
1.152 albertel 484:
1.261 albertel 485: my ($symb)=&Apache::lonnet::whichuser();
1.212 albertel 486: &Apache::lonxml::debug("symb:".$symb);
487: #if ($env{'request.state'} ne "construct" && $symb ne '') {
1.204 albertel 488: if ($env{'request.state'} ne "construct") {
1.288 raeburn 489: my $idacc = &Apache::lonnet::EXT("resource.$id.acc");
1.297 raeburn 490: my $allowed=&Apache::loncommon::check_ip_acc($idacc);
1.163 albertel 491: if (!$allowed && ($Apache::lonhomework::browse ne 'F')) {
492: $status='INVALID_ACCESS';
493: $date=&mt("can not be accessed from your location.");
1.145 albertel 494: return($status,$date);
495: }
1.327 raeburn 496: if ($env{'form.grade_imsexport'}) {
497: if (($env{'request.course.id'}) &&
498: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
499: return ('SHOW_ANSWER');
500: }
501: }
1.226 albertel 502: foreach my $temp ("opendate","duedate","answerdate") {
1.163 albertel 503: $lastdate = $date;
1.247 albertel 504: if ($temp eq 'duedate') {
505: $date = &due_date($id);
506: } else {
507: $date = &Apache::lonnet::EXT("resource.$id.$temp");
508: }
509:
1.163 albertel 510: my $thistype = &Apache::lonnet::EXT("resource.$id.$temp.type");
511: if ($thistype =~ /^(con_lost|no_such_host)/ ||
512: $date =~ /^(con_lost|no_such_host)/) {
513: $status='UNAVAILABLE';
514: $date=&mt("may open later.");
515: return($status,$date);
516: }
517: if ($thistype eq 'date_interval') {
518: if ($temp eq 'opendate') {
519: $date=&Apache::lonnet::EXT("resource.$id.duedate")-$date;
520: }
521: if ($temp eq 'answerdate') {
522: $date=&Apache::lonnet::EXT("resource.$id.duedate")+$date;
523: }
1.145 albertel 524: }
1.163 albertel 525: &Apache::lonxml::debug("found :$date: for :$temp:");
526: if ($date eq '') {
527: $date = &mt("an unknown date"); $passed = 0;
528: } elsif ($date eq 'con_lost') {
529: $date = &mt("an indeterminate date"); $passed = 0;
530: } else {
531: if (time < $date) { $passed = 0; } else { $passed = 1; }
1.274 www 532: $date = &Apache::lonlocal::locallocaltime($date);
1.145 albertel 533: }
1.163 albertel 534: if (!$passed) { $type=$temp; last; }
1.145 albertel 535: }
1.163 albertel 536: &Apache::lonxml::debug("have :$type:$passed:");
537: if ($passed) {
538: $status='SHOW_ANSWER';
539: $datemsg=$date;
540: } elsif ($type eq 'opendate') {
541: $status='CLOSED';
1.343 bisitz 542: $datemsg = &mt('will open on [_1]',$date);
1.163 albertel 543: } elsif ($type eq 'duedate') {
544: $status='CAN_ANSWER';
1.343 bisitz 545: $datemsg = &mt('is due at [_1]',$date);
1.163 albertel 546: } elsif ($type eq 'answerdate') {
547: $status='CLOSED';
1.343 bisitz 548: $datemsg = &mt('was due on [_1], and answers will be available on [_2]',
549: $lastdate,$date);
1.145 albertel 550: }
551: }
1.212 albertel 552: if ($status eq 'CAN_ANSWER' ||
553: (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED'))) {
1.145 albertel 554: #check #tries, and if correct.
555: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
556: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
557: if ( $tries eq '' ) { $tries = '0'; }
1.164 albertel 558: if ( $maxtries eq '' &&
1.204 albertel 559: $env{'request.state'} ne 'construct') { $maxtries = '2'; }
1.164 albertel 560: if ($maxtries && $tries >= $maxtries) { $status = 'CANNOT_ANSWER'; }
1.145 albertel 561: # if (correct and show prob status) or excused then CANNOT_ANSWER
1.331 raeburn 562: if ( ($Apache::lonhomework::history{"resource.$id.solved"}=~/^correct/)
563: && (&show_problem_status()) ) {
1.333 raeburn 564: if (($Apache::lonhomework::history{"resource.$id.awarded"} >= 1) ||
565: (&Apache::lonnet::EXT("resource.$id.retrypartial") !~/^1|on|yes$/i)) {
1.331 raeburn 566: $status = 'CANNOT_ANSWER';
567: }
568: } elsif ($Apache::lonhomework::history{"resource.$id.solved"}=~/^excused/) {
1.145 albertel 569: $status = 'CANNOT_ANSWER';
570: }
1.285 albertel 571: if ($status eq 'CANNOT_ANSWER'
572: && &show_answer_problem_status()) {
573: $status = 'SHOW_ANSWER';
574: }
1.121 albertel 575: }
1.181 albertel 576: if ($status eq 'CAN_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.286 albertel 577: my @interval=&Apache::lonnet::EXT("resource.$id.interval");
578: &Apache::lonxml::debug("looking for interval @interval");
579: if ($interval[0]) {
580: my $first_access=&Apache::lonnet::get_first_access($interval[1]);
1.177 albertel 581: &Apache::lonxml::debug("looking for accesstime $first_access");
582: if (!$first_access) {
583: $status='NOT_YET_VIEWED';
1.247 albertel 584: my $due_date = &due_date($id);
1.256 albertel 585: my $seconds_left = $due_date - time;
1.286 albertel 586: if ($seconds_left > $interval[0] || $due_date eq '') {
587: $seconds_left = $interval[0];
1.256 albertel 588: }
589: $datemsg=&seconds_to_human_length($seconds_left);
1.177 albertel 590: }
591: }
592: }
1.247 albertel 593:
1.133 albertel 594: #if (($status ne 'CLOSED') && ($Apache::lonhomework::type eq 'exam') &&
595: # (!$Apache::lonhomework::history{"resource.0.outtoken"})) {
596: # return ('UNCHECKEDOUT','needs to be checked out');
597: #}
1.54 www 598:
1.145 albertel 599: &Apache::lonxml::debug("sending back :$status:$datemsg:");
600: if (($Apache::lonhomework::browse eq 'F') && ($status eq 'CLOSED')) {
601: &Apache::lonxml::debug("should be allowed to browse a resource when closed");
602: $status='CAN_ANSWER';
1.146 albertel 603: $datemsg=&mt('is closed but you are allowed to view it');
1.145 albertel 604: }
1.106 albertel 605:
1.145 albertel 606: return ($status,$datemsg);
1.20 albertel 607: }
1.301 jms 608: # this should work exactly like the copy in lonnavmaps.pm
1.246 albertel 609: sub due_date {
1.250 albertel 610: my ($part_id,$symb,$udom,$uname)=@_;
1.246 albertel 611: my $date;
1.286 albertel 612: my @interval= &Apache::lonnet::EXT("resource.$part_id.interval",$symb,
1.250 albertel 613: $udom,$uname);
1.286 albertel 614: &Apache::lonxml::debug("looking for interval $part_id $symb @interval");
1.250 albertel 615: my $due_date= &Apache::lonnet::EXT("resource.$part_id.duedate",$symb,
616: $udom,$uname);
1.247 albertel 617: &Apache::lonxml::debug("looking for due_date $part_id $symb $due_date");
1.286 albertel 618: if ($interval[0] =~ /\d+/) {
619: my $first_access=&Apache::lonnet::get_first_access($interval[1],$symb);
620: &Apache::lonxml::debug("looking for first_access $first_access ($interval[1])");
1.247 albertel 621: if (defined($first_access)) {
1.286 albertel 622: my $interval = $first_access+$interval[0];
1.283 albertel 623: $date = (!$due_date || $interval < $due_date) ? $interval
624: : $due_date;
1.247 albertel 625: } else {
626: $date = $due_date;
627: }
628: } else {
629: $date = $due_date;
1.246 albertel 630: }
1.344.2.1 raeburn 631: return $date;
1.246 albertel 632: }
633:
1.192 albertel 634: sub seconds_to_human_length {
635: my ($length)=@_;
636:
637: my $seconds=$length%60; $length=int($length/60);
638: my $minutes=$length%60; $length=int($length/60);
639: my $hours=$length%24; $length=int($length/24);
640: my $days=$length;
641:
642: my $timestr;
643: if ($days > 0) { $timestr.=&mt('[quant,_1,day]',$days); }
644: if ($hours > 0) { $timestr.=($timestr?", ":"").
645: &mt('[quant,_1,hour]',$hours); }
646: if ($minutes > 0) { $timestr.=($timestr?", ":"").
647: &mt('[quant,_1,minute]',$minutes); }
648: if ($seconds > 0) { $timestr.=($timestr?", ":"").
649: &mt('[quant,_1,second]',$seconds); }
650: return $timestr;
651: }
652:
1.41 albertel 653: sub showhash {
1.145 albertel 654: my (%hash) = @_;
655: &showhashsubset(\%hash,'.');
656: return '';
1.79 albertel 657: }
658:
1.106 albertel 659: sub showarray {
660: my ($array)=@_;
661: my $string="(";
662: foreach my $elm (@{ $array }) {
1.193 albertel 663: if (ref($elm) eq 'ARRAY') {
664: $string.=&showarray($elm);
665: } elsif (ref($elm) eq 'HASH') {
666: $string.= "HASH --- \n<br />";
667: $string.= &showhashsubset($elm,'.');
1.106 albertel 668: } else {
669: $string.="$elm,"
670: }
671: }
672: chop($string);
673: $string.=")";
674: return $string;
675: }
676:
1.79 albertel 677: sub showhashsubset {
1.145 albertel 678: my ($hash,$keyre) = @_;
679: my $resultkey;
1.344.2.1 raeburn 680: foreach $resultkey (sort(keys(%$hash))) {
1.193 albertel 681: if ($resultkey !~ /$keyre/) { next; }
682: if (ref($$hash{$resultkey}) eq 'ARRAY' ) {
683: &Apache::lonxml::debug("$resultkey ---- ".
684: &showarray($$hash{$resultkey}));
685: } elsif (ref($$hash{$resultkey}) eq 'HASH' ) {
686: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
687: &showhashsubset($$hash{$resultkey},'.');
688: } else {
689: &Apache::lonxml::debug("$resultkey ---- $$hash{$resultkey}");
1.145 albertel 690: }
691: }
692: &Apache::lonxml::debug("\n<br />restored values^</br>\n");
693: return '';
1.41 albertel 694: }
695:
696: sub setuppermissions {
1.204 albertel 697: $Apache::lonhomework::browse= &Apache::lonnet::allowed('bre',$env{'request.filename'});
1.337 raeburn 698: unless ($Apache::lonhomework::browse eq 'F') {
699: $Apache::lonhomework::browse=&Apache::lonnet::allowed('bro',$env{'request.filename'});
700: }
1.204 albertel 701: my $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1.145 albertel 702: if (! $viewgrades &&
1.204 albertel 703: exists($env{'request.course.sec'}) &&
704: $env{'request.course.sec'} !~ /^\s*$/) {
705: $viewgrades = &Apache::lonnet::allowed('vgr',$env{'request.course.id'}.
706: '/'.$env{'request.course.sec'});
1.145 albertel 707: }
1.244 albertel 708: $Apache::lonhomework::viewgrades = $viewgrades;
709:
1.185 albertel 710: if ($Apache::lonhomework::browse eq 'F' &&
1.204 albertel 711: $env{'form.devalidatecourseresdata'} eq 'on') {
1.261 albertel 712: my (undef,$courseid) = &Apache::lonnet::whichuser();
1.204 albertel 713: &Apache::lonnet::devalidatecourseresdata($env{"course.$courseid.num"},
714: $env{"course.$courseid.domain"});
1.185 albertel 715: }
1.244 albertel 716:
1.205 albertel 717: my $modifygrades = &Apache::lonnet::allowed('mgr',$env{'request.course.id'});
718: if (! $modifygrades &&
719: exists($env{'request.course.sec'}) &&
720: $env{'request.course.sec'} !~ /^\s*$/) {
721: $modifygrades =
722: &Apache::lonnet::allowed('mgr',$env{'request.course.id'}.
723: '/'.$env{'request.course.sec'});
724: }
725: $Apache::lonhomework::modifygrades = $modifygrades;
1.244 albertel 726:
727: my $queuegrade = &Apache::lonnet::allowed('mqg',$env{'request.course.id'});
728: if (! $queuegrade &&
729: exists($env{'request.course.sec'}) &&
730: $env{'request.course.sec'} !~ /^\s*$/) {
731: $queuegrade =
732: &Apache::lonnet::allowed('qgr',$env{'request.course.id'}.
733: '/'.$env{'request.course.sec'});
734: }
735: $Apache::lonhomework::queuegrade = $queuegrade;
1.205 albertel 736: return '';
1.41 albertel 737: }
738:
1.253 albertel 739: sub unset_permissions {
740: undef($Apache::lonhomework::queuegrade);
741: undef($Apache::lonhomework::modifygrades);
742: undef($Apache::lonhomework::viewgrades);
743: undef($Apache::lonhomework::browse);
744: }
745:
1.41 albertel 746: sub setupheader {
1.120 albertel 747: my $request=$_[0];
1.197 albertel 748: &Apache::loncommon::content_type($request,'text/html');
1.120 albertel 749: if (!$Apache::lonxml::debug && ($ENV{'REQUEST_METHOD'} eq 'GET')) {
750: &Apache::loncommon::no_cache($request);
751: }
1.196 albertel 752: # $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
753: # 'lastrevisiondate'));
1.120 albertel 754: $request->send_http_header;
755: return OK if $request->header_only;
756: return ''
1.41 albertel 757: }
1.35 albertel 758:
1.47 albertel 759: sub handle_save_or_undo {
1.338 raeburn 760: my ($request,$problem,$result,$getobjref) = @_;
1.323 www 761:
1.145 albertel 762: my $file = &Apache::lonnet::filelocation("",$request->uri);
763: my $filebak =$file.".bak";
764: my $filetmp =$file.".tmp";
765: my $error=0;
1.323 www 766: if (($env{'form.problemmode'} eq 'undo') || ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 767: my $error=0;
1.284 albertel 768: if (!&File::Copy::copy($file,$filetmp)) { $error=1; }
769: if ((!$error) && (!&File::Copy::copy($filebak,$file))) { $error=1; }
770: if ((!$error) && (!&File::Copy::move($filetmp,$filebak))) { $error=1; }
1.145 albertel 771: if (!$error) {
1.266 albertel 772: &Apache::lonxml::info("<p><b>".
773: &mt("Undid changes, Switched [_1] and [_2]",
774: '<span class="LC_filename">'.$filebak.
775: '</span>',
776: '<span class="LC_filename">'.$file.
777: '</span>')."</b></p>");
1.145 albertel 778: } else {
1.266 albertel 779: &Apache::lonxml::info("<p><span class=\"LC_error\">".
780: &mt("Unable to undo, unable to switch [_1] and [_2]",
781: '<span class="LC_filename">'.
782: $filebak.'</span>',
783: '<span class="LC_filename">'.
784: $file.'</span>')."</span></p>");
1.145 albertel 785: $error=1;
786: }
1.52 albertel 787: } else {
1.262 banghart 788: &Apache::lonnet::correct_line_ends($result);
1.323 www 789:
1.145 albertel 790: my $fs=Apache::File->new(">$filebak");
791: if (defined($fs)) {
792: print $fs $$problem;
793: } else {
1.266 albertel 794: &Apache::lonxml::info("<span class=\"LC_error\">".
795: &mt("Unable to make backup [_1]",
796: '<span class="LC_filename">'.
797: $filebak.'</span>')."</span>");
1.145 albertel 798: $error=2;
799: }
800: my $fh=Apache::File->new(">$file");
801: if (defined($fh)) {
802: print $fh $$result;
1.338 raeburn 803: if (ref($getobjref) eq 'SCALAR') {
804: if ($file =~ m{([^/]+)\.(html?)$}) {
805: my $fname = $1;
806: my $ext = $2;
807: my $path = $file;
808: $path =~ s/\Q$fname\E\.\Q$ext\E$//;
809: my (%allfiles,%codebase);
810: &Apache::lonnet::extract_embedded_items($file,\%allfiles,
811: \%codebase,$result);
812: if (keys(%allfiles) > 0) {
813: my $url = $request->uri;
814: my $state = <<STATE;
815: <input type="hidden" name="action" value="upload_embedded" />
816: <input type="hidden" name="url" value="$url" />
817: STATE
818: $$getobjref = "<h3>".&mt("Reference Warning")."</h3>".
819: "<p>".&mt("Completed upload of the file. This file contained references to other files.")."</p>".
820: "<p>".&mt("Please select the locations from which the referenced files are to be uploaded.")."</p>".
821: &Apache::loncommon::ask_for_embedded_content($url,$state,\%allfiles,\%codebase,
822: {'error_on_invalid_names' => 1,
823: 'ignore_remote_references' => 1,});
824: }
825: }
826: }
1.145 albertel 827: } else {
1.266 albertel 828: &Apache::lonxml::info('<span class="LC_error">'.
829: &mt("Unable to write to [_1]",
830: '<span class="LC_filename">'.
831: $file.'</span>').
832: '</span>');
1.145 albertel 833: $error|=4;
834: }
1.52 albertel 835: }
1.145 albertel 836: return $error;
1.64 albertel 837: }
838:
1.101 albertel 839: sub analyze_header {
840: my ($request) = @_;
1.289 raeburn 841: my $js = &Apache::structuretags::setmode_javascript();
1.312 bisitz 842:
843: # Breadcrumbs
1.330 raeburn 844: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.338 raeburn 845: 'text' => 'Authoring Space'},
1.312 bisitz 846: {'href' => '',
847: 'text' => 'Problem Testing'},
848: {'href' => '',
849: 'text' => 'Analyzing a problem'}];
850:
1.238 albertel 851: my $result =
1.312 bisitz 852: &Apache::loncommon::start_page('Analyzing a problem',
853: $js,
854: {'bread_crumbs' => $brcrum,})
1.311 bisitz 855: .&Apache::loncommon::head_subbox(
856: &Apache::loncommon::CSTR_pageheader());
1.238 albertel 857: $result .=
858: &Apache::lonxml::message_location().'
1.310 bisitz 859: <form name="lonhomework" method="post" action="'.
1.204 albertel 860: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.289 raeburn 861: '<input type="hidden" name="problemmode" value="'.
862: $env{'form.problemmode'}.'" />'.
1.179 albertel 863: &Apache::structuretags::remember_problem_state().'
1.278 albertel 864: <div class="LC_edit_problem_analyze_header">
1.289 raeburn 865: <input type="button" name="submitmode" value="'.&mt("EditXML").'" '.
866: 'onclick="javascript:setmode(this.form,'."'editxml'".')" />
867: <input type="button" name="submitmode" value="'.&mt('Edit').'" '.
868: 'onclick="javascript:setmode(this.form,'."'edit'".')" />
1.313 bisitz 869: <hr />
1.289 raeburn 870: <input type="button" name="submitmode" value="'.&mt("View").'" '.
871: 'onclick="javascript:setmode(this.form,'."'view'".')" />
1.313 bisitz 872: <hr />
1.278 albertel 873: </div>
1.101 albertel 874: </form>';
1.171 albertel 875: &Apache::lonxml::add_messages(\$result);
1.101 albertel 876: $request->print($result);
877: $request->rflush();
878: }
879:
1.109 albertel 880: sub analyze_footer {
881: my ($request) = @_;
1.237 albertel 882: $request->print(&Apache::loncommon::end_page());
1.109 albertel 883: $request->rflush();
884: }
885:
1.74 albertel 886: sub analyze {
1.101 albertel 887: my ($request,$file) = @_;
888: &Apache::lonxml::debug("Analyze");
889: my $result;
890: my %overall;
1.219 www 891: my %seedexample;
1.101 albertel 892: my %allparts;
1.204 albertel 893: my $rndseed=$env{'form.rndseed'};
1.101 albertel 894: &analyze_header($request);
1.114 albertel 895: my %prog_state=
1.335 www 896: &Apache::lonhtmlcommon::Create_PrgWin($request,$env{'form.numtoanalyze'});
1.204 albertel 897: for(my $i=1;$i<$env{'form.numtoanalyze'}+1;$i++) {
1.335 www 898: &Apache::lonhtmlcommon::Increment_PrgWin($request,\%prog_state,'last problem');
1.182 albertel 899: if (&Apache::loncommon::connection_aborted($request)) { return; }
1.219 www 900: my $thisseed=$i+$rndseed;
1.101 albertel 901: my $subresult=&Apache::lonnet::ssi($request->uri,
902: ('grade_target' => 'analyze'),
1.219 www 903: ('rndseed' => $thisseed));
1.101 albertel 904: (my $garbage,$subresult)=split(/_HASH_REF__/,$subresult,2);
905: my %analyze=&Apache::lonnet::str2hash($subresult);
1.114 albertel 906: my @parts;
1.336 raeburn 907: if (ref($analyze{'parts'}) eq 'ARRAY') {
1.114 albertel 908: @parts=@{ $analyze{'parts'} };
909: }
1.101 albertel 910: foreach my $part (@parts) {
911: if (!exists($allparts{$part})) {$allparts{$part}=1;};
1.109 albertel 912: if ($analyze{$part.'.type'} eq 'numericalresponse' ||
913: $analyze{$part.'.type'} eq 'stringresponse' ||
914: $analyze{$part.'.type'} eq 'formularesponse' ) {
1.263 albertel 915: foreach my $name (keys(%{ $analyze{$part.'.answer'} })) {
916: my $i=0;
917: foreach my $answer_part (@{ $analyze{$part.'.answer'}{$name} }) {
918: push( @{ $overall{$part.'.answer'}[$i] },
919: $answer_part);
920: my $concatanswer= join("\0",@{ $answer_part });
921: if (($concatanswer eq '') || ($concatanswer=~/^\@/)) {
1.266 albertel 922: $answer_part = ['<span class="LC_error">'.&mt('Error').'</span>'];
1.263 albertel 923: }
924: $seedexample{join("\0",$part,$i,@{$answer_part})}=
925: $thisseed;
926: $i++;
927: }
1.219 www 928: }
1.275 albertel 929: if (!keys(%{ $analyze{$part.'.answer'} })) {
930: my $answer_part =
931: ['<span class="LC_error">'.&mt('Error').'</span>'];
932: $seedexample{join("\0",$part,0,@{$answer_part})}=
933: $thisseed;
934: push( @{ $overall{$part.'.answer'}[0] },
935: $answer_part);
936: }
1.101 albertel 937: }
938: }
939: }
1.335 www 940: &Apache::lonhtmlcommon::Update_PrgWin($request,\%prog_state,&mt('Analyzing Results'));
1.313 bisitz 941: $request->print('<hr />'
1.308 bisitz 942: .'<h3>'
943: .&mt('List of possible answers')
944: .'</h3>'
945: );
1.134 albertel 946: foreach my $part (sort(keys(%allparts))) {
1.336 raeburn 947: if ((ref($overall{$part.'.answer'}) eq 'ARRAY') &&
948: (@{$overall{$part.'.answer'}} > 0)) {
1.263 albertel 949: for (my $i=0;$i<scalar(@{ $overall{$part.'.answer'} });$i++) {
950: my $num_cols=scalar(@{ $overall{$part.'.answer'}[$i][0] });
1.308 bisitz 951: $request->print(&Apache::loncommon::start_data_table()
952: .&Apache::loncommon::start_data_table_header_row()
953: .'<th colspan="'.($num_cols+1).'">'
954: .&mt('Part').' '.$part
955: );
1.263 albertel 956: if (scalar(@{ $overall{$part.'.answer'} }) > 1) {
1.308 bisitz 957: $request->print(' '.&mt('Answer [_1]',$i+1));
1.263 albertel 958: }
1.308 bisitz 959: $request->print('</th>'
960: .&Apache::loncommon::end_data_table_header_row()
961: );
1.263 albertel 962: my %frequency;
963: foreach my $answer (sort {$a->[0] <=> $b->[0]} (@{ $overall{$part.'.answer'}[$i] })) {
964: $frequency{join("\0",@{ $answer })}++;
965: }
1.308 bisitz 966: $request->print(&Apache::loncommon::start_data_table_header_row()
967: .'<th colspan="'.($num_cols).'">'.&mt('Answer').'</th>'
968: .'<th>'.&mt('Frequency').'<br />'
969: .'('.&mt('click for example').')</th>'
970: .&Apache::loncommon::end_data_table_header_row()
971: );
1.263 albertel 972: foreach my $answer (sort {(split("\0",$a))[0] <=> (split("\0",$b))[0]} (keys(%frequency))) {
1.308 bisitz 973: $request->print(&Apache::loncommon::start_data_table_row()
974: .'<td>'
975: .join('</td><td>',split("\0",$answer))
976: .'</td>'
977: .'<td>'
978: .'<a href="'.$request->uri.'?rndseed='.$seedexample{join("\0",$part,$i,$answer)}.'">'.$frequency{$answer}.'</a>'
979: .'</td>'
980: .&Apache::loncommon::end_data_table_row()
981: );
1.263 albertel 982: }
1.308 bisitz 983: $request->print(&Apache::loncommon::end_data_table());
1.109 albertel 984: }
985: } else {
1.307 bisitz 986: $request->print('<p class="LC_warning">'
987: .&mt('Response [_1] is not analyzable at this time.',$part)
988: .'</p>'
989: );
1.101 albertel 990: }
991: }
1.130 albertel 992: if (scalar(keys(%allparts)) == 0 ) {
1.307 bisitz 993: $request->print('<p class="LC_warning">'
994: .&mt('Found no analyzable responses in this problem.'
995: .' Currently only Numerical, Formula and String response styles are supported.')
996: .'</p>'
997: );
1.130 albertel 998: }
1.114 albertel 999: &Apache::lonhtmlcommon::Close_PrgWin($request,\%prog_state);
1.109 albertel 1000: &analyze_footer($request);
1.101 albertel 1001: &Apache::lonhomework::showhash(%overall);
1002: return $result;
1.74 albertel 1003: }
1004:
1.277 albertel 1005: {
1006: my $show_problem_status;
1007: sub reset_show_problem_status {
1008: undef($show_problem_status);
1009: }
1010:
1011: sub set_show_problem_status {
1012: my ($new_status) = @_;
1013: $show_problem_status = lc($new_status);
1014: }
1015:
1016: sub hide_problem_status {
1017: return ($show_problem_status eq 'no'
1018: || $show_problem_status eq 'no_feedback_ever');
1019: }
1020:
1021: sub show_problem_status {
1022: return ($show_problem_status eq 'yes'
1.285 albertel 1023: || $show_problem_status eq 'answer'
1.277 albertel 1024: || $show_problem_status eq '');
1025: }
1026:
1027: sub show_some_problem_status {
1028: return ($show_problem_status eq 'no');
1029: }
1030:
1031: sub show_no_problem_status {
1032: return ($show_problem_status eq 'no_feedback_ever');
1033: }
1.285 albertel 1034:
1035: sub show_answer_problem_status {
1036: return ($show_problem_status eq 'answer');
1037: }
1.277 albertel 1038: }
1039:
1.64 albertel 1040: sub editxmlmode {
1.145 albertel 1041: my ($request,$file) = @_;
1042: my $result;
1043: my $problem=&Apache::lonnet::getfile($file);
1044: if ($problem eq -1) {
1.305 bisitz 1045: &Apache::lonxml::error(
1.328 bisitz 1046: '<p class="LC_error">'
1.305 bisitz 1047: .&mt('Unable to find [_1]',
1048: '<span class="LC_filename">'.$file.'</span>')
1.328 bisitz 1049: .'</p>');
1.305 bisitz 1050:
1.145 albertel 1051: $problem='';
1052: }
1.323 www 1053:
1054: if (($env{'form.problemmode'} eq 'saveeditxml') ||
1055: ($env{'form.problemmode'} eq 'saveviewxml') ||
1056: ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 1057: my $error=&handle_save_or_undo($request,\$problem,
1.204 albertel 1058: \$env{'form.editxmltext'});
1.145 albertel 1059: if (!$error) { $problem=&Apache::lonnet::getfile($file); }
1060: }
1.204 albertel 1061: &Apache::lonhomework::showhashsubset(\%env,'^form');
1.323 www 1062: if ($env{'form.problemmode'} eq 'saveviewxml') {
1.204 albertel 1063: &Apache::lonhomework::showhashsubset(\%env,'^form');
1.289 raeburn 1064: $env{'form.problemmode'}='view';
1.145 albertel 1065: &renderpage($request,$file);
1066: } else {
1067: my ($rows,$cols) = &Apache::edit::textarea_sizes(\$problem);
1068: if ($cols > 80) { $cols = 80; }
1069: if ($cols < 70) { $cols = 70; }
1070: if ($rows < 20) { $rows = 20; }
1.269 albertel 1071: my $js =
1072: &Apache::edit::js_change_detection().
1.289 raeburn 1073: &Apache::loncommon::resize_textarea_js().
1.296 raeburn 1074: &Apache::structuretags::setmode_javascript().
1.298 foxr 1075: &Apache::lonhtmlcommon::dragmath_js("EditMathPopup");
1.312 bisitz 1076:
1077: # Breadcrumbs
1.330 raeburn 1078: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.338 raeburn 1079: 'text' => 'Authoring Space'},
1.312 bisitz 1080: {'href' => '',
1081: 'text' => 'Problem Editing'}];
1082:
1.238 albertel 1083: my $start_page =
1.269 albertel 1084: &Apache::loncommon::start_page(&mt("EditXML [_1]",$file),$js,
1085: {'no_auto_mt_title' => 1,
1.318 droeschl 1086: 'only_body' => 0,
1.269 albertel 1087: 'add_entries' => {
1088: 'onresize' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
1089: 'onload' => q[resize_textarea('LC_editxmltext','LC_aftertextarea')],
1.323 www 1090: },
1.312 bisitz 1091: 'bread_crumbs' => $brcrum,
1.323 www 1092: });
1.311 bisitz 1093:
1094: $result=$start_page
1095: .&Apache::loncommon::head_subbox(
1096: &Apache::loncommon::CSTR_pageheader());
1097: $result.=&renderpage($request,$file,['no_output_web'],1).
1.310 bisitz 1098: '<form '.&Apache::edit::form_change_detection().' name="lonhomework" method="post" action="'.
1.204 albertel 1099: &HTML::Entities::encode($env{'request.uri'},'<>&"').'">'.
1.179 albertel 1100: &Apache::structuretags::remember_problem_state().'
1.278 albertel 1101: <div class="LC_edit_problem_editxml_header">
1.280 albertel 1102: <table class="LC_edit_problem_header_title"><tr><td>
1.317 bisitz 1103: <h2>'.&mt('Problem Editing').' '.&Apache::loncommon::help_open_topic('Problem_Editor_XML_Index').'</h2>
1.280 albertel 1104: </td><td align="right">
1.301 jms 1105: '.&Apache::loncommon::helpLatexCheatsheet('Problem_LON-CAPA_Functions','Script Functions').'
1.280 albertel 1106: </td></tr>
1.323 www 1107: </table>';
1108:
1109: $result.='<input type="hidden" name="problemmode" value="saveedit" />'.
1110: &Apache::structuretags::problem_edit_buttons('editxml');
1111:
1112: $result.='<hr style="clear:both;" />'.&Apache::lonxml::message_location().'</div>'.
1113: '<textarea '.&Apache::edit::element_change_detection().
1.269 albertel 1114: ' rows="'.$rows.'" cols="'.$cols.'" style="width:100%" '.
1115: ' name="editxmltext" id="LC_editxmltext">'.
1116: &HTML::Entities::encode($problem,'<>&"').'</textarea>
1117: <div id="LC_aftertextarea">
1118: </div>
1.238 albertel 1119: </form>'.&Apache::loncommon::end_page();
1.171 albertel 1120: &Apache::lonxml::add_messages(\$result);
1.145 albertel 1121: $request->print($result);
1122: }
1123: return '';
1.47 albertel 1124: }
1.189 albertel 1125:
1.301 jms 1126: #
1127: # Render the page in whatever target desired.
1128: #
1.41 albertel 1129: sub renderpage {
1.213 albertel 1130: my ($request,$file,$targets,$return_string) = @_;
1.52 albertel 1131:
1.211 albertel 1132: my @targets = @{$targets || [&get_target()]};
1.204 albertel 1133: &Apache::lonhomework::showhashsubset(\%env,'form.');
1.145 albertel 1134: &Apache::lonxml::debug("Running targets ".join(':',@targets));
1.268 albertel 1135:
1.171 albertel 1136: my $overall_result;
1.145 albertel 1137: foreach my $target (@targets) {
1.183 albertel 1138: # FIXME need to do something intelligent when a problem goes
1139: # from viewable to not viewable due to map conditions
1140: #&setuppermissions();
1141: #if ( $Apache::lonhomework::browse ne '2'
1142: # && $Apache::lonhomework::browse ne 'F' ) {
1143: # $request->print(" You most likely shouldn't see me.");
1144: #}
1.145 albertel 1145: #my $t0 = [&gettimeofday()];
1.211 albertel 1146: my $output=1;
1147: if ($target eq 'no_output_web') {
1148: $target = 'web'; $output=0;
1149: }
1.145 albertel 1150: my $problem=&Apache::lonnet::getfile($file);
1.222 albertel 1151: my $result;
1.145 albertel 1152: if ($problem eq -1) {
1.260 albertel 1153: $problem='';
1.222 albertel 1154: my $filename=(split('/',$file))[-1];
1.260 albertel 1155: my $error =
1.339 bisitz 1156: &mt('Unable to find [_1]',
1157: '<span class="LC_filename">'.$filename.'</span>');
1.260 albertel 1158: $result.=
1159: &Apache::loncommon::simple_error_page($request,'Not available',
1.340 bisitz 1160: $error,{'no_auto_mt_msg' => 1});
1.260 albertel 1161: return;
1.145 albertel 1162: }
1.52 albertel 1163:
1.145 albertel 1164: my %mystyle;
1165: if ($target eq 'analyze') { %Apache::lonhomework::analyze=(); }
1166: if ($target eq 'answer') { &showhash(%Apache::lonhomework::history); }
1.204 albertel 1167: if ($target eq 'web') {&Apache::lonhomework::showhashsubset(\%env,'^form');}
1.145 albertel 1168:
1169: &Apache::lonxml::debug("Should be parsing now");
1.222 albertel 1170: $result .= &Apache::lonxml::xmlparse($request, $target, $problem,
1171: &setup_vars($target),%mystyle);
1.273 albertel 1172: &finished_parsing();
1.214 albertel 1173: if (!$output) { $result = ''; }
1.145 albertel 1174: #$request->print("Result follows:");
1175: if ($target eq 'modified') {
1176: &handle_save_or_undo($request,\$problem,\$result);
1177: } else {
1178: if ($target eq 'analyze') {
1179: $result=&Apache::lonnet::hashref2str(\%Apache::lonhomework::analyze);
1180: undef(%Apache::lonhomework::analyze);
1181: }
1182: #my $td=&tv_interval($t0);
1183: #if ( $Apache::lonxml::debug) {
1184: #$result =~ s:</body>::;
1185: #$result.="<br />Spent $td seconds processing target $target\n</body>";
1186: #}
1.171 albertel 1187: # $request->print($result);
1188: $overall_result.=$result;
1189: # $request->rflush();
1.145 albertel 1190: }
1191: #$request->print(":Result ends");
1192: #my $td=&tv_interval($t0);
1.52 albertel 1193: }
1.213 albertel 1194: if (!$return_string) {
1195: &Apache::lonxml::add_messages(\$overall_result);
1196: $request->print($overall_result);
1197: $request->rflush();
1198: } else {
1199: return $overall_result;
1200: }
1.41 albertel 1201: }
1202:
1.273 albertel 1203: sub finished_parsing {
1204: undef($Apache::lonhomework::parsing_a_problem);
1205: undef($Apache::lonhomework::parsing_a_task);
1206: }
1207:
1.42 albertel 1208: sub get_template_list {
1.282 albertel 1209: my ($extension) = @_;
1.145 albertel 1210: my $result;
1211: my @allnames;
1212: &Apache::lonxml::debug("Looking for :$extension:");
1.282 albertel 1213: my $glob_extension = $extension;
1214: if ($extension eq 'survey' || $extension eq 'exam') {
1215: $glob_extension = 'problem';
1216: }
1217: my @files = glob($Apache::lonnet::perlvar{'lonIncludes'}.
1218: '/templates/*.'.$glob_extension);
1.292 www 1219: @files = map {[$_,&mt(&Apache::lonnet::metadata($_, 'title')),
1220: (&Apache::lonnet::metadata($_, 'category')?&mt(&Apache::lonnet::metadata($_, 'category')):&mt('Miscellaneous')),
1.342 bisitz 1221: &Apache::lonnet::metadata($_, 'help')]} (@files);
1.292 www 1222: @files = sort {$a->[2].$a->[1] cmp $b->[2].$b->[1]} (@files);
1.287 raeburn 1223: my ($midpoint,$seconddiv,$numfiles);
1.341 bisitz 1224: my @noexamplelink = ('blank.problem','blank.library','script.library');
1.287 raeburn 1225: $numfiles = 0;
1226: foreach my $file (@files) {
1227: next if ($file->[1] !~ /\S/);
1228: $numfiles ++;
1229: }
1230: if ($numfiles > 0) {
1231: $result = '<div class="LC_left_float">';
1232: $midpoint = int($numfiles/2);
1233: if ($numfiles%2) {
1234: $midpoint ++;
1235: }
1236: }
1237: my $count = 0;
1.292 www 1238: my $currentcategory='';
1.314 bisitz 1239: my $first = 1;
1.329 raeburn 1240: my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.282 albertel 1241: foreach my $file (@files) {
1242: next if ($file->[1] !~ /\S/);
1.292 www 1243: if ($file->[2] ne $currentcategory) {
1244: $currentcategory=$file->[2];
1245: if ((!$seconddiv) && ($count >= $midpoint)) {
1.314 bisitz 1246: $result .= '</div></div>'."\n".'<div class="LC_left_float">'."\n";
1.292 www 1247: $seconddiv = 1;
1.314 bisitz 1248: } elsif (!$first) {
1249: $result.='</div>'."\n";
1250: } else {
1251: $first = 0;
1.292 www 1252: }
1.314 bisitz 1253: $result.= '<div class="LC_Box">'."\n"
1254: .'<h3 class="LC_hcell">'.$currentcategory.'</h3>'."\n";
1.293 www 1255: $count++;
1.292 www 1256: }
1.282 albertel 1257: $result .=
1258: '<label><input type="radio" name="template" value="'.$file->[0].'" />'.
1.292 www 1259: $file->[1].'</label>';
1260: if ($file->[3]) {
1261: $result.=&Apache::loncommon::help_open_topic($file->[3]);
1262: }
1.341 bisitz 1263: # Provide example link
1.292 www 1264: my $filename=$file->[0];
1.329 raeburn 1265: $filename=~s{^\Q$londocroot\E}{};
1.344 raeburn 1266: if (!(grep($filename =~ /\Q$_\E$/,@noexamplelink))) {
1267: $result .= ' <span class="LC_fontsize_small">'
1268: .&Apache::loncommon::modal_link(
1269: $filename.'?inhibitmenu=yes',&mt('Example'),600,420,'sample')
1270: .'</span>';
1271: }
1.341 bisitz 1272: $result .= '<br />'."\n";
1.287 raeburn 1273: $count ++;
1274: }
1275: if ($numfiles > 0) {
1.314 bisitz 1276: $result .= '</div></div>'."\n".'<div class="LC_clear_float_footer"></div>'."\n";
1.42 albertel 1277: }
1.145 albertel 1278: return $result;
1.42 albertel 1279: }
1280:
1281: sub newproblem {
1.65 matthew 1282: my ($request) = @_;
1.282 albertel 1283:
1284: if ($env{'form.template'}) {
1285: my $file = $env{'form.template'};
1.65 matthew 1286: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1287: &File::Copy::copy($file,$dest);
1.65 matthew 1288: &renderpage($request,$dest);
1.282 albertel 1289: return;
1290: }
1291:
1292: my ($extension) = ($request->uri =~ m/\.(\w+)$/);
1293: &Apache::lonxml::debug("Looking for :$extension:");
1294: my $templatelist=&get_template_list($extension);
1295: if ($env{'form.newfile'} && !$templatelist) {
1296: # no templates found
1.131 albertel 1297: my $templatefilename =
1.258 albertel 1298: $request->dir_config('lonIncludes').'/templates/blank.'.$extension;
1.131 albertel 1299: &Apache::lonxml::debug("$templatefilename");
1300: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.282 albertel 1301: &File::Copy::copy($templatefilename,$dest);
1.131 albertel 1302: &renderpage($request,$dest);
1.85 albertel 1303: } else {
1.176 albertel 1304: my $url=&HTML::Entities::encode($request->uri,'<>&"');
1.65 matthew 1305: my $dest = &Apache::lonnet::filelocation("",$request->uri);
1.128 albertel 1306: my $errormsg;
1.85 albertel 1307: my $instructions;
1.330 raeburn 1308: my $brcrum = [{'href' => &Apache::loncommon::authorspace($request->uri),
1.338 raeburn 1309: 'text' => 'Authoring Space'},
1.312 bisitz 1310: {'href' => '',
1311: 'text' => "Create New $extension"}];
1.240 albertel 1312: my $start_page =
1.312 bisitz 1313: &Apache::loncommon::start_page("Create New $extension",
1314: undef,
1315: {'bread_crumbs' => $brcrum,});
1.311 bisitz 1316: $request->print(
1317: $start_page
1318: .&Apache::loncommon::head_subbox(
1319: &Apache::loncommon::CSTR_pageheader())
1320: .'<h1>'.&mt("Creating a new $extension resource.")."</h1>
1.128 albertel 1321: $errormsg
1.258 albertel 1322: ".&mt("The requested file [_1] currently does not exist.",
1.329 raeburn 1323: '<span class="LC_filename">'.$url.'</span>').'
1.314 bisitz 1324: <p class="LC_info">
1325: '.&mt("To create a new $extension, select a template from the".
1326: " list below. Then click on the \"Create $extension\" button.").'
1327: </p><div><form action="'.$url.'" method="post">');
1.258 albertel 1328:
1.85 albertel 1329: if (defined($templatelist)) {
1.282 albertel 1330: $request->print($templatelist);
1.85 albertel 1331: }
1.282 albertel 1332: $request->print('<br /><input type="submit" name="newfile" value="'.
1333: &mt("Create $extension").'" />');
1.314 bisitz 1334: $request->print('</form></div>'.&Apache::loncommon::end_page());
1.65 matthew 1335: }
1.282 albertel 1336: return;
1.42 albertel 1337: }
1338:
1.268 albertel 1339: sub update_construct_style {
1340: if ($env{'request.state'} eq "construct"
1.289 raeburn 1341: && $env{'form.problemmode'} eq 'view'
1.268 albertel 1342: && defined($env{'form.submitted'})
1343: && !defined($env{'form.resetdata'})
1344: && !defined($env{'form.newrandomization'})) {
1345: if ((!$env{'form.style_file'} && $env{'construct.style'})
1346: ||$env{'form.clear_style_file'}) {
1.303 raeburn 1347: &Apache::lonnet::delenv('construct.style');
1.268 albertel 1348: } elsif ($env{'form.style_file'}
1349: && $env{'construct.style'} ne $env{'form.style_file'}) {
1.291 raeburn 1350: &Apache::lonnet::appenv({'construct.style' =>
1351: $env{'form.style_file'}});
1.268 albertel 1352: }
1353: }
1354: }
1355:
1356:
1.41 albertel 1357: sub handler {
1.145 albertel 1358: #my $t0 = [&gettimeofday()];
1359: my $request=$_[0];
1.223 albertel 1360: $Apache::lonxml::request=$request;
1.204 albertel 1361: $Apache::lonxml::debug=$env{'user.debug'};
1362: $env{'request.uri'}=$request->uri;
1.180 albertel 1363: &setuppermissions();
1.193 albertel 1364:
1.145 albertel 1365: my $file=&Apache::lonnet::filelocation("",$request->uri);
1366:
1367: #check if we know where we are
1.344.2.2 raeburn 1368: if ($env{'request.course.fn'} && !&Apache::lonnet::symbread('','',1,1)) {
1.145 albertel 1369: # if we are browsing we might not be able to know where we are
1.173 albertel 1370: if ($Apache::lonhomework::browse ne 'F' &&
1.204 albertel 1371: $env{'request.state'} ne "construct") {
1.145 albertel 1372: #should know where we are, so ask
1.253 albertel 1373: &unset_permissions();
1374: $request->internal_redirect('/adm/ambiguous');
1375: return OK;
1.145 albertel 1376: }
1377: }
1.253 albertel 1378: if (&setupheader($request)) {
1379: &unset_permissions();
1380: return OK;
1381: }
1.244 albertel 1382: &Apache::lonxml::debug("Permissions:$Apache::lonhomework::browse:$Apache::lonhomework::viewgrades:$Apache::lonhomework::modifygrades:$Apache::lonhomework::queuegrade");
1.204 albertel 1383: &Apache::lonxml::debug("Problem Mode ".$env{'form.problemmode'});
1.261 albertel 1384: my ($symb) = &Apache::lonnet::whichuser();
1.145 albertel 1385: &Apache::lonxml::debug('symb is '.$symb);
1.204 albertel 1386: if ($env{'request.state'} eq "construct") {
1.145 albertel 1387: if ( -e $file ) {
1388: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1389: ['problemmode']);
1.204 albertel 1390: if (!(defined $env{'form.problemmode'})) {
1.145 albertel 1391: #first visit to problem in construction space
1.289 raeburn 1392: $env{'form.problemmode'}= 'view';
1.145 albertel 1393: &renderpage($request,$file);
1.323 www 1394: } elsif (($env{'form.problemmode'} eq 'editxml') ||
1395: ($env{'form.problemmode'} eq 'saveeditxml') ||
1396: ($env{'form.problemmode'} eq 'saveviewxml') ||
1397: ($env{'form.problemmode'} eq 'undoxml')) {
1.145 albertel 1398: &editxmlmode($request,$file);
1.289 raeburn 1399: } elsif ($env{'form.problemmode'} eq 'calcanswers') {
1.145 albertel 1400: &analyze($request,$file);
1401: } else {
1.268 albertel 1402: &update_construct_style();
1.145 albertel 1403: &renderpage($request,$file);
1404: }
1405: } else {
1406: # requested file doesn't exist in contruction space
1407: &newproblem($request);
1408: }
1409: } else {
1410: # just render the page normally outside of construction space
1411: &Apache::lonxml::debug("not construct");
1.52 albertel 1412: &renderpage($request,$file);
1.41 albertel 1413: }
1.145 albertel 1414: #my $td=&tv_interval($t0);
1415: #&Apache::lonxml::debug("Spent $td seconds processing");
1416: # always turn off debug messages
1417: $Apache::lonxml::debug=0;
1.253 albertel 1418: &unset_permissions();
1.145 albertel 1419: return OK;
1.52 albertel 1420:
1.1 albertel 1421: }
1422:
1423: 1;
1424: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>