MDL-14838: Corrected javadoc package names in wiki
[moodle.git] / mod / quiz / report / reportlib.php
CommitLineData
f33c438e 1<?php
2define('QUIZ_REPORT_DEFAULT_PAGE_SIZE', 30);
4469159e 3
4define('QUIZ_REPORT_ATTEMPTS_ALL', 0);
5define('QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO', 1);
6define('QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH', 2);
7define('QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS', 3);
2badf2e6 8/**
9 * Get newest graded state or newest state for a number of attempts. Pass in the
10 * uniqueid field from quiz_attempt table not the id. Use question_state_is_graded
11 * function to check that the question is actually graded.
12 */
13function quiz_get_newgraded_states($attemptids, $idxattemptq = true){
14 global $CFG;
0217f932 15 if ($attemptids){
16 $attemptidlist = join($attemptids, ',');
17 $gradedstatesql = "SELECT qs.* FROM " .
18 "{$CFG->prefix}question_sessions qns, " .
19 "{$CFG->prefix}question_states qs " .
20 "WHERE qns.attemptid IN ($attemptidlist) AND " .
21 "qns.newgraded = qs.id";
22 $gradedstates = get_records_sql($gradedstatesql);
23 if ($idxattemptq){
24 $gradedstatesbyattempt = array();
25 foreach ($gradedstates as $gradedstate){
26 if (!isset($gradedstatesbyattempt[$gradedstate->attempt])){
27 $gradedstatesbyattempt[$gradedstate->attempt] = array();
28 }
29 $gradedstatesbyattempt[$gradedstate->attempt][$gradedstate->question] = $gradedstate;
2badf2e6 30 }
0217f932 31 return $gradedstatesbyattempt;
32 } else {
33 return $gradedstates;
2badf2e6 34 }
2badf2e6 35 } else {
0217f932 36 return array();
2badf2e6 37 }
38}
39/**
40 * Load the question data necessary in the reports.
41 * - Remove description questions.
42 * - Order questions in order that they are in the quiz
43 * - Add question numbers.
44 * - Add grade from quiz_questions_instance
45 */
46function quiz_report_load_questions($quiz){
47 global $CFG;
48 $questionlist = quiz_questions_in_quiz($quiz->questions);
49 //In fact in most cases the id IN $questionlist below is redundant
50 //since we are also doing a JOIN on the qqi table. But will leave it in
51 //since this double check will probably do no harm.
52 if (!$questions = get_records_sql("SELECT q.*, qqi.grade " .
53 "FROM {$CFG->prefix}question q, " .
54 "{$CFG->prefix}quiz_question_instances qqi " .
55 "WHERE q.id IN ($questionlist) AND " .
56 "qqi.question = q.id AND " .
57 "qqi.quiz =".$quiz->id)) {
58 print_error('No questions found');
59 }
60 //Now we have an array of questions from a quiz we work out there question nos and remove
61 //questions with zero length ie. description questions etc.
62 //also put questions in order.
63 $number = 1;
64 $realquestions = array();
65 $questionids = explode(',', $questionlist);
66 foreach ($questionids as $id) {
67 if ($questions[$id]->length) {
68 // Ignore questions of zero length
69 $realquestions[$id] = $questions[$id];
70 $realquestions[$id]->number = $number;
71 $number += $questions[$id]->length;
72 }
73 }
74 return $realquestions;
75}
4469159e 76/**
77 * Given the quiz grading method return sub select sql to find the id of the
78 * one attempt that will be graded for each user. Or return
79 * empty string if all attempts contribute to final grade.
80 */
b621e1a0 81function quiz_report_qm_filter_subselect($quiz){
4469159e 82 global $CFG;
b621e1a0 83 if ($quiz->attempts == 1) {//only one attempt allowed on this quiz
84 return '';
85 }
4469159e 86 $qmfilterattempts = true;
b621e1a0 87 switch ($quiz->grademethod) {
4469159e 88 case QUIZ_GRADEHIGHEST :
89 $qmorderby = 'sumgrades DESC, timestart ASC';
90 break;
91 case QUIZ_GRADEAVERAGE :
92 $qmfilterattempts = false;
93 break;
94 case QUIZ_ATTEMPTFIRST :
95 $qmorderby = 'timestart ASC';
96 break;
97 case QUIZ_ATTEMPTLAST :
98 $qmorderby = 'timestart DESC';
99 break;
100 }
101 if ($qmfilterattempts){
7d4d5cb9 102 $qmsubselect = "(SELECT id FROM {$CFG->prefix}quiz_attempts " .
103 "WHERE quiz = {$quiz->id} AND u.id = userid " .
104 "ORDER BY $qmorderby LIMIT 1)=qa.id";
4469159e 105 } else {
106 $qmsubselect = '';
107 }
108 return $qmsubselect;
109}
8b87ab00 110
111function quiz_report_grade_bands($bands, $quizid, $useridlist){
112 $sql = "SELECT
113 FLOOR(qg.grade*$bands/q.grade) AS band,
114 COUNT(1) AS num
115 FROM
116 mdl_quiz_grades qg,
117 mdl_quiz q
c187720b 118 WHERE qg.quiz = q.id AND qg.quiz = $quizid AND qg.userid IN ($useridlist)
8b87ab00 119 GROUP BY band
120 ORDER BY band";
121 $data = get_records_sql_menu($sql);
122 //need to create array elements with values 0 at indexes where there is no element
123 $data = $data + array_fill(0, $bands, 0);
124 ksort($data);
125 return $data;
126}
b621e1a0 127function quiz_report_highlighting_grading_method($quiz, $qmsubselect, $qmfilter){
128 if ($quiz->attempts == 1) {//only one attempt allowed on this quiz
129 return "<p>".get_string('onlyoneattemptallowed', "quiz_overview")."</p>";
130 } else if (!$qmsubselect){
131 return "<p>".get_string('allattemptscontributetograde', "quiz_overview")."</p>";
132 } else if ($qmfilter){
133 return "<p>".get_string('showinggraded', "quiz_overview")."</p>";
134 }else {
135 return "<p>".get_string('showinggradedandungraded', "quiz_overview",
136 ('<span class="highlight">'.quiz_get_grading_option_name($quiz->grademethod).'</span>'))."</p>";
137 }
138}
7d4d5cb9 139?>