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

1.1       matthew     1: # The LearningOnline Network with CAPA
                      2: #
1.33    ! albertel    3: # $Id: lonstathelpers.pm,v 1.32 2004/12/02 21:53:33 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).
1.33    ! albertel  153:                             qq{<a target="preview" href="$res->{'src'}?symb=$res->{'symb'}">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) = @_;
1.31      matthew   558:     my %answers;
1.12      matthew   559:     my $status_type;
                    560:     if (defined($formname)) {
                    561:         $status_type = 'inline';
                    562:     } else {
                    563:         $status_type = 'popup';
                    564:     }    
1.9       matthew   565:     my $c = $r->connection();
                    566:     my %Answers;
                    567:     my ($resource,$partid,$respid) = ($problem->{'resource'},
                    568:                                       $problem->{'part'},
                    569:                                       $problem->{'respid'});
                    570:     # Read in the cache (if it exists) before we start timing things.
                    571:     &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
                    572:     # Open progress window
                    573:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
                    574:         ($r,'Student Answer Compilation Status',
1.12      matthew   575:          'Student Answer Compilation Progress', scalar(@$Students),
                    576:          $status_type,undef,$formname,$inputname);
1.9       matthew   577:     $r->rflush();
                    578:     foreach my $student (@$Students) {
                    579:         last if ($c->aborted());
                    580:         my $sname = $student->{'username'};
                    581:         my $sdom = $student->{'domain'};
1.30      matthew   582:         my $answer = &Apache::lonstathelpers::get_student_answer
1.9       matthew   583:             ($resource,$sname,$sdom,$partid,$respid);
                    584:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
                    585:                                                  &mt('last student'));
1.31      matthew   586:         $answers{$answer}++;
1.9       matthew   587:         $student->{'answer'} = $answer;
                    588:     }
1.29      matthew   589:     &Apache::lonstathelpers::write_analysis_cache();
1.9       matthew   590:     return if ($c->aborted());
                    591:     $r->rflush();
                    592:     # close progress window
                    593:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
1.31      matthew   594:     return \%answers;
1.9       matthew   595: }
1.4       matthew   596: 
                    597: #####################################################
                    598: #####################################################
                    599: 
                    600: =pod
                    601: 
                    602: =item analyze_problem_as_student
                    603: 
1.30      matthew   604: Analyzes a homework problem for a student
1.4       matthew   605: 
                    606: Inputs: $resource: a resource object
                    607:         $sname, $sdom, $partid, $respid
                    608: 
1.30      matthew   609: Returns: the problem analysis hash
1.6       matthew   610: 
1.4       matthew   611: =cut
                    612: 
                    613: #####################################################
                    614: #####################################################
                    615: sub analyze_problem_as_student {
1.30      matthew   616:     my ($resource,$sname,$sdom) = @_;
1.22      matthew   617:     if (ref($resource) ne 'HASH') {
                    618:         my $res = $resource;
                    619:         $resource = { 'src' => $res->src,
                    620:                       'symb' => $res->symb,
                    621:                       'parts' => $res->parts };
                    622:         foreach my $part (@{$resource->{'parts'}}) {
                    623:             $resource->{'partdata'}->{$part}->{'ResponseIds'}=
                    624:                 [$res->responseIds($part)];
                    625:         }
                    626:     }
1.4       matthew   627:     my $url = $resource->{'src'};
                    628:     my $symb = $resource->{'symb'};
1.29      matthew   629:     my $analysis = &get_from_analysis_cache($sname,$sdom,$symb);
                    630:     if (! defined($analysis)) {
                    631:         my $courseid = $ENV{'request.course.id'};
                    632:         my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
                    633:                                             'grade_domain' => $sdom,
                    634:                                             'grade_username' => $sname,
                    635:                                             'grade_symb' => $symb,
                    636:                                             'grade_courseid' => $courseid));
                    637:         (my $garbage,$analysis)=split(/_HASH_REF__/,$Answ,2);
                    638:         &store_analysis($sname,$sdom,$symb,$analysis);
                    639:     }
                    640:     my %Answer=&Apache::lonnet::str2hash($analysis);
