File:  [LON-CAPA] / loncom / interface / statistics / lonstathelpers.pm
Revision 1.7: download - view: text, annotated - select for diffs
Fri Mar 12 21:05:08 2004 UTC (20 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Removed debug spew.

    1: # The LearningOnline Network with CAPA
    2: #
    3: # $Id: lonstathelpers.pm,v 1.7 2004/03/12 21:05:08 matthew Exp $
    4: #
    5: # Copyright Michigan State University Board of Trustees
    6: #
    7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    8: #
    9: # LON-CAPA is free software; you can redistribute it and/or modify
   10: # it under the terms of the GNU General Public License as published by
   11: # the Free Software Foundation; either version 2 of the License, or
   12: # (at your option) any later version.
   13: #
   14: # LON-CAPA is distributed in the hope that it will be useful,
   15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17: # GNU General Public License for more details.
   18: #
   19: # You should have received a copy of the GNU General Public License
   20: # along with LON-CAPA; if not, write to the Free Software
   21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   22: #
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: ####################################################
   28: ####################################################
   29: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonstathelpers - helper routines used by statistics
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: This module provides a place to consolidate much of the statistics 
   39: routines that are needed across multiple statistics functions.
   40: 
   41: =head1 OVERVIEW
   42: 
   43: 
   44: =over 4
   45: 
   46: =cut
   47: 
   48: ####################################################
   49: ####################################################
   50: package Apache::lonstathelpers;
   51: 
   52: use strict;
   53: use Apache::lonnet();
   54: use Apache::loncommon();
   55: use Apache::lonhtmlcommon();
   56: use Apache::loncoursedata();
   57: use Apache::lonstatistics;
   58: use Apache::lonlocal;
   59: use HTML::Entities();
   60: use Time::Local();
   61: use Spreadsheet::WriteExcel();
   62: 
   63: ####################################################
   64: ####################################################
   65: 
   66: =pod
   67: 
   68: =item &render_resource($resource)
   69: 
   70: Input: a resource generated from 
   71: &Apache::loncoursedata::get_sequence_assessment_data().
   72: 
   73: Retunrs: a scalar containing html for a rendering of the problem
   74: within a table.
   75: 
   76: =cut
   77: 
   78: ####################################################
   79: ####################################################
   80: sub render_resource {
   81:     my ($resource) = @_;
   82:     ##
   83:     ## Render the problem
   84:     my $base;
   85:     ($base,undef) = ($resource->{'src'} =~ m|(.*/)[^/]*$|);
   86:     $base = "http://".$ENV{'SERVER_NAME'}.$base;
   87:     my $rendered_problem = 
   88:         &Apache::lonnet::ssi_body($resource->{'src'});
   89:     $rendered_problem =~ s/<\s*form\s*/<nop /g;
   90:     $rendered_problem =~ s|(<\s*/form\s*>)|<\/nop>|g;
   91:     return '<table bgcolor="ffffff"><tr><td>'.
   92:         '<base href="'.$base.'" />'.
   93:         $rendered_problem.
   94:         '</td></tr></table>';
   95: }
   96: 
   97: ####################################################
   98: ####################################################
   99: 
  100: =pod
  101: 
  102: =item &ProblemSelector($AcceptedResponseTypes)
  103: 
  104: Input: scalar containing regular expression which matches response
  105: types to show.  '.' will yield all, '(option|radiobutton)' will match
  106: all option response and radiobutton problems.
  107: 
  108: Returns: A string containing html for a table which lists the sequences
  109: and their contents.  A radiobutton is provided for each problem.
  110: 
  111: =cut
  112: 
  113: ####################################################
  114: ####################################################
  115: sub ProblemSelector {
  116:     my ($AcceptedResponseTypes) = @_;
  117:     my $Str;
  118:     $Str = "\n<table>\n";
  119:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  120:         next if ($seq->{'num_assess'}<1);
  121:         my $seq_str = '';
  122:         foreach my $res (@{$seq->{'contents'}}) {
  123:             next if ($res->{'type'} ne 'assessment');
  124:             foreach my $part (@{$res->{'parts'}}) {
  125:                 my $partdata = $res->{'partdata'}->{$part};
  126:                 for (my $i=0;$i<scalar(@{$partdata->{'ResponseTypes'}});$i++){
  127:                     my $respid = $partdata->{'ResponseIds'}->[$i];
  128:                     my $resptype = $partdata->{'ResponseTypes'}->[$i];
  129:                     if ($resptype =~ m/$AcceptedResponseTypes/) {
  130:                         my $value = &make_target_id({symb=>$res->{'symb'},
  131:                                                      part=>$part,
  132:                                                      respid=>$respid,
  133:                                                      resptype=>$resptype});
  134:                         my $checked = '';
  135:                         if ($ENV{'form.problemchoice'} eq $value) {
  136:                             $checked = 'checked ';
  137:                         }
  138:                         my $title = $res->{'title'};
  139:                         if (! defined($title) || $title eq '') {
  140:                             ($title) = ($res->{'src'} =~ m:/([^/]*)$:);
  141:                         }
  142:                         $seq_str .= '<tr><td>'.
  143:   '<input type="radio" name="problemchoice" value="'.$value.'" '.$checked.'/>'.
  144:   '</td><td>'.          
  145:   $resptype.'</td><td>'.
  146:   '<a href="'.$res->{'src'}.'">'.$title.'</a> ';
  147: #  '<a href="'.$res->{'src'}.'">'.$resptype.' '.$res->{'title'}.'</a> ';
  148:                         if (scalar(@{$partdata->{'ResponseIds'}}) > 1) {
  149:                             $seq_str .= &mt('response').' '.$respid;
  150:                         }
  151:                         $seq_str .= "</td></tr>\n";
  152:                     }
  153:                 }
  154:             }
  155:         }
  156:         if ($seq_str ne '') {
  157:             $Str .= '<tr><td>&nbsp</td><td colspan="2"><b>'.$seq->{'title'}.'</b></td>'.
  158:                 "</tr>\n".$seq_str;
  159:         }
  160:     }
  161:     $Str .= "</table>\n";
  162:     return $Str;
  163: }
  164: 
  165: ####################################################
  166: ####################################################
  167: 
  168: =pod
  169: 
  170: =item &make_target_id($target)
  171: 
  172: Inputs: Hash ref with the following entries:
  173:     $target->{'symb'}, $target->{'part'}, $target->{'respid'}, 
  174:     $target->{'resptype'}.
  175: 
  176: Returns: A string, suitable for a form parameter, which uniquely identifies
  177: the problem, part, and response to do statistical analysis on.
  178: 
  179: Used by Apache::lonstathelpers::ProblemSelector().
  180: 
  181: =cut
  182: 
  183: ####################################################
  184: ####################################################
  185: sub make_target_id {
  186:     my ($target) = @_;
  187:     my $id = &Apache::lonnet::escape($target->{'symb'}).':'.
  188:              &Apache::lonnet::escape($target->{'part'}).':'.
  189:              &Apache::lonnet::escape($target->{'respid'}).':'.
  190:              &Apache::lonnet::escape($target->{'resptype'});
  191:     return $id;
  192: }
  193: 
  194: ####################################################
  195: ####################################################
  196: 
  197: =pod
  198: 
  199: =item &get_target_from_id($id)
  200: 
  201: Inputs: $id, a scalar string from Apache::lonstathelpers::make_target_id().
  202: 
  203: Returns: A hash reference, $target, containing the following keys:
  204:     $target->{'symb'}, $target->{'part'}, $target->{'respid'}, 
  205:     $target->{'resptype'}.
  206: 
  207: =cut
  208: 
  209: ####################################################
  210: ####################################################
  211: sub get_target_from_id {
  212:     my ($id) = @_;
  213:     my ($symb,$part,$respid,$resptype) = split(':',$id);
  214:     return ({ symb    =>&Apache::lonnet::unescape($symb),
  215:              part     =>&Apache::lonnet::unescape($part),
  216:              respid   =>&Apache::lonnet::unescape($respid),
  217:              resptype =>&Apache::lonnet::unescape($resptype)});
  218: }
  219: 
  220: ####################################################
  221: ####################################################
  222: 
  223: =pod
  224: 
  225: =item &get_prev_curr_next($target)
  226: 
  227: Determine the problem parts or responses preceeding and following the
  228: current resource.
  229: 
  230: Inputs: $target (see &Apache::lonstathelpers::get_target_from_id())
  231:   $AcceptableResponseTypes, regular expression matching acceptable
  232:                             response types,
  233:   $granularity, either 'part' or 'response'
  234: 
  235: Returns: three hash references, $prev, $curr, $next, which refer to the
  236: preceeding, current, or following problem parts or responses, depending
  237: on the value of $granularity.  Values of undef indicate there is no
  238: previous or next part/response.  A value of undef for all three indicates
  239: there was no match found to the current part/resource.
  240: 
  241: The hash references contain the following keys:
  242:     symb, part, resource
  243: 
  244: If $granularity eq 'response', the following ADDITIONAL keys will be present:
  245:     respid, resptype
  246: 
  247: =cut
  248: 
  249: ####################################################
  250: ####################################################
  251: sub get_prev_curr_next {
  252:     my ($target,$AcceptableResponseTypes,$granularity) = @_;
  253:     #
  254:     # Build an array with the data we need to search through
  255:     my @Resource;
  256:     foreach my $seq (&Apache::lonstatistics::Sequences_with_Assess()) {
  257:         foreach my $res (@{$seq->{'contents'}}) {
  258:             next if ($res->{'type'} ne 'assessment');
  259:             foreach my $part (@{$res->{'parts'}}) {
  260:                 my $partdata = $res->{'partdata'}->{$part};
  261:                 if ($granularity eq 'part') {
  262:                     push (@Resource,
  263:                           { symb     => $res->{symb},
  264:                             part     => $part,
  265:                             resource => $res,
  266:                         } );
  267:                 } elsif ($granularity eq 'response') {
  268:                     for (my $i=0;
  269:                          $i<scalar(@{$partdata->{'ResponseTypes'}});
  270:                          $i++){
  271:                         my $respid = $partdata->{'ResponseIds'}->[$i];
  272:                         my $resptype = $partdata->{'ResponseTypes'}->[$i];
  273:                         next if ($resptype !~ m/$AcceptableResponseTypes/);
  274:                         push (@Resource,
  275:                               { symb     => $res->{symb},
  276:                                 part     => $part,
  277:                                 respid   => $partdata->{'ResponseIds'}->[$i],
  278:                                 resource => $res,
  279:                                 resptype => $resptype
  280:                                 } );
  281:                     }
  282:                 }
  283:             }
  284:         }
  285:     }
  286:     #
  287:     # Get the index of the current situation
  288:     my $curr_idx;
  289:     for ($curr_idx=0;$curr_idx<$#Resource;$curr_idx++) {
  290:         my $curr_item = $Resource[$curr_idx];
  291:         if ($granularity eq 'part') {
  292:             if ($curr_item->{'symb'} eq $target->{'symb'} &&
  293:                 $curr_item->{'part'} eq $target->{'part'}) {
  294:                 last;
  295:             }
  296:         } elsif ($granularity eq 'response') {
  297:             if ($curr_item->{'symb'} eq $target->{'symb'} &&
  298:                 $curr_item->{'part'} eq $target->{'part'} &&
  299:                 $curr_item->{'respid'} eq $target->{'respid'} &&
  300:                 $curr_item->{'resptype'} eq $target->{'resptype'}) {
  301:                 last;
  302:             }
  303:         }
  304:     }
  305:     my $curr_item = $Resource[$curr_idx];
  306:     if ($granularity eq 'part') {
  307:         if ($curr_item->{'symb'}     ne $target->{'symb'} ||
  308:             $curr_item->{'part'}     ne $target->{'part'}) {
  309:             # bogus symb - return nothing
  310:             return (undef,undef,undef);
  311:         }
  312:     } elsif ($granularity eq 'response') {
  313:         if ($curr_item->{'symb'}     ne $target->{'symb'} ||
  314:             $curr_item->{'part'}     ne $target->{'part'} ||
  315:             $curr_item->{'respid'}   ne $target->{'respid'} ||
  316:             $curr_item->{'resptype'} ne $target->{'resptype'}){
  317:             # bogus symb - return nothing
  318:             return (undef,undef,undef);
  319:         }
  320:     }
  321:     #
  322:     # Now just pick up the data we need
  323:     my ($prev,$curr,$next);
  324:     if ($curr_idx == 0) {
  325:         $prev = undef;
  326:         $curr = $Resource[$curr_idx  ];
  327:         $next = $Resource[$curr_idx+1];
  328:     } elsif ($curr_idx == $#Resource) {
  329:         $prev = $Resource[$curr_idx-1];
  330:         $curr = $Resource[$curr_idx  ];
  331:         $next = undef;
  332:     } else {
  333:         $prev = $Resource[$curr_idx-1];
  334:         $curr = $Resource[$curr_idx  ];
  335:         $next = $Resource[$curr_idx+1];
  336:     }
  337:     return ($prev,$curr,$next);
  338: }
  339: 
  340: 
  341: #####################################################
  342: #####################################################
  343: 
  344: =pod
  345: 
  346: =item analyze_problem_as_student
  347: 
  348: Analyzes a homework problem for a student and returns the correct answer
  349: for the student.  Attempts to put together an answer for problem types 
  350: that do not natively support it.
  351: 
  352: Inputs: $resource: a resource object
  353:         $sname, $sdom, $partid, $respid
  354: 
  355: Returns: $answer
  356: 
  357: If $partid and $respid are specified, $answer is simply a scalar containing
  358: the correct answer for the response.
  359: 
  360: If $partid or $respid are undefined, $answer will be a hash reference with
  361: keys $partid.'.'.$respid.'.answer'.
  362: 
  363: =cut
  364: 
  365: #####################################################
  366: #####################################################
  367: sub analyze_problem_as_student {
  368:     my ($resource,$sname,$sdom,$partid,$respid) = @_;
  369:     my $returnvalue;
  370:     my $url = $resource->{'src'};
  371:     my $symb = $resource->{'symb'};
  372:     my $courseid = $ENV{'request.course.id'};
  373:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze',
  374:                                         'grade_domain' => $sdom,
  375:                                         'grade_username' => $sname,
  376:                                         'grade_symb' => $symb,
  377:                                         'grade_courseid' => $courseid));
  378:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
  379:     my %Answer=&Apache::lonnet::str2hash($Answ);
  380:     #
  381:     if (! defined($partid)) {
  382:         # If you do not specify a partid, you get them all.
  383:         foreach my $partid (@{$resource->{'parts'}}) {
  384:             my $partdata = $resource->{'partdata'}->{$partid};
  385:             foreach my $respid (@{$partdata->{'ResponseIds'}}) {
  386:                 my $prefix = $partid.'.'.$respid;
  387:                 my $key = $prefix.'.answer';
  388:                 $returnvalue->{$key} = &get_answer($prefix,$key,%Answer);
  389:             }
  390:         }
  391:     } elsif (! defined($respid)) {
  392:         my $partdata = $resource->{'partdata'}->{$partid};
  393:         foreach my $respid (@{$partdata->{'ResponseIds'}}) {
  394:             my $prefix = $partid.'.'.$respid;
  395:             my $key = $prefix.'.answer';
  396:             $returnvalue->{$key} = &get_answer($prefix,$key,%Answer);
  397:         }
  398:     } else {
  399:         my $prefix = $partid.'.'.$respid;
  400:         my $key = $prefix.'.answer';
  401:         $returnvalue = &get_answer($prefix,$key,%Answer);
  402:     }
  403:     return $returnvalue;
  404: }
  405: 
  406: sub get_answer {
  407:     my ($prefix,$key,%Answer) = @_;
  408:     my $returnvalue;
  409:     if (exists($Answer{$key})) {
  410:         my $student_answer = $Answer{$key}->[0];
  411:         if (! defined($student_answer)) {
  412:             $student_answer = $Answer{$key}->[1];
  413:         }
  414:         $returnvalue = $student_answer;
  415:     } else {
  416:         if (exists($Answer{$prefix.'.shown'})) {
  417:             # The response has foils
  418:             my %values;
  419:             while (my ($k,$v) = each(%Answer)) {
  420:                 next if ($k !~ /^$prefix\.foil\.(value|area)\.(.*)$/);
  421:                 my $foilname = $2;
  422:                 $values{$foilname}=$v;
  423:             }
  424:             foreach my $foil (@{$Answer{$prefix.'.shown'}}) {
  425:                 if (ref($values{$foil}) eq 'ARRAY') {
  426:                     $returnvalue.=&HTML::Entities::encode($foil).'='.
  427:                         join(',',map {&HTML::Entities::encode($_)} @{$values{$foil}}).'&';
  428:                 } else {
  429:                     $returnvalue.=&HTML::Entities::encode($foil).'='.
  430:                         &HTML::Entities::encode($values{$foil}).'&';
  431:                 }
  432:             }
  433:             $returnvalue =~ s/ /\%20/g;
  434:             chop ($returnvalue);
  435:         }
  436:     }
  437:     return $returnvalue;
  438: }
  439: 
  440: ##
  441: ## The following is copied from datecalc1.pl, part of the 
  442: ## Spreadsheet::WriteExcel CPAN module.
  443: ##
  444: ##
  445: ######################################################################
  446: #
  447: # Demonstration of writing date/time cells to Excel spreadsheets,
  448: # using UNIX/Perl time as source of date/time.
  449: #
  450: # Copyright 2000, Andrew Benham, adsb@bigfoot.com
  451: #
  452: ######################################################################
  453: #
  454: # UNIX/Perl time is the time since the Epoch (00:00:00 GMT, 1 Jan 1970)
  455: # measured in seconds.
  456: #
  457: # An Excel file can use exactly one of two different date/time systems.
  458: # In these systems, a floating point number represents the number of days
  459: # (and fractional parts of the day) since a start point. The floating point
  460: # number is referred to as a 'serial'.
  461: # The two systems ('1900' and '1904') use different starting points:
  462: #  '1900'; '1.00' is 1 Jan 1900 BUT 1900 is erroneously regarded as
  463: #          a leap year - see:
  464: #            http://support.microsoft.com/support/kb/articles/Q181/3/70.asp
  465: #          for the excuse^H^H^H^H^H^Hreason.
  466: #  '1904'; '1.00' is 2 Jan 1904.
  467: #
  468: # The '1904' system is the default for Apple Macs. Windows versions of
  469: # Excel have the option to use the '1904' system.
  470: #
  471: # Note that Visual Basic's "DateSerial" function does NOT erroneously
  472: # regard 1900 as a leap year, and thus its serials do not agree with
  473: # the 1900 serials of Excel for dates before 1 Mar 1900.
  474: #
  475: # Note that StarOffice (at least at version 5.2) does NOT erroneously
  476: # regard 1900 as a leap year, and thus its serials do not agree with
  477: # the 1900 serials of Excel for dates before 1 Mar 1900.
  478: #
  479: ######################################################################
  480: #
  481: # Calculation description
  482: # =======================
  483: #
  484: # 1900 system
  485: # -----------
  486: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 70 years after 1 Jan 1900.
  487: # Of those 70 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  488: # were leap years with an extra day.
  489: # Thus there were 17 + 70*365 days = 25567 days between 1 Jan 1900 and
  490: # 1 Jan 1970.
  491: # In the 1900 system, '1' is 1 Jan 1900, but as 1900 was not a leap year
  492: # 1 Jan 1900 should really be '2', so 1 Jan 1970 is '25569'.
  493: #
  494: # 1904 system
  495: # -----------
  496: # Unix time is '0' at 00:00:00 GMT 1 Jan 1970, i.e. 66 years after 1 Jan 1904.
  497: # Of those 66 years, 17 (1904,08,12,16,20,24,28,32,36,40,44,48,52,56,60,64,68)
  498: # were leap years with an extra day.
  499: # Thus there were 17 + 66*365 days = 24107 days between 1 Jan 1904 and
  500: # 1 Jan 1970.
  501: # In the 1904 system, 2 Jan 1904 being '1', 1 Jan 1970 is '24107'.
  502: #
  503: ######################################################################
  504: #
  505: # Copyright (c) 2000, Andrew Benham.
  506: # This program is free software. It may be used, redistributed and/or
  507: # modified under the same terms as Perl itself.
  508: #
  509: # Andrew Benham, adsb@bigfoot.com
  510: # London, United Kingdom
  511: # 11 Nov 2000
  512: #
  513: ######################################################################
  514: #-----------------------------------------------------------
  515: # calc_serial()
  516: #
  517: # Called with (up to) 2 parameters.
  518: #   1.  Unix timestamp.  If omitted, uses current time.
  519: #   2.  GMT flag. Set to '1' to return serial in GMT.
  520: #       If omitted, returns serial in appropriate timezone.
  521: #
  522: # Returns date/time serial according to $DATE_SYSTEM selected
  523: #-----------------------------------------------------------
  524: sub calc_serial {
  525:     # Use 1900 date system on all platforms other than Apple Mac (for which
  526:     # use 1904 date system).
  527:     my $DATE_SYSTEM = ($^O eq 'MacOS') ? 1 : 0;
  528:     my $time = (defined $_[0]) ? $_[0] : time();
  529:     my $gmtflag = (defined $_[1]) ? $_[1] : 0;
  530:     #
  531:     # Divide timestamp by number of seconds in a day.
  532:     # This gives a date serial with '0' on 1 Jan 1970.
  533:     my $serial = $time / 86400;
  534:     #
  535:     # Adjust the date serial by the offset appropriate to the
  536:     # currently selected system (1900/1904).
  537:     if ($DATE_SYSTEM == 0) {        # use 1900 system
  538:         $serial += 25569;
  539:     } else {                        # use 1904 system
  540:         $serial += 24107;
  541:     }
  542:     #
  543:     unless ($gmtflag) {
  544:         # Now have a 'raw' serial with the right offset. But this
  545:         # gives a serial in GMT, which is false unless the timezone
  546:         # is GMT. We need to adjust the serial by the appropriate
  547:         # timezone offset.
  548:         # Calculate the appropriate timezone offset by seeing what
  549:         # the differences between localtime and gmtime for the given
  550:         # time are.
  551:         #    
  552:         my @gmtime = gmtime($time);
  553:         my @ltime  = localtime($time);
  554:         #
  555:         # For the first 7 elements of the two arrays, adjust the
  556:         # date serial where the elements differ.
  557:         for (0 .. 6) {
  558:             my $diff = $ltime[$_] - $gmtime[$_];
  559:             if ($diff) {
  560:                 $serial += _adjustment($diff,$_);
  561:             }
  562:         }
  563:     }
  564:     #
  565:     # Perpetuate the error that 1900 was a leap year by decrementing
  566:     # the serial if we're using the 1900 system and the date is prior to
  567:     # 1 Mar 1900. This has the effect of making serial value '60'
  568:     # 29 Feb 1900.
  569:     #
  570:     # This fix only has any effect if UNIX/Perl time on the platform
  571:     # can represent 1900. Many can't.
  572:     #
  573:     unless ($DATE_SYSTEM) {
  574:         $serial-- if ($serial < 61);    # '61' is 1 Mar 1900
  575:     }
  576:     return $serial;
  577: }
  578: 
  579: sub _adjustment {
  580:     # Based on the difference in the localtime/gmtime array elements
  581:     # number, return the adjustment required to the serial.
  582:     #
  583:     # We only look at some elements of the localtime/gmtime arrays:
  584:     #    seconds    unlikely to be different as all known timezones
  585:     #               have an offset of integral multiples of 15 minutes,
  586:     #               but it's easy to do.
  587:     #    minutes    will be different for timezone offsets which are
  588:     #               not an exact number of hours.
  589:     #    hours      very likely to be different.
  590:     #    weekday    will differ when localtime/gmtime difference
  591:     #               straddles midnight.
  592:     #
  593:     # Assume that difference between localtime and gmtime is less than
  594:     # 5 days, then don't have to do maths for day of month, month number,
  595:     # year number, etc...
  596:     #
  597:     my ($delta,$element) = @_;
  598:     my $adjust = 0;
  599:     #
  600:     if ($element == 0) {            # Seconds
  601:         $adjust = $delta/86400;         # 60 * 60 * 24
  602:     } elsif ($element == 1) {       # Minutes
  603:         $adjust = $delta/1440;          # 60 * 24
  604:     } elsif ($element == 2) {       # Hours
  605:         $adjust = $delta/24;            # 24
  606:     } elsif ($element == 6) {       # Day of week number
  607:         # Catch difference straddling Sat/Sun in either direction
  608:         $delta += 7 if ($delta < -4);
  609:         $delta -= 7 if ($delta > 4);
  610:         #    
  611:         $adjust = $delta;
  612:     }
  613:     return $adjust;
  614: }
  615: 
  616: ###########################################################
  617: ###########################################################
  618: 
  619: =pod
  620: 
  621: =item get_problem_data
  622: 
  623: Returns a data structure describing the problem.
  624: 
  625: Inputs: $url
  626: 
  627: Returns: %Partdata
  628: 
  629: =cut
  630: 
  631: ## note: we must force each foil and option to not begin or end with
  632: ##       spaces as they are stored without such data.
  633: ##
  634: ###########################################################
  635: ###########################################################
  636: sub get_problem_data {
  637:     my ($url) = @_;
  638:     my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze'));
  639:     (my $garbage,$Answ)=split(/_HASH_REF__/,$Answ,2);
  640:     my %Answer;
  641:     %Answer=&Apache::lonnet::str2hash($Answ);
  642:     my %Partdata;
  643:     foreach my $part (@{$Answer{'parts'}}) {
  644:         while (my($key,$value) = each(%Answer)) {
  645:             #
  646:             # Logging code:
  647:             if (0) {
  648:                 &Apache::lonnet::logthis($part.' got key "'.$key.'"');
  649:                 if (ref($value) eq 'ARRAY') {
  650:                     &Apache::lonnet::logthis('    @'.join(',',@$value));
  651:                 } else {
  652:                     &Apache::lonnet::logthis('    '.$value);
  653:                 }
  654:             }
  655:             # End of logging code
  656:             next if ($key !~ /^$part/);
  657:             $key =~ s/^$part\.//;
  658:             if (ref($value) eq 'ARRAY') {
  659:                 if ($key eq 'options') {
  660:                     $Partdata{$part}->{'_Options'}=$value;
  661:                 } elsif ($key eq 'concepts') {
  662:                     $Partdata{$part}->{'_Concepts'}=$value;
  663:                 } elsif ($key =~ /^concept\.(.*)$/) {
  664:                     my $concept = $1;
  665:                     foreach my $foil (@$value) {
  666:                         $Partdata{$part}->{'_Foils'}->{$foil}->{'_Concept'}=
  667:                                                                       $concept;
  668:                     }
  669:                 } elsif ($key =~ /^(incorrect|answer|ans_low|ans_high)$/) {
  670:                     $Partdata{$part}->{$key}=$value;
  671:                 }
  672:             } else {
  673:                 if ($key=~ /^foil\.text\.(.*)$/) {
  674:                     my $foil = $1;
  675:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'name'}=$foil;
  676:                     $value =~ s/(\s*$|^\s*)//g;
  677:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'text'}=$value;
  678:                 } elsif ($key =~ /^foil\.value\.(.*)$/) {
  679:                     my $foil = $1;
  680:                     $Partdata{$part}->{'_Foils'}->{$foil}->{'value'}=$value;
  681:                 }
  682:             }
  683:         }
  684:     }
  685:     return %Partdata;
  686: }
  687: 
  688: ####################################################
  689: ####################################################
  690: 
  691: =pod
  692: 
  693: =item &limit_by_time()
  694: 
  695: =cut
  696: 
  697: ####################################################
  698: ####################################################
  699: sub limit_by_time_form {
  700:     my $Starttime_form = '';
  701:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  702:         ('limitby_startdate');
  703:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  704:         ('limitby_enddate');
  705:     if (! defined($endtime)) {
  706:         $endtime = time;
  707:     }
  708:     if (! defined($starttime)) {
  709:         $starttime = $endtime - 60*60*24*7;
  710:     }
  711:     my $state;
  712:     if (&limit_by_time()) {
  713:         $state = '';
  714:     } else {
  715:         $state = 'disabled';
  716:     }
  717:     my $startdateform = &Apache::lonhtmlcommon::date_setter
  718:         ('Statistics','limitby_startdate',$starttime,undef,undef,$state);
  719:     my $enddateform = &Apache::lonhtmlcommon::date_setter
  720:         ('Statistics','limitby_enddate',$endtime,undef,undef,$state);
  721:     my $Str;
  722:     $Str .= '<script language="Javascript" >';
  723:     $Str .= 'function toggle_limitby_activity(state) {';
  724:     $Str .= '    if (state) {';
  725:     $Str .= '        limitby_startdate_enable();';
  726:     $Str .= '        limitby_enddate_enable();';
  727:     $Str .= '    } else {';
  728:     $Str .= '        limitby_startdate_disable();';
  729:     $Str .= '        limitby_enddate_disable();';
  730:     $Str .= '    }';    
  731:     $Str .= '}';
  732:     $Str .= '</script>';
  733:     $Str .= '<fieldset>';
  734:     my $timecheckbox = '<input type="checkbox" name="limit_by_time" ';
  735:     if (&limit_by_time()) {
  736:         $timecheckbox .= ' checked ';
  737:     } 
  738:     $timecheckbox .= 'OnChange="javascript:toggle_limitby_activity(this.checked);" ';
  739:     $timecheckbox .= ' />';
  740:     $Str .= '<legend>'.&mt('[_1] Limit by time',$timecheckbox).'</legend>';
  741:     $Str .= &mt('Start Time: [_1]',$startdateform).'<br />';
  742:     $Str .= &mt('&nbsp;End Time: [_1]',$enddateform).'<br />';
  743:     $Str .= '</fieldset>';
  744:     return $Str;
  745: }
  746: 
  747: sub limit_by_time {
  748:     if (exists($ENV{'form.limit_by_time'}) &&
  749:         $ENV{'form.limit_by_time'} ne '' ) {
  750:         return 1;
  751:     } else {
  752:         return 0;
  753:     }
  754: }
  755: 
  756: sub get_time_limits {
  757:     my $starttime = &Apache::lonhtmlcommon::get_date_from_form
  758:         ('limitby_startdate');
  759:     my $endtime = &Apache::lonhtmlcommon::get_date_from_form
  760:         ('limitby_enddate');
  761:     return ($starttime,$endtime);
  762: }
  763: 
  764: ####################################################
  765: ####################################################
  766: 
  767: =pod
  768: 
  769: =back
  770: 
  771: =cut
  772: 
  773: ####################################################
  774: ####################################################
  775: 
  776: 1;
  777: 
  778: __END__

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