Skip to content

Commit cccba5d

Browse files
Merge pull request #8518 from christianbeeznest/fixes-plugin-helloworld01
Plugin: Adapt HelloWorld plugin to Chamilo 2 regions
2 parents 597b524 + fa2d7dc commit cccba5d

9 files changed

Lines changed: 213 additions & 65 deletions

File tree

public/main/admin/settings.lib.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ function getStablePluginAllowList(): array
353353
'ExtraMenuFromWebservice',
354354
'Mobidico',
355355
'ShowRegions',
356+
'HelloWorld',
356357
];
357358
}
358359

public/plugin/HelloWorld/README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
Hello world plugin
2-
===
1+
# HelloWorld plugin for Chamilo 2
32

4-
This plugin is a minimal code to serve as example of what a plugin has to contain.
3+
This is a minimal example plugin for Chamilo 2.
54

6-
This file will be linked in the plugin settings page through a README button.
5+
## Purpose
6+
7+
The plugin renders a small translated greeting in any plugin region where it is assigned. It is useful as a simple reference for:
8+
9+
- `plugin.php` metadata.
10+
- A small `Plugin` subclass.
11+
- Plugin settings.
12+
- Region rendering through `index.php`.
13+
14+
## Recommended regions
15+
16+
In the Vue SPA, Chamilo 2 currently mounts these regions globally:
17+
18+
- `content_bottom`
19+
- `pre_footer`
20+
21+
For visible UI, prefer `content_bottom`.
22+
23+
## Configuration
24+
25+
The plugin has one setting:
26+
27+
- `show_type`: selects which greeting is rendered.
28+
29+
Available values:
30+
31+
- Hello world
32+
- Hello
33+
- Hi
34+
35+
## Notes
36+
37+
This plugin does not create database tables, course tools, Vue components or Symfony controllers.

public/plugin/HelloWorld/index.php

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,57 @@
11
<?php
22

3-
// See also the share_user_info plugin
3+
/* For licensing terms, see /license.txt */
44

