/
qtype_matrix_grading.class.php
196 lines (179 loc) · 4.71 KB
/
qtype_matrix_grading.class.php
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Base class for grading types
*
* @copyright (c) 2011 University of Geneva
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
* @author Laurent Opprecht
* @abstract
*/
abstract class qtype_matrix_grading
{
public static function gradings()
{
static $result = false;
if ($result)
{
return $result;
}
$result = array();
$dir = dirname(__FILE__) . '/grading';
$files = scandir($dir);
$files = array_diff($files, array('.', '..'));
foreach ($files as $file)
{
include_once("$dir/$file");
$class = $file;
$class = str_replace('.class.php', '', $file);
if (class_exists($class))
{
$result[] = new $class();
}
}
return $result;
}
public static function default_grading()
{
return self::create('kprime');
}
/**
*
* @param string $type
* @return qtype_matrix_grading
*/
public static function create($type)
{
static $result = array();
if (isset($result[$type]))
{
return $result[$type];
}
$class = 'qtype_matrix_grading_' . $type;
require_once dirname(__FILE__) . '/grading/' . $class . '.class.php';
return $resut[$type] = call_user_func(array($class, 'create'));
}
public static function get_name()
{
$class = get_called_class();
$result = str_replace('qtype_matrix_grading_', '', $class);
return $result;
}
public static function get_title()
{
$identifier = self::get_name();
return qtype_matrix::get_string($identifier);
}
/**
* Create the form element used to define the weight of the cell
*
* @param MoodleQuickForm $form
* @param int $row row number
* @param int $col column number
* @return object
*/
public function create_cell_element($form, $row, $col, $multiple)
{
$cell_name = $this->cell_name($row, $col, $multiple);
if ($multiple)
{
return $form->createElement('checkbox', $cell_name, 'label');
}
else
{
return $form->createElement('radio', $cell_name, '', '', $col);
}
}
/**
* Returns a cell name.
* Should be a valid php and html identifier
*
* @param int $row row number
* @param int $col col number
* @param bool $multiple one answer per row or several
*
* @return string cellname
*/
public static function cell_name($row, $col, $multiple)
{
$row = $row ? $row : '0';
$col = $col ? $col : '0';
return $multiple ? "cell{$row}_{$col}" : "cell{$row}";
}
public static function cell_index($name)
{
$name = str_replace('cell', '', $name);
$result = explode('_', $name);
return $result;
}
/**
* Returns the question's grade. By default it is the average of correct questions.
*
* @param qtype_matrix_question $question
* @param array $answers
* @return float
*/
public function grade_question($question, $answers)
{
$grades = array();
foreach ($question->rows as $row)
{
$grades[] = $this->grade_row($question, $row, $answers);
}
$result = array_sum($grades) / count($grades);
$result = min(1, $result);
$result = max(0, $result);
return $result;
}
/**
* Grade a specific row
*
* @param qtype_matrix_question $question
* @param object $row
* @param array $answers
* @return float
*/
public function grade_row($question, $row, $answers)
{
return 0;
}
/**
* validate
*
* @param array $data the raw form data
*
* @return array of errors
*/
public function validation($data)
{
return array();
}
// /**
// * whether this grade method requires manual intervention
// */
// public function is_manual_graded()
// {
// return false;
// }
protected function col_count($data)
{
foreach ($data['colshort'] as $index => $value)
{
if (empty($value))
{
return $index++;
}
}
return count($data['colshort']);
}
protected function row_count($data)
{
foreach ($data['rowshort'] as $index => $value)
{
if (empty($value))
{
return $index++;
}
}
return count($data['rowshort']);
}
}