--- loncom/homework/bridgetask.pm	2006/06/13 20:28:49	1.169
+++ loncom/homework/bridgetask.pm	2006/10/11 21:06:49	1.184
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # definition of tags that give a structure to a document
 #
-# $Id: bridgetask.pm,v 1.169 2006/06/13 20:28:49 albertel Exp $
+# $Id: bridgetask.pm,v 1.184 2006/10/11 21:06:49 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,27 @@ 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)) {
+	$user = $env{'user.name'};
+	$domain = $env{'user.domain'};
+    }
+
+    $Apache::lonhomework::results{"resource.$id.checkedin"}=
+	$user.':'.$domain;
+
+    if (defined($slot_name)) {
+	$Apache::lonhomework::results{"resource.$id.checkedin.slot"}=
+	    $slot_name;
     }
 }
 
@@ -316,6 +350,38 @@ 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();
+	&Apache::lonxml::debug(" tossing out $result ");
+	&Apache::lonxml::debug(" usining out $dim 's  ". $dimension{$dim}{'result'});
+	return $dimension{$dim}{'result'};
+    }
+    return $result;
+}
+
 sub internal_location {
     my ($id)=@_;
     return '<!-- LONCAPA_INTERNAL_ADD_TASK_STATUS'.$id.' -->';
@@ -326,8 +392,10 @@ sub submission_time_stamp {
     my $submissiontime;
     my $version=$Apache::lonhomework::history{'resource.0.version'};
     for (my $v=$Apache::lonhomework::history{'version'};$v>0;$v--) {
-	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})) {
+	if (defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.bridgetask.portfiles'})
+	    && defined($Apache::lonhomework::history{$v.':resource.'.$version.'.0.tries'})) {
 	    $submissiontime=$Apache::lonhomework::history{$v.':timestamp'};
+	    last;
 	}
     }
     my $result;
@@ -338,16 +406,18 @@ sub submission_time_stamp {
 	my ($color,$when)=('#FF6666','after');
 	if ($diff > 0) { ($color,$when)=('#336600','before'); }
 	my $info;
+	$diff = abs($diff);
 	if ($diff%60) { $info=($diff%60).' seconds'; }
 	$diff=int($diff/60);
 	if ($diff%60) { $info=($diff%60).' minutes '.$info; }
 	$diff=int($diff/60);
 	if ($diff) {    $info=$diff.' hours '.$info; }
 	$result='<p><font color="'.$color.'">'.
-	    &mt('Student submitted [_1] [_2] the deadline. 
-                 (Submission was at [_3], end of period was [_4].)',
-		$info,$when,scalar(localtime($submissiontime)),
-		scalar(localtime($slot{'endtime'}))).
+	    &mt('Student submitted [_1] [_2] the deadline. '.
+		'(Submission was at [_3], end of period was [_4].)',
+		$info,$when,
+		&Apache::lonlocal::locallocaltime($submissiontime),
+		&Apache::lonlocal::locallocaltime($slot{'endtime'})).
 		'</font></p>';
     }
     return $result;
@@ -390,7 +460,7 @@ sub webgrade_standard_info {
 	    'fail'   => 'Fail Rest',
 	    'cancel' => 'Cancel',
 	    );
-    my %lt=&Apache::lonlocal::texthash(%lt);
+    %lt=&Apache::lonlocal::texthash(%lt);
 
     my $result=<<INFO;
   <div class="LC_GRADING_maincontrols">
@@ -469,8 +539,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 +603,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});
@@ -995,6 +1067,17 @@ DONEBUTTON
 		    &Apache::loncommon::end_page({'discussion' => 1});
 	    }
 	}
