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

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

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