File:  [LON-CAPA] / loncom / interface / statistics / lonstathelpers.pm
Revision 1.76.2.5.2.1: download - view: text, annotated - select for diffs
Fri Sep 8 00:52:36 2023 UTC (8 months, 4 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.76.2.5: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.75, 1.81

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

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