Annotation of loncom/interface/statistics/lonstathelpers.pm, revision 1.28

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>