Browse Source

Add user related to category see BT#11081

Julio 9 years ago
parent
commit
83dcda110b

+ 2 - 1
main/inc/lib/usermanager.lib.php

@@ -1282,7 +1282,7 @@ class UserManager
 
     /**
      * Get a list of users of which the given conditions match with an = 'cond'
-     * @param array $conditions a list of condition (exemple : status=>STUDENT)
+     * @param array $conditions a list of condition (example : status=>STUDENT)
      * @param array $order_by a list of fields on which sort
      * @return array An array with all users of the platform.
      * @todo optional course code parameter, optional sorting parameters...
@@ -1312,6 +1312,7 @@ class UserManager
         }
         $sql_result = Database::query($sql_query);
         while ($result = Database::fetch_array($sql_result)) {
+            $result['complete_name'] = api_get_person_name($result['firstname'], $result['lastname']);
             $return_array[] = $result;
         }
         return $return_array;

+ 1 - 0
plugin/ticket/config.php

@@ -14,6 +14,7 @@ define('TABLE_TICKET_PRIORITY', 'plugin_ticket_priority');
 define('TABLE_TICKET_PROJECT', 'plugin_ticket_project');
 define('TABLE_TICKET_STATUS', 'plugin_ticket_status');
 define('TABLE_TICKET_TICKET', 'plugin_ticket_ticket');
+define('TABLE_TICKET_CATEGORY_REL_USER', 'plugin_ticket_category_rel_user');
 define('TABLE_TICKET_MESSAGE_ATTACHMENTS', 'plugin_ticket_message_attachments');
 
 /* Ticket status constants */

+ 7 - 0
plugin/ticket/database.php

@@ -220,6 +220,13 @@ $sql = "CREATE TABLE IF NOT EXISTS ".$table." (
         KEY FK_ticket_category (project_id,category_id))";
 Database::query($sql);
 
+$table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
+$sql = "CREATE TABLE IF NOT EXISTS ".$table." (
+        id int UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
+        category_id INT NOT NULL,
+        user_id INT NOT NULL
+)";
+
 //Menu main tabs
 $rsTab = $objPlugin->addTab('Ticket', 'plugin/ticket/src/myticket.php');
 

+ 65 - 9
plugin/ticket/src/categories.php

@@ -27,7 +27,7 @@ unset($_SESSION['this_section']);
 $table = new SortableTable(
     'TicketCategories',
     array('TicketManager', 'getCategoriesCount'),
-    array('TicketManager', 'get_all_tickets_categories'),
+    array('TicketManager', 'getCategories'),
     1
 );
 
@@ -35,13 +35,38 @@ if ($table->per_page == 0) {
     $table->per_page = 20;
 }
 