1.6       matthew   641:     #
1.30      matthew   642:     return \%Answer;
                    643: }
                    644: 
                    645: #####################################################
                    646: #####################################################
                    647: 
                    648: =pod
                    649: 
                    650: =item get_student_answer
                    651: 
                    652: Analyzes a homework problem for a particular student and returns the correct 
                    653: answer.  Attempts to put together an answer for problem types 
                    654: that do not natively support it.
                    655: 
                    656: Inputs: $resource: a resource object (from navmaps or hash from loncoursedata)
                    657:         $sname, $sdom, $partid, $respid
                    658: 
                    659: Returns: $answer
                    660: 
                    661: If $partid and $respid are specified, $answer is simply a scalar containing
                    662: the correct answer for the response.
                    663: 
                    664: If $partid or $respid are undefined, $answer will be a hash reference with
                    665: keys $partid.'.'.$respid.'.answer'.
                    666: 
                    667: =cut
                    668: 
                    669: #####################################################
                    670: #####################################################
                    671: sub get_student_answer {
                    672:     my ($resource,$sname,$sdom,$partid,$respid) = @_;
                    673:     #
                    674:     if (ref($resource) ne 'HASH') {
                    675:         my $res = $resource;
                    676:         $resource = { 'src' => $res->src,
                    677:                       'symb' => $res->symb,
                    678:                       'parts' => $res->parts };
                    679:         foreach my $part (@{$resource->{'parts'}}) {
                    680:             $resource->{'partdata'}->{$part}->{'ResponseIds'}=
                    681:                 [$res->responseIds($part)];
                    682:         }
                    683:     }
                    684:     #
                    685:     my $analysis = 
                    686:         &analyze_problem_as_student($resource,$sname,$sdom);
1.29      matthew   687:     my $answer;
1.8       matthew   688:     foreach my $partid (@{$resource->{'parts'}}) {
1.6       matthew   689:         my $partdata = $resource->{'partdata'}->{$partid};
                    690:         foreach my $respid (@{$partdata->{'ResponseIds'}}) {
                    691:             my $prefix = $partid.'.'.$respid;
                    692:             my $key = $prefix.'.answer';
1.30      matthew   693:             $answer->{$partid}->{$respid} = 
                    694:                 &get_answer($prefix,$key,%$analysis);
1.6       matthew   695:         }
1.8       matthew   696:     }
1.30      matthew   697:     my $returnvalue;
1.8       matthew   698:     if (! defined($partid)) {
                    699:         $returnvalue = $answer;
                    700:     } elsif (! defined($respid)) {
                    701:         $returnvalue = $answer->{$partid};
1.6       matthew   702:     } else {
1.8       matthew   703:         $returnvalue = $answer->{$partid}->{$respid};
1.6       matthew   704:     }
                    705:     return $returnvalue;
                    706: }
                    707: 
                    708: sub get_answer {
                    709:     my ($prefix,$key,%Answer) = @_;
                    710:     my $returnvalue;
1.4       matthew   711:     if (exists($Answer{$key})) {
                    712:         my $student_answer = $Answer{$key}->[0];
                    713:         if (! defined($student_answer)) {
                    714:             $student_answer = $Answer{$key}->[1];
                    715:         }
                    716:         $returnvalue = $student_answer;
                    717:     } else {
                    718:         if (exists($Answer{$prefix.'.shown'})) {
                    719:             # The response has foils
                    720:             my %values;
                    721:             while (my ($k,$v) = each(%Answer)) {
                    722:                 next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
                    723:                 my $foilname = $2;
                    724:                 $values{$foilname}=$v;
                    725:             }
                    726:             foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
                    727:                 if (ref($values{$foil}) eq 'ARRAY') {
1.10      albertel  728:                     $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
                    729:                         join(',',map {&HTML::Entities::encode($_,'<>&"')} @{$values{$foil}}).'&';
1.4       matthew   730:                 } else {
1.10      albertel  731:                     $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
                    732:                         &HTML::Entities::encode($values{$foil},'<>&"').'&';
1.4       matthew   733:                 }
                    734:             }
                    735:             $returnvalue =~ s/ /\%20/g;
                    736:             chop ($returnvalue);
                    737:         }
                    738:     }
                    739:     return $returnvalue;
                    740: }
1.8       matthew   741: 
                    742: #####################################################
                    743: #####################################################
                    744: 
                    745: =pod
                    746: 
                    747: =item Caching routines
                    748: 
                    749: =over 4
                    750: 
