Annotation of loncom/interface/loncoursedata.pm, revision 1.70

1.1       stredwic    1: # The LearningOnline Network with CAPA
                      2: #
1.70    ! matthew     3: # $Id: loncoursedata.pm,v 1.69 2003/04/18 19:05:30 matthew Exp $
1.1       stredwic    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: =pod
                     30: 
                     31: =head1 NAME
                     32: 
                     33: loncoursedata
                     34: 
                     35: =head1 SYNOPSIS
                     36: 
1.22      stredwic   37: Set of functions that download and process student and course information.
1.1       stredwic   38: 
                     39: =head1 PACKAGES USED
                     40: 
                     41:  Apache::Constants qw(:common :http)
                     42:  Apache::lonnet()
1.22      stredwic   43:  Apache::lonhtmlcommon
1.1       stredwic   44:  HTML::TokeParser
                     45:  GDBM_File
                     46: 
                     47: =cut
                     48: 
                     49: package Apache::loncoursedata;
                     50: 
                     51: use strict;
                     52: use Apache::Constants qw(:common :http);
                     53: use Apache::lonnet();
1.13      stredwic   54: use Apache::lonhtmlcommon;
1.57      matthew    55: use Time::HiRes;
                     56: use Apache::lonmysql;
1.1       stredwic   57: use HTML::TokeParser;
                     58: use GDBM_File;
                     59: 
                     60: =pod
                     61: 
                     62: =head1 DOWNLOAD INFORMATION
                     63: 
1.22      stredwic   64: This section contains all the functions that get data from other servers 
                     65: and/or itself.
1.1       stredwic   66: 
                     67: =cut
                     68: 
1.50      matthew    69: ####################################################
                     70: ####################################################
1.45      matthew    71: 
                     72: =pod
                     73: 
                     74: =item &get_sequence_assessment_data()
                     75: 
                     76: AT THIS TIME THE USE OF THIS FUNCTION IS *NOT* RECOMMENDED
                     77: 
                     78: Use lonnavmaps to build a data structure describing the order and 
                     79: assessment contents of each sequence in the current course.
                     80: 
                     81: The returned structure is a hash reference. 
                     82: 
1.61      matthew    83: { title => 'title',
                     84:   symb  => 'symb',
                     85:   src   => '/s/o/u/r/c/e',
1.45      matthew    86:   type  => (container|assessment),
1.50      matthew    87:   num_assess   => 2,               # only for container
1.45      matthew    88:   parts        => [11,13,15],      # only for assessment
1.50      matthew    89:   response_ids => [12,14,16],      # only for assessment
                     90:   contents     => [........]       # only for container
1.45      matthew    91: }
                     92: 
1.50      matthew    93: $hash->{'contents'} is a reference to an array of hashes of the same structure.
                     94: 
                     95: Also returned are array references to the sequences and assessments contained
                     96: in the course.
1.49      matthew    97: 
1.45      matthew    98: 
                     99: =cut
                    100: 
1.50      matthew   101: ####################################################
                    102: ####################################################
1.45      matthew   103: sub get_sequence_assessment_data {
                    104:     my $fn=$ENV{'request.course.fn'};
                    105:     ##
                    106:     ## use navmaps
1.65      bowersj2  107:     my $navmap = Apache::lonnavmaps::navmap->new($fn.".db",
1.61      matthew   108:                                                  $fn."_parms.db",1,0);
1.45      matthew   109:     if (!defined($navmap)) {
                    110:         return 'Can not open Coursemap';
                    111:     }
                    112:     my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.61      matthew   113:     my $curRes = $iterator->next(); # Top level sequence
1.45      matthew   114:     ##
                    115:     ## Prime the pump 
                    116:     ## 
                    117:     ## We are going to loop until we run out of sequences/pages to explore for
                    118:     ## resources.  This means we have to start out with something to look
                    119:     ## at.
1.52      matthew   120:     my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
                    121:     my $symb  = 'top';
                    122:     my $src   = 'not applicable';
1.45      matthew   123:     #
1.49      matthew   124:     my @Sequences; 
                    125:     my @Assessments;
1.45      matthew   126:     my @Nested_Sequences = ();   # Stack of sequences, keeps track of depth
                    127:     my $top = { title    => $title,
1.52      matthew   128:                 src      => $src,
1.45      matthew   129:                 symb     => $symb,
                    130:                 type     => 'container',
                    131:                 num_assess => 0,
1.53      matthew   132:                 num_assess_parts => 0,
1.45      matthew   133:                 contents   => [], };
1.49      matthew   134:     push (@Sequences,$top);
1.45      matthew   135:     push (@Nested_Sequences, $top);
                    136:     #
                    137:     # We need to keep track of which sequences contain homework problems
                    138:     # 
1.52      matthew   139:     my $previous;
1.45      matthew   140:     while (scalar(@Nested_Sequences)) {
1.50      matthew   141:         $previous = $curRes;
1.45      matthew   142:         $curRes = $iterator->next();
                    143:         my $currentmap = $Nested_Sequences[-1]; # Last one on the stack
                    144:         if ($curRes == $iterator->BEGIN_MAP()) {
                    145:             # get the map itself, instead of BEGIN_MAP
1.51      matthew   146:             $title = $previous->title();
                    147:             $symb  = $previous->symb();
                    148:             $src   = $previous->src();
1.45      matthew   149:             my $newmap = { title    => $title,
                    150:                            src      => $src,
                    151:                            symb     => $symb,
                    152:                            type     => 'container',
                    153:                            num_assess => 0,
                    154:                            contents   => [],
                    155:                        };
                    156:             push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49      matthew   157:             push (@Sequences,$newmap);
1.45      matthew   158:             push (@Nested_Sequences, $newmap); # this is a stack
                    159:             next;
                    160:         }
                    161:         if ($curRes == $iterator->END_MAP()) {
                    162:             pop(@Nested_Sequences);
                    163:             next;
                    164:         }
                    165:         next if (! ref($curRes));
1.50      matthew   166:         next if (! $curRes->is_problem());# && !$curRes->randomout);
1.45      matthew   167:         # Okay, from here on out we only deal with assessments
                    168:         $title = $curRes->title();
                    169:         $symb  = $curRes->symb();
                    170:         $src   = $curRes->src();
                    171:         my $parts = $curRes->parts();
                    172:         my $assessment = { title => $title,
                    173:                            src   => $src,
                    174:                            symb  => $symb,
                    175:                            type  => 'assessment',
1.53      matthew   176:                            parts => $parts,
                    177:                            num_parts => scalar(@$parts),
1.45      matthew   178:                        };
1.49      matthew   179:         push(@Assessments,$assessment);
1.45      matthew   180:         push(@{$currentmap->{'contents'}},$assessment);
                    181:         $currentmap->{'num_assess'}++;
1.53      matthew   182:         $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45      matthew   183:     }
1.58      matthew   184:     $navmap->untieHashes();
1.49      matthew   185:     return ($top,\@Sequences,\@Assessments);
1.45      matthew   186: }
1.50      matthew   187: 
1.4       stredwic  188: sub LoadDiscussion {
1.13      stredwic  189:     my ($courseID)=@_;
1.5       minaeibi  190:     my %Discuss=();
                    191:     my %contrib=&Apache::lonnet::dump(
                    192:                 $courseID,
                    193:                 $ENV{'course.'.$courseID.'.domain'},
                    194:                 $ENV{'course.'.$courseID.'.num'});
                    195: 				 
                    196:     #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
                    197: 
1.4       stredwic  198:     foreach my $temp(keys %contrib) {
                    199: 	if ($temp=~/^version/) {
                    200: 	    my $ver=$contrib{$temp};
                    201: 	    my ($dummy,$prb)=split(':',$temp);
                    202: 	    for (my $idx=1; $idx<=$ver; $idx++ ) {
                    203: 		my $name=$contrib{"$idx:$prb:sendername"};
1.5       minaeibi  204: 		$Discuss{"$name:$prb"}=$idx;	
1.4       stredwic  205: 	    }
                    206: 	}
                    207:     }       
1.5       minaeibi  208: 
                    209:     return \%Discuss;
1.1       stredwic  210: }
                    211: 
                    212: =pod
                    213: 
                    214: =item &ProcessFullName()
                    215: 
                    216: Takes lastname, generation, firstname, and middlename (or some partial
                    217: set of this data) and returns the full name version as a string.  Format
                    218: is Lastname generation, firstname middlename or a subset of this.
                    219: 
                    220: =cut
                    221: 
                    222: sub ProcessFullName {
                    223:     my ($lastname, $generation, $firstname, $middlename)=@_;
                    224:     my $Str = '';
                    225: 
1.34      matthew   226:     # Strip whitespace preceeding & following name components.
                    227:     $lastname   =~ s/(\s+$|^\s+)//g;
                    228:     $generation =~ s/(\s+$|^\s+)//g;
                    229:     $firstname  =~ s/(\s+$|^\s+)//g;
                    230:     $middlename =~ s/(\s+$|^\s+)//g;
                    231: 
1.1       stredwic  232:     if($lastname ne '') {
1.34      matthew   233: 	$Str .= $lastname;
                    234: 	$Str .= ' '.$generation if ($generation ne '');
                    235: 	$Str .= ',';
                    236:         $Str .= ' '.$firstname  if ($firstname ne '');
                    237:         $Str .= ' '.$middlename if ($middlename ne '');
1.1       stredwic  238:     } else {
1.34      matthew   239:         $Str .= $firstname      if ($firstname ne '');
                    240:         $Str .= ' '.$middlename if ($middlename ne '');
                    241:         $Str .= ' '.$generation if ($generation ne '');
1.1       stredwic  242:     }
                    243: 
                    244:     return $Str;
                    245: }
                    246: 
