--- loncom/homework/bridgetask.pm	2006/06/13 21:34:28	1.171
+++ loncom/homework/bridgetask.pm	2006/06/24 01:28:17	1.179
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # definition of tags that give a structure to a document
 #
-# $Id: bridgetask.pm,v 1.171 2006/06/13 21:34:28 albertel Exp $
+# $Id: bridgetask.pm,v 1.179 2006/06/24 01:28:17 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -50,7 +50,7 @@ my %dimension;
 sub initialize_bridgetask {
     # id of current Dimension, 0 means that no dimension is current 
     # (inside <Task> only)
-    %Apache::bridgetask::dimension=();
+    @Apache::bridgetask::dimension=();
     # list of all Dimension ids seen
     %Apache::bridgetask::top_dimensionlist=();
     # list of all current Instance ids
@@ -83,7 +83,7 @@ sub proctor_check_auth {
 		}
 	    }
 	    if ($authenticated) {
-		&create_new_version($type,$user,$domain,$slot_name);
+		&check_in($type,$user,$domain,$slot_name);
 		return 1;
 	    }
 	}
@@ -91,8 +91,43 @@ sub proctor_check_auth {
     return 0;
 }
 
+sub check_in {
+    my ($type,$user,$domain,$slot_name) = @_;
+    my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
+    if ( $useslots eq 'map_map') {
+	&check_in_sequence($user,$domain,$slot_name);
+    } else {
+	&create_new_version($type,$user,$domain,$slot_name);
+    }
+    return 1;
+}
+
+sub check_in_sequence {
+    my ($user,$domain,$slot_name) = @_;
+    my $navmap = Apache::lonnavmaps::navmap->new();
+    my ($symb) = &Apache::lonxml::whichuser();
+    my ($map)  = &Apache::lonnet::decode_symb($symb);
+    my @resources = 
+	$navmap->retrieveResources($map, sub { $_[0]->is_problem() },0,0);
+    my %old_history = %Apache::lonhomework::history;
+    my %old_results = %Apache::lonhomework::results;
+
+    foreach my $res (@resources) {
+	&Apache::lonxml::debug("doing ".$res->src);
+	&Apache::structuretags::initialize_storage($res->symb);
+	my $type = ($res->is_task()) ? 'Task' : 'problem';
+	&create_new_version($type,$user,$domain,$slot_name);
+	&Apache::structuretags::finalize_storage($res->symb);
+    }
+    
+    %Apache::lonhomework::history = %old_history;
+    %Apache::lonhomework::results = %old_results;
+}
+
 sub create_new_version {
     my ($type,$user,$domain,$slot_name) = @_;
+    
+    my $id = '0';
     if ($type eq 'Task') {
 	# increment version
 	my $version=
@@ -107,28 +142,24 @@ sub create_new_version {
 	}
 	
 	#setup new version and who did it
-	$Apache::lonhomework::results{'resource.0.version'}=$version;
-	if (defined($user) && defined($domain)) {
-	    $Apache::lonhomework::results{"resource.$version.0.checkedin"}=
-		$user.':'.$domain;
-	} else {
-	    $Apache::lonhomework::results{"resource.$version.0.checkedin"}=
-		$env{'user.name'}.':'.$env{'user.domain'};
-	}
-	if (defined($slot_name)) {
-	    $Apache::lonhomework::results{"resource.$version.0.checkedin.slot"}=
-		$slot_name;
+       	$Apache::lonhomework::results{'resource.0.version'}=$version;
+	$id = "$version.0";
+	if (!defined($user) || !defined($domain)) {
+	    $user = $env{'user.name'};
+	    $domain = $env{'user.domain'};
 	}
+	
     } elsif ($type eq 'problem') {
 	&Apache::lonxml::debug("authed $slot_name");
-	if (defined($user) && defined($domain)) {
-	    $Apache::lonhomework::results{"resource.0.checkedin"}=
-		$user.':'.$domain;
-	}
-	if (defined($slot_name)) {
-	    $Apache::lonhomework::results{"resource.0.checkedin.slot"}=
-		$slot_name;
-	}
+    }
+
+    if (defined($user) && defined($domain)) {
+	$Apache::lonhomework::results{"resource.$id.checkedin"}=
+	    $user.':'.$domain;
+    }
+    if (defined($slot_name)) {
+	$Apache::lonhomework::results{"resource.$id.checkedin.slot"}=
+	    $slot_name;
     }
 }
 
@@ -316,6 +347,36 @@ sub show_task {
     return 1;
 }
 
+my @delay;
+sub nest { 
+    if (@delay) {
+	return $delay[-1];
+    } else {
+	return;
+    }
+}
+
+sub nested_parse {
+    my ($str,$env,$args) = @_;
+    my @old_env = @Apache::scripttag::parser_env;
+    @Apache::scripttag::parser_env = @$env;
+    if (exists($args->{'set_dim_id'})) {
+	&enable_dimension_parsing($args->{'set_dim_id'});
+    }
+    push(@delay,(($args->{'delayed_dim_results'})? 1 : 0));
+    my $result = &Apache::scripttag::xmlparse($$str);
+    pop(@delay);
+    if (exists($args->{'set_dim_id'})) {
+	&disable_dimension_parsing();
+    }
+    @Apache::scripttag::parser_env = @old_env;
+    if ($args->{'delayed_dim_results'}) {
+	my $dim = &get_dim_id();
+	return $dimension{$dim}{'result'};
+    }
+    return $result;
+}
+
 sub internal_location {
     my ($id)=@_;
     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
@@ -469,8 +530,8 @@ sub start_Task {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
 
     my ($status,$accessmsg,$slot);
+    &Apache::structuretags::init_problem_globals('Task');
     if ($target ne 'webgrade') {
-	&Apache::structuretags::init_problem_globals('Task');
 	&Apache::structuretags::initialize_storage();
 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
 	if ($env{'request.state'} eq 'construct') {
@@ -533,7 +594,9 @@ sub start_Task {
 	($status,$accessmsg,my $slot_name,$slot) = 
 	    &Apache::lonhomework::check_slot_access('0','Task');
 	if ($status eq 'CAN_ANSWER' && $version eq '') {
-	    &create_new_version('Task',undef,undef,$slot_name);
+	    # CAN_ANSWER mode, and no current version, unproctored access
+	    # thus self-checkedin
+	    &check_in('Task',undef,undef,$slot_name);
 	    &add_to_queue('gradingqueue',{'type' => 'Task',
 					  'time' => time,
 					  'slot' => $slot_name});
@@ -1158,6 +1221,7 @@ DONEBUTTON
 	$result.=&Apache::response::meta_stores_write('status','string',
 						      'Bridge Task Status');
     }
+    &Apache::structuretags::reset_problem_globals('Task');
     undef($Apache::lonhomework::parsing_a_task);
     return $result;
 }
@@ -1907,27 +1971,55 @@ sub start_Setup {
     &Apache::lonxml::startredirection();
     return &internal_location($dim);
 }
+
+{
+    my @allowed;
+    sub enable_dimension_parsing {
+	my ($id) = @_;
+	push(@allowed,$id);
+    }
+    sub disable_dimension_parsing {
+	pop(@allowed);
+    }
+    sub skip_dimension_parsing {
+	my ($check) = @_;
+	if (!@allowed) { return 0;}
+	# if unspecified allow any id
+	if ($allowed[-1] eq undef) { return 0;}
+
+	return ($check ne $allowed[-1]);
+    }
+}
+
 sub start_Question { return &start_Dimension(@_); }
 sub start_Dimension {
-    my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
     my $dim = &get_id($parstack,$safeeval);
     my $previous_dim;
     if (@Apache::bridgetask::dimension) {
 	$previous_dim = $Apache::bridgetask::dimension[-1];
 	push(@{$Apache::bridgetask::dimension{$previous_dim}{'contains'}},
 	     $dim);
-	$dimension{$previous_dim}{'criteria.'.$dim}='';
+	if(&skip_dimension_parsing($dim)) {
+	    $dimension{$previous_dim}{'criteria.'.$dim} =
+		$token->[4]
+		.&Apache::lonxml::get_all_text('/'.$tagstack->[-1],$parser,
+					      $style)
+		.'</'.$tagstack->[-1].'>';
+	}
 	$dimension{$previous_dim}{'criteria.'.$dim.'.type'}='dimension';
 	$dimension{$previous_dim}{'criteria.'.$dim.'.mandatory'}=
 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
 	push(@{$dimension{$previous_dim}{'criterias'}},$dim);
 	$dimension{$dim}{'nested'}=$previous_dim;
+	&Apache::lonxml::debug("adding $dim as criteria to $previous_dim");
     } else {
 	$Apache::bridgetask::top_dimensionlist{$dim}{'manadatory'}=
 	    &Apache::lonxml::get_param('Mandatory',$parstack,$safeeval);
     }
     push(@Apache::bridgetask::dimension,$dim);
     &Apache::lonxml::startredirection();
+    &enable_dimension_parsing($dim);
     return &internal_location($dim);
 }
 
@@ -1964,20 +2056,22 @@ sub get_instance {
 	if (defined($instance)) { return $instance; }
 
 	&Apache::response::pushrandomnumber();
-	my @instances = @{$dimension{$dim}{'instances'}};
-	# remove disabled instances
-	for (my $i=0; $i < $#instances; $i++) {
-	    if ($dimension{$dim}{$instances[$i].'.disabled'}) {
-		splice(@instances,$i,1);
-		$i--;
+	if (ref($dimension{$dim}{'instances'}) eq 'ARRAY') {
+	    my @instances = @{$dimension{$dim}{'instances'}};
+	    # remove disabled instances
+	    for (my $i=0; $i < $#instances; $i++) {
+		if ($dimension{$dim}{$instances[$i].'.disabled'}) {
+		    splice(@instances,$i,1);
+		    $i--;
+		}
+	    }
+	    @instances = &Math::Random::random_permutation(@instances);
+	    $instance  = $instances[($version-1)%scalar(@instances)];
+	    if ($version =~ /^\d$/) {
+		$Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
+		    $instance;
+		$Apache::lonhomework::results{'INTERNAL_store'} = 1; 
 	    }
-	}
-	@instances = &Math::Random::random_permutation(@instances);
-	$instance  = $instances[($version-1)%scalar(@instances)];
-	if ($version =~ /^\d$/) {
-	    $Apache::lonhomework::results{"resource.$version.0.$dim.instance"} = 
-		$instance;
-	    $Apache::lonhomework::results{'INTERNAL_store'} = 1; 
 	}
 	&Apache::response::poprandomnumber();
 	return $instance;
@@ -2008,26 +2102,40 @@ sub get_criteria {
 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 	my $result=&Apache::lonxml::endredirection();
 	my $dim=&get_id($parstack,$safeeval);
+	if (&skip_dimension_parsing($dim)) {
+	    &disable_dimension_parsing($dim);
+	    pop(@Apache::bridgetask::dimension);
+	    return;
+	}
 	my $instance=&get_instance($dim);
 	my $version=&get_version();
 	if ($target eq 'web') {
-	    @Apache::scripttag::parser_env = @_;
-	    $result.=&Apache::scripttag::xmlparse($dimension{$dim}{'intro'});
+	    $result .= &nested_parse(\$dimension{$dim}{'intro'},[@_]);
 	    my @instances = $instance;
 	    if (&Apache::response::showallfoils()) {
 		@instances = @{$dimension{$dim}{'instances'}};
 	    }
 	    my $shown_question_text;
 	    foreach my $instance (@instances) {
-		@Apache::scripttag::parser_env = @_;
-		$result.=&Apache::scripttag::xmlparse($dimension{$dim}{$instance.'.text'});
-		@Apache::scripttag::parser_env = @_;
-		$result.=&Apache::scripttag::xmlparse($dimension{$dim}{'questiontext'});
-
+		$result .= &nested_parse(\$dimension{$dim}{$instance.'.text'},
+					 [@_]);
+		$result .= &nested_parse(\$dimension{$dim}{'questiontext'},
+					 [@_],{'set_dim_id' => undef});
 		my $task_status = 
 		    $Apache::lonhomework::history{"resource.$version.0.status"};
-		if ($task_status eq 'pass' || $task_status eq 'fail') {
+		if ($task_status ne 'pass' && $task_status ne 'fail') {
 
+		    foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
+				    @{$dimension{$dim}{'criterias'}}) {
+			my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
+			&Apache::lonxml::debug("$id is $type");
+			if ($type eq 'dimension') {
+			    $result.=
+				&nested_parse(\$dimension{$dim}{'criteria.'.$id},
+					      [@_],{'set_dim_id' => $id});
+			}
+		    }
+		} else {
 		    my $dim_status=$Apache::lonhomework::history{"resource.$version.0.$dim.status"};
 		    my $mandatory='Mandatory';
 		    if ($Apache::bridgetask::dimensionmandatory{$dim} eq 'N') {
@@ -2095,8 +2203,9 @@ sub get_criteria {
 			$result.=
 			    '<div class="LC_'.$status.' LC_criteria"><h4>'
 			    .$mandatory.' Criteria</h4><p>';
-			@Apache::scripttag::parser_env = @_;
-			$result.=&Apache::scripttag::xmlparse($dimension{$dim}{'criteria.'.$id});
+			$result.=
+			    &nested_parse(\$dimension{$dim}{'criteria.'.$id},
+					  [@_],{'set_dim_id' => $id});
 			$result.='</p><p class="LC_grade">'.$status_display.'</p>';
 			if ($comment) {
 			    $result.='<p class="LC_comment">'.
@@ -2106,24 +2215,22 @@ sub get_criteria {
 		    }
 		}
 	    }
-	    if (exists($dimension{$dim}{'nested'})) {
-		$dimension{$dim}{'result'}=$result;
-		undef($result);
-	    }
 	} elsif ($target eq 'webgrade') {
 	    # in case of any side effects that we need
-	    @Apache::scripttag::parser_env = @_;
-	    &Apache::scripttag::xmlparse($dimension{$dim}{'intro'});
-	    @Apache::scripttag::parser_env = @_;
-	    &Apache::scripttag::xmlparse($dimension{$dim}{$instance.'.text'});
-	    @Apache::scripttag::parser_env = @_;
-	    &Apache::scripttag::xmlparse($dimension{$dim}{'questiontext'});
+	    &nested_parse(\$dimension{$dim}{'intro'},[@_]);
+	    &nested_parse(\$dimension{$dim}{$instance.'.text'},[@_]);
+	    $result.=
+		&nested_parse(\$dimension{$dim}{'questiontext'},[@_],
+			      {'set_dim_id'        => undef,
+			       'delay_dim_results' => 1});
 	    foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
 			    @{$dimension{$dim}{'criterias'}} ) {
 		my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
 		if ($type eq 'dimension') {
 		    # dimensional 'criteria' don't get assigned grades
-		    $result.=$dimension{$id}{'result'};
+		    $result.=
+			&nested_parse(\$dimension{$dim}{'criteria.'.$id},
+				      [@_],{'set_dim_id' => $id});
 		    next;
 		}
 
@@ -2131,10 +2238,10 @@ sub get_criteria {
 		my $status= &get_criteria('status',$version,$dim,$id);
 		$result.='<div class="LC_GRADING_criteria" id="'.$link.'">'."\n".
 		    '<div class="LC_GRADING_criteriatext" id="next_'.$last_link.'">'."\n";
-		@Apache::scripttag::parser_env = @_;
-		$result.=&Apache::scripttag::xmlparse($dimension{$dim}{'criteria.'.$id});
+		$result.=
+		    &nested_parse(\$dimension{$dim}{'criteria.'.$id},[@_]);
+
 		$result.='</div>'."\n".
-		    #$dimension{$dim}{'criteria.'.$id}.
 		    '<div class="LC_GRADING_grade">'."\n".
 		    '<label class="LC_GRADING_ungraded"><input type="radio" name="HWVAL_'.$link.'" value="ungraded" '.($status eq 'ungraded' || !$status ? 'checked="checked"':'').' />'.&mt('Ungraded').'</label>'."\n".
 		    '<label class="LC_GRADING_fail"><input type="radio" name="HWVAL_'.$link.'" value="fail" '.($status eq 'fail' ? 'checked="checked"':'').' />'.&mt('Fail').'</label>'."\n".
@@ -2152,8 +2259,9 @@ sub get_criteria {
 		$result.=&grading_history($version,$dim,$id);
 		$last_link=$link;
 	    }
-	    if (exists($dimension{$dim}{'nested'})) {
-		$dimension{$dim}{'result'}=$result;
+	    if (&delay_result()) {
+		&Apache::lonxml::debug(" for $dim stashing results into ".$dimension{$dim}{'nested'});
+		$dimension{$dimension{$dim}{'nested'}}{'result'}.=$result;
 		undef($result);
 	    }
 	} elsif ($target eq 'grade' && $env{'form.webgrade'}) {
@@ -2162,12 +2270,11 @@ sub get_criteria {
 	    my $ungraded=0;
 	    my $review=0;
 
-	    @Apache::scripttag::parser_env = @_;
-	    $result.=&Apache::scripttag::xmlparse($dimension{$dim}{'intro'});
-	    @Apache::scripttag::parser_env = @_;
-	    $result.=&Apache::scripttag::xmlparse($dimension{$dim}{$instance.'.text'});
-	    @Apache::scripttag::parser_env = @_;
-	    &Apache::scripttag::xmlparse($dimension{$dim}{'questiontext'});
+	    $result .= &nested_parse(\$dimension{$dim}{'intro'},[@_]);
+	    $result .= &nested_parse(\$dimension{$dim}{$instance.'.text'},
+				     [@_]);
+	    $result .= &nested_parse(\$dimension{$dim}{'questiontext'},
+				     [@_],{'set_dim_id' => undef});
 
 	    foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
 			    @{$dimension{$dim}{'criterias'}}) {
@@ -2178,6 +2285,10 @@ sub get_criteria {
 		    # dimensional 'criteria' don't get assigned grades
 		    $Apache::lonhomework::results{"resource.$version.0.$dim.$id.status"}=$env{'form.HWVAL_'.$link};
 		    $Apache::lonhomework::results{"resource.$version.0.$dim.$id.comment"}=$env{'form.HWVAL_comment_'.$link};
+		} else {
+		    $result .=
+			&nested_parse(\$dimension{$dim}{'criteria.'.$id},
+				      [@_],{'set_dim_id' => $id});
 		}
 		my $status= &get_criteria('status',$version,$dim,$id);
 
@@ -2223,6 +2334,7 @@ sub get_criteria {
 	    # any other targets no output
 	    undef($result);
 	}
+	&disable_dimension_parsing();
 	pop(@Apache::bridgetask::dimension);
 	return $result;
     }