Annotation of loncom/interface/loncoursedata.pm, revision 1.13
1.1 stredwic 1: # The LearningOnline Network with CAPA
2: # (Publication Handler
3: #
1.13 ! stredwic 4: # $Id: loncoursedata.pm,v 1.12 2002/08/05 14:16:19 stredwic Exp $
1.1 stredwic 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28: ###
29:
30: =pod
31:
32: =head1 NAME
33:
34: loncoursedata
35:
36: =head1 SYNOPSIS
37:
38: Set of functions that download and process student information.
39:
40: =head1 PACKAGES USED
41:
42: Apache::Constants qw(:common :http)
43: Apache::lonnet()
44: HTML::TokeParser
45: GDBM_File
46:
47: =cut
48:
49: package Apache::loncoursedata;
50:
51: use strict;
52: use Apache::Constants qw(:common :http);
53: use Apache::lonnet();
1.13 ! stredwic 54: use Apache::lonhtmlcommon;
1.1 stredwic 55: use HTML::TokeParser;
56: use GDBM_File;
57:
58: =pod
59:
60: =head1 DOWNLOAD INFORMATION
61:
62: This section contains all the files that get data from other servers
63: and/or itself. There is one function that has a call to get remote
64: information but isn't included here which is ProcessTopLevelMap. The
65: usage was small enough to be ignored, but that portion may be moved
66: here in the future.
67:
68: =cut
69:
70: # ----- DOWNLOAD INFORMATION -------------------------------------------
71:
72: =pod
73:
1.3 stredwic 74: =item &DownloadClasslist()
1.1 stredwic 75:
76: Collects lastname, generation, middlename, firstname, PID, and section for each
77: student from their environment database. The list of students is built from
78: collecting a classlist for the course that is to be displayed.
79:
80: =over 4
81:
82: Input: $courseID, $c
83:
84: $courseID: The id of the course
85:
86: $c: The connection class that can determine if the browser has aborted. It
87: is used to short circuit this function so that it doesn't continue to
88: get information when there is no need.
89:
90: Output: \%classlist
91:
92: \%classlist: A pointer to a hash containing the following data:
93:
94: -A list of student name:domain (as keys) (known below as $name)
95:
96: -A hash pointer for each student containing lastname, generation, firstname,
97: middlename, and PID : Key is $name.'studentInformation'
98:
99: -A hash pointer to each students section data : Key is $name.section
100:
101: =back
102:
103: =cut
104:
1.3 stredwic 105: sub DownloadClasslist {
106: my ($courseID, $lastDownloadTime, $c)=@_;
1.1 stredwic 107: my ($courseDomain,$courseNumber)=split(/\_/,$courseID);
1.3 stredwic 108: my %classlist;
1.1 stredwic 109:
1.7 stredwic 110: my $modifiedTime = &GetFileTimestamp($courseDomain, $courseNumber,
111: 'classlist.db',
112: $Apache::lonnet::perlvar{'lonUsersDir'});
113:
114: if($lastDownloadTime ne 'Not downloaded' &&
115: $lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
116: $classlist{'lastDownloadTime'}=time;
117: $classlist{'UpToDate'} = 'true';
118: return \%classlist;
119: }
1.3 stredwic 120:
121: %classlist=&Apache::lonnet::dump('classlist',$courseDomain, $courseNumber);
1.1 stredwic 122: my ($checkForError)=keys (%classlist);
123: if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
124: return \%classlist;
125: }
126:
127: foreach my $name (keys(%classlist)) {
128: if($c->aborted()) {
129: $classlist{'error'}='aborted';
130: return \%classlist;
131: }
132:
133: my ($studentName,$studentDomain) = split(/\:/,$name);
134: # Download student environment data, specifically the full name and id.
135: my %studentInformation=&Apache::lonnet::get('environment',
136: ['lastname','generation',
137: 'firstname','middlename',
138: 'id'],
139: $studentDomain,
140: $studentName);
141: $classlist{$name.':studentInformation'}=\%studentInformation;
142:
143: if($c->aborted()) {
144: $classlist{'error'}='aborted';
145: return \%classlist;
146: }
147:
148: #Section
149: my %section=&Apache::lonnet::dump('roles',$studentDomain,$studentName);
1.3 stredwic 150: $classlist{$name.':sections'}=\%section;
1.1 stredwic 151: }
152:
1.3 stredwic 153: $classlist{'UpToDate'} = 'false';
154: $classlist{'lastDownloadTime'}=time;
155:
1.1 stredwic 156: return \%classlist;
157: }
158:
159: =pod
160:
1.4 stredwic 161: =item &DownloadCourseInformation()
1.1 stredwic 162:
163: Dump of all the course information for a single student. There is no
1.3 stredwic 164: pruning of data, it is all stored in a hash and returned. It also
165: checks the timestamp of the students course database file and only downloads
166: if it has been modified since the last download.
1.1 stredwic 167:
168: =over 4
169:
170: Input: $name, $courseID
171:
172: $name: student name:domain
173:
174: $courseID: The id of the course
175:
176: Output: \%courseData
177:
178: \%courseData: A hash pointer to the raw data from the student's course
179: database.
180:
181: =back
182:
183: =cut
184:
1.4 stredwic 185: sub DownloadCourseInformation {
1.12 stredwic 186: my ($namedata,$courseID,$lastDownloadTime,$WhatIWant)=@_;
1.3 stredwic 187: my %courseData;
1.4 stredwic 188: my ($name,$domain) = split(/\:/,$namedata);
1.1 stredwic 189:
1.7 stredwic 190: my $modifiedTime = &GetFileTimestamp($domain, $name,
191: $courseID.'.db',
192: $Apache::lonnet::perlvar{'lonUsersDir'});
193:
1.13 ! stredwic 194: if($lastDownloadTime >= $modifiedTime && $modifiedTime >= 0) {
! 195: $courseData{$namedata.':lastDownloadTime'}=time;
! 196: $courseData{$namedata.':UpToDate'} = 'true';
1.7 stredwic 197: return \%courseData;
198: }
1.3 stredwic 199:
1.4 stredwic 200: # Download course data
1.12 stredwic 201: if(!defined($WhatIWant)) {
202: $WhatIWant = '.';
203: }
204: %courseData=&Apache::lonnet::dump($courseID, $domain, $name, $WhatIWant);
1.3 stredwic 205: $courseData{'UpToDate'} = 'false';
206: $courseData{'lastDownloadTime'}=time;
1.13 ! stredwic 207:
! 208: my %newData;
! 209: foreach (keys(%courseData)) {
! 210: $newData{$namedata.':'.$_} = $courseData{$_};
! 211: }
! 212:
! 213: return \%newData;
1.1 stredwic 214: }
215:
216: # ----- END DOWNLOAD INFORMATION ---------------------------------------
217:
218: =pod
219:
220: =head1 PROCESSING FUNCTIONS
221:
222: These functions process all the data for all the students. Also, they
223: are the only functions that access the cache database for writing. Thus
224: they are the only functions that cache data. The downloading and caching
225: were separated to reduce problems with stopping downloading then can't
226: tie hash to database later.
227:
228: =cut
229:
230: # ----- PROCESSING FUNCTIONS ---------------------------------------
231:
232: =pod
233:
234: =item &ProcessTopResourceMap()
235:
236: Trace through the "big hash" created in rat/lonuserstate.pm::loadmap.
237: Basically, this function organizes a subset of the data and stores it in
238: cached data. The data stored is the problems, sequences, sequence titles,
239: parts of problems, and their ordering. Column width information is also
240: partially handled here on a per sequence basis.
241:
242: =over 4
243:
244: Input: $cache, $c
245:
246: $cache: A pointer to a hash to store the information
247:
248: $c: The connection class used to determine if an abort has been sent to the
249: browser
250:
251: Output: A string that contains an error message or "OK" if everything went
252: smoothly.
253:
254: =back
255:
256: =cut
257:
258: sub ProcessTopResourceMap {
1.11 stredwic 259: my ($cache,$c)=@_;
1.1 stredwic 260: my %hash;
261: my $fn=$ENV{'request.course.fn'};
262: if(-e "$fn.db") {
263: my $tieTries=0;
264: while($tieTries < 3) {
265: if($c->aborted()) {
266: return;
267: }
1.10 stredwic 268: if(tie(%hash,'GDBM_File',"$fn.db",&GDBM_READER(),0640)) {
1.1 stredwic 269: last;
270: }
271: $tieTries++;
272: sleep 1;
273: }
274: if($tieTries >= 3) {
275: return 'Coursemap undefined.';
276: }
277: } else {
278: return 'Can not open Coursemap.';
279: }
280:
281: # Initialize state machine. Set information pointing to top level map.
282: my (@sequences, @currentResource, @finishResource);
283: my ($currentSequence, $currentResourceID, $lastResourceID);
284:
285: $currentResourceID=$hash{'ids_/res/'.$ENV{'request.course.uri'}};
286: push(@currentResource, $currentResourceID);
287: $lastResourceID=-1;
288: $currentSequence=-1;
289: my $topLevelSequenceNumber = $currentSequence;
290:
1.11 stredwic 291: my %sequenceRecord;
1.1 stredwic 292: while(1) {
293: if($c->aborted()) {
294: last;
295: }
296: # HANDLE NEW SEQUENCE!
297: #if page || sequence
1.11 stredwic 298: if(defined($hash{'map_pc_'.$hash{'src_'.$currentResourceID}}) &&
299: !defined($sequenceRecord{$currentResourceID})) {
300: $sequenceRecord{$currentResourceID}++;
1.1 stredwic 301: push(@sequences, $currentSequence);
302: push(@currentResource, $currentResourceID);
303: push(@finishResource, $lastResourceID);
304:
305: $currentSequence=$hash{'map_pc_'.$hash{'src_'.$currentResourceID}};
306:
307: # Mark sequence as containing problems. If it doesn't, then
308: # it will be removed when processing for this sequence is
309: # complete. This allows the problems in a sequence
310: # to be outputed before problems in the subsequences
311: if(!defined($cache->{'orderedSequences'})) {
312: $cache->{'orderedSequences'}=$currentSequence;
313: } else {
314: $cache->{'orderedSequences'}.=':'.$currentSequence;
315: }
316:
317: $lastResourceID=$hash{'map_finish_'.
318: $hash{'src_'.$currentResourceID}};
319: $currentResourceID=$hash{'map_start_'.
320: $hash{'src_'.$currentResourceID}};
321:
322: if(!($currentResourceID) || !($lastResourceID)) {
323: $currentSequence=pop(@sequences);
324: $currentResourceID=pop(@currentResource);
325: $lastResourceID=pop(@finishResource);
326: if($currentSequence eq $topLevelSequenceNumber) {
327: last;
328: }
329: }
1.12 stredwic 330: next;
1.1 stredwic 331: }
332:
333: # Handle gradable resources: exams, problems, etc
334: $currentResourceID=~/(\d+)\.(\d+)/;
335: my $partA=$1;
336: my $partB=$2;
337: if($hash{'src_'.$currentResourceID}=~
338: /\.(problem|exam|quiz|assess|survey|form)$/ &&
1.11 stredwic 339: $partA eq $currentSequence &&
340: !defined($sequenceRecord{$currentSequence.':'.
341: $currentResourceID})) {
342: $sequenceRecord{$currentSequence.':'.$currentResourceID}++;
1.1 stredwic 343: my $Problem = &Apache::lonnet::symbclean(
344: &Apache::lonnet::declutter($hash{'map_id_'.$partA}).
345: '___'.$partB.'___'.
346: &Apache::lonnet::declutter($hash{'src_'.
347: $currentResourceID}));
348:
349: $cache->{$currentResourceID.':problem'}=$Problem;
350: if(!defined($cache->{$currentSequence.':problems'})) {
351: $cache->{$currentSequence.':problems'}=$currentResourceID;
352: } else {
353: $cache->{$currentSequence.':problems'}.=
354: ':'.$currentResourceID;
355: }
356:
1.2 stredwic 357: my $meta=$hash{'src_'.$currentResourceID};
358: # $cache->{$currentResourceID.':title'}=
359: # &Apache::lonnet::metdata($meta,'title');
360: $cache->{$currentResourceID.':title'}=
361: $hash{'title_'.$currentResourceID};
1.9 minaeibi 362: $cache->{$currentResourceID.':source'}=
363: $hash{'src_'.$currentResourceID};
1.2 stredwic 364:
1.1 stredwic 365: # Get Parts for problem
1.8 stredwic 366: my %beenHere;
367: foreach (split(/\,/,&Apache::lonnet::metadata($meta,'packages'))) {
368: if(/^\w+response_\d+.*/) {
369: my (undef, $partId, $responseId) = split(/_/,$_);
370: if($beenHere{'p:'.$partId} == 0) {
371: $beenHere{'p:'.$partId}++;
372: if(!defined($cache->{$currentSequence.':'.
373: $currentResourceID.':parts'})) {
374: $cache->{$currentSequence.':'.$currentResourceID.
375: ':parts'}=$partId;
376: } else {
377: $cache->{$currentSequence.':'.$currentResourceID.
378: ':parts'}.=':'.$partId;
379: }
380: }
381: if($beenHere{'r:'.$partId.':'.$responseId} == 0) {
382: $beenHere{'r:'.$partId.':'.$responseId}++;
383: if(!defined($cache->{$currentSequence.':'.
384: $currentResourceID.':'.$partId.
385: ':responseIDs'})) {
386: $cache->{$currentSequence.':'.$currentResourceID.
387: ':'.$partId.':responseIDs'}=$responseId;
388: } else {
389: $cache->{$currentSequence.':'.$currentResourceID.
390: ':'.$partId.':responseIDs'}.=':'.
391: $responseId;
392: }
1.1 stredwic 393: }
1.8 stredwic 394: if(/^optionresponse/ &&
395: $beenHere{'o:'.$partId.':'.$currentResourceID} == 0) {
396: $beenHere{'o:'.$partId.$currentResourceID}++;
397: if(defined($cache->{'OptionResponses'})) {
398: $cache->{'OptionResponses'}.= ':::'.
399: $currentResourceID.':'.
400: $partId.':'.$responseId;
401: } else {
402: $cache->{'OptionResponses'}= $currentResourceID.
403: ':'.$partId.':'.$responseId;
1.2 stredwic 404: }
405: }
406: }
1.8 stredwic 407: }
408: }
1.1 stredwic 409:
410: # if resource == finish resource, then it is the end of a sequence/page
411: if($currentResourceID eq $lastResourceID) {
412: # pop off last resource of sequence
413: $currentResourceID=pop(@currentResource);
414: $lastResourceID=pop(@finishResource);
415:
416: if(defined($cache->{$currentSequence.':problems'})) {
417: # Capture sequence information here
418: $cache->{$currentSequence.':title'}=
419: $hash{'title_'.$currentResourceID};
1.2 stredwic 420: $cache->{$currentSequence.':source'}=
421: $hash{'src_'.$currentResourceID};
1.1 stredwic 422:
423: my $totalProblems=0;
424: foreach my $currentProblem (split(/\:/,
425: $cache->{$currentSequence.
426: ':problems'})) {
427: foreach (split(/\:/,$cache->{$currentSequence.':'.
428: $currentProblem.
429: ':parts'})) {
430: $totalProblems++;
431: }
432: }
433: my @titleLength=split(//,$cache->{$currentSequence.
434: ':title'});
435: # $extra is 3 for problems correct and 3 for space
436: # between problems correct and problem output
437: my $extra = 6;
438: if(($totalProblems + $extra) > (scalar @titleLength)) {
439: $cache->{$currentSequence.':columnWidth'}=
440: $totalProblems + $extra;
441: } else {
442: $cache->{$currentSequence.':columnWidth'}=
443: (scalar @titleLength);
444: }
445: } else {
446: # Remove sequence from list, if it contains no problems to
447: # display.
448: $cache->{'orderedSequences'}=~s/$currentSequence//;
449: $cache->{'orderedSequences'}=~s/::/:/g;
450: $cache->{'orderedSequences'}=~s/^:|:$//g;
451: }
452:
453: $currentSequence=pop(@sequences);
454: if($currentSequence eq $topLevelSequenceNumber) {
455: last;
456: }
1.11 stredwic 457: }
1.1 stredwic 458:
459: # MOVE!!!
460: # move to next resource
461: unless(defined($hash{'to_'.$currentResourceID})) {
462: # big problem, need to handle. Next is probably wrong
1.11 stredwic 463: my $errorMessage = 'Big problem in ';
464: $errorMessage .= 'loncoursedata::ProcessTopLevelMap.';
465: $errorMessage .= ' bighash to_$currentResourceID not defined!';
466: &Apache::lonnet::logthis($errorMessage);
1.1 stredwic 467: last;
468: }
469: my @nextResources=();
470: foreach (split(/\,/,$hash{'to_'.$currentResourceID})) {
1.11 stredwic 471: if(!defined($sequenceRecord{$currentSequence.':'.
472: $hash{'goesto_'.$_}})) {
473: push(@nextResources, $hash{'goesto_'.$_});
474: }
1.1 stredwic 475: }
476: push(@currentResource, @nextResources);
477: # Set the next resource to be processed
478: $currentResourceID=pop(@currentResource);
479: }
480:
481: unless (untie(%hash)) {
482: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
483: "Could not untie coursemap $fn (browse)".
484: ".</font>");
485: }
486:
487: return 'OK';
488: }
489:
490: =pod
491:
1.3 stredwic 492: =item &ProcessClasslist()
1.1 stredwic 493:
1.3 stredwic 494: Taking the class list dumped from &DownloadClasslist(), all the
1.1 stredwic 495: students and their non-class information is processed using the
496: &ProcessStudentInformation() function. A date stamp is also recorded for
497: when the data was processed.
498:
1.3 stredwic 499: Takes data downloaded for a student and breaks it up into managable pieces and
500: stored in cache data. The username, domain, class related date, PID,
501: full name, and section are all processed here.
502:
503:
1.1 stredwic 504: =over 4
505:
506: Input: $cache, $classlist, $courseID, $ChartDB, $c
507:
508: $cache: A hash pointer to store the data
509:
510: $classlist: The hash of data collected about a student from
1.3 stredwic 511: &DownloadClasslist(). The hash contains a list of students, a pointer
1.1 stredwic 512: to a hash of student information for each student, and each student's section
513: number.
514:
515: $courseID: The course ID
516:
517: $ChartDB: The name of the cache database file.
518:
519: $c: The connection class used to determine if an abort has been sent to the
520: browser
521:
522: Output: @names
523:
524: @names: An array of students whose information has been processed, and are to
525: be considered in an arbitrary order.
526:
527: =back
528:
529: =cut
530:
1.3 stredwic 531: sub ProcessClasslist {
532: my ($cache,$classlist,$courseID,$c)=@_;
1.1 stredwic 533: my @names=();
534:
1.3 stredwic 535: $cache->{'ClasslistTimeStamp'}=$classlist->{'lastDownloadTime'};
536: if($classlist->{'UpToDate'} eq 'true') {
537: return split(/:::/,$cache->{'NamesOfStudents'});;
538: }
539:
1.1 stredwic 540: foreach my $name (keys(%$classlist)) {
541: if($name =~ /\:section/ || $name =~ /\:studentInformation/ ||
1.3 stredwic 542: $name eq '' || $name eq 'UpToDate' || $name eq 'lastDownloadTime') {
1.1 stredwic 543: next;
544: }
545: if($c->aborted()) {
1.3 stredwic 546: return ();
1.1 stredwic 547: }
548: push(@names,$name);
1.3 stredwic 549: my $studentInformation = $classlist->{$name.':studentInformation'},
550: my $sectionData = $classlist->{$name.':sections'},
551: my $date = $classlist->{$name},
552: my ($studentName,$studentDomain) = split(/\:/,$name);
553:
554: $cache->{$name.':username'}=$studentName;
555: $cache->{$name.':domain'}=$studentDomain;
1.10 stredwic 556: # Initialize timestamp for student
1.3 stredwic 557: if(!defined($cache->{$name.':lastDownloadTime'})) {
558: $cache->{$name.':lastDownloadTime'}='Not downloaded';
1.6 stredwic 559: $cache->{$name.':updateTime'}=' Not updated';
1.3 stredwic 560: }
561:
562: my ($checkForError)=keys(%$studentInformation);
563: if($checkForError =~ /^(con_lost|error|no_such_host)/i) {
564: $cache->{$name.':error'}=
565: 'Could not download student environment data.';
566: $cache->{$name.':fullname'}='';
567: $cache->{$name.':id'}='';
568: } else {
569: $cache->{$name.':fullname'}=&ProcessFullName(
570: $studentInformation->{'lastname'},
571: $studentInformation->{'generation'},
572: $studentInformation->{'firstname'},
573: $studentInformation->{'middlename'});
574: $cache->{$name.':id'}=$studentInformation->{'id'};
575: }
576:
577: my ($end, $start)=split(':',$date);
578: $courseID=~s/\_/\//g;
579: $courseID=~s/^(\w)/\/$1/;
580:
581: my $sec='';
582: foreach my $key (keys (%$sectionData)) {
583: my $value = $sectionData->{$key};
584: if ($key=~/^$courseID(?:\/)*(\w+)*\_st$/) {
585: my $tempsection=$1;
586: if($key eq $courseID.'_st') {
587: $tempsection='';
588: }
589: my ($dummy,$roleend,$rolestart)=split(/\_/,$value);
590: if($roleend eq $end && $rolestart eq $start) {
591: $sec = $tempsection;
592: last;
593: }
594: }
595: }
596:
597: my $status='Expired';
598: if(((!$end) || time < $end) && ((!$start) || (time > $start))) {
599: $status='Active';
600: }
601: $cache->{$name.':Status'}=$status;
602: $cache->{$name.':section'}=$sec;
1.7 stredwic 603:
604: if($sec eq '' || !defined($sec) || $sec eq ' ') {
605: $sec = 'none';
606: }
607: if(defined($cache->{'sectionList'})) {
608: if($cache->{'sectionList'} !~ /(^$sec:|^$sec$|:$sec$|:$sec:)/) {
609: $cache->{'sectionList'} .= ':'.$sec;
610: }
611: } else {
612: $cache->{'sectionList'} = $sec;
613: }
1.1 stredwic 614: }
615:
1.3 stredwic 616: $cache->{'ClasslistTimestamp'}=time;
617: $cache->{'NamesOfStudents'}=join(':::',@names);
1.1 stredwic 618:
619: return @names;
620: }
621:
622: =pod
623:
624: =item &ProcessStudentData()
625:
626: Takes the course data downloaded for a student in
1.4 stredwic 627: &DownloadCourseInformation() and breaks it up into key value pairs
1.1 stredwic 628: to be stored in the cached data. The keys are comprised of the
629: $username:$domain:$keyFromCourseDatabase. The student username:domain is
630: stored away signifying that the student's information has been downloaded and
631: can be reused from cached data.
632:
633: =over 4
634:
635: Input: $cache, $courseData, $name
636:
637: $cache: A hash pointer to store data
638:
639: $courseData: A hash pointer that points to the course data downloaded for a
640: student.
641:
642: $name: username:domain
643:
644: Output: None
645:
646: *NOTE: There is no output, but an error message is stored away in the cache
647: data. This is checked in &FormatStudentData(). The key username:domain:error
648: will only exist if an error occured. The error is an error from
1.4 stredwic 649: &DownloadCourseInformation().
1.1 stredwic 650:
651: =back
652:
653: =cut
654:
655: sub ProcessStudentData {
656: my ($cache,$courseData,$name)=@_;
657:
1.13 ! stredwic 658: if(!&CheckDateStampError($courseData, $cache, $name)) {
! 659: return;
! 660: }
! 661:
! 662: foreach (keys %$courseData) {
! 663: $cache->{$_}=$courseData->{$_};
! 664: }
! 665:
! 666: return;
! 667: }
! 668:
! 669: sub ExtractStudentData {
! 670: my ($input, $output, $data, $name)=@_;
! 671:
! 672: if(!&CheckDateStampError($input, $data, $name)) {
1.3 stredwic 673: return;
674: }
675:
1.13 ! stredwic 676: my ($username,$domain)=split(':',$name);
! 677:
! 678: my $Version;
! 679: my $problemsCorrect = 0;
! 680: my $totalProblems = 0;
! 681: my $problemsSolved = 0;
! 682: my $numberOfParts = 0;
! 683: foreach my $sequence (split(':', $data->{'orderedSequences'})) {
! 684: foreach my $problemID (split(':', $data->{$sequence.':problems'})) {
! 685: my $problem = $data->{$problemID.':problem'};
! 686: my $LatestVersion = $input->{$name.':version:'.$problem};
! 687:
! 688: # Output dashes for all the parts of this problem if there
! 689: # is no version information about the current problem.
! 690: if(!$LatestVersion) {
! 691: foreach my $part (split(/\:/,$data->{$sequence.':'.
! 692: $problemID.
! 693: ':parts'})) {
! 694: $totalProblems++;
! 695: }
! 696: $output->{$name.':'.$problemID.':NoVersion'} = 'true';
! 697: next;
! 698: }
! 699:
! 700: my %partData=undef;
! 701: # Initialize part data, display skips correctly
! 702: # Skip refers to when a student made no submissions on that
! 703: # part/problem.
! 704: foreach my $part (split(/\:/,$data->{$sequence.':'.
! 705: $problemID.
! 706: ':parts'})) {
! 707: $partData{$part.':tries'}=0;
! 708: $partData{$part.':code'}=' ';
! 709: $partData{$part.':awarded'}=0;
! 710: $partData{$part.':timestamp'}=0;
! 711: foreach my $response (split(':', $data->{$sequence.':'.
! 712: $problemID.':'.
! 713: $part.':responseIDs'})) {
! 714: $partData{$part.':'.$response.':submission'}='';
! 715: }
! 716: }
! 717:
! 718: # Looping through all the versions of each part, starting with the
! 719: # oldest version. Basically, it gets the most recent
! 720: # set of grade data for each part.
! 721: my @submissions = ();
! 722: for(my $Version=1; $Version<=$LatestVersion; $Version++) {
! 723: foreach my $part (split(/\:/,$data->{$sequence.':'.
! 724: $problemID.
! 725: ':parts'})) {
! 726:
! 727: if(!defined($input->{"$name:$Version:$problem".
! 728: ":resource.$part.solved"})) {
! 729: # No grade for this submission, so skip
! 730: next;
! 731: }
! 732:
! 733: my $tries=0;
! 734: my $code=' ';
! 735: my $awarded=0;
! 736:
! 737: $tries = $input->{$name.':'.$Version.':'.$problem.
! 738: ':resource.'.$part.'.tries'};
! 739: $awarded = $input->{$name.':'.$Version.':'.$problem.
! 740: ':resource.'.$part.'.awarded'};
! 741:
! 742: $partData{$part.':awarded'}=($awarded) ? $awarded : 0;
! 743: $partData{$part.':tries'}=($tries) ? $tries : 0;
! 744:
! 745: $partData{$part.':timestamp'}=$input->{$name.':'.$Version.':'.
! 746: $problem.
! 747: ':timestamp'};
! 748: if(!$input->{$name.':'.$Version.':'.$problem.':resource.'.$part.
! 749: '.previous'}) {
! 750: foreach my $response (split(':',
! 751: $data->{$sequence.':'.
! 752: $problemID.':'.
! 753: $part.':responseIDs'})) {
! 754: @submissions=($input->{$name.':'.$Version.':'.
! 755: $problem.
! 756: ':resource.'.$part.'.'.
! 757: $response.'.submission'},
! 758: @submissions);
! 759: }
! 760: }
! 761:
! 762: my $val = $input->{$name.':'.$Version.':'.$problem.
! 763: ':resource.'.$part.'.solved'};
! 764: if ($val eq 'correct_by_student') {$code = '*';}
! 765: elsif ($val eq 'correct_by_override') {$code = '+';}
! 766: elsif ($val eq 'incorrect_attempted') {$code = '.';}
! 767: elsif ($val eq 'incorrect_by_override'){$code = '-';}
! 768: elsif ($val eq 'excused') {$code = 'x';}
! 769: elsif ($val eq 'ungraded_attempted') {$code = '#';}
! 770: else {$code = ' ';}
! 771: $partData{$part.':code'}=$code;
! 772: }
! 773: }
! 774:
! 775: foreach my $part (split(/\:/,$data->{$sequence.':'.$problemID.
! 776: ':parts'})) {
! 777: $output->{$name.':'.$problemID.':'.$part.':wrong'} =
! 778: $partData{$part.':tries'};
! 779:
! 780: if($partData{$part.':code'} eq '*') {
! 781: $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
! 782: $problemsCorrect++;
! 783: } elsif($partData{$part.':code'} eq '+') {
! 784: $output->{$name.':'.$problemID.':'.$part.':wrong'}--;
! 785: $problemsCorrect++;
! 786: }
! 787:
! 788: $output->{$name.':'.$problemID.':'.$part.':tries'} =
! 789: $partData{$part.':tries'};
! 790: $output->{$name.':'.$problemID.':'.$part.':code'} =
! 791: $partData{$part.':code'};
! 792: $output->{$name.':'.$problemID.':'.$part.':awarded'} =
! 793: $partData{$part.':awarded'};
! 794: $output->{$name.':'.$problemID.':'.$part.':timestamp'} =
! 795: $partData{$part.':timestamp'};
! 796: foreach my $response (split(':', $data->{$sequence.':'.
! 797: $problemID.':'.
! 798: $part.':responseIDs'})) {
! 799: $output->{$name.':'.$problemID.':'.$part.':'.$response.
! 800: ':submission'}=join(':::',@submissions);
! 801: }
1.3 stredwic 802:
1.13 ! stredwic 803: if($partData{$part.':code'} ne 'x') {
! 804: $totalProblems++;
! 805: }
! 806: }
1.1 stredwic 807: }
1.13 ! stredwic 808:
! 809: $output->{$name.':'.$sequence.':problemsCorrect'} = $problemsCorrect;
! 810: $problemsSolved += $problemsCorrect;
! 811: $problemsCorrect=0;
1.3 stredwic 812: }
813:
1.13 ! stredwic 814: $output->{$name.':problemsSolved'} = $problemsSolved;
! 815: $output->{$name.':totalProblems'} = $totalProblems;
1.1 stredwic 816:
817: return;
1.4 stredwic 818: }
819:
820: sub LoadDiscussion {
1.13 ! stredwic 821: my ($courseID)=@_;
1.5 minaeibi 822: my %Discuss=();
823: my %contrib=&Apache::lonnet::dump(
824: $courseID,
825: $ENV{'course.'.$courseID.'.domain'},
826: $ENV{'course.'.$courseID.'.num'});
827:
828: #my %contrib=&DownloadCourseInformation($name, $courseID, 0);
829:
1.4 stredwic 830: foreach my $temp(keys %contrib) {
831: if ($temp=~/^version/) {
832: my $ver=$contrib{$temp};
833: my ($dummy,$prb)=split(':',$temp);
834: for (my $idx=1; $idx<=$ver; $idx++ ) {
835: my $name=$contrib{"$idx:$prb:sendername"};
1.5 minaeibi 836: $Discuss{"$name:$prb"}=$idx;
1.4 stredwic 837: }
838: }
839: }
1.5 minaeibi 840:
841: return \%Discuss;
1.1 stredwic 842: }
843:
844: # ----- END PROCESSING FUNCTIONS ---------------------------------------
845:
846: =pod
847:
848: =head1 HELPER FUNCTIONS
849:
850: These are just a couple of functions do various odd and end
851: jobs.
852:
853: =cut
854:
855: # ----- HELPER FUNCTIONS -----------------------------------------------
856:
1.13 ! stredwic 857: sub CheckDateStampError {
! 858: my ($courseData, $cache, $name)=@_;
! 859: if($courseData->{$name.':UpToDate'} eq 'true') {
! 860: $cache->{$name.':lastDownloadTime'} =
! 861: $courseData->{$name.':lastDownloadTime'};
! 862: if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
! 863: $cache->{$name.':updateTime'} = ' Not updated';
! 864: } else {
! 865: $cache->{$name.':updateTime'}=
! 866: localtime($courseData->{$name.':lastDownloadTime'});
! 867: }
! 868: return 0;
! 869: }
! 870:
! 871: $cache->{$name.':lastDownloadTime'}=$courseData->{$name.':lastDownloadTime'};
! 872: if($courseData->{$name.':lastDownloadTime'} eq 'Not downloaded') {
! 873: $cache->{$name.':updateTime'} = ' Not updated';
! 874: } else {
! 875: $cache->{$name.':updateTime'}=
! 876: localtime($courseData->{$name.':lastDownloadTime'});
! 877: }
! 878:
! 879: if(defined($courseData->{$name.':error'})) {
! 880: $cache->{$name.':error'}=$courseData->{$name.':error'};
! 881: return 0;
! 882: }
! 883:
! 884: return 1;
! 885: }
! 886:
1.1 stredwic 887: =pod
888:
889: =item &ProcessFullName()
890:
891: Takes lastname, generation, firstname, and middlename (or some partial
892: set of this data) and returns the full name version as a string. Format
893: is Lastname generation, firstname middlename or a subset of this.
894:
895: =cut
896:
897: sub ProcessFullName {
898: my ($lastname, $generation, $firstname, $middlename)=@_;
899: my $Str = '';
900:
901: if($lastname ne '') {
902: $Str .= $lastname.' ';
903: if($generation ne '') {
904: $Str .= $generation;
905: } else {
906: chop($Str);
907: }
908: $Str .= ', ';
909: if($firstname ne '') {
910: $Str .= $firstname.' ';
911: }
912: if($middlename ne '') {
913: $Str .= $middlename;
914: } else {
915: chop($Str);
916: if($firstname eq '') {
917: chop($Str);
918: }
919: }
920: } else {
921: if($firstname ne '') {
922: $Str .= $firstname.' ';
923: }
924: if($middlename ne '') {
925: $Str .= $middlename.' ';
926: }
927: if($generation ne '') {
928: $Str .= $generation;
929: } else {
930: chop($Str);
931: }
932: }
933:
934: return $Str;
935: }
936:
937: =pod
938:
939: =item &TestCacheData()
940:
941: Determine if the cache database can be accessed with a tie. It waits up to
942: ten seconds before returning failure. This function exists to help with
943: the problems with stopping the data download. When an abort occurs and the
944: user quickly presses a form button and httpd child is created. This
945: child needs to wait for the other to finish (hopefully within ten seconds).
946:
947: =over 4
948:
949: Input: $ChartDB
950:
951: $ChartDB: The name of the cache database to be opened
952:
953: Output: -1, 0, 1
954:
955: -1: Couldn't tie database
956: 0: Use cached data
957: 1: New cache database created, use that.
958:
959: =back
960:
961: =cut
962:
963: sub TestCacheData {
964: my ($ChartDB,$isRecalculate,$totalDelay)=@_;
965: my $isCached=-1;
966: my %testData;
967: my $tieTries=0;
968:
969: if(!defined($totalDelay)) {
970: $totalDelay = 10;
971: }
972:
973: if ((-e "$ChartDB") && (!$isRecalculate)) {
974: $isCached = 1;
975: } else {
976: $isCached = 0;
977: }
978:
979: while($tieTries < $totalDelay) {
980: my $result=0;
981: if($isCached) {
1.10 stredwic 982: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_READER(),0640);
1.1 stredwic 983: } else {
1.10 stredwic 984: $result=tie(%testData,'GDBM_File',$ChartDB,&GDBM_NEWDB(),0640);
1.1 stredwic 985: }
986: if($result) {
987: last;
988: }
989: $tieTries++;
990: sleep 1;
991: }
992: if($tieTries >= $totalDelay) {
993: return -1;
994: }
995:
996: untie(%testData);
997:
998: return $isCached;
999: }
1.2 stredwic 1000:
1.13 ! stredwic 1001: sub DownloadStudentCourseData {
! 1002: my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
! 1003:
! 1004: my $title = 'LON-CAPA Statistics';
! 1005: my $heading = 'Download and Process Course Data';
! 1006: my $studentCount = scalar(@$students);
! 1007: my %cache;
! 1008:
! 1009: my $WhatIWant;
! 1010: $WhatIWant = '(^version:(\w|\/|\.|-)+?$|';
! 1011: $WhatIWant .= '^\d+:(\w|\/|\.|-)+?:(resource\.\d+\.';
! 1012: $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
! 1013: $WhatIWant .= '|timestamp)';
! 1014: $WhatIWant .= ')';
! 1015:
! 1016: if($status eq 'true') {
! 1017: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
! 1018: }
! 1019: my $count=1;
! 1020: foreach (@$students) {
! 1021: if($c->aborted()) { return 'Aborted'; }
! 1022:
! 1023: if($status eq 'true') {
! 1024: my $displayString = $count.'/'.$studentCount.': '.$_;
! 1025: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
! 1026: }
! 1027:
! 1028: my $downloadTime='Not downloaded';
! 1029: if($checkDate eq 'true' &&
! 1030: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
! 1031: $downloadTime = $cache{$_.':lastDownloadTime'};
! 1032: untie(%cache);
! 1033: }
! 1034:
! 1035: if($c->aborted()) { return 'Aborted'; }
! 1036:
! 1037: if($downloadTime eq 'Not downloaded') {
! 1038: my $courseData =
! 1039: &DownloadCourseInformation($_, $courseID, $downloadTime,
! 1040: $WhatIWant);
! 1041: if(tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
! 1042: foreach my $key (keys(%$courseData)) {
! 1043: if($key =~ /^(con_lost|error|no_such_host)/i) {
! 1044: $courseData->{$_.':error'} = 'No course data for '.$_;
! 1045: last;
! 1046: }
! 1047: }
! 1048: if($extract eq 'true') {
! 1049: &ExtractStudentData($courseData, \%cache, \%cache, $_);
! 1050: } else {
! 1051: &ProcessStudentData(\%cache, $courseData, $_);
! 1052: }
! 1053: untie(%cache);
! 1054: } else {
! 1055: next;
! 1056: }
! 1057: }
! 1058: $count++;
! 1059: }
! 1060: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
! 1061:
! 1062: return 'OK';
! 1063: }
! 1064:
! 1065: sub DownloadStudentCourseDataSeparate {
! 1066: my ($students,$checkDate,$cacheDB,$extract,$status,$courseID,$r,$c)=@_;
! 1067: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
! 1068: my $title = 'LON-CAPA Statistics';
! 1069: my $heading = 'Download Course Data';
! 1070:
! 1071: my $WhatIWant;
! 1072: $WhatIWant = '(^version:(\w|\/|\.|-)+?$|';
! 1073: $WhatIWant .= '^\d+:(\w|\/|\.|-)+?:(resource\.\d+\.';
! 1074: $WhatIWant .= '(solved|tries|previous|awarded|(\d+\.submission))\s*$';
! 1075: $WhatIWant .= '|timestamp)';
! 1076: $WhatIWant .= ')';
! 1077:
! 1078: &CheckForResidualDownload($courseID, $cacheDB, $students, $c);
! 1079:
! 1080: my %cache;
! 1081: my %downloadData;
! 1082: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_NEWDB(),0640)) {
! 1083: return 'Failed to tie temporary download hash.';
! 1084: }
! 1085:
! 1086: my $studentCount = scalar(@$students);
! 1087: if($status eq 'true') {
! 1088: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
! 1089: }
! 1090: my $count=1;
! 1091: foreach (@$students) {
! 1092: if($c->aborted()) {
! 1093: untie(%downloadData);
! 1094: return 'Aborted';
! 1095: }
! 1096:
! 1097: if($status eq 'true') {
! 1098: my $displayString = $count.'/'.$studentCount.': '.$_;
! 1099: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
! 1100: }
! 1101:
! 1102: my $downloadTime='Not downloaded';
! 1103: if($checkDate eq 'true' &&
! 1104: tie(%cache,'GDBM_File',$cacheDB,&GDBM_READER(),0640)) {
! 1105: $downloadTime = $cache{$_.':lastDownloadTime'};
! 1106: untie(%cache);
! 1107: }
! 1108:
! 1109: if($c->aborted()) {
! 1110: untie(%downloadData);
! 1111: return 'Aborted';
! 1112: }
! 1113:
! 1114: if($downloadTime eq 'Not downloaded') {
! 1115: my $error = 0;
! 1116: my $courseData =
! 1117: &DownloadCourseInformation($_, $courseID, $downloadTime,
! 1118: $WhatIWant);
! 1119: foreach my $key (keys(%$courseData)) {
! 1120: $downloadData{$key} = $courseData->{$key};
! 1121: if($key =~ /^(con_lost|error|no_such_host)/i) {
! 1122: $error = 1;
! 1123: last;
! 1124: }
! 1125: }
! 1126: if($error) {
! 1127: foreach my $deleteKey (keys(%$courseData)) {
! 1128: delete $downloadData{$deleteKey};
! 1129: }
! 1130: $downloadData{$_.':error'} = 'No course data for '.$_;
! 1131: }
! 1132: }
! 1133: $count++;
! 1134: }
! 1135: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
! 1136:
! 1137: return &CheckForResidualDownload($cacheDB, 'true', 'true',
! 1138: $courseID, $r, $c);
! 1139: }
! 1140:
! 1141: sub CheckForResidualDownload {
! 1142: my ($cacheDB,$extract,$status,$courseID,$r,$c)=@_;
! 1143:
! 1144: my $residualFile = '/home/httpd/perl/tmp/'.$courseID.'DownloadFile.db';
! 1145: if(!-e $residualFile) {
! 1146: return;
! 1147: }
! 1148:
! 1149: my %downloadData;
! 1150: my %cache;
! 1151: unless(tie(%downloadData,'GDBM_File',$residualFile,&GDBM_READER(),0640) &&
! 1152: tie(%cache,'GDBM_File',$cacheDB,&GDBM_WRCREAT(),0640)) {
! 1153: return;
! 1154: }
! 1155:
! 1156: my @dataKeys=keys(%downloadData);
! 1157: my @students=();
! 1158: my %checkStudent;
! 1159: foreach(@dataKeys) {
! 1160: my @temp = split(':', $_);
! 1161: my $student = $temp[0].':'.$temp[1];
! 1162: if(!defined($checkStudent{$student})) {
! 1163: $checkStudent{$student}++;
! 1164: push(@students, $student);
! 1165: }
! 1166: }
! 1167:
! 1168: my $heading = 'Process Course Data';
! 1169: my $title = 'LON-CAPA Statistics';
! 1170: my $studentCount = scalar(@students);
! 1171: if($status eq 'true') {
! 1172: &Apache::lonhtmlcommon::Create_PrgWin($r, $title, $heading);
! 1173: }
! 1174:
! 1175: my $count=1;
! 1176: foreach my $name (@students) {
! 1177: last if($c->aborted());
! 1178:
! 1179: if($status eq 'true') {
! 1180: my $displayString = $count.'/'.$studentCount.': '.$_;
! 1181: &Apache::lonhtmlcommon::Update_PrgWin($displayString, $r);
! 1182: }
! 1183:
! 1184: if($extract eq 'true') {
! 1185: &ExtractStudentData(\%downloadData, \%cache, \%cache, $name);
! 1186: } else {
! 1187: &ProcessStudentData(\%cache, \%downloadData, $name);
! 1188: }
! 1189: foreach (@dataKeys) {
! 1190: if(/^$name/) {
! 1191: delete $downloadData{$_};
! 1192: }
! 1193: }
! 1194: $count++;
! 1195: }
! 1196:
! 1197: if($status eq 'true') { &Apache::lonhtmlcommon::Close_PrgWin($r); }
! 1198:
! 1199: untie(%cache);
! 1200: untie(%downloadData);
! 1201:
! 1202: if(!$c->aborted()) {
! 1203: my @files = ($residualFile);
! 1204: unlink(@files);
! 1205: }
! 1206:
! 1207: return 'OK';
! 1208: }
! 1209:
1.3 stredwic 1210: sub GetFileTimestamp {
1211: my ($studentDomain,$studentName,$filename,$root)=@_;
1212: $studentDomain=~s/\W//g;
1213: $studentName=~s/\W//g;
1214: my $subdir=$studentName.'__';
1215: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
1216: my $proname="$studentDomain/$subdir/$studentName";
1217: $proname .= '/'.$filename;
1218: my @dir = &Apache::lonnet::dirlist($proname, $studentDomain, $studentName,
1219: $root);
1220: my $fileStat = $dir[0];
1221: my @stats = split('&', $fileStat);
1.13 ! stredwic 1222: if($stats[0] ne 'empty' && $stats[0] ne 'no_such_dir') {
1.3 stredwic 1223: return $stats[9];
1224: } else {
1225: return -1;
1226: }
1227: }
1.1 stredwic 1228:
1229: # ----- END HELPER FUNCTIONS --------------------------------------------
1230:
1231: 1;
1232: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>