+$formToString = '';
+$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
+
 if (isset($_GET['action'])) {
     global $table;
     $action = $_GET['action'];
     switch ($action) {
-        case 'assign':
-            if ($isAdmin && isset($_GET['ticket_id']))
-                TicketManager::assign_ticket_user($_GET['ticket_id'], $user_id);
+
+        case 'delete':
+            TicketManager::deleteCategory($id);
+
+            header("Location: ".api_get_self());
+            break;
+        case 'edit':
+            $form = new FormValidator('category', 'post', api_get_self().'?action=edit&id='.$id);
+            $form->addText('name', get_lang('Nsme'));
+            $form->addHtmlEditor('description', get_lang('Description'));
+            $form->addButtonUpdate(get_lang('Update'));
+            $cat = TicketManager::getCategory($_GET['id']);
+            $form->setDefaults($cat);
+            $formToString = $form->returnForm();
+            if ($form->validate()) {
+                $values =$form->getSubmitValues();
+
+                $params = [
+                    'name' => $values['name'],
+                    'description' => $values['description'],
+                ];
+                $cat = TicketManager::updateCategory($_GET['id'], $params);
+
+                header("Location: ".api_get_self());
+            }
             break;
         default:
             break;
@@ -51,13 +76,44 @@ if (isset($_GET['action'])) {
 $user_id = api_get_user_id();
 $isAdmin = api_is_platform_admin();
 
-Display::display_header($plugin->get_lang('MyTickets'));
 
-$table->set_header(0, $plugin->get_lang('Title'), true);
-$table->set_header(1, get_lang('Description'), true, array("style" => "width:200px"));
-$table->set_header(2, $plugin->get_lang('TotalTickets'), true);
-$table->set_header(3, get_lang('Actions'), true);
+/**
+ * Build the modify-column of the table
+ * @param   int     The user id
+ * @param   string  URL params to add to table links
+ * @param   array   Row of elements to alter
+ * @return string Some HTML-code with modify-buttons
+ */
+function modify_filter($id, $params, $row)
+{
+    $result = Display::url(
+        Display::return_icon('edit.png', get_lang('Edit')),
+        "categories.php?action=edit&id={$row['id']}"
+    );
+
+    $result .= Display::url(
+        Display::return_icon('user.png', get_lang('AssignUser')),
+        "categories_add_user.php?id={$row['id']}"
+    );
+
+    $result .= Display::url(
+        Display::return_icon('delete.png', get_lang('Delete')),
+        "categories.php?action=delete&id={$row['id']}"
+    );
+
+	return $result;
+}
 
+
+Display::display_header($plugin->get_lang('MyTickets'));
+$table->set_header(0, '', false);
+$table->set_header(1, $plugin->get_lang('Title'), true);
+$table->set_header(2, get_lang('Description'), true, array("style" => "width:200px"));
+$table->set_header(3, $plugin->get_lang('TotalTickets'), true);
+$table->set_header(4, get_lang('Actions'), true);
+$table->set_column_filter(4, 'modify_filter');
+
+echo $formToString;
 echo $table->return_table();
 
 Display::display_footer();

+ 52 - 0
plugin/ticket/src/categories_add_user.php

@@ -0,0 +1,52 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ *
+ * @package chamilo.plugin.ticket
+ */
+
+require_once '../config.php';
+$plugin = TicketPlugin::create();
+
+api_protect_admin_script(true);
+
+$categoryId = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
+if (empty($categoryId)) {
+    api_not_allowed(true);
+}
+
+$form = new FormValidator('edit', 'post', api_get_self().'?id='.$categoryId);
+
+
+$users = UserManager::get_user_list([], ['firstname']);
+
+$users = array_column($users, 'complete_name', 'user_id');
+
+$form->addElement(
+    'advmultiselect',
+    'users',
+    get_lang('Users'),
+    $users,
+    'style="width: 280px;"'
+);
+
+$usersAdded = TicketManager::getUsersInCategory($categoryId);
+if (!empty($usersAdded)) {
+    $usersAdded = array_column($usersAdded, 'user_id');
+}
+
+$form->setDefaults(['users' => $usersAdded]);
+// submit button
+$form->addButtonSave(get_lang('Save'));
+
+if ($form->validate()) {
+    $values = $form->exportValues();
+    TicketManager::deleteAllUserInCategory($categoryId);
+    TicketManager::addUsersToCategory($categoryId, $values['users']);
+    Display::addFlash(Display::return_message(get_lang('Updated')));
+    header("Location: ".api_get_self()."?id=".$categoryId);
+    exit;
+}
+Display::display_header();
+$form->display();

+ 154 - 0
plugin/ticket/src/ticket.class.php

@@ -35,6 +35,159 @@ class TicketManager
         return $types;
     }
 
+    /**
+     * @param $from
+     * @param $numberItems
+     * @param $column
+     * @param $direction
+     * @return array
+     */
+    public static function getCategories($from, $numberItems, $column, $direction)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
+        $sql = "SELECT id, name, description, total_tickets
+                FROM $table";
+
+        if (!in_array($direction, array('ASC','DESC'))) {
+            $direction = 'ASC';
+        }
+        $column = intval($column);
+        $from 	= intval($from);
+        $numberItems = intval($numberItems);
+
+        //$sql .= " ORDER BY col$column $direction ";
+        $sql .= " LIMIT $from,$numberItems";
+
+        $result = Database::query($sql);
+        $types = array();
+        while ($row = Database::fetch_array($result)) {
+            $types[] = $row;
+        }
+
+        return $types;
+    }
+
+    /**
+     * @param int $id
+     * @return array|mixed
+     */
+    public static function getCategory($id)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
+        $id = intval($id);
+        $sql = "SELECT id, name, description, total_tickets
+                FROM $table WHERE id = $id";
+
+        $result = Database::query($sql);
+        $category = Database::fetch_array($result);
+
+        return $category;
+    }
+
+    /**
+     * @return int
+     */
+    public static function getCategoriesCount()
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
+
+        $sql = "SELECT count(id) count
+                FROM $table ";
+
+        $result = Database::query($sql);
+        $category = Database::fetch_array($result);
+
+        return $category['count'];
+    }
+
+    /**
+     * @param int $id
+     * @param array $params
+     */
+    public static function updateCategory($id, $params)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
+        $id = intval($id);
+         Database::update($table, $params, ['id = ?' => $id]);
+    }
+
+    /**
+     * @param int $id
+     */
+    public static function deleteCategory($id)
+    {
+        $id = intval($id);
+
+        $table = Database::get_main_table(TABLE_TICKET_TICKET);
+        $sql = "UPDATE $table SET category_id = NULL WHERE category_id = $id";
+        Database::query($sql);
+
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY);
+        $sql = "DELETE FROM $table WHERE id = $id";
+        Database::query($sql);
+    }
+
+    /**
+     * @param int $categoryId
+     * @param array $users
+     */
+    public static function addUsersToCategory($categoryId, $users)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
+        if (empty($users) || empty($categoryId)) {
+            return false;
+        }
+
+        foreach ($users as $userId) {
+            if (self::userIsAssignedToCategory($userId, $categoryId) == false) {
+                $params = [
+                    'category_id' => $categoryId,
+                    'user_id' => $userId
+                ];
+                Database::insert($table, $params);
+            }
+        }
+    }
+
+    /**
+     * @param int $userId
+     * @param int $categoryId
+     */
+    public static function userIsAssignedToCategory($userId, $categoryId)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
+        $userId = intval($userId);
+        $categoryId = intval($categoryId);
+        $sql = "SELECT * FROM $table WHERE category_id = $categoryId AND user_id = $userId";
+        $result = Database::query($sql);
+
+        return Database::num_rows($result) > 0;
+    }
+
+    /**
+     * @param int $categoryId
+     */
+    public static function getUsersInCategory($categoryId)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
+        $categoryId = intval($categoryId);
+        $sql = "SELECT * FROM $table WHERE category_id = $categoryId";
+        $result = Database::query($sql);
+
+        return Database::store_result($result);
+    }
+
+    /**
+     * @param int $categoryId
+     */
+    public static function deleteAllUserInCategory($categoryId)
+    {
+        $table = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
+        $categoryId = intval($categoryId);
+        $sql = "DELETE FROM $table WHERE category_id = $categoryId";
+        Database::query($sql);
+    }
+
     /**
      * Get all possible tickets statuses
      * @return array
@@ -1551,4 +1704,5 @@ class TicketManager
 
         return $tickets;
     }
+
 }