1.29      matthew   751: =item &load_analysis_cache($symb)
1.8       matthew   752: 
                    753: Loads the cache for the given symb into memory from disk.  
                    754: Requires the cache filename be set.  
                    755: Only should be called by &ensure_proper_cache.
                    756: 
                    757: =cut
                    758: 
                    759: #####################################################
                    760: #####################################################
                    761: {
                    762:     my $cache_filename = undef;
                    763:     my $current_symb = undef;
                    764:     my %cache;
                    765: 
1.29      matthew   766: sub load_analysis_cache {
1.8       matthew   767:     my ($symb) = @_;
                    768:     return if (! defined($cache_filename));
                    769:     if (! defined($current_symb) || $current_symb ne $symb) {
                    770:         undef(%cache);
                    771:         my $storedstring;
                    772:         my %cache_db;
                    773:         if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_READER(),0640)) {
                    774:             $storedstring = $cache_db{&Apache::lonnet::escape($symb)};
                    775:             untie(%cache_db);
                    776:         }
                    777:         if (defined($storedstring)) {
                    778:             %cache = %{thaw($storedstring)};
                    779:         }
                    780:     }
                    781:     return;
                    782: }
                    783: 
                    784: #####################################################
                    785: #####################################################
                    786: 
                    787: =pod
                    788: 
1.29      matthew   789: =item &get_from_analysis_cache($sname,$sdom,$symb,$partid,$respid)
1.8       matthew   790: 
                    791: Returns the appropriate data from the cache, or undef if no data exists.
                    792: 
                    793: =cut
                    794: 
                    795: #####################################################
                    796: #####################################################
1.29      matthew   797: sub get_from_analysis_cache {
                    798:     my ($sname,$sdom,$symb) = @_;
1.8       matthew   799:     &ensure_proper_cache($symb);
                    800:     my $returnvalue;
1.29      matthew   801:     if (exists($cache{$sname.':'.$sdom})) {
                    802:         $returnvalue = $cache{$sname.':'.$sdom};
1.8       matthew   803:     } else {
                    804:         $returnvalue = undef;
                    805:     }
                    806:     return $returnvalue;
                    807: }
                    808: 
                    809: #####################################################
                    810: #####################################################
                    811: 
                    812: =pod
                    813: 
1.29      matthew   814: =item &write_analysis_cache($symb)
1.8       matthew   815: 
                    816: Writes the in memory cache to disk so that it can be read in with
1.29      matthew   817: &load_analysis_cache($symb).
1.8       matthew   818: 
                    819: =cut
                    820: 
                    821: #####################################################
                    822: #####################################################
1.29      matthew   823: sub write_analysis_cache {
1.8       matthew   824:     return if (! defined($current_symb) || ! defined($cache_filename));
                    825:     my %cache_db;
                    826:     my $key = &Apache::lonnet::escape($current_symb);
                    827:     if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_WRCREAT(),0640)) {
                    828:         my $storestring = freeze(\%cache);
                    829:         $cache_db{$key}=$storestring;
                    830:         $cache_db{$key.'.time'}=time;
                    831:         untie(%cache_db);
                    832:     }
                    833:     undef(%cache);
                    834:     undef($current_symb);
                    835:     undef($cache_filename);
                    836:     return;
                    837: }
                    838: 
                    839: #####################################################
                    840: #####################################################
                    841: 
                    842: =pod
                    843: 
                    844: =item &ensure_proper_cache($symb)
                    845: 
                    846: Called to make sure we have the proper cache set up.  This is called
1.29      matthew   847: prior to every analysis lookup.
1.8       matthew   848: 
                    849: =cut
                    850: 
                    851: #####################################################
                    852: #####################################################
                    853: sub ensure_proper_cache {
                    854:     my ($symb) = @_;
                    855:     my $cid = $ENV{'request.course.id'};
                    856:     my $new_filename =  '/home/httpd/perl/tmp/'.
1.29      matthew   857:         'problemanalysis_'.$cid.'_analysis_cache.db';
1.8       matthew   858:     if (! defined($cache_filename) ||
                    859:         $cache_filename ne $new_filename ||
                    860:         ! defined($current_symb)   ||
                    861:         $current_symb ne $symb) {
                    862:         $cache_filename = $new_filename;
                    863:         # Notice: $current_symb is not set to $symb until after the cache is
1.29      matthew   864:         # loaded.  This is what tells &load_analysis_cache to load in a new
1.8       matthew   865:         # symb cache.
1.29      matthew   866:         &load_analysis_cache($symb);
1.8       matthew   867:         $current_symb = $symb;
                    868:     }
                    869: }
                    870: 
                    871: #####################################################
                    872: #####################################################
                    873: 
                    874: =pod
                    875: 