1.46      matthew   247: ################################################
                    248: ################################################
                    249: 
                    250: =pod
                    251: 
1.47      matthew   252: =item &make_into_hash($values);
                    253: 
                    254: Returns a reference to a hash as described by $values.  $values is
                    255: assumed to be the result of 
1.57      matthew   256:     join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47      matthew   257: 
                    258: This is a helper function for get_current_state.
                    259: 
                    260: =cut
                    261: 
                    262: ################################################
                    263: ################################################
                    264: sub make_into_hash {
                    265:     my $values = shift;
                    266:     my %tmp = map { &Apache::lonnet::unescape($_); }
                    267:                                            split(':',$values);
                    268:     return \%tmp;
                    269: }
                    270: 
                    271: 
                    272: ################################################
                    273: ################################################
                    274: 
                    275: =pod
                    276: 
1.57      matthew   277: =head1 LOCAL DATA CACHING SUBROUTINES
                    278: 
                    279: The local caching is done using MySQL.  There is no fall-back implementation
                    280: if MySQL is not running.
                    281: 
                    282: The programmers interface is to call &get_current_state() or some other
                    283: primary interface subroutine (described below).  The internals of this 
                    284: storage system are documented here.
                    285: 
                    286: There are six tables used to store student performance data (the results of
                    287: a dumpcurrent).  Each of these tables is created in MySQL with a name of
                    288: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate 
                    289: for the table.  The tables and their purposes are described below.
                    290: 
                    291: Some notes before we get started.
                    292: 
                    293: Each table must have a PRIMARY KEY, which is a column or set of columns which
                    294: will serve to uniquely identify a row of data.  NULL is not allowed!
                    295: 
                    296: INDEXes work best on integer data.
                    297: 
                    298: JOIN is used to combine data from many tables into one output.
                    299: 
                    300: lonmysql.pm is used for some of the interface, specifically the table creation
                    301: calls.  The inserts are done in bulk by directly calling the database handler.
                    302: The SELECT ... JOIN statement used to retrieve the data does not have an
                    303: interface in lonmysql.pm and I shudder at the thought of writing one.
                    304: 
                    305: =head3 Table Descriptions
                    306: 
                    307: =over 4
                    308: 
                    309: =item $symb_table
                    310: 
                    311: The symb_table has two columns.  The first is a 'symb_id' and the second
                    312: is the text name for the 'symb' (limited to 64k).  The 'symb_id' is generated
                    313: automatically by MySQL so inserts should be done on this table with an
                    314: empty first element.  This table has its PRIMARY KEY on the 'symb_id'.
                    315: 
                    316: =item $part_table
                    317: 
                    318: The part_table has two columns.  The first is a 'part_id' and the second
                    319: is the text name for the 'part' (limited to 100 characters).  The 'part_id' is
                    320: generated automatically by MySQL so inserts should be done on this table with
                    321: an empty first element.  This table has its PRIMARY KEY on the 'part' (100
                    322: characters) and a KEY on 'part_id'.
                    323: 
                    324: =item $student_table
                    325: 
                    326: The student_table has two columns.  The first is a 'student_id' and the second
                    327: is the text description of the 'student' (typically username:domain) (less
                    328: than 100 characters).  The 'student_id' is automatically generated by MySQL.
                    329: The use of the name 'student_id' is loaded, I know, but this ID is used ONLY 
                    330: internally to the MySQL database and is not the same as the students ID 
                    331: (stored in the students environment).  This table has its PRIMARY KEY on the
                    332: 'student' (100 characters).
                    333: 
                    334: =item $updatetime_table
                    335: 
                    336: The updatetime_table has two columns.  The first is 'student' (100 characters,
                    337: typically username:domain).  The second is 'updatetime', which is an unsigned
                    338: integer, NOT a MySQL date.  This table has its PRIMARY KEY on 'student' (100
                    339: characters).
                    340: 
                    341: =item $performance_table
                    342: 
                    343: The performance_table has 9 columns.  The first three are 'symb_id', 
                    344: 'student_id', and 'part_id'.  These comprise the PRIMARY KEY for this table
                    345: and are directly related to the $symb_table, $student_table, and $part_table
                    346: described above.  MySQL does better indexing on numeric items than text,
                    347: so we use these three "index tables".  The remaining columns are
                    348: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
                    349: These are either the MySQL type TINYTEXT or various integers ('tries' and 
                    350: 'timestamp').  This table has KEYs of 'student_id' and 'symb_id'.
                    351: For use of this table, see the functions described below.
                    352: 
                    353: =item $parameters_table
                    354: 
                    355: The parameters_table holds the data that does not fit neatly into the
                    356: performance_table.  The parameters table has four columns: 'symb_id',
                    357: 'student_id', 'parameter', and 'value'.  'symb_id', 'student_id', and
                    358: 'parameter' comprise the PRIMARY KEY for this table.  'parameter' is 
                    359: limited to 255 characters.  'value' is limited to 64k characters.
                    360: 
                    361: =back
                    362: 
                    363: =head3 Important Subroutines
                    364: 
                    365: Here is a brief overview of the subroutines which are likely to be of 
                    366: interest:
                    367: 
                    368: =over 4
                    369: 
                    370: =item &get_current_state(): programmers interface.
                    371: 
                    372: =item &init_dbs(): table creation
                    373: 
                    374: =item &update_student_data(): data storage calls
                    375: 
                    376: =item &get_student_data_from_performance_cache(): data retrieval
                    377: 
                    378: =back
                    379: 
                    380: =head3 Main Documentation
                    381: 
                    382: =over 4
                    383: 
                    384: =cut
                    385: 
                    386: ################################################
                    387: ################################################
                    388: 
                    389: ################################################
                    390: ################################################
                    391: {
                    392: 
                    393: my $current_course ='';
                    394: my $symb_table;
                    395: my $part_table;
                    396: my $student_table;
                    397: my $updatetime_table;
                    398: my $performance_table;
                    399: my $parameters_table;
                    400: 
                    401: ################################################
                    402: ################################################
                    403: 
                    404: =pod
                    405: 
                    406: =item &init_dbs()
                    407: 
                    408: Input: course id
                    409: 
                    410: Output: 0 on success, positive integer on error
                    411: 
                    412: This routine issues the calls to lonmysql to create the tables used to
                    413: store student data.
                    414: 
                    415: =cut
                    416: 
                    417: ################################################
                    418: ################################################
                    419: sub init_dbs {
                    420:     my $courseid = shift;
                    421:     &setup_table_names($courseid);
                    422:     #
                    423:     # Note - changes to this table must be reflected in the code that 
                    424:     # stores the data (calls &Apache::lonmysql::store_row with this table
                    425:     # id
                    426:     my $symb_table_def = {
                    427:         id => $symb_table,
                    428:         permanent => 'no',
                    429:         columns => [{ name => 'symb_id',
                    430:                       type => 'MEDIUMINT UNSIGNED',
                    431:                       restrictions => 'NOT NULL',
                    432:                       auto_inc     => 'yes', },
                    433:                     { name => 'symb',
                    434:                       type => 'MEDIUMTEXT',
                    435:                       restrictions => 'NOT NULL'},
                    436:                     ],
                    437:         'PRIMARY KEY' => ['symb_id'],
                    438:     };
                    439:     #
                    440:     my $part_table_def = {
                    441:         id => $part_table,
                    442:         permanent => 'no',
                    443:         columns => [{ name => 'part_id',
                    444:                       type => 'MEDIUMINT UNSIGNED',
                    445:                       restrictions => 'NOT NULL',
                    446:                       auto_inc     => 'yes', },
                    447:                     { name => 'part',
                    448:                       type => 'VARCHAR(100)',
                    449:                       restrictions => 'NOT NULL'},
                    450:                     ],
                    451:         'PRIMARY KEY' => ['part (100)'],
                    452:         'KEY' => [{ columns => ['part_id']},],
                    453:     };
                    454:     #
                    455:     my $student_table_def = {
                    456:         id => $student_table,
                    457:         permanent => 'no',
                    458:         columns => [{ name => 'student_id',
                    459:                       type => 'MEDIUMINT UNSIGNED',
                    460:                       restrictions => 'NOT NULL',
                    461:                       auto_inc     => 'yes', },
                    462:                     { name => 'student',
                    463:                       type => 'VARCHAR(100)',
                    464:                       restrictions => 'NOT NULL'},
                    465:                     ],
                    466:         'PRIMARY KEY' => ['student (100)'],
                    467:         'KEY' => [{ columns => ['student_id']},],
                    468:     };
                    469:     #
                    470:     my $updatetime_table_def = {
                    471:         id => $updatetime_table,
                    472:         permanent => 'no',
                    473:         columns => [{ name => 'student',
                    474:                       type => 'VARCHAR(100)',
                    475:                       restrictions => 'NOT NULL UNIQUE',},
                    476:                     { name => 'updatetime',
                    477:                       type => 'INT UNSIGNED',
                    478:                       restrictions => 'NOT NULL' },
                    479:                     ],
                    480:         'PRIMARY KEY' => ['student (100)'],
                    481:     };
                    482:     #
                    483:     my $performance_table_def = {
                    484:         id => $performance_table,
                    485:         permanent => 'no',
                    486:         columns => [{ name => 'symb_id',
                    487:                       type => 'MEDIUMINT UNSIGNED',
                    488:                       restrictions => 'NOT NULL'  },
                    489:                     { name => 'student_id',
                    490:                       type => 'MEDIUMINT UNSIGNED',
                    491:                       restrictions => 'NOT NULL'  },
                    492:                     { name => 'part_id',
                    493:                       type => 'MEDIUMINT UNSIGNED',
                    494:                       restrictions => 'NOT NULL' },
                    495:                     { name => 'solved',
                    496:                       type => 'TINYTEXT' },
                    497:                     { name => 'tries',
                    498:                       type => 'SMALLINT UNSIGNED' },
                    499:                     { name => 'awarded',
                    500:                       type => 'TINYTEXT' },
                    501:                     { name => 'award',
                    502:                       type => 'TINYTEXT' },
                    503:                     { name => 'awarddetail',
                    504:                       type => 'TINYTEXT' },
                    505:                     { name => 'timestamp',
                    506:                       type => 'INT UNSIGNED'},
                    507:                     ],
                    508:         'PRIMARY KEY' => ['symb_id','student_id','part_id'],
                    509:         'KEY' => [{ columns=>['student_id'] },
                    510:                   { columns=>['symb_id'] },],
                    511:     };
                    512:     #
                    513:     my $parameters_table_def = {
                    514:         id => $parameters_table,
                    515:         permanent => 'no',
                    516:         columns => [{ name => 'symb_id',
                    517:                       type => 'MEDIUMINT UNSIGNED',
                    518:                       restrictions => 'NOT NULL'  },
                    519:                     { name => 'student_id',
                    520:                       type => 'MEDIUMINT UNSIGNED',
                    521:                       restrictions => 'NOT NULL'  },
                    522:                     { name => 'parameter',
                    523:                       type => 'TINYTEXT',
                    524:                       restrictions => 'NOT NULL'  },
                    525:                     { name => 'value',
                    526:                       type => 'MEDIUMTEXT' },
                    527:                     ],
                    528:         'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
                    529:     };
                    530:     #
                    531:     # Create the tables
                    532:     my $tableid;
                    533:     $tableid = &Apache::lonmysql::create_table($symb_table_def);
                    534:     if (! defined($tableid)) {
                    535:         &Apache::lonnet::logthis("error creating symb_table: ".
                    536:                                  &Apache::lonmysql::get_error());
                    537:         return 1;
                    538:     }
                    539:     #
                    540:     $tableid = &Apache::lonmysql::create_table($part_table_def);
                    541:     if (! defined($tableid)) {
                    542:         &Apache::lonnet::logthis("error creating part_table: ".
                    543:                                  &Apache::lonmysql::get_error());
                    544:         return 2;
                    545:     }
                    546:     #
                    547:     $tableid = &Apache::lonmysql::create_table($student_table_def);
                    548:     if (! defined($tableid)) {
                    549:         &Apache::lonnet::logthis("error creating student_table: ".
                    550:                                  &Apache::lonmysql::get_error());
                    551:         return 3;
                    552:     }
                    553:     #
                    554:     $tableid = &Apache::lonmysql::create_table($updatetime_table_def);
                    555:     if (! defined($tableid)) {
                    556:         &Apache::lonnet::logthis("error creating updatetime_table: ".
                    557:                                  &Apache::lonmysql::get_error());
                    558:         return 4;
                    559:     }
                    560:     #
                    561:     $tableid = &Apache::lonmysql::create_table($performance_table_def);
                    562:     if (! defined($tableid)) {
                    563:         &Apache::lonnet::logthis("error creating preformance_table: ".
                    564:                                  &Apache::lonmysql::get_error());
                    565:         return 5;
                    566:     }
                    567:     #
                    568:     $tableid = &Apache::lonmysql::create_table($parameters_table_def);
                    569:     if (! defined($tableid)) {
                    570:         &Apache::lonnet::logthis("error creating parameters_table: ".
                    571:                                  &Apache::lonmysql::get_error());
                    572:         return 6;
                    573:     }
                    574:     return 0;
1.70    ! matthew   575: }
        !           576: 
        !           577: ################################################
        !           578: ################################################
        !           579: 
        !           580: =pod
        !           581: 
        !           582: =item &delete_caches()
        !           583: 
        !           584: =cut
        !           585: 
        !           586: ################################################
        !           587: ################################################
        !           588: sub delete_caches {
        !           589:     my $courseid = shift;
        !           590:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
        !           591:     #
        !           592:     &setup_table_names($courseid);
        !           593:     #
        !           594:     my $dbh = &Apache::lonmysql::get_dbh();
        !           595:     foreach my $table ($symb_table,$part_table,$student_table,
        !           596:                        $updatetime_table,$performance_table,
        !           597:                        $parameters_table ){
        !           598:         my $command = 'DROP TABLE '.$table.';';
        !           599:         $dbh->do($command);
        !           600:         if ($dbh->err) {
        !           601:             &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
        !           602:         }
        !           603:     }
        !           604:     return;
1.57      matthew   605: }
                    606: 
                    607: ################################################
                    608: ################################################
                    609: 
                    610: =pod
                    611: 
                    612: =item &get_part_id()
                    613: 
                    614: Get the MySQL id of a problem part string.
                    615: 
                    616: Input: $part
                    617: 
                    618: Output: undef on error, integer $part_id on success.
                    619: 
                    620: =item &get_part()
                    621: 
                    622: Get the string describing a part from the MySQL id of the problem part.
                    623: 
                    624: Input: $part_id
                    625: 
                    626: Output: undef on error, $part string on success.
                    627: 
                    628: =cut
                    629: 
                    630: ################################################
                    631: ################################################
                    632: 
1.61      matthew   633: my $have_read_part_table = 0;
1.57      matthew   634: my %ids_by_part;
                    635: my %parts_by_id;
                    636: 
                    637: sub get_part_id {
                    638:     my ($part) = @_;
1.61      matthew   639:     $part = 0 if (! defined($part));
                    640:     if (! $have_read_part_table) {
                    641:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    642:         foreach (@Result) {
                    643:             $ids_by_part{$_->[1]}=$_->[0];
                    644:         }
                    645:         $have_read_part_table = 1;
                    646:     }
1.57      matthew   647:     if (! exists($ids_by_part{$part})) {
                    648:         &Apache::lonmysql::store_row($part_table,[undef,$part]);
                    649:         undef(%ids_by_part);
                    650:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    651:         foreach (@Result) {
                    652:             $ids_by_part{$_->[1]}=$_->[0];
                    653:         }
                    654:     }
                    655:     return $ids_by_part{$part} if (exists($ids_by_part{$part}));
                    656:     return undef; # error
                    657: }
                    658: 
                    659: sub get_part {
                    660:     my ($part_id) = @_;
                    661:     if (! exists($parts_by_id{$part_id})  || 
                    662:         ! defined($parts_by_id{$part_id}) ||
                    663:         $parts_by_id{$part_id} eq '') {
                    664:         my @Result = &Apache::lonmysql::get_rows($part_table);
                    665:         foreach (@Result) {
                    666:             $parts_by_id{$_->[0]}=$_->[1];
                    667:         }
                    668:     }
                    669:     return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
                    670:     return undef; # error
                    671: }
                    672: 
                    673: ################################################
                    674: ################################################
                    675: 
                    676: =pod
                    677: 
                    678: =item &get_symb_id()
                    679: 
                    680: Get the MySQL id of a symb.
                    681: 
                    682: Input: $symb
                    683: 
                    684: Output: undef on error, integer $symb_id on success.
                    685: 
                    686: =item &get_symb()
                    687: 
                    688: Get the symb associated with a MySQL symb_id.
                    689: 
                    690: Input: $symb_id
                    691: 
                    692: Output: undef on error, $symb on success.
                    693: 
                    694: =cut
                    695: 
                    696: ################################################
                    697: ################################################
                    698: 
1.61      matthew   699: my $have_read_symb_table = 0;
1.57      matthew   700: my %ids_by_symb;
                    701: my %symbs_by_id;
                    702: 
                    703: sub get_symb_id {
                    704:     my ($symb) = @_;
1.61      matthew   705:     if (! $have_read_symb_table) {
                    706:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    707:         foreach (@Result) {
                    708:             $ids_by_symb{$_->[1]}=$_->[0];
                    709:         }
                    710:         $have_read_symb_table = 1;
                    711:     }
1.57      matthew   712:     if (! exists($ids_by_symb{$symb})) {
                    713:         &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
                    714:         undef(%ids_by_symb);
                    715:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    716:         foreach (@Result) {
                    717:             $ids_by_symb{$_->[1]}=$_->[0];
                    718:         }
                    719:     }
                    720:     return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
                    721:     return undef; # error
                    722: }
                    723: 
                    724: sub get_symb {
                    725:     my ($symb_id) = @_;
                    726:     if (! exists($symbs_by_id{$symb_id})  || 
                    727:         ! defined($symbs_by_id{$symb_id}) ||
                    728:         $symbs_by_id{$symb_id} eq '') {
                    729:         my @Result = &Apache::lonmysql::get_rows($symb_table);
                    730:         foreach (@Result) {
                    731:             $symbs_by_id{$_->[0]}=$_->[1];
                    732:         }
                    733:     }
                    734:     return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
                    735:     return undef; # error
                    736: }
                    737: 
                    738: ################################################
                    739: ################################################
                    740: 
                    741: =pod
                    742: 
                    743: =item &get_student_id()
                    744: 
                    745: Get the MySQL id of a student.
                    746: 
                    747: Input: $sname, $dom
                    748: 
                    749: Output: undef on error, integer $student_id on success.
                    750: 
                    751: =item &get_student()
                    752: 
                    753: Get student username:domain associated with the MySQL student_id.
                    754: 
                    755: Input: $student_id
                    756: 
                    757: Output: undef on error, string $student (username:domain) on success.
                    758: 
                    759: =cut
                    760: 
                    761: ################################################
                    762: ################################################
                    763: 
1.61      matthew   764: my $have_read_student_table = 0;
1.57      matthew   765: my %ids_by_student;
                    766: my %students_by_id;
                    767: 
                    768: sub get_student_id {
                    769:     my ($sname,$sdom) = @_;
                    770:     my $student = $sname.':'.$sdom;
1.61      matthew   771:     if (! $have_read_student_table) {
                    772:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    773:         foreach (@Result) {
                    774:             $ids_by_student{$_->[1]}=$_->[0];
                    775:         }
                    776:         $have_read_student_table = 1;
                    777:     }
1.57      matthew   778:     if (! exists($ids_by_student{$student})) {
                    779:         &Apache::lonmysql::store_row($student_table,[undef,$student]);
                    780:         undef(%ids_by_student);
                    781:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    782:         foreach (@Result) {
                    783:             $ids_by_student{$_->[1]}=$_->[0];
                    784:         }
                    785:     }
                    786:     return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
                    787:     return undef; # error
                    788: }
                    789: 
                    790: sub get_student {
                    791:     my ($student_id) = @_;
                    792:     if (! exists($students_by_id{$student_id})  || 
                    793:         ! defined($students_by_id{$student_id}) ||
                    794:         $students_by_id{$student_id} eq '') {
                    795:         my @Result = &Apache::lonmysql::get_rows($student_table);
                    796:         foreach (@Result) {
                    797:             $students_by_id{$_->[0]}=$_->[1];
                    798:         }
                    799:     }
                    800:     return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
                    801:     return undef; # error
                    802: }
                    803: 
                    804: ################################################
                    805: ################################################
                    806: 
                    807: =pod
                    808: 
                    809: =item &update_student_data()
                    810: 
                    811: Input: $sname, $sdom, $courseid
                    812: 
                    813: Output: $returnstatus, \%student_data
                    814: 
                    815: $returnstatus is a string describing any errors that occured.  'okay' is the
                    816: default.
                    817: \%student_data is the data returned by a call to lonnet::currentdump.
                    818: 
                    819: This subroutine loads a students data using lonnet::currentdump and inserts
                    820: it into the MySQL database.  The inserts are done on two tables, 
                    821: $performance_table and $parameters_table.  $parameters_table holds the data 
                    822: that is not included in $performance_table.  See the description of 
                    823: $performance_table elsewhere in this file.  The INSERT calls are made
                    824: directly by this subroutine, not through lonmysql because we do a 'bulk'
                    825: insert which takes advantage of MySQLs non-SQL compliant INSERT command to 
                    826: insert multiple rows at a time.  If anything has gone wrong during this
                    827: process, $returnstatus is updated with a description of the error and
                    828: \%student_data is returned.  
                    829: 
                    830: Notice we do not insert the data and immediately query it.  This means it
                    831: is possible for there to be data returned this first time that is not 
                    832: available the second time.  CYA.
                    833: 
                    834: =cut
                    835: 
                    836: ################################################
                    837: ################################################
                    838: sub update_student_data {
                    839:     my ($sname,$sdom,$courseid) = @_;
                    840:     #
1.60      matthew   841:     # Set up database names
                    842:     &setup_table_names($courseid);
                    843:     #
1.57      matthew   844:     my $student_id = &get_student_id($sname,$sdom);
                    845:     my $student = $sname.':'.$sdom;
                    846:     #
                    847:     my $returnstatus = 'okay';
                    848:     #
                    849:     # Download students data
                    850:     my $time_of_retrieval = time;
                    851:     my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
                    852:     if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
                    853:         &Apache::lonnet::logthis('error getting data for '.
                    854:                                  $sname.':'.$sdom.' in course '.$courseid.
                    855:                                  ':'.$tmp[0]);
                    856:         $returnstatus = 'error getting data';
                    857:         return $returnstatus;
                    858:     }
                    859:     if (scalar(@tmp) < 1) {
                    860:         return ('no data',undef);
                    861:     }
                    862:     my %student_data = @tmp;
                    863:     #
                    864:     # Remove all of the students data from the table
1.60      matthew   865:     my $dbh = &Apache::lonmysql::get_dbh();
                    866:     $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
                    867:              $student_id);
                    868:     $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
                    869:              $student_id);
