forked from openwebwork/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGanswergroup.pm
151 lines (132 loc) · 4.61 KB
/
PGanswergroup.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
################################################################################
# WeBWorK Online Homework Delivery System
# Copyright © 2000-2024 The WeBWorK Project, https://github.com/openwebwork
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################
package PGanswergroup;
use Exporter;
use PGUtil qw(not_null);
use PGresponsegroup;
our @ISA = qw(PGcore);
#############################################
# An object which contains an answer label and
# an answer evaluator
# and the links to and contents of all associated answer blanks
# (i.e the student responses)
#############################################
# Notes
# Answergroup -- input to a single answerEvaluator, may have several answer blanks
# for example an array or a radio button group or several checkboxes
# 1. create a answerEvaluator label name
# 2. provide space for an answerEvaluator
# 3. indicate that an answer blank or blanks has been published to receive the responses
# for example store the number of response strings associated with this answerEvaluator label name
# 4. space for the contents of the responses is provided in the PGresponse group
# 5. provide a method for applying the evaluator to the responses
#
# use Tie: IxHash??? to create ordered hash? (see Perl Cookbook)
sub new {
my $class = shift;
my $label = shift;
my $self = {
ans_label => $label,
ans_eval => undef, # usually an AnswerEvaluator, sometimes a CODE
response => new PGresponsegroup($label), # A PGresponse object which holds the responses
# which make up the answer
active => 1, # whether this answer group is currently active (for multistate problems)
@_,
};
bless $self, $class;
return $self;
}
sub evaluate { # applies the answer evaluator to the student response and returns an answer hash
}
sub complete { # test to see if answer evaluator and appropriate response blanks are all present
}
sub ans_eval {
my $self = shift;
my $ans_eval = shift;
$self->{ans_eval} = $ans_eval if ref($ans_eval);
$self->{ans_eval};
}
sub response_obj { #this may not be desirable -- perhaps you should always access response object indirectly
my $self = shift;
my $response_obj = shift;
$self->{response} = $response_obj if ref($response_obj);
$self->{response};
}
sub append_responses { #add or modify a response to the PGresponsegroup object
my $self = shift;
my @response_list = @_; # ordered list of label/ value pairs
$self->{response}->append_responses(@response_list);
}
sub insert_responses { # add a group of responses ( label/value pairs)
my $self = shift;
my @response_list = @_;
$self->{response}->clear();
$self->{response}->append_responses(@response_list);
}
sub insert_response_value { # add a response value(with label defined by answer group label)
my $self = shift;
my $value = shift;
$self->{response}->append_reponse($self->{ans_label}, $value);
}
sub replace_response { # add a response value(with label defined by answer group label)
my $self = shift;
my $response_label = shift;
my $value = shift;
$self->{response}->replace_response($response_label, $value);
}
sub insert { # add new values to PGanswergroup keys preserve existing values
my $self = shift;
my @in = @_;
my %hash = ();
if (ref($in[0] =~ /HASH/)) {
%hash = %{ $in[0] };
} else {
%hash = @in;
}
foreach my $key (keys %hash) {
next if not_null($self->{$key});
$self->{$key} = $hash{$key};
}
$self;
}
sub replace { # add new values ot PGanswergroup, overwriting existing values when duplicated
my $self = shift;
my @in = @_;
my %hash = ();
if (ref($in[0] =~ /HASH/)) {
%hash = %{ $in[0] };
} else {
%hash = @in;
}
foreach my $key (keys %hash) {
$self->{$key} = $hash{$key};
}
$self;
}
sub delete_from_hash { # don't want to redefine delete
my $self = shift;
my @in = @_;
my %hash = ();
if (ref($in[0] =~ /HASH/)) {
%hash = %{ $in[0] };
} else {
%hash = @in;
}
foreach my $key (keys %hash) {
$self->{$key} = undef;
}
$self;
}
1;