@ -1,73 +1,166 @@
<?php
/* For licensing terms, see /license.txt */
/** @author hubert.borderiou **/
if (!class_exists('Testcategory')):
class Testcategory {
/**
* @author hubert.borderiou
*/
class Testcategory
{
public $id;
public $name;
public $name; //why using name?? Use title as in the db!
public $title;
public $description;
public $parent_id;
public $parent_path;
public $category_array_tree;
public $type;
public $course_id;
/**
* Constructor of the class Category
* @author - Hubert Borderiou
If you give an in_id and no in_name, you get info concerning the category of id=in_id
otherwise, you've got an category objet avec your in_id, in_name, in_descr
* @todo fix this function
*/
function Testcategory($in_id=0, $in_name = '', $in_description="") {
public function Testcategory($in_id = 0, $in_name = '', $in_description = "", $parent_id = 0, $type = 'simple', $course_id = null)
{
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->parent_id = $tmpobj->parent_id;
$this->parent_path = $this->name;
if (!empty($tmpobj->parent_id)) {
$category = new Testcategory($tmpobj->parent_id);
$this->parent_path = $category->parent_path.' > '.$this->name;
}
else {
} else {
$this->id = $in_id;
$this->name = $in_name;
$this->description = $in_description;
}
$this->parent_id = $parent_id;
}
$this->type = $type;
if (!empty($course_id)) {
$this->course_id = $course_id;
} else {
$this->course_id = api_get_course_int_id();
}
}
/** return the Testcategory object with id=in_id
/**
* Return the Testcategory object with id=in_id
*
* @assert () === false
*/
function getCategory($in_id) {
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$in_id = Database::escape_string($in_id);
$sql = "SELECT * FROM $t_cattable WHERE id=$in_id AND c_id=".api_get_course_int_id();
$sql = "SELECT * FROM $t_cattable WHERE iid = $in_id " ;
$res = Database::query($sql);
$numrows = Database::num_rows($res);
if ($numrows > 0) {
$row = Database::fetch_array($res);
$this->id = $row[' id'];
$this->name = $row['title'];
$this->id = $row['i id'];
$this->title = $this->name = $row['title'];
$this->description = $row['description'];
$this->parent_id = $row['parent_id'];
} else {
return false;
}
}
/** add Testcategory in the database if name doesn't already exists
*/
/**
* Add Testcategory in the database if name doesn't already exists
*
*/
function addCategoryInBDD() {
$t_cattable = 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);
// check if name already exists
$sql_verif = "SELECT count(*) AS nb FROM $t_cattable WHERE title = '$v_name' AND c_id=".api_get_course_int_id();
$result_verif = Database::query($sql_verif);
$data_verif = Database::fetch_array($result_verif);
// lets add in BDD if not the same name
if ($data_verif['nb'] < = 0) {
$c_id = api_get_course_int_id();
$sql = "INSERT INTO $t_cattable VALUES ('$c_id', '', '$v_name', '$v_description')";
$res = Database::query($sql);
return true;
}
else {
return false;
$v_name = Database::escape_string($this->name);
$v_description = Database::escape_string($this->description);
$parent_id = intval($this->parent_id);
$course_id = $this->course_id;
$courseCondition = " AND c_id = $course_id ";
if ($this->type == 'global') {
$course_id = '';
$courseCondition = null;
}
// 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);
// lets add in BD if not the same name
if ($data['nb'] < = 0) {
$sql = "INSERT INTO $t_cattable (c_id, title, description, parent_id) VALUES ('$course_id', '$v_name', '$v_description', '$parent_id')";
Database::query($sql);
return Database::insert_id();
} else {
return false;
}
}
function get_category_by_title($title , $course_id = 0) {
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$course_id = intval($course_id);
$sql = "SELECT * FROM $table WHERE title = '$title' AND c_id IN ('0', '$course_id')LIMIT 1";
$title = Database::escape_string($title);
$result = Database::query($sql);
if (Database::num_rows($result)) {
$result = Database::store_result($result, 'ASSOC');
return $result[0];
}
return false;
}
/**
*
* Get categories by title for json calls
*
* @assert() === false
*/
function get_categories_by_keyword($tag) {
if (empty($tag)) {
return false;
}
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$sql = "SELECT iid, title, c_id FROM $table WHERE 1=1 ";
$tag = Database::escape_string($tag);
$where_condition = array();
if (!empty($tag)) {
$condition = ' LIKE "%'.$tag.'%"';
$where_condition = array(
"title $condition",
);
$where_condition = ' AND ('.implode(' OR ', $where_condition).') ';
}
switch ($this->type) {
case 'simple':
$course_condition = " AND c_id = '".api_get_course_int_id()."' ";
break;
case 'global':
$course_condition = " AND c_id = '0' ";
break;
case 'all':
$course_condition = " AND c_id IN ('0', '".api_get_course_int_id()."')";
break;
}
$where_condition .= $course_condition;
$order_clause = " ORDER BY title";
$sql .= $where_condition.$order_clause;
$result = Database::query($sql);
if (Database::num_rows($result)) {
return Database::store_result($result, 'ASSOC');
}
return false;
}
/**
@ -75,12 +168,11 @@ class Testcategory {
* @todo I'm removing the $in_id parameter because it seems that you're using $this->id instead of $in_id after confirmation delete this
* jmontoya
*/
//function removeCategory($in_id) {
function removeCategory() {
$t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$v_id = Database::escape_string($this->id);
$sql = "DELETE FROM $t_cattable WHERE id=$v_id AND c_id=".api_get_course_int_id() ;
$res = Database::query($sql);
$sql = "DELETE FROM $t_cattable WHERE iid = $v_id" ;
Database::query($sql);
if (Database::affected_rows() < = 0) {
return false;
} else {
@ -89,20 +181,29 @@ class Testcategory {
}
/** modify category name or description of category with id=in_id
/**
* Modify category name or description of category with id=in_id
*/
//function modifyCategory($in_id, $in_name, $in_description) {
function modifyCategory() {
$t_cattable = Database :: get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$v_id = Database::escape_string($this->id);
$v_name = Database::escape_string($this->name);
$v_description = Database::escape_string($this->description);
$sql = "UPDATE $t_cattable SET title='$v_name', description='$v_description' WHERE id='$v_id' AND c_id=".api_get_course_int_id();
$res = Database::query($sql);
$parent_id = intval($this->parent_id);
//Avoid recursive categories
if ($parent_id == $v_id) {
$parent_id = 0;
}
$sql = "UPDATE $t_cattable SET
title = '$v_name',
description = '$v_description',
parent_id = '$parent_id'
WHERE iid = '$v_id'";
Database::query($sql);
if (Database::affected_rows() < = 0) {
return false;
}
else {
} else {
return true;
}
}
@ -116,7 +217,7 @@ class Testcategory {
function getCategoryQuestionsNumber() {
$t_reltable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$in_id = Database::escape_string($this->id);
$sql = "SELECT count(*) AS nb FROM $t_reltable WHERE category_id=$in_id AND c_id= ".api_get_course_int_id() ;
$sql = "SELECT count(*) AS nb FROM $t_reltable WHERE category_id = $in_id";
$res = Database::query($sql);
$row = Database::fetch_array($res);
return $row['nb'];
@ -145,11 +246,10 @@ class Testcategory {
$sql = "SELECT * FROM $t_cattable WHERE c_id=$in_courseid ORDER BY title ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
$tmpcat = new Testcategory($row[' id'], $row['title'], $row['description']);
$tmpcat = new Testcategory($row['i id'], $row['title'], $row['description'], $row['parent_id ']);
$tabres[] = $tmpcat;
}
}
else {
} else {
$sql = "SELECT $in_field FROM $t_cattable WHERE c_id=$in_courseid ORDER BY $in_field ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res)) {
@ -164,39 +264,68 @@ class Testcategory {
Return the testcategory id for question with question_id = $in_questionid
In this version, a question has only 1 testcategory.
Return the testcategory id, 0 if none
* @assert () === false
*/
public static function getCategoryForQuestion($in_question id, $in_courseid = null) {
public static function getCategoryForQuestion($question_ id, $in_courseid = null) {
$result = array(); // result
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$question_id = Database::escape_string($in_question id);
$question_id = Database::escape_string($question_ id);
$sql = "SELECT category_id FROM $t_cattable WHERE question_id = '$question_id' AND c_id = $in_courseid";
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
while ($row = Database::fetch_array($res)) {
while ($row = Database::fetch_array($res, 'ASSOC' )) {
$result[] = $row['category_id'];
}
}
return $result;
}
public static function getCategoryNames ForQuestion($in_ questionid, $in_courseid = null, $display_into_labels = true ) {
public static function getCategoryForQuestionWithCategoryData ($question_ id, $in_courseid = null) {
$result = array(); // result
if (empty($in_courseid) || $in_courseid=="") {
$in_courseid = api_get_course_int_id();
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$question_id = Database::escape_string($in_questionid);
$sql = "SELECT c.title FROM $t_cattable qc INNER JOIN $table_category c
ON (qc.category_id = c.id AND qc.c_id = $in_courseid AND c.c_id = $in_courseid)
$question_id = Database::escape_string($question_id);
$sql = "SELECT * FROM $t_cattable qc INNER JOIN $table_category c ON (category_id = c.iid) WHERE question_id = '$question_id' AND qc.c_id = $in_courseid";
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
while ($row = Database::fetch_array($res, 'ASSOC')) {
$result[] = $row;
}
}
return $result;
}
/**
*
* @param int $question_id
* @param int $course_id
* @param bool $display_into_labels
* @return string
*/
public static function getCategoryNamesForQuestion($question_id, $course_id = null, $display_into_labels = true) {
$result = array(); // result
if (empty($course_id) || $course_id == "") {
$course_id = api_get_course_int_id();
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$question_id = intval($question_id);
$course_id = intval($course_id);
$sql = "SELECT c.title, c.iid FROM $t_cattable qc INNER JOIN $table_category c
ON (qc.category_id = c.iid AND qc.c_id = $course_id)
WHERE question_id = '$question_id' ";
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
while ($row = Database::fetch_array($res)) {
$result[] = $row['title'];
$cat = new Testcategory($row['iid']);
$result[] = $cat->parent_path;
}
}
@ -209,9 +338,12 @@ class Testcategory {
/**
* true if question id has a category
* @assert () === false
* @assert (null) === false
* @assert (-1) === false
*/
public static function isQuestionHasCategory($in_questionid) {
$category_list = Testcategory::getCategoryForQuestion($in_questionid);
public static function isQuestionHasCategory($question_ id) {
$category_list = Testcategory::getCategoryForQuestion($question_ id);
if (!empty($category_list)) {
return true;
}
@ -225,18 +357,16 @@ class Testcategory {
In this version, a question has only 1 category.
Return the category id, "" if none
*/
public static function getCategoryNameForQuestion($question_ id, $course_id = null) {
public static function getCategoryNameForQuestion($cat id, $course_id = null) {
if (empty($course_id) || $course_id == "") {
$course_id = api_get_course_int_id();
}
$course_id = intval($course_id);
$category_list = Testcategory::getCategoryForQuestion($question_id, $course_id);
$result = array(); // result
$result = array();
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$catid = Database::escape_string($catid);
$sql = "SELECT title FROM $t_cattable WHERE id= '$catid' AND c_id = $course_id";
$sql = "SELECT title FROM $t_cattable WHERE iid = '$catid' AND c_id = $course_id";
$res = Database::query($sql);
$data = Database::fetch_array($res);
if (Database::num_rows($res) > 0) {
@ -251,15 +381,34 @@ class Testcategory {
* return : array of category id (integer)
* @author hubert.borderiou 07-04-2011, Julio Montoya
*/
public static function getListOfCategoriesIDForTest($exercise_id) {
public static function getListOfCategoriesIDForTest($exercise_id, $grouped_by_category = true ) {
// parcourir les questions d'un test, recup les categories uniques dans un tableau
$categories_in_exercise = array();
$quiz = new Exercise();
$quiz->read($exercise_id);
$question_list = $quiz->selectQuestionList();
$exercise = new Exercise();
$exercise->setCategoriesGrouping($grouped_by_category);
$exercise->read($exercise_id);
$question_list = $exercise->selectQuestionList();
// the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
for ($i=1; $i < = count($question_list); $i++) {
$category_list = Testcategory::getCategoryForQuestion($question_list[$i]);
foreach ($question_list as $question_id) {
$category_list = Testcategory::getCategoryForQuestion($question_id);
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;
}
public static function getListOfCategoriesIDForTestObject($exercise_obj) {
// parcourir les questions d'un test, recup les categories uniques dans un tableau
$categories_in_exercise = array();
$question_list = $exercise_obj->selectQuestionList();
// the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ???
foreach ($question_list as $question_id) {
$category_list = Testcategory::getCategoryForQuestion($question_id);
if (!empty($category_list)) {
$categories_in_exercise = array_merge($categories_in_exercise, $category_list);
}
@ -277,14 +426,29 @@ class Testcategory {
* hubert.borderiou 07-04-2011
* @author function rewrote by jmontoya
*/
public static function getListOfCategoriesNameForTest($in_testid) {
$tabcatName = array();
$tabcatID = self::getListOfCategoriesIDForTest($in_testid);
for ($i=0; $i < count ( $ tabcatID ) ; $ i + + ) {
$cat = new Testcategory($tabcatID[$i]);
$tabcatName[$cat->id] = $cat->name;
public static function getListOfCategoriesNameForTest($exercise_id, $grouped_by_category = true) {
$result = array();
$categories = self::getListOfCategoriesIDForTest($exercise_id, $grouped_by_category);
foreach ($categories as $cat_id) {
$cat = new Testcategory($cat_id);
if (!empty($cat->id)) {
$result[$cat->id] = $cat->name;
}
return $tabcatName;
}
return $result;
}
public static function getListOfCategoriesForTest($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;
}
/**
@ -293,8 +457,8 @@ class Testcategory {
* return : integer
* hubert.borderiou 07-04-2011
*/
public static function getNumberOfCategoriesForTest($in_test id) {
return count(Testcategory::getListOfCategoriesIDForTest($in_test id));
public static function getNumberOfCategoriesForTest($exercise_ id) {
return count(Testcategory::getListOfCategoriesIDForTest($exercise_ id));
}
/**
@ -309,8 +473,8 @@ class Testcategory {
$exercise->read($exercise_id);
$question_list = $exercise->selectQuestionList();
// the array given by selectQuestionList start at indice 1 and not at indice 0 !!! ? ? ?
for ($i=1; $i < = count($question_list); $i++ ) {
$category_in_question = Testcategory::getCategoryForQuestion($question_list[$i] );
foreach ($question_list as $question_id ) {
$category_in_question = Testcategory::getCategoryForQuestion($question_id );
if (in_array($category_id, $category_in_question)) {
$number_questions_in_category++;
}
@ -329,9 +493,10 @@ class Testcategory {
$list_categories = Testcategory::getListOfCategoriesIDForTest($exercise_id);
if (!empty($list_categories)) {
for ($i=0; $i < count ( $ list_categories ) ; $ i + + ) {
if ($list_categories[$i] > 0) { // 0 = no category for this question
$nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($exercise_id, $list_categories[$i]);
foreach ($list_categories as $category_item) {
if ($category_item > 0) {
// 0 = no category for this question
$nbQuestionInThisCat = Testcategory::getNumberOfQuestionsInCategoryForTest($exercise_id, $category_item);
if ($nbQuestionInThisCat > $in_nbrandom) {
$nbquestionresult += $in_nbrandom;
@ -363,39 +528,48 @@ class Testcategory {
}
/**
* return an array of question_id for each category
* tabres[0] = array of question id with category id = 0 (i.e. no category)
* tabres[24] = array of question id with category id = 24
* In this version, a question has 0 or 1 category
* 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
*/
static function getQuestionsByCat($in_exerciceId ) {
$tabr es = array();
static function getQuestionsByCat($exerciceId, $check_in_question_list = array() ) {
$categori es = array();
$TBL_EXERCICE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
$TBL_QUESTION_REL_CATEGORY = Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY);
$in_exerciceId = intval($in_exerciceId);
$sql = "SELECT qrc.question_id, qrc.category_id FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
WHERE exercice_id=$in_exerciceId AND eq.question_id=qrc.question_id AND eq.c_id=".api_get_course_int_id()." AND eq.c_id=qrc.c_id ORDER BY category_id, question_id";
$exerciceId = intval($exerciceId);
$sql = "SELECT DISTINCT qrc.question_id, qrc.category_id
FROM $TBL_QUESTION_REL_CATEGORY qrc, $TBL_EXERCICE_QUESTION eq
WHERE exercice_id = $exerciceId AND
eq.question_id = qrc.question_id AND
eq.c_id =".api_get_course_int_id()." AND
eq.c_id = qrc.c_id
ORDER BY category_id, question_id";
$res = Database::query($sql);
while ($data = Database::fetch_array($res)) {
if (!is_array($tabres[$data['category_id']])) {
$tabres[$data['category_id']] = array();
}
$tabres[$data['category_id']][] = $data['question_id'];
}
return $tabres;
}
if (!empty($check_in_question_list)) {
if (!in_array($data['question_id'], $check_in_question_list)) {
continue;
}
}
if (!is_array($categories[$data['category_id']])) {
$categories[$data['category_id']] = array();
}
$categories[$data['category_id']][] = $data['question_id'];
}
return $categories;
}
/**
* return a tab of $in_number random elements of $in_tab
* Return an array of X elements of an array
*/
static function getNElementsFromArray($in_tab, $in_number) {
$tabres = $in_tab;
shuffle($tabres);
if ($in_number < count ( $ tabres ) ) {
$tabres = array_slice($tabres, 0, $in_number);
static function getNElementsFromArray($array, $random_number) {
shuffle($array);
if ($random_number < count ( $ array ) ) {
$array = array_slice($array, 0, $random_number);
}
return $tabres ;
return $array ;
}
/**
@ -412,24 +586,24 @@ class Testcategory {
}
/**
* sortTabByBracketLabel ($tabCategoryQuestions)
* key of $tabCategoryQuestions are the categopy id (0 for not in a category)
* key of $array are the categopy id (0 for not in a category)
* value is the array of question id of this category
* Sort question by Category
*/
static function sortTabByBracketLabel($in_tab ) {
static function sortCategoriesQuestionByName($array ) {
$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;
$category_array = array();
// tab of category name
while (list($cat_id, $tabquestion) = each($array)) {
$cat = new Testcategory($cat_id);
$category_array[$cat_id] = $cat->title;
}
reset($in_tab );
reset($array );
// sort table by value, keeping keys as they are
asort($tabCatName );
asort($category_array );
// keys of $tabCatName are keys order for $in_tab
while (list($key, $val) = each($tabCatName )) {
$tabResult[$key] = $in_tab [$key];
while (list($key, $val) = each($category_array )) {
$tabResult[$key] = $array [$key];
}
return $tabResult;
}
@ -538,7 +712,7 @@ class Testcategory {
if (empty($category_list)) {
return null;
}
$category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id);
$category_name_list = Testcategory::getListOfCategoriesNameForTest($exercise_id, false );
$table = new HTML_Table(array('class' => 'data_table'));
$table->setHeaderContents(0, 0, get_lang('Categories'));
@ -581,7 +755,138 @@ class Testcategory {
return null;
}
function get_all_categories() {
$table = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$sql = "SELECT * FROM $table ORDER BY title ASC";
$res = Database::query($sql);
while ($row = Database::fetch_array($res,'ASSOC')) {
$array[] = $row;
}
return $array;
}
function get_category_exercise_tree($exercise_id, $course_id, $order = null) {
$table = Database::get_course_table(TABLE_QUIZ_REL_CATEGORY);
$table_category = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$sql = "SELECT * FROM $table qc INNER JOIN $table_category c ON (category_id = c.iid) WHERE exercise_id = {$exercise_id} AND qc.c_id = {$course_id} ";
if (!empty($order)) {
$sql .= "ORDER BY $order";
}
$result = Database::query($sql);
if (Database::num_rows($result)) {
while ($row = Database::fetch_array($result, 'ASSOC')) {
$list[$row['category_id']] = $row;
}
if (!empty($list)) {
$array = $this->sort_tree_array($list);
$this->create_tree_array($array);
}
return $this->category_array_tree;
}
return false;
}
function get_category_tree_by_type($type) {
$course_id = api_get_course_int_id();
if ($type == 'global') {
$course_condition = 'c_id = 0';
} else {
$course_condition = " c_id IN ('$course_id', '0') ";
}
$t_cattable = Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY);
$sql = "SELECT * FROM $t_cattable WHERE $course_condition";
$res = Database::query($sql);
$categories = array();
while ($row = Database::fetch_array($res, 'ASSOC')) {
$row['id'] = $row['iid'];
$categories[] = $row;
}
if (!empty($categories)) {
$array = $this->sort_tree_array($categories);
$this->create_tree_array($array);
}
return $this->category_array_tree;
}
function return_category_form($exercise_obj) {
$categories = $this->getListOfCategoriesForTest($exercise_obj);
if (!empty($categories)) {
$array = $this->sort_tree_array($categories);
$this->create_tree_array($array);
}
$saved_categories = $exercise_obj->get_categories_in_exercise();
if (!empty($this->category_array_tree)) {
$nbQuestionsTotal = $exercise_obj->getNumberQuestionExerciseCategory();
$original_grouping = $exercise_obj->categories_grouping;
$exercise_obj->setCategoriesGrouping(true);
$real_question_count = count($exercise_obj->selectQuestionList(true));
$exercise_obj->setCategoriesGrouping($original_grouping);
$warning = null;
if ($nbQuestionsTotal != $real_question_count) {
$warning = Display::return_message(get_lang('CheckThatYouHaveEnoughQuestionsInYourCategories'), 'warning');
}
}
endif;
$return .= $warning;
$return .= '< table class = "data_table" > ';
$return .= '< tr > ';
$return .= '< th height = "24" > ' . get_lang('Categories') . '< / th > ';
$return .= '< th width = "70" height = "24" > ' . get_lang('Number') . '< / th > < / tr > ';
for ($i = 0; $i < count ( $ this- > category_array_tree); $i++) {
$cat_id = $this->category_array_tree[$i]['id'];
$return .= '< tr > ';
$return .= '< td > ';
$style = 'margin-left:' . $this->category_array_tree[$i]['depth'] * 20 . 'px; margin-right:10px;';
$return .= Display::div($this->category_array_tree[$i]['title'], array('style' => $style));
$return .= '< / td > ';
$return .= '< td > ';
$value = isset($saved_categories) & & isset($saved_categories[$cat_id]) ? $saved_categories[$cat_id]['count_questions'] : 0;
$return .= '< input name = "category['.$cat_id.']" value = "' .$value.'" / > ';
$return .= '< / td > ';
$return .= '< / tr > ';
}
$return .= '< / table > ';
return $return;
}
}
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;
}
public function create_tree_array($array, $parent = 0, $depth = -1, $tmp = array()) {
if (is_array($array)) {
for ($i = 0; $i < count ( $ array ) ; $ i + + ) {
//var_dump($array[$i], $parent, $tmp);
if ($array[$i]['parent_id'] == $parent) {
if (!in_array($array[$i]['parent_id'], $tmp)) {
$tmp[] = $array[$i]['parent_id'];
$depth++;
}
$row = $array[$i];
$row['id'] = $array[$i]['iid'];
$row['depth'] = $depth;
$this->category_array_tree[] = $row;
$this->create_tree_array($array, $array[$i]['iid'], $depth, $tmp);
}
}
}
}
}