浏览代码

Adding delete/edit global category for question admin

Julio Montoya 12 年之前
父节点
当前提交
fe8b0e3141

+ 15 - 5
main/exercice/testcategory.class.php

@@ -218,7 +218,7 @@ class Testcategory
         //Only admins can delete global categories
         $courseId = $category->getCId();
         //Only admins can delete global categories
-        if (empty($courseId) && !api_is_platform_admin()) {
+        if (empty($courseId) && !api_is_platform_admin() || api_is_question_manager()) {
             return false;
         }
 
@@ -1146,14 +1146,24 @@ class Testcategory
                     $this->create_tree_array($array, $array[$i]['iid'], $depth, $tmp);
                 }
             }
-
         }
     }
 
-    public function editForm(& $form) {
+    public function getForm(& $form, $action = 'new') {
+
+        switch($action) {
+            case 'new':
+                $header = get_lang('AddACategory');
+                $submit = get_lang('AddTestCategory');
+                break;
+            case 'edit':
+                $header = get_lang('EditCategory');
+                $submit = get_lang('ModifyCategory');
+                break;
+        }
 
          // settting the form elements
-        $form->addElement('header', get_lang('EditCategory'));
+        $form->addElement('header', $header);
         $form->addElement('hidden', 'category_id');
         $form->addElement('text', 'category_name', get_lang('CategoryName'), array('class' => 'span6'));
         $form->add_html_editor('category_description', get_lang('CategoryDescription'), false, false, array('ToolbarSet' => 'test_category', 'Width' => '90%', 'Height' => '200'));
@@ -1168,7 +1178,7 @@ class Testcategory
         $form->addElement('html', $script);
 
         $form->addElement('select', 'parent_id', get_lang('Parent'), $category_parent_list, array('id' => 'parent_id'));
-        $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyCategory'), 'class="add"');
+        $form->addElement('style_submit_button', 'SubmitNote', $submit, 'class="add"');
 
         // setting the defaults
         $defaults = array();

+ 1 - 1
main/exercice/tests_category.php

@@ -118,7 +118,7 @@ function edit_category_form($in_action, $type = 'simple') {
         // initiate the object
         $form = new FormValidator('note', 'post', api_get_self().'?action='.$in_action.'&category_id='.$category_id."&type=".$type);
 
-        $objcat->editForm($form);
+        $objcat->getForm($form, 'edit');
 
         // The validation or display
         if ($form->validate()) {

+ 14 - 0
main/inc/routes.php

@@ -515,6 +515,20 @@ $app->match('/admin/questionmanager/categories/{id}/edit', 'question_manager.con
     ->before($adminAndQuestionManagerCondition)
     ->bind('admin_category_edit');
 
+$app->match('/admin/questionmanager/categories/{id}', 'question_manager.controller:showCategoryAction', 'GET')
+    ->assert('id', '\d+')
+    ->assert('type', '.+')
+    ->before($adminAndQuestionManagerCondition)
+    ->bind('admin_category_show');
+
+$app->match('/admin/questionmanager/categories/new', 'question_manager.controller:newCategoryAction', 'GET|POST')
+    ->before($adminAndQuestionManagerCondition)
+    ->bind('admin_category_new');
+
+$app->match('/admin/questionmanager/categories/{id}/delete', 'question_manager.controller:deleteCategoryAction', 'POST')
+    ->before($adminAndQuestionManagerCondition)
+    ->bind('admin_category_delete');
+
 /** Editor */
 $app->match('/editor/filemanager', 'editor.controller:filemanagerAction', 'GET|POST')
     ->assert('type', '.+')

+ 3 - 3
main/template/default/admin/questionmanager/questionmanager.tpl

@@ -9,11 +9,11 @@
                         <a href="{{ app.url_generator.generate('admin_questions') }}">
                         {{ 'Questions' | get_lang }}</a>
                     </li>
-                    <li><a href="user_list.php">{{ 'Exercises' | get_lang }}</a></li>
-                    <li><a href="user_add.php">{{ 'Categories' | get_lang }}</a></li>
+                    <li><a href="user_list.php">{{ 'ExtraFields' | get_lang }}</a></li>
+                    <li><a href="{{ app.url_generator.generate('admin_category_new')}}">{{ 'AddACategory' | get_lang }}</a></li>
                 </ul>
 
             </div>
         </div>
     </div>
-{% endblock %}
+{% endblock %}

+ 6 - 0
main/template/default/admin/questionmanager/questions.tpl

@@ -122,11 +122,17 @@
 <div class="questions">
     {% if category %}
         <h3>{{  category.title }}
+
         {% if category.cId == 0 %}
             <a href="{{ app.url_generator.generate('admin_category_edit', {id : category.iid}) }}">
                 <img src="{{ "edit.png"|icon(22) }}">
             </a>
         {% endif %}
+        {% if category_children == 0 %}
+            &nbsp; <a href="{{ app.url_generator.generate('admin_category_delete', {id : category.iid}) }}">
+                <img src="{{ "delete.png"|icon(22) }}">
+            </a>
+        {% endif %}
         </h3>
     {% endif %}
     <div class="clear"></div>

+ 7 - 0
main/template/default/admin/questionmanager/show_category.tpl

@@ -0,0 +1,7 @@
+{% extends app.template_style ~ "/layout/layout_1_col.tpl" %}
+{% block content %}
+
+<h3>{{ category.title }}</h3>
+<p>{{ category.description}}</p>
+
+{% endblock %}

+ 93 - 5
src/ChamiloLMS/Controller/Admin/QuestionManager/QuestionManagerController.php

@@ -173,7 +173,8 @@ class QuestionManagerController
         }';
 
         $js = \Display::grid_js('questions', $url, $columns, $columnModel, $extraParams, array(), $actionLinks, true);
-
+        $count = $repo->childCount($category);
+        $app['template']->assign('category_children', $count);
         $app['template']->assign('category', $category);
         $app['template']->assign('grid', $grid);
         $app['template']->assign('js', $js);
@@ -262,7 +263,7 @@ class QuestionManagerController
             ->createQueryBuilder()
             ->select('node')
             ->from('Entity\CQuizCategory', 'node')
-            ->where('node.cId = 0')
+            ->where('node.cId = 0 AND node.lvl = 0')
             ->orderBy('node.root, node.lft', 'ASC')
             ->getQuery();
 
@@ -274,6 +275,94 @@ class QuestionManagerController
 
     }
 
+    /**
+    * New category
+    *
+    * @param Application $app
+    * @param int $id
+    * @return Response
+    */
+    public function newCategoryAction(Application $app)
+    {
+        $extraJS = array();
+        //@todo improve this JS includes should be added using twig
+        $extraJS[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
+        $extraJS[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript"></script>';
+        $app['extraJS'] = $extraJS;
+
+        $url = $app['url_generator']->generate('admin_category_new');
+        $form = new \FormValidator('new', 'post', $url);
+
+        $objcat = new \Testcategory();
+        $objcat->getForm($form, 'new');
+        $message = null;
+        if ($form->validate()) {
+            $values = $form->getSubmitValues();
+            $parent_id = isset($values['parent_id']) && isset($values['parent_id'][0]) ? $values['parent_id'][0] : null;
+            $objcat = new \Testcategory(0, $values['category_name'], $values['category_description'], $parent_id, 'global');
+            $categoryId = $objcat->addCategoryInBDD();
+            if ($categoryId) {
+                $message = \Display::return_message(get_lang('AddCategoryDone'), 'confirmation');
+                $url = $app['url_generator']->generate('admin_category_show', array('id' => $categoryId));
+                return $app->redirect($url);
+            } else {
+                $message = \Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning');
+            }
+        }
+        //$app['template']->assign('message', $message);
+
+        $app['template']->assign('form', $form->toHtml());
+        $response = $app['template']->render_template('admin/questionmanager/edit_category.tpl');
+
+        return new Response($response, 200, array());
+    }
+
+    /**
+     * @param Application $app
+     * @param int $id
+     */
+    public function deleteCategoryAction(Application $app, $id)
+    {
+        $repo = $app['orm.em']->getRepository('Entity\CQuizCategory');
+        $category = $repo->find($id);
+        if (empty($category)) {
+            $app->abort(404);
+        }
+        $count = $repo->childCount($category);
+
+        if ($count == 0) {
+            $objcat = new \Testcategory($id);
+            $objcat->removeCategory();
+            $url = $app['url_generator']->generate('admin_questions');
+            return $app->redirect($url);
+        } else {
+            $app->abort(401);
+        }
+    }
+
+
+    /**
+     * Show category
+     *
+     * @param Application $app
+     * @param $id
+     * @return Response
+     */
+    public function showCategoryAction(Application $app, $id)
+    {
+        $objcat = new \Testcategory($id);
+
+        if (!empty($objcat->c_id)) {
+            $app->abort(401);
+        }
+
+        $app['template']->assign('category', $objcat);
+
+        $response = $app['template']->render_template('admin/questionmanager/show_category.tpl');
+
+        return new Response($response, 200, array());
+    }
+
     /**
      * Edit category
      *
@@ -291,13 +380,13 @@ class QuestionManagerController
 
         $objcat = new \Testcategory($id);
 
-        if (!empty($objcat->c_id)) {
+        if (!empty($objcat->c_id) || empty($objcat->id)) {
             $app->abort(401);
         }
         $url = $app['url_generator']->generate('admin_category_edit', array('id' => $id));
         $form = new \FormValidator('edit', 'post', $url);
 
-        $objcat->editForm($form);
+        $objcat->getForm($form, 'edit');
         $message = null;
         if ($form->validate()) {
             $values = $form->getSubmitValues();
@@ -313,6 +402,5 @@ class QuestionManagerController
         $response = $app['template']->render_template('admin/questionmanager/edit_category.tpl');
 
         return new Response($response, 200, array());
-
     }
 }