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