From 13f892a229dca81bcafd0f77124953122f1e0975 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sat, 26 Aug 2017 13:39:47 +0800 Subject: [PATCH] Removed usage of magic getter and abuse of $this->element --- classes/element.php | 191 +++++++++++++++++++++- classes/element_helper.php | 28 ++-- element/bgimage/classes/element.php | 6 +- element/border/classes/element.php | 9 +- element/categoryname/classes/element.php | 2 +- element/code/classes/element.php | 2 +- element/coursename/classes/element.php | 2 +- element/date/classes/element.php | 26 +-- element/grade/classes/element.php | 24 +-- element/gradeitemname/classes/element.php | 13 +- element/image/classes/element.php | 33 ++-- element/teachername/classes/element.php | 9 +- element/text/classes/element.php | 9 +- element/userfield/classes/element.php | 9 +- element/userpicture/classes/element.php | 24 +-- 15 files changed, 293 insertions(+), 94 deletions(-) diff --git a/classes/element.php b/classes/element.php index 1897256..9a38f70 100644 --- a/classes/element.php +++ b/classes/element.php @@ -38,14 +38,69 @@ defined('MOODLE_INTERNAL') || die(); abstract class element { /** - * @var \stdClass $element The data for the element we are adding. + * @var \stdClass $element The data for the element we are adding - do not use, kept for legacy reasons. */ - public $element; + protected $element; + + /** + * @var int The id. + */ + protected $id; + + /** + * @var int The page id. + */ + protected $pageid; + + /** + * @var string The name. + */ + protected $name; + + /** + * @var mixed The data. + */ + protected $data; + + /** + * @var string The font name. + */ + protected $font; + + /** + * @var int The font size. + */ + protected $fontsize; + + /** + * @var string The font colour. + */ + protected $colour; + + /** + * @var int The position x. + */ + protected $posx; + + /** + * @var int The position y. + */ + protected $posy; + + /** + * @var int The width. + */ + protected $width; + + /** + * @var int The refpoint. + */ + protected $refpoint; /** * @var bool $showposxy Show position XY form elements? */ - public $showposxy; + protected $showposxy; /** * Constructor. @@ -55,10 +110,121 @@ abstract class element { public function __construct($element) { $showposxy = get_config('customcert', 'showposxy'); + // Keeping this for legacy reasons so we do not break third-party elements. $this->element = clone($element); + + $this->id = $element->id; + $this->name = $element->name; + $this->data = $element->data; + $this->font = $element->font; + $this->fontsize = $element->fontsize; + $this->colour = $element->colour; + $this->posx = $element->posx; + $this->posy = $element->posy; + $this->width = $element->width; + $this->refpoint = $element->refpoint; $this->showposxy = isset($showposxy) && $showposxy; } + /** + * Returns the id. + * + * @return int + */ + public function get_id() { + return $this->id; + } + + /** + * Returns the page id. + * + * @return int + */ + public function get_pageid() { + return $this->pageid; + } + + /** + * Returns the name. + * + * @return int + */ + public function get_name() { + return $this->name; + } + + /** + * Returns the data. + * + * @return mixed + */ + public function get_data() { + return $this->data; + } + + /** + * Returns the font name. + * + * @return string + */ + public function get_font() { + return $this->font; + } + + /** + * Returns the font size. + * + * @return int + */ + public function get_fontsize() { + return $this->fontsize; + } + + /** + * Returns the font colour. + * + * @return string + */ + public function get_colour() { + return $this->colour; + } + + /** + * Returns the position x. + * + * @return int + */ + public function get_posx() { + return $this->posx; + } + + /** + * Returns the position y. + * + * @return int + */ + public function get_posy() { + return $this->posy; + } + + /** + * Returns the width. + * + * @return int + */ + public function get_width() { + return $this->width; + } + + /** + * Returns the refpoint. + * + * @return int + */ + public function get_refpoint() { + return $this->refpoint; + } + /** * This function renders the form elements when adding a customcert element. * Can be overridden if more functionality is needed. @@ -84,7 +250,17 @@ abstract class element { public function definition_after_data($mform) { // Loop through the properties of the element and set the values // of the corresponding form element, if it exists. - foreach ($this->element as $property => $value) { + $properties = [ + 'name' => $this->name, + 'font' => $this->font, + 'fontsize' => $this->fontsize, + 'colour' => $this->colour, + 'posx' => $this->posx, + 'posy' => $this->posy, + 'width' => $this->width, + 'refpoint' => $this->refpoint + ]; + foreach ($properties as $property => $value) { if (!is_null($value) && $mform->elementExists($property)) { $element = $mform->getElement($property); $element->setValue($value); @@ -140,8 +316,8 @@ abstract class element { $element->timemodified = time(); // Check if we are updating, or inserting a new element. - if (!empty($this->element->id)) { // Must be updating a record in the database. - $element->id = $this->element->id; + if (!empty($this->id)) { // Must be updating a record in the database. + $element->id = $this->id; return $DB->update_record('customcert_elements', $element); } else { // Must be adding a new one. $element->element = $data->element; @@ -208,7 +384,7 @@ abstract class element { public function delete() { global $DB; - return $DB->delete_records('customcert_elements', array('id' => $this->element->id)); + return $DB->delete_records('customcert_elements', array('id' => $this->id)); } /** @@ -230,6 +406,7 @@ abstract class element { * @param string $name */ public function __get($name) { + debugging('Please call the appropriate get_* function instead of relying on magic getters', DEBUG_DEVELOPER); if (property_exists($this->element, $name)) { return $this->element->$name; } diff --git a/classes/element_helper.php b/classes/element_helper.php index 2cfb921..bdaf335 100644 --- a/classes/element_helper.php +++ b/classes/element_helper.php @@ -61,14 +61,14 @@ class element_helper { */ public static function render_content($pdf, $element, $content) { list($font, $attr) = self::get_font($element); - $pdf->setFont($font, $attr, $element->fontsize); - $fontcolour = \TCPDF_COLORS::convertHTMLColorToDec($element->colour, $fontcolour); + $pdf->setFont($font, $attr, $element->get_fontsize()); + $fontcolour = \TCPDF_COLORS::convertHTMLColorToDec($element->get_colour(), $fontcolour); $pdf->SetTextColor($fontcolour['R'], $fontcolour['G'], $fontcolour['B']); - $x = $element->posx; - $y = $element->posy; - $w = $element->width; - $refpoint = $element->refpoint; + $x = $element->get_posx(); + $y = $element->get_posy(); + $w = $element->get_width(); + $refpoint = $element->get_refpoint(); $actualwidth = $pdf->GetStringWidth($content); if ($w and $w < $actualwidth) { @@ -77,19 +77,19 @@ class element_helper { switch ($refpoint) { case self::CUSTOMCERT_REF_POINT_TOPRIGHT: - $x = $element->posx - $actualwidth; + $x = $element->get_posx() - $actualwidth; if ($x < 0) { $x = 0; - $w = $element->posx; + $w = $element->get_posx(); } else { $w = $actualwidth; } break; case self::CUSTOMCERT_REF_POINT_TOPCENTER: - $x = $element->posx - $actualwidth / 2; + $x = $element->get_posx() - $actualwidth / 2; if ($x < 0) { $x = 0; - $w = $element->posx * 2; + $w = $element->get_posx() * 2; } else { $w = $actualwidth; } @@ -120,9 +120,9 @@ class element_helper { $fontstyle .= '; font-style: italic'; } - $style = $fontstyle . '; color: ' . $element->colour . '; font-size: ' . $element->fontsize . 'pt;'; - if ($element->width) { - $style .= ' width: ' . $element->width . 'mm'; + $style = $fontstyle . '; color: ' . $element->get_colour() . '; font-size: ' . $element->get_fontsize() . 'pt;'; + if ($element->get_width()) { + $style .= ' width: ' . $element->get_width() . 'mm'; } return \html_writer::div($content, '', array('style' => $style)); } @@ -253,7 +253,7 @@ class element_helper { */ public static function get_font($element) { // Variable for the font. - $font = $element->font; + $font = $element->get_font(); // Get the last two characters of the font name. $fontlength = strlen($font); $lastchar = $font[$fontlength - 1]; diff --git a/element/bgimage/classes/element.php b/element/bgimage/classes/element.php index 7c2ea24..540fe13 100644 --- a/element/bgimage/classes/element.php +++ b/element/bgimage/classes/element.php @@ -67,7 +67,7 @@ class element extends \customcertelement_image\element { */ public function render($pdf, $preview, $user) { // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } @@ -97,7 +97,7 @@ class element extends \customcertelement_image\element { global $DB; // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return ''; } @@ -105,7 +105,7 @@ class element extends \customcertelement_image\element { $url = \moodle_url::make_pluginfile_url($file->get_contextid(), 'mod_customcert', 'image', $file->get_itemid(), $file->get_filepath(), $file->get_filename()); // Get the page we are rendering this on. - $page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST); + $page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST); // Set the image to the size of the page. $style = 'width: ' . $page->width . 'mm; height: ' . $page->height . 'mm'; diff --git a/element/border/classes/element.php b/element/border/classes/element.php index b2c580d..127754c 100644 --- a/element/border/classes/element.php +++ b/element/border/classes/element.php @@ -58,8 +58,8 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - $colour = \TCPDF_COLORS::convertHTMLColorToDec($this->element->colour, $colour); - $pdf->SetLineStyle(array('width' => $this->element->data, 'color' => $colour)); + $colour = \TCPDF_COLORS::convertHTMLColorToDec($this->get_colour(), $colour); + $pdf->SetLineStyle(array('width' => $this->get_data(), 'color' => $colour)); $pdf->Line(0, 0, $pdf->getPageWidth(), 0); $pdf->Line($pdf->getPageWidth(), 0, $pdf->getPageWidth(), $pdf->getPageHeight()); $pdf->Line(0, $pdf->getPageHeight(), $pdf->getPageWidth(), $pdf->getPageHeight()); @@ -106,8 +106,9 @@ class element extends \mod_customcert\element { * @param \mod_customcert\edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { - if (!empty($this->element->data)) { - $this->element->width = $this->element->data; + if (!empty($this->get_data())) { + $element = $mform->getElement('width'); + $element->setValue($this->get_data()); } parent::definition_after_data($mform); } diff --git a/element/categoryname/classes/element.php b/element/categoryname/classes/element.php index 4985cf8..adf7d70 100644 --- a/element/categoryname/classes/element.php +++ b/element/categoryname/classes/element.php @@ -43,7 +43,7 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - \mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name($this->id)); + \mod_customcert\element_helper::render_content($pdf, $this, self::get_category_name($this->get_id())); } /** diff --git a/element/code/classes/element.php b/element/code/classes/element.php index c6de625..db05c43 100644 --- a/element/code/classes/element.php +++ b/element/code/classes/element.php @@ -49,7 +49,7 @@ class element extends \mod_customcert\element { $code = \mod_customcert\certificate::generate_code(); } else { // Get the page. - $page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST); + $page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST); // Get the customcert this page belongs to. $customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST); // Now we can get the issue for this user. diff --git a/element/coursename/classes/element.php b/element/coursename/classes/element.php index 61af7f6..a4d1058 100644 --- a/element/coursename/classes/element.php +++ b/element/coursename/classes/element.php @@ -43,7 +43,7 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - $courseid = \mod_customcert\element_helper::get_courseid($this->id); + $courseid = \mod_customcert\element_helper::get_courseid($this->get_id()); $course = get_course($courseid); \mod_customcert\element_helper::render_content($pdf, $this, $course->fullname); diff --git a/element/date/classes/element.php b/element/date/classes/element.php index 2f67de0..265b244 100644 --- a/element/date/classes/element.php +++ b/element/date/classes/element.php @@ -109,14 +109,14 @@ class element extends \mod_customcert\element { global $DB; // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } $courseid = \mod_customcert\element_helper::get_courseid($this->id); // Decode the information stored in the database. - $dateinfo = json_decode($this->element->data); + $dateinfo = json_decode($this->get_data()); $dateitem = $dateinfo->dateitem; $dateformat = $dateinfo->dateformat; @@ -125,7 +125,7 @@ class element extends \mod_customcert\element { $date = time(); } else { // Get the page. - $page = $DB->get_record('customcert_pages', array('id' => $this->element->pageid), '*', MUST_EXIST); + $page = $DB->get_record('customcert_pages', array('id' => $this->get_pageid()), '*', MUST_EXIST); // Get the customcert this page belongs to. $customcert = $DB->get_record('customcert', array('templateid' => $page->templateid), '*', MUST_EXIST); // Now we can get the issue for this user. @@ -175,12 +175,12 @@ class element extends \mod_customcert\element { */ public function render_html() { // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } // Decode the information stored in the database. - $dateinfo = json_decode($this->element->data); + $dateinfo = json_decode($this->get_data()); $dateformat = $dateinfo->dateformat; return \mod_customcert\element_helper::render_html_content($this, $this->get_date_format_string(time(), $dateformat)); @@ -193,10 +193,14 @@ class element extends \mod_customcert\element { */ public function definition_after_data($mform) { // Set the item and format for this element. - if (!empty($this->element->data)) { - $dateinfo = json_decode($this->element->data); - $this->element->dateitem = $dateinfo->dateitem; - $this->element->dateformat = $dateinfo->dateformat; + if (!empty($this->get_data())) { + $dateinfo = json_decode($this->get_data()); + + $element = $mform->getElement('dateitem'); + $element->setValue($dateinfo->dateitem); + + $element = $mform->getElement('dateformat'); + $element->setValue($dateinfo->dateformat); } parent::definition_after_data($mform); @@ -213,10 +217,10 @@ class element extends \mod_customcert\element { public function after_restore($restore) { global $DB; - $dateinfo = json_decode($this->element->data); + $dateinfo = json_decode($this->get_data()); if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $dateinfo->dateitem)) { $dateinfo->dateitem = $newitem->newitemid; - $DB->set_field('customcert_elements', 'data', self::save_unique_data($dateinfo), array('id' => $this->element->id)); + $DB->set_field('customcert_elements', 'data', self::save_unique_data($dateinfo), array('id' => $this->get_id())); } } diff --git a/element/grade/classes/element.php b/element/grade/classes/element.php index 812407c..8ae807c 100644 --- a/element/grade/classes/element.php +++ b/element/grade/classes/element.php @@ -96,14 +96,14 @@ class element extends \mod_customcert\element { */ public function render($pdf, $preview, $user) { // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } $courseid = \mod_customcert\element_helper::get_courseid($this->id); // Decode the information stored in the database. - $gradeinfo = json_decode($this->element->data); + $gradeinfo = json_decode($this->get_data()); // If we are previewing this certificate then just show a demonstration grade. if ($preview) { @@ -129,12 +129,12 @@ class element extends \mod_customcert\element { global $COURSE; // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } // Decode the information stored in the database. - $gradeinfo = json_decode($this->element->data); + $gradeinfo = json_decode($this->get_data()); $courseitem = \grade_item::fetch_course_item($COURSE->id); // Define how many decimals to display. @@ -154,10 +154,14 @@ class element extends \mod_customcert\element { */ public function definition_after_data($mform) { // Set the item and format for this element. - if (!empty($this->element->data)) { - $gradeinfo = json_decode($this->element->data); - $this->element->gradeitem = $gradeinfo->gradeitem; - $this->element->gradeformat = $gradeinfo->gradeformat; + if (!empty($this->get_data())) { + $gradeinfo = json_decode($this->get_data()); + + $element = $mform->getElement('gradeitem'); + $element->setValue($gradeinfo->gradeitem); + + $element = $mform->getElement('gradeformat'); + $element->setValue($gradeinfo->gradeformat); } parent::definition_after_data($mform); @@ -174,10 +178,10 @@ class element extends \mod_customcert\element { public function after_restore($restore) { global $DB; - $gradeinfo = json_decode($this->element->data); + $gradeinfo = json_decode($this->get_data()); if ($newitem = \restore_dbops::get_backup_ids_record($restore->get_restoreid(), 'course_module', $gradeinfo->gradeitem)) { $gradeinfo->gradeitem = $newitem->newitemid; - $DB->set_field('customcert_elements', 'data', self::save_unique_data($gradeinfo), array('id' => $this->element->id)); + $DB->set_field('customcert_elements', 'data', self::save_unique_data($gradeinfo), array('id' => $this->get_id())); } } diff --git a/element/gradeitemname/classes/element.php b/element/gradeitemname/classes/element.php index de25948..d7f1001 100644 --- a/element/gradeitemname/classes/element.php +++ b/element/gradeitemname/classes/element.php @@ -74,9 +74,9 @@ class element extends \mod_customcert\element { global $DB; // Check that the grade item is not empty. - if (!empty($this->element->data)) { + if (!empty($this->get_data())) { // Get the course module information. - $cm = $DB->get_record('course_modules', array('id' => $this->element->data), '*', MUST_EXIST); + $cm = $DB->get_record('course_modules', array('id' => $this->get_data()), '*', MUST_EXIST); $module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST); // Get the name of the item. @@ -98,9 +98,9 @@ class element extends \mod_customcert\element { global $DB; // Check that the grade item is not empty. - if (!empty($this->element->data)) { + if (!empty($this->get_data())) { // Get the course module information. - $cm = $DB->get_record('course_modules', array('id' => $this->element->data), '*', MUST_EXIST); + $cm = $DB->get_record('course_modules', array('id' => $this->get_data()), '*', MUST_EXIST); $module = $DB->get_record('modules', array('id' => $cm->module), '*', MUST_EXIST); // Get the name of the item. @@ -118,8 +118,9 @@ class element extends \mod_customcert\element { * @param \mod_customcert\edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { - if (!empty($this->element->data)) { - $this->element->gradeitem = $this->element->data; + if (!empty($this->get_data())) { + $element = $mform->getElement('gradeitem'); + $element->setValue($this->get_data()); } parent::definition_after_data($mform); } diff --git a/element/image/classes/element.php b/element/image/classes/element.php index 94f4b39..90da7eb 100644 --- a/element/image/classes/element.php +++ b/element/image/classes/element.php @@ -172,11 +172,11 @@ class element extends \mod_customcert\element { */ public function render($pdf, $preview, $user) { // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } - $imageinfo = json_decode($this->element->data); + $imageinfo = json_decode($this->get_data()); if ($file = $this->get_file()) { $location = make_request_directory() . '/target'; @@ -184,9 +184,9 @@ class element extends \mod_customcert\element { $mimetype = $file->get_mimetype(); if ($mimetype == 'image/svg+xml') { - $pdf->ImageSVG($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height); + $pdf->ImageSVG($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } else { - $pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height); + $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } } } @@ -201,11 +201,11 @@ class element extends \mod_customcert\element { */ public function render_html() { // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return ''; } - $imageinfo = json_decode($this->element->data); + $imageinfo = json_decode($this->get_data()); // Get the image. $fs = get_file_storage(); @@ -246,12 +246,17 @@ class element extends \mod_customcert\element { global $COURSE, $SITE; // Set the image, width and height for this element. - if (!empty($this->element->data)) { - $imageinfo = json_decode($this->element->data); + if (!empty($this->get_data())) { + $imageinfo = json_decode($this->get_data()); if ($file = $this->get_file()) { - $this->element->fileid = $file->get_id(); - $this->element->width = $imageinfo->width; - $this->element->height = $imageinfo->height; + $element = $mform->getElement('fileid'); + $element->setValue($this->get_data()); + + $element = $mform->getElement('width'); + $element->setValue($imageinfo->width); + + $element = $mform->getElement('height'); + $element->setValue($imageinfo->height); } } @@ -282,7 +287,7 @@ class element extends \mod_customcert\element { global $DB; // Get the current data we have stored for this element. - $elementinfo = json_decode($this->element->data); + $elementinfo = json_decode($this->get_data()); // Update the context. $elementinfo->contextid = \context_course::instance($restore->get_courseid())->id; @@ -291,7 +296,7 @@ class element extends \mod_customcert\element { $elementinfo = json_encode($elementinfo); // Perform the update. - $DB->set_field('customcert_elements', 'data', $elementinfo, array('id' => $this->element->id)); + $DB->set_field('customcert_elements', 'data', $elementinfo, array('id' => $this->get_id())); } /** @@ -300,7 +305,7 @@ class element extends \mod_customcert\element { * @return \stored_file|bool stored_file instance if exists, false if not */ public function get_file() { - $imageinfo = json_decode($this->element->data); + $imageinfo = json_decode($this->get_data()); $fs = get_file_storage(); diff --git a/element/teachername/classes/element.php b/element/teachername/classes/element.php index 3c61745..a2cba2d 100644 --- a/element/teachername/classes/element.php +++ b/element/teachername/classes/element.php @@ -71,7 +71,7 @@ class element extends \mod_customcert\element { public function render($pdf, $preview, $user) { global $DB; - $teacher = $DB->get_record('user', array('id' => $this->element->data)); + $teacher = $DB->get_record('user', array('id' => $this->get_data())); $teachername = fullname($teacher); \mod_customcert\element_helper::render_content($pdf, $this, $teachername); @@ -88,7 +88,7 @@ class element extends \mod_customcert\element { public function render_html() { global $DB; - $teacher = $DB->get_record('user', array('id' => $this->element->data)); + $teacher = $DB->get_record('user', array('id' => $this->get_data())); $teachername = fullname($teacher); return \mod_customcert\element_helper::render_html_content($this, $teachername); @@ -121,8 +121,9 @@ class element extends \mod_customcert\element { * @param \mod_customcert\edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { - if (!empty($this->element->data)) { - $this->element->teacher = $this->element->data; + if (!empty($this->get_data())) { + $element = $mform->getElement('teacher'); + $element->setValue($this->get_data()); } parent::definition_after_data($mform); } diff --git a/element/text/classes/element.php b/element/text/classes/element.php index 5c0c642..9ae500f 100644 --- a/element/text/classes/element.php +++ b/element/text/classes/element.php @@ -67,7 +67,7 @@ class element extends \mod_customcert\element { * @param \stdClass $user the user we are rendering this for */ public function render($pdf, $preview, $user) { - \mod_customcert\element_helper::render_content($pdf, $this, $this->element->data); + \mod_customcert\element_helper::render_content($pdf, $this, $this->get_data()); } /** @@ -79,7 +79,7 @@ class element extends \mod_customcert\element { * @return string the html */ public function render_html() { - return \mod_customcert\element_helper::render_html_content($this, $this->element->data); + return \mod_customcert\element_helper::render_html_content($this, $this->get_data()); } /** @@ -88,8 +88,9 @@ class element extends \mod_customcert\element { * @param \mod_customcert\edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { - if (!empty($this->element->data)) { - $this->element->text = $this->element->data; + if (!empty($this->get_data())) { + $element = $mform->getElement('text'); + $element->setValue($this->get_data()); } parent::definition_after_data($mform); } diff --git a/element/userfield/classes/element.php b/element/userfield/classes/element.php index 550a2a2..1b87086 100644 --- a/element/userfield/classes/element.php +++ b/element/userfield/classes/element.php @@ -101,7 +101,7 @@ class element extends \mod_customcert\element { global $CFG, $DB; // The user field to display. - $field = $this->element->data; + $field = $this->get_data(); // The value to display on the PDF. $value = ''; if (is_number($field)) { // Must be a custom user profile field. @@ -132,7 +132,7 @@ class element extends \mod_customcert\element { global $CFG, $DB, $USER; // The user field to display. - $field = $this->element->data; + $field = $this->get_data(); // The value to display - we always want to show a value here so it can be repositioned. $value = $field; if (is_number($field)) { // Must be a custom user profile field. @@ -164,8 +164,9 @@ class element extends \mod_customcert\element { * @param \mod_customcert\edit_element_form $mform the edit_form instance */ public function definition_after_data($mform) { - if (!empty($this->element->data)) { - $this->element->userfield = $this->element->data; + if (!empty($this->get_data())) { + $element = $mform->getElement('userfield'); + $element->setValue($this->get_data()); } parent::definition_after_data($mform); } diff --git a/element/userpicture/classes/element.php b/element/userpicture/classes/element.php index efe143f..ce945cb 100644 --- a/element/userpicture/classes/element.php +++ b/element/userpicture/classes/element.php @@ -113,11 +113,11 @@ class element extends \mod_customcert\element { global $CFG; // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return; } - $imageinfo = json_decode($this->element->data); + $imageinfo = json_decode($this->get_data()); $context = \context_user::instance($user->id); @@ -138,10 +138,10 @@ class element extends \mod_customcert\element { if ($file) { $location = make_request_directory() . '/target'; $file->copy_content_to($location); - $pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height); + $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } else if ($preview) { // Can't find an image, but we are in preview mode then display default pic. $location = $CFG->dirroot . '/pix/u/f1.png'; - $pdf->Image($location, $this->element->posx, $this->element->posy, $imageinfo->width, $imageinfo->height); + $pdf->Image($location, $this->get_posx(), $this->get_posy(), $imageinfo->width, $imageinfo->height); } } @@ -157,11 +157,11 @@ class element extends \mod_customcert\element { global $PAGE, $USER; // If there is no element data, we have nothing to display. - if (empty($this->element->data)) { + if (empty($this->get_data())) { return ''; } - $imageinfo = json_decode($this->element->data); + $imageinfo = json_decode($this->get_data()); // Get the image. $userpicture = new \user_picture($USER); @@ -194,10 +194,14 @@ class element extends \mod_customcert\element { */ public function definition_after_data($mform) { // Set the image, width and height for this element. - if (!empty($this->element->data)) { - $imageinfo = json_decode($this->element->data); - $this->element->width = $imageinfo->width; - $this->element->height = $imageinfo->height; + if (!empty($this->get_data())) { + $imageinfo = json_decode($this->get_data()); + + $element = $mform->getElement('width'); + $element->setValue($imageinfo->width); + + $element = $mform->getElement('height'); + $element->setValue($imageinfo->height); } parent::definition_after_data($mform);