/[drupal]/contributions/modules/quiz/quiz.pages.inc
ViewVC logotype

Contents of /contributions/modules/quiz/quiz.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.7 - (show annotations) (download) (as text)
Thu Aug 13 16:48:17 2009 UTC (3 months, 1 week ago) by sivaji
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +1 -1 lines
File MIME type: text/x-php
Merging GSoC code with HEAD.
1 <?php
2 // $Id: quiz.pages.inc,v 1.6 2009/08/13 16:37:12 sivaji Exp $
3
4 /**
5 * User pages.
6 * @file
7 */
8
9 /*
10 * Quiz Results User.
11 */
12 function quiz_user_results($result_id) {
13 global $user;
14
15 $sql = 'SELECT qnp.nid, qnrs.uid
16 FROM {quiz_node_properties} qnp
17 INNER JOIN {quiz_node_results} qnrs ON qnrs.nid = qnp.nid
18 WHERE qnrs.result_id = %d';
19 $result = db_fetch_object(db_query( $sql, $result_id));
20 if ($result->nid) {
21
22 // User can view own results (quiz_menu sets access to 'own results').
23 // User with role 'user results' can view other user's results.
24 if ($result->uid != $user->uid && !user_access('view user results')) {
25 drupal_access_denied();
26 return;
27 }
28
29 $quiz = node_load($result->nid);
30 $questions = _quiz_get_answers($result_id);
31 $score = quiz_calculate_score($quiz, $result_id);
32 $summary = _quiz_get_summary_text($quiz, $score);
33 return theme('quiz_user_summary', $quiz, $questions, $score, $summary);
34 }
35 else {
36 drupal_not_found();
37 }
38 }
39
40 /**
41 * Displays all the quizzes the user has taken part in.
42 *
43 * @return
44 * HTML output for page.
45 */
46 function quiz_get_user_results($user_id) {
47 global $user;
48
49 if ($user_id == $user->uid || user_access('view user results')) {
50 $results = array();
51 $sql = "SELECT n.nid, n.title, u.name, qnrs.result_id, qnrs.time_start, qnrs.time_end
52 FROM {node} n
53 INNER JOIN {quiz_node_properties} qnp ON n.nid = qnp.nid
54 INNER JOIN {quiz_node_results} qnrs ON qnrs.nid = qnp.nid
55 INNER JOIN {users} u ON u.uid = qnrs.uid
56 WHERE n.type = 'quiz'
57 AND u.uid = %d
58 ORDER BY qnrs.result_id ASC";
59
60 $dbresult = db_query($sql, $user_id);
61 // Create an array out of the results.
62 while ($line = db_fetch_array($dbresult)) {
63 $results[$line['result_id']] = $line;
64 }
65 return theme('quiz_get_user_results', $results);
66 }
67 else {
68 return 'You have no permission to view the results of this user.';
69 }
70 }
71
72 // THEME FUNCTIONS
73
74 /**
75 * Theme a message about the quiz's availability for quiz takers.
76 *
77 * @ingroup themeable
78 */
79 function theme_quiz_availability($node) {
80 $status = _quiz_availability($node);
81 $output = '<div class="quiz_availability"><p>';
82 switch ($status) {
83 case 'future':
84 $output .= t('This quiz will not be available until %time.', array('%time' => format_date($node->quiz_open)));
85 break;
86 case 'open':
87 $output .= t('This quiz closes %time.', array('%time' => format_date($node->quiz_close)));
88 break;
89 case 'closed':
90 $output .= t('This quiz is no longer available.');
91 break;
92 }
93 $output .= '</p></div>'."\n";
94 return $output;
95 }
96
97
98 /**
99 * Theme the node view for quizzes.
100 *
101 * @ingroup themeable
102 */
103 function theme_quiz_view($node, $teaser = FALSE, $page = FALSE) {
104 $output = '';
105 // Output quiz options.
106 $output .= '<h3>'. t('@quiz Options', array('@quiz' => QUIZ_NAME)) .'</h3>';
107 $header = array(
108 t('# of Random Questions'),
109 t('Shuffle?'),
110 t('Feedback'),
111 t('Number of takes'),
112 );
113 $shuffle = $node->shuffle == 1 ? t('Yes') : t('No');
114 $feedback_options = _quiz_get_feedback_options();
115 $feedback = $feedback_options[$node->feedback_time];
116 $takes = $node->takes == 0 ? t('Unlimited') : check_plain($node->takes);
117 $rows = array();
118 $rows[] = array(
119 check_plain($node->number_of_random_questions),
120 $shuffle,
121 $feedback,
122 $takes,
123 );
124 $output .= theme('table', $header, $rows);
125 // Format Quiz Dates.
126 $output .= '<h3>'. t('@quiz start/end', array('@quiz' => QUIZ_NAME)) .'</h3>';
127 if (!$node->quiz_always) {
128 // If we are previewing, make sure the dates are timestamps and not form arrays.
129 if (is_array($node->quiz_open)) {
130 quiz_translate_form_date($node, 'quiz_open');
131 }
132 if (is_array($node->quiz_close)) {
133 quiz_translate_form_date($node, 'quiz_close');
134 }
135
136 // Format the availability info.
137 $output .= '<p>'. format_date($node->quiz_open) .' &mdash; '. format_date($node->quiz_close) .'</p>';
138 $output .= '<p><strong>'. t('Days @quiz live for: ', array('@quiz' => QUIZ_NAME)) .'</strong> '. floor(($node->quiz_close - $node->quiz_open) / 60 / 60 / 24) .'</p>';
139 $remaining = floor(($node->quiz_close - time()) / 60 / 60 / 24);
140 $remaining = ($remaining < 0) ? t('Expired') : $remaining;
141 $output .= '<p><strong>'. t('Days remaining:') . '</strong> '. $remaining .'</p>';
142 $elapsed = floor((time() - $node->quiz_open) / 60 / 60 / 24);
143 $elapsed = ($elapsed < 0)?(-$elapsed) . t(' days to go') : $elapsed;
144 $output .= '<p><strong>' . t('Days since start:') . '</strong> '. $elapsed .'</p>';
145 }
146 else {
147 $output .= '<p>'. t('This Quiz is always available.') .'</p>'."\n";
148 }
149
150 // Format taxonomy selection (if applicable).
151 if (function_exists('taxonomy_node_get_terms')) {
152 $output .= '<h3>'. t('Taxonomy selection') .'</h3>';
153 $terms = array();
154 foreach (taxonomy_node_get_terms($node) as $term) {
155 $terms[] = check_plain($term->name);
156 }
157 if (!empty($terms)) {
158 $terms = implode(', ', $terms);
159 $output .= "<p>$terms</p>";
160 }
161 else {
162 $output .= '<p>'. t('No selected terms found') .'</p>';
163 }
164 }
165
166 // Format pass / fail and summary options.
167 if ($node->pass_rate || $node->summary_default || $node->summary_pass) {
168 if ($node->pass_rate) {
169 $output .= '<h3>'. t('Pass / fail and summary options') .'</h3>'."\n";
170 $output .= '<p><strong>'. t('Percentage needed to pass:') .'</strong> '. check_plain($node->pass_rate) .'</p>'."\n";
171 $output .= '<div><strong>'. t('Summary text if the user passed:') .'</strong> ';
172 $output .= ($node->summary_pass) ? check_markup($node->summary_pass, $node->format) : t('No text defined.');
173 $output .= '</div>'."\n";
174 }
175 $output .= '<div><strong>'. t('Default summary text:') .'</strong> ';
176 $output .= !empty($node->summary_default) ? check_markup($node->summary_default, $node->format) : t('No text defined.');
177 $output .= '</div>'."\n";
178 }
179
180 // Format result options if available.
181 if (!empty($node->resultoptions)) {
182 $scored_quiz = ($node->pass_rate > 0);
183
184 $output .= '<h3>'. t('!quiz Results', array('!quiz' => QUIZ_NAME)) .'</h3>';
185
186 $header = array(t('Name') => 'option_name', t('Summary') => 'option_summary');
187 if ($scored_quiz) {
188 $header = array_merge($header, array(t('Start') => 'option_start', t('End') => 'option_end'));
189 }
190 $values = array_values($header);
191
192 foreach ($node->resultoptions as $option) {
193 $row = array();
194 foreach ($values as $field) {
195 $row[] = $option[$field];
196 }
197 $option_rows[] = $row;
198 }
199 $output .= theme('table', array_keys($header), $option_rows);
200
201 }
202
203 // Format quiz questions.
204 if (is_numeric(arg(1))) {
205 $output .= '<h3>'. t('@quiz Questions', array('@quiz' => QUIZ_NAME)) .'</h3>';
206 $questions = _quiz_get_questions($node->nid, $node->vid);
207 $output .= theme('quiz_question_table', $questions, $node->nid);
208 }
209 return $output;
210 }
211
212 /**
213 * Theme the user results page.
214 *
215 * @param $results
216 * An array of quiz information.
217 * @return
218 * Themed html.
219 *
220 * @ingroup themeable
221 */
222 function theme_quiz_get_user_results($results) {
223 $output = '';
224 $rows = array();
225 while (list($key, $result) = each($results)) {
226 $rows[] = array(
227 l('view', 'user/quiz/'. $result['result_id'] .'/userresults'),
228 check_plain($result['title']),
229 check_plain($result['name']),
230 $result['result_id'],
231 format_date($result['time_start'], 'small'),
232 ($result['time_end'] > 0) ? format_date($result['time_end'], 'small') : t('In Progress'),
233 );
234 }
235
236 $header = array(
237 t('Action'),
238 t('@quiz Title', array('@quiz' => QUIZ_NAME)),
239 t('Username'),
240 t('Result<br />ID'),
241 t('Time Started'),
242 t('Finished?'));
243
244 if (!empty($rows)) {
245 $output .= theme('table', $header, $rows);
246 }
247 else {
248 $output .= t('No @quiz results found.', array('@quiz' => QUIZ_NAME));
249 }
250 return $output;
251 }
252
253 /**
254 * Theme the filtered question list.
255 *
256 * @ingroup themeable
257 */
258 function theme_quiz_filtered_questions($form) {
259 $quiz_id = is_numeric(arg(1)) ? arg(1) : NULL;
260 $header = array(t('Random'), t('Always'), t('Never'), t('Question'), t('Type'), t('Edit'));
261 $rows = array();
262 $output = '';
263
264 while (list($nid, $values) = each($form['question_status'])) {
265 if (is_numeric($nid)) {
266 $rows[] = array(
267 drupal_render($form['question_status'][$nid][QUIZ_FEEDBACK_END]),
268 drupal_render($form['question_status'][$nid][QUIZ_FEEDBACK_QUESTION]),
269 drupal_render($form['question_status'][$nid][QUIZ_FEEDBACK_NEVER]),
270 drupal_render($form['question'][$nid]),
271 drupal_render($form['type'][$nid]),
272 l(t('Edit'), 'node/'. $nid .'/edit/'. $quiz_id),
273 );
274 }
275 }
276 if (!empty($rows)) {
277 $output .= theme('table', $header, $rows);
278 }
279 else {
280 $output .= t('No questions found.');
281 }
282 return $output;
283 }
284
285 /**
286 * Theme a table containing array of questions and options.
287 *
288 * @param $questions
289 * Array of question nodes.
290 * @return
291 * HTML for a table.
292 *
293 * @ingroup themeable
294 */
295 function theme_quiz_question_table($questions, $quiz_id = NULL) {
296 $output = '';
297 $rows = array();
298 $status_descriptions = array(t('Random'), t('Always'), t('Never'));
299 while (list($key, $question) = each($questions)) {
300 $rows[] = array(
301 $status_descriptions[$question->question_status],
302 $question->question,
303 $question->type,
304 l(t('Edit'), 'node/'. $question->nid .'/edit/'. $quiz_id));
305 }
306 $header = array(t('Status'), t('Question'), t('Type'), t('Edit'));
307
308 if (!empty($rows)) {
309 $output .= theme('table', $header, $rows);
310 }
311 else {
312 $output .= t('No questions found.');
313 }
314 return $output;
315 }
316
317 /**
318 * Pass the correct mark to the theme so that theme authors can use an image.
319 *
320 * @ingroup themeable
321 */
322 function theme_quiz_score_correct() {
323 return theme('image', drupal_get_path('module', 'quiz') .'/images/correct.gif', t('correct'));
324 }
325
326 /**
327 * Pass the incorrect mark to the theme so that theme authors can use an image.
328 *
329 * @ingroup themeable
330 */
331 function theme_quiz_score_incorrect() {
332 return theme('image', drupal_get_path('module', 'quiz') .'/images/incorrect.gif', t('incorrect'));
333 }
334
335 /**
336 * Theme a progress indicator for use during a quiz.
337 *
338 * @param $question_number
339 * The position of the current question in the sessions' array.
340 * @param $num_of_question
341 * The number of questions for this quiz as returned by quiz_get_number_of_questions().
342 * @return
343 * Themed html.
344 *
345 * @ingroup themeable
346 */
347 function theme_quiz_progress($question_number, $num_of_question) {
348 // Determine the percentage finished (not used, but left here for other implementations).
349 //$progress = ($question_number*100)/$num_of_question;
350
351 // Get the current question # by adding one.
352 $current_question = $question_number + 1;
353
354 $output = '';
355 $output .= '<div id="quiz_progress">';
356 $output .= t('Question %x of %y', array('%x' => $current_question, '%y' => $num_of_question));
357 $output .= '</div><br />'."\n";
358 $output .= '<div class="countdown"></div>';
359 return $output;
360 }
361
362 /**
363 * Theme a question page.
364 *
365 * @param $quiz
366 * The quiz node object.
367 * @param $question_node
368 * The question node.
369 * @return
370 * Themed html.
371 *
372 * @ingroup themeable
373 *
374 * @deprecated This should not be used. Rendering is now done through the standard node rendering system.
375 */
376 function theme_quiz_take_question($quiz, $question_node) {
377 //Calculation for quiz progress bar.
378 $number_of_questions = quiz_get_number_of_questions($quiz->vid, $quiz->nid);
379 $question_number = $number_of_questions - count($_SESSION['quiz_'. $quiz->nid]['quiz_questions']);
380 $question_node->question_number = $question_number;
381 // Set the title here in case themers want to do something different.
382 drupal_set_title(check_plain($quiz->title));
383
384 // Return the elements of the page.
385 $output = '';
386 $output .= theme('quiz_progress', $question_number, $number_of_questions);
387 $output .= module_invoke($question_node->type, 'render_question', $question_node);
388 return $output;
389 }
390
391 /**
392 * Theme the summary page after the quiz has been completed.
393 *
394 * @param $quiz
395 * The quiz node object.
396 * @param $questions
397 * The questions array as defined by _quiz_get_answers.
398 * @param $score
399 * Array of score information as returned by quiz_calculate_score().
400 * @param $summary
401 * Filtered text of the summary.
402 * @return
403 * Themed html.
404 *
405 * @ingroup themeable
406 */
407 function theme_quiz_take_summary($quiz, $questions, $score, $summary) {
408 // Set the title here so themers can adjust.
409 drupal_set_title(check_plain($quiz->title));
410
411 // Display overall result.
412 $output = '';
413
414 // Only display scoring information if this is not a personality test:
415 //if ($score['percentage_score']) {
416 if (!empty($score['possible_score'])) {
417 if (!$score['is_evaluated']) {
418 $msg = t(
419 'Parts of this @quiz have not been evaluated yet. The score below is not final.',
420 array('@quiz' => QUIZ_NAME)
421 );
422 drupal_set_message($msg, 'error');
423 }
424 $output .= '<div id="quiz_score_possible">'. t('You got %num_correct of %question_count possible points.', array('%num_correct' => $score['numeric_score'], '%question_count' => $score['possible_score'])) .'</div>'."\n";
425 $output .= '<div id="quiz_score_percent">'. t('Your score: %score%', array('%score' => $score['percentage_score'])) .'</div><br />'."\n";
426 }
427 $output .= '<div id="quiz_summary">'. check_markup($summary, $quiz->format) .'</div><br />'."\n";
428 // Get the feedback for all questions.
429 $output .= theme('quiz_feedback', $questions, ($quiz->pass_rate > 0), TRUE);
430
431 return $output;
432 }
433
434 /**
435 * Theme the summary page for user results.
436 *
437 * @param $quiz
438 * The quiz node object.
439 * @param $questions
440 * The questions array as defined by _quiz_get_answers.
441 * @param $score
442 * Array of score information as returned by quiz_calculate_score().
443 * @param $summary
444 * Filtered text of the summary.
445 * @return
446 * Themed html.
447 *
448 * @ingroup themeable
449 */
450 function theme_quiz_user_summary($quiz, $questions, $score, $summary) {
451 // Set the title here so themers can adjust.
452 drupal_set_title(check_plain($quiz->title));
453
454 if (!$score['is_evaluated']) {
455 $msg = t('Parts of this @quiz have not been evaluated yet. The score below is not final.', array('@quiz' => QUIZ_NAME));
456 drupal_set_message($msg, 'status');
457 }
458
459 // Display overall result.
460 $output = '';
461 $output .= '<div id="quiz_score_possible">'. t('You got %num_correct of %question_count possible points.', array('%num_correct' => $score['numeric_score'], '%question_count' => $score['possible_score'])) .'</div>'."\n";
462 $output .= '<div id="quiz_score_percent">'. t('Your score was: @score%', array('@score' => $score['percentage_score'])) .'</div><br />'."\n";
463 $output .= '<div id="quiz_summary">'. check_markup($summary,$quiz->format) .'</div><br />'."\n";
464 // Get the feedback for all questions.
465 $output .= theme('quiz_feedback', $questions, FALSE, TRUE);
466 return $output;
467 }
468
469 /**
470 * Theme the question feedback.
471 *
472 * @param $questions
473 * Array of quiz questions objects as returned by _quiz_get_answers.
474 * @param showpoints
475 * Binary flag for whether to show the actual answers or not.
476 * @param $showfeedback
477 * Binary flag for whether to show question feedback.
478 * @return
479 * Themed html.
480 *
481 * @ingroup themeable
482 */
483 function theme_quiz_feedback($questions, $showpoints = TRUE, $showfeedback = FALSE) {
484 $header = array(format_plural(count($questions), t('Question Result'), t('Question Results')));
485 $rows = array();
486
487 // Go through each of the questions.
488 foreach ($questions as $question) {
489 $cols = array();
490 // Ask each question to render a themed report of how the user did.
491 $module = quiz_module_for_type($question->type);
492 $report = theme($module .'_report', $question, $showpoints, $showfeedback);
493 // Only include reports with actual data:
494 if (!empty($report) && strlen($report) > 0) {
495
496 $cols[] = array('data' => $report, 'class' => 'quiz_summary_qrow');
497
498 // Get the score result for each question only if it's a scored quiz.
499 if ($showpoints) {
500
501 $theme = ($question->correct) ? 'quiz_score_correct' : 'quiz_score_incorrect';
502 $cols[] = array('data' => theme($theme), 'class' => 'quiz_summary_qcell');
503 }
504 // Pack all of this into a table row.
505 $rows[] = array('data' => $cols, 'class' => 'quiz_summary_qrow');
506 }
507 }
508 return theme('table', $header, $rows);
509 }
510
511 /**
512 * Theme feedback for one question.
513 *
514 * @param $quiz
515 * Quiz node (may not be needed).
516 * @param $question_node
517 * The question node giving feedback for.
518 * @param $answer
519 * User's response to previous question.
520 * @return
521 * Themed html.
522 *
523 * @ingroup themeable
524 */
525 function theme_quiz_single_question_feedback($quiz, $report) {
526 $output = '<div class="quiz-summary-question">';
527 $output .= theme($report->type .'_feedback', $quiz, $report);
528 $output .= '</div><br class="clear" />';
529 return $output;
530 }
531
532 /**
533 * Allow the option to theme the questions form.
534 *
535 * @ingroup themeable
536 */
537 function theme_quiz_questions($form) {
538 $output = '';
539 $output .= drupal_render($form);
540 return $output;
541 }
542
543 /**
544 * Theme the "no feedback" option.
545 *
546 * @return
547 * Themed html feedback.
548 *
549 * @ingroup themeable
550 */
551 function theme_quiz_no_feedback() {
552 return t('Thanks for taking the quiz!');
553 }
554
555 function theme_quiz_single_question_node($node) {
556 return $node->body;
557 }
558
559 /*
560 * includes css and js for jQuery table sorter
561 *
562 */
563 function quiz_add_table_sorter() {
564 $table_sorter_path = drupal_get_path('module', 'quiz') . '/includes/table_sorter/';
565 drupal_add_css($table_sorter_path . 'quiz.table_sorter.css');
566 drupal_add_js($table_sorter_path . 'jquery.tablesorter.min.js');
567 drupal_add_js($table_sorter_path . 'quiz.table_sorter.js');
568 }

  ViewVC Help
Powered by ViewVC 1.1.2