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

Add sequence CRUD see BT#9893

Julio Montoya 9 éve
szülő
commit
f61a34f705

+ 4 - 1
composer.json

@@ -76,7 +76,10 @@
         "bower-asset/modernizr": "2.8.*",
         "bower-asset/jqueryui-timepicker-addon": "@stable",
         "bower-asset/imageMap-resizer": "0.5.3",
-        "bower-asset/simplewebrtc": "@stable"
+        "bower-asset/simplewebrtc": "@stable",
+        "clue/graph": "~0.9.0",
+        "graphp/graphviz": "~0.2.0",
+        "graphp/algorithms": "~0.8.0"
     },
     "require-dev": {
         "behat/behat": "2.5.*@stable",

+ 2 - 0
main/admin/index.php

@@ -345,7 +345,9 @@ if (api_is_platform_admin()) {
     if (is_dir(api_get_path(SYS_TEST_PATH) . 'datafiller/')) {
         $items[] = array('url' => 'filler.php', 'label' => get_lang('DataFiller'));
     }
+
     $items[] = array('url' => 'archive_cleanup.php', 'label' => get_lang('ArchiveDirCleanup'));
+    $items[] = array('url' => 'resource_sequence.php', 'label' => get_lang('ResourceSequencing'));
 
     if (isset($_configuration['db_manager_enabled']) &&
         $_configuration['db_manager_enabled'] == true &&

+ 31 - 0
main/admin/resource_sequence.php

@@ -0,0 +1,31 @@
+<?php
+/* For licensing terms, see /chamilo_license.txt */
+
+$cidReset = true;
+
+require_once '../inc/global.inc.php';
+
+$tpl = new Template(get_lang('ResourceSequencing'));
+$layout = $tpl->get_template('admin/resource_sequence.tpl');
+$form = new FormValidator('');
+$sessionList = SessionManager::get_sessions_list();
+
+if (!empty($sessionList)) {
+    //$sessionList[] = ['name' => get_lang('PleaseSelect'), 'id' => 0];
+    $sessionList = array_column($sessionList, 'name', 'id');
+}
+
+$form->addHidden('sequence_type', 'session');
+$form->addSelect(
+    'sessions',
+    get_lang('Sessions'),
+    $sessionList,
+    ['id' => 'item', 'multiple' => 'multiple']
+);
+$form->addButtonNext(get_lang('UseAsReference'), 'use_as_reference');
+$form->addButtonCreate(get_lang('SetAsRequirementForSelected'), 'set_requirement');
+$form->addButtonSave(get_lang('Save'), 'save_resource');
+
+$tpl->assign('left_block', $form->returnForm());
+$tpl->display($layout);
+

+ 188 - 0
main/inc/ajax/sequence.ajax.php

@@ -0,0 +1,188 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Responses to AJAX calls
+ */
+
+use Chamilo\CoreBundle\Entity\SequenceResource;
+use Fhaculty\Graph\Graph;
+use Fhaculty\Graph\Vertex;
+use Graphp\GraphViz\GraphViz;
+
+require_once '../global.inc.php';
+
+api_protect_admin_script();
+
+$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
+$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : null;
+$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
+$manager = Database::getManager();
+$repository = $manager->getRepository('ChamiloCoreBundle:SequenceResource');
+switch ($action) {
+    case 'get_icon':
+        $link = '';
+        switch ($type) {
+            case 'session':
+                $showDelete = isset($_REQUEST['show_delete']) ? $_REQUEST['show_delete'] : false;
+                $image = Display::return_icon('window_list.png');
+                $sessionInfo = api_get_session_info($id);
+                if (!empty($sessionInfo)) {
+
+
+                $linkDelete = '';
+                if ($showDelete) {
+                    $linkDelete = Display::url(
+                        get_lang('Delete'),
+                        '#',
+                        ['class' => 'delete_vertex', 'data-id' => $id]
+                    );
+                }
+
+                $link = '<div class="parent" data-id="'.$id.'">'.
+                    $image.' '.$sessionInfo['name'].$linkDelete.
+                    '</div>';
+                }
+                break;
+        }
+        echo $link;
+        break;
+    case 'delete_vertex':
+        $vertexId = isset($_REQUEST['vertex_id']) ? $_REQUEST['vertex_id'] : null;
+        /** @var SequenceResource $resource */
+        $resource = $repository->findOneByResourceId($id);
+
+        if (empty($resource)) {
+            exit;
+        }
+
+        $graph = $resource->getGraph();
+
+        if (!empty($graph)) {
+            /** @var Graph $graph */
+            $graph = unserialize($graph);
+            if ($graph->hasVertex($vertexId)) {
+                $vertex = $graph->getVertex($vertexId);
+                $vertex->destroy();
+
+                $resource->setGraph(serialize($graph));
+
+                $manager->persist($resource);
+                $manager->flush();
+            }
+        }
+
+        break;
+    case 'load_resource':
+        // children or parent
+        $loadResourceType = isset($_REQUEST['load_resource_type']) ? $_REQUEST['load_resource_type'] : null;
+        /** @var SequenceResource $resource */
+        $resource = $repository->findOneByResourceId($id);
+
+        if (empty($resource)) {
+            exit;
+        }
+
+        $graph = $resource->getGraph();
+
+        if (!empty($graph)) {
+            /** @var Graph $graph */
+            $graph = unserialize($graph);
+
+            $graphviz = new GraphViz();
+            //echo $graphviz->createImageHtml($graph);
+
+            /** @var Vertex $mainVertice */
+            if ($graph->hasVertex($id)) {
+                $mainVertice = $graph->getVertex($id);
+
+                if (!empty($mainVertice)) {
+                    $list = [];
+                    switch ($loadResourceType) {
+                        case 'parent':
+                            $verticeList = $mainVertice->getVerticesEdgeFrom();
+
+                            break;
+                        case 'children':
+                            $verticeList = $mainVertice->getVerticesEdgeTo();
+                            break;
+                    }
+                    foreach ($verticeList as $vertice) {
+                        $list[] = $vertice->getId();
+                    }
+
+                    if (!empty($list)) {
+                        echo implode(',', $list);
+                    }
+                }
+            }
+        }
+        break;
+    case 'save_resource':
+        $parents = isset($_REQUEST['parents']) ? $_REQUEST['parents'] : null;
+        $parents = str_replace($id, '', $parents);
+        $parents = explode(',', $parents);
+        $parents = array_filter($parents);
+
+        $graph = new Graph();
+
+        switch ($type) {
+            case 'session':
+                $sessionInfo = api_get_session_info($id);
+                $name = $sessionInfo['name'];
+
+                $main = $graph->createVertex($id);
+
+                foreach ($parents as $parentId) {
+                    $parent = $graph->createVertex($parentId);
+                    // Check if parent Id exists in the DB
+                    /** @var SequenceResource $resource */
+                    $resource = $repository->findOneByResourceId($parentId);
+                    if ($resource) {
+                        $parentGraph = $resource->getGraph();
+
+                        if (!empty($parentGraph)) {
+                            /** @var Graph $parentGraph */
+                            $parentGraph = unserialize($parentGraph);
+                            try {
+                                $vertex = $parentGraph->getVertex($parentId);
+                                $parentMain = $parentGraph->createVertex($id);
+                                $vertex->createEdgeTo($parentMain);
+                                $resource->setGraph(serialize($parentGraph));
+
+                                $manager->persist($resource);
+                                $manager->flush();
+/*
+                                $graphviz = new GraphViz();
+                                echo $graphviz->createImageHtml($parentGraph);*/
+                            } catch (Exception $e) {
+
+                            }
+                        }
+                    }
+
+                    $parent->createEdgeTo($main);
+                }
+
+                $graphviz = new GraphViz();
+                //echo $graphviz->createImageHtml($graph);
+
+                /** @var SequenceResource $sequence */
+                $sequence = $repository->findOneByResourceId($id);
+                if (empty($sequence)) {
+                    $sequence = new SequenceResource();
+                    $sequence
+                        ->setGraph(serialize($graph))
+                        ->setType(SequenceResource::SESSION_TYPE)
+                        ->setResourceId($id);
+                } else {
+                    $sequence->setGraph(serialize($graph));
+                }
+                $manager->persist($sequence);
+                $manager->flush();
+                break;
+        }
+
+
+        break;
+}

+ 139 - 0
main/template/default/admin/resource_sequence.tpl

@@ -0,0 +1,139 @@
+{% extends template ~ "/layout/layout_1_col.tpl" %}
+
+{% block content %}
+    <script>
+        var url = '{{ _p.web_ajax }}sequence.ajax.php';
+        var parentList = [];
+        var resourceId = 0;
+
+        $(document).ready(function() {
+            var type = $('input[name="sequence_type"]').val();
+
+            $('button[name="set_requirement"]').prop('disabled', true);
+
+            $('#parents').on('click', 'a', function() {
+                var vertexId = $(this).attr('data-id');
+                var parent = $(this).parent();
+
+                if (vertexId) {
+                    $.ajax({
+                        url: url + '?a=delete_vertex&id='+resourceId+'&vertex_id=' + vertexId + '&type=' + type,
+                        success: function (data) {
+                            parent.remove();
+                        }
+                    });
+                }
+            });
+
+            $('button[name="use_as_reference"]').click(function() {
+                $('button[name="set_requirement"]').prop('disabled', false);
+
+                var id = $("#item option:selected" ).val();
+
+                // Cleaning parent list.
+                parentList = [];
+
+                // Check if data exists and load parents
+                $.ajax({
+                    url: url + '?a=load_resource&load_resource_type=parent&id=' + id + '&type='+type,
+                    success: function (data) {
+                        if (data) {
+                            var listLoaded = data.split(',');
+                            listLoaded.forEach(function(value) {
+                                $.ajax({
+                                    url: url + '?a=get_icon&id='+ value+'&type='+type+'&show_delete=1',
+                                    success:function(data){
+                                        $('#parents').append(data);
+                                        parentList.push(id);
+                                    }
+                                });
+                            });
+                        }
+                    }
+                });
+
+                // Check if data exists and load children
+                $.ajax({
+                    url: url + '?a=load_resource&load_resource_type=children&id=' + id + '&type='+type,
+                    success: function (data) {
+                        if (data) {
+                            var listLoaded = data.split(',');
+                            listLoaded.forEach(function(value) {
+                                $.ajax({
+                                    url: url + '?a=get_icon&id='+ value+'&type='+type,
+                                    success:function(data){
+                                        $('#children').append(data);
+                                    }
+                                });
+                            });
+                        }
+                    }
+                });
+
+                // Cleaning
+                $('#parents').html('');
+                $('#children').html('');
+
+                $.ajax({
+                    url: url + '?a=get_icon&id='+ id+'&type='+type,
+                    success:function(data){
+                        $('#resource').html(data);
+                        parentList.push(id);
+                        resourceId = id;
+                    }
+                });
+
+                return false;
+            });
+
+            $('button[name="set_requirement"]').click(function() {
+                $("#item option:selected" ).each(function() {
+                    var id = $(this).val();
+                    if ($.inArray(id, parentList) == -1) {
+                        $.ajax({
+                            url: url + '?a=get_icon&id=' + id + '&type=' + type,
+                            success: function (data) {
+                                $('#parents').append(data);
+                                parentList.push(id);
+                            }
+                        });
+                    }
+                });
+                return false;
+            });
+
+            $('button[name="save_resource"]').click(function() {
+                if (resourceId != 0) {
+                    var params = decodeURIComponent(parentList);
+                    $.ajax({
+                        url: url + '?a=save_resource&id=' + resourceId + '&parents=' + params+'&type='+type,
+                        success: function (data) {
+                            alert('saved');
+                        }
+                    });
+                }
+                return false;
+            });
+        });
+    </script>
+
+    <div class="row">
+        <div class="col-md-3">
+            {{ left_block }}
+        </div>
+        <div class="col-md-9">
+            <h3>
+                {{ 'ItemsTheReferenceDependsOn' | get_lang }}
+            </h3>
+            <div id="parents">
+            </div>
+            <h3>{{ 'Item' | get_lang }}</h3>
+            <div id="resource">
+            </div>
+            <h3>{{ 'Dependencies' | get_lang }}</h3>
+            <div id="children">
+            </div>
+            {{ right_block }}
+        </div>
+    </div>
+{% endblock %}

+ 17 - 0
src/Chamilo/CoreBundle/Entity/Manager/SequenceManager.php

@@ -0,0 +1,17 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\CoreBundle\Entity\Manager;
+
+use Sonata\CoreBundle\Model\BaseEntityManager;
+
+/**
+ * Class SequenceManager
+ * CRUD for the course
+ *
+ * @package Chamilo\CoreBundle\Entity\Repository
+ */
+class SequenceManager extends BaseEntityManager
+{
+
+}

+ 18 - 0
src/Chamilo/CoreBundle/Entity/Repository/SequenceRepository.php

@@ -0,0 +1,18 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\CoreBundle\Entity\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+/**
+ * Class SequenceRepository
+ * The functions inside this class must return an instance of QueryBuilder
+ *
+ * @package Chamilo\CoreBundle\Entity\Repository
+ */
+class SequenceRepository extends EntityRepository
+{
+
+
+}

+ 114 - 0
src/Chamilo/CoreBundle/Entity/SequenceResource.php

@@ -0,0 +1,114 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\CoreBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * Class SequenceResource
+ * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Entity\Repository\SequenceRepository")
+ * @ORM\Table(name="sequence_resource")
+ * @ORM\Entity
+ */
+class SequenceResource
+{
+    const COURSE_TYPE = 1;
+    const SESSION_TYPE = 2;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue()
+     */
+    private $id;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="type", type="integer")
+     */
+    private $type;
+
+    /**
+     * @var integer
+     *
+     * @ORM\Column(name="resource_id", type="integer")
+     */
+    private $resourceId;
+
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="graph", type="text")
+     */
+    private $graph;
+
+    /**
+     * Get id
+     *
+     * @return integer
+     */
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * @return string
+     */
+    public function getType()
+    {
+        return $this->type;
+    }
+
+    /**
+     * @param string $type
+     * @return SequenceResource
+     */
+    public function setType($type)
+    {
+        $this->type = $type;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getGraph()
+    {
+        return $this->graph;
+    }
+
+    /**
+     * @param string $graph
+     * @return SequenceResource
+     */
+    public function setGraph($graph)
+    {
+        $this->graph = $graph;
+
+        return $this;
+    }
+
+    /**
+     * @return int
+     */
+    public function getResourceId()
+    {
+        return $this->resourceId;
+    }
+
+    /**
+     * @param int $resourceId
+     */
+    public function setResourceId($resourceId)
+    {
+        $this->resourceId = $resourceId;
+    }
+
+
+}