2013-02-20 12:21:57 +00:00
|
|
|
<?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
|
2013-07-22 05:06:18 +00:00
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
2013-02-20 12:21:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The base class for the customcert elements.
|
|
|
|
*
|
2013-04-26 07:30:33 +00:00
|
|
|
* @package mod_customcert
|
2013-07-22 05:06:18 +00:00
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
2013-02-20 12:21:57 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2015-12-11 06:53:45 +00:00
|
|
|
namespace mod_customcert;
|
|
|
|
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2013-06-12 10:35:27 +00:00
|
|
|
/**
|
2015-12-11 06:53:45 +00:00
|
|
|
* Class element
|
2013-06-12 10:35:27 +00:00
|
|
|
*
|
|
|
|
* All customercert element plugins are based on this class.
|
|
|
|
*/
|
2015-12-11 06:53:45 +00:00
|
|
|
abstract class element {
|
2013-02-20 12:21:57 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* @var \stdClass $element The data for the element we are adding.
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
|
|
|
public $element;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $element the element data
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
2013-07-23 04:29:27 +00:00
|
|
|
public function __construct($element) {
|
2013-06-06 10:59:08 +00:00
|
|
|
$this->element = clone($element);
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function renders the form elements when adding a customcert element.
|
2013-02-26 09:22:52 +00:00
|
|
|
* Can be overridden if more functionality is needed.
|
2013-02-20 12:21:57 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param edit_element_form $mform the edit_form instance.
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
|
|
|
public function render_form_elements($mform) {
|
2013-05-30 08:28:47 +00:00
|
|
|
// Render the common elements.
|
2016-02-16 09:03:34 +00:00
|
|
|
element_helper::render_form_element_font($mform);
|
|
|
|
element_helper::render_form_element_colour($mform);
|
|
|
|
element_helper::render_form_element_position($mform);
|
2013-05-03 10:51:41 +00:00
|
|
|
}
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
/**
|
|
|
|
* Sets the data on the form when editing an element.
|
|
|
|
* Can be overridden if more functionality is needed.
|
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param edit_element_form $mform the edit_form instance
|
2013-06-06 10:59:08 +00:00
|
|
|
*/
|
|
|
|
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) {
|
|
|
|
if ($mform->elementExists($property)) {
|
|
|
|
$element = $mform->getElement($property);
|
|
|
|
$element->setValue($value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
/**
|
|
|
|
* Performs validation on the element values.
|
|
|
|
* Can be overridden if more functionality is needed.
|
|
|
|
*
|
|
|
|
* @param array $data the submitted data
|
2013-03-14 01:16:37 +00:00
|
|
|
* @param array $files the submitted files
|
2013-02-20 12:21:57 +00:00
|
|
|
* @return array the validation errors
|
|
|
|
*/
|
|
|
|
public function validate_form_elements($data, $files) {
|
2013-05-30 08:28:47 +00:00
|
|
|
// Array to return the errors.
|
2013-02-20 12:21:57 +00:00
|
|
|
$errors = array();
|
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
// Common validation methods.
|
2016-02-16 09:03:34 +00:00
|
|
|
$errors += element_helper::validate_form_element_colour($data);
|
|
|
|
$errors += element_helper::validate_form_element_position($data);
|
2013-02-20 12:21:57 +00:00
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles saving the form elements created by this element.
|
2013-04-29 10:10:06 +00:00
|
|
|
* Can be overridden if more functionality is needed.
|
2013-02-20 12:21:57 +00:00
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $data the form data
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
|
|
|
public function save_form_elements($data) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
// Get the data from the form.
|
2015-12-11 06:53:45 +00:00
|
|
|
$element = new \stdClass();
|
2013-06-06 10:59:08 +00:00
|
|
|
$element->name = $data->name;
|
2013-05-30 08:28:47 +00:00
|
|
|
$element->data = $this->save_unique_data($data);
|
2013-06-06 10:59:08 +00:00
|
|
|
$element->font = (isset($data->font)) ? $data->font : null;
|
|
|
|
$element->size = (isset($data->size)) ? $data->size : null;
|
|
|
|
$element->colour = (isset($data->colour)) ? $data->colour : null;
|
2015-07-29 01:29:16 +00:00
|
|
|
$element->width = (isset($data->width)) ? $data->width : null;
|
2015-07-30 04:51:50 +00:00
|
|
|
$element->refpoint = (isset($data->refpoint)) ? $data->refpoint : null;
|
2013-04-24 09:24:57 +00:00
|
|
|
$element->timemodified = time();
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2013-06-06 10:59:08 +00:00
|
|
|
// 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;
|
|
|
|
$DB->update_record('customcert_elements', $element);
|
|
|
|
} else { // Must be adding a new one.
|
|
|
|
$element->element = $data->element;
|
|
|
|
$element->pageid = $data->pageid;
|
2016-02-16 09:03:34 +00:00
|
|
|
$element->sequence = \mod_customcert\element_helper::get_element_sequence($element->pageid);
|
2013-06-06 10:59:08 +00:00
|
|
|
$element->timecreated = time();
|
|
|
|
$DB->insert_record('customcert_elements', $element);
|
|
|
|
}
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 08:15:32 +00:00
|
|
|
/**
|
|
|
|
* This will handle how form data will be saved into the data column in the
|
2016-02-16 09:03:34 +00:00
|
|
|
* customcert_elements table.
|
2013-04-29 10:10:06 +00:00
|
|
|
* Can be overridden if more functionality is needed.
|
2013-02-22 08:15:32 +00:00
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $data the form data
|
2013-05-28 08:33:46 +00:00
|
|
|
* @return string the unique data to save
|
2013-02-22 08:15:32 +00:00
|
|
|
*/
|
|
|
|
public function save_unique_data($data) {
|
2013-05-28 08:33:46 +00:00
|
|
|
return '';
|
2013-02-22 08:15:32 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 10:51:41 +00:00
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* This handles copying data from another element of the same type.
|
2013-05-03 10:51:41 +00:00
|
|
|
* Can be overridden if more functionality is needed.
|
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \stdClass $data the form data
|
2016-02-16 09:03:34 +00:00
|
|
|
* @return bool returns true if the data was copied successfully, false otherwise
|
2013-05-03 10:51:41 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public function copy_element($data) {
|
2013-05-03 10:51:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
/**
|
2013-04-10 09:53:56 +00:00
|
|
|
* Handles rendering the element on the pdf.
|
2016-02-16 09:03:34 +00:00
|
|
|
*
|
2013-04-29 10:10:06 +00:00
|
|
|
* Must be overridden.
|
2013-02-20 12:21:57 +00:00
|
|
|
*
|
2015-12-11 06:53:45 +00:00
|
|
|
* @param \pdf $pdf the pdf object
|
2013-06-28 07:17:43 +00:00
|
|
|
* @param bool $preview true if it is a preview, false otherwise
|
2016-08-23 08:22:58 +00:00
|
|
|
* @param \stdClass $user the user we are rendering this for
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
2016-08-23 08:22:58 +00:00
|
|
|
public abstract function render($pdf, $preview, $user);
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
/**
|
|
|
|
* Render the element in html.
|
|
|
|
*
|
|
|
|
* Must be overridden.
|
|
|
|
*
|
|
|
|
* This function is used to render the element when we are using the
|
|
|
|
* drag and drop interface to position it.
|
|
|
|
*
|
|
|
|
* @return string the html
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public abstract function render_html();
|
|
|
|
|
2015-08-05 05:38:35 +00:00
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
/**
|
|
|
|
* Handles deleting any data this element may have introduced.
|
2013-04-29 10:10:06 +00:00
|
|
|
* Can be overridden if more functionality is needed.
|
2013-02-20 12:21:57 +00:00
|
|
|
*
|
|
|
|
* @return bool success return true if deletion success, false otherwise
|
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public function delete() {
|
2013-02-20 12:21:57 +00:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
return $DB->delete_records('customcert_elements', array('id' => $this->element->id));
|
|
|
|
}
|
2013-02-24 15:41:33 +00:00
|
|
|
|
2013-05-30 08:28:47 +00:00
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* This function is responsible for handling the restoration process of the element.
|
2013-05-30 08:28:47 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* For example, the function may save data that is related to another course module, this
|
|
|
|
* data will need to be updated if we are restoring the course as the course module id will
|
|
|
|
* be different in the new course.
|
2013-05-30 08:28:47 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param \restore_customcert_activity_task $restore
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public function after_restore($restore) { }
|
2013-05-30 08:28:47 +00:00
|
|
|
|
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* Magic getter for read only access.
|
2013-05-30 08:28:47 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @param string $name
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public function __get($name) {
|
|
|
|
if (property_exists($this->element, $name)) {
|
|
|
|
return $this->element->$name;
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* Returns an instance of the element class.
|
2013-05-30 08:28:47 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @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.
|
2013-05-30 08:28:47 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public static function instance($element) {
|
|
|
|
// Get the class name.
|
|
|
|
$classname = '\\customcertelement_' . $element->element . '\\element';
|
2013-05-30 08:28:47 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
// Ensure the necessary class exists.
|
|
|
|
if (class_exists($classname)) {
|
|
|
|
return new $classname($element);
|
2015-07-29 01:29:16 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
return false;
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
2013-05-17 04:15:01 +00:00
|
|
|
/**
|
2016-02-16 09:03:34 +00:00
|
|
|
* Return the list of possible elements to add.
|
2013-05-17 04:15:01 +00:00
|
|
|
*
|
2016-02-16 09:03:34 +00:00
|
|
|
* @return array the list of element types that can be used.
|
2013-05-17 04:15:01 +00:00
|
|
|
*/
|
2016-02-16 09:03:34 +00:00
|
|
|
public static function get_available_types() {
|
|
|
|
global $CFG;
|
|
|
|
|
|
|
|
// Array to store the element types.
|
|
|
|
$options = array();
|
|
|
|
|
|
|
|
// Check that the directory exists.
|
|
|
|
$elementdir = "$CFG->dirroot/mod/customcert/element";
|
|
|
|
if (file_exists($elementdir)) {
|
|
|
|
// Get directory contents.
|
|
|
|
$elementfolders = new \DirectoryIterator($elementdir);
|
|
|
|
// Loop through the elements folder.
|
|
|
|
foreach ($elementfolders as $elementfolder) {
|
|
|
|
// If it is not a directory or it is '.' or '..', skip it.
|
|
|
|
if (!$elementfolder->isDir() || $elementfolder->isDot()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check that the standard class exists, if not we do
|
|
|
|
// not want to display it as an option as it will not work.
|
|
|
|
$foldername = $elementfolder->getFilename();
|
|
|
|
// Get the class name.
|
|
|
|
$classname = '\\customcertelement_' . $foldername . '\\element';
|
|
|
|
// Ensure the necessary class exists.
|
|
|
|
if (class_exists($classname)) {
|
|
|
|
$component = "customcertelement_{$foldername}";
|
|
|
|
$options[$foldername] = get_string('pluginname', $component);
|
|
|
|
}
|
2013-05-17 04:15:01 +00:00
|
|
|
}
|
2013-02-24 15:41:33 +00:00
|
|
|
}
|
2013-04-11 11:25:55 +00:00
|
|
|
|
2016-02-16 09:03:34 +00:00
|
|
|
\core_collator::asort($options);
|
|
|
|
return $options;
|
2013-02-24 15:41:33 +00:00
|
|
|
}
|
|
|
|
}
|