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