]> git.mjollnir.org Git - moodle.git/commitdiff
blocks: MDL-19889 Convert quiz results block to use edit_form.php
authortjhunt <tjhunt>
Wed, 29 Jul 2009 03:55:12 +0000 (03:55 +0000)
committertjhunt <tjhunt>
Wed, 29 Jul 2009 03:55:12 +0000 (03:55 +0000)
blocks/quiz_results/block_quiz_results.php
blocks/quiz_results/config_instance.html [deleted file]
blocks/quiz_results/edit_form.php [new file with mode: 0644]

index d7050667e6193ca2de5cd64304f974cf430cb114..af2e0b45672e530be97d9189f467ba5a1413227f 100644 (file)
@@ -17,6 +17,33 @@ class block_quiz_results extends block_base {
         return array('course' => true, 'mod-quiz' => true);
     }
 
+    /**
+     * If this block belongs to a quiz context, then return that quiz's id.
+     * Otherwise, return 0.
+     * @return integer the quiz id.
+     */
+    public function get_owning_quiz() {
+        if (empty($this->instance->parentcontextid)) {
+            return 0;
+        }
+        $parentcontext = get_context_instance_by_id($this->instance->parentcontextid);
+        if ($parentcontext->contextlevel != CONTEXT_MODULE) {
+            return 0;
+        }
+        $cm = get_coursemodule_from_id('quiz', $parentcontext->instanceid);
+        if (!$cm) {
+            return 0;
+        }
+        return $cm->instance;
+    }
+
+    function instance_config_save($data) {
+        if (empty($data->quizid)) {
+            $data->quizid = $this->get_owning_quiz();
+        }
+        parent::instance_config_save($data);
+    }
+
     function get_content() {
         global $USER, $CFG, $DB;
 
@@ -32,7 +59,7 @@ class block_quiz_results extends block_base {
             return $this->content;
         }
 
-        if ($this->page->activityname == 'quiz') {
+        if ($this->page->activityname == 'quiz' && $this->page->context->id == $this->instance->parentcontextid) {
             $quiz = $this->page->activityrecord;
             $quizid = $quiz->id;
             $courseid = $this->page->course->id;
diff --git a/blocks/quiz_results/config_instance.html b/blocks/quiz_results/config_instance.html
deleted file mode 100644 (file)
index cd9ffa9..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-<table cellpadding="9" cellspacing="0">
-<?php if ($this->page->activityname != 'quiz') { ?>
-<tr valign="top">
-    <td align="right">
-        <?php print_string('config_select_quiz', 'block_quiz_results') ?>
-    </td>
-    <td>
-        <?php
-            $quizzes = $DB->get_records('quiz', array('course' => $this->page->course->id), '', 'id, name');
-            if(empty($quizzes)) {
-                echo '<strong>'.get_string('config_no_quizzes_in_course', 'block_quiz_results').'</strong>';
-                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 in quiz ?>
-<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_name_format', 'block_quiz_results') ?>
-    </td>
-    <td>
-    <?php
-        $usenames = array(B_QUIZRESULTS_NAME_FORMAT_FULL => get_string('config_names_full', 'block_quiz_results'), B_QUIZRESULTS_NAME_FORMAT_ID => get_string('config_names_id', 'block_quiz_results'), B_QUIZRESULTS_NAME_FORMAT_ANON => get_string('config_names_anon', 'block_quiz_results'));
-        choose_from_menu($usenames, 'nameformat', empty($this->config->nameformat) ? B_QUIZRESULTS_NAME_FORMAT_FULL : $this->config->nameformat, '');
-    ?>
-    </td>
-</tr>
-<tr valign="top">
-    <td align="right">
-        <?php print_string('config_grade_format', 'block_quiz_results') ?>
-    </td>
-    <td>
-    <?php
-        $formats = array(B_QUIZRESULTS_GRADE_FORMAT_PCT => get_string('config_format_percentage', 'block_quiz_results'), B_QUIZRESULTS_GRADE_FORMAT_FRA => get_string('config_format_fraction', 'block_quiz_results'), B_QUIZRESULTS_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
diff --git a/blocks/quiz_results/edit_form.php b/blocks/quiz_results/edit_form.php
new file mode 100644 (file)
index 0000000..1bfbfa6
--- /dev/null
@@ -0,0 +1,78 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/ 
+// 
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Form for editing Quiz results block instances.
+ *
+ * @package   moodlecore
+ * @copyright 2009 Tim Hunt
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Form for editing Quiz results block instances.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class block_quiz_results_edit_form extends block_edit_form {
+    protected function specific_definition($mform) {
+        global $DB;
+
+        // Fields for editing HTML block title and contents.
+        $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
+
+        if (!$this->block->get_owning_quiz()) {
+            $quizzes = $DB->get_records_menu('quiz', array('course' => $this->page->course->id), '', 'id, name');
+            if(empty($quizzes)) {
+                $mform->addElement('static', 'noquizzeswarning', get_string('config_select_quiz', 'block_quiz_results'),
+                        get_string('config_no_quizzes_in_course', 'block_quiz_results'));
+            } else {
+                foreach($quizzes as $id => $name) {
+                    $quizzes[$id] = strip_tags(format_string($name));
+                }
+                $mform->addElement('select', 'config_quizid', get_string('config_select_quiz', 'block_quiz_results'), $quizzes);
+            }
+        }
+
+        $mform->addElement('text', 'config_showbest', get_string('config_show_best', 'block_quiz_results'), array('size' => 3));
+        $mform->setDefault('config_showbest', 3);
+        $mform->setType('config_showbest', PARAM_INTEGER);
+
+        $mform->addElement('text', 'config_showworst', get_string('config_show_worst', 'block_quiz_results'), array('size' => 3));
+        $mform->setDefault('config_showworst', 0);
+        $mform->setType('config_showworst', PARAM_INTEGER);
+
+        $mform->addElement('selectyesno', 'config_usegroups', get_string('config_use_groups', 'block_quiz_results'));
+
+        $nameoptions = array(
+            B_QUIZRESULTS_NAME_FORMAT_FULL => get_string('config_names_full', 'block_quiz_results'),
+            B_QUIZRESULTS_NAME_FORMAT_ID => get_string('config_names_id', 'block_quiz_results'),
+            B_QUIZRESULTS_NAME_FORMAT_ANON => get_string('config_names_anon', 'block_quiz_results')
+        );
+        $mform->addElement('select', 'config_nameformat', get_string('config_name_format', 'block_quiz_results'), $nameoptions);
+        $mform->setDefault('config_nameformat', B_QUIZRESULTS_NAME_FORMAT_FULL);
+
+        $gradeeoptions = array(
+            B_QUIZRESULTS_GRADE_FORMAT_PCT => get_string('config_format_percentage', 'block_quiz_results'),
+            B_QUIZRESULTS_GRADE_FORMAT_FRA => get_string('config_format_fraction', 'block_quiz_results'),
+            B_QUIZRESULTS_GRADE_FORMAT_ABS => get_string('config_format_absolute', 'block_quiz_results')
+        );
+        $mform->addElement('select', 'config_gradeformat', get_string('config_grade_format', 'block_quiz_results'), $gradeeoptions);
+        $mform->setDefault('config_gradeformat', B_QUIZRESULTS_GRADE_FORMAT_PCT);
+    }
+}
\ No newline at end of file