5-
echo '<div class="well">';
6-
if (!empty($plugin_info['settings']['hello_world_show_type'])) {
7-
echo '<h2>'.$plugin_info['settings']['hello_world_show_type'].'</h2>';
8-
} else {
9-
echo '<h2>Hello world</h2>';
5+
/**
6+
* Render a minimal translated greeting in the assigned plugin region.
7+
*/
8+
9+
if (!class_exists('Plugin', false)) {
10+
$globalInc = __DIR__.'/../../main/inc/global.inc.php';
11+
12+
if (is_file($globalInc)) {
13+
require_once $globalInc;
14+
}
15+
}
16+
17+
if (!class_exists('HelloWorldPlugin', false)) {
18+
require_once __DIR__.'/src/HelloWorldPlugin.php';
19+
}
20+
21+
$plugin = HelloWorldPlugin::create();
22+
23+
$region = '';
24+
if (isset($plugin_info) && is_array($plugin_info)) {
25+
$region = (string) ($plugin_info['current_region'] ?? '');
1026
}
1127

12-
//Using get_lang inside a plugin
13-
echo get_lang('Hello plugin');
28+
if ('' === $region && isset($_GET['region'])) {
29+
$region = (string) $_GET['region'];
30+
}
31+
32+
$region = preg_replace('/[^a-zA-Z0-9_\-]/', '', $region) ?: 'unknown';
33+
34+
$greeting = htmlspecialchars($plugin->getConfiguredGreeting(), ENT_QUOTES, 'UTF-8');
35+
$title = htmlspecialchars($plugin->get_lang('region_title'), ENT_QUOTES, 'UTF-8');
36+
$description = htmlspecialchars($plugin->get_lang('region_description'), ENT_QUOTES, 'UTF-8');
37+
$regionLabel = htmlspecialchars($region, ENT_QUOTES, 'UTF-8');
1438

15-
echo '</div>';
39+
echo <<<HTML
40+
<div class="hello-world-plugin my-4 rounded-2xl border border-support-3 bg-white p-5 shadow-sm" data-hello-world-region="{$regionLabel}">
41+
<div class="flex flex-col gap-4 sm:flex-row sm:items-start">
42+
<div class="flex h-12 w-12 shrink-0 items-center justify-center rounded-2xl bg-support-2 text-primary">
43+
<span class="mdi mdi-hand-wave-outline text-2xl" aria-hidden="true"></span>
44+
</div>
45+
<div class="min-w-0 flex-1">
46+
<div class="mb-1 flex flex-wrap items-center gap-2">
47+
<h3 class="mb-0 text-lg font-semibold text-gray-90">{$title}</h3>
48+
<span class="rounded-full bg-support-1 px-2 py-0.5 text-xs font-medium text-primary">{$regionLabel}</span>
49+
</div>
50+
<p class="mb-3 text-body-2 text-gray-50">{$description}</p>
51+
<div class="rounded-xl bg-gray-15 px-4 py-3 text-xl font-semibold text-primary">
52+
{$greeting}
53+
</div>
54+
</div>
55+
</div>
56+
</div>
57+
HTML;
Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php
2-
/* PHP code to install the plugin
3-
* For example:
4-
*
5-
// To query something to the database
62

7-
$table = Database::get_main_table(TABLE_MAIN_USER); // TABLE_MAIN_USER is a constant check the main/inc/database.constants.inc.php
8-
$sql = "SELECT firstname, lastname FROM $table_users ";
9-
$users = Database::query($sql);
3+
/* For licensing terms, see /license.txt */
104

11-
You can also use the Chamilo classes
12-
$users = UserManager::get_user_list();
5+
require_once __DIR__.'/src/HelloWorldPlugin.php';
136

14-
*/
7+
HelloWorldPlugin::create()->install();
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
<?php
2-
/**
3-
* @copyright (c) 2012 University of Geneva
4-
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
5-
* @author Laurent Opprecht <laurent@opprecht.info>
6-
*/
2+
3+
/* For licensing terms, see /license.txt */
4+
5+
$strings['plugin_title'] = 'HelloWorld';
6+
$strings['plugin_comment'] = 'Displays a simple greeting in an assigned plugin region.';
7+
$strings['show_type'] = 'Greeting';
8+
$strings['show_type_help'] = 'Choose the greeting rendered by the plugin.';
9+
$strings['option_hello_world'] = 'Hello world';
10+
$strings['option_hello'] = 'Hello';
11+
$strings['option_hi'] = 'Hi!';
12+
$strings['region_title'] = 'HelloWorld plugin';
13+
$strings['region_description'] = 'This message is rendered by a Chamilo 2 plugin region.';
14+
$strings['message_hello_world'] = 'Hello world';
15+
$strings['message_hello'] = 'Hello';
16+
$strings['message_hi'] = 'Hi!';
717
$strings['HelloPlugin'] = 'Hello!';
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
<?php
22

3-
$strings['HelloPlugin'] = 'Hola chaval!';
3+
/* For licensing terms, see /license.txt */
4+
5+
$strings['plugin_title'] = 'HelloWorld';
6+
$strings['plugin_comment'] = 'Muestra un saludo simple en una región de plugin asignada.';
7+
$strings['show_type'] = 'Saludo';
8+
$strings['show_type_help'] = 'Elige el saludo que mostrará el plugin.';
9+
$strings['option_hello_world'] = 'Hola mundo';
10+
$strings['option_hello'] = 'Hola';
11+
$strings['option_hi'] = '¡Hola!';
12+
$strings['region_title'] = 'Plugin HelloWorld';
13+
$strings['region_description'] = 'Este mensaje se muestra desde una región de plugin de Chamilo 2.';
14+
$strings['message_hello_world'] = 'Hola mundo';
15+
$strings['message_hello'] = 'Hola';
16+
$strings['message_hi'] = '¡Hola!';
17+
$strings['HelloPlugin'] = '¡Hola!';
Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
<?php
2-
/**
3-
* This script is a configuration file for the date plugin.
4-
* You can use it as a master for other platform plugins (course plugins are slightly different).
5-
* These settings will be used in the administration interface for plugins (Chamilo configuration settings->Plugins).
6-
*
7-
* @author Julio Montoya <gugli100@gmail.com>
8-
*/
9-
/**
10-
* Plugin details (must be present).
11-
*/
122

13-
/* Plugin config */
3+
/* For licensing terms, see /license.txt */
144

15-
//the plugin title
16-
$plugin_info['title'] = 'Hello world';
17-
//the comments that go with the plugin
18-
$plugin_info['comment'] = 'Shows a hello world message';
19-
//the plugin version
20-
$plugin_info['version'] = '1.0';
21-
//the plugin author
22-
$plugin_info['author'] = 'Julio Montoya';
5+
require_once __DIR__.'/src/HelloWorldPlugin.php';
236

24-
/* Plugin optional settings */
25-
26-
/*
27-
* This form will be showed in the plugin settings once the plugin was installed
28-
* in the plugin/HelloWorld/index.php you can have
29-
* access to the value: $plugin_info['settings']['hello_world_show_type']
30-
*/
31-
32-
$form = new FormValidator('hello_world_form');
33-
34-
//A simple select
35-
$options = ['hello_world' => 'Hello World', 'hello' => 'Hello', 'hi' => 'Hi!'];
36-
$form->addSelect('show_type', 'Hello world types', $options);
37-
$form->addButtonSave(get_lang('Save'), 'submit_button');
38-
39-
$plugin_info['settings_form'] = $form;
7+
$plugin_info = HelloWorldPlugin::create()->get_info();
8+
$plugin_info['source'] = 'official';
9+
$plugin_info['commercial_model'] = 'free';
10+
$plugin_info['supports_regions'] = true;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/* For licensing terms, see /license.txt */
6+
7+
/**
8+
* Minimal Chamilo 2 example plugin.
9+
*
10+
* This plugin is intentionally simple: it can be assigned to a plugin region
11+
* and renders a small translated message using a configurable greeting.
12+
*/
13+
class HelloWorldPlugin extends Plugin
14+
{
15+
public const SHOW_HELLO_WORLD = 'hello_world';
16+
public const SHOW_HELLO = 'hello';
17+
public const SHOW_HI = 'hi';
18+
19+
protected function __construct()
20+
{
21+
parent::__construct(
22+
'2.0.0',
23+
'Chamilo',
24+
[
25+
'show_type' => [
26+
'type' => 'select',
27+
'options' => [
28+
self::SHOW_HELLO_WORLD => 'option_hello_world',
29+
self::SHOW_HELLO => 'option_hello',
30+
self::SHOW_HI => 'option_hi',
31+
],
32+
'translate_options' => true,
33+
],
34+
]
35+
);
36+
}
37+
38+
public static function create(): self
39+
{
40+
static $instance = null;
41+
42+
return $instance ??= new self();
43+
}
44+
45+
public function get_info()
46+
{
47+
$info = parent::get_info();
48+
$info['supports_regions'] = true;
49+
50+
return $info;
51+
}
52+
53+
public function install(): void
54+
{
55+
// No database changes are required.
56+
}
57+
58+
public function uninstall(): void
59+
{
60+
// No data is created by this plugin.
61+
}
62+
63+
public function getConfiguredGreeting(): string
64+
{
65+
$showType = (string) ($this->get('show_type') ?: self::SHOW_HELLO_WORLD);
66+
67+
if (!in_array($showType, [self::SHOW_HELLO_WORLD, self::SHOW_HELLO, self::SHOW_HI], true)) {
68+
$showType = self::SHOW_HELLO_WORLD;
69+
}
70+
71+
return $this->getGreetingByType($showType);
72+
}
73+
74+
private function getGreetingByType(string $showType): string
75+
{
76+
return match ($showType) {
77+
self::SHOW_HELLO => $this->get_lang('message_hello'),
78+
self::SHOW_HI => $this->get_lang('message_hi'),
79+
default => $this->get_lang('message_hello_world'),
80+
};
81+
}
82+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?php
22

3-
/* PHP code to uninstall the plugin */
3+
/* For licensing terms, see /license.txt */
4+
5+
require_once __DIR__.'/src/HelloWorldPlugin.php';
6+
7+
HelloWorldPlugin::create()->uninstall();

0 commit comments

Comments
 (0)