Skip to content

Commit 77617a2

Browse files
committed
Feat:(First version plugin)
1 parent c956301 commit 77617a2

File tree

5 files changed

+195
-85
lines changed

5 files changed

+195
-85
lines changed

front/myobject.form.php

+44-44
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
<?php
22
include ("../../../inc/includes.php");
33

4-
// Check if plugin is activated...
5-
$plugin = new Plugin();
6-
if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {
7-
Html::displayNotFoundError();
8-
}
4+
// // Check if plugin is activated...
5+
// $plugin = new Plugin();
6+
// if (!$plugin->isInstalled('myexampleplugin') || !$plugin->isActivated('myexampleplugin')) {
7+
// Html::displayNotFoundError();
8+
// }
99

10-
$object = new PluginMyExampleMyObject();
10+
// $object = new PluginMyExampleMyObject();
1111

12-
if (isset($_POST['add'])) {
13-
//Check CREATE ACL
14-
$object->check(-1, CREATE, $_POST);
15-
//Do object creation
16-
$newid = $object->add($_POST);
17-
//Redirect to newly created object form
18-
Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.form.php?id=$newid");
19-
} else if (isset($_POST['update'])) {
20-
//Check UPDATE ACL
21-
$object->check($_POST['id'], UPDATE);
22-
//Do object update
23-
$object->update($_POST);
24-
//Redirect to object form
25-
Html::back();
26-
} else if (isset($_POST['delete'])) {
27-
//Check DELETE ACL
28-
$object->check($_POST['id'], DELETE);
29-
//Put object in dustbin
30-
$object->delete($_POST);
31-
//Redirect to objects list
32-
$object->redirectToList();
33-
} else if (isset($_POST['purge'])) {
34-
//Check PURGE ACL
35-
$object->check($_POST['id'], PURGE);
36-
//Do object purge
37-
$object->delete($_POST, 1);
38-
//Redirect to objects list
39-
Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.php");
40-
} else {
41-
//per default, display object
42-
$withtemplate = (isset($_GET['withtemplate']) ? $_GET['withtemplate'] : 0);
43-
$object->display(
44-
[
45-
'id' => $_GET['id'],
46-
'withtemplate' => $withtemplate
47-
]
48-
);
49-
}
12+
// if (isset($_POST['add'])) {
13+
// //Check CREATE ACL
14+
// $object->check(-1, CREATE, $_POST);
15+
// //Do object creation
16+
// $newid = $object->add($_POST);
17+
// //Redirect to newly created object form
18+
// Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.form.php?id=$newid");
19+
// } else if (isset($_POST['update'])) {
20+
// //Check UPDATE ACL
21+
// $object->check($_POST['id'], UPDATE);
22+
// //Do object update
23+
// $object->update($_POST);
24+
// //Redirect to object form
25+
// Html::back();
26+
// } else if (isset($_POST['delete'])) {
27+
// //Check DELETE ACL
28+
// $object->check($_POST['id'], DELETE);
29+
// //Put object in dustbin
30+
// $object->delete($_POST);
31+
// //Redirect to objects list
32+
// $object->redirectToList();
33+
// } else if (isset($_POST['purge'])) {
34+
// //Check PURGE ACL
35+
// $object->check($_POST['id'], PURGE);
36+
// //Do object purge
37+
// $object->delete($_POST, 1);
38+
// //Redirect to objects list
39+
// Html::redirect("{$CFG_GLPI['root_doc']}/plugins/front/myobject.php");
40+
// } else {
41+
// //per default, display object
42+
// $withtemplate = (isset($_GET['withtemplate']) ? $_GET['withtemplate'] : 0);
43+
// $object->display(
44+
// [
45+
// 'id' => $_GET['id'],
46+
// 'withtemplate' => $withtemplate
47+
// ]
48+
// );
49+
// }