1.29      matthew   876: =item &store_analysis($sname,$sdom,$symb,$partid,$respid,$dataset)
1.8       matthew   877: 
1.29      matthew   878: Stores the analysis data in the in memory cache.
1.8       matthew   879: 
                    880: =cut
                    881: 
                    882: #####################################################
                    883: #####################################################
1.29      matthew   884: sub store_analysis {
                    885:     my ($sname,$sdom,$symb,$dataset) = @_;
1.8       matthew   886:     return if ($symb ne $current_symb);
1.29      matthew   887:     $cache{$sname.':'.$sdom}=$dataset;
1.8       matthew   888:     return;
                    889: }
                    890: 
                    891: }
                    892: #####################################################
                    893: #####################################################
                    894: 
                    895: =pod
                    896: 
                    897: =back
                    898: 
                    899: =cut
                    900: 
                    901: #####################################################
                    902: #####################################################
1.4       matthew   903: 
                    904: ##
                    905: ## The following is copied from datecalc1.pl, part of the 
                    906: ## Spreadsheet::WriteExcel CPAN module.
                    907: ##
                    908: ##
                    909: ######################################################################
                    910: #
                    911: # Demonstration of writing date/time cells to Excel spreadsheets,
                    912: # using UNIX/Perl time as source of date/time.
                    913: #
                    914: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
                    915: #
                    916: ######################################################################
                    917: #
                    918: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
                    919: # measured in seconds.
                    920: #
                    921: # An Excel file can use exactly one of two different date/time systems.
                    922: # In these systems, a floating point number represents the number of days
                    923: # (and fractional parts of the day) since a start point. The floating point
                    924: # number is referred to as a 'serial'.
                    925: # The two systems ('1900' and '1904') use different starting points:
                    926: #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
                    927: #          a leap year - see:
                    928: #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
                    929: #          for the excuse^H^H^H^H^H^Hreason.
                    930: #  '1904'; '1.00' is 2 Jan 1904.
                    931: #
                    932: # The '1904' system is the default for Apple Macs. Windows versions of
                    933: # Excel have the option to use the '1904' system.
                    934: #
                    935: # Note that Visual Basic's "DateSerial" function does NOT erroneously
                    936: # regard 1900 as a leap year, and thus its serials do not agree with
                    937: # the 1900 serials of Excel for dates before 1 Mar 1900.
                    938: #
                    939: # Note that StarOffice (at least at version 5.2) does NOT erroneously
                    940: # regard 1900 as a leap year, and thus its serials do not agree with
                    941: # the 1900 serials of Excel for dates before 1 Mar 1900.
                    942: #
                    943: ######################################################################
                    944: #
                    945: # Calculation description
                    946: # =======================
                    947: #
                    948: # 1900 system
                    949: # -----------
                    950: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
                    951: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
                    952: # were leap years with an extra day.
                    953: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
                    954: # 1 Jan 1970.
                    955: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
                    956: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
                    957: #
                    958: # 1904 system
                    959: # -----------
                    960: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
                    961: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
                    962: # were leap years with an extra day.
                    963: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
                    964: # 1 Jan 1970.
                    965: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
                    966: #
                    967: ######################################################################
                    968: #
                    969: # Copyright (c) 2000, Andrew Benham.
                    970: # This program is free software. It may be used, redistributed and/or
                    971: # modified under the same terms as Perl itself.
                    972: #
                    973: # Andrew Benham, adsb@bigfoot.com
                    974: # London, United Kingdom
                    975: # 11 Nov 2000
                    976: #
                    977: ######################################################################
                    978: #-----------------------------------------------------------
                    979: # calc_serial()
                    980: #
                    981: # Called with (up to) 2 parameters.
                    982: #   1.  Unix timestamp.  If omitted, uses current time.
                    983: #   2.  GMT flag. Set to '1' to return serial in GMT.
                    984: #       If omitted, returns serial in appropriate timezone.
                    985: #
                    986: # Returns date/time serial according to $DATE_SYSTEM selected
                    987: #-----------------------------------------------------------
                    988: sub calc_serial {
                    989:     # Use 1900 date system on all platforms other than Apple Mac (for which
                    990:     # use 1904 date system).
                    991:     my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
                    992:     my $time = (defined $_[0]) ? $_[0] : time();
                    993:     my $gmtflag = (defined $_[1]) ? $_[1] : 0;
                    994:     #
                    995:     # Divide timestamp by number of seconds in a day.
                    996:     # This gives a date serial with '0' on 1 Jan 1970.
                    997:     my $serial = $time / 86400;
                    998:     #
                    999:     # Adjust the date serial by the offset appropriate to the
                   1000:     # currently selected system (1900/1904).
                   1001:     if ($DATE_SYSTEM == 0) {        # use 1900 system
                   1002:         $serial += 25569;
                   1003:     } else {                        # use 1904 system
                   1004:         $serial += 24107;
                   1005:     }
                   1006:     #
                   1007:     unless ($gmtflag) {
                   1008:         # Now have a 'raw' serial with the right offset. But this
                   1009:         # gives a serial in GMT, which is false unless the timezone
                   1010:         # is GMT. We need to adjust the serial by the appropriate
                   1011:         # timezone offset.
                   1012:         # Calculate the appropriate timezone offset by seeing what
                   1013:         # the differences between localtime and gmtime for the given
                   1014:         # time are.
                   1015:         #    
                   1016:         my @gmtime = gmtime($time);
                   1017:         my @ltime  = localtime($time);
                   1018:         #
                   1019:         # For the first 7 elements of the two arrays, adjust the
                   1020:         # date serial where the elements differ.
                   1021:         for (0 .. 6) {
                   1022:             my $diff = $ltime[$_] - $gmtime[$_];
                   1023:             if ($diff) {
                   1024:                 $serial += _adjustment($diff,$_);
                   1025:             }
                   1026:         }
                   1027:     }
                   1028:     #
                   1029:     # Perpetuate the error that 1900 was a leap year by decrementing
                   1030:     # the serial if we're using the 1900 system and the date is prior to
                   1031:     # 1 Mar 1900. This has the effect of making serial value '60'
                   1032:     # 29 Feb 1900.
                   1033:     #
                   1034:     # This fix only has any effect if UNIX/Perl time on the platform
                   1035:     # can represent 1900. Many can't.
                   1036:     #
                   1037:     unless ($DATE_SYSTEM) {
                   1038:         $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
                   1039:     }
                   1040:     return $serial;
                   1041: }
                   1042: 
                   1043: sub _adjustment {
                   1044:     # Based on the difference in the localtime/gmtime array elements
                   1045:     # number, return the adjustment required to the serial.
                   1046:     #
                   1047:     # We only look at some elements of the localtime/gmtime arrays:
                   1048:     #    seconds    unlikely to be different as all known timezones
                   1049:     #               have an offset of integral multiples of 15 minutes,
                   1050:     #               but it's easy to do.
                   1051:     #    minutes    will be different for timezone offsets which are
                   1052:     #               not an exact number of hours.
                   1053:     #    hours      very likely to be different.
                   1054:     #    weekday    will differ when localtime/gmtime difference
                   1055:     #               straddles midnight.
                   1056:     #
                   1057:     # Assume that difference between localtime and gmtime is less than
                   1058:     # 5 days, then don't have to do maths for day of month, month number,
                   1059:     # year number, etc...
                   1060:     #
                   1061:     my ($delta,$element) = @_;
                   1062:     my $adjust = 0;
                   1063:     #
                   1064:     if ($element == 0) {            # Seconds
                   1065:         $adjust = $delta/86400;         # 60 * 60 * 24
                   1066:     } elsif ($element == 1) {       # Minutes
                   1067:         $adjust = $delta/1440;          # 60 * 24
                   1068:     } elsif ($element == 2) {       # Hours
                   1069:         $adjust = $delta/24;            # 24
                   1070:     } elsif ($element == 6) {       # Day of week number
                   1071:         # Catch difference straddling Sat/Sun in either direction
                   1072:         $delta += 7 if ($delta < -4);
                   1073:         $delta -= 7 if ($delta > 4);
                   1074:         #    
                   1075:         $adjust = $delta;
                   1076:     }
                   1077:     return $adjust;
                   1078: }
                   1079: 
                   1080: ###########################################################
                   1081: ###########################################################
                   1082: 
                   1083: =pod
                   1084: 
                   1085: =item get_problem_data
                   1086: 
                   1087: Returns a data structure describing the problem.
                   1088: 
                   1089: Inputs: $url
                   1090: 
                   1091: Returns: %Partdata
                   1092: 
                   1093: =cut
                   1094: 
                   1095: ## note: we must force each foil and option to not begin or end with
                   1096: ##       spaces as they are stored without such data.
                   1097: ##
                   1098: ###########################################################
                   1099: ###########################################################
                   1100: sub get_problem_data {
                   1101:     my ($url) = @_;
                   1102:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
                   1103:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
                   1104:     my %Answer;
                   1105:     %Answer=&Apache::lonnet::str2hash($Answ);
                   1106:     my %Partdata;
                   1107:     foreach my $part (@{$Answer{'parts'}}) {
                   1108:         while (my($key,$value) = each(%Answer)) {
                   1109:             #
                   1110:             # Logging code:
1.7       matthew  1111:             if (0) {
1.4       matthew  1112:                 &Apache::lonnet::logthis($part.' got key "'.$key.'"');
                   1113:                 if (ref($value) eq 'ARRAY') {
                   1114:                     &Apache::lonnet::logthis('    @'.join(',',@$value));
                   1115:                 } else {
                   1116:                     &Apache::lonnet::logthis('    '.$value);
                   1117:                 }
                   1118:             }
                   1119:             # End of logging code
                   1120:             next if ($key !~ /^$part/);
                   1121:             $key =~ s/^$part\.//;
                   1122:             if (ref($value) eq 'ARRAY') {
                   1123:                 if ($key eq 'options') {
                   1124:                     $Partdata{$part}->{'_Options'}=$value;
                   1125:                 } elsif ($key eq 'concepts') {
                   1126:                     $Partdata{$part}->{'_Concepts'}=$value;
1.28      matthew  1127:                 } elsif ($key eq 'items') {
                   1128:                     $Partdata{$part}->{'_Items'}=$value;
1.4       matthew  1129:                 } elsif ($key =~ /^concept\.(.*)$/) {
                   1130:                     my $concept = $1;
                   1131:                     foreach my $foil (@$value) {
                   1132:                         $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
                   1133:                                                                       $concept;
                   1134:                     }
1.32      matthew  1135:                 } elsif ($key =~ /^(unit|incorrect|answer|ans_low|ans_high|str_type)$/) {
1.4       matthew  1136:                     $Partdata{$part}->{$key}=$value;
                   1137:                 }
                   1138:             } else {
                   1139:                 if ($key=~ /^foil\.text\.(.*)$/) {
                   1140:                     my $foil = $1;
                   1141:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
                   1142:                     $value =~ s/(\s*$|^\s*)//g;
                   1143:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
                   1144:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
                   1145:                     my $foil = $1;
                   1146:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
1.28      matthew  1147:                 } elsif ($key eq 'answercomputed') {
                   1148:                     $Partdata{$part}->{'answercomputed'} = $value;
1.4       matthew  1149:                 }
                   1150:             }
                   1151:         }
                   1152:     }
1.28      matthew  1153:     # Further debugging code
                   1154:     if (0) {
                   1155:         &Apache::lonnet::logthis('lonstathelpers::get_problem_data');
                   1156:         &log_hash_ref(\%Partdata);
                   1157:     }
1.4       matthew  1158:     return %Partdata;
1.5       matthew  1159: }
                   1160: 
