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

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

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