File:  [LON-CAPA] / loncom / interface / statistics / lonstathelpers.pm
Revision 1.63: download - view: text, annotated - select for diffs
Wed Aug 4 01:56:07 2010 UTC (13 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, HEAD
- Bug 6119.
  - identifying information about user not displayed for problem parts
    with type set to anonymous survey (with/without credit).
  - all items must be either anonymous survey items, or named items
    when displaying submissions for selected items.
  - rows of students is randomized when showing rows of submission data
    for anonymous survey items.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstathelpers.pm,v 1.63 2010/08/04 01:56:07 raeburn 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: use lib '/home/httpd/lib/perl/';
   64: use LONCAPA;
   65:  
   66: 
   67: ####################################################
   68: ####################################################
   69: 
   70: =pod
   71: 
   72: =item &render_resource($resource)
   73: 
   74: Input: a navmaps resource
   75: 
   76: Retunrs: a scalar containing html for a rendering of the problem
   77: within a table.
   78: 
   79: =cut
   80: 
   81: ####################################################
   82: ####################################################
   83: sub render_resource {
   84:     my ($resource) = @_;
   85:     ##
   86:     ## Render the problem
   87:     my ($base) = ($resource->src =~ m|^(.*/)[^/]*$|);
   88:     $base="http://".$ENV{'SERVER_NAME'}.$base;
   89:     my ($src,$symb)=($resource->link,&escape($resource->shown_symb));
   90:     my $rendered_problem = &Apache::lonnet::ssi_body($src.'?symb='.$symb);
   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: }
   98: 
   99: ####################################################
  100: ####################################################
  101: 
  102: =pod
  103: 
  104: =item &get_resources
  105: 
  106: =cut
  107: 
  108: ####################################################
  109: ####################################################
  110: sub get_resources {
  111:     my ($navmap,$sequence) = @_;
  112:     my @resources = $navmap->retrieveResources($sequence,
  113:                                                sub { shift->is_problem(); },
  114:                                                0,0,0);
  115:     return @resources;
  116: }
  117: 
  118: ####################################################
  119: ####################################################
  120: 
  121: =pod
  122: 
  123: =item &problem_selector($AcceptedResponseTypes)
  124: 
  125: Input: scalar containing regular expression which matches response
  126: types to show.  '.' will yield all, '(option|radiobutton)' will match
  127: all option response and radiobutton problems.
  128: 
  129: Returns: A string containing html for a table which lists the sequences
  130: and their contents.  A radiobutton is provided for each problem.
  131: Skips 'survey' problems.
  132: 
  133: =cut
  134: 
  135: ####################################################
  136: ####################################################
  137: sub problem_selector {
  138:     my ($AcceptedResponseTypes,$sequence_addendum,$symbmode) = @_;
  139:     my $Str;
  140:     $Str = &Apache::loncommon::start_data_table();
  141:     my $rb_count =0;
  142:     my ($navmap,@sequences) = 
  143:         &Apache::lonstatistics::selected_sequences_with_assessments('all');
  144:     return $navmap if (! ref($navmap)); # error
  145:     foreach my $seq (@sequences) {
  146:         my $seq_str = '';
  147:         foreach my $res (&get_resources($navmap,$seq)) {
  148:             foreach my $part (@{$res->parts}) {
  149:                 my @response_ids   = $res->responseIds($part);
  150:                 my @response_types = $res->responseType($part);
  151:                 for (my $i=0;$i<scalar(@response_types);$i++){
  152:                     my $respid = $response_ids[$i];
  153:                     my $resptype = $response_types[$i];
  154:                     if ($resptype =~ m/$AcceptedResponseTypes/) {
  155:                         my $value = &make_target_id({symb=>$res->symb,
  156:                                                      part=>$part,
  157:                                                      respid=>$respid,
  158:                                                      resptype=>$resptype});
  159:                         my $checked = '';
  160:                         if ($env{'form.problemchoice'} eq $value) {
  161:                             $checked = ' checked="checked"';
  162:                         }
  163:                         my $title = $res->compTitle;
  164:                         if (! defined($title) || $title eq '') {
  165:                             ($title) = ($res->src =~ m:/([^/]*)$:);
  166:                         }
  167:                         $seq_str .=  &Apache::loncommon::start_data_table_row().
  168:                             ($symbmode?
  169:                              '<td><input type="radio" id="'.$rb_count.'" name="symb" value="'.&HTML::Entities::encode($res->symb,'<>&"').'" '.$checked.' /></td>'
  170:                             :qq{<td><input type="radio" id="$rb_count" name="problemchoice" value="$value"$checked /></td>}).
  171:                             '<td><label for="'.$rb_count.'">'.$resptype.'</label></td>'.
  172:                             '<td><label for="'.$rb_count.'">'.$title.'</label>';
  173:                         if (scalar(@response_ids) > 1) {
  174:                             $seq_str .= &mt('response').' '.$respid;
  175:                         }
  176:                         my $link = $res->link.'?symb='.&escape($res->shown_symb);
  177:                         $seq_str .= ('&nbsp;'x2).
  178:                             '<a target="preview" href="'.$link.'">'.&mt('view').'</a>';
  179:                         $seq_str .= "</td>". &Apache::loncommon::end_data_table_row()."\n";
  180:                         $rb_count++;
  181:                     }
  182:                 }
  183:             }
  184:         }
  185:         if ($seq_str ne '') {
  186:             $Str .= &Apache::loncommon::start_data_table_header_row().
  187:                 '<th colspan="3">'.$seq->compTitle.'</th>'.
  188:                 &Apache::loncommon::end_data_table_header_row()."\n".$seq_str;
  189:             if (defined($sequence_addendum)) {
  190:                 $Str .= &Apache::loncommon::start_data_table_header_row().
  191:                     ('<td>&nbsp;</td>'x2).
  192:                     '<td align="right">'.$sequence_addendum.'</td>'.
  193:                     &Apache::loncommon::end_data_table_header_row()."\n";
  194:             }
  195:         }
  196:     }
  197:     $Str .= &Apache::loncommon::end_data_table()."\n";
  198:     return $Str;
  199: }
  200: 
  201: ####################################################
  202: ####################################################
  203: 
  204: =pod
  205: 
  206: =item &MultipleProblemSelector($navmap,$selected,$inputname)
  207: 
  208: Generate HTML with checkboxes for problem selection.
  209: 
  210: Input: 
  211: 
  212: $navmap: a navmap object.  If undef, navmaps will be called to create a
  213: new object.
  214: 
  215: $selected: Scalar, Array, or hash reference of currently selected items.
  216: 
  217: $inputname: The name of the form elements to use for the checkboxs.
  218: 
  219: Returns: A string containing html for a table which lists the sequences
  220: and their contents.  A checkbox is provided for each problem.
  221: 
  222: =cut
  223: 
  224: ####################################################
  225: ####################################################
  226: sub MultipleProblemSelector {
  227:     my ($navmap,$inputname,$formname,$anoncounter)=@_;
  228:     my $cid = $env{'request.course.id'};
  229:     my $Str;
  230:     # Massage the input as needed.
  231:     if (! defined($navmap)) {
  232:         $navmap = Apache::lonnavmaps::navmap->new();
  233:         if (! defined($navmap)) {
  234:             $Str .= '<div class="LC_error">'
  235:                    .&mt('Error: cannot process course structure')
  236:                    .'</div>';
  237:             return $Str;
  238:         }
  239:     }
  240:     my $selected = {map { ($_,1) } (&get_selected_symbs($inputname))};
  241:     # Header
  242:     $Str .= <<"END";
  243: <script type="text/javascript" language="JavaScript">
  244:     function checkall(value,seqid) {
  245:         for (i=0; i<document.forms.$formname.elements.length; i++) {
  246:             ele = document.forms.$formname.elements[i];
  247:             if (ele.name == '$inputname') {
  248:                 if (seqid != null) {
  249:                     itemid = document.forms.$formname.elements[i].id;
  250:                     thing = itemid.split(':');
  251:                     if (thing[0] == seqid) {
  252:                         document.forms.$formname.elements[i].checked=value;
  253:                     }
  254:                 } else {
  255:                     document.forms.$formname.elements[i].checked=value;
  256:                 }
  257:             }
  258:         }
  259:     }
  260: </script>
  261: END
  262:     my $checkanonjs = <<"END";
  263:  
  264: <script type="text/javascript" language="JavaScript">
  265:     function checkanon() {
  266:         return true;
  267:     }
  268: </script>
  269: 
  270: END
  271:     if (ref($anoncounter) eq 'HASH') {
  272:         if (keys(%{$anoncounter}) > 0) {
  273:             my $anonwarning = &mt('Your selection includes both problems with and without anonymous submissions.').'\n'.&mt('You must select either only anonymous or only named problems.').'\n\n'.&mt('If a selection contains both anonymous and named parts, [_1]use the Anoymous/Named buttons to ensure selections will be either all anonymous [_1]or all named.','\n');
  274:             $checkanonjs = <<"END";
  275: 
  276: <script type="text/javascript" language="JavaScript">
  277:     function checkanon() {
  278:         anoncount = 0;
  279:         namedcount = 0;
  280:         for (i=0; i<document.forms.$formname.elements.length; i++) {
  281:             ele = document.forms.$formname.elements[i];
  282:             if (ele.name == '$inputname') {
  283:                 itemid = document.forms.$formname.elements[i].id;
  284:                 if (document.forms.$formname.elements[i].checked) {
  285:                     anonid = 'anonymous_'+itemid;
  286:                     mixid = 'mixed_'+itemid;
  287:                     anonele = document.getElementById(anonid);
  288:                     mixele = document.getElementById(mixid);
  289:                     if (anonele.value > 0) {
  290:                         if (mixele.value == 'none') {
  291:                             anoncount ++;
  292:                         } else {
  293:                             if (mixele.value == '0') {
  294:                                 if (mixele.checked) {
  295:                                     anoncount ++; 
  296:                                 } else {
  297:                                     namedcount ++;
  298:                                 } 
  299:                             } else {
  300:                                 namedcount ++;
  301:                             }
  302:                         }
  303:                     } else {
  304:                         namedcount ++;
  305:                     }
  306:                 }
  307:             }
  308:         }
  309:         if (anoncount > 0 && namedcount > 0) {
  310:             alert("$anonwarning");
  311:             return false;
  312:         } 
  313:     }
  314: </script>
  315: 
  316: END
  317:         }
  318:     }
  319:     $Str .= $checkanonjs.
  320:         '<a href="javascript:checkall(true)">'.&mt('Select All').'</a>'.
  321:         ('&nbsp;'x4).
  322:         '<a href="javascript:checkall(false)">'.&mt('Unselect All').'</a>';
  323:     $Str .= $/.'<table>'.$/;
  324:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
  325:     my $sequence_string;
  326:     my $seq_id = 0;
  327:     my @Accumulator = (&new_accumulator($env{'course.'.$cid.'.description'},
  328:                                         '',
  329:                                         '',
  330:                                         $seq_id++,
  331:                                         $inputname));
  332:     my @Sequence_Data;
  333:     while (my $curRes = $iterator->next()) {
  334:         if ($curRes == $iterator->END_MAP) {
  335:             if (ref($Accumulator[-1]) eq 'CODE') {
  336:                 my $old_accumulator = pop(@Accumulator);
  337:                 push(@Sequence_Data,&{$old_accumulator}());
  338:             }
  339:         } elsif ($curRes == $iterator->BEGIN_MAP) {
  340:             # Not much to do here.
  341:         }
  342:         next if (! ref($curRes));
  343:         if ($curRes->is_map) {
  344:             push(@Accumulator,&new_accumulator($curRes->compTitle,
  345:                                                $curRes->src,
  346:                                                $curRes->symb,
  347:                                                $seq_id++,
  348:                                                $inputname));
  349:         } elsif ($curRes->is_problem) {
  350:             my $anonpart = 0;
  351:             my $namedpart = 0;
  352:             my @parts = @{$curRes->parts()};
  353:             if (ref($anoncounter) eq 'HASH') {
  354:                 if (keys(%{$anoncounter}) > 0) {
  355:                     my @parts = @{$curRes->parts()};
  356:                     my $symb = $curRes->symb();
  357:                     foreach my $part (@parts) {
  358:                         if ((exists($anoncounter->{$symb."\0".$part})) ||
  359:                             $curRes->is_anonsurvey($part)) {
  360:                             $anonpart ++;
  361:                         } else {
  362:                             $namedpart ++ 
  363:                         }
  364:                     }
  365:                 }
  366:             }
  367:             if (@Accumulator && $Accumulator[-1] ne '') {
  368:                 &{$Accumulator[-1]}($curRes,
  369:                                     exists($selected->{$curRes->symb}),
  370:                                     $anonpart,$namedpart);
  371:             }
  372:         }
  373:     }
  374:     my $course_seq = pop(@Sequence_Data);
  375:     foreach my $seq ($course_seq,@Sequence_Data) {
  376:         #my $seq = pop(@Sequence_Data);
  377:         next if (! defined($seq) || ref($seq) ne 'HASH');
  378:         $Str.= '<tr><td colspan="2">'.
  379:             '<b>'.$seq->{'title'}.'</b>'.('&nbsp;'x2).
  380:             '<a href="javascript:checkall(true,'.$seq->{'id'}.')">'.
  381:                                   &mt('Select').'</a>'.('&nbsp;'x2).
  382:             '<a href="javascript:checkall(false,'.$seq->{'id'}.')">'.
  383:                                   &mt('Unselect').'</a>'.('&nbsp;'x2).
  384:             '</td></tr>'.$/;
  385:         $Str.= $seq->{'html'};
  386:     }
  387:     $Str .= '</table>'.$/;
  388:     return $Str;
  389: }
  390: 
  391: sub new_accumulator {
  392:     my ($title,$src,$symb,$seq_id,$inputname) = @_;
  393:     my $target;
  394:     my $item_id=0;
  395:     return 
  396:         sub {
  397:             if (@_) { 
  398:                 my ($res,$checked,$anonpart,$namedpart) = @_;
  399:                 $target.='<tr><td><label>'.
  400:                     '<input type="checkbox" name="'.$inputname.'" ';
  401:                 if ($checked) {
  402:                     $target .= 'checked="checked" ';
  403:                 }
  404:                 my $anon_id = $item_id;
  405:                 $target .= 'id="'.$seq_id.':'.$item_id++.'" ';
  406:                 my $esc_symb = &escape($res->symb);
  407:                 $target.= 
  408:                     'value="'.$esc_symb.'" />'.
  409:                     '&nbsp;'.$res->compTitle.'</label>'.
  410:                     ('&nbsp;'x2).'<a target="preview" '.
  411:                     'href="'.$res->link.'?symb='.
  412:                     &escape($res->shown_symb).'">'.&mt('view').'</a>'.
  413:                     '<input type="hidden" id="anonymous_'.$seq_id.':'.$anon_id.'" name="hidden_'.$seq_id.':'.$anon_id.'" value="'.$anonpart.'" />';
  414:                 my $mixed = '<input type="hidden" id="mixed_'.$seq_id.':'.$anon_id.'" value="none" name="mixed_'.$seq_id.':'.$anon_id.'" />';
  415:                 if ($anonpart) {
  416:                     if ($namedpart) {
  417:                         my $checknamed = '';
  418:                         my $checkedanon = ' checked="checked"';
  419:                         if ($env{'form.mixed_'.$seq_id.':'.$anon_id} eq $esc_symb) {
  420:                             $checknamed = $checkedanon;
  421:                             $checkedanon = '';
  422:                         }
  423:                         $mixed = '&nbsp;('.
  424:     &mt('Both anonymous and named submissions -- display: [_1]Anonymous [_2]Named[_3]',
  425:     '<span class="LC_nobreak"><label>'.
  426:     '<input type="radio" name="mixed_'.$seq_id.':'.$anon_id.
  427:     '" value="0" id="mixed_'.$seq_id.':'.$anon_id.'"'.$checkedanon.' />',
  428:     '</label></span>'.('&nbsp;'x2).' <span class="LC_nobreak">'.
  429:     '<label><input type="radio" name="mixed_'.$seq_id.':'.$anon_id.
  430:     '" value="symb_'.$esc_symb.'" id="named_'.$seq_id.':'.$anon_id.'"'.$checknamed.' />',
  431:     '</label></span>').')';
  432:                     } else {
  433:                         $target .= '&nbsp;'.&mt('(Anonymous Survey)');
  434:                     }
  435:                 }
  436:                 $target.= $mixed.'</td></tr>'.$/;
  437:             } else { 
  438:                 if (defined($target)) {
  439:                     return { title => $title,
  440:                              symb  => $symb,
  441:                              src   => $src,
  442:                              id    => $seq_id,
  443:                              html  => $target, }; 
  444:                 }
  445:                 return undef;
  446:             }
  447:         };
  448: }
  449: 
  450: sub get_selected_symbs {
  451:     my ($inputfield) = @_;
  452:     my $field = 'form.'.$inputfield;
  453:     my @symbs = (map {
  454:                      &unescape($_);
  455:                      } &Apache::loncommon::get_env_multiple($field));
  456:     return @symbs;
  457: }
  458: 
  459: ####################################################
  460: ####################################################
  461: 
  462: =pod
  463: 
  464: =item &make_target_id($target)
  465: 
  466: Inputs: Hash ref with the following entries:
  467:     $target->{'symb'}, $target->{'part'}, $target->{'respid'}, 
  468:     $target->{'resptype'}.
  469: 
  470: Returns: A string, suitable for a form parameter, which uniquely identifies
  471: the problem, part, and response to do statistical analysis on.
  472: 
  473: Used by Apache::lonstathelpers::ProblemSelector().
  474: 
  475: =cut
  476: 
  477: ####################################################
  478: ####################################################
  479: sub make_target_id {
  480:     my ($target) = @_;
  481:     my $id = &escape($target->{'symb'}).':'.
  482:              &escape($target->{'part'}).':'.
  483:              &escape($target->{'respid'}).':'.
  484:              &escape($target->{'resptype'});
  485:     return $id;
  486: }
  487: 
  488: ####################################################
  489: ####################################################
  490: 
  491: =pod
  492: 
  493: =item &get_target_from_id($id)
  494: 
  495: Inputs: $id, a scalar string from Apache::lonstathelpers::make_target_id().
  496: 
  497: Returns: A hash reference, $target, containing the following keys:
  498:     $target->{'symb'}, $target->{'part'}, $target->{'respid'}, 
  499:     $target->{'resptype'}.
  500: 
  501: =cut
  502: 
  503: ####################################################
  504: ####################################################
  505: sub get_target_from_id {
  506:     my ($id) = @_;
  507:     if (! ref($id)) {
  508:         my ($symb,$part,$respid,$resptype) = split(':',$id);
  509:         return ({ symb     => &unescape($symb),
  510:                   part     => &unescape($part),
  511:                   respid   => &unescape($respid),
  512:                   resptype => &unescape($resptype)});
  513:     } elsif (ref($id) eq 'ARRAY') {
  514:         my @Return;
  515:         foreach my $selected (@$id) {
  516:             my ($symb,$part,$respid,$resptype) = split(':',$selected);
  517:             push(@Return,{ symb     => &unescape($symb),
  518:                            part     => &unescape($part),
  519:                            respid   => &unescape($respid),
  520:                            resptype => &unescape($resptype)});
  521:         }
  522:         return \@Return;
  523:     }
  524: }
  525: 
  526: ####################################################
  527: ####################################################
  528: 
  529: =pod
  530: 
  531: =item &get_prev_curr_next($target,$AcceptableResponseTypes,$granularity)
  532: 
  533: Determine the problem parts or responses preceeding and following the
  534: current resource.
  535: 
  536: Inputs: $target (see &Apache::lonstathelpers::get_target_from_id())
  537:   $AcceptableResponseTypes, regular expression matching acceptable
  538:                             response types,
  539:   $granularity, either 'part', 'response', 'part_survey', or 'part_task'
  540: 
  541: Returns: three hash references, $prev, $curr, $next, which refer to the
  542: preceeding, current, or following problem parts or responses, depending
  543: on the value of $granularity.  Values of undef indicate there is no
  544: previous or next part/response.  A value of undef for all three indicates
  545: there was no match found to the current part/resource.
  546: 
  547: The hash references contain the following keys:
  548:     symb, part, resource
  549: 
  550: If $granularity eq 'response', the following ADDITIONAL keys will be present:
  551:     respid, resptype
  552: 
  553: =cut
  554: 
  555: ####################################################
  556: ####################################################
  557: sub get_prev_curr_next {
  558:     my ($target,$AcceptableResponseTypes,$granularity) = @_;
  559:     #
  560:     # Build an array with the data we need to search through
  561:     my @Resource;
  562:     my ($navmap,@sequences) = 
  563:         &Apache::lonstatistics::selected_sequences_with_assessments('all');
  564:     return $navmap if (! ref($navmap));
  565:     foreach my $seq (@sequences) {
  566:         my @resources = &get_resources($navmap,$seq);
  567:         foreach my $res (@resources) {
  568:             foreach my $part (@{$res->parts}) {
  569:                 if (($res->is_survey($part) || ($res->is_anonsurvey($part))) && 
  570:                     ($granularity eq 'part_survey')) {
  571:                     push (@Resource,
  572:                           { symb     => $res->symb,
  573:                             part     => $part,
  574:                             resource => $res,
  575:                         } );
  576: 		} elsif ($res->is_task($part) && ($granularity eq 'part_task')){
  577:                     push (@Resource,
  578:                           { symb     => $res->symb,
  579:                             part     => $part,
  580:                             resource => $res,
  581:                         } );
  582:                 } elsif ($granularity eq 'part') {
  583:                     push (@Resource,
  584:                           { symb     => $res->symb,
  585:                             part     => $part,
  586:                             resource => $res,
  587:                         } );
  588:                 } elsif ($granularity eq 'response') {
  589:                     my @response_ids   = $res->responseIds($part);
  590:                     my @response_types = $res->responseType($part);
  591:                     for (my $i=0;
  592:                          $i<scalar(@response_ids);
  593:                          $i++){
  594:                         my $respid   = $response_ids[$i];
  595:                         my $resptype = $response_types[$i];
  596:                         next if ($resptype !~ m/$AcceptableResponseTypes/);
  597:                         push (@Resource,
  598:                               { symb     => $res->symb,
  599:                                 part     => $part,
  600:                                 respid   => $respid,
  601:                                 resptype => $resptype,
  602:                                 resource => $res,
  603:                                 } );
  604:                     }
  605:                 }
  606:             }
  607:         }
  608:     }
  609:     #
  610:     # Get the index of the current situation
  611:     my $curr_idx;
  612:     for ($curr_idx=0;$curr_idx<$#Resource;$curr_idx++) {
  613:         my $curr_item = $Resource[$curr_idx];
  614:         if ($granularity =~ /^(part|part_survey|part_task)$/) {
  615:             if ($curr_item->{'symb'} eq $target->{'symb'} &&
  616:                 $curr_item->{'part'} eq $target->{'part'}) {
  617:                 last;
  618:             }
  619:         } elsif ($granularity eq 'response') {
  620:             if ($curr_item->{'symb'} eq $target->{'symb'} &&
  621:                 $curr_item->{'part'} eq $target->{'part'} &&
  622:                 $curr_item->{'respid'} eq $target->{'respid'} &&
  623:                 $curr_item->{'resptype'} eq $target->{'resptype'}) {
  624:                 last;
  625:             }
  626:         }
  627:     }
  628:     my $curr_item = $Resource[$curr_idx];
  629:     if ($granularity =~ /^(part|part_survey|part_task)$/) {
  630:         if ($curr_item->{'symb'}     ne $target->{'symb'} ||
  631:             $curr_item->{'part'}     ne $target->{'part'}) {
  632:             # bogus symb - return nothing
  633:             return (undef,undef,undef);
  634:         }
  635:     } elsif ($granularity eq 'response') {
  636:         if ($curr_item->{'symb'}     ne $target->{'symb'} ||
  637:             $curr_item->{'part'}     ne $target->{'part'} ||
  638:             $curr_item->{'respid'}   ne $target->{'respid'} ||
  639:             $curr_item->{'resptype'} ne $target->{'resptype'}){
  640:             # bogus symb - return nothing
  641:             return (undef,undef,undef);
  642:         }
  643:     }
  644:     #
  645:     # Now just pick up the data we need
  646:     my ($prev,$curr,$next);
  647:     if ($curr_idx == 0) {
  648:         $prev = undef;
  649:         $curr = $Resource[$curr_idx  ];
  650:         $next = $Resource[$curr_idx+1];
  651:     } elsif ($curr_idx == $#Resource) {
  652:         $prev = $Resource[$curr_idx-1];
  653:         $curr = $Resource[$curr_idx  ];
  654:         $next = undef;
  655:     } else {
  656:         $prev = $Resource[$curr_idx-1];
  657:         $curr = $Resource[$curr_idx  ];
  658:         $next = $Resource[$curr_idx+1];
  659:     }
  660:     return ($navmap,$prev,$curr,$next);
  661: }
  662: 
  663: 
  664: #####################################################
  665: #####################################################
  666: 
  667: =pod
  668: 
  669: =item GetStudentAnswers($r,$problem,$Students)
  670: 
  671: Determines the correct answer for a set of students on a given problem.
  672: The students answers are stored in the student hashes pointed to by the
  673: array @$Students under the key 'answer'.
  674: 
  675: Inputs: $r
  676: $problem: hash reference containing the keys 'resource', 'part', and 'respid'.
  677: $Students: reference to array containing student hashes (need 'username', 
  678:     'domain').  
  679: 
  680: Returns: nothing 
  681: 
  682: =cut
  683: 
  684: #####################################################
  685: #####################################################
  686: sub GetStudentAnswers {
  687:     my ($r,$problem,$Students,$formname,$inputname) = @_;
  688:     my %answers;
  689:     my $status_type;
  690:     if (defined($formname)) {
  691:         $status_type = 'inline';
  692:     } else {
  693:         $status_type = 'popup';
  694:     }    
  695:     my $c = $r->connection();
  696:     my %Answers;
  697:     my ($resource,$partid,$respid) = ($problem->{'resource'},
  698:                                       $problem->{'part'},
  699:                                       $problem->{'respid'});
  700:     # Read in the cache (if it exists) before we start timing things.
  701:     &Apache::lonstathelpers::ensure_proper_cache($resource->{'symb'});
  702:     # Open progress window
  703:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
  704:         ($r,'Student Answer Compilation Status',
  705:          'Student Answer Compilation Progress', scalar(@$Students),
  706:          $status_type,undef,$formname,$inputname);
  707:     $r->rflush();
  708:     foreach my $student (@$Students) {
  709:         last if ($c->aborted());
  710:         my $sname = $student->{'username'};
  711:         my $sdom = $student->{'domain'};
  712:         my $answer = &Apache::lonstathelpers::get_student_answer
  713:             ($resource,$sname,$sdom,$partid,$respid);
  714:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
  715:                                                      'last student');
  716:         $answers{$answer}++;
  717:         $student->{'answer'} = $answer;
  718:     }
  719:     &Apache::lonstathelpers::write_analysis_cache();
  720:     return if ($c->aborted());
  721:     $r->rflush();
  722:     # close progress window
  723:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
  724:     return \%answers;
  725: }
  726: 
  727: #####################################################
  728: #####################################################
  729: 
  730: =pod
  731: 
  732: =item analyze_problem_as_student
  733: 
  734: Analyzes a homework problem for a student
  735: 
  736: Inputs: $resource: a resource object
  737:         $sname, $sdom, $partid, $respid
  738: 
  739: Returns: the problem analysis hash
  740: 
  741: =cut
  742: 
  743: #####################################################
  744: #####################################################
  745: sub analyze_problem_as_student {
  746:     my ($resource,$sname,$sdom) = @_;
  747:     if (ref($resource) ne 'HASH') {
  748:         my $res = $resource;
  749:         $resource = { 'src' => $res->src,
  750:                       'symb' => $res->symb,
  751:                       'parts' => $res->parts };
  752:         foreach my $part (@{$resource->{'parts'}}) {
  753:             $resource->{'partdata'}->{$part}->{'ResponseIds'}=
  754:                 [$res->responseIds($part)];
  755:         }
  756:     }
  757:     my $url = $resource->{'src'};
  758:     my $symb = $resource->{'symb'};
  759:     my $analysis = &get_from_analysis_cache($sname,$sdom,$symb);
  760:     if (! defined($analysis)) {
  761:         my $courseid = $env{'request.course.id'};
  762:         my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
  763:                                             'grade_domain' => $sdom,
  764:                                             'grade_username' => $sname,
  765:                                             'grade_symb' => $symb,
  766:                                             'grade_courseid' => $courseid));
  767:         (my $garbage,$analysis)=split(/_HASH_REF__/,$Answ,2);
  768:         &store_analysis($sname,$sdom,$symb,$analysis);
  769:     }
  770:     my %Answer=&Apache::lonnet::str2hash($analysis);
  771:     #
  772:     return \%Answer;
  773: }
  774: 
  775: #####################################################
  776: #####################################################
  777: 
  778: =pod
  779: 
  780: =item get_student_answer
  781: 
  782: Analyzes a homework problem for a particular student and returns the correct 
  783: answer.  Attempts to put together an answer for problem types 
  784: that do not natively support it.
  785: 
  786: Inputs: $resource: a resource object (from navmaps or hash from loncoursedata)
  787:         $sname, $sdom, $partid, $respid
  788: 
  789: Returns: $answer
  790: 
  791: If $partid and $respid are specified, $answer is simply a scalar containing
  792: the correct answer for the response.
  793: 
  794: If $partid or $respid are undefined, $answer will be a hash reference with
  795: keys $partid.'.'.$respid.'.answer'.
  796: 
  797: =cut
  798: 
  799: #####################################################
  800: #####################################################
  801: sub get_student_answer {
  802:     my ($resource,$sname,$sdom,$partid,$respid) = @_;
  803:     #
  804:     if (ref($resource) ne 'HASH') {
  805:         my $res = $resource;
  806:         $resource = { 'src' => $res->src,
  807:                       'symb' => $res->symb,
  808:                       'parts' => $res->parts };
  809:         foreach my $part (@{$resource->{'parts'}}) {
  810:             $resource->{'partdata'}->{$part}->{'ResponseIds'}=
  811:                 [$res->responseIds($part)];
  812:         }
  813:     }
  814:     #
  815:     my $analysis = 
  816:         &analyze_problem_as_student($resource,$sname,$sdom);
  817:     my $answer;
  818:     foreach my $partid (@{$resource->{'parts'}}) {
  819:         my $partdata = $resource->{'partdata'}->{$partid};
  820:         foreach my $respid (@{$partdata->{'ResponseIds'}}) {
  821:             my $prefix = $partid.'.'.$respid;
  822:             my $key = $prefix.'.answer';
  823:             $answer->{$partid}->{$respid} = 
  824:                 &get_answer($prefix,$key,%$analysis);
  825:         }
  826:     }
  827:     my $returnvalue;
  828:     if (! defined($partid)) {
  829:         $returnvalue = $answer;
  830:     } elsif (! defined($respid)) {
  831:         $returnvalue = $answer->{$partid};
  832:     } else {
  833:         $returnvalue = $answer->{$partid}->{$respid};
  834:     }
  835:     return $returnvalue;
  836: }
  837: 
  838: sub get_answer {
  839:     my ($prefix,$key,%Answer) = @_;
  840:     my $returnvalue;
  841:     if (exists($Answer{$key})) {
  842: 	if (ref($Answer{$key}) eq 'HASH') {
  843: 	    my $which = 'INTERNAL';
  844: 	    if (!exists($Answer{$key}{$which})) {
  845: 		$which = (sort(keys(%{ $Answer{$key} })))[0];
  846: 	    }
  847: 	    my $student_answer = $Answer{$key}{$which}[0][0];
  848: 	    $returnvalue = $student_answer; 
  849: 	} else {
  850: 	    &Apache::lonnet::logthis("error analyzing problem. got a answer of type ".ref($Answer{$key}));
  851: 	}
  852:     } else {
  853:         if (exists($Answer{$prefix.'.shown'})) {
  854:             # The response has foils
  855:             my %values;
  856:             while (my ($k,$v) = each(%Answer)) {
  857:                 next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
  858:                 my $foilname = $2;
  859:                 $values{$foilname}=$v;
  860:             }
  861:             foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
  862:                 if (ref($values{$foil}) eq 'ARRAY') {
  863:                     $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
  864:                         join(',',map {&HTML::Entities::encode($_,'<>&"')} @{$values{$foil}}).'&';
  865:                 } else {
  866:                     $returnvalue.=&HTML::Entities::encode($foil,'<>&"').'='.
  867:                         &HTML::Entities::encode($values{$foil},'<>&"').'&';
  868:                 }
  869:             }
  870:             $returnvalue =~ s/ /\%20/g;
  871:             chop ($returnvalue);
  872:         }
  873:     }
  874:     return $returnvalue;
  875: }
  876: 
  877: #####################################################
  878: #####################################################
  879: 
  880: =pod
  881: 
  882: =item Caching routines
  883: 
  884: =over 4
  885: 
  886: =item &load_analysis_cache($symb)
  887: 
  888: Loads the cache for the given symb into memory from disk.  
  889: Requires the cache filename be set.  
  890: Only should be called by &ensure_proper_cache.
  891: 
  892: =cut
  893: 
  894: #####################################################
  895: #####################################################
  896: {
  897:     my $cache_filename = undef;
  898:     my $current_symb = undef;
  899:     my %cache;
  900: 
  901: sub load_analysis_cache {
  902:     my ($symb) = @_;
  903:     return if (! defined($cache_filename));
  904:     if (! defined($current_symb) || $current_symb ne $symb) {
  905:         undef(%cache);
  906:         my $storedstring;
  907:         my %cache_db;
  908:         if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_READER(),0640)) {
  909:             $storedstring = $cache_db{&escape($symb)};
  910:             untie(%cache_db);
  911:         }
  912:         if (defined($storedstring)) {
  913:             %cache = %{thaw($storedstring)};
  914:         }
  915:     }
  916:     return;
  917: }
  918: 
  919: #####################################################
  920: #####################################################
  921: 
  922: =pod
  923: 
  924: =item &get_from_analysis_cache($sname,$sdom,$symb,$partid,$respid)
  925: 
  926: Returns the appropriate data from the cache, or undef if no data exists.
  927: 
  928: =cut
  929: 
  930: #####################################################
  931: #####################################################
  932: sub get_from_analysis_cache {
  933:     my ($sname,$sdom,$symb) = @_;
  934:     &ensure_proper_cache($symb);
  935:     my $returnvalue;
  936:     if (exists($cache{$sname.':'.$sdom})) {
  937:         $returnvalue = $cache{$sname.':'.$sdom};
  938:     } else {
  939:         $returnvalue = undef;
  940:     }
  941:     return $returnvalue;
  942: }
  943: 
  944: #####################################################
  945: #####################################################
  946: 
  947: =pod
  948: 
  949: =item &write_analysis_cache($symb)
  950: 
  951: Writes the in memory cache to disk so that it can be read in with
  952: &load_analysis_cache($symb).
  953: 
  954: =cut
  955: 
  956: #####################################################
  957: #####################################################
  958: sub write_analysis_cache {
  959:     return if (! defined($current_symb) || ! defined($cache_filename));
  960:     my %cache_db;
  961:     my $key = &escape($current_symb);
  962:     if (tie(%cache_db,'GDBM_File',$cache_filename,&GDBM_WRCREAT(),0640)) {
  963:         my $storestring = freeze(\%cache);
  964:         $cache_db{$key}=$storestring;
  965:         $cache_db{$key.'.time'}=time;
  966:         untie(%cache_db);
  967:     }
  968:     undef(%cache);
  969:     undef($current_symb);
  970:     undef($cache_filename);
  971:     return;
  972: }
  973: 
  974: #####################################################
  975: #####################################################
  976: 
  977: =pod
  978: 
  979: =item &ensure_proper_cache($symb)
  980: 
  981: Called to make sure we have the proper cache set up.  This is called
  982: prior to every analysis lookup.
  983: 
  984: =cut
  985: 
  986: #####################################################
  987: #####################################################
  988: sub ensure_proper_cache {
  989:     my ($symb) = @_;
  990:     my $cid = $env{'request.course.id'};
  991:     my $new_filename =  '/home/httpd/perl/tmp/'.
  992:         'problemanalysis_'.$cid.'_analysis_cache.db';
  993:     if (! defined($cache_filename) ||
  994:         $cache_filename ne $new_filename ||
  995:         ! defined($current_symb)   ||
  996:         $current_symb ne $symb) {
  997:         $cache_filename = $new_filename;
  998:         # Notice: $current_symb is not set to $symb until after the cache is
  999:         # loaded.  This is what tells &load_analysis_cache to load in a new
 1000:         # symb cache.
 1001:         &load_analysis_cache($symb);
 1002:         $current_symb = $symb;
 1003:     }
 1004: }
 1005: 
 1006: #####################################################
 1007: #####################################################
 1008: 
 1009: =pod
 1010: 
 1011: =item &store_analysis($sname,$sdom,$symb,$partid,$respid,$dataset)
 1012: 
 1013: Stores the analysis data in the in memory cache.
 1014: 
 1015: =cut
 1016: 
 1017: #####################################################
 1018: #####################################################
 1019: sub store_analysis {
 1020:     my ($sname,$sdom,$symb,$dataset) = @_;
 1021:     return if ($symb ne $current_symb);
 1022:     $cache{$sname.':'.$sdom}=$dataset;
 1023:     return;
 1024: }
 1025: 
 1026: }
 1027: #####################################################
 1028: #####################################################
 1029: 
 1030: =pod
 1031: 
 1032: =back
 1033: 
 1034: =cut
 1035: 
 1036: #####################################################
 1037: #####################################################
 1038: 
 1039: ##
 1040: ## The following is copied from datecalc1.pl, part of the 
 1041: ## Spreadsheet::WriteExcel CPAN module.
 1042: ##
 1043: ##
 1044: ######################################################################
 1045: #
 1046: # Demonstration of writing date/time cells to Excel spreadsheets,
 1047: # using UNIX/Perl time as source of date/time.
 1048: #
 1049: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
 1050: #
 1051: ######################################################################
 1052: #
 1053: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
 1054: # measured in seconds.
 1055: #
 1056: # An Excel file can use exactly one of two different date/time systems.
 1057: # In these systems, a floating point number represents the number of days
 1058: # (and fractional parts of the day) since a start point. The floating point
 1059: # number is referred to as a 'serial'.
 1060: # The two systems ('1900' and '1904') use different starting points:
 1061: #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
 1062: #          a leap year - see:
 1063: #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
 1064: #          for the excuse^H^H^H^H^H^Hreason.
 1065: #  '1904'; '1.00' is 2 Jan 1904.
 1066: #
 1067: # The '1904' system is the default for Apple Macs. Windows versions of
 1068: # Excel have the option to use the '1904' system.
 1069: #
 1070: # Note that Visual Basic's "DateSerial" function does NOT erroneously
 1071: # regard 1900 as a leap year, and thus its serials do not agree with
 1072: # the 1900 serials of Excel for dates before 1 Mar 1900.
 1073: #
 1074: # Note that StarOffice (at least at version 5.2) does NOT erroneously
 1075: # regard 1900 as a leap year, and thus its serials do not agree with
 1076: # the 1900 serials of Excel for dates before 1 Mar 1900.
 1077: #
 1078: ######################################################################
 1079: #
 1080: # Calculation description
 1081: # =======================
 1082: #
 1083: # 1900 system
 1084: # -----------
 1085: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
 1086: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
 1087: # were leap years with an extra day.
 1088: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
 1089: # 1 Jan 1970.
 1090: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
 1091: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
 1092: #
 1093: # 1904 system
 1094: # -----------
 1095: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
 1096: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
 1097: # were leap years with an extra day.
 1098: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
 1099: # 1 Jan 1970.
 1100: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
 1101: #
 1102: ######################################################################
 1103: #
 1104: # Copyright (c) 2000, Andrew Benham.
 1105: # This program is free software. It may be used, redistributed and/or
 1106: # modified under the same terms as Perl itself.
 1107: #
 1108: # Andrew Benham, adsb@bigfoot.com
 1109: # London, United Kingdom
 1110: # 11 Nov 2000
 1111: #
 1112: ######################################################################
 1113: #-----------------------------------------------------------
 1114: # calc_serial()
 1115: #
 1116: # Called with (up to) 2 parameters.
 1117: #   1.  Unix timestamp.  If omitted, uses current time.
 1118: #   2.  GMT flag. Set to '1' to return serial in GMT.
 1119: #       If omitted, returns serial in appropriate timezone.
 1120: #
 1121: # Returns date/time serial according to $DATE_SYSTEM selected
 1122: #-----------------------------------------------------------
 1123: sub calc_serial {
 1124:     # Use 1900 date system on all platforms other than Apple Mac (for which
 1125:     # use 1904 date system).
 1126:     my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
 1127:     my $time = (defined $_[0]) ? $_[0] : time();
 1128:     my $gmtflag = (defined $_[1]) ? $_[1] : 0;
 1129:     #
 1130:     # Divide timestamp by number of seconds in a day.
 1131:     # This gives a date serial with '0' on 1 Jan 1970.
 1132:     my $serial = $time / 86400;
 1133:     #
 1134:     # Adjust the date serial by the offset appropriate to the
 1135:     # currently selected system (1900/1904).
 1136:     if ($DATE_SYSTEM == 0) {        # use 1900 system
 1137:         $serial += 25569;
 1138:     } else {                        # use 1904 system
 1139:         $serial += 24107;
 1140:     }
 1141:     #
 1142:     unless ($gmtflag) {
 1143:         # Now have a 'raw' serial with the right offset. But this
 1144:         # gives a serial in GMT, which is false unless the timezone
 1145:         # is GMT. We need to adjust the serial by the appropriate
 1146:         # timezone offset.
 1147:         # Calculate the appropriate timezone offset by seeing what
 1148:         # the differences between localtime and gmtime for the given
 1149:         # time are.
 1150:         #    
 1151:         my @gmtime = gmtime($time);
 1152:         my @ltime  = localtime($time);
 1153:         #
 1154:         # For the first 7 elements of the two arrays, adjust the
 1155:         # date serial where the elements differ.
 1156:         for (0 .. 6) {
 1157:             my $diff = $ltime[$_] - $gmtime[$_];
 1158:             if ($diff) {
 1159:                 $serial += _adjustment($diff,$_);
 1160:             }
 1161:         }
 1162:     }
 1163:     #
 1164:     # Perpetuate the error that 1900 was a leap year by decrementing
 1165:     # the serial if we're using the 1900 system and the date is prior to
 1166:     # 1 Mar 1900. This has the effect of making serial value '60'
 1167:     # 29 Feb 1900.
 1168:     #
 1169:     # This fix only has any effect if UNIX/Perl time on the platform
 1170:     # can represent 1900. Many can't.
 1171:     #
 1172:     unless ($DATE_SYSTEM) {
 1173:         $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
 1174:     }
 1175:     return $serial;
 1176: }
 1177: 
 1178: sub _adjustment {
 1179:     # Based on the difference in the localtime/gmtime array elements
 1180:     # number, return the adjustment required to the serial.
 1181:     #
 1182:     # We only look at some elements of the localtime/gmtime arrays:
 1183:     #    seconds    unlikely to be different as all known timezones
 1184:     #               have an offset of integral multiples of 15 minutes,
 1185:     #               but it's easy to do.
 1186:     #    minutes    will be different for timezone offsets which are
 1187:     #               not an exact number of hours.
 1188:     #    hours      very likely to be different.
 1189:     #    weekday    will differ when localtime/gmtime difference
 1190:     #               straddles midnight.
 1191:     #
 1192:     # Assume that difference between localtime and gmtime is less than
 1193:     # 5 days, then don't have to do maths for day of month, month number,
 1194:     # year number, etc...
 1195:     #
 1196:     my ($delta,$element) = @_;
 1197:     my $adjust = 0;
 1198:     #
 1199:     if ($element == 0) {            # Seconds
 1200:         $adjust = $delta/86400;         # 60 * 60 * 24
 1201:     } elsif ($element == 1) {       # Minutes
 1202:         $adjust = $delta/1440;          # 60 * 24
 1203:     } elsif ($element == 2) {       # Hours
 1204:         $adjust = $delta/24;            # 24
 1205:     } elsif ($element == 6) {       # Day of week number
 1206:         # Catch difference straddling Sat/Sun in either direction
 1207:         $delta += 7 if ($delta < -4);
 1208:         $delta -= 7 if ($delta > 4);
 1209:         #    
 1210:         $adjust = $delta;
 1211:     }
 1212:     return $adjust;
 1213: }
 1214: 
 1215: ###########################################################
 1216: ###########################################################
 1217: 
 1218: =pod
 1219: 
 1220: =item get_problem_data
 1221: 
 1222: Returns a data structure describing the problem.
 1223: 
 1224: Inputs: $url
 1225: 
 1226: Returns: %Partdata
 1227: 
 1228: =cut
 1229: 
 1230: ## note: we must force each foil and option to not begin or end with
 1231: ##       spaces as they are stored without such data.
 1232: ##
 1233: ###########################################################
 1234: ###########################################################
 1235: sub get_problem_data {
 1236:     my ($url) = @_;
 1237:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
 1238:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
 1239:     my %Answer;
 1240:     %Answer=&Apache::lonnet::str2hash($Answ);
 1241:     my %Partdata;
 1242:     foreach my $part (@{$Answer{'parts'}}) {
 1243:         while (my($key,$value) = each(%Answer)) {
 1244:             #
 1245:             # Logging code:
 1246:             if (0) {
 1247:                 &Apache::lonnet::logthis($part.' got key "'.$key.'"');
 1248:                 if (ref($value) eq 'ARRAY') {
 1249:                     &Apache::lonnet::logthis('    @'.join(',',@$value));
 1250:                 } else {
 1251:                     &Apache::lonnet::logthis('    '.$value);
 1252:                 }
 1253:             }
 1254:             # End of logging code
 1255:             next if ($key !~ /^\Q$part\E/);
 1256:             $key =~ s/^\Q$part\E\.//;
 1257:             if (ref($value) eq 'ARRAY') {
 1258:                 if ($key eq 'options') {
 1259:                     $Partdata{$part}->{'_Options'}=$value;
 1260:                 } elsif ($key eq 'concepts') {
 1261:                     $Partdata{$part}->{'_Concepts'}=$value;
 1262:                 } elsif ($key eq 'items') {
 1263:                     $Partdata{$part}->{'_Items'}=$value;
 1264:                 } elsif ($key =~ /^concept\.(.*)$/) {
 1265:                     my $concept = $1;
 1266:                     foreach my $foil (@$value) {
 1267:                         $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
 1268:                                                                       $concept;
 1269:                     }
 1270:                 } elsif ($key =~ /^(unit|incorrect|answer|ans_low|ans_high|str_type)$/) {
 1271:                     $Partdata{$part}->{$key}=$value;
 1272:                 }
 1273:             } else {
 1274:                 if ($key=~ /^foil\.text\.(.*)$/) {
 1275:                     my $foil = $1;
 1276:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
 1277:                     $value =~ s/(\s*$|^\s*)//g;
 1278:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
 1279:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
 1280:                     my $foil = $1;
 1281:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
 1282:                 } elsif ($key eq 'answercomputed') {
 1283:                     $Partdata{$part}->{'answercomputed'} = $value;
 1284:                 }
 1285:             }
 1286:         }
 1287:     }
 1288:     # Further debugging code
 1289:     if (0) {
 1290:         &Apache::lonnet::logthis('lonstathelpers::get_problem_data');
 1291:         &log_hash_ref(\%Partdata);
 1292:     }
 1293:     return %Partdata;
 1294: }
 1295: 
 1296: sub log_array_ref {
 1297:     my ($arrayref,$prefix) = @_;
 1298:     return if (ref($arrayref) ne 'ARRAY');
 1299:     if (! defined($prefix)) { $prefix = ''; };
 1300:     foreach my $v (@$arrayref) {
 1301:         if (ref($v) eq 'ARRAY') {
 1302:             &log_array_ref($v,$prefix.'  ');
 1303:         } elsif (ref($v) eq 'HASH') {
 1304:             &log_hash_ref($v,$prefix.'  ');
 1305:         } else {
 1306:             &Apache::lonnet::logthis($prefix.'"'.$v.'"');
 1307:         }
 1308:     }
 1309: }
 1310: 
 1311: sub log_hash_ref {
 1312:     my ($hashref,$prefix) = @_;
 1313:     return if (ref($hashref) ne 'HASH');
 1314:     if (! defined($prefix)) { $prefix = ''; };
 1315:     while (my ($k,$v) = each(%$hashref)) {
 1316:         if (ref($v) eq 'ARRAY') {
 1317:             &Apache::lonnet::logthis($prefix.'"'.$k.'" = array');
 1318:             &log_array_ref($v,$prefix.'  ');
 1319:         } elsif (ref($v) eq 'HASH') {
 1320:             &Apache::lonnet::logthis($prefix.'"'.$k.'" = hash');
 1321:             &log_hash_ref($v,$prefix.'  ');
 1322:         } else {
 1323:             &Apache::lonnet::logthis($prefix.'"'.$k.'" => "'.$v.'"');
 1324:         }
 1325:     }
 1326: }
 1327: ####################################################
 1328: ####################################################
 1329: 
 1330: =pod
 1331: 
 1332: =item &limit_by_time()
 1333: 
 1334: =cut
 1335: 
 1336: ####################################################
 1337: ####################################################
 1338: sub limit_by_time_form {
 1339:     my $Starttime_form = '';
 1340:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
 1341:         ('limitby_startdate');
 1342:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
 1343:         ('limitby_enddate');
 1344:     if (! defined($endtime)) {
 1345:         $endtime = time;
 1346:     }
 1347:     if (! defined($starttime)) {
 1348:         $starttime = $endtime - 60*60*24*7;
 1349:     }
 1350:     my $state;
 1351:     if (&limit_by_time()) {
 1352:         $state = '';
 1353:     } else {
 1354:         $state = 'disabled';
 1355:     }
 1356:     my $startdateform = &Apache::lonhtmlcommon::date_setter
 1357:         ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
 1358:     my $enddateform = &Apache::lonhtmlcommon::date_setter
 1359:         ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
 1360:     my $Str;
 1361:     $Str .= '<script type="text/javascript" language="JavaScript">';
 1362:     $Str .= 'function toggle_limitby_activity(state) {';
 1363:     $Str .= '    if (state) {';
 1364:     $Str .= '        limitby_startdate_enable();';
 1365:     $Str .= '        limitby_enddate_enable();';
 1366:     $Str .= '    } else {';
 1367:     $Str .= '        limitby_startdate_disable();';
 1368:     $Str .= '        limitby_enddate_disable();';
 1369:     $Str .= '    }';    
 1370:     $Str .= '}';
 1371:     $Str .= '</script>';
 1372:     $Str .= '<fieldset>';
 1373:     my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
 1374:     if (&limit_by_time()) {
 1375:         $timecheckbox .= ' checked ';
 1376:     } 
 1377:     $timecheckbox .= 'OnChange="javascript:toggle_limitby_activity(this.checked);" ';
 1378:     $timecheckbox .= ' />';
 1379:     $Str .= '<legend><label>'.&mt('[_1] Limit by time',$timecheckbox).'</label></legend>';
 1380:     $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
 1381:     $Str .= &mt('&nbsp;End Time: [_1]',$enddateform).'<br />';
 1382:     $Str .= '</fieldset>';
 1383:     return $Str;
 1384: }
 1385: 
 1386: sub limit_by_time {
 1387:     if (exists($env{'form.limit_by_time'}) &&
 1388:         $env{'form.limit_by_time'} ne '' ) {
 1389:         return 1;
 1390:     } else {
 1391:         return 0;
 1392:     }
 1393: }
 1394: 
 1395: sub get_time_limits {
 1396:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
 1397:         ('limitby_startdate');
 1398:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
 1399:         ('limitby_enddate');
 1400:     return ($starttime,$endtime);
 1401: }
 1402: 
 1403: ####################################################
 1404: ####################################################
 1405: 
 1406: =pod
 1407: 
 1408: =item &manage_caches
 1409: 
 1410: Inputs: $r, apache request object
 1411: 
 1412: Returns: An array of scalars containing html for buttons.
 1413: 
 1414: =cut
 1415: 
 1416: ####################################################
 1417: ####################################################
 1418: sub manage_caches {
 1419:     my ($r,$formname,$inputname,$update_message) = @_;
 1420:     &Apache::loncoursedata::clear_internal_caches();
 1421:     my $sectionkey = 
 1422:         join(',',
 1423:              map {
 1424:                      &escape($_);
 1425:                  } sort(&Apache::lonstatistics::get_selected_sections())
 1426:              );
 1427:     my $statuskey = $Apache::lonstatistics::enrollment_status;
 1428:     if (exists($env{'form.ClearCache'}) || 
 1429:         exists($env{'form.updatecaches'}) || 
 1430:         (exists($env{'form.firstrun'}) && $env{'form.firstrun'} ne 'no') ||
 1431:         (exists($env{'form.prevsection'}) &&
 1432:             $env{'form.prevsection'} ne $sectionkey) ||
 1433:         (exists($env{'form.prevenrollstatus'}) &&
 1434:             $env{'form.prevenrollstatus'} ne $statuskey)
 1435:         ) {
 1436:         if (defined($update_message)) {
 1437:             $r->print($update_message);
 1438:         }
 1439:         if (0) {
 1440:             &Apache::lonnet::logthis('Updating mysql student data caches');
 1441:         }
 1442:         &gather_full_student_data($r,$formname,$inputname);
 1443:     }
 1444:     #
 1445:     my @Buttons = 
 1446:         ('<input type="submit" name="ClearCache" '.
 1447:              'value="'.&mt('Clear Caches').'" />',
 1448:          '<input type="submit" name="updatecaches" '.
 1449:              'value="'.&mt('Update Caches').'" />'.
 1450:          &Apache::loncommon::help_open_topic('Statistics_Cache'),
 1451:          '<input type="hidden" name="prevsection" value="'.$sectionkey.'" />',
 1452:          '<input type="hidden" name="prevenrollstatus" value="'.$statuskey.'" />'
 1453:          );
 1454:     #
 1455:     if (! exists($env{'form.firstrun'})) {
 1456:         $r->print('<input type="hidden" name="firstrun" value="yes" />');
 1457:     } else {
 1458:         $r->print('<input type="hidden" name="firstrun" value="no" />');
 1459:     }
 1460:     #
 1461:     return @Buttons;
 1462: }
 1463: 
 1464: sub gather_full_student_data {
 1465:     my ($r,$formname,$inputname) = @_;
 1466:     my $status_type;
 1467:     if (defined($formname)) {
 1468:         $status_type = 'inline';
 1469:     } else {
 1470:         $status_type = 'popup';
 1471:     }
 1472:     my $c = $r->connection();
 1473:     #
 1474:     &Apache::loncoursedata::clear_internal_caches();
 1475:     #
 1476:     my @Students = @Apache::lonstatistics::Students;
 1477:     #
 1478:     # Open the progress window
 1479:     my %prog_state=&Apache::lonhtmlcommon::Create_PrgWin
 1480:         ($r,&mt('Student Data Compilation Status'),
 1481:          &mt('Student Data Compilation Progress'), scalar(@Students),
 1482:          $status_type,undef,$formname,$inputname);
 1483:     #
 1484:     while (my $student = shift @Students) {
 1485:         return if ($c->aborted());
 1486:         my $status = &Apache::loncoursedata::ensure_current_full_data
 1487:             ($student->{'username'},$student->{'domain'},
 1488:              $env{'request.course.id'});
 1489:         &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state,
 1490:                                                  &mt('last student'));
 1491:     }
 1492:     &Apache::lonhtmlcommon::Close_PrgWin($r,\%prog_state);
 1493:     $r->rflush();
 1494:     return;
 1495: }
 1496: 
 1497: ####################################################
 1498: ####################################################
 1499: 
 1500: =pod
 1501: 
 1502: =item &submission_report_form
 1503: 
 1504: Input: The originating reportSelected value for the current stats page.
 1505: 
 1506: Output: Scalar containing HTML with needed form elements and a link to 
 1507: the student submission reports page.
 1508: 
 1509: =cut
 1510: 
 1511: ####################################################
 1512: ####################################################
 1513: sub submission_report_form {
 1514:     my ($original_report) = @_;
 1515:     # Note: In the link below we change the reportSelected value.  If
 1516:     # the user hits the 'back' button on the browser after getting their
 1517:     # student submissions report, this value may still be around.  So we
 1518:     # output a script block to set it properly.  If the $original_report
 1519:     # value is unset, you are just asking for trouble.
 1520:     if (! defined($original_report)) {
 1521:         &Apache::lonnet::logthis
 1522:             ('someone called lonstathelpers::submission_report_form without '.
 1523:              ' enough input.');
 1524:     }
 1525:     my $html = $/.
 1526:         '<script type="text/javascript" language="JavaScript">'.
 1527:         "document.Statistics.reportSelected.value='$original_report';".
 1528:         '</script>'.
 1529:         '<input type="hidden" name="correctans" value="true" />'.
 1530:         '<input type="hidden" name="prob_status" value="true" />'.
 1531:         '<input type="hidden" name="all_sub" value="true" />';
 1532:     my $output_selector = $/.'<select name="output">'.$/;
 1533:     foreach ('HTML','Excel','CSV') {
 1534:         $output_selector .= '    <option value="'.lc($_).'"';
 1535:         if ($env{'form.output'} eq lc($_)) {
 1536:             $output_selector .= ' selected ';
 1537:         }
 1538:         $output_selector .='>'.&mt($_).'</option>'.$/;
 1539:     } 
 1540:     $output_selector .= '</select>'.$/;
 1541:     my $link = '<a href="javascript:'.
 1542:        q{document.Statistics.reportSelected.value='student_submission_reports';}.
 1543:        'document.Statistics.submit();">';
 1544:     $html.= &mt('View data as [_1] [_2]go[_3]',$output_selector,
 1545:                 $link,'</a>').$/;
 1546:     return $html
 1547: }
 1548: 
 1549: ####################################################
 1550: ####################################################
 1551: 
 1552: =pod
 1553: 
 1554: =back
 1555: 
 1556: =cut
 1557: 
 1558: ####################################################
 1559: ####################################################
 1560: 
 1561: 1;
 1562: 
 1563: __END__

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