1.28      matthew  1161: sub log_array_ref {
                   1162:     my ($arrayref,$prefix) = @_;
                   1163:     return if (ref($arrayref) ne 'ARRAY');
                   1164:     if (! defined($prefix)) { $prefix = ''; };
                   1165:     foreach my $v (@$arrayref) {
                   1166:         if (ref($v) eq 'ARRAY') {
                   1167:             &log_array_ref($v,$prefix.'  ');
                   1168:         } elsif (ref($v) eq 'HASH') {
                   1169:             &log_hash_ref($v,$prefix.'  ');
                   1170:         } else {
                   1171:             &Apache::lonnet::logthis($prefix.'"'.$v.'"');
                   1172:         }
                   1173:     }
                   1174: }
                   1175: 
                   1176: sub log_hash_ref {
                   1177:     my ($hashref,$prefix) = @_;
                   1178:     return if (ref($hashref) ne 'HASH');
                   1179:     if (! defined($prefix)) { $prefix = ''; };
                   1180:     while (my ($k,$v) = each(%$hashref)) {
                   1181:         if (ref($v) eq 'ARRAY') {
                   1182:             &Apache::lonnet::logthis($prefix.'"'.$k.'" = array');
                   1183:             &log_array_ref($v,$prefix.'  ');
                   1184:         } elsif (ref($v) eq 'HASH') {
                   1185:             &Apache::lonnet::logthis($prefix.'"'.$k.'" = hash');
                   1186:             &log_hash_ref($v,$prefix.'  ');
                   1187:         } else {
                   1188:             &Apache::lonnet::logthis($prefix.'"'.$k.'" => "'.$v.'"');
                   1189:         }
                   1190:     }
                   1191: }
