From fb13a1484ddd4fdd552c6ee7339751cec15c65f7 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Wed, 5 Oct 2011 03:49:21 +0200 Subject: [PATCH] MDL-29479 Improved grading manager factory function --- grade/grading/lib.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/grade/grading/lib.php b/grade/grading/lib.php index 6cebef60491..294bc9bf4cc 100644 --- a/grade/grading/lib.php +++ b/grade/grading/lib.php @@ -29,15 +29,27 @@ defined('MOODLE_INTERNAL') || die(); /** * Factory method returning an instance of the grading manager * - * @param stdClass $context + * @param stdClass|int $context or $areaid * @param string $component the frankenstyle name of the component * @param string $area the name of the gradable area * @return grading_manager */ -function get_grading_manager($context = null, $component = null, $area = null) { +function get_grading_manager($context_or_areaid = null, $component = null, $area = null) { + global $DB; $manager = new grading_manager(); + if (is_object($context_or_areaid)) { + $context = $context_or_areaid; + } else { + $context = null; + + if (is_numeric($context_or_areaid)) { + $manager->load($context_or_areaid); + return $manager; + } + } + if (!is_null($context)) { $manager->set_context($context); } @@ -110,6 +122,20 @@ class grading_manager { $this->area = $area; } + /** + * Loads the gradable area info from the database + * + * @param int $areaid + */ + public function load($areaid) { + global $DB; + + $this->areacache = $DB->get_record('grading_areas', array('id' => $areaid), '*', MUST_EXIST); + $this->context = get_context_instance_by_id($this->areacache->contextid, MUST_EXIST); + $this->component = $this->areacache->component; + $this->area = $this->areacache->areaname; + } + /** * Returns the list of available grading methods in the given context *