+
+	my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
+	my %queue_data = ('type' => 'Task',
+			  'time' => time,);
+	if (defined($Apache::inputtags::slot_name)) {
+	    $queue_data{'slot'} = $Apache::inputtags::slot_name;
+	} elsif (defined($Apache::lonhomework::history{"resource.$version.0.checkedin.slot"})) {
+	    $queue_data{'slot'} = $Apache::lonhomework::history{"resource.$version.0.checkedin.slot"};
+	}
+	
+
 	if ($target eq 'grade' && !$env{'form.webgrade'} && !$previous) {
 	    my $award='SUBMITTED';
 	    &Apache::essayresponse::file_submission("$version.0",'bridgetask',
@@ -1017,20 +1100,8 @@ DONEBUTTON
 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
 	    &Apache::structuretags::finalize_storage();
 	    if ($award eq 'SUBMITTED') {
-		my $useslots = &Apache::lonnet::EXT("resource.0.useslots");
-		if ($useslots =~ /^\s*no\s*$/i) {
-		    &add_to_queue('gradingqueue',
-				  {'type' => 'Task',
-				   'time' => time});
-		} elsif (defined($Apache::inputtags::slot_name)) {
-		    &add_to_queue('gradingqueue',
-				  {'type' => 'Task',
-				   'time' => time,
-				   'slot' => $Apache::inputtags::slot_name});
-		}
+		&add_to_queue('gradingqueue',\%queue_data);
 	    }
-	} elsif ($Apache::lonhomework::results{'INTERNAL_store'}) {
-	    &Apache::structuretags::finalize_storage();
 	}
 	if ($target eq 'grade' && $env{'form.webgrade'} eq 'yes' 
 	    && exists($env{'form.cancel'})) {
@@ -1075,14 +1146,20 @@ DONEBUTTON
 		    &check_queue_unlock($env{'form.queue'});
 		    &Apache::lonxml::debug(" still needs review not changing status.");
 		} else {
-		    &move_between_queues($env{'form.queue'},'reviewqueue');
+		    if ($env{'form.queue'} ne 'none') {
+			&move_between_queues($env{'form.queue'},'reviewqueue');
+		    } else {
+			&add_to_queue('reviewqueue',\%queue_data);
+		    }
 		}
 	    } elsif ($ungraded) {
 		$Apache::lonhomework::results{"resource.$version.0.status"}='ungraded';
-		if ($env{'form.queue'} eq 'reviewqueue' ||
-		    $env{'form.queue'} eq 'none' ) {
+		if ($env{'form.queue'} eq 'reviewqueue') {
 		    &Apache::lonxml::debug("moving back.");
-		    &move_between_queues($env{'form.queue'},'gradingqueue');
+		    &move_between_queues($env{'form.queue'},
+					 'gradingqueue');
+		} elsif ($env{'form.queue'} eq 'none' ) {
+		    &add_to_queue('gradingqueue',\%queue_data);	
 		} else {
 		    &check_queue_unlock($env{'form.queue'});
 		}
@@ -1135,6 +1212,10 @@ DONEBUTTON
 	    &minimize_storage();
 	    &Apache::structuretags::finalize_storage();
 	}
+	if (exists($Apache::lonhomework::results{'INTERNAL_store'})) {
+	    # instance generation occured and hasn't yet been stored
+	    &Apache::structuretags::finalize_storage();
+	}
     } elsif ($target eq 'webgrade') {
 	$result.="</div>";
 	#$result.='<input type="submit" name="next" value="'.
@@ -1157,7 +1238,12 @@ DONEBUTTON
 						      'Partial Credit Factor');
 	$result.=&Apache::response::meta_stores_write('status','string',
 						      'Bridge Task Status');
+    } elsif ($target eq 'edit') {
+	&Apache::structuretags::reset_problem_globals('Task');
+	undef($Apache::lonhomework::parsing_a_task);
+	return ('','no');
     }
+    &Apache::structuretags::reset_problem_globals('Task');
     undef($Apache::lonhomework::parsing_a_task);
     return $result;
 }
@@ -1907,27 +1993,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);
-	# FIXME need to add as a 'criteria' for nesting
-	$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 +2078,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 +2124,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') {
@@ -2072,10 +2202,11 @@ sub get_criteria {
 
 		    foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
 				    @{$dimension{$dim}{'criterias'}}) {
-			# FIXME need to collect the dimension style criteria 
-			# and output them here.
 			my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
-			next if ($type eq 'dimension');
+			if ($type eq 'dimension') {
+			    $result.=$dimension{$id}{'result'};
+			    next;
+			}
 			my $status= &get_criteria('status', $version,$dim,$id);
 			my $comment=&get_criteria('comment',$version,$dim,$id);
 			my $mandatory=($dimension{$dim}{'criteria.'.$id.'.mandatory'} ne 'N');
@@ -2094,8 +2225,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">'.
@@ -2107,26 +2239,31 @@ sub get_criteria {
 	    }
 	} 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,
+			       'delayed_dim_results' => 1});
 	    foreach my $id (@{$dimension{$dim}{$instance.'.criterias'}},
 			    @{$dimension{$dim}{'criterias'}} ) {
-		# dimensional 'criteria' don't get assigned grades
 		my $type = $dimension{$dim}{'criteria.'.$id.'.type'};
-		next if ($type eq 'dimension');
+		if ($type eq 'dimension') {
+		    # dimensional 'criteria' don't get assigned grades
+		    $result.=
+			&nested_parse(\$dimension{$dim}{'criteria.'.$id},
+				      [@_],{'set_dim_id' => $id});
+		    next;
+		}
 
 		my $link=&link($id);
 		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".
@@ -2144,18 +2281,22 @@ sub get_criteria {
 		$result.=&grading_history($version,$dim,$id);
 		$last_link=$link;
 	    }
+	    if (&nest()) {
+		&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'}) {
 	    my $optional_passed=0;
 	    my $mandatory_failed=0;
 	    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'}}) {
@@ -2166,6 +2307,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);
 
@@ -2211,6 +2356,7 @@ sub get_criteria {
 	    # any other targets no output
 	    undef($result);
 	}
+	&disable_dimension_parsing();
 	pop(@Apache::bridgetask::dimension);
 	return $result;
     }