Skip to content

Commit c6fdfd3

Browse files
committed
Initial commit
0 parents  commit c6fdfd3

8 files changed

+309
-0
lines changed

h5p_quiz.info

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = H5p Quiz
2+
description = Provides integration for H5p and quiz
3+
core = 7.x
4+
package = Quiz

h5p_quiz.install

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Install logic.
6+
*/
7+
8+
/**
9+
* Implements hook_schema().
10+
*/
11+
function h5p_quiz_schema() {
12+
return array(
13+
'h5p_quiz_data' => array(
14+
'description' => 'H5p quiz data',
15+
'fields' => array(
16+
'uid' => array(
17+
'description' => 'The user ID this data belongs to.',
18+
'type' => 'int',
19+
'unsigned' => TRUE,
20+
'not null' => TRUE,
21+
),
22+
'question_id' => array(
23+
'description' => 'The h5p question id.',
24+
'type' => 'int',
25+
'unsigned' => TRUE,
26+
'not null' => TRUE,
27+
),
28+
'question_vid' => array(
29+
'description' => 'The h5p question vid.',
30+
'type' => 'int',
31+
'unsigned' => TRUE,
32+
'not null' => TRUE,
33+
),
34+
'value' => array(
35+
'type' => 'text',
36+
),
37+
),
38+
'primary key' => array('uid', 'question_id','question_vid'),
39+
'indexes' => array(
40+
'uid' => array('uid'),
41+
'question_id' => array('question_id'),
42+
'question_vid' => array('question_vid'),
43+
),
44+
),
45+
);
46+
}

