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