Skip to content

Commit

Permalink
blocks: MDL-19889 Convert quiz results block to use edit_form.php
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhunt committed Jul 29, 2009
1 parent 05d3c06 commit c6084d3
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 82 deletions.
29 changes: 28 additions & 1 deletion blocks/quiz_results/block_quiz_results.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ function applicable_formats() {
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;

Expand All @@ -32,7 +59,7 @@ function get_content() {
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;
Expand Down
81 changes: 0 additions & 81 deletions blocks/quiz_results/config_instance.html

This file was deleted.

78 changes: 78 additions & 0 deletions blocks/quiz_results/edit_form.php
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c6084d3

Please sign in to comment.