h5p_quiz.module

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
function h5p_quiz_data_load_score($uid, $question_id, $question_vid) {
4+
$data = NULL;
5+
$result = db_select('h5p_quiz_data', 'h5pd')
6+
->fields('h5pd', array('value'))
7+
->condition('h5pd.uid', $uid)
8+
->condition('h5pd.question_id', $question_id)
9+
->condition('h5pd.question_id', $question_vid)
10+
->execute()
11+
->fetchObject();
12+
if (isset($result->value)) {
13+
$data = $result->value;
14+
}
15+
return $data;
16+
}
17+
18+
function h5p_quiz_data_set_score($uid, $question_id, $question_vid, $value) {
19+
$result = db_merge('h5p_quiz_data')
20+
->key(array(
21+
'uid' => $uid,
22+
'question_id' => $question_id,
23+
))
24+
->fields(array(
25+
'uid' => $uid,
26+
'question_id' => $question_id,
27+
'value' => $value,
28+
))
29+
->execute();
30+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name = H5p Quiz Question
2+
description = Quiz question type for H5p
3+
core = 7.x
4+
package = Quiz
5+
6+
dependencies[] = h5p_quiz
7+
8+
files[] = includes/h5p_quiz_question.question.inc
9+
files[] = includes/h5p_quiz_question.response.inc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Install logic.
6+
*/
7+
8+
/**
9+
* Implements hook_schema().
10+
*/
11+
function h5p_quiz_question_schema() {
12+
return array(
13+
'h5p_quiz_user_results' => array(
14+
'fields' => array(
15+
'question_nid' => array(
16+
'type' => 'int',
17+
'unsiged' => TRUE,
18+
'not null' => TRUE,
19+
),
20+
'question_vid' => array(
21+
'type' => 'int',
22+
'unsiged' => TRUE,
23+
'not null' => TRUE,
24+
),
25+
'result_id' => array(
26+
'type' => 'int',
27+
'unsigned' => TRUE,
28+
'not null' => TRUE,
29+
),
30+
'score_scaled' => array(
31+
'type' => 'float',
32+
'size' => 'big',
33+
),
34+
),
35+
'primary key' => array('question_nid', 'question_vid', 'result_id'),
36+
),
37+
);
38+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Module hook definitions.
6+
*/
7+
8+
/**
9+
* Implements of hook_quiz_question_info().
10+
*/
11+
function h5p_quiz_question_quiz_question_info() {
12+
return array(
13+
'h5p_quiz_question' => array(
14+
'name' => t('H5p Question'),
15+
'description' => t('Question using H5p.'),
16+
'question provider' => 'H5pQuizQuestion',
17+
'response provider' => 'H5pQuizResponse',
18+
'module' => 'quiz_question',
19+
),
20+
);
21+
}
22+
23+
/**
24+
* Implements hook_config().
25+
*
26+
* Quiz Question triggers a Fatal Error if this function is not present.
27+
* This is a no op.
28+
*/
29+
function h5p_quiz_question_config() {
30+
// No op
31+
}
32+
33+
/**
34+
* Implements hook_node_type_insert().
35+
*/
36+
function h5p_quiz_question_node_type_insert($info) {
37+
if ($info->type == 'h5p_quiz_question') {
38+
quiz_question_add_body_field('h5p_quiz_question');
39+
variable_set('node_options_h5p_quiz_question', array('status'));
40+
// Disable comments by default.
41+
if (module_exists('comment')) {
42+
variable_set('comment_h5p_quiz_question', COMMENT_NODE_CLOSED);
43+
}
44+
variable_set('node_submitted_h5p_quiz_question', 0);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Question type class definition.
6+
*/
7+
8+
class H5pQuizQuestion extends QuizQuestion {
9+
10+
/**
11+
* @copydoc QuizQuestion::validateNode()
12+
*/
13+
public function validateNode(array &$form) {
14+
// No validation required
15+
}
16+
17+
/**
18+
* @copydoc QuizQuestion::getCreationForm()
19+
*/
20+
public function getCreationForm(array &$form_state = NULL) {
21+
// No special fields.
22+
return array();
23+
}
24+
25+
/**
26+
* @copydoc QuizQuestion::getAnsweringForm()
27+
*/
28+
public function getAnsweringForm(array $form_state = NULL, $rid) {
29+
return array(
30+
'tries' => array(
31+
'#type' => 'hidden',
32+
'#default_value' => 1,
33+
),
34+
);
35+
}
36+
37+
/**
38+
* @copydoc QuizQuestion::getMaximumScore()
39+
*/
40+
public function getMaximumScore() {
41+
return variable_get('h5p_quiz_question_max_score', 50);
42+
}
43+
44+
/**
45+
* @copydoc QuizQuestion::saveNodeProperties()
46+
*/
47+
public function saveNodeProperties($is_new = FALSE) {
48+
// No properties to save.
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Response type class definition.
6+
*/
7+
8+
class H5pQuizResponse extends QuizQuestionResponse {
9+
10+
/**
11+
* @copydoc QuizQuestionResponse::save()
12+
*/
13+
public function save() {
14+
global $user;
15+
16+
$scaled = h5p_quiz_data_load_score($user->uid,$this->question->nid,$this->question->vid);
17+
18+
// Something went wrong. Set a score of -1.
19+
if (!isset($scaled) || !is_numeric($scaled)) {
20+
$scaled = -1;
21+
}
22+
23+
db_insert('h5p_quiz_user_results')
24+
->fields(array(
25+
'question_nid' => $this->question->nid,
26+
'question_vid' => $this->question->vid,
27+
'result_id' => $this->rid,
28+
'score_scaled' => $scaled,
29+
))
30+
->execute();
31+
}
32+
33+
/**
34+
* @copydoc QuizQuestionResponse::delete()
35+
*/
36+
public function delete() {
37+
db_delete('h5p_quiz_user_results')
38+
->condition('question_nid', $this->question->nid)
39+
->condition('question_vid', $this->question->vid)
40+
->condition('result_id', $this->rid)
41+
->execute();
42+
}
43+
44+
/**
45+
* @copydoc QuizQuestionResponse::score()
46+
*/
47+
public function score() {
48+
$scaled = db_select('h5p_quiz_user_results', 'o')
49+
->fields('o', array('score_scaled'))
50+
->condition('question_nid', $this->question->nid)
51+
->condition('question_vid', $this->question->vid)
52+
->condition('result_id', $this->rid)
53+
->execute()
54+
->fetchField();
55+
56+
57+
$scaled *= $this->getMaxScore();
58+
59+
return $scaled;
60+
}
61+
62+
/**
63+
* @copydoc QuizQuestionResponse::getResponse()
64+
*/
65+
public function getResponse() {
66+
return 'In H5p package';
67+
}
68+
69+
/**
70+
* Implementation of getReportForm
71+
*
72+
* @see QuizQuestionResponse#getReportForm($showpoints, $showfeedback, $allow_scoring)
73+
*/
74+
public function getReportForm($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
75+
return array(
76+
'#no_report' => TRUE,
77+
);
78+
}
79+
80+
81+
public function getReportFormAnswerFeedback($showpoints = TRUE, $showfeedback = TRUE, $allow_scoring = FALSE) {
82+
return FALSE;
83+
}
84+
85+
}

0 commit comments

Comments
 (0)