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

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

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