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