Skip to content

Commit 8866f97

Browse files
committed
feat: style question inputs like Moodle
Adds bootstrap classes to question inputs to roughly emulate Moodle's look-and-feel.
1 parent a78bf06 commit 8866f97

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: classes/question_ui_renderer.php

+34
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ private function render_part(DOMNode $part, question_attempt $qa, ?question_disp
216216
$this->hide_unwanted_feedback($xpath, $options);
217217
$this->set_input_values_and_readonly($xpath, $qa, $options);
218218
$this->shuffle_contents($xpath);
219+
$this->add_bootstrap_classes($xpath);
219220
$this->mangle_ids_and_names($xpath, $qa);
220221
$this->clean_up($xpath);
221222
$this->resolve_placeholders($xpath);
@@ -449,4 +450,37 @@ private function resolve_placeholders(DOMXPath $xpath): void {
449450
}
450451
}
451452
}
453+
454+
private function add_bootstrap_classes(DOMXPath $xpath): void {
455+
/** @var DOMElement $element */
456+
foreach ($xpath->query("
457+
//xhtml:input[@type != 'checkbox' and @type != 'radio' and @type != 'button' and @type != 'submit' and @type != 'reset']
458+
| //xhtml:select | //xhtml:textarea
459+
") as $element) {
460+
$this->add_class_names($element, "form-control", "qpy-input");
461+
}
462+
463+
foreach ($xpath->query("//xhtml:input[@type = 'button' or @type = 'submit' or @type = 'reset'] | //xhtml:button") as $element) {
464+
$this->add_class_names($element, "btn", "btn-primary", "qpy-input");
465+
}
466+
467+
foreach ($xpath->query("//xhtml:input[@type = 'checkbox' or @type = 'radio']") as $element) {
468+
$this->add_class_names($element, "qpy-input");
469+
}
470+
}
471+
472+
private function add_class_names(DOMElement $element, string ...$newclasses): void {
473+
$classarray = [];
474+
for ($class = strtok($element->getAttribute("class"), " \t\n"); $class; $class = strtok(" \t\n")) {
475+
$classarray[] = $class;
476+
}
477+
478+
foreach ($newclasses as $newclass) {
479+
if (!in_array($newclass, $classarray)) {
480+
$classarray[] = $newclass;
481+
}
482+
}
483+
484+
$element->setAttribute("class", implode(" ", $classarray));
485+
}
452486
}

Diff for: styles.css

+7
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@
7373
.qpy-repetition-remove {
7474
margin: .5em;
7575
}
76+
77+
.form-control.qpy-input {
78+
/* Matches the styling used by most bundled question types. */
79+
display: inline;
80+
width: auto;
81+
vertical-align: baseline;
82+
}

0 commit comments

Comments
 (0)