]> git.mjollnir.org Git - moodle.git/commitdiff
First commit of the requested quiz_results block.
authordefacer <defacer>
Tue, 25 Jan 2005 23:49:36 +0000 (23:49 +0000)
committerdefacer <defacer>
Tue, 25 Jan 2005 23:49:36 +0000 (23:49 +0000)
blocks/quiz_results/block_quiz_results.php [new file with mode: 0644]
blocks/quiz_results/config_instance.html [new file with mode: 0644]

diff --git a/blocks/quiz_results/block_quiz_results.php b/blocks/quiz_results/block_quiz_results.php
new file mode 100644 (file)
index 0000000..b4081ad
--- /dev/null
@@ -0,0 +1,159 @@
+<?php //$Id$
+
+define('GRADE_FORMAT_PCT', 1);
+define('GRADE_FORMAT_FRA', 2);
+define('GRADE_FORMAT_ABS', 3);
+
+class block_quiz_results extends block_base {
+    function init() {
+        $this->title = get_string('formaltitle', 'block_quiz_results');
+        $this->content_type = BLOCK_TYPE_TEXT;
+        $this->version = 2005012500;
+    }
+
+    function get_content() {
+        global $USER, $CFG;
+
+        if ($this->content !== NULL) {
+            return $this->content;
+        }
+        if (empty($this->instance)) {
+            $this->content = '';
+            return $this->content;
+        }
+
+        $this->content = new stdClass;
+        $this->content->text = '';
+        $this->content->footer = '';
+
+        if($this->instance->pagetype == MOODLE_PAGE_COURSE) {
+            // We need to see if we are monitoring a quiz 
+            $quizid   = empty($this->config->quizid) ? 0 : $this->config->quizid;
+            $courseid = $this->instance->pageid;
+        }
+        else {
+            // Assuming we are displayed in the quiz view page
+            // TODO
+            $quizid   = 0;
+            $courseid = 0;
+        }
+
+        if(empty($quizid)) {
+            return $this->content;
+        }
+
+        // Get the quiz record
+        $quiz = get_record('quiz', 'id', $quizid);
+        if(empty($quiz)) {
+            return $this->content;
+        }
+
+        // Get the grades for this quiz
+        $grades = get_records('quiz_grades', 'quiz', $quizid, 'grade, timemodified DESC');
+
+        if(empty($grades)) {
+            // No grades, sorry
+            // TODO
+        }
+
+        $numbest  = empty($this->config->showbest) ? 0 : min($this->config->showbest, count($grades));
+        $numworst = empty($this->config->showworst) ? 0 : min($this->config->showworst, count($grades) - $numbest);
+        $best  = array();
+        $worst = array();
+
+        if(!empty($this->config->usegroups)) {
+            // Group mode activated, what about it?
+            // TODO
+        }
+        else {
+            // Single user mode
+
+            // Collect all the usernames we are going to need
+            $remaining = $numbest;
+            $grade = end($grades);
+            while($remaining--) {
+                $best[$grade->userid] = $grade->id;
+                $grade = prev($grades);
+            }
+
+            $remaining = $numworst;
+            $grade = reset($grades);
+            while($remaining--) {
+                $worst[$grade->userid] = $grade->id;
+                $grade = next($grades);
+            }
+
+            if(empty($best) && empty($worst)) {
+                // Nothing to show, for some reason...
+                return $this->content;
+            }
+
+            // Now grab all the users from the database
+            $userids = array_merge(array_keys($best), array_keys($worst));
+            $users = get_records_list('user', 'id', implode(',',$userids), '', 'id, firstname, lastname');
+
+            // Ready for output!
+
+            $gradeformat = intval(empty($this->config->gradeformat) ? GRADE_FORMAT_PCT : $this->config->gradeformat);
+
+            $this->content->text .= '<h1><a href="'.$CFG->wwwroot.'/mod/quiz/view.php?q='.$quizid.'">'.$quiz->name.'</a></h1>';
+
+            $rank = 0;
+            if(!empty($best)) {
+                $this->content->text .= '<h2>'.get_string('bestgrades', 'block_quiz_results', $numbest).'</h2>';
+                $this->content->text .= '<table class="grades"><tbody>';
+                foreach($best as $userid => $gradeid) {
+                    $this->content->text .= '<tr><td width="10%">'.(++$rank).'.</td><td><a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'&amp;course='.$courseid.'">'.fullname($users[$userid]).'</a></td><td width="10%">';
+                    switch($gradeformat) {
+                        case GRADE_FORMAT_FRA:
+                            $this->content->text .= ($grades[$gradeid]->grade.'/'.$quiz->grade);
+                        break;
+                        case GRADE_FORMAT_ABS:
+                            $this->content->text .= $grades[$gradeid]->grade;
+                        break;
+                        default:
+                        case GRADE_FORMAT_PCT:
+                            $this->content->text .= round(intval($grades[$gradeid]->grade) / intval($quiz->grade) * 100).'%';
+                        break;
+                    }
+                    $this->content->text .= '</td></tr>';
+                }
+                $this->content->text .= '</tbody></table>';
+            }
+
+            $rank = 0;
+            if(!empty($worst)) {
+                $worst = array_reverse($worst, true);
+                $this->content->text .= '<h2>'.get_string('worstgrades', 'block_quiz_results', $numworst).'</h2>';
+                $this->content->text .= '<table class="grades"><tbody>';
+                foreach($worst as $userid => $gradeid) {
+                    $this->content->text .= '<tr><td width="10%">'.(++$rank).'.</td><td><a href="'.$CFG->wwwroot.'/user/view.php?id='.$userid.'&amp;course='.$courseid.'">'.fullname($users[$userid]).'</a></td><td width="10%">';
+                    switch($gradeformat) {
+                        case GRADE_FORMAT_FRA:
+                            $this->content->text .= ($grades[$gradeid]->grade.'/'.$quiz->grade);
+                        break;
+                        case GRADE_FORMAT_ABS:
+                            $this->content->text .= $grades[$gradeid]->grade;
+                        break;
+                        default:
+                        case GRADE_FORMAT_PCT:
+                            $this->content->text .= round(intval($grades[$gradeid]->grade) / intval($quiz->grade) * 100).'%';
+                        break;
+                    }
+                    $this->content->text .= '</td></tr>';
+                }
+                $this->content->text .= '</tbody></table>';
+            }
+
+        }
+
+
+        return $this->content;
+    }
+
+    function instance_allow_config() {
+        return true;
+    }
+}
+
+?>
diff --git a/blocks/quiz_results/config_instance.html b/blocks/quiz_results/config_instance.html
new file mode 100644 (file)
index 0000000..95aedb7
--- /dev/null
@@ -0,0 +1,70 @@
+<table cellpadding="9" cellspacing="0">
+<?php if($this->instance->pagetype == MOODLE_PAGE_COURSE) { ?>
+<tr valign="top">
+    <td align="right">
+        <?php print_string('config_select_quiz', 'block_quiz_results') ?>
+    </td>
+    <td>
+        <?php
+            $quizzes = get_records('quiz', 'course', $this->instance->pageid, '', 'id, name');
+            if(empty($quizzes)) {
+                print_string('config_no_quizzes_in_course', 'block_quiz_results');
+                echo '<p><input type="hidden" name="quizid" value="0" /></p>';
+            }
+            else {
+                $options = array();
+                foreach($quizzes as $quiz) {
+                    $options[$quiz->id] = $quiz->name;
+                }
+                choose_from_menu($options, 'quizid', empty($this->config->quizid) ? 0 : $this->config->quizid);
+            }
+        ?>
+    </td>
+</tr>
+<?php } // end if($this->instance->pagetype == MOODLE_PAGE_COURSE) ?>
+<tr valign="top">
+    <td align="right">
+        <?php print_string('config_show_best', 'block_quiz_results') ?>
+    </td>
+    <td>
+        <input name="showbest" type="text" size="3" value="<?php
+            p(empty($this->config->showbest) ? 0 : max(0, intval($this->config->showbest)));
+        ?>" />
+    </td>
+</tr>
+<tr valign="top">
+    <td align="right">
+        <?php print_string('config_show_worst', 'block_quiz_results') ?>
+    </td>
+    <td>
+        <input name="showworst" type="text" size="3" value="<?php
+            p(empty($this->config->showworst) ? 0 : max(0, intval($this->config->showworst)));
+        ?>" />
+    </td>
+</tr>
+<tr valign="top">
+    <td align="right">
+        <?php print_string('config_use_groups', 'block_quiz_results') ?>
+    </td>
+    <td>
+        <input name="usegroups" type="radio" id="usegroups_yes" <?php if(!empty($this->config->usegroups)) echo 'checked="checked"'; ?> value="1" /> <label for="usegroups_yes"><?php print_string('yes'); ?></label>
+        <input name="usegroups" type="radio" id="usegroups_no"  <?php if(empty($this->config->usegroups)) echo 'checked="checked"'; ?> value="0" /> <label for="usegroups_no"><?php print_string('no'); ?></label>
+    </td>
+</tr>
+<tr valign="top">
+    <td align="right">
+        <?php print_string('config_grade_format', 'block_quiz_results') ?>
+    </td>
+    <td>
+    <?php
+        $formats = array(GRADE_FORMAT_PCT => get_string('config_format_percentage', 'block_quiz_results'), GRADE_FORMAT_FRA => get_string('config_format_fraction', 'block_quiz_results'), GRADE_FORMAT_ABS => get_string('config_format_absolute', 'block_quiz_results'));
+        choose_from_menu($formats, 'gradeformat', empty($this->config->gradeformat) ? 0 : $this->config->gradeformat, '');
+    ?>
+    </td>
+</tr>
+<tr>
+    <td colspan="2" align="center">
+        <input type="submit" value="<?php print_string('savechanges') ?>">
+    </td>
+</tr>
+</table>
\ No newline at end of file