98ad7484 |
1 | <?php //$Id$ |
2 | |
3 | define('GRADE_FORMAT_PCT', 1); |
4 | define('GRADE_FORMAT_FRA', 2); |
5 | define('GRADE_FORMAT_ABS', 3); |
6 | |
7 | class block_quiz_results extends block_base { |
8 | function init() { |
9 | $this->title = get_string('formaltitle', 'block_quiz_results'); |
10 | $this->content_type = BLOCK_TYPE_TEXT; |
11 | $this->version = 2005012500; |
12 | } |
13 | |
14 | function get_content() { |
15 | global $USER, $CFG; |
16 | |
17 | if ($this->content !== NULL) { |
18 | return $this->content; |
19 | } |
20 | if (empty($this->instance)) { |
21 | $this->content = ''; |
22 | return $this->content; |
23 | } |
24 | |
25 | $this->content = new stdClass; |
26 | $this->content->text = ''; |
27 | $this->content->footer = ''; |
28 | |
29 | if($this->instance->pagetype == MOODLE_PAGE_COURSE) { |
30 | // We need to see if we are monitoring a quiz |
31 | $quizid = empty($this->config->quizid) ? 0 : $this->config->quizid; |
32 | $courseid = $this->instance->pageid; |
33 | } |
34 | else { |
35 | // Assuming we are displayed in the quiz view page |
36 | // TODO |
37 | $quizid = 0; |
38 | $courseid = 0; |
39 | } |
40 | |
41 | if(empty($quizid)) { |
42 | return $this->content; |
43 | } |
44 | |
45 | // Get the quiz record |
46 | $quiz = get_record('quiz', 'id', $quizid); |
47 | if(empty($quiz)) { |
48 | return $this->content; |
49 | } |
50 | |
51 | // Get the grades for this quiz |
41dafe04 |
52 | $grades = get_records('quiz_grades', 'quiz', $quizid, 'grade + 0, timemodified DESC'); |
98ad7484 |
53 | |
54 | if(empty($grades)) { |
55 | // No grades, sorry |
56 | // TODO |
57 | } |
58 | |
59 | $numbest = empty($this->config->showbest) ? 0 : min($this->config->showbest, count($grades)); |
60 | $numworst = empty($this->config->showworst) ? 0 : min($this->config->showworst, count($grades) - $numbest); |
61 | $best = array(); |
62 | $worst = array(); |
63 | |
64 | if(!empty($this->config->usegroups)) { |
65 | // Group mode activated, what about it? |
66 | // TODO |
67 | } |
68 | else { |
69 | // Single user mode |
70 | |
71 | // Collect all the usernames we are going to need |
72 | $remaining = $numbest; |
73 | $grade = end($grades); |
74 | while($remaining--) { |
75 | $best[$grade->userid] = $grade->id; |
76 | $grade = prev($grades); |
77 | } |
78 | |
79 | $remaining = $numworst; |
80 | $grade = reset($grades); |
81 | while($remaining--) { |
82 | $worst[$grade->userid] = $grade->id; |
83 | $grade = next($grades); |
84 | } |
85 | |
86 | if(empty($best) && empty($worst)) { |
87 | // Nothing to show, for some reason... |
88 | return $this->content; |
89 | } |
90 | |
91 | // Now grab all the users from the database |
92 | $userids = array_merge(array_keys($best), array_keys($worst)); |
93 | $users = get_records_list('user', 'id', implode(',',$userids), '', 'id, firstname, lastname'); |
94 | |
95 | // Ready for output! |
96 | |
97 | $gradeformat = intval(empty($this->config->gradeformat) ? GRADE_FORMAT_PCT : $this->config->gradeformat); |
98 | |
99 | $this->content->text .= '<h1><a href="'.$CFG->wwwroot.'/mod/quiz/view.php?q='.$quizid.'">'.$quiz->name.'</a></h1>'; |
100 | |
101 | $rank = 0; |
102 | if(!empty($best)) { |
103 | $this->content->text .= '<h2>'.get_string('bestgrades', 'block_quiz_results', $numbest).'</h2>'; |
104 | $this->content->text .= '<table class="grades"><tbody>'; |
105 | foreach($best as $userid => $gradeid) { |
106 | $this->content->text .= '<tr><td width="10%">'.(++$rank).'.</td><td><a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'&course='.$courseid.'">'.fullname($users[$userid]).'</a></td><td width="10%">'; |
107 | switch($gradeformat) { |
108 | case GRADE_FORMAT_FRA: |
109 | $this->content->text .= ($grades[$gradeid]->grade.'/'.$quiz->grade); |
110 | break; |
111 | case GRADE_FORMAT_ABS: |
112 | $this->content->text .= $grades[$gradeid]->grade; |
113 | break; |
114 | default: |
115 | case GRADE_FORMAT_PCT: |
116 | $this->content->text .= round(intval($grades[$gradeid]->grade) / intval($quiz->grade) * 100).'%'; |
117 | break; |
118 | } |
119 | $this->content->text .= '</td></tr>'; |
120 | } |
121 | $this->content->text .= '</tbody></table>'; |
122 | } |
123 | |
124 | $rank = 0; |
125 | if(!empty($worst)) { |
126 | $worst = array_reverse($worst, true); |
127 | $this->content->text .= '<h2>'.get_string('worstgrades', 'block_quiz_results', $numworst).'</h2>'; |
128 | $this->content->text .= '<table class="grades"><tbody>'; |
129 | foreach($worst as $userid => $gradeid) { |
130 | $this->content->text .= '<tr><td width="10%">'.(++$rank).'.</td><td><a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'&course='.$courseid.'">'.fullname($users[$userid]).'</a></td><td width="10%">'; |
131 | switch($gradeformat) { |
132 | case GRADE_FORMAT_FRA: |
133 | $this->content->text .= ($grades[$gradeid]->grade.'/'.$quiz->grade); |
134 | break; |
135 | case GRADE_FORMAT_ABS: |
136 | $this->content->text .= $grades[$gradeid]->grade; |
137 | break; |
138 | default: |
139 | case GRADE_FORMAT_PCT: |
140 | $this->content->text .= round(intval($grades[$gradeid]->grade) / intval($quiz->grade) * 100).'%'; |
141 | break; |
142 | } |
143 | $this->content->text .= '</td></tr>'; |
144 | } |
145 | $this->content->text .= '</tbody></table>'; |
146 | } |
147 | |
148 | } |
149 | |
150 | |
151 | return $this->content; |
152 | } |
153 | |
154 | function instance_allow_config() { |
155 | return true; |
156 | } |
157 | } |
158 | |
159 | ?> |