getUserSubmission_children fix
[mirrors/Kyberia-bloodline.git] / wwwroot / backend / mysql / backend.inc
index a3ee375bd645043ceb6950fa60464bbbc13935a1..8b21936a6d4e737e3b84f099d9cffa8cc7dc19fb 100644 (file)
@@ -3,10 +3,10 @@ class nodes {
 // All mysql code should go here
 // Split into multiple files if needed
 
-function processContent($node_content) { return processContent_hack($node_content); } //XXX TODO FIXME HACK
+public static function processContent($node_content) { return processContent_hack($node_content); } //XXX TODO FIXME HACK
 
 // Called for every node view. Updates node views, neurons, 
-function update_nodes($user_id,$node_id,$referer_id) {
+public static function update_nodes($user_id,$node_id,$referer_id) {
        global $node,$db,$error;
        $db->update("update nodes set node_views=node_views+1 where".
                     "node_id='".$node_id."'");
@@ -35,7 +35,7 @@ function update_nodes($user_id,$node_id,$referer_id) {
 }
 
 
-function putNode($what,$where,$checkpermissions=true) {
+public static function putNode($what,$where,$checkpermissions=true) {
        global $db,$error,$error_messages;
        $user_id = $_SESSION['user_id'];
        if (!$user_id) {
@@ -64,15 +64,18 @@ function putNode($what,$where,$checkpermissions=true) {
        $db->query("update nodes set lastdescendant_created=NOW(),node_children_count=node_children_count+1 where node_id='$where'");
 }
 
-function addNode($params) {
+public static function addNode($params) {
         global $db,$node,$error,$error_messages;
         $parent_id=$params['node_parent'];
 
+        if ($params['flag']=='registration') $params['node_creator']=UBIK_ID;
+
         if (!is_numeric($parent_id)) {
                 $parent_id=$node['node_id'];
         }
 
         $parent_permissions=permissions::checkPerms($parent_id);
+
         if (!$parent_permissions['w']) {
                 $error=$error_messages['WRITE_PERMISSION_ERROR'];
                 logger::log('add','error','WRITE_PERMISSION_ERROR');
@@ -84,11 +87,8 @@ function addNode($params) {
         $kset->next();
         $user_k=$kset->getString('user_k');
 
-        if ($params['flag']=='registration') $params['node_creator']=UBIK_ID;
-
         if (!$user_k && $params['node_creator']!=UBIK_ID) {
                 $error=$error_messages['K_SPENT'];
-                return false;
         }
 
 
@@ -165,7 +165,7 @@ node_vector='".$params['node_vector']."'";
 }
 
 
-function getUserByLogin($login) {
+public static function getUserByLogin($login) {
        global $error, $error_messages;
        $q2="select user_id from users where login='".$login."'";
        $userset=$db->query($q2);
@@ -178,19 +178,19 @@ function getUserByLogin($login) {
        }
 }
 
-static function getNodeIdByName($name, $external_link=false) {
+public static function getNodeIdByName($name, $external_link=false) {
        global $db;
 
-       $qh = sprintf('select node_id from nodes where node_name = "%s"', mysql_real_escape_string($name));
+       $qh = sprintf('select node_id from nodes where node_name = "%s"', db_escape_string($name));
        if ($external_link)
-               $qh .= sprintf(' and external_link="%s"', mysql_real_escape_string($external_link));
+               $qh .= sprintf(' and external_link="%s"', db_escape_string($external_link));
 
        $set = $db->query($qh);
        $set->next();
        return $set->getString('node_id');
 }
 
-function getNodeById($node_handle,$user_id, $table_name="nodes") {
+public static function getNodeById($node_handle,$user_id, $table_name="nodes") {
        global $db, $error;
        $q="select length(concat($table_name.node_vector)) as
 vector_depth,$table_name.*,$table_name.node_creator as
@@ -219,7 +219,7 @@ where $table_name.node_id='$node_handle'";
 }
 
 
-function redirByName($node_handle) {
+public static function redirByName($node_handle) {
        global $db, $error;
        $user_id=$_SESSION['user_id'];
        $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'");
@@ -239,7 +239,7 @@ function redirByName($node_handle) {
 
 }
 
-function getNodesByName($node_handle) {
+public static function getNodesByName($node_handle) {
        global $db, $error;
        $q="select nodes.* from nodes where node_name='$node_handle%'";
 
@@ -254,14 +254,14 @@ function getNodesByName($node_handle) {
 
 
 
-function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
+public static function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
        global $db, $error, $node;
        $node_handle=$node['node_id'];
 
        $q="select users.*,nodes.* from nodes
 left join users on users.user_id=nodes.node_creator where
 node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit";
-       echo $q;
+       #echo $q;
        $result=$db->query($q);
 
        while ($result->next()) {
@@ -273,7 +273,42 @@ node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit";
 }
 
 
-function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
+public static function GetUserSubmissionsChildren($user_id,$limit=23,$offset=0,$orderby='') {
+  global $db;
+
+       if (!is_numeric($user_id)) {
+               return -1;
+       }
+       if (!is_numeric($limit)) {
+               return -1;
+       }
+       if (!is_numeric($offset)) {
+               return -1;
+       }
+
+       // XXX orderby mysql escape
+
+       $q = "select n1.* from nodes as n1 join 
+               (select node_id,node_creator from nodes where node_creator='$user_id')
+               as j2 on n1.node_parent=j2.node_id 
+               join users as j3 on j3.user_id=n1.node_creator order by node_created 
+               $orderby LIMIT $offset,$limit";
+
+//       $q="select users.*,nodes.* from nodes
+// left join users on users.user_id=nodes.node_creator where
+// node_creator='$user_id' order by node_created $orderby LIMIT $offset,$limit";
+       //$q="select * from nodes LIMIT 10,10";
+  #echo $q;
+  $result=$db->query($q);
+
+  while ($result->next()) {
+    $array[]=addBase36id($result->getRecord());
+  }
+
+  return $array;
+}
+
+public static function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
        global $db, $error, $node;
        $node_handle=$node['node_id'];
 
@@ -291,7 +326,7 @@ function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTI
 
 
 
-function getNodeAccessData() {
+public static function getNodeAccessData() {
        global $node,$db;
        $q="select users.login,node_access.* from node_access left join users on users.user_id=node_access.user_id where node_id='".$node['node_id']."' and node_permission!=''";
        $result=$db->query($q);
@@ -307,7 +342,7 @@ function getNodeAccessData() {
 
 // Simple internal function to set node parrent
 
-function setParent($node_id,$parent_id) {
+public static function setParent($node_id,$parent_id) {
        global $db,$node,$error,$error_messages;
 
        if (!is_numeric($parent_id)) {
@@ -328,7 +363,7 @@ function setParent($node_id,$parent_id) {
 
 // Get nodes sorted by weight_k specific to user
 
-function getKNeurons($user_id,$interval) {
+public static function getKNeurons($user_id,$interval) {
        global $db,$node,$error,$error_messages;
 
        $q="call k_neurons('$user_id','$interval')";
@@ -341,4 +376,77 @@ function getKNeurons($user_id,$interval) {
        return $k_array;
 }
 
+// setSynapse
+
+// Set synapse weight. 
+// If synapse does not exists, create a new one.
+// Secure.
+// Returns true on sucess, otherwise false.
+
+public static function setSynapse($params){
+       global $db,$node,$error,$error_messages;
+
+       // security check
+       if ((!is_numeric($params['src'])) 
+               or (!is_numeric($params['dst']))
+               or (!is_numeric($params['weight']))) {
+
+               return false;
+       }
+
+       $src=$params['src'];
+       $dst=$params['dst'];
+       $weight=$params['weight'];
+
+       // weight could be only positive
+       if ( $weight < 0) {
+               return false;
+       }
+
+       // if already exists
+       $q="select count(src) from neurons where dst ='$dst' and src = '$src'";
+       $set=$db->query($q);
+       $set->next();
+       $isrc=$set->getString('count(src)');
+
+       if ( $isrc > 0 ) {
+               $q="update neurons set synapse_weight='$weight' 
+                       where dst = '$dst' and src = '$src'";
+       } else { 
+               // FIXME no vector set
+               $q="insert into neurons values('$weight','$dst','$src',0,'synapse',
+                                               CURRENT_TIMESTAMP(),now(),NULL,$src)";
+       }
+       $set=$db->query($q);
+
+       return true;
+}
+
+// getSynapseWeight
+
+// If synapse does not exists, weight is 1
+// Secure.
+// Returns synapse weight (from user to node)
+
+public static function getSynapseWeight($user_id,$node_id){
+       global $db;
+
+       if (!is_numeric($user_id)) 
+               { return -1; }
+       if (!is_numeric($node_id)) 
+               { return -1; }
+
+       $set=$db->query("select synapse_weight from neurons where src =".
+               $user_id." and dst = ".$node_id." and link='synapse'");
+
+       $set->next();
+       $synapse_weight=$set->getString('synapse_weight');
+
+       if (! ($synapse_weight) ) { $synapse_weight = 1;}       
+
+       return $synapse_weight;
+
+}
 }
+
+?>
This page took 0.149034 seconds and 4 git commands to generate.