Chamilo is a learning management system focused on ease of use and accessibility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
chamilo-lms/main/inc/lib/plugin.class.php

160 lines
4.5 KiB

14 years ago
<?php
/**
* Base class for plugins
*
* @copyright (c) 2012 University of Geneva
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
* @author Laurent Opprecht <laurent@opprecht.info>
*/
class Plugin {
14 years ago
protected $version = '';
protected $author = '';
protected $fields = array();
protected function __construct($version, $author, $settings = array()) {
14 years ago
$this->version = $version;
$this->author = $author;
$this->fields = $settings;
global $language_files;
$language_files[] = 'plugin_' . $this->get_name();
}
function get_info() {
14 years ago
$result = array();
$result['title'] = $this->get_title();
$result['comment'] = $this->get_comment();
$result['version'] = $this->get_version();
$result['author'] = $this->get_author();
if ($form = $this->get_settings_form()) {
14 years ago
$result['settings_form'] = $form;
foreach ($this->fields as $name => $type) {
14 years ago
$value = $this->get($name);
$result[$name] = $value;
}
}
return $result;
}
function get_name() {
14 years ago
$result = get_class($this);
$result = str_replace('Plugin', '', $result);
$result = strtolower($result);
return $result;
}
function get_title() {
14 years ago
return $this->get_lang('plugin_title');
}
function get_comment() {
14 years ago
return $this->get_lang('plugin_comment');
}
function get_version() {
14 years ago
return $this->version;
}
function get_author() {
14 years ago
return $this->author;
}
function get_css() {
14 years ago
$name = $this->get_name();
$root = api_get_path(SYS_PLUGIN_PATH);
$path = "$root/$name/resources/$name.css";
if (!is_readable($path)) {
14 years ago
return '';
}
$css = array();
$css[] = file_get_contents($path);
$result = implode($css);
return $result;
}
/**
*
* @return FormValidator
*/
function get_settings_form() {
14 years ago
$result = new FormValidator($this->get_name());
$defaults = array();
foreach ($this->fields as $name => $type) {
14 years ago
$value = $this->get($name);
$defaults[$name] = $value;
$type = $type ? $type : 'text';
if ($type == 'wysiwyg') {
14 years ago
$result->add_html_editor($name, $this->get_lang($name));
} else {
14 years ago
$result->addElement($type, $name, $this->get_lang($name));
}
}
$result->setDefaults($defaults);
$result->addElement('style_submit_button', 'submit_button', $this->get_lang('Save'));
return $result;
}
function get($name) {
14 years ago
$content = '';
$title = 'Static';
$settings = $this->get_settings();
foreach ($settings as $setting) {
if ($setting['variable'] == ($this->get_name() . '_' . $name)) {
14 years ago
return $setting['selected_value'];
}
}
return false;
}
private $settings = null;
public function get_settings() {
if (is_null($this->settings)) {
14 years ago
$settings = api_get_settings_params(array("subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting')));
$this->settings = $settings;
}
return $this->settings;
}
private $strings = null;
public function get_lang($name) {
if (is_null($this->strings)) {
14 years ago
global $language_interface;
$root = api_get_path(SYS_PLUGIN_PATH);
$plugin_name = $this->get_name();
14 years ago
$language = $language_interface;
$path = "$root/$plugin_name/lang/$language.php";
//1. Loading english if exists
$english_path = "$root/$plugin_name/lang/english.php";
if (is_readable($english_path)) {
include $english_path;
14 years ago
$this->strings = $strings;
}
//2. Loading the system language
if (is_readable($path)) {
include $path;
if (!empty($strings)) {
foreach ($strings as $key => $string) {
$this->strings[$key] = $string;
}
}
} else {
14 years ago
$this->strings = array();
}
}
if (isset($this->strings[$name])) {
14 years ago
return $this->strings[$name];
}
return get_lang($name);
}
}