Annotation of loncom/interface/statistics/lonstathelpers.pm, revision 1.75
1.1 matthew 1: # The LearningOnline Network with CAPA
2: #
1.75 ! raeburn 3: # $Id: lonstathelpers.pm,v 1.74 2015/06/09 21:23:02 damieng 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;
1.46 albertel 52: use Apache::lonnet;
1.1 matthew 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.53 www 63: use lib '/home/httpd/lib/perl/';
64: use LONCAPA;
65:
1.1 matthew 66:
67: ####################################################
68: ####################################################
69:
70: =pod
71:
72: =item &render_resource($resource)
73:
1.42 matthew 74: Input: a navmaps resource
1.1 matthew 75:
76: Retunrs: a scalar containing html for a rendering of the problem
77: within a table.
78:
79: =cut
80:
81: ####################################################
82: ####################################################
83: sub render_resource {
84: my ($resource) = @_;
85: ##
86: ## Render the problem
1.43 matthew 87: my ($base) = ($resource->src =~ m|^(.*/)[^/]*$|);
88: $base="http://".$ENV{'SERVER_NAME'}.$base;
1.55 raeburn 89: my ($src,$symb)=($resource->link,&escape($resource->shown_symb));
1.40 matthew 90: my $rendered_problem = &Apache::lonnet::ssi_body($src.'?symb='.$symb);
1.1 matthew 91: $rendered_problem =~ s/<\s*form\s*/<nop /g;
92: $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
1.70 golterma 93: return '<div class="LC_Box">'.
94: '<h4 class="LC_hcell">'.&mt('Problem').'</h4>'.
95: '<base href="'.$base.'" />'.$rendered_problem.
96: '</div>';
1.1 matthew 97: }
1.2 matthew 98:
99: ####################################################
100: ####################################################
101:
102: =pod
103:
1.40 matthew 104: =item &get_resources
105:
106: =cut
107:
108: ####################################################
109: ####################################################
110: sub get_resources {
1.75 ! raeburn 111: my ($navmap,$sequence,$include_tools) = @_;
! 112: my @resources;
! 113: if ($include_tools) {
! 114: @resources = $navmap->retrieveResources($sequence,
! 115: sub { shift->is_gradable(); },
! 116: 0,0,0);
! 117: } else {
! 118: @resources = $navmap->retrieveResources($sequence,
! 119: sub { shift->is_problem(); },
! 120: 0,0,0);
! 121: }
1.40 matthew 122: return @resources;
123: }
124:
125: ####################################################
126: ####################################################
127:
128: =pod
129:
130: =item &problem_selector($AcceptedResponseTypes)
1.2 matthew 131:
132: Input: scalar containing regular expression which matches response
133: types to show. '.' will yield all, '(option|radiobutton)' will match
134: all option response and radiobutton problems.
135:
136: Returns: A string containing html for a table which lists the sequences
137: and their contents. A radiobutton is provided for each problem.
1.14 matthew 138: Skips 'survey' problems.
1.2 matthew 139:
140: =cut
141:
142: ####################################################
143: ####################################################
1.40 matthew 144: sub problem_selector {
1.75 ! raeburn 145: my ($AcceptedResponseTypes,$sequence_addendum,$symbmode,$all,$prefix,$smallbox,$onclick,
! 146: $include_tools) = @_;
1.65 www 147: # all: also make sequences selectable
148: # prefix: prefix for all form names
149: # smallbox: use smaller box
150: # onclick: javascript to execute when clicked
1.2 matthew 151: my $Str;
1.65 www 152: my $jsadd='';
153: if ($onclick) {
1.71 bisitz 154: $jsadd="onclick='$onclick'";
1.65 www 155: }
1.66 www 156: $Str = &Apache::loncommon::start_scrollbox(($smallbox?'420px':'620px'),
157: ($smallbox?'400px':'600px'),
158: ($smallbox?'60px':'300px')).
1.65 www 159: &Apache::loncommon::start_data_table();
1.27 matthew 160: my $rb_count =0;
1.40 matthew 161: my ($navmap,@sequences) =
162: &Apache::lonstatistics::selected_sequences_with_assessments('all');
163: return $navmap if (! ref($navmap)); # error
164: foreach my $seq (@sequences) {
1.2 matthew 165: my $seq_str = '';
1.75 ! raeburn 166: foreach my $res (&get_resources($navmap,$seq,$include_tools)) {
1.40 matthew 167: foreach my $part (@{$res->parts}) {
1.75 ! raeburn 168: my (@response_ids,@response_types);
! 169: if ($res->is_tool) {
! 170: @response_ids = ();
! 171: @response_types = ('tool');
! 172: } else {
! 173: @response_ids = $res->responseIds($part);
! 174: @response_types = $res->responseType($part);
! 175: }
1.40 matthew 176: for (my $i=0;$i<scalar(@response_types);$i++){
177: my $respid = $response_ids[$i];
178: my $resptype = $response_types[$i];
1.2 matthew 179: if ($resptype =~ m/$AcceptedResponseTypes/) {
1.40 matthew 180: my $value = &make_target_id({symb=>$res->symb,
1.2 matthew 181: part=>$part,
182: respid=>$respid,
183: resptype=>$resptype});
184: my $checked = '';
1.46 albertel 185: if ($env{'form.problemchoice'} eq $value) {
1.59 bisitz 186: $checked = ' checked="checked"';
1.2 matthew 187: }
1.40 matthew 188: my $title = $res->compTitle;
1.2 matthew 189: if (! defined($title) || $title eq '') {
1.40 matthew 190: ($title) = ($res->src =~ m:/([^/]*)$:);
1.2 matthew 191: }
1.61 www 192: $seq_str .= &Apache::loncommon::start_data_table_row().
193: ($symbmode?
1.65 www 194: '<td><input type="radio" id="'.$prefix.$rb_count.'" name="'.$prefix.'symb" value="'.&HTML::Entities::encode($res->symb,'<>&"').'" '.$checked.' '.
195: $jsadd.
196: ' /></td>'
1.61 www 197: :qq{<td><input type="radio" id="$rb_count" name="problemchoice" value="$value"$checked /></td>}).
1.65 www 198: '<td><label for="'.$prefix.$rb_count.'">'.$resptype.'</label></td>'.
199: '<td><label for="'.$prefix.$rb_count.'">'.$title.'</label>';
1.40 matthew 200: if (scalar(@response_ids) > 1) {
1.2 matthew 201: $seq_str .= &mt('response').' '.$respid;
202: }
1.55 raeburn 203: my $link = $res->link.'?symb='.&escape($res->shown_symb);
1.27 matthew 204: $seq_str .= (' 'x2).
1.56 bisitz 205: '<a target="preview" href="'.$link.'">'.&mt('view').'</a>';
1.61 www 206: $seq_str .= "</td>". &Apache::loncommon::end_data_table_row()."\n";
1.27 matthew 207: $rb_count++;
1.2 matthew 208: }
209: }
210: }
211: }
212: if ($seq_str ne '') {
1.61 www 213: $Str .= &Apache::loncommon::start_data_table_header_row().
1.65 www 214: '<th colspan="3">'.
215: ($all?'<input type="radio" id="'.$prefix.'s'.$rb_count.'" name="'.$prefix.'symb" value="'.&HTML::Entities::encode($seq->symb,'<>&').'" '.$jsadd.' />':'').
216: $seq->compTitle.'</th>'.
1.61 www 217: &Apache::loncommon::end_data_table_header_row()."\n".$seq_str;
1.50 matthew 218: if (defined($sequence_addendum)) {
1.61 www 219: $Str .= &Apache::loncommon::start_data_table_header_row().
1.57 bisitz 220: ('<td> </td>'x2).
1.50 matthew 221: '<td align="right">'.$sequence_addendum.'</td>'.
1.61 www 222: &Apache::loncommon::end_data_table_header_row()."\n";
1.50 matthew 223: }
1.2 matthew 224: }
1.64 www 225:
1.2 matthew 226: }
1.64 www 227: $Str .= &Apache::loncommon::end_data_table().&Apache::loncommon::end_scrollbox()."\n";
1.2 matthew 228: return $Str;
229: }
230:
231: ####################################################
232: ####################################################
233:
234: =pod
235:
1.24 matthew 236: =item &MultipleProblemSelector($navmap,$selected,$inputname)
1.21 matthew 237:
238: Generate HTML with checkboxes for problem selection.
239:
240: Input:
241:
242: $navmap: a navmap object. If undef, navmaps will be called to create a
243: new object.
244:
245: $selected: Scalar, Array, or hash reference of currently selected items.
246:
247: $inputname: The name of the form elements to use for the checkboxs.
248:
249: Returns: A string containing html for a table which lists the sequences
250: and their contents. A checkbox is provided for each problem.
251:
252: =cut
253:
254: ####################################################
255: ####################################################
256: sub MultipleProblemSelector {
1.63 raeburn 257: my ($navmap,$inputname,$formname,$anoncounter)=@_;
1.46 albertel 258: my $cid = $env{'request.course.id'};
1.21 matthew 259: my $Str;
260: # Massage the input as needed.
261: if (! defined($navmap)) {
262: $navmap = Apache::lonnavmaps::navmap->new();
263: if (! defined($navmap)) {
1.56 bisitz 264: $Str .= '<div class="LC_error">'
265: .&mt('Error: cannot process course structure')
266: .'</div>';
1.21 matthew 267: return $Str;
268: }
269: }
270: my $selected = {map { ($_,1) } (&get_selected_symbs($inputname))};
271: # Header
272: $Str .= <<"END";
1.58 bisitz 273: <script type="text/javascript" language="JavaScript">
1.25 matthew 274: function checkall(value,seqid) {
1.21 matthew 275: for (i=0; i<document.forms.$formname.elements.length; i++) {
276: ele = document.forms.$formname.elements[i];
277: if (ele.name == '$inputname') {
1.25 matthew 278: if (seqid != null) {
279: itemid = document.forms.$formname.elements[i].id;
280: thing = itemid.split(':');
281: if (thing[0] == seqid) {
282: document.forms.$formname.elements[i].checked=value;
283: }
284: } else {
285: document.forms.$formname.elements[i].checked=value;
286: }
1.21 matthew 287: }
288: }
289: }
290: </script>
291: END
1.63 raeburn 292: my $checkanonjs = <<"END";
293:
294: <script type="text/javascript" language="JavaScript">
295: function checkanon() {
296: return true;
297: }
298: </script>
299:
300: END
301: if (ref($anoncounter) eq 'HASH') {
302: if (keys(%{$anoncounter}) > 0) {
1.74 damieng 303: my $anonwarning = &mt('Your selection includes both problems with and without anonymous submissions.')."\n".&mt('You must select either only anonymous or only named problems.')."\n\n".&mt('If a selection contains both anonymous and named parts,[_1]use the Anonymous/Named buttons to ensure selections will be either all anonymous[_1]or all named.',"\n");
304: &js_escape(\$anonwarning);
1.63 raeburn 305: $checkanonjs = <<"END";
306:
307: <script type="text/javascript" language="JavaScript">
308: function checkanon() {
309: anoncount = 0;
310: namedcount = 0;
311: for (i=0; i<document.forms.$formname.elements.length; i++) {
312: ele = document.forms.$formname.elements[i];
313: if (ele.name == '$inputname') {
314: itemid = document.forms.$formname.elements[i].id;
315: if (document.forms.$formname.elements[i].checked) {
316: anonid = 'anonymous_'+itemid;
317: mixid = 'mixed_'+itemid;
318: anonele = document.getElementById(anonid);
319: mixele = document.getElementById(mixid);
320: if (anonele.value > 0) {
321: if (mixele.value == 'none') {
322: anoncount ++;
323: } else {
324: if (mixele.value == '0') {
325: if (mixele.checked) {
326: anoncount ++;
327: } else {
328: namedcount ++;
329: }
330: } else {
331: namedcount ++;
332: }
333: }
334: } else {
335: namedcount ++;
336: }
337: }
338: }
339: }
340: if (anoncount > 0 && namedcount > 0) {
341: alert("$anonwarning");
342: return false;
343: }
344: }
345: </script>
346:
347: END
348: }
349: }
350: $Str .= $checkanonjs.
1.21 matthew 351: '<a href="javascript:checkall(true)">'.&mt('Select All').'</a>'.
352: (' 'x4).
353: '<a href="javascript:checkall(false)">'.&mt('Unselect All').'</a>';
354: $Str .= $/.'<table>'.$/;
355: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
356: my $sequence_string;
1.25 matthew 357: my $seq_id = 0;
1.46 albertel 358: my @Accumulator = (&new_accumulator($env{'course.'.$cid.'.description'},
1.21 matthew 359: '',
360: '',
1.25 matthew 361: $seq_id++,
1.21 matthew 362: $inputname));
363: my @Sequence_Data;
364: while (my $curRes = $iterator->next()) {
365: if ($curRes == $iterator->END_MAP) {
366: if (ref($Accumulator[-1]) eq 'CODE') {
1.24 matthew 367: my $old_accumulator = pop(@Accumulator);
368: push(@Sequence_Data,&{$old_accumulator}());
1.21 matthew 369: }
370: } elsif ($curRes == $iterator->BEGIN_MAP) {
371: # Not much to do here.
372: }
373: next if (! ref($curRes));
374: if ($curRes->is_map) {
1.24 matthew 375: push(@Accumulator,&new_accumulator($curRes->compTitle,
1.21 matthew 376: $curRes->src,
377: $curRes->symb,
1.25 matthew 378: $seq_id++,
1.21 matthew 379: $inputname));
380: } elsif ($curRes->is_problem) {
1.63 raeburn 381: my $anonpart = 0;
382: my $namedpart = 0;
383: my @parts = @{$curRes->parts()};
384: if (ref($anoncounter) eq 'HASH') {
385: if (keys(%{$anoncounter}) > 0) {
386: my @parts = @{$curRes->parts()};
387: my $symb = $curRes->symb();
388: foreach my $part (@parts) {
389: if ((exists($anoncounter->{$symb."\0".$part})) ||
390: $curRes->is_anonsurvey($part)) {
391: $anonpart ++;
392: } else {
393: $namedpart ++
394: }
395: }
396: }
397: }
1.21 matthew 398: if (@Accumulator && $Accumulator[-1] ne '') {
399: &{$Accumulator[-1]}($curRes,
1.63 raeburn 400: exists($selected->{$curRes->symb}),
401: $anonpart,$namedpart);
1.21 matthew 402: }
403: }
404: }
405: my $course_seq = pop(@Sequence_Data);
406: foreach my $seq ($course_seq,@Sequence_Data) {
407: #my $seq = pop(@Sequence_Data);
408: next if (! defined($seq) || ref($seq) ne 'HASH');
409: $Str.= '<tr><td colspan="2">'.
1.25 matthew 410: '<b>'.$seq->{'title'}.'</b>'.(' 'x2).
411: '<a href="javascript:checkall(true,'.$seq->{'id'}.')">'.
412: &mt('Select').'</a>'.(' 'x2).
413: '<a href="javascript:checkall(false,'.$seq->{'id'}.')">'.
414: &mt('Unselect').'</a>'.(' 'x2).
1.21 matthew 415: '</td></tr>'.$/;
416: $Str.= $seq->{'html'};
417: }
418: $Str .= '</table>'.$/;
419: return $Str;
420: }
421:
422: sub new_accumulator {
1.25 matthew 423: my ($title,$src,$symb,$seq_id,$inputname) = @_;
1.21 matthew 424: my $target;
1.25 matthew 425: my $item_id=0;
1.21 matthew 426: return
427: sub {
428: if (@_) {
1.63 raeburn 429: my ($res,$checked,$anonpart,$namedpart) = @_;
1.23 matthew 430: $target.='<tr><td><label>'.
1.21 matthew 431: '<input type="checkbox" name="'.$inputname.'" ';
432: if ($checked) {
1.56 bisitz 433: $target .= 'checked="checked" ';
1.21 matthew 434: }
1.63 raeburn 435: my $anon_id = $item_id;
1.25 matthew 436: $target .= 'id="'.$seq_id.':'.$item_id++.'" ';
1.63 raeburn 437: my $esc_symb = &escape($res->symb);
1.21 matthew 438: $target.=
1.63 raeburn 439: 'value="'.$esc_symb.'" />'.
1.26 matthew 440: ' '.$res->compTitle.'</label>'.
441: (' 'x2).'<a target="preview" '.
1.55 raeburn 442: 'href="'.$res->link.'?symb='.
1.56 bisitz 443: &escape($res->shown_symb).'">'.&mt('view').'</a>'.
1.63 raeburn 444: '<input type="hidden" id="anonymous_'.$seq_id.':'.$anon_id.'" name="hidden_'.$seq_id.':'.$anon_id.'" value="'.$anonpart.'" />';
445: my $mixed = '<input type="hidden" id="mixed_'.$seq_id.':'.$anon_id.'" value="none" name="mixed_'.$seq_id.':'.$anon_id.'" />';
446: if ($anonpart) {
447: if ($namedpart) {
448: my $checknamed = '';
449: my $checkedanon = ' checked="checked"';
450: if ($env{'form.mixed_'.$seq_id.':'.$anon_id} eq $esc_symb) {
451: $checknamed = $checkedanon;
452: $checkedanon = '';
453: }
454: $mixed = ' ('.
455: &mt('Both anonymous and named submissions -- display: [_1]Anonymous [_2]Named[_3]',
456: '<span class="LC_nobreak"><label>'.
457: '<input type="radio" name="mixed_'.$seq_id.':'.$anon_id.
458: '" value="0" id="mixed_'.$seq_id.':'.$anon_id.'"'.$checkedanon.' />',
459: '</label></span>'.(' 'x2).' <span class="LC_nobreak">'.
460: '<label><input type="radio" name="mixed_'.$seq_id.':'.$anon_id.
461: '" value="symb_'.$esc_symb.'" id="named_'.$seq_id.':'.$anon_id.'"'.$checknamed.' />',
462: '</label></span>').')';
463: } else {
464: $target .= ' '.&mt('(Anonymous Survey)');
465: }
466: }
467: $target.= $mixed.'</td></tr>'.$/;
1.21 matthew 468: } else {
469: if (defined($target)) {
470: return { title => $title,
471: symb => $symb,
472: src => $src,
1.25 matthew 473: id => $seq_id,
1.21 matthew 474: html => $target, };
475: }
476: return undef;
477: }
478: };
479: }
480:
481: sub get_selected_symbs {
482: my ($inputfield) = @_;
483: my $field = 'form.'.$inputfield;
1.48 matthew 484: my @symbs = (map {
1.53 www 485: &unescape($_);
1.48 matthew 486: } &Apache::loncommon::get_env_multiple($field));
487: return @symbs;
1.21 matthew 488: }
489:
490: ####################################################
491: ####################################################
492:
493: =pod
494:
1.2 matthew 495: =item &make_target_id($target)
496:
497: Inputs: Hash ref with the following entries:
498: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
499: $target->{'resptype'}.
500:
501: Returns: A string, suitable for a form parameter, which uniquely identifies
502: the problem, part, and response to do statistical analysis on.
503:
504: Used by Apache::lonstathelpers::ProblemSelector().
505:
506: =cut
507:
508: ####################################################
509: ####################################################
510: sub make_target_id {
511: my ($target) = @_;
1.53 www 512: my $id = &escape($target->{'symb'}).':'.
513: &escape($target->{'part'}).':'.
514: &escape($target->{'respid'}).':'.
515: &escape($target->{'resptype'});
1.2 matthew 516: return $id;
517: }
518:
519: ####################################################
520: ####################################################
521:
522: =pod
523:
524: =item &get_target_from_id($id)
525:
526: Inputs: $id, a scalar string from Apache::lonstathelpers::make_target_id().
527:
528: Returns: A hash reference, $target, containing the following keys:
529: $target->{'symb'}, $target->{'part'}, $target->{'respid'},
530: $target->{'resptype'}.
531:
532: =cut
533:
534: ####################################################
535: ####################################################
536: sub get_target_from_id {
537: my ($id) = @_;
1.21 matthew 538: if (! ref($id)) {
539: my ($symb,$part,$respid,$resptype) = split(':',$id);
1.53 www 540: return ({ symb => &unescape($symb),
541: part => &unescape($part),
542: respid => &unescape($respid),
543: resptype => &unescape($resptype)});
1.21 matthew 544: } elsif (ref($id) eq 'ARRAY') {
545: my @Return;
546: foreach my $selected (@$id) {
547: my ($symb,$part,$respid,$resptype) = split(':',$selected);
1.53 www 548: push(@Return,{ symb => &unescape($symb),
549: part => &unescape($part),
550: respid => &unescape($respid),
551: resptype => &unescape($resptype)});
1.21 matthew 552: }
553: return \@Return;
554: }
1.2 matthew 555: }
556:
557: ####################################################
558: ####################################################
559:
560: =pod
561:
1.13 matthew 562: =item &get_prev_curr_next($target,$AcceptableResponseTypes,$granularity)
1.2 matthew 563:
564: Determine the problem parts or responses preceeding and following the
565: current resource.
566:
567: Inputs: $target (see &Apache::lonstathelpers::get_target_from_id())
568: $AcceptableResponseTypes, regular expression matching acceptable
569: response types,
1.52 albertel 570: $granularity, either 'part', 'response', 'part_survey', or 'part_task'
1.2 matthew 571:
572: Returns: three hash references, $prev, $curr, $next, which refer to the
573: preceeding, current, or following problem parts or responses, depending
574: on the value of $granularity. Values of undef indicate there is no
575: previous or next part/response. A value of undef for all three indicates
576: there was no match found to the current part/resource.
577:
578: The hash references contain the following keys:
579: symb, part, resource
580:
581: If $granularity eq 'response', the following ADDITIONAL keys will be present:
582: respid, resptype
583:
584: =cut
585:
586: ####################################################
587: ####################################################
588: sub get_prev_curr_next {
589: my ($target,$AcceptableResponseTypes,$granularity) = @_;
590: #
591: # Build an array with the data we need to search through
592: my @Resource;
1.40 matthew 593: my ($navmap,@sequences) =
594: &Apache::lonstatistics::selected_sequences_with_assessments('all');
595: return $navmap if (! ref($navmap));
596: foreach my $seq (@sequences) {
597: my @resources = &get_resources($navmap,$seq);
598: foreach my $res (@resources) {
599: foreach my $part (@{$res->parts}) {
1.60 raeburn 600: if (($res->is_survey($part) || ($res->is_anonsurvey($part))) &&
601: ($granularity eq 'part_survey')) {
1.20 matthew 602: push (@Resource,
1.40 matthew 603: { symb => $res->symb,
1.20 matthew 604: part => $part,
605: resource => $res,
606: } );
1.52 albertel 607: } elsif ($res->is_task($part) && ($granularity eq 'part_task')){
608: push (@Resource,
609: { symb => $res->symb,
610: part => $part,
611: resource => $res,
612: } );
1.13 matthew 613: } elsif ($granularity eq 'part') {
1.2 matthew 614: push (@Resource,
1.40 matthew 615: { symb => $res->symb,
1.2 matthew 616: part => $part,
617: resource => $res,
618: } );
619: } elsif ($granularity eq 'response') {
1.40 matthew 620: my @response_ids = $res->responseIds($part);
621: my @response_types = $res->responseType($part);
1.2 matthew 622: for (my $i=0;
1.40 matthew 623: $i<scalar(@response_ids);
1.2 matthew 624: $i++){
1.40 matthew 625: my $respid = $response_ids[$i];
626: my $resptype = $response_types[$i];
1.2 matthew 627: next if ($resptype !~ m/$AcceptableResponseTypes/);
628: push (@Resource,
1.40 matthew 629: { symb => $res->symb,
1.2 matthew 630: part => $part,
1.40 matthew 631: respid => $respid,
632: resptype => $resptype,
1.2 matthew 633: resource => $res,
634: } );
635: }
636: }
637: }
638: }
639: }
640: #
641: # Get the index of the current situation
642: my $curr_idx;
643: for ($curr_idx=0;$curr_idx<$#Resource;$curr_idx++) {
644: my $curr_item = $Resource[$curr_idx];
1.52 albertel 645: if ($granularity =~ /^(part|part_survey|part_task)$/) {
1.2 matthew 646: if ($curr_item->{'symb'} eq $target->{'symb'} &&
647: $curr_item->{'part'} eq $target->{'part'}) {
648: last;
649: }
650: } elsif ($granularity eq 'response') {
651: if ($curr_item->{'symb'} eq $target->{'symb'} &&
652: $curr_item->{'part'} eq $target->{'part'} &&
653: $curr_item->{'respid'} eq $target->{'respid'} &&
654: $curr_item->{'resptype'} eq $target->{'resptype'}) {
655: last;
656: }
657: }
658: }
659: my $curr_item = $Resource[$curr_idx];
1.52 albertel 660: if ($granularity =~ /^(part|part_survey|part_task)$/) {
1.2 matthew 661: if ($curr_item->{'symb'} ne $target->{'symb'} ||
662: $curr_item->{'part'} ne $target->{'part'}) {
663: # bogus symb - return nothing
664: return (undef,undef,undef);
665: }
666: } elsif ($granularity eq 'response') {
667: if ($curr_item->{'symb'} ne $target->{'symb'} ||
668: $curr_item->{'part'} ne $target->{'part'} ||
669: $curr_item->{'respid'} ne $target->{'respid'} ||
670: $curr_item->{'resptype'} ne $target->{'resptype'}){
671: # bogus symb - return nothing
672: return (undef,undef,undef);
673: }
674: }
675: #
676: # Now just pick up the data we need
677: my ($prev,$curr,$next);
678: if ($curr_idx == 0) {
679: $prev = undef;
680: $curr = $Resource[$curr_idx ];
681: $next = $Resource[$curr_idx+1];
682: } elsif ($curr_idx == $#Resource) {
683: $prev = $Resource[$curr_idx-1];
684: $curr = $Resource[$curr_idx ];
685: $next = undef;
686: } else {
687: $prev = $Resource[$curr_idx-1];
688: $curr = $Resource[$curr_idx ];
689: $next = $Resource[$curr_idx+1];
690: }
1.40 matthew 691: return ($navmap,$prev,$curr,$next);
1.4 matthew 692: }
693:
1.9 matthew 694:
695: #####################################################
696: #####################################################
697:
698: =pod
699:
700: =item GetStudentAnswers($r,$problem,$Students)
701:
702: Determines the correct answer for a set of students on a given problem.
703: The students answers are stored in the student hashes pointed to by the
704: array @$Students under the key 'answer'.
705:
706: Inputs: $r
707: $problem: hash reference containing the keys 'resource', 'part', and 'respid'.
708: $Students: reference to array containing student hashes (need 'username',
709: 'domain').
710:
711: Returns: nothing
712:
713: =cut
714:
715: #####################################################
716: #####################################################
717: sub GetStudentAnswers {
1.12 matthew 718: my ($r,$problem,$Students,$formname,$inputname) = @_;
1.31 matthew 719: my %answers;
1.12 matthew 720: my $status_type;
721: if (defined($formname)) {
722: $status_type = 'inline';
723: } else {
724: $status_type = 'popup';
725: }
1.9 matthew 726: my $c = $r->connection();
727: my %Answers;
728: my ($resource,$partid,$respid) = ($problem->{'resource'},
729: $problem->{'part'},
730: $problem->{'respid'});
731: # Read in the cache (if it exists) before we start timing things.
732: &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
733: # Open progress window
1.68 www 734: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@$Students));
1.9 matthew 735: $r->rflush();
736: foreach my $student (@$Students) {
737: last if ($c->aborted());
738: my $sname = $student->{'username'};
739: my $sdom = $student->{'domain'};
1.30 matthew 740: my $answer = &Apache::lonstathelpers::get_student_answer
1.9 matthew 741: ($resource,$sname,$sdom,$partid,$respid);
742: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1.62 bisitz 743: 'last student');
1.31 matthew 744: $answers{$answer}++;
1.9 matthew 745: $student->{'answer'} = $answer;
746: }
1.29 matthew 747: &Apache::lonstathelpers::write_analysis_cache();
1.9 matthew 748: return if ($c->aborted());
749: $r->rflush();
750: # close progress window
751: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.31 matthew 752: return \%answers;
1.9 matthew 753: }
1.4 matthew 754:
755: #####################################################
756: #####################################################
757:
758: =pod
759:
760: =item analyze_problem_as_student
761:
1.30 matthew 762: Analyzes a homework problem for a student
1.4 matthew 763:
764: Inputs: $resource: a resource object
765: $sname, $sdom, $partid, $respid
766:
1.30 matthew 767: Returns: the problem analysis hash
1.6 matthew 768:
1.4 matthew 769: =cut
770:
771: #####################################################
772: #####################################################
773: sub analyze_problem_as_student {
1.30 matthew 774: my ($resource,$sname,$sdom) = @_;
1.22 matthew 775: if (ref($resource) ne 'HASH') {
776: my $res = $resource;
777: $resource = { 'src' => $res->src,
778: 'symb' => $res->symb,
779: 'parts' => $res->parts };
780: foreach my $part (@{$resource->{'parts'}}) {
781: $resource->{'partdata'}->{$part}->{'ResponseIds'}=
782: [$res->responseIds($part)];
783: }
784: }
1.4 matthew 785: my $url = $resource->{'src'};
786: my $symb = $resource->{'symb'};
1.29 matthew 787: my $analysis = &get_from_analysis_cache($sname,$sdom,$symb);
788: if (! defined($analysis)) {
1.46 albertel 789: my $courseid = $env{'request.course.id'};
1.29 matthew 790: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
791: 'grade_domain' => $sdom,
792: 'grade_username' => $sname,
793: 'grade_symb' => $symb,
794: 'grade_courseid' => $courseid));
795: (my $garbage,$analysis)=split(/_HASH_REF__/,$Answ,2);
796: &store_analysis($sname,$sdom,$symb,$analysis);
797: }
798: my %Answer=&Apache::lonnet::str2hash($analysis);
1.6 matthew 799: #
1.30 matthew 800: return \%Answer;
801: }
802:
803: #####################################################
804: #####################################################
805:
806: =pod
807:
808: =item get_student_answer
809:
810: Analyzes a homework problem for a particular student and returns the correct
811: answer. Attempts to put together an answer for problem types
812: that do not natively support it.
813:
814: Inputs: $resource: a resource object (from navmaps or hash from loncoursedata)
815: $sname, $sdom, $partid, $respid
816:
817: Returns: $answer
818:
819: If $partid and $respid are specified, $answer is simply a scalar containing
820: the correct answer for the response.
821:
822: If $partid or $respid are undefined, $answer will be a hash reference with
823: keys $partid.'.'.$respid.'.answer'.
824:
825: =cut
826:
827: #####################################################
828: #####################################################
829: sub get_student_answer {
830: my ($resource,$sname,$sdom,$partid,$respid) = @_;
831: #
832: if (ref($resource) ne 'HASH') {
833: my $res = $resource;
834: $resource = { 'src' => $res->src,
835: 'symb' => $res->symb,
836: 'parts' => $res->parts };
837: foreach my $part (@{$resource->{'parts'}}) {
838: $resource->{'partdata'}->{$part}->{'ResponseIds'}=
839: [$res->responseIds($part)];
840: }
841: }
842: #
843: my $analysis =
844: &analyze_problem_as_student($resource,$sname,$sdom);
1.29 matthew 845: my $answer;
1.8 matthew 846: foreach my $partid (@{$resource->{'parts'}}) {
1.6 matthew 847: my $partdata = $resource->{'partdata'}->{$partid};
848: foreach my $respid (@{$partdata->{'ResponseIds'}}) {
849: my $prefix = $partid.'.'.$respid;
850: my $key = $prefix.'.answer';
1.30 matthew 851: $answer->{$partid}->{$respid} =
852: &get_answer($prefix,$key,%$analysis);
1.6 matthew 853: }
1.8 matthew 854: }
1.30 matthew 855: my $returnvalue;
1.8 matthew 856: if (! defined($partid)) {
857: $returnvalue = $answer;
858: } elsif (! defined($respid)) {
859: $returnvalue = $answer->{$partid};
1.6 matthew 860: } else {
1.8 matthew 861: $returnvalue = $answer->{$partid}->{$respid};
1.6 matthew 862: }
863: return $returnvalue;
864: }
865:
866: sub get_answer {
867: my ($prefix,$key,%Answer) = @_;
868: my $returnvalue;
1.4 matthew 869: if (exists($Answer{$key})) {
1.54 albertel 870: if (ref($Answer{$key}) eq 'HASH') {
871: my $which = 'INTERNAL';
872: if (!exists($Answer{$key}{$which})) {
873: $which = (sort(keys(%{ $Answer{$key} })))[0];
874: }
875: my $student_answer = $Answer{$key}{$which}[0][0];
876: $returnvalue = $student_answer;
877: } else {
878: &Apache::lonnet::logthis("error analyzing problem. got a answer of type ".ref($Answer{$key}));
879: }
1.4 matthew 880: } else {
881: if (exists($Answer{$prefix.'.shown'})) {
882: # The response has foils
883: my %values;
884: while (my ($k,$v) = each(%Answer)) {
885: next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
886: my $foilname = $2;
887: $values{$foilname}=$v;
888: }
889: foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
890: if (ref($values{$foil}) eq 'ARRAY') {
1.10 albertel 891: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
892: join(',',map {&HTML::Entities::encode($_,'<>&"')} @{$values{$foil}}).'&';
1.4 matthew 893: } else {
1.10 albertel 894: $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
895: &HTML::Entities::encode($values{$foil},'<>&"').'&';
1.4 matthew 896: }
897: }
898: $returnvalue =~ s/ /\%20/g;
899: chop ($returnvalue);
900: }
901: }
902: return $returnvalue;
903: }
1.8 matthew 904:
905: #####################################################
906: #####################################################
907:
908: =pod
909:
910: =item Caching routines
911:
912: =over 4
913:
1.29 matthew 914: =item &load_analysis_cache($symb)
1.8 matthew 915:
916: Loads the cache for the given symb into memory from disk.
917: Requires the cache filename be set.
918: Only should be called by &ensure_proper_cache.
919:
920: =cut
921:
922: #####################################################
923: #####################################################
924: {
925: my $cache_filename = undef;
926: my $current_symb = undef;
927: my %cache;
928:
1.29 matthew 929: sub load_analysis_cache {
1.8 matthew 930: my ($symb) = @_;
931: return if (! defined($cache_filename));
932: if (! defined($current_symb) || $current_symb ne $symb) {
933: undef(%cache);
934: my $storedstring;
935: my %cache_db;
936: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_READER(),0640)) {
1.53 www 937: $storedstring = $cache_db{&escape($symb)};
1.8 matthew 938: untie(%cache_db);
939: }
940: if (defined($storedstring)) {
941: %cache = %{thaw($storedstring)};
942: }
943: }
944: return;
945: }
946:
947: #####################################################
948: #####################################################
949:
950: =pod
951:
1.29 matthew 952: =item &get_from_analysis_cache($sname,$sdom,$symb,$partid,$respid)
1.8 matthew 953:
954: Returns the appropriate data from the cache, or undef if no data exists.
955:
956: =cut
957:
958: #####################################################
959: #####################################################
1.29 matthew 960: sub get_from_analysis_cache {
961: my ($sname,$sdom,$symb) = @_;
1.8 matthew 962: &ensure_proper_cache($symb);
963: my $returnvalue;
1.29 matthew 964: if (exists($cache{$sname.':'.$sdom})) {
965: $returnvalue = $cache{$sname.':'.$sdom};
1.8 matthew 966: } else {
967: $returnvalue = undef;
968: }
969: return $returnvalue;
970: }
971:
972: #####################################################
973: #####################################################
974:
975: =pod
976:
1.29 matthew 977: =item &write_analysis_cache($symb)
1.8 matthew 978:
979: Writes the in memory cache to disk so that it can be read in with
1.29 matthew 980: &load_analysis_cache($symb).
1.8 matthew 981:
982: =cut
983:
984: #####################################################
985: #####################################################
1.29 matthew 986: sub write_analysis_cache {
1.8 matthew 987: return if (! defined($current_symb) || ! defined($cache_filename));
988: my %cache_db;
1.53 www 989: my $key = &escape($current_symb);
1.8 matthew 990: if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_WRCREAT(),0640)) {
991: my $storestring = freeze(\%cache);
992: $cache_db{$key}=$storestring;
993: $cache_db{$key.'.time'}=time;
994: untie(%cache_db);
995: }
996: undef(%cache);
997: undef($current_symb);
998: undef($cache_filename);
999: return;
1000: }
1001:
1002: #####################################################
1003: #####################################################
1004:
1005: =pod
1006:
1007: =item &ensure_proper_cache($symb)
1008:
1009: Called to make sure we have the proper cache set up. This is called
1.29 matthew 1010: prior to every analysis lookup.
1.8 matthew 1011:
1012: =cut
1013:
1014: #####################################################
1015: #####################################################
1016: sub ensure_proper_cache {
1017: my ($symb) = @_;
1.46 albertel 1018: my $cid = $env{'request.course.id'};
1.67 foxr 1019: my $new_filename = LONCAPA::tempdir() .
1.29 matthew 1020: 'problemanalysis_'.$cid.'_analysis_cache.db';
1.8 matthew 1021: if (! defined($cache_filename) ||
1022: $cache_filename ne $new_filename ||
1023: ! defined($current_symb) ||
1024: $current_symb ne $symb) {
1025: $cache_filename = $new_filename;
1026: # Notice: $current_symb is not set to $symb until after the cache is
1.29 matthew 1027: # loaded. This is what tells &load_analysis_cache to load in a new
1.8 matthew 1028: # symb cache.
1.29 matthew 1029: &load_analysis_cache($symb);
1.8 matthew 1030: $current_symb = $symb;
1031: }
1032: }
1033:
1034: #####################################################
1035: #####################################################
1036:
1037: =pod
1038:
1.29 matthew 1039: =item &store_analysis($sname,$sdom,$symb,$partid,$respid,$dataset)
1.8 matthew 1040:
1.29 matthew 1041: Stores the analysis data in the in memory cache.
1.8 matthew 1042:
1043: =cut
1044:
1045: #####################################################
1046: #####################################################
1.29 matthew 1047: sub store_analysis {
1048: my ($sname,$sdom,$symb,$dataset) = @_;
1.8 matthew 1049: return if ($symb ne $current_symb);
1.29 matthew 1050: $cache{$sname.':'.$sdom}=$dataset;
1.8 matthew 1051: return;
1052: }
1053:
1054: }
1055: #####################################################
1056: #####################################################
1057:
1058: =pod
1059:
1060: =back
1061:
1062: =cut
1063:
1064: #####################################################
1065: #####################################################
1.4 matthew 1066:
1067: ##
1068: ## The following is copied from datecalc1.pl, part of the
1069: ## Spreadsheet::WriteExcel CPAN module.
1070: ##
1071: ##
1072: ######################################################################
1073: #
1074: # Demonstration of writing date/time cells to Excel spreadsheets,
1075: # using UNIX/Perl time as source of date/time.
1076: #
1077: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
1078: #
1079: ######################################################################
1080: #
1081: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
1082: # measured in seconds.
1083: #
1084: # An Excel file can use exactly one of two different date/time systems.
1085: # In these systems, a floating point number represents the number of days
1086: # (and fractional parts of the day) since a start point. The floating point
1087: # number is referred to as a 'serial'.
1088: # The two systems ('1900' and '1904') use different starting points:
1089: # '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
1090: # a leap year - see:
1091: # http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
1092: # for the excuse^H^H^H^H^H^Hreason.
1093: # '1904'; '1.00' is 2 Jan 1904.
1094: #
1095: # The '1904' system is the default for Apple Macs. Windows versions of
1096: # Excel have the option to use the '1904' system.
1097: #
1098: # Note that Visual Basic's "DateSerial" function does NOT erroneously
1099: # regard 1900 as a leap year, and thus its serials do not agree with
1100: # the 1900 serials of Excel for dates before 1 Mar 1900.
1101: #
1102: # Note that StarOffice (at least at version 5.2) does NOT erroneously
1103: # regard 1900 as a leap year, and thus its serials do not agree with
1104: # the 1900 serials of Excel for dates before 1 Mar 1900.
1105: #
1106: ######################################################################
1107: #
1108: # Calculation description
1109: # =======================
1110: #
1111: # 1900 system
1112: # -----------
1113: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
1114: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
1115: # were leap years with an extra day.
1116: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
1117: # 1 Jan 1970.
1118: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
1119: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
1120: #
1121: # 1904 system
1122: # -----------
1123: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
1124: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
1125: # were leap years with an extra day.
1126: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
1127: # 1 Jan 1970.
1128: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
1129: #
1130: ######################################################################
1131: #
1132: # Copyright (c) 2000, Andrew Benham.
1133: # This program is free software. It may be used, redistributed and/or
1134: # modified under the same terms as Perl itself.
1135: #
1136: # Andrew Benham, adsb@bigfoot.com
1137: # London, United Kingdom
1138: # 11 Nov 2000
1139: #
1140: ######################################################################
1141: #-----------------------------------------------------------
1142: # calc_serial()
1143: #
1144: # Called with (up to) 2 parameters.
1145: # 1. Unix timestamp. If omitted, uses current time.
1146: # 2. GMT flag. Set to '1' to return serial in GMT.
1147: # If omitted, returns serial in appropriate timezone.
1148: #
1149: # Returns date/time serial according to $DATE_SYSTEM selected
1150: #-----------------------------------------------------------
1151: sub calc_serial {
1152: # Use 1900 date system on all platforms other than Apple Mac (for which
1153: # use 1904 date system).
1154: my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
1155: my $time = (defined $_[0]) ? $_[0] : time();
1156: my $gmtflag = (defined $_[1]) ? $_[1] : 0;
1157: #
1158: # Divide timestamp by number of seconds in a day.
1159: # This gives a date serial with '0' on 1 Jan 1970.
1160: my $serial = $time / 86400;
1161: #
1162: # Adjust the date serial by the offset appropriate to the
1163: # currently selected system (1900/1904).
1164: if ($DATE_SYSTEM == 0) { # use 1900 system
1165: $serial += 25569;
1166: } else { # use 1904 system
1167: $serial += 24107;
1168: }
1169: #
1170: unless ($gmtflag) {
1171: # Now have a 'raw' serial with the right offset. But this
1172: # gives a serial in GMT, which is false unless the timezone
1173: # is GMT. We need to adjust the serial by the appropriate
1174: # timezone offset.
1175: # Calculate the appropriate timezone offset by seeing what
1176: # the differences between localtime and gmtime for the given
1177: # time are.
1178: #
1179: my @gmtime = gmtime($time);
1180: my @ltime = localtime($time);
1181: #
1182: # For the first 7 elements of the two arrays, adjust the
1183: # date serial where the elements differ.
1184: for (0 .. 6) {
1185: my $diff = $ltime[$_] - $gmtime[$_];
1186: if ($diff) {
1187: $serial += _adjustment($diff,$_);
1188: }
1189: }
1190: }
1191: #
1192: # Perpetuate the error that 1900 was a leap year by decrementing
1193: # the serial if we're using the 1900 system and the date is prior to
1194: # 1 Mar 1900. This has the effect of making serial value '60'
1195: # 29 Feb 1900.
1196: #
1197: # This fix only has any effect if UNIX/Perl time on the platform
1198: # can represent 1900. Many can't.
1199: #
1200: unless ($DATE_SYSTEM) {
1201: $serial-- if ($serial < 61); # '61' is 1 Mar 1900
1202: }
1203: return $serial;
1204: }
1205:
1206: sub _adjustment {
1207: # Based on the difference in the localtime/gmtime array elements
1208: # number, return the adjustment required to the serial.
1209: #
1210: # We only look at some elements of the localtime/gmtime arrays:
1211: # seconds unlikely to be different as all known timezones
1212: # have an offset of integral multiples of 15 minutes,
1213: # but it's easy to do.
1214: # minutes will be different for timezone offsets which are
1215: # not an exact number of hours.
1216: # hours very likely to be different.
1217: # weekday will differ when localtime/gmtime difference
1218: # straddles midnight.
1219: #
1220: # Assume that difference between localtime and gmtime is less than
1221: # 5 days, then don't have to do maths for day of month, month number,
1222: # year number, etc...
1223: #
1224: my ($delta,$element) = @_;
1225: my $adjust = 0;
1226: #
1227: if ($element == 0) { # Seconds
1228: $adjust = $delta/86400; # 60 * 60 * 24
1229: } elsif ($element == 1) { # Minutes
1230: $adjust = $delta/1440; # 60 * 24
1231: } elsif ($element == 2) { # Hours
1232: $adjust = $delta/24; # 24
1233: } elsif ($element == 6) { # Day of week number
1234: # Catch difference straddling Sat/Sun in either direction
1235: $delta += 7 if ($delta < -4);
1236: $delta -= 7 if ($delta > 4);
1237: #
1238: $adjust = $delta;
1239: }
1240: return $adjust;
1241: }
1242:
1243: ###########################################################
1244: ###########################################################
1245:
1246: =pod
1247:
1248: =item get_problem_data
1249:
1250: Returns a data structure describing the problem.
1251:
1252: Inputs: $url
1253:
1254: Returns: %Partdata
1255:
1256: =cut
1257:
1258: ## note: we must force each foil and option to not begin or end with
1259: ## spaces as they are stored without such data.
1260: ##
1261: ###########################################################
1262: ###########################################################
1263: sub get_problem_data {
1264: my ($url) = @_;
1265: my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
1266: (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
1267: my %Answer;
1268: %Answer=&Apache::lonnet::str2hash($Answ);
1269: my %Partdata;
1270: foreach my $part (@{$Answer{'parts'}}) {
1271: while (my($key,$value) = each(%Answer)) {
1272: #
1273: # Logging code:
1.7 matthew 1274: if (0) {
1.4 matthew 1275: &Apache::lonnet::logthis($part.' got key "'.$key.'"');
1276: if (ref($value) eq 'ARRAY') {
1277: &Apache::lonnet::logthis(' @'.join(',',@$value));
1278: } else {
1279: &Apache::lonnet::logthis(' '.$value);
1280: }
1281: }
1282: # End of logging code
1.51 albertel 1283: next if ($key !~ /^\Q$part\E/);
1284: $key =~ s/^\Q$part\E\.//;
1.4 matthew 1285: if (ref($value) eq 'ARRAY') {
1286: if ($key eq 'options') {
1287: $Partdata{$part}->{'_Options'}=$value;
1288: } elsif ($key eq 'concepts') {
1289: $Partdata{$part}->{'_Concepts'}=$value;
1.28 matthew 1290: } elsif ($key eq 'items') {
1291: $Partdata{$part}->{'_Items'}=$value;
1.4 matthew 1292: } elsif ($key =~ /^concept\.(.*)$/) {
1293: my $concept = $1;
1294: foreach my $foil (@$value) {
1295: $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
1296: $concept;
1297: }
1.32 matthew 1298: } elsif ($key =~ /^(unit|incorrect|answer|ans_low|ans_high|str_type)$/) {
1.4 matthew 1299: $Partdata{$part}->{$key}=$value;
1300: }
1301: } else {
1302: if ($key=~ /^foil\.text\.(.*)$/) {
1303: my $foil = $1;
1304: $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
1305: $value =~ s/(\s*$|^\s*)//g;
1306: $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
1307: } elsif ($key =~ /^foil\.value\.(.*)$/) {
1308: my $foil = $1;
1309: $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
1.28 matthew 1310: } elsif ($key eq 'answercomputed') {
1311: $Partdata{$part}->{'answercomputed'} = $value;
1.4 matthew 1312: }
1313: }
1314: }
1315: }
1.28 matthew 1316: # Further debugging code
1317: if (0) {
1318: &Apache::lonnet::logthis('lonstathelpers::get_problem_data');
1319: &log_hash_ref(\%Partdata);
1320: }
1.4 matthew 1321: return %Partdata;
1.5 matthew 1322: }
1323:
1.28 matthew 1324: sub log_array_ref {
1325: my ($arrayref,$prefix) = @_;
1326: return if (ref($arrayref) ne 'ARRAY');
1327: if (! defined($prefix)) { $prefix = ''; };
1328: foreach my $v (@$arrayref) {
1329: if (ref($v) eq 'ARRAY') {
1330: &log_array_ref($v,$prefix.' ');
1331: } elsif (ref($v) eq 'HASH') {
1332: &log_hash_ref($v,$prefix.' ');
1333: } else {
1334: &Apache::lonnet::logthis($prefix.'"'.$v.'"');
1335: }
1336: }
1337: }
1338:
1339: sub log_hash_ref {
1340: my ($hashref,$prefix) = @_;
1341: return if (ref($hashref) ne 'HASH');
1342: if (! defined($prefix)) { $prefix = ''; };
1343: while (my ($k,$v) = each(%$hashref)) {
1344: if (ref($v) eq 'ARRAY') {
1345: &Apache::lonnet::logthis($prefix.'"'.$k.'" = array');
1346: &log_array_ref($v,$prefix.' ');
1347: } elsif (ref($v) eq 'HASH') {
1348: &Apache::lonnet::logthis($prefix.'"'.$k.'" = hash');
1349: &log_hash_ref($v,$prefix.' ');
1350: } else {
1351: &Apache::lonnet::logthis($prefix.'"'.$k.'" => "'.$v.'"');
1352: }
1353: }
1354: }
1.5 matthew 1355: ####################################################
1356: ####################################################
1357:
1358: =pod
1359:
1360: =item &limit_by_time()
1361:
1362: =cut
1363:
1364: ####################################################
1365: ####################################################
1366: sub limit_by_time_form {
1367: my $Starttime_form = '';
1368: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
1369: ('limitby_startdate');
1370: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
1371: ('limitby_enddate');
1372: if (! defined($endtime)) {
1373: $endtime = time;
1374: }
1375: if (! defined($starttime)) {
1376: $starttime = $endtime - 60*60*24*7;
1377: }
1378: my $state;
1379: if (&limit_by_time()) {
1380: $state = '';
1381: } else {
1382: $state = 'disabled';
1383: }
1384: my $startdateform = &Apache::lonhtmlcommon::date_setter
1385: ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
1386: my $enddateform = &Apache::lonhtmlcommon::date_setter
1387: ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
1388: my $Str;
1.58 bisitz 1389: $Str .= '<script type="text/javascript" language="JavaScript">';
1.5 matthew 1390: $Str .= 'function toggle_limitby_activity(state) {';
1391: $Str .= ' if (state) {';
1392: $Str .= ' limitby_startdate_enable();';
1393: $Str .= ' limitby_enddate_enable();';
1394: $Str .= ' } else {';
1395: $Str .= ' limitby_startdate_disable();';
1396: $Str .= ' limitby_enddate_disable();';
1397: $Str .= ' }';
1398: $Str .= '}';
1399: $Str .= '</script>';
1400: $Str .= '<fieldset>';
1401: my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
1402: if (&limit_by_time()) {
1.72 bisitz 1403: $timecheckbox .= 'checked="checked" ';
1.5 matthew 1404: }
1.72 bisitz 1405: $timecheckbox .= 'onchange="javascript:toggle_limitby_activity(this.checked);" ';
1.5 matthew 1406: $timecheckbox .= ' />';
1.49 albertel 1407: $Str .= '<legend><label>'.&mt('[_1] Limit by time',$timecheckbox).'</label></legend>';
1.5 matthew 1408: $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
1409: $Str .= &mt(' End Time: [_1]',$enddateform).'<br />';
1410: $Str .= '</fieldset>';
1411: return $Str;
1412: }
1413:
1414: sub limit_by_time {
1.46 albertel 1415: if (exists($env{'form.limit_by_time'}) &&
1416: $env{'form.limit_by_time'} ne '' ) {
1.5 matthew 1417: return 1;
1418: } else {
1419: return 0;
1420: }
1421: }
1422:
1423: sub get_time_limits {
1424: my $starttime = &Apache::lonhtmlcommon::get_date_from_form
1425: ('limitby_startdate');
1426: my $endtime = &Apache::lonhtmlcommon::get_date_from_form
1427: ('limitby_enddate');
1428: return ($starttime,$endtime);
1.11 matthew 1429: }
1430:
1.2 matthew 1431: ####################################################
1432: ####################################################
1433:
1434: =pod
1435:
1.12 matthew 1436: =item &manage_caches
1437:
1438: Inputs: $r, apache request object
1439:
1440: Returns: An array of scalars containing html for buttons.
1441:
1442: =cut
1443:
1444: ####################################################
1445: ####################################################
1446: sub manage_caches {
1.23 matthew 1447: my ($r,$formname,$inputname,$update_message) = @_;
1.12 matthew 1448: &Apache::loncoursedata::clear_internal_caches();
1.16 matthew 1449: my $sectionkey =
1450: join(',',
1451: map {
1.53 www 1452: &escape($_);
1.44 matthew 1453: } sort(&Apache::lonstatistics::get_selected_sections())
1.16 matthew 1454: );
1455: my $statuskey = $Apache::lonstatistics::enrollment_status;
1.46 albertel 1456: if (exists($env{'form.ClearCache'}) ||
1457: exists($env{'form.updatecaches'}) ||
1458: (exists($env{'form.firstrun'}) && $env{'form.firstrun'} ne 'no') ||
1459: (exists($env{'form.prevsection'}) &&
1460: $env{'form.prevsection'} ne $sectionkey) ||
1461: (exists($env{'form.prevenrollstatus'}) &&
1462: $env{'form.prevenrollstatus'} ne $statuskey)
1.16 matthew 1463: ) {
1.23 matthew 1464: if (defined($update_message)) {
1465: $r->print($update_message);
1466: }
1.40 matthew 1467: if (0) {
1468: &Apache::lonnet::logthis('Updating mysql student data caches');
1469: }
1.36 matthew 1470: &gather_full_student_data($r,$formname,$inputname);
1.12 matthew 1471: }
1472: #
1.16 matthew 1473: my @Buttons =
1474: ('<input type="submit" name="ClearCache" '.
1475: 'value="'.&mt('Clear Caches').'" />',
1476: '<input type="submit" name="updatecaches" '.
1.17 matthew 1477: 'value="'.&mt('Update Caches').'" />'.
1478: &Apache::loncommon::help_open_topic('Statistics_Cache'),
1.16 matthew 1479: '<input type="hidden" name="prevsection" value="'.$sectionkey.'" />',
1480: '<input type="hidden" name="prevenrollstatus" value="'.$statuskey.'" />'
1481: );
1482: #
1.46 albertel 1483: if (! exists($env{'form.firstrun'})) {
1.12 matthew 1484: $r->print('<input type="hidden" name="firstrun" value="yes" />');
1485: } else {
1486: $r->print('<input type="hidden" name="firstrun" value="no" />');
1487: }
1488: #
1489: return @Buttons;
1490: }
1491:
1.36 matthew 1492: sub gather_full_student_data {
1493: my ($r,$formname,$inputname) = @_;
1494: my $status_type;
1495: if (defined($formname)) {
1496: $status_type = 'inline';
1497: } else {
1498: $status_type = 'popup';
1499: }
1500: my $c = $r->connection();
1501: #
1502: &Apache::loncoursedata::clear_internal_caches();
1503: #
1504: my @Students = @Apache::lonstatistics::Students;
1505: #
1506: # Open the progress window
1.68 www 1507: my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin($r,scalar(@Students));
1.36 matthew 1508: #
1509: while (my $student = shift @Students) {
1510: return if ($c->aborted());
1511: my $status = &Apache::loncoursedata::ensure_current_full_data
1512: ($student->{'username'},$student->{'domain'},
1.46 albertel 1513: $env{'request.course.id'});
1.36 matthew 1514: &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
1515: &mt('last student'));
1516: }
1517: &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1518: $r->rflush();
1519: return;
1520: }
1.12 matthew 1521:
1.38 matthew 1522: ####################################################
1523: ####################################################
1524:
1525: =pod
1526:
1527: =item &submission_report_form
1528:
1529: Input: The originating reportSelected value for the current stats page.
1530:
1531: Output: Scalar containing HTML with needed form elements and a link to
1532: the student submission reports page.
1533:
1534: =cut
1535:
1536: ####################################################
1537: ####################################################
1538: sub submission_report_form {
1539: my ($original_report) = @_;
1540: # Note: In the link below we change the reportSelected value. If
1541: # the user hits the 'back' button on the browser after getting their
1542: # student submissions report, this value may still be around. So we
1543: # output a script block to set it properly. If the $original_report
1544: # value is unset, you are just asking for trouble.
1545: if (! defined($original_report)) {
1546: &Apache::lonnet::logthis
1547: ('someone called lonstathelpers::submission_report_form without '.
1548: ' enough input.');
1549: }
1550: my $html = $/.
1.58 bisitz 1551: '<script type="text/javascript" language="JavaScript">'.
1.38 matthew 1552: "document.Statistics.reportSelected.value='$original_report';".
1553: '</script>'.
1554: '<input type="hidden" name="correctans" value="true" />'.
1555: '<input type="hidden" name="prob_status" value="true" />'.
1556: '<input type="hidden" name="all_sub" value="true" />';
1557: my $output_selector = $/.'<select name="output">'.$/;
1558: foreach ('HTML','Excel','CSV') {
1559: $output_selector .= ' <option value="'.lc($_).'"';
1.46 albertel 1560: if ($env{'form.output'} eq lc($_)) {
1.38 matthew 1561: $output_selector .= ' selected ';
1562: }
1563: $output_selector .='>'.&mt($_).'</option>'.$/;
1564: }
1565: $output_selector .= '</select>'.$/;
1566: my $link = '<a href="javascript:'.
1567: q{document.Statistics.reportSelected.value='student_submission_reports';}.
1568: 'document.Statistics.submit();">';
1569: $html.= &mt('View data as [_1] [_2]go[_3]',$output_selector,
1570: $link,'</a>').$/;
1571: return $html
1572: }
1.12 matthew 1573:
1574: ####################################################
1575: ####################################################
1576:
1577: =pod
1578:
1.2 matthew 1579: =back
1580:
1581: =cut
1582:
1583: ####################################################
1584: ####################################################
1.1 matthew 1585:
1586: 1;
1587:
1588: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>