1.5       matthew  1192: ####################################################
                   1193: ####################################################
                   1194: 
                   1195: =pod
                   1196: 
                   1197: =item &limit_by_time()
                   1198: 
                   1199: =cut
                   1200: 
                   1201: ####################################################
                   1202: ####################################################
                   1203: sub limit_by_time_form {
                   1204:     my $Starttime_form = '';
                   1205:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
                   1206:         ('limitby_startdate');
                   1207:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
                   1208:         ('limitby_enddate');
                   1209:     if (! defined($endtime)) {
                   1210:         $endtime = time;
                   1211:     }
                   1212:     if (! defined($starttime)) {
                   1213:         $starttime = $endtime - 60*60*24*7;
                   1214:     }
                   1215:     my $state;
                   1216:     if (&limit_by_time()) {
                   1217:         $state = '';
                   1218:     } else {
                   1219:         $state = 'disabled';
                   1220:     }
                   1221:     my $startdateform = &Apache::lonhtmlcommon::date_setter
                   1222:         ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
                   1223:     my $enddateform = &Apache::lonhtmlcommon::date_setter
                   1224:         ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
                   1225:     my $Str;
                   1226:     $Str .= '<script language="Javascript" >';
                   1227:     $Str .= 'function toggle_limitby_activity(state) {';
                   1228:     $Str .= '    if (state) {';
                   1229:     $Str .= '        limitby_startdate_enable();';
                   1230:     $Str .= '        limitby_enddate_enable();';
                   1231:     $Str .= '    } else {';
                   1232:     $Str .= '        limitby_startdate_disable();';
                   1233:     $Str .= '        limitby_enddate_disable();';
                   1234:     $Str .= '    }';    
                   1235:     $Str .= '}';
                   1236:     $Str .= '</script>';
                   1237:     $Str .= '<fieldset>';
                   1238:     my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
                   1239:     if (&limit_by_time()) {
                   1240:         $timecheckbox .= ' checked ';
                   1241:     } 
                   1242:     $timecheckbox .= 'OnChange="javascript:toggle_limitby_activity(this.checked);" ';
                   1243:     $timecheckbox .= ' />';
                   1244:     $Str .= '<legend>'.&mt('[_1] Limit by time',$timecheckbox).'</legend>';
                   1245:     $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
                   1246:     $Str .= &mt('&nbsp;End Time: [_1]',$enddateform).'<br />';
                   1247:     $Str .= '</fieldset>';
                   1248:     return $Str;
                   1249: }
                   1250: 
                   1251: sub limit_by_time {
                   1252:     if (exists($ENV{'form.limit_by_time'}) &&
                   1253:         $ENV{'form.limit_by_time'} ne '' ) {
                   1254:         return 1;
                   1255:     } else {
                   1256:         return 0;
                   1257:     }
                   1258: }
                   1259: 
                   1260: sub get_time_limits {
                   1261:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
                   1262:         ('limitby_startdate');
                   1263:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
                   1264:         ('limitby_enddate');
                   1265:     return ($starttime,$endtime);
1.11      matthew  1266: }
                   1267: 
                   1268: 
                   1269: 
                   1270: ####################################################
                   1271: ####################################################
                   1272: 
                   1273: =pod
                   1274: 
                   1275: =item sections_description 
                   1276: 
                   1277: Inputs: @Sections, an array of sections
                   1278: 
                   1279: Returns: A text description of the sections selected.
                   1280: 
                   1281: =cut
                   1282: 
                   1283: ####################################################
                   1284: ####################################################
                   1285: sub sections_description {
                   1286:     my @Sections = @_;
                   1287:     my $sectionstring = '';
                   1288:     if (scalar(@Sections) > 1) {
                   1289:         if (scalar(@Sections) > 2) {
                   1290:             my $last = pop(@Sections);
                   1291:             $sectionstring = "Sections ".join(', ',@Sections).', and '.$last;
                   1292:         } else {
                   1293:             $sectionstring = "Sections ".join(' and ',@Sections);
                   1294:         }
                   1295:     } else {
                   1296:         if ($Sections[0] eq 'all') {
                   1297:             $sectionstring = "All sections";
                   1298:         } else {
                   1299:             $sectionstring = "Section ".$Sections[0];
                   1300:         }
                   1301:     }
                   1302:     return $sectionstring;
1.2       matthew  1303: }
                   1304: 
                   1305: ####################################################
                   1306: ####################################################
                   1307: 
                   1308: =pod
                   1309: 