1.57      matthew   870:     #
                    871:     # Store away the data
                    872:     #
                    873:     my $starttime = Time::HiRes::time;
                    874:     my $elapsed = 0;
                    875:     my $rows_stored;
                    876:     my $store_parameters_command  = 'INSERT INTO '.$parameters_table.
1.60      matthew   877:         ' VALUES '."\n";
1.61      matthew   878:     my $num_parameters = 0;
1.57      matthew   879:     my $store_performance_command = 'INSERT INTO '.$performance_table.
1.60      matthew   880:         ' VALUES '."\n";
1.57      matthew   881:     return 'error' if (! defined($dbh));
                    882:     while (my ($current_symb,$param_hash) = each(%student_data)) {
                    883:         #
                    884:         # make sure the symb is set up properly
                    885:         my $symb_id = &get_symb_id($current_symb);
                    886:         #
                    887:         # Load data into the tables
1.63      matthew   888:         while (my ($parameter,$value) = each(%$param_hash)) {
1.57      matthew   889:             my $newstring;
1.63      matthew   890:             if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57      matthew   891:                 $newstring = "('".join("','",
                    892:                                        $symb_id,$student_id,
1.69      matthew   893:                                        $parameter)."',".
                    894:                                            $dbh->quote($value)."),\n";
1.61      matthew   895:                 $num_parameters ++;
1.57      matthew   896:                 if ($newstring !~ /''/) {
                    897:                     $store_parameters_command .= $newstring;
                    898:                     $rows_stored++;
                    899:                 }
                    900:             }
                    901:             next if ($parameter !~ /^resource\.(.*)\.solved$/);
                    902:             #
                    903:             my $part = $1;
                    904:             my $part_id = &get_part_id($part);
                    905:             next if (!defined($part_id));
                    906:             my $solved  = $value;
                    907:             my $tries   = $param_hash->{'resource.'.$part.'.tries'};
                    908:             my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
                    909:             my $award   = $param_hash->{'resource.'.$part.'.award'};
                    910:             my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
                    911:             my $timestamp = $param_hash->{'timestamp'};
1.60      matthew   912:             #
1.57      matthew   913:             $solved      = '' if (! defined($awarded));
                    914:             $tries       = '' if (! defined($tries));
                    915:             $awarded     = '' if (! defined($awarded));
                    916:             $award       = '' if (! defined($award));
                    917:             $awarddetail = '' if (! defined($awarddetail));
                    918:             $newstring = "('".join("','",$symb_id,$student_id,$part_id,
                    919:                                    $solved,$tries,$awarded,$award,
1.63      matthew   920:                                    $awarddetail,$timestamp)."'),\n";
1.57      matthew   921:             $store_performance_command .= $newstring;
                    922:             $rows_stored++;
                    923:         }
                    924:     }
                    925:     chop $store_parameters_command;
