Forráskód Böngészése

Adding position in the Lp category table using the sortable doctrine extensions see BT#5763

Julio Montoya 12 éve
szülő
commit
e7821f1ef8

+ 22 - 4
main/inc/Entity/EntityCLpCategory.php

@@ -2,6 +2,8 @@
 
 namespace Entity;
 
+use Gedmo\Mapping\Annotation as Gedmo;
+
 use Doctrine\ORM\Mapping as ORM;
 use Doctrine\ORM\Mapping\Id;
 use Doctrine\ORM\Mapping\Column;
@@ -12,13 +14,13 @@ use Doctrine\ORM\Mapping\GeneratedValue;
  * EntityCBlog
  *
  * @Table(name="c_lp_category")
- * @Entity
+ * @Entity(repositoryClass="\Gedmo\Sortable\Entity\Repository\SortableRepository")
  */
 class EntityCLpCategory
 {
     /**
      * @var integer
-     *
+     * @Gedmo\SortableGroup
      * @Column(name="c_id", type="integer")
      */
     private $cId;
@@ -39,6 +41,12 @@ class EntityCLpCategory
      */
     private $name;
 
+    /**
+     * @Gedmo\SortablePosition
+     * @Column(name="position", type="integer")
+     */
+    private $position;
+
     /**
      * Set cId
      *
@@ -85,7 +93,7 @@ class EntityCLpCategory
     }
 
     /**
-     * Set blogName
+     * Set category name
      *
      * @param string $blogName
      * @return EntityCBlog
@@ -98,7 +106,7 @@ class EntityCLpCategory
     }
 
     /**
-     * Get blogName
+     * Get category name
      *
      * @return string
      */
@@ -106,4 +114,14 @@ class EntityCLpCategory
     {
         return $this->name;
     }
+
+    public function setPosition($position)
+    {
+        $this->position = $position;
+    }
+
+    public function getPosition()
+    {
+        return $this->position;
+    }
 }

+ 5 - 5
main/inc/global.inc.php

@@ -29,7 +29,7 @@ $starttime = $mtime;
 define('START', $starttime);
 
 // Showing/hiding error codes in global error messages.
-define('SHOW_ERROR_CODES', false);
+//define('SHOW_ERROR_CODES', false);
 
 // Determine the directory path where this current file lies.
 // This path will be useful to include the other intialisation files.
@@ -404,12 +404,12 @@ $app->register(new ChamiloServiceProvider(), array());
 //Manage error messages
 use Symfony\Component\HttpKernel\Exception\HttpException;
 
