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