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

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

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