Annotation of loncom/interface/statistics/lonstathelpers.pm, revision 1.22
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.22 ! matthew 3: # $Id: lonstathelpers.pm,v 1.21 2004/09/15 21:07:34 matthew Exp $
1.1 matthew 4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: ####################################################
28: ####################################################
29:
30: =pod
31:
32: =head1 NAME
33:
34: Apache::lonstathelpers - helper routines used by statistics
35:
36: =head1 SYNOPSIS
37:
38: This module provides a place to consolidate much of the statistics
39: routines that are needed across multiple statistics functions.
40:
41: =head1 OVERVIEW
42:
43: =over 4
44:
45: =cut
46:
47: ####################################################
48: ####################################################
49: package Apache::lonstathelpers;
50:
51: use strict;
52: use Apache::lonnet();
53: use Apache::loncommon();
54: use Apache::lonhtmlcommon();
55: use Apache::loncoursedata();
56: use Apache::lonstatistics;
57: use Apache::lonlocal;
58: use HTML::Entities();
59: use Time::Local();
60: use Spreadsheet::WriteExcel();
1.8 matthew 61: use GDBM_File;
62: use Storable qw(freeze thaw);
1.1 matthew 63:
64: ####################################################
65: ####################################################
66:
67: =pod
68:
69: =item &render_resource($resource)
70:
71: Input: a resource generated from
72: &Apache::loncoursedata::get_sequence_assessment_data().
73:
74: Retunrs: a scalar containing html for a rendering of the problem
75: within a table.
76:
77: =cut
78:
79: ####################################################
80: ####################################################
81: sub render_resource {
82: my ($resource) = @_;
83: ##
84: ## Render the problem
85: my $base;
86: ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
87: $base = "http://".$ENV{'SERVER_NAME'}.$base;
88: my $rendered_problem =
89: &Apache::lonnet::ssi_body($resource->{'src'});
90: $rendered_problem =~ s/<\s*form\s*/<nop /g;
91: $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
92: return '<table bgcolor="ffffff"><tr><td>'.
93: '<base href="'.$base.'" />'.
94: $rendered_problem.
95: '</td></tr></table>';
96: }
1.2 matthew 97:
98: ####################################################
99: ####################################################
100:
101: =pod
102:
103: =item &ProblemSelector($AcceptedResponseTypes)
104:
105: Input: scalar containing regular expression which matches response
106: types to show. '.' will yield all, '(option|radiobutton)' will match
107: all option response and radiobutton problems.
108:
109: Returns: A string containing html for a table which lists the sequences
110: and their contents. A radiobutton is provided for each problem.
1.14 matthew 111: Skips 'survey' problems.
1.2 matthew 112:
113: =cut
114:
115: ####################################################
116: ####################################################
117: sub ProblemSelector {
118: my ($AcceptedResponseTypes) = @_;
119: my $Str;
120: $Str = "\n<table>\n";
1.12 matthew 121: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess('all')) {
1.2 matthew 122: next if ($seq->{'num_assess'}<1);
123: my $seq_str = '';
124: foreach my $res (@{$seq->{'contents'}}) {
125: next if ($res->{'type'} ne 'assessment');
126: foreach my $part (@{$res->{'parts'}}) {
127: my $partdata = $res->{'partdata'}->{$part};
128: for (my $i=0;$i<scalar(@{$partdata->{'ResponseTypes'}});$i++){
129: my $respid = $partdata->{'ResponseIds'}->[$i];
130: my $resptype = $partdata->{'ResponseTypes'}->[$i];
131: if ($resptype =~ m/$AcceptedResponseTypes/) {
132: my $value = &make_target_id({symb=>$res->{'symb'},
133: part=>$part,
134: respid=>$respid,
135: resptype=>$resptype});
136: my $checked = '';
137: if ($ENV{'form.problemchoice'} eq $value) {
138: $checked = 'checked ';
139: }
140: my $title = $res->{'title'};
141: if (! defined($title) || $title eq '') {
142: ($title) = ($res->{'src'} =~ m:/([^/]*)$:);
143: }
144: $seq_str .= '<tr><td>'.
145: '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
146: '</td><td>'.
147: $resptype.'</td><td>'.
148: '<a href="'.$res->{'src'}.'">'.$title.'</a> ';
149: # '<a href="'.$res->{'src'}.'">'.$resptype.' '.$res->{'title'}.'</a> ';
1.5 matthew 150: if (scalar(@{$partdata->{'ResponseIds'}}) > 1) {
1.2 matthew 151: $seq_str .= &mt('response').' '.$respid;
152: }
153: $seq_str .= "</td></tr>\n";
154: }
155: }
156: }
157: }
158: if ($seq_str ne '') {
159: $Str .= '<tr><td> </td><td colspan="2"><b>'.$seq->{'title'}.'</b></td>'.
160: "</tr>\n".$seq_str;
161: }
162: }
163: $Str .= "</table>\n";
164: return $Str;
165: }
166:
167: ####################################################
168: ####################################################
169:
170: =pod
171:
1.21 matthew 172: =item &MultpleProblemSelector($navmap,$ResponseTypes,$selected,$inputname)
173:
174: Generate HTML with checkboxes for problem selection.
175:
176: Input:
177:
178: $navmap: a navmap object. If undef, navmaps will be called to create a
179: new object.
180:
181: $ResponseTypes: scalar containing regular expression which matches response
182: types. Only those problems which contain the given response type will be
183: shown.
184:
185: $selected: Scalar, Array, or hash reference of currently selected items.
186:
187: $inputname: The name of the form elements to use for the checkboxs.
188:
189: Returns: A string containing html for a table which lists the sequences
190: and their contents. A checkbox is provided for each problem.
191:
192: =cut
193:
194: ####################################################
195: ####################################################
196: sub MultipleProblemSelector {
197: my ($navmap,$ReponseTypes,$inputname,$formname)=@_;
198: my $cid = $ENV{'request.course.id'};
199: my $Str;
200: # Massage the input as needed.
201: if (! defined($navmap)) {
202: $navmap = Apache::lonnavmaps::navmap->new();
203: if (! defined($navmap)) {
204: $Str .=
205: '<h1>'.&mt('Error: cannot process course structure').'</h1>';
206: return $Str;
207: }
208: }
209: my $selected = {map { ($_,1) } (&get_selected_symbs($inputname))};
210: # Header
211: $Str .= <<"END";
212: <script>
213: function checkall(value, checkName) {
214: for (i=0; i<document.forms.$formname.elements.length; i++) {
215: ele = document.forms.$formname.elements[i];
216: if (ele.name == '$inputname') {
217: document.forms.$formname.elements[i].checked=value;
218: }
219: }
220: }
221: </script>
222: END
223: $Str .=
224: '<a href="javascript:checkall(true)">'.&mt('Select All').'</a>'.
225: (' 'x4).
226: '<a href="javascript:checkall(false)">'.&mt('Unselect All').'</a>';
227: $Str .= $/.'<table>'.$/;
228: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
229: my $sequence_string;
230: my @Accumulator = (&new_accumulator($ENV{'course.'.$cid.'.description'},
231: '',
232: '',
233: $inputname));
234: my @Sequence_Data;
235: while (my $curRes = $iterator->next()) {
236: if ($curRes == $iterator->END_MAP) {
237: if (ref($Accumulator[-1]) eq 'CODE') {
238: push(@Sequence_Data,&{$Accumulator[-1]}());
239: pop(@Accumulator);
240: }
241: } elsif ($curRes == $iterator->BEGIN_MAP) {
242: # Not much to do here.
243: }
244: next if (! ref($curRes));
245: if ($curRes->is_map) {
246: push(@Accumulator,&new_accumulator($curRes->title,
247: $curRes->src,
248: $curRes->symb,
249: $inputname));
250: } elsif ($curRes->is_problem) {
251: if (@Accumulator && $Accumulator[-1] ne '') {
252: &{$Accumulator[-1]}($curRes,
253: exists($selected->{$curRes->symb}));
254: }
255: }
256: }
257: my $course_seq = pop(@Sequence_Data);
258: foreach my $seq ($course_seq,@Sequence_Data) {
259: #my $seq = pop(@Sequence_Data);
260: next if (! defined($seq) || ref($seq) ne 'HASH');
261: $Str.= '<tr><td colspan="2">'.
262: '<b>'.&get_title($seq->{'title'},$seq->{'src'}).'</b>'.
263: '</td></tr>'.$/;
264: $Str.= $seq->{'html'};
265: }
266: $Str .= '</table>'.$/;
267: return $Str;
268: }
269:
270: sub get_title {
271: my ($title,$src) = @_;
272: if ($title eq '') {
273: ($title) = ($src =~ m|/([^/]+)$|);
274: } else {
275: $title =~ s/\:/:/g;
276: }
277: return $title;
278: }
279:
280: sub new_accumulator {
281: my ($title,$src,$symb,$inputname) = @_;
282: my $target;
283: return
284: sub {
285: if (@_) {
286: my ($res,$checked) = @_;
287: $target.='<tr><td>'.
288: '<input type="checkbox" name="'.$inputname.'" ';
289: if ($checked) {
290: $target .= 'checked ';
291: }
292: $target.=
293: 'value="'.&Apache::lonnet::escape($res->symb).'" />'.
294: '</td><td>'.&get_title($res->title,$res->symb).'</td>'.
295: '</tr>'.$/;
296: } else {
297: if (defined($target)) {
298: return { title => $title,
299: symb => $symb,
300: src => $src,
301: html => $target, };
302: }
303: return undef;
304: }
305: };
306: }
307:
308: sub get_selected_symbs {
309: my ($inputfield) = @_;
310: my $field = 'form.'.$inputfield;
311: my @Symbs;
312: if (exists($ENV{$field})) {
313: if (! ref($ENV{$field})) {
314: @Symbs = (&Apache::lonnet::unescape($ENV{$field}));
315: } else {
316: @Symbs = (map {&Apache::lonnet::unescape($_);} @{$ENV{$field}});
317: }
318: }
319: return @Symbs;
320: }
321:
322: ####################################################
323: ####################################################
324:
325: =pod
326:
1.2 matthew 327: =item &make_target_id($target)
328:
329: Inputs: Hash ref with the following entries:
330: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
331: $target->{'resptype'}.
332:
333: Returns: A string, suitable for a form parameter, which uniquely identifies
334: the problem, part, and response to do statistical analysis on.
335:
336: Used by Apache::lonstathelpers::ProblemSelector().
337:
338: =cut
339:
340: ####################################################
341: ####################################################
342: sub make_target_id {
343: my ($target) = @_;
344: my $id = &Apache::lonnet::escape($target->{'symb'}).':'.
345: &Apache::lonnet::escape($target->{'part'}).':'.
346: &Apache::lonnet::escape($target->{'respid'}).':'.
347: &Apache::lonnet::escape($target->{'resptype'});
348: return $id;
349: }
350:
351: ####################################################
352: ####################################################
353:
354: =pod
355:
356: =item &get_target_from_id($id)
357:
358: Inputs: $id, a scalar string from Apache::lonstathelpers::make_target_id().
359:
360: Returns: A hash reference, $target, containing the following keys:
361: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
362: $target->{'resptype'}.
363:
364: =cut
365:
366: ####################################################
367: ####################################################
368: sub get_target_from_id {
369: my ($id) = @_;
1.21 matthew 370: if (! ref($id)) {
371: my ($symb,$part,$respid,$resptype) = split(':',$id);
372: return ({ symb => &Apache::lonnet::unescape($symb),
373: part => &Apache::lonnet::unescape($part),
374: respid => &Apache::lonnet::unescape($respid),
375: resptype => &Apache::lonnet::unescape($resptype)});
376: } elsif (ref($id) eq 'ARRAY') {
377: my @Return;
378: foreach my $selected (@$id) {
379: my ($symb,$part,$respid,$resptype) = split(':',$selected);
380: push(@Return,{ symb => &Apache::lonnet::unescape($symb),
381: part => &Apache::lonnet::unescape($part),
382: respid => &Apache::lonnet::unescape($respid),
383: resptype => &Apache::lonnet::unescape($resptype)});
384: }
385: return \@Return;
386: }
1.2 matthew 387: }
388:
389: ####################################################
390: ####################################################
391:
392: =pod
393:
1.13 matthew 394: =item &get_prev_curr_next($target,$AcceptableResponseTypes,$granularity)
1.2 matthew 395:
396: Determine the problem parts or responses preceeding and following the
397: current resource.
398:
399: Inputs: $target (see &Apache::lonstathelpers::get_target_from_id())
400: $AcceptableResponseTypes, regular expression matching acceptable
401: response types,
1.13 matthew 402: $granularity, either 'part', 'response', or 'part_survey'
1.2 matthew 403:
404: Returns: three hash references, $prev, $curr, $next, which refer to the
405: preceeding, current, or following problem parts or responses, depending
406: on the value of $granularity. Values of undef indicate there is no
407: previous or next part/response. A value of undef for all three indicates
408: there was no match found to the current part/resource.
409:
410: The hash references contain the following keys:
411: symb, part, resource
412:
413: If $granularity eq 'response', the following ADDITIONAL keys will be present:
414: respid, resptype
415:
416: =cut
417:
418: ####################################################
419: ####################################################
420: sub get_prev_curr_next {
421: my ($target,$AcceptableResponseTypes,$granularity) = @_;
422: #
423: # Build an array with the data we need to search through
424: my @Resource;
1.12 matthew 425: foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess('all')) {
1.2 matthew 426: foreach my $res (@{$seq->{'contents'}}) {
427: next if ($res->{'type'} ne 'assessment');
428: foreach my $part (@{$res->{'parts'}}) {
429: my $partdata = $res->{'partdata'}->{$part};
1.20 matthew 430: if ($partdata->{'Survey'} && ($granularity eq 'part_survey')){
431: push (@Resource,
432: { symb => $res->{symb},
433: part => $part,
434: resource => $res,
435: } );
1.13 matthew 436: } elsif ($granularity eq 'part') {
1.2 matthew 437: push (@Resource,
438: { symb => $res->{symb},
439: part => $part,
440: resource => $res,
441: } );
442: } elsif ($granularity eq 'response') {
443: for (my $i=0;
444: $i<scalar(@{$partdata->{'ResponseTypes'}});
445: $i++){
446: my $respid = $partdata->{'ResponseIds'}->[$i];
447: my $resptype = $partdata->{'ResponseTypes'}->[$i];
448: next if ($resptype !~ m/$AcceptableResponseTypes/);
449: push (@Resource,
450: { symb => $res->{symb},
451: part => $part,
452: respid => $partdata->{'ResponseIds'}->[$i],
453: resource => $res,
454: resptype => $resptype
455: } );
456: }
457: }
458: }
459: }
460: }
461: #
462: # Get the index of the current situation
463: my $curr_idx;
464: for ($curr_idx=0;$curr_idx<$#Resource;$curr_idx++) {
465: my $curr_item = $Resource[$curr_idx];
1.13 matthew 466: if ($granularity eq 'part' || $granularity eq 'part_survey') {
1.2 matthew 467: if ($curr_item->{'symb'} eq $target->{'symb'} &&
468: $curr_item->{'part'} eq $target->{'part'}) {
469: last;
470: }
471: } elsif ($granularity eq 'response') {
472: if ($curr_item->{'symb'} eq $target->{'symb'} &&
473: $curr_item->{'part'} eq $target->{'part'} &&
474: $curr_item->{'respid'} eq $target->{'respid'} &&
475: $curr_item->{'resptype'} eq $target->{'resptype'}) {
476: last;
477: }
478: }
479: }
480: my $curr_item = $Resource[$curr_idx];
1.13 matthew 481: if ($granularity eq 'part' || $granularity eq 'part_survey') {
1.2 matthew 482: if ($curr_item->{'symb'} ne $target->{'symb'} ||
483: $curr_item->{'part'} ne $target->{'part'}) {
484: # bogus symb - return nothing
485: return (undef,undef,undef);
486: }
487: } elsif ($granularity eq 'response') {
488: if ($curr_item->{'symb'} ne $target->{'symb'} ||
489: $curr_item->{'part'} ne $target->{'part'} ||
490: $curr_item->{'respid'} ne $target->{'respid'} ||
491: $curr_item->{'resptype'} ne $target->{'resptype'}){
492: # bogus symb - return nothing
493: return (undef,undef,undef);
494: }
495: }
496: #
497: # Now just pick up the data we need
498: my ($prev,$curr,$next);
499: if ($curr_idx == 0) {
500: $prev = undef;
501: $curr = $Resource[$curr_idx ];
502: $next = $Resource[$curr_idx+1];
503: } elsif ($curr_idx == $#Resource) {
504: $prev = $Resource[$curr_idx-1];
505: $curr = $Resource[$curr_idx ];
506: $next = undef;
507: } else {
508: $prev = $Resource[$curr_idx-1];
509: $curr = $Resource[$curr_idx ];
510: $next = $Resource[$curr_idx+1];
511: }
512: return ($prev,$curr,$next);
1.4 matthew 513: }
514:
1.9 matthew 515:
516: #####################################################
517: #####################################################
518:
519: =pod
520:
521: =item GetStudentAnswers($r,$problem,$Students)
522:
523: Determines the correct answer for a set of students on a given problem.
524: The students answers are stored in the student hashes pointed to by the
525: array @$Students under the key 'answer'.
526:
527: Inputs: $r
528: $problem: hash reference containing the keys 'resource', 'part', and 'respid'.
529: $Students: reference to array containing student hashes (need 'username',
530: 'domain').
531:
532: Returns: nothing
533:
534: =cut
535:
536: #####################################################
537: #####################################################
538: sub GetStudentAnswers {
1.12 matthew 539: my ($r,$problem,$Students,$formname,$inputname) = @_;
540: my $status_type;
541: if (defined($formname)) {
542: $status_type = 'inline';
543: } else {
544: $status_type = 'popup';
545: }
1.9 matthew 546: my $c = $r->connection();
547: my %Answers;
548: my ($resource,$partid,$respid) = ($problem->{'resource'},
549: $problem->{'part'},
550: $problem->{'respid'});
551: # Read in the cache (if it exists) before we start timing things.
552: &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
553: # Open progress window
554: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
555: ($r,'Student Answer Compilation Status',
1.12 matthew 556: 'Student Answer Compilation Progress', scalar(@$Students),
557: $status_type,undef,$formname,$inputname);
1.9 matthew 558: $r->rflush();
559: foreach my $student (@$Students) {
560: last if ($c->aborted());
561: my $sname = $student->{'username'};
562: my $sdom = $student->{'domain'};
563: my $answer = &Apache::lonstathelpers::analyze_problem_as_student
564: ($resource,$sname,$sdom,$partid,$respid);
565: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
566: &mt('last student'));
567: $student->{'answer'} = $answer;
568: }
569: &Apache::lonstathelpers::write_answer_cache();
570: return if ($c->aborted());
571: $r->rflush();
572: # close progress window
573: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
574: return;
575: }
1.4 matthew 576:
577: #####################################################
578: #####################################################
579:
580: =pod
581:
582: =item analyze_problem_as_student
583:
584: Analyzes a homework problem for a student and returns the correct answer
585: for the student. Attempts to put together an answer for problem types
586: that do not natively support it.
587:
588: Inputs: $resource: a resource object
589: $sname, $sdom, $partid, $respid
590:
591: Returns: $answer
592:
1.6 matthew 593: If $partid and $respid are specified, $answer is simply a scalar containing
594: the correct answer for the response.
595:
596: If $partid or $respid are undefined, $answer will be a hash reference with
597: keys $partid.'.'.$respid.'.answer'.
598:
1.4 matthew 599: =cut
600:
601: #####################################################
602: #####################################################
603: sub analyze_problem_as_student {
604: my ($resource,$sname,$sdom,$partid,$respid) = @_;
1.22 ! matthew 605: if (ref($resource) ne 'HASH') {
! 606: my $res = $resource;
! 607: $resource = { 'src' => $res->src,
! 608: 'symb' => $res->symb,
! 609: 'parts' => $res->parts };
! 610: foreach my $part (@{$resource->{'parts'}}) {
! 611: $resource->{'partdata'}->{$part}->{'ResponseIds'}=
! 612: [$res->responseIds($part)];
! 613: }
! 614: }
1.4 matthew 615: my $returnvalue;
616: my $url = $resource->{'src'};
617: my $symb = $resource->{'symb'};
1.8 matthew 618: my $answer = &get_from_answer_cache($sname,$sdom,$symb,$partid,$respid);
619: if (defined($answer)) {
620: return($answer);
621: }
1.4 matthew 622: my $courseid = $ENV{'request.course.id'};
623: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
624: 'grade_domain' => $sdom,
625: 'grade_username' => $sname,
626: 'grade_symb' => $symb,
627: 'grade_courseid' => $courseid));
628: (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
629: my %Answer=&Apache::lonnet::str2hash($Answ);
1.6 matthew 630: #
1.8 matthew 631: undef($answer);
632: foreach my $partid (@{$resource->{'parts'}}) {
1.6 matthew 633: my $partdata = $resource->{'partdata'}->{$partid};
634: foreach my $respid (@{$partdata->{'ResponseIds'}}) {
635: my $prefix = $partid.'.'.$respid;
636: my $key = $prefix.'.answer';
1.8 matthew 637: $answer->{$partid}->{$respid} = &get_answer($prefix,$key,%Answer);
1.6 matthew 638: }
1.8 matthew 639: }
640: &store_answer($sname,$sdom,$symb,undef,undef,$answer);
641: if (! defined($partid)) {
642: $returnvalue = $answer;
643: } elsif (! defined($respid)) {
644: $returnvalue = $answer->{$partid};
1.6 matthew 645: } else {
1.8 matthew 646: $returnvalue = $answer->{$partid}->{$respid};
1.6 matthew 647: }
648: return $returnvalue;
649: }
650:
651: sub get_answer {
652: my ($prefix,$key,%Answer) = @_;
653: my $returnvalue;
1.4 matthew 654: if (exists($Answer{$key})) {
655: my $student_answer = $Answer{$key}->[0];
656: if (! defined($student_answer)) {
657: $student_answer = $Answer{$key}->[1];
658: }
659: $returnvalue = $student_answer;
660: } else {
661: if (exists($Answer{$prefix.'.shown'})) {
662: # The response has foils
663: my %values;
664: while (my ($k,$v) = each(%Answer)) {
665: next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
666: my $foilname = $2;
667: $values{$foilname}=$v;
668: }
669: foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
670: if (ref($values{$foil}) eq 'ARRAY') {
1.10 albertel 671: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
672: join(',',map {&HTML::Entities::encode($_,'<>&"')} @{$values{$foil}}).'&';
1.4 matthew 673: } else {
1.10 albertel 674: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
675: &HTML::Entities::encode($values{$foil},'<>&"').'&';
1.4 matthew 676: }
677: }
678: $returnvalue =~ s/ /\%20/g;
679: chop ($returnvalue);
680: }
681: }
682: return $returnvalue;
683: }
1.8 matthew 684:
685:
686: #####################################################
687: #####################################################
688:
689: =pod
690:
691: =item Caching routines
692:
693: =over 4
694:
695: =item &load_answer_cache($symb)
696:
697: Loads the cache for the given symb into memory from disk.
698: Requires the cache filename be set.
699: Only should be called by &ensure_proper_cache.
700:
701: =cut
702:
703: #####################################################
704: #####################################################
705: {
706: my $cache_filename = undef;
707: my $current_symb = undef;
708: my %cache;
709:
710: sub load_answer_cache {
711: my ($symb) = @_;
712: return if (! defined($cache_filename));
713: if (! defined($current_symb) || $current_symb ne $symb) {
714: undef(%cache);
715: my $storedstring;
716: my %cache_db;
717: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_READER(),0640)) {
718: $storedstring = $cache_db{&Apache::lonnet::escape($symb)};
719: untie(%cache_db);
720: }
721: if (defined($storedstring)) {
722: %cache = %{thaw($storedstring)};
723: }
724: }
725: return;
726: }
727:
728: #####################################################
729: #####################################################
730:
731: =pod
732:
733: =item &get_from_answer_cache($sname,$sdom,$symb,$partid,$respid)
734:
735: Returns the appropriate data from the cache, or undef if no data exists.
736: If $respid is undefined, a hash ref containing the answers for the given
737: $partid is returned. If $partid is undefined, a hash ref containing answers
738: for all of the parts is returned.
739:
740: =cut
741:
742: #####################################################
743: #####################################################
744: sub get_from_answer_cache {
745: my ($sname,$sdom,$symb,$partid,$respid) = @_;
746: &ensure_proper_cache($symb);
747: my $returnvalue;
748: if (exists($cache{$sname.':'.$sdom}) &&
749: ref($cache{$sname.':'.$sdom}) eq 'HASH') {
750: if (defined($partid) &&
751: exists($cache{$sname.':'.$sdom}->{$partid})) {
752: if (defined($respid) &&
753: exists($cache{$sname.':'.$sdom}->{$partid}->{$respid})) {
754: $returnvalue = $cache{$sname.':'.$sdom}->{$partid}->{$respid};
755: } else {
756: $returnvalue = $cache{$sname.':'.$sdom}->{$partid};
757: }
758: } else {
759: $returnvalue = $cache{$sname.':'.$sdom};
760: }
761: } else {
762: $returnvalue = undef;
763: }
764: return $returnvalue;
765: }
766:
767: #####################################################
768: #####################################################
769:
770: =pod
771:
772: =item &write_answer_cache($symb)
773:
774: Writes the in memory cache to disk so that it can be read in with
775: &load_answer_cache($symb).
776:
777: =cut
778:
779: #####################################################
780: #####################################################
781: sub write_answer_cache {
782: return if (! defined($current_symb) || ! defined($cache_filename));
783: my %cache_db;
784: my $key = &Apache::lonnet::escape($current_symb);
785: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_WRCREAT(),0640)) {
786: my $storestring = freeze(\%cache);
787: $cache_db{$key}=$storestring;
788: $cache_db{$key.'.time'}=time;
789: untie(%cache_db);
790: }
791: undef(%cache);
792: undef($current_symb);
793: undef($cache_filename);
794: return;
795: }
796:
797: #####################################################
798: #####################################################
799:
800: =pod
801:
802: =item &ensure_proper_cache($symb)
803:
804: Called to make sure we have the proper cache set up. This is called
805: prior to every answer lookup.
806:
807: =cut
808:
809: #####################################################
810: #####################################################
811: sub ensure_proper_cache {
812: my ($symb) = @_;
813: my $cid = $ENV{'request.course.id'};
814: my $new_filename = '/home/httpd/perl/tmp/'.
1.18 matthew 815: 'problemanalysis_'.$cid.'_answer_cache.db';
1.8 matthew 816: if (! defined($cache_filename) ||
817: $cache_filename ne $new_filename ||
818: ! defined($current_symb) ||
819: $current_symb ne $symb) {
820: $cache_filename = $new_filename;
821: # Notice: $current_symb is not set to $symb until after the cache is
822: # loaded. This is what tells &load_answer_cache to load in a new
823: # symb cache.
824: &load_answer_cache($symb);
825: $current_symb = $symb;
826: }
827: }
828:
829: #####################################################
830: #####################################################
831:
832: =pod
833:
834: =item &store_answer($sname,$sdom,$symb,$partid,$respid,$dataset)
835:
836: Stores the answer data in the in memory cache.
837:
838: =cut
839:
840: #####################################################
841: #####################################################
842: sub store_answer {
843: my ($sname,$sdom,$symb,$partid,$respid,$dataset) = @_;
844: return if ($symb ne $current_symb);
845: if (defined($partid)) {
846: if (defined($respid)) {
847: $cache{$sname.':'.$sdom}->{$partid}->{$respid} = $dataset;
848: } else {
849: $cache{$sname.':'.$sdom}->{$partid} = $dataset;
850: }
851: } else {
852: $cache{$sname.':'.$sdom}=$dataset;
853: }
854: return;
855: }
856:
857: }
858: #####################################################
859: #####################################################
860:
861: =pod
862:
863: =back
864:
865: =cut
866:
867: #####################################################
868: #####################################################
1.4 matthew 869:
870: ##
871: ## The following is copied from datecalc1.pl, part of the
872: ## Spreadsheet::WriteExcel CPAN module.
873: ##
874: ##
875: ######################################################################
876: #
877: # Demonstration of writing date/time cells to Excel spreadsheets,
878: # using UNIX/Perl time as source of date/time.
879: #
880: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
881: #
882: ######################################################################
883: #
884: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
885: # measured in seconds.
886: #
887: # An Excel file can use exactly one of two different date/time systems.
888: # In these systems, a floating point number represents the number of days
889: # (and fractional parts of the day) since a start point. The floating point
890: # number is referred to as a 'serial'.
891: # The two systems ('1900' and '1904') use different starting points:
892: # '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
893: # a leap year - see:
894: # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
895: # for the excuse^H^H^H^H^H^Hreason.
896: # '1904'; '1.00' is 2 Jan 1904.
897: #
898: # The '1904' system is the default for Apple Macs. Windows versions of
899: # Excel have the option to use the '1904' system.
900: #
901: # Note that Visual Basic's "DateSerial" function does NOT erroneously
902: # regard 1900 as a leap year, and thus its serials do not agree with
903: # the 1900 serials of Excel for dates before 1 Mar 1900.
904: #
905: # Note that StarOffice (at least at version 5.2) does NOT erroneously
906: # regard 1900 as a leap year, and thus its serials do not agree with
907: # the 1900 serials of Excel for dates before 1 Mar 1900.
908: #
909: ######################################################################
910: #
911: # Calculation description
912: # =======================
913: #
914: # 1900 system
915: # -----------
916: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
917: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
918: # were leap years with an extra day.
919: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
920: # 1 Jan 1970.
921: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
922: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
923: #
924: # 1904 system
925: # -----------
926: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
927: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
928: # were leap years with an extra day.
929: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
930: # 1 Jan 1970.
931: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
932: #
933: ######################################################################
934: #
935: # Copyright (c) 2000, Andrew Benham.
936: # This program is free software. It may be used, redistributed and/or
937: # modified under the same terms as Perl itself.
938: #
939: # Andrew Benham, adsb@bigfoot.com
940: # London, United Kingdom
941: # 11 Nov 2000
942: #
943: ######################################################################
944: #-----------------------------------------------------------
945: # calc_serial()
946: #
947: # Called with (up to) 2 parameters.
948: # 1. Unix timestamp. If omitted, uses current time.
949: # 2. GMT flag. Set to '1' to return serial in GMT.
950: # If omitted, returns serial in appropriate timezone.
951: #
952: # Returns date/time serial according to $DATE_SYSTEM selected
953: #-----------------------------------------------------------
954: sub calc_serial {
955: # Use 1900 date system on all platforms other than Apple Mac (for which
956: # use 1904 date system).
957: my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
958: my $time = (defined $_[0]) ? $_[0] : time();
959: my $gmtflag = (defined $_[1]) ? $_[1] : 0;
960: #
961: # Divide timestamp by number of seconds in a day.
962: # This gives a date serial with '0' on 1 Jan 1970.
963: my $serial = $time / 86400;
964: #
965: # Adjust the date serial by the offset appropriate to the
966: # currently selected system (1900/1904).
967: if ($DATE_SYSTEM == 0) { # use 1900 system
968: $serial += 25569;
969: } else { # use 1904 system
970: $serial += 24107;
971: }
972: #
973: unless ($gmtflag) {
974: # Now have a 'raw' serial with the right offset. But this
975: # gives a serial in GMT, which is false unless the timezone
976: # is GMT. We need to adjust the serial by the appropriate
977: # timezone offset.
978: # Calculate the appropriate timezone offset by seeing what
979: # the differences between localtime and gmtime for the given
980: # time are.
981: #
982: my @gmtime = gmtime($time);
983: my @ltime = localtime($time);
984: #
985: # For the first 7 elements of the two arrays, adjust the
986: # date serial where the elements differ.
987: for (0 .. 6) {
988: my $diff = $ltime[$_] - $gmtime[$_];
989: if ($diff) {
990: $serial += _adjustment($diff,$_);
991: }
992: }
993: }
994: #
995: # Perpetuate the error that 1900 was a leap year by decrementing
996: # the serial if we're using the 1900 system and the date is prior to
997: # 1 Mar 1900. This has the effect of making serial value '60'
998: # 29 Feb 1900.
999: #
1000: # This fix only has any effect if UNIX/Perl time on the platform
1001: # can represent 1900. Many can't.
1002: #
1003: unless ($DATE_SYSTEM) {
1004: $serial-- if ($serial < 61); # '61' is 1 Mar 1900
1005: }
1006: return $serial;
1007: }
1008:
1009: sub _adjustment {
1010: # Based on the difference in the localtime/gmtime array elements
1011: # number, return the adjustment required to the serial.
1012: #
1013: # We only look at some elements of the localtime/gmtime arrays:
1014: # seconds unlikely to be different as all known timezones
1015: # have an offset of integral multiples of 15 minutes,
1016: # but it's easy to do.
1017: # minutes will be different for timezone offsets which are
1018: # not an exact number of hours.
1019: # hours very likely to be different.
1020: # weekday will differ when localtime/gmtime difference
1021: # straddles midnight.
1022: #
1023: # Assume that difference between localtime and gmtime is less than
1024: # 5 days, then don't have to do maths for day of month, month number,
1025: # year number, etc...
1026: #
1027: my ($delta,$element) = @_;
1028: my $adjust = 0;
1029: #
1030: if ($element == 0) { # Seconds
1031: $adjust = $delta/86400; # 60 * 60 * 24
1032: } elsif ($element == 1) { # Minutes
1033: $adjust = $delta/1440; # 60 * 24
1034: } elsif ($element == 2) { # Hours
1035: $adjust = $delta/24; # 24
1036: } elsif ($element == 6) { # Day of week number
1037: # Catch difference straddling Sat/Sun in either direction
1038: $delta += 7 if ($delta < -4);
1039: $delta -= 7 if ($delta > 4);
1040: #
1041: $adjust = $delta;
1042: }
1043: return $adjust;
1044: }
1045:
1046: ###########################################################
1047: ###########################################################
1048:
1049: =pod
1050:
1051: =item get_problem_data
1052:
1053: Returns a data structure describing the problem.
1054:
1055: Inputs: $url
1056:
1057: Returns: %Partdata
1058:
1059: =cut
1060:
1061: ## note: we must force each foil and option to not begin or end with
1062: ## spaces as they are stored without such data.
1063: ##
1064: ###########################################################
1065: ###########################################################
1066: sub get_problem_data {
1067: my ($url) = @_;
1068: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
1069: (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
1070: my %Answer;
1071: %Answer=&Apache::lonnet::str2hash($Answ);
1072: my %Partdata;
1073: foreach my $part (@{$Answer{'parts'}}) {
1074: while (my($key,$value) = each(%Answer)) {
1075: #
1076: # Logging code:
1.7 matthew 1077: if (0) {
1.4 matthew 1078: &Apache::lonnet::logthis($part.' got key "'.$key.'"');
1079: if (ref($value) eq 'ARRAY') {
1080: &Apache::lonnet::logthis(' @'.join(',',@$value));
1081: } else {
1082: &Apache::lonnet::logthis(' '.$value);
1083: }
1084: }
1085: # End of logging code
1086: next if ($key !~ /^$part/);
1087: $key =~ s/^$part\.//;
1088: if (ref($value) eq 'ARRAY') {
1089: if ($key eq 'options') {
1090: $Partdata{$part}->{'_Options'}=$value;
1091: } elsif ($key eq 'concepts') {
1092: $Partdata{$part}->{'_Concepts'}=$value;
1093: } elsif ($key =~ /^concept\.(.*)$/) {
1094: my $concept = $1;
1095: foreach my $foil (@$value) {
1096: $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
1097: $concept;
1098: }
1099: } elsif ($key =~ /^(incorrect|answer|ans_low|ans_high)$/) {
1100: $Partdata{$part}->{$key}=$value;
1101: }
1102: } else {
1103: if ($key=~ /^foil\.text\.(.*)$/) {
1104: my $foil = $1;
1105: $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
1106: $value =~ s/(\s*$|^\s*)//g;
1107: $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
1108: } elsif ($key =~ /^foil\.value\.(.*)$/) {
1109: my $foil = $1;
1110: $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
1111: }
1112: }
1113: }
1114: }
1115: return %Partdata;
1.5 matthew 1116: }
1117:
1118: ####################################################
1119: ####################################################
1120:
1121: =pod
1122:
1123: =item &limit_by_time()
1124:
1125: =cut
1126:
1127: ####################################################
1128: ####################################################
1129: sub limit_by_time_form {
1130: my $Starttime_form = '';
1131: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
1132: ('limitby_startdate');
1133: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
1134: ('limitby_enddate');
1135: if (! defined($endtime)) {
1136: $endtime = time;
1137: }
1138: if (! defined($starttime)) {
1139: $starttime = $endtime - 60*60*24*7;
1140: }
1141: my $state;
1142: if (&limit_by_time()) {
1143: $state = '';
1144: } else {
1145: $state = 'disabled';
1146: }
1147: my $startdateform = &Apache::lonhtmlcommon::date_setter
1148: ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
1149: my $enddateform = &Apache::lonhtmlcommon::date_setter
1150: ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
1151: my $Str;
1152: $Str .= '<script language="Javascript" >';
1153: $Str .= 'function toggle_limitby_activity(state) {';
1154: $Str .= ' if (state) {';
1155: $Str .= ' limitby_startdate_enable();';
1156: $Str .= ' limitby_enddate_enable();';
1157: $Str .= ' } else {';
1158: $Str .= ' limitby_startdate_disable();';
1159: $Str .= ' limitby_enddate_disable();';
1160: $Str .= ' }';
1161: $Str .= '}';
1162: $Str .= '</script>';
1163: $Str .= '<fieldset>';
1164: my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
1165: if (&limit_by_time()) {
1166: $timecheckbox .= ' checked ';
1167: }
1168: $timecheckbox .= 'OnChange="javascript:toggle_limitby_activity(this.checked);" ';
1169: $timecheckbox .= ' />';
1170: $Str .= '<legend>'.&mt('[_1] Limit by time',$timecheckbox).'</legend>';
1171: $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
1172: $Str .= &mt(' End Time: [_1]',$enddateform).'<br />';
1173: $Str .= '</fieldset>';
1174: return $Str;
1175: }
1176:
1177: sub limit_by_time {
1178: if (exists($ENV{'form.limit_by_time'}) &&
1179: $ENV{'form.limit_by_time'} ne '' ) {
1180: return 1;
1181: } else {
1182: return 0;
1183: }
1184: }
1185:
1186: sub get_time_limits {
1187: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
1188: ('limitby_startdate');
1189: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
1190: ('limitby_enddate');
1191: return ($starttime,$endtime);
1.11 matthew 1192: }
1193:
1194:
1195:
1196: ####################################################
1197: ####################################################
1198:
1199: =pod
1200:
1201: =item sections_description
1202:
1203: Inputs: @Sections, an array of sections
1204:
1205: Returns: A text description of the sections selected.
1206:
1207: =cut
1208:
1209: ####################################################
1210: ####################################################
1211: sub sections_description {
1212: my @Sections = @_;
1213: my $sectionstring = '';
1214: if (scalar(@Sections) > 1) {
1215: if (scalar(@Sections) > 2) {
1216: my $last = pop(@Sections);
1217: $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
1218: } else {
1219: $sectionstring = "Sections ".join(' and ',@Sections);
1220: }
1221: } else {
1222: if ($Sections[0] eq 'all') {
1223: $sectionstring = "All sections";
1224: } else {
1225: $sectionstring = "Section ".$Sections[0];
1226: }
1227: }
1228: return $sectionstring;
1.2 matthew 1229: }
1230:
1231: ####################################################
1232: ####################################################
1233:
1234: =pod
1235:
1.12 matthew 1236: =item &manage_caches
1237:
1238: Inputs: $r, apache request object
1239:
1240: Returns: An array of scalars containing html for buttons.
1241:
1242: =cut
1243:
1244: ####################################################
1245: ####################################################
1246: sub manage_caches {
1247: my ($r,$formname,$inputname) = @_;
1248: &Apache::loncoursedata::clear_internal_caches();
1.16 matthew 1249: my $sectionkey =
1250: join(',',
1251: map {
1252: &Apache::lonnet::escape($_);
1253: } sort(@Apache::lonstatistics::SelectedSections)
1254: );
1255: my $statuskey = $Apache::lonstatistics::enrollment_status;
1.12 matthew 1256: if (exists($ENV{'form.ClearCache'}) ||
1.16 matthew 1257: exists($ENV{'form.updatecaches'}) ||
1258: (exists($ENV{'form.firstrun'}) && $ENV{'form.firstrun'} ne 'no') ||
1259: (exists($ENV{'form.prevsection'}) &&
1260: $ENV{'form.prevsection'} ne $sectionkey) ||
1261: (exists($ENV{'form.prevenrollstatus'}) &&
1262: $ENV{'form.prevenrollstatus'} ne $statuskey)
1263: ) {
1.12 matthew 1264: &Apache::lonstatistics::Gather_Full_Student_Data($r,$formname,
1265: $inputname);
1266: }
1267: #
1.16 matthew 1268: my @Buttons =
1269: ('<input type="submit" name="ClearCache" '.
1270: 'value="'.&mt('Clear Caches').'" />',
1271: '<input type="submit" name="updatecaches" '.
1.17 matthew 1272: 'value="'.&mt('Update Caches').'" />'.
1273: &Apache::loncommon::help_open_topic('Statistics_Cache'),
1.16 matthew 1274: '<input type="hidden" name="prevsection" value="'.$sectionkey.'" />',
1275: '<input type="hidden" name="prevenrollstatus" value="'.$statuskey.'" />'
1276: );
1277: #
1.12 matthew 1278: if (! exists($ENV{'form.firstrun'})) {
1279: $r->print('<input type="hidden" name="firstrun" value="yes" />');
1280: } else {
1281: $r->print('<input type="hidden" name="firstrun" value="no" />');
1282: }
1283: #
1284: return @Buttons;
1285: }
1286:
1287:
1288:
1289:
1290: ####################################################
1291: ####################################################
1292:
1293: =pod
1294:
1.2 matthew 1295: =back
1296:
1297: =cut
1298:
1299: ####################################################
1300: ####################################################
1.1 matthew 1301:
1302: 1;
1303:
1304: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>