Annotation of loncom/interface/loncoursedata.pm, revision 1.92
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: #
1.92 ! matthew 3: # $Id: loncoursedata.pm,v 1.91 2003/09/29 19:48:17 matthew Exp $
1.1 stredwic 4: #
5: # Copyright Michigan State University Board of Trustees
6: #
7: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
8: #
9: # LON-CAPA is free software; you can redistribute it and/or modify
10: # it under the terms of the GNU General Public License as published by
11: # the Free Software Foundation; either version 2 of the License, or
12: # (at your option) any later version.
13: #
14: # LON-CAPA is distributed in the hope that it will be useful,
15: # but WITHOUT ANY WARRANTY; without even the implied warranty of
16: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: # GNU General Public License for more details.
18: #
19: # You should have received a copy of the GNU General Public License
20: # along with LON-CAPA; if not, write to the Free Software
21: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27: ###
28:
29: =pod
30:
31: =head1 NAME
32:
33: loncoursedata
34:
35: =head1 SYNOPSIS
36:
1.22 stredwic 37: Set of functions that download and process student and course information.
1.1 stredwic 38:
39: =head1 PACKAGES USED
40:
41: Apache::Constants qw(:common :http)
42: Apache::lonnet()
1.22 stredwic 43: Apache::lonhtmlcommon
1.1 stredwic 44: HTML::TokeParser
45: GDBM_File
46:
47: =cut
48:
49: package Apache::loncoursedata;
50:
51: use strict;
52: use Apache::Constants qw(:common :http);
53: use Apache::lonnet();
1.13 stredwic 54: use Apache::lonhtmlcommon;
1.57 matthew 55: use Time::HiRes;
56: use Apache::lonmysql;
1.1 stredwic 57: use HTML::TokeParser;
58: use GDBM_File;
59:
60: =pod
61:
62: =head1 DOWNLOAD INFORMATION
63:
1.22 stredwic 64: This section contains all the functions that get data from other servers
65: and/or itself.
1.1 stredwic 66:
67: =cut
68:
1.50 matthew 69: ####################################################
70: ####################################################
1.45 matthew 71:
72: =pod
73:
74: =item &get_sequence_assessment_data()
75:
76: AT THIS TIME THE USE OF THIS FUNCTION IS *NOT* RECOMMENDED
77:
78: Use lonnavmaps to build a data structure describing the order and
79: assessment contents of each sequence in the current course.
80:
81: The returned structure is a hash reference.
82:
1.61 matthew 83: { title => 'title',
84: symb => 'symb',
85: src => '/s/o/u/r/c/e',
1.45 matthew 86: type => (container|assessment),
1.50 matthew 87: num_assess => 2, # only for container
1.45 matthew 88: parts => [11,13,15], # only for assessment
1.50 matthew 89: response_ids => [12,14,16], # only for assessment
90: contents => [........] # only for container
1.45 matthew 91: }
92:
1.50 matthew 93: $hash->{'contents'} is a reference to an array of hashes of the same structure.
94:
95: Also returned are array references to the sequences and assessments contained
96: in the course.
1.49 matthew 97:
1.45 matthew 98:
99: =cut
100:
1.50 matthew 101: ####################################################
102: ####################################################
1.45 matthew 103: sub get_sequence_assessment_data {
104: my $fn=$ENV{'request.course.fn'};
105: ##
106: ## use navmaps
1.83 bowersj2 107: my $navmap = Apache::lonnavmaps::navmap->new();
1.45 matthew 108: if (!defined($navmap)) {
109: return 'Can not open Coursemap';
110: }
1.75 matthew 111: # We explicity grab the top level map because I am not sure we
112: # are pulling it from the iterator.
113: my $top_level_map = $navmap->getById('0.0');
114: #
1.45 matthew 115: my $iterator = $navmap->getIterator(undef, undef, undef, 1);
1.61 matthew 116: my $curRes = $iterator->next(); # Top level sequence
1.45 matthew 117: ##
118: ## Prime the pump
119: ##
120: ## We are going to loop until we run out of sequences/pages to explore for
121: ## resources. This means we have to start out with something to look
122: ## at.
1.76 matthew 123: my $title = $ENV{'course.'.$ENV{'request.course.id'}.'.description'};
1.75 matthew 124: my $symb = $top_level_map->symb();
125: my $src = $top_level_map->src();
126: my $randompick = $top_level_map->randompick();
1.45 matthew 127: #
1.49 matthew 128: my @Sequences;
129: my @Assessments;
1.45 matthew 130: my @Nested_Sequences = (); # Stack of sequences, keeps track of depth
131: my $top = { title => $title,
1.52 matthew 132: src => $src,
1.45 matthew 133: symb => $symb,
134: type => 'container',
135: num_assess => 0,
1.53 matthew 136: num_assess_parts => 0,
1.75 matthew 137: contents => [],
138: randompick => $randompick,
139: };
1.49 matthew 140: push (@Sequences,$top);
1.45 matthew 141: push (@Nested_Sequences, $top);
142: #
143: # We need to keep track of which sequences contain homework problems
144: #
1.78 matthew 145: my $previous_too;
1.52 matthew 146: my $previous;
1.45 matthew 147: while (scalar(@Nested_Sequences)) {
1.78 matthew 148: $previous_too = $previous;
1.50 matthew 149: $previous = $curRes;
1.45 matthew 150: $curRes = $iterator->next();
151: my $currentmap = $Nested_Sequences[-1]; # Last one on the stack
152: if ($curRes == $iterator->BEGIN_MAP()) {
1.78 matthew 153: if (! ref($previous)) {
154: $previous = $previous_too;
155: }
156: if (! ref($previous)) {
157: next;
158: }
1.45 matthew 159: # get the map itself, instead of BEGIN_MAP
1.51 matthew 160: $title = $previous->title();
1.84 matthew 161: $title =~ s/\:/\&\#058;/g;
1.51 matthew 162: $symb = $previous->symb();
163: $src = $previous->src();
1.81 matthew 164: # pick up the filename if there is no title available
165: if (! defined($title) || $title eq '') {
166: ($title) = ($src=~/\/([^\/]*)$/);
167: }
1.75 matthew 168: $randompick = $previous->randompick();
1.45 matthew 169: my $newmap = { title => $title,
170: src => $src,
171: symb => $symb,
172: type => 'container',
173: num_assess => 0,
1.75 matthew 174: randompick => $randompick,
1.45 matthew 175: contents => [],
176: };
177: push (@{$currentmap->{'contents'}},$newmap); # this is permanent
1.49 matthew 178: push (@Sequences,$newmap);
1.45 matthew 179: push (@Nested_Sequences, $newmap); # this is a stack
180: next;
181: }
182: if ($curRes == $iterator->END_MAP()) {
183: pop(@Nested_Sequences);
184: next;
185: }
186: next if (! ref($curRes));
1.50 matthew 187: next if (! $curRes->is_problem());# && !$curRes->randomout);
1.45 matthew 188: # Okay, from here on out we only deal with assessments
189: $title = $curRes->title();
1.84 matthew 190: $title =~ s/\:/\&\#058;/g;
1.45 matthew 191: $symb = $curRes->symb();
192: $src = $curRes->src();
193: my $parts = $curRes->parts();
1.87 matthew 194: my %partdata;
195: foreach my $part (@$parts) {
1.88 matthew 196: my @Responses = $curRes->responseType($part);
197: my @Ids = $curRes->responseIds($part);
198: $partdata{$part}->{'ResponseTypes'}= \@Responses;
199: $partdata{$part}->{'ResponseIds'} = \@Ids;
1.91 matthew 200: # Count how many responses of each type there are in this part
201: foreach (@Responses) {
202: $partdata{$part}->{$_}++;
203: }
1.87 matthew 204: }
1.45 matthew 205: my $assessment = { title => $title,
206: src => $src,
207: symb => $symb,
208: type => 'assessment',
1.53 matthew 209: parts => $parts,
210: num_parts => scalar(@$parts),
1.87 matthew 211: partdata => \%partdata,
1.45 matthew 212: };
1.49 matthew 213: push(@Assessments,$assessment);
1.45 matthew 214: push(@{$currentmap->{'contents'}},$assessment);
215: $currentmap->{'num_assess'}++;
1.53 matthew 216: $currentmap->{'num_assess_parts'}+= scalar(@$parts);
1.45 matthew 217: }
1.58 matthew 218: $navmap->untieHashes();
1.49 matthew 219: return ($top,\@Sequences,\@Assessments);
1.45 matthew 220: }
1.50 matthew 221:
1.4 stredwic 222: sub LoadDiscussion {
1.13 stredwic 223: my ($courseID)=@_;
1.5 minaeibi 224: my %Discuss=();
225: my %contrib=&Apache::lonnet::dump(
226: $courseID,
227: $ENV{'course.'.$courseID.'.domain'},
228: $ENV{'course.'.$courseID.'.num'});
229:
230: #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
231:
1.4 stredwic 232: foreach my $temp(keys %contrib) {
233: if ($temp=~/^version/) {
234: my $ver=$contrib{$temp};
235: my ($dummy,$prb)=split(':',$temp);
236: for (my $idx=1; $idx<=$ver; $idx++ ) {
237: my $name=$contrib{"$idx:$prb:sendername"};
1.5 minaeibi 238: $Discuss{"$name:$prb"}=$idx;
1.4 stredwic 239: }
240: }
241: }
1.5 minaeibi 242:
243: return \%Discuss;
1.1 stredwic 244: }
245:
1.71 matthew 246: ################################################
247: ################################################
248:
249: =pod
250:
251: =item &GetUserName(username,userdomain)
252:
253: Returns a hash with the following entries:
254: 'firstname', 'middlename', 'lastname', 'generation', and 'fullname'
255:
256: 'fullname' is the result of &Apache::loncoursedata::ProcessFullName.
257:
258: =cut
259:
260: ################################################
261: ################################################
262: sub GetUserName {
263: my ($username,$userdomain) = @_;
264: $username = $ENV{'user.name'} if (! defined($username));
265: $userdomain = $ENV{'user.domain'} if (! defined($username));
266: my %userenv = &Apache::lonnet::get('environment',
267: ['firstname','middlename','lastname','generation'],
268: $userdomain,$username);
269: $userenv{'fullname'} = &ProcessFullName($userenv{'lastname'},
270: $userenv{'generation'},
271: $userenv{'firstname'},
272: $userenv{'middlename'});
273: return %userenv;
274: }
275:
276: ################################################
277: ################################################
278:
1.1 stredwic 279: =pod
280:
281: =item &ProcessFullName()
282:
283: Takes lastname, generation, firstname, and middlename (or some partial
284: set of this data) and returns the full name version as a string. Format
285: is Lastname generation, firstname middlename or a subset of this.
286:
287: =cut
288:
1.71 matthew 289: ################################################
290: ################################################
1.1 stredwic 291: sub ProcessFullName {
292: my ($lastname, $generation, $firstname, $middlename)=@_;
293: my $Str = '';
294:
1.34 matthew 295: # Strip whitespace preceeding & following name components.
296: $lastname =~ s/(\s+$|^\s+)//g;
297: $generation =~ s/(\s+$|^\s+)//g;
298: $firstname =~ s/(\s+$|^\s+)//g;
299: $middlename =~ s/(\s+$|^\s+)//g;
300:
1.1 stredwic 301: if($lastname ne '') {
1.34 matthew 302: $Str .= $lastname;
303: $Str .= ' '.$generation if ($generation ne '');
304: $Str .= ',';
305: $Str .= ' '.$firstname if ($firstname ne '');
306: $Str .= ' '.$middlename if ($middlename ne '');
1.1 stredwic 307: } else {
1.34 matthew 308: $Str .= $firstname if ($firstname ne '');
309: $Str .= ' '.$middlename if ($middlename ne '');
310: $Str .= ' '.$generation if ($generation ne '');
1.1 stredwic 311: }
312:
313: return $Str;
314: }
315:
1.46 matthew 316: ################################################
317: ################################################
318:
319: =pod
320:
1.47 matthew 321: =item &make_into_hash($values);
322:
323: Returns a reference to a hash as described by $values. $values is
324: assumed to be the result of
1.57 matthew 325: join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47 matthew 326:
327: This is a helper function for get_current_state.
328:
329: =cut
330:
331: ################################################
332: ################################################
333: sub make_into_hash {
334: my $values = shift;
335: my %tmp = map { &Apache::lonnet::unescape($_); }
336: split(':',$values);
337: return \%tmp;
338: }
339:
340:
341: ################################################
342: ################################################
343:
344: =pod
345:
1.57 matthew 346: =head1 LOCAL DATA CACHING SUBROUTINES
347:
348: The local caching is done using MySQL. There is no fall-back implementation
349: if MySQL is not running.
350:
351: The programmers interface is to call &get_current_state() or some other
352: primary interface subroutine (described below). The internals of this
353: storage system are documented here.
354:
355: There are six tables used to store student performance data (the results of
356: a dumpcurrent). Each of these tables is created in MySQL with a name of
357: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate
358: for the table. The tables and their purposes are described below.
359:
360: Some notes before we get started.
361:
362: Each table must have a PRIMARY KEY, which is a column or set of columns which
363: will serve to uniquely identify a row of data. NULL is not allowed!
364:
365: INDEXes work best on integer data.
366:
367: JOIN is used to combine data from many tables into one output.
368:
369: lonmysql.pm is used for some of the interface, specifically the table creation
370: calls. The inserts are done in bulk by directly calling the database handler.
371: The SELECT ... JOIN statement used to retrieve the data does not have an
372: interface in lonmysql.pm and I shudder at the thought of writing one.
373:
374: =head3 Table Descriptions
375:
376: =over 4
377:
1.89 matthew 378: =item Tables used to store meta information
379:
380: The following tables hold data required to keep track of the current status
381: of a students data in the tables or to look up the students data in the tables.
382:
383: =over 4
384:
1.57 matthew 385: =item $symb_table
386:
387: The symb_table has two columns. The first is a 'symb_id' and the second
388: is the text name for the 'symb' (limited to 64k). The 'symb_id' is generated
389: automatically by MySQL so inserts should be done on this table with an
390: empty first element. This table has its PRIMARY KEY on the 'symb_id'.
391:
392: =item $part_table
393:
394: The part_table has two columns. The first is a 'part_id' and the second
395: is the text name for the 'part' (limited to 100 characters). The 'part_id' is
396: generated automatically by MySQL so inserts should be done on this table with
397: an empty first element. This table has its PRIMARY KEY on the 'part' (100
398: characters) and a KEY on 'part_id'.
399:
400: =item $student_table
401:
402: The student_table has two columns. The first is a 'student_id' and the second
403: is the text description of the 'student' (typically username:domain) (less
404: than 100 characters). The 'student_id' is automatically generated by MySQL.
405: The use of the name 'student_id' is loaded, I know, but this ID is used ONLY
406: internally to the MySQL database and is not the same as the students ID
407: (stored in the students environment). This table has its PRIMARY KEY on the
408: 'student' (100 characters).
409:
1.87 matthew 410: =item $studentdata_table
1.57 matthew 411:
1.89 matthew 412: The studentdata_table has four columns: 'student_id' (the unique id of
413: the student), 'updatetime' (the time the students data was last updated),
414: 'fullupdatetime' (the time the students full data was last updated),
415: 'section', and 'classification'( the students current classification).
416: This table has its PRIMARY KEY on 'student_id'.
417:
418: =back
419:
420: =item Tables used to store current status data
421:
422: The following tables store data only about the students current status on
423: a problem, meaning only the data related to the last attempt on a problem.
424:
425: =over 4
1.57 matthew 426:
427: =item $performance_table
428:
429: The performance_table has 9 columns. The first three are 'symb_id',
430: 'student_id', and 'part_id'. These comprise the PRIMARY KEY for this table
431: and are directly related to the $symb_table, $student_table, and $part_table
432: described above. MySQL does better indexing on numeric items than text,
433: so we use these three "index tables". The remaining columns are
434: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
435: These are either the MySQL type TINYTEXT or various integers ('tries' and
436: 'timestamp'). This table has KEYs of 'student_id' and 'symb_id'.
437: For use of this table, see the functions described below.
438:
439: =item $parameters_table
440:
441: The parameters_table holds the data that does not fit neatly into the
442: performance_table. The parameters table has four columns: 'symb_id',
443: 'student_id', 'parameter', and 'value'. 'symb_id', 'student_id', and
444: 'parameter' comprise the PRIMARY KEY for this table. 'parameter' is
445: limited to 255 characters. 'value' is limited to 64k characters.
446:
447: =back
448:
1.89 matthew 449: =item Tables used for storing historic data
450:
451: The following tables are used to store almost all of the transactions a student
452: has made on a homework problem. See loncapa/docs/homework/datastorage for
453: specific information about each of the parameters stored.
454:
455: =over 4
456:
457: =item $fulldump_response_table
458:
459: The response table holds data (documented in loncapa/docs/homework/datastorage)
460: associated with a particular response id which is stored when a student
461: attempts a problem. The following are the columns of the table, in order:
462: 'symb_id','part_id','response_id','student_id','transaction','tries',
463: 'awarddetail', 'awarded','response_specific' (data particular to the response
464: type), 'response_specific_value', and 'submission (the text of the students
465: submission). The primary key is based on the first five columns listed above.
466:
467: =item $fulldump_part_table
468:
469: The part table holds data (documented in loncapa/docs/homework/datastorage)
470: associated with a particular part id which is stored when a student attempts
471: a problem. The following are the columns of the table, in order:
472: 'symb_id','part_id','student_id','transaction','tries','award','awarded',
473: and 'previous'. The primary key is based on the first five columns listed
474: above.
475:
476: =item $fulldump_timestamp_table
477:
478: The timestamp table holds the timestamps of the transactions which are
479: stored in $fulldump_response_table and $fulldump_part_table. This data is
480: about both the response and part data. Columns: 'symb_id','student_id',
481: 'transaction', and 'timestamp'.
482: The primary key is based on the first 3 columns.
483:
484: =back
485:
486: =back
487:
1.57 matthew 488: =head3 Important Subroutines
489:
490: Here is a brief overview of the subroutines which are likely to be of
491: interest:
492:
493: =over 4
494:
495: =item &get_current_state(): programmers interface.
496:
497: =item &init_dbs(): table creation
498:
499: =item &update_student_data(): data storage calls
500:
501: =item &get_student_data_from_performance_cache(): data retrieval
502:
503: =back
504:
505: =head3 Main Documentation
506:
507: =over 4
508:
509: =cut
510:
511: ################################################
512: ################################################
513:
514: ################################################
515: ################################################
1.89 matthew 516: { # Begin scope of table identifiers
1.57 matthew 517:
518: my $current_course ='';
519: my $symb_table;
520: my $part_table;
521: my $student_table;
1.87 matthew 522: my $studentdata_table;
1.57 matthew 523: my $performance_table;
524: my $parameters_table;
1.89 matthew 525: my $fulldump_response_table;
526: my $fulldump_part_table;
527: my $fulldump_timestamp_table;
1.57 matthew 528:
1.89 matthew 529: my @Tables;
1.57 matthew 530: ################################################
531: ################################################
532:
533: =pod
534:
535: =item &init_dbs()
536:
537: Input: course id
538:
539: Output: 0 on success, positive integer on error
540:
541: This routine issues the calls to lonmysql to create the tables used to
542: store student data.
543:
544: =cut
545:
546: ################################################
547: ################################################
548: sub init_dbs {
549: my $courseid = shift;
550: &setup_table_names($courseid);
551: #
1.73 matthew 552: # Drop any of the existing tables
1.89 matthew 553: foreach my $table (@Tables) {
1.73 matthew 554: &Apache::lonmysql::drop_table($table);
555: }
556: #
1.57 matthew 557: # Note - changes to this table must be reflected in the code that
558: # stores the data (calls &Apache::lonmysql::store_row with this table
559: # id
560: my $symb_table_def = {
561: id => $symb_table,
562: permanent => 'no',
563: columns => [{ name => 'symb_id',
564: type => 'MEDIUMINT UNSIGNED',
565: restrictions => 'NOT NULL',
566: auto_inc => 'yes', },
567: { name => 'symb',
568: type => 'MEDIUMTEXT',
569: restrictions => 'NOT NULL'},
570: ],
571: 'PRIMARY KEY' => ['symb_id'],
572: };
573: #
574: my $part_table_def = {
575: id => $part_table,
576: permanent => 'no',
577: columns => [{ name => 'part_id',
578: type => 'MEDIUMINT UNSIGNED',
579: restrictions => 'NOT NULL',
580: auto_inc => 'yes', },
581: { name => 'part',
582: type => 'VARCHAR(100)',
583: restrictions => 'NOT NULL'},
584: ],
585: 'PRIMARY KEY' => ['part (100)'],
586: 'KEY' => [{ columns => ['part_id']},],
587: };
588: #
589: my $student_table_def = {
590: id => $student_table,
591: permanent => 'no',
592: columns => [{ name => 'student_id',
593: type => 'MEDIUMINT UNSIGNED',
594: restrictions => 'NOT NULL',
595: auto_inc => 'yes', },
596: { name => 'student',
597: type => 'VARCHAR(100)',
598: restrictions => 'NOT NULL'},
1.85 matthew 599: { name => 'classification',
600: type => 'varchar(100)', },
1.57 matthew 601: ],
602: 'PRIMARY KEY' => ['student (100)'],
603: 'KEY' => [{ columns => ['student_id']},],
604: };
605: #
1.87 matthew 606: my $studentdata_table_def = {
607: id => $studentdata_table,
1.57 matthew 608: permanent => 'no',
1.87 matthew 609: columns => [{ name => 'student_id',
610: type => 'MEDIUMINT UNSIGNED',
1.57 matthew 611: restrictions => 'NOT NULL UNIQUE',},
612: { name => 'updatetime',
1.89 matthew 613: type => 'INT UNSIGNED'},
614: { name => 'fullupdatetime',
615: type => 'INT UNSIGNED'},
1.87 matthew 616: { name => 'section',
617: type => 'VARCHAR(100)'},
618: { name => 'classification',
619: type => 'VARCHAR(100)', },
1.57 matthew 620: ],
1.87 matthew 621: 'PRIMARY KEY' => ['student_id'],
1.57 matthew 622: };
623: #
624: my $performance_table_def = {
625: id => $performance_table,
626: permanent => 'no',
627: columns => [{ name => 'symb_id',
628: type => 'MEDIUMINT UNSIGNED',
629: restrictions => 'NOT NULL' },
630: { name => 'student_id',
631: type => 'MEDIUMINT UNSIGNED',
632: restrictions => 'NOT NULL' },
633: { name => 'part_id',
634: type => 'MEDIUMINT UNSIGNED',
635: restrictions => 'NOT NULL' },
1.73 matthew 636: { name => 'part',
637: type => 'VARCHAR(100)',
638: restrictions => 'NOT NULL'},
1.57 matthew 639: { name => 'solved',
640: type => 'TINYTEXT' },
641: { name => 'tries',
642: type => 'SMALLINT UNSIGNED' },
643: { name => 'awarded',
644: type => 'TINYTEXT' },
645: { name => 'award',
646: type => 'TINYTEXT' },
647: { name => 'awarddetail',
648: type => 'TINYTEXT' },
649: { name => 'timestamp',
650: type => 'INT UNSIGNED'},
651: ],
652: 'PRIMARY KEY' => ['symb_id','student_id','part_id'],
653: 'KEY' => [{ columns=>['student_id'] },
654: { columns=>['symb_id'] },],
655: };
656: #
1.89 matthew 657: my $fulldump_part_table_def = {
658: id => $fulldump_part_table,
659: permanent => 'no',
660: columns => [
661: { name => 'symb_id',
662: type => 'MEDIUMINT UNSIGNED',
663: restrictions => 'NOT NULL' },
664: { name => 'part_id',
665: type => 'MEDIUMINT UNSIGNED',
666: restrictions => 'NOT NULL' },
667: { name => 'student_id',
668: type => 'MEDIUMINT UNSIGNED',
669: restrictions => 'NOT NULL' },
670: { name => 'transaction',
671: type => 'MEDIUMINT UNSIGNED',
672: restrictions => 'NOT NULL' },
673: { name => 'tries',
674: type => 'SMALLINT UNSIGNED',
675: restrictions => 'NOT NULL' },
676: { name => 'award',
677: type => 'TINYTEXT' },
678: { name => 'awarded',
679: type => 'TINYTEXT' },
680: { name => 'previous',
681: type => 'SMALLINT UNSIGNED' },
682: # { name => 'regrader',
683: # type => 'TINYTEXT' },
684: # { name => 'afterduedate',
685: # type => 'TINYTEXT' },
686: ],
687: 'PRIMARY KEY' => ['symb_id','part_id','student_id','transaction'],
688: 'KEY' => [
689: { columns=>['symb_id'] },
690: { columns=>['part_id'] },
691: { columns=>['student_id'] },
692: ],
693: };
694: #
695: my $fulldump_response_table_def = {
696: id => $fulldump_response_table,
697: permanent => 'no',
698: columns => [
699: { name => 'symb_id',
700: type => 'MEDIUMINT UNSIGNED',
701: restrictions => 'NOT NULL' },
702: { name => 'part_id',
703: type => 'MEDIUMINT UNSIGNED',
704: restrictions => 'NOT NULL' },
705: { name => 'response_id',
706: type => 'MEDIUMINT UNSIGNED',
707: restrictions => 'NOT NULL' },
708: { name => 'student_id',
709: type => 'MEDIUMINT UNSIGNED',
710: restrictions => 'NOT NULL' },
711: { name => 'transaction',
712: type => 'MEDIUMINT UNSIGNED',
713: restrictions => 'NOT NULL' },
714: { name => 'tries',
715: type => 'SMALLINT UNSIGNED',
716: restrictions => 'NOT NULL' },
717: { name => 'awarddetail',
718: type => 'TINYTEXT' },
719: { name => 'awarded',
720: type => 'TINYTEXT' },
721: # { name => 'message',
722: # type => 'CHAR' },
723: { name => 'response_specific',
724: type => 'TINYTEXT' },
725: { name => 'response_specific_value',
726: type => 'TINYTEXT' },
727: { name => 'submission',
728: type => 'TEXT'},
729: ],
730: 'PRIMARY KEY' => ['symb_id','part_id','response_id','student_id',
731: 'transaction'],
732: 'KEY' => [
733: { columns=>['symb_id'] },
734: { columns=>['part_id','response_id'] },
735: { columns=>['student_id'] },
736: ],
737: };
738: my $fulldump_timestamp_table_def = {
739: id => $fulldump_timestamp_table,
740: permanent => 'no',
741: columns => [
742: { name => 'symb_id',
743: type => 'MEDIUMINT UNSIGNED',
744: restrictions => 'NOT NULL' },
745: { name => 'student_id',
746: type => 'MEDIUMINT UNSIGNED',
747: restrictions => 'NOT NULL' },
748: { name => 'transaction',
749: type => 'MEDIUMINT UNSIGNED',
750: restrictions => 'NOT NULL' },
751: { name => 'timestamp',
752: type => 'INT UNSIGNED'},
753: ],
754: 'PRIMARY KEY' => ['symb_id','student_id','transaction'],
755: 'KEY' => [
756: { columns=>['symb_id'] },
757: { columns=>['student_id'] },
758: { columns=>['transaction'] },
759: ],
760: };
761:
762: #
1.57 matthew 763: my $parameters_table_def = {
764: id => $parameters_table,
765: permanent => 'no',
766: columns => [{ name => 'symb_id',
767: type => 'MEDIUMINT UNSIGNED',
768: restrictions => 'NOT NULL' },
769: { name => 'student_id',
770: type => 'MEDIUMINT UNSIGNED',
771: restrictions => 'NOT NULL' },
772: { name => 'parameter',
773: type => 'TINYTEXT',
774: restrictions => 'NOT NULL' },
775: { name => 'value',
776: type => 'MEDIUMTEXT' },
777: ],
778: 'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
779: };
780: #
781: # Create the tables
782: my $tableid;
783: $tableid = &Apache::lonmysql::create_table($symb_table_def);
784: if (! defined($tableid)) {
785: &Apache::lonnet::logthis("error creating symb_table: ".
786: &Apache::lonmysql::get_error());
787: return 1;
788: }
789: #
790: $tableid = &Apache::lonmysql::create_table($part_table_def);
791: if (! defined($tableid)) {
792: &Apache::lonnet::logthis("error creating part_table: ".
793: &Apache::lonmysql::get_error());
794: return 2;
795: }
796: #
797: $tableid = &Apache::lonmysql::create_table($student_table_def);
798: if (! defined($tableid)) {
799: &Apache::lonnet::logthis("error creating student_table: ".
800: &Apache::lonmysql::get_error());
801: return 3;
802: }
803: #
1.87 matthew 804: $tableid = &Apache::lonmysql::create_table($studentdata_table_def);
1.57 matthew 805: if (! defined($tableid)) {
1.87 matthew 806: &Apache::lonnet::logthis("error creating studentdata_table: ".
1.57 matthew 807: &Apache::lonmysql::get_error());
808: return 4;
809: }
810: #
811: $tableid = &Apache::lonmysql::create_table($performance_table_def);
812: if (! defined($tableid)) {
813: &Apache::lonnet::logthis("error creating preformance_table: ".
814: &Apache::lonmysql::get_error());
815: return 5;
816: }
817: #
818: $tableid = &Apache::lonmysql::create_table($parameters_table_def);
819: if (! defined($tableid)) {
820: &Apache::lonnet::logthis("error creating parameters_table: ".
821: &Apache::lonmysql::get_error());
822: return 6;
823: }
1.89 matthew 824: #
825: $tableid = &Apache::lonmysql::create_table($fulldump_part_table_def);
826: if (! defined($tableid)) {
827: &Apache::lonnet::logthis("error creating fulldump_part_table: ".
828: &Apache::lonmysql::get_error());
829: return 7;
830: }
831: #
832: $tableid = &Apache::lonmysql::create_table($fulldump_response_table_def);
833: if (! defined($tableid)) {
834: &Apache::lonnet::logthis("error creating fulldump_response_table: ".
835: &Apache::lonmysql::get_error());
836: return 8;
837: }
838: $tableid = &Apache::lonmysql::create_table($fulldump_timestamp_table_def);
839: if (! defined($tableid)) {
840: &Apache::lonnet::logthis("error creating fulldump_timestamp_table: ".
841: &Apache::lonmysql::get_error());
842: return 9;
843: }
1.57 matthew 844: return 0;
1.70 matthew 845: }
846:
847: ################################################
848: ################################################
849:
850: =pod
851:
852: =item &delete_caches()
853:
854: =cut
855:
856: ################################################
857: ################################################
858: sub delete_caches {
859: my $courseid = shift;
860: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
861: #
862: &setup_table_names($courseid);
863: #
864: my $dbh = &Apache::lonmysql::get_dbh();
1.89 matthew 865: foreach my $table (@Tables) {
1.70 matthew 866: my $command = 'DROP TABLE '.$table.';';
867: $dbh->do($command);
868: if ($dbh->err) {
869: &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
870: }
871: }
872: return;
1.57 matthew 873: }
874:
875: ################################################
876: ################################################
877:
878: =pod
879:
880: =item &get_part_id()
881:
882: Get the MySQL id of a problem part string.
883:
884: Input: $part
885:
886: Output: undef on error, integer $part_id on success.
887:
888: =item &get_part()
889:
890: Get the string describing a part from the MySQL id of the problem part.
891:
892: Input: $part_id
893:
894: Output: undef on error, $part string on success.
895:
896: =cut
897:
898: ################################################
899: ################################################
900:
1.61 matthew 901: my $have_read_part_table = 0;
1.57 matthew 902: my %ids_by_part;
903: my %parts_by_id;
904:
905: sub get_part_id {
906: my ($part) = @_;
1.61 matthew 907: $part = 0 if (! defined($part));
908: if (! $have_read_part_table) {
909: my @Result = &Apache::lonmysql::get_rows($part_table);
910: foreach (@Result) {
911: $ids_by_part{$_->[1]}=$_->[0];
912: }
913: $have_read_part_table = 1;
914: }
1.57 matthew 915: if (! exists($ids_by_part{$part})) {
916: &Apache::lonmysql::store_row($part_table,[undef,$part]);
917: undef(%ids_by_part);
918: my @Result = &Apache::lonmysql::get_rows($part_table);
919: foreach (@Result) {
920: $ids_by_part{$_->[1]}=$_->[0];
921: }
922: }
923: return $ids_by_part{$part} if (exists($ids_by_part{$part}));
924: return undef; # error
925: }
926:
927: sub get_part {
928: my ($part_id) = @_;
929: if (! exists($parts_by_id{$part_id}) ||
930: ! defined($parts_by_id{$part_id}) ||
931: $parts_by_id{$part_id} eq '') {
932: my @Result = &Apache::lonmysql::get_rows($part_table);
933: foreach (@Result) {
934: $parts_by_id{$_->[0]}=$_->[1];
935: }
936: }
937: return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
938: return undef; # error
939: }
940:
941: ################################################
942: ################################################
943:
944: =pod
945:
946: =item &get_symb_id()
947:
948: Get the MySQL id of a symb.
949:
950: Input: $symb
951:
952: Output: undef on error, integer $symb_id on success.
953:
954: =item &get_symb()
955:
956: Get the symb associated with a MySQL symb_id.
957:
958: Input: $symb_id
959:
960: Output: undef on error, $symb on success.
961:
962: =cut
963:
964: ################################################
965: ################################################
966:
1.61 matthew 967: my $have_read_symb_table = 0;
1.57 matthew 968: my %ids_by_symb;
969: my %symbs_by_id;
970:
971: sub get_symb_id {
972: my ($symb) = @_;
1.61 matthew 973: if (! $have_read_symb_table) {
974: my @Result = &Apache::lonmysql::get_rows($symb_table);
975: foreach (@Result) {
976: $ids_by_symb{$_->[1]}=$_->[0];
977: }
978: $have_read_symb_table = 1;
979: }
1.57 matthew 980: if (! exists($ids_by_symb{$symb})) {
981: &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
982: undef(%ids_by_symb);
983: my @Result = &Apache::lonmysql::get_rows($symb_table);
984: foreach (@Result) {
985: $ids_by_symb{$_->[1]}=$_->[0];
986: }
987: }
988: return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
989: return undef; # error
990: }
991:
992: sub get_symb {
993: my ($symb_id) = @_;
994: if (! exists($symbs_by_id{$symb_id}) ||
995: ! defined($symbs_by_id{$symb_id}) ||
996: $symbs_by_id{$symb_id} eq '') {
997: my @Result = &Apache::lonmysql::get_rows($symb_table);
998: foreach (@Result) {
999: $symbs_by_id{$_->[0]}=$_->[1];
1000: }
1001: }
1002: return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
1003: return undef; # error
1004: }
1005:
1006: ################################################
1007: ################################################
1008:
1009: =pod
1010:
1011: =item &get_student_id()
1012:
1013: Get the MySQL id of a student.
1014:
1015: Input: $sname, $dom
1016:
1017: Output: undef on error, integer $student_id on success.
1018:
1019: =item &get_student()
1020:
1021: Get student username:domain associated with the MySQL student_id.
1022:
1023: Input: $student_id
1024:
1025: Output: undef on error, string $student (username:domain) on success.
1026:
1027: =cut
1028:
1029: ################################################
1030: ################################################
1031:
1.61 matthew 1032: my $have_read_student_table = 0;
1.57 matthew 1033: my %ids_by_student;
1034: my %students_by_id;
1035:
1036: sub get_student_id {
1037: my ($sname,$sdom) = @_;
1038: my $student = $sname.':'.$sdom;
1.61 matthew 1039: if (! $have_read_student_table) {
1040: my @Result = &Apache::lonmysql::get_rows($student_table);
1041: foreach (@Result) {
1042: $ids_by_student{$_->[1]}=$_->[0];
1043: }
1044: $have_read_student_table = 1;
1045: }
1.57 matthew 1046: if (! exists($ids_by_student{$student})) {
1.85 matthew 1047: &Apache::lonmysql::store_row($student_table,[undef,$student,undef]);
1.57 matthew 1048: undef(%ids_by_student);
1049: my @Result = &Apache::lonmysql::get_rows($student_table);
1050: foreach (@Result) {
1051: $ids_by_student{$_->[1]}=$_->[0];
1052: }
1053: }
1054: return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
1055: return undef; # error
1056: }
1057:
1058: sub get_student {
1059: my ($student_id) = @_;
1060: if (! exists($students_by_id{$student_id}) ||
1061: ! defined($students_by_id{$student_id}) ||
1062: $students_by_id{$student_id} eq '') {
1063: my @Result = &Apache::lonmysql::get_rows($student_table);
1064: foreach (@Result) {
1065: $students_by_id{$_->[0]}=$_->[1];
1066: }
1067: }
1068: return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
1069: return undef; # error
1070: }
1071:
1072: ################################################
1073: ################################################
1074:
1075: =pod
1076:
1.89 matthew 1077: =item &update_full_student_data($sname,$sdom,$courseid)
1078:
1079: Does a lonnet::dump on a student to populate the courses tables.
1080:
1081: Input: $sname, $sdom, $courseid
1082:
1083: Output: $returnstatus
1084:
1085: $returnstatus is a string describing any errors that occured. 'okay' is the
1086: default.
1087:
1088: This subroutine loads a students data using lonnet::dump and inserts
1089: it into the MySQL database. The inserts are done on three tables,
1090: $fulldump_response_table, $fulldump_part_table, and $fulldump_timestamp_table.
1091: The INSERT calls are made directly by this subroutine, not through lonmysql
1092: because we do a 'bulk'insert which takes advantage of MySQLs non-SQL
1093: compliant INSERT command to insert multiple rows at a time.
1094: If anything has gone wrong during this process, $returnstatus is updated with
1095: a description of the error.
1096:
1097: Once the "fulldump" tables are updated, the tables used for chart and
1098: spreadsheet (which hold only the current state of the student on their
1099: homework, not historical data) are updated. If all updates have occured
1100: successfully, the studentdata table is updated to reflect the time of the
1101: update.
1102:
1103: Notice we do not insert the data and immediately query it. This means it
1104: is possible for there to be data returned this first time that is not
1105: available the second time. CYA.
1106:
1107: =cut
1108:
1109: ################################################
1110: ################################################
1111: sub update_full_student_data {
1112: my ($sname,$sdom,$courseid) = @_;
1113: #
1114: # Set up database names
1115: &setup_table_names($courseid);
1116: #
1117: my $student_id = &get_student_id($sname,$sdom);
1118: my $student = $sname.':'.$sdom;
1119: #
1120: my $returnstatus = 'okay';
1121: #
1122: # Download students data
1123: my $time_of_retrieval = time;
1124: my @tmp = &Apache::lonnet::dump($courseid,$sdom,$sname);
1125: if (@tmp && $tmp[0] =~ /^error/) {
1126: $returnstatus = 'error retrieving full student data';
1127: return $returnstatus;
1128: } elsif (! @tmp) {
1129: $returnstatus = 'okay: no student data';
1130: return $returnstatus;
1131: }
1132: my %studentdata = @tmp;
1133: #
1134: # Get database handle and clean out the tables
1135: my $dbh = &Apache::lonmysql::get_dbh();
1136: $dbh->do('DELETE FROM '.$fulldump_response_table.' WHERE student_id='.
1137: $student_id);
1138: $dbh->do('DELETE FROM '.$fulldump_part_table.' WHERE student_id='.
1139: $student_id);
1140: $dbh->do('DELETE FROM '.$fulldump_timestamp_table.' WHERE student_id='.
1141: $student_id);
1142: #
1143: # Parse and store the data into a form we can handle
1144: my $partdata;
1145: my $respdata;
1146: while (my ($key,$value) = each(%studentdata)) {
1147: next if ($key =~ /^(\d+):(resource$|subnum$|keys:)/);
1148: my ($transaction,$symb,$parameter) = split(':',$key);
1149: my $symb_id = &get_symb_id($symb);
1150: if ($parameter eq 'timestamp') {
1151: # We can deal with 'timestamp' right away
1152: my @timestamp_storage = ($symb_id,$student_id,
1153: $transaction,$value);
1154: my $store_command = 'INSERT INTO '.$fulldump_timestamp_table.
1155: " VALUES ('".join("','",@timestamp_storage)."');";
1156: $dbh->do($store_command);
1157: if ($dbh->err()) {
1158: &Apache::lonnet::logthis('unable to execute '.$store_command);
1159: &Apache::lonnet::logthis($dbh->errstr());
1160: }
1161: next;
1162: } elsif ($parameter eq 'version') {
1163: next;
1.90 matthew 1164: } elsif ($parameter =~ /^resource\.(.*)\.(tries|
1165: award|
1166: awarded|
1167: previous|
1168: solved|
1169: awarddetail|
1170: submission|
1171: submissiongrading|
1172: molecule)\s*$/x){
1.89 matthew 1173: # we do not have enough information to store an
1174: # entire row, so we save it up until later.
1175: my ($part_and_resp_id,$field) = ($1,$2);
1176: my ($part,$part_id,$resp,$resp_id);
1177: if ($part_and_resp_id =~ /\./) {
1178: ($part,$resp) = split(/\./,$part_and_resp_id);
1179: $part_id = &get_part_id($part);
1180: $resp_id = &get_part_id($resp);
1181: } else {
1182: $part_id = &get_part_id($part_and_resp_id);
1183: }
1.90 matthew 1184: # Deal with part specific data
1.89 matthew 1185: if ($field =~ /^(tries|award|awarded|previous)$/) {
1186: $partdata->{$symb_id}->{$part_id}->{$transaction}->{$field}=$value;
1187: }
1.90 matthew 1188: # deal with response specific data
1.89 matthew 1189: if (defined($resp_id) &&
1.90 matthew 1190: $field =~ /^(tries|
1191: awarddetail|
1192: awarded|
1193: submission|
1194: submissiongrading|
1195: molecule)$/x) {
1.89 matthew 1196: if ($field eq 'submission') {
1197: # We have to be careful with user supplied input.
1198: # most of the time we are okay because it is escaped.
1199: # However, there is one wrinkle: submissions which end in
1200: # and odd number of '\' cause insert errors to occur.
1201: # Best trap this somehow...
1202: my ($offensive_string) = ($value =~ /(\\+)$/);
1203: if (length($offensive_string) % 2) {
1204: $value =~ s/\\$/\\\\/;
1205: }
1206: }
1.90 matthew 1207: if ($field eq 'submissiongrading' ||
1208: $field eq 'molecule') {
1209: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific'}=$field;
1210: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific_value'}=$value;
1211: } else {
1212: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{$field}=$value;
1213: }
1.89 matthew 1214: }
1215: }
1216: }
1217: ##
1218: ## Store the part data
1219: my $store_command = 'INSERT INTO '.$fulldump_part_table.
1220: ' VALUES '."\n";
1221: my $store_rows = 0;
1222: while (my ($symb_id,$hash1) = each (%$partdata)) {
1223: while (my ($part_id,$hash2) = each (%$hash1)) {
1224: while (my ($transaction,$data) = each (%$hash2)) {
1225: $store_command .= "('".join("','",$symb_id,$part_id,
1226: $student_id,
1227: $transaction,
1228: $data->{'tries'},
1229: $data->{'award'},
1230: $data->{'awarded'},
1231: $data->{'previous'})."'),";
1232: $store_rows++;
1233: }
1234: }
1235: }
1236: if ($store_rows) {
1237: chop($store_command);
1238: $dbh->do($store_command);
1239: if ($dbh->err) {
1240: $returnstatus = 'error storing part data';
1241: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1242: &Apache::lonnet::logthis("While attempting\n".$store_command);
1243: }
1244: }
1245: ##
1246: ## Store the response data
1247: $store_command = 'INSERT INTO '.$fulldump_response_table.
1248: ' VALUES '."\n";
1249: $store_rows = 0;
1250: while (my ($symb_id,$hash1) = each (%$respdata)) {
1251: while (my ($part_id,$hash2) = each (%$hash1)) {
1252: while (my ($resp_id,$hash3) = each (%$hash2)) {
1253: while (my ($transaction,$data) = each (%$hash3)) {
1254: $store_command .= "('".join("','",$symb_id,$part_id,
1255: $resp_id,$student_id,
1256: $transaction,
1257: $data->{'tries'},
1258: $data->{'awarddetail'},
1259: $data->{'awarded'},
1.90 matthew 1260: $data->{'response_specific'},
1261: $data->{'response_specific_value'},
1.89 matthew 1262: $data->{'submission'})."'),";
1263: $store_rows++;
1264: }
1265: }
1266: }
1267: }
1268: if ($store_rows) {
1269: chop($store_command);
1270: $dbh->do($store_command);
1271: if ($dbh->err) {
1272: $returnstatus = 'error storing response data';
1273: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1274: &Apache::lonnet::logthis("While attempting\n".$store_command);
1275: }
1276: }
1277: ##
1278: ## Update the students "current" data in the performance
1279: ## and parameters tables.
1280: my ($status,undef) = &store_student_data
1281: ($sname,$sdom,$courseid,
1282: &Apache::lonnet::convert_dump_to_currentdump(\%studentdata));
1283: if ($returnstatus eq 'okay' && $status ne 'okay') {
1284: $returnstatus = 'error storing current data:'.$status;
1285: } elsif ($status ne 'okay') {
1286: $returnstatus .= ' error storing current data:'.$status;
1287: }
1288: ##
1289: ## Update the students time......
1290: if ($returnstatus eq 'okay') {
1291: &Apache::lonmysql::replace_row
1292: ($studentdata_table,
1293: [$student_id,$time_of_retrieval,$time_of_retrieval,undef,undef]);
1294: }
1295: return $returnstatus;
1296: }
1297:
1298: ################################################
1299: ################################################
1300:
1301: =pod
1302:
1.57 matthew 1303: =item &update_student_data()
1304:
1305: Input: $sname, $sdom, $courseid
1306:
1307: Output: $returnstatus, \%student_data
1308:
1309: $returnstatus is a string describing any errors that occured. 'okay' is the
1310: default.
1311: \%student_data is the data returned by a call to lonnet::currentdump.
1312:
1313: This subroutine loads a students data using lonnet::currentdump and inserts
1314: it into the MySQL database. The inserts are done on two tables,
1315: $performance_table and $parameters_table. $parameters_table holds the data
1316: that is not included in $performance_table. See the description of
1317: $performance_table elsewhere in this file. The INSERT calls are made
1318: directly by this subroutine, not through lonmysql because we do a 'bulk'
1319: insert which takes advantage of MySQLs non-SQL compliant INSERT command to
1320: insert multiple rows at a time. If anything has gone wrong during this
1321: process, $returnstatus is updated with a description of the error and
1322: \%student_data is returned.
1323:
1324: Notice we do not insert the data and immediately query it. This means it
1325: is possible for there to be data returned this first time that is not
1326: available the second time. CYA.
1327:
1328: =cut
1329:
1330: ################################################
1331: ################################################
1332: sub update_student_data {
1333: my ($sname,$sdom,$courseid) = @_;
1334: #
1.60 matthew 1335: # Set up database names
1336: &setup_table_names($courseid);
1337: #
1.57 matthew 1338: my $student_id = &get_student_id($sname,$sdom);
1339: my $student = $sname.':'.$sdom;
1340: #
1341: my $returnstatus = 'okay';
1342: #
1343: # Download students data
1344: my $time_of_retrieval = time;
1345: my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
1346: if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
1347: &Apache::lonnet::logthis('error getting data for '.
1348: $sname.':'.$sdom.' in course '.$courseid.
1349: ':'.$tmp[0]);
1350: $returnstatus = 'error getting data';
1.79 matthew 1351: return ($returnstatus,undef);
1.57 matthew 1352: }
1353: if (scalar(@tmp) < 1) {
1354: return ('no data',undef);
1355: }
1356: my %student_data = @tmp;
1.89 matthew 1357: my @Results = &store_student_data($sname,$sdom,$courseid,\%student_data);
1358: #
1359: # Set the students update time
1360: &Apache::lonmysql::replace_row($studentdata_table,
1361: [$student_id,$time_of_retrieval,undef,undef,undef]);
1362: #
1363: return @Results;
1364: }
1365:
1366: sub store_student_data {
1367: my ($sname,$sdom,$courseid,$student_data) = @_;
1368: #
1369: my $student_id = &get_student_id($sname,$sdom);
1370: my $student = $sname.':'.$sdom;
1371: #
1372: my $returnstatus = 'okay';
1.57 matthew 1373: #
1374: # Remove all of the students data from the table
1.60 matthew 1375: my $dbh = &Apache::lonmysql::get_dbh();
1376: $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
1377: $student_id);
1378: $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
1379: $student_id);
1.57 matthew 1380: #
1381: # Store away the data
1382: #
1383: my $starttime = Time::HiRes::time;
1384: my $elapsed = 0;
1385: my $rows_stored;
1386: my $store_parameters_command = 'INSERT INTO '.$parameters_table.
1.60 matthew 1387: ' VALUES '."\n";
1.61 matthew 1388: my $num_parameters = 0;
1.57 matthew 1389: my $store_performance_command = 'INSERT INTO '.$performance_table.
1.60 matthew 1390: ' VALUES '."\n";
1.79 matthew 1391: return ('error',undef) if (! defined($dbh));
1.89 matthew 1392: while (my ($current_symb,$param_hash) = each(%{$student_data})) {
1.57 matthew 1393: #
1394: # make sure the symb is set up properly
1395: my $symb_id = &get_symb_id($current_symb);
1396: #
1397: # Load data into the tables
1.63 matthew 1398: while (my ($parameter,$value) = each(%$param_hash)) {
1.57 matthew 1399: my $newstring;
1.63 matthew 1400: if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.57 matthew 1401: $newstring = "('".join("','",
1402: $symb_id,$student_id,
1.69 matthew 1403: $parameter)."',".
1404: $dbh->quote($value)."),\n";
1.61 matthew 1405: $num_parameters ++;
1.57 matthew 1406: if ($newstring !~ /''/) {
1407: $store_parameters_command .= $newstring;
1408: $rows_stored++;
1409: }
1410: }
1411: next if ($parameter !~ /^resource\.(.*)\.solved$/);
1412: #
1413: my $part = $1;
1414: my $part_id = &get_part_id($part);
1415: next if (!defined($part_id));
1416: my $solved = $value;
1417: my $tries = $param_hash->{'resource.'.$part.'.tries'};
1418: my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
1419: my $award = $param_hash->{'resource.'.$part.'.award'};
1420: my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
1421: my $timestamp = $param_hash->{'timestamp'};
1.60 matthew 1422: #
1.74 matthew 1423: $solved = '' if (! defined($solved));
1.57 matthew 1424: $tries = '' if (! defined($tries));
1425: $awarded = '' if (! defined($awarded));
1426: $award = '' if (! defined($award));
1427: $awarddetail = '' if (! defined($awarddetail));
1.73 matthew 1428: $newstring = "('".join("','",$symb_id,$student_id,$part_id,$part,
1.57 matthew 1429: $solved,$tries,$awarded,$award,
1.63 matthew 1430: $awarddetail,$timestamp)."'),\n";
1.57 matthew 1431: $store_performance_command .= $newstring;
1432: $rows_stored++;
1433: }
1434: }
1435: chop $store_parameters_command;
1.60 matthew 1436: chop $store_parameters_command;
1437: chop $store_performance_command;
1.57 matthew 1438: chop $store_performance_command;
1439: my $start = Time::HiRes::time;
1.61 matthew 1440: $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57 matthew 1441: if ($dbh->err()) {
1442: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61 matthew 1443: &Apache::lonnet::logthis('command = '.$store_parameters_command);
1.87 matthew 1444: &Apache::lonnet::logthis('rows_stored = '.$rows_stored);
1445: &Apache::lonnet::logthis('student_id = '.$student_id);
1.57 matthew 1446: $returnstatus = 'error: unable to insert parameters into database';
1.89 matthew 1447: return ($returnstatus,$student_data);
1.57 matthew 1448: }
1449: $dbh->do($store_performance_command);
1450: if ($dbh->err()) {
1451: &Apache::lonnet::logthis(' bigass insert error:'.$dbh->errstr());
1.61 matthew 1452: &Apache::lonnet::logthis('command = '.$store_performance_command);
1.57 matthew 1453: $returnstatus = 'error: unable to insert performance into database';
1.89 matthew 1454: return ($returnstatus,$student_data);
1.57 matthew 1455: }
1456: $elapsed += Time::HiRes::time - $start;
1.89 matthew 1457: return ($returnstatus,$student_data);
1.57 matthew 1458: }
1459:
1.89 matthew 1460: ######################################
1461: ######################################
1.57 matthew 1462:
1463: =pod
1464:
1.89 matthew 1465: =item &ensure_tables_are_set_up($courseid)
1.57 matthew 1466:
1.89 matthew 1467: Checks to be sure the MySQL tables for the given class are set up.
1468: If $courseid is omitted it will be obtained from the environment.
1.57 matthew 1469:
1.89 matthew 1470: Returns nothing on success and 'error' on failure
1.57 matthew 1471:
1472: =cut
1473:
1.89 matthew 1474: ######################################
1475: ######################################
1476: sub ensure_tables_are_set_up {
1477: my ($courseid) = @_;
1.61 matthew 1478: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1479: #
1480: # Clean out package variables
1.57 matthew 1481: &setup_table_names($courseid);
1482: #
1483: # if the tables do not exist, make them
1484: my @CurrentTable = &Apache::lonmysql::tables_in_db();
1.87 matthew 1485: my ($found_symb,$found_student,$found_part,$found_studentdata,
1.89 matthew 1486: $found_performance,$found_parameters,$found_fulldump_part,
1487: $found_fulldump_response,$found_fulldump_timestamp);
1.57 matthew 1488: foreach (@CurrentTable) {
1489: $found_symb = 1 if ($_ eq $symb_table);
1490: $found_student = 1 if ($_ eq $student_table);
1491: $found_part = 1 if ($_ eq $part_table);
1.87 matthew 1492: $found_studentdata = 1 if ($_ eq $studentdata_table);
1.57 matthew 1493: $found_performance = 1 if ($_ eq $performance_table);
1494: $found_parameters = 1 if ($_ eq $parameters_table);
1.89 matthew 1495: $found_fulldump_part = 1 if ($_ eq $fulldump_part_table);
1496: $found_fulldump_response = 1 if ($_ eq $fulldump_response_table);
1497: $found_fulldump_timestamp = 1 if ($_ eq $fulldump_timestamp_table);
1.57 matthew 1498: }
1.87 matthew 1499: if (!$found_symb || !$found_studentdata ||
1.57 matthew 1500: !$found_student || !$found_part ||
1.89 matthew 1501: !$found_performance || !$found_parameters ||
1502: !$found_fulldump_part || !$found_fulldump_response ||
1503: !$found_fulldump_timestamp ) {
1.57 matthew 1504: if (&init_dbs($courseid)) {
1.89 matthew 1505: return 'error';
1.57 matthew 1506: }
1507: }
1.89 matthew 1508: }
1509:
1510: ################################################
1511: ################################################
1512:
1513: =pod
1514:
1515: =item &ensure_current_data()
1516:
1517: Input: $sname, $sdom, $courseid
1518:
1519: Output: $status, $data
1520:
1521: This routine ensures the data for a given student is up to date.
1522: The $studentdata_table is queried to determine the time of the last update.
1523: If the students data is out of date, &update_student_data() is called.
1524: The return values from the call to &update_student_data() are returned.
1525:
1526: =cut
1527:
1528: ################################################
1529: ################################################
1530: sub ensure_current_data {
1531: my ($sname,$sdom,$courseid) = @_;
1532: my $status = 'okay'; # return value
1533: #
1534: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1535: &ensure_tables_are_set_up($courseid);
1.57 matthew 1536: #
1537: # Get the update time for the user
1538: my $updatetime = 0;
1.60 matthew 1539: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1540: ($sdom,$sname,$courseid.'.db',
1541: $Apache::lonnet::perlvar{'lonUsersDir'});
1.57 matthew 1542: #
1.87 matthew 1543: my $student_id = &get_student_id($sname,$sdom);
1544: my @Result = &Apache::lonmysql::get_rows($studentdata_table,
1545: "student_id ='$student_id'");
1.57 matthew 1546: my $data = undef;
1547: if (@Result) {
1548: $updatetime = $Result[0]->[1];
1549: }
1550: if ($modifiedtime > $updatetime) {
1551: ($status,$data) = &update_student_data($sname,$sdom,$courseid);
1552: }
1553: return ($status,$data);
1554: }
1555:
1556: ################################################
1557: ################################################
1558:
1559: =pod
1560:
1.89 matthew 1561: =item &ensure_current_full_data($sname,$sdom,$courseid)
1562:
1563: Input: $sname, $sdom, $courseid
1564:
1565: Output: $status
1566:
1567: This routine ensures the fulldata (the data from a lonnet::dump, not a
1568: lonnet::currentdump) for a given student is up to date.
1569: The $studentdata_table is queried to determine the time of the last update.
1570: If the students fulldata is out of date, &update_full_student_data() is
1571: called.
1572:
1573: The return value from the call to &update_full_student_data() is returned.
1574:
1575: =cut
1576:
1577: ################################################
1578: ################################################
1579: sub ensure_current_full_data {
1580: my ($sname,$sdom,$courseid) = @_;
1581: my $status = 'okay'; # return value
1582: #
1583: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1584: &ensure_tables_are_set_up($courseid);
1585: #
1586: # Get the update time for the user
1587: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1588: ($sdom,$sname,$courseid.'.db',
1589: $Apache::lonnet::perlvar{'lonUsersDir'});
1590: #
1591: my $student_id = &get_student_id($sname,$sdom);
1592: my @Result = &Apache::lonmysql::get_rows($studentdata_table,
1593: "student_id ='$student_id'");
1594: my $updatetime;
1595: if (@Result && ref($Result[0]) eq 'ARRAY') {
1596: $updatetime = $Result[0]->[2];
1597: }
1598: if (! defined($updatetime) || $modifiedtime > $updatetime) {
1599: $status = &update_full_student_data($sname,$sdom,$courseid);
1600: }
1601: return $status;
1602: }
1603:
1604: ################################################
1605: ################################################
1606:
1607: =pod
1608:
1.57 matthew 1609: =item &get_student_data_from_performance_cache()
1610:
1611: Input: $sname, $sdom, $symb, $courseid
1612:
1613: Output: hash reference containing the data for the given student.
1614: If $symb is undef, all the students data is returned.
1615:
1616: This routine is the heart of the local caching system. See the description
1617: of $performance_table, $symb_table, $student_table, and $part_table. The
1618: main task is building the MySQL request. The tables appear in the request
1619: in the order in which they should be parsed by MySQL. When searching
1620: on a student the $student_table is used to locate the 'student_id'. All
1621: rows in $performance_table which have a matching 'student_id' are returned,
1622: with data from $part_table and $symb_table which match the entries in
1623: $performance_table, 'part_id' and 'symb_id'. When searching on a symb,
1624: the $symb_table is processed first, with matching rows grabbed from
1625: $performance_table and filled in from $part_table and $student_table in
1626: that order.
1627:
1628: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite
1629: interesting, especially if you play with the order the tables are listed.
1630:
1631: =cut
1632:
1633: ################################################
1634: ################################################
1635: sub get_student_data_from_performance_cache {
1636: my ($sname,$sdom,$symb,$courseid)=@_;
1637: my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61 matthew 1638: &setup_table_names($courseid);
1.57 matthew 1639: #
1640: # Return hash
1641: my $studentdata;
1642: #
1643: my $dbh = &Apache::lonmysql::get_dbh();
1644: my $request = "SELECT ".
1.73 matthew 1645: "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63 matthew 1646: "a.timestamp ";
1.57 matthew 1647: if (defined($student)) {
1648: $request .= "FROM $student_table AS b ".
1649: "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73 matthew 1650: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1651: "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
1652: "WHERE student='$student'";
1653: if (defined($symb) && $symb ne '') {
1.67 matthew 1654: $request .= " AND d.symb=".$dbh->quote($symb);
1.57 matthew 1655: }
1656: } elsif (defined($symb) && $symb ne '') {
1657: $request .= "FROM $symb_table as d ".
1658: "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73 matthew 1659: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1660: "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
1661: "WHERE symb='".$dbh->quote($symb)."'";
1662: }
1663: my $starttime = Time::HiRes::time;
1664: my $rows_retrieved = 0;
1665: my $sth = $dbh->prepare($request);
1666: $sth->execute();
1667: if ($sth->err()) {
1668: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1669: &Apache::lonnet::logthis("\n".$request."\n");
1670: &Apache::lonnet::logthis("error is:".$sth->errstr());
1671: return undef;
1672: }
1673: foreach my $row (@{$sth->fetchall_arrayref}) {
1674: $rows_retrieved++;
1.63 matthew 1675: my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) =
1.57 matthew 1676: (@$row);
1677: my $base = 'resource.'.$part;
1678: $studentdata->{$symb}->{$base.'.solved'} = $solved;
1679: $studentdata->{$symb}->{$base.'.tries'} = $tries;
1680: $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
1681: $studentdata->{$symb}->{$base.'.award'} = $award;
1682: $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
1683: $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67 matthew 1684: }
1685: if (defined($symb) && $symb ne '') {
1686: $studentdata = $studentdata->{$symb};
1.57 matthew 1687: }
1688: return $studentdata;
1689: }
1690:
1691: ################################################
1692: ################################################
1693:
1694: =pod
1695:
1696: =item &get_current_state()
1697:
1698: Input: $sname,$sdom,$symb,$courseid
1699:
1700: Output: Described below
1.46 matthew 1701:
1.47 matthew 1702: Retrieve the current status of a students performance. $sname and
1.46 matthew 1703: $sdom are the only required parameters. If $symb is undef the results
1.47 matthew 1704: of an &Apache::lonnet::currentdump() will be returned.
1.46 matthew 1705: If $courseid is undef it will be retrieved from the environment.
1706:
1707: The return structure is based on &Apache::lonnet::currentdump. If
1708: $symb is unspecified, all the students data is returned in a hash of
1709: the form:
1710: (
1711: symb1 => { param1 => value1, param2 => value2 ... },
1712: symb2 => { param1 => value1, param2 => value2 ... },
1713: )
1714:
1715: If $symb is specified, a hash of
1716: (
1717: param1 => value1,
1718: param2 => value2,
1719: )
1720: is returned.
1721:
1.57 matthew 1722: If no data is found for $symb, or if the student has no performance data,
1.46 matthew 1723: an empty list is returned.
1724:
1725: =cut
1726:
1727: ################################################
1728: ################################################
1729: sub get_current_state {
1.47 matthew 1730: my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
1731: #
1.46 matthew 1732: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1.47 matthew 1733: #
1.61 matthew 1734: return () if (! defined($sname) || ! defined($sdom));
1735: #
1.57 matthew 1736: my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77 matthew 1737: # &Apache::lonnet::logthis
1738: # ('sname = '.$sname.
1739: # ' domain = '.$sdom.
1740: # ' status = '.$status.
1741: # ' data is '.(defined($data)?'defined':'undefined'));
1.73 matthew 1742: # while (my ($symb,$hash) = each(%$data)) {
1743: # &Apache::lonnet::logthis($symb."\n----------------------------------");
1744: # while (my ($key,$value) = each (%$hash)) {
1745: # &Apache::lonnet::logthis(" ".$key." = ".$value);
1746: # }
1747: # }
1.47 matthew 1748: #
1.79 matthew 1749: if (defined($data) && defined($symb) && ref($data->{$symb})) {
1750: return %{$data->{$symb}};
1751: } elsif (defined($data) && ! defined($symb) && ref($data)) {
1752: return %$data;
1753: }
1754: if ($status eq 'no data') {
1.57 matthew 1755: return ();
1756: } else {
1757: if ($status ne 'okay' && $status ne '') {
1758: &Apache::lonnet::logthis('status = '.$status);
1.47 matthew 1759: return ();
1760: }
1.57 matthew 1761: my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
1762: $symb,$courseid);
1763: return %$returnhash if (defined($returnhash));
1.46 matthew 1764: }
1.57 matthew 1765: return ();
1.61 matthew 1766: }
1767:
1768: ################################################
1769: ################################################
1770:
1771: =pod
1772:
1773: =item &get_problem_statistics()
1774:
1775: Gather data on a given problem. The database is assumed to be
1776: populated and all local caching variables are assumed to be set
1777: properly. This means you need to call &ensure_current_data for
1778: the students you are concerned with prior to calling this routine.
1779:
1780: Inputs: $students, $symb, $part, $courseid
1781:
1.64 matthew 1782: =over 4
1783:
1784: =item $students is an array of hash references.
1785: Each hash must contain at least the 'username' and 'domain' of a student.
1786:
1787: =item $symb is the symb for the problem.
1788:
1789: =item $part is the part id you need statistics for
1790:
1791: =item $courseid is the course id, of course!
1792:
1793: =back
1794:
1.66 matthew 1795: Outputs: See the code for up to date information. A hash reference is
1796: returned. The hash has the following keys defined:
1.64 matthew 1797:
1798: =over 4
1799:
1.66 matthew 1800: =item num_students The number of students attempting the problem
1801:
1802: =item tries The total number of tries for the students
1803:
1804: =item max_tries The maximum number of tries taken
1805:
1806: =item mean_tries The average number of tries
1807:
1808: =item num_solved The number of students able to solve the problem
1809:
1810: =item num_override The number of students whose answer is 'correct_by_override'
1811:
1812: =item deg_of_diff The degree of difficulty of the problem
1813:
1814: =item std_tries The standard deviation of the number of tries
1815:
1816: =item skew_tries The skew of the number of tries
1.64 matthew 1817:
1.66 matthew 1818: =item per_wrong The number of students attempting the problem who were not
1819: able to answer it correctly.
1.64 matthew 1820:
1821: =back
1822:
1.61 matthew 1823: =cut
1824:
1825: ################################################
1826: ################################################
1827: sub get_problem_statistics {
1828: my ($students,$symb,$part,$courseid) = @_;
1829: return if (! defined($symb) || ! defined($part));
1830: $courseid = $ENV{'request.course.id'} if (! defined($courseid));
1831: #
1832: my $symb_id = &get_symb_id($symb);
1833: my $part_id = &get_part_id($part);
1834: my $stats_table = $courseid.'_problem_stats';
1835: #
1836: my $dbh = &Apache::lonmysql::get_dbh();
1837: return undef if (! defined($dbh));
1838: #
1839: # A) Number of Students attempting problem
1840: # B) Total number of tries of students attempting problem
1841: # C) Mod (largest number of tries for solving the problem)
1842: # D) Mean (average number of tries for solving the problem)
1843: # E) Number of students to solve the problem
1844: # F) Number of students to solve the problem by override
1845: # G) Number of students unable to solve the problem
1846: # H) Degree of difficulty : 1-(E+F)/B
1847: # I) Standard deviation of number of tries
1848: # J) Skew of tries: sqrt(sum(Xi-D)^3)/A
1849: #
1850: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1851: my $request =
1852: 'CREATE TEMPORARY TABLE '.$stats_table.
1853: ' SELECT student_id,solved,award,tries FROM '.$performance_table.
1854: ' WHERE symb_id='.$symb_id.' AND part_id='.$part_id;
1.64 matthew 1855: if (defined($students)) {
1856: $request .= ' AND ('.
1857: join(' OR ', map {'student_id='.
1858: &get_student_id($_->{'username'},
1859: $_->{'domain'})
1860: } @$students
1861: ).')';
1862: }
1.61 matthew 1863: # &Apache::lonnet::logthis($request);
1864: $dbh->do($request);
1865: my ($num,$tries,$mod,$mean,$STD) = &execute_SQL_request
1866: ($dbh,
1867: 'SELECT COUNT(*),SUM(tries),MAX(tries),AVG(tries),STD(tries) FROM '.
1868: $stats_table);
1869: my ($Solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
1870: $stats_table.
1.92 ! matthew 1871: " WHERE solved='correct_by_student' OR solved='correct_by_scantron'");
1.61 matthew 1872: my ($solved) = &execute_SQL_request($dbh,'SELECT COUNT(tries) FROM '.
1873: $stats_table.
1874: " WHERE solved='correct_by_override'");
1875: $num = 0 if (! defined($num));
1876: $tries = 0 if (! defined($tries));
1877: $mod = 0 if (! defined($mod));
1878: $STD = 0 if (! defined($STD));
1879: $Solved = 0 if (! defined($Solved));
1880: $solved = 0 if (! defined($solved));
1881: #
1882: my $DegOfDiff = 'nan';
1.66 matthew 1883: $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.61 matthew 1884:
1885: my $SKEW = 'nan';
1.66 matthew 1886: my $wrongpercent = 0;
1.61 matthew 1887: if ($num > 0) {
1888: ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
1889: 'POWER(tries - '.$STD.',3)'.
1890: '))/'.$num.' FROM '.$stats_table);
1.66 matthew 1891: $wrongpercent=int(10*100*($num-$Solved+$solved)/$num)/10;
1.61 matthew 1892: }
1893: #
1894: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1.81 matthew 1895: #
1896: # Store in metadata
1897: #
1.80 www 1898: if ($num) {
1899: my %storestats=();
1900:
1.86 www 1901: my $urlres=(&Apache::lonnet::decode_symb($symb))[2];
1.80 www 1902:
1903: $storestats{$courseid.'___'.$urlres.'___timestamp'}=time;
1904: $storestats{$courseid.'___'.$urlres.'___stdno'}=$num;
1905: $storestats{$courseid.'___'.$urlres.'___avetries'}=$mean;
1906: $storestats{$courseid.'___'.$urlres.'___difficulty'}=$DegOfDiff;
1907:
1908: $urlres=~/^(\w+)\/(\w+)/;
1909: &Apache::lonnet::put('nohist_resevaldata',\%storestats,$1,$2);
1910: }
1.81 matthew 1911: #
1912: # Return result
1913: #
1.66 matthew 1914: return { num_students => $num,
1915: tries => $tries,
1916: max_tries => $mod,
1917: mean_tries => $mean,
1918: std_tries => $STD,
1919: skew_tries => $SKEW,
1920: num_solved => $Solved,
1921: num_override => $solved,
1922: per_wrong => $wrongpercent,
1.81 matthew 1923: deg_of_diff => $DegOfDiff };
1.61 matthew 1924: }
1925:
1926: sub execute_SQL_request {
1927: my ($dbh,$request)=@_;
1928: # &Apache::lonnet::logthis($request);
1929: my $sth = $dbh->prepare($request);
1930: $sth->execute();
1931: my $row = $sth->fetchrow_arrayref();
1932: if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
1933: return @$row;
1934: }
1935: return ();
1936: }
1937:
1938:
1939: ################################################
1940: ################################################
1941:
1942: =pod
1943:
1944: =item &setup_table_names()
1945:
1946: input: course id
1947:
1948: output: none
1949:
1950: Cleans up the package variables for local caching.
1951:
1952: =cut
1953:
1954: ################################################
1955: ################################################
1956: sub setup_table_names {
1957: my ($courseid) = @_;
1958: if (! defined($courseid)) {
1959: $courseid = $ENV{'request.course.id'};
1960: }
1961: #
1962: if (! defined($current_course) || $current_course ne $courseid) {
1963: # Clear out variables
1964: $have_read_part_table = 0;
1965: undef(%ids_by_part);
1966: undef(%parts_by_id);
1967: $have_read_symb_table = 0;
1968: undef(%ids_by_symb);
1969: undef(%symbs_by_id);
1970: $have_read_student_table = 0;
1971: undef(%ids_by_student);
1972: undef(%students_by_id);
1973: #
1974: $current_course = $courseid;
1975: }
1976: #
1977: # Set up database names
1978: my $base_id = $courseid;
1979: $symb_table = $base_id.'_'.'symb';
1980: $part_table = $base_id.'_'.'part';
1981: $student_table = $base_id.'_'.'student';
1.87 matthew 1982: $studentdata_table = $base_id.'_'.'studentdata';
1.61 matthew 1983: $performance_table = $base_id.'_'.'performance';
1984: $parameters_table = $base_id.'_'.'parameters';
1.89 matthew 1985: $fulldump_part_table = $base_id.'_'.'partdata';
1986: $fulldump_response_table = $base_id.'_'.'responsedata';
1987: $fulldump_timestamp_table = $base_id.'_'.'timestampdata';
1988: #
1989: @Tables = (
1990: $symb_table,
1991: $part_table,
1992: $student_table,
1993: $studentdata_table,
1994: $performance_table,
1995: $parameters_table,
1996: $fulldump_part_table,
1997: $fulldump_response_table,
1998: $fulldump_timestamp_table,
1999: );
1.61 matthew 2000: return;
1.3 stredwic 2001: }
1.1 stredwic 2002:
1.35 matthew 2003: ################################################
2004: ################################################
2005:
2006: =pod
2007:
1.57 matthew 2008: =back
2009:
2010: =item End of Local Data Caching Subroutines
2011:
2012: =cut
2013:
2014: ################################################
2015: ################################################
2016:
1.89 matthew 2017: } # End scope of table identifiers
1.57 matthew 2018:
2019: ################################################
2020: ################################################
2021:
2022: =pod
2023:
2024: =head3 Classlist Subroutines
2025:
1.35 matthew 2026: =item &get_classlist();
2027:
2028: Retrieve the classist of a given class or of the current class. Student
2029: information is returned from the classlist.db file and, if needed,
2030: from the students environment.
2031:
2032: Optional arguments are $cid, $cdom, and $cnum (course id, course domain,
2033: and course number, respectively). Any omitted arguments will be taken
2034: from the current environment ($ENV{'request.course.id'},
2035: $ENV{'course.'.$cid.'.domain'}, and $ENV{'course.'.$cid.'.num'}).
2036:
2037: Returns a reference to a hash which contains:
2038: keys '$sname:$sdom'
1.54 bowersj2 2039: values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status]
2040:
2041: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
2042: as indices into the returned list to future-proof clients against
2043: changes in the list order.
1.35 matthew 2044:
2045: =cut
2046:
2047: ################################################
2048: ################################################
1.54 bowersj2 2049:
2050: sub CL_SDOM { return 0; }
2051: sub CL_SNAME { return 1; }
2052: sub CL_END { return 2; }
2053: sub CL_START { return 3; }
2054: sub CL_ID { return 4; }
2055: sub CL_SECTION { return 5; }
2056: sub CL_FULLNAME { return 6; }
2057: sub CL_STATUS { return 7; }
1.35 matthew 2058:
2059: sub get_classlist {
2060: my ($cid,$cdom,$cnum) = @_;
2061: $cid = $cid || $ENV{'request.course.id'};
2062: $cdom = $cdom || $ENV{'course.'.$cid.'.domain'};
2063: $cnum = $cnum || $ENV{'course.'.$cid.'.num'};
1.57 matthew 2064: my $now = time;
1.35 matthew 2065: #
2066: my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
2067: while (my ($student,$info) = each(%classlist)) {
1.60 matthew 2068: if ($student =~ /^(con_lost|error|no_such_host)/i) {
2069: &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
2070: return undef;
2071: }
1.35 matthew 2072: my ($sname,$sdom) = split(/:/,$student);
2073: my @Values = split(/:/,$info);
2074: my ($end,$start,$id,$section,$fullname);
2075: if (@Values > 2) {
2076: ($end,$start,$id,$section,$fullname) = @Values;
2077: } else { # We have to get the data ourselves
2078: ($end,$start) = @Values;
1.37 matthew 2079: $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35 matthew 2080: my %info=&Apache::lonnet::get('environment',
2081: ['firstname','middlename',
2082: 'lastname','generation','id'],
2083: $sdom, $sname);
2084: my ($tmp) = keys(%info);
2085: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
2086: $fullname = 'not available';
2087: $id = 'not available';
1.38 matthew 2088: &Apache::lonnet::logthis('unable to retrieve environment '.
2089: 'for '.$sname.':'.$sdom);
1.35 matthew 2090: } else {
2091: $fullname = &ProcessFullName(@info{qw/lastname generation
2092: firstname middlename/});
2093: $id = $info{'id'};
2094: }
1.36 matthew 2095: # Update the classlist with this students information
2096: if ($fullname ne 'not available') {
2097: my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
2098: my $reply=&Apache::lonnet::cput('classlist',
2099: {$student => $enrolldata},
2100: $cdom,$cnum);
2101: if ($reply !~ /^(ok|delayed)/) {
2102: &Apache::lonnet::logthis('Unable to update classlist for '.
2103: 'student '.$sname.':'.$sdom.
2104: ' error:'.$reply);
2105: }
2106: }
1.35 matthew 2107: }
2108: my $status='Expired';
2109: if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
2110: $status='Active';
2111: }
2112: $classlist{$student} =
2113: [$sdom,$sname,$end,$start,$id,$section,$fullname,$status];
2114: }
2115: if (wantarray()) {
2116: return (\%classlist,['domain','username','end','start','id',
2117: 'section','fullname','status']);
2118: } else {
2119: return \%classlist;
2120: }
2121: }
2122:
1.1 stredwic 2123: # ----- END HELPER FUNCTIONS --------------------------------------------
2124:
2125: 1;
2126: __END__
1.36 matthew 2127:
1.35 matthew 2128:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>