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