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