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

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

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