Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
 
18
/**
19
 * Advance grading form element
20
 *
21
 * Element-container for advanced grading custom input
22
 *
23
 * @package   core_form
24
 * @copyright 2011 Marina Glancy
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
global $CFG;
29
require_once("HTML/QuickForm/element.php");
30
require_once($CFG->dirroot.'/grade/grading/form/lib.php');
31
require_once('templatable_form_element.php');
32
 
33
if (class_exists('HTML_QuickForm')) {
34
    HTML_QuickForm::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
35
}
36
 
37
/**
38
 * Advance grading form element
39
 *
40
 * HTML class for a grading element. This is a wrapper for advanced grading plugins.
41
 * When adding the 'grading' element to the form, developer must pass an object of
42
 * class gradingform_instance as $attributes['gradinginstance']. Otherwise an exception will be
43
 * thrown.
44
 * This object is responsible for implementing functions to render element html and validate it
45
 *
46
 * @package   core_form
47
 * @category  form
48
 * @copyright 2011 Marina Glancy
49
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
50
 */
51
class MoodleQuickForm_grading extends HTML_QuickForm_input implements templatable {
52
    use templatable_form_element {
53
        export_for_template as export_for_template_base;
54
    }
55
 
56
    /** @var string html for help button, if empty then no help */
57
    var $_helpbutton='';
58
 
59
    /** @var array Stores attributes passed to the element */
60
    private $gradingattributes;
61
 
62
    /**
63
     * Class constructor
64
     *
65
     * @param string $elementName Input field name attribute
66
     * @param mixed $elementLabel Label(s) for the input field
67
     * @param mixed $attributes Either a typical HTML attribute string or an associative array
68
     */
69
    public function __construct($elementName=null, $elementLabel=null, $attributes=null) {
70
        parent::__construct($elementName, $elementLabel, $attributes);
71
        $this->_type = 'grading';
72
        $this->gradingattributes = $attributes;
73
    }
74
 
75
    /**
76
     * Old syntax of class constructor. Deprecated in PHP7.
77
     *
78
     * @deprecated since Moodle 3.1
79
     */
80
    public function MoodleQuickForm_grading($elementName=null, $elementLabel=null, $attributes=null) {
81
        debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
82
        self::__construct($elementName, $elementLabel, $attributes);
83
    }
84
 
85
    /**
86
     * Helper function to retrieve gradingform_instance passed in element attributes
87
     *
88
     * @return gradingform_instance
89
     */
90
    public function get_gradinginstance() {
91
        if (is_array($this->gradingattributes) && array_key_exists('gradinginstance', $this->gradingattributes)) {
92
            return $this->gradingattributes['gradinginstance'];
93
        } else {
94
            return null;
95
        }
96
    }
97
 
98
    /**
99
     * Returns the input field in HTML
100
     *
101
     * @return string
102
     */
103
    public function toHtml(){
104
        global $PAGE;
105
        return $this->get_gradinginstance()->render_grading_element($PAGE, $this);
106
    }
107
 
108
    /**
109
     * get html for help button
110
     *
111
     * @return string html for help button
112
     */
113
    public function getHelpButton(){
114
        return $this->_helpbutton;
115
    }
116
 
117
    /**
118
     * The renderer of gradingform_instance will take care itself about different display
119
     * in normal and frozen states
120
     *
121
     * @return string
122
     */
123
    public function getElementTemplateType(){
124
        return 'default';
125
    }
126
 
127
    /**
128
     * Called by HTML_QuickForm whenever form event is made on this element.
129
     * Adds necessary rules to the element and checks that coorenct instance of gradingform_instance
130
     * is passed in attributes
131
     *
132
     * @param string $event Name of event
133
     * @param mixed $arg event arguments
134
     * @param object $caller calling object
135
     * @return bool
136
     * @throws moodle_exception
137
     */
138
    public function onQuickFormEvent($event, $arg, &$caller) {
139
        if ($event == 'createElement') {
140
            $attributes = $arg[2];
141
            if (!is_array($attributes) || !array_key_exists('gradinginstance', $attributes) || !($attributes['gradinginstance'] instanceof gradingform_instance)) {
142
                throw new moodle_exception('exc_gradingformelement', 'grading');
143
            }
144
        }
145
 
146
        $name = $this->getName();
147
        if ($name && $caller->elementExists($name)) {
148
            $caller->addRule($name, $this->get_gradinginstance()->default_validation_error_message(), 'gradingvalidated', $this->gradingattributes);
149
        }
150
        return parent::onQuickFormEvent($event, $arg, $caller);
151
    }
152
 
153
    /**
154
     * Function registered as rule for this element and is called when this element is being validated.
155
     * This is a wrapper to pass the validation to the method gradingform_instance::validate_grading_element
156
     *
157
     * @param mixed $elementvalue value of element to be validated
158
     * @param array $attributes element attributes
159
     * @return MoodleQuickForm_grading
160
     */
161
    public static function _validate($elementvalue, $attributes = null) {
162
        if (!$attributes['gradinginstance']->is_empty_form($elementvalue)) {
163
            return $attributes['gradinginstance']->validate_grading_element($elementvalue);
164
        }
165
        return true;
166
    }
167
 
168
    public function export_for_template(renderer_base $output) {
169
        $context = $this->export_for_template_base($output);
170
        $context['html'] = $this->toHtml();
171
        return $context;
172
    }
173
}