-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassessment_question.classes.inc
More file actions
executable file
·488 lines (452 loc) · 15.1 KB
/
Copy pathassessment_question.classes.inc
File metadata and controls
executable file
·488 lines (452 loc) · 15.1 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Long answer classes.
*
* @file
* Classes modelling the assessment question question and the assessment question question response
*/
/**
* Extension of QuizQuestion.
*/
class AssessmentQuestion extends QuizQuestion {
/**
* Implementation of saveNodeProperties
* @see QuizQuestion#saveNodeProperties($is_new)
*/
public function saveNodeProperties($is_new = FALSE) {
if (!isset($this->node->feedback)) {
$this->node->feedback = '';
}
if ($is_new || $this->node->revision == 1) {
$id = db_insert('quiz_assessment_question_node_properties')
->fields(array(
'nid' => $this->node->nid,
'vid' => $this->node->vid,
))
->execute();
}
else {
/*
db_update('quiz_assessment_question_node_properties')
->fields(array('rubric' => $this->node->rubric))
->condition('nid', $this->node->nid)
->condition('vid', $this->node->vid)
->execute();
*/
}
}
/**
* Implementation of validateNode
*
* @see QuizQuestion#validateNode($form)
*/
public function validateNode(array &$form) { }
/**
* Implementation of delete
*
* @see QuizQuestion#delete($only_this_version)
*/
public function delete($only_this_version = FALSE) {
if ($only_this_version) {
db_delete('quiz_assessment_question_user_answers')
->condition('question_nid', $this->node->nid)
->condition('question_vid', $this->node->vid)
->execute();
db_delete('quiz_assessment_question_node_properties')
->condition('nid', $this->node->nid)
->condition('vid', $this->node->vid)
->execute();
}
else {
db_delete('quiz_assessment_question_node_properties')
->condition('nid', $this->node->nid)
->execute();
db_delete('quiz_assessment_question_user_answers')
->condition('question_nid', $this->node->nid)
->execute();
}
parent::delete($only_this_version);
}
/**
* Implementation of getNodeProperties
*
* @see QuizQuestion#getNodeProperties()
*/
public function getNodeProperties() {
if (isset($this->nodeProperties)) {
return $this->nodeProperties;
}
$props = parent::getNodeProperties();
$this->nodeProperties = $props;
return $props;
}
/**
* Implementation of getNodeView
*
* @see QuizQuestion#getNodeView()
*/
public function getNodeView() {
$content = parent::getNodeView();
/**
* @TODO: this code results in undefined property errors as-is and is not required for instructor assessment.
* We can revisit it if we need to put something in so students can review the result.
*
if ($this->viewCanRevealCorrect()) {
$content['answers'] = array(
'#type' => 'item',
'#markup' => '<div class="quiz-solution">' . check_markup($this->node->rubric, $this->getFormat()) . '</div>',
'#weight' => 1,
);
}
else {
$content['answers'] = array(
'#markup' => '<div class="quiz-answer-hidden">Answer hidden</div>',
'#weight' => 1,
);
}
*/
return $content;
}
/**
* Implementation of getAnweringForm
*
* @see QuizQuestion#getAnsweringForm($form_state, $rid)
*/
public function getAnsweringForm(array $form_state = NULL, $rid) {
$response = new AssessmentResponse($rid, $this->node);
return;
/*
$form = parent::getAnsweringForm($form_state, $rid);
$form['#theme'] = 'assessment_question_answering_form';
$form['tries'] = array(
'#type' => 'textarea',
'#title' => t('Answer'),
'#description' => t('Enter your answer here. If you need more space, click on the grey bar at the bottom of this area and drag it down.'),
'#rows' => 15,
'#cols' => 60,
'#required' => FALSE, // See isValid() in AssessmentResponse
);
if (isset($rid)) {
$response = new AssessmentResponse($rid, $this->node);
$form['tries']['#default_value'] = $response->getResponse();
}
return $form;
*/
}
/**
* Implementation of getCreationForm
*
* @see QuizQuestion#getCreationForm($form_state)
*/
public function getCreationForm(array &$form_state = NULL) {
/*
$form['rubric'] = array(
'#type' => 'options',
'#title' => t('Rubric'),
'#description' => t('Specify the criteria for grading the response.'),
'#default_value' => isset($this->node->rubric) ? $this->node->rubric : '',
//'#size' => 60,
//'#maxlength' => 2048,
'#key_type' => 'numeric',
'#key_type_toggle' => t('Customize values'),
'#key_type_toggled' => FALSE,
'#required' => FALSE,
);
return $form;
*/
}
/**
* Implementation of getMaximumScore
*
* @see QuizQuestion#getMaximumScore()
*/
public function getMaximumScore() {
return variable_get('assessment_question_default_max_score', 10);
}
}
/**
* Extension of QuizQuestionResponse
*/
class AssessmentResponse extends QuizQuestionResponse {
protected $rid = 0;
protected $is_correct = FALSE;
protected $evaluated = FALSE;
// The question node(not a quiz question instance)
public $question = NULL;
protected $answer = NULL;
protected $score = 0;
public $is_skipped;
public $is_doubtful;
/**
* Get all scores that have not yet been evaluated.
*
* @param $count
* Number of items to return (default: 50).
* @param $offset
* Where in the results we should start (default: 0).
*
* @return
* Array of objects describing unanswered questions. Each object will have result_id, question_nid, and question_vid.
*/
public static function fetchAllUnscoredAnswers($count = 50, $offset = 0) {
global $user;
$query = db_select('quiz_assessment_question_user_answers', 'a');
$query->fields('a', array('result_id', 'question_nid', 'question_vid', 'answer_feedback', 'answer_feedback_format'));
$query->fields('r', array('title'));
$query->fields('qnr', array('time_end', 'time_start', 'uid'));
$query->join('node_revision', 'r', 'a.question_vid = r.vid');
$query->join('quiz_node_results', 'qnr', 'a.result_id = qnr.result_id');
$query->join('node', 'n', 'qnr.nid = n.nid');
$query->condition('a.is_evaluated', 0);
if (user_access('score own quiz') && user_access('score taken quiz answer')) {
$query->condition(db_or()->condition('n.uid', $user->uid)->condition('qnr.uid', $user->uid));
}
else if (user_access('score own quiz')) {
$query->condition('n.uid', $user->uid);
}
else if (user_access('score taken quiz answer')) {
$query->condition('qnr.uid', $user->uid);
}
$results = $query->execute();
$unscored = array();
foreach ($results as $row) {
$unscored[] = $row;
}
return $unscored;
}
/**
* Given a quiz, return a list of all of the unscored answers.
*
* @param $nid
* Node ID for the quiz to check.
* @param $vid
* Version ID for the quiz to check.
* @param $count
* Number of items to return (default: 50).
* @param $offset
* Where in the results we should start (default: 0).
*
* @return
* Indexed array of result IDs that need to be scored.
*/
public static function fetchUnscoredAnswersByQuestion($nid, $vid, $count = 50, $offset = 0) {
$results = db_query('SELECT result_id FROM {quiz_assessment_question_user_answers}
WHERE is_evaluated = :is_evaluated
AND question_nid = :question_nid
AND question_vid = :question_vid', array(':is_evaluated' => 0, ':question_nid' => $nid, ':question_vid' => $vid));
$unscored = array();
foreach ($results as $row) {
$unscored[] = $row->result_id;
}
return $unscored;
}
/**
* ID of the answer.
*/
protected $answer_id = 0;
/**
* Constructor
*/
public function __construct($result_id, stdClass $question_node, $answer = NULL) {
parent::__construct($result_id, $question_node, $answer);
if (!isset($answer)) {
// Question has been answered allready. We fetch the answer data from the database.
$r = db_query('SELECT answer_id, answer, is_evaluated, score, question_vid, question_nid, result_id, answer_feedback, answer_feedback_format
FROM {quiz_assessment_question_user_answers}
WHERE question_nid = :qnid AND question_vid = :qvid AND result_id = :rid', array(':qnid' => $question_node->nid, ':qvid' => $question_node->vid, ':rid' => $result_id))->fetch();
if (!empty($r)) {
$this->answer = $r->answer;
$this->score = $r->score;
$this->evaluated = $r->is_evaluated;
$this->answer_id = $r->answer_id;
$this->answer_feedback = $r->answer_feedback;
$this->answer_feedback_format = $r->answer_feedback_format;
}
}
else {
$this->answer = $answer;
$this->evaluated = FALSE;
}
}
/**
* Implementation of isValid
*
* @see QuizQuestionResponse#isValid()
*/
public function isValid() {
return TRUE;
}
/**
* Implementation of save
*
* @see QuizQuestionResponse#save()
*/
public function save() {
$this->answer_id = db_insert('quiz_assessment_question_user_answers')
->fields(array(
'answer' => 0,
'question_nid' => $this->question->nid,
'question_vid' => $this->question->vid,
'result_id' => $this->rid,
))
->execute();
}
/**
* Implementation of delete
*
* @see QuizQuestionResponse#delete()
*/
public function delete() {
db_delete('quiz_assessment_question_user_answers')
->condition('question_nid', $this->question->nid)
->condition('question_vid', $this->question->vid)
->condition('result_id', $this->rid)
->execute();
}
/**
* Implementation of score
*
* @see QuizQuestionResponse#score()
*/
public function score() {
return (int) db_query('SELECT score FROM {quiz_assessment_question_user_answers}
WHERE result_id = :result_id AND question_vid = :question_vid', array(':result_id' => $this->rid, ':question_vid' => $this->question->vid))->fetchField();
}
/**
* Implementation of getResponse
*
* @see QuizQuestionResponse#getResponse()
*/
public function getResponse() {
return $this->answer;
}
/**
* Implementation of getReportFormResponse
*
* @see QuizQuestionResponse#getReportFormResponse($showpoints, $showfeedback, $allow_scoring)
*/
public function getReportFormResponse($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
$form = array();
$form['#theme'] = 'assessment_question_response_form';
if ($this->question && !empty($this->question->answers)) {
$answer = (object) current($this->question->answers);
}
else {
return $form;
}
if ($answer->is_evaluated == 1) {
// Show feedback, if any.
if ($showfeedback && !empty($answer->feedback)) {
$feedback = check_markup($answer->feedback);
}
}
else {
$feedback = t('This answer has not yet been scored.') .
'<br/>' .
t('Until the answer is scored, the total score will not be correct.');
}
if ($allow_scoring) {
$question = node_load($this->question->nid);
$rubric_options_result = field_get_items('node', $question, 'field_rubric_options');
$options = array();
foreach ($rubric_options_result as $key => $value) {
$options[$key] = $value['safe_value'];
}
$question->content = node_view($question, 'full');
$form['rubric'] = array(
'#type' => 'radios',
'#title' => $question->title,
'#description' => t('Select an option to grade this criteria'),
'#options' => $options,
'#default_value' => $this->answer,
'#attributes' => array('class' => array('rubric')),
);
}
/*
if (!$allow_scoring && !empty($this->answer_feedback)) {
$form['answer_feedback'] = array(
'#title' => t('Feedback'),
'#type' => 'item',
'#markup' => '<span class="quiz_answer_feedback">' . check_markup($this->answer_feedback, $this->answer_feedback_format) . '</span>',
);
}
if (!empty($feedback)) {
$form['feedback'] = array('#markup' => '<span class="quiz_answer_feedback">' . $feedback . '</span>');
}
*/
$form['#attached']['css'][] = drupal_get_path('module', 'assessment_question') . '/theme/assessment-question-radios.css';
$form['#attached']['js'][] = drupal_get_path('module', 'assessment_question') . '/theme/assessment-question-radios.js';
return $form;
}
/**
* Implementation of getReportFormScore
*
* @see QuizQuestionResponse#getReportFormScore($showpoints, $showfeedback, $allow_scoring)
*/
public function getReportFormScore($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
// The score will be shown as a questionmark if the question haven't been scored already
$score = ($this->isEvaluated()) ? $this->getScore() : 0;
// We show a textfield if the quiz shall be scored. Markup otherwise
if (quiz_access_to_score() && $allow_scoring) {
return array(
'#type' => 'textfield',
'#default_value' => $score,
'#size' => 3,
'#maxlength' => 3,
'#attributes' => array('class' => array('quiz-report-score')),
);
}
else {
return array('#markup' => $score);
}
}
public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
if (quiz_access_to_score() && $allow_scoring) {
return array(
'#title' => t('Feedback'),
'#type' => 'textarea',
'#rows' => 2,
'#default_value' => isset($this->answer_feedback) ? check_markup($this->answer_feedback, $this->answer_feedback_format) : '',
'#format' => isset($this->answer_feedback_format) ? $this->answer_feedback_format : NULL,
'#attributes' => array('class' => array('quiz-report-score')),
);
}
return FALSE;
}
/**
* Implementation of getReportFormSubmit
*
* @see QuizQuestionResponse#getReportFormSubmit($showfeedback, $showpoints, $allow_scoring)
*/
public function getReportFormSubmit($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
return $allow_scoring ? 'assessment_question_report_submit' : FALSE;
}
/**
* Implementation of getReportFormValidate
*
* @see QuizQuestionResponse#getReportFormValidate($showfeedback, $showpoints, $allow_scoring)
*/
public function getReportFormValidate($showfeedback = TRUE, $showpoints = TRUE, $allow_scoring = FALSE) {
return $allow_scoring ? 'assessment_question_report_validate' : FALSE;
}
/**
* Check to see if the answer is marked as correct.
*
* This default version returns TRUE iff the score is equal to the maximum possible score.
*/
function getScore($weight_adjusted = TRUE) {
if ($this->is_skipped) {
return 0;
}
if (!isset($this->score)) {
$this->score = $this->score();
}
if (isset($this->question->score_weight) && $weight_adjusted) {
return round($this->score * $this->question->score_weight);
}
return $this->score;
}
public function getReportFormTheme($showfeedback = TRUE, $showpoints = TRUE) {
return 'assessment_question_report_form';
}
}