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

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

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