1.60      matthew   926:     chop $store_parameters_command;
                    927:     chop $store_performance_command;
1.57      matthew   928:     chop $store_performance_command;
                    929:     my $start = Time::HiRes::time;
1.61      matthew   930:     $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57      matthew   931:     if ($dbh->err()) {
                    932:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew   933:         &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.57      matthew   934:         $returnstatus = 'error: unable to insert parameters into database';
                    935:         return $returnstatus,\%student_data;
                    936:     }
                    937:     $dbh->do($store_performance_command);
                    938:     if ($dbh->err()) {
                    939:         &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61      matthew   940:         &Apache::lonnet::logthis('command = '.$store_performance_command);
1.57      matthew   941:         $returnstatus = 'error: unable to insert performance into database';
                    942:         return $returnstatus,\%student_data;
                    943:     }
                    944:     $elapsed += Time::HiRes::time - $start;
                    945:     #
                    946:     # Set the students update time
                    947:     &Apache::lonmysql::replace_row($updatetime_table,
                    948:                                    [$student,$time_of_retrieval]);
                    949:     return ($returnstatus,\%student_data);
                    950: }
                    951: 
                    952: ################################################
                    953: ################################################
                    954: 
                    955: =pod
                    956: 
                    957: =item &ensure_current_data()
                    958: 
                    959: Input: $sname, $sdom, $courseid
                    960: 
                    961: Output: $status, $data
                    962: 
                    963: This routine ensures the data for a given student is up to date.  It calls
                    964: &init_dbs() if the tables do not exist.  The $updatetime_table is queried
                    965: to determine the time of the last update.  If the students data is out of
                    966: date, &update_student_data() is called.  The return values from the call
                    967: to &update_student_data() are returned.
                    968: 
                    969: =cut
                    970: 
                    971: ################################################
                    972: ################################################
                    973: sub ensure_current_data {
                    974:     my ($sname,$sdom,$courseid) = @_;
                    975:     my $status = 'okay';   # return value
                    976:     #
1.61      matthew   977:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                    978:     # 
                    979:     # Clean out package variables
1.57      matthew   980:     &setup_table_names($courseid);
                    981:     #
                    982:     # if the tables do not exist, make them
                    983:     my @CurrentTable = &Apache::lonmysql::tables_in_db();
                    984:     my ($found_symb,$found_student,$found_part,$found_update,
                    985:         $found_performance,$found_parameters);
                    986:     foreach (@CurrentTable) {
                    987:         $found_symb        = 1 if ($_ eq $symb_table);
                    988:         $found_student     = 1 if ($_ eq $student_table);
                    989:         $found_part        = 1 if ($_ eq $part_table);
                    990:         $found_update      = 1 if ($_ eq $updatetime_table);
                    991:         $found_performance = 1 if ($_ eq $performance_table);
                    992:         $found_parameters  = 1 if ($_ eq $parameters_table);
                    993:     }
                    994:     if (!$found_symb        || !$found_update || 
                    995:         !$found_student     || !$found_part   ||
                    996:         !$found_performance || !$found_parameters) {
                    997:         if (&init_dbs($courseid)) {
                    998:             return 'error';
                    999:         }
                   1000:     }
                   1001:     #
                   1002:     # Get the update time for the user
                   1003:     my $updatetime = 0;
1.60      matthew  1004:     my $modifiedtime = &Apache::lonnet::GetFileTimestamp
                   1005:         ($sdom,$sname,$courseid.'.db',
                   1006:          $Apache::lonnet::perlvar{'lonUsersDir'});
1.57      matthew  1007:     #
                   1008:     my $student = $sname.':'.$sdom;
                   1009:     my @Result = &Apache::lonmysql::get_rows($updatetime_table,
                   1010:                                              "student ='$student'");
                   1011:     my $data = undef;
                   1012:     if (@Result) {
                   1013:         $updatetime = $Result[0]->[1];
                   1014:     }
                   1015:     if ($modifiedtime > $updatetime) {
                   1016:         ($status,$data) = &update_student_data($sname,$sdom,$courseid);
                   1017:     }
                   1018:     return ($status,$data);
                   1019: }
                   1020: 
                   1021: ################################################
                   1022: ################################################
                   1023: 
                   1024: =pod
                   1025: 
                   1026: =item &get_student_data_from_performance_cache()
                   1027: 
                   1028: Input: $sname, $sdom, $symb, $courseid
                   1029: 
                   1030: Output: hash reference containing the data for the given student.
                   1031: If $symb is undef, all the students data is returned.
                   1032: 
                   1033: This routine is the heart of the local caching system.  See the description
                   1034: of $performance_table, $symb_table, $student_table, and $part_table.  The
                   1035: main task is building the MySQL request.  The tables appear in the request
                   1036: in the order in which they should be parsed by MySQL.  When searching
                   1037: on a student the $student_table is used to locate the 'student_id'.  All
                   1038: rows in $performance_table which have a matching 'student_id' are returned,
                   1039: with data from $part_table and $symb_table which match the entries in
                   1040: $performance_table, 'part_id' and 'symb_id'.  When searching on a symb,
                   1041: the $symb_table is processed first, with matching rows grabbed from 
                   1042: $performance_table and filled in from $part_table and $student_table in
                   1043: that order.  
                   1044: 
                   1045: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite 
                   1046: interesting, especially if you play with the order the tables are listed.  
                   1047: 
                   1048: =cut
                   1049: 
                   1050: ################################################
                   1051: ################################################
                   1052: sub get_student_data_from_performance_cache {
                   1053:     my ($sname,$sdom,$symb,$courseid)=@_;
                   1054:     my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61      matthew  1055:     &setup_table_names($courseid);
1.57      matthew  1056:     #
                   1057:     # Return hash
                   1058:     my $studentdata;
                   1059:     #
                   1060:     my $dbh = &Apache::lonmysql::get_dbh();
                   1061:     my $request = "SELECT ".
                   1062:         "d.symb,c.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63      matthew  1063:             "a.timestamp ";
1.57      matthew  1064:     if (defined($student)) {
                   1065:         $request .= "FROM $student_table AS b ".
                   1066:             "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
                   1067:             "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
                   1068:             "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
                   1069:                 "WHERE student='$student'";
                   1070:         if (defined($symb) && $symb ne '') {
1.67      matthew  1071:             $request .= " AND d.symb=".$dbh->quote($symb);
1.57      matthew  1072:         }
                   1073:     } elsif (defined($symb) && $symb ne '') {
                   1074:         $request .= "FROM $symb_table as d ".
                   1075:             "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
                   1076:             "LEFT JOIN $part_table    AS c ON c.part_id = a.part_id ".
                   1077:             "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
                   1078:                 "WHERE symb='".$dbh->quote($symb)."'";
                   1079:     }
                   1080:     my $starttime = Time::HiRes::time;
                   1081:     my $rows_retrieved = 0;
                   1082:     my $sth = $dbh->prepare($request);
                   1083:     $sth->execute();
                   1084:     if ($sth->err()) {
                   1085:         &Apache::lonnet::logthis("Unable to execute MySQL request:");
                   1086:         &Apache::lonnet::logthis("\n".$request."\n");
                   1087:         &Apache::lonnet::logthis("error is:".$sth->errstr());
                   1088:         return undef;
                   1089:     }
                   1090:     foreach my $row (@{$sth->fetchall_arrayref}) {
                   1091:         $rows_retrieved++;
1.63      matthew  1092:         my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) = 
1.57      matthew  1093:             (@$row);
                   1094:         my $base = 'resource.'.$part;
                   1095:         $studentdata->{$symb}->{$base.'.solved'}  = $solved;
                   1096:         $studentdata->{$symb}->{$base.'.tries'}   = $tries;
                   1097:         $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
                   1098:         $studentdata->{$symb}->{$base.'.award'}   = $award;
                   1099:         $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
                   1100:         $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67      matthew  1101:     }
                   1102:     if (defined($symb) && $symb ne '') {
                   1103:         $studentdata = $studentdata->{$symb};
1.57      matthew  1104:     }
                   1105:     return $studentdata;
                   1106: }
                   1107: 
                   1108: ################################################
                   1109: ################################################
                   1110: 
                   1111: =pod
                   1112: 
                   1113: =item &get_current_state()
                   1114: 
                   1115: Input: $sname,$sdom,$symb,$courseid
                   1116: 
                   1117: Output: Described below
