Created factory class

This commit is contained in:
Mark Nelson 2017-08-26 13:27:19 +08:00
parent ae90c7ee4a
commit 327750bb38
9 changed files with 82 additions and 32 deletions

View file

@ -112,7 +112,7 @@ class restore_customcert_activity_task extends restore_activity_task {
// Go through the elements for the certificate. // Go through the elements for the certificate.
foreach ($elements as $e) { foreach ($elements as $e) {
// Get an instance of the element class. // 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); $e->after_restore($this);
} }
} }

View file

@ -63,7 +63,7 @@ class edit_element_form extends \moodleform {
$mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->addHelpButton('name', 'elementname', 'customcert'); $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->element->render_form_elements($mform);
$this->add_action_buttons(true); $this->add_action_buttons(true);

View file

@ -85,7 +85,7 @@ abstract class element {
// Loop through the properties of the element and set the values // Loop through the properties of the element and set the values
// of the corresponding form element, if it exists. // of the corresponding form element, if it exists.
foreach ($this->element as $property => $value) { foreach ($this->element as $property => $value) {
if ($mform->elementExists($property)) { if (!is_null($value) && $mform->elementExists($property)) {
$element = $mform->getElement($property); $element = $mform->getElement($property);
$element->setValue($value); $element->setValue($value);
} }
@ -234,23 +234,4 @@ abstract class element {
return $this->element->$name; 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;
}
} }

View file

@ -0,0 +1,69 @@
<?php
// This file is part of the customcert module for Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contains the factory class responsible for creating custom certificate instances.
*
* @package mod_customcert
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @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 <markn@moodle.com>
* @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;
}
}

View file

@ -99,7 +99,7 @@ class external extends \external_api {
} }
// Get an instance of the element class. // 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); return $e->save_form_elements($data);
} }
@ -159,7 +159,7 @@ class external extends \external_api {
} }
// Get an instance of the element class. // 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(); return $e->render_html();
} }

View file

@ -159,7 +159,7 @@ class template {
if ($elements = $DB->get_records_sql($sql, array('templateid' => $this->id))) { if ($elements = $DB->get_records_sql($sql, array('templateid' => $this->id))) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // 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(); $e->delete();
} else { } else {
// The plugin files are missing, so just remove the entry from the DB. // 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))) { if ($elements = $DB->get_records('customcert_elements', array('pageid' => $page->id))) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // 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(); $e->delete();
} else { } else {
// The plugin files are missing, so just remove the entry from the DB. // 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); $element = $DB->get_record('customcert_elements', array('id' => $elementid), '*', MUST_EXIST);
// Get an instance of the element class. // 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(); $e->delete();
} else { } else {
// The plugin files are missing, so just remove the entry from the DB. // The plugin files are missing, so just remove the entry from the DB.
@ -299,7 +299,7 @@ class template {
// Loop through and display. // Loop through and display.
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // 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); $e->render($pdf, $preview, $user);
} }
} }

View file

@ -86,7 +86,7 @@ if ($data = $mform->get_data()) {
// Set the element variable. // Set the element variable.
$data->element = $element->element; $data->element = $element->element;
// Get an instance of the element class. // 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); $e->save_form_elements($data);
} }

View file

@ -52,7 +52,7 @@ if ($confirm) {
if ($elements = $DB->get_records_sql($sql, array('templateid' => $template->get_id()))) { if ($elements = $DB->get_records_sql($sql, array('templateid' => $template->get_id()))) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // 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(); $e->delete();
} }
} }
@ -84,7 +84,7 @@ if ($confirm) {
// Ok, now we want to insert this into the database. // Ok, now we want to insert this into the database.
$element->id = $DB->insert_record('customcert_elements', $element); $element->id = $DB->insert_record('customcert_elements', $element);
// Load any other information the element may need to for the template. // 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)) { if (!$e->copy_element($templateelement)) {
// Failed to copy - delete the element. // Failed to copy - delete the element.
$e->delete(); $e->delete();

View file

@ -91,7 +91,7 @@ if ($page->leftmargin) {
if ($elements) { if ($elements) {
foreach ($elements as $element) { foreach ($elements as $element) {
// Get an instance of the element class. // 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) { switch ($element->refpoint) {
case \mod_customcert\element_helper::CUSTOMCERT_REF_POINT_TOPRIGHT: case \mod_customcert\element_helper::CUSTOMCERT_REF_POINT_TOPRIGHT:
$class = 'element refpoint-right'; $class = 'element refpoint-right';