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

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

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