1.46      matthew  1118: 
1.47      matthew  1119: Retrieve the current status of a students performance.  $sname and
1.46      matthew  1120: $sdom are the only required parameters.  If $symb is undef the results
1.47      matthew  1121: of an &Apache::lonnet::currentdump() will be returned.  
1.46      matthew  1122: If $courseid is undef it will be retrieved from the environment.
                   1123: 
                   1124: The return structure is based on &Apache::lonnet::currentdump.  If
                   1125: $symb is unspecified, all the students data is returned in a hash of
                   1126: the form:
                   1127: ( 
                   1128:   symb1 => { param1 => value1, param2 => value2 ... },
                   1129:   symb2 => { param1 => value1, param2 => value2 ... },
                   1130: )
                   1131: 
                   1132: If $symb is specified, a hash of 
                   1133: (
                   1134:   param1 => value1, 
                   1135:   param2 => value2,
                   1136: )
                   1137: is returned.
                   1138: 
1.57      matthew  1139: If no data is found for $symb, or if the student has no performance data,
1.46      matthew  1140: an empty list is returned.
                   1141: 
                   1142: =cut
                   1143: 
                   1144: ################################################
                   1145: ################################################
                   1146: sub get_current_state {
1.47      matthew  1147:     my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
                   1148:     #
1.46      matthew  1149:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47      matthew  1150:     #
1.61      matthew  1151:     return () if (! defined($sname) || ! defined($sdom));
                   1152:     #
1.57      matthew  1153:     my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.47      matthew  1154:     #
1.57      matthew  1155:     if (defined($data)) {
1.68      matthew  1156:         if (defined($symb)) {
                   1157:             return %{$data->{$symb}};
                   1158:         } else {
                   1159:             return %$data;
                   1160:         }
1.57      matthew  1161:     } elsif ($status eq 'no data') {
                   1162:         return ();
                   1163:     } else {
                   1164:         if ($status ne 'okay' && $status ne '') {
                   1165:             &Apache::lonnet::logthis('status = '.$status);
1.47      matthew  1166:             return ();
                   1167:         }
1.57      matthew  1168:         my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
                   1169:                                                       $symb,$courseid);
                   1170:         return %$returnhash if (defined($returnhash));
1.46      matthew  1171:     }
1.57      matthew  1172:     return ();
1.61      matthew  1173: }
                   1174: 
                   1175: ################################################
                   1176: ################################################
                   1177: 
                   1178: =pod
                   1179: 
                   1180: =item &get_problem_statistics()
                   1181: 
                   1182: Gather data on a given problem.  The database is assumed to be 
                   1183: populated and all local caching variables are assumed to be set
                   1184: properly.  This means you need to call &ensure_current_data for
                   1185: the students you are concerned with prior to calling this routine.
                   1186: 
                   1187: Inputs: $students, $symb, $part, $courseid
                   1188: 
