From b5a16eb72ec96b963700f5c674ee4451aa3c3fbd Mon Sep 17 00:00:00 2001 From: tjhunt Date: Fri, 21 Sep 2007 11:05:04 +0000 Subject: [PATCH] MDL-6376 - Include quizzes on the MyMoodle page. Initial implementation by Stephen Bourget, with refinements by Tim Hunt. The logic for which quizzes are listed is very similar to the lesson module. --- lang/en_utf8/quiz.php | 2 + mod/quiz/lib.php | 91 +++++++++++++++++++++++++++++++++++++++++++ mod/quiz/locallib.php | 25 ------------ 3 files changed, 93 insertions(+), 25 deletions(-) diff --git a/lang/en_utf8/quiz.php b/lang/en_utf8/quiz.php index e0a1a65c18..d3e8f9e866 100644 --- a/lang/en_utf8/quiz.php +++ b/lang/en_utf8/quiz.php @@ -365,6 +365,7 @@ $string['notenoughanswers'] = 'This type of question requires at least $a answer $string['notenoughsubquestions'] = 'Not enough sub-questions have been defined!
Do you want to go back and fix this question?'; $string['notimedependentitems'] = 'Time dependent items are not currently supported by the quiz module. As a work around, set a time limit for the whole quiz. Do you wish to choose a different item (or use the current item regardless)?'; $string['numattempts'] = '$a->studentnum $a->studentstring have made $a->attemptnum attempts'; +$string['numattemptsmade'] = '$a attempts made on this quiz'; $string['numberabbr'] = '#'; $string['numerical'] = 'Numerical'; $string['onlyteachersexport'] = 'Only teachers can export questions'; @@ -426,6 +427,7 @@ $string['quizavailable'] = 'The quiz is available until: $a'; $string['quizclose'] = 'Close the quiz'; $string['quizclosed'] = 'This quiz closed on $a'; $string['quizcloses'] = 'Quiz closes'; +$string['quizcloseson'] = 'This quiz will close at $a'; $string['quiznotavailable'] = 'The quiz will not be available until: $a'; $string['quizopen'] = 'Open the quiz'; $string['quizopens'] = 'Quiz opens'; diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index b92cf28465..1dd6175b25 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -231,6 +231,30 @@ function quiz_cron () { return true; } +/** + * @param integer $quizid the quiz id. + * @param integer $userid the userid. + * @param string $status 'all', 'finished' or 'unfinished' to control + * @return an array of all the user's attempts at this quiz. Returns an empty array if there are none. + */ +function quiz_get_user_attempts($quizid, $userid, $status = 'finished', $includepreviews = false) { + $status_condition = array( + 'all' => '', + 'finished' => ' AND timefinish > 0', + 'unfinished' => ' AND timefinish = 0' + ); + $previewclause = ''; + if (!$includepreviews) { + $previewclause = ' AND preview = 0'; + } + if ($attempts = get_records_select('quiz_attempts', + "quiz = '$quizid' AND userid = '$userid'" . $previewclause . $status_condition[$status], + 'attempt ASC')) { + return $attempts; + } else { + return array(); + } +} /** * Return grade for given user or all users. @@ -955,4 +979,71 @@ function quiz_check_file_access($attemptid, $questionid) { // otherwise, this user does not have permission return false; } + +/** + * Prints quiz summaries on MyMoodle Page + */ +function quiz_print_overview($courses, &$htmlarray) { + global $USER, $CFG; +/// These next 6 Lines are constant in all modules (just change module name) + if (empty($courses) || !is_array($courses) || count($courses) == 0) { + return array(); + } + + if (!$quizs = get_all_instances_in_courses('quiz', $courses)) { + return; + } + +/// Fetch some language strings outside the main loop. + $strquiz = get_string('modulename', 'quiz'); + $strnoattempts = get_string('noattempts', 'quiz'); + +/// We want to list quizzes that are currently available, and which have a close date. +/// This is the same as what the lesson does, and the dabate is in MDL-10568. + $now = date(); + foreach ($quizs as $quiz) { + if ($quiz->timeclose >= $now && $quiz->timeopen < $now) { + /// Give a link to the quiz, and the deadline. + $str = '
' . + ''; + $str .= '
' . get_string('quizcloseson', 'quiz', userdate($quiz->timeclose)) . '
'; + + /// Now provide more information depending on the uers's role. + $context = get_context_instance(CONTEXT_MODULE, $quiz->coursemodule); + if (has_capability('mod/quiz:viewreports', $context)) { + /// For teacher-like people, show a summary of the number of student attempts. + $a = new stdClass; + if ($a->attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) { + $a->studentnum = count_records_select('quiz_attempts', "quiz = '$quiz->id' AND preview = '0'", 'COUNT(DISTINCT userid)'); + } else { + $a->studentnum = 0; + $a->attemptnum = 0; + } + $a->studentstring = $course->students; + $str .= '
' . get_string('numattempts', 'quiz', $a) . '
'; + } else if (has_capability('mod/quiz:attempt', $context)){ // Student + /// For student-like people, tell them how many attempts they have made. + if (isset($USER->id) && ($attempts = quiz_get_user_attempts($quiz->id, $USER->id))) { + $numattempts = count($attempts); + $str .= '
' . get_string('numattemptsmade', 'quiz', $numattempts) . '
'; + } else { + $str .= '
' . $strnoattempts . '
'; + } + } else { + /// For ayone else, there is no point listing this quiz, so stop processing. + continue; + } + + /// Add the output for this quiz to the rest. + $str .= '
'; + if (empty($htmlarray[$quiz->course]['quiz'])) { + $htmlarray[$quiz->course]['quiz'] = $str; + } else { + $htmlarray[$quiz->course]['quiz'] .= $str; + } + } + } +} ?> diff --git a/mod/quiz/locallib.php b/mod/quiz/locallib.php index 46f5556340..fb6b8b24d2 100644 --- a/mod/quiz/locallib.php +++ b/mod/quiz/locallib.php @@ -90,31 +90,6 @@ function quiz_get_user_attempt_unfinished($quizid, $userid) { } } -/** - * @param integer $quizid the quiz id. - * @param integer $userid the userid. - * @param string $status 'all', 'finished' or 'unfinished' to control - * @return an array of all the user's attempts at this quiz. Returns an empty array if there are none. - */ -function quiz_get_user_attempts($quizid, $userid, $status = 'finished', $includepreviews = false) { - $status_condition = array( - 'all' => '', - 'finished' => ' AND timefinish > 0', - 'unfinished' => ' AND timefinish = 0' - ); - $previewclause = ''; - if (!$includepreviews) { - $previewclause = ' AND preview = 0'; - } - if ($attempts = get_records_select('quiz_attempts', - "quiz = '$quizid' AND userid = '$userid'" . $previewclause . $status_condition[$status], - 'attempt ASC')) { - return $attempts; - } else { - return array(); - } -} - /** * Delete a quiz attempt. */ -- 2.39.5