name = ''; $this->description = ''; } /** * return the TestCategory object with id=in_id. * * @param int $id * @param int $courseId * * @return TestCategory */ public function getCategory($id, $courseId = 0) { $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $id = (int) $id; $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; $sql = "SELECT * FROM $table WHERE id = $id AND c_id = ".$courseId; $res = Database::query($sql); if (Database::num_rows($res)) { $row = Database::fetch_array($res); $this->id = $row['id']; $this->name = $row['title']; $this->description = $row['description']; return $this; } return false; } /** * Save TestCategory in the database if name doesn't exists. * * @param int $courseId * * @return bool */ public function save($courseId = 0) { $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; $courseInfo = api_get_course_info_by_id($courseId); if (empty($courseInfo)) { return false; } $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); // check if name already exists $sql = "SELECT count(*) AS nb FROM $table WHERE title = '".Database::escape_string($this->name)."' AND c_id = $courseId"; $result = Database::query($sql); $row = Database::fetch_array($result); // lets add in BDD if not the same name if ($row['nb'] <= 0) { $params = [ 'c_id' => $courseId, 'title' => $this->name, 'description' => $this->description, ]; $newId = Database::insert($table, $params); if ($newId) { $sql = "UPDATE $table SET id = iid WHERE iid = $newId"; Database::query($sql); api_item_property_update( $courseInfo, TOOL_TEST_CATEGORY, $newId, 'TestCategoryAdded', api_get_user_id() ); } return $newId; } else { return false; } } /** * Removes the category from the database * if there were question in this category, the link between question and category is removed. * * @param int $id * * @return bool */ public function removeCategory($id) { $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $tbl_question_rel_cat = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); $id = (int) $id; $course_id = api_get_course_int_id(); $category = $this->getCategory($id); if ($category) { $sql = "DELETE FROM $table WHERE id= $id AND c_id=".$course_id; Database::query($sql); // remove link between question and category $sql = "DELETE FROM $tbl_question_rel_cat WHERE category_id = $id AND c_id=".$course_id; Database::query($sql); // item_property update $courseInfo = api_get_course_info_by_id($course_id); api_item_property_update( $courseInfo, TOOL_TEST_CATEGORY, $this->id, 'TestCategoryDeleted', api_get_user_id() ); return true; } return false; } /** * Modify category name or description of category with id=in_id. * * @param int $courseId * * @return bool */ public function modifyCategory($courseId = 0) { $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $id = intval($this->id); $name = Database::escape_string($this->name); $description = Database::escape_string($this->description); $cat = $this->getCategory($id, $courseId); $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; $courseInfo = api_get_course_info_by_id($courseId); if (empty($courseInfo)) { return false; } if ($cat) { $sql = "UPDATE $table SET title = '$name', description = '$description' WHERE id = $id AND c_id = ".$courseId; Database::query($sql); // item_property update api_item_property_update( $courseInfo, TOOL_TEST_CATEGORY, $this->id, 'TestCategoryModified', api_get_user_id() ); return true; } return false; } /** * Gets the number of question of category id=in_id. */ public function getCategoryQuestionsNumber() { $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); $id = (int) $this->id; $sql = "SELECT count(*) AS nb FROM $table WHERE category_id = $id AND c_id=".api_get_course_int_id(); $res = Database::query($sql); $row = Database::fetch_array($res); return $row['nb']; } /** * Return an array of all Category objects in the database * If $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). * * @param string $field * @param int $courseId * * @return array */ public static function getCategoryListInfo($field = '', $courseId = 0) { $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId; $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $categories = []; if (empty($field)) { $sql = "SELECT id FROM $table WHERE c_id = $courseId ORDER BY title ASC"; $res = Database::query($sql); while ($row = Database::fetch_array($res)) { $category = new TestCategory(); $categories[] = $category->getCategory($row['id'], $courseId); } } else { $field = Database::escape_string($field); $sql = "SELECT $field FROM $table WHERE c_id = $courseId ORDER BY $field ASC"; $res = Database::query($sql); while ($row = Database::fetch_array($res)) { $categories[] = $row[$field]; } } return $categories; } /** * Return the TestCategory id for question with question_id = $questionId * In this version, a question has only 1 TestCategory. * Return the TestCategory id, 0 if none. * * @param int $questionId * @param int $courseId * * @return int */ public static function getCategoryForQuestion($questionId, $courseId = 0) { $courseId = (int) $courseId; $questionId = (int) $questionId; if (empty($courseId)) { $courseId = api_get_course_int_id(); } if (empty($courseId) || empty($questionId)) { return 0; } $table = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); $sql = "SELECT category_id FROM $table WHERE question_id = $questionId AND c_id = $courseId"; $res = Database::query($sql); $result = 0; if (Database::num_rows($res) > 0) { $data = Database::fetch_array($res); $result = (int) $data['category_id']; } return $result; } /** * true if question id has a category. * * @param int $questionId * * @return bool */ public static function isQuestionHasCategory($questionId) { if (self::getCategoryForQuestion($questionId) > 0) { return true; } return false; } /** * Return the category name for question with question_id = $questionId * In this version, a question has only 1 category. * * @param $questionId * @param int $courseId * * @return string */ public static function getCategoryNameForQuestion($questionId, $courseId = 0) { if (empty($courseId)) { $courseId = api_get_course_int_id(); } $courseId = (int) $courseId; $categoryId = self::getCategoryForQuestion($questionId, $courseId); $table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $sql = "SELECT title FROM $table WHERE id = $categoryId AND c_id = $courseId"; $res = Database::query($sql); $data = Database::fetch_array($res); $result = ''; if (Database::num_rows($res) > 0) { $result = $data['title']; } 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 * @param int $courseId * * @return array */ public static function getListOfCategoriesIDForTest($exerciseId, $courseId = 0) { // parcourir les questions d'un test, recup les categories uniques dans un tableau $exercise = new Exercise($courseId); $exercise->read($exerciseId, false); $categoriesInExercise = $exercise->getQuestionWithCategories(); // the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ??? $categories = []; if (!empty($categoriesInExercise)) { foreach ($categoriesInExercise as $category) { $categories[$category['id']] = $category; } } return $categories; } /** * @param Exercise $exercise * * @return array */ public static function getListOfCategoriesIDForTestObject(Exercise $exercise) { // parcourir les questions d'un test, recup les categories uniques dans un tableau $categories_in_exercise = []; $question_list = $exercise->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 = [$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; } /** * Return the list of different categories NAME for a test. * * @param int $exerciseId * @param bool * * @return array * * @author function rewrote by jmontoya */ public static function getListOfCategoriesNameForTest($exerciseId, $grouped_by_category = true) { $result = []; $categories = self::getListOfCategoriesIDForTest($exerciseId); foreach ($categories as $catInfo) { $categoryId = $catInfo['id']; if (!empty($categoryId)) { $result[$categoryId] = [ 'title' => $catInfo['title'], //'parent_id' => $catInfo['parent_id'], 'parent_id' => '', 'c_id' => $catInfo['c_id'], ]; } } return $result; } /** * @param Exercise $exercise * * @return array */ public static function getListOfCategoriesForTest(Exercise $exercise) { $result = []; $categories = self::getListOfCategoriesIDForTestObject($exercise); foreach ($categories as $cat_id) { $cat = new TestCategory(); $cat = (array) $cat->getCategory($cat_id); $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 count(self::getListOfCategoriesIDForTest($id)); } /** * return the number of question of a category id in a test. * * @param int $exerciseId * @param int $categoryId * * @return int * * @author hubert.borderiou 07-04-2011 */ public static function getNumberOfQuestionsInCategoryForTest($exerciseId, $categoryId) { $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 (self::getCategoryForQuestion($questionList[$i]) == $categoryId) { $nbCatResult++; } } return $nbCatResult; } /** * return the number of question for a test using random by category * input : test_id, number of random question (min 1). * * @param int $exerciseId * @param int $random * * @return int * hubert.borderiou 07-04-2011 * question without categories are not counted */ public static function getNumberOfQuestionRandomByCategory($exerciseId, $random) { $count = 0; $categories = self::getListOfCategoriesIDForTest($exerciseId); foreach ($categories as $category) { if (empty($category['id'])) { continue; } $nbQuestionInThisCat = self::getNumberOfQuestionsInCategoryForTest( $exerciseId, $category['id'] ); if ($nbQuestionInThisCat > $random) { $count += $random; } else { $count += $nbQuestionInThisCat; } } return $count; } /** * Return an array (id=>name) * array[0] = get_lang('NoCategory');. * * @param int $courseId * * @return array */ public static function getCategoriesIdAndName($courseId = 0) { if (empty($courseId)) { $courseId = api_get_course_int_id(); } $categories = self::getCategoryListInfo('', $courseId); $result = ['0' => get_lang('NoCategorySelected')]; for ($i = 0; $i < count($categories); $i++) { $result[$categories[$i]->id] = $categories[$i]->name; } return $result; } /** * Returns an array of question ids for each category * $categories[1][30] = 10, array with category id = 1 and question_id = 10 * A question has "n" categories. * * @param int $exerciseId * @param array $check_in_question_list * @param array $categoriesAddedInExercise * * @return array */ public static function getQuestionsByCat( $exerciseId, $check_in_question_list = [], $categoriesAddedInExercise = [] ) { $tableQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); $TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); $TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY); $categoryTable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY); $exerciseId = (int) $exerciseId; $courseId = api_get_course_int_id(); $sql = "SELECT DISTINCT qrc.question_id, qrc.category_id FROM $TBL_QUESTION_REL_CATEGORY qrc INNER JOIN $TBL_EXERCICE_QUESTION eq ON (eq.question_id = qrc.question_id AND qrc.c_id = eq.c_id) INNER JOIN $categoryTable c ON (c.id = qrc.category_id AND c.c_id = eq.c_id) INNER JOIN $tableQuestion q ON (q.id = qrc.question_id AND q.c_id = eq.c_id) WHERE exercice_id = $exerciseId AND qrc.c_id = $courseId "; $res = Database::query($sql); $categories = []; while ($data = Database::fetch_array($res)) { if (!empty($check_in_question_list)) { if (!in_array($data['question_id'], $check_in_question_list)) { continue; } } if (!isset($categories[$data['category_id']]) || !is_array($categories[$data['category_id']]) ) { $categories[$data['category_id']] = []; } $categories[$data['category_id']][] = $data['question_id']; } if (!empty($categoriesAddedInExercise)) { $newCategoryList = []; foreach ($categoriesAddedInExercise as $category) { $categoryId = $category['category_id']; if (isset($categories[$categoryId])) { $newCategoryList[$categoryId] = $categories[$categoryId]; } } $checkQuestionsWithNoCategory = false; foreach ($categoriesAddedInExercise as $category) { if (empty($category['category_id'])) { // Check $checkQuestionsWithNoCategory = true; break; } } // Select questions that don't have any category related if ($checkQuestionsWithNoCategory) { $originalQuestionList = $check_in_question_list; foreach ($originalQuestionList as $questionId) { $categoriesFlatten = array_flatten($categories); if (!in_array($questionId, $categoriesFlatten)) { $newCategoryList[0][] = $questionId; } } } $categories = $newCategoryList; } return $categories; } /** * Returns an array of $numberElements from $array. * * @param array * @param int * * @return array */ public static function getNElementsFromArray($array, $numberElements) { $list = $array; shuffle($list); if ($numberElements < count($list)) { $list = array_slice($list, 0, $numberElements); } return $list; } /** * @param int $questionId * @param int $displayCategoryName */ public static function displayCategoryAndTitle($questionId, $displayCategoryName = 1) { echo self::returnCategoryAndTitle($questionId, $displayCategoryName); } /** * @param int $questionId * @param int $in_display_category_name * * @return string|null */ public static function returnCategoryAndTitle($questionId, $in_display_category_name = 1) { $is_student = !(api_is_allowed_to_edit(null, true) || api_is_session_admin()); $objExercise = Session::read('objExercise'); if (!empty($objExercise)) { $in_display_category_name = $objExercise->display_category_name; } $content = null; if (self::getCategoryNameForQuestion($questionId) != '' && ($in_display_category_name == 1 || !$is_student) ) { $content .= '
'.get_lang('Categories').' | '; $return .= ''.get_lang('Number').' |
---|---|
'; $return .= Display::div($category['name']); $return .= ' | '; $return .= ''; $value = isset($saved_categories) && isset($saved_categories[$cat_id]) ? $saved_categories[$cat_id]['count_questions'] : -1; $return .= ''; $return .= ' | '; $return .= '