1.64      matthew  1189: =over 4
                   1190: 
                   1191: =item $students is an array of hash references.  
                   1192: Each hash must contain at least the 'username' and 'domain' of a student.
                   1193: 
                   1194: =item $symb is the symb for the problem.
                   1195: 
                   1196: =item $part is the part id you need statistics for
                   1197: 
                   1198: =item $courseid is the course id, of course!
                   1199: 
                   1200: =back
                   1201: 
1.66      matthew  1202: Outputs: See the code for up to date information.  A hash reference is
                   1203: returned.  The hash has the following keys defined:
1.64      matthew  1204: 
                   1205: =over 4
                   1206: 
1.66      matthew  1207: =item num_students The number of students attempting the problem
                   1208:       
                   1209: =item tries The total number of tries for the students
                   1210:       
                   1211: =item max_tries The maximum number of tries taken
                   1212:       
                   1213: =item mean_tries The average number of tries
                   1214:       
                   1215: =item num_solved The number of students able to solve the problem
                   1216:       
                   1217: =item num_override The number of students whose answer is 'correct_by_override'
                   1218:       
                   1219: =item deg_of_diff The degree of difficulty of the problem
                   1220:       
                   1221: =item std_tries The standard deviation of the number of tries
                   1222:       
                   1223: =item skew_tries The skew of the number of tries
