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
|
|
|
*
|
2018-06-06 13:31:39 +00:00
|
|
|
* All customcert element plugins are based on this class.
|
2017-02-16 12:12:19 +00:00
|
|
|
*
|
|
|
|
* @package mod_customcert
|
|
|
|
* @copyright 2013 Mark Nelson <markn@moodle.com>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2013-06-12 10:35:27 +00:00
|
|
|
*/
|
2015-12-11 06:53:45 +00:00
|
|
|
abstract class element {
|
2013-02-20 12:21:57 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-26 05:39:47 +00:00
|
|
|
* @var \stdClass $element The data for the element we are adding - do not use, kept for legacy reasons.
|
2013-02-20 12:21:57 +00:00
|
|
|
*/
|
2017-08-26 05:39:47 +00:00
|
|
|
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;
|
2013-02-20 12:21:57 +00:00
|
|
|
|
2016-10-21 13:33:59 +00:00
|
|
|
/**
|
|
|
|
* @var bool $showposxy Show position XY form elements?
|
|
|
|
*/
|
2017-08-26 05:39:47 +00:00
|
|
|
protected $showposxy;
|
2016-10-21 13:33:59 +00:00
|
|
|
|
2013-02-20 12:21:57 +00:00
|
|
|
/**
|
|
|
|
* 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) {
|
2016-10-21 13:33:59 +00:00
|
|
|
$showposxy = get_config('customcert', 'showposxy');
|
|
|
|
|
2017-08-26 05:39:47 +00:00
|
|
|
// Keeping this for legacy reasons so we do not break third-party elements.
|
2013-06-06 10:59:08 +00:00
|
|
|
$this->element = clone($element);
|
2017-08-26 05:39:47 +00:00
|
|
|
|
|
|
|
$this->id = $element->id;
|
2017-08-26 06:27:22 +00:00
|
|
|
$this->pageid = $element->pageid;
|
2017-08-26 05:39:47 +00:00
|
|
|
$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;
|
2016-10-21 13:33:59 +00:00
|
|
|
$this->showposxy = isset($showposxy) && $showposxy;
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 05:39:47 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
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);
|
2016-10-21 13:33:59 +00:00
|
|
|
if ($this->showposxy) {
|
|
|
|
element_helper::render_form_element_position($mform);
|
|
|
|
}
|
|
|
|
element_helper::render_form_element_width($mform);
|
2019-01-03 03:05:25 +00:00
|
|
|
element_helper::render_form_element_refpoint($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.
|
2017-08-26 05:39:47 +00:00
|
|
|
$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) {
|
2017-08-26 05:27:19 +00:00
|
|
|
if (!is_null($value) && $mform->elementExists($property)) {
|
2013-06-06 10:59:08 +00:00
|
|
|
$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);
|
2016-10-21 13:33:59 +00:00
|
|
|
if ($this->showposxy) {
|
|
|
|
$errors += element_helper::validate_form_element_position($data);
|
|
|
|
}
|
|
|
|
$errors += element_helper::validate_form_element_width($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
|
2016-06-13 11:39:57 +00:00
|
|
|
* @return bool true of success, false otherwise.
|
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;
|
2017-08-05 07:45:35 +00:00
|
|
|
$element->fontsize = (isset($data->fontsize)) ? $data->fontsize : null;
|
2013-06-06 10:59:08 +00:00
|
|
|
$element->colour = (isset($data->colour)) ? $data->colour : null;
|
2016-10-21 13:33:59 +00:00
|
|
|
if ($this->showposxy) {
|
|
|
|
$element->posx = (isset($data->posx)) ? $data->posx : null;
|
|
|
|
$element->posy = (isset($data->posy)) ? $data->posy : 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.
|
2017-08-26 05:39:47 +00:00
|
|
|
if (!empty($this->id)) { // Must be updating a record in the database.
|
|
|
|
$element->id = $this->id;
|
2016-06-13 11:39:57 +00:00
|
|
|
return $DB->update_record('customcert_elements', $element);
|
2013-06-06 10:59:08 +00:00
|
|
|
} 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();
|
2016-06-13 11:39:57 +00:00
|
|
|
return $DB->insert_record('customcert_elements', $element, false);
|
2013-06-06 10:59:08 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-08 11:45:20 +00:00
|
|
|
/**
|
|
|
|
* This defines if an element plugin can be added to a certificate.
|
|
|
|
* Can be overridden if an element plugin wants to take over the control.
|
|
|
|
*
|
|
|
|
* @return bool returns true if the element can be added, false otherwise
|
|
|
|
*/
|
2018-08-23 12:37:32 +00:00
|
|
|
public static function can_add() {
|
2018-08-08 11:45:20 +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();
|
|
|
|
|
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;
|
|
|
|
|
2017-08-26 05:39:47 +00:00
|
|
|
return $DB->delete_records('customcert_elements', array('id' => $this->id));
|
2013-02-20 12:21:57 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2017-02-16 12:12:19 +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) {
|
2017-08-26 05:39:47 +00:00
|
|
|
debugging('Please call the appropriate get_* function instead of relying on magic getters', DEBUG_DEVELOPER);
|
2016-02-16 09:03:34 +00:00
|
|
|
if (property_exists($this->element, $name)) {
|
|
|
|
return $this->element->$name;
|
2013-05-30 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-24 15:41:33 +00:00
|
|
|
}
|