front/score.php

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
include("../../../inc/includes.php");
3+
4+
// Check if plugin is activated...
5+
$plugin = new Plugin();
6+
if (!$plugin->isInstalled('gamification') || !$plugin->isActivated('gamification')) {
7+
Html::displayNotFoundError();
8+
}
9+
10+
function get_points()
11+
{
12+
global $DB;
13+
14+
$query = "
15+
select gpgs.score from glpi_plugin_gamification_score gpgs
16+
where user_id = " . Session::getLoginUserID() . "";
17+
18+
$return = $DB->query($query) or die("error" . $DB->error());
19+
$data = $DB->fetchArray($return);
20+
21+
return $data['score'];
22+
}
23+
24+
function plugin_check_score($score)
25+
{
26+
global $DB;
27+
$rank_name = 'N/A';
28+
29+
$query = "SELECT * FROM glpi_plugin_gamification_configs gpgc";
30+
$return = $DB->query($query);
31+
32+
$config = $DB->fetch_assoc($return);
33+
34+
foreach ($config as $key => $value) {
35+
if (strpos($key, 'level_') === 0) {
36+
if ($score <= $value) {
37+
$rank_name = plugin_return_rank_name($key);
38+
break;
39+
}
40+
}
41+
}
42+
43+
return $rank_name;
44+
}
45+
46+
function plugin_return_rank_name($key)
47+
{
48+
switch ($key) {
49+
case 'level_beginner':
50+
return 'Beginner';
51+
break;
52+
53+
case 'level_intermediate':
54+
return 'Intermediate';
55+
break;
56+
57+
case 'level_professional':
58+
return 'Professional';
59+
break;
60+
61+
case 'level_expert':
62+
return 'Expert';
63+
break;
64+
65+
case 'level_master':
66+
return 'Master';
67+
break;
68+
69+
case 'level_jedi_master':
70+
return 'Jedi Master';
71+
break;
72+
default:
73+
return 'N/A';
74+
break;
75+
};
76+
}
77+
78+
//check for ACLs
79+
if (PluginGamificationScore::canView()) {
80+
get_points();
81+
//Add page header
82+
Html::header("Gamification", $_SERVER['PHP_SELF'], "management", "plugingamificationscore", "");
83+
84+
?>
85+
<div class="ui-tabs-panel ui-corner-bottom ui-widget-content">
86+
<div class="spaced">
87+
<table class="tab_cadre_fixe">
88+
<tbody>
89+
<tr class="headerRow responsive_hidden">
90+
<th colspan="6" class="align-center">Score:</th>
91+
</tr>
92+
<tr>
93+
<th class="bg-white">
94+
<label>Fast (<1h)</label>
95+
</th>
96+
<td>
97+
<input style="font-weight: bold" disabled value="<?php echo get_points() ?>" type="text">
98+
</td>
99+
<th class="bg-white">
100+
<label>On Time</label>
101+
</th>
102+
<td>
103+
<input style="font-weight: bold" disabled value="<?php echo plugin_check_score(get_points()) ?>" name="late" type="text">
104+
</td>
105+
</tr>
106+
</tbody>
107+
</table>
108+
</div>
109+
</div>
110+
<?php
111+
Html::footer();
112+
} else {
113+
//View is not granted.
114+
Html::displayRightError();
115+
}

hook.php

