From 430ae35d711e9647b2499d0840939dfa372b44cd Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sat, 26 Aug 2017 13:27:19 +0800 Subject: [PATCH] Created factory class --- ...restore_customcert_activity_task.class.php | 2 +- classes/edit_element_form.php | 2 +- classes/element.php | 21 +----- classes/element_factory.php | 69 +++++++++++++++++++ classes/external.php | 4 +- classes/template.php | 8 +-- edit_element.php | 2 +- load_template.php | 4 +- rearrange.php | 2 +- 9 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 classes/element_factory.php diff --git a/backup/moodle2/restore_customcert_activity_task.class.php b/backup/moodle2/restore_customcert_activity_task.class.php index 934e6de..609dc21 100644 --- a/backup/moodle2/restore_customcert_activity_task.class.php +++ b/backup/moodle2/restore_customcert_activity_task.class.php @@ -112,7 +112,7 @@ class restore_customcert_activity_task extends restore_activity_task { // Go through the elements for the certificate. foreach ($elements as $e) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($e)) { + if ($e = \mod_customcert\element_factory::get_element_instance($e)) { $e->after_restore($this); } } diff --git a/classes/edit_element_form.php b/classes/edit_element_form.php index e357530..350f39c 100644 --- a/classes/edit_element_form.php +++ b/classes/edit_element_form.php @@ -63,7 +63,7 @@ class edit_element_form extends \moodleform { $mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->addHelpButton('name', 'elementname', 'customcert'); - $this->element = \mod_customcert\element::instance($element); + $this->element = \mod_customcert\element_factory::get_element_instance($element); $this->element->render_form_elements($mform); $this->add_action_buttons(true); diff --git a/classes/element.php b/classes/element.php index 8c829e0..1897256 100644 --- a/classes/element.php +++ b/classes/element.php @@ -85,7 +85,7 @@ abstract class element { // 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) { - if ($mform->elementExists($property)) { + if (!is_null($value) && $mform->elementExists($property)) { $element = $mform->getElement($property); $element->setValue($value); } @@ -234,23 +234,4 @@ abstract class element { return $this->element->$name; } } - - /** - * Returns an instance of the element class. - * - * @param \stdClass $element the element - * @return \mod_customcert\element|bool returns the instance of the element class, or false if element - * class does not exists. - */ - public static function instance($element) { - // Get the class name. - $classname = '\\customcertelement_' . $element->element . '\\element'; - - // Ensure the necessary class exists. - if (class_exists($classname)) { - return new $classname($element); - } - - return false; - } } diff --git a/classes/element_factory.php b/classes/element_factory.php new file mode 100644 index 0000000..94fef13 --- /dev/null +++ b/classes/element_factory.php @@ -0,0 +1,69 @@ +. + +/** + * Contains the factory class responsible for creating custom certificate instances. + * + * @package mod_customcert + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_customcert; + +defined('MOODLE_INTERNAL') || die(); + +/** + * The factory class responsible for creating custom certificate instances. + * + * @package mod_customcert + * @copyright 2017 Mark Nelson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class element_factory { + + /** + * Returns an instance of the element class. + * + * @param \stdClass $element the element + * @return \mod_customcert\element|bool returns the instance of the element class, or false if element + * class does not exists. + */ + public static function get_element_instance($element) { + // Get the class name. + $classname = '\\customcertelement_' . $element->element . '\\element'; + + $data = new \stdClass(); + $data->id = isset($element->id) ? $element->id : null; + $data->name = isset($element->name) ? $element->name : get_string('pluginname', 'customcertelement_' . $element->element); + $data->element = $element->element; + $data->data = isset($element->data) ? $element->data : null; + $data->font = isset($element->font) ? $element->font : null; + $data->fontsize = isset($element->fontsize) ? $element->fontsize : null; + $data->colour = isset($element->colour) ? $element->colour : null; + $data->posx = isset($element->posx) ? $element->posx : null; + $data->posy = isset($element->posy) ? $element->posy : null; + $data->width = isset($element->width) ? $element->width : null; + $data->refpoint = isset($element->refpoint) ? $element->refpoint : null; + + // Ensure the necessary class exists. + if (class_exists($classname)) { + return new $classname($data); + } + + return false; + } +} diff --git a/classes/external.php b/classes/external.php index f74bf47..d8e48da 100644 --- a/classes/external.php +++ b/classes/external.php @@ -99,7 +99,7 @@ class external extends \external_api { } // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { return $e->save_form_elements($data); } @@ -159,7 +159,7 @@ class external extends \external_api { } // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { return $e->render_html(); } diff --git a/classes/template.php b/classes/template.php index b0cfea3..97bdf2e 100644 --- a/classes/template.php +++ b/classes/template.php @@ -159,7 +159,7 @@ class template { if ($elements = $DB->get_records_sql($sql, array('templateid' => $this->id))) { foreach ($elements as $element) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { $e->delete(); } else { // The plugin files are missing, so just remove the entry from the DB. @@ -199,7 +199,7 @@ class template { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) { foreach ($elements as $element) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { $e->delete(); } else { // The plugin files are missing, so just remove the entry from the DB. @@ -229,7 +229,7 @@ class template { $element = $DB->get_record('customcert_elements', array('id' => $elementid), '*', MUST_EXIST); // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { $e->delete(); } else { // The plugin files are missing, so just remove the entry from the DB. @@ -299,7 +299,7 @@ class template { // Loop through and display. foreach ($elements as $element) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { $e->render($pdf, $preview, $user); } } diff --git a/edit_element.php b/edit_element.php index eac6125..c39d89d 100644 --- a/edit_element.php +++ b/edit_element.php @@ -86,7 +86,7 @@ if ($data = $mform->get_data()) { // Set the element variable. $data->element = $element->element; // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($data)) { + if ($e = \mod_customcert\element_factory::get_element_instance($data)) { $e->save_form_elements($data); } diff --git a/load_template.php b/load_template.php index 4f3181a..caa4a1c 100644 --- a/load_template.php +++ b/load_template.php @@ -52,7 +52,7 @@ if ($confirm) { if ($elements = $DB->get_records_sql($sql, array('templateid' => $template->get_id()))) { foreach ($elements as $element) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { $e->delete(); } } @@ -84,7 +84,7 @@ if ($confirm) { // Ok, now we want to insert this into the database. $element->id = $DB->insert_record('customcert_elements', $element); // Load any other information the element may need to for the template. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { if (!$e->copy_element($templateelement)) { // Failed to copy - delete the element. $e->delete(); diff --git a/rearrange.php b/rearrange.php index 5a98cb6..5505422 100644 --- a/rearrange.php +++ b/rearrange.php @@ -91,7 +91,7 @@ if ($page->leftmargin) { if ($elements) { foreach ($elements as $element) { // Get an instance of the element class. - if ($e = \mod_customcert\element::instance($element)) { + if ($e = \mod_customcert\element_factory::get_element_instance($element)) { switch ($element->refpoint) { case \mod_customcert\element_helper::CUSTOMCERT_REF_POINT_TOPRIGHT: $class = 'element refpoint-right';