[LON-CAPA-users] Referencing data from one problem in another

Stuart Raeburn raeburn at msu.edu
Tue Apr 17 23:24:32 EDT 2012


One thing to keep in mind when creating problems which use information  
from a different problem is the impact on re-usability.

If you require paired problems to operate together in a course in such  
a way that one problem requires data from another problem, it would be  
good coding practice to test for the existence of the other problem,  
and to alert the user (e.g., via an <instructorcomment>) if the other  
problem is not present in the course.

Another thing to bear in mind is that such problems will show degraded  
behavior when previewing and testing in Construction Space, and  
instead need to be published and imported into a course for the full  
capabilities to be seen.

That is not too much of an inconvenience, given (a) access to a test  
course, and (b) the availability of the "Edit resource" function for  
an item in a course, for which the corresponding Construction Space is  
housed on the server hosting your current session.

One question I have is what is your particular use case where separate  
problems are preferred to a multi-part problem?

Anyway, the technique you identify -- defining mapalias(es) for the  
problem(s) in a published sequence file, and importing the published  
sequence into a course, is the strategy to use here.

You will, however, be restricted in the data to which you have access  
between problems. You should be able to retrieve preserved data (i.e.,  
data stored for a particular problem -- including a student's  
submission), but you will not have access to perl variables in a  
different problem.  That restriction occurs because of the way  
LON-CAPA carries out its randomization - this occurs during the  
process of rendering a problem.

Future LON-CAPA versions will store the randomseed as part of the  
stored submission history, which could provide a means to re-construct  
perl variables used in one problem, when rendering a different  
problem, as long as the student has made at least one submission in  
the first problem.  Shared script blocks in published library files  
would be a way to take advantage of that.

However, for now, you'll be restricted to the data currently available  
from the stored submission record, which includes things such as  
submission, award, solved etc., accessed via an &EXT() call, as shown  
in /res/msu/albertel/test/ext_examples.library etc., when browsing  
resource space.


As a proof of concept the following pair of problems within a  
published sequence, use an &EXT() call in the second problem to access  
the submission from the first problem to determine what the correct  
answer will be for the second problem.


p1.problem (mapalias = first)


<script type="loncapa/perl">
$a = &random(0,5,0.1);
$b = &random(3,12,0.1);
$c = $a + $b;</script>
<startouttext />What is $a + $b?
<endouttext />
<numericalresponse answer="$c" id="11">
     <responseparam name="tol" type="tolerance" default="0.01%"  
description="Numerical Tolerance" />

	<textline readonly="no" />


p2.problem (mapalias = second)


<script type="loncapa/perl">
$othersub = &EXT('user.resource.resource.0.11.submission','first');
if ($othersub eq '') {
     $othersub = 0;
$answer = 2.0*$othersub;
<startouttext />What is double the last number you entered for the  
previous question?<br />
<endouttext />
<numericalresponse answer="$answer" id="11">
     <responseparam name="tol" type="tolerance" default="0.01%"  
description="Numerical Tolerance" />
	<textline readonly="no" />


published sequence (with mapaliases)


<param to="1" value="first" name="parameter_0_mapalias" type="string" />
<param to="2" value="second" name="parameter_0_mapalias" type="string" />
<resource src="/res/raeburn/stuart/paired/p1.problem" id="1"  
type="start" title="First One" />
<resource src="/res/raeburn/stuart/paired/p2.problem" id="2"  
type="finish" title="Last One" />
<link to="2" index="1" from="1" />


If you import the published sequence into a course (having set  
appropriate domain and username in the src attributes in the map), you  
will find that p2.problem will access the submission from p1.problem.

If you take a look at the EXT subroutine in lonnet.pm (e.g., see:

you will see this block of code:

     if ($realm eq 'user') {
# ---------------------------------------------------------------  
         if ($space eq 'resource') {
             if ( (defined($Apache::lonhomework::parsing_a_problem)
                   || defined($Apache::lonhomework::parsing_a_task))
                  ($symbparm eq &symbread()) ) {
                 # if we are in the middle of processing the resource the
                 # get the value we are planning on committing
                 if (defined($Apache::lonhomework::results{$qualifierrest})) {
                     return $Apache::lonhomework::results{$qualifierrest};
                 } else {
                     return $Apache::lonhomework::history{$qualifierrest};
             } else {
                 my %restored;
                 if ($publicuser || $env{'request.state'} eq 'construct') {
                 } else {
                 return $restored{$qualifierrest};

The key item here is this test:

($symbparm eq &symbread())

which compares the unique resource instance identifier (the "symb") of  
the item you are asking about -- which will have been interpolated  
from the mapalias argument in your &EXT() call -- with the value of  
symbread, which is the "symb" for the problem currently being rendered.

If they are different, which is the case you are asking about, the  
data available come from:


which is a call to restore the record for the non-current "symb" in  
course: $courseid for user $uname:$udom.


Stuart Raeburn

Quoting "Mills, Douglas G" <dmills at illinois.edu>:

> Following up on the question below, I know that you can use &EXT   
> functions inside a Conditional statement in a sequence, for example,  
>  to access information from within one of the problems in the   
> sequence via that problem's mapalias. I'm experimenting to see if   
> that works also between two problems in a course if one of them has   
> a mapalias set up, etc.  It appears that this does not work, unless   
> perhaps I'm not doing it right...  Any tips on the boundaries that   
> an &EXT function can cross vs. those it cannot, or guidance in its   
> use or documentation besides
> https://s10.lite.msu.edu/res/msu/albertel/test/ext_examples.problem#0
> Thank you!
> Doug
> On 4/12/12 3:29 PM, "Douglas Mills" <dmills at ad.uiuc.edu> wrote:
> Hi All,
> I know this can be done between parts in a multipart problem, but is  
>  it also possible to reference student input or other PERL variables  
>  in one problem from a separate problem?  If so, what is the   
> mechanism for doing so?  Is there an &EXT function that allows that?  
>  Thanks!
> Doug
> Douglas Mills
> Director of Instructional Technologies
> Department of Chemistry
> University of Illinois
> dmills at illinois.edu
> (217) 244-5739

More information about the LON-CAPA-users mailing list