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

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

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