Actualización
This commit is contained in:
18
plugin/redirection/README.md
Normal file
18
plugin/redirection/README.md
Normal file
@@ -0,0 +1,18 @@
|
||||
Redirection plugin
|
||||
===
|
||||
|
||||
Chamilo plugin for the redirection of specific users after they login and
|
||||
redirect from the index.php to the selected URL.
|
||||
|
||||
1. Requires the addition of the following in configuration.php:
|
||||
|
||||
```
|
||||
$_configuration['plugin_redirection_enabled'] = true;
|
||||
```
|
||||
|
||||
This setting is defined in configuration.php rather than in settings_current to reduce the
|
||||
load, as it is used on every login.
|
||||
|
||||
2. Add the plugin in the "menu administration" region.
|
||||
|
||||
@TODO Check the load difference for *just* checking it in settings_current rather than building the plugin object
|
||||
180
plugin/redirection/RedirectionPlugin.php
Normal file
180
plugin/redirection/RedirectionPlugin.php
Normal file
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Redirection plugin. Allows the configuration of redirection of specific users right after they login.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
class RedirectionPlugin extends Plugin
|
||||
{
|
||||
public $isAdminPlugin = true;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
protected function __construct()
|
||||
{
|
||||
$version = '1.0';
|
||||
$author = 'Enrique Alcaraz, Julio Montoya';
|
||||
parent::__construct($version, $author, ['enabled' => 'boolean']);
|
||||
$this->isAdminPlugin = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RedirectionPlugin
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
static $result = null;
|
||||
|
||||
return $result ? $result : $result = new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inser a new redirection (and delete the previous one for this user, if any).
|
||||
*
|
||||
* @param int $userId
|
||||
* @param string $url
|
||||
*
|
||||
* @return false|string
|
||||
*/
|
||||
public static function insert($userId, $url)
|
||||
{
|
||||
$userId = (int) $userId;
|
||||
|
||||
if (empty($userId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$sql = "DELETE FROM plugin_redirection WHERE user_id = $userId";
|
||||
Database::query($sql);
|
||||
|
||||
$userInfo = api_get_user_info($userId);
|
||||
|
||||
if (empty($userInfo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Database::insert(
|
||||
'plugin_redirection',
|
||||
[
|
||||
'user_id' => $userId,
|
||||
'url' => $url,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current redirection for a given user (if any).
|
||||
*
|
||||
* @param int $userId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getUrlFromUser($userId)
|
||||
{
|
||||
$userId = (int) $userId;
|
||||
$userInfo = api_get_user_info($userId);
|
||||
if (empty($userInfo)) {
|
||||
return false;
|
||||
}
|
||||
$sql = "SELECT * FROM plugin_redirection WHERE user_id = $userId LIMIT 1";
|
||||
$result = Database::query($sql);
|
||||
|
||||
return Database::fetch_array($result, 'ASSOC');
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes redirection from user.
|
||||
*
|
||||
* @param int $userId
|
||||
*/
|
||||
public static function deleteUserRedirection($userId)
|
||||
{
|
||||
$table = Database::get_main_table('plugin_redirection');
|
||||
Database::delete(
|
||||
$table,
|
||||
['user_id = ?' => [$userId]]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an existing redirection.
|
||||
*
|
||||
* @param int $id
|
||||
*/
|
||||
public static function delete($id)
|
||||
{
|
||||
$table = Database::get_main_table('plugin_redirection');
|
||||
Database::delete(
|
||||
$table,
|
||||
['id = ?' => [$id]]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all current redirection records.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getAll()
|
||||
{
|
||||
$table = Database::get_main_table('plugin_redirection');
|
||||
|
||||
return Database::select('*', $table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Install the required DB structure to store the plugin data.
|
||||
*/
|
||||
public static function install()
|
||||
{
|
||||
$table = Database::get_main_table('plugin_redirection');
|
||||
|
||||
$sql = "CREATE TABLE IF NOT EXISTS $table (
|
||||
id INT unsigned NOT NULL auto_increment PRIMARY KEY,
|
||||
user_id INT unsigned NOT NULL DEFAULT 0,
|
||||
url VARCHAR(255) NOT NULL DEFAULT ''
|
||||
)";
|
||||
|
||||
Database::query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstall the plugin, cleaning up the database structure created on install.
|
||||
*/
|
||||
public static function uninstall()
|
||||
{
|
||||
$table = Database::get_main_table('plugin_redirection');
|
||||
$sql = "DROP TABLE IF EXISTS $table";
|
||||
Database::query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect user if plugin is installed.
|
||||
*
|
||||
* @param int $userId
|
||||
*/
|
||||
public static function redirectUser($userId)
|
||||
{
|
||||
// Check redirection plugin
|
||||
$plugin = new AppPlugin();
|
||||
$pluginList = $plugin->getInstalledPlugins();
|
||||
$redirectionInstalled = in_array('redirection', $pluginList);
|
||||
if ($redirectionInstalled) {
|
||||
$pluginInfo = $plugin->getPluginInfo('redirection');
|
||||
if (!empty($pluginInfo) && isset($pluginInfo['obj'])) {
|
||||
/** @var RedirectionPlugin $redirectionPlugin */
|
||||
$redirectionPlugin = $pluginInfo['obj'];
|
||||
$record = $redirectionPlugin->getUrlFromUser($userId);
|
||||
if (!empty($record) && !empty($record['url'])) {
|
||||
header('Location: '.$record['url']);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
88
plugin/redirection/admin.php
Normal file
88
plugin/redirection/admin.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Admin interface for the plugin configuration.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
require_once __DIR__.'/config.php';
|
||||
|
||||
api_protect_admin_script();
|
||||
|
||||
$list = RedirectionPlugin::getAll();
|
||||
|
||||
$url = api_get_path(WEB_PLUGIN_PATH).'redirection/admin.php';
|
||||
|
||||
$form = new FormValidator('add', 'post', api_get_self());
|
||||
$form->addHeader('Redirection');
|
||||
$form->addSelectAjax(
|
||||
'user_id',
|
||||
get_lang('User'),
|
||||
null,
|
||||
[
|
||||
'url' => api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_like',
|
||||
'id' => 'user_id',
|
||||
]
|
||||
);
|
||||
$form->addUrl('url', 'URL');
|
||||
$form->addButtonSend(get_lang('Add'));
|
||||
|
||||
if (isset($_REQUEST['id'])) {
|
||||
RedirectionPlugin::delete($_REQUEST['id']);
|
||||
Display::addFlash(Display::return_message(get_lang('Deleted')));
|
||||
header('Location: admin.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($form->validate()) {
|
||||
$result = RedirectionPlugin::insert($_POST['user_id'], $_POST['url']);
|
||||
if ($result) {
|
||||
Display::addFlash(Display::return_message(get_lang('Added')));
|
||||
} else {
|
||||
Display::addFlash(Display::return_message(get_lang('Error'), 'warning'));
|
||||
}
|
||||
header('Location: '.$url);
|
||||
exit;
|
||||
}
|
||||
|
||||
$content = $form->returnForm();
|
||||
$content .= '
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover table-striped table-bordered table-condensed">
|
||||
<tr>
|
||||
<th>User</th>
|
||||
<th>URL</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
';
|
||||
|
||||
foreach ($list as $item) {
|
||||
$userInfo = api_get_user_info($item['user_id']);
|
||||
$userName = get_lang('Unknown');
|
||||
if (!empty($userInfo)) {
|
||||
$userName = $userInfo['complete_name_with_username'].' - '.$item['user_id'];
|
||||
}
|
||||
$content .= '<tr>';
|
||||
$content .= '<td>'.$userName.'</td>';
|
||||
$content .= '<td>'.$item['url'].'</td>';
|
||||
$content .= '<td><a class="btn btn-danger" href="'.$url.'?id='.$item['id'].'">Delete</a></td>';
|
||||
$content .= '</tr>';
|
||||
}
|
||||
|
||||
$content .= '
|
||||
</table>
|
||||
</div>';
|
||||
|
||||
$tpl = new Template(
|
||||
'',
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false
|
||||
);
|
||||
$tpl->assign('content', $content);
|
||||
$tpl->display_one_col_template();
|
||||
10
plugin/redirection/config.php
Normal file
10
plugin/redirection/config.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Config the plugin.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
require_once __DIR__.'/../../main/inc/global.inc.php';
|
||||
2
plugin/redirection/index.php
Normal file
2
plugin/redirection/index.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
12
plugin/redirection/install.php
Normal file
12
plugin/redirection/install.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Install.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
api_protect_admin_script();
|
||||
|
||||
RedirectionPlugin::create()->install();
|
||||
5
plugin/redirection/lang/english.php
Normal file
5
plugin/redirection/lang/english.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Custom redirection by user";
|
||||
$strings['plugin_comment'] = "Redirect specific users to any specific URL";
|
||||
9
plugin/redirection/lang/french.php
Normal file
9
plugin/redirection/lang/french.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
$strings['plugin_title'] = "Redirection personalisée par utilisateur";
|
||||
$strings['plugin_comment'] = "Rediriger des utilisateurs spécifiques vers des URLs spécifiques";
|
||||
5
plugin/redirection/lang/spanish.php
Normal file
5
plugin/redirection/lang/spanish.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
$strings['plugin_title'] = "Redirección personalizada";
|
||||
$strings['plugin_comment'] = "Redirecciona a una url personalizada a un usuario en concreto";
|
||||
13
plugin/redirection/plugin.php
Normal file
13
plugin/redirection/plugin.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
|
||||
/**
|
||||
* Plugin.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
|
||||
/* Plugin config */
|
||||
$plugin_info = RedirectionPlugin::create()->get_info();
|
||||
12
plugin/redirection/uninstall.php
Normal file
12
plugin/redirection/uninstall.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
/* For licensing terms, see /license.txt */
|
||||
/**
|
||||
* Uninstall the plugin.
|
||||
*
|
||||
* @author Enrique Alcaraz Lopez
|
||||
*
|
||||
* @package chamilo.plugin.redirection
|
||||
*/
|
||||
api_protect_admin_script();
|
||||
|
||||
RedirectionPlugin::create()->uninstall();
|
||||
Reference in New Issue
Block a user