From 3ae340930e3eeac7349dc754093c6b8e8d24f165 Mon Sep 17 00:00:00 2001 From: jmontoyaa Date: Mon, 15 Aug 2016 11:36:02 +0200 Subject: [PATCH] Fix variables, add AddFlash functions --- main/exercise/TestCategory.php | 952 ++++++++++++++++--------------- main/exercise/tests_category.php | 51 +- 2 files changed, 499 insertions(+), 504 deletions(-) diff --git a/main/exercise/TestCategory.php b/main/exercise/TestCategory.php index 8616727962..7afab568fd 100644 --- a/main/exercise/TestCategory.php +++ b/main/exercise/TestCategory.php @@ -69,22 +69,20 @@ class TestCategory public function addCategoryInBDD() { $table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); - $v_name = $this->name; - $v_name = Database::escape_string($v_name); - $v_description = $this->description; - $v_description = Database::escape_string($v_description); + $v_name = Database::escape_string($this->name); + $v_description = Database::escape_string($this->description); // check if name already exists $sql = "SELECT count(*) AS nb FROM $table WHERE title = '$v_name' AND c_id=".api_get_course_int_id(); - $result_verif = Database::query($sql); - $data_verif = Database::fetch_array($result_verif); + $result = Database::query($sql); + $data_verif = Database::fetch_array($result); // lets add in BDD if not the same name if ($data_verif['nb'] <= 0) { $c_id = api_get_course_int_id(); $params = [ 'c_id' => $c_id, 'title' => $v_name, - 'description' => $v_description, + 'description' => $v_description ]; $new_id = Database::insert($table, $params); @@ -130,9 +128,9 @@ class TestCategory return false; } else { // remove link between question and category - $sql2 = "DELETE FROM $tbl_question_rel_cat + $sql = "DELETE FROM $tbl_question_rel_cat WHERE category_id = $v_id AND c_id=".$course_id; - Database::query($sql2); + Database::query($sql); // item_property update $course_info = api_get_course_info_by_id($course_id); api_item_property_update( @@ -195,31 +193,25 @@ class TestCategory return $row['nb']; } - /** - * @param string $in_color - */ - public function display($in_color="#E0EBF5") - { - echo ""; - } - /** * Return an array of all Category objects in the database * If in_field=="" Return an array of all category objects in the database * Otherwise, return an array of all in_field value * in the database (in_field = id or name or description) - */ - public static function getCategoryListInfo($in_field = "", $courseId = "") + * + * @param string $in_field + * @param int $courseId + * @return array + */ + public static function getCategoryListInfo($in_field = '', $courseId = 0) { - if (empty($courseId) || $courseId=="") { + if (empty($courseId)) { $courseId = api_get_course_int_id(); } $table = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $in_field = Database::escape_string($in_field); - $tabres = array(); - if ($in_field == "") { + $categories = array(); + if ($in_field == '') { $sql = "SELECT * FROM $table WHERE c_id=$courseId ORDER BY title ASC"; $res = Database::query($sql); @@ -229,7 +221,7 @@ class TestCategory $row['title'], $row['description'] ); - $tabres[] = $tmpcat; + $categories[] = $tmpcat; } } else { $sql = "SELECT $in_field FROM $table @@ -237,11 +229,11 @@ class TestCategory ORDER BY $in_field ASC"; $res = Database::query($sql); while ($row = Database::fetch_array($res)) { - $tabres[] = $row[$in_field]; + $categories[] = $row[$in_field]; } } - return $tabres; + return $categories; } /** @@ -253,212 +245,218 @@ class TestCategory * * @return int */ - public static function getCategoryForQuestion($questionId, $courseId ="") + public static function getCategoryForQuestion($questionId, $courseId = 0) { - $result = 0; - if (empty($courseId) || $courseId == "") { + $result = 0; + if (empty($courseId)) { $courseId = api_get_course_int_id(); } - $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); + $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); $questionId = intval($questionId); - $sql = "SELECT category_id - FROM $table - WHERE question_id = $questionId AND c_id = $courseId"; - $res = Database::query($sql); - if (Database::num_rows($res) > 0) { + $sql = "SELECT category_id + FROM $table + WHERE question_id = $questionId AND c_id = $courseId"; + $res = Database::query($sql); + if (Database::num_rows($res) > 0) { $data = Database::fetch_array($res); - $result = $data['category_id']; - } + $result = $data['category_id']; + } - return $result; - } + return $result; + } - /** - * true if question id has a category - */ - public static function isQuestionHasCategory($questionId) + /** + * true if question id has a category + * + * @param int $questionId + * @return bool + */ + public static function isQuestionHasCategory($questionId) { - if (TestCategory::getCategoryForQuestion($questionId) > 0) { - return true; - } - return false; - } + if (TestCategory::getCategoryForQuestion($questionId) > 0) { - /** - Return the category name for question with question_id = $questionId - In this version, a question has only 1 category. - Return the category id, "" if none - */ + return true; + } + + return false; + } + + /** + Return the category name for question with question_id = $questionId + In this version, a question has only 1 category. + Return the category id, "" if none + */ public static function getCategoryNameForQuestion( $questionId, - $courseId = "" + $courseId = 0 ) { - if (empty($courseId) || $courseId=="") { - $courseId = api_get_course_int_id(); - } - $catid = TestCategory::getCategoryForQuestion($questionId, $courseId); - $result = ""; // result - $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); - $catid = intval($catid); - $sql = "SELECT title FROM $table - WHERE id = $catid AND c_id = $courseId"; - $res = Database::query($sql); - $data = Database::fetch_array($res); - if (Database::num_rows($res) > 0) { - $result = $data['title']; - } + if (empty($courseId)) { + $courseId = api_get_course_int_id(); + } + $catid = TestCategory::getCategoryForQuestion($questionId, $courseId); + $result = ''; // result + $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); + $catid = intval($catid); + $sql = "SELECT title FROM $table + WHERE id = $catid AND c_id = $courseId"; + $res = Database::query($sql); + $data = Database::fetch_array($res); + if (Database::num_rows($res) > 0) { + $result = $data['title']; + } - return $result; - } + return $result; + } - /** - * Return the list of differents categories ID for a test in the current course - * input : test_id - * return : array of category id (integer) - * hubert.borderiou 07-04-2011 - * @param int $exerciseId - */ - public static function getListOfCategoriesIDForTest($exerciseId) + /** + * Return the list of differents categories ID for a test in the current course + * input : test_id + * return : array of category id (integer) + * hubert.borderiou 07-04-2011 + * @param int $exerciseId + */ + public static function getListOfCategoriesIDForTest($exerciseId) { - // parcourir les questions d'un test, recup les categories uniques dans un tableau - $exercise = new Exercise(); - $exercise->read($exerciseId, false); - $categoriesInExercise = $exercise->getQuestionWithCategories(); - // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ??? - $categories = array(); + // parcourir les questions d'un test, recup les categories uniques dans un tableau + $exercise = new Exercise(); + $exercise->read($exerciseId, false); + $categoriesInExercise = $exercise->getQuestionWithCategories(); + // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ??? + $categories = array(); if (!empty($categoriesInExercise)) { - foreach ($categoriesInExercise as $category) { - //$category['id'] = $category['iid']; - $categories[$category['id']] = $category; - } - } + foreach ($categoriesInExercise as $category) { + //$category['id'] = $category['iid']; + $categories[$category['id']] = $category; + } + } - return $categories; - } + return $categories; + } - /** - * @param Exercise $exercise_obj - * @return array - */ - public static function getListOfCategoriesIDForTestObject(Exercise $exercise_obj) - { - // parcourir les questions d'un test, recup les categories uniques dans un tableau - $categories_in_exercise = array(); - // $question_list = $exercise_obj->getQuestionList(); - $question_list = $exercise_obj->getQuestionOrderedListByName(); - - // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ??? - foreach ($question_list as $questionInfo) { - $question_id = $questionInfo['question_id']; - $category_list = self::getCategoryForQuestion($question_id); - if (is_numeric($category_list)) { - $category_list = array($category_list); - } - - if (!empty($category_list)) { - $categories_in_exercise = array_merge($categories_in_exercise, $category_list); - } - } - if (!empty($categories_in_exercise)) { - $categories_in_exercise = array_unique(array_filter($categories_in_exercise)); - } - return $categories_in_exercise; - } + /** + * @param Exercise $exercise_obj + * @return array + */ + public static function getListOfCategoriesIDForTestObject(Exercise $exercise_obj) + { + // parcourir les questions d'un test, recup les categories uniques dans un tableau + $categories_in_exercise = array(); + // $question_list = $exercise_obj->getQuestionList(); + $question_list = $exercise_obj->getQuestionOrderedListByName(); + + // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ??? + foreach ($question_list as $questionInfo) { + $question_id = $questionInfo['question_id']; + $category_list = self::getCategoryForQuestion($question_id); + if (is_numeric($category_list)) { + $category_list = array($category_list); + } - /** - * Return the list of differents categories NAME for a test - * @param int exercise id - * @param bool - * @return integer of string - * + if (!empty($category_list)) { + $categories_in_exercise = array_merge($categories_in_exercise, $category_list); + } + } + if (!empty($categories_in_exercise)) { + $categories_in_exercise = array_unique(array_filter($categories_in_exercise)); + } + + return $categories_in_exercise; + } + + /** + * Return the list of differents categories NAME for a test + * @param int exercise id + * @param bool + * @return integer of string + * * @author function rewrote by jmontoya - */ - public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true) + */ + public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true) { - $result = array(); - $categories = self::getListOfCategoriesIDForTest($exercise_id, $grouped_by_category); + $result = array(); + $categories = self::getListOfCategoriesIDForTest($exercise_id, $grouped_by_category); - foreach ($categories as $catInfo) { - $categoryId = $catInfo['id']; - if (!empty($categoryId)) { - $result[$categoryId] = array( + foreach ($categories as $catInfo) { + $categoryId = $catInfo['id']; + if (!empty($categoryId)) { + $result[$categoryId] = array( 'title' => $catInfo['title'], //'parent_id' => $catInfo['parent_id'], - 'parent_id' => '', + 'parent_id' => '', 'c_id' => $catInfo['c_id'] ); - } - } + } + } - return $result; - } + return $result; + } - /** - * @param Exercise $exercise_obj - * @return array - */ - public static function getListOfCategoriesForTest(Exercise $exercise_obj) - { - $result = array(); - $categories = self::getListOfCategoriesIDForTestObject($exercise_obj); - foreach ($categories as $cat_id) { - $cat = new TestCategory($cat_id); - $cat = (array)$cat; - $cat['iid'] = $cat['id']; - $cat['title'] = $cat['name']; - $result[$cat['id']] = $cat; - } - return $result; - } + /** + * @param Exercise $exercise_obj + * @return array + */ + public static function getListOfCategoriesForTest(Exercise $exercise_obj) + { + $result = array(); + $categories = self::getListOfCategoriesIDForTestObject($exercise_obj); + foreach ($categories as $cat_id) { + $cat = new TestCategory($cat_id); + $cat = (array)$cat; + $cat['iid'] = $cat['id']; + $cat['title'] = $cat['name']; + $result[$cat['id']] = $cat; + } + return $result; + } - /** - * return the number of differents categories for a test - * input : test_id - * return : integer - * hubert.borderiou 07-04-2011 - */ - public static function getNumberOfCategoriesForTest($id) + /** + * return the number of differents categories for a test + * input : test_id + * return : integer + * hubert.borderiou 07-04-2011 + */ + public static function getNumberOfCategoriesForTest($id) { - return count(TestCategory::getListOfCategoriesIDForTest($id)); - } + return count(TestCategory::getListOfCategoriesIDForTest($id)); + } - /** - * return the number of question of a category id in a test - * @param int $exerciseId + /** + * return the number of question of a category id in a test + * @param int $exerciseId * @param int $categoryId * - * @return integer + * @return integer * - * @author hubert.borderiou 07-04-2011 - */ - public static function getNumberOfQuestionsInCategoryForTest($exerciseId, $categoryId) + * @author hubert.borderiou 07-04-2011 + */ + public static function getNumberOfQuestionsInCategoryForTest($exerciseId, $categoryId) { - $nbCatResult = 0; - $quiz = new Exercise(); - $quiz->read($exerciseId); - $tabQuestionList = $quiz->selectQuestionList(); - // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ? - for ($i=1; $i <= count($tabQuestionList); $i++) { - if (TestCategory::getCategoryForQuestion($tabQuestionList[$i]) == $categoryId) { - $nbCatResult++; - } - } - - return $nbCatResult; - } + $nbCatResult = 0; + $quiz = new Exercise(); + $quiz->read($exerciseId); + $questionList = $quiz->selectQuestionList(); + // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ? + for ($i=1; $i <= count($questionList); $i++) { + if (TestCategory::getCategoryForQuestion($questionList[$i]) == $categoryId) { + $nbCatResult++; + } + } - /** - * return the number of question for a test using random by category - * input : test_id, number of random question (min 1) - * hubert.borderiou 07-04-2011 - * question without categories are not counted - */ - public static function getNumberOfQuestionRandomByCategory($exerciseId, $in_nbrandom) + return $nbCatResult; + } + + /** + * return the number of question for a test using random by category + * input : test_id, number of random question (min 1) + * hubert.borderiou 07-04-2011 + * question without categories are not counted + */ + public static function getNumberOfQuestionRandomByCategory($exerciseId, $in_nbrandom) { - $nbquestionresult = 0; - $tabcatid = TestCategory::getListOfCategoriesIDForTest($exerciseId); + $nbquestionresult = 0; + $categories = TestCategory::getListOfCategoriesIDForTest($exerciseId); - foreach ($tabcatid as $category) { + foreach ($categories as $category) { if (empty($category['id'])) { continue; } @@ -472,30 +470,31 @@ class TestCategory } } - return $nbquestionresult; - } + return $nbquestionresult; + } - /** - * Return an array (id=>name) - * tabresult[0] = get_lang('NoCategory'); + /** + * Return an array (id=>name) + * tabresult[0] = get_lang('NoCategory'); * * @param int $courseId * * @return array - * - */ - public static function getCategoriesIdAndName($courseId = "") + * + */ + public static function getCategoriesIdAndName($courseId = 0) { - if (empty($courseId)) { - $courseId = api_get_course_int_id(); - } - $tabcatobject = TestCategory::getCategoryListInfo("", $courseId); - $tabresult = array("0"=>get_lang('NoCategorySelected')); - for ($i=0; $i < count($tabcatobject); $i++) { - $tabresult[$tabcatobject[$i]->id] = $tabcatobject[$i]->name; - } - return $tabresult; - } + if (empty($courseId)) { + $courseId = api_get_course_int_id(); + } + $categories = TestCategory::getCategoryListInfo('', $courseId); + $tabresult = array('0' => get_lang('NoCategorySelected')); + for ($i=0; $i < count($categories); $i++) { + $tabresult[$categories[$i]->id] = $categories[$i]->name; + } + + return $tabresult; + } /** * Returns an array of question ids for each category @@ -530,7 +529,7 @@ class TestCategory ON (q.id = qrc.question_id ) WHERE exercice_id = $exerciseId AND - qrc.c_id = ".$courseId." + qrc.c_id = $courseId "; $res = Database::query($sql); @@ -585,28 +584,30 @@ class TestCategory } return $categories; - } + } - /** - * return a tab of $in_number random elements of $in_tab - */ + /** + * return a tab of $in_number random elements of $in_tab + */ public static function getNElementsFromArray($in_tab, $in_number) { - $tabres = $in_tab; - shuffle($tabres); - if ($in_number < count($tabres)) { - $tabres = array_slice($tabres, 0, $in_number); - } - return $tabres; - } + $list = $in_tab; + shuffle($list); + if ($in_number < count($list)) { + $list = array_slice($list, 0, $in_number); + } - /** - * display the category - */ - public static function displayCategoryAndTitle($questionId, $in_display_category_name = 1) + return $list; + } + + /** + * @param int $questionId + * @param int $in_display_category_name + */ + public static function displayCategoryAndTitle($questionId, $in_display_category_name = 1) { echo self::returnCategoryAndTitle($questionId, $in_display_category_name); - } + } /** * @param int $questionId @@ -622,13 +623,13 @@ class TestCategory $in_display_category_name = $objExercise->display_category_name; } $content = null; - if (TestCategory::getCategoryNameForQuestion($questionId) != '' && ($in_display_category_name == 1 || !$is_student)) { + if (TestCategory::getCategoryNameForQuestion($questionId) != '' && ($in_display_category_name == 1 || !$is_student)) { $content .= '"; - } + } return $content; - } + } /** * Display signs [+] and/or (>0) after question title if question has options @@ -636,66 +637,66 @@ class TestCategory */ public function displayQuestionOption($in_objQuestion) { - if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->scoreAlwaysPositive) { - echo " (>0)"; - } - if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->uncheckedMayScore) { - echo " [+]"; - } - } + if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->scoreAlwaysPositive) { + echo " (>0)"; + } + if ($in_objQuestion->type == MULTIPLE_ANSWER && $in_objQuestion->uncheckedMayScore) { + echo " [+]"; + } + } - /** - * sortTabByBracketLabel ($tabCategoryQuestions) - * key of $tabCategoryQuestions are the category id (0 for not in a category) - * value is the array of question id of this category - * Sort question by Category - */ + /** + * sortTabByBracketLabel ($tabCategoryQuestions) + * key of $tabCategoryQuestions are the category id (0 for not in a category) + * value is the array of question id of this category + * Sort question by Category + */ public static function sortTabByBracketLabel($in_tab) { - $tabResult = array(); - $tabCatName = array(); // tab of category name - while (list($cat_id, $tabquestion) = each($in_tab)) { - $catTitle = new TestCategory($cat_id); - $tabCatName[$cat_id] = $catTitle->name; - } - reset($in_tab); - // sort table by value, keeping keys as they are - asort($tabCatName); - // keys of $tabCatName are keys order for $in_tab - while (list($key, $val) = each($tabCatName)) { - $tabResult[$key] = $in_tab[$key]; - } - return $tabResult; - } + $tabResult = array(); + $tabCatName = array(); // tab of category name + while (list($cat_id, $tabquestion) = each($in_tab)) { + $catTitle = new TestCategory($cat_id); + $tabCatName[$cat_id] = $catTitle->name; + } + reset($in_tab); + // sort table by value, keeping keys as they are + asort($tabCatName); + // keys of $tabCatName are keys order for $in_tab + while (list($key, $val) = each($tabCatName)) { + $tabResult[$key] = $in_tab[$key]; + } + return $tabResult; + } /** - * return total score for test exe_id for all question in the category $in_cat_id for user - * If no question for this category, return "" - */ - public static function getCatScoreForExeidForUserid($in_cat_id, $in_exe_id, $in_user_id) - { - $tbl_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); - $tbl_question_rel_category = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); - $in_cat_id = intval($in_cat_id); - $in_exe_id = intval($in_exe_id); - $in_user_id = intval($in_user_id); - - $query = "SELECT DISTINCT - marks, exe_id, user_id, ta.question_id, category_id + * return total score for test exe_id for all question in the category $in_cat_id for user + * If no question for this category, return "" + */ + public static function getCatScoreForExeidForUserid($in_cat_id, $in_exe_id, $in_user_id) + { + $tbl_track_attempt = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); + $tbl_question_rel_category = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); + $in_cat_id = intval($in_cat_id); + $in_exe_id = intval($in_exe_id); + $in_user_id = intval($in_user_id); + + $query = "SELECT DISTINCT + marks, exe_id, user_id, ta.question_id, category_id FROM $tbl_track_attempt ta , $tbl_question_rel_category qrc WHERE ta.question_id=qrc.question_id AND qrc.category_id=$in_cat_id AND exe_id=$in_exe_id AND user_id=$in_user_id"; - $res = Database::query($query); - $totalcatscore = ""; - while ($data = Database::fetch_array($res)) { - $totalcatscore += $data['marks']; - } - return $totalcatscore; - } + $res = Database::query($query); + $totalcatscore = ""; + while ($data = Database::fetch_array($res)) { + $totalcatscore += $data['marks']; + } + return $totalcatscore; + } - /** + /** * return the number max of question in a category * count the number of questions in all categories, and return the max * @param int $exerciseId @@ -705,9 +706,9 @@ class TestCategory { $res_num_max = 0; // foreach question - $tabcatid = TestCategory::getListOfCategoriesIDForTest($exerciseId); + $categories = TestCategory::getListOfCategoriesIDForTest($exerciseId); - foreach ($tabcatid as $category) { + foreach ($categories as $category) { if (empty($category['id'])) { continue; } @@ -718,6 +719,7 @@ class TestCategory $res_num_max = $nbQuestionInThisCat; } } + return $res_num_max; } @@ -777,34 +779,36 @@ class TestCategory } /** - * @return array - */ - function get_all_categories() - { - $table = Database::get_course_table(TABLE_QUIZ_CATEGORY); - $sql = "SELECT * FROM $table ORDER BY title ASC"; - $res = Database::query($sql); - while ($row = Database::fetch_array($res,'ASSOC')) { - $array[] = $row; - } - return $array; - } + * @return array + */ + public static function get_all_categories() + { + $table = Database::get_course_table(TABLE_QUIZ_CATEGORY); + $sql = "SELECT * FROM $table ORDER BY title ASC"; + $res = Database::query($sql); + $array = []; + while ($row = Database::fetch_array($res,'ASSOC')) { + $array[] = $row; + } - /** - * @param Exercise $exercise - * @param int $course_id - * @param string $order - * @param bool $shuffle - * @param bool $excludeCategoryWithNoQuestions - * @return array|bool - */ - public function getCategoryExerciseTree( - $exercise, - $course_id, - $order = null, - $shuffle = false, - $excludeCategoryWithNoQuestions = true - ) { + return $array; + } + + /** + * @param Exercise $exercise + * @param int $course_id + * @param string $order + * @param bool $shuffle + * @param bool $excludeCategoryWithNoQuestions + * @return array|bool + */ + public function getCategoryExerciseTree( + $exercise, + $course_id, + $order = null, + $shuffle = false, + $excludeCategoryWithNoQuestions = true + ) { if (empty($exercise)) { return array(); } @@ -814,178 +818,184 @@ class TestCategory } $course_id = intval($course_id); - $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY); + $table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY); $categoryTable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); - $sql = "SELECT * FROM $table qc - LEFT JOIN $categoryTable c + $sql = "SELECT * FROM $table qc + LEFT JOIN $categoryTable c ON (qc.c_id = c.c_id AND c.id = qc.category_id) WHERE qc.c_id = $course_id AND exercise_id = {$exercise->id} "; - if (!empty($order)) { - $sql .= "ORDER BY $order"; - } - - $categories = array(); - - $result = Database::query($sql); - if (Database::num_rows($result)) { - while ($row = Database::fetch_array($result, 'ASSOC')) { - if ($excludeCategoryWithNoQuestions) { - if ($row['count_questions'] == 0) { - continue; - } - } - if (empty($row['title']) && empty($row['category_id'])) { - $row['title'] = get_lang('NoCategory'); - } + if (!empty($order)) { + $sql .= "ORDER BY $order"; + } + + $categories = array(); + + $result = Database::query($sql); + if (Database::num_rows($result)) { + while ($row = Database::fetch_array($result, 'ASSOC')) { + if ($excludeCategoryWithNoQuestions) { + if ($row['count_questions'] == 0) { + continue; + } + } + if (empty($row['title']) && empty($row['category_id'])) { + $row['title'] = get_lang('NoCategory'); + } $categories[$row['category_id']] = $row; - } - } + } + } - if ($shuffle) { - shuffle_assoc($categories); - } + if ($shuffle) { + shuffle_assoc($categories); + } - return $categories; - } + return $categories; + } - 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', $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')); - $category_parent_list = array(); - - $options = array( - '1' => get_lang('Visible'), - '0' => get_lang('Hidden') - ); - $form->addElement('select', 'visibility', get_lang('Visibility'), $options); - $script = null; - if (!empty($this->parent_id)) { - $parent_cat = new TestCategory($this->parent_id); - $category_parent_list = array($parent_cat->id => $parent_cat->name); - $script .= ''; - } - $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', $submit, 'class="add"'); - - // setting the defaults - $defaults = array(); - $defaults["category_id"] = $this->id; - $defaults["category_name"] = $this->name; - $defaults["category_description"] = $this->description; - $defaults["parent_id"] = $this->parent_id; - $defaults["visibility"] = $this->visibility; - $form->setDefaults($defaults); - - // setting the rules - $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required'); - } + /** + * @param $form + * @param string $action + */ + 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; + } - /** - * Returns the category form. - * @param Exercise $exercise_obj - * @return string - */ - public function returnCategoryForm(Exercise $exercise_obj) - { - $categories = $this->getListOfCategoriesForTest($exercise_obj); - - $saved_categories = $exercise_obj->get_categories_in_exercise(); - $return = null; - - if (!empty($categories)) { - $nbQuestionsTotal = $exercise_obj->getNumberQuestionExerciseCategory(); - $exercise_obj->setCategoriesGrouping(true); - $real_question_count = count($exercise_obj->getQuestionList()); - - $warning = null; - if ($nbQuestionsTotal != $real_question_count) { - $warning = Display::return_message(get_lang('CheckThatYouHaveEnoughQuestionsInYourCategories'), 'warning'); - } - - $return .= $warning; - $return .= ''; - $return .= ''; - $return .= ''; - $return .= ''; - - $emptyCategory = array( - 'id' => '0', - 'name' => get_lang('NoCategory'), - 'description' => '', - 'iid' => '0', - 'title' => get_lang('NoCategory') - ); - - $categories[] = $emptyCategory; - - foreach ($categories as $category) { - $cat_id = $category['iid']; - $return .= ''; - $return .= ''; - $return .= ''; - $return .= ''; - } - - $return .= '
' . get_lang('Categories') . '' . get_lang('Number') . '
'; - //$return .= Display::div(isset($category['parent_path']) ? $category['parent_path'] : ''); - $return .= Display::div($category['name']); - $return .= ''; - $value = isset($saved_categories) && isset($saved_categories[$cat_id]) ? $saved_categories[$cat_id]['count_questions'] : -1; - $return .= ''; - $return .= '
'; - $return .= get_lang('ZeroMeansNoQuestionWillBeSelectedMinusOneMeansThatAllQuestionsWillBeSelected'); - return $return; - } - } + // settting the form elements + $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')); + $category_parent_list = array(); + + $options = array( + '1' => get_lang('Visible'), + '0' => get_lang('Hidden') + ); + $form->addElement('select', 'visibility', get_lang('Visibility'), $options); + $script = null; + if (!empty($this->parent_id)) { + $parent_cat = new TestCategory($this->parent_id); + $category_parent_list = array($parent_cat->id => $parent_cat->name); + $script .= ''; + } + $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', $submit, 'class="add"'); + + // setting the defaults + $defaults = array(); + $defaults["category_id"] = $this->id; + $defaults["category_name"] = $this->name; + $defaults["category_description"] = $this->description; + $defaults["parent_id"] = $this->parent_id; + $defaults["visibility"] = $this->visibility; + $form->setDefaults($defaults); + + // setting the rules + $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required'); + } - /** - * Sorts an array - * @param $array - * @return mixed - */ - public function sort_tree_array($array) - { - foreach ($array as $key => $row) { - $parent[$key] = $row['parent_id']; - } - if (count($array) > 0) { - array_multisort($parent, SORT_ASC, $array); - } - return $array; - } + /** + * Returns the category form. + * @param Exercise $exercise_obj + * @return string + */ + public function returnCategoryForm(Exercise $exercise_obj) + { + $categories = $this->getListOfCategoriesForTest($exercise_obj); - /** + $saved_categories = $exercise_obj->get_categories_in_exercise(); + $return = null; + + if (!empty($categories)) { + $nbQuestionsTotal = $exercise_obj->getNumberQuestionExerciseCategory(); + $exercise_obj->setCategoriesGrouping(true); + $real_question_count = count($exercise_obj->getQuestionList()); + + $warning = null; + if ($nbQuestionsTotal != $real_question_count) { + $warning = Display::return_message(get_lang('CheckThatYouHaveEnoughQuestionsInYourCategories'), 'warning'); + } + + $return .= $warning; + $return .= ''; + $return .= ''; + $return .= ''; + $return .= ''; + + $emptyCategory = array( + 'id' => '0', + 'name' => get_lang('NoCategory'), + 'description' => '', + 'iid' => '0', + 'title' => get_lang('NoCategory') + ); + + $categories[] = $emptyCategory; + + foreach ($categories as $category) { + $cat_id = $category['iid']; + $return .= ''; + $return .= ''; + $return .= ''; + $return .= ''; + } + + $return .= '
' . get_lang('Categories') . '' . get_lang('Number') . '
'; + //$return .= Display::div(isset($category['parent_path']) ? $category['parent_path'] : ''); + $return .= Display::div($category['name']); + $return .= ''; + $value = isset($saved_categories) && isset($saved_categories[$cat_id]) ? $saved_categories[$cat_id]['count_questions'] : -1; + $return .= ''; + $return .= '
'; + $return .= get_lang('ZeroMeansNoQuestionWillBeSelectedMinusOneMeansThatAllQuestionsWillBeSelected'); + return $return; + } + } + + /** + * Sorts an array + * @param $array + * @return mixed + */ + public function sort_tree_array($array) + { + foreach ($array as $key => $row) { + $parent[$key] = $row['parent_id']; + } + if (count($array) > 0) { + array_multisort($parent, SORT_ASC, $array); + } + return $array; + } + + /** * Return true if a category already exists with the same name - * @param string $in_name + * @param string $name * * @return bool */ - public static function category_exists_with_title($in_name) + public static function category_exists_with_title($name) { - $tab_test_category = TestCategory::getCategoryListInfo("title"); - foreach ($tab_test_category as $title) { - if ($title == $in_name) { + $categories = TestCategory::getCategoryListInfo('title'); + foreach ($categories as $title) { + if ($title == $name) { + return true; } } + return false; } @@ -1018,8 +1028,8 @@ class TestCategory * @param int $categoryId * @param int $questionId * @param int $courseId - * - * @return string|false + * + * @return string|false */ public static function add_category_for_question_id($categoryId, $questionId, $courseId) { @@ -1027,18 +1037,18 @@ class TestCategory // if question doesn't have a category // @todo change for 1.10 when a question can have several categories if (TestCategory::getCategoryForQuestion($questionId, $courseId) == 0 && - $questionId > 0 && - $courseId > 0 + $questionId > 0 && + $courseId > 0 ) { $sql = "INSERT INTO $table (c_id, question_id, category_id) VALUES (".intval($courseId).", ".intval($questionId).", ".intval($categoryId).")"; Database::query($sql); - $id = Database::insert_id(); + $id = Database::insert_id(); - return $id; + return $id; } - return false; + return false; } /** diff --git a/main/exercise/tests_category.php b/main/exercise/tests_category.php index 4b506972a9..936d004933 100755 --- a/main/exercise/tests_category.php +++ b/main/exercise/tests_category.php @@ -19,7 +19,7 @@ $htmlHeadXtra[] = ' } '; -$nameTools = ""; +$nameTools = ''; require_once '../inc/global.inc.php'; @@ -94,10 +94,8 @@ Display::display_footer(); function importCategoryForm() { $form = new FormValidator('import', 'post', api_get_self().'?action=import_category&'.api_get_cidreq()); - //$form->addElement('header', get_lang('ImportGroups')); $form->addElement('file', 'file', get_lang('ImportCSVFileLocation')); $form->addRule('file', get_lang('ThisFieldIsRequired'), 'required'); - //$form->addElement('label', null, Display::url(get_lang('ExampleCSVFile'), api_get_path(WEB_CODE_PATH).'group/example.csv')); $form->addButtonImport(get_lang('Import')); return $form; @@ -149,19 +147,19 @@ function edit_category_form($action) $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); - $v_id = Security::remove_XSS($values['category_id']); - $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER); - $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); - $objcat = new TestCategory($v_id, $v_name, $v_description); - if ($objcat->modifyCategory()) { - Display::addFlash(Display::return_message(get_lang('MofidfyCategoryDone'))); + $category = new TestCategory( + $values['category_id'], + $values['category_name'], + $values['category_description'] + ); + if ($category->modifyCategory()) { + Display::addFlash(Display::return_message(get_lang('Updated'))); } else { - Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'))); + Display::addFlash(Display::return_message(get_lang('ModifyCategoryError'), 'error')); } } Security::clear_token(); } else { - display_goback(); $token = Security::get_token(); $form->addElement('hidden', 'sec_token'); $form->setConstants(array('sec_token' => $token)); @@ -179,15 +177,14 @@ function edit_category_form($action) function delete_category_form($action) { if (isset($_GET['category_id']) && is_numeric($_GET['category_id'])) { - $category_id = Security::remove_XSS($_GET['category_id']); - $catobject = new TestCategory($category_id); - if ($catobject->removeCategory()) { - Display::display_confirmation_message(get_lang('DeleteCategoryDone')); + $category = new TestCategory($_GET['category_id']); + if ($category->removeCategory()) { + Display::addFlash(Display::return_message(get_lang('DeleteCategoryDone'))); } else { - Display::display_error_message(get_lang('CannotDeleteCategoryError')); + Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error')); } } else { - Display::display_error_message(get_lang('CannotDeleteCategoryError')); + Display::addFlash(Display::return_message(get_lang('CannotDeleteCategoryError'), 'error')); } } @@ -219,18 +216,15 @@ function add_category_form($action) $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); - $v_name = Security::remove_XSS($values['category_name'], COURSEMANAGER); - $v_description = Security::remove_XSS($values['category_description'], COURSEMANAGER); - $objcat = new TestCategory(0, $v_name, $v_description); - if ($objcat->addCategoryInBDD()) { - Display::display_confirmation_message(get_lang('AddCategoryDone')); + $category = new TestCategory(0, $values['category_name'], $values['category_description']); + if ($category->addCategoryInBDD()) { + Display::addFlash(Display::return_message(get_lang('AddCategoryDone'))); } else { - Display::display_confirmation_message(get_lang('AddCategoryNameAlreadyExists')); + Display::addFlash(Display::return_message(get_lang('AddCategoryNameAlreadyExists'), 'warning')); } } Security::clear_token(); } else { - //display_goback(); $token = Security::get_token(); $form->addElement('hidden', 'sec_token'); $form->setConstants(array('sec_token' => $token)); @@ -263,12 +257,3 @@ function displayActionBar() echo "
"; echo "
" . get_lang('QuestionCategory') . "
"; } - -// display goback to category list page link -function display_goback() -{ - echo '
'; - echo '' . - Display::return_icon('back.png', get_lang('BackToCategoryList'), array(), 32) . ''; - echo '
'; -}