|
1 |
| - |
2 | 1 | ################################################################################
|
3 | 2 | # WeBWorK Online Homework Delivery System
|
4 | 3 | # Copyright © 2000-2024 The WeBWorK Project, https://github.com/openwebwork
|
|
16 | 15 |
|
17 | 16 | package Statistics;
|
18 | 17 |
|
19 |
| -#use strict; |
20 |
| -use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
21 |
| -require Exporter; |
22 |
| -@ISA = qw(Exporter); |
23 |
| -@EXPORT_OK = qw( ); |
24 |
| -@EXPORT = qw( ); |
25 |
| -$VERSION = '0.01'; |
26 |
| - |
27 |
| -# ########################################## |
28 |
| -# Initialize the class |
29 |
| -# Expected argument: a ref to a valid PGcore object |
30 |
| -# |
31 |
| -sub new { |
32 |
| - my $class = shift; |
33 |
| - my $self = { PG => shift }; |
34 |
| - |
35 |
| - bless $self, $class; |
36 |
| - |
37 |
| -} |
38 |
| - |
39 |
| -# ########################################## |
40 |
| -# Create an alias to a csv file. |
41 |
| -# Return the url that can be used in a browser |
42 |
| -# to access the file. |
43 |
| -# |
44 |
| -sub make_csv_alias { |
45 |
| - |
46 |
| - my $self = shift; |
47 |
| - my $studentLogin = shift; |
48 |
| - my $problemSeed = shift; |
49 |
| - my $setname = shift; |
50 |
| - my $prob = shift; |
| 18 | +use strict; |
| 19 | +use warnings; |
51 | 20 |
|
52 |
| - # Clean the student login string to make it appropriate for a file name. |
53 |
| - $studentLogin =~ s/Q/QQ/g; |
54 |
| - $studentLogin =~ s/\./-Q-/g; |
55 |
| - $studentLogin =~ s/\,/-Q-/g; |
56 |
| - $studentLogin =~ s/\@/-Q-/g; |
| 21 | +require WeBWorK::PG::IO; |
57 | 22 |
|
58 |
| - # Define the file name, clean it up and convert to a url. |
59 |
| - my $filePath = "data/$studentLogin-$problemSeed-set" . $setName . "prob$prob.html"; |
60 |
| - $filePath = $self->{PG}->surePathToTmpFile($filePath); |
61 |
| - my $url = $self->{PG}->{PG_alias}->make_alias($filePath); |
62 |
| - |
63 |
| - # Remove the .html off the end and replace it with a .csv |
64 |
| - $filePath =~ s/\.html$/.csv/; |
65 |
| - $url =~ s/\.html$/.csv/; |
66 |
| - |
67 |
| - ($filePath, $url); |
68 |
| -} |
69 |
| - |
70 |
| -# ########################################## |
71 | 23 | # Write the given data to a csv file.
|
72 |
| -# Return the URL if the file is written, |
73 |
| -# an empty string otherwise |
74 |
| -# |
75 | 24 | sub write_array_to_CSV {
|
76 |
| - my $self = shift; |
77 |
| - my $fileName = shift; |
78 |
| - my @dataRefs = @_; |
| 25 | + my ($fileName, @dataRefs) = @_; |
| 26 | + |
| 27 | + die 'No data set was provided.' unless @dataRefs && ref $dataRefs[0] eq 'ARRAY'; |
79 | 28 |
|
80 | 29 | # Make sure all of the data sets have the same number of elements
|
81 |
| - my $numberDataPoints = "nd"; |
82 |
| - my $data; |
83 |
| - foreach $data (@dataRefs) { |
84 |
| - my @dataArray = @{$data}; |
85 |
| - if ($numberDataPoints eq "nd") { |
86 |
| - $numberDataPoints = $#dataArray; |
87 |
| - } elsif ($numberDataPoints != $#dataArray) { |
88 |
| - die("$0", "The number of elements in the data sets are not all the same. No data set written to file."); |
89 |
| - return; |
90 |
| - } |
| 30 | + my $numberDataPoints = $#{ $dataRefs[0] }; |
| 31 | + for my $data (@dataRefs) { |
| 32 | + die 'The number of elements in the data sets are not all the same. No data set written to file.' |
| 33 | + if $numberDataPoints != $#$data; |
91 | 34 | }
|
92 | 35 |
|
93 |
| - # Open the file |
94 |
| - local (*OUTPUT); # create local file handle so it won't overwrite other open files. |
95 |
| - open(OUTPUT, ">$fileName") || warn("$0", "Can't open $fileName<BR>", ""); |
96 |
| - chmod(0777, $filePath); |
97 |
| - |
98 |
| - # write the header to the first row. |
99 |
| - my $header = ""; |
100 |
| - foreach $data (@dataRefs) { |
101 |
| - $header .= pop(@{$data}) . ","; |
102 |
| - } |
103 |
| - $header =~ s/,$//; |
104 |
| - print OUTPUT ($header . "\n") || warn("$0", "Can't print data file to $fileName<BR>", ""); |
| 36 | + # Add the header to the first row of the output. |
| 37 | + my $output = join(',', map { $_->[-1] } @dataRefs) . "\n"; |
105 | 38 |
|
106 |
| - # Go through each data point and write it out. |
107 |
| - my $lupe; |
108 |
| - for ($lupe = 0; $lupe < $numberDataPoints; ++$lupe) { |
109 |
| - my $line = ""; |
110 |
| - foreach $data (@dataRefs) { |
111 |
| - my @dataSet = @{$data}; |
112 |
| - $line .= $dataSet[$lupe] . ","; |
113 |
| - } |
114 |
| - $line =~ s/,$//; |
115 |
| - print OUTPUT ($line, "\n"); |
| 39 | + # Add each data point as another row in the output. |
| 40 | + for my $i (0 .. $numberDataPoints - 1) { |
| 41 | + $output .= join(',', map { $_->[$i] } @dataRefs) . "\n"; |
116 | 42 | }
|
117 | 43 |
|
118 |
| - # Close it up and move on. |
119 |
| - close(OUTPUT) || warn("$0", "Can't close $filePath<BR>", ""); |
| 44 | + # Write the output to disk. |
| 45 | + WeBWorK::PG::IO::saveDataToFile($output, $fileName); |
120 | 46 |
|
| 47 | + return; |
121 | 48 | }
|
122 | 49 |
|
123 | 50 | 1;
|
0 commit comments