|
27 | 27 | defined('MOODLE_INTERNAL') || die(); |
28 | 28 | require_once($CFG->dirroot.'/message/output/popup/lib.php'); |
29 | 29 |
|
| 30 | +use core\output\mustache_template_finder; |
| 31 | +use core\output\templatable; |
30 | 32 | use stdClass; |
31 | 33 | use context_course; |
32 | 34 | use context_system; |
@@ -3116,4 +3118,85 @@ protected function snap_page_is_whitelisted_mod() { |
3116 | 3118 | return $this->page->context->contextlevel === CONTEXT_MODULE |
3117 | 3119 | && in_array($this->page->cm->modname, $whitelist); |
3118 | 3120 | } |
| 3121 | + |
| 3122 | + /** |
| 3123 | + * Renders an mform element from a template. |
| 3124 | + * |
| 3125 | + * Copied from core_renderer::mform_element() to allow injecting |
| 3126 | + * extra context variables specific to certain modules (e.g., mod_feedback). |
| 3127 | + * |
| 3128 | + * @param HTML_QuickForm_element $element element |
| 3129 | + * @param bool $required if input is required field |
| 3130 | + * @param bool $advanced if input is an advanced field |
| 3131 | + * @param string $error error message to display |
| 3132 | + * @param bool $ingroup True if this element is rendered as part of a group |
| 3133 | + * @return mixed string|bool |
| 3134 | + */ |
| 3135 | + public function mform_element($element, $required, $advanced, $error, $ingroup) { |
| 3136 | + $templatename = 'core_form/element-' . $element->getType(); |
| 3137 | + if ($ingroup) { |
| 3138 | + $templatename .= "-inline"; |
| 3139 | + } |
| 3140 | + try { |
| 3141 | + // We call this to generate a file not found exception if there is no template. |
| 3142 | + // We don't want to call export_for_template if there is no template. |
| 3143 | + mustache_template_finder::get_template_filepath($templatename); |
| 3144 | + |
| 3145 | + if ($element instanceof templatable) { |
| 3146 | + $elementcontext = $element->export_for_template($this); |
| 3147 | + |
| 3148 | + $helpbutton = ''; |
| 3149 | + if (method_exists($element, 'getHelpButton')) { |
| 3150 | + $helpbutton = $element->getHelpButton(); |
| 3151 | + } |
| 3152 | + $label = $element->getLabel(); |
| 3153 | + $text = ''; |
| 3154 | + if (method_exists($element, 'getText')) { |
| 3155 | + // There currently exists code that adds a form element with an empty label. |
| 3156 | + // If this is the case then set the label to the description. |
| 3157 | + if (empty($label)) { |
| 3158 | + $label = $element->getText(); |
| 3159 | + } else { |
| 3160 | + $text = $element->getText(); |
| 3161 | + } |
| 3162 | + } |
| 3163 | + |
| 3164 | + // Generate the form element wrapper ids and names to pass to the template. |
| 3165 | + // This differs between group and non-group elements. |
| 3166 | + if ($element->getType() === 'group') { |
| 3167 | + // Group element. |
| 3168 | + // The id will be something like 'fgroup_id_NAME'. E.g. fgroup_id_mygroup. |
| 3169 | + $elementcontext['wrapperid'] = $elementcontext['id']; |
| 3170 | + |
| 3171 | + // Ensure group elements pass through the group name as the element name. |
| 3172 | + $elementcontext['name'] = $elementcontext['groupname']; |
| 3173 | + } else { |
| 3174 | + // Non grouped element. |
| 3175 | + // Creates an id like 'fitem_id_NAME'. E.g. fitem_id_mytextelement. |
| 3176 | + $elementcontext['wrapperid'] = 'fitem_' . $elementcontext['id']; |
| 3177 | + } |
| 3178 | + |
| 3179 | + // Detect if we are in a feedback module |
| 3180 | + global $SCRIPT; |
| 3181 | + $isfeedbackform = strpos($SCRIPT, '/mod/feedback/') !== false; |
| 3182 | + $elementcontext['isfeedbackform'] = $isfeedbackform; |
| 3183 | + |
| 3184 | + $context = [ |
| 3185 | + 'element' => $elementcontext, |
| 3186 | + 'label' => $label, |
| 3187 | + 'text' => $text, |
| 3188 | + 'required' => $required, |
| 3189 | + 'advanced' => $advanced, |
| 3190 | + 'helpbutton' => $helpbutton, |
| 3191 | + 'error' => $error, |
| 3192 | + ]; |
| 3193 | + |
| 3194 | + return $this->render_from_template($templatename, $context); |
| 3195 | + } |
| 3196 | + } catch (\Exception $e) { |
| 3197 | + // No template for this element. |
| 3198 | + return false; |
| 3199 | + } |
| 3200 | + } |
| 3201 | + |
3119 | 3202 | } |
0 commit comments