Annotation of loncom/interface/lonnavmaps.pm, revision 1.576

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

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