File:  [LON-CAPA] / loncom / interface / lonnavmaps.pm
Revision 1.509.2.14.2.8: download - view: text, annotated - select for diffs
Sat May 27 12:40:25 2023 UTC (11 months, 3 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.509.2.14: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.563

    1: # The LearningOnline Network with CAPA
    2: # Navigate Maps Handler
    3: #
    4: # $Id: lonnavmaps.pm,v 1.509.2.14.2.8 2023/05/27 12:40:25 raeburn Exp $
    5: 
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::lonnavmaps - Subroutines to handle and render the navigation
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles navigational maps.
   40: 
   41: The main handler generates the navigational listing for the course,
   42: the other objects export this information in a usable fashion for
   43: other modules.
   44: 
   45: 
   46: This is part of the LearningOnline Network with CAPA project
   47: described at http://www.lon-capa.org.
   48: 
   49: 
   50: =head1 OVERVIEW
   51: 
   52: X<lonnavmaps, overview> When a user enters a course, LON-CAPA examines the
   53: course structure and caches it in what is often referred to as the
   54: "big hash" X<big hash>. You can see it if you are logged into
   55: LON-CAPA, in a course, by going to /adm/test. (You may need to
   56: tweak the /home/httpd/lonTabs/htpasswd file to view it.) The
   57: content of the hash will be under the heading "Big Hash".
   58: 
   59: Big Hash contains, among other things, how resources are related
   60: to each other (next/previous), what resources are maps, which 
   61: resources are being chosen to not show to the student (for random
   62: selection), and a lot of other things that can take a lot of time
   63: to compute due to the amount of data that needs to be collected and
   64: processed.
   65: 
   66: Apache::lonnavmaps provides an object model for manipulating this
   67: information in a higher-level fashion than directly manipulating 
   68: the hash. It also provides access to several auxilary functions 
   69: that aren't necessarily stored in the Big Hash, but are a per-
   70: resource sort of value, like whether there is any feedback on 
   71: a given resource.
   72: 
   73: Apache::lonnavmaps also abstracts away branching, and someday, 
   74: conditions, for the times where you don't really care about those
   75: things.
   76: 
   77: Apache::lonnavmaps also provides fairly powerful routines for
   78: rendering navmaps, and last but not least, provides the navmaps
   79: view for when the user clicks the NAV button.
   80: 
   81: B<Note>: Apache::lonnavmaps I<only> works for the "currently
   82: logged in user"; if you want things like "due dates for another
   83: student" lonnavmaps can not directly retrieve information like
   84: that. You need the EXT function. This module can still help,
   85: because many things, such as the course structure, are constant
   86: between users, and Apache::lonnavmaps can help by providing
   87: symbs for the EXT call.
   88: 
   89: The rest of this file will cover the provided rendering routines, 
   90: which can often be used without fiddling with the navmap object at
   91: all, then documents the Apache::lonnavmaps::navmap object, which
   92: is the key to accessing the Big Hash information, covers the use
   93: of the Iterator (which provides the logic for traversing the 
   94: somewhat-complicated Big Hash data structure), documents the
   95: Apache::lonnavmaps::Resource objects that are returned by 
   96: 
   97: =head1 Subroutine: render
   98: 
   99: The navmap renderer package provides a sophisticated rendering of the
  100: standard navigation maps interface into HTML. The provided nav map
  101: handler is actually just a glorified call to this.
  102: 
  103: Because of the large number of parameters this function accepts,
  104: instead of passing it arguments as is normal, pass it in an anonymous
  105: hash with the desired options.
  106: 
  107: The package provides a function called 'render', called as
  108: Apache::lonnavmaps::render({}).
  109: 
  110: =head2 Overview of Columns
  111: 
  112: The renderer will build an HTML table for the navmap and return
  113: it. The table consists of several columns, and a row for each
  114: resource (or possibly each part). You tell the renderer how many
  115: columns to create and what to place in each column, optionally using
  116: one or more of the prepared columns, and the renderer will assemble
  117: the table.
  118: 
  119: Any additional generally useful column types should be placed in the
  120: renderer code here, so anybody can use it anywhere else. Any code
  121: specific to the current application (such as the addition of <input>
  122: elements in a column) should be placed in the code of the thing using
  123: the renderer.
  124: 
  125: At the core of the renderer is the array reference COLS (see Example
  126: section below for how to pass this correctly). The COLS array will
  127: consist of entries of one of two types of things: Either an integer
  128: representing one of the pre-packaged column types, or a sub reference
  129: that takes a resource reference, a part number, and a reference to the
  130: argument hash passed to the renderer, and returns a string that will
  131: be inserted into the HTML representation as it.
  132: 
  133: All other parameters are ways of either changing how the columns
  134: are printing, or which rows are shown.
  135: 
  136: The pre-packaged column names are refered to by constants in the
  137: Apache::lonnavmaps namespace. The following currently exist:
  138: 
  139: =over 4
  140: 
  141: =item * B<Apache::lonnavmaps::resource>:
  142: 
  143: The general info about the resource: Link, icon for the type, etc. The
  144: first column in the standard nav map display. This column provides the
  145: indentation effect seen in the B<NAV> screen. This column also accepts
  146: the following parameters in the renderer hash:
  147: 
  148: =over 4
  149: 
  150: =item * B<resource_nolink>: default false
  151: 
  152: If true, the resource will not be linked. By default, all non-folder
  153: resources are linked.
  154: 
  155: =item * B<resource_part_count>: default true
  156: 
  157: If true, the resource will show a part count B<if> the full
  158: part list is not displayed. (See "condense_parts" later.) If false,
  159: the resource will never show a part count.
  160: 
  161: =item * B<resource_no_folder_link>:
  162: 
  163: If true, the resource's folder will not be clickable to open or close
  164: it. Default is false. True implies printCloseAll is false, since you
  165: can't close or open folders when this is on anyhow.
  166: 
  167: =item * B<map_no_edit_link>:
  168: 
  169: If true, the title of the folder or page will not be followed by an
  170: icon/link to direct editing of a folder or composite page, originally
  171: added via the Course Editor.
  172: 
  173: =back
  174: 
  175: =item * B<Apache::lonnavmaps::communication_status>:
  176: 
  177: Whether there is discussion on the resource, email for the user, or
  178: (lumped in here) perl errors in the execution of the problem. This is
  179: the second column in the main nav map.
  180: 
  181: =item * B<Apache::lonnavmaps::quick_status>:
  182: 
  183: An icon for the status of a problem, with five possible states:
  184: Correct, incorrect, open, awaiting grading (for a problem where the
  185: computer's grade is suppressed, or the computer can't grade, like
  186: essay problem), or none (not open yet, not a problem). The
  187: third column of the standard navmap.
  188: 
  189: =item * B<Apache::lonnavmaps::long_status>:
  190: 
  191: A text readout of the details of the current status of the problem,
  192: such as "Due in 22 hours". The fourth column of the standard navmap.
  193: 
  194: =item * B<Apache::lonnavmaps::part_status_summary>:
  195: 
  196: A text readout summarizing the status of the problem. If it is a
  197: single part problem, will display "Correct", "Incorrect", 
  198: "Not yet open", "Open", "Attempted", or "Error". If there are
  199: multiple parts, this will output a string that in HTML will show a
  200: status of how many parts are in each status, in color coding, trying
  201: to match the colors of the icons within reason.
  202: 
  203: Note this only makes sense if you are I<not> showing parts. If 
  204: C<showParts> is true (see below), this column will not output
  205: anything. 
  206: 
  207: =back
  208: 
  209: If you add any others please be sure to document them here.
  210: 
  211: An example of a column renderer that will show the ID number of a
  212: resource, along with the part name if any:
  213: 
  214:  sub { 
  215:   my ($resource, $part, $params) = @_;   
  216:   if ($part) { return '<td>' . $resource->{ID} . ' ' . $part . '</td>'; }
  217:   return '<td>' . $resource->{ID} . '</td>';
  218:  }
  219: 
  220: Note these functions are responsible for the TD tags, which allow them
  221: to override vertical and horizontal alignment, etc.
  222: 
  223: =head2 Parameters
  224: 
  225: Minimally, you should be
  226: able to get away with just using 'cols' (to specify the columns
  227: shown), 'url' (necessary for the folders to link to the current screen
  228: correctly), and possibly 'queryString' if your app calls for it. In
  229: that case, maintaining the state of the folders will be done
  230: automatically.
  231: 
  232: =over 4
  233: 
  234: =item * B<iterator>: default: constructs one from %env
  235: 
  236: A reference to a fresh ::iterator to use from the navmaps. The
  237: rendering will reflect the options passed to the iterator, so you can
  238: use that to just render a certain part of the course, if you like. If
  239: one is not passed, the renderer will attempt to construct one from
  240: env{'form.filter'} and env{'form.condition'} information, plus the
  241: 'iterator_map' parameter if any.
  242: 
  243: =item * B<iterator_map>: default: not used
  244: 
  245: If you are letting the renderer do the iterator handling, you can
  246: instruct the renderer to render only a particular map by passing it
  247: the source of the map you want to process, like
  248: '/res/103/jerf/navmap.course.sequence'.
  249: 
  250: =item * B<include_top_level_map>: default: false
  251: 
  252: If you need to include the top level map (meaning the course) in the
  253: rendered output set this to true
  254: 
  255: =item * B<navmap>: default: constructs one from %env
  256: 
  257: A reference to a navmap, used only if an iterator is not passed in. If
  258: this is necessary to make an iterator but it is not passed in, a new
  259: one will be constructed based on env info. This is useful to do basic
  260: error checking before passing it off to render.
  261: 
  262: =item * B<r>: default: must be passed in
  263: 
  264: The standard Apache response object. This must be passed to the
  265: renderer or the course hash will be locked.
  266: 
  267: =item * B<cols>: default: empty (useless)
  268: 
  269: An array reference
  270: 
  271: =item * B<showParts>:default true
  272: 
  273: A flag. If true, a line for the resource itself, and a line
  274: for each part will be displayed. If not, only one line for each
  275: resource will be displayed.
  276: 
  277: =item * B<condenseParts>: default true
  278: 
  279: A flag. If true, if all parts of the problem have the same
  280: status and that status is Nothing Set, Correct, or Network Failure,
  281: then only one line will be displayed for that resource anyhow. If no,
  282: all parts will always be displayed. If showParts is 0, this is
  283: ignored.
  284: 
  285: =item * B<jumpCount>: default: determined from %env
  286: 
  287: A string identifying the URL to place the anchor 'curloc' at.
  288: It is the responsibility of the renderer user to
  289: ensure that the #curloc is in the URL. By default, determined through
  290: the use of the env{} 'jump' information, and should normally "just
  291: work" correctly.
  292: 
  293: =item * B<here>: default: empty string
  294: 
  295: A Symb identifying where to place the 'here' marker. The empty
  296: string means no marker.
  297: 
  298: =item * B<indentString>: default: 25 pixel whitespace image
  299: 
  300: A string identifying the indentation string to use. 
  301: 
  302: =item * B<queryString>: default: empty
  303: 
  304: A string which will be prepended to the query string used when the
  305: folders are opened or closed. You can use this to pass
  306: application-specific values.
  307: 
  308: =item * B<url>: default: none
  309: 
  310: The url the folders will link to, which should be the current
  311: page. Required if the resource info column is shown, and you 
  312: are allowing the user to open and close folders.
  313: 
  314: =item * B<currentJumpIndex>: default: no jumping
  315: 
  316: Describes the currently-open row number to cause the browser to jump
  317: to, because the user just opened that folder. By default, pulled from
  318: the Jump information in the env{'form.*'}.
  319: 
  320: =item * B<printKey>: default: false
  321: 
  322: If true, print the key that appears on the top of the standard
  323: navmaps.
  324: 
  325: =item * B<printCloseAll>: default: true
  326: 
  327: If true, print the "Close all folders" or "open all folders"
  328: links.
  329: 
  330: =item * B<filterFunc>: default: sub {return 1;} (accept everything)
  331: 
  332: A function that takes the resource object as its only parameter and
  333: returns a true or false value. If true, the resource is displayed. If
  334: false, it is simply skipped in the display.
  335: 
  336: =item * B<suppressEmptySequences>: default: false
  337: 
  338: If you're using a filter function, and displaying sequences to orient
  339: the user, then frequently some sequences will be empty. Setting this to
  340: true will cause those sequences not to display, so as not to confuse the
  341: user into thinking that if the sequence is there there should be things
  342: under it; for example, see the "Show Uncompleted Homework" view on the
  343: B<NAV> screen.
  344: 
  345: =item * B<suppressNavmap>: default: false
  346: 
  347: If true, will not display Navigate Content resources. 
  348: 
  349: =back
  350: 
  351: =head2 Additional Info
  352: 
  353: In addition to the parameters you can pass to the renderer, which will
  354: be passed through unchange to the column renderers, the renderer will
  355: generate the following information which your renderer may find
  356: useful:
  357: 
  358: =over 4
  359: 
  360: =item * B<counter>: 
  361: 
  362: Contains the number of rows printed. Useful after calling the render 
  363: function, as you can detect whether anything was printed at all.
  364: 
  365: =item * B<isNewBranch>:
  366: 
  367: Useful for renderers: If this resource is currently the first resource
  368: of a new branch, this will be true. The Resource column (leftmost in the
  369: navmaps screen) uses this to display the "new branch" icon 
  370: 
  371: =back
  372: 
  373: =cut
  374: 
  375: 
  376: =head1 SUBROUTINES
  377: 
  378: =over
  379: 
  380: =item update()
  381: 
  382: =item addToFilter()
  383: 
  384: Convenience functions: Returns a string that adds or subtracts
  385: the second argument from the first hash, appropriate for the 
  386: query string that determines which folders to recurse on
  387: 
  388: =item removeFromFilter()
  389: 
  390: =item getLinkForResource()
  391: 
  392: Convenience function: Given a stack returned from getStack on the iterator,
  393: return the correct src() value.
  394: 
  395: =item getDescription()
  396: 
  397: Convenience function: This separates the logic of how to create
  398: the problem text strings ("Due: DATE", "Open: DATE", "Not yet assigned",
  399: etc.) into a separate function. It takes a resource object as the
  400: first parameter, and the part number of the resource as the second.
  401: It's basically a big switch statement on the status of the resource.
  402: 
  403: =item dueInLessThan24Hours()
  404: 
  405: Convenience function, so others can use it: Is the problem due in less than 24 hours, and still can be done?
  406: 
  407: =item lastTry()
  408: 
  409: Convenience function, so others can use it: Is there only one try remaining for the
  410: part, with more than one try to begin with, not due yet and still can be done?
  411: 
  412: =item advancedUser()
  413: 
  414: This puts a human-readable name on the env variable.
  415: 
  416: =item timeToHumanString()
  417: 
  418: timeToHumanString takes a time number and converts it to a
  419: human-readable representation, meant to be used in the following
  420: manner:
  421: 
  422: =over 4
  423: 
  424: =item * print "Due $timestring"
  425: 
  426: =item * print "Open $timestring"
  427: 
  428: =item * print "Answer available $timestring"
  429: 
  430: =back
  431: 
  432: Very, very, very, VERY English-only... goodness help a localizer on
  433: this func...
  434: 
  435: =item resource()
  436: 
  437: returns 0
  438: 
  439: =item communication_status()
  440: 
  441: returns 1
  442: 
  443: =item quick_status()
  444: 
  445: returns 2
  446: 
  447: =item long_status()
  448: 
  449: returns 3
  450: 
  451: =item part_status_summary()
  452: 
  453: returns 4
  454: 
  455: =item render_resource()
  456: 
  457: =item render_communication_status()
  458: 
  459: =item render_quick_status()
  460: 
  461: =item render_long_status()
  462: 
  463: =item render_parts_summary_status()
  464: 
  465: =item setDefault()
  466: 
  467: =item cmp_title()
  468: 
  469: =item render()
  470: 
  471: =item add_linkitem()
  472: 
  473: =item show_linkitems_toolbar()
  474: 
  475: =back
  476: 
  477: =cut
  478: 
  479: package Apache::lonnavmaps;
  480: 
  481: use strict;
  482: use GDBM_File;
  483: use Apache::loncommon();
  484: use Apache::lonenc();
  485: use Apache::lonlocal;
  486: use Apache::lonnet;
  487: use Apache::lonmap;
  488: 
  489: use POSIX qw (ceil floor strftime);
  490: use Time::HiRes qw( gettimeofday tv_interval );
  491: use LONCAPA;
  492: use DateTime();
  493: use HTML::Entities;
  494: 
  495: # For debugging
  496: 
  497: #use Data::Dumper;
  498: 
  499: 
  500: # symbolic constants
  501: sub SYMB { return 1; }
  502: sub URL { return 2; }
  503: sub NOTHING { return 3; }
  504: 
  505: # Some data
  506: 
  507: my $resObj = "Apache::lonnavmaps::resource";
  508: 
  509: # Keep these mappings in sync with lonquickgrades, which usesthe colors
  510: # instead of the icons.
  511: my %statusIconMap = 
  512:     (
  513:      $resObj->CLOSED       => '',
  514:      $resObj->OPEN         => 'navmap.open.gif',
  515:      $resObj->CORRECT      => 'navmap.correct.gif',
  516:      $resObj->PARTIALLY_CORRECT      => 'navmap.partial.gif',
  517:      $resObj->INCORRECT    => 'navmap.wrong.gif',
  518:      $resObj->ATTEMPTED    => 'navmap.ellipsis.gif',
  519:      $resObj->ERROR        => ''
  520:      );
  521: 
  522: my %iconAltTags =   #texthash does not work here
  523:     ( 'navmap.correct.gif'  => 'Correct',
  524:       'navmap.wrong.gif'    => 'Incorrect',
  525:       'navmap.open.gif'     => 'Is Open',
  526:       'navmap.partial.gif'  => 'Partially Correct',
  527:       'navmap.ellipsis.gif' => 'Attempted',
  528:      );
  529: 
  530: # Defines a status->color mapping, null string means don't color
  531: my %colormap = 
  532:     ( $resObj->NETWORK_FAILURE        => '',
  533:       $resObj->CORRECT                => '',
  534:       $resObj->EXCUSED                => '#3333FF',
  535:       $resObj->PAST_DUE_ANSWER_LATER  => '',
  536:       $resObj->PAST_DUE_NO_ANSWER     => '',
  537:       $resObj->PAST_DUE_ATMPT_ANS     => '',
  538:       $resObj->PAST_DUE_ATMPT_NOANS   => '',
  539:       $resObj->PAST_DUE_NO_ATMT_ANS   => '',
  540:       $resObj->PAST_DUE_NO_ATMT_NOANS => '',
  541:       $resObj->ANSWER_OPEN            => '#006600',
  542:       $resObj->OPEN_LATER             => '',
  543:       $resObj->TRIES_LEFT             => '',
  544:       $resObj->INCORRECT              => '',
  545:       $resObj->OPEN                   => '',
  546:       $resObj->NOTHING_SET            => '',
  547:       $resObj->ATTEMPTED              => '',
  548:       $resObj->CREDIT_ATTEMPTED       => '',
  549:       $resObj->ANSWER_SUBMITTED       => '',
  550:       $resObj->PARTIALLY_CORRECT      => '#006600'
  551:       );
  552: # And a special case in the nav map; what to do when the assignment
  553: # is not yet done and due in less than 24 hours
  554: my $hurryUpColor = "#FF0000";
  555: 
  556: sub addToFilter {
  557:     my $hashIn = shift;
  558:     my $addition = shift;
  559:     my %hash = %$hashIn;
  560:     $hash{$addition} = 1;
  561: 
  562:     return join (",", keys(%hash));
  563: }
  564: 
  565: sub removeFromFilter {
  566:     my $hashIn = shift;
  567:     my $subtraction = shift;
  568:     my %hash = %$hashIn;
  569: 
  570:     delete $hash{$subtraction};
  571:     return join(",", keys(%hash));
  572: }
  573: 
  574: sub getLinkForResource {
  575:     my $stack = shift;
  576:     my $res;
  577: 
  578:     # Check to see if there are any pages in the stack
  579:     foreach $res (@$stack) {
  580:         if (defined($res)) {
  581: 	    my $anchor;
  582: 	    if ($res->is_page()) {
  583: 		foreach my $item (@$stack) { if (defined($item)) { $anchor = $item; }  }
  584: 		if ($anchor->encrypted() && !&advancedUser()) {
  585: 		    $anchor='LC_'.$anchor->id();
  586: 		} else {
  587: 		    $anchor=&escape($anchor->shown_symb());
  588: 		}
  589: 		return ($res->link(),$res->shown_symb(),$anchor);
  590: 	    }
  591:             # in case folder was skipped over as "only sequence"
  592: 	    my ($map,$id,$src)=&Apache::lonnet::decode_symb($res->symb());
  593: 	    if ($map=~/\.page$/) {
  594: 		my $url=&Apache::lonnet::clutter($map);
  595: 		$anchor=&escape($res->shown_symb());
  596: 		return ($url,$res->shown_symb(),$anchor);
  597: 	    }
  598:         }
  599:     }
  600: 
  601:     # Failing that, return the src of the last resource that is defined
  602:     # (when we first recurse on a map, it puts an undefined resource
  603:     # on the bottom because $self->{HERE} isn't defined yet, and we
  604:     # want the src for the map anyhow)
  605:     foreach my $item (@$stack) {
  606:         if (defined($item)) { $res = $item; }
  607:     }
  608: 
  609:     if ($res) {
  610: 	return ($res->link(),$res->shown_symb());
  611:     }
  612:     return;
  613: }
  614: 
  615: 
  616: 
  617: sub getDescription {
  618:     my $res = shift;
  619:     my $part = shift;
  620:     my $status = $res->status($part);
  621: 
  622:     my $open = $res->opendate($part);
  623:     my $due = $res->duedate($part);
  624:     my $answer = $res->answerdate($part);
  625: 
  626:     if ($status == $res->NETWORK_FAILURE) { 
  627:         return &mt("Having technical difficulties; please check status later"); 
  628:     }
  629:     if ($status == $res->NOTHING_SET) {
  630:         return &Apache::lonhtmlcommon::direct_parm_link(&mt('Not currently assigned'),$res->symb(),'opendate',$part);
  631:     }
  632:     if ($status == $res->OPEN_LATER) {
  633:         return &mt("Open [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($open,'start'),$res->symb(),'opendate',$part));
  634:     }
  635:     my $slotinfo;
  636:     if ($res->simpleStatus($part) == $res->OPEN) {
  637:         unless (&Apache::lonnet::allowed('mgr',$env{'request.course.id'})) {
  638:             my ($slot_status,$slot_time,$slot_name)=$res->check_for_slot($part);
  639:             my $slotmsg;
  640:             if ($slot_status == $res->UNKNOWN) {
  641:                 $slotmsg = &mt('Reservation status unknown');
  642:             } elsif ($slot_status == $res->RESERVED) {
  643:                 $slotmsg = &mt('Reserved - ends [_1]',
  644:                            timeToHumanString($slot_time,'end'));
  645:             } elsif ($slot_status == $res->RESERVED_LOCATION) {
  646:                 $slotmsg = &mt('Reserved - specific location(s) - ends [_1]',
  647:                            timeToHumanString($slot_time,'end'));
  648:             } elsif ($slot_status == $res->RESERVED_LATER) {
  649:                 $slotmsg = &mt('Reserved - next open [_1]',
  650:                            timeToHumanString($slot_time,'start'));
  651:             } elsif ($slot_status == $res->RESERVABLE) {
  652:                 $slotmsg = &mt('Reservable, reservations close [_1]',
  653:                            timeToHumanString($slot_time,'end'));
  654:             } elsif ($slot_status == $res->NEEDS_CHECKIN) {
  655:                 $slotmsg = &mt('Reserved, check-in needed - ends [_1]',
  656:                            timeToHumanString($slot_time,'end'));
  657:             } elsif ($slot_status == $res->RESERVABLE_LATER) {
  658:                 $slotmsg = &mt('Reservable, reservations open [_1]',
  659:                            timeToHumanString($slot_time,'start'));
  660:             } elsif ($slot_status == $res->NOT_IN_A_SLOT) {
  661:                 $slotmsg = &mt('Reserve a time/place to work');
  662:             } elsif ($slot_status == $res->NOTRESERVABLE) {
  663:                 $slotmsg = &mt('Reservation not available');
  664:             } elsif ($slot_status == $res->WAITING_FOR_GRADE) {
  665:                 $slotmsg = &mt('Submission in grading queue');
  666:             }
  667:             if ($slotmsg) {
  668:                 if ($res->is_task() || !$due) {
  669:                      return $slotmsg;
  670:                 }
  671:                 $slotinfo = ('&nbsp;' x 2).'('.$slotmsg.')';
  672:             }
  673:         }
  674:     }
  675:     if ($status == $res->OPEN) {
  676:         if ($due) {
  677: 	    if ($res->is_practice()) {
  678: 		return &mt("Closes [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($due,'start'),$res->symb(),'duedate',$part)).$slotinfo;
  679: 	    } else {
  680: 		return &mt("Due [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($due,'end'),$res->symb(),'duedate',$part)).$slotinfo;
  681: 	    }
  682:         } else {
  683:             return &Apache::lonhtmlcommon::direct_parm_link(&mt("Open, no due date"),$res->symb(),'duedate',$part).$slotinfo;
  684:         }
  685:     }
  686:     if (($status == $res->PAST_DUE_ANSWER_LATER) || ($status == $res->PAST_DUE_ATMPT_ANS) || ($status == $res->PAST_DUE_NO_ATMT_ANS)) {
  687:         return &mt("Answer open [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($answer,'start'),$res->symb(),'answerdate',$part));
  688:     }
  689:     if (($status == $res->PAST_DUE_NO_ANSWER) || ($status == $res->PAST_DUE_ATMPT_NOANS) || ($status == $res->PAST_DUE_NO_ATMT_NOANS)) {
  690: 	if ($res->is_practice()) {
  691: 	    return &mt("Closed [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($due,'start'),$res->symb(),'answerdate,duedate',$part));
  692: 	} else {
  693: 	    return &mt("Was due [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($due,'end'),$res->symb(),'answerdate,duedate',$part));
  694: 	}
  695:     }
  696:     if (($status == $res->ANSWER_OPEN || $status == $res->PARTIALLY_CORRECT)
  697: 	&& $res->handgrade($part) ne 'yes') {
  698:         return &Apache::lonhtmlcommon::direct_parm_link(&mt("Answer available"),$res->symb(),'answerdate,duedate',$part);
  699:     }
  700:     if ($status == $res->EXCUSED) {
  701:         return &mt("Excused by instructor");
  702:     }
  703:     if ($status == $res->ATTEMPTED) {
  704:         if ($res->is_anonsurvey($part) || $res->is_survey($part)) {
  705:             return &mt("Survey submission recorded");
  706:         } else {
  707:             return &mt("Answer submitted, not yet graded");
  708:         }
  709:     }
  710:     if ($status == $res->CREDIT_ATTEMPTED) {
  711:         if ($res->is_anonsurvey($part) || $res->is_survey($part)) {
  712:             return &mt("Credit for survey submission");
  713:         }
  714:     }
  715:     if ($status == $res->TRIES_LEFT) {
  716:         my $tries = $res->tries($part);
  717:         my $maxtries = $res->maxtries($part);
  718:         my $triesString = "";
  719:         if ($tries && $maxtries) {
  720:             $triesString = '<span class="LC_fontsize_medium"><i>('.&mt('[_1] of [quant,_2,try,tries] used',$tries,$maxtries).')</i></span>';
  721:             if ($maxtries > 1 && $maxtries - $tries == 1) {
  722:                 $triesString = "<b>$triesString</b>";
  723:             }
  724:         }
  725:         if ($due) {
  726:             return &mt("Due [_1]",&Apache::lonhtmlcommon::direct_parm_link(&timeToHumanString($due,'end'),$res->symb(),'duedate',$part)) .
  727:                 " $triesString";
  728:         } else {
  729:             return &Apache::lonhtmlcommon::direct_parm_link(&mt("No due date"),$res->symb(),'duedate',$part)." $triesString";
  730:         }
  731:     }
  732:     if ($status == $res->ANSWER_SUBMITTED) {
  733:         return &mt('Answer submitted');
  734:     }
  735: }
  736: 
  737: 
  738: sub dueInLessThan24Hours {
  739:     my $res = shift;
  740:     my $part = shift;
  741:     my $status = $res->status($part);
  742: 
  743:     return ($status == $res->OPEN() ||
  744:             $status == $res->TRIES_LEFT()) &&
  745: 	    $res->duedate($part) && $res->duedate($part) < time()+(24*60*60) &&
  746: 	    $res->duedate($part) > time();
  747: }
  748: 
  749: 
  750: sub lastTry {
  751:     my $res = shift;
  752:     my $part = shift;
  753: 
  754:     my $tries = $res->tries($part);
  755:     my $maxtries = $res->maxtries($part);
  756:     return $tries && $maxtries && $maxtries > 1 &&
  757:         $maxtries - $tries == 1 && $res->duedate($part) &&
  758:         $res->duedate($part) > time();
  759: }
  760: 
  761: 
  762: sub advancedUser {
  763:     return $env{'request.role.adv'};
  764: }
  765: 
  766: sub timeToHumanString {
  767:     my ($time,$type,$format) = @_;
  768: 
  769:     # zero, '0' and blank are bad times
  770:     if (!$time) {
  771:         return &mt('never');
  772:     }
  773:     unless (&Apache::lonlocal::current_language()=~/^en/) {
  774: 	return &Apache::lonlocal::locallocaltime($time);
  775:     } 
  776:     my $now = time();
  777: 
  778:     # Positive = future
  779:     my $delta = $time - $now;
  780: 
  781:     my $minute = 60;
  782:     my $hour = 60 * $minute;
  783:     my $day = 24 * $hour;
  784:     my $week = 7 * $day;
  785:     my $inPast = 0;
  786: 
  787:     # Logic in comments:
  788:     # Is it now? (extremely unlikely)
  789:     if ( $delta == 0 ) {
  790:         return "this instant";
  791:     }
  792: 
  793:     if ($delta < 0) {
  794:         $inPast = 1;
  795:         $delta = -$delta;
  796:     }
  797: 
  798:     if ( $delta > 0 ) {
  799: 
  800:         my $tense = $inPast ? " ago" : "";
  801:         my $prefix = $inPast ? "" : "in ";
  802:         
  803:         # Less than a minute
  804:         if ( $delta < $minute ) {
  805:             if ($delta == 1) { return "${prefix}1 second$tense"; }
  806:             return "$prefix$delta seconds$tense";
  807:         }
  808: 
  809:         # Less than an hour
  810:         if ( $delta < $hour ) {
  811:             # If so, use minutes; or minutes, seconds (if format requires)
  812:             my $minutes = floor($delta / 60);
  813:             if (($format ne '') && ($format =~ /\%(T|S)/)) {
  814:                 my $display;
  815:                 if ($minutes == 1) {
  816:                     $display = "${prefix}1 minute";
  817:                 } else {
  818:                     $display = "$prefix$minutes minutes";
  819:                 }
  820:                 my $seconds = $delta % $minute;
  821:                 if ($seconds == 0) {
  822:                     $display .= $tense;
  823:                 } elsif ($seconds == 1) {
  824:                     $display .= ", 1 second$tense";
  825:                 } else {
  826:                     $display .= ", $seconds seconds$tense";
  827:                 }
  828:                 return $display;
  829:             }
  830:             if ($minutes == 1) { return "${prefix}1 minute$tense"; }
  831:             return "$prefix$minutes minutes$tense";
  832:         }
  833:         
  834:         # Is it less than 24 hours away? If so,
  835:         # display hours + minutes, (and + seconds, if format specified it)  
  836:         if ( $delta < $hour * 24) {
  837:             my $hours = floor($delta / $hour);
  838:             my $minutes = floor(($delta % $hour) / $minute);
  839:             my $hourString = "$hours hours";
  840:             my $minuteString = ", $minutes minutes";
  841:             if ($hours == 1) {
  842:                 $hourString = "1 hour";
  843:             }
  844:             if ($minutes == 1) {
  845:                 $minuteString = ", 1 minute";
  846:             }
  847:             if ($minutes == 0) {
  848:                 $minuteString = "";
  849:             }
  850:             if (($format ne '') && ($format =~ /\%(T|S)/)) {
  851:                 my $display = "$prefix$hourString$minuteString";
  852:                 my $seconds = $delta-(($hours * $hour)+($minutes * $minute));
  853:                 if ($seconds == 0) {
  854:                     $display .= $tense;
  855:                 } elsif ($seconds == 1) {
  856:                     $display .= ", 1 second$tense";
  857:                 } else {
  858:                     $display .= ", $seconds seconds$tense";
  859:                 }
  860:                 return $display;
  861:             }
  862:             return "$prefix$hourString$minuteString$tense";
  863:         }
  864: 
  865:         # Date/time is more than 24 hours away
  866: 
  867: 	my $dt = DateTime->from_epoch(epoch => $time)
  868: 	                 ->set_time_zone(&Apache::lonlocal::gettimezone());
  869: 
  870: 	# If there's a caller supplied format, use it, unless it only displays
  871:         # H:M:S or H:M.
  872: 
  873: 	if (($format ne '') && ($format ne '%T') && ($format ne '%R')) {
  874: 	    my $timeStr = $dt->strftime($format);
  875: 	    return $timeStr.' ('.$dt->time_zone_short_name().')';
  876: 	}
  877: 
  878:         # Less than 5 days away, display day of the week and
  879:         # HH:MM
  880: 
  881:         if ( $delta < $day * 5 ) {
  882:             my $timeStr = $dt->strftime("%A, %b %e at %I:%M %P (%Z)");
  883:             $timeStr =~ s/12:00 am/00:00/;
  884:             $timeStr =~ s/12:00 pm/noon/;
  885:             return ($inPast ? "last " : "this ") .
  886:                 $timeStr;
  887:         }
  888:         
  889: 	my $conjunction='on';
  890: 	if ($type eq 'start') {
  891: 	    $conjunction='at';
  892: 	} elsif ($type eq 'end') {
  893: 	    $conjunction='by';
  894: 	}
  895:         # Is it this year?
  896: 	my $dt_now = DateTime->from_epoch(epoch => $now)
  897: 	                     ->set_time_zone(&Apache::lonlocal::gettimezone());
  898:         if ( $dt->year() == $dt_now->year()) {
  899:             # Return on Month Day, HH:MM meridian
  900:             my $timeStr = $dt->strftime("$conjunction %A, %b %e at %I:%M %P (%Z)");
  901:             $timeStr =~ s/12:00 am/00:00/;
  902:             $timeStr =~ s/12:00 pm/noon/;
  903:             return $timeStr;
  904:         }
  905: 
  906:         # Not this year, so show the year
  907:         my $timeStr = 
  908: 	    $dt->strftime("$conjunction %A, %b %e %Y at %I:%M %P (%Z)");
  909:         $timeStr =~ s/12:00 am/00:00/;
  910:         $timeStr =~ s/12:00 pm/noon/;
  911:         return $timeStr;
  912:     }
  913: }
  914: 
  915: 
  916: sub resource { return 0; }
  917: sub communication_status { return 1; }
  918: sub quick_status { return 2; }
  919: sub long_status { return 3; }
  920: sub part_status_summary { return 4; }
  921: 
  922: sub render_resource {
  923:     my ($resource, $part, $params) = @_;
  924: 
  925:     my $editmapLink;
  926:     my $nonLinkedText = ''; # stuff after resource title not in link
  927: 
  928:     my $link = $params->{"resourceLink"};
  929:     if ($resource->ext()) {
  930:         $link =~ s/\#.+(\?)/$1/g;
  931:     }
  932: 
  933:     #  The URL part is not escaped at this point, but the symb is... 
  934: 
  935:     my $src = $resource->src();
  936:     my $it = $params->{"iterator"};
  937:     my $filter = $it->{FILTER};
  938: 
  939:     my $title = $resource->compTitle();
  940: 
  941:     my $partLabel = "";
  942:     my $newBranchText = "";
  943:     my $location=&Apache::loncommon::lonhttpdurl("/adm/lonIcons");
  944:     # If this is a new branch, label it so
  945:     if ($params->{'isNewBranch'}) {
  946:         $newBranchText = "<img src='$location/branch.gif' alt=".mt('Branch')." />";
  947:     }
  948: 
  949:     # links to open and close the folder
  950: 
  951:     my $whitespace = $location.'/whitespace_21.gif';
  952:     my ($nomodal,$linkopen,$linkclose);
  953:     unless ($resource->is_map() || $params->{'resource_nolink'}) {
  954:         $linkopen = "<img src='$whitespace' alt='' />";
  955:         $linkclose = "</a>";
  956:         if (($params->{'modalLink'}) && (!$resource->is_sequence())) {
  957:             if ($link =~m{^(?:|/adm/wrapper)/ext/([^#]+)}) {
  958:                 my $exturl = $1;
  959:                 if (($ENV{'SERVER_PORT'} == 443) && ($exturl !~ /^https:/)) {
  960:                     $nomodal = 1;
  961:                 }
  962:             } elsif (($link eq "/public/$LONCAPA::match_domain/$LONCAPA::match_courseid/syllabus") &&
  963:                      ($env{'request.course.id'}) && ($ENV{'SERVER_PORT'} == 443) &&
  964:                      ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
  965:                 $nomodal = 1;
  966:             }
  967:             my $esclink = &js_escape($link);
  968:             if ($nomodal) {
  969:                 $linkopen .= "<a href=\"#\" onclick=\"javascript:window.open('$esclink','resourcepreview','height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1'); return false;\" />";
  970:             } else {
  971:                 $linkopen .= "<a href=\"$link\" onclick=\"javascript:openMyModal('$esclink',600,500,'yes','true'); return false;\">";
  972:             }
  973:         } else {
  974:             $linkopen .= "<a href=\"$link\">";
  975:         }
  976:     }
  977: 
  978:     # Default icon: unknown page
  979:     my $icon = "<img class=\"LC_contentImage\" src='$location/unknown.gif' alt='' />";
  980:     
  981:     if ($resource->is_problem()) {
  982:         if ($part eq '0' || $params->{'condensed'}) {
  983: 	    $icon = '<img class="LC_contentImage" src="'.$location.'/';
  984: 	    if ($resource->is_task()) {
  985: 		$icon .= 'task.gif" alt="'.&mt('Task');
  986: 	    } else {
  987: 		$icon .= 'problem.gif" alt="'.&mt('Problem');
  988: 	    }
  989: 	    $icon .='" />';
  990:         } else {
  991:             $icon = $params->{'indentString'};
  992:         }
  993:     } else {
  994: 	$icon = "<img class=\"LC_contentImage\" src='".&Apache::loncommon::icon($resource->src)."' alt='' />";
  995:     }
  996: 
  997:     # Display the correct map icon to open or shut map
  998:     if ($resource->is_map()) {
  999:         my $mapId = $resource->map_pc();
 1000:         my $nowOpen = !defined($filter->{$mapId});
 1001:         if ($it->{CONDITION}) {
 1002:             $nowOpen = !$nowOpen;
 1003:         }
 1004: 	my $folderType;
 1005: 	if (&advancedUser() && $resource->is_missing_map()) {
 1006: 	    $folderType = 'none';
 1007: 	} else {
 1008: 	    $folderType = $resource->is_sequence() ? 'folder' : 'page';
 1009: 	}
 1010:         my $title=$resource->title;
 1011: 		$title=~s/\"/\&qout;/g;
 1012:         if (!$params->{'resource_no_folder_link'}) {
 1013:             $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') . '.gif';
 1014:             $icon = "<img src='$location/arrow." . ($nowOpen ? 'closed' : 'open') . ".gif' alt='' />"
 1015:                     ."<img class=\"LC_contentImage\" src='$location/$icon' alt=\""
 1016:                     .($nowOpen ? &mt('Open Folder') : &mt('Close Folder')).' '.$title."\" />";			
 1017:             $linkopen = "<a href=\"" . $params->{'url'} . '?' . 
 1018:                 $params->{'queryString'} . '&amp;filter=';
 1019:             $linkopen .= ($nowOpen xor $it->{CONDITION}) ?
 1020:                 addToFilter($filter, $mapId) :
 1021:                 removeFromFilter($filter, $mapId);
 1022:             $linkopen .= "&amp;condition=" . $it->{CONDITION} . '&amp;hereType='
 1023:                 . $params->{'hereType'} . '&amp;here=' .
 1024:                 &escape($params->{'here'}) . 
 1025:                 '&amp;jump=' .
 1026:                 &escape($resource->symb()) . 
 1027:                 "&amp;folderManip=1\">";
 1028:             $linkclose = '</a>';
 1029:         } else {
 1030:             # Don't allow users to manipulate folder
 1031:             $icon = "navmap.$folderType." . ($nowOpen ? 'closed' : 'open') . '.gif';
 1032:             $icon = "<img class=\"LC_space\" src='$whitespace' alt='' />"."<img class=\"LC_contentImage\" src='$location/$icon' alt=\"".($nowOpen ? &mt('Open Folder') : &mt('Close Folder')).' '.$title."\" />";
 1033:             if ($params->{'caller'} eq 'sequence') {
 1034:                 $linkopen = "<a href=\"$link\">";
 1035:                 $linkclose = '</a>';
 1036:             } else {
 1037:                 $linkopen = "";
 1038:                 $linkclose = "";
 1039:             }
 1040:         }
 1041:         if (((&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) ||
 1042:              (&Apache::lonnet::allowed('cev',$env{'request.course.id'}))) &&
 1043:             ($resource->symb=~/\_\_\_[^\_]+\_\_\_uploaded/)) {
 1044:             if (!$params->{'map_no_edit_link'}) {
 1045:                 my $icon = &Apache::loncommon::lonhttpdurl('/res/adm/pages').'/editmap.png';
 1046:                 $editmapLink='&nbsp;'.
 1047:                          '<a href="/adm/coursedocs?command=directnav&amp;symb='.&escape($resource->symb()).'">'.
 1048:                          '<img src="'.$icon.'" alt="'.&mt('Edit Content').'" title="'.&mt('Edit Content').'" />'.
 1049:                          '</a>';
 1050:             }
 1051:         }
 1052:         if ($params->{'mapHidden'} || $resource->randomout()) {
 1053:             $nonLinkedText .= ' <span class="LC_warning">('.&mt('hidden').')</span> ';
 1054:         } elsif ($params->{'mapUnlisted'}) {
 1055:             $nonLinkedText .= ' <span class="LC_warning">('.&mt('unlisted').')</span> ';
 1056:         } elsif ($params->{'mapHiddenDeepLink'} || $resource->deeplinkout()) {
 1057:             $nonLinkedText .= ' <span class="LC_warning">('.&mt('not shown').')</span> ';
 1058:         }
 1059:     } else {
 1060:         if ($resource->randomout()) {
 1061:             $nonLinkedText .= ' <span class="LC_warning">('.&mt('hidden').')</span> ';
 1062:         } elsif ($resource->deeplinkout()) {
 1063:             $nonLinkedText .= ' <span class="LC_warning">('.&mt('not shown').')</span> ';
 1064:         } else {
 1065:             my $deeplink = $resource->deeplink($params->{caller});
 1066:             if ((($deeplink eq 'absent') || ($deeplink eq 'grades')) &&
 1067:                   &advancedUser()) {
 1068:                 $nonLinkedText .= ' <span class="LC_warning">('.&mt('unlisted').')</span> ';
 1069:             } elsif (($deeplink) && ($deeplink) ne 'full') {
 1070:                 if (&advancedUser()) {
 1071:                     $nonLinkedText .= ' <span class="LC_warning">('.&mt('deep-link access').
 1072:                                       ')</span> ';
 1073:                 } else {
 1074:                     $nonLinkedText .= ' <span class="LC_warning">('.&mt('access via external site').
 1075:                                       ')</span> ';
 1076:                 }
 1077:             }
 1078:         }
 1079:     }
 1080:     if (!$resource->condval()) {
 1081:         $nonLinkedText .= ' <span class="LC_info">('.&mt('conditionally hidden').')</span> ';
 1082:     }
 1083:     if (($resource->is_practice()) && ($resource->is_raw_problem())) {
 1084:         $nonLinkedText .=' <span class="LC_info"><b>'.&mt('not graded').'</b></span>';
 1085:     }
 1086: 
 1087:     # We're done preparing and finally ready to start the rendering
 1088:     my $result = '<td class="LC_middle">';
 1089:     my $newfolderType = $resource->is_sequence() ? 'folder' : 'page';
 1090: 	
 1091:     my $indentLevel = $params->{'indentLevel'};
 1092:     if ($newBranchText) { $indentLevel--; }
 1093: 
 1094:     # print indentation
 1095:     for (my $i = 0; $i < $indentLevel; $i++) {
 1096:         $result .= $params->{'indentString'};
 1097:     }
 1098: 
 1099:     # Decide what to display
 1100:     $result .= "$newBranchText$linkopen$icon$linkclose";
 1101:     
 1102:     my $curMarkerBegin = '';
 1103:     my $curMarkerEnd = '';
 1104: 
 1105:     # Is this the current resource?
 1106:     if (!$params->{'displayedHereMarker'} && 
 1107:         $resource->symb() eq $params->{'here'} ) {
 1108:         unless ($resource->is_map()) {
 1109:             $curMarkerBegin = '<span class="LC_current_nav_location">';
 1110:             $curMarkerEnd = '</span>';
 1111:         }
 1112: 	$params->{'displayedHereMarker'} = 1;
 1113:     }
 1114: 
 1115:     if ($resource->is_problem() && $part ne '0' && 
 1116:         !$params->{'condensed'}) {
 1117: 	my $displaypart=$resource->part_display($part);
 1118:         $partLabel = " (".&mt('Part: [_1]', $displaypart).")";
 1119: 	if ($link!~/\#/) { $link.='#'.&escape($part); }
 1120:         $title = "";
 1121:     }
 1122: 
 1123:     if ($params->{'condensed'} && $resource->countParts() > 1) {
 1124:         $nonLinkedText .= ' ('.&mt('[_1] parts', $resource->countParts()).')';
 1125:     }
 1126: 
 1127:     if (!$params->{'resource_nolink'} && !$resource->is_sequence() && !$resource->is_empty_sequence) {
 1128:         $linkclose = '</a>';
 1129:         if ($params->{'modalLink'}) {
 1130:             my $esclink = &js_escape($link);
 1131:             if ($nomodal) {
 1132:                 $linkopen = "<a href=\"#\" onclick=\"javascript:window.open('$esclink','resourcepreview','height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1'); return false;\" />";
 1133:             } else {
 1134:                 $linkopen = "<a href=\"$link\" onclick=\"javascript:openMyModal('$esclink',600,500,'yes','true'); return false;\">";
 1135:             }
 1136:         } else {
 1137:             $linkopen = "<a href=\"$link\">";
 1138:         }
 1139:     }
 1140:     $result .= "$curMarkerBegin$linkopen$title$partLabel$linkclose$curMarkerEnd$editmapLink$nonLinkedText</td>";
 1141: 
 1142:     return $result;
 1143: }
 1144: 
 1145: sub render_communication_status {
 1146:     my ($resource, $part, $params) = @_;
 1147:     my $discussionHTML = ""; my $feedbackHTML = ""; my $errorHTML = "";
 1148: 
 1149:     my $link = $params->{"resourceLink"};
 1150:     my $linkopen = "<a href=\"$link\">";
 1151:     my $linkclose = "</a>";
 1152:     my $location=&Apache::loncommon::lonhttpdurl("/adm/lonMisc");
 1153: 
 1154:     if ($resource->hasDiscussion()) {
 1155:         $discussionHTML = $linkopen .
 1156:             '<img alt="'.&mt('New Discussion').'" src="'.$location.'/chat.gif" title="'.&mt('New Discussion').'"/>' .
 1157:             $linkclose;
 1158:     }
 1159:     
 1160:     if ($resource->getFeedback()) {
 1161:         my $feedback = $resource->getFeedback();
 1162:         foreach my $msgid (split(/\,/, $feedback)) {
 1163:             if ($msgid) {
 1164:                 $feedbackHTML .= '&nbsp;<a href="/adm/email?display='
 1165:                     . &escape($msgid) . '">'
 1166:                     . '<img alt="'.&mt('New E-mail').'" src="'.$location.'/feedback.gif" title="'.&mt('New E-mail').'"/></a>';
 1167:             }
 1168:         }
 1169:     }
 1170:     
 1171:     if ($resource->getErrors()) {
 1172:         my $errors = $resource->getErrors();
 1173:         my $errorcount = 0;
 1174:         foreach my $msgid (split(/,/, $errors)) {
 1175:             last if ($errorcount>=10); # Only output 10 bombs maximum
 1176:             if ($msgid) {
 1177:                 $errorcount++;
 1178:                 $errorHTML .= '&nbsp;<a href="/adm/email?display='
 1179:                     . &escape($msgid) . '">'
 1180:                     . '<img alt="'.&mt('New Error').'" src="'.$location.'/bomb.gif" title="'.&mt('New Error').'"/></a>';
 1181:             }
 1182:         }
 1183:     }
 1184: 
 1185:     if ($params->{'multipart'} && $part != '0') {
 1186: 	$discussionHTML = $feedbackHTML = $errorHTML = '';
 1187:     }
 1188:     return "<td class=\"LC_middle\">$discussionHTML$feedbackHTML$errorHTML&nbsp;</td>";
 1189: 
 1190: }
 1191: sub render_quick_status {
 1192:     my ($resource, $part, $params) = @_;
 1193:     my $result = "";
 1194:     my $firstDisplayed = !$params->{'condensed'} && 
 1195:         $params->{'multipart'} && $part eq "0";
 1196: 
 1197:     my $link = $params->{"resourceLink"};
 1198:     my $linkopen = "<a href=\"$link\">";
 1199:     my $linkclose = "</a>";
 1200: 	
 1201: 	$result .= '<td class="LC_middle">';
 1202:     if ($resource->is_problem() &&
 1203:         !$firstDisplayed) {
 1204:         my $icon = $statusIconMap{$resource->simpleStatus($part)};
 1205:         my $alt = $iconAltTags{$icon};
 1206:         if ($icon) {
 1207: 	    my $location=
 1208: 		&Apache::loncommon::lonhttpdurl("/adm/lonIcons/$icon");
 1209: 		$result .= $linkopen.'<img src="'.$location.'" alt="'.&mt($alt).'" title="'.&mt($alt).'" />'.$linkclose;            
 1210:         } else {
 1211:             $result .= "&nbsp;";
 1212:         }
 1213:     } else { # not problem, no icon
 1214:         $result .= "&nbsp;";
 1215:     }
 1216: 	$result .= "</td>\n";
 1217:     return $result;
 1218: }
 1219: sub render_long_status {
 1220:     my ($resource, $part, $params) = @_;
 1221:     my $result = '<td class="LC_middle LC_right">';
 1222:     my $firstDisplayed = !$params->{'condensed'} && 
 1223:         $params->{'multipart'} && $part eq "0";
 1224:                 
 1225:     my $color;
 1226:     my $info = '';
 1227:     if ($resource->is_problem() || $resource->is_practice()) {
 1228:         $color = $colormap{$resource->status};
 1229: 
 1230:         if (dueInLessThan24Hours($resource, $part)) {
 1231:             $color = $hurryUpColor;
 1232:             $info = ' title="'.&mt('Due in less than 24 hours!').'"';
 1233:         } elsif (lastTry($resource, $part)) {
 1234:             unless (($resource->problemstatus($part) eq 'no') ||
 1235:                     ($resource->problemstatus($part) eq 'no_feedback_ever')) {
 1236:                 $color = $hurryUpColor;
 1237:                 $info = ' title="'.&mt('One try remaining!').'"';
 1238:             }
 1239:          }
 1240:     }
 1241:     
 1242:     if ($resource->kind() eq "res" &&
 1243:         $resource->is_raw_problem() &&
 1244:         !$firstDisplayed) {
 1245:         if ($color) {$result .= '<span style="color:'.$color.'"'.$info.'><b>'; }
 1246:         $result .= getDescription($resource, $part);
 1247:         if ($color) {$result .= "</b></span>"; }
 1248:     }
 1249:     if ($resource->is_map() && &advancedUser() && $resource->randompick()) {
 1250:         $result .= &mt('(randomly select [_1])', $resource->randompick());
 1251:     }
 1252:     if ($resource->is_map() && &advancedUser() && $resource->randomorder()) {
 1253:         $result .= &mt('(randomly ordered)');
 1254:     }
 1255: 
 1256:     # Debugging code
 1257:     #$result .= " " . $resource->awarded($part) . '/' . $resource->weight($part) .
 1258:     #	' - Part: ' . $part;
 1259: 
 1260:     $result .= "</td>\n";
 1261:     
 1262:     return $result;
 1263: }
 1264: 
 1265: # Colors obtained by taking the icons, matching the colors, and
 1266: # possibly reducing the Value (HSV) of the color, if it's too bright
 1267: # for text, generally by one third or so.
 1268: my %statusColors = 
 1269:     (
 1270:      $resObj->CLOSED => '#000000',
 1271:      $resObj->OPEN   => '#998b13',
 1272:      $resObj->CORRECT => '#26933f',
 1273:      $resObj->INCORRECT => '#c48207',
 1274:      $resObj->ATTEMPTED => '#a87510',
 1275:      $resObj->ERROR => '#000000'
 1276:      );
 1277: my %statusStrings = 
 1278:     (
 1279:      $resObj->CLOSED => 'Not yet open',
 1280:      $resObj->OPEN   => 'Open',
 1281:      $resObj->CORRECT => 'Correct',
 1282:      $resObj->INCORRECT => 'Incorrect',
 1283:      $resObj->ATTEMPTED => 'Attempted',
 1284:      $resObj->ERROR => 'Network Error'
 1285:      );
 1286: my @statuses = ($resObj->CORRECT, $resObj->ATTEMPTED, $resObj->INCORRECT, $resObj->OPEN, $resObj->CLOSED, $resObj->ERROR);
 1287: 
 1288: sub render_parts_summary_status {
 1289:     my ($resource, $part, $params) = @_;
 1290:     if (!$resource->is_problem() && !$resource->contains_problem) { return '<td></td>'; }
 1291:     if ($params->{showParts}) { 
 1292: 	return '<td></td>';
 1293:     }
 1294: 
 1295:     my $td = "<td align='right'>\n";
 1296:     my $endtd = "</td>\n";
 1297:     my @probs;
 1298: 
 1299:     if ($resource->contains_problem) {
 1300: 	@probs=$resource->retrieveResources($resource,sub { $_[0]->is_problem() },1,0);
 1301:     } else {
 1302: 	@probs=($resource);
 1303:     }
 1304:     my $return;
 1305:     my %overallstatus;
 1306:     my $totalParts;
 1307:     foreach my $resource (@probs) {
 1308: 	# If there is a single part, just show the simple status
 1309: 	if ($resource->singlepart()) {
 1310: 	    my $status = $resource->simpleStatus(${$resource->parts}[0]);
 1311: 	    $overallstatus{$status}++;
 1312: 	    $totalParts++;
 1313: 	    next;
 1314: 	}
 1315: 	# Now we can be sure the $part doesn't really matter.
 1316: 	my $statusCount = $resource->simpleStatusCount();
 1317: 	my @counts;
 1318: 	foreach my $status (@statuses) {
 1319: 	    # decouple display order from the simpleStatusCount order
 1320: 	    my $slot = Apache::lonnavmaps::resource::statusToSlot($status);
 1321: 	    if ($statusCount->[$slot]) {
 1322: 		$overallstatus{$status}+=$statusCount->[$slot];
 1323: 		$totalParts+=$statusCount->[$slot];
 1324: 	    }
 1325: 	}
 1326:     }
 1327:     $return.= $td . $totalParts . ' parts: ';
 1328:     foreach my $status (@statuses) {
 1329:         if ($overallstatus{$status}) {
 1330:             $return.='<span style="color:' . $statusColors{$status}
 1331:                    . '">' . $overallstatus{$status} . ' '
 1332:                    . $statusStrings{$status} . '</span>';
 1333:         }
 1334:     }
 1335:     $return.= $endtd;
 1336:     return $return;
 1337: }
 1338: 
 1339: my @preparedColumns = (\&render_resource, \&render_communication_status,
 1340:                        \&render_quick_status, \&render_long_status,
 1341: 		       \&render_parts_summary_status);
 1342: 
 1343: sub setDefault {
 1344:     my ($val, $default) = @_;
 1345:     if (!defined($val)) { return $default; }
 1346:     return $val;
 1347: }
 1348: 
 1349: sub cmp_title {
 1350:     my ($atitle,$btitle) = (lc($_[0]->compTitle),lc($_[1]->compTitle));
 1351:     $atitle=~s/^\s*//;
 1352:     $btitle=~s/^\s*//;
 1353:     return $atitle cmp $btitle;
 1354: }
 1355: 
 1356: sub render {
 1357:     my $args = shift;
 1358:     &Apache::loncommon::get_unprocessed_cgi($ENV{QUERY_STRING});
 1359:     my $result = '';
 1360:     # Configure the renderer.
 1361:     my $cols = $args->{'cols'};
 1362:     if (!defined($cols)) {
 1363:         # no columns, no nav maps.
 1364:         return '';
 1365:     }
 1366:     my $navmap;
 1367:     if (defined($args->{'navmap'})) {
 1368:         $navmap = $args->{'navmap'};
 1369:     }
 1370: 
 1371:     my $r = $args->{'r'};
 1372:     my $queryString = $args->{'queryString'};
 1373:     my $jump = $args->{'jump'};
 1374:     my $here = $args->{'here'};
 1375:     my $suppressNavmap = setDefault($args->{'suppressNavmap'}, 0);
 1376:     my $closeAllPages = setDefault($args->{'closeAllPages'}, 0);
 1377:     my $currentJumpDelta = 2; # change this to change how many resources are displayed
 1378:                              # before the current resource when using #current
 1379: 
 1380:     # If we were passed 'here' information, we are not rendering
 1381:     # after a folder manipulation, and we were not passed an
 1382:     # iterator, make sure we open the folders to show the "here"
 1383:     # marker
 1384:     my $filterHash = {};
 1385:     # Figure out what we're not displaying
 1386:     foreach my $item (split(/\,/, $env{"form.filter"})) {
 1387:         if ($item) {
 1388:             $filterHash->{$item} = "1";
 1389:         }
 1390:     }
 1391: 
 1392:     # Filter: Remember filter function and add our own filter: Refuse
 1393:     # to show hidden resources unless the user can see them.
 1394:     my $userCanSeeHidden = advancedUser();
 1395:     my $filterFunc = setDefault($args->{'filterFunc'},
 1396:                                 sub {return 1;});
 1397:     if (!$userCanSeeHidden) {
 1398:         # Without renaming the filterfunc, the server seems to go into
 1399:         # an infinite loop
 1400:         my $oldFilterFunc = $filterFunc;
 1401:         $filterFunc = sub { my $res = shift; return !$res->randomout() && 
 1402:                                 ($res->deeplink($args->{'caller'}) ne 'absent') &&
 1403:                                 ($res->deeplink($args->{'caller'}) ne 'grades') &&
 1404:                                 !$res->deeplinkout() &&
 1405:                                 &$oldFilterFunc($res);};
 1406:     }
 1407: 
 1408:     my $condition = 0;
 1409:     if ($env{'form.condition'}) {
 1410:         $condition = 1;
 1411:     } elsif (($env{'request.deeplink.login'}) && ($env{'request.course.id'}) && (!$userCanSeeHidden)) {
 1412:         if (!defined($navmap)) {
 1413:             $navmap = Apache::lonnavmaps::navmap->new();
 1414:         }
 1415:         if (defined($navmap)) {
 1416:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1417:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1418:             my $symb = &Apache::loncommon::symb_from_tinyurl($env{'request.deeplink.login'},$cnum,$cdom);
 1419:             if ($symb) {
 1420:                 my $deeplink;
 1421:                 my $res = $navmap->getBySymb($symb);
 1422:                 if ($res->is_map()) {
 1423:                     my $mapname = &Apache::lonnet::declutter($res->src());
 1424:                     $mapname = &Apache::lonnet::deversion($mapname);
 1425:                     $deeplink = $navmap->get_mapparam(undef,$mapname,"0.deeplink");
 1426:                 } else {
 1427:                     $deeplink = $res->deeplink();
 1428:                 }
 1429:                 if ($deeplink ne '') {
 1430:                     if ((split(/,/,$deeplink))[1] eq 'hide') {
 1431:                         if ($res->is_map()) {
 1432:                             map { $filterHash->{$_} = 1 if $_ } split(/,/,$res->map_hierarchy());
 1433:                         } else {
 1434:                             my $mapurl = (&Apache::lonnet::decode_symb($symb))[0];
 1435:                             my $map = $navmap->getResourceByUrl($mapurl);
 1436:                             map { $filterHash->{$_} = 1 if $_ } split(/,/,$map->map_hierarchy());
 1437:                         }
 1438:                     }
 1439:                 }
 1440:             }
 1441:         }
 1442:     }
 1443: 
 1444:     if (!$env{'form.folderManip'} && !defined($args->{'iterator'}) && !$args->{'nocurrloc'}) {
 1445:         # Step 1: Check to see if we have a navmap
 1446:         if (!defined($navmap)) {
 1447:             $navmap = Apache::lonnavmaps::navmap->new();
 1448: 	    if (!defined($navmap)) {
 1449: 		# no longer in course
 1450: 		return '<span class="LC_error">'.&mt('No course selected').'</span><br />
 1451:                         <a href="/adm/roles">'.&mt('Select a course').'</a><br />';
 1452: 	    }
 1453: 	}
 1454: 
 1455:         # Step two: Locate what kind of here marker is necessary
 1456:         # Determine where the "here" marker is and where the screen jumps to.
 1457: 
 1458:         if ($env{'form.postsymb'} ne '') {
 1459:             $here = $jump = &Apache::lonnet::symbclean($env{'form.postsymb'});
 1460:         } elsif ($env{'form.postdata'} ne '') {
 1461:             # couldn't find a symb, is there a URL?
 1462:             my $currenturl = $env{'form.postdata'};
 1463:             #$currenturl=~s/^http\:\/\///;
 1464:             #$currenturl=~s/^[^\/]+//;
 1465:             unless ($args->{'caller'} eq 'sequence') { 
 1466:                 $here = $jump = &Apache::lonnet::symbread($currenturl);
 1467:             }
 1468: 	}
 1469: 	if (($here eq '') && ($args->{'caller'} ne 'sequence')) { 
 1470: 	    my $last;
 1471: 	    if (tie(my %hash,'GDBM_File',$env{'request.course.fn'}.'_symb.db',
 1472:                     &GDBM_READER(),0640)) {
 1473: 		$last=$hash{'last_known'};
 1474: 		untie(%hash);
 1475: 	    }
 1476: 	    if ($last) { $here = $jump = $last; }
 1477: 	}
 1478: 
 1479:         # Step three: Ensure the folders are open
 1480:         my $mapIterator = $navmap->getIterator(undef, undef, undef, 1);
 1481:         my $curRes;
 1482:         my $found = 0;
 1483:         my $here_is_navmaps = 0;
 1484:         if ($here =~ m{___\d+___adm/navmaps$}) {
 1485:             $here_is_navmaps = 1;
 1486:         }
 1487:         
 1488:         # We only need to do this if we need to open the maps to show the
 1489:         # current position. This will change the counter so we can't count
 1490:         # for the jump marker with this loop.
 1491:         while ($here && ($curRes = $mapIterator->next()) && !$found && !$here_is_navmaps) {
 1492:             if (ref($curRes) && $curRes->symb() eq $here) {
 1493:                 my $mapStack = $mapIterator->getStack();
 1494:                 
 1495:                 # Ensure the parent maps are open
 1496:                 for my $map (@{$mapStack}) {
 1497:                     if ($condition) {
 1498:                         undef $filterHash->{$map->map_pc()};
 1499:                     } else {
 1500:                         $filterHash->{$map->map_pc()} = 1;
 1501:                     }
 1502:                 }
 1503:                 $found = 1;
 1504:             }
 1505:         }            
 1506:     }        
 1507: 
 1508:     if ( !defined($args->{'iterator'}) && $env{'form.folderManip'} ) { # we came from a user's manipulation of the nav page
 1509:         # If this is a click on a folder or something, we want to preserve the "here"
 1510:         # from the querystring, and get the new "jump" marker
 1511:         $here = $env{'form.here'};
 1512:         $jump = $env{'form.jump'};
 1513:     } 
 1514:     
 1515:     my $it = $args->{'iterator'};
 1516:     if (!defined($it)) {
 1517:         # Construct a default iterator based on $env{'form.'} information
 1518:         
 1519:         # Step 1: Check to see if we have a navmap
 1520:         if (!defined($navmap)) {
 1521:             $navmap = Apache::lonnavmaps::navmap->new();
 1522:             if (!defined($navmap)) {
 1523:                 # no longer in course
 1524:                 return '<span class="LC_error">'.&mt('No course selected').'</span><br />
 1525:                         <a href="/adm/roles">'.&mt('Select a course').'</a><br />';
 1526:             }
 1527:         }
 1528: 
 1529:         # See if we're being passed a specific map
 1530:         if ($args->{'iterator_map'}) {
 1531:             my $map = $args->{'iterator_map'};
 1532:             $map = $navmap->getResourceByUrl($map);
 1533:             if (ref($map)) {
 1534:                 my $firstResource = $map->map_start();
 1535:                 my $finishResource = $map->map_finish();
 1536:                 $args->{'iterator'} = $it = $navmap->getIterator($firstResource, $finishResource, $filterHash, $condition);
 1537:             } else {
 1538:                 return;
 1539:             }
 1540:         } else {
 1541:             $args->{'iterator'} = $it = $navmap->getIterator(undef, undef, $filterHash, $condition,undef,$args->{'include_top_level_map'});
 1542:         }
 1543:     }
 1544: 
 1545:     # (re-)Locate the jump point, if any
 1546:     # Note this does not take filtering or hidden into account... need
 1547:     # to be fixed?
 1548:     my $mapIterator = $navmap->getIterator(undef, undef, $filterHash, 0);
 1549:     my $curRes;
 1550:     my $foundJump = 0;
 1551:     my $counter = 0;
 1552:     
 1553:     while (($curRes = $mapIterator->next()) && !$foundJump) {
 1554:         if (ref($curRes)) { $counter++; }
 1555:         
 1556:         if (ref($curRes) && $jump eq $curRes->symb()) {
 1557:             
 1558:             # This is why we have to use the main iterator instead of the
 1559:             # potentially faster DFS: The count has to be the same, so
 1560:             # the order has to be the same, which DFS won't give us.
 1561:             $args->{'currentJumpIndex'} = $counter;
 1562:             $foundJump = 1;
 1563:         }
 1564:     }
 1565: 
 1566:     my $showParts = setDefault($args->{'showParts'}, 1);
 1567:     my $condenseParts = setDefault($args->{'condenseParts'}, 1);
 1568:     # keeps track of when the current resource is found,
 1569:     # so we can back up a few and put the anchor above the
 1570:     # current resource
 1571:     my $printKey = $args->{'printKey'};
 1572:     my $printCloseAll = $args->{'printCloseAll'};
 1573:     if (!defined($printCloseAll)) { $printCloseAll = 1; }
 1574:    
 1575:     # Print key?
 1576:     if ($printKey) {
 1577:         $result .= '<table border="0" cellpadding="2" cellspacing="0">';
 1578:         $result.='<tr><td align="right" valign="bottom">Key:&nbsp;&nbsp;</td>';
 1579: 	my $location=&Apache::loncommon::lonhttpdurl("/adm/lonMisc");
 1580:         if ($navmap->{LAST_CHECK}) {
 1581:             $result .= 
 1582:                 '<img src="'.$location.'/chat.gif" alt="" /> '.&mt('New discussion since').' '.
 1583:                 strftime("%A, %b %e at %I:%M %P", localtime($navmap->{LAST_CHECK})).
 1584:                 '</td><td align="center" valign="bottom">&nbsp;&nbsp;'.
 1585:                 '<img src="'.$location.'/feedback.gif" alt="" /> '.&mt('New message (click to open)').'<p>'.
 1586:                 '</td>'; 
 1587:         } else {
 1588:             $result .= '<td align="center" valign="bottom">&nbsp;&nbsp;'.
 1589:                 '<img src="'.$location.'/chat.gif" alt="" /> '.&mt('Discussions').'</td><td align="center" valign="bottom">'.
 1590:                 '&nbsp;&nbsp;<img src="'.$location.'/feedback.gif" alt="" /> '.&mt('New message (click to open)').
 1591:                 '</td>'; 
 1592:         }
 1593: 
 1594:         $result .= '</tr></table>';
 1595:     }
 1596: 
 1597:     if ($printCloseAll && !$args->{'resource_no_folder_link'}) {
 1598: 	my ($link,$text);
 1599:         if ($condition) {
 1600: 	    $link='navmaps?condition=0&amp;filter=&amp;'.$queryString.
 1601: 		'&amp;here='.&escape($here);
 1602: 	    $text='Close all folders';
 1603:         } else {
 1604: 	    $link='navmaps?condition=1&amp;filter=&amp;'.$queryString.
 1605: 		'&amp;here='.&escape($here);
 1606: 	    $text='Open all folders';
 1607:         }
 1608: 	if ($args->{'caller'} eq 'navmapsdisplay') {
 1609:             unless ($args->{'notools'}) {
 1610:                 &add_linkitem($args->{'linkitems'},'changefolder',
 1611:                               "location.href='$link'",$text);
 1612:             }
 1613: 	} else {
 1614: 	    $result.= '<a href="'.$link.'">'.&mt($text).'</a>';
 1615: 	}
 1616:         $result .= "\n";
 1617:     }
 1618: 
 1619:     # Check for any unread discussions in all resources.
 1620:     if (($args->{'caller'} eq 'navmapsdisplay') && (!$args->{'notools'})) {
 1621: 	&add_linkitem($args->{'linkitems'},'clearbubbles',
 1622: 		      'document.clearbubbles.submit()',
 1623: 		      'Mark all posts read');
 1624: 	my $time=time;
 1625:         my $querystr = &HTML::Entities::encode($ENV{'QUERY_STRING'},'<>&"');
 1626: 	$result .= (<<END);
 1627:     <form name="clearbubbles" method="post" action="/adm/feedback">
 1628: 	<input type="hidden" name="navurl" value="$querystr" />
 1629: 	<input type="hidden" name="navtime" value="$time" />
 1630: END
 1631:         if ($args->{'sort'} eq 'discussion') { 
 1632: 	    my $totdisc = 0;
 1633: 	    my $haveDisc = '';
 1634: 	    my @allres=$navmap->retrieveResources();
 1635: 	    foreach my $resource (@allres) {
 1636: 		if ($resource->hasDiscussion()) {
 1637: 		    $haveDisc .= $resource->wrap_symb().':';
 1638: 		    $totdisc ++;
 1639: 		}
 1640: 	    }
 1641: 	    if ($totdisc > 0) {
 1642: 		$haveDisc =~ s/:$//;
 1643: 		$result .= (<<END);
 1644: 	<input type="hidden" name="navmaps" value="$haveDisc" />
 1645:     </form>
 1646: END
 1647:             }
 1648: 	}
 1649: 	$result.='</form>';
 1650:     }
 1651:     if (($args->{'caller'} eq 'navmapsdisplay') && ($env{'request.course.id'})) {
 1652:         my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1653:         my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1654:         if ($env{'course.'.$env{'request.course.id'}.'.url'} eq
 1655:             "uploaded/$cdom/$cnum/default.sequence") {
 1656:             if ((&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) ||
 1657:                 (&Apache::lonnet::allowed('cev',$env{'request.course.id'}))) {
 1658:                 &add_linkitem($args->{'linkitems'},'edittoplevel',
 1659:                               "javascript:gocmd('/adm/coursedocs','editdocs');",
 1660:                               'Content Editor');
 1661:             }
 1662:             if ($counter) {
 1663:                 &add_linkitem($args->{'linkitems'},'printout',
 1664:                               "javascript:gopost('/adm/printout','/adm/navmaps');",
 1665:                               'Prepare a printable document');
 1666:             }
 1667:         }
 1668:     }
 1669: 
 1670:     if ($args->{'caller'} eq 'navmapsdisplay') {
 1671:         $result .= &show_linkitems_toolbar($args,$condition);
 1672:     } elsif ($args->{'sort_html'}) { 
 1673:         $result.=$args->{'sort_html'}; 
 1674:     }
 1675: 
 1676:     #$result .= "<br />\n";
 1677:     if ($r) {
 1678:         $r->print($result);
 1679:         $r->rflush();
 1680:         $result = "";
 1681:     }
 1682:     # End parameter setting
 1683:     
 1684:     $result .= "<br />\n";
 1685: 
 1686:     # Data
 1687:     $result.=&Apache::loncommon::start_data_table("LC_tableOfContent");    
 1688: 
 1689:     my $res = "Apache::lonnavmaps::resource";
 1690:     my %condenseStatuses =
 1691:         ( $res->NETWORK_FAILURE    => 1,
 1692:           $res->NOTHING_SET        => 1,
 1693:           $res->CORRECT            => 1 );
 1694: 
 1695:     # Shared variables
 1696:     $args->{'counter'} = 0; # counts the rows
 1697:     $args->{'indentLevel'} = 0;
 1698:     $args->{'isNewBranch'} = 0;
 1699:     $args->{'condensed'} = 0;   
 1700: 
 1701:     my $location = &Apache::loncommon::lonhttpdurl("/adm/lonIcons/whitespace_21.gif");
 1702:     $args->{'indentString'} = setDefault($args->{'indentString'}, "<img src='$location' alt='' />");
 1703:     $args->{'displayedHereMarker'} = 0;
 1704: 
 1705:     # If we're suppressing empty sequences, look for them here.
 1706:     # We also do this even if $args->{'suppressEmptySequences'}
 1707:     # is not true, so we can hide empty sequences for which the
 1708:     # hiddenresource parameter is set to yes (at map level), or
 1709:     # mark as hidden for users who have $userCanSeeHidden.
 1710:     # Use DFS for speed, since structure actually doesn't matter,
 1711:     # except what map has what resources.
 1712:     #
 1713:     # To ensure the "Selected Resources from selected folder in course"
 1714:     # printout generation option will work in sessions launched via a
 1715:     # deep link, the value of $args->{'filterFunc'} included in the
 1716:     # call to lonnavmaps::render() is omitted from the filter function
 1717:     # used with the DFS Iterator when $args->{'caller'} is 'printout'.
 1718:     #
 1719:     # As a result $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} can be
 1720:     # set to 1 for folder(s) which include resources only accessible
 1721:     # for sessions launched via a deep link, when the current session
 1722:     # is of that type.
 1723: 
 1724:     my $dfsit = Apache::lonnavmaps::DFSiterator->new($navmap,
 1725:                                                      $it->{FIRST_RESOURCE},
 1726:                                                      $it->{FINISH_RESOURCE},
 1727:                                                      {}, undef, 1);
 1728: 
 1729:     my $dfsFilterFunc;
 1730:     if ($args->{'caller'} eq 'printout') {
 1731:         $dfsFilterFunc = sub { my $res = shift; return !$res->randomout() &&
 1732:                               ($res->deeplink($args->{'caller'}) ne 'absent') &&
 1733:                               ($res->deeplink($args->{'caller'}) ne 'grades') &&
 1734:                               !$res->deeplinkout();};
 1735:     } else {
 1736:         $dfsFilterFunc = $filterFunc;
 1737:     }
 1738:     my $depth = 0;
 1739:     $dfsit->next();
 1740:     my $curRes = $dfsit->next();
 1741:     while ($depth > -1) {
 1742:         if ($curRes == $dfsit->BEGIN_MAP()) { $depth++; }
 1743:         if ($curRes == $dfsit->END_MAP()) { $depth--; }
 1744: 
 1745:         if (ref($curRes)) {
 1746:             # Parallel pre-processing: Do sequences have non-filtered-out children?
 1747:             if ($curRes->is_map()) {
 1748:                 $curRes->{DATA}->{HAS_VISIBLE_CHILDREN} = 0;
 1749:                 # Sequences themselves do not count as visible children,
 1750:                 # unless those sequences also have visible children.
 1751:                 # This means if a sequence appears, there's a "promise"
 1752:                 # that there's something under it if you open it, somewhere.
 1753:             } elsif ($curRes->src()) {
 1754:                 # Not a sequence: if it's filtered, ignore it, otherwise
 1755:                 # rise up the stack and mark the sequences as having children
 1756:                 if (&$dfsFilterFunc($curRes)) {
 1757:                     for my $sequence (@{$dfsit->getStack()}) {
 1758:                         next unless ($sequence->is_map());
 1759:                         $sequence->{DATA}->{HAS_VISIBLE_CHILDREN} = 1;
 1760:                     }
 1761:                 }
 1762:             }
 1763:         }
 1764:     } continue {
 1765:         $curRes = $dfsit->next();
 1766:     }
 1767: 
 1768:     my $displayedJumpMarker = 0;
 1769:     # Set up iteration.
 1770:     my $now = time();
 1771:     my $in24Hours = $now + 24 * 60 * 60;
 1772:     my $rownum = 0;
 1773: 
 1774:     # export "here" marker information
 1775:     $args->{'here'} = $here;
 1776: 
 1777:     $args->{'indentLevel'} = -1; # first BEGIN_MAP takes this to 0
 1778:     my @resources;
 1779:     my $code='';# sub { !(shift->is_map();) };
 1780:     if ($args->{'sort'} eq 'title') {
 1781:         my $oldFilterFunc = $filterFunc;
 1782: 	my $filterFunc= 
 1783: 	    sub {
 1784: 		my ($res)=@_;
 1785: 		if ($res->is_map()) { return 0;}
 1786: 		return &$oldFilterFunc($res);
 1787: 	    };
 1788: 	@resources=$navmap->retrieveResources(undef,$filterFunc);
 1789: 	@resources= sort { &cmp_title($a,$b) } @resources;
 1790:     } elsif ($args->{'sort'} eq 'duedate') {
 1791: 	my $oldFilterFunc = $filterFunc;
 1792: 	my $filterFunc= 
 1793: 	    sub {
 1794: 		my ($res)=@_;
 1795: 		if (!$res->is_problem()) { return 0;}
 1796: 		return &$oldFilterFunc($res);
 1797: 	    };
 1798: 	@resources=$navmap->retrieveResources(undef,$filterFunc);
 1799: 	@resources= sort {
 1800: 	    if ($a->duedate ne $b->duedate) {
 1801: 	        return $a->duedate cmp $b->duedate;
 1802: 	    }
 1803: 	    my $value=&cmp_title($a,$b);
 1804: 	    return $value;
 1805: 	} @resources;
 1806:     } elsif ($args->{'sort'} eq 'discussion') {
 1807: 	my $oldFilterFunc = $filterFunc;
 1808: 	my $filterFunc= 
 1809: 	    sub {
 1810: 		my ($res)=@_;
 1811: 		if (!$res->hasDiscussion() &&
 1812: 		    !$res->getFeedback() &&
 1813: 		    !$res->getErrors()) { return 0;}
 1814: 		return &$oldFilterFunc($res);
 1815: 	    };
 1816: 	@resources=$navmap->retrieveResources(undef,$filterFunc);
 1817: 	@resources= sort { &cmp_title($a,$b) } @resources;
 1818:     } else {
 1819: 	#unknow sort mechanism or default
 1820: 	undef($args->{'sort'});
 1821:     }
 1822: 
 1823:     # Determine if page will be served with https in case
 1824:     # it contains a syllabus which uses an external URL
 1825:     # which points at an http site.
 1826: 
 1827:     my ($is_ssl,$cdom,$cnum,$hostname);
 1828:     if ($ENV{'SERVER_PORT'} == 443) {
 1829:         $is_ssl = 1;
 1830:         if ($r) {
 1831:             $hostname = $r->hostname();
 1832:         } else {
 1833:             $hostname = $ENV{'SERVER_NAME'};
 1834:         }
 1835:     }
 1836:     if ($env{'request.course.id'}) {
 1837:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1838:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 1839:     }
 1840: 
 1841:     my $inhibitmenu;
 1842:     if ($args->{'modalLink'}) {
 1843:         $inhibitmenu = '&amp;inhibitmenu=yes';
 1844:     }
 1845: 
 1846:     while (1) {
 1847: 	if ($args->{'sort'}) {
 1848: 	    $curRes = shift(@resources);
 1849: 	} else {
 1850: 	    $curRes = $it->next($closeAllPages);
 1851: 	}
 1852: 	if (!$curRes) { last; }
 1853: 
 1854:         # Maintain indentation level.
 1855:         if ($curRes == $it->BEGIN_MAP() ||
 1856:             $curRes == $it->BEGIN_BRANCH() ) {
 1857:             $args->{'indentLevel'}++;
 1858:         }
 1859:         if ($curRes == $it->END_MAP() ||
 1860:             $curRes == $it->END_BRANCH() ) {
 1861:             $args->{'indentLevel'}--;
 1862:         }
 1863:         # Notice new branches
 1864:         if ($curRes == $it->BEGIN_BRANCH()) {
 1865:             $args->{'isNewBranch'} = 1;
 1866:         }
 1867: 
 1868:         # If this isn't an actual resource, continue on
 1869:         if (!ref($curRes)) {
 1870:             next;
 1871:         }
 1872: 
 1873:         # If this has been filtered out, continue on
 1874:         if (!(&$filterFunc($curRes))) {
 1875:             $args->{'isNewBranch'} = 0; # Don't falsely remember this
 1876:             next;
 1877:         } 
 1878: 
 1879:         # If this is an empty sequence and we're filtering them, continue on
 1880:         $args->{'mapHidden'} = 0;
 1881:         $args->{'mapUnlisted'} = 0;
 1882:         $args->{'mapHiddenDeepLink'} = 0;
 1883:         if (($curRes->is_map()) && (!$curRes->{DATA}->{HAS_VISIBLE_CHILDREN})) {
 1884:             if ($args->{'suppressEmptySequences'}) {
 1885:                 next;
 1886:             } else {
 1887:                 my $mapname = &Apache::lonnet::declutter($curRes->src());
 1888:                 $mapname = &Apache::lonnet::deversion($mapname);
 1889:                 if (lc($navmap->get_mapparam(undef,$mapname,"0.hiddenresource")) eq 'yes') {
 1890:                     if ($userCanSeeHidden) {
 1891:                         $args->{'mapHidden'} = 1;
 1892:                     } else {
 1893:                         next;
 1894:                     }
 1895:                 } elsif ($curRes->deeplinkout) {
 1896:                     if ($userCanSeeHidden) {
 1897:                         $args->{'mapHiddenDeepLink'} = 1;
 1898:                     } else {
 1899:                         next;
 1900:                     }
 1901:                 } else {
 1902:                     my $deeplink = $navmap->get_mapparam(undef,$mapname,"0.deeplink");
 1903:                     my ($state,$others,$listed) = split(/,/,$deeplink);
 1904:                     if (($listed eq 'absent') || ($listed eq 'grades')) {
 1905:                         if ($userCanSeeHidden) {
 1906:                             $args->{'mapUnlisted'} = 1;
 1907:                         } else {
 1908:                             next;
 1909:                         }
 1910:                     }
 1911:                 }
 1912:             }
 1913:         }
 1914: 
 1915:         # If we're suppressing navmaps and this is a navmap, continue on
 1916:         if ($suppressNavmap && $curRes->src() =~ /^\/adm\/navmaps/) {
 1917:             next;
 1918:         }
 1919: 
 1920:         $args->{'counter'}++;
 1921: 
 1922:         # Does it have multiple parts?
 1923:         $args->{'multipart'} = 0;
 1924:         $args->{'condensed'} = 0;
 1925:         my @parts;
 1926:             
 1927:         # Decide what parts to show.
 1928:         if ($curRes->is_problem() && $showParts) {
 1929:             @parts = @{$curRes->parts()};
 1930:             $args->{'multipart'} = $curRes->multipart();
 1931:             
 1932:             if ($condenseParts) { # do the condensation
 1933:                 if (!$args->{'condensed'}) {
 1934:                     # Decide whether to condense based on similarity
 1935:                     my $status = $curRes->status($parts[0]);
 1936:                     my $due = $curRes->duedate($parts[0]);
 1937:                     my $open = $curRes->opendate($parts[0]);
 1938:                     my $statusAllSame = 1;
 1939:                     my $dueAllSame = 1;
 1940:                     my $openAllSame = 1;
 1941:                     for (my $i = 1; $i < scalar(@parts); $i++) {
 1942:                         if ($curRes->status($parts[$i]) != $status){
 1943:                             $statusAllSame = 0;
 1944:                         }
 1945:                         if ($curRes->duedate($parts[$i]) != $due ) {
 1946:                             $dueAllSame = 0;
 1947:                         }
 1948:                         if ($curRes->opendate($parts[$i]) != $open) {
 1949:                             $openAllSame = 0;
 1950:                         }
 1951:                     }
 1952:                     # $*allSame is true if all the statuses were
 1953:                     # the same. Now, if they are all the same and
 1954:                     # match one of the statuses to condense, or they
 1955:                     # are all open with the same due date, or they are
 1956:                     # all OPEN_LATER with the same open date, display the
 1957:                     # status of the first non-zero part (to get the 'correct'
 1958:                     # status right, since 0 is never 'correct' or 'open').
 1959:                     if (($statusAllSame && defined($condenseStatuses{$status})) ||
 1960:                         ($dueAllSame && $status == $curRes->OPEN && $statusAllSame)||
 1961:                         ($openAllSame && $status == $curRes->OPEN_LATER && $statusAllSame) ){
 1962:                         @parts = ($parts[0]);
 1963:                         $args->{'condensed'} = 1;
 1964:                     }
 1965:                 }
 1966: 		# Multipart problem with one part: always "condense" (happens
 1967: 		#  to match the desirable behavior)
 1968: 		if ($curRes->countParts() == 1) {
 1969: 		    @parts = ($parts[0]);
 1970: 		    $args->{'condensed'} = 1;
 1971: 		}
 1972:             }
 1973:         }
 1974:         # If deep-link parameter is set (and is not set to full) suppress link
 1975:         # unless privileged user, tinyurl used for login resolved to a map, and
 1976:         # the resource is within the map.
 1977:         if ((!$curRes->deeplink($args->{'caller'})) ||
 1978:             ($curRes->deeplink($args->{'caller'}) eq 'full') || &advancedUser()) {
 1979:             $args->{'resource_nolink'} = 0;
 1980:         } else {
 1981:             $args->{'resource_nolink'} = 1;
 1982:         }
 1983:             
 1984:         # If the multipart problem was condensed, "forget" it was multipart
 1985:         if (scalar(@parts) == 1) {
 1986:             $args->{'multipart'} = 0;
 1987:         } else {
 1988:             # Add part 0 so we display it correctly.
 1989:             unshift @parts, '0';
 1990:         }
 1991: 	
 1992: 	{
 1993: 	    my ($src,$symb,$anchor,$stack);
 1994: 	    if ($args->{'sort'}) {
 1995: 		my $it = $navmap->getIterator(undef, undef, undef, 1);
 1996: 		while ( my $res=$it->next()) {
 1997: 		    if (ref($res) &&
 1998: 			$res->symb() eq  $curRes->symb()) { last; }
 1999: 		}
 2000: 		$stack=$it->getStack();
 2001: 	    } else {
 2002: 		$stack=$it->getStack();
 2003: 	    }
 2004: 	    ($src,$symb,$anchor)=getLinkForResource($stack);
 2005:             my $srcHasQuestion = $src =~ /\?/;
 2006:             if ($env{'request.course.id'}) {
 2007:                 if (($is_ssl) && ($src =~ m{^\Q/public/$cdom/$cnum/syllabus\E($|\?)}) &&
 2008:                     ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
 2009:                     unless ((&Apache::lonnet::uses_sts()) || (&Apache::lonnet::waf_allssl($hostname))) {
 2010:                         if ($hostname ne '') {
 2011:                             $src = 'http://'.$hostname.$src;
 2012:                         }
 2013:                         $src .= ($srcHasQuestion? '&amp;' : '?') . 'usehttp=1';
 2014:                         $srcHasQuestion = 1;
 2015:                     }
 2016:                 } elsif (($is_ssl) && ($src =~ m{^\Q/adm/wrapper/ext/\E(?!https:)})) {
 2017:                     unless ((&Apache::lonnet::uses_sts()) || (&Apache::lonnet::waf_allssl($hostname))) {
 2018:                         if ($hostname ne '') {
 2019:                             $src = 'http://'.$hostname.$src;
 2020:                         }
 2021:                         $src .= ($srcHasQuestion? '&amp;' : '?') . 'usehttp=1';
 2022:                         $srcHasQuestion = 1;
 2023:                     }
 2024:                 }
 2025:             }
 2026: 	    if (defined($anchor)) { $anchor='#'.$anchor; }
 2027:             if (($args->{'caller'} eq 'sequence') && ($curRes->is_map())) {
 2028:                 $args->{"resourceLink"} = $src.($srcHasQuestion?'&amp;':'?') .'navmap=1';
 2029:             } else {
 2030: 	        $args->{"resourceLink"} = $src.
 2031: 		    ($srcHasQuestion?'&amp;':'?') .
 2032: 		    'symb=' . &escape($symb).$inhibitmenu.$anchor;
 2033:             }
 2034: 	}
 2035:         # Now, we've decided what parts to show. Loop through them and
 2036:         # show them.
 2037:         foreach my $part (@parts) {
 2038:             $rownum ++;
 2039:             
 2040:             $result .= &Apache::loncommon::start_data_table_row();
 2041: 
 2042:             # Set up some data about the parts that the cols might want
 2043:             my $filter = $it->{FILTER};
 2044: 
 2045:             # Now, display each column.
 2046:             foreach my $col (@$cols) {
 2047:                 my $colHTML = '';
 2048:                 if (ref($col)) {
 2049:                     $colHTML .= &$col($curRes, $part, $args);
 2050:                 } else {
 2051:                     $colHTML .= &{$preparedColumns[$col]}($curRes, $part, $args);
 2052:                 }
 2053: 
 2054:                 # If this is the first column and it's time to print
 2055:                 # the anchor, do so
 2056:                 if ($col == $cols->[0] && 
 2057:                     $args->{'counter'} == $args->{'currentJumpIndex'} - 
 2058:                     $currentJumpDelta) {
 2059:                     # Jam the anchor after the <td> tag;
 2060:                     # necessary for valid HTML (which Mozilla requires)
 2061:                     $colHTML =~ s/\>/\>\<a name="curloc" \>\<\/a\>/;
 2062:                     $displayedJumpMarker = 1;
 2063:                 }
 2064:                 $result .= $colHTML . "\n";
 2065:             }
 2066:             $result .= &Apache::loncommon::end_data_table_row();
 2067:             $args->{'isNewBranch'} = 0;
 2068:         }
 2069: 
 2070:         if ($r && $rownum % 20 == 0) {
 2071:             $r->print($result);
 2072:             $result = "";
 2073:             $r->rflush();
 2074:         }
 2075:     } continue {
 2076: 	if ($r) {
 2077: 	    # If we have the connection, make sure the user is still connected
 2078: 	    my $c = $r->connection;
 2079: 	    if ($c->aborted()) {
 2080: 		# Who cares what we do, nobody will see it anyhow.
 2081: 		return '';
 2082: 	    }
 2083: 	}
 2084:     }
 2085: 
 2086:     $result.=&Apache::loncommon::end_data_table();
 2087:     
 2088:     # Print out the part that jumps to #curloc if it exists
 2089:     # delay needed because the browser is processing the jump before
 2090:     # it finishes rendering, so it goes to the wrong place!
 2091:     # onload might be better, but this routine has no access to that.
 2092:     # On mozilla, the 0-millisecond timeout seems to prevent this;
 2093:     # it's quite likely this might fix other browsers, too, and 
 2094:     # certainly won't hurt anything.
 2095:     if ($displayedJumpMarker) {
 2096:         $result .= &Apache::lonhtmlcommon::scripttag("
 2097: if (location.href.indexOf('#curloc')==-1) {
 2098:     setTimeout(\"location += '#curloc';\", 0)
 2099: }
 2100: ");
 2101:     }
 2102: 
 2103:     if ($r) {
 2104:         $r->print($result);
 2105:         $result = "";
 2106:         $r->rflush();
 2107:     }
 2108:         
 2109:     return $result;
 2110: }
 2111: 
 2112: sub add_linkitem {
 2113:     my ($linkitems,$name,$cmd,$text)=@_;
 2114:     $$linkitems{$name}{'cmd'}=$cmd;
 2115:     $$linkitems{$name}{'text'}=&mt($text);
 2116: }
 2117: 
 2118: sub show_linkitems_toolbar {
 2119:     my ($args,$condition) = @_;
 2120:     my $result;
 2121:     if (ref($args) eq 'HASH') {
 2122:         if (ref($args->{'linkitems'}) eq 'HASH') {
 2123:             my $numlinks = scalar(keys(%{$args->{'linkitems'}}));
 2124:             if ($numlinks > 1) {
 2125:                 $result = '<td>'.
 2126:                           &Apache::loncommon::help_open_menu('Navigation Screen','Navigation_Screen',
 2127:                                                              undef,'RAT').
 2128:                           '</td>'.
 2129:                           '<td>&nbsp;</td>'.
 2130:                           '<td class="LC_middle">'.&mt('Tools:').'</td>';
 2131:             }
 2132:             $result .= '<td align="left">'."\n".
 2133:                        '<ul id="LC_toolbar">';
 2134:             my @linkorder = ('firsthomework','everything','uncompleted',
 2135:                              'changefolder','clearbubbles','printout','edittoplevel');
 2136:             foreach my $link (@linkorder) {
 2137:                 if (ref($args->{'linkitems'}{$link}) eq 'HASH') {
 2138:                     if ($args->{'linkitems'}{$link}{'text'} ne '') {
 2139:                         $args->{'linkitems'}{$link}{'cmd'}=~s/"/'/g;
 2140:                         if ($args->{'linkitems'}{$link}{'cmd'}) {
 2141:                             my $link_id = 'LC_content_toolbar_'.$link;
 2142:                             if ($link eq 'changefolder') {
 2143:                                 if ($condition) {
 2144:                                     $link_id='LC_content_toolbar_changefolder_toggled';
 2145:                                 } else {
 2146:                                     $link_id='LC_content_toolbar_changefolder';
 2147:                                 }
 2148:                             }
 2149:                             $result .= '<li><a href="#" '.
 2150:                                        'onclick="'.$args->{'linkitems'}{$link}{'cmd'}.'" '.
 2151:                                        'id="'.$link_id.'" '.
 2152:                                        'class="LC_toolbarItem" '.
 2153:                                        'title="'.$args->{'linkitems'}{$link}{'text'}.'">'.
 2154:                                        '</a></li>'."\n";
 2155:                         }
 2156:                     }
 2157:                 }
 2158:             }
 2159:             $result .= '</ul>'.
 2160:                        '</td>';
 2161:             if (($numlinks==1) && (exists($args->{'linkitems'}{'edittoplevel'}))) {
 2162:                 $result .= '<td><a href="'.$args->{'linkitems'}{'edittoplevel'}{'cmd'}.'">'.
 2163:                            &mt('Content Editor').'</a></td>';
 2164:             }
 2165:         }
 2166:         if ($args->{'sort_html'}) {
 2167:             $result .= '<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>'.
 2168:                        '<td align="right">'.$args->{'sort_html'}.'</td>';
 2169:         }
 2170:     }
 2171:     if ($result) {
 2172:         $result = "<table><tr>$result</tr></table>";
 2173:     }
 2174:     return $result;
 2175: }
 2176: 
 2177: 1;
 2178: 
 2179: 
 2180: 
 2181: 
 2182: 
 2183: 
 2184: 
 2185: 
 2186: 
 2187: package Apache::lonnavmaps::navmap;
 2188: 
 2189: =pod
 2190: 
 2191: =head1 Object: Apache::lonnavmaps::navmap
 2192: 
 2193: =head2 Overview
 2194: 
 2195: The navmap object's job is to provide access to the resources
 2196: in the course as Apache::lonnavmaps::resource objects, and to
 2197: query and manage the relationship between those resource objects.
 2198: 
 2199: Generally, you'll use the navmap object in one of three basic ways.
 2200: In order of increasing complexity and power:
 2201: 
 2202: =over 4
 2203: 
 2204: =item * C<$navmap-E<gt>getByX>, where X is B<Id>, B<Symb> or B<MapPc> and getResourceByUrl. This provides
 2205:     various ways to obtain resource objects, based on various identifiers.
 2206:     Use this when you want to request information about one object or 
 2207:     a handful of resources you already know the identities of, from some
 2208:     other source. For more about Ids, Symbs, and MapPcs, see the
 2209:     Resource documentation. Note that Url should be a B<last resort>,
 2210:     not your first choice; it only really works when there is only one
 2211:     instance of the resource in the course, which only applies to
 2212:     maps, and even that may change in the future (see the B<getResourceByUrl>
 2213:     documentation for more details.)
 2214: 
 2215: =item * C<my @resources = $navmap-E<gt>retrieveResources(args)>. This
 2216:     retrieves resources matching some criterion and returns them
 2217:     in a flat array, with no structure information. Use this when
 2218:     you are manipulating a series of resources, based on what map
 2219:     the are in, but do not care about branching, or exactly how
 2220:     the maps and resources are related. This is the most common case.
 2221: 
 2222: =item * C<$it = $navmap-E<gt>getIterator(args)>. This allows you traverse
 2223:     the course's navmap in various ways without writing the traversal
 2224:     code yourself. See iterator documentation below. Use this when
 2225:     you need to know absolutely everything about the course, including
 2226:     branches and the precise relationship between maps and resources.
 2227: 
 2228: =back
 2229: 
 2230: =head2 Creation And Destruction
 2231: 
 2232: To create a navmap object, use the following function:
 2233: 
 2234: =over 4
 2235: 
 2236: =item * B<Apache::lonnavmaps::navmap-E<gt>new>():
 2237: 
 2238: Creates a new navmap object. Returns the navmap object if this is
 2239: successful, or B<undef> if not.
 2240: 
 2241: =back
 2242: 
 2243: =head2 Methods
 2244: 
 2245: =over 4
 2246: 
 2247: =item * B<getIterator>(first, finish, filter, condition):
 2248: 
 2249: See iterator documentation below.
 2250: 
 2251: =cut
 2252: 
 2253: use strict;
 2254: use GDBM_File;
 2255: use Apache::lonnet;
 2256: use LONCAPA;
 2257: 
 2258: sub new {
 2259:     # magic invocation to create a class instance
 2260:     my $proto = shift;
 2261:     my $class = ref($proto) || $proto;
 2262:     my $self = {};
 2263:     bless($self);		# So we can call change_user if necessary
 2264: 
 2265:     $self->{USERNAME} = shift || $env{'user.name'};
 2266:     $self->{DOMAIN}   = shift || $env{'user.domain'};
 2267:     $self->{CODE}     = shift;
 2268:     $self->{NOHIDE} = shift;
 2269: 
 2270: 
 2271: 
 2272:     # Resource cache stores navmap resources as we reference them. We generate
 2273:     # them on-demand so we don't pay for creating resources unless we use them.
 2274:     $self->{RESOURCE_CACHE} = {};
 2275: 
 2276:     # Network failure flag, if we accessed the course or user opt and
 2277:     # failed
 2278:     $self->{NETWORK_FAILURE} = 0;
 2279: 
 2280:     # We can only tie the nav hash as done below if the username/domain
 2281:     # match the env one. Otherwise change_user does everything we need...since we can't
 2282:     # assume there are course hashes for the specific requested user:domain
 2283:     # Note: change_user is also called if we need the nav hash when printing CODEd 
 2284:     # assignments or printing an exam, in which the enclosing folder for the items in
 2285:     # the exam has hidden set.
 2286:     #
 2287: 
 2288:     if (($self->{USERNAME} eq $env{'user.name'}) && ($self->{DOMAIN} eq $env{'user.domain'}) &&
 2289:          !$self->{CODE} && !$self->{NOHIDE}) {
 2290: 	
 2291: 	# tie the nav hash
 2292: 	
 2293: 	my %navmaphash;
 2294: 	my %parmhash;
 2295: 	my $courseFn = $env{"request.course.fn"};
 2296: 	if (!(tie(%navmaphash, 'GDBM_File', "${courseFn}.db",
 2297: 		  &GDBM_READER(), 0640))) {
 2298: 	    return undef;
 2299: 	}
 2300: 	
 2301: 	if (!(tie(%parmhash, 'GDBM_File', "${courseFn}_parms.db",
 2302: 		  &GDBM_READER(), 0640)))
 2303: 	{
 2304: 	    untie %{$self->{PARM_HASH}};
 2305: 	    return undef;
 2306: 	}
 2307: 	
 2308: 	$self->{NAV_HASH} = \%navmaphash;
 2309: 	$self->{PARM_HASH} = \%parmhash;
 2310: 	$self->{PARM_CACHE} = {};
 2311:     } else {
 2312: 	$self->change_user($self->{USERNAME}, $self->{DOMAIN},  $self->{CODE}, $self->{NOHIDE});
 2313:     }
 2314: 
 2315:     return $self;
 2316: }
 2317: 
 2318: #
 2319: #  In some instances it is useful to be able to dynamically change the
 2320: # username/domain associated with a navmap (e.g. to navigate for someone
 2321: # else besides the current user...if sufficiently privileged.
 2322: # Parameters:
 2323: #    user  - New user.
 2324: #    domain- Domain the user belongs to.
 2325: #    code  - Anonymous CODE in use.
 2326: # Implicit inputs:
 2327: #   
 2328: sub change_user {
 2329:     my $self = shift;
 2330:     $self->{USERNAME} = shift;
 2331:     $self->{DOMAIN}   = shift;
 2332:     $self->{CODE}     = shift;
 2333:     $self->{NOHIDE}   = shift;
 2334: 
 2335:     # If the hashes are already tied make sure to break that bond:
 2336: 
 2337:     untie %{$self->{NAV_HASH}}; 
 2338:     untie %{$self->{PARM_HASH}};
 2339: 
 2340:     # The assumption is that we have to
 2341:     # use lonmap here to re-read the hash and from it reconstruct
 2342:     # new big and parameter hashes.  An implicit assumption at this time
 2343:     # is that the course file is probably not created locally yet
 2344:     # an that we will therefore just read without tying.
 2345: 
 2346:     my ($cdom, $cnum) = split(/\_/, $env{'request.course.id'});
 2347: 
 2348:     my %big_hash;
 2349:     &Apache::lonmap::loadmap($cnum, $cdom, $self->{USERNAME}, $self->{DOMAIN}, $self->{CODE}, $self->{NOHIDE}, \%big_hash);
 2350:     $self->{NAV_HASH} = \%big_hash;
 2351: 
 2352: 
 2353: 
 2354:     # Now clear the parm cache and reconstruct the parm hash fromt he big_hash
 2355:     # param.xxxx keys.
 2356: 
 2357:     $self->{PARM_CACHE} = {};
 2358:     
 2359:     my %parm_hash = {};
 2360:     foreach my $key (keys(%big_hash)) {
 2361: 	if ($key =~ /^param\./) {
 2362: 	    my $param_key = $key;
 2363: 	    $param_key =~ s/^param\.//;
 2364: 	    $parm_hash{$param_key} = $big_hash{$key};
 2365: 	}
 2366:     }
 2367: 
 2368:     $self->{PARM_HASH} = \%parm_hash;
 2369: 
 2370: }
 2371: 
 2372: sub generate_course_user_opt {
 2373:     my $self = shift;
 2374:     if ($self->{COURSE_USER_OPT_GENERATED}) { return; }
 2375: 
 2376:     my $uname=$self->{USERNAME};
 2377:     my $udom=$self->{DOMAIN};
 2378: 
 2379:     my $cid=$env{'request.course.id'};
 2380:     my $cdom=$env{'course.'.$cid.'.domain'};
 2381:     my $cnum=$env{'course.'.$cid.'.num'};
 2382:     
 2383: # ------------------------------------------------- Get coursedata (if present)
 2384:     my $courseopt=&Apache::lonnet::get_courseresdata($cnum,$cdom);
 2385:     # Check for network failure
 2386:     if (!ref($courseopt)) {
 2387: 	if ( $courseopt =~ /no.such.host/i || $courseopt =~ /con_lost/i) {
 2388: 	    $self->{NETWORK_FAILURE} = 1;
 2389: 	}
 2390: 	undef($courseopt);
 2391:     }
 2392: 
 2393: # --------------------------------------------------- Get userdata (if present)
 2394: 	
 2395:     my $useropt=&Apache::lonnet::get_userresdata($uname,$udom);
 2396:     # Check for network failure
 2397:     if (!ref($useropt)) {
 2398: 	if ( $useropt =~ /no.such.host/i || $useropt =~ /con_lost/i) {
 2399: 	    $self->{NETWORK_FAILURE} = 1;
 2400: 	}
 2401: 	undef($useropt);
 2402:     }
 2403: 
 2404:     $self->{COURSE_OPT} = $courseopt;
 2405:     $self->{USER_OPT} = $useropt;
 2406: 
 2407:     $self->{COURSE_USER_OPT_GENERATED} = 1;
 2408:     
 2409:     return;
 2410: }
 2411: 
 2412: 
 2413: 
 2414: sub generate_email_discuss_status {
 2415:     my $self = shift;
 2416:     my $symb = shift;
 2417:     if ($self->{EMAIL_DISCUSS_GENERATED}) { return; }
 2418: 
 2419:     my $cid=$env{'request.course.id'};
 2420:     my $cdom=$env{'course.'.$cid.'.domain'};
 2421:     my $cnum=$env{'course.'.$cid.'.num'};
 2422:     
 2423:     my %emailstatus = &Apache::lonnet::dump('email_status',$self->{DOMAIN},$self->{USERNAME});
 2424:     my $logoutTime = $emailstatus{'logout'};
 2425:     my $courseLeaveTime = $emailstatus{'logout_'.$env{'request.course.id'}};
 2426:     $self->{LAST_CHECK} = (($courseLeaveTime > $logoutTime) ?
 2427: 			   $courseLeaveTime : $logoutTime);
 2428:     my %discussiontime = &Apache::lonnet::dump('discussiontimes', 
 2429: 					       $cdom, $cnum);
 2430:     my %lastread = &Apache::lonnet::dump('nohist_'.$cid.'_discuss',
 2431:                                         $self->{DOMAIN},$self->{USERNAME},'lastread');
 2432:     my %lastreadtime = ();
 2433:     foreach my $key (keys(%lastread)) {
 2434:         my $shortkey = $key;
 2435:         $shortkey =~ s/_lastread$//;
 2436:         $lastreadtime{$shortkey} = $lastread{$key};
 2437:     }
 2438: 
 2439:     my %feedback=();
 2440:     my %error=();
 2441:     my @keys = &Apache::lonnet::getkeys('nohist_email',$self->{DOMAIN},
 2442: 					$self->{USERNAME});
 2443:     
 2444:     foreach my $msgid (@keys) {
 2445: 	if ((!$emailstatus{$msgid}) || ($emailstatus{$msgid} eq 'new')) {
 2446:             my ($sendtime,$shortsubj,$fromname,$fromdomain,$status,$fromcid,
 2447:                 $symb,$error) = &Apache::lonmsg::unpackmsgid(&LONCAPA::escape($msgid));
 2448:             &Apache::lonenc::check_decrypt(\$symb); 
 2449:             if (($fromcid ne '') && ($fromcid ne $cid)) {
 2450:                 next;
 2451:             }
 2452:             if (defined($symb)) {
 2453:                 if (defined($error) && $error == 1) {
 2454:                     $error{$symb}.=','.$msgid;
 2455:                 } else {
 2456:                     $feedback{$symb}.=','.$msgid;
 2457:                 }
 2458:             } else {
 2459:                 my $plain=
 2460:                     &LONCAPA::unescape(&LONCAPA::unescape($msgid));
 2461:                 if ($plain=~/ \[([^\]]+)\]\:/) {
 2462:                     my $url=$1;
 2463:                     if ($plain=~/\:Error \[/) {
 2464:                         $error{$url}.=','.$msgid;
 2465:                     } else {
 2466:                         $feedback{$url}.=','.$msgid;
 2467:                     }
 2468:                 }
 2469:             }
 2470: 	}
 2471:     }
 2472:     
 2473:     #symbs of resources that have feedbacks (will be urls pre-2.3)
 2474:     $self->{FEEDBACK} = \%feedback;
 2475:     #or errors (will be urls pre 2.3)
 2476:     $self->{ERROR_MSG} = \%error;
 2477:     $self->{DISCUSSION_TIME} = \%discussiontime;
 2478:     $self->{EMAIL_STATUS} = \%emailstatus;
 2479:     $self->{LAST_READ} = \%lastreadtime;
 2480:     
 2481:     $self->{EMAIL_DISCUSS_GENERATED} = 1;
 2482: }
 2483: 
 2484: sub get_user_data {
 2485:     my $self = shift;
 2486:     if ($self->{RETRIEVED_USER_DATA}) { return; }
 2487: 
 2488:     # Retrieve performance data on problems
 2489:     my %student_data = Apache::lonnet::currentdump($env{'request.course.id'},
 2490: 						   $self->{DOMAIN},
 2491: 						   $self->{USERNAME});
 2492:     $self->{STUDENT_DATA} = \%student_data;
 2493: 
 2494:     $self->{RETRIEVED_USER_DATA} = 1;
 2495: }
 2496: 
 2497: sub get_discussion_data {
 2498:     my $self = shift;
 2499:     if ($self->{RETRIEVED_DISCUSSION_DATA}) {
 2500: 	return $self->{DISCUSSION_DATA};
 2501:     }
 2502: 
 2503:     $self->generate_email_discuss_status();    
 2504: 
 2505:     my $cid=$env{'request.course.id'};
 2506:     my $cdom=$env{'course.'.$cid.'.domain'};
 2507:     my $cnum=$env{'course.'.$cid.'.num'};
 2508:     # Retrieve discussion data for resources in course
 2509:     my %discussion_data = &Apache::lonnet::dumpstore($cid,$cdom,$cnum);
 2510: 
 2511: 
 2512:     $self->{DISCUSSION_DATA} = \%discussion_data;
 2513:     $self->{RETRIEVED_DISCUSSION_DATA} = 1;
 2514:     return $self->{DISCUSSION_DATA};
 2515: }
 2516: 
 2517: 
 2518: # Internal function: Takes a key to look up in the nav hash and implements internal
 2519: # memory caching of that key.
 2520: sub navhash {
 2521:     my $self = shift; my $key = shift;
 2522:     return $self->{NAV_HASH}->{$key};
 2523: }
 2524: 
 2525: =pod
 2526: 
 2527: =item * B<courseMapDefined>(): Returns true if the course map is defined, 
 2528:     false otherwise. Undefined course maps indicate an error somewhere in
 2529:     LON-CAPA, and you will not be able to proceed with using the navmap.
 2530:     See the B<NAV> screen for an example of using this.
 2531: 
 2532: =cut
 2533: 
 2534: # Checks to see if coursemap is defined, matching test in old lonnavmaps
 2535: sub courseMapDefined {
 2536:     my $self = shift;
 2537:     my $uri = &Apache::lonnet::clutter($env{'request.course.uri'});
 2538: 
 2539:     my $firstres = $self->navhash("map_start_$uri");
 2540:     my $lastres = $self->navhash("map_finish_$uri");
 2541:     return $firstres && $lastres;
 2542: }
 2543: 
 2544: sub getIterator {
 2545:     my $self = shift;
 2546:     my $iterator = Apache::lonnavmaps::iterator->new($self, shift, shift,
 2547:                                                      shift, undef, shift,
 2548: 						     shift, shift, shift);
 2549:     return $iterator;
 2550: }
 2551: 
 2552: # Private method: Does the given resource (as a symb string) have
 2553: # current discussion? Returns 0 if chat/mail data not extracted.
 2554: sub hasDiscussion {
 2555:     my $self = shift;
 2556:     my $symb = shift;
 2557:     $self->generate_email_discuss_status();
 2558: 
 2559:     if (!defined($self->{DISCUSSION_TIME})) { return 0; }
 2560: 
 2561:     #return defined($self->{DISCUSSION_TIME}->{$symb});
 2562: 
 2563:     # backward compatibility (bulletin boards used to be 'wrapped')
 2564:     my $ressymb = $self->wrap_symb($symb);
 2565:     if ( defined ( $self->{LAST_READ}->{$ressymb} ) ) {
 2566:         return $self->{DISCUSSION_TIME}->{$ressymb} > $self->{LAST_READ}->{$ressymb};
 2567:     } else {
 2568: #        return $self->{DISCUSSION_TIME}->{$ressymb} >  $self->{LAST_CHECK}; # v.1.1 behavior 
 2569:         return $self->{DISCUSSION_TIME}->{$ressymb} >  0; # in 1.2 will display speech bubble icons for all items with posts until marked as read (even if read in v 1.1).
 2570:     }
 2571: }
 2572: 
 2573: sub last_post_time {
 2574:     my $self = shift;
 2575:     my $symb = shift;
 2576:     my $ressymb = $self->wrap_symb($symb);
 2577:     return $self->{DISCUSSION_TIME}->{$ressymb};
 2578: }
 2579: 
 2580: sub discussion_info {
 2581:     my $self = shift;
 2582:     my $symb = shift;
 2583:     my $filter = shift;
 2584: 
 2585:     $self->get_discussion_data();
 2586: 
 2587:     my $ressymb = $self->wrap_symb($symb);
 2588:     # keys used to store bulletinboard postings use 'unwrapped' symb. 
 2589:     my $discsymb = &escape($self->unwrap_symb($ressymb));
 2590:     my $version = $self->{DISCUSSION_DATA}{'version:'.$discsymb};
 2591:     if (!$version) { return; }
 2592: 
 2593:     my $prevread = $self->{LAST_READ}{$ressymb};
 2594: 
 2595:     my $count = 0;
 2596:     my $hiddenflag = 0;
 2597:     my $deletedflag = 0;
 2598:     my ($hidden,$deleted,%info);
 2599: 
 2600:     for (my $id=$version; $id>0; $id--) {
 2601: 	my $vkeys=$self->{DISCUSSION_DATA}{$id.':keys:'.$discsymb};
 2602: 	my @keys=split(/:/,$vkeys);
 2603: 	if (grep(/^hidden$/ ,@keys)) {
 2604: 	    if (!$hiddenflag) {
 2605: 		$hidden = $self->{DISCUSSION_DATA}{$id.':'.$discsymb.':hidden'};
 2606: 		$hiddenflag = 1;
 2607: 	    }
 2608: 	} elsif (grep(/^deleted$/,@keys)) {
 2609: 	    if (!$deletedflag) {
 2610: 		$deleted = $self->{DISCUSSION_DATA}{$id.':'.$discsymb.':deleted'};
 2611: 		$deletedflag = 1;
 2612: 	    }
 2613: 	} else {
 2614: 	    if (($hidden !~/\.$id\./) && ($deleted !~/\.$id\./)) {
 2615:                 if ($filter eq 'unread') {
 2616: 		    if ($prevread >= $self->{DISCUSSION_DATA}{$id.':'.$discsymb.':timestamp'}) {
 2617:                         next;
 2618:                     }
 2619:                 }
 2620: 		$count++;
 2621: 		$info{$count}{'subject'} =
 2622: 		    $self->{DISCUSSION_DATA}{$id.':'.$discsymb.':subject'};
 2623:                 $info{$count}{'id'} = $id;
 2624:                 $info{$count}{'timestamp'} = $self->{DISCUSSION_DATA}{$id.':'.$discsymb.':timestamp'};
 2625:             }
 2626: 	}
 2627:     }
 2628:     if (wantarray) {
 2629: 	return ($count,%info);
 2630:     }
 2631:     return $count;
 2632: }
 2633: 
 2634: sub wrap_symb {
 2635:     my $self = shift;
 2636:     my $symb = shift;
 2637:     if ($symb =~ m-___(adm/[^/]+/[^/]+/)(\d+)(/bulletinboard)$-) {
 2638:         unless ($symb =~ m|adm/wrapper/adm|) {
 2639:             $symb = 'bulletin___'.$2.'___adm/wrapper/'.$1.$2.$3;
 2640:         }
 2641:     }
 2642:     return $symb;
 2643: }
 2644: 
 2645: sub unwrap_symb {
 2646:     my $self = shift;
 2647:     my $ressymb = shift;
 2648:     my $discsymb = $ressymb;
 2649:     if ($ressymb =~ m-^(bulletin___\d+___)adm/wrapper/(adm/[^/]+/[^/]+/\d+/bulletinboard)$-) {
 2650:          $discsymb = $1.$2;
 2651:     }
 2652:     return $discsymb;
 2653: }
 2654: 
 2655: # Private method: Does the given resource (as a symb string) have
 2656: # current feedback? Returns the string in the feedback hash, which
 2657: # will be false if it does not exist.
 2658: 
 2659: sub getFeedback { 
 2660:     my $self = shift;
 2661:     my $symb = shift;
 2662:     my $source = shift;
 2663: 
 2664:     $self->generate_email_discuss_status();
 2665: 
 2666:     if (!defined($self->{FEEDBACK})) { return ""; }
 2667:     
 2668:     my $feedback;
 2669:     if ($self->{FEEDBACK}->{$symb}) {
 2670:         $feedback = $self->{FEEDBACK}->{$symb};
 2671:         if ($self->{FEEDBACK}->{$source}) {
 2672:             $feedback .= ','.$self->{FEEDBACK}->{$source};
 2673:         }
 2674:     } else {
 2675:         if ($self->{FEEDBACK}->{$source}) {
 2676:             $feedback = $self->{FEEDBACK}->{$source};
 2677:         }
 2678:     }
 2679:     return $feedback;
 2680: }
 2681: 
 2682: # Private method: Get the errors for that resource (by source).
 2683: sub getErrors { 
 2684:     my $self = shift;
 2685:     my $symb = shift;
 2686:     my $src = shift;
 2687: 
 2688:     $self->generate_email_discuss_status();
 2689: 
 2690:     if (!defined($self->{ERROR_MSG})) { return ""; }
 2691: 
 2692:     my $errors;
 2693:     if ($self->{ERROR_MSG}->{$symb}) {
 2694:         $errors = $self->{ERROR_MSG}->{$symb};
 2695:         if ($self->{ERROR_MSG}->{$src}) {
 2696:             $errors .= ','.$self->{ERROR_MSG}->{$src};
 2697:         }
 2698:     } else {
 2699:         if ($self->{ERROR_MSG}->{$src}) {
 2700:             $errors = $self->{ERROR_MSG}->{$src};
 2701:         }
 2702:     }
 2703:     return $errors;
 2704: }
 2705: 
 2706: =pod
 2707: 
 2708: =item * B<getById>(id):
 2709: 
 2710: Based on the ID of the resource (1.1, 3.2, etc.), get a resource
 2711: object for that resource. This method, or other methods that use it
 2712: (as in the resource object) is the only proper way to obtain a
 2713: resource object.
 2714: 
 2715: =item * B<getBySymb>(symb):
 2716: 
 2717: Based on the symb of the resource, get a resource object for that
 2718: resource. This is one of the proper ways to get a resource object.
 2719: 
 2720: =item * B<getByMapPc>(map_pc):
 2721: 
 2722: Based on the map_pc of the resource, get a resource object for
 2723: the given map. This is one of the proper ways to get a resource object.
 2724: 
 2725: =cut
 2726: 
 2727: # The strategy here is to cache the resource objects, and only construct them
 2728: # as we use them. The real point is to prevent reading any more from the tied
 2729: # hash than we have to, which should hopefully alleviate speed problems.
 2730: 
 2731: sub getById {
 2732:     my $self = shift;
 2733:     my $id = shift;
 2734: 
 2735:     if (defined ($self->{RESOURCE_CACHE}->{$id}))
 2736:     {
 2737:         return $self->{RESOURCE_CACHE}->{$id};
 2738:     }
 2739: 
 2740:     # resource handles inserting itself into cache.
 2741:     # Not clear why the quotes are necessary, but as of this
 2742:     # writing it doesn't work without them.
 2743:     return "Apache::lonnavmaps::resource"->new($self, $id);
 2744: }
 2745: 
 2746: sub getBySymb {
 2747:     my $self = shift;
 2748:     my $symb = shift;
 2749: 
 2750:     my ($mapUrl, $id, $filename) = &Apache::lonnet::decode_symb($symb);
 2751:     my $map = $self->getResourceByUrl($mapUrl);
 2752:     my $returnvalue = undef;
 2753:     if (ref($map)) {
 2754:         $returnvalue = $self->getById($map->map_pc() .'.'.$id);
 2755:     }
 2756:     return $returnvalue;
 2757: }
 2758: 
 2759: sub getByMapPc {
 2760:     my $self = shift;
 2761:     my $map_pc = shift;
 2762:     my $map_id = $self->{NAV_HASH}->{'map_id_' . $map_pc};
 2763:     $map_id = $self->{NAV_HASH}->{'ids_' . $map_id};
 2764:     return $self->getById($map_id);
 2765: }
 2766: 
 2767: =pod
 2768: 
 2769: =item * B<firstResource>():
 2770: 
 2771: Returns a resource object reference corresponding to the first
 2772: resource in the navmap.
 2773: 
 2774: =cut
 2775: 
 2776: sub firstResource {
 2777:     my $self = shift;
 2778:     my $firstResource = $self->navhash('map_start_' .
 2779:                      &Apache::lonnet::clutter($env{'request.course.uri'}));
 2780:     return $self->getById($firstResource);
 2781: }
 2782: 
 2783: =pod
 2784: 
 2785: =item * B<finishResource>():
 2786: 
 2787: Returns a resource object reference corresponding to the last resource
 2788: in the navmap.
 2789: 
 2790: =cut
 2791: 
 2792: sub finishResource {
 2793:     my $self = shift;
 2794:     my $firstResource = $self->navhash('map_finish_' .
 2795:                      &Apache::lonnet::clutter($env{'request.course.uri'}));
 2796:     return $self->getById($firstResource);
 2797: }
 2798: 
 2799: # Parmval reads the parm hash and cascades the lookups. parmval_real does
 2800: # the actual lookup; parmval caches the results.
 2801: sub parmval {
 2802:     my $self = shift;
 2803:     my ($what,$symb,$recurse)=@_;
 2804:     my $hashkey = $what."|||".$symb;
 2805:     my $cache = $self->{PARM_CACHE};
 2806:     if (defined($self->{PARM_CACHE}->{$hashkey})) {
 2807:         if (ref($self->{PARM_CACHE}->{$hashkey}) eq 'ARRAY') { 
 2808:             if (defined($self->{PARM_CACHE}->{$hashkey}->[0])) {
 2809:                 if (wantarray) {
 2810:                     return @{$self->{PARM_CACHE}->{$hashkey}};
 2811:                 } else {
 2812:                     return $self->{PARM_CACHE}->{$hashkey}->[0];
 2813:                 }
 2814:             }
 2815:         } else {
 2816:             return $self->{PARM_CACHE}->{$hashkey};
 2817:         }
 2818:     }
 2819:     my $result = $self->parmval_real($what, $symb, $recurse);
 2820:     $self->{PARM_CACHE}->{$hashkey} = $result;
 2821:     if (wantarray) {
 2822:         return @{$result};
 2823:     }
 2824:     return $result->[0];
 2825: }
 2826: 
 2827: 
 2828: sub parmval_real {
 2829:     my $self = shift;
 2830:     my ($what,$symb,$recurse) = @_;
 2831: 
 2832: 
 2833:     # Make sure the {USER_OPT} and {COURSE_OPT} hashes are populated
 2834:     $self->generate_course_user_opt();
 2835: 
 2836:     my $cid=$env{'request.course.id'};
 2837:     my $csec=$env{'request.course.sec'};
 2838:     my $cgroup='';
 2839:     my @cgrps=split(/:/,$env{'request.course.groups'});
 2840:     if (@cgrps > 0) {
 2841:         @cgrps = sort(@cgrps);
 2842:         $cgroup = $cgrps[0];
 2843:     } 
 2844:     my $uname=$self->{USERNAME};
 2845:     my $udom=$self->{DOMAIN};
 2846: 
 2847:     unless ($symb) { return ['']; }
 2848:     my $result='';
 2849: 
 2850:     my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
 2851:     $mapname = &Apache::lonnet::deversion($mapname);
 2852: # ----------------------------------------------------- Cascading lookup scheme
 2853:     my $rwhat=$what;
 2854:     $what=~s/^parameter\_//;
 2855:     $what=~s/\_/\./;
 2856: 
 2857:     my $symbparm=$symb.'.'.$what;
 2858:     my $mapparm=$mapname.'___(all).'.$what;
 2859:     my $usercourseprefix=$cid;
 2860: 
 2861: 
 2862: 
 2863:     my $grplevel=$usercourseprefix.'.['.$cgroup.'].'.$what;
 2864:     my $grplevelr=$usercourseprefix.'.['.$cgroup.'].'.$symbparm;
 2865:     my $grplevelm=$usercourseprefix.'.['.$cgroup.'].'.$mapparm;
 2866: 
 2867: 
 2868:     my $seclevel= $usercourseprefix.'.['.$csec.'].'.$what;
 2869:     my $seclevelr=$usercourseprefix.'.['.$csec.'].'.$symbparm;
 2870:     my $seclevelm=$usercourseprefix.'.['.$csec.'].'.$mapparm;
 2871: 
 2872: 
 2873:     my $courselevel= $usercourseprefix.'.'.$what;
 2874:     my $courselevelr=$usercourseprefix.'.'.$symbparm;
 2875:     my $courselevelm=$usercourseprefix.'.'.$mapparm;
 2876: 
 2877: 
 2878:     my $useropt = $self->{USER_OPT};
 2879:     my $courseopt = $self->{COURSE_OPT};
 2880:     my $parmhash = $self->{PARM_HASH};
 2881: 
 2882: # ---------------------------------------------------------- first, check user
 2883:     if ($uname and defined($useropt)) {
 2884:         if (defined($$useropt{$courselevelr})) { return [$$useropt{$courselevelr},'resource']; }
 2885:         if (defined($$useropt{$courselevelm})) { return [$$useropt{$courselevelm},'map']; }
 2886:         if (defined($$useropt{$courselevel})) { return [$$useropt{$courselevel},'course']; }
 2887:     }
 2888: 
 2889: # ------------------------------------------------------- second, check course
 2890:     if ($cgroup ne '' and defined($courseopt)) {
 2891:         if (defined($$courseopt{$grplevelr})) { return [$$courseopt{$grplevelr},'resource']; }
 2892:         if (defined($$courseopt{$grplevelm})) { return [$$courseopt{$grplevelm},'map']; }
 2893:         if (defined($$courseopt{$grplevel})) { return [$$courseopt{$grplevel},'course']; }
 2894:     }
 2895: 
 2896:     if ($csec and defined($courseopt)) {
 2897:         if (defined($$courseopt{$seclevelr})) { return [$$courseopt{$seclevelr},'resource']; }
 2898:         if (defined($$courseopt{$seclevelm})) { return [$$courseopt{$seclevelm},'map']; }
 2899:         if (defined($$courseopt{$seclevel})) { return [$$courseopt{$seclevel},'course']; }
 2900:     }
 2901: 
 2902:     if (defined($courseopt)) {
 2903:         if (defined($$courseopt{$courselevelr})) { return [$$courseopt{$courselevelr},'resource']; }
 2904:     }
 2905: 
 2906: # ----------------------------------------------------- third, check map parms
 2907: 
 2908:     my $thisparm=$$parmhash{$symbparm};
 2909:     if (defined($thisparm)) { return [$thisparm,'map']; }
 2910: 
 2911: # ----------------------------------------------------- fourth , check default
 2912: 
 2913:     my $meta_rwhat=$rwhat;
 2914:     $meta_rwhat=~s/\./_/g;
 2915:     my $default=&Apache::lonnet::metadata($fn,$meta_rwhat);
 2916:     if (defined($default)) { return [$default,'resource']}
 2917:     $default=&Apache::lonnet::metadata($fn,'parameter_'.$meta_rwhat);
 2918:     if (defined($default)) { return [$default,'resource']}
 2919: # --------------------------------------------------- fifth, check more course
 2920:     if (defined($courseopt)) {
 2921:         if (defined($$courseopt{$courselevelm})) { return [$$courseopt{$courselevelm},'map']; }
 2922:         if (defined($$courseopt{$courselevel})) {
 2923:            my $ret = [$$courseopt{$courselevel},'course'];
 2924:            return $ret;
 2925:        }
 2926:     }
 2927: # --------------------------------------------------- sixth , cascade up parts
 2928: 
 2929:     my ($space,@qualifier)=split(/\./,$rwhat);
 2930:     my $qualifier=join('.',@qualifier);
 2931:     unless ($space eq '0') {
 2932: 	my @parts=split(/_/,$space);
 2933: 	my $id=pop(@parts);
 2934: 	my $part=join('_',@parts);
 2935: 	if ($part eq '') { $part='0'; }
 2936:        my @partgeneral=$self->parmval($part.".$qualifier",$symb,1);
 2937:        if (defined($partgeneral[0])) { return \@partgeneral; }
 2938:     }
 2939:     if ($recurse) { return []; }
 2940:     my $pack_def=&Apache::lonnet::packages_tab_default($fn,'resource.'.$rwhat);
 2941:     if (defined($pack_def)) { return [$pack_def,'resource']; }
 2942:     return [''];
 2943: }
 2944: 
 2945: sub recurseup_maps {
 2946:     my ($self,$mapname) = @_;
 2947:     my @recurseup;
 2948:     if ($mapname) {
 2949:         my $res = $self->getResourceByUrl($mapname);
 2950:         if (ref($res)) {
 2951:             my @pcs = split(/,/,$res->map_hierarchy());
 2952:             shift(@pcs);
 2953:             if (@pcs) {
 2954:                 @recurseup = map { &Apache::lonnet::declutter($self->getByMapPc($_)->src()); } reverse(@pcs);
 2955:             }
 2956:         }
 2957:     }
 2958:     return @recurseup;
 2959: }
 2960: 
 2961: sub recursed_crumbs {
 2962:     my ($self,$mapurl,$restitle) = @_;
 2963:     my (@revmapinfo,@revmapres);
 2964:     my $mapres = $self->getResourceByUrl($mapurl);
 2965:     if (ref($mapres)) {
 2966:         @revmapres = map { $self->getByMapPc($_); } split(/,/,$mapres->map_breadcrumbs());
 2967:         shift(@revmapres);
 2968:     }
 2969:     my $allowedlength = 60;
 2970:     my $minlength = 5;
 2971:     my $allowedtitle = 30;
 2972:     if (($env{'environment.icons'} eq 'iconsonly') && (!$env{'browser.mobile'})) {
 2973:         $allowedlength = 100;
 2974:         $allowedtitle = 70;
 2975:     }
 2976:     if (length($restitle) > $allowedtitle) {
 2977:         $restitle = &truncate_crumb_text($restitle,$allowedtitle);
 2978:     }
 2979:     my $totallength = length($restitle);
 2980:     my @links;
 2981: 
 2982:     foreach my $map (@revmapres) {
 2983:         my $pc = $map->map_pc();
 2984:         next if ((!$pc) || ($pc == 1));
 2985:         push(@links,$map);
 2986:         my $text = $map->title();
 2987:         if ($text eq '') {
 2988:             $text = '...';
 2989:         }
 2990:         push(@revmapinfo,{'href' => $env{'request.use_absolute'}.$map->link().'?navmap=1','text' => $text,'no_mt' => 1,});
 2991:         $totallength += length($text);
 2992:     }
 2993:     my $numlinks = scalar(@links);
 2994:     if ($numlinks) {
 2995:         if ($totallength - $allowedlength > 0) {
 2996:             my $available = $allowedlength - length($restitle);
 2997:             my $avg = POSIX::ceil($available/$numlinks);
 2998:             if ($avg < $minlength) {
 2999:                 $avg = $minlength;
 3000:             }
 3001:             @revmapinfo = ();
 3002:             foreach my $map (@links) {
 3003:                 my $title = $map->title();
 3004:                 if ($title eq '') {
 3005:                     $title = '...';
 3006:                 }
 3007:                 my $showntitle = &truncate_crumb_text($title,$avg);
 3008:                 if ($showntitle ne '') {
 3009:                     push(@revmapinfo,{'href' => $env{'request.use_absolute'}.$map->link().'?navmap=1','text' => $showntitle,'no_mt' => 1,});
 3010:                 }
 3011:             }
 3012:         }
 3013:     }
 3014:     if ($restitle ne '') {
 3015:         push(@revmapinfo,{'text' => $restitle, 'no_mt' => 1});
 3016:     }
 3017:     return @revmapinfo;
 3018: }
 3019: 
 3020: sub truncate_crumb_text {
 3021:     my ($title,$limit) = @_;
 3022:     my $showntitle = '';
 3023:     if (length($title) > $limit) {
 3024:         my @words = split(/\b\s*/,$title);
 3025:         if (@words == 1) {
 3026:             $showntitle = substr($title,0,$limit).' ...';
 3027:         } else {
 3028:             my $linklength = 0;
 3029:             my $num = 0;
 3030:             foreach my $word (@words) {
 3031:                 $linklength += 1+length($word);
 3032:                 if ($word eq '-') {
 3033:                     $showntitle =~ s/ $//;
 3034:                     $showntitle .= $word;
 3035:                 } elsif ($linklength > $limit) {
 3036:                     if ($num < @words) {
 3037:                         $showntitle .= $word.' ...';
 3038:                         last;
 3039:                     } else {
 3040:                         $showntitle .= $word;
 3041:                     }
 3042:                 } else {
 3043:                     $showntitle .= $word.' ';
 3044:                 }
 3045:             }
 3046:             $showntitle =~ s/ $//;
 3047:         }
 3048:         return $showntitle;
 3049:     } else {
 3050:         return $title;
 3051:     }
 3052: }
 3053: 
 3054: #
 3055: #  Determines the open/close dates for printing a map that
 3056: #  encloses a resource.
 3057: #
 3058: sub map_printdates {
 3059:     my ($self, $res, $part) = @_;
 3060: 
 3061: 
 3062: 
 3063: 
 3064: 
 3065:     my $opendate = $self->get_mapparam($res->symb(),'',"$part.printstartdate");
 3066:     my $closedate= $self->get_mapparam($res->symb(),'', "$part.printenddate");
 3067: 
 3068: 
 3069:     return ($opendate, $closedate);
 3070: }
 3071: 
 3072: sub get_mapparam {
 3073:     my ($self, $symb, $mapname, $what) = @_;
 3074: 
 3075:     # Ensure the course option hash is populated:
 3076: 
 3077:     $self->generate_course_user_opt();
 3078: 
 3079:     # Get the course id and section if there is one.
 3080: 
 3081:     my $cid=$env{'request.course.id'};
 3082:     my $csec=$env{'request.course.sec'};
 3083:     my $cgroup='';
 3084:     my @cgrps=split(/:/,$env{'request.course.groups'});
 3085:     if (@cgrps > 0) {
 3086:         @cgrps = sort(@cgrps);
 3087:         $cgroup = $cgrps[0];
 3088:     } 
 3089:     my $uname=$self->{USERNAME};
 3090:     my $udom=$self->{DOMAIN};
 3091: 
 3092:     unless ($symb || $mapname) { return; }
 3093:     my $result='';
 3094:     my ($recursed,@recurseup);
 3095: 
 3096:     # Figure out which map we are in.
 3097: 
 3098:     if ($symb && !$mapname) {
 3099:         my ($id,$fn);
 3100:         ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
 3101:         $mapname = &Apache::lonnet::deversion($mapname);
 3102:     }
 3103: 
 3104:     my $rwhat=$what;
 3105:     $what=~s/^parameter\_//;
 3106:     $what=~s/\_/\./;
 3107: 
 3108:     # Build the hash keys for the lookup:
 3109: 
 3110:     my $symbparm=$symb.'.'.$what;
 3111:     my $mapparm=$mapname.'___(all).'.$what;
 3112:     my $usercourseprefix=$cid;
 3113: 
 3114: 
 3115:     my $grplevel    = "$usercourseprefix.[$cgroup].$mapparm";
 3116:     my $seclevel    = "$usercourseprefix.[$csec].$mapparm";
 3117:     my $courselevel = "$usercourseprefix.$mapparm";
 3118: 
 3119: 
 3120:     # Get handy references to the hashes we need in $self:
 3121: 
 3122:     my $useropt = $self->{USER_OPT};
 3123:     my $courseopt = $self->{COURSE_OPT};
 3124:     my $parmhash = $self->{PARM_HASH};
 3125: 
 3126:     # Check per user 
 3127: 
 3128: 
 3129: 
 3130:     if ($uname and defined($useropt)) {
 3131: 	if (defined($$useropt{$courselevel})) {
 3132: 	    return $$useropt{$courselevel};
 3133: 	}
 3134:         if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3135:             unless ($recursed) {
 3136:                 @recurseup = $self->recurseup_maps($mapname);
 3137:                 $recursed = 1;
 3138:             }
 3139:             foreach my $item (@recurseup) {
 3140:                 my $norecursechk=$usercourseprefix.'.'.$item.'___(all).'.$what;
 3141:                 if (defined($$useropt{$norecursechk})) {
 3142:                     if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3143:                         return $$useropt{$norecursechk};
 3144:                     }
 3145:                 }
 3146:             }
 3147:         }
 3148:     }
 3149: 
 3150:     # Check course -- group
 3151: 
 3152: 
 3153: 
 3154:     if ($cgroup ne '' and defined ($courseopt)) {
 3155: 	if (defined($$courseopt{$grplevel})) {
 3156: 	    return $$courseopt{$grplevel};
 3157: 	}
 3158:         if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3159:             unless ($recursed) {
 3160:                 @recurseup = $self->recurseup_maps($mapname);
 3161:                 $recursed = 1;
 3162:             }
 3163:             foreach my $item (@recurseup) {
 3164:                 my $norecursechk=$usercourseprefix.'.['.$cgroup.'].'.$item.'___(all).'.$what;
 3165:                 if (defined($$courseopt{$norecursechk})) {
 3166:                     if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3167:                         return $$courseopt{$norecursechk};
 3168:                     }
 3169:                 }
 3170:             }
 3171:         }
 3172:     }
 3173: 
 3174:     # Check course -- section
 3175: 
 3176: 
 3177: 
 3178: 
 3179: 
 3180:     if ($csec and defined($courseopt)) {
 3181: 	if (defined($$courseopt{$seclevel})) {
 3182: 	    return $$courseopt{$seclevel};
 3183: 	}
 3184:         if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3185:             unless ($recursed) {
 3186:                 @recurseup = $self->recurseup_maps($mapname);
 3187:                 $recursed = 1;
 3188:             }
 3189:             foreach my $item (@recurseup) {
 3190:                 my $norecursechk=$usercourseprefix.'.['.$csec.'].'.$item.'___(all).'.$what;
 3191:                 if (defined($$courseopt{$norecursechk})) {
 3192:                     if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3193:                         return $$courseopt{$norecursechk};
 3194:                     }
 3195:                 }
 3196:             }
 3197:         }
 3198:     }
 3199:     # Check the map parameters themselves:
 3200: 
 3201:     if ($symb) {
 3202:         my $symbparm=$symb.'.'.$what;
 3203:         my $thisparm = $$parmhash{$symbparm};
 3204:         if (defined($thisparm)) {
 3205:             return $thisparm;
 3206:         }
 3207:     }
 3208: 
 3209: 
 3210:     # Additional course parameters:
 3211: 
 3212:     if (defined($courseopt)) {
 3213: 	if (defined($$courseopt{$courselevel})) {
 3214: 	    return $$courseopt{$courselevel};
 3215: 	}
 3216:         if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3217:             unless ($recursed) {
 3218:                 @recurseup = $self->recurseup_maps($mapname);
 3219:                 $recursed = 1;
 3220:             }
 3221:             foreach my $item (@recurseup) {
 3222:                 my $norecursechk=$usercourseprefix.'.'.$item.'___(all).'.$what;
 3223:                 if (defined($$courseopt{$norecursechk})) {
 3224:                     if ($what =~ /\.(encrypturl|hiddenresource)$/) {
 3225:                         return $$courseopt{$norecursechk};
 3226:                     }
 3227:                 }
 3228:             }
 3229:         }
 3230:     }
 3231:     return undef;		# Unefined if we got here.
 3232: }
 3233: 
 3234: sub course_printdates {
 3235:     my ($self, $symb,  $part) = @_;
 3236: 
 3237: 
 3238:     my $opendate  = $self->getcourseparam($symb, $part . '.printstartdate');
 3239:     my $closedate = $self->getcourseparam($symb, $part . '.printenddate');
 3240:     return ($opendate, $closedate);
 3241: 
 3242: }
 3243: 
 3244: sub getcourseparam {
 3245:     my ($self, $symb, $what) = @_;
 3246: 
 3247:     $self->generate_course_user_opt(); # If necessary populate the hashes.
 3248: 
 3249:     my $uname = $self->{USERNAME};
 3250:     my $udom  = $self->{DOMAIN};
 3251:     
 3252:     # Course, section, group ids come from the env:
 3253: 
 3254:     my $cid   = $env{'request.course.id'};
 3255:     my $csec  = $env{'request.course.sec'};
 3256:     my $cgroup = '';		# Assume no group
 3257: 
 3258:     my @cgroups = split(/:/, $env{'request.course.groups'});
 3259:     if(@cgroups > 0) {
 3260: 	@cgroups = sort(@cgroups);
 3261: 	$cgroup  = $cgroups[0];	# There is a course group. 
 3262:    }
 3263:     my ($mapname,$id,$fn)=&Apache::lonnet::decode_symb($symb);
 3264:     $mapname = &Apache::lonnet::deversion($mapname);
 3265: 
 3266:     #
 3267:     # Make the various lookup keys:
 3268:     #
 3269: 
 3270:     $what=~s/^parameter\_//;
 3271:     $what=~s/\_/\./;
 3272: 
 3273: 
 3274:     my $symbparm = $symb . '.' . $what;
 3275:     my $mapparm=$mapname.'___(all).'.$what;
 3276: 
 3277:     # Local refs to the hashes we're going to look at:
 3278: 
 3279:     my $useropt   = $self->{USER_OPT};
 3280:     my $courseopt = $self->{COURSE_OPT};
 3281: 
 3282:     # 
 3283:     # We want the course level stuff from the way
 3284:     # parmval_real operates 
 3285:     # TODO: Factor some of this stuff out of
 3286:     # both parmval_real and here
 3287:     #
 3288:     my $courselevel = $cid . '.' .  $what;
 3289:     my $grplevel    = $cid . '.[' . $cgroup   . ']' . $what;
 3290:     my $seclevel    = $cid . '.[' . $csec     . ']' . $what;
 3291: 
 3292: 
 3293:     # Try for the user's course level option:
 3294: 
 3295:     if ($uname and defined($useropt)) {
 3296: 	if (defined($$useropt{$courselevel})) {
 3297: 	    return $$useropt{$courselevel};
 3298: 	}
 3299:     }
 3300:     # Try for the group's course level option:
 3301: 
 3302:     if ($cgroup ne '' and defined($courseopt)) {
 3303: 	if (defined($$courseopt{$grplevel})) {
 3304: 	    return $$courseopt{$grplevel};
 3305: 	}
 3306:     }
 3307: 
 3308:     #  Try for section level parameters:
 3309: 
 3310:     if ($csec ne '' and defined($courseopt)) {
 3311: 	if (defined($$courseopt{$seclevel})) {
 3312: 	    return $$courseopt{$seclevel};
 3313: 	}
 3314:     }
 3315:     # Try for 'additional' course parameters:
 3316: 
 3317:     if (defined($courseopt)) {
 3318: 	if (defined($$courseopt{$courselevel})) {
 3319: 	    return $$courseopt{$courselevel};
 3320: 	}
 3321:     }
 3322:     return undef;
 3323: 
 3324: }
 3325: 
 3326: 
 3327: =pod
 3328: 
 3329: =item * B<getResourceByUrl>(url,multiple):
 3330: 
 3331: Retrieves a resource object by URL of the resource, unless the optional
 3332: multiple parameter is included in which case an array of resource 
 3333: objects is returned. If passed a resource object, it will simply return  
 3334: it, so it is safe to use this method in code like
 3335: "$res = $navmap->getResourceByUrl($res)"
 3336: if you're not sure if $res is already an object, or just a URL. If the
 3337: resource appears multiple times in the course, only the first instance 
 3338: will be returned (useful for maps), unless the multiple parameter has
 3339: been included, in which case all instances are returned in an array.
 3340: 
 3341: =item * B<retrieveResources>(map, filterFunc, recursive, bailout, showall, noblockcheck):
 3342: 
 3343: The map is a specification of a map to retreive the resources from,
 3344: either as a url or as an object. The filterFunc is a reference to a
 3345: function that takes a resource object as its one argument and returns
 3346: true if the resource should be included, or false if it should not
 3347: be. If recursive is true, the map will be recursively examined,
 3348: otherwise it will not be. If bailout is true, the function will return
 3349: as soon as it finds a resource, if false it will finish. If showall is
 3350: true it will not hide maps that contain nothing but one other map. The 
 3351: noblockcheck arg is propagated to become the sixth arg in the call to
 3352: lonnet::allowed when checking a resource's availability during collection
 3353: of resources using the iterator. noblockcheck needs to be true if 
 3354: retrieveResources() was called by a routine that itself was called by 
 3355: lonnet::allowed, in order to avoid recursion.  By default the map  
 3356: is the top-level map of the course, filterFunc is a function that 
 3357: always returns 1, recursive is true, bailout is false, showall is
 3358: false. The resources will be returned in a list containing the
 3359: resource objects for the corresponding resources, with B<no structure 
 3360: information> in the list; regardless of branching, recursion, etc.,
 3361: it will be a flat list.
 3362: 
 3363: Thus, this is suitable for cases where you don't want the structure,
 3364: just a list of all resources. It is also suitable for finding out how
 3365: many resources match a given description; for this use, if all you
 3366: want to know is if I<any> resources match the description, the bailout
 3367: parameter will allow you to avoid potentially expensive enumeration of
 3368: all matching resources.
 3369: 
 3370: =item * B<hasResource>(map, filterFunc, recursive, showall):
 3371: 
 3372: Convenience method for
 3373: 
 3374:  scalar(retrieveResources($map, $filterFunc, $recursive, 1, $showall)) > 0
 3375: 
 3376: which will tell whether the map has resources matching the description
 3377: in the filter function.
 3378: 
 3379: =item * B<usedVersion>(url):
 3380: 
 3381: Retrieves version infomation for a url. Returns the version (a number, or 
 3382: the string "mostrecent") for resources which have version information in  
 3383: the big hash.
 3384: 
 3385: =cut
 3386: 
 3387: 
 3388: sub getResourceByUrl {
 3389:     my $self = shift;
 3390:     my $resUrl = shift;
 3391:     my $multiple = shift;
 3392: 
 3393:     if (ref($resUrl)) { return $resUrl; }
 3394: 
 3395:     $resUrl = &Apache::lonnet::clutter($resUrl);
 3396:     my $resId = $self->{NAV_HASH}->{'ids_' . $resUrl};
 3397:     if (!$resId) { return ''; }
 3398:     if ($multiple) {
 3399:         my @resources = ();
 3400:         my @resIds = split (/,/, $resId);
 3401:         foreach my $id (@resIds) {
 3402:             my $resourceId = $self->getById($id);
 3403:             if ($resourceId) { 
 3404:                 push(@resources,$resourceId);
 3405:             }
 3406:         }
 3407:         return @resources;
 3408:     } else {
 3409:         if ($resId =~ /,/) {
 3410:             $resId = (split (/,/, $resId))[0];
 3411:         }
 3412:         return $self->getById($resId);
 3413:     }
 3414: }
 3415: 
 3416: sub retrieveResources {
 3417:     my $self = shift;
 3418:     my $map = shift;
 3419:     my $filterFunc = shift;
 3420:     if (!defined ($filterFunc)) {
 3421:         $filterFunc = sub {return 1;};
 3422:     }
 3423:     my $recursive = shift;
 3424:     if (!defined($recursive)) { $recursive = 1; }
 3425:     my $bailout = shift;
 3426:     if (!defined($bailout)) { $bailout = 0; }
 3427:     my $showall = shift;
 3428:     my $noblockcheck = shift;
 3429:     # Create the necessary iterator.
 3430:     if (!ref($map)) { # assume it's a url of a map.
 3431:         $map = $self->getResourceByUrl($map);
 3432:     }
 3433: 
 3434:     # If nothing was passed, assume top-level map
 3435:     if (!$map) {
 3436: 	$map = $self->getById('0.0');
 3437:     }
 3438: 
 3439:     # Check the map's validity.
 3440:     if (!$map->is_map()) {
 3441:         # Oh, to throw an exception.... how I'd love that!
 3442:         return ();
 3443:     }
 3444: 
 3445:     # Get an iterator.
 3446:     my $it = $self->getIterator($map->map_start(), $map->map_finish(),
 3447:                                 undef, $recursive, $showall);
 3448: 
 3449:     my @resources = ();
 3450: 
 3451:     if (&$filterFunc($map)) {
 3452: 	push(@resources, $map);
 3453:     }
 3454: 
 3455:     # Run down the iterator and collect the resources.
 3456:     my $curRes;
 3457: 
 3458:     while ($curRes = $it->next(undef,$noblockcheck)) {
 3459:         if (ref($curRes)) {
 3460:             if (!&$filterFunc($curRes)) {
 3461:                 next;
 3462:             }
 3463: 
 3464:             push(@resources, $curRes);
 3465: 
 3466:             if ($bailout) {
 3467:                 return @resources;
 3468:             }
 3469:         }
 3470: 
 3471:     }
 3472: 
 3473:     return @resources;
 3474: }
 3475: 
 3476: sub hasResource {
 3477:     my $self = shift;
 3478:     my $map = shift;
 3479:     my $filterFunc = shift;
 3480:     my $recursive = shift;
 3481:     my $showall = shift;
 3482:     
 3483:     return scalar($self->retrieveResources($map, $filterFunc, $recursive, 1, $showall)) > 0;
 3484: }
 3485: 
 3486: sub usedVersion {
 3487:     my $self = shift;
 3488:     my $linkurl = shift;
 3489:     return $self->navhash("version_$linkurl");
 3490: }
 3491: 
 3492: 1;
 3493: 
 3494: package Apache::lonnavmaps::iterator;
 3495: use Scalar::Util qw(weaken);
 3496: use Apache::lonnet;
 3497: 
 3498: =pod
 3499: 
 3500: =back
 3501: 
 3502: =head1 Object: navmap Iterator
 3503: 
 3504: An I<iterator> encapsulates the logic required to traverse a data
 3505: structure. navmap uses an iterator to traverse the course map
 3506: according to the criteria you wish to use.
 3507: 
 3508: To obtain an iterator, call the B<getIterator>() function of a
 3509: B<navmap> object. (Do not instantiate Apache::lonnavmaps::iterator
 3510: directly.) This will return a reference to the iterator:
 3511: 
 3512: C<my $resourceIterator = $navmap-E<gt>getIterator();>
 3513: 
 3514: To get the next thing from the iterator, call B<next>:
 3515: 
 3516: C<my $nextThing = $resourceIterator-E<gt>next()>
 3517: 
 3518: getIterator behaves as follows:
 3519: 
 3520: =over 4
 3521: 
 3522: =item * B<getIterator>(firstResource, finishResource, filterHash, condition, forceTop, returnTopMap, $deeplinklisted):
 3523: 
 3524: All parameters are optional. firstResource is a resource reference
 3525: corresponding to where the iterator should start. It defaults to
 3526: navmap->firstResource() for the corresponding nav map. finishResource
 3527: corresponds to where you want the iterator to end, defaulting to
 3528: navmap->finishResource(). filterHash is a hash used as a set
 3529: containing strings representing the resource IDs, defaulting to
 3530: empty. Condition is a 1 or 0 that sets what to do with the filter
 3531: hash: If a 0, then only resources that exist IN the filterHash will be
 3532: recursed on. If it is a 1, only resources NOT in the filterHash will
 3533: be recursed on. Defaults to 0. forceTop is a boolean value. If it is
 3534: false (default), the iterator will only return the first level of map
 3535: that is not just a single, 'redirecting' map. If true, the iterator
 3536: will return all information, starting with the top-level map,
 3537: regardless of content. returnTopMap, if true (default false), will
 3538: cause the iterator to return the top-level map object (resource 0.0)
 3539: before anything else. deeplinklisted if true (default false), will
 3540: check "listed" status of a resource with a deeplink, and unless "absent"
 3541: will exclude deeplink checking when retrieving the browsePriv from
 3542: lonnet::allowed().
 3543: 
 3544: Thus, by default, only top-level resources will be shown. Change the
 3545: condition to a 1 without changing the hash, and all resources will be
 3546: shown. Changing the condition to 1 and including some values in the
 3547: hash will allow you to selectively suppress parts of the navmap, while
 3548: leaving it on 0 and adding things to the hash will allow you to
 3549: selectively add parts of the nav map. See the handler code for
 3550: examples.
 3551: 
 3552: The iterator will return either a reference to a resource object, or a
 3553: token representing something in the map, such as the beginning of a
 3554: new branch. The possible tokens are:
 3555: 
 3556: =over 4
 3557: 
 3558: =item * B<END_ITERATOR>:
 3559: 
 3560: The iterator has returned all that it's going to. Further calls to the
 3561: iterator will just produce more of these. This is a "false" value, and
 3562: is the only false value the iterator which will be returned, so it can
 3563: be used as a loop sentinel.
 3564: 
 3565: =item * B<BEGIN_MAP>:
 3566: 
 3567: A new map is being recursed into. This is returned I<after> the map
 3568: resource itself is returned.
 3569: 
 3570: =item * B<END_MAP>:
 3571: 
 3572: The map is now done.
 3573: 
 3574: =item * B<BEGIN_BRANCH>:
 3575: 
 3576: A branch is now starting. The next resource returned will be the first
 3577: in that branch.
 3578: 
 3579: =item * B<END_BRANCH>:
 3580: 
 3581: The branch is now done.
 3582: 
 3583: =back
 3584: 
 3585: The tokens are retreivable via methods on the iterator object, i.e.,
 3586: $iterator->END_MAP.
 3587: 
 3588: Maps can contain empty resources. The iterator will automatically skip
 3589: over such resources, but will still treat the structure
 3590: correctly. Thus, a complicated map with several branches, but
 3591: consisting entirely of empty resources except for one beginning or
 3592: ending resource, will cause a lot of BRANCH_STARTs and BRANCH_ENDs,
 3593: but only one resource will be returned.
 3594: 
 3595: =back
 3596: 
 3597: =head2 Normal Usage
 3598: 
 3599: Normal usage of the iterator object is to do the following:
 3600: 
 3601:  my $it = $navmap->getIterator([your params here]);
 3602:  my $curRes;
 3603:  while ($curRes = $it->next()) {
 3604:    [your logic here]
 3605:  }
 3606: 
 3607: Note that inside of the loop, it's frequently useful to check if
 3608: "$curRes" is a reference or not with the reference function; only
 3609: resource objects will be references, and any non-references will 
 3610: be the tokens described above.
 3611: 
 3612: The next() routine can take two (optional) arguments:
 3613: closeAllPages - if true will not recurse down a .page
 3614: noblockcheck - passed to browsePriv() for passing as sixth arg to
 3615: call to lonnet::allowed. This needs to be set if retrieveResources
 3616: was already called from another routine called within lonnet::allowed, 
 3617: so as to prevent recursion.
 3618: 
 3619: Also note there is some old code floating around that tries to track
 3620: the depth of the iterator to see when it's done; do not copy that 
 3621: code. It is difficult to get right and harder to understand than
 3622: this. They should be migrated to this new style.
 3623: 
 3624: =cut
 3625: 
 3626: # Here are the tokens for the iterator:
 3627: 
 3628: sub END_ITERATOR { return 0; }
 3629: sub BEGIN_MAP { return 1; }    # begining of a new map
 3630: sub END_MAP { return 2; }      # end of the map
 3631: sub BEGIN_BRANCH { return 3; } # beginning of a branch
 3632: sub END_BRANCH { return 4; }   # end of a branch
 3633: sub FORWARD { return 1; }      # go forward
 3634: sub BACKWARD { return 2; }
 3635: 
 3636: sub min {
 3637:     (my $a, my $b) = @_;
 3638:     if ($a < $b) { return $a; } else { return $b; }
 3639: }
 3640: 
 3641: sub new {
 3642:     # magic invocation to create a class instance
 3643:     my $proto = shift;
 3644:     my $class = ref($proto) || $proto;
 3645:     my $self = {};
 3646: 
 3647:     weaken($self->{NAV_MAP} = shift);
 3648:     return undef unless ($self->{NAV_MAP});
 3649: 
 3650:     $self->{USERNAME} = $self->{NAV_MAP}->{USERNAME};
 3651:     $self->{DOMAIN}   = $self->{NAV_MAP}->{DOMAIN};
 3652: 
 3653:     # Handle the parameters
 3654:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
 3655:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
 3656: 
 3657:     # If the given resources are just the ID of the resource, get the
 3658:     # objects
 3659:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
 3660:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
 3661:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
 3662:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
 3663: 
 3664:     $self->{FILTER} = shift;
 3665: 
 3666:     # A hash, used as a set, of resource already seen
 3667:     $self->{ALREADY_SEEN} = shift;
 3668:     if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
 3669:     $self->{CONDITION} = shift;
 3670: 
 3671:     # Do we want to automatically follow "redirection" maps?
 3672:     $self->{FORCE_TOP} = shift;
 3673: 
 3674:     # Do we want to return the top-level map object (resource 0.0)?
 3675:     $self->{RETURN_0} = shift;
 3676:     # have we done that yet?
 3677:     $self->{HAVE_RETURNED_0} = 0;
 3678: 
 3679:     # Do we want to check the "listed" status for a resource for which
 3680:     # deeplinking applies.
 3681:     $self->{DEEPLINKLISTED} = shift;
 3682: 
 3683:     # Now, we need to pre-process the map, by walking forward and backward
 3684:     # over the parts of the map we're going to look at.
 3685: 
 3686:     # The processing steps are exactly the same, except for a few small 
 3687:     # changes, so I bundle those up in the following list of two elements:
 3688:     # (direction_to_iterate, VAL_name, next_resource_method_to_call,
 3689:     # first_resource).
 3690:     # This prevents writing nearly-identical code twice.
 3691:     my @iterations = ( [FORWARD(), 'TOP_DOWN_VAL', 'getNext', 
 3692:                         'FIRST_RESOURCE'],
 3693:                        [BACKWARD(), 'BOT_UP_VAL', 'getPrevious', 
 3694:                         'FINISH_RESOURCE'] );
 3695: 
 3696:     my $maxDepth = 0; # tracks max depth
 3697: 
 3698:     # If there is only one resource in this map, and it's a map, we
 3699:     # want to remember that, so the user can ask for the first map
 3700:     # that isn't just a redirector.
 3701:     my $resource; my $resourceCount = 0;
 3702: 
 3703:     # Documentation on this algorithm can be found in the CVS repository at 
 3704:     # /docs/lonnavdocs; these "**#**" markers correspond to documentation
 3705:     # in that file.
 3706:     # **1**
 3707: 
 3708:     foreach my $pass (@iterations) {
 3709:         my $direction = $pass->[0];
 3710:         my $valName = $pass->[1];
 3711:         my $nextResourceMethod = $pass->[2];
 3712:         my $firstResourceName = $pass->[3];
 3713: 
 3714:         my $iterator = Apache::lonnavmaps::DFSiterator->new($self->{NAV_MAP}, 
 3715:                                                             $self->{FIRST_RESOURCE},
 3716:                                                             $self->{FINISH_RESOURCE},
 3717:                                                             {}, undef, 0, $direction);
 3718:     
 3719:         # prime the recursion
 3720:         $self->{$firstResourceName}->{DATA}->{$valName} = 0;
 3721: 	$iterator->next();
 3722:         my $curRes = $iterator->next();
 3723: 	my $depth = 1;
 3724:         while ($depth > 0) {
 3725: 	    if ($curRes == $iterator->BEGIN_MAP()) { $depth++; }
 3726: 	    if ($curRes == $iterator->END_MAP()) { $depth--; }
 3727: 
 3728:             if (ref($curRes)) {
 3729:                 # If there's only one resource, this will save it
 3730:                 # we have to filter empty resources from consideration here,
 3731:                 # or even "empty", redirecting maps have two (start & finish)
 3732:                 # or three (start, finish, plus redirector)
 3733:                 if($direction == FORWARD && $curRes->src()) { 
 3734:                     $resource = $curRes; $resourceCount++; 
 3735:                 }
 3736:                 my $resultingVal = $curRes->{DATA}->{$valName};
 3737:                 my $nextResources = $curRes->$nextResourceMethod();
 3738:                 my $nextCount = scalar(@{$nextResources});
 3739: 
 3740:                 if ($nextCount == 1) { # **3**
 3741:                     my $current = $nextResources->[0]->{DATA}->{$valName} || 999999999;
 3742:                     $nextResources->[0]->{DATA}->{$valName} = min($resultingVal, $current);
 3743:                 }
 3744:                 
 3745:                 if ($nextCount > 1) { # **4**
 3746:                     foreach my $res (@{$nextResources}) {
 3747:                         my $current = $res->{DATA}->{$valName} || 999999999;
 3748:                         $res->{DATA}->{$valName} = min($current, $resultingVal + 1);
 3749:                     }
 3750:                 }
 3751:             }
 3752:             
 3753:             # Assign the final val (**2**)
 3754:             if (ref($curRes) && $direction == BACKWARD()) {
 3755:                 my $finalDepth = min($curRes->{DATA}->{TOP_DOWN_VAL},
 3756:                                      $curRes->{DATA}->{BOT_UP_VAL});
 3757:                 
 3758:                 $curRes->{DATA}->{DISPLAY_DEPTH} = $finalDepth;
 3759:                 if ($finalDepth > $maxDepth) {$maxDepth = $finalDepth;}
 3760:             }
 3761: 
 3762: 	    $curRes = $iterator->next();
 3763:         }
 3764:     }
 3765: 
 3766:     # Check: Was this only one resource, a map?
 3767:     if ($resourceCount == 1 && $resource->is_sequence() && !$self->{FORCE_TOP}) { 
 3768:         my $firstResource = $resource->map_start();
 3769:         my $finishResource = $resource->map_finish();
 3770: 	return Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
 3771: 						 $finishResource, $self->{FILTER},
 3772: 						 $self->{ALREADY_SEEN}, 
 3773: 						 $self->{CONDITION},
 3774: 						 $self->{FORCE_TOP},
 3775:                                                  undef,$self->{DEEPLINKLISTED});
 3776:     }
 3777: 
 3778:     # Set up some bookkeeping information.
 3779:     $self->{CURRENT_DEPTH} = 0;
 3780:     $self->{MAX_DEPTH} = $maxDepth;
 3781:     $self->{STACK} = [];
 3782:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
 3783:     $self->{FINISHED} = 0; # When true, the iterator has finished
 3784: 
 3785:     for (my $i = 0; $i <= $self->{MAX_DEPTH}; $i++) {
 3786:         push @{$self->{STACK}}, [];
 3787:     }
 3788: 
 3789:     # Prime the recursion w/ the first resource **5**
 3790:     push @{$self->{STACK}->[0]}, $self->{FIRST_RESOURCE};
 3791:     $self->{ALREADY_SEEN}->{$self->{FIRST_RESOURCE}->{ID}} = 1;
 3792: 
 3793:     bless ($self);
 3794:     return $self;
 3795: }
 3796: 
 3797: sub next {
 3798:     my $self = shift;
 3799:     my $closeAllPages=shift;
 3800:     my $noblockcheck = shift;
 3801:     if ($self->{FINISHED}) {
 3802: 	return END_ITERATOR();
 3803:     }
 3804: 
 3805:     # If we want to return the top-level map object, and haven't yet,
 3806:     # do so.
 3807:     if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0}) {
 3808:         $self->{HAVE_RETURNED_0} = 1;
 3809: 	my $nextTopLevel = $self->{NAV_MAP}->getById('0.0');
 3810:         return $self->{NAV_MAP}->getById('0.0');
 3811:     }
 3812:     if ($self->{RETURN_0} && !$self->{HAVE_RETURNED_0_BEGIN_MAP}) {
 3813: 	$self->{HAVE_RETURNED_0_BEGIN_MAP} = 1;
 3814: 	return $self->BEGIN_MAP();
 3815:     }
 3816: 
 3817:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
 3818:         # grab the next from the recursive iterator 
 3819:         my $next = $self->{RECURSIVE_ITERATOR}->next($closeAllPages);
 3820: 
 3821:         # is it a begin or end map? If so, update the depth
 3822:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
 3823:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
 3824: 
 3825:         # Are we back at depth 0? If so, stop recursing
 3826:         if ($self->{RECURSIVE_DEPTH} == 0) {
 3827:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
 3828:         }
 3829:         return $next;
 3830:     }
 3831: 
 3832:     if (defined($self->{FORCE_NEXT})) {
 3833:         my $tmp = $self->{FORCE_NEXT};
 3834:         $self->{FORCE_NEXT} = undef;
 3835:         return $tmp;
 3836:     }
 3837: 
 3838:     # Have we not yet begun? If not, return BEGIN_MAP and
 3839:     # remember we've started.
 3840:     if ( !$self->{STARTED} ) { 
 3841:         $self->{STARTED} = 1;
 3842:         return $self->BEGIN_MAP();
 3843:     }
 3844: 
 3845:     # Here's the guts of the iterator.
 3846:     
 3847:     # Find the next resource, if any.
 3848:     my $found = 0;
 3849:     my $i = $self->{MAX_DEPTH};
 3850:     my $newDepth;
 3851:     my $here;
 3852:     while ( $i >= 0 && !$found ) {
 3853:         if ( scalar(@{$self->{STACK}->[$i]}) > 0 ) { # **6**
 3854:             $here = pop @{$self->{STACK}->[$i]}; # **7**
 3855:             $found = 1;
 3856:             $newDepth = $i;
 3857:         }
 3858:         $i--;
 3859:     }
 3860: 
 3861:     # If we still didn't find anything, we're done.
 3862:     if ( !$found ) {
 3863:         # We need to get back down to the correct branch depth
 3864:         if ( $self->{CURRENT_DEPTH} > 0 ) {
 3865:             $self->{CURRENT_DEPTH}--;
 3866:             return END_BRANCH();
 3867:         } else {
 3868: 	    $self->{FINISHED} = 1;
 3869:             return END_MAP();
 3870:         }
 3871:     }
 3872: 
 3873:     # If this is not a resource, it must be an END_BRANCH marker we want
 3874:     # to return directly.
 3875:     if (!ref($here)) { # **8**
 3876:         if ($here == END_BRANCH()) { # paranoia, in case of later extension
 3877:             $self->{CURRENT_DEPTH}--;
 3878:             return $here;
 3879:         }
 3880:     }
 3881: 
 3882:     # Otherwise, it is a resource and it's safe to store in $self->{HERE}
 3883:     $self->{HERE} = $here;
 3884: 
 3885:     # Get to the right level
 3886:     if ( $self->{CURRENT_DEPTH} > $newDepth ) {
 3887:         push @{$self->{STACK}->[$newDepth]}, $here;
 3888:         $self->{CURRENT_DEPTH}--;
 3889:         return END_BRANCH();
 3890:     }
 3891:     if ( $self->{CURRENT_DEPTH} < $newDepth) {
 3892:         push @{$self->{STACK}->[$newDepth]}, $here;
 3893:         $self->{CURRENT_DEPTH}++;
 3894:         return BEGIN_BRANCH();
 3895:     }
 3896: 
 3897:     # If we made it here, we have the next resource, and we're at the
 3898:     # right branch level. So let's examine the resource for where
 3899:     # we can get to from here.
 3900: 
 3901:     # So we need to look at all the resources we can get to from here,
 3902:     # categorize them if we haven't seen them, remember if we have a new
 3903:     my $nextUnfiltered = $here->getNext();
 3904: 
 3905: 
 3906:     my $maxDepthAdded = -1;
 3907:     
 3908:     for (@$nextUnfiltered) {
 3909:         if (!defined($self->{ALREADY_SEEN}->{$_->{ID}})) {
 3910:             my $depth = $_->{DATA}->{DISPLAY_DEPTH};
 3911:             push @{$self->{STACK}->[$depth]}, $_;
 3912:             $self->{ALREADY_SEEN}->{$_->{ID}} = 1;
 3913:             if ($maxDepthAdded < $depth) { $maxDepthAdded = $depth; }
 3914:         }
 3915:     }
 3916: 
 3917:     # Is this the end of a branch? If so, all of the resources examined above
 3918:     # led to lower levels than the one we are currently at, so we push a END_BRANCH
 3919:     # marker onto the stack so we don't forget.
 3920:     # Example: For the usual A(BC)(DE)F case, when the iterator goes down the
 3921:     # BC branch and gets to C, it will see F as the only next resource, but it's
 3922:     # one level lower. Thus, this is the end of the branch, since there are no
 3923:     # more resources added to this level or above.
 3924:     # We don't do this if the examined resource is the finish resource,
 3925:     # because the condition given above is true, but the "END_MAP" will
 3926:     # take care of things and we should already be at depth 0.
 3927:     my $isEndOfBranch = $maxDepthAdded < $self->{CURRENT_DEPTH};
 3928:     if ($isEndOfBranch && $here != $self->{FINISH_RESOURCE}) { # **9**
 3929:         push @{$self->{STACK}->[$self->{CURRENT_DEPTH}]}, END_BRANCH();
 3930:     }
 3931: 
 3932:     # That ends the main iterator logic. Now, do we want to recurse
 3933:     # down this map (if this resource is a map)?
 3934:     if ( ($self->{HERE}->is_sequence() || (!$closeAllPages && $self->{HERE}->is_page())) &&
 3935:         (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION}) &&
 3936:         ($env{'request.role.adv'} || !$self->{HERE}->randomout())) {
 3937:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
 3938:         my $firstResource = $self->{HERE}->map_start();
 3939:         my $finishResource = $self->{HERE}->map_finish();
 3940:         $self->{RECURSIVE_ITERATOR} = 
 3941:             Apache::lonnavmaps::iterator->new($self->{NAV_MAP}, $firstResource,
 3942:                                               $finishResource, $self->{FILTER},
 3943:                                               $self->{ALREADY_SEEN},
 3944: 					      $self->{CONDITION},
 3945: 					      $self->{FORCE_TOP},
 3946:                                               undef,$self->{DEEPLINKLISTED});
 3947:     }
 3948: 
 3949:     # If this is a blank resource, don't actually return it.
 3950:     # Should you ever find you need it, make sure to add an option to the code
 3951:     #  that you can use; other things depend on this behavior.
 3952:     my $browsePriv = $self->{HERE}->browsePriv($noblockcheck,$self->{DEEPLINKLISTED});
 3953:     if (!$self->{HERE}->src() || 
 3954:         (!($browsePriv eq 'F') && !($browsePriv eq '2')) ) {
 3955:         return $self->next($closeAllPages);
 3956:     }
 3957: 
 3958:     return $self->{HERE};
 3959: 
 3960: }
 3961: 
 3962: =pod
 3963: 
 3964: The other method available on the iterator is B<getStack>, which
 3965: returns an array populated with the current 'stack' of maps, as
 3966: references to the resource objects. Example: This is useful when
 3967: making the navigation map, as we need to check whether we are under a
 3968: page map to see if we need to link directly to the resource, or to the
 3969: page. The first elements in the array will correspond to the top of
 3970: the stack (most inclusive map).
 3971: 
 3972: =cut
 3973: 
 3974: sub getStack {
 3975:     my $self=shift;
 3976: 
 3977:     my @stack;
 3978: 
 3979:     $self->populateStack(\@stack);
 3980: 
 3981:     return \@stack;
 3982: }
 3983: 
 3984: # Private method: Calls the iterators recursively to populate the stack.
 3985: sub populateStack {
 3986:     my $self=shift;
 3987:     my $stack = shift;
 3988: 
 3989:     push @$stack, $self->{HERE} if ($self->{HERE});
 3990: 
 3991:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
 3992:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
 3993:     }
 3994: }
 3995: 
 3996: 1;
 3997: 
 3998: package Apache::lonnavmaps::DFSiterator;
 3999: use Scalar::Util qw(weaken);
 4000: use Apache::lonnet;
 4001: 
 4002: # Not documented in the perldoc: This is a simple iterator that just walks
 4003: #  through the nav map and presents the resources in a depth-first search
 4004: #  fashion, ignorant of conditionals, randomized resources, etc. It presents
 4005: #  BEGIN_MAP and END_MAP, but does not understand branches at all. It is
 4006: #  useful for pre-processing of some kind, and is in fact used by the main
 4007: #  iterator that way, but that's about it.
 4008: # One could imagine merging this into the init routine of the main iterator,
 4009: #  but this might as well be left separate, since it is possible some other
 4010: #  use might be found for it. - Jeremy
 4011: 
 4012: # Unlike the main iterator, this DOES return all resources, even blank ones.
 4013: #  The main iterator needs them to correctly preprocess the map.
 4014: 
 4015: sub BEGIN_MAP { return 1; }    # begining of a new map
 4016: sub END_MAP { return 2; }      # end of the map
 4017: sub FORWARD { return 1; }      # go forward
 4018: sub BACKWARD { return 2; }
 4019: 
 4020: # Params: Nav map ref, first resource id/ref, finish resource id/ref,
 4021: #         filter hash ref (or undef), already seen hash or undef, condition
 4022: #         (as in main iterator), direction FORWARD or BACKWARD (undef->forward).
 4023: sub new {
 4024:     # magic invocation to create a class instance
 4025:     my $proto = shift;
 4026:     my $class = ref($proto) || $proto;
 4027:     my $self = {};
 4028: 
 4029:     weaken($self->{NAV_MAP} = shift);
 4030:     return undef unless ($self->{NAV_MAP});
 4031: 
 4032:     $self->{USERNAME} = $self->{NAV_MAP}->{USERNAME};
 4033:     $self->{DOMAIN}   = $self->{NAV_MAP}->{DOMAIN};
 4034: 
 4035:     $self->{FIRST_RESOURCE} = shift || $self->{NAV_MAP}->firstResource();
 4036:     $self->{FINISH_RESOURCE} = shift || $self->{NAV_MAP}->finishResource();
 4037: 
 4038:     # If the given resources are just the ID of the resource, get the
 4039:     # objects
 4040:     if (!ref($self->{FIRST_RESOURCE})) { $self->{FIRST_RESOURCE} = 
 4041:              $self->{NAV_MAP}->getById($self->{FIRST_RESOURCE}); }
 4042:     if (!ref($self->{FINISH_RESOURCE})) { $self->{FINISH_RESOURCE} = 
 4043:              $self->{NAV_MAP}->getById($self->{FINISH_RESOURCE}); }
 4044: 
 4045:     $self->{FILTER} = shift;
 4046: 
 4047:     # A hash, used as a set, of resource already seen
 4048:     $self->{ALREADY_SEEN} = shift;
 4049:      if (!defined($self->{ALREADY_SEEN})) { $self->{ALREADY_SEEN} = {} };
 4050:     $self->{CONDITION} = shift;
 4051:     $self->{DIRECTION} = shift || FORWARD();
 4052: 
 4053:     # Flag: Have we started yet?
 4054:     $self->{STARTED} = 0;
 4055: 
 4056:     # Should we continue calling the recursive iterator, if any?
 4057:     $self->{RECURSIVE_ITERATOR_FLAG} = 0;
 4058:     # The recursive iterator, if any
 4059:     $self->{RECURSIVE_ITERATOR} = undef;
 4060:     # Are we recursing on a map, or a branch?
 4061:     $self->{RECURSIVE_MAP} = 1; # we'll manually unset this when recursing on branches
 4062:     # And the count of how deep it is, so that this iterator can keep track of
 4063:     # when to pick back up again.
 4064:     $self->{RECURSIVE_DEPTH} = 0;
 4065: 
 4066:     # For keeping track of our branches, we maintain our own stack
 4067:     $self->{STACK} = [];
 4068: 
 4069:     # Start with the first resource
 4070:     if ($self->{DIRECTION} == FORWARD) {
 4071:         push @{$self->{STACK}}, $self->{FIRST_RESOURCE};
 4072:     } else {
 4073:         push @{$self->{STACK}}, $self->{FINISH_RESOURCE};
 4074:     }
 4075: 
 4076:     bless($self);
 4077:     return $self;
 4078: }
 4079: 
 4080: sub next {
 4081:     my $self = shift;
 4082:     
 4083:     # Are we using a recursive iterator? If so, pull from that and
 4084:     # watch the depth; we want to resume our level at the correct time.
 4085:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
 4086:         # grab the next from the recursive iterator
 4087:         my $next = $self->{RECURSIVE_ITERATOR}->next();
 4088:         
 4089:         # is it a begin or end map? Update depth if so
 4090:         if ($next == BEGIN_MAP() ) { $self->{RECURSIVE_DEPTH}++; }
 4091:         if ($next == END_MAP() ) { $self->{RECURSIVE_DEPTH}--; }
 4092: 
 4093:         # Are we back at depth 0? If so, stop recursing.
 4094:         if ($self->{RECURSIVE_DEPTH} == 0) {
 4095:             $self->{RECURSIVE_ITERATOR_FLAG} = 0;
 4096:         }
 4097:         
 4098:         return $next;
 4099:     }
 4100: 
 4101:     # Is there a current resource to grab? If not, then return
 4102:     # END_MAP, which will end the iterator.
 4103:     if (scalar(@{$self->{STACK}}) == 0) {
 4104:         return $self->END_MAP();
 4105:     }
 4106: 
 4107:     # Have we not yet begun? If not, return BEGIN_MAP and 
 4108:     # remember that we've started.
 4109:     if ( !$self->{STARTED} ) {
 4110:         $self->{STARTED} = 1;
 4111:         return $self->BEGIN_MAP;
 4112:     }
 4113: 
 4114:     # Get the next resource in the branch
 4115:     $self->{HERE} = pop @{$self->{STACK}};
 4116: 
 4117:     # remember that we've seen this, so we don't return it again later
 4118:     $self->{ALREADY_SEEN}->{$self->{HERE}->{ID}} = 1;
 4119:     
 4120:     # Get the next possible resources
 4121:     my $nextUnfiltered;
 4122:     if ($self->{DIRECTION} == FORWARD()) {
 4123:         $nextUnfiltered = $self->{HERE}->getNext();
 4124:     } else {
 4125:         $nextUnfiltered = $self->{HERE}->getPrevious();
 4126:     }
 4127:     my $next = [];
 4128: 
 4129:     # filter the next possibilities to remove things we've 
 4130:     # already seen.
 4131:     foreach my $item (@$nextUnfiltered) {
 4132:         if (!defined($self->{ALREADY_SEEN}->{$item->{ID}})) {
 4133:             push @$next, $item;
 4134:         }
 4135:     }
 4136: 
 4137:     while (@$next) {
 4138:         # copy the next possibilities over to the stack
 4139:         push @{$self->{STACK}}, shift @$next;
 4140:     }
 4141: 
 4142:     # If this is a map and we want to recurse down it... (not filtered out)
 4143:     if ($self->{HERE}->is_map() && 
 4144:          (defined($self->{FILTER}->{$self->{HERE}->map_pc()}) xor $self->{CONDITION})) { 
 4145:         $self->{RECURSIVE_ITERATOR_FLAG} = 1;
 4146:         my $firstResource = $self->{HERE}->map_start();
 4147:         my $finishResource = $self->{HERE}->map_finish();
 4148: 
 4149:         $self->{RECURSIVE_ITERATOR} =
 4150:           Apache::lonnavmaps::DFSiterator->new ($self->{NAV_MAP}, $firstResource, 
 4151:                      $finishResource, $self->{FILTER}, $self->{ALREADY_SEEN},
 4152:                                              $self->{CONDITION}, $self->{DIRECTION});
 4153:     }
 4154: 
 4155:     return $self->{HERE};
 4156: }
 4157: 
 4158: # Identical to the full iterator methods of the same name. Hate to copy/paste
 4159: # but I also hate to "inherit" either iterator from the other.
 4160: 
 4161: sub getStack {
 4162:     my $self=shift;
 4163: 
 4164:     my @stack;
 4165: 
 4166:     $self->populateStack(\@stack);
 4167: 
 4168:     return \@stack;
 4169: }
 4170: 
 4171: # Private method: Calls the iterators recursively to populate the stack.
 4172: sub populateStack {
 4173:     my $self=shift;
 4174:     my $stack = shift;
 4175: 
 4176:     push @$stack, $self->{HERE} if ($self->{HERE});
 4177: 
 4178:     if ($self->{RECURSIVE_ITERATOR_FLAG}) {
 4179:         $self->{RECURSIVE_ITERATOR}->populateStack($stack);
 4180:     }
 4181: }
 4182: 
 4183: 1;
 4184: 
 4185: package Apache::lonnavmaps::resource;
 4186: use Scalar::Util qw(weaken);
 4187: use Apache::lonnet;
 4188: 
 4189: =pod
 4190: 
 4191: =head1 Object: resource 
 4192: 
 4193: X<resource, navmap object>
 4194: A resource object encapsulates a resource in a resource map, allowing
 4195: easy manipulation of the resource, querying the properties of the
 4196: resource (including user properties), and represents a reference that
 4197: can be used as the canonical representation of the resource by
 4198: lonnavmap clients like renderers.
 4199: 
 4200: A resource only makes sense in the context of a navmap, as some of the
 4201: data is stored in the navmap object.
 4202: 
 4203: You will probably never need to instantiate this object directly. Use
 4204: Apache::lonnavmaps::navmap, and use the "start" method to obtain the
 4205: starting resource.
 4206: 
 4207: Resource objects respect the parameter_hiddenparts, which suppresses 
 4208: various parts according to the wishes of the map author. As of this
 4209: writing, there is no way to override this parameter, and suppressed
 4210: parts will never be returned, nor will their response types or ids be
 4211: stored.
 4212: 
 4213: =head2 Overview
 4214: 
 4215: A B<Resource> is the most granular type of object in LON-CAPA that can
 4216: be included in a course. It can either be a particular resource, like
 4217: an HTML page, external resource, problem, etc., or it can be a
 4218: container sequence, such as a "page" or a "map".
 4219: 
 4220: To see a sequence from the user's point of view, please see the
 4221: B<Creating a Course: Maps and Sequences> chapter of the Author's
 4222: Manual.
 4223: 
 4224: A Resource Object, once obtained from a navmap object via a B<getBy*>
 4225: method of the navmap, or from an iterator, allows you to query
 4226: information about that resource.
 4227: 
 4228: Generally, you do not ever want to create a resource object yourself,
 4229: so creation has been left undocumented. Always retrieve resources
 4230: from navmap objects.
 4231: 
 4232: =head3 Identifying Resources
 4233: 
 4234: X<big hash>Every resource is identified by a Resource ID in the big hash that is
 4235: unique to that resource for a given course. X<resource ID, in big hash>
 4236: The Resource ID has the form #.#, where the first number is the same
 4237: for every resource in a map, and the second is unique. For instance,
 4238: for a course laid out like this:
 4239: 
 4240:  * Problem 1
 4241:  * Map
 4242:    * Resource 2
 4243:    * Resource 3
 4244: 
 4245: C<Problem 1> and C<Map> will share a first number, and C<Resource 2>
 4246: C<Resource 3> will share a first number. The second number may end up
 4247: re-used between the two groups.
 4248: 
 4249: The resource ID is only used in the big hash, but can be used in the
 4250: context of a course to identify a resource easily. (For instance, the
 4251: printing system uses it to record which resources from a sequence you 
 4252: wish to print.)
 4253: 
 4254: X<symb> X<resource, symb>
 4255: All resources also have B<symb>s, which uniquely identify a resource
 4256: in a course. Many internal LON-CAPA functions expect a symb. A symb
 4257: carries along with it the URL of the resource, and the map it appears
 4258: in. Symbs are much larger than resource IDs.
 4259: 
 4260: =cut
 4261: 
 4262: sub new {
 4263:     # magic invocation to create a class instance
 4264:     my $proto = shift;
 4265:     my $class = ref($proto) || $proto;
 4266:     my $self = {};
 4267: 
 4268:     weaken($self->{NAV_MAP} = shift);
 4269:     $self->{ID} = shift;
 4270: 
 4271:     $self->{USERNAME} = $self->{NAV_MAP}->{USERNAME};
 4272:     $self->{DOMAIN}   = $self->{NAV_MAP}->{DOMAIN};
 4273: 
 4274:     # Store this new resource in the parent nav map's cache.
 4275:     $self->{NAV_MAP}->{RESOURCE_CACHE}->{$self->{ID}} = $self;
 4276:     $self->{RESOURCE_ERROR} = 0;
 4277: 
 4278:     $self->{DUEDATE_CACHE} = undef;
 4279: 
 4280:     # A hash that can be used by two-pass algorithms to store data
 4281:     # about this resource in. Not used by the resource object
 4282:     # directly.
 4283:     $self->{DATA} = {};
 4284:     
 4285:     bless($self);
 4286:     
 4287:     # This is a speed optimization, to avoid calling symb() too often.
 4288:     $self->{SYMB} = $self->symb();
 4289:    
 4290:     return $self;
 4291: }
 4292: 
 4293: # private function: simplify the NAV_HASH lookups we keep doing
 4294: # pass the name, and to automatically append my ID, pass a true val on the
 4295: # second param
 4296: sub navHash {
 4297:     my $self = shift;
 4298:     my $param = shift;
 4299:     my $id = shift;
 4300:     my $arg = $param . ($id?$self->{ID}:"");
 4301:     if (ref($self) && ref($self->{NAV_MAP}) && defined($arg)) {
 4302:         return $self->{NAV_MAP}->navhash($arg);
 4303:     }
 4304:     return;
 4305: }
 4306: 
 4307: =pod
 4308: 
 4309: =head2 Methods
 4310: 
 4311: Once you have a resource object, here's what you can do with it:
 4312: 
 4313: =head3 Attribute Retrieval
 4314: 
 4315: Every resource has certain attributes that can be retrieved and used:
 4316: 
 4317: =over 4
 4318: 
 4319: =item * B<ID>: Every resource has an ID that is unique for that
 4320:     resource in the course it is in. The ID is actually in the hash
 4321:     representing the resource, so for a resource object $res, obtain
 4322:     it via C<$res->{ID}).
 4323: 
 4324: =item * B<compTitle>:
 4325: 
 4326: Returns a "composite title", that is equal to $res->title() if the
 4327: resource has a title, and is otherwise the last part of the URL (e.g.,
 4328: "problem.problem").
 4329: 
 4330: =item * B<ext>:
 4331: 
 4332: Returns true if the resource is external.
 4333: 
 4334: =item * B<kind>:
 4335: 
 4336: Returns the kind of the resource from the compiled nav map.
 4337: 
 4338: =item * B<randomout>:
 4339: 
 4340: Returns true if this resource was chosen to NOT be shown to the user
 4341: by the random map selection feature. In other words, this is usually
 4342: false.
 4343: 
 4344: =item * B<randompick>:
 4345: 
 4346: Returns the number of randomly picked items for a map if the randompick
 4347: feature is being used on the map. 
 4348: 
 4349: =item * B<randomorder>:
 4350: 
 4351: Returns true for a map if the randomorder feature is being used on the
 4352: map.
 4353: 
 4354: =item * B<src>:
 4355: 
 4356: Returns the source for the resource.
 4357: 
 4358: =item * B<symb>:
 4359: 
 4360: Returns the symb for the resource.
 4361: 
 4362: =item * B<title>:
 4363: 
 4364: Returns the title of the resource.
 4365: 
 4366: =back
 4367: 
 4368: =cut
 4369: 
 4370: # These info functions can be used directly, as they don't return
 4371: # resource information.
 4372: sub comesfrom { my $self=shift; return $self->navHash("comesfrom_", 1); }
 4373: sub encrypted { my $self=shift; return $self->navHash("encrypted_", 1); }
 4374: sub ext { my $self=shift; return $self->navHash("ext_", 1) eq 'true:'; }
 4375: sub from { my $self=shift; return $self->navHash("from_", 1); }
 4376: # considered private and undocumented
 4377: sub goesto { my $self=shift; return $self->navHash("goesto_", 1); }
 4378: sub kind { my $self=shift; return $self->navHash("kind_", 1); }
 4379: sub randomout { my $self=shift; return $self->navHash("randomout_", 1); }
 4380: sub deeplinkout { my $self=shift; return $self->navHash("deeplinkout_", 1); }
 4381: sub randompick { 
 4382:     my $self = shift;
 4383:     my $randompick = $self->parmval('randompick');
 4384:     return $randompick;
 4385: }
 4386: sub randomorder { 
 4387:     my $self = shift;
 4388:     my $randomorder = $self->parmval('randomorder');
 4389:     return ($randomorder =~ /^yes$/i);
 4390: }
 4391: sub link {
 4392:     my $self=shift;
 4393:     if ($self->encrypted()) { return &Apache::lonenc::encrypted($self->src); }
 4394:     return $self->src;
 4395: }
 4396: sub src { 
 4397:     my $self=shift;
 4398:     return $self->navHash("src_", 1);
 4399: }
 4400: sub shown_symb {
 4401:     my $self=shift;
 4402:     if ($self->encrypted()) {return &Apache::lonenc::encrypted($self->{SYMB});}
 4403:     return $self->{SYMB};
 4404: }
 4405: sub id {
 4406:     my $self=shift;
 4407:     return $self->{ID};
 4408: }
 4409: sub enclosing_map_src {
 4410:     my $self=shift;
 4411:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
 4412:     return $self->navHash('map_id_'.$first);
 4413: }
 4414: sub symb {
 4415:     my $self=shift;
 4416:     if (defined($self->{SYMB})) { return $self->{SYMB}; }
 4417:     (my $first, my $second) = $self->{ID} =~ /(\d+).(\d+)/;
 4418:     my $symbSrc = &Apache::lonnet::declutter($self->src());
 4419:     my $symb = &Apache::lonnet::declutter($self->navHash('map_id_'.$first)) 
 4420:         . '___' . $second . '___' . $symbSrc;
 4421:     return &Apache::lonnet::symbclean($symb);
 4422: }
 4423: sub wrap_symb {
 4424:     my $self = shift;
 4425:     return $self->{NAV_MAP}->wrap_symb($self->{SYMB});
 4426: }
 4427: sub title { 
 4428:     my $self=shift; 
 4429:     if ($self->{ID} eq '0.0') {
 4430: 	# If this is the top-level map, return the title of the course
 4431: 	# since this map can not be titled otherwise.
 4432: 	return $env{'course.'.$env{'request.course.id'}.'.description'};
 4433:     }
 4434:     return $self->navHash("title_", 1); }
 4435: # considered private and undocumented
 4436: sub to { my $self=shift; return $self->navHash("to_", 1); }
 4437: sub condition {
 4438:     my $self=shift;
 4439:     my $undercond=$self->navHash("undercond_", 1);
 4440:     if (!defined($undercond)) { return 1; };
 4441:     my $condid=$self->navHash("condid_$undercond");
 4442:     if (!defined($condid)) { return 1; };
 4443:     my $condition=&Apache::lonnet::directcondval($condid);
 4444:     return $condition;
 4445: }
 4446: sub condval {
 4447:     my $self=shift;
 4448:     my ($pathname,$filename) = 
 4449: 	&Apache::lonnet::split_uri_for_cond($self->src());
 4450: 
 4451:     my $match=($env{'acc.res.'.$env{'request.course.id'}.'.'.$pathname}=~
 4452: 	       /\&\Q$filename\E\:([\d\|]+)\&/);
 4453:     if ($match) {
 4454: 	return &Apache::lonnet::condval($1);
 4455:     }
 4456:     return 0;
 4457: }
 4458: sub compTitle {
 4459:     my $self = shift;
 4460:     my $title = $self->title();
 4461:     $title=~s/\&colon\;/\:/gs;
 4462:     if (!$title) {
 4463:         $title = $self->src();
 4464:         $title = substr($title, rindex($title, '/') + 1);
 4465:     }
 4466:     return $title;
 4467: }
 4468: 
 4469: =pod
 4470: 
 4471: B<Predicate Testing the Resource>
 4472: 
 4473: These methods are shortcuts to deciding if a given resource has a given property.
 4474: 
 4475: =over 4
 4476: 
 4477: =item * B<is_map>:
 4478: 
 4479: Returns true if the resource is a map type.
 4480: 
 4481: =item * B<is_problem>:
 4482: 
 4483: Returns true if the resource is a problem type, false
 4484: otherwise. (Looks at the extension on the src field; might need more
 4485: to work correctly.)
 4486: 
 4487: =item * B<is_page>:
 4488: 
 4489: Returns true if the resource is a page.
 4490: 
 4491: =item * B<is_sequence>:
 4492: 
 4493: Returns true if the resource is a sequence.
 4494: 
 4495: =back
 4496: 
 4497: =cut
 4498: 
 4499: sub hasResource {
 4500:    my $self = shift;
 4501:    return $self->{NAV_MAP}->hasResource(@_);
 4502: }
 4503: 
 4504: sub retrieveResources {
 4505:    my $self = shift;
 4506:    return $self->{NAV_MAP}->retrieveResources(@_);
 4507: }
 4508: 
 4509: sub is_exam {
 4510:     my ($self,$part) = @_;
 4511:     my $type = $self->parmval('type',$part);
 4512:     if ($type eq 'exam') {
 4513:         return 1;
 4514:     }
 4515:     if ($self->src() =~ /\.(exam)$/) {
 4516:         return 1;
 4517:     }
 4518:     return 0;
 4519: }
 4520: sub is_html {
 4521:     my $self=shift;
 4522:     my $src = $self->src();
 4523:     return ($src =~ /html$/);
 4524: }
 4525: sub is_map { my $self=shift; return defined($self->navHash("is_map_", 1)); }
 4526: sub is_page {
 4527:     my $self=shift;
 4528:     my $src = $self->src();
 4529:     return $self->navHash("is_map_", 1) && 
 4530: 	$self->navHash("map_type_" . $self->map_pc()) eq 'page';
 4531: }
 4532: sub is_practice {
 4533:     my $self=shift;
 4534:     my ($part) = @_;
 4535:     my $type = $self->parmval('type',$part);
 4536:     if ($type eq 'practice') {
 4537:         return 1;
 4538:     }
 4539:     return 0;
 4540: }
 4541: sub is_problem {
 4542:     my $self=shift;
 4543:     my $src = $self->src();
 4544:     if ($src =~ /$LONCAPA::assess_re/) {
 4545: 	return !($self->is_practice());
 4546:     }
 4547:     return 0;
 4548: }
 4549: #
 4550: #  The has below is the set of status that are considered 'incomplete'
 4551: #
 4552: my %incomplete_hash = 
 4553: (
 4554:  TRIES_LEFT()     => 1,
 4555:  OPEN()           => 1,
 4556:  ATTEMPTED()      => 1
 4557: 
 4558:  );
 4559: #
 4560: #  Return tru if a problem is incomplete... for now incomplete means that
 4561: #  any part of the problem is incomplete. 
 4562: #  Note that if the resources is not a problem, 0 is returned.
 4563: #
 4564: sub is_incomplete {
 4565:     my $self = shift;
 4566:     if ($self->is_problem()) {
 4567: 	foreach my $part (@{$self->parts()}) {
 4568: 	    if (exists($incomplete_hash{$self->status($part)})) {
 4569: 		return 1;
 4570: 	    }
 4571: 	}
 4572:     }
 4573:     return 0;
 4574:        
 4575: }
 4576: sub is_raw_problem {
 4577:     my $self=shift;
 4578:     my $src = $self->src();
 4579:     if ($src =~ /$LONCAPA::assess_re/) {
 4580:         return 1;
 4581:     }
 4582:     return 0;
 4583: }
 4584: 
 4585: sub contains_problem {
 4586:     my $self=shift;
 4587:     if ($self->is_page()) {
 4588: 	my $hasProblem=$self->hasResource($self,sub { $_[0]->is_problem() },1);
 4589: 	return $hasProblem;
 4590:     }
 4591:     return 0;
 4592: }
 4593: sub map_contains_problem {
 4594:     my $self=shift;
 4595:     if ($self->is_map()) {
 4596: 	my $has_problem=
 4597: 	    $self->hasResource($self,sub { $_[0]->is_problem() },1);
 4598: 	return $has_problem;
 4599:     }
 4600:     return 0;
 4601: }
 4602: sub is_sequence {
 4603:     my $self=shift;
 4604:     return $self->navHash("is_map_", 1) && 
 4605:     $self->navHash("map_type_" . $self->map_pc()) eq 'sequence';
 4606: }
 4607: sub is_missing_map {
 4608:     my $self=shift;
 4609:     return $self->navHash("is_map_", 1) &&
 4610:     $self->navHash("map_type_" . $self->map_pc()) eq 'none';
 4611: }
 4612: sub is_survey {
 4613:     my $self = shift();
 4614:     my $part = shift();
 4615:     my $type = $self->parmval('type',$part);
 4616:     if (($type eq 'survey') || ($type eq 'surveycred')) {
 4617:         return 1;
 4618:     }
 4619:     if ($self->src() =~ /\.(survey)$/) {
 4620:         return 1;
 4621:     }
 4622:     return 0;
 4623: }
 4624: sub is_anonsurvey {
 4625:     my $self = shift();
 4626:     my $part = shift();
 4627:     my $type = $self->parmval('type',$part);
 4628:     if (($type eq 'anonsurvey') || ($type eq 'anonsurveycred')) {
 4629:         return 1;
 4630:     }
 4631:     return 0;
 4632: }
 4633: sub is_task {
 4634:     my $self=shift;
 4635:     my $src = $self->src();
 4636:     return ($src =~ /\.(task)$/)
 4637: }
 4638: 
 4639: sub is_empty_sequence {
 4640:     my $self=shift;
 4641:     return !$self->is_page() && $self->navHash("is_map_", 1) && !$self->navHash("map_type_" . $self->map_pc());
 4642: }
 4643: 
 4644: # Private method: Shells out to the parmval in the nav map, handler parts.
 4645: sub parmval {
 4646:     my $self = shift;
 4647:     my $what = shift;
 4648:     my $part = shift;
 4649:     if (!defined($part)) { 
 4650:         $part = '0'; 
 4651:     }
 4652:     return $self->{NAV_MAP}->parmval($part.'.'.$what, $self->{SYMB});
 4653: }
 4654: 
 4655: =pod
 4656: 
 4657: B<Map Methods>
 4658: 
 4659: These methods are useful for getting information about the map
 4660: properties of the resource, if the resource is a map (B<is_map>).
 4661: 
 4662: =over 4
 4663: 
 4664: =item * B<map_finish>:
 4665: 
 4666: Returns a reference to a resource object corresponding to the finish
 4667: resource of the map.
 4668: 
 4669: =item * B<map_pc>:
 4670: 
 4671: Returns the pc value of the map, which is the first number that
 4672: appears in the resource ID of the resources in the map, and is the
 4673: number that appears around the middle of the symbs of the resources in
 4674: that map.
 4675: 
 4676: =item * B<map_start>:
 4677: 
 4678: Returns a reference to a resource object corresponding to the start
 4679: resource of the map.
 4680: 
 4681: =item * B<map_type>:
 4682: 
 4683: Returns a string with the type of the map in it.
 4684: 
 4685: =item * B<map_hierarchy>:
 4686: 
 4687: Returns a string with a comma-separated ordered list of map_pc IDs
 4688: for the hierarchy of maps containing a map, with the top level
 4689: map first, then descending to deeper levels, with the enclosing map last.
 4690: 
 4691: =item * B<map_breadcrumbs>:
 4692: 
 4693: Same as map_hierarchy, except maps containing only a single itemm if
 4694: it's a map, or containing no items are omitted, unless it's the top
 4695: level map (map_pc = 1), which is always included.
 4696: 
 4697: =back
 4698: 
 4699: =cut
 4700: 
 4701: sub map_finish {
 4702:     my $self = shift;
 4703:     my $src = $self->src();
 4704:     $src = &Apache::lonnet::clutter($src);
 4705:     my $res = $self->navHash("map_finish_$src", 0);
 4706:     $res = $self->{NAV_MAP}->getById($res);
 4707:     return $res;
 4708: }
 4709: sub map_pc {
 4710:     my $self = shift;
 4711:     my $src = $self->src();
 4712:     return $self->navHash("map_pc_$src", 0);
 4713: }
 4714: sub map_start {
 4715:     my $self = shift;
 4716:     my $src = $self->src();
 4717:     $src = &Apache::lonnet::clutter($src);
 4718:     my $res = $self->navHash("map_start_$src", 0);
 4719:     $res = $self->{NAV_MAP}->getById($res);
 4720:     return $res;
 4721: }
 4722: sub map_type {
 4723:     my $self = shift;
 4724:     my $pc = $self->map_pc();
 4725:     return $self->navHash("map_type_$pc", 0);
 4726: }
 4727: sub map_hierarchy {
 4728:     my $self = shift;
 4729:     my $pc = $self->map_pc();
 4730:     return $self->navHash("map_hierarchy_$pc", 0);
 4731: }
 4732: sub map_breadcrumbs {
 4733:     my $self = shift;
 4734:     my $pc = $self->map_pc();
 4735:     return $self->navHash("map_breadcrumbs_$pc", 0);
 4736: }
 4737: 
 4738: #####
 4739: # Property queries
 4740: #####
 4741: 
 4742: # These functions will be responsible for returning the CORRECT
 4743: # VALUE for the parameter, no matter what. So while they may look
 4744: # like direct calls to parmval, they can be more than that.
 4745: # So, for instance, the duedate function should use the "duedatetype"
 4746: # information, rather than the resource object user.
 4747: 
 4748: =pod
 4749: 
 4750: =head2 Resource Parameters
 4751: 
 4752: In order to use the resource parameters correctly, the nav map must
 4753: have been instantiated with genCourseAndUserOptions set to true, so
 4754: the courseopt and useropt is read correctly. Then, you can call these
 4755: functions to get the relevant parameters for the resource. Each
 4756: function defaults to part "0", but can be directed to another part by
 4757: passing the part as the parameter.
 4758: 
 4759: These methods are responsible for getting the parameter correct, not
 4760: merely reflecting the contents of the GDBM hashes. As we move towards
 4761: dates relative to other dates, these methods should be updated to
 4762: reflect that. (Then, anybody using these methods will not have to update
 4763: their code.)
 4764: 
 4765: =over 4
 4766: 
 4767: 
 4768: =item * B<printable>
 4769: 
 4770: returns true if the current date is such that the 
 4771: specified resource part is printable.
 4772: 
 4773: 
 4774: =item * B<resprintable>
 4775: 
 4776: Returns true if all parts in the resource are printable making the
 4777: entire resource printable.
 4778: 
 4779: =item * B<acc>
 4780: 
 4781: Get the Client IP/Name Access Control information.
 4782: 
 4783: =item * B<answerdate>:
 4784: 
 4785: Get the answer-reveal date for the problem.
 4786: 
 4787: =item * B<awarded>: 
 4788: 
 4789: Gets the awarded value for the problem part. Requires genUserData set to
 4790: true when the navmap object was created.
 4791: 
 4792: =item * B<duedate>:
 4793: 
 4794: Get the due date for the problem.
 4795: 
 4796: =item * B<tries>:
 4797: 
 4798: Get the number of tries the student has used on the problem.
 4799: 
 4800: =item * B<maxtries>:
 4801: 
 4802: Get the number of max tries allowed.
 4803: 
 4804: =item * B<opendate>:
 4805: 
 4806: Get the open date for the problem.
 4807: 
 4808: =item * B<sig>:
 4809: 
 4810: Get the significant figures setting.
 4811: 
 4812: =item * B<tol>:
 4813: 
 4814: Get the tolerance for the problem.
 4815: 
 4816: =item * B<tries>:
 4817: 
 4818: Get the number of tries the user has already used on the problem.
 4819: 
 4820: =item * B<type>:
 4821: 
 4822: Get the question type for the problem.
 4823: 
 4824: =item * B<weight>:
 4825: 
 4826: Get the weight for the problem.
 4827: 
 4828: =back
 4829: 
 4830: =cut
 4831: 
 4832: 
 4833: 
 4834: 
 4835: sub printable {
 4836: 
 4837:     my ($self, $part) = @_;
 4838: 
 4839:     #  The following cases apply:
 4840:     #  - If a start date is not set, it is replaced by the open date.
 4841:     #  - Ditto for start/open replaced by content open.
 4842:     #  - If neither start nor printdates are set the part is printable.
 4843:     #  - Start date set but no end date: Printable if now >= start date.
 4844:     #  - End date set but no start date: Printable if now <= end date.
 4845:     #  - both defined: printable if start <= now <= end
 4846:     #
 4847: 
 4848:     # Get the print open/close dates for the resource.
 4849: 
 4850:     my $start = $self->parmval("printstartdate", $part);
 4851:     my $end   = $self->parmval("printenddate", $part);
 4852: 
 4853:     if (!$start) {
 4854: 	$start = $self->parmval("opendate", $part);
 4855:     }
 4856:     if (!$start) {
 4857: 	$start = $self->parmval("contentopen", $part);
 4858:     }
 4859: 
 4860: 
 4861:     my $now  = time();
 4862: 
 4863: 
 4864:     my $startok = 1;
 4865:     my $endok   = 1;
 4866: 
 4867:     if ((defined $start) && ($start ne '')) {
 4868: 	$startok = $start <= $now;
 4869:     }
 4870:     if ((defined $end) && ($end != '')) {
 4871: 	$endok = $end >= $now;
 4872:     }
 4873:     return $startok && $endok;
 4874: }
 4875: 
 4876: sub resprintable {
 4877:     my $self = shift;
 4878: 
 4879:     # get parts...or realize there are no parts.
 4880: 
 4881:     my $partsref = $self->parts();
 4882:     my @parts    = @$partsref;
 4883: 
 4884:     if (!@parts) {
 4885: 	return $self->printable(0);
 4886:     } else {
 4887: 	foreach my $part  (@parts) {
 4888: 	    if (!$self->printable($part)) { 
 4889: 		return 0; 
 4890: 	    }
 4891: 	}
 4892: 	return 1;
 4893:     }
 4894: }
 4895: 
 4896: sub acc {
 4897:     (my $self, my $part) = @_;
 4898:     my $acc = $self->parmval("acc", $part);
 4899:     return $acc;
 4900: }
 4901: sub answerdate {
 4902:     (my $self, my $part) = @_;
 4903:     # Handle intervals
 4904:     my $answerdatetype = $self->parmval("answerdate.type", $part);
 4905:     my $answerdate = $self->parmval("answerdate", $part);
 4906:     my $duedate = $self->parmval("duedate", $part);
 4907:     if ($answerdatetype eq 'date_interval') {
 4908:         $answerdate = $duedate + $answerdate; 
 4909:     }
 4910:     return $answerdate;
 4911: }
 4912: sub awarded { 
 4913:     my $self = shift; my $part = shift;
 4914:     $self->{NAV_MAP}->get_user_data();
 4915:     if (!defined($part)) { $part = '0'; }
 4916:     return $self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$part.'.awarded'};
 4917: }
 4918: sub taskversion {
 4919:     my $self = shift; my $part = shift;
 4920:     $self->{NAV_MAP}->get_user_data();
 4921:     if (!defined($part)) { $part = '0'; }
 4922:     return $self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$part.'.version'};
 4923: }
 4924: sub taskstatus {
 4925:     my $self = shift; my $part = shift;
 4926:     $self->{NAV_MAP}->get_user_data();
 4927:     if (!defined($part)) { $part = '0'; }
 4928:     return $self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$self->taskversion($part).'.'.$part.'.status'};
 4929: }
 4930: sub solved {
 4931:     my $self = shift; my $part = shift;
 4932:     $self->{NAV_MAP}->get_user_data();
 4933:     if (!defined($part)) { $part = '0'; }
 4934:     return $self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$part.'.solved'};
 4935: }
 4936: sub checkedin {
 4937:     my $self = shift; my $part = shift;
 4938:     $self->{NAV_MAP}->get_user_data();
 4939:     if (!defined($part)) { $part = '0'; }
 4940:     if ($self->is_task()) {
 4941:         my $version = $self->taskversion($part);
 4942:         return ($self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$version .'.'.$part.'.checkedin'},$self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$version .'.'.$part.'.checkedin.slot'});
 4943:     } else {
 4944:         return ($self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$part.'.checkedin'},$self->{NAV_MAP}->{STUDENT_DATA}->{$self->{SYMB}}->{'resource.'.$part.'.checkedin.slot'});
 4945:     }
 4946: }
 4947: # this should work exactly like the copy in lonhomework.pm
 4948: # Why is there a copy in lonhomework?  Why not centralized?
 4949: #
 4950: #  TODO: Centralize duedate.
 4951: #
 4952: 
 4953: sub duedate {
 4954:     (my $self, my $part) = @_;
 4955:     if (defined ($self->{DUEDATE_CACHE}->{$part})) {
 4956:         return $self->{DUEDATE_CACHE}->{$part};
 4957:     }
 4958:     my $date;
 4959:     my @interval=$self->parmval("interval", $part);
 4960:     my $due_date=$self->parmval("duedate", $part);
 4961:     if ($interval[0] =~ /(\d+)/) {
 4962:         my $timelimit = $1;
 4963:         my $first_access=&Apache::lonnet::get_first_access($interval[1],
 4964:                                                           $self->{SYMB});
 4965: 	if (defined($first_access)) {
 4966:             my $interval = $first_access+$timelimit;
 4967: 	    $date = (!$due_date || $interval < $due_date) ? $interval 
 4968:                                                           : $due_date;
 4969: 	} else {
 4970: 	    $date = $due_date;
 4971: 	}
 4972:     } else {
 4973: 	$date = $due_date;
 4974:     }
 4975:     $self->{DUEDATE_CACHE}->{$part} = $date;
 4976:     return $date;
 4977: }
 4978: sub handgrade {
 4979:     (my $self, my $part) = @_;
 4980:     my @response_ids = $self->responseIds($part);
 4981:     if (@response_ids) {
 4982: 	foreach my $response_id (@response_ids) {
 4983:             my $handgrade = $self->parmval("handgrade",$part.'_'.$response_id);
 4984: 	    if (lc($handgrade) eq 'yes') {
 4985: 		return 'yes';
 4986: 	    }
 4987: 	}
 4988:     }
 4989:     my $handgrade = $self->parmval("handgrade", $part);
 4990:     return $handgrade;
 4991: }
 4992: sub maxtries {
 4993:     (my $self, my $part) = @_;
 4994:     my $maxtries = $self->parmval("maxtries", $part);
 4995:     return $maxtries;
 4996: }
 4997: sub opendate {
 4998:     (my $self, my $part) = @_;
 4999:     my $opendatetype = $self->parmval("opendate.type", $part);
 5000:     my $opendate = $self->parmval("opendate", $part); 
 5001:     if ($opendatetype eq 'date_interval') {
 5002:         my $duedate = $self->duedate($part);
 5003:         $opendate = $duedate - $opendate; 
 5004:     }
 5005:     return $opendate;
 5006: }
 5007: sub problemstatus {
 5008:     (my $self, my $part) = @_;
 5009:     my $problemstatus = $self->parmval("problemstatus", $part);
 5010:     return lc($problemstatus);
 5011: }
 5012: sub sig {
 5013:     (my $self, my $part) = @_;
 5014:     my $sig = $self->parmval("sig", $part);
 5015:     return $sig;
 5016: }
 5017: sub tol {
 5018:     (my $self, my $part) = @_;
 5019:     my $tol = $self->parmval("tol", $part);
 5020:     return $tol;
 5021: }
 5022: sub tries {
 5023:     my $self = shift; 
 5024:     my $tries = $self->queryRestoreHash('tries', shift);
 5025:     if (!defined($tries)) { return '0';}
 5026:     return $tries;
 5027: }
 5028: sub type {
 5029:     (my $self, my $part) = @_;
 5030:     my $type = $self->parmval("type", $part);
 5031:     return $type;
 5032: }
 5033: sub weight { 
 5034:     my $self = shift; my $part = shift;
 5035:     if (!defined($part)) { $part = '0'; }
 5036:     my $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
 5037:                                 $self->{SYMB}, $self->{DOMAIN},
 5038:                                 $self->{USERNAME},
 5039:                                 $env{'request.course.sec'});
 5040:     return $weight;
 5041: }
 5042: sub part_display {
 5043:     my $self= shift(); my $partID = shift();
 5044:     if (! defined($partID)) { $partID = '0'; }
 5045:     my $display=&Apache::lonnet::EXT('resource.'.$partID.'.display',
 5046:                                      $self->{SYMB});
 5047:     if (! defined($display) || $display eq '') {
 5048:         $display = $partID;
 5049:     }
 5050:     return $display;
 5051: }
 5052: sub slot_control {
 5053:     my $self=shift(); my $part = shift();
 5054:     if (!defined($part)) { $part = '0'; }
 5055:     my $useslots = $self->parmval("useslots", $part);
 5056:     my $availablestudent = $self->parmval("availablestudent", $part);
 5057:     my $available = $self->parmval("available", $part); 
 5058:     return ($useslots,$availablestudent,$available);
 5059: }
 5060: sub deeplink {
 5061:     my ($self,$caller,$action) = @_;
 5062:     my $deeplink = $self->parmval("deeplink");
 5063:     if ($deeplink) {
 5064:         my ($state,$others,$listed,$scope) = split(/,/,$deeplink);
 5065:         if ($action eq 'getlisted') {
 5066:             return $listed;
 5067:         }
 5068:         if ($env{'request.deeplink.login'}) {
 5069:             my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
 5070:             my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 5071:             my $deeplink_symb = &Apache::loncommon::deeplink_login_symb($cnum,$cdom);
 5072:             if ($deeplink_symb) {
 5073:                 my ($loginmap,$mapname);
 5074:                 if ($deeplink_symb =~ /\.(page|sequence)$/) {
 5075:                     $mapname = $self->enclosing_map_src();
 5076:                     $loginmap = &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($deeplink_symb))[2]);
 5077:                     return if ($mapname eq $loginmap);
 5078:                 } else {
 5079:                     return if ($deeplink_symb eq $self->symb());
 5080:                     if (($scope eq 'map') || ($scope eq 'rec')) {
 5081:                         $mapname = $self->enclosing_map_src();
 5082:                         $loginmap = &Apache::lonnet::clutter((&Apache::lonnet::decode_symb($deeplink_symb))[0]);
 5083:                         return if ($mapname eq $loginmap);
 5084:                     }
 5085:                 }
 5086:                 if ($scope eq 'rec') {
 5087:                     my $map_pc = $self->navHash('map_pc_'.$mapname);
 5088:                     my @recurseup = split(/,/,$self->navHash('map_hierarchy_'.$map_pc));
 5089:                     my $login_pc = $self->navHash('map_pc_'.$loginmap);
 5090:                     return if (grep(/^\Q$login_pc\E$/,@recurseup));
 5091:                 }
 5092:             }
 5093:         }
 5094:         unless (($caller eq 'sequence') || ($state eq 'both')) {
 5095:             return $listed;
 5096:         }
 5097:     }
 5098:     return;
 5099: }
 5100: 
 5101: # Multiple things need this
 5102: sub getReturnHash {
 5103:     my $self = shift;
 5104:     
 5105:     if (!defined($self->{RETURN_HASH})) {
 5106:         my %tmpHash  = &Apache::lonnet::restore($self->{SYMB},undef,$self->{DOMAIN},$self->{USERNAME});
 5107:         $self->{RETURN_HASH} = \%tmpHash;
 5108:     }
 5109: }       
 5110: 
 5111: ######
 5112: # Status queries
 5113: ######
 5114: 
 5115: # These methods query the status of problems.
 5116: 
 5117: # If we need to count parts, this function determines the number of
 5118: # parts from the metadata. When called, it returns a reference to a list
 5119: # of strings corresponding to the parts. (Thus, using it in a scalar context
 5120: # tells you how many parts you have in the problem:
 5121: # $partcount = scalar($resource->countParts());
 5122: # Don't use $self->{PARTS} directly because you don't know if it's been
 5123: # computed yet.
 5124: 
 5125: =pod
 5126: 
 5127: =head2 Resource misc
 5128: 
 5129: Misc. functions for the resource.
 5130: 
 5131: =over 4
 5132: 
 5133: =item * B<hasDiscussion>:
 5134: 
 5135: Returns a false value if there has been discussion since the user last
 5136: logged in, true if there has. Always returns false if the discussion
 5137: data was not extracted when the nav map was constructed.
 5138: 
 5139: =item * B<last_post_time>:
 5140: 
 5141: Returns a false value if there hasn't been discussion otherwise returns
 5142: unix timestamp of last time a discussion posting (or edit) was made.
 5143: 
 5144: =item * B<discussion_info>:
 5145: 
 5146: optional argument is a filter (currently can be 'unread');
 5147: returns in scalar context the count of the number of discussion postings.
 5148: 
 5149: returns in list context both the count of postings and a hash ref
 5150: containing information about the postings (subject, id, timestamp) in a hash.
 5151: 
 5152: Default is to return counts for all postings.  However if called with a second argument set to 'unread', will return information about only unread postings.
 5153: 
 5154: =item * B<getFeedback>:
 5155: 
 5156: Gets the feedback for the resource and returns the raw feedback string
 5157: for the resource, or the null string if there is no feedback or the
 5158: email data was not extracted when the nav map was constructed. Usually
 5159: used like this:
 5160: 
 5161:  for my $url (split(/\,/, $res->getFeedback())) {
 5162:     my $link = &escape($url);
 5163:     ...
 5164: 
 5165: and use the link as appropriate.
 5166: 
 5167: =cut
 5168: 
 5169: sub hasDiscussion {
 5170:     my $self = shift;
 5171:     return $self->{NAV_MAP}->hasDiscussion($self->{SYMB});
 5172: }
 5173: 
 5174: sub last_post_time {
 5175:     my $self = shift;
 5176:     return $self->{NAV_MAP}->last_post_time($self->{SYMB});
 5177: }
 5178: 
 5179: sub discussion_info {
 5180:     my ($self,$filter) = @_;
 5181:     return $self->{NAV_MAP}->discussion_info($self->{SYMB},$filter);
 5182: }
 5183: 
 5184: sub getFeedback {
 5185:     my $self = shift;
 5186:     my $source = $self->src();
 5187:     my $symb = $self->{SYMB};
 5188:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
 5189:     return $self->{NAV_MAP}->getFeedback($symb,$source);
 5190: }
 5191: 
 5192: sub getErrors {
 5193:     my $self = shift;
 5194:     my $source = $self->src();
 5195:     my $symb = $self->{SYMB};
 5196:     if ($source =~ /^\/res\//) { $source = substr $source, 5; }
 5197:     return $self->{NAV_MAP}->getErrors($symb,$source);
 5198: }
 5199: 
 5200: =pod
 5201: 
 5202: =item * B<parts>():
 5203: 
 5204: Returns a list reference containing sorted strings corresponding to
 5205: each part of the problem. Single part problems have only a part '0'.
 5206: Multipart problems do not return their part '0', since they typically
 5207: do not really matter. 
 5208: 
 5209: =item * B<countParts>():
 5210: 
 5211: Returns the number of parts of the problem a student can answer. Thus,
 5212: for single part problems, returns 1. For multipart, it returns the
 5213: number of parts in the problem, not including psuedo-part 0. 
 5214: 
 5215: =item * B<countResponses>():
 5216: 
 5217: Returns the total number of responses in the problem a student can answer.
 5218: 
 5219: =item * B<responseTypes>():
 5220: 
 5221: Returns a hash whose keys are the response types.  The values are the number 
 5222: of times each response type is used.  This is for the I<entire> problem, not 
 5223: just a single part.
 5224: 
 5225: =item * B<multipart>():
 5226: 
 5227: Returns true if the problem is multipart, false otherwise. Use this instead
 5228: of countParts if all you want is multipart/not multipart.
 5229: 
 5230: =item * B<responseType>($part):
 5231: 
 5232: Returns the response type of the part, without the word "response" on the
 5233: end. Example return values: 'string', 'essay', 'numeric', etc.
 5234: 
 5235: =item * B<responseIds>($part):
 5236: 
 5237: Retreives the response IDs for the given part as an array reference containing
 5238: strings naming the response IDs. This may be empty.
 5239: 
 5240: =back
 5241: 
 5242: =cut
 5243: 
 5244: sub parts {
 5245:     my $self = shift;
 5246: 
 5247:     if ($self->ext) { return []; }
 5248: 
 5249:     $self->extractParts();
 5250:     return $self->{PARTS};
 5251: }
 5252: 
 5253: sub countParts {
 5254:     my $self = shift;
 5255:     
 5256:     my $parts = $self->parts();
 5257: 
 5258:     # If I left this here, then it's not necessary.
 5259:     #my $delta = 0;
 5260:     #for my $part (@$parts) {
 5261:     #    if ($part eq '0') { $delta--; }
 5262:     #}
 5263: 
 5264:     if ($self->{RESOURCE_ERROR}) {
 5265:         return 0;
 5266:     }
 5267: 
 5268:     return scalar(@{$parts}); # + $delta;
 5269: }
 5270: 
 5271: sub countResponses {
 5272:     my $self = shift;
 5273:     my $count;
 5274:     foreach my $part (@{$self->parts()}) {
 5275:         $count+= scalar($self->responseIds($part));
 5276:     }
 5277:     return $count;
 5278: }
 5279: 
 5280: sub responseTypes {
 5281:     my $self = shift;
 5282:     my %responses;
 5283:     foreach my $part (@{$self->parts()}) {
 5284:         foreach my $responsetype ($self->responseType($part)) {
 5285:             $responses{$responsetype}++ if (defined($responsetype));
 5286:         }
 5287:     }
 5288:     return %responses;
 5289: }
 5290: 
 5291: sub multipart {
 5292:     my $self = shift;
 5293:     return $self->countParts() > 1;
 5294: }
 5295: 
 5296: sub singlepart {
 5297:     my $self = shift;
 5298:     return $self->countParts() == 1;
 5299: }
 5300: 
 5301: sub responseType {
 5302:     my $self = shift;
 5303:     my $part = shift;
 5304: 
 5305:     $self->extractParts();
 5306:     if (defined($self->{RESPONSE_TYPES}->{$part})) {
 5307: 	return @{$self->{RESPONSE_TYPES}->{$part}};
 5308:     } else {
 5309: 	return undef;
 5310:     }
 5311: }
 5312: 
 5313: sub responseIds {
 5314:     my $self = shift;
 5315:     my $part = shift;
 5316: 
 5317:     $self->extractParts();
 5318:     if (defined($self->{RESPONSE_IDS}->{$part})) {
 5319: 	return @{$self->{RESPONSE_IDS}->{$part}};
 5320:     } else {
 5321: 	return undef;
 5322:     }
 5323: }
 5324: 
 5325: # Private function: Extracts the parts information, both part names and
 5326: # part types, and saves it. 
 5327: sub extractParts { 
 5328:     my $self = shift;
 5329:     
 5330:     return if (defined($self->{PARTS}));
 5331:     return if ($self->ext);
 5332: 
 5333:     $self->{PARTS} = [];
 5334: 
 5335:     my %parts;
 5336: 
 5337:     # Retrieve part count, if this is a problem
 5338:     if ($self->is_problem()) {
 5339: 	my $partorder = &Apache::lonnet::metadata($self->src(), 'partorder');
 5340:         my $metadata = &Apache::lonnet::metadata($self->src(), 'packages');
 5341: 
 5342: 	if ($partorder) {
 5343: 	    my @parts;
 5344: 	    for my $part (split (/,/,$partorder)) {
 5345: 		if (!Apache::loncommon::check_if_partid_hidden($part, $self->{SYMB})) {
 5346: 		    push @parts, $part;
 5347: 		    $parts{$part} = 1;
 5348: 		}
 5349: 	    }
 5350: 	    $self->{PARTS} = \@parts;
 5351: 	} else {
 5352: 	    if (!$metadata) {
 5353: 		$self->{RESOURCE_ERROR} = 1;
 5354: 		$self->{PARTS} = [];
 5355: 		$self->{PART_TYPE} = {};
 5356: 		return;
 5357: 	    }
 5358: 	    foreach my $entry (split(/\,/,$metadata)) {
 5359: 		if ($entry =~ /^(?:part|Task)_(.*)$/) {
 5360: 		    my $part = $1;
 5361: 		    # This floods the logs if it blows up
 5362: 		    if (defined($parts{$part})) {
 5363: 			&Apache::lonnet::logthis("$part multiply defined in metadata for " . $self->{SYMB});
 5364: 		    }
 5365: 		    
 5366: 		    # check to see if part is turned off.
 5367: 		    
 5368: 		    if (!Apache::loncommon::check_if_partid_hidden($part, $self->{SYMB})) {
 5369: 			$parts{$part} = 1;
 5370: 		    }
 5371: 		}
 5372: 	    }
 5373: 	    my @sortedParts = sort(keys(%parts));
 5374: 	    $self->{PARTS} = \@sortedParts;
 5375:         }
 5376:         
 5377: 
 5378:         # These hashes probably do not need names that end with "Hash"....
 5379:         my %responseIdHash;
 5380:         my %responseTypeHash;
 5381: 
 5382: 
 5383:         # Init the responseIdHash
 5384:         foreach my $part (@{$self->{PARTS}}) {
 5385:             $responseIdHash{$part} = [];
 5386:         }
 5387: 
 5388:         # Now, the unfortunate thing about this is that parts, part name, and
 5389:         # response id are delimited by underscores, but both the part
 5390:         # name and response id can themselves have underscores in them.
 5391:         # So we have to use our knowlege of part names to figure out 
 5392:         # where the part names begin and end, and even then, it is possible
 5393:         # to construct ambiguous situations.
 5394:         foreach my $data (split(/,/, $metadata)) {
 5395:             if ($data =~ /^([a-zA-Z]+)response_(.*)/
 5396: 		|| $data =~ /^(Task)_(.*)/) {
 5397:                 my $responseType = $1;
 5398:                 my $partStuff = $2;
 5399:                 my $partIdSoFar = '';
 5400:                 my @partChunks = split(/_/, $partStuff);
 5401:                 my $i = 0;
 5402:                 for ($i = 0; $i < scalar(@partChunks); $i++) {
 5403:                     if ($partIdSoFar) { $partIdSoFar .= '_'; }
 5404:                     $partIdSoFar .= $partChunks[$i];
 5405:                     if ($parts{$partIdSoFar}) {
 5406:                         my @otherChunks = @partChunks[$i+1..$#partChunks];
 5407:                         my $responseId = join('_', @otherChunks);
 5408: 			if ($self->is_task()) {
 5409: 			    push(@{$responseIdHash{$partIdSoFar}},
 5410: 				 $partIdSoFar);
 5411: 			} else {
 5412: 			    push(@{$responseIdHash{$partIdSoFar}},
 5413: 				 $responseId);
 5414: 			}
 5415:                         push(@{$responseTypeHash{$partIdSoFar}},
 5416: 			     $responseType);
 5417:                     }
 5418:                 }
 5419:             }
 5420:         }
 5421: 	my $resorder = &Apache::lonnet::metadata($self->src(),'responseorder');
 5422:         #
 5423:         # Reorder the arrays in the %responseIdHash and %responseTypeHash
 5424: 	if ($resorder) {
 5425: 	    my @resorder=split(/,/,$resorder);
 5426: 	    foreach my $part (keys(%responseIdHash)) {
 5427: 		my $i=0;
 5428: 		my %resids = map { ($_,$i++) } @{ $responseIdHash{$part} };
 5429: 		my @neworder;
 5430: 		foreach my $possibleid (@resorder) {
 5431: 		    if (exists($resids{$possibleid})) {
 5432: 			push(@neworder,$resids{$possibleid});
 5433: 		    }
 5434: 		}
 5435: 		my @ids;
 5436: 		my @type;
 5437: 		foreach my $element (@neworder) {
 5438: 		    push (@ids,$responseIdHash{$part}->[$element]);
 5439: 		    push (@type,$responseTypeHash{$part}->[$element]);
 5440: 		}
 5441: 		$responseIdHash{$part}=\@ids;
 5442: 		$responseTypeHash{$part}=\@type;
 5443: 	    }
 5444: 	}
 5445:         $self->{RESPONSE_IDS} = \%responseIdHash;
 5446:         $self->{RESPONSE_TYPES} = \%responseTypeHash;
 5447:     }
 5448: 
 5449:     return;
 5450: }
 5451: 
 5452: =pod
 5453: 
 5454: =head2 Resource Status
 5455: 
 5456: Problem resources have status information, reflecting their various
 5457: dates and completion statuses.
 5458: 
 5459: There are two aspects to the status: the date-related information and
 5460: the completion information.
 5461: 
 5462: Idiomatic usage of these two methods would probably look something
 5463: like
 5464: 
 5465:  foreach my $part ($resource->parts()) {
 5466:     my $dateStatus = $resource->getDateStatus($part);
 5467:     my $completionStatus = $resource->getCompletionStatus($part);
 5468: 
 5469:     or
 5470: 
 5471:     my $status = $resource->status($part);
 5472: 
 5473:     ... use it here ...
 5474:  }
 5475: 
 5476: Which you use depends on exactly what you are looking for. The
 5477: status() function has been optimized for the nav maps display and may
 5478: not precisely match what you need elsewhere.
 5479: 
 5480: The symbolic constants shown below can be accessed through the
 5481: resource object: C<$res->OPEN>.
 5482: 
 5483: =over 4
 5484: 
 5485: =item * B<getDateStatus>($part):
 5486: 
 5487: ($part defaults to 0). A convenience function that returns a symbolic
 5488: constant telling you about the date status of the part. The possible
 5489: return values are:
 5490: 
 5491: =back
 5492: 
 5493: B<Date Codes>
 5494: 
 5495: =over 4
 5496: 
 5497: =item * B<OPEN_LATER>:
 5498: 
 5499: The problem will be opened later.
 5500: 
 5501: =item * B<OPEN>:
 5502: 
 5503: Open and not yet due.
 5504: 
 5505: =item * B<PAST_DUE_ANSWER_LATER>:
 5506: 
 5507: The due date has passed, but the answer date has not yet arrived.
 5508: 
 5509: =item * B<PAST_DUE_NO_ANSWER>:
 5510: 
 5511: The due date has passed and there is no answer opening date set.
 5512: 
 5513: =item * B<ANSWER_OPEN>:
 5514: 
 5515: The answer date is here.
 5516: 
 5517: =item * B<NOTHING_SET>:
 5518: 
 5519: No dates have been set for this problem at all.
 5520: 
 5521: =item * B<PAST_DUE_ATMPT_ANS>:
 5522: 
 5523: The due date has passed, feedback is suppressed, the problem was attempted, and the answer date has not yet arrived.
 5524: 
 5525: =item * B<PAST_DUE_ATMPT_NOANS>:
 5526: 
 5527: The due date has passed, feedback is suppressed, the problem was attempted, and there is no answer opening date set.
 5528: 
 5529: =item * B<PAST_DUE_NO_ATMT_ANS>:
 5530: 
 5531: The due date has passed, feedback is suppressed, the problem was not attempted, and the answer date has not yet arrived.
 5532: 
 5533: =item * B<PAST_DUE_NO_ATMT_NOANS>:
 5534: 
 5535: The due date has passed, feedback is suppressed, the problem was not attempted, and there is no answer opening date set.
 5536: 
 5537: =item * B<NETWORK_FAILURE>:
 5538: 
 5539: The information is unknown due to network failure.
 5540: 
 5541: =back
 5542: 
 5543: =cut
 5544: 
 5545: # Apparently the compiler optimizes these into constants automatically
 5546: sub OPEN_LATER             { return 0; }
 5547: sub OPEN                   { return 1; }
 5548: sub PAST_DUE_NO_ANSWER     { return 2; }
 5549: sub PAST_DUE_ANSWER_LATER  { return 3; }
 5550: sub ANSWER_OPEN            { return 4; }
 5551: sub NOTHING_SET            { return 5; }
 5552: sub PAST_DUE_ATMPT_ANS     { return 6; }
 5553: sub PAST_DUE_ATMPT_NOANS   { return 7; }
 5554: sub PAST_DUE_NO_ATMT_ANS   { return 8; }
 5555: sub PAST_DUE_NO_ATMT_NOANS { return 9; }
 5556: sub NETWORK_FAILURE        { return 100; }
 5557: 
 5558: # getDateStatus gets the date status for a given problem part. 
 5559: # Because answer date, due date, and open date are fully independent
 5560: # (i.e., it is perfectly possible to *only* have an answer date), 
 5561: # we have to completely cover the 3x3 maxtrix of (answer, due, open) x
 5562: # (past, future, none given). This function handles this with a decision
 5563: # tree. Read the comments to follow the decision tree.
 5564: 
 5565: sub getDateStatus {
 5566:     my $self = shift;
 5567:     my $part = shift;
 5568:     $part = "0" if (!defined($part));
 5569: 
 5570:     # Always return network failure if there was one.
 5571:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
 5572: 
 5573:     my $now = time();
 5574: 
 5575:     my $open = $self->opendate($part);
 5576:     my $due = $self->duedate($part);
 5577:     my $answer = $self->answerdate($part);
 5578: 
 5579:     if (!$open && !$due && !$answer) {
 5580:         # no data on the problem at all
 5581:         # should this be the same as "open later"? think multipart.
 5582:         return $self->NOTHING_SET;
 5583:     }
 5584:     if (!$open || $now < $open) {return $self->OPEN_LATER}
 5585:     if (!$due || $now < $due) {return $self->OPEN}
 5586:     if ($answer && $now < $answer) {return $self->PAST_DUE_ANSWER_LATER}
 5587:     if ($answer) { return $self->ANSWER_OPEN; }
 5588:     return PAST_DUE_NO_ANSWER;
 5589: }
 5590: 
 5591: =pod
 5592: 
 5593: B<>
 5594: 
 5595: =over 4
 5596: 
 5597: =item * B<getCompletionStatus>($part):
 5598: 
 5599: ($part defaults to 0.) A convenience function that returns a symbolic
 5600: constant telling you about the completion status of the part, with the
 5601: following possible results:
 5602: 
 5603: =back
 5604: 
 5605: B<Completion Codes>
 5606: 
 5607: =over 4
 5608: 
 5609: =item * B<NOT_ATTEMPTED>:
 5610: 
 5611: Has not been attempted at all.
 5612: 
 5613: =item * B<INCORRECT>:
 5614: 
 5615: Attempted, but wrong by student.
 5616: 
 5617: =item * B<INCORRECT_BY_OVERRIDE>:
 5618: 
 5619: Attempted, but wrong by instructor override.
 5620: 
 5621: =item * B<CORRECT>:
 5622: 
 5623: Correct or correct by instructor.
 5624: 
 5625: =item * B<CORRECT_BY_OVERRIDE>:
 5626: 
 5627: Correct by instructor override.
 5628: 
 5629: =item * B<EXCUSED>:
 5630: 
 5631: Excused. Not yet implemented.
 5632: 
 5633: =item * B<NETWORK_FAILURE>:
 5634: 
 5635: Information not available due to network failure.
 5636: 
 5637: =item * B<ATTEMPTED>:
 5638: 
 5639: Attempted, and not yet graded.
 5640: 
 5641: =item * B<CREDIT_ATTEMPTED>:
 5642: 
 5643: Attempted, and credit received for attempt (survey and anonymous survey only).
 5644: 
 5645: =back
 5646: 
 5647: =cut
 5648: 
 5649: sub NOT_ATTEMPTED         { return 10; }
 5650: sub INCORRECT             { return 11; }
 5651: sub INCORRECT_BY_OVERRIDE { return 12; }
 5652: sub CORRECT               { return 13; }
 5653: sub CORRECT_BY_OVERRIDE   { return 14; }
 5654: sub EXCUSED               { return 15; }
 5655: sub ATTEMPTED             { return 16; }
 5656: sub CREDIT_ATTEMPTED      { return 17; }
 5657: 
 5658: sub getCompletionStatus {
 5659:     my $self = shift;
 5660:     my $part = shift;
 5661:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
 5662: 
 5663:     my $status = $self->queryRestoreHash('solved', $part);
 5664: 
 5665:     # Left as separate if statements in case we ever do more with this
 5666:     if ($status eq 'correct_by_student') {return $self->CORRECT;}
 5667:     if ($status eq 'correct_by_scantron') {return $self->CORRECT;}
 5668:     if ($status eq 'correct_by_override') {
 5669: 	return $self->CORRECT_BY_OVERRIDE;
 5670:     }
 5671:     if ($status eq 'incorrect_attempted') {return $self->INCORRECT; }
 5672:     if ($status eq 'incorrect_by_override') {return $self->INCORRECT_BY_OVERRIDE; }
 5673:     if ($status eq 'excused') {return $self->EXCUSED; }
 5674:     if ($status eq 'ungraded_attempted') {return $self->ATTEMPTED; }
 5675:     if ($status eq 'credit_attempted') {
 5676:         if ($self->is_anonsurvey($part) || $self->is_survey($part)) {
 5677:             return $self->CREDIT_ATTEMPTED;
 5678:         } else {
 5679:             return $self->ATTEMPTED;
 5680:         }
 5681:     }
 5682:     return $self->NOT_ATTEMPTED;
 5683: }
 5684: 
 5685: sub queryRestoreHash {
 5686:     my $self = shift;
 5687:     my $hashentry = shift;
 5688:     my $part = shift;
 5689:     $part = "0" if (!defined($part) || $part eq '');
 5690:     return $self->NETWORK_FAILURE if ($self->{NAV_MAP}->{NETWORK_FAILURE});
 5691: 
 5692:     $self->getReturnHash();
 5693: 
 5694:     return $self->{RETURN_HASH}->{'resource.'.$part.'.'.$hashentry};
 5695: }
 5696: 
 5697: =pod
 5698: 
 5699: B<Composite Status>
 5700: 
 5701: Along with directly returning the date or completion status, the
 5702: resource object includes a convenience function B<status>() that will
 5703: combine the two status tidbits into one composite status that can
 5704: represent the status of the resource as a whole. This method represents
 5705: the concept of the thing we want to display to the user on the nav maps
 5706: screen, which is a combination of completion and open status. The precise logic is
 5707: documented in the comments of the status method. The following results
 5708: may be returned, all available as methods on the resource object
 5709: ($res->NETWORK_FAILURE): In addition to the return values that match
 5710: the date or completion status, this function can return "ANSWER_SUBMITTED"
 5711: if that problemstatus parameter value is set to No, suppressing the
 5712: incorrect/correct feedback.
 5713: 
 5714: =over 4
 5715: 
 5716: =item * B<NETWORK_FAILURE>:
 5717: 
 5718: The network has failed and the information is not available.
 5719: 
 5720: =item * B<NOTHING_SET>:
 5721: 
 5722: No dates have been set for this problem (part) at all. (Because only
 5723: certain parts of a multi-part problem may be assigned, this can not be
 5724: collapsed into "open later", as we do not know a given part will EVER
 5725: be opened. For single part, this is the same as "OPEN_LATER".)
 5726: 
 5727: =item * B<CORRECT>:
 5728: 
 5729: For any reason at all, the part is considered correct.
 5730: 
 5731: =item * B<EXCUSED>:
 5732: 
 5733: For any reason at all, the problem is excused.
 5734: 
 5735: =item * B<PAST_DUE_NO_ANSWER>:
 5736: 
 5737: The problem is past due, not considered correct, and no answer date is
 5738: set.
 5739: 
 5740: =item * B<PAST_DUE_ANSWER_LATER>:
 5741: 
 5742: The problem is past due, not considered correct, and an answer date in
 5743: the future is set.
 5744: 
 5745: =item * B<PAST_DUE_ATMPT_ANS>:
 5746: 
 5747: The problem is past due, feedback is suppressed, the problem was
 5748: attempted and an answer date in the future is set.
 5749: 
 5750: =item * B<PAST_DUE_ATMPT_NOANS>:
 5751: 
 5752: The problem is past due, feedback is suppressed, the problem was
 5753: attempted and no answer date is set.
 5754: 
 5755: =item * B<PAST_DUE_NO_ATMT_ANS>:
 5756: 
 5757: The problem is past due, feedback is suppressed, the problem was
 5758: not attempted and an answer date in the future is set.
 5759: 
 5760: =item * B<PAST_DUE_NO_ATMT_NOANS>:
 5761: 
 5762: The problem is past due, feedback is suppressed, the problem was
 5763: not attempted and no answer date is set.
 5764: 
 5765: =item * B<ANSWER_OPEN>:
 5766: 
 5767: The problem is past due, not correct, and the answer is now available.
 5768: 
 5769: =item * B<OPEN_LATER>:
 5770: 
 5771: The problem is not yet open.
 5772: 
 5773: =item * B<TRIES_LEFT>:
 5774: 
 5775: The problem is open, has been tried, is not correct, but there are
 5776: tries left.
 5777: 
 5778: =item * B<INCORRECT>:
 5779: 
 5780: The problem is open, and all tries have been used without getting the
 5781: correct answer.
 5782: 
 5783: =item * B<OPEN>:
 5784: 
 5785: The item is open and not yet tried.
 5786: 
 5787: =item * B<ATTEMPTED>:
 5788: 
 5789: The problem has been attempted.
 5790: 
 5791: =item * B<CREDIT_ATTEMPTED>:
 5792: 
 5793: The problem has been attempted, and credit given for the attempt (survey and anonymous survey only).
 5794: 
 5795: =item * B<ANSWER_SUBMITTED>:
 5796: 
 5797: An answer has been submitted, but the student should not see it.
 5798: 
 5799: =back
 5800: 
 5801: =cut
 5802: 
 5803: sub TRIES_LEFT        { return 20; }
 5804: sub ANSWER_SUBMITTED  { return 21; }
 5805: sub PARTIALLY_CORRECT { return 22; }
 5806: 
 5807: sub RESERVED_LATER    { return 30; }
 5808: sub RESERVED          { return 31; }
 5809: sub RESERVED_LOCATION { return 32; }
 5810: sub RESERVABLE        { return 33; }
 5811: sub RESERVABLE_LATER  { return 34; }
 5812: sub NOTRESERVABLE     { return 35; }
 5813: sub NOT_IN_A_SLOT     { return 36; }
 5814: sub NEEDS_CHECKIN     { return 37; }
 5815: sub WAITING_FOR_GRADE { return 38; }
 5816: sub UNKNOWN           { return 39; }
 5817: 
 5818: sub status {
 5819:     my $self = shift;
 5820:     my $part = shift;
 5821:     if (!defined($part)) { $part = "0"; }
 5822:     my $completionStatus = $self->getCompletionStatus($part);
 5823:     my $dateStatus = $self->getDateStatus($part);
 5824: 
 5825:     # What we have is a two-dimensional matrix with 4 entries on one
 5826:     # dimension and 5 entries on the other, which we want to colorize,
 5827:     # plus network failure and "no date data at all".
 5828: 
 5829:     #if ($self->{RESOURCE_ERROR}) { return NETWORK_FAILURE; }
 5830:     if ($completionStatus == NETWORK_FAILURE) { return NETWORK_FAILURE; }
 5831: 
 5832:     my $suppressFeedback = 0;
 5833:     if (($self->problemstatus($part) eq 'no') ||
 5834:         ($self->problemstatus($part) eq 'no_feedback_ever')) {
 5835:         $suppressFeedback = 1;
 5836:     }
 5837:     # If there's an answer date and we're past it, don't
 5838:     # suppress the feedback; student should know
 5839:     if ($self->duedate($part) && $self->duedate($part) < time() &&
 5840: 	$self->answerdate($part) && $self->answerdate($part) < time()) {
 5841: 	$suppressFeedback = 0;
 5842:     }
 5843: 
 5844:     # There are a few whole rows we can dispose of:
 5845:     if ($completionStatus == CORRECT ||
 5846:         $completionStatus == CORRECT_BY_OVERRIDE ) {
 5847: 	if ( $suppressFeedback ) {
 5848:             if ($dateStatus == PAST_DUE_ANSWER_LATER ||
 5849:                 $dateStatus == PAST_DUE_NO_ANSWER ) {
 5850:                 if ($dateStatus == PAST_DUE_ANSWER_LATER) {
 5851:                     return PAST_DUE_ATMPT_ANS;
 5852:                 } else {
 5853:                     return PAST_DUE_ATMPT_NOANS;
 5854:                 }
 5855:             } else {
 5856:                 return ANSWER_SUBMITTED;
 5857:             }
 5858:         }
 5859: 	my $awarded=$self->awarded($part);
 5860: 	if ($awarded < 1 && $awarded > 0) {
 5861:             return PARTIALLY_CORRECT;
 5862: 	} elsif ($awarded<1) {
 5863: 	    return INCORRECT;
 5864: 	}
 5865: 	return CORRECT; 
 5866:     }
 5867: 
 5868:     # If it's WRONG... and not open
 5869:     if ( ($completionStatus == INCORRECT || 
 5870: 	  $completionStatus == INCORRECT_BY_OVERRIDE)
 5871: 	 && (!$self->opendate($part) ||  $self->opendate($part) > time()) ) {
 5872: 	return INCORRECT;
 5873:     }
 5874: 
 5875:     if ($completionStatus == ATTEMPTED) {
 5876:         return ATTEMPTED;
 5877:     }
 5878: 
 5879:     if ($completionStatus == CREDIT_ATTEMPTED) {
 5880:         return CREDIT_ATTEMPTED;
 5881:     }
 5882: 
 5883:     # If it's EXCUSED, then return that no matter what
 5884:     if ($completionStatus == EXCUSED) {
 5885:         return EXCUSED; 
 5886:     }
 5887: 
 5888:     if ($dateStatus == NOTHING_SET) {
 5889:         return NOTHING_SET;
 5890:     }
 5891: 
 5892:     # Now we're down to a 4 (incorrect, incorrect_override, not_attempted)
 5893:     # by 4 matrix (date statuses).
 5894: 
 5895:     if ($dateStatus == PAST_DUE_ANSWER_LATER ||
 5896:         $dateStatus == PAST_DUE_NO_ANSWER ) {
 5897:         if ($suppressFeedback) {
 5898:             if ($completionStatus == NOT_ATTEMPTED) {
 5899:                 if ($dateStatus == PAST_DUE_ANSWER_LATER) {
 5900:                     return PAST_DUE_NO_ATMT_ANS;
 5901:                 } else {
 5902:                     return PAST_DUE_NO_ATMT_NOANS;
 5903:                 }
 5904:             } else {
 5905:                 if ($dateStatus == PAST_DUE_ANSWER_LATER) {
 5906:                     return PAST_DUE_ATMPT_ANS;
 5907:                 } else {
 5908:                     return PAST_DUE_ATMPT_NOANS;
 5909:                 }
 5910:             }
 5911:         } else {
 5912:             return $dateStatus;
 5913:         }
 5914:     }
 5915: 
 5916:     if ($dateStatus == ANSWER_OPEN) {
 5917:         return ANSWER_OPEN;
 5918:     }
 5919: 
 5920:     # Now: (incorrect, incorrect_override, not_attempted) x 
 5921:     # (open_later), (open)
 5922:     
 5923:     if ($dateStatus == OPEN_LATER) {
 5924:         return OPEN_LATER;
 5925:     }
 5926: 
 5927:     # If it's WRONG...
 5928:     if ($completionStatus == INCORRECT || $completionStatus == INCORRECT_BY_OVERRIDE) {
 5929:         # and there are TRIES LEFT:
 5930:         if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
 5931:             return $suppressFeedback ? ANSWER_SUBMITTED : TRIES_LEFT;
 5932:         }
 5933:         return $suppressFeedback ? ANSWER_SUBMITTED : INCORRECT; # otherwise, return orange; student can't fix this
 5934:     }
 5935: 
 5936:     # Otherwise, it's untried and open
 5937:     return OPEN;
 5938: }
 5939: 
 5940: sub check_for_slot {
 5941:     my $self = shift;
 5942:     my $part = shift;
 5943:     my $symb = $self->{SYMB};
 5944:     my ($use_slots,$available,$availablestudent) = $self->slot_control($part);
 5945:     if (($use_slots ne '') && ($use_slots !~ /^\s*no\s*$/i)) {
 5946:         my @slots = (split(/:/,$availablestudent),split(/:/,$available));
 5947:         my $cid=$env{'request.course.id'};
 5948:         my $cdom=$env{'course.'.$cid.'.domain'};
 5949:         my $cnum=$env{'course.'.$cid.'.num'};
 5950:         my $now = time;
 5951:         my $num_usable_slots = 0;
 5952:         my ($checkedin,$checkedinslot,%consumed_uniq,%slots);
 5953:         if (@slots > 0) {
 5954:             %slots=&Apache::lonnet::get('slots',[@slots],$cdom,$cnum);
 5955:             if (&Apache::lonnet::error(%slots)) {
 5956:                 return (UNKNOWN);
 5957:             }
 5958:             my @sorted_slots = &Apache::loncommon::sorted_slots(\@slots,\%slots,'starttime');
 5959:             foreach my $slot_name (@sorted_slots) {
 5960:                 next if (!defined($slots{$slot_name}) || !ref($slots{$slot_name}));
 5961:                 my $end = $slots{$slot_name}->{'endtime'};
 5962:                 my $start = $slots{$slot_name}->{'starttime'};
 5963:                 my $ip = $slots{$slot_name}->{'ip'};
 5964:                 if ($self->simpleStatus() == OPEN) {
 5965:                     if ($end > $now) {
 5966:                         if ($start > $now) {
 5967:                             return (RESERVED_LATER,$start,$slot_name);
 5968:                         } else {
 5969:                             if ($ip ne '') {
 5970:                                 if (!&Apache::loncommon::check_ip_acc($ip)) {
 5971:                                     return (RESERVED_LOCATION,$end,$slot_name);
 5972:                                 }
 5973:                             }
 5974:                             my @proctors;
 5975:                             if ($slots{$slot_name}->{'proctor'} ne '') {
 5976:                                 @proctors = split(',',$slots{$slot_name}->{'proctor'});
 5977:                             }
 5978:                             if (@proctors > 0) {
 5979:                                 ($checkedin,$checkedinslot) = $self->checkedin();
 5980:                                 unless ((grep(/^\Q$checkedin\E/,@proctors)) &&
 5981:                                         ($checkedinslot eq $slot_name)) {
 5982:                                     return (NEEDS_CHECKIN,$end,$slot_name); 
 5983:                                 }
 5984:                             }
 5985:                             return (RESERVED,$end,$slot_name);
 5986:                         }
 5987:                     }
 5988:                 } elsif ($end > $now) {
 5989:                     $num_usable_slots ++;
 5990:                 }
 5991:             }
 5992:             my ($is_correct,$wait_for_grade);
 5993:             if ($self->is_task()) {
 5994:                 my $taskstatus = $self->taskstatus();
 5995:                 $is_correct = (($taskstatus eq 'pass') || 
 5996:                                ($self->solved() =~ /^correct_/));
 5997:                 unless ($taskstatus =~ /^(?:pass|fail)$/) {
 5998:                     $wait_for_grade = 1;
 5999:                 }
 6000:             } else {
 6001:                 unless ($self->completable()) {
 6002:                     $wait_for_grade = 1;
 6003:                 }
 6004:                 unless (($self->problemstatus($part) eq 'no') ||
 6005:                         ($self->problemstatus($part) eq 'no_feedback_ever')) {
 6006:                     $is_correct = ($self->solved($part) =~ /^correct_/);
 6007:                     $wait_for_grade = 0;
 6008:                 }
 6009:             }
 6010:             ($checkedin,$checkedinslot) = $self->checkedin();
 6011:             if ($checkedin) {
 6012:                 if (ref($slots{$checkedinslot}) eq 'HASH') {
 6013:                     $consumed_uniq{$checkedinslot} = $slots{$checkedinslot}{'uniqueperiod'};
 6014:                 }
 6015:                 if ($wait_for_grade) {
 6016:                     return (WAITING_FOR_GRADE);
 6017:                 } elsif ($is_correct) {
 6018:                     return (CORRECT); 
 6019:                 }
 6020:             }
 6021:             if ($num_usable_slots) {
 6022:                 return(NOT_IN_A_SLOT);
 6023:             }
 6024:         }
 6025:         my $reservable = &Apache::lonnet::get_reservable_slots($cnum,$cdom,$env{'user.name'},
 6026:                                                                $env{'user.domain'});
 6027:         if (ref($reservable) eq 'HASH') {
 6028:             if ((ref($reservable->{'now_order'}) eq 'ARRAY') && (ref($reservable->{'now'}) eq 'HASH')) {
 6029:                 foreach my $slot (reverse (@{$reservable->{'now_order'}})) {
 6030:                     my $canuse;
 6031:                     if (($reservable->{'now'}{$slot}{'symb'} eq '') ||
 6032:                         ($reservable->{'now'}{$slot}{'symb'} eq $symb)) {
 6033:                         $canuse = 1;
 6034:                     }
 6035:                     if ($canuse) {
 6036:                         if ($checkedin) {
 6037:                             if (ref($consumed_uniq{$checkedinslot}) eq 'ARRAY') {
 6038:                                 my ($uniqstart,$uniqend)=@{$consumed_uniq{$checkedinslot}};
 6039:                                 if ($reservable->{'now'}{$slot}{'uniqueperiod'} =~ /^(\d+),(\d+)$/) {
 6040:                                     my ($new_uniq_start,$new_uniq_end) = ($1,$2);
 6041:                                     next if (!
 6042:                                         ($uniqstart < $new_uniq_start && $uniqend < $new_uniq_start) ||
 6043:                                         ($uniqstart > $new_uniq_end   &&  $uniqend > $new_uniq_end  ));
 6044:                                 }
 6045:                             }
 6046:                         }
 6047:                         return(RESERVABLE,$reservable->{'now'}{$slot}{'endreserve'});
 6048:                     }
 6049:                 }
 6050:             }
 6051:             if ((ref($reservable->{'future_order'}) eq 'ARRAY') && (ref($reservable->{'future'}) eq 'HASH')) {
 6052:                 foreach my $slot (@{$reservable->{'future_order'}}) {
 6053:                     my $canuse;
 6054:                     if (($reservable->{'future'}{$slot}{'symb'} eq '') ||
 6055:                         ($reservable->{'future'}{$slot}{'symb'} eq $symb)) {
 6056:                         $canuse = 1;
 6057:                     }
 6058:                     if ($canuse) {
 6059:                         if ($checkedin) {
 6060:                             if (ref($consumed_uniq{$checkedinslot}) eq 'ARRAY') {
 6061:                                 my ($uniqstart,$uniqend)=@{$consumed_uniq{$checkedinslot}};
 6062:                                 if ($reservable->{'future'}{$slot}{'uniqueperiod'} =~ /^(\d+),(\d+)$/) {
 6063:                                     my ($new_uniq_start,$new_uniq_end) = ($1,$2);
 6064:                                     next if (!
 6065:                                         ($uniqstart < $new_uniq_start && $uniqend < $new_uniq_start) ||
 6066:                                         ($uniqstart > $new_uniq_end   &&  $uniqend > $new_uniq_end  ));
 6067:                                 }
 6068:                             }
 6069:                         }
 6070:                         return(RESERVABLE_LATER,$reservable->{'future'}{$slot}{'startreserve'});
 6071:                     }
 6072:                 }
 6073:             }
 6074:         }
 6075:         return(NOTRESERVABLE);
 6076:     }
 6077:     return;
 6078: }
 6079: 
 6080: sub CLOSED { return 23; }
 6081: sub ERROR { return 24; }
 6082: 
 6083: =pod
 6084: 
 6085: B<Simple Status>
 6086: 
 6087: Convenience method B<simpleStatus> provides a "simple status" for the resource.
 6088: "Simple status" corresponds to "which icon is shown on the
 6089: Navmaps". There are six "simple" statuses:
 6090: 
 6091: =over 4
 6092: 
 6093: =item * B<CLOSED>: The problem is currently closed. (No icon shown.)
 6094: 
 6095: =item * B<OPEN>: The problem is open and unattempted.
 6096: 
 6097: =item * B<CORRECT>: The problem is correct for any reason.
 6098: 
 6099: =item * B<INCORRECT>: The problem is incorrect and can still be
 6100: completed successfully.
 6101: 
 6102: =item * B<ATTEMPTED>: The problem has been attempted, but the student
 6103: does not know if they are correct. (The ellipsis icon.)
 6104: 
 6105: =item * B<ERROR>: There is an error retrieving information about this
 6106: problem.
 6107: 
 6108: =back
 6109: 
 6110: =cut
 6111: 
 6112: # This hash maps the composite status to this simple status, and
 6113: # can be used directly, if you like
 6114: my %compositeToSimple = 
 6115:     (
 6116:       NETWORK_FAILURE()       => ERROR,
 6117:       NOTHING_SET()           => CLOSED,
 6118:       CORRECT()               => CORRECT,
 6119:       PARTIALLY_CORRECT()     => PARTIALLY_CORRECT,
 6120:       EXCUSED()               => CORRECT,
 6121:       PAST_DUE_NO_ANSWER()    => INCORRECT,
 6122:       PAST_DUE_ANSWER_LATER() => INCORRECT,
 6123:       PAST_DUE_ATMPT_ANS()    => ATTEMPTED,
 6124:       PAST_DUE_ATMPT_NOANS()  => ATTEMPTED,
 6125:       PAST_DUE_NO_ATMT_ANS()  => CLOSED,
 6126:       PAST_DUE_NO_ATMT_NOANS() => CLOSED,
 6127:       ANSWER_OPEN()           => INCORRECT,
 6128:       OPEN_LATER()            => CLOSED,
 6129:       TRIES_LEFT()            => OPEN,
 6130:       INCORRECT()             => INCORRECT,
 6131:       OPEN()                  => OPEN,
 6132:       ATTEMPTED()             => ATTEMPTED,
 6133:       CREDIT_ATTEMPTED()      => CORRECT,
 6134:       ANSWER_SUBMITTED()      => ATTEMPTED
 6135:      );
 6136: 
 6137: sub simpleStatus {
 6138:     my $self = shift;
 6139:     my $part = shift;
 6140:     my $status = $self->status($part);
 6141:     return $compositeToSimple{$status};
 6142: }
 6143: 
 6144: =pod
 6145: 
 6146: B<simpleStatusCount> will return an array reference containing, in
 6147: this order, the number of OPEN, CLOSED, CORRECT, INCORRECT, ATTEMPTED,
 6148: and ERROR parts the given problem has.
 6149: 
 6150: =cut
 6151:     
 6152: # This maps the status to the slot we want to increment
 6153: my %statusToSlotMap = 
 6154:     (
 6155:      OPEN()      => 0,
 6156:      CLOSED()    => 1,
 6157:      CORRECT()   => 2,
 6158:      INCORRECT() => 3,
 6159:      ATTEMPTED() => 4,
 6160:      ERROR()     => 5
 6161:      );
 6162: 
 6163: sub statusToSlot { return $statusToSlotMap{shift()}; }
 6164: 
 6165: sub simpleStatusCount {
 6166:     my $self = shift;
 6167: 
 6168:     my @counts = (0, 0, 0, 0, 0, 0, 0);
 6169:     foreach my $part (@{$self->parts()}) {
 6170: 	$counts[$statusToSlotMap{$self->simpleStatus($part)}]++;
 6171:     }
 6172: 
 6173:     return \@counts;
 6174: }
 6175: 
 6176: =pod
 6177: 
 6178: B<Completable>
 6179: 
 6180: The completable method represents the concept of I<whether the student can
 6181: currently do the problem>. If the student can do the problem, which means
 6182: that it is open, there are tries left, and if the problem is manually graded
 6183: or the grade is suppressed via problemstatus, the student has not tried it
 6184: yet, then the method returns 1. Otherwise, it returns 0, to indicate that 
 6185: either the student has tried it and there is no feedback, or that for
 6186: some reason it is no longer completable (not open yet, successfully completed,
 6187: out of tries, etc.). As an example, this is used as the filter for the
 6188: "Uncompleted Homework" option for the nav maps.
 6189: 
 6190: If this does not quite meet your needs, do not fiddle with it (unless you are
 6191: fixing it to better match the student's conception of "completable" because
 6192: it's broken somehow)... make a new method.
 6193: 
 6194: =cut
 6195: 
 6196: sub completable {
 6197:     my $self = shift;
 6198:     if (!$self->is_problem()) { return 0; }
 6199:     my $partCount = $self->countParts();
 6200: 
 6201:     foreach my $part (@{$self->parts()}) {
 6202:         if ($part eq '0' && $partCount != 1) { next; }
 6203:         my $status = $self->status($part);
 6204:         # "If any of the parts are open, or have tries left (implies open),
 6205:         # and it is not "attempted" (manually graded problem), it is
 6206:         # not "complete"
 6207: 	if ($self->getCompletionStatus($part) == ATTEMPTED() ||
 6208:             $self->getCompletionStatus($part) == CREDIT_ATTEMPTED() ||
 6209: 	    $status == ANSWER_SUBMITTED() ) {
 6210: 	    # did this part already, as well as we can
 6211: 	    next;
 6212: 	}
 6213: 	if ($status == OPEN() || $status == TRIES_LEFT()) {
 6214: 	    return 1;
 6215: 	}
 6216:     }
 6217:         
 6218:     # If all the parts were complete, so was this problem.
 6219:     return 0;
 6220: }
 6221: 
 6222: =pod
 6223: 
 6224: B<Answerable>
 6225: 
 6226: The answerable method differs from the completable method in its handling of problem parts
 6227: for which feedback on correctness is suppressed, but the student still has tries left, and
 6228: the problem part is not past due, (i.e., the student could submit a different answer if
 6229: he/she so chose). For that case completable will return 0, whereas answerable will return 1.
 6230: 
 6231: =cut
 6232: 
 6233: sub answerable {
 6234:     my $self = shift;
 6235:     if (!$self->is_problem()) { return 0; }
 6236:     my $partCount = $self->countParts();
 6237:     foreach my $part (@{$self->parts()}) {
 6238:         if ($part eq '0' && $partCount != 1) { next; }
 6239:         my $status = $self->status($part);
 6240:         if ($self->getCompletionStatus($part) == ATTEMPTED() ||
 6241:             $self->getCompletionStatus($part) == CREDIT_ATTEMPTED() ||
 6242:             $status == ANSWER_SUBMITTED() ) {
 6243:             if ($self->tries($part) < $self->maxtries($part) || !$self->maxtries($part)) {
 6244:                 return 1;
 6245:             }
 6246:         }
 6247:         if ($status == OPEN() || $status == TRIES_LEFT() || $status == NETWORK_FAILURE()) {
 6248:             return 1;
 6249:         }
 6250:     }
 6251:     # None of the parts were answerable, so neither is this problem.
 6252:     return 0;
 6253: }
 6254: 
 6255: =pod
 6256: 
 6257: =head2 Resource/Nav Map Navigation
 6258: 
 6259: =over 4
 6260: 
 6261: =item * B<getNext>():
 6262: 
 6263: Retreive an array of the possible next resources after this
 6264: one. Always returns an array, even in the one- or zero-element case.
 6265: 
 6266: =item * B<getPrevious>():
 6267: 
 6268: Retreive an array of the possible previous resources from this
 6269: one. Always returns an array, even in the one- or zero-element case.
 6270: 
 6271: =cut
 6272: 
 6273: sub getNext {
 6274:     my $self = shift;
 6275:     my @branches;
 6276:     my $to = $self->to();
 6277:     foreach my $branch ( split(/,/, $to) ) {
 6278:         my $choice = $self->{NAV_MAP}->getById($branch);
 6279:         #if (!$choice->condition()) { next; }
 6280:         my $next = $choice->goesto();
 6281:         $next = $self->{NAV_MAP}->getById($next);
 6282: 
 6283:         push @branches, $next;
 6284:     }
 6285:     return \@branches;
 6286: }
 6287: 
 6288: sub getPrevious {
 6289:     my $self = shift;
 6290:     my @branches;
 6291:     my $from = $self->from();
 6292:     foreach my $branch ( split(/,/, $from)) {
 6293:         my $choice = $self->{NAV_MAP}->getById($branch);
 6294:         my $prev = $choice->comesfrom();
 6295:         $prev = $self->{NAV_MAP}->getById($prev);
 6296: 
 6297:         push @branches, $prev;
 6298:     }
 6299:     return \@branches;
 6300: }
 6301: 
 6302: sub browsePriv {
 6303:     my $self = shift;
 6304:     my $noblockcheck = shift;
 6305:     my $deeplinklisted = shift;
 6306:     if (defined($self->{BROWSE_PRIV})) {
 6307:         return $self->{BROWSE_PRIV};
 6308:     }
 6309:     my ($nodeeplinkcheck,$nodeeplinkout);
 6310:     if ($deeplinklisted) {
 6311:         my $deeplink = $self->deeplink(undef,'getlisted');
 6312:         if (($deeplink) && ($deeplink ne 'absent')) {
 6313:             $nodeeplinkcheck = 1;
 6314:         }
 6315:         $nodeeplinkout = 1;
 6316:     }
 6317:     $self->{BROWSE_PRIV} = &Apache::lonnet::allowed('bre',$self->src(),
 6318: 						    $self->{SYMB},undef,
 6319:                                                     undef,$noblockcheck,
 6320:                                                     undef,$nodeeplinkcheck,
 6321:                                                     $nodeeplinkout);
 6322: }
 6323: 
 6324: =pod
 6325: 
 6326: =back
 6327: 
 6328: =cut
 6329: 
 6330: 1;
 6331: 
 6332: __END__
 6333: 
 6334: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>