1.64      matthew  1224: 
1.66      matthew  1225: =item per_wrong The number of students attempting the problem who were not
                   1226: able to answer it correctly.
1.64      matthew  1227: 
                   1228: =back
                   1229: 
1.61      matthew  1230: =cut
                   1231: 
                   1232: ################################################
                   1233: ################################################
                   1234: sub get_problem_statistics {
                   1235:     my ($students,$symb,$part,$courseid) = @_;
                   1236:     return if (! defined($symb) || ! defined($part));
                   1237:     $courseid = $ENV{'request.course.id'} if (! defined($courseid));
                   1238:     #
                   1239:     my $symb_id = &get_symb_id($symb);
                   1240:     my $part_id = &get_part_id($part);
                   1241:     my $stats_table = $courseid.'_problem_stats';
                   1242:     #
                   1243:     my $dbh = &Apache::lonmysql::get_dbh();
                   1244:     return undef if (! defined($dbh));
                   1245:     #
                   1246:     # A) Number of Students attempting problem
                   1247:     # B) Total number of tries of students attempting problem
                   1248:     # C) Mod (largest number of tries for solving the problem)
                   1249:     # D) Mean (average number of tries for solving the problem)
                   1250:     # E) Number of students to solve the problem
                   1251:     # F) Number of students to solve the problem by override
                   1252:     # G) Number of students unable to solve the problem
                   1253:     # H) Degree of difficulty : 1-(E+F)/B
                   1254:     # I) Standard deviation of number of tries
                   1255:     # J) Skew of tries: sqrt(sum(Xi-D)^3)/A
                   1256:     #
                   1257:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
                   1258:     my $request = 
                   1259:         'CREATE TEMPORARY TABLE '.$stats_table.
                   1260:             ' SELECT student_id,solved,award,tries FROM '.$performance_table.
                   1261:                 ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64      matthew  1262:     if (defined($students)) {
                   1263:         $request .= ' AND ('.
                   1264:             join(' OR ', map {'student_id='.
                   1265:                                   &get_student_id($_->{'username'},
                   1266:                                                   $_->{'domain'})
                   1267:                                   } @$students
                   1268:                  ).')';
                   1269:     }
1.61      matthew  1270: #    &Apache::lonnet::logthis($request);
                   1271:     $dbh->do($request);
                   1272:     my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
                   1273:         ($dbh,
                   1274:          'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) FROM '.
                   1275:          $stats_table);
                   1276:     my ($Solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1277:                                         $stats_table.
                   1278:                                         " WHERE solved='correct_by_student'");
                   1279:     my ($solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
                   1280:                                         $stats_table.
                   1281:                                         " WHERE solved='correct_by_override'");
                   1282:     $num    = 0 if (! defined($num));
                   1283:     $tries  = 0 if (! defined($tries));
                   1284:     $mod    = 0 if (! defined($mod));
                   1285:     $STD    = 0 if (! defined($STD));
                   1286:     $Solved = 0 if (! defined($Solved));
                   1287:     $solved = 0 if (! defined($solved));
                   1288:     #
                   1289:     my $DegOfDiff = 'nan';
1.66      matthew  1290:     $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61      matthew  1291: 
                   1292:     my $SKEW = 'nan';
1.66      matthew  1293:     my $wrongpercent = 0;
1.61      matthew  1294:     if ($num > 0) {
                   1295:         ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
                   1296:                                      'POWER(tries - '.$STD.',3)'.
                   1297:                                      '))/'.$num.' FROM '.$stats_table);
1.66      matthew  1298:         $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61      matthew  1299:     }
                   1300:     #
                   1301:     $dbh->do('DROP TABLE '.$stats_table);  # May return an error
1.66      matthew  1302:     return { num_students => $num,
                   1303:              tries        => $tries,
                   1304:              max_tries    => $mod,
                   1305:              mean_tries   => $mean,
                   1306:              std_tries    => $STD,
                   1307:              skew_tries   => $SKEW,
                   1308:              num_solved   => $Solved,
                   1309:              num_override => $solved,
                   1310:              per_wrong    => $wrongpercent,
                   1311:              deg_of_diff  => $DegOfDiff }
