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

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

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