+
 $app->error(
     function (\Exception $e, $code) use ($app) {
         if ($app['debug']) {
             //return;
         }
-
         if (isset($code)) {
             switch ($code) {
                 case 404:
@@ -792,8 +792,8 @@ if (is_array($language_files)) {
 }
 
 
-//error_reporting(-1);
-error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
+
+//error_reporting(E_COMPILE_ERROR | E_ERROR | E_CORE_ERROR);
 
 if (api_get_setting('server_type') == 'test') {
     //error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
@@ -845,7 +845,7 @@ if (api_get_setting('server_type') == 'test') {
         }*/
 }
 
-
+error_reporting(-1);
 //Filters
 $app->before(
     function () use ($app) {

+ 1 - 1
main/install/db_main.sql

@@ -3084,4 +3084,4 @@ CREATE TABLE branch_transaction (
 );
 
 -- Do not move this
-UPDATE settings_current SET selected_value = '1.10.0.21541' WHERE variable = 'chamilo_database_version';
+UPDATE settings_current SET selected_value = '1.10.0.21566' WHERE variable = 'chamilo_database_version';

+ 1 - 0
main/install/install.lib.php

@@ -3751,6 +3751,7 @@ function create_course_tables($course_db_name = null)
         id int unsigned NOT NULL auto_increment,
         $add_to_all_tables
         name VARCHAR(255),
+        position INT,
         PRIMARY KEY (id)
         )".$charset_clause;
     Database::query($sql);

+ 2 - 2
main/install/migrate-db-1.9.0-1.10.0-pre.sql

@@ -120,7 +120,7 @@ ALTER TABLE c_lp ADD COLUMN category_id INT unsigned NOT NULL default 0;
 ALTER TABLE c_lp ADD COLUMN max_attempts INT NOT NULL default 0;
 ALTER TABLE c_lp ADD COLUMN subscribe_users INT NOT NULL default 0;
 
-CREATE TABLE c_lp_category (id int unsigned NOT NULL auto_increment, c_id INT unsigned NOT NULL, name VARCHAR(255), PRIMARY KEY (id));
+CREATE TABLE c_lp_category (id int unsigned NOT NULL auto_increment, c_id INT unsigned NOT NULL, name VARCHAR(255), position INT, PRIMARY KEY (id));
 
 ALTER TABLE user MODIFY COLUMN hr_dept_id int unsigned NOT NULL default 0;
 
@@ -149,4 +149,4 @@ ALTER TABLE c_item_property MODIFY COLUMN end_visible datetime default NULL;
 INSERT INTO settings_current (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext, access_url_changeable) VALUES ('user_name_order', NULL, 'textfield', 'Platform', '', 'UserNameOrderTitle', 'UserNameOrderComment', NULL, NULL, 1);
 
 -- Do not move this
-UPDATE settings_current SET selected_value = '1.10.0.21541' WHERE variable = 'chamilo_database_version';
+UPDATE settings_current SET selected_value = '1.10.0.21566' WHERE variable = 'chamilo_database_version';

+ 45 - 2
main/newscorm/learnpath.class.php

@@ -10559,14 +10559,57 @@ EOD;
         }
     }
 
+    static function move_up_category($id) {
+        global $app;
+        $em = $app['orm.em'];
+        $item = $em->find('Entity\EntityCLpCategory', $id);
+        if ($item) {
+            $position = $item->getPosition() - 1;
+            $item->setPosition($position);
+            $em->persist($item);
+            $em->flush();
+        }
+    }
+
+    static function move_down_category($id) {
+        global $app;
+        $em = $app['orm.em'];
+        $item = $em->find('Entity\EntityCLpCategory', $id);
+        if ($item) {
+            $position = $item->getPosition() + 1;
+            $item->setPosition($position);
+            $em->persist($item);
+            $em->flush();
+        }
+    }
+    static function get_count_categories($course_id)
+    {
+        global $app;
+        if (empty($course_id)) {
+            return 0;
+        }
+        $em = $app['orm.em'];
+        $query = $em->createQuery('SELECT COUNT(u.id) FROM Entity\EntityCLpCategory u WHERE u.cId = :id');
+        $query->setParameter('id', $course_id);
+        return $query->getSingleScalarResult();
+    }
+
+
     static function get_categories($course_id)
     {
         global $app;
         $em = $app['orm.em'];
-        $items = $em->getRepository('Entity\EntityCLpCategory')->findBy(
+
+        //Default behaviour
+        /*$items = $em->getRepository('Entity\EntityCLpCategory')->findBy(
             array('cId' => $course_id),
             array('name' => 'ASC')
-        );
+        );*/
+
+        //Using doctrine extensions
+        $items = $em->getRepository('Entity\EntityCLpCategory')->getBySortableGroupsQuery(
+            array('cId' => $course_id)
+        )->getResult();
 
         return $items;
     }

+ 18 - 0
main/newscorm/lp_controller.php

@@ -318,6 +318,24 @@ switch ($action) {
         }
         require 'lp_add_category.php';
         break;
+    case 'move_up_category':
+        if (!$is_allowed_to_edit) {
+            api_not_allowed(true);
+        }
+        if (isset($_REQUEST['id'])) {
+            learnpath::move_up_category($_REQUEST['id']);
+        }
+        require 'lp_list.php';
+        break;
+    case 'move_down_category':
+        if (!$is_allowed_to_edit) {
+            api_not_allowed(true);
+        }
+        if (isset($_REQUEST['id'])) {
+            learnpath::move_down_category($_REQUEST['id']);
+        }
+        require 'lp_list.php';
+        break;
     case 'delete_lp_category':
         if (!$is_allowed_to_edit) {
             api_not_allowed(true);

+ 23 - 3
main/newscorm/lp_list.php

@@ -105,9 +105,11 @@ if ($is_allowed_to_edit) {
 
 $categories_temp = learnpath::get_categories(api_get_course_int_id());
 
-$category_test = new Entity\EntityCLpCategory;
+$category_test = new Entity\EntityCLpCategory();
 $category_test->setId(0);
 $category_test->setName(get_lang('NoCategory'));
+$category_test->setPosition(0);
+
 $categories = array(
     $category_test
 );
@@ -119,7 +121,8 @@ if (!empty($categories_temp)) {
 $test_mode = api_get_setting('server_type');
 
 $lp_showed = false;
-
+$total = count($categories);
+$counter = 1;
 foreach ($categories as $item) {
     $list = new LearnpathList(api_get_user_id(), null, null, null, false, $item->getId());
     $flat_list = $list->get_flat_list();
@@ -130,9 +133,26 @@ foreach ($categories as $item) {
         $url = 'lp_controller.php?'.api_get_cidreq().'&action=add_lp_category&id='.$item->getId();
         $edit_link = Display::url(Display::return_icon('edit.png', get_lang('Edit')), $url);
         $delete_url = 'lp_controller.php?'.api_get_cidreq().'&action=delete_lp_category&id='.$item->getId();
+
+
+        $moveUpUrl = 'lp_controller.php?'.api_get_cidreq().'&action=move_up_category&id='.$item->getId();
+        $moveDownUrl = 'lp_controller.php?'.api_get_cidreq().'&action=move_down_category&id='.$item->getId();
+
+        if ($counter == 1) {
+            $moveUpLink = Display::return_icon('up_na.png', get_lang('Move'));
+        } else {
+            $moveUpLink = Display::url(Display::return_icon('up.png', get_lang('Move')), $moveUpUrl);
+        }
+        if (($total -1) == $counter) {
+            $moveDownLink = Display::return_icon('down_na.png', get_lang('Move'));
+        } else {
+            $moveDownLink = Display::url(Display::return_icon('down.png', get_lang('Move')), $moveDownUrl);
+        }
+
+        $counter++;
         $delete_link = Display::url(Display::return_icon('delete.png', get_lang('Delete')), $delete_url);
     }
-    echo Display::page_subheader2($item->getName().$edit_link.$delete_link);
+    echo Display::page_subheader2($item->getName().$edit_link.$moveUpLink.$moveDownLink.$delete_link);
 
     if (!empty($flat_list)) {
         echo '<table class="data_table">';