1.61      matthew  1312: }
                   1313: 
                   1314: sub execute_SQL_request {
                   1315:     my ($dbh,$request)=@_;
                   1316: #    &Apache::lonnet::logthis($request);
                   1317:     my $sth = $dbh->prepare($request);
                   1318:     $sth->execute();
                   1319:     my $row = $sth->fetchrow_arrayref();
                   1320:     if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
                   1321:         return @$row;
                   1322:     }
                   1323:     return ();
                   1324: }
                   1325: 
                   1326: 
                   1327: ################################################
                   1328: ################################################
                   1329: 
                   1330: =pod
                   1331: 
                   1332: =item &setup_table_names()
                   1333: 
                   1334: input: course id
                   1335: 
                   1336: output: none
                   1337: 
                   1338: Cleans up the package variables for local caching.
                   1339: 
                   1340: =cut
                   1341: 
                   1342: ################################################
                   1343: ################################################
                   1344: sub setup_table_names {
                   1345:     my ($courseid) = @_;
                   1346:     if (! defined($courseid)) {
                   1347:         $courseid = $ENV{'request.course.id'};
                   1348:     }
                   1349:     #
                   1350:     if (! defined($current_course) || $current_course ne $courseid) {
                   1351:         # Clear out variables
                   1352:         $have_read_part_table = 0;
                   1353:         undef(%ids_by_part);
                   1354:         undef(%parts_by_id);
                   1355:         $have_read_symb_table = 0;
                   1356:         undef(%ids_by_symb);
                   1357:         undef(%symbs_by_id);
                   1358:         $have_read_student_table = 0;
                   1359:         undef(%ids_by_student);
                   1360:         undef(%students_by_id);
                   1361:         #
                   1362:         $current_course = $courseid;
                   1363:     }
                   1364:     #
                   1365:     # Set up database names
                   1366:     my $base_id = $courseid;
                   1367:     $symb_table        = $base_id.'_'.'symb';
                   1368:     $part_table        = $base_id.'_'.'part';
                   1369:     $student_table     = $base_id.'_'.'student';
                   1370:     $updatetime_table  = $base_id.'_'.'updatetime';
                   1371:     $performance_table = $base_id.'_'.'performance';
                   1372:     $parameters_table  = $base_id.'_'.'parameters';
                   1373:     return;
1.3       stredwic 1374: }
1.1       stredwic 1375: 
1.35      matthew  1376: ################################################
                   1377: ################################################
                   1378: 
                   1379: =pod
                   1380: 
1.57      matthew  1381: =back
                   1382: 
                   1383: =item End of Local Data Caching Subroutines
                   1384: 
                   1385: =cut
                   1386: 
                   1387: ################################################
                   1388: ################################################
                   1389: 
                   1390: 
                   1391: }
                   1392: ################################################
                   1393: ################################################
                   1394: 
                   1395: =pod
                   1396: 
                   1397: =head3 Classlist Subroutines
                   1398: 
1.35      matthew  1399: =item &get_classlist();
                   1400: 
                   1401: Retrieve the classist of a given class or of the current class.  Student
                   1402: information is returned from the classlist.db file and, if needed,
                   1403: from the students environment.
                   1404: 
                   1405: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
                   1406: and course number, respectively).  Any omitted arguments will be taken 
                   1407: from the current environment ($ENV{'request.course.id'},
                   1408: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
                   1409: 
                   1410: Returns a reference to a hash which contains:
                   1411:  keys    '$sname:$sdom'
1.54      bowersj2 1412:  values  [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
                   1413: 
                   1414: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
                   1415: as indices into the returned list to future-proof clients against
                   1416: changes in the list order.
1.35      matthew  1417: 
                   1418: =cut
                   1419: 
                   1420: ################################################
                   1421: ################################################
1.54      bowersj2 1422: 
                   1423: sub CL_SDOM     { return 0; }
                   1424: sub CL_SNAME    { return 1; }
                   1425: sub CL_END      { return 2; }
                   1426: sub CL_START    { return 3; }
                   1427: sub CL_ID       { return 4; }
                   1428: sub CL_SECTION  { return 5; }
                   1429: sub CL_FULLNAME { return 6; }
                   1430: sub CL_STATUS   { return 7; }
1.35      matthew  1431: 
                   1432: sub get_classlist {
                   1433:     my ($cid,$cdom,$cnum) = @_;
                   1434:     $cid = $cid || $ENV{'request.course.id'};
                   1435:     $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
                   1436:     $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57      matthew  1437:     my $now = time;
1.35      matthew  1438:     #
                   1439:     my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
                   1440:     while (my ($student,$info) = each(%classlist)) {
1.60      matthew  1441:         if ($student =~ /^(con_lost|error|no_such_host)/i) {
                   1442:             &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
                   1443:             return undef;
                   1444:         }
1.35      matthew  1445:         my ($sname,$sdom) = split(/:/,$student);
                   1446:         my @Values = split(/:/,$info);
                   1447:         my ($end,$start,$id,$section,$fullname);
                   1448:         if (@Values > 2) {
                   1449:             ($end,$start,$id,$section,$fullname) = @Values;
                   1450:         } else { # We have to get the data ourselves
                   1451:             ($end,$start) = @Values;
1.37      matthew  1452:             $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35      matthew  1453:             my %info=&Apache::lonnet::get('environment',
                   1454:                                           ['firstname','middlename',
                   1455:                                            'lastname','generation','id'],
                   1456:                                           $sdom, $sname);
                   1457:             my ($tmp) = keys(%info);
                   1458:             if ($tmp =~/^(con_lost|error|no_such_host)/i) {
                   1459:                 $fullname = 'not available';
                   1460:                 $id = 'not available';
1.38      matthew  1461:                 &Apache::lonnet::logthis('unable to retrieve environment '.
                   1462:                                          'for '.$sname.':'.$sdom);
1.35      matthew  1463:             } else {
                   1464:                 $fullname = &ProcessFullName(@info{qw/lastname generation 
                   1465:                                                        firstname middlename/});
                   1466:                 $id = $info{'id'};
                   1467:             }
1.36      matthew  1468:             # Update the classlist with this students information
                   1469:             if ($fullname ne 'not available') {
                   1470:                 my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
                   1471:                 my $reply=&Apache::lonnet::cput('classlist',
                   1472:                                                 {$student => $enrolldata},
                   1473:                                                 $cdom,$cnum);
                   1474:                 if ($reply !~ /^(ok|delayed)/) {
                   1475:                     &Apache::lonnet::logthis('Unable to update classlist for '.
                   1476:                                              'student '.$sname.':'.$sdom.
                   1477:                                              ' error:'.$reply);
                   1478:                 }
                   1479:             }
1.35      matthew  1480:         }
                   1481:         my $status='Expired';
                   1482:         if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
                   1483:             $status='Active';
                   1484:         }
                   1485:         $classlist{$student} = 
                   1486:             [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
                   1487:     }
                   1488:     if (wantarray()) {
                   1489:         return (\%classlist,['domain','username','end','start','id',
                   1490:                              'section','fullname','status']);
                   1491:     } else {
                   1492:         return \%classlist;
                   1493:     }
                   1494: }
                   1495: 
1.1       stredwic 1496: # ----- END HELPER FUNCTIONS --------------------------------------------
                   1497: 
                   1498: 1;
                   1499: __END__
1.36      matthew  1500: 
1.35      matthew  1501: 

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