Annotation of loncom/interface/loncoursedata.pm, revision 1.158
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: #
1.158 ! albertel 3: # $Id: loncoursedata.pm,v 1.157 2006/03/04 06:12:40 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;
1.146 albertel 52: use Apache::lonnet;
1.13 stredwic 53: use Apache::lonhtmlcommon;
1.57 matthew 54: use Time::HiRes;
55: use Apache::lonmysql;
1.1 stredwic 56: use HTML::TokeParser;
57: use GDBM_File;
58:
59: =pod
60:
61: =head1 DOWNLOAD INFORMATION
62:
1.22 stredwic 63: This section contains all the functions that get data from other servers
64: and/or itself.
1.1 stredwic 65:
66: =cut
67:
1.71 matthew 68: ################################################
69: ################################################
70:
71: =pod
72:
1.47 matthew 73: =item &make_into_hash($values);
74:
75: Returns a reference to a hash as described by $values. $values is
76: assumed to be the result of
1.57 matthew 77: join(':',map {&Apache::lonnet::escape($_)} %orighash);
1.47 matthew 78:
79: This is a helper function for get_current_state.
80:
81: =cut
82:
83: ################################################
84: ################################################
85: sub make_into_hash {
86: my $values = shift;
87: my %tmp = map { &Apache::lonnet::unescape($_); }
88: split(':',$values);
89: return \%tmp;
90: }
91:
92:
93: ################################################
94: ################################################
95:
96: =pod
97:
1.57 matthew 98: =head1 LOCAL DATA CACHING SUBROUTINES
99:
100: The local caching is done using MySQL. There is no fall-back implementation
101: if MySQL is not running.
102:
103: The programmers interface is to call &get_current_state() or some other
104: primary interface subroutine (described below). The internals of this
105: storage system are documented here.
106:
107: There are six tables used to store student performance data (the results of
108: a dumpcurrent). Each of these tables is created in MySQL with a name of
109: $courseid_*****, where ***** is 'symb', 'part', or whatever is appropriate
110: for the table. The tables and their purposes are described below.
111:
112: Some notes before we get started.
113:
114: Each table must have a PRIMARY KEY, which is a column or set of columns which
115: will serve to uniquely identify a row of data. NULL is not allowed!
116:
117: INDEXes work best on integer data.
118:
119: JOIN is used to combine data from many tables into one output.
120:
121: lonmysql.pm is used for some of the interface, specifically the table creation
122: calls. The inserts are done in bulk by directly calling the database handler.
123: The SELECT ... JOIN statement used to retrieve the data does not have an
124: interface in lonmysql.pm and I shudder at the thought of writing one.
125:
126: =head3 Table Descriptions
127:
128: =over 4
129:
1.89 matthew 130: =item Tables used to store meta information
131:
132: The following tables hold data required to keep track of the current status
133: of a students data in the tables or to look up the students data in the tables.
134:
135: =over 4
136:
1.57 matthew 137: =item $symb_table
138:
139: The symb_table has two columns. The first is a 'symb_id' and the second
140: is the text name for the 'symb' (limited to 64k). The 'symb_id' is generated
141: automatically by MySQL so inserts should be done on this table with an
142: empty first element. This table has its PRIMARY KEY on the 'symb_id'.
143:
144: =item $part_table
145:
146: The part_table has two columns. The first is a 'part_id' and the second
147: is the text name for the 'part' (limited to 100 characters). The 'part_id' is
148: generated automatically by MySQL so inserts should be done on this table with
149: an empty first element. This table has its PRIMARY KEY on the 'part' (100
150: characters) and a KEY on 'part_id'.
151:
152: =item $student_table
153:
1.113 matthew 154: The student_table has 7 columns. The first is a 'student_id' assigned by
155: MySQL. The second is 'student' which is username:domain. The third through
156: fifth are 'section', 'status' (enrollment status), and 'classification'
157: (to be used in the future). The sixth and seventh ('updatetime' and
158: 'fullupdatetime') contain the time of last update and full update of student
159: data. This table has its PRIMARY KEY on the 'student_id' column and is indexed
160: on 'student', 'section', and 'status'.
1.89 matthew 161:
162: =back
163:
164: =item Tables used to store current status data
165:
166: The following tables store data only about the students current status on
167: a problem, meaning only the data related to the last attempt on a problem.
168:
169: =over 4
1.57 matthew 170:
171: =item $performance_table
172:
173: The performance_table has 9 columns. The first three are 'symb_id',
174: 'student_id', and 'part_id'. These comprise the PRIMARY KEY for this table
175: and are directly related to the $symb_table, $student_table, and $part_table
176: described above. MySQL does better indexing on numeric items than text,
177: so we use these three "index tables". The remaining columns are
178: 'solved', 'tries', 'awarded', 'award', 'awarddetail', and 'timestamp'.
179: These are either the MySQL type TINYTEXT or various integers ('tries' and
180: 'timestamp'). This table has KEYs of 'student_id' and 'symb_id'.
181: For use of this table, see the functions described below.
182:
183: =item $parameters_table
184:
185: The parameters_table holds the data that does not fit neatly into the
186: performance_table. The parameters table has four columns: 'symb_id',
187: 'student_id', 'parameter', and 'value'. 'symb_id', 'student_id', and
188: 'parameter' comprise the PRIMARY KEY for this table. 'parameter' is
189: limited to 255 characters. 'value' is limited to 64k characters.
190:
191: =back
192:
1.89 matthew 193: =item Tables used for storing historic data
194:
195: The following tables are used to store almost all of the transactions a student
196: has made on a homework problem. See loncapa/docs/homework/datastorage for
197: specific information about each of the parameters stored.
198:
199: =over 4
200:
201: =item $fulldump_response_table
202:
203: The response table holds data (documented in loncapa/docs/homework/datastorage)
204: associated with a particular response id which is stored when a student
205: attempts a problem. The following are the columns of the table, in order:
206: 'symb_id','part_id','response_id','student_id','transaction','tries',
1.93 matthew 207: 'awarddetail', 'response_specific' (data particular to the response
1.89 matthew 208: type), 'response_specific_value', and 'submission (the text of the students
209: submission). The primary key is based on the first five columns listed above.
210:
211: =item $fulldump_part_table
212:
213: The part table holds data (documented in loncapa/docs/homework/datastorage)
214: associated with a particular part id which is stored when a student attempts
215: a problem. The following are the columns of the table, in order:
216: 'symb_id','part_id','student_id','transaction','tries','award','awarded',
217: and 'previous'. The primary key is based on the first five columns listed
218: above.
219:
220: =item $fulldump_timestamp_table
221:
222: The timestamp table holds the timestamps of the transactions which are
223: stored in $fulldump_response_table and $fulldump_part_table. This data is
224: about both the response and part data. Columns: 'symb_id','student_id',
225: 'transaction', and 'timestamp'.
226: The primary key is based on the first 3 columns.
227:
1.127 matthew 228: =item $weight_table
229:
230: The weight table holds the weight for the problems used in the class.
231: Whereas the weight of a problem can vary by section and student the data
232: here is applied to the class as a whole.
233: Columns: 'symb_id','part_id','response_id','weight'.
234:
1.89 matthew 235: =back
236:
237: =back
238:
1.57 matthew 239: =head3 Important Subroutines
240:
241: Here is a brief overview of the subroutines which are likely to be of
242: interest:
243:
244: =over 4
245:
246: =item &get_current_state(): programmers interface.
247:
248: =item &init_dbs(): table creation
249:
250: =item &update_student_data(): data storage calls
251:
252: =item &get_student_data_from_performance_cache(): data retrieval
253:
254: =back
255:
256: =head3 Main Documentation
257:
258: =over 4
259:
260: =cut
261:
262: ################################################
263: ################################################
264:
265: ################################################
266: ################################################
1.89 matthew 267: { # Begin scope of table identifiers
1.57 matthew 268:
269: my $current_course ='';
270: my $symb_table;
271: my $part_table;
272: my $student_table;
273: my $performance_table;
274: my $parameters_table;
1.89 matthew 275: my $fulldump_response_table;
276: my $fulldump_part_table;
277: my $fulldump_timestamp_table;
1.127 matthew 278: my $weight_table;
1.57 matthew 279:
1.89 matthew 280: my @Tables;
1.57 matthew 281: ################################################
282: ################################################
283:
284: =pod
285:
286: =item &init_dbs()
287:
288: Input: course id
289:
290: Output: 0 on success, positive integer on error
291:
292: This routine issues the calls to lonmysql to create the tables used to
293: store student data.
294:
295: =cut
296:
297: ################################################
298: ################################################
299: sub init_dbs {
1.134 matthew 300: my ($courseid,$drop) = @_;
1.57 matthew 301: &setup_table_names($courseid);
302: #
1.73 matthew 303: # Drop any of the existing tables
1.134 matthew 304: if ($drop) {
305: foreach my $table (@Tables) {
306: &Apache::lonmysql::drop_table($table);
307: }
1.73 matthew 308: }
309: #
1.57 matthew 310: # Note - changes to this table must be reflected in the code that
311: # stores the data (calls &Apache::lonmysql::store_row with this table
312: # id
313: my $symb_table_def = {
314: id => $symb_table,
315: permanent => 'no',
316: columns => [{ name => 'symb_id',
317: type => 'MEDIUMINT UNSIGNED',
318: restrictions => 'NOT NULL',
319: auto_inc => 'yes', },
320: { name => 'symb',
321: type => 'MEDIUMTEXT',
322: restrictions => 'NOT NULL'},
323: ],
324: 'PRIMARY KEY' => ['symb_id'],
325: };
326: #
327: my $part_table_def = {
328: id => $part_table,
329: permanent => 'no',
330: columns => [{ name => 'part_id',
331: type => 'MEDIUMINT UNSIGNED',
332: restrictions => 'NOT NULL',
333: auto_inc => 'yes', },
334: { name => 'part',
1.132 matthew 335: type => 'VARCHAR(100) BINARY',
1.57 matthew 336: restrictions => 'NOT NULL'},
337: ],
338: 'PRIMARY KEY' => ['part (100)'],
339: 'KEY' => [{ columns => ['part_id']},],
340: };
341: #
342: my $student_table_def = {
343: id => $student_table,
344: permanent => 'no',
345: columns => [{ name => 'student_id',
346: type => 'MEDIUMINT UNSIGNED',
347: restrictions => 'NOT NULL',
348: auto_inc => 'yes', },
349: { name => 'student',
1.132 matthew 350: type => 'VARCHAR(100) BINARY',
1.113 matthew 351: restrictions => 'NOT NULL UNIQUE'},
352: { name => 'section',
1.132 matthew 353: type => 'VARCHAR(100) BINARY',
1.113 matthew 354: restrictions => 'NOT NULL'},
355: { name => 'status',
1.132 matthew 356: type => 'VARCHAR(15) BINARY',
1.57 matthew 357: restrictions => 'NOT NULL'},
1.85 matthew 358: { name => 'classification',
1.132 matthew 359: type => 'VARCHAR(100) BINARY', },
1.57 matthew 360: { name => 'updatetime',
1.89 matthew 361: type => 'INT UNSIGNED'},
362: { name => 'fullupdatetime',
363: type => 'INT UNSIGNED'},
1.57 matthew 364: ],
1.87 matthew 365: 'PRIMARY KEY' => ['student_id'],
1.113 matthew 366: 'KEY' => [{ columns => ['student (100)',
367: 'section (100)',
368: 'status (15)',]},],
1.57 matthew 369: };
370: #
371: my $performance_table_def = {
372: id => $performance_table,
373: permanent => 'no',
374: columns => [{ name => 'symb_id',
375: type => 'MEDIUMINT UNSIGNED',
376: restrictions => 'NOT NULL' },
377: { name => 'student_id',
378: type => 'MEDIUMINT UNSIGNED',
379: restrictions => 'NOT NULL' },
380: { name => 'part_id',
381: type => 'MEDIUMINT UNSIGNED',
382: restrictions => 'NOT NULL' },
1.73 matthew 383: { name => 'part',
1.132 matthew 384: type => 'VARCHAR(100) BINARY',
1.73 matthew 385: restrictions => 'NOT NULL'},
1.57 matthew 386: { name => 'solved',
387: type => 'TINYTEXT' },
388: { name => 'tries',
389: type => 'SMALLINT UNSIGNED' },
390: { name => 'awarded',
1.127 matthew 391: type => 'REAL' },
1.57 matthew 392: { name => 'award',
393: type => 'TINYTEXT' },
394: { name => 'awarddetail',
395: type => 'TINYTEXT' },
396: { name => 'timestamp',
397: type => 'INT UNSIGNED'},
398: ],
399: 'PRIMARY KEY' => ['symb_id','student_id','part_id'],
400: 'KEY' => [{ columns=>['student_id'] },
401: { columns=>['symb_id'] },],
402: };
403: #
1.89 matthew 404: my $fulldump_part_table_def = {
405: id => $fulldump_part_table,
406: permanent => 'no',
407: columns => [
408: { name => 'symb_id',
409: type => 'MEDIUMINT UNSIGNED',
410: restrictions => 'NOT NULL' },
411: { name => 'part_id',
412: type => 'MEDIUMINT UNSIGNED',
413: restrictions => 'NOT NULL' },
414: { name => 'student_id',
415: type => 'MEDIUMINT UNSIGNED',
416: restrictions => 'NOT NULL' },
417: { name => 'transaction',
418: type => 'MEDIUMINT UNSIGNED',
419: restrictions => 'NOT NULL' },
420: { name => 'tries',
421: type => 'SMALLINT UNSIGNED',
422: restrictions => 'NOT NULL' },
423: { name => 'award',
424: type => 'TINYTEXT' },
425: { name => 'awarded',
1.127 matthew 426: type => 'REAL' },
1.89 matthew 427: { name => 'previous',
428: type => 'SMALLINT UNSIGNED' },
429: # { name => 'regrader',
430: # type => 'TINYTEXT' },
431: # { name => 'afterduedate',
432: # type => 'TINYTEXT' },
433: ],
434: 'PRIMARY KEY' => ['symb_id','part_id','student_id','transaction'],
435: 'KEY' => [
436: { columns=>['symb_id'] },
437: { columns=>['part_id'] },
438: { columns=>['student_id'] },
439: ],
440: };
441: #
442: my $fulldump_response_table_def = {
443: id => $fulldump_response_table,
444: permanent => 'no',
445: columns => [
446: { name => 'symb_id',
447: type => 'MEDIUMINT UNSIGNED',
448: restrictions => 'NOT NULL' },
449: { name => 'part_id',
450: type => 'MEDIUMINT UNSIGNED',
451: restrictions => 'NOT NULL' },
452: { name => 'response_id',
453: type => 'MEDIUMINT UNSIGNED',
454: restrictions => 'NOT NULL' },
455: { name => 'student_id',
456: type => 'MEDIUMINT UNSIGNED',
457: restrictions => 'NOT NULL' },
458: { name => 'transaction',
459: type => 'MEDIUMINT UNSIGNED',
460: restrictions => 'NOT NULL' },
461: { name => 'awarddetail',
462: type => 'TINYTEXT' },
463: # { name => 'message',
1.132 matthew 464: # type => 'CHAR BINARY'},
1.89 matthew 465: { name => 'response_specific',
466: type => 'TINYTEXT' },
467: { name => 'response_specific_value',
468: type => 'TINYTEXT' },
469: { name => 'submission',
470: type => 'TEXT'},
471: ],
472: 'PRIMARY KEY' => ['symb_id','part_id','response_id','student_id',
473: 'transaction'],
474: 'KEY' => [
475: { columns=>['symb_id'] },
476: { columns=>['part_id','response_id'] },
477: { columns=>['student_id'] },
478: ],
479: };
480: my $fulldump_timestamp_table_def = {
481: id => $fulldump_timestamp_table,
482: permanent => 'no',
483: columns => [
484: { name => 'symb_id',
485: type => 'MEDIUMINT UNSIGNED',
486: restrictions => 'NOT NULL' },
487: { name => 'student_id',
488: type => 'MEDIUMINT UNSIGNED',
489: restrictions => 'NOT NULL' },
490: { name => 'transaction',
491: type => 'MEDIUMINT UNSIGNED',
492: restrictions => 'NOT NULL' },
493: { name => 'timestamp',
494: type => 'INT UNSIGNED'},
495: ],
496: 'PRIMARY KEY' => ['symb_id','student_id','transaction'],
497: 'KEY' => [
498: { columns=>['symb_id'] },
499: { columns=>['student_id'] },
500: { columns=>['transaction'] },
501: ],
502: };
503: #
1.57 matthew 504: my $parameters_table_def = {
505: id => $parameters_table,
506: permanent => 'no',
507: columns => [{ name => 'symb_id',
508: type => 'MEDIUMINT UNSIGNED',
509: restrictions => 'NOT NULL' },
510: { name => 'student_id',
511: type => 'MEDIUMINT UNSIGNED',
512: restrictions => 'NOT NULL' },
513: { name => 'parameter',
514: type => 'TINYTEXT',
515: restrictions => 'NOT NULL' },
516: { name => 'value',
517: type => 'MEDIUMTEXT' },
518: ],
519: 'PRIMARY KEY' => ['symb_id','student_id','parameter (255)'],
520: };
521: #
1.127 matthew 522: my $weight_table_def = {
523: id => $weight_table,
524: permanent => 'no',
525: columns => [{ name => 'symb_id',
526: type => 'MEDIUMINT UNSIGNED',
527: restrictions => 'NOT NULL' },
528: { name => 'part_id',
529: type => 'MEDIUMINT UNSIGNED',
530: restrictions => 'NOT NULL' },
531: { name => 'weight',
532: type => 'REAL',
533: restrictions => 'NOT NULL' },
534: ],
535: 'PRIMARY KEY' => ['symb_id','part_id'],
536: };
537: #
1.57 matthew 538: # Create the tables
539: my $tableid;
540: $tableid = &Apache::lonmysql::create_table($symb_table_def);
541: if (! defined($tableid)) {
542: &Apache::lonnet::logthis("error creating symb_table: ".
543: &Apache::lonmysql::get_error());
544: return 1;
545: }
546: #
547: $tableid = &Apache::lonmysql::create_table($part_table_def);
548: if (! defined($tableid)) {
549: &Apache::lonnet::logthis("error creating part_table: ".
550: &Apache::lonmysql::get_error());
551: return 2;
552: }
553: #
554: $tableid = &Apache::lonmysql::create_table($student_table_def);
555: if (! defined($tableid)) {
556: &Apache::lonnet::logthis("error creating student_table: ".
557: &Apache::lonmysql::get_error());
558: return 3;
559: }
560: #
561: $tableid = &Apache::lonmysql::create_table($performance_table_def);
562: if (! defined($tableid)) {
563: &Apache::lonnet::logthis("error creating preformance_table: ".
564: &Apache::lonmysql::get_error());
565: return 5;
566: }
567: #
568: $tableid = &Apache::lonmysql::create_table($parameters_table_def);
569: if (! defined($tableid)) {
570: &Apache::lonnet::logthis("error creating parameters_table: ".
571: &Apache::lonmysql::get_error());
572: return 6;
573: }
1.89 matthew 574: #
575: $tableid = &Apache::lonmysql::create_table($fulldump_part_table_def);
576: if (! defined($tableid)) {
577: &Apache::lonnet::logthis("error creating fulldump_part_table: ".
578: &Apache::lonmysql::get_error());
579: return 7;
580: }
581: #
582: $tableid = &Apache::lonmysql::create_table($fulldump_response_table_def);
583: if (! defined($tableid)) {
584: &Apache::lonnet::logthis("error creating fulldump_response_table: ".
585: &Apache::lonmysql::get_error());
586: return 8;
587: }
588: $tableid = &Apache::lonmysql::create_table($fulldump_timestamp_table_def);
589: if (! defined($tableid)) {
590: &Apache::lonnet::logthis("error creating fulldump_timestamp_table: ".
591: &Apache::lonmysql::get_error());
592: return 9;
593: }
1.127 matthew 594: $tableid = &Apache::lonmysql::create_table($weight_table_def);
595: if (! defined($tableid)) {
596: &Apache::lonnet::logthis("error creating weight_table: ".
597: &Apache::lonmysql::get_error());
598: return 10;
599: }
1.57 matthew 600: return 0;
1.70 matthew 601: }
602:
603: ################################################
604: ################################################
605:
606: =pod
607:
608: =item &delete_caches()
609:
1.144 matthew 610: This routine drops all the tables associated with a course from the
611: MySQL database.
612:
613: Input: course id (optional, determined by environment if omitted)
614:
615: Returns: nothing
616:
1.70 matthew 617: =cut
618:
619: ################################################
620: ################################################
621: sub delete_caches {
622: my $courseid = shift;
1.146 albertel 623: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.70 matthew 624: #
625: &setup_table_names($courseid);
626: #
627: my $dbh = &Apache::lonmysql::get_dbh();
1.89 matthew 628: foreach my $table (@Tables) {
1.70 matthew 629: my $command = 'DROP TABLE '.$table.';';
630: $dbh->do($command);
631: if ($dbh->err) {
632: &Apache::lonnet::logthis($command.' resulted in error: '.$dbh->errstr);
633: }
634: }
635: return;
1.57 matthew 636: }
637:
638: ################################################
639: ################################################
640:
641: =pod
642:
643: =item &get_part_id()
644:
645: Get the MySQL id of a problem part string.
646:
647: Input: $part
648:
649: Output: undef on error, integer $part_id on success.
650:
651: =item &get_part()
652:
653: Get the string describing a part from the MySQL id of the problem part.
654:
655: Input: $part_id
656:
657: Output: undef on error, $part string on success.
658:
659: =cut
660:
661: ################################################
662: ################################################
663:
1.61 matthew 664: my $have_read_part_table = 0;
1.57 matthew 665: my %ids_by_part;
666: my %parts_by_id;
667:
668: sub get_part_id {
669: my ($part) = @_;
1.61 matthew 670: $part = 0 if (! defined($part));
671: if (! $have_read_part_table) {
672: my @Result = &Apache::lonmysql::get_rows($part_table);
673: foreach (@Result) {
674: $ids_by_part{$_->[1]}=$_->[0];
675: }
676: $have_read_part_table = 1;
677: }
1.57 matthew 678: if (! exists($ids_by_part{$part})) {
679: &Apache::lonmysql::store_row($part_table,[undef,$part]);
680: undef(%ids_by_part);
681: my @Result = &Apache::lonmysql::get_rows($part_table);
682: foreach (@Result) {
683: $ids_by_part{$_->[1]}=$_->[0];
684: }
685: }
686: return $ids_by_part{$part} if (exists($ids_by_part{$part}));
687: return undef; # error
688: }
689:
690: sub get_part {
691: my ($part_id) = @_;
692: if (! exists($parts_by_id{$part_id}) ||
693: ! defined($parts_by_id{$part_id}) ||
694: $parts_by_id{$part_id} eq '') {
695: my @Result = &Apache::lonmysql::get_rows($part_table);
696: foreach (@Result) {
697: $parts_by_id{$_->[0]}=$_->[1];
698: }
699: }
700: return $parts_by_id{$part_id} if(exists($parts_by_id{$part_id}));
701: return undef; # error
702: }
703:
704: ################################################
705: ################################################
706:
707: =pod
708:
709: =item &get_symb_id()
710:
711: Get the MySQL id of a symb.
712:
713: Input: $symb
714:
715: Output: undef on error, integer $symb_id on success.
716:
717: =item &get_symb()
718:
719: Get the symb associated with a MySQL symb_id.
720:
721: Input: $symb_id
722:
723: Output: undef on error, $symb on success.
724:
725: =cut
726:
727: ################################################
728: ################################################
729:
1.61 matthew 730: my $have_read_symb_table = 0;
1.57 matthew 731: my %ids_by_symb;
732: my %symbs_by_id;
733:
734: sub get_symb_id {
735: my ($symb) = @_;
1.61 matthew 736: if (! $have_read_symb_table) {
737: my @Result = &Apache::lonmysql::get_rows($symb_table);
738: foreach (@Result) {
739: $ids_by_symb{$_->[1]}=$_->[0];
740: }
741: $have_read_symb_table = 1;
742: }
1.57 matthew 743: if (! exists($ids_by_symb{$symb})) {
744: &Apache::lonmysql::store_row($symb_table,[undef,$symb]);
745: undef(%ids_by_symb);
746: my @Result = &Apache::lonmysql::get_rows($symb_table);
747: foreach (@Result) {
748: $ids_by_symb{$_->[1]}=$_->[0];
749: }
750: }
751: return $ids_by_symb{$symb} if(exists( $ids_by_symb{$symb}));
752: return undef; # error
753: }
754:
755: sub get_symb {
756: my ($symb_id) = @_;
757: if (! exists($symbs_by_id{$symb_id}) ||
758: ! defined($symbs_by_id{$symb_id}) ||
759: $symbs_by_id{$symb_id} eq '') {
760: my @Result = &Apache::lonmysql::get_rows($symb_table);
761: foreach (@Result) {
762: $symbs_by_id{$_->[0]}=$_->[1];
763: }
764: }
765: return $symbs_by_id{$symb_id} if(exists( $symbs_by_id{$symb_id}));
766: return undef; # error
767: }
768:
769: ################################################
770: ################################################
771:
772: =pod
773:
774: =item &get_student_id()
775:
776: Get the MySQL id of a student.
777:
778: Input: $sname, $dom
779:
780: Output: undef on error, integer $student_id on success.
781:
782: =item &get_student()
783:
784: Get student username:domain associated with the MySQL student_id.
785:
786: Input: $student_id
787:
788: Output: undef on error, string $student (username:domain) on success.
789:
790: =cut
791:
792: ################################################
793: ################################################
794:
1.61 matthew 795: my $have_read_student_table = 0;
1.57 matthew 796: my %ids_by_student;
797: my %students_by_id;
798:
799: sub get_student_id {
800: my ($sname,$sdom) = @_;
801: my $student = $sname.':'.$sdom;
1.61 matthew 802: if (! $have_read_student_table) {
803: my @Result = &Apache::lonmysql::get_rows($student_table);
804: foreach (@Result) {
805: $ids_by_student{$_->[1]}=$_->[0];
806: }
807: $have_read_student_table = 1;
808: }
1.57 matthew 809: if (! exists($ids_by_student{$student})) {
1.113 matthew 810: &populate_student_table();
1.57 matthew 811: undef(%ids_by_student);
1.113 matthew 812: undef(%students_by_id);
1.57 matthew 813: my @Result = &Apache::lonmysql::get_rows($student_table);
814: foreach (@Result) {
815: $ids_by_student{$_->[1]}=$_->[0];
816: }
817: }
818: return $ids_by_student{$student} if(exists( $ids_by_student{$student}));
819: return undef; # error
820: }
821:
822: sub get_student {
823: my ($student_id) = @_;
824: if (! exists($students_by_id{$student_id}) ||
825: ! defined($students_by_id{$student_id}) ||
826: $students_by_id{$student_id} eq '') {
827: my @Result = &Apache::lonmysql::get_rows($student_table);
828: foreach (@Result) {
829: $students_by_id{$_->[0]}=$_->[1];
830: }
831: }
832: return $students_by_id{$student_id} if(exists($students_by_id{$student_id}));
833: return undef; # error
834: }
1.99 matthew 835:
1.113 matthew 836: sub populate_student_table {
837: my ($courseid) = @_;
838: if (! defined($courseid)) {
1.146 albertel 839: $courseid = $env{'request.course.id'};
1.113 matthew 840: }
841: #
842: &setup_table_names($courseid);
1.134 matthew 843: &init_dbs($courseid,0);
1.113 matthew 844: my $dbh = &Apache::lonmysql::get_dbh();
845: my $request = 'INSERT IGNORE INTO '.$student_table.
846: "(student,section,status) VALUES ";
1.150 albertel 847: my $cdom = $env{'course.'.$courseid.'.domain'};
848: my $cnum = $env{'course.'.$courseid.'.num'};
849: my $classlist = &get_classlist($cdom,$cnum);
1.113 matthew 850: my $student_count=0;
851: while (my ($student,$data) = each %$classlist) {
852: my ($section,$status) = ($data->[&CL_SECTION()],
853: $data->[&CL_STATUS()]);
854: if ($section eq '' || $section =~ /^\s*$/) {
855: $section = 'none';
856: }
857: $request .= "('".$student."','".$section."','".$status."'),";
858: $student_count++;
859: }
860: return if ($student_count == 0);
861: chop($request);
862: $dbh->do($request);
863: if ($dbh->err()) {
864: &Apache::lonnet::logthis("error ".$dbh->errstr().
865: " occured executing \n".
866: $request);
867: }
868: return;
869: }
870:
1.99 matthew 871: ################################################
872: ################################################
873:
874: =pod
875:
876: =item &clear_internal_caches()
877:
878: Causes the internal caches used in get_student_id, get_student,
879: get_symb_id, get_symb, get_part_id, and get_part to be undef'd.
880:
881: Needs to be called before the first operation with the MySQL database
882: for a given Apache request.
883:
884: =cut
885:
886: ################################################
887: ################################################
888: sub clear_internal_caches {
889: $have_read_part_table = 0;
890: undef(%ids_by_part);
891: undef(%parts_by_id);
892: $have_read_symb_table = 0;
893: undef(%ids_by_symb);
894: undef(%symbs_by_id);
895: $have_read_student_table = 0;
896: undef(%ids_by_student);
897: undef(%students_by_id);
898: }
899:
1.57 matthew 900: ################################################
901: ################################################
902:
903: =pod
904:
1.89 matthew 905: =item &update_full_student_data($sname,$sdom,$courseid)
906:
907: Does a lonnet::dump on a student to populate the courses tables.
908:
909: Input: $sname, $sdom, $courseid
910:
911: Output: $returnstatus
912:
913: $returnstatus is a string describing any errors that occured. 'okay' is the
914: default.
915:
916: This subroutine loads a students data using lonnet::dump and inserts
917: it into the MySQL database. The inserts are done on three tables,
918: $fulldump_response_table, $fulldump_part_table, and $fulldump_timestamp_table.
919: The INSERT calls are made directly by this subroutine, not through lonmysql
920: because we do a 'bulk'insert which takes advantage of MySQLs non-SQL
921: compliant INSERT command to insert multiple rows at a time.
922: If anything has gone wrong during this process, $returnstatus is updated with
923: a description of the error.
924:
925: Once the "fulldump" tables are updated, the tables used for chart and
926: spreadsheet (which hold only the current state of the student on their
927: homework, not historical data) are updated. If all updates have occured
1.113 matthew 928: successfully, $student_table is updated to reflect the time of the update.
1.89 matthew 929:
930: Notice we do not insert the data and immediately query it. This means it
931: is possible for there to be data returned this first time that is not
932: available the second time. CYA.
933:
934: =cut
935:
936: ################################################
937: ################################################
938: sub update_full_student_data {
939: my ($sname,$sdom,$courseid) = @_;
940: #
941: # Set up database names
942: &setup_table_names($courseid);
943: #
944: my $student_id = &get_student_id($sname,$sdom);
945: my $student = $sname.':'.$sdom;
946: #
947: my $returnstatus = 'okay';
948: #
949: # Download students data
950: my $time_of_retrieval = time;
1.157 albertel 951: my @tmp = &Apache::lonnet::dumpstore($courseid,$sdom,$sname);
1.89 matthew 952: if (@tmp && $tmp[0] =~ /^error/) {
953: $returnstatus = 'error retrieving full student data';
954: return $returnstatus;
955: } elsif (! @tmp) {
956: $returnstatus = 'okay: no student data';
957: return $returnstatus;
958: }
959: my %studentdata = @tmp;
960: #
961: # Get database handle and clean out the tables
962: my $dbh = &Apache::lonmysql::get_dbh();
963: $dbh->do('DELETE FROM '.$fulldump_response_table.' WHERE student_id='.
964: $student_id);
965: $dbh->do('DELETE FROM '.$fulldump_part_table.' WHERE student_id='.
966: $student_id);
967: $dbh->do('DELETE FROM '.$fulldump_timestamp_table.' WHERE student_id='.
968: $student_id);
969: #
970: # Parse and store the data into a form we can handle
971: my $partdata;
972: my $respdata;
973: while (my ($key,$value) = each(%studentdata)) {
974: next if ($key =~ /^(\d+):(resource$|subnum$|keys:)/);
975: my ($transaction,$symb,$parameter) = split(':',$key);
976: my $symb_id = &get_symb_id($symb);
977: if ($parameter eq 'timestamp') {
978: # We can deal with 'timestamp' right away
979: my @timestamp_storage = ($symb_id,$student_id,
980: $transaction,$value);
1.98 matthew 981: my $store_command = 'INSERT IGNORE INTO '.$fulldump_timestamp_table.
1.89 matthew 982: " VALUES ('".join("','",@timestamp_storage)."');";
983: $dbh->do($store_command);
984: if ($dbh->err()) {
985: &Apache::lonnet::logthis('unable to execute '.$store_command);
986: &Apache::lonnet::logthis($dbh->errstr());
987: }
988: next;
989: } elsif ($parameter eq 'version') {
990: next;
1.90 matthew 991: } elsif ($parameter =~ /^resource\.(.*)\.(tries|
992: award|
993: awarded|
994: previous|
995: solved|
996: awarddetail|
997: submission|
998: submissiongrading|
999: molecule)\s*$/x){
1.89 matthew 1000: # we do not have enough information to store an
1001: # entire row, so we save it up until later.
1002: my ($part_and_resp_id,$field) = ($1,$2);
1003: my ($part,$part_id,$resp,$resp_id);
1004: if ($part_and_resp_id =~ /\./) {
1005: ($part,$resp) = split(/\./,$part_and_resp_id);
1006: $part_id = &get_part_id($part);
1007: $resp_id = &get_part_id($resp);
1008: } else {
1009: $part_id = &get_part_id($part_and_resp_id);
1010: }
1.90 matthew 1011: # Deal with part specific data
1.89 matthew 1012: if ($field =~ /^(tries|award|awarded|previous)$/) {
1013: $partdata->{$symb_id}->{$part_id}->{$transaction}->{$field}=$value;
1014: }
1.90 matthew 1015: # deal with response specific data
1.89 matthew 1016: if (defined($resp_id) &&
1.100 matthew 1017: $field =~ /^(awarddetail|
1.90 matthew 1018: submission|
1019: submissiongrading|
1020: molecule)$/x) {
1.89 matthew 1021: if ($field eq 'submission') {
1022: # We have to be careful with user supplied input.
1023: # most of the time we are okay because it is escaped.
1024: # However, there is one wrinkle: submissions which end in
1025: # and odd number of '\' cause insert errors to occur.
1026: # Best trap this somehow...
1.116 matthew 1027: $value = $dbh->quote($value);
1.89 matthew 1028: }
1.90 matthew 1029: if ($field eq 'submissiongrading' ||
1030: $field eq 'molecule') {
1031: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific'}=$field;
1032: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{'response_specific_value'}=$value;
1033: } else {
1034: $respdata->{$symb_id}->{$part_id}->{$resp_id}->{$transaction}->{$field}=$value;
1035: }
1.89 matthew 1036: }
1037: }
1038: }
1039: ##
1040: ## Store the part data
1.98 matthew 1041: my $store_command = 'INSERT IGNORE INTO '.$fulldump_part_table.
1.89 matthew 1042: ' VALUES '."\n";
1043: my $store_rows = 0;
1044: while (my ($symb_id,$hash1) = each (%$partdata)) {
1045: while (my ($part_id,$hash2) = each (%$hash1)) {
1046: while (my ($transaction,$data) = each (%$hash2)) {
1047: $store_command .= "('".join("','",$symb_id,$part_id,
1048: $student_id,
1049: $transaction,
1.101 matthew 1050: $data->{'tries'},
1.89 matthew 1051: $data->{'award'},
1052: $data->{'awarded'},
1053: $data->{'previous'})."'),";
1054: $store_rows++;
1055: }
1056: }
1057: }
1058: if ($store_rows) {
1059: chop($store_command);
1060: $dbh->do($store_command);
1061: if ($dbh->err) {
1062: $returnstatus = 'error storing part data';
1063: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1064: &Apache::lonnet::logthis("While attempting\n".$store_command);
1065: }
1066: }
1067: ##
1068: ## Store the response data
1.98 matthew 1069: $store_command = 'INSERT IGNORE INTO '.$fulldump_response_table.
1.89 matthew 1070: ' VALUES '."\n";
1071: $store_rows = 0;
1072: while (my ($symb_id,$hash1) = each (%$respdata)) {
1073: while (my ($part_id,$hash2) = each (%$hash1)) {
1074: while (my ($resp_id,$hash3) = each (%$hash2)) {
1075: while (my ($transaction,$data) = each (%$hash3)) {
1.112 matthew 1076: my $submission = $data->{'submission'};
1077: # We have to be careful with user supplied input.
1078: # most of the time we are okay because it is escaped.
1079: # However, there is one wrinkle: submissions which end in
1080: # and odd number of '\' cause insert errors to occur.
1081: # Best trap this somehow...
1082: $submission = $dbh->quote($submission);
1083: $store_command .= "('".
1084: join("','",$symb_id,$part_id,
1085: $resp_id,$student_id,
1086: $transaction,
1087: $data->{'awarddetail'},
1088: $data->{'response_specific'},
1089: $data->{'response_specific_value'}).
1090: "',".$submission."),";
1.89 matthew 1091: $store_rows++;
1092: }
1093: }
1094: }
1095: }
1096: if ($store_rows) {
1097: chop($store_command);
1098: $dbh->do($store_command);
1099: if ($dbh->err) {
1100: $returnstatus = 'error storing response data';
1101: &Apache::lonnet::logthis('insert error '.$dbh->errstr());
1102: &Apache::lonnet::logthis("While attempting\n".$store_command);
1103: }
1104: }
1105: ##
1106: ## Update the students "current" data in the performance
1107: ## and parameters tables.
1108: my ($status,undef) = &store_student_data
1109: ($sname,$sdom,$courseid,
1110: &Apache::lonnet::convert_dump_to_currentdump(\%studentdata));
1111: if ($returnstatus eq 'okay' && $status ne 'okay') {
1112: $returnstatus = 'error storing current data:'.$status;
1113: } elsif ($status ne 'okay') {
1114: $returnstatus .= ' error storing current data:'.$status;
1115: }
1116: ##
1117: ## Update the students time......
1118: if ($returnstatus eq 'okay') {
1.113 matthew 1119: &store_updatetime($student_id,$time_of_retrieval,$time_of_retrieval);
1120: if ($dbh->err) {
1121: if ($returnstatus eq 'okay') {
1122: $returnstatus = 'error updating student time';
1123: } else {
1124: $returnstatus = 'error updating student time';
1125: }
1126: }
1.89 matthew 1127: }
1128: return $returnstatus;
1129: }
1130:
1131: ################################################
1132: ################################################
1133:
1134: =pod
1135:
1.57 matthew 1136: =item &update_student_data()
1137:
1138: Input: $sname, $sdom, $courseid
1139:
1140: Output: $returnstatus, \%student_data
1141:
1142: $returnstatus is a string describing any errors that occured. 'okay' is the
1143: default.
1144: \%student_data is the data returned by a call to lonnet::currentdump.
1145:
1146: This subroutine loads a students data using lonnet::currentdump and inserts
1147: it into the MySQL database. The inserts are done on two tables,
1148: $performance_table and $parameters_table. $parameters_table holds the data
1149: that is not included in $performance_table. See the description of
1150: $performance_table elsewhere in this file. The INSERT calls are made
1151: directly by this subroutine, not through lonmysql because we do a 'bulk'
1152: insert which takes advantage of MySQLs non-SQL compliant INSERT command to
1153: insert multiple rows at a time. If anything has gone wrong during this
1154: process, $returnstatus is updated with a description of the error and
1155: \%student_data is returned.
1156:
1157: Notice we do not insert the data and immediately query it. This means it
1158: is possible for there to be data returned this first time that is not
1159: available the second time. CYA.
1160:
1161: =cut
1162:
1163: ################################################
1164: ################################################
1165: sub update_student_data {
1166: my ($sname,$sdom,$courseid) = @_;
1167: #
1.60 matthew 1168: # Set up database names
1169: &setup_table_names($courseid);
1170: #
1.57 matthew 1171: my $student_id = &get_student_id($sname,$sdom);
1172: my $student = $sname.':'.$sdom;
1173: #
1174: my $returnstatus = 'okay';
1175: #
1176: # Download students data
1177: my $time_of_retrieval = time;
1178: my @tmp = &Apache::lonnet::currentdump($courseid,$sdom,$sname);
1179: if ((scalar(@tmp) > 0) && ($tmp[0] =~ /^error:/)) {
1180: &Apache::lonnet::logthis('error getting data for '.
1181: $sname.':'.$sdom.' in course '.$courseid.
1182: ':'.$tmp[0]);
1183: $returnstatus = 'error getting data';
1.79 matthew 1184: return ($returnstatus,undef);
1.57 matthew 1185: }
1186: if (scalar(@tmp) < 1) {
1187: return ('no data',undef);
1188: }
1189: my %student_data = @tmp;
1.89 matthew 1190: my @Results = &store_student_data($sname,$sdom,$courseid,\%student_data);
1191: #
1192: # Set the students update time
1.96 matthew 1193: if ($Results[0] eq 'okay') {
1.148 matthew 1194: &store_updatetime($student_id,$time_of_retrieval);
1.95 matthew 1195: }
1.89 matthew 1196: #
1197: return @Results;
1198: }
1199:
1.113 matthew 1200: sub store_updatetime {
1201: my ($student_id,$updatetime,$fullupdatetime)=@_;
1202: my $values = '';
1203: if (defined($updatetime)) {
1204: $values = 'updatetime='.$updatetime.' ';
1205: }
1206: if (defined($fullupdatetime)) {
1207: if ($values ne '') {
1208: $values .= ',';
1209: }
1210: $values .= 'fullupdatetime='.$fullupdatetime.' ';
1211: }
1212: return if ($values eq '');
1213: my $dbh = &Apache::lonmysql::get_dbh();
1214: my $request = 'UPDATE '.$student_table.' SET '.$values.
1215: ' WHERE student_id='.$student_id.' LIMIT 1';
1216: $dbh->do($request);
1217: }
1218:
1.89 matthew 1219: sub store_student_data {
1220: my ($sname,$sdom,$courseid,$student_data) = @_;
1221: #
1222: my $student_id = &get_student_id($sname,$sdom);
1223: my $student = $sname.':'.$sdom;
1224: #
1225: my $returnstatus = 'okay';
1.57 matthew 1226: #
1227: # Remove all of the students data from the table
1.60 matthew 1228: my $dbh = &Apache::lonmysql::get_dbh();
1229: $dbh->do('DELETE FROM '.$performance_table.' WHERE student_id='.
1230: $student_id);
1231: $dbh->do('DELETE FROM '.$parameters_table.' WHERE student_id='.
1232: $student_id);
1.57 matthew 1233: #
1234: # Store away the data
1235: #
1236: my $starttime = Time::HiRes::time;
1237: my $elapsed = 0;
1238: my $rows_stored;
1.98 matthew 1239: my $store_parameters_command = 'INSERT IGNORE INTO '.$parameters_table.
1.60 matthew 1240: ' VALUES '."\n";
1.61 matthew 1241: my $num_parameters = 0;
1.98 matthew 1242: my $store_performance_command = 'INSERT IGNORE INTO '.$performance_table.
1.60 matthew 1243: ' VALUES '."\n";
1.79 matthew 1244: return ('error',undef) if (! defined($dbh));
1.89 matthew 1245: while (my ($current_symb,$param_hash) = each(%{$student_data})) {
1.57 matthew 1246: #
1247: # make sure the symb is set up properly
1248: my $symb_id = &get_symb_id($current_symb);
1249: #
1.147 matthew 1250: # Parameters
1.63 matthew 1251: while (my ($parameter,$value) = each(%$param_hash)) {
1252: if ($parameter !~ /(timestamp|resource\.(.*)\.(solved|tries|awarded|award|awarddetail|previous))/) {
1.147 matthew 1253: my $sql_parameter = "('".join("','",
1254: $symb_id,$student_id,
1255: $parameter)."',".
1256: $dbh->quote($value)."),\n";
1.61 matthew 1257: $num_parameters ++;
1.147 matthew 1258: if ($sql_parameter !~ /''/) {
1259: $store_parameters_command .= $sql_parameter;
1.149 albertel 1260: #$rows_stored++;
1.57 matthew 1261: }
1262: }
1.147 matthew 1263: }
1264: # Performance
1265: my %stored;
1266: while (my ($parameter,$value) = each(%$param_hash)) {
1267: next if ($parameter !~ /^resource\.(.*)\.(solved|awarded)$/);
1268: my $part = $1;
1.151 albertel 1269: next if ($part =~ /\./);
1.147 matthew 1270: next if (exists($stored{$part}));
1271: $stored{$part}++;
1.57 matthew 1272: #
1273: my $part_id = &get_part_id($part);
1274: next if (!defined($part_id));
1275: my $solved = $value;
1276: my $tries = $param_hash->{'resource.'.$part.'.tries'};
1277: my $awarded = $param_hash->{'resource.'.$part.'.awarded'};
1278: my $award = $param_hash->{'resource.'.$part.'.award'};
1279: my $awarddetail = $param_hash->{'resource.'.$part.'.awarddetail'};
1280: my $timestamp = $param_hash->{'timestamp'};
1.60 matthew 1281: #
1.74 matthew 1282: $solved = '' if (! defined($solved));
1.57 matthew 1283: $tries = '' if (! defined($tries));
1284: $awarded = '' if (! defined($awarded));
1285: $award = '' if (! defined($award));
1286: $awarddetail = '' if (! defined($awarddetail));
1.147 matthew 1287: my $sql_performance =
1288: "('".join("','",$symb_id,$student_id,$part_id,$part,
1289: $solved,$tries,$awarded,$award,
1290: $awarddetail,$timestamp)."'),\n";
1291: $store_performance_command .= $sql_performance;
1.57 matthew 1292: $rows_stored++;
1293: }
1294: }
1.149 albertel 1295: if (! $rows_stored) { return ($returnstatus, undef); }
1.147 matthew 1296: $store_parameters_command =~ s|,\n$||;
1297: $store_performance_command =~ s|,\n$||;
1.57 matthew 1298: my $start = Time::HiRes::time;
1.94 matthew 1299: $dbh->do($store_performance_command);
1300: if ($dbh->err()) {
1.147 matthew 1301: &Apache::lonnet::logthis('performance bigass insert error:'.
1302: $dbh->errstr());
1303: &Apache::lonnet::logthis('command = '.$/.$store_performance_command);
1.94 matthew 1304: $returnstatus = 'error: unable to insert performance into database';
1305: return ($returnstatus,$student_data);
1306: }
1.61 matthew 1307: $dbh->do($store_parameters_command) if ($num_parameters>0);
1.57 matthew 1308: if ($dbh->err()) {
1.147 matthew 1309: &Apache::lonnet::logthis('parameters bigass insert error:'.
1310: $dbh->errstr());
1311: &Apache::lonnet::logthis('command = '.$/.$store_parameters_command);
1.87 matthew 1312: &Apache::lonnet::logthis('rows_stored = '.$rows_stored);
1313: &Apache::lonnet::logthis('student_id = '.$student_id);
1.57 matthew 1314: $returnstatus = 'error: unable to insert parameters into database';
1.89 matthew 1315: return ($returnstatus,$student_data);
1.57 matthew 1316: }
1317: $elapsed += Time::HiRes::time - $start;
1.89 matthew 1318: return ($returnstatus,$student_data);
1.57 matthew 1319: }
1320:
1.89 matthew 1321: ######################################
1322: ######################################
1.57 matthew 1323:
1324: =pod
1325:
1.89 matthew 1326: =item &ensure_tables_are_set_up($courseid)
1.57 matthew 1327:
1.89 matthew 1328: Checks to be sure the MySQL tables for the given class are set up.
1329: If $courseid is omitted it will be obtained from the environment.
1.57 matthew 1330:
1.89 matthew 1331: Returns nothing on success and 'error' on failure
1.57 matthew 1332:
1333: =cut
1334:
1.89 matthew 1335: ######################################
1336: ######################################
1337: sub ensure_tables_are_set_up {
1338: my ($courseid) = @_;
1.146 albertel 1339: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.61 matthew 1340: #
1341: # Clean out package variables
1.57 matthew 1342: &setup_table_names($courseid);
1343: #
1344: # if the tables do not exist, make them
1345: my @CurrentTable = &Apache::lonmysql::tables_in_db();
1.113 matthew 1346: my ($found_symb,$found_student,$found_part,
1.89 matthew 1347: $found_performance,$found_parameters,$found_fulldump_part,
1.127 matthew 1348: $found_fulldump_response,$found_fulldump_timestamp,
1349: $found_weight);
1.57 matthew 1350: foreach (@CurrentTable) {
1351: $found_symb = 1 if ($_ eq $symb_table);
1352: $found_student = 1 if ($_ eq $student_table);
1353: $found_part = 1 if ($_ eq $part_table);
1354: $found_performance = 1 if ($_ eq $performance_table);
1355: $found_parameters = 1 if ($_ eq $parameters_table);
1.89 matthew 1356: $found_fulldump_part = 1 if ($_ eq $fulldump_part_table);
1357: $found_fulldump_response = 1 if ($_ eq $fulldump_response_table);
1358: $found_fulldump_timestamp = 1 if ($_ eq $fulldump_timestamp_table);
1.127 matthew 1359: $found_weight = 1 if ($_ eq $weight_table);
1.57 matthew 1360: }
1.127 matthew 1361: if (!$found_symb ||
1362: !$found_student || !$found_part ||
1363: !$found_performance || !$found_parameters ||
1.89 matthew 1364: !$found_fulldump_part || !$found_fulldump_response ||
1.127 matthew 1365: !$found_fulldump_timestamp || !$found_weight ) {
1.134 matthew 1366: if (&init_dbs($courseid,1)) {
1.89 matthew 1367: return 'error';
1.57 matthew 1368: }
1369: }
1.89 matthew 1370: }
1371:
1372: ################################################
1373: ################################################
1374:
1375: =pod
1376:
1377: =item &ensure_current_data()
1378:
1379: Input: $sname, $sdom, $courseid
1380:
1381: Output: $status, $data
1382:
1383: This routine ensures the data for a given student is up to date.
1.113 matthew 1384: The $student_table is queried to determine the time of the last update.
1.89 matthew 1385: If the students data is out of date, &update_student_data() is called.
1386: The return values from the call to &update_student_data() are returned.
1387:
1388: =cut
1389:
1390: ################################################
1391: ################################################
1392: sub ensure_current_data {
1393: my ($sname,$sdom,$courseid) = @_;
1394: my $status = 'okay'; # return value
1395: #
1.146 albertel 1396: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.89 matthew 1397: &ensure_tables_are_set_up($courseid);
1.57 matthew 1398: #
1399: # Get the update time for the user
1400: my $updatetime = 0;
1.60 matthew 1401: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1402: ($sdom,$sname,$courseid.'.db',
1403: $Apache::lonnet::perlvar{'lonUsersDir'});
1.57 matthew 1404: #
1.87 matthew 1405: my $student_id = &get_student_id($sname,$sdom);
1.113 matthew 1406: my @Result = &Apache::lonmysql::get_rows($student_table,
1.87 matthew 1407: "student_id ='$student_id'");
1.57 matthew 1408: my $data = undef;
1409: if (@Result) {
1.113 matthew 1410: $updatetime = $Result[0]->[5]; # Ack! This is dumb!
1.57 matthew 1411: }
1412: if ($modifiedtime > $updatetime) {
1413: ($status,$data) = &update_student_data($sname,$sdom,$courseid);
1414: }
1415: return ($status,$data);
1416: }
1417:
1418: ################################################
1419: ################################################
1420:
1421: =pod
1422:
1.89 matthew 1423: =item &ensure_current_full_data($sname,$sdom,$courseid)
1424:
1425: Input: $sname, $sdom, $courseid
1426:
1427: Output: $status
1428:
1429: This routine ensures the fulldata (the data from a lonnet::dump, not a
1430: lonnet::currentdump) for a given student is up to date.
1.113 matthew 1431: The $student_table is queried to determine the time of the last update.
1.89 matthew 1432: If the students fulldata is out of date, &update_full_student_data() is
1433: called.
1434:
1435: The return value from the call to &update_full_student_data() is returned.
1436:
1437: =cut
1438:
1439: ################################################
1440: ################################################
1441: sub ensure_current_full_data {
1442: my ($sname,$sdom,$courseid) = @_;
1443: my $status = 'okay'; # return value
1444: #
1.146 albertel 1445: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.89 matthew 1446: &ensure_tables_are_set_up($courseid);
1447: #
1448: # Get the update time for the user
1449: my $modifiedtime = &Apache::lonnet::GetFileTimestamp
1450: ($sdom,$sname,$courseid.'.db',
1451: $Apache::lonnet::perlvar{'lonUsersDir'});
1452: #
1453: my $student_id = &get_student_id($sname,$sdom);
1.113 matthew 1454: my @Result = &Apache::lonmysql::get_rows($student_table,
1.89 matthew 1455: "student_id ='$student_id'");
1456: my $updatetime;
1457: if (@Result && ref($Result[0]) eq 'ARRAY') {
1.113 matthew 1458: $updatetime = $Result[0]->[6];
1.89 matthew 1459: }
1460: if (! defined($updatetime) || $modifiedtime > $updatetime) {
1461: $status = &update_full_student_data($sname,$sdom,$courseid);
1462: }
1463: return $status;
1464: }
1465:
1466: ################################################
1467: ################################################
1468:
1469: =pod
1470:
1.57 matthew 1471: =item &get_student_data_from_performance_cache()
1472:
1473: Input: $sname, $sdom, $symb, $courseid
1474:
1475: Output: hash reference containing the data for the given student.
1476: If $symb is undef, all the students data is returned.
1477:
1478: This routine is the heart of the local caching system. See the description
1479: of $performance_table, $symb_table, $student_table, and $part_table. The
1480: main task is building the MySQL request. The tables appear in the request
1481: in the order in which they should be parsed by MySQL. When searching
1482: on a student the $student_table is used to locate the 'student_id'. All
1483: rows in $performance_table which have a matching 'student_id' are returned,
1484: with data from $part_table and $symb_table which match the entries in
1485: $performance_table, 'part_id' and 'symb_id'. When searching on a symb,
1486: the $symb_table is processed first, with matching rows grabbed from
1487: $performance_table and filled in from $part_table and $student_table in
1488: that order.
1489:
1490: Running 'EXPLAIN ' on the 'SELECT' statements generated can be quite
1491: interesting, especially if you play with the order the tables are listed.
1492:
1493: =cut
1494:
1495: ################################################
1496: ################################################
1497: sub get_student_data_from_performance_cache {
1498: my ($sname,$sdom,$symb,$courseid)=@_;
1499: my $student = $sname.':'.$sdom if (defined($sname) && defined($sdom));
1.61 matthew 1500: &setup_table_names($courseid);
1.57 matthew 1501: #
1502: # Return hash
1503: my $studentdata;
1504: #
1505: my $dbh = &Apache::lonmysql::get_dbh();
1506: my $request = "SELECT ".
1.73 matthew 1507: "d.symb,a.part,a.solved,a.tries,a.awarded,a.award,a.awarddetail,".
1.63 matthew 1508: "a.timestamp ";
1.57 matthew 1509: if (defined($student)) {
1510: $request .= "FROM $student_table AS b ".
1511: "LEFT JOIN $performance_table AS a ON b.student_id=a.student_id ".
1.73 matthew 1512: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1513: "LEFT JOIN $symb_table AS d ON d.symb_id = a.symb_id ".
1514: "WHERE student='$student'";
1515: if (defined($symb) && $symb ne '') {
1.67 matthew 1516: $request .= " AND d.symb=".$dbh->quote($symb);
1.57 matthew 1517: }
1518: } elsif (defined($symb) && $symb ne '') {
1519: $request .= "FROM $symb_table as d ".
1520: "LEFT JOIN $performance_table AS a ON d.symb_id=a.symb_id ".
1.73 matthew 1521: # "LEFT JOIN $part_table AS c ON c.part_id = a.part_id ".
1.57 matthew 1522: "LEFT JOIN $student_table AS b ON b.student_id = a.student_id ".
1523: "WHERE symb='".$dbh->quote($symb)."'";
1524: }
1525: my $starttime = Time::HiRes::time;
1526: my $rows_retrieved = 0;
1527: my $sth = $dbh->prepare($request);
1528: $sth->execute();
1529: if ($sth->err()) {
1530: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1531: &Apache::lonnet::logthis("\n".$request."\n");
1532: &Apache::lonnet::logthis("error is:".$sth->errstr());
1533: return undef;
1534: }
1535: foreach my $row (@{$sth->fetchall_arrayref}) {
1536: $rows_retrieved++;
1.63 matthew 1537: my ($symb,$part,$solved,$tries,$awarded,$award,$awarddetail,$time) =
1.57 matthew 1538: (@$row);
1539: my $base = 'resource.'.$part;
1540: $studentdata->{$symb}->{$base.'.solved'} = $solved;
1541: $studentdata->{$symb}->{$base.'.tries'} = $tries;
1542: $studentdata->{$symb}->{$base.'.awarded'} = $awarded;
1543: $studentdata->{$symb}->{$base.'.award'} = $award;
1544: $studentdata->{$symb}->{$base.'.awarddetail'} = $awarddetail;
1545: $studentdata->{$symb}->{'timestamp'} = $time if (defined($time) && $time ne '');
1.67 matthew 1546: }
1.97 matthew 1547: ## Get misc parameters
1548: $request = 'SELECT c.symb,a.parameter,a.value '.
1549: "FROM $student_table AS b ".
1550: "LEFT JOIN $parameters_table AS a ON b.student_id=a.student_id ".
1551: "LEFT JOIN $symb_table AS c ON c.symb_id = a.symb_id ".
1552: "WHERE student='$student'";
1553: if (defined($symb) && $symb ne '') {
1554: $request .= " AND c.symb=".$dbh->quote($symb);
1555: }
1556: $sth = $dbh->prepare($request);
1557: $sth->execute();
1558: if ($sth->err()) {
1559: &Apache::lonnet::logthis("Unable to execute MySQL request:");
1560: &Apache::lonnet::logthis("\n".$request."\n");
1561: &Apache::lonnet::logthis("error is:".$sth->errstr());
1562: if (defined($symb) && $symb ne '') {
1563: $studentdata = $studentdata->{$symb};
1564: }
1565: return $studentdata;
1566: }
1567: #
1568: foreach my $row (@{$sth->fetchall_arrayref}) {
1569: $rows_retrieved++;
1570: my ($symb,$parameter,$value) = (@$row);
1571: $studentdata->{$symb}->{$parameter} = $value;
1572: }
1573: #
1.67 matthew 1574: if (defined($symb) && $symb ne '') {
1575: $studentdata = $studentdata->{$symb};
1.57 matthew 1576: }
1577: return $studentdata;
1578: }
1579:
1580: ################################################
1581: ################################################
1582:
1583: =pod
1584:
1585: =item &get_current_state()
1586:
1587: Input: $sname,$sdom,$symb,$courseid
1588:
1589: Output: Described below
1.46 matthew 1590:
1.47 matthew 1591: Retrieve the current status of a students performance. $sname and
1.46 matthew 1592: $sdom are the only required parameters. If $symb is undef the results
1.47 matthew 1593: of an &Apache::lonnet::currentdump() will be returned.
1.46 matthew 1594: If $courseid is undef it will be retrieved from the environment.
1595:
1596: The return structure is based on &Apache::lonnet::currentdump. If
1597: $symb is unspecified, all the students data is returned in a hash of
1598: the form:
1599: (
1600: symb1 => { param1 => value1, param2 => value2 ... },
1601: symb2 => { param1 => value1, param2 => value2 ... },
1602: )
1603:
1604: If $symb is specified, a hash of
1605: (
1606: param1 => value1,
1607: param2 => value2,
1608: )
1609: is returned.
1610:
1.57 matthew 1611: If no data is found for $symb, or if the student has no performance data,
1.46 matthew 1612: an empty list is returned.
1613:
1614: =cut
1615:
1616: ################################################
1617: ################################################
1618: sub get_current_state {
1.47 matthew 1619: my ($sname,$sdom,$symb,$courseid,$forcedownload)=@_;
1620: #
1.146 albertel 1621: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.47 matthew 1622: #
1.61 matthew 1623: return () if (! defined($sname) || ! defined($sdom));
1624: #
1.57 matthew 1625: my ($status,$data) = &ensure_current_data($sname,$sdom,$courseid);
1.77 matthew 1626: # &Apache::lonnet::logthis
1627: # ('sname = '.$sname.
1628: # ' domain = '.$sdom.
1629: # ' status = '.$status.
1630: # ' data is '.(defined($data)?'defined':'undefined'));
1.73 matthew 1631: # while (my ($symb,$hash) = each(%$data)) {
1632: # &Apache::lonnet::logthis($symb."\n----------------------------------");
1633: # while (my ($key,$value) = each (%$hash)) {
1634: # &Apache::lonnet::logthis(" ".$key." = ".$value);
1635: # }
1636: # }
1.47 matthew 1637: #
1.79 matthew 1638: if (defined($data) && defined($symb) && ref($data->{$symb})) {
1639: return %{$data->{$symb}};
1640: } elsif (defined($data) && ! defined($symb) && ref($data)) {
1641: return %$data;
1642: }
1643: if ($status eq 'no data') {
1.57 matthew 1644: return ();
1645: } else {
1646: if ($status ne 'okay' && $status ne '') {
1647: &Apache::lonnet::logthis('status = '.$status);
1.47 matthew 1648: return ();
1649: }
1.57 matthew 1650: my $returnhash = &get_student_data_from_performance_cache($sname,$sdom,
1651: $symb,$courseid);
1652: return %$returnhash if (defined($returnhash));
1.46 matthew 1653: }
1.57 matthew 1654: return ();
1.61 matthew 1655: }
1656:
1657: ################################################
1658: ################################################
1659:
1660: =pod
1661:
1662: =item &get_problem_statistics()
1663:
1664: Gather data on a given problem. The database is assumed to be
1665: populated and all local caching variables are assumed to be set
1666: properly. This means you need to call &ensure_current_data for
1667: the students you are concerned with prior to calling this routine.
1668:
1.124 matthew 1669: Inputs: $Sections, $status, $symb, $part, $courseid, $starttime, $endtime
1.61 matthew 1670:
1.64 matthew 1671: =over 4
1672:
1.124 matthew 1673: =item $Sections Array ref containing section names for students.
1674: 'all' is allowed to be the first (and only) item in the array.
1675:
1676: =item $status String describing the status of students
1.64 matthew 1677:
1678: =item $symb is the symb for the problem.
1679:
1680: =item $part is the part id you need statistics for
1681:
1682: =item $courseid is the course id, of course!
1683:
1.122 matthew 1684: =item $starttime and $endtime are unix times which to use to limit
1685: the statistical data.
1686:
1.64 matthew 1687: =back
1688:
1.66 matthew 1689: Outputs: See the code for up to date information. A hash reference is
1690: returned. The hash has the following keys defined:
1.64 matthew 1691:
1692: =over 4
1693:
1.66 matthew 1694: =item num_students The number of students attempting the problem
1695:
1696: =item tries The total number of tries for the students
1697:
1698: =item max_tries The maximum number of tries taken
1699:
1700: =item mean_tries The average number of tries
1701:
1702: =item num_solved The number of students able to solve the problem
1703:
1704: =item num_override The number of students whose answer is 'correct_by_override'
1705:
1706: =item deg_of_diff The degree of difficulty of the problem
1707:
1708: =item std_tries The standard deviation of the number of tries
1709:
1710: =item skew_tries The skew of the number of tries
1.64 matthew 1711:
1.66 matthew 1712: =item per_wrong The number of students attempting the problem who were not
1713: able to answer it correctly.
1.64 matthew 1714:
1715: =back
1716:
1.61 matthew 1717: =cut
1718:
1719: ################################################
1720: ################################################
1721: sub get_problem_statistics {
1.122 matthew 1722: my ($Sections,$status,$symb,$part,$courseid,$starttime,$endtime) = @_;
1.61 matthew 1723: return if (! defined($symb) || ! defined($part));
1.146 albertel 1724: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.61 matthew 1725: #
1.100 matthew 1726: &setup_table_names($courseid);
1.61 matthew 1727: my $symb_id = &get_symb_id($symb);
1728: my $part_id = &get_part_id($part);
1729: my $stats_table = $courseid.'_problem_stats';
1730: #
1731: my $dbh = &Apache::lonmysql::get_dbh();
1732: return undef if (! defined($dbh));
1733: #
1.123 matthew 1734: # Clean out the table
1.61 matthew 1735: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1736: my $request =
1.115 matthew 1737: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
1738: 'SELECT a.student_id,a.solved,a.award,a.awarded,a.tries '.
1739: 'FROM '.$performance_table.' AS a ';
1.123 matthew 1740: #
1741: # See if we need to include some requirements on the students
1.115 matthew 1742: if ((defined($Sections) && lc($Sections->[0]) ne 'all') ||
1743: (defined($status) && lc($status) ne 'any')) {
1744: $request .= 'NATURAL LEFT JOIN '.$student_table.' AS b ';
1745: }
1746: $request .= ' WHERE a.symb_id='.$symb_id.' AND a.part_id='.$part_id;
1.123 matthew 1747: #
1748: # Limit the students included to those specified
1.115 matthew 1749: if (defined($Sections) && lc($Sections->[0]) ne 'all') {
1.64 matthew 1750: $request .= ' AND ('.
1.115 matthew 1751: join(' OR ', map { "b.section='".$_."'" } @$Sections
1.64 matthew 1752: ).')';
1753: }
1.115 matthew 1754: if (defined($status) && lc($status) ne 'any') {
1755: $request .= " AND b.status='".$status."'";
1.122 matthew 1756: }
1757: #
1.123 matthew 1758: # Limit by starttime and endtime
1.122 matthew 1759: my $time_requirements = undef;
1760: if (defined($starttime)) {
1761: $time_requirements .= 'a.timestamp>='.$starttime;
1762: if (defined($endtime)) {
1763: $time_requirements .= ' AND a.timestamp<='.$endtime;
1764: }
1765: } elsif (defined($endtime)) {
1766: $time_requirements .= 'a.timestamp<='.$endtime;
1767: }
1768: if (defined($time_requirements)) {
1769: $request .= ' AND '.$time_requirements;
1.115 matthew 1770: }
1.123 matthew 1771: #
1772: # Finally, execute the request to create the temporary table
1.61 matthew 1773: $dbh->do($request);
1.123 matthew 1774: #
1775: # Collect the first suite of statistics
1.128 matthew 1776: $request = 'SELECT COUNT(*),SUM(tries),'.
1777: 'AVG(tries),STD(tries) '.
1.109 matthew 1778: 'FROM '.$stats_table;
1.128 matthew 1779: my ($num,$tries,$mean,$STD) = &execute_SQL_request
1.109 matthew 1780: ($dbh,$request);
1.128 matthew 1781: #
1782: $request = 'SELECT MAX(tries),MIN(tries) FROM '.$stats_table.
1783: ' WHERE awarded>0';
1784: if (defined($time_requirements)) {
1785: $request .= ' AND '.$time_requirements;
1786: }
1787: my ($max,$min) = &execute_SQL_request($dbh,$request);
1788: #
1.109 matthew 1789: $request = 'SELECT SUM(awarded) FROM '.$stats_table;
1.128 matthew 1790: if (defined($time_requirements)) {
1791: $request .= ' AND '.$time_requirements;
1792: }
1.109 matthew 1793: my ($Solved) = &execute_SQL_request($dbh,$request);
1.128 matthew 1794: #
1.109 matthew 1795: $request = 'SELECT SUM(awarded) FROM '.$stats_table.
1796: " WHERE solved='correct_by_override'";
1.128 matthew 1797: if (defined($time_requirements)) {
1798: $request .= ' AND '.$time_requirements;
1799: }
1.109 matthew 1800: my ($solved) = &execute_SQL_request($dbh,$request);
1801: #
1.133 matthew 1802: $Solved -= $solved;
1803: #
1.61 matthew 1804: $num = 0 if (! defined($num));
1805: $tries = 0 if (! defined($tries));
1.128 matthew 1806: $max = 0 if (! defined($max));
1807: $min = 0 if (! defined($min));
1.61 matthew 1808: $STD = 0 if (! defined($STD));
1.133 matthew 1809: $Solved = 0 if (! defined($Solved) || $Solved < 0);
1.61 matthew 1810: $solved = 0 if (! defined($solved));
1811: #
1.123 matthew 1812: # Compute the more complicated statistics
1.61 matthew 1813: my $DegOfDiff = 'nan';
1.66 matthew 1814: $DegOfDiff = 1-($Solved)/$tries if ($tries>0);
1.123 matthew 1815: #
1.61 matthew 1816: my $SKEW = 'nan';
1.66 matthew 1817: my $wrongpercent = 0;
1.128 matthew 1818: my $numwrong = 'nan';
1.61 matthew 1819: if ($num > 0) {
1820: ($SKEW) = &execute_SQL_request($dbh,'SELECT SQRT(SUM('.
1821: 'POWER(tries - '.$STD.',3)'.
1822: '))/'.$num.' FROM '.$stats_table);
1.128 matthew 1823: $numwrong = $num-$Solved;
1824: $wrongpercent=int(10*100*$numwrong/$num)/10;
1.61 matthew 1825: }
1826: #
1.123 matthew 1827: # Drop the temporary table
1828: $dbh->do('DROP TABLE '.$stats_table); # May return an error
1.81 matthew 1829: #
1830: # Return result
1.66 matthew 1831: return { num_students => $num,
1832: tries => $tries,
1.128 matthew 1833: max_tries => $max,
1834: min_tries => $min,
1.66 matthew 1835: mean_tries => $mean,
1836: std_tries => $STD,
1837: skew_tries => $SKEW,
1838: num_solved => $Solved,
1839: num_override => $solved,
1.128 matthew 1840: num_wrong => $numwrong,
1.66 matthew 1841: per_wrong => $wrongpercent,
1.81 matthew 1842: deg_of_diff => $DegOfDiff };
1.61 matthew 1843: }
1844:
1.127 matthew 1845: ##
1846: ## This is a helper for get_statistics
1.61 matthew 1847: sub execute_SQL_request {
1848: my ($dbh,$request)=@_;
1849: # &Apache::lonnet::logthis($request);
1850: my $sth = $dbh->prepare($request);
1.153 albertel 1851: if (!$sth) {
1852: die($dbh->errstr . " SQL: $request");
1853: }
1.61 matthew 1854: $sth->execute();
1855: my $row = $sth->fetchrow_arrayref();
1856: if (ref($row) eq 'ARRAY' && scalar(@$row)>0) {
1857: return @$row;
1858: }
1859: return ();
1860: }
1.123 matthew 1861:
1.127 matthew 1862: ######################################################
1863: ######################################################
1864:
1865: =pod
1866:
1867: =item &populate_weight_table
1868:
1869: =cut
1870:
1871: ######################################################
1872: ######################################################
1873: sub populate_weight_table {
1874: my ($courseid) = @_;
1875: if (! defined($courseid)) {
1.146 albertel 1876: $courseid = $env{'request.course.id'};
1.127 matthew 1877: }
1878: #
1879: &setup_table_names($courseid);
1.144 matthew 1880: my $navmap = Apache::lonnavmaps::navmap->new();
1881: if (!defined($navmap)) {
1882: &Apache::lonnet::logthis('loncoursedata::populate_weight_table:'.$/.
1883: ' unable to get navmaps resource'.$/.
1884: ' '.join(' ',(caller)));
1885: return;
1886: }
1887: my @sequences = $navmap->retrieveResources(undef,
1888: sub { shift->is_map(); },1,0,1);
1889: my @resources;
1890: foreach my $seq (@sequences) {
1891: push(@resources,$navmap->retrieveResources($seq,
1892: sub {shift->is_problem();},
1893: 0,0,0));
1894: }
1895: if (! scalar(@resources)) {
1896: &Apache::lonnet::logthis('loncoursedata::populate_weight_table:'.$/.
1897: ' no resources returned for '.$courseid);
1.127 matthew 1898: return;
1899: }
1900: # Since we use lonnet::EXT to retrieve problem weights,
1901: # to ensure current data we must clear the caches out.
1902: &Apache::lonnet::clear_EXT_cache_status();
1903: my $dbh = &Apache::lonmysql::get_dbh();
1904: my $request = 'INSERT IGNORE INTO '.$weight_table.
1905: "(symb_id,part_id,weight) VALUES ";
1906: my $weight;
1.144 matthew 1907: foreach my $res (@resources) {
1908: my $symb_id = &get_symb_id($res->symb);
1909: foreach my $part (@{$res->parts}) {
1.127 matthew 1910: my $part_id = &get_part_id($part);
1911: $weight = &Apache::lonnet::EXT('resource.'.$part.'.weight',
1.144 matthew 1912: $res->symb,
1.127 matthew 1913: undef,undef,undef);
1914: if (!defined($weight) || ($weight eq '')) {
1915: $weight=1;
1916: }
1917: $request .= "('".$symb_id."','".$part_id."','".$weight."'),";
1918: }
1919: }
1920: $request =~ s/(,)$//;
1921: # &Apache::lonnet::logthis('request = '.$/.$request);
1922: $dbh->do($request);
1923: if ($dbh->err()) {
1924: &Apache::lonnet::logthis("error ".$dbh->errstr().
1925: " occured executing \n".
1926: $request);
1927: }
1928: return;
1929: }
1930:
1931: ##########################################################
1932: ##########################################################
1.61 matthew 1933:
1.127 matthew 1934: =pod
1935:
1.129 matthew 1936: =item &limit_by_start_end_times
1937:
1938: Build SQL WHERE condition which limits the data collected by the start
1939: and end times provided
1940:
1941: Inputs: $starttime, $endtime, $table
1942:
1943: Returns: $time_limits
1944:
1945: =cut
1946:
1947: ##########################################################
1948: ##########################################################
1949: sub limit_by_start_end_time {
1950: my ($starttime,$endtime,$table) = @_;
1951: my $time_requirements = undef;
1952: if (defined($starttime)) {
1953: $time_requirements .= $table.".timestamp>='".$starttime."'";
1954: if (defined($endtime)) {
1955: $time_requirements .= " AND ".$table.".timestamp<='".$endtime."'";
1956: }
1957: } elsif (defined($endtime)) {
1958: $time_requirements .= $table.".timestamp<='".$endtime."'";
1959: }
1960: return $time_requirements;
1961: }
1962:
1963: ##########################################################
1964: ##########################################################
1965:
1966: =pod
1967:
1.127 matthew 1968: =item &limit_by_section_and_status
1969:
1970: Build SQL WHERE condition which limits the data collected by section and
1971: student status.
1972:
1973: Inputs: $Sections (array ref)
1974: $enrollment (string: 'any', 'expired', 'active')
1975: $tablename The name of the table that holds the student data
1976:
1977: Returns: $student_requirements,$enrollment_requirements
1978:
1979: =cut
1980:
1981: ##########################################################
1982: ##########################################################
1983: sub limit_by_section_and_status {
1984: my ($Sections,$enrollment,$tablename) = @_;
1985: my $student_requirements = undef;
1986: if ( (defined($Sections) && $Sections->[0] ne 'all')) {
1987: $student_requirements = '('.
1988: join(' OR ', map { $tablename.".section='".$_."'" } @$Sections
1989: ).')';
1990: }
1991: #
1992: my $enrollment_requirements=undef;
1993: if (defined($enrollment) && $enrollment ne 'Any') {
1994: $enrollment_requirements = $tablename.".status='".$enrollment."'";
1995: }
1996: return ($student_requirements,$enrollment_requirements);
1997: }
1998:
1999: ######################################################
2000: ######################################################
2001:
2002: =pod
2003:
2004: =item rank_students_by_scores_on_resources
2005:
2006: Inputs:
2007: $resources: array ref of hash ref. Each hash ref needs key 'symb'.
2008: $Sections: array ref of sections to include,
2009: $enrollment: string,
2010: $courseid (may be omitted)
1.154 bowersj2 2011: $starttime (may be omitted)
2012: $endtime (may be omitted)
2013: $has_award_for (may be omitted)
1.127 matthew 2014:
2015: Returns; An array of arrays. The sub arrays contain a student name and
1.154 bowersj2 2016: their score on the resources. $starttime and $endtime constrain the
2017: list to awards obtained during the given time limits. $has_score_on
2018: constrains the list to those students who at least attempted the
2019: resource identified by the given symb, which is used to filter out
2020: such students for statistics that would be adversely affected by such
2021: students.
1.127 matthew 2022:
2023: =cut
2024:
2025: ######################################################
2026: ######################################################
2027: sub RNK_student { return 0; };
2028: sub RNK_score { return 1; };
2029:
2030: sub rank_students_by_scores_on_resources {
1.154 bowersj2 2031: my ($resources,$Sections,$enrollment,$courseid,$starttime,$endtime,$has_award_for) = @_;
1.127 matthew 2032: return if (! defined($resources) || ! ref($resources) eq 'ARRAY');
2033: if (! defined($courseid)) {
1.146 albertel 2034: $courseid = $env{'request.course.id'};
1.127 matthew 2035: }
2036: #
2037: &setup_table_names($courseid);
2038: my $dbh = &Apache::lonmysql::get_dbh();
2039: my ($section_limits,$enrollment_limits)=
2040: &limit_by_section_and_status($Sections,$enrollment,'b');
2041: my $symb_limits = '('.join(' OR ',map {'a.symb_id='.&get_symb_id($_);
2042: } @$resources
2043: ).')';
1.154 bowersj2 2044: my ($award_col, $award_join, $award_clause) = ('', '', '');
1.155 albertel 2045: if ($has_award_for) {
1.154 bowersj2 2046: my $resource_id = &get_symb_id($has_award_for);
2047: $award_col = ", perf.awarded";
2048: $award_join = "LEFT JOIN $performance_table AS perf ON perf.symb_id"
2049: ." = $resource_id AND perf.student_id = b.student_id ";
2050: $award_clause = "AND perf.awarded IS NOT NULL";
2051: }
1.130 matthew 2052: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
1.154 bowersj2 2053: my $request = "SELECT b.student,SUM(a.awarded*w.weight) AS score "
2054: ."$award_col FROM $performance_table AS a ".
2055: "NATURAL LEFT JOIN $weight_table AS w ".
2056: "LEFT JOIN $student_table AS b ON a.student_id=b.student_id ".
2057: "$award_join WHERE ";
1.127 matthew 2058: if (defined($section_limits)) {
2059: $request .= $section_limits.' AND ';
2060: }
2061: if (defined($enrollment_limits)) {
2062: $request .= $enrollment_limits.' AND ';
2063: }
1.130 matthew 2064: if (defined($time_limits)) {
2065: $request .= $time_limits.' AND ';
2066: }
1.127 matthew 2067: if ($symb_limits ne '()') {
2068: $request .= $symb_limits.' AND ';
2069: }
2070: $request =~ s/( AND )$//; # Remove extra conjunction
2071: $request =~ s/( WHERE )$//; # In case there were no limits placed on it
1.154 bowersj2 2072: $request .= " $award_clause GROUP BY a.student_id ORDER BY score";
1.127 matthew 2073: #&Apache::lonnet::logthis('request = '.$/.$request);
1.154 bowersj2 2074: my $sth = $dbh->prepare($request) or die "Can't prepare $request";
1.127 matthew 2075: $sth->execute();
2076: my $rows = $sth->fetchall_arrayref();
2077: return ($rows);
2078: }
2079:
2080: ########################################################
2081: ########################################################
2082:
2083: =pod
2084:
2085: =item &get_sum_of_scores
2086:
2087: Inputs: $resource (hash ref, needs {'symb'} key),
2088: $part, (the part id),
2089: $students (array ref, contents of array are scalars holding 'sname:sdom'),
2090: $courseid
2091:
2092: Returns: the sum of the score on the problem part over the students and the
2093: maximum possible value for the sum (taken from the weight table).
2094:
2095: =cut
2096:
2097: ########################################################
2098: ########################################################
2099: sub get_sum_of_scores {
1.143 matthew 2100: my ($symb,$part,$students,$courseid,$starttime,$endtime) = @_;
1.127 matthew 2101: if (! defined($courseid)) {
1.146 albertel 2102: $courseid = $env{'request.course.id'};
1.127 matthew 2103: }
1.138 matthew 2104: if (defined($students) &&
2105: ((@$students == 0) ||
2106: (@$students == 1 && (! defined($students->[0]) ||
2107: $students->[0] eq ''))
2108: )
2109: ){
2110: undef($students);
2111: }
1.127 matthew 2112: #
2113: &setup_table_names($courseid);
2114: my $dbh = &Apache::lonmysql::get_dbh();
1.130 matthew 2115: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
1.127 matthew 2116: my $request = 'SELECT SUM(a.awarded*w.weight),SUM(w.weight) FROM '.
2117: $performance_table.' AS a '.
2118: 'NATURAL LEFT JOIN '.$weight_table.' AS w ';
1.143 matthew 2119: $request .= 'WHERE a.symb_id='.&get_symb_id($symb).
1.127 matthew 2120: ' AND a.part_id='.&get_part_id($part);
1.130 matthew 2121: if (defined($time_limits)) {
2122: $request .= ' AND '.$time_limits;
2123: }
1.127 matthew 2124: if (defined($students)) {
2125: $request .= ' AND ('.
2126: join(' OR ',map {'a.student_id='.&get_student_id(split(':',$_));
2127: } @$students).
2128: ')';
2129: }
2130: my $sth = $dbh->prepare($request);
2131: $sth->execute();
2132: my $rows = $sth->fetchrow_arrayref();
2133: if ($dbh->err) {
1.138 matthew 2134: &Apache::lonnet::logthis('error 1 = '.$dbh->errstr());
2135: &Apache::lonnet::logthis('prepared then executed, fetchrow_arrayrefed'.
2136: $/.$request);
1.127 matthew 2137: return (undef,undef);
2138: }
2139: return ($rows->[0],$rows->[1]);
2140: }
2141:
1.129 matthew 2142: ########################################################
2143: ########################################################
2144:
2145: =pod
2146:
2147: =item &score_stats
2148:
2149: Inputs: $Sections, $enrollment, $symbs, $starttime,
2150: $endtime, $courseid
2151:
2152: $Sections, $enrollment, $starttime, $endtime, and $courseid are the same as
2153: elsewhere in this module.
2154: $symbs is an array ref of symbs
2155:
2156: Returns: minimum, maximum, mean, s.d., number of students, and maximum
2157: possible of student scores on the given resources
2158:
2159: =cut
2160:
2161: ########################################################
2162: ########################################################
2163: sub score_stats {
2164: my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_;
2165: if (! defined($courseid)) {
1.146 albertel 2166: $courseid = $env{'request.course.id'};
1.129 matthew 2167: }
2168: #
2169: &setup_table_names($courseid);
2170: my $dbh = &Apache::lonmysql::get_dbh();
2171: #
2172: my ($section_limits,$enrollment_limits)=
2173: &limit_by_section_and_status($Sections,$enrollment,'b');
2174: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
2175: my @Symbids = map { &get_symb_id($_); } @{$symbs};
2176: #
2177: my $stats_table = $courseid.'_problem_stats';
2178: my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids);
2179: my $request = 'DROP TABLE '.$stats_table;
2180: $dbh->do($request);
2181: $request =
2182: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
2183: 'SELECT a.student_id,'.
2184: 'SUM(a.awarded*w.weight) AS score FROM '.
2185: $performance_table.' AS a '.
2186: 'NATURAL LEFT JOIN '.$weight_table.' AS w '.
2187: 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '.
2188: 'WHERE ('.$symb_restriction.')';
2189: if ($time_limits) {
2190: $request .= ' AND '.$time_limits;
2191: }
2192: if ($section_limits) {
2193: $request .= ' AND '.$section_limits;
2194: }
2195: if ($enrollment_limits) {
2196: $request .= ' AND '.$enrollment_limits;
2197: }
2198: $request .= ' GROUP BY a.student_id';
2199: # &Apache::lonnet::logthis('request = '.$/.$request);
2200: my $sth = $dbh->prepare($request);
2201: $sth->execute();
2202: $request =
2203: 'SELECT AVG(score),STD(score),MAX(score),MIN(score),COUNT(score) '.
2204: 'FROM '.$stats_table;
2205: my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request);
2206: # &Apache::lonnet::logthis('request = '.$/.$request);
2207:
2208: $request = 'SELECT SUM(weight) FROM '.$weight_table.
1.152 bowersj2 2209: ' AS a WHERE ('.$symb_restriction.')';
1.129 matthew 2210: my ($max_possible) = &execute_SQL_request($dbh,$request);
2211: # &Apache::lonnet::logthis('request = '.$/.$request);
2212: return($min,$max,$ave,$std,$count,$max_possible);
2213: }
2214:
2215:
2216: ########################################################
2217: ########################################################
2218:
2219: =pod
2220:
2221: =item &count_stats
2222:
2223: Inputs: $Sections, $enrollment, $symbs, $starttime,
2224: $endtime, $courseid
2225:
2226: $Sections, $enrollment, $starttime, $endtime, and $courseid are the same as
2227: elsewhere in this module.
2228: $symbs is an array ref of symbs
2229:
2230: Returns: minimum, maximum, mean, s.d., and number of students
2231: of the number of items correct on the given resources
2232:
2233: =cut
2234:
2235: ########################################################
2236: ########################################################
2237: sub count_stats {
2238: my ($Sections,$enrollment,$symbs,$starttime,$endtime,$courseid)=@_;
2239: if (! defined($courseid)) {
1.146 albertel 2240: $courseid = $env{'request.course.id'};
1.129 matthew 2241: }
2242: #
2243: &setup_table_names($courseid);
2244: my $dbh = &Apache::lonmysql::get_dbh();
2245: #
2246: my ($section_limits,$enrollment_limits)=
2247: &limit_by_section_and_status($Sections,$enrollment,'b');
2248: my $time_limits = &limit_by_start_end_time($starttime,$endtime,'a');
2249: my @Symbids = map { &get_symb_id($_); } @{$symbs};
2250: #
2251: my $stats_table = $courseid.'_problem_stats';
2252: my $symb_restriction = join(' OR ',map {'a.symb_id='.$_;} @Symbids);
2253: my $request = 'DROP TABLE '.$stats_table;
2254: $dbh->do($request);
2255: $request =
2256: 'CREATE TEMPORARY TABLE '.$stats_table.' '.
2257: 'SELECT a.student_id,'.
1.151 albertel 2258: 'SUM(a.awarded) AS count FROM '.
1.129 matthew 2259: $performance_table.' AS a '.
2260: 'LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id '.
1.151 albertel 2261: 'WHERE ('.$symb_restriction.')';
1.129 matthew 2262: if ($time_limits) {
2263: $request .= ' AND '.$time_limits;
2264: }
2265: if ($section_limits) {
2266: $request .= ' AND '.$section_limits;
2267: }
2268: if ($enrollment_limits) {
2269: $request .= ' AND '.$enrollment_limits;
2270: }
2271: $request .= ' GROUP BY a.student_id';
1.131 matthew 2272: # &Apache::lonnet::logthis('request = '.$/.$request);
1.129 matthew 2273: my $sth = $dbh->prepare($request);
2274: $sth->execute();
2275: $request =
2276: 'SELECT AVG(count),STD(count),MAX(count),MIN(count),COUNT(count) '.
2277: 'FROM '.$stats_table;
2278: my ($ave,$std,$max,$min,$count) = &execute_SQL_request($dbh,$request);
1.131 matthew 2279: # &Apache::lonnet::logthis('request = '.$/.$request);
1.129 matthew 2280: return($min,$max,$ave,$std,$count);
2281: }
1.127 matthew 2282:
2283: ######################################################
2284: ######################################################
2285:
2286: =pod
2287:
2288: =item get_student_data
2289:
2290: =cut
2291:
2292: ######################################################
2293: ######################################################
1.105 matthew 2294: sub get_student_data {
2295: my ($students,$courseid) = @_;
1.146 albertel 2296: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.105 matthew 2297: &setup_table_names($courseid);
2298: my $dbh = &Apache::lonmysql::get_dbh();
2299: return undef if (! defined($dbh));
2300: my $request = 'SELECT '.
2301: 'student_id, student '.
2302: 'FROM '.$student_table;
2303: if (defined($students)) {
2304: $request .= ' WHERE ('.
2305: join(' OR ', map {'student_id='.
2306: &get_student_id($_->{'username'},
2307: $_->{'domain'})
2308: } @$students
2309: ).')';
2310: }
2311: $request.= ' ORDER BY student_id';
2312: my $sth = $dbh->prepare($request);
2313: $sth->execute();
2314: if ($dbh->err) {
1.138 matthew 2315: &Apache::lonnet::logthis('error 2 = '.$dbh->errstr());
2316: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.105 matthew 2317: return undef;
2318: }
2319: my $dataset = $sth->fetchall_arrayref();
2320: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2321: return $dataset;
2322: }
2323: }
2324:
1.108 matthew 2325: sub RD_student_id { return 0; }
2326: sub RD_awarddetail { return 1; }
2327: sub RD_response_eval { return 2; }
2328: sub RD_submission { return 3; }
2329: sub RD_timestamp { return 4; }
2330: sub RD_tries { return 5; }
2331: sub RD_sname { return 6; }
2332:
2333: sub get_response_data {
1.126 matthew 2334: my ($Sections,$enrollment,$symb,$response,$courseid) = @_;
1.103 matthew 2335: return undef if (! defined($symb) ||
1.100 matthew 2336: ! defined($response));
1.146 albertel 2337: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.100 matthew 2338: #
2339: &setup_table_names($courseid);
2340: my $symb_id = &get_symb_id($symb);
1.137 matthew 2341: if (! defined($symb_id)) {
2342: &Apache::lonnet::logthis('Unable to find symb for '.$symb.' in '.$courseid);
2343: return undef;
2344: }
1.100 matthew 2345: my $response_id = &get_part_id($response);
1.137 matthew 2346: if (! defined($response_id)) {
2347: &Apache::lonnet::logthis('Unable to find id for '.$response.' in '.$courseid);
2348: return undef;
2349: }
1.100 matthew 2350: #
2351: my $dbh = &Apache::lonmysql::get_dbh();
2352: return undef if (! defined($dbh));
1.126 matthew 2353: #
1.127 matthew 2354: my ($student_requirements,$enrollment_requirements) =
2355: &limit_by_section_and_status($Sections,$enrollment,'d');
1.100 matthew 2356: my $request = 'SELECT '.
1.105 matthew 2357: 'a.student_id, a.awarddetail, a.response_specific_value, '.
1.108 matthew 2358: 'a.submission, b.timestamp, c.tries, d.student '.
1.100 matthew 2359: 'FROM '.$fulldump_response_table.' AS a '.
2360: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2361: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2362: 'a.transaction = b.transaction '.
2363: 'LEFT JOIN '.$fulldump_part_table.' AS c '.
2364: 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '.
2365: 'a.part_id=c.part_id AND a.transaction = c.transaction '.
1.108 matthew 2366: 'LEFT JOIN '.$student_table.' AS d '.
2367: 'ON a.student_id=d.student_id '.
1.100 matthew 2368: 'WHERE '.
2369: 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id;
1.126 matthew 2370: if (defined($student_requirements) || defined($enrollment_requirements)) {
2371: $request .= ' AND ';
2372: if (defined($student_requirements)) {
2373: $request .= $student_requirements.' AND ';
2374: }
2375: if (defined($enrollment_requirements)) {
2376: $request .= $enrollment_requirements.' AND ';
2377: }
2378: $request =~ s/( AND )$//;
1.100 matthew 2379: }
2380: $request .= ' ORDER BY b.timestamp';
1.103 matthew 2381: # &Apache::lonnet::logthis("request =\n".$request);
1.100 matthew 2382: my $sth = $dbh->prepare($request);
2383: $sth->execute();
1.105 matthew 2384: if ($dbh->err) {
1.138 matthew 2385: &Apache::lonnet::logthis('error 3 = '.$dbh->errstr());
2386: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.105 matthew 2387: return undef;
2388: }
1.100 matthew 2389: my $dataset = $sth->fetchall_arrayref();
2390: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
1.117 matthew 2391: # Clear the \'s from around the submission
2392: for (my $i =0;$i<scalar(@$dataset);$i++) {
2393: $dataset->[$i]->[3] =~ s/(\'$|^\')//g;
2394: }
1.103 matthew 2395: return $dataset;
1.100 matthew 2396: }
1.118 matthew 2397: }
2398:
2399:
2400: sub RDs_awarddetail { return 3; }
2401: sub RDs_submission { return 2; }
2402: sub RDs_timestamp { return 1; }
2403: sub RDs_tries { return 0; }
1.119 matthew 2404: sub RDs_awarded { return 4; }
1.118 matthew 2405:
2406: sub get_response_data_by_student {
2407: my ($student,$symb,$response,$courseid) = @_;
2408: return undef if (! defined($symb) ||
2409: ! defined($response));
1.146 albertel 2410: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.118 matthew 2411: #
2412: &setup_table_names($courseid);
2413: my $symb_id = &get_symb_id($symb);
2414: my $response_id = &get_part_id($response);
2415: #
2416: my $student_id = &get_student_id($student->{'username'},
2417: $student->{'domain'});
2418: #
2419: my $dbh = &Apache::lonmysql::get_dbh();
2420: return undef if (! defined($dbh));
2421: my $request = 'SELECT '.
1.119 matthew 2422: 'c.tries, b.timestamp, a.submission, a.awarddetail, e.awarded '.
1.118 matthew 2423: 'FROM '.$fulldump_response_table.' AS a '.
2424: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2425: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2426: 'a.transaction = b.transaction '.
2427: 'LEFT JOIN '.$fulldump_part_table.' AS c '.
2428: 'ON a.symb_id=c.symb_id AND a.student_id=c.student_id AND '.
2429: 'a.part_id=c.part_id AND a.transaction = c.transaction '.
2430: 'LEFT JOIN '.$student_table.' AS d '.
2431: 'ON a.student_id=d.student_id '.
1.119 matthew 2432: 'LEFT JOIN '.$performance_table.' AS e '.
2433: 'ON a.symb_id=e.symb_id AND a.part_id=e.part_id AND '.
2434: 'a.student_id=e.student_id AND c.tries=e.tries '.
1.118 matthew 2435: 'WHERE '.
2436: 'a.symb_id='.$symb_id.' AND a.response_id='.$response_id.
2437: ' AND a.student_id='.$student_id.' ORDER BY b.timestamp';
1.125 matthew 2438: # &Apache::lonnet::logthis("request =\n".$request);
1.118 matthew 2439: my $sth = $dbh->prepare($request);
2440: $sth->execute();
2441: if ($dbh->err) {
1.138 matthew 2442: &Apache::lonnet::logthis('error 4 = '.$dbh->errstr());
2443: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.118 matthew 2444: return undef;
2445: }
2446: my $dataset = $sth->fetchall_arrayref();
2447: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2448: # Clear the \'s from around the submission
2449: for (my $i =0;$i<scalar(@$dataset);$i++) {
2450: $dataset->[$i]->[2] =~ s/(\'$|^\')//g;
2451: }
2452: return $dataset;
2453: }
2454: return undef; # error occurred
1.106 matthew 2455: }
1.108 matthew 2456:
2457: sub RT_student_id { return 0; }
2458: sub RT_awarded { return 1; }
2459: sub RT_tries { return 2; }
2460: sub RT_timestamp { return 3; }
1.106 matthew 2461:
2462: sub get_response_time_data {
1.142 matthew 2463: my ($sections,$enrollment,$symb,$part,$courseid) = @_;
1.106 matthew 2464: return undef if (! defined($symb) ||
1.107 matthew 2465: ! defined($part));
1.146 albertel 2466: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.106 matthew 2467: #
2468: &setup_table_names($courseid);
2469: my $symb_id = &get_symb_id($symb);
1.142 matthew 2470: if (! defined($symb_id)) {
2471: &Apache::lonnet::logthis('Unable to find symb for '.$symb.' in '.$courseid);
2472: return undef;
2473: }
1.107 matthew 2474: my $part_id = &get_part_id($part);
1.142 matthew 2475: if (! defined($part_id)) {
2476: &Apache::lonnet::logthis('Unable to find id for '.$part.' in '.$courseid);
2477: return undef;
2478: }
1.106 matthew 2479: #
2480: my $dbh = &Apache::lonmysql::get_dbh();
2481: return undef if (! defined($dbh));
1.142 matthew 2482: my ($student_requirements,$enrollment_requirements) =
2483: &limit_by_section_and_status($sections,$enrollment,'d');
1.106 matthew 2484: my $request = 'SELECT '.
1.107 matthew 2485: 'a.student_id, a.awarded, a.tries, b.timestamp '.
2486: 'FROM '.$fulldump_part_table.' AS a '.
1.142 matthew 2487: 'LEFT JOIN '.$fulldump_timestamp_table.' AS b '.
2488: 'ON a.symb_id=b.symb_id AND a.student_id=b.student_id AND '.
2489: 'a.transaction = b.transaction '.
2490: 'LEFT JOIN '.$student_table.' as d '.
2491: 'ON a.student_id=d.student_id '.
1.106 matthew 2492: 'WHERE '.
1.107 matthew 2493: 'a.symb_id='.$symb_id.' AND a.part_id='.$part_id;
1.142 matthew 2494: if (defined($student_requirements) || defined($enrollment_requirements)) {
2495: $request .= ' AND ';
2496: if (defined($student_requirements)) {
2497: $request .= $student_requirements.' AND ';
2498: }
2499: if (defined($enrollment_requirements)) {
2500: $request .= $enrollment_requirements.' AND ';
2501: }
2502: $request =~ s/( AND )$//;
1.106 matthew 2503: }
2504: $request .= ' ORDER BY b.timestamp';
2505: # &Apache::lonnet::logthis("request =\n".$request);
2506: my $sth = $dbh->prepare($request);
2507: $sth->execute();
2508: if ($dbh->err) {
1.138 matthew 2509: &Apache::lonnet::logthis('error 5 = '.$dbh->errstr());
2510: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.106 matthew 2511: return undef;
2512: }
2513: my $dataset = $sth->fetchall_arrayref();
2514: if (ref($dataset) eq 'ARRAY' && scalar(@$dataset)>0) {
2515: return $dataset;
2516: }
2517:
1.100 matthew 2518: }
1.61 matthew 2519:
2520: ################################################
2521: ################################################
2522:
2523: =pod
2524:
1.116 matthew 2525: =item &get_student_scores($Sections,$Symbs,$enrollment,$courseid)
1.113 matthew 2526:
2527: =cut
2528:
2529: ################################################
2530: ################################################
2531: sub get_student_scores {
1.145 matthew 2532: my ($sections,$Symbs,$enrollment,$courseid,$starttime,$endtime) = @_;
1.146 albertel 2533: $courseid = $env{'request.course.id'} if (! defined($courseid));
1.113 matthew 2534: &setup_table_names($courseid);
2535: my $dbh = &Apache::lonmysql::get_dbh();
2536: return (undef) if (! defined($dbh));
2537: my $tmptable = $courseid.'_temp_'.time;
1.145 matthew 2538: my $request = 'DROP TABLE IF EXISTS '.$tmptable;
2539: # &Apache::lonnet::logthis('request = '.$/.$request);
2540: $dbh->do($request);
1.114 matthew 2541: #
2542: my $symb_requirements;
1.113 matthew 2543: if (defined($Symbs) && @$Symbs) {
2544: $symb_requirements = '('.
1.114 matthew 2545: join(' OR ', map{ "(a.symb_id='".&get_symb_id($_->{'symb'}).
1.121 matthew 2546: "' AND a.part_id='".&get_part_id($_->{'part'}).
2547: "')"
1.113 matthew 2548: } @$Symbs).')';
2549: }
1.114 matthew 2550: #
1.145 matthew 2551: my ($student_requirements,$enrollment_requirements) =
2552: &limit_by_section_and_status($sections,$enrollment,'b');
1.114 matthew 2553: #
1.145 matthew 2554: my $time_requirements = &limit_by_start_end_time($starttime,$endtime,'a');
1.114 matthew 2555: ##
1.145 matthew 2556: $request = 'CREATE TEMPORARY TABLE IF NOT EXISTS '.$tmptable.
2557: ' SELECT a.student_id,SUM(a.awarded*c.weight) AS score FROM '.
1.114 matthew 2558: $performance_table.' AS a ';
1.145 matthew 2559: $request .= "LEFT JOIN ".$weight_table.' AS c ON a.symb_id=c.symb_id AND a.part_id=c.part_id ';
1.114 matthew 2560: if (defined($student_requirements) || defined($enrollment_requirements)) {
1.145 matthew 2561: $request .= ' LEFT JOIN '.$student_table.' AS b ON a.student_id=b.student_id';
1.114 matthew 2562: }
2563: if (defined($symb_requirements) ||
2564: defined($student_requirements) ||
2565: defined($enrollment_requirements) ) {
1.113 matthew 2566: $request .= ' WHERE ';
2567: }
1.114 matthew 2568: if (defined($symb_requirements)) {
2569: $request .= $symb_requirements.' AND ';
2570: }
2571: if (defined($student_requirements)) {
2572: $request .= $student_requirements.' AND ';
2573: }
2574: if (defined($enrollment_requirements)) {
2575: $request .= $enrollment_requirements.' AND ';
2576: }
1.121 matthew 2577: if (defined($time_requirements)) {
2578: $request .= $time_requirements.' AND ';
2579: }
2580: $request =~ s/ AND $//; # Strip of the trailing ' AND '.
1.114 matthew 2581: $request .= ' GROUP BY a.student_id';
2582: # &Apache::lonnet::logthis("request = \n".$request);
1.113 matthew 2583: my $sth = $dbh->prepare($request);
2584: $sth->execute();
2585: if ($dbh->err) {
1.138 matthew 2586: &Apache::lonnet::logthis('error 6 = '.$dbh->errstr());
2587: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.113 matthew 2588: return undef;
2589: }
2590: $request = 'SELECT score,COUNT(*) FROM '.$tmptable.' GROUP BY score';
2591: # &Apache::lonnet::logthis("request = \n".$request);
2592: $sth = $dbh->prepare($request);
2593: $sth->execute();
2594: if ($dbh->err) {
1.138 matthew 2595: &Apache::lonnet::logthis('error 7 = '.$dbh->errstr());
2596: &Apache::lonnet::logthis('prepared then executed '.$/.$request);
1.113 matthew 2597: return undef;
2598: }
2599: my $dataset = $sth->fetchall_arrayref();
2600: return $dataset;
2601: }
2602:
2603: ################################################
2604: ################################################
2605:
2606: =pod
2607:
1.61 matthew 2608: =item &setup_table_names()
2609:
2610: input: course id
2611:
2612: output: none
2613:
2614: Cleans up the package variables for local caching.
2615:
2616: =cut
2617:
2618: ################################################
2619: ################################################
2620: sub setup_table_names {
2621: my ($courseid) = @_;
2622: if (! defined($courseid)) {
1.146 albertel 2623: $courseid = $env{'request.course.id'};
1.61 matthew 2624: }
2625: #
2626: if (! defined($current_course) || $current_course ne $courseid) {
2627: # Clear out variables
2628: $have_read_part_table = 0;
2629: undef(%ids_by_part);
2630: undef(%parts_by_id);
2631: $have_read_symb_table = 0;
2632: undef(%ids_by_symb);
2633: undef(%symbs_by_id);
2634: $have_read_student_table = 0;
2635: undef(%ids_by_student);
2636: undef(%students_by_id);
2637: #
2638: $current_course = $courseid;
2639: }
2640: #
2641: # Set up database names
2642: my $base_id = $courseid;
2643: $symb_table = $base_id.'_'.'symb';
2644: $part_table = $base_id.'_'.'part';
2645: $student_table = $base_id.'_'.'student';
2646: $performance_table = $base_id.'_'.'performance';
2647: $parameters_table = $base_id.'_'.'parameters';
1.89 matthew 2648: $fulldump_part_table = $base_id.'_'.'partdata';
2649: $fulldump_response_table = $base_id.'_'.'responsedata';
2650: $fulldump_timestamp_table = $base_id.'_'.'timestampdata';
1.127 matthew 2651: $weight_table = $base_id.'_'.'weight';
1.89 matthew 2652: #
2653: @Tables = (
2654: $symb_table,
2655: $part_table,
2656: $student_table,
2657: $performance_table,
2658: $parameters_table,
2659: $fulldump_part_table,
2660: $fulldump_response_table,
2661: $fulldump_timestamp_table,
1.127 matthew 2662: $weight_table,
1.89 matthew 2663: );
1.61 matthew 2664: return;
1.3 stredwic 2665: }
1.1 stredwic 2666:
1.35 matthew 2667: ################################################
2668: ################################################
2669:
2670: =pod
2671:
1.57 matthew 2672: =back
2673:
2674: =item End of Local Data Caching Subroutines
2675:
2676: =cut
2677:
2678: ################################################
2679: ################################################
2680:
1.89 matthew 2681: } # End scope of table identifiers
1.57 matthew 2682:
2683: ################################################
2684: ################################################
2685:
2686: =pod
2687:
2688: =head3 Classlist Subroutines
2689:
1.35 matthew 2690: =item &get_classlist();
2691:
2692: Retrieve the classist of a given class or of the current class. Student
2693: information is returned from the classlist.db file and, if needed,
2694: from the students environment.
2695:
1.150 albertel 2696: Optional arguments are $cdom, and $cnum (course domain,
2697: and course number, respectively). If either is ommitted the course
2698: will be taken from the current environment ($env{'request.course.id'},
1.146 albertel 2699: $env{'course.'.$cid.'.domain'}, and $env{'course.'.$cid.'.num'}).
1.35 matthew 2700:
2701: Returns a reference to a hash which contains:
2702: keys '$sname:$sdom'
1.136 raeburn 2703: values [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype]
1.54 bowersj2 2704:
2705: The constant values CL_SDOM, CL_SNAME, CL_END, etc. can be used
2706: as indices into the returned list to future-proof clients against
2707: changes in the list order.
1.35 matthew 2708:
2709: =cut
2710:
2711: ################################################
2712: ################################################
1.54 bowersj2 2713:
2714: sub CL_SDOM { return 0; }
2715: sub CL_SNAME { return 1; }
2716: sub CL_END { return 2; }
2717: sub CL_START { return 3; }
2718: sub CL_ID { return 4; }
2719: sub CL_SECTION { return 5; }
2720: sub CL_FULLNAME { return 6; }
2721: sub CL_STATUS { return 7; }
1.111 raeburn 2722: sub CL_TYPE { return 8; }
1.136 raeburn 2723: sub CL_LOCKEDTYPE { return 9; }
1.35 matthew 2724:
2725: sub get_classlist {
1.150 albertel 2726: my ($cdom,$cnum) = @_;
2727: my $cid = $cdom.'_'.$cnum;
2728: if (!defined($cdom) || !defined($cnum)) {
2729: $cid = $env{'request.course.id'};
2730: $cdom = $env{'course.'.$cid.'.domain'};
2731: $cnum = $env{'course.'.$cid.'.num'};
2732: }
1.57 matthew 2733: my $now = time;
1.35 matthew 2734: #
2735: my %classlist=&Apache::lonnet::dump('classlist',$cdom,$cnum);
2736: while (my ($student,$info) = each(%classlist)) {
1.60 matthew 2737: if ($student =~ /^(con_lost|error|no_such_host)/i) {
2738: &Apache::lonnet::logthis('get_classlist error for '.$cid.':'.$student);
2739: return undef;
2740: }
1.35 matthew 2741: my ($sname,$sdom) = split(/:/,$student);
2742: my @Values = split(/:/,$info);
1.136 raeburn 2743: my ($end,$start,$id,$section,$fullname,$type,$lockedtype);
1.35 matthew 2744: if (@Values > 2) {
1.136 raeburn 2745: ($end,$start,$id,$section,$fullname,$type,$lockedtype) = @Values;
1.35 matthew 2746: } else { # We have to get the data ourselves
2747: ($end,$start) = @Values;
1.37 matthew 2748: $section = &Apache::lonnet::getsection($sdom,$sname,$cid);
1.35 matthew 2749: my %info=&Apache::lonnet::get('environment',
2750: ['firstname','middlename',
2751: 'lastname','generation','id'],
2752: $sdom, $sname);
2753: my ($tmp) = keys(%info);
2754: if ($tmp =~/^(con_lost|error|no_such_host)/i) {
2755: $fullname = 'not available';
2756: $id = 'not available';
1.38 matthew 2757: &Apache::lonnet::logthis('unable to retrieve environment '.
2758: 'for '.$sname.':'.$sdom);
1.35 matthew 2759: } else {
1.141 albertel 2760: $fullname = &Apache::lonnet::format_name(@info{qw/firstname middlename lastname generation/},'lastname');
1.35 matthew 2761: $id = $info{'id'};
2762: }
1.36 matthew 2763: # Update the classlist with this students information
2764: if ($fullname ne 'not available') {
1.141 albertel 2765: my $enrolldata = join(':',$end,$start,$id,$section,$fullname);
2766: my $reply=&Apache::lonnet::cput('classlist',
1.36 matthew 2767: {$student => $enrolldata},
2768: $cdom,$cnum);
2769: if ($reply !~ /^(ok|delayed)/) {
2770: &Apache::lonnet::logthis('Unable to update classlist for '.
2771: 'student '.$sname.':'.$sdom.
2772: ' error:'.$reply);
2773: }
2774: }
1.35 matthew 2775: }
2776: my $status='Expired';
2777: if(((!$end) || $now < $end) && ((!$start) || ($now > $start))) {
2778: $status='Active';
2779: }
2780: $classlist{$student} =
1.136 raeburn 2781: [$sdom,$sname,$end,$start,$id,$section,$fullname,$status,$type,$lockedtype];
1.35 matthew 2782: }
2783: if (wantarray()) {
2784: return (\%classlist,['domain','username','end','start','id',
1.136 raeburn 2785: 'section','fullname','status','type','lockedtype']);
1.35 matthew 2786: } else {
2787: return \%classlist;
2788: }
2789: }
2790:
1.1 stredwic 2791: # ----- END HELPER FUNCTIONS --------------------------------------------
2792:
2793: 1;
2794: __END__
1.36 matthew 2795:
1.35 matthew 2796:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>