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

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

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