3 define('B_QUIZRESULTS_NAME_FORMAT_FULL', 1);
4 define('B_QUIZRESULTS_NAME_FORMAT_ID', 2);
5 define('B_QUIZRESULTS_NAME_FORMAT_ANON', 3);
6 define('B_QUIZRESULTS_GRADE_FORMAT_PCT', 1);
7 define('B_QUIZRESULTS_GRADE_FORMAT_FRA', 2);
8 define('B_QUIZRESULTS_GRADE_FORMAT_ABS', 3);
10 class block_quiz_results
extends block_base
{
12 $this->title
= get_string('formaltitle', 'block_quiz_results');
13 $this->version
= 2005082300;
16 function applicable_formats() {
17 return array('course' => true, 'mod-quiz' => true);
20 function get_content() {
23 if ($this->content
!== NULL) {
24 return $this->content
;
27 $this->content
= new stdClass
;
28 $this->content
->text
= '';
29 $this->content
->footer
= '';
31 if (empty($this->instance
)) {
32 return $this->content
;
35 if($this->instance
->pagetype
== 'course-view') {
36 // We need to see if we are monitoring a quiz
37 $quizid = empty($this->config
->quizid
) ?
0 : $this->config
->quizid
;
38 $courseid = $this->instance
->pageid
;
41 // Assuming we are displayed in the quiz view page
42 $quizid = $this->instance
->pageid
;
44 // A trick to take advantage of instance config and save queries
45 if(empty($this->config
->courseid
)) {
46 $modrecord = get_record('modules', 'name', 'quiz');
47 $cmrecord = get_record('course_modules', 'module', $modrecord->id
, 'instance', $quizid);
48 $this->config
->courseid
= intval($cmrecord->course
);
49 $this->instance_config_commit();
51 $courseid = $this->config
->courseid
;
54 $context = get_context_instance(CONTEXT_COURSE
, $courseid);
58 $this->content
->text
= get_string('error_emptyquizid', 'block_quiz_results');
59 return $this->content
;
62 // Get the quiz record
63 $quiz = get_record('quiz', 'id', $quizid);
65 $this->content
->text
= get_string('error_emptyquizrecord', 'block_quiz_results');
66 return $this->content
;
69 // Get the grades for this quiz
70 $grades = get_records('quiz_grades', 'quiz', $quizid, 'grade, timemodified DESC');
74 // The block will hide itself in this case
75 return $this->content
;
78 if(empty($this->config
->showbest
) && empty($this->config
->showworst
)) {
79 $this->content
->text
= get_string('configuredtoshownothing', 'block_quiz_results');
80 return $this->content
;
83 $groupmode = NOGROUPS
;
87 $nameformat = intval(empty($this->config
->nameformat
) ? B_QUIZRESULTS_NAME_FORMAT_FULL
: $this->config
->nameformat
);
89 // If the block is configured to operate in group mode, or if the name display format
90 // is other than "fullname", then we need to retrieve the full course record
91 if(!empty($this->config
->usegroups
) ||
$nameformat != B_QUIZRESULTS_NAME_FORMAT_FULL
) {
92 $course = get_record_select('course', 'id = '.$courseid, 'groupmode, groupmodeforce, student');
95 if(!empty($this->config
->usegroups
)) {
96 // The block was configured to operate in group mode
97 if($course->groupmodeforce
) {
98 $groupmode = $course->groupmode
;
101 $module = get_record_sql('SELECT cm.groupmode FROM '.$CFG->prefix
.'modules m LEFT JOIN '.$CFG->prefix
.'course_modules cm ON m.id = cm.module WHERE m.name = \'quiz\' AND cm.instance = '.$quizid);
102 $groupmode = $module->groupmode
;
104 // The actual groupmode for the quiz is now known to be $groupmode
107 if(has_capability('moodle/site:accessallgroups', $context) && $groupmode == SEPARATEGROUPS
) {
108 // We 'll make an exception in this case
109 $groupmode = VISIBLEGROUPS
;
114 // Display group-mode results
115 $groups = get_groups($courseid);
118 // No groups exist, sorry
119 $this->content
->text
= get_string('error_nogroupsexist', 'block_quiz_results');
120 return $this->content
;
123 // Find out all the userids which have a submitted grade
125 foreach($grades as $grade) {
126 $userids[] = $grade->userid
;
129 // Now find which groups these users belong in
130 $groupofuser = groups_get_groups_users($userids, $courseid); /*TODO: get_records_sql(
131 'SELECT m.userid, m.groupid, g.name FROM '.$CFG->prefix.'groups g LEFT JOIN '.$CFG->prefix.'groups_members m ON g.id = m.groupid '.
132 'WHERE g.courseid = '.$courseid.' AND m.userid IN ('.implode(',', $userids).')'
135 $groupgrades = array();
137 // OK... now, iterate the grades again and sum them up for each group
138 foreach($grades as $grade) {
139 if(isset($groupofuser[$grade->userid
])) {
140 // Count this result only if the user is in a group
141 $groupid = $groupofuser[$grade->userid
]->groupid
;
142 if(!isset($groupgrades[$groupid])) {
143 $groupgrades[$groupid] = array('sum' => (float)$grade->grade
, 'number' => 1, 'group' => $groupofuser[$grade->userid
]->name
);
146 $groupgrades[$groupid]['sum'] +
= $grade->grade
;
147 ++
$groupgrades[$groupid]['number'];
152 foreach($groupgrades as $groupid => $groupgrade) {
153 $groupgrades[$groupid]['average'] = $groupgrades[$groupid]['sum'] / $groupgrades[$groupid]['number'];
156 // Sort groupgrades according to average grade, ascending
157 uasort($groupgrades, create_function('$a, $b', 'if($a["average"] == $b["average"]) return 0; return ($a["average"] > $b["average"] ? 1 : -1);'));
159 // How many groups do we have with graded member submissions to show?
160 $numbest = empty($this->config
->showbest
) ?
0 : min($this->config
->showbest
, count($groupgrades));
161 $numworst = empty($this->config
->showworst
) ?
0 : min($this->config
->showworst
, count($groupgrades) - $numbest);
163 // Collect all the group results we are going to use in $best and $worst
164 $remaining = $numbest;
165 $groupgrade = end($groupgrades);
166 while($remaining--) {
167 $best[key($groupgrades)] = $groupgrade['average'];
168 $groupgrade = prev($groupgrades);
171 $remaining = $numworst;
172 $groupgrade = reset($groupgrades);
173 while($remaining--) {
174 $worst[key($groupgrades)] = $groupgrade['average'];
175 $groupgrade = next($groupgrades);
179 $gradeformat = intval(empty($this->config
->gradeformat
) ? B_QUIZRESULTS_GRADE_FORMAT_PCT
: $this->config
->gradeformat
);
181 if($this->instance
->pagetype
!= 'mod-quiz-view') {
182 // Don't show header and link to the quiz if we ARE at the quiz...
183 $this->content
->text
.= '<h1><a href="'.$CFG->wwwroot
.'/mod/quiz/view.php?q='.$quizid.'">'.$quiz->name
.'</a></h1>';
188 $this->content
->text
.= '<table class="grades"><caption>';
189 $this->content
->text
.= ($numbest == 1?
get_string('bestgroupgrade', 'block_quiz_results'):get_string('bestgroupgrades', 'block_quiz_results', $numbest));
190 $this->content
->text
.= '</caption><colgroup class="number" /><colgroup class="name" /><colgroup class="grade" /><tbody>';
191 foreach($best as $groupid => $averagegrade) {
192 switch($nameformat) {
193 case B_QUIZRESULTS_NAME_FORMAT_ANON
:
194 case B_QUIZRESULTS_NAME_FORMAT_ID
:
195 $thisname = get_string('group');
198 case B_QUIZRESULTS_NAME_FORMAT_FULL
:
199 $thisname = '<a href="'.$CFG->wwwroot
.'/course/group.php?group='.$groupid.'&id='.$courseid.'">'.$groupgrades[$groupid]['group'].'</a>';
202 $this->content
->text
.= '<tr><td>'.(++
$rank).'.</td><td>'.$thisname.'</td><td>';
203 switch($gradeformat) {
204 case B_QUIZRESULTS_GRADE_FORMAT_FRA
:
205 $this->content
->text
.= (format_float($averagegrade,$quiz->decimalpoints
).'/'.$quiz->grade
);
207 case B_QUIZRESULTS_GRADE_FORMAT_ABS
:
208 $this->content
->text
.= format_float($averagegrade,$quiz->decimalpoints
);
211 case B_QUIZRESULTS_GRADE_FORMAT_PCT
:
212 $this->content
->text
.= round((float)$averagegrade / (float)$quiz->grade
* 100).'%';
215 $this->content
->text
.= '</td></tr>';
217 $this->content
->text
.= '</tbody></table>';
222 $worst = array_reverse($worst, true);
223 $this->content
->text
.= '<table class="grades"><caption>';
224 $this->content
->text
.= ($numworst == 1?
get_string('worstgroupgrade', 'block_quiz_results'):get_string('worstgroupgrades', 'block_quiz_results', $numworst));
225 $this->content
->text
.= '</caption><colgroup class="number" /><colgroup class="name" /><colgroup class="grade" /><tbody>';
226 foreach($worst as $groupid => $averagegrade) {
227 switch($nameformat) {
228 case B_QUIZRESULTS_NAME_FORMAT_ANON
:
229 case B_QUIZRESULTS_NAME_FORMAT_ID
:
230 $thisname = get_string('group');
233 case B_QUIZRESULTS_NAME_FORMAT_FULL
:
234 $thisname = '<a href="'.$CFG->wwwroot
.'/course/group.php?group='.$groupid.'&id='.$courseid.'">'.$groupgrades[$groupid]['group'].'</a>';
237 $this->content
->text
.= '<tr><td>'.(++
$rank).'.</td><td>'.$thisname.'</td><td>';
238 switch($gradeformat) {
239 case B_QUIZRESULTS_GRADE_FORMAT_FRA
:
240 $this->content
->text
.= (format_float($averagegrade,$quiz->decimalpoints
).'/'.$quiz->grade
);
242 case B_QUIZRESULTS_GRADE_FORMAT_ABS
:
243 $this->content
->text
.= format_float($averagegrade,$quiz->decimalpoints
);
246 case B_QUIZRESULTS_GRADE_FORMAT_PCT
:
247 $this->content
->text
.= round((float)$averagegrade / (float)$quiz->grade
* 100).'%';
250 $this->content
->text
.= '</td></tr>';
252 $this->content
->text
.= '</tbody></table>';
258 // This is going to be just like no-groups mode, only we 'll filter
259 // out the grades from people not in our group.
260 if(empty($USER) ||
empty($USER->id
)) {
261 // Not logged in, so show nothing
262 return $this->content
;
265 $mygroups = get_groups($courseid, $USER->id
);
266 if(empty($mygroups)) {
267 // Not member of a group, show nothing
268 return $this->content
;
271 $mygroupsusers = get_records_list('groups_members', 'groupid', implode(',', array_keys($mygroups)), '', 'userid, id');
272 // There should be at least one user there, ourselves. So no more tests.
274 // Just filter out the grades belonging to other users, and proceed as if there were no groups
275 $strallowedusers = implode(',', array_keys($mygroupsusers));
276 $grades = array_filter($grades, create_function('$el', '$allowed = explode(",", "'.$strallowedusers.'"); return in_array($el->userid, $allowed);'));
278 // NO break; HERE, JUST GO AHEAD
282 $numbest = empty($this->config
->showbest
) ?
0 : min($this->config
->showbest
, count($grades));
283 $numworst = empty($this->config
->showworst
) ?
0 : min($this->config
->showworst
, count($grades) - $numbest);
285 // Collect all the usernames we are going to need
286 $remaining = $numbest;
287 $grade = end($grades);
288 while($remaining--) {
289 $best[$grade->userid
] = $grade->id
;
290 $grade = prev($grades);
293 $remaining = $numworst;
294 $grade = reset($grades);
295 while($remaining--) {
296 $worst[$grade->userid
] = $grade->id
;
297 $grade = next($grades);
300 if(empty($best) && empty($worst)) {
301 // Nothing to show, for some reason...
302 return $this->content
;
305 // Now grab all the users from the database
306 $userids = array_merge(array_keys($best), array_keys($worst));
307 $users = get_records_list('user', 'id', implode(',',$userids), '', 'id, firstname, lastname, idnumber');
311 $gradeformat = intval(empty($this->config
->gradeformat
) ? B_QUIZRESULTS_GRADE_FORMAT_PCT
: $this->config
->gradeformat
);
313 if($this->instance
->pagetype
!= 'mod-quiz-view') {
314 // Don't show header and link to the quiz if we ARE at the quiz...
315 $this->content
->text
.= '<h1><a href="'.$CFG->wwwroot
.'/mod/quiz/view.php?q='.$quizid.'">'.$quiz->name
.'</a></h1>';
320 $this->content
->text
.= '<table class="grades"><caption>';
321 $this->content
->text
.= ($numbest == 1?
get_string('bestgrade', 'block_quiz_results'):get_string('bestgrades', 'block_quiz_results', $numbest));
322 $this->content
->text
.= '</caption><colgroup class="number" /><colgroup class="name" /><colgroup class="grade" /><tbody>';
323 foreach($best as $userid => $gradeid) {
324 switch($nameformat) {
325 case B_QUIZRESULTS_NAME_FORMAT_ID
:
326 $thisname = $course->student
.' '.intval($users[$userid]->idnumber
);
328 case B_QUIZRESULTS_NAME_FORMAT_ANON
:
329 $thisname = $course->student
;
332 case B_QUIZRESULTS_NAME_FORMAT_FULL
:
333 $thisname = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$userid.'&course='.$courseid.'">'.fullname($users[$userid]).'</a>';
336 $this->content
->text
.= '<tr><td>'.(++
$rank).'.</td><td>'.$thisname.'</td><td>';
337 switch($gradeformat) {
338 case B_QUIZRESULTS_GRADE_FORMAT_FRA
:
339 $this->content
->text
.= (format_float($grades[$gradeid]->grade
,$quiz->decimalpoints
).'/'.$quiz->grade
);
341 case B_QUIZRESULTS_GRADE_FORMAT_ABS
:
342 $this->content
->text
.= format_float($grades[$gradeid]->grade
,$quiz->decimalpoints
);
345 case B_QUIZRESULTS_GRADE_FORMAT_PCT
:
347 $this->content
->text
.= round((float)$grades[$gradeid]->grade
/ (float)$quiz->grade
* 100).'%';
349 $this->content
->text
.= '--%';
353 $this->content
->text
.= '</td></tr>';
355 $this->content
->text
.= '</tbody></table>';
360 $worst = array_reverse($worst, true);
361 $this->content
->text
.= '<table class="grades"><caption>';
362 $this->content
->text
.= ($numworst == 1?
get_string('worstgrade', 'block_quiz_results'):get_string('worstgrades', 'block_quiz_results', $numworst));
363 $this->content
->text
.= '</caption><colgroup class="number" /><colgroup class="name" /><colgroup class="grade" /><tbody>';
364 foreach($worst as $userid => $gradeid) {
365 switch($nameformat) {
366 case B_QUIZRESULTS_NAME_FORMAT_ID
:
367 $thisname = $course->student
.' '.intval($users[$userid]->idnumber
);
369 case B_QUIZRESULTS_NAME_FORMAT_ANON
:
370 $thisname = $course->student
;
373 case B_QUIZRESULTS_NAME_FORMAT_FULL
:
374 $thisname = '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$userid.'&course='.$courseid.'">'.fullname($users[$userid]).'</a>';
377 $this->content
->text
.= '<tr><td>'.(++
$rank).'.</td><td>'.$thisname.'</td><td>';
378 switch($gradeformat) {
379 case B_QUIZRESULTS_GRADE_FORMAT_FRA
:
380 $this->content
->text
.= (format_float($grades[$gradeid]->grade
,$quiz->decimalpoints
).'/'.$quiz->grade
);
382 case B_QUIZRESULTS_GRADE_FORMAT_ABS
:
383 $this->content
->text
.= format_float($grades[$gradeid]->grade
,$quiz->decimalpoints
);
386 case B_QUIZRESULTS_GRADE_FORMAT_PCT
:
387 $this->content
->text
.= round((float)$grades[$gradeid]->grade
/ (float)$quiz->grade
* 100).'%';
390 $this->content
->text
.= '</td></tr>';
392 $this->content
->text
.= '</tbody></table>';
397 return $this->content
;
400 function instance_allow_multiple() {