+2-34
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function plugin_add_score_satisfaction($item)
109109
if (!empty($item->oldvalues)) {
110110
foreach ($item->oldvalues as $key => $value) {
111111
if (empty($value)) {
112-
if($key == 'satisfaction'){
112+
if ($key == 'satisfaction') {
113113
$empty++;
114114
}
115115
}
@@ -180,14 +180,9 @@ function plugin_get_configs()
180180
$query = "SELECT * FROM glpi_plugin_gamification_configs gpgc";
181181
$return = $DB->query($query) or die("error" . $DB->error());
182182

183-
return ($DB->fetchArray($return));
183+
return ($DB->fetch_row($return));
184184
}
185185

186-
function plugin_get_rank($score)
187-
{
188-
$config = plugin_get_configs();
189-
190-
}
191186
function plugin_check_time($item, $config)
192187
{
193188

@@ -242,31 +237,4 @@ function plugin_get_user_by_ticket_id($ticket_id)
242237
";
243238
$return = $DB->query($query) or die("error" . $DB->error());
244239
return ($DB->fetchArray($return));
245-
}
246-
247-
function plugin_example_display_central($item)
248-
{
249-
global $DB;
250-
251-
$query = "
252-
select gpgs.score from glpi_plugin_gamification_score gpgs
253-
where user_id = ".Session::getLoginUserID()."";
254-
255-
$return = $DB->query($query) or die("error" . $DB->error());
256-
$data = $DB->fetchArray($return);
257-
258-
// $config = plugin_get_configs();
259-
// echo '<pre>';
260-
// var_dump($config);
261-
// foreach($config as $key=>$value) {
262-
// var_dump($value);
263-
// }
264-
// echo '</pre>';
265-
// var_dump($config["agent_fast_ticket_solve"]);
266-
// die;
267-
echo "<tr><th colspan='2'>";
268-
echo "<div style='text-align:center; font-size:2em'>";
269-
echo __("Score: ".$data['score']);
270-
echo "</div>";
271-
echo "</th></tr>";
272240
}

inc/score.class.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
class PluginGamificationScore extends CommonDBTM {
3+
4+
static function getTypeName($nb = 0) {
5+
6+
return 'Gamification';
7+
}
8+
9+
static function canCreate() {
10+
if (isset($_SESSION["glpi_plugin_gamification_profile"])) {
11+
return ($_SESSION["glpi_plugin_gamification_profile"]['gamification'] == 'w');
12+
}
13+
return false;
14+
}
15+
16+
static function canView() {
17+
18+
if (isset($_SESSION["glpi_plugin_gamification_profile"])) {
19+
return ($_SESSION["glpi_plugin_gamification_profile"]['gamification'] == 'w'
20+
|| $_SESSION["glpi_plugin_gamification_profile"]['gamification'] == 'r');
21+
}
22+
return false;
23+
}
24+
}

setup.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ function plugin_init_gamification() {
1212

1313
//required!
1414
$PLUGIN_HOOKS['csrf_compliant']['gamification'] = true;
15-
16-
// $PLUGIN_HOOKS['item_add']['gamification'] = ['Computer' => ['PluginExampleExample',
17-
// 'item_add_test']];
18-
1915

16+
Plugin::registerClass('PluginGamificationScore');
17+
Plugin::registerClass('PluginCustomfieldsDropdowns');
18+
2019
$PLUGIN_HOOKS['item_update']['gamification'] = ['Ticket' => 'plugin_add_score',
2120
'TicketSatisfaction' => 'plugin_add_score_satisfaction'];
22-
23-
$PLUGIN_HOOKS['display_central']['gamification'] = "plugin_example_display_central";
2421

25-
// $PLUGIN_HOOKS['item_update']['gamification'] = ['TicketSatisfaction' => 'plugin_add_score_satisfaction'];
22+
$PLUGIN_HOOKS['item_update']['gamification'] = ['TicketSatisfaction' => 'plugin_add_score_satisfaction'];
2623

2724
if (Session::haveRight('config', UPDATE)) {
2825
$PLUGIN_HOOKS['config_page']['gamification'] = 'config.php';
2926
}
3027

28+
$_SESSION["glpi_plugin_gamification_profile"]['gamification'] = 'w';
29+
30+
if (isset($_SESSION["glpi_plugin_gamification_profile"])) {
31+
$PLUGIN_HOOKS['menu_toadd']['gamification'] = ['management' => 'PluginGamificationScore'];
32+
}
33+
3134
$PLUGIN_HOOKS['add_css']['gamification'] = 'style.css';
3235

3336
}

0 commit comments

Comments
 (0)