MDL-29480 Adding new fields into grading_definitions
[moodle.git] / grade / grading / form / lib.php
CommitLineData
9b8550f8
DM
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package core
20 * @subpackage grading
21 * @copyright 2011 David Mudrak <david@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27/**
28 * Grading method controller encapsulates the logic of the plugin
29 *
30 * @copyright 2011 David Mudrak <david@moodle.com>
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 */
3e43eff5 33abstract class gradingform_controller {
9b8550f8
DM
34
35 /** @var stdClass the context */
36 protected $context;
37
38 /** @var string the frankenstyle name of the component */
39 protected $component;
40
41 /** @var string the name of the gradable area */
42 protected $area;
43
44 /** @var int the id of the gradable area record */
45 protected $areaid;
46
3e43eff5
DM
47 /** @var moodle_page the target page we embed our widgets to */
48 protected $page;
49
50 /** @var stdClass|false the raw {grading_definitions} record */
51 protected $definition;
52
53 /** @var bool is the target grading page finalized for sending output to the browser */
54 protected $pagefinalized = false;
55
9b8550f8
DM
56 /**
57 * Do not instantinate this directly, use {@link grading_manager::get_controller()}
58 *
3e43eff5 59 * @return gradingform_controller instance
9b8550f8
DM
60 */
61 public function __construct(stdClass $context, $component, $area, $areaid) {
3e43eff5
DM
62 global $DB;
63
64 $this->context = $context;
65 $this->component = $component;
66 $this->area = $area;
67 $this->areaid = $areaid;
68
69 $this->load_definition();
70 }
71
72 /**
73 * Is the grading form defined and released for usage?
74 *
75 * @return boolean
76 */
77 public function is_form_available() {
78 return true; // todo make this dependent on grading_definitions existence and its status
79 }
80
81 /**
82 * Prepare a grading widget for the given rater and item
83 *
84 * If you make multiple widgets, pass bulk = true. Note that then it is
85 * the caller's responsibility to call {@link finalize_page()} method explicitly.
86 *
87 * @param int $raterid the user who will use the widget for grading
88 * @param int $itemid the graded item
89 * @param bool $bulk are more widgets to be made by this instance or is this the last one?
90 * @return gradingform_widget renderable widget to insert into the page
91 */
92 abstract public function make_grading_widget($raterid, $itemid, $bulk = false);
93
94 /**
95 * Does everything needed before the page is sent to the browser
96 */
97 public function finalize_page() {
98 $this->pagefinalized = true;
9b8550f8
DM
99 }
100
101 /**
102 * Extends the module settings navigation
103 *
104 * This function is called when the context for the page is an activity module with the
105 * FEATURE_ADVANCED_GRADING, the user has the permission moodle/grade:managegradingforms
106 * and there is an area with the active grading method set to the given plugin.
107 *
108 * @param settings_navigation $settingsnav {@link settings_navigation}
109 * @param navigation_node $node {@link navigation_node}
110 */
111 public function extend_settings_navigation(settings_navigation $settingsnav, navigation_node $node=null) {
112 // do not extend by default
113 }
3e43eff5
DM
114
115 /**
116 * Returns the name of the grading method, eg 'rubric'
117 */
118 abstract protected function get_method_name();
119
120 /**
121 * Sets the target page and returns a renderer for this plugin
122 *
123 * @param moodle_page $page the target page
124 * @return core_renderer
125 */
126 public function prepare_renderer(moodle_page $page) {
127 global $CFG;
128
129 $this->page = $page;
130 require_once($CFG->dirroot.'/grade/grading/form/'.$this->get_method_name().'/renderer.php');
131 return $page->get_renderer('gradingform_'.$this->get_method_name());
132 }
133
134 /**
135 * Loads the form definition is it exists
136 *
137 * The default implementation tries to load just the record ftom the {grading_definitions}
138 * table. The plugins are likely to override this with a more complex query that loads
139 * all required data at once.
140 */
141 protected function load_definition() {
142 global $DB;
143
144 $this->definition = $DB->get_record('grading_definitions', array(
145 'areaid' => $this->areaid,
146 'method' => $this->get_method_name()), '*', IGNORE_MISSING);
147 }
148}
149
150
151/**
152 * Base class for all gradingform plugins renderers
153 */
154abstract class gradingform_renderer extends plugin_renderer_base {
155}
156
157
158/**
159 * Base class for all gradingform renderable widgets
160 */
161abstract class gradingform_widget implements renderable {
9b8550f8 162}