MDL-29479 Active grading method can be set for the given area

This commit is contained in:
David Mudrak 2011-10-02 23:31:05 +02:00
parent 8a4acb3aca
commit 6440286719
3 changed files with 119 additions and 4 deletions

View File

@ -590,6 +590,19 @@ if ($mform->is_cancelled()) {
}
}
if (plugin_supports('mod', $fromform->modulename, FEATURE_ADVANCED_GRADING, false)) {
require_once($CFG->dirroot.'/grade/grading/lib.php');
$context = get_context_instance(CONTEXT_MODULE, $fromform->coursemodule);
$gradingman = get_grading_manager($context, 'mod_'.$fromform->modulename);
foreach ($gradingman->get_available_areas() as $areaname => $aretitle) {
$formfield = 'advancedgradingmethod_'.$areaname;
if (isset($fromform->{$formfield})) {
$gradingman->set_area($areaname);
$gradingman->set_active_method($fromform->{$formfield});
}
}
}
rebuild_course_cache($course->id);
grade_regrade_final_grades($course->id);
plagiarism_save_form_elements($fromform); //save plagiarism settings

View File

@ -153,14 +153,66 @@ class grading_manager {
/**
* Returns the currently active grading method in the gradable area
*
* @return string the name of the grading plugin
* @return string|null the name of the grading plugin of null if it has not been set
*/
public function get_active_method() {
global $DB;
$this->ensure_isset(array('context', 'component', 'area'));
// todo - hardcoded value for now
return 'rubric';
// get the current grading area record if it exists
$area = $DB->get_record('grading_areas',
array('contextid' => $this->context->id, 'component' => $this->component, 'areaname' => $this->area), 'id,activemethod', IGNORE_MISSING);
if (empty($area)) {
// no area record yet
return null;
}
return $area->activemethod;
}
/**
* Sets the currently active grading method in the gradable area
*
* @param string $method the method name, eg 'rubric' (must be available)
*/
public function set_active_method($method) {
global $DB;
$this->ensure_isset(array('context', 'component', 'area'));
// make sure the passed method is a valid plugin name
if ('gradingform_'.$method !== clean_param('gradingform_'.$method, PARAM_COMPONENT)) {
throw new moodle_exception('invalid_method_name', 'core_grading');
}
$available = $this->get_available_methods(false);
if (!array_key_exists($method, $available)) {
throw new moodle_exception('invalid_method_name', 'core_grading');
}
// get the current grading area record if it exists
$area = $DB->get_record('grading_areas',
array('contextid' => $this->context->id, 'component' => $this->component, 'areaname' => $this->area), 'id,activemethod', IGNORE_MISSING);
if (empty($area)) {
// no area record yet, create one with the active method set
$area = array(
'contextid' => $this->context->id,
'component' => $this->component,
'areaname' => $this->area,
'activemethod' => $method);
$DB->insert_record('grading_areas', $area);
} else {
// update the existing record if needed
if ($area->activemethod != $method) {
$DB->set_field('grading_areas', 'activemethod', $method, array('id' => $area->id));
}
}
}
/**
* Make sure that the given properties were set to some not-null value
*

View File

@ -16,7 +16,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for Moodle language manipulation library defined in mlanglib.php
* Unit tests for the advanced grading subsystem
*
* @package core
* @subpackage grading
@ -32,6 +32,10 @@ if (empty($CFG->unittestprefix)) {
die('You must define $CFG->unittestprefix to run these unit tests.');
}
if ($CFG->unittestprefix == $CFG->prefix) {
die('Eh? Do you play with the fire? Fireman Sam won\'t come dude. The unittestprefix must be different from the standard prefix.');
}
require_once($CFG->dirroot . '/grade/grading/lib.php'); // Include the code to test
/**
@ -45,10 +49,31 @@ class testable_grading_manager extends grading_manager {
*/
class grading_manager_test extends UnitTestCase {
/** @var moodle_database current real driver instance */
protected $realDB;
public function setUp() {
global $DB, $CFG;
$this->realDB = $DB;
$dbclass = get_class($this->realDB);
$DB = new $dbclass();
$DB->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->unittestprefix);
$dbman = $DB->get_manager();
// drop everything we have in the mock DB
$dbman->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml');
// create all tables we need for this test case
$dbman->install_one_table_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml', 'grading_areas');
}
public function tearDown() {
global $DB, $CFG;
// clean everything we have in the mock DB
//$DB->get_manager()->delete_tables_from_xmldb_file($CFG->dirroot . '/lib/db/install.xml');
// switch to the real database
$DB = $this->realDB;
}
public function test_basic_instantiation() {
@ -66,4 +91,29 @@ class grading_manager_test extends UnitTestCase {
$manager3 = get_grading_manager($fakecontext, 'assignment_upload');
$manager4 = get_grading_manager($fakecontext, 'assignment_upload', 'submission');
}
public function test_set_and_get_grading_area() {
global $DB;
sleep(2); // to make sure the microtime will always return unique values
$areaname = 'area' . (string)microtime(true);
$fakecontext = (object)array(
'id' => 42,
'contextlevel' => CONTEXT_MODULE,
'instanceid' => 22,
'path' => '/1/3/15/42',
'depth' => 4);
// non-existing area
$gradingman = get_grading_manager($fakecontext, 'mod_foobar', $areaname);
$this->assertNull($gradingman->get_active_method());
// create area and set active method
$gradingman->set_active_method('rubric');
$this->assertEqual('rubric', $gradingman->get_active_method());
// attempting to set an invalid method
$this->expectException('moodle_exception');
$gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name');
}
}