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