1.12      matthew  1310: =item &manage_caches
                   1311: 
                   1312: Inputs: $r, apache request object
                   1313: 
                   1314: Returns: An array of scalars containing html for buttons.
                   1315: 
                   1316: =cut
                   1317: 
                   1318: ####################################################
                   1319: ####################################################
                   1320: sub manage_caches {
1.23      matthew  1321:     my ($r,$formname,$inputname,$update_message) = @_;
1.12      matthew  1322:     &Apache::loncoursedata::clear_internal_caches();
1.16      matthew  1323:     my $sectionkey = 
                   1324:         join(',',
                   1325:              map {
                   1326:                      &Apache::lonnet::escape($_);
                   1327:                  } sort(@Apache::lonstatistics::SelectedSections)
                   1328:              );
                   1329:     my $statuskey = $Apache::lonstatistics::enrollment_status;
1.12      matthew  1330:     if (exists($ENV{'form.ClearCache'}) || 
1.16      matthew  1331:         exists($ENV{'form.updatecaches'}) || 
                   1332:         (exists($ENV{'form.firstrun'}) && $ENV{'form.firstrun'} ne 'no') ||
                   1333:         (exists($ENV{'form.prevsection'}) &&
                   1334:             $ENV{'form.prevsection'} ne $sectionkey) ||
                   1335:         (exists($ENV{'form.prevenrollstatus'}) &&
                   1336:             $ENV{'form.prevenrollstatus'} ne $statuskey)
                   1337:         ) {
1.23      matthew  1338:         if (defined($update_message)) {
                   1339:             $r->print($update_message);
                   1340:         }
1.12      matthew  1341:         &Apache::lonstatistics::Gather_Full_Student_Data($r,$formname,
                   1342:                                                          $inputname);
1.23      matthew  1343:             
1.12      matthew  1344:     }
                   1345:     #
1.16      matthew  1346:     my @Buttons = 
                   1347:         ('<input type="submit" name="ClearCache" '.
                   1348:              'value="'.&mt('Clear Caches').'" />',
                   1349:          '<input type="submit" name="updatecaches" '.
1.17      matthew  1350:              'value="'.&mt('Update Caches').'" />'.
                   1351:          &Apache::loncommon::help_open_topic('Statistics_Cache'),
1.16      matthew  1352:          '<input type="hidden" name="prevsection" value="'.$sectionkey.'" />',
                   1353:          '<input type="hidden" name="prevenrollstatus" value="'.$statuskey.'" />'
                   1354:          );
                   1355:     #
1.12      matthew  1356:     if (! exists($ENV{'form.firstrun'})) {
                   1357:         $r->print('<input type="hidden" name="firstrun" value="yes" />');
                   1358:     } else {
                   1359:         $r->print('<input type="hidden" name="firstrun" value="no" />');
                   1360:     }
                   1361:     #
                   1362:     return @Buttons;
                   1363: }
                   1364: 
                   1365: 
                   1366: 
                   1367: 
                   1368: ####################################################
                   1369: ####################################################
                   1370: 
                   1371: =pod
                   1372: 
1.2       matthew  1373: =back
                   1374: 
                   1375: =cut
                   1376: 
                   1377: ####################################################
                   1378: ####################################################
1.1       matthew  1379: 
                   1380: 1;
                   1381: 
                   1382: __END__

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