Skip to content

Add background color functionality to certificate #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: MOODLE_400_STABLE
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions element/image/classes/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public function render_form_elements($mform) {
$mform->hideIf('posy', 'isbackground', 'checked');
}

// Add a color picker for background color
$mform->addElement('certificate_colourpicker', 'backgroundcolor', get_string('backgroundcolor', 'tool_certificate'));
$mform->setType('backgroundcolor', PARAM_RAW);
$mform->addHelpButton('backgroundcolor', 'backgroundcolor', 'tool_certificate');

// Add input fields for entering dimensions for the background color
$mform->addElement('text', 'backgroundwidth', get_string('backgroundwidth', 'tool_certificate'), ['size' => 10]);
$mform->setType('backgroundwidth', PARAM_INT);
$mform->addHelpButton('backgroundwidth', 'backgroundwidth', 'tool_certificate');

$mform->addElement('text', 'backgroundheight', get_string('backgroundheight', 'tool_certificate'), ['size' => 10]);
$mform->setType('backgroundheight', PARAM_INT);
$mform->addHelpButton('backgroundheight', 'backgroundheight', 'tool_certificate');
}

/**
Expand Down Expand Up @@ -130,6 +143,9 @@ private function calculate_additional_data($data) {
$arrtostore = [
'width' => !empty($data->width) ? (int) $data->width : 0,
'height' => !empty($data->height) ? (int) $data->height : 0,
'backgroundcolor' => !empty($data->backgroundcolor) ? $data->backgroundcolor : '',
'backgroundwidth' => !empty($data->backgroundwidth) ? (int) $data->backgroundwidth : 0,
'backgroundheight' => !empty($data->backgroundheight) ? (int) $data->backgroundheight : 0,
];
if ($this->can_be_used_as_a_background()) {
$arrtostore['isbackground'] = !empty($data->isbackground);
Expand Down Expand Up @@ -172,6 +188,12 @@ public function render($pdf, $preview, $user, $issue) {
}

element_helper::render_image($pdf, $this, $file, [], $imageinfo['width'], $imageinfo['height']);

// Render background color
if (!empty($imageinfo['backgroundcolor'])) {
$pdf->SetFillColorArray(\TCPDF_COLORS::convertHTMLColorToDec($imageinfo['backgroundcolor']));
$pdf->Rect(0, 0, $imageinfo['backgroundwidth'], $imageinfo['backgroundheight'], 'F');
}
}

/**
Expand Down Expand Up @@ -204,8 +226,17 @@ public function render_html() {
$fileimageinfo = $file->get_imageinfo();
}

return element_helper::render_image_html($url, $fileimageinfo, (float)$imageinfo['width'], (float)$imageinfo['height'],
$html = element_helper::render_image_html($url, $fileimageinfo, (float)$imageinfo['width'], (float)$imageinfo['height'],
$this->get_display_name());

// Render background color
if (!empty($imageinfo['backgroundcolor'])) {
$html .= \html_writer::div('', '', [
'style' => 'background-color: ' . $imageinfo['backgroundcolor'] . '; width: ' . $imageinfo['backgroundwidth'] . 'mm; height: ' . $imageinfo['backgroundheight'] . 'mm;'
]);
}

return $html;
}

/**
Expand All @@ -217,7 +248,7 @@ public function prepare_data_for_form() {
$record = parent::prepare_data_for_form();
if (!empty($this->get_data())) {
$dateinfo = json_decode($this->get_data());
$keys = ['width', 'height', 'isbackground'];
$keys = ['width', 'height', 'isbackground', 'backgroundcolor', 'backgroundwidth', 'backgroundheight'];
foreach ($keys as $key) {
if (isset($dateinfo->$key)) {
$record->$key = $dateinfo->$key;
Expand Down
Loading