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

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

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