[LON-CAPA-dev] Extracting problems from page or sequence

H. K. Ng lon-capa-dev@mail.lon-capa.org
Wed, 26 Feb 2003 13:37:06 -0500


Here is what I want to do.

     my $navmap = Apache::lonnavmaps::navmap-> new(
                                       $ENV{'request.course.fn'}.'.db',
                                       $ENV{'request.course.fn'}.'_parms.db',1, 
1);

# Below is the sym name of the page I am interested in. How do I extract 
the mapId from the sym? I had tried a few things but none of them works.

/fsu/ng/2053c_winter2003/CollegePhysicsA_Spring_2003.sequence___8___fsu/ng/2053c_winter2003/Assignment01.page

     my $map = $navmap->getById($mapId);

     my $iterator = $navmap->getIterator($map->map_start(),
                               $map->map_finish());


     my $depth = 1;
     $iterator->next(); # skip the first BEGIN_MAP
     my $curRes = $iterator->next(); # for "current resource"

     while ($depth > 0) {
         if($curRes == $iterator->BEGIN_MAP) { $depth++; }
         if($curRes == $iterator->END_MAP) { $depth++; }

         if (ref($curRes) && $curRes->is_problem()) {
# What I want to do here is
(1) to get the sym name for each problem that is enclosed in the above 
sequence/page,
(2) the title of the problem, and
(3) the parts in order.
# Once I have the 3 pieces of info, I can get the other things I need.

# I got it to work extracting the title for each sequence/page in a course 
but not the problems within a sequence/page.

             urRes->compTitle();

        }
         $curRes = $iterator->next();
     }





At 03:24 PM 2/21/03 -0500, you wrote:
>H. K. Ng wrote:
>>Presumably you are referring to the ID that is embedded in the symbolic 
>>name. I was able to get the page from which the problem comes from but 
>>could not get the symbolic names for other problems from the same page.
>
>I just committed a new lonnavmaps that includes a "getResourceByUrl" 
>method on the lonnavmaps object, so you can ask the $navmap for the map 
>directly if you know the URL. (It turns out to be useful in many contexts, 
>not surprisingly.)
>
>Of course if you want this before the next release that doesn't help much, 
>but you can look at the code as an example if you want, as the 
>"getResourceByUrl" code should work in the .6.2 release, if you want to 
>implement it somewhere else or include it in your local copy.
>
>Here's how it's coded (and documented) in the latest lonnavmaps, package 
>Apache::lonnavmaps::navmap :
>
>=pod
>
>=item * B<getResourceByUrl>(url): Retrieves a resource object by URL of 
>the resource. If passed a resource object, it will simply return it, so it 
>is safe to use this method in code like "$res = 
>$navmap->getResourceByUrl($res)", if you're not sure if $res is already an 
>object, or just a URL. If the resource appears multiple times in the 
>course, only the first instance will be returned. As a result, this is 
>probably useful only for maps.
>
>=cut
>
>sub getResourceByUrl {
>     my $self = shift;
>     my $resUrl = shift;
>
>     if (ref($resUrl)) { return $resUrl; }
>
>     $resUrl = &Apache::lonnet::clutter($resUrl);
>     my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
>     if ($resId =~ /,/) {
>         $resId = (split (/,/, $resId))[0];
>     }
>     if (!$resId) { return ''; }
>     return $self->getById($resId);
>}
>
>_______________________________________________
>LON-CAPA-dev mailing list
>LON-CAPA-dev@mail.lon-capa.org
>http://mail.lon-capa.org/mailman/listinfo/lon-capa-dev