Browse Source

Merge branch 'chamilo10'

Julio Montoya 11 years ago
parent
commit
a5e6beb4c9

+ 103 - 6
main/exercice/exercise.class.php

@@ -99,6 +99,7 @@ class Exercise
     public $hideQuestionTitle = 0;
     public $scoreTypeModel = 0;
     public $categoryMinusOne = true; // Shows the category -1: See BT#6540
+    public $globalCategoryId = null;
 
     /**
      * Constructor of the class
@@ -132,6 +133,7 @@ class Exercise
         $this->questionSelectionType = EX_Q_SELECTION_ORDERED;
         $this->endButton = 0;
         $this->scoreTypeModel = 0;
+        $this->globalCategoryId = null;
 
         if (!empty($course_id)) {
             $course_info = api_get_course_info_by_id($course_id);
@@ -201,6 +203,7 @@ class Exercise
             $this->questionSelectionType = $object->question_selection_type;
             $this->hideQuestionTitle = $object->hide_question_title;
             $this->scoreTypeModel = $object->score_type_model;
+            $this->globalCategoryId = $object->global_category_id;
 
             $this->review_answers = (isset($object->review_answers) && $object->review_answers == 1) ? true : false;
             $sql = "SELECT max_score FROM $table_lp_item
@@ -549,6 +552,20 @@ class Exercise
     {
         $this->scoreTypeModel = intval($value);
     }
+
+    public function getGlobalCategoryId()
+    {
+        return $this->globalCategoryId;
+    }
+
+    public function setGlobalCategoryId($value)
+    {
+        if (is_array($value) && isset($value[0])) {
+            $value = $value[0];
+        }
+        $this->globalCategoryId = intval($value);
+    }
+
     /**
      *
      * @param int $start
@@ -1310,7 +1327,6 @@ class Exercise
 
         $id = $this->id;
         $exercise = $this->exercise;
-        $description = $this->description;
         $sound = $this->sound;
         $type = $this->type;
         $attempts = $this->attempts;
@@ -1350,7 +1366,7 @@ class Exercise
         if ($id) {
             $sql = "UPDATE $TBL_EXERCICES SET
 				    title='".Database::escape_string($exercise)."',
-					description='".Database::escape_string($description)."'";
+					description='".Database::escape_string($this->description)."'";
 
             if ($type_e != 'simple') {
                 $sql .= ",sound='".Database::escape_string($sound)."',
@@ -1375,12 +1391,13 @@ class Exercise
                     question_selection_type = '".$this->getQuestionSelectionType()."',
                     hide_question_title = '".$this->getHideQuestionTitle()."',
                     score_type_model = '".$this->getScoreTypeModel()."',
+                    global_category_id = '".$this->getGlobalCategoryId()."',
 					results_disabled='".Database::escape_string($results_disabled)."'";
             }
             $sql .= " WHERE iid = ".Database::escape_string($id)." AND c_id = {$this->course_id}";
             Database::query($sql);
 
-            // update into the item_property table
+            // Update into the item_property table
             api_item_property_update($_course, TOOL_QUIZ, $id, 'QuizUpdated', api_get_user_id());
 
             if (api_get_setting('search_enabled') == 'true') {
@@ -1392,13 +1409,13 @@ class Exercise
                         c_id, start_time, end_time, title, description, sound, type, random, random_answers, active,
                         max_attempt, feedback_type, expired_time, session_id, review_answers, random_by_category,
                         text_when_finished, display_category_name, pass_percentage, end_button, email_notification_template,
-                        results_disabled, model_type, question_selection_type, score_type_model, hide_question_title)
+                        results_disabled, model_type, question_selection_type, score_type_model, global_category_id, hide_question_title)
 					VALUES(
 						".$this->course_id.",
 						'$start_time',
                         '$end_time',
 						'".Database::escape_string($exercise)."',
-						'".Database::escape_string($description)."',
+						'".Database::escape_string($this->description)."',
 						'".Database::escape_string($sound)."',
 						'".Database::escape_string($type)."',
 						'".Database::escape_string($random)."',
@@ -1419,6 +1436,7 @@ class Exercise
                         '".Database::escape_string($this->getModelType())."',
                         '".Database::escape_string($this->getQuestionSelectionType())."',
                         '".Database::escape_string($this->getScoreTypeModel())."',
+                        '".Database::escape_string($this->getGlobalCategoryId())."',
                         '".Database::escape_string($this->getHideQuestionTitle())."'
 						)";
             Database::query($sql);
@@ -1998,15 +2016,90 @@ class Exercise
             );
             $form->addElement('html', '</div>');
 
-            // Pass percentage.
+
+              // Pass percentage.
             $form->addElement(
                 'text',
                 'pass_percentage',
                 array(get_lang('PassPercentage'), null, '%'),
                 array('id' => 'pass_percentage')
             );
+
             $form->addRule('pass_percentage', get_lang('Numeric'), 'numeric');
 
+
+
+            $url = api_get_path(WEB_AJAX_PATH).'exercise.ajax.php?1=1';
+
+
+            $js = '<script>
+
+            function check() {
+                var counter = 0;
+                $("#global_category_id option:selected").each(function() {
+                    var id = $(this).val();
+                    var name = $(this).text();
+                    if (id != "" ) {
+
+                        $.ajax({
+                            async: false,
+                            url: "'.$url.'&a=exercise_category_exists&type=global",
+                            data: "id="+id,
+                            success: function(return_value) {
+                                if (return_value == 0 ) {
+                                    alert("'.addslashes(get_lang('CategoryDoesNotExists')).'");
+                                    // Deleting select option tag
+                                    $("#global_category_id").find("option").remove();
+
+                                    $(".holder li").each(function () {
+                                        if ($(this).attr("rel") == id) {
+                                            $(this).remove();
+                                        }
+                                    });
+                                }
+                            },
+                        });
+                    }
+                    counter++;
+                });
+            }
+
+            $(function() {
+                $("#global_category_id").fcbkcomplete({
+                    json_url: "'.$url.'&a=search_category_parent&type=global&",
+                    maxitems: 1 ,
+                    addontab: false,
+                    input_min_size: 1,
+                    cache: false,
+                    complete_text:"'.get_lang('StartToType').'",
+                    firstselected: false,
+                    onselect: check,
+                    filter_selected: true,
+                    newel: true
+                });
+            });
+
+            </script>';
+            $form->addElement('html', $js);
+
+            $categoryJS = null;
+            $globalCategoryId = $this->getGlobalCategoryId();
+            if (!empty($globalCategoryId)) {
+                $cat = new Testcategory($globalCategoryId);
+                $trigger = '$("#global_category_id").trigger("addItem",[{ "title": "'.$cat->title.'", "value": "'.$globalCategoryId.'"}]);';
+                $categoryJS .= '<script>$(function() { '.$trigger.' });</script>';
+            }
+            $form->addElement('html', $categoryJS);
+
+             // Global category id.
+            $form->addElement(
+                'select',
+                'global_category_id',
+                array(get_lang('GlobalCategory')),
+                array(),
+                array('id' => 'global_category_id')
+            );
+
             // Text when ending an exam
             $form->add_html_editor('text_when_finished', get_lang('TextWhenFinished'), false, false, $editor_config);
 
@@ -2111,6 +2204,7 @@ class Exercise
                 $defaults['model_type'] = $this->getModelType();
                 $defaults['question_selection_type'] = $this->getQuestionSelectionType();
                 $defaults['score_type_model'] = $this->getScoreTypeModel();
+                //$defaults['global_category_id'] = $this->getScoreTypeModel();
 
                 $defaults['hide_question_title'] = $this->getHideQuestionTitle();
 
@@ -2170,6 +2264,7 @@ class Exercise
      */
     function processCreation($form, $type = '')
     {
+        $values = $form->exportValues();
         $this->updateTitle($form->getSubmitValue('exerciseTitle'));
         $this->updateDescription($form->getSubmitValue('exerciseDescription'));
         $this->updateAttempts($form->getSubmitValue('exerciseAttempts'));
@@ -2191,7 +2286,9 @@ class Exercise
         $this->setModelType($form->getSubmitValue('model_type'));
         $this->setQuestionSelectionType($form->getSubmitValue('question_selection_type'));
         $this->setHideQuestionTitle($form->getSubmitValue('hide_question_title'));
+        var_dump($values);
         $this->setScoreTypeModel($form->getSubmitValue('score_type_model'));
+        $this->setGlobalCategoryId($form->getSubmitValue('global_category_id'));
 
         if ($form->getSubmitValue('activate_start_date_check') == 1) {
             $start_time = $form->getSubmitValue('start_time');

+ 2 - 2
main/exercice/question.class.php

@@ -1387,8 +1387,8 @@ abstract class Question
 
             $(function() {
                 $("#category_id").fcbkcomplete({
-                    json_url: "'.$url.'&a=search_category_parent&type=all&",
-                    maxitems: "'.$maxCategories.'" ,
+                    json_url: "'.$url.'&a=search_category_parent&type=all&filter_by_global='.$this->exercise->getGlobalCategoryId().',
+                    maxitems: "'.$maxCategories.'",
                     addontab: false,
                     input_min_size: 1,
                     cache: false,

+ 27 - 25
main/exercice/testcategory.class.php

@@ -29,18 +29,19 @@ class Testcategory
      * @param string $in_name
      * @param string $in_description
      * @param int $parent_id
-     * @param string $type
+     * @param string $type Posible values: all, simple, global
      * @param int $course_id
+     * @param int visibility
      */
     public function Testcategory($in_id = 0, $in_name = '', $in_description = "", $parent_id = 0, $type = 'simple', $course_id = null, $visibility = 1)
     {
-		if ($in_id != 0 && $in_name == "") {
-			$tmpobj = new Testcategory();
-			$tmpobj->getCategory($in_id);
-			$this->id = $tmpobj->id;
-			$this->name = $tmpobj->name;
+        if ($in_id != 0 && $in_name == "") {
+            $tmpobj = new Testcategory();
+            $tmpobj->getCategory($in_id);
+            $this->id = $tmpobj->id;
+            $this->name = $tmpobj->name;
             $this->title = $tmpobj->name;
-			$this->description = $tmpobj->description;
+            $this->description = $tmpobj->description;
             $this->parent_id = $tmpobj->parent_id;
             $this->parent_path = $this->name;
             $this->c_id = $tmpobj->c_id;
@@ -50,22 +51,22 @@ class Testcategory
             if (!empty($tmpobj->parent_id)) {
                 $category = new Testcategory($tmpobj->parent_id);
                 $this->parent_path = $category->parent_path.' > '.$this->name;
-		    }
+            }
         } else {
-			$this->id = $in_id;
-			$this->name = $in_name;
-			$this->description = $in_description;
+            $this->id = $in_id;
+            $this->name = $in_name;
+            $this->description = $in_description;
             $this->parent_id = $parent_id;
             $this->visibility = $visibility;
         }
         $this->type = $type;
 
-        if (!empty($course_id))  {
+        if (!empty($course_id)) {
             $this->course_id = $course_id;
-		} else {
+        } else {
             $this->course_id = api_get_course_int_id();
         }
-	}
+    }
 
     /**
     * Return the Testcategory object with id=in_id
@@ -80,7 +81,7 @@ class Testcategory
         }
 
         $t_cattable = Database::get_course_table(TABLE_QUIZ_CATEGORY);
-        $id = Database::escape_string($id);
+        $id = intval($id);
         $sql = "SELECT * FROM $t_cattable WHERE iid = $id ";
         $res = Database::query($sql);
         $numRows = Database::num_rows($res);
@@ -119,14 +120,14 @@ class Testcategory
             return false;
         }
 
-		// Check if name already exists.
+        // Check if name already exists.
         $sql = "SELECT count(*) AS nb FROM $t_cattable WHERE title = '$v_name' $courseCondition";
-		$result = Database::query($sql);
-		$data = Database::fetch_array($result);
+        $result = Database::query($sql);
+        $data = Database::fetch_array($result);
 
         // lets add in BD if not the same name
         if ($data['nb'] <= 0) {
-            // @todo inject the app in the claas
+            // @todo inject the app in the class
             global $app;
             $category = new \Entity\CQuizCategory();
             $category->setTitle($this->name);
@@ -271,7 +272,7 @@ class Testcategory
             $where_condition = array(
                 "title $condition",
             );
-            $where_condition = ' AND  ('.implode(' OR ',  $where_condition).') ';
+            $where_condition = ' AND  ('.implode(' OR ', $where_condition).') ';
         }
 
         switch ($this->type) {
@@ -910,11 +911,12 @@ class Testcategory
         $html = null;
         if (!empty($new_category_list)) {
             switch ($type) {
-            case 'label':
-                $html = implode(' ', $new_category_list);
-                break;
-            case 'header':
-                $html = Display::page_subheader3(get_lang('Category').': '.implode(', ', $new_category_list));
+                case 'label':
+                    $html = implode(' ', $new_category_list);
+                    break;
+                case 'header':
+                    $html = Display::page_subheader3(get_lang('Category').': '.implode(', ', $new_category_list));
+                    break;
             }
         }
         return $html;

+ 25 - 1
main/inc/ajax/exercise.ajax.php

@@ -74,7 +74,8 @@ switch ($action) {
         }
         break;
     case 'exercise_category_exists':
-        $category = new Testcategory();
+        $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'simple';
+        $category = new Testcategory(null, null, null, null, $type);
         $category->getCategory($_REQUEST['id']);
         if (empty($category->id)) {
             echo 0;
@@ -100,16 +101,39 @@ switch ($action) {
         break;
     case 'search_category_parent':
         $type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 'simple';
+        $filterByGlobal = isset($_REQUEST['filter_by_global']) ? $_REQUEST['filter_by_global'] : null;
 
         $cat = new Testcategory(null, null, null, null, $type);
         $items = $cat->get_categories_by_keyword($_REQUEST['tag']);
 
         $courseId = api_get_course_int_id();
 
+        $em = $this->get('orm.em');
+        $repo = $em->getRepository('Entity\CQuizCategory');
+
         $json_items = array();
         if (!empty($items)) {
             foreach ($items as $item) {
                 if ($item['c_id'] == 0) {
+                    if ($filterByGlobal) {
+                        $cat = $em->find('Entity\CQuizCategory', $item['iid']);
+                        $idList = array();
+                        if ($cat) {
+                            $path = $repo->getPath($cat);
+                            if (!empty($path)) {
+                                /** @var Entity\CQuizCategory $cat */
+                                foreach ($path as $cat) {
+                                    $idList[] = $cat->getIid();
+                                }
+                            }
+                        }
+
+                        if (isset($idList) && !empty($idList)) {
+                            if (!in_array($filterByGlobal, $idList)) {
+                                continue;
+                            }
+                        }
+                    }
                     $item['title'] .= " [".get_lang('Global')."]";
                 } else {
                     if (isset($courseId)) {

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

@@ -680,11 +680,10 @@ $app->before(
 
         $app['translator.cache.enabled'] = false;
 
-        $app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
+        $app['translator'] = $app->share($app->extend('translator', function ($translator, $app) {
 
             $locale = $translator->getLocale();
 
-
             /** @var Symfony\Component\Translation\Translator $translator  */
             if ($app['translator.cache.enabled']) {
 
@@ -712,17 +711,15 @@ $app->before(
             }
         }));
 
-        // Setting course entity for controllers.
-
-        // The course parameter is loaded
-        $course = $request->get('course');
-
         // Check if we are inside a Chamilo course tool
         $isCourseTool = (strpos($request->getPathInfo(), 'courses/') === false) ? false : true;
 
         //$controllerName = $request->get('_controller');
 
-        // Setting converts
+        // The course parameter is loaded
+        $course = $request->get('course');
+
+        // Setting course entity for controllers and templates
         if ($isCourseTool) {
             // Converting /courses/XXX/ to a Entity/Course object
             $course = $app['orm.em']->getRepository('Entity\Course')->findOneByCode($course);
@@ -842,7 +839,6 @@ if (isset($app['configuration']['main_database']) && isset($app['db.event_manage
     $app['dbs.event_manager']['db_read']->addEventSubscriber($sluggableListener);
     $app['dbs.event_manager']['db_write']->addEventSubscriber($sluggableListener);
 
-
     $sortableListener = new Gedmo\Sortable\SortableListener();
     // $app['db.event_manager']->addEventSubscriber($sortableListener);
     $app['dbs.event_manager']['db_read']->addEventSubscriber($sortableListener);

+ 1 - 0
vendor/chamilo/chash/src/Chash/Resources/Database/1.10.0/db_course.sql

@@ -1324,6 +1324,7 @@ CREATE TABLE c_quiz (
   question_selection_type INT default 1,
   hide_question_title INT default 0,
   score_type_model INT default 0,
+  global_category_id INT default NULL,
   PRIMARY KEY (iid)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 /*!40101 SET character_set_client = @saved_cs_client */;

+ 1 - 0
vendor/chamilo/chash/src/Chash/Resources/Database/1.10.0/migrate-db-1.9.0-1.10.0-pre.sql

@@ -309,6 +309,7 @@ INSERT INTO roles (id, name, role) VALUES('17', 'Question Manager', 'ROLE_QUESTI
 
 ALTER TABLE c_quiz ADD COLUMN question_selection_type INT DEFAULT 1;
 ALTER TABLE c_quiz ADD COLUMN hide_question_title INT DEFAULT 0;
+ALTER TABLE c_quiz ADD COLUMN global_category_id INT DEFAULT NULL;
 
 -- Admin
 INSERT INTO users_roles VALUES (1, 11);