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

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

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