File:  [LON-CAPA] / loncom / interface / statistics / lonstathelpers.pm
Revision 1.26: download - view: text, annotated - select for diffs
Wed Sep 29 14:56:59 2004 UTC (19 years, 9 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
&MultipleProblemSelector: Added 'view' links for problems.

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

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