$result=api_sql_query("SELECT t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count,COUNT(DISTINCT t3.code) AS nbr_courses FROM $tbl_category t1 LEFT JOIN $tbl_category t2 ON t1.code=t2.parent_id LEFT JOIN $tbl_course t3 ON t3.category_code=t1.code WHERE t1.parent_id ".(empty($category)?"IS NULL":"='$category'")." GROUP BY t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count ORDER BY t1.tree_pos",__FILE__,__LINE__);
$myquery="SELECT t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count,COUNT(DISTINCT t3.code) AS nbr_courses FROM $tbl_category t1 LEFT JOIN $tbl_category t2 ON t1.code=t2.parent_id LEFT JOIN $tbl_course t3 ON t3.category_code=t1.code WHERE t1.parent_id ".(empty($category)?"IS NULL":"='$category'")." GROUP BY t1.name,t1.code,t1.parent_id,t1.tree_pos,t1.children_count ORDER BY t1.tree_pos";
$result=api_sql_query("SELECT parent_id,tree_pos FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
if($row=mysql_fetch_array($result))
if($row=Database::fetch_array($result))
{
if(!empty($row['parent_id']))
{
api_sql_query("UPDATE $tbl_course SET category_code='$row[parent_id]' WHERE category_code='$node'",__FILE__,__LINE__);
api_sql_query("UPDATE $tbl_category SET parent_id='$row[parent_id]' WHERE parent_id='$node'",__FILE__,__LINE__);
api_sql_query("UPDATE $tbl_course SET category_code='".$row['parent_id']."' WHERE category_code='$node'",__FILE__,__LINE__);
api_sql_query("UPDATE $tbl_category SET parent_id='".$row['parent_id']."' WHERE parent_id='$node'",__FILE__,__LINE__);
}
else
{
@ -253,7 +263,7 @@ function deleteNode($node)
api_sql_query("UPDATE $tbl_category SET parent_id=NULL WHERE parent_id='$node'",__FILE__,__LINE__);
}
api_sql_query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '$row[tree_pos]'",__FILE__,__LINE__);
api_sql_query("UPDATE $tbl_category SET tree_pos=tree_pos-1 WHERE tree_pos > '".$row['tree_pos']."'",__FILE__,__LINE__);
api_sql_query("DELETE FROM $tbl_category WHERE code='$node'",__FILE__,__LINE__);
if(!empty($row['parent_id']))
@ -268,17 +278,20 @@ function addNode($code,$name,$canHaveCourses,$parent_id)
global $tbl_category;
$canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
$code = Database::escape_string($code);
$name = Database::escape_string($name);
$parent_id = Database::escape_string($parent_id);
$result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
if(mysql_num_rows($result))
if(Database::num_rows($result))
{
return false;
}
$result=api_sql_query("SELECT MAX(tree_pos) AS maxTreePos FROM $tbl_category",__FILE__,__LINE__);
$row=mysql_fetch_array($result);
$row=Database::fetch_array($result);
$tree_pos=$row['maxTreePos']+1;
@ -294,12 +307,15 @@ function editNode($code,$name,$canHaveCourses,$old_code)
global $tbl_category;
$canHaveCourses=$canHaveCourses?'TRUE':'FALSE';
$code = Database::escape_string($code);
$name = Database::escape_string($name);
$old_code = Database::escape_string($old_code);
if($code != $old_code)
{
$result=api_sql_query("SELECT 1 FROM $tbl_category WHERE code='$code'",__FILE__,__LINE__);
if(mysql_num_rows($result))
if(Database::num_rows($result))
{
return false;
}
@ -313,14 +329,17 @@ function editNode($code,$name,$canHaveCourses,$old_code)
function moveNodeUp($code,$tree_pos,$parent_id)
{
global $tbl_category;
$code = Database::escape_string($code);
$tree_pos = Database::escape_string($tree_pos);
$parent_id = Database::escape_string($parent_id);
$result=api_sql_query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id ".(empty($parent_id)?"IS NULL":"='$parent_id'")." AND tree_pos<'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1",__FILE__,__LINE__);
if(!$row=mysql_fetch_array($result))
if(!$row=Database::fetch_array($result))
{
$result=api_sql_query("SELECT code,tree_pos FROM $tbl_category WHERE parent_id ".(empty($parent_id)?"IS NULL":"='$parent_id'")." AND tree_pos>'$tree_pos' ORDER BY tree_pos DESC LIMIT 0,1",__FILE__,__LINE__);
if(!$row=mysql_fetch_array($result))
if(!$row=Database::fetch_array($result))
{
return false;
}
@ -333,10 +352,10 @@ function moveNodeUp($code,$tree_pos,$parent_id)
function updateFils($category)
{
global $tbl_category;
$category = Database::escape_string($category);
$result=api_sql_query("SELECT parent_id FROM $tbl_category WHERE code='$category'",__FILE__,__LINE__);
if($row=mysql_fetch_array($result))
if($row=Database::fetch_array($result))
{
updateFils($row['parent_id']);
}
@ -349,14 +368,14 @@ function updateFils($category)
function compterFils($pere,$cpt)
{
global $tbl_category;
$pere = Database::escape_string($pere);
$result=api_sql_query("SELECT code FROM $tbl_category WHERE parent_id='$pere'",__FILE__,__LINE__);