? cre.info Index: cre.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cre/cre.install,v retrieving revision 1.12 diff -u -p -r1.12 cre.install --- cre.install 16 Dec 2006 03:52:22 -0000 1.12 +++ cre.install 10 Jan 2007 11:15:40 -0000 @@ -1,11 +1,20 @@ id + 1); - //_cre_load_all_diff_avg(); - - _cre_user_average(); - + //_cre_load_all_diff_avg(); + + _cre_user_average(); } function cre_update_1() { - $db_average = db_query("CREATE TABLE {cre_average_vote}( - uid int(10) unsigned default 0, - sum float(10) unsigned default 0, - count int(10) unsigned default 0, - PRIMARY KEY (uid) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); - update_sql("ALTER TABLE {cre_similarity_matrix} DROP PRIMARY KEY"); - - update_sql("ALTER TABLE {cre_similarity_matrix} ADD INDEX content_ids(content_id1,content_id2);"); - update_sql("ALTER TABLE {cre_similarity_matrix} ADD INDEX content_type(content_type1,content_type2);"); - update_sql("ALTER TABLE {cre_similarity_matrix} ADD COLUMN similarity_id int(10) NOT NULL auto_increment, ADD PRIMARY KEY(similarity_id);"); + $db_average = db_query( + 'CREATE TABLE {cre_average_vote}( + uid int(10) unsigned default 0, + sum float(10) unsigned default 0, + count int(10) unsigned default 0, + PRIMARY KEY (uid) + ) /*!40100 DEFAULT CHARACTER SET utf8 */;'); + + update_sql('ALTER TABLE {cre_similarity_matrix} DROP PRIMARY KEY'); + update_sql('ALTER TABLE {cre_similarity_matrix} ADD INDEX content_ids(content_id1,content_id2);'); + update_sql('ALTER TABLE {cre_similarity_matrix} ADD INDEX content_type(content_type1,content_type2);'); + update_sql('ALTER TABLE {cre_similarity_matrix} ADD COLUMN similarity_id int(10) NOT NULL auto_increment, ADD PRIMARY KEY(similarity_id);'); _cre_user_average(); - } \ No newline at end of file Index: cre.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cre/cre.module,v retrieving revision 1.15 diff -u -p -r1.15 cre.module --- cre.module 16 Dec 2006 03:52:22 -0000 1.15 +++ cre.module 10 Jan 2007 11:15:40 -0000 @@ -1,27 +1,53 @@ 'textfield', - '#title' => t('Number of users to process in one install run'), - '#description' => t('Specifies the number of votes to process in one cron run to further install the module'), - '#default_value' => variable_get('cre_max_install_votes', 5), - ); - $form['cre_query_type'] = array( +/** + * Implementation of hook_menu() + */ +function cre_menu($may_cache) { + $items = array(); + if ($may_cache) { + $items[] = array( + 'path' => 'admin/voting/cre', + 'title' => t('CRE'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + 'callback' => 'drupal_get_form', + 'callback arguments' => '_cre_settings', + 'description' => t('Manage CRE settings') + ); + } + return $items; +} + +/** + * Form builder function for admin settings (was hook_settings() in Drupal 4.7). + */ +function _cre_settings() { + $form['cre_max_install_votes'] = array( + '#type' => 'textfield', + '#title' => t('Number of users to process in one install run'), + '#description' => t('Specifies the number of votes to process in one cron run to further install the module'), + '#default_value' => variable_get('cre_max_install_votes', 5), + ); + $form['cre_query_type'] = array( '#type' => 'radios', '#title' => t('Select a query type'), '#description' => t('On sites that contain many votes, it is oftentimes much faster you use basic query. It lacks some accuracy but is considerably faster'), @@ -30,84 +56,26 @@ function cre_settings() { 'accurate' => t('Accurate query'), 'fast' => t('Faster query'), 'points' => t('VotingAPI Point System Query') - ), + ) ); - - return $form; -} - -/** - * this is a check for module dependencies. the only way we - * can ensure this check happening when the module is initially - * enabled is to insert the check for when the form is initially - * built, which will also be caught when the admin/module page is - * reloaded upon submission. this means we never want to call this - * function when the form has been submitted, so make sure there's - * no $_POST. - */ -function cre_form_alter($form_id, &$form) { - if ($form_id == 'system_modules' && !$_POST) { - cre_system_module_validate($form); - } + return system_settings_form($form); } /** - * Validates module dependencies for the module. - * - * @param $form The form array passed from hook_form_alter. - * - * Set the $module variable to a string which is the name of the module, minus - * the .module extension. Set $dependencies to an array of module names which - * the module is dependent on--each element is a string which is the module name - * minus the .module extension. Note that this will not check for any dependencies - * for the modules this module depends on--only those that are explicitly listed in - * the $dependencies array. - */ -function cre_system_module_validate(&$form) { - - $module = 'cre'; - $dependencies = array('votingapi'); - - foreach ($dependencies as $dependency) { - if (!in_array($dependency, $form['status']['#default_value'])) { - $missing_dependency = TRUE; - $missing_dependency_list[] = $dependency; - } - } - - if (in_array($module, $form['status']['#default_value']) && isset($missing_dependency)) { - db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module); - $key = array_search($module, $form['status']['#default_value']); - unset($form['status']['#default_value'][$key]); - drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error'); - - } - if (!file_exists(drupal_get_path('module', 'cre') . '/cre_query_obj.inc')) { - drupal_set_message(drupal_get_path('module', 'cre') . '/cre_query_obj.inc'); - db_query("UPDATE {system} SET status = 0 WHERE type = 'module' AND name = '%s'", $module); - $key = array_search($module, $form['status']['#default_value']); - unset($form['status']['#default_value'][$key]); - drupal_set_message(t('CRE requires that the file: cre_query_obj.inc be in the same directory as cre.module','error')); - } - -} - -/* - * Creates or updates a record in cre_similarity_matrix. Maintains the sum and count - * Therefore, the avgerage difference between the two pieces of content can be calculated + * Creates or updates a record in the cre_similarity_matrix. Maintains the sum and count. + * Therefore, the average difference between the two pieces of content can be calculated. * * @param $userID - * The uid of the vote + * The uid of the vote * * @param $itemID - * content_id of the new vote + * content_id of the new vote * * @return - * NONE - * + * NONE */ function cre_modify_avg_difference($userID, $itemID, $content_type) { - // Run my magik! put the values in cre_similarity_matrix table + // Run my magic! put the values in cre_similarity_matrix table. $db_result = db_query("SELECT DISTINCT r.content_id, r2.value-r.value as rating_difference, r.content_type as content_type1, r2.content_type as content_type2 FROM {votingapi_vote} r, {votingapi_vote} r2 @@ -116,322 +84,276 @@ function cre_modify_avg_difference($user drupal_set_message(t('ERROR')); } else { - while($row = db_fetch_array($db_result)) { - $other_node=$row['content_id']; - $rating_difference=$row['rating_difference']; - - // check to see if the pair of content ($itemID and $other_node) are already in the cre_similarity_matrix table - if (db_num_rows(db_query("SELECT content_id1 from {cre_similarity_matrix} WHERE content_id1=%d AND content_id2=%d AND content_type1='%s'", $itemID, $other_node, $content_type)) > 0) { - //update the two rows - //drupal_set_message(t('updated!')); - db_query("UPDATE {cre_similarity_matrix} SET count=count+1, sum=sum+%d - WHERE content_id1=%d AND content_id2=%d AND content_type1 = '%s'", $rating_difference, $itemID, $other_node, $content_type); - // update the second row ONLY if the two nodes are different + while($row = db_fetch_array($db_result)) { + $other_node=$row['content_id']; + $rating_difference=$row['rating_difference']; + + // Check to see if the pair of content ($itemID and $other_node) are already in the cre_similarity_matrix table. + if (db_num_rows(db_query("SELECT content_id1 from {cre_similarity_matrix} WHERE content_id1=%d AND content_id2=%d AND content_type1='%s'", $itemID, $other_node, $content_type)) > 0) { + // Update the two rows. + db_query("UPDATE {cre_similarity_matrix} SET count=count+1, sum=sum+%d WHERE content_id1=%d AND content_id2=%d AND content_type1 = '%s'", $rating_difference, $itemID, $other_node, $content_type); + // Update the second row ONLY if the two nodes are different. if ($itemID != $other_node) { - db_query("UPDATE {cre_similarity_matrix} SET count=count+1, sum=sum-%d - WHERE content_id1=%d AND content_id2=%d AND content_type1 = '%s'", $rating_difference, $other_node, $itemID, $content_type); + db_query("UPDATE {cre_similarity_matrix} SET count=count+1, sum=sum-%d WHERE content_id1=%d AND content_id2=%d AND content_type1 = '%s'", $rating_difference, $other_node, $itemID, $content_type); } - } - // if this is a new 'pairing' create two rows - else { + } + else { + // If this is a new 'pairing' create two rows. $content_type1 = $row['content_type1']; $content_type2 = $row['content_type2']; - // insert 2 rows only if the nodes are different + // Insert 2 rows only if the nodes are different. db_query("INSERT INTO {cre_similarity_matrix} (content_id1, content_id2, content_type1, content_type2, count, sum) VALUES (%d, %d, '%s', '%s', 1, %d)", $itemID, $other_node, $content_type1, $content_type2, $rating_difference); - if ($itemID != $other_node) { db_query("INSERT INTO {cre_similarity_matrix} (content_id1, content_id2, content_type1, content_type2, count, sum) VALUES(%d, %d, '%s', '%s', 1, %d)", $other_node, $itemID, $content_type2, $content_type1, -$rating_difference); } - } - } + } + } } } /** - * Returns an array of the top n content + * Returns an array of the top n content. * * @param $uid - * user id for the personalized recommendation + * User id for the personalised recommendation. * - *@param $calling_module - *specifies the name of the calling module. This string is then used to invoke hook_cre_query() in that module - *and only that module. + * @param $calling_module + * Specifies the name of the calling module. This string is then used to invoke hook_cre_query() in that module and only that module. * * @param $n default 10 - * specifies the number of contents to return + * Specifies the number of contents to return. * * @param $target_type - * the type of content that the calling function would like + * The type of content that the calling function would like. * * @param $tag default 'vote' - * same as in votingapi. See votingapi + * Same as in votingapi. See votingapi. * * @param $reference_type default NULL - * When set only those votes whos content_type is equal to this - * will be considered for the recommendations. + * When set only those votes for those content_types is equal to this will be considered for the recommendations. * * @return - * returns an array of objs with content_id and score + * Returns an array of objects with content_id and score. */ -function cre_top($uid, $callback, $n=10, $target_type = 'node', $tag = 'vote',$reference_type = NULL) -{ - // get the users average vote - - - // create query obj - $query = new _cre_query($uid, $target_type, $tag,$reference_type); - - // invoke call back function - $function = $callback; - $result = ($function($query,$uid)); - - // execute query obj - $query_string = $query->execute(); - if (!$query_string) { - return NULL; - } - $db_result = db_query($query_string); - - - $count = 0; - - if (!$db_result) { - drupal_set_message(t('query failed '. $callback)); - return; - } - - $qtype = variable_get('cre_query_type', 'accurate'); - if ($qtype == 'accurate' || 'fast') { - $value_type = 'percent'; - } - else { - $value_type = 'points'; - } - - while($matrixobj = db_fetch_object($db_result)) { - if($count >= $n) { - break; - } - // do a check to see if the content was rated by the uid - $content_id = $matrixobj->content_id; - $user_check_result = votingapi_get_vote($target_type, $content_id,$qtype, $tag, $uid); - - if(!$user_check_result) { - $return_value[] = $matrixobj; - $count++; - } - } - if(isset($return_value)) { - return $return_value; - } - else - { - // do something special when user has rated all nodes - // return top rated nodes? - //return "No nodes"; - } +function cre_top($uid, $callback, $n=10, $target_type = 'node', $tag = 'vote', $reference_type = NULL) { + // Get the users average vote. + + // Create query object + $query = new _cre_query($uid, $target_type, $tag, $reference_type); + + // Invoke call back function. + $function = $callback; + $result = ($function($query, $uid)); + + // Execute query object. + $query_string = $query->execute(); + if (!$query_string) { + return NULL; + } + $db_result = db_query($query_string); + + $count = 0; + + if (!$db_result) { + drupal_set_message(t('query failed '. $callback)); + return; + } + + $qtype = variable_get('cre_query_type', 'accurate'); + if ($qtype == 'accurate' || 'fast') { + $value_type = 'percent'; + } + else { + $value_type = 'points'; + } + + while($matrixobj = db_fetch_object($db_result)) { + if ($count >= $n) { + break; + } + // Do a check to see if the content was rated by the uid. + $content_id = $matrixobj->content_id; + $user_check_result = votingapi_get_vote($target_type, $content_id, $value_type, $tag, $uid); + + if (!$user_check_result) { + $return_value[] = $matrixobj; + $count++; + } + } + if (isset($return_value)) { + return $return_value; + } + else { + // Do something special when user has rated all nodes return top rated nodes? + return; // No nodes to return. + } } -/* +/** * Returns an array of content objs - * + * * @param $n - * The number of objs to return - * + * The number of objs to return. + * * @param $nid - * content_id that the return objects will be similar too + * content_id that the return objects will be similar too. * * @param $target_type - * Type of content to return from this function + * Type of content to return from this function. * * @param $tag - * votingapi tag. See Votingapi.module + * votingapi tag. See Votingapi.module. * * @param $reference_type - * specifies the type of content that will be only be used to determine the revelenance of - * a certain piece of content + * Specifies the type of content that will be only be used to determine the relevance of a certain piece of content. * * @return - * $return_value[] is an array of objs that have two fields, content_id and average. - * -*/ - -function cre_similar($n,$nid,$target_type='node',$tag='vote',$reference_type = NULL) // reference_type is sets which content_types are allowed to be reference when determining score -{ - // need to allow for reference_type to be an array! + * $return_value[] is an array of objs that have two fields, content_id and average. + */ +function cre_similar($n, $nid, $target_type='node', $tag='vote', $reference_type = NULL) { + // Reference_type are sets which content_types are allowed to be reference when determining score needed to allow for reference_type to be an array. - if(isset($reference_type)) { - $sql_result = db_query("SELECT d.content_id2 as 'content_id', (d.sum / d.count) AS 'average' + if (isset($reference_type)) { + $sql_result = db_query("SELECT d.content_id2 as 'content_id', (d.sum / d.count) AS 'average' FROM {cre_similarity_matrix} d WHERE d.content_id1=%d AND d.content_id2 <> %d AND content_type1='%s' AND content_type2='%s' - ORDER BY (sum/count) DESC LIMIT %d",$nid,$nid, $reference_type,$target_type,$n); - } - else { - $sql_result = db_query("SELECT d.content_id2 as 'content_id', (d.sum / d.count) AS 'average' + ORDER BY (sum/count) DESC LIMIT %d", $nid, $nid, $reference_type, $target_type, $n); + } + else { + $sql_result = db_query("SELECT d.content_id2 as 'content_id', (d.sum / d.count) AS 'average' FROM {cre_similarity_matrix} d WHERE d.content_id1=%d AND d.content_id2 <> %d AND content_type2='%s' - ORDER BY (sum/count) DESC LIMIT %d",$nid,$nid,$target_type,$n); - } - while($recommend_obj = db_fetch_object($sql_result)) { - $return_value[] = $recommend_obj; - } - return $return_value; + ORDER BY (sum/count) DESC LIMIT %d", $nid, $nid, $target_type, $n); + } + while($recommend_obj = db_fetch_object($sql_result)) { + $return_value[] = $recommend_obj; + } + return $return_value; +} +/** + * Implementation of votingapi's hook_insert() + */ +function cre_votingapi_insert(&$vote) { + if ($vote->value_type != 'percent' && $vote->value_type != 'points') { + return; + } + + // Run my magic! + // $vote obj has all the fields for a vote record! extract uid, nid. + cre_modify_avg_difference($vote->uid, $vote->content_id, $vote->content_type); + + // Update cre_average_vote table. + if (db_num_rows(db_query("SELECT uid from {cre_average_vote} where uid = %d", $vote->uid)) > 0) { + $db_result = db_query("UPDATE {cre_average_vote} SET count = count + 1, sum = sum + %d WHERE uid = %d", $vote->value, $vote->uid); + } + else { + db_query("INSERT INTO {cre_average_vote} (uid, sum, count) VALUES (%d, %d, %d)", $vote->uid, $vote->value, 1); + } + return; } -/* - * implementation of votingapi's hook_insert() -*/ - -function cre_votingapi_insert(&$vote) -{ - if ($vote->value_type != 'percent' && $vote->value_type != 'points') { - return; - } - // run my magik! - // $vote obj has all the fields for a vote record! extract uid, nid - cre_modify_avg_difference($vote->uid,$vote->content_id, $vote->content_type); - - // update cre_average_vote table - if (db_num_rows(db_query("SELECT uid from {cre_average_vote} where uid = %d",$vote->uid)) > 0) { - $db_result = db_query("UPDATE {cre_average_vote} SET count = count + 1, sum = sum + %d - WHERE uid = %d",$vote->value,$vote->uid); - } - else { - db_query("INSERT INTO {cre_average_vote} (uid, sum,count) VALUES (%d,%d,%d)",$vote->uid,$vote->value,1); - } - return; -} - -/* - * implementation of votingapi's hook_update -*/ - -function cre_votingapi_update(&$vote,$new_value) -{ - // when a vote is updated need to just adjust sum NOT COUNT!! - // for all records in cre_similarity_matrix where content_id1 or content_id2 equals vote->content_id - // and the adjustment to sum MUST BE THE RATING_DIFFERENCE - - $rating_difference = $new_value-$vote->value; - //drupal_set_message(t('difference = ' . $rating_difference)); - - db_query("UPDATE {cre_similarity_matrix} SET sum=sum+%d - WHERE content_id1=%d AND content_type1 = '%s'", $rating_difference, $vote->content_id, $vote->content_type); - - db_query("UPDATE {cre_similarity_matrix} SET sum=sum-%d - WHERE content_id2=%d AND content_type2 = '%s'",$rating_difference, $vote->content_id, $vote->content_type); - - // update sum and not count in cre_average_vote - if ($vote->value > $new_value) { - $difference = $vote->value - $new_value; - $db_average = db_query("UPDATE {cre_average_vote} SET sum = sum - %d - WHERE uid = %d",$difference,$vote->uid); - } - else { - $difference = $new_value - $vote->value; - $db_average = db_query("UPDATE {cre_average_vote} SET sum = sum + %d - WHERE uid = %d",$difference, $vote->uid); - } - -} -/* - * implementation of votingapi's hook_delete -*/ -function cre_votingapi_delete(&$vote) -{ - // modifiy the cre_similarity_matrix table by subratcting from count - // and subtracting vote value from sum - // OVER ALL REFERENCES TO THE CONTENT_ID - // THINK !!! reverse of build!! - $userID = $vote->uid; - $itemID = $vote->content_id; - $db_result = db_query("SELECT DISTINCT r.content_id, r2.value-r.value as rating_difference, r.content_type as content_type1, r2.content_type as content_type2 - FROM {votingapi_vote} r, {votingapi_vote} r2 - WHERE r.uid=%d AND r2.content_id=%d AND r2.uid=r.uid", $userID, $itemID); - if(!$db_result) - { - drupal_set_message(t('ERROR $userID, $itemID')); - } - else - { - while($row = db_fetch_array($db_result)) - { - $other_node=$row["content_id"]; - $rating_difference=$row["rating_difference"]; - - - //update the two rows - db_query("UPDATE {cre_similarity_matrix} SET count=count-1, sum=sum-%d - WHERE content_id1=%d AND content_id2=%d", $rating_difference, $itemID, $other_node); - - // update the second row ONLY if the two nodes are different - if($itemID != $other_node) - { - db_query("UPDATE {cre_similarity_matrix} SET count=count-1, sum=sum+%d - WHERE content_id1=%d AND content_id2=%d", $rating_difference, $other_node, $itemID); - } +/** + * Implementation of votingapi's hook_update + */ +function cre_votingapi_update(&$vote, $new_value) { + // When a vote is updated need to just adjust the sum, NOT COUNT!! + // For all records in cre_similarity_matrix where content_id1 or content_id2 equals vote->content_id and the adjustment to sum MUST BE THE RATING_DIFFERENCE. + + $rating_difference = $new_value-$vote->value; + //drupal_set_message(t('difference = ' . $rating_difference)); + + db_query("UPDATE {cre_similarity_matrix} SET sum=sum+%d WHERE content_id1=%d AND content_type1 = '%s'", $rating_difference, $vote->content_id, $vote->content_type); + db_query("UPDATE {cre_similarity_matrix} SET sum=sum-%d WHERE content_id2=%d AND content_type2 = '%s'", $rating_difference, $vote->content_id, $vote->content_type); + + // Update sum and not count in cre_average_vote. + if ($vote->value > $new_value) { + $difference = $vote->value - $new_value; + $db_average = db_query("UPDATE {cre_average_vote} SET sum = sum - %d WHERE uid = %d", $difference, $vote->uid); + } + else { + $difference = $new_value - $vote->value; + $db_average = db_query("UPDATE {cre_average_vote} SET sum = sum + %d WHERE uid = %d", $difference, $vote->uid); + } +} + +/** + * Implementation of votingapi's hook_delete + */ +function cre_votingapi_delete(&$vote) { + // Modify the cre_similarity_matrix table by subtracting from count and subtracting vote value from sum. + // OVER ALL REFERENCES TO THE CONTENT_ID + // THINK !!! reverse of build!! + $userID = $vote->uid; + $itemID = $vote->content_id; + $db_result = db_query("SELECT DISTINCT r.content_id, r2.value-r.value as rating_difference, r.content_type as content_type1, r2.content_type as content_type2 FROM {votingapi_vote} r, {votingapi_vote} r2 WHERE r.uid=%d AND r2.content_id=%d AND r2.uid=r.uid", $userID, $itemID); + if (!$db_result) { + drupal_set_message(t('ERROR $userID, $itemID'), 'error'); + } + else { + while($row = db_fetch_array($db_result)) { + $other_node=$row['content_id']; + $rating_difference=$row['rating_difference']; + + // Update the two rows. + db_query("UPDATE {cre_similarity_matrix} SET count=count-1, sum=sum-%d WHERE content_id1=%d AND content_id2=%d", $rating_difference, $itemID, $other_node); + + // Update the second row ONLY if the two nodes are different. + if ($itemID != $other_node) { + db_query("UPDATE {cre_similarity_matrix} SET count=count-1, sum=sum+%d WHERE content_id1=%d AND content_id2=%d", $rating_difference, $other_node, $itemID); } - } - // update teh cre_average_vote table - $db_average = db_query("UPDATE {cre_average_vote} SET count=count-1, sum=sum-%d - WHERE uid = %d",$vote->value,$vote->uid); -} - -/* - * private function to install the module over existing voting data -*/ - -function _cre_load_all_diff_avg() -{ - $starting_pos = variable_get('cre_starting_position',-1); - $vote_limit = variable_get('cre_max_install_votes', 5); - - // for every vote within range...^ - $db_result = db_query("SELECT vote_id, uid, content_id, content_type FROM votingapi_vote - WHERE vote_id < %d - ORDER BY vote_id DESC LIMIT %d", $starting_pos, $vote_limit); - while ($vote_obj = db_fetch_object($db_result)) { - // call function cre_modify_avg_difference($userID,$itemID) - cre_modify_avg_difference($vote_obj->uid, $vote_obj->content_id,$vote_obj->content_type); - $vid = $vote_obj->vote_id; - } - // now save the starting position for next run - variable_set('cre_starting_position', $vid); - + } + } + // Update the cre_average_vote table. + $db_average = db_query("UPDATE {cre_average_vote} SET count=count-1, sum=sum-%d WHERE uid = %d", $vote->value, $vote->uid); +} + +/** + * Private function to install the module over existing voting data. + */ +function _cre_load_all_diff_avg() { + $starting_pos = variable_get('cre_starting_position', -1); + $vote_limit = variable_get('cre_max_install_votes', 5); + + // For every vote within range. + $db_result = db_query("SELECT vote_id, uid, content_id, content_type FROM votingapi_vote WHERE vote_id < %d ORDER BY vote_id DESC LIMIT %d", $starting_pos, $vote_limit); + while ($vote_obj = db_fetch_object($db_result)) { + // Call function cre_modify_avg_difference($userID, $itemID). + cre_modify_avg_difference($vote_obj->uid, $vote_obj->content_id, $vote_obj->content_type); + $vid = $vote_obj->vote_id; + } + // Now save the starting position for next run. + variable_set('cre_starting_position', $vid); } function cre_cron() { - _cre_load_all_diff_avg(); + _cre_load_all_diff_avg(); } -/* -* This function will be responsible for installing the users' average vote -*/ +/** + * This function will be responsible for installing the users' average vote. + */ function _cre_user_average() { - - // for all users - $db_users = db_query("SELECT DISTINCT uid from {votingapi_vote}"/* ORDER BY uid ASC"*/); - while( $user_obj = db_fetch_object($db_users) ) { - - $uid = $user_obj->uid; - // get all vote values made by user - $db_total = db_query("SELECT sum(value) as 'total' from votingapi_vote - WHERE uid = %d AND value_type = 'percent'",$uid); - // as long as they are percent... - $total_obj = db_fetch_object($db_total); - $total = $total_obj->total; - - // count = num_rows - $db_count = db_query("SELECT vote_id from votingapi_vote where uid = %d",$uid); - $num_rows = db_num_rows($db_count); - // store to cre_average_vote - db_query("INSERT INTO cre_average_vote (uid, sum, count) - VALUES (%d,%d,%d)", $uid, $total, $num_rows); - } + // For all users. + $db_users = db_query("SELECT DISTINCT uid from {votingapi_vote}"); // ORDER BY uid ASC clause removed. + while($user_obj = db_fetch_object($db_users)) { + $uid = $user_obj->uid; + // Get all vote values made by user. + $db_total = db_query("SELECT sum(value) as 'total' from votingapi_vote WHERE uid = %d AND value_type = 'percent'", $uid); + // As long as they are percent. + $total_obj = db_fetch_object($db_total); + $total = $total_obj->total; + + // Count = num_rows + $db_count = db_query("SELECT vote_id from votingapi_vote where uid = %d", $uid); + $num_rows = db_num_rows($db_count); + // Store to cre_average_vote. + db_query("INSERT INTO cre_average_vote (uid, sum, count) VALUES (%d, %d, %d)", $uid, $total, $num_rows); + } } function cre_remove($content_id, $content_type) { - db_query("DELETE FROM {cre_similarity_matrix} WHERE content_id1=%d AND content_type1 = '%s'",$content_id,$content_type); - db_query("DELETE FROM {cre_similarity_matrix} WHERE content_id2=%d AND content_type2 = '%s'",$content_id,$content_type); -} \ No newline at end of file + db_query("DELETE FROM {cre_similarity_matrix} WHERE content_id1=%d AND content_type1 = '%s'", $content_id, $content_type); + db_query("DELETE FROM {cre_similarity_matrix} WHERE content_id2=%d AND content_type2 = '%s'", $content_id, $content_type); +} + Index: cre_query_obj.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cre/cre_query_obj.inc,v retrieving revision 1.10 diff -u -p -r1.10 cre_query_obj.inc --- cre_query_obj.inc 16 Dec 2006 03:52:22 -0000 1.10 +++ cre_query_obj.inc 10 Jan 2007 11:15:41 -0000 @@ -1,94 +1,94 @@ average = $average; - $this->columns = array(); - $this->tables = array(); - $this->conditions = array(); - $this->execute = false; - - $query_type = variable_get('cre_query_type', 'accurate'); - - if ($query_type == 'accurate') { - - $this->columns[] = "d.content_id1 as 'content_id'"; - $this->columns[] = "sum(d.sum+d.count*r.value)/sum(d.count) as 'score'"; - - $this->tables[] = "{cre_similarity_matrix} d"; - $this->tables[] = "{votingapi_vote} r"; - - $this->conditions[] = sprintf("d.content_type1='%s'",$target_type); - $this->conditions[] = sprintf("r.uid=%d",$uid); - $this->conditions[] = "d.content_id1<>r.content_id"; - $this->conditions[] = "d.content_id2=r.content_id"; - } - else if ($query_type == 'fast'){ - $db_average = db_query("SELECT sum/count as 'average' from {cre_average_vote} - WHERE uid = %d",$uid); - $average_obj = db_fetch_object($db_average); - $average= $average_obj->average; - if (!$average) { - return; - } - $this->columns[] = "d.content_id1 as 'content_id'"; - $this->columns[] = "sum(d.sum+d.count)/sum(d.count)+". $average . " as 'score'"; - - $this->tables[] = "{cre_similarity_matrix} d"; - - $this->conditions[] = sprintf("d.content_type1='%s'",$target_type); - } - else { - /* - * point votingapi system query - */ - $this->columns[] = "d.content_id1 as 'content_id'"; - $this->columns[] = "d.sum/d.count as 'score'"; - - $this->tables[] = "{cre_similarity_matrix} d"; - $this->tables[] = "{votingapi_vote} r"; - - $this->conditions[] = sprintf("d.content_type1='%s'",$target_type); - $this->conditions[] = sprintf("r.uid = %d",$uid); - $this->conditions[] = "r.value = 1"; - $this->conditions[] = "d.content_id2 = r.content_id"; - $this->conditions[] = "d.content_id1 <> d.content_id2"; - } - $this->execute = true; - } - - function add_column($column) { - $this->columns[] = $column; - } - function add_table($proper_table_name) { - $this->tables[] = $proper_table_name; - } - function add_where($new_condtion) { - $this->conditions[] = $new_condtion; - } - function execute() { - if(!$this->execute) { - return false; + // constructor + function _cre_query($uid, $target_type, $reference_type = NULL) { + $this->average = $average; + $this->columns = array(); + $this->tables = array(); + $this->conditions = array(); + $this->conditions_values = array(); + $this->execute = false; + + $query_type = variable_get('cre_query_type', 'accurate'); + + if ($query_type == 'accurate') { + $this->columns[] = "d.content_id1 as 'content_id'"; + $this->columns[] = "sum(d.sum+d.count*r.value)/sum(d.count) as 'score'"; + $this->tables[] = "{cre_similarity_matrix} d"; + $this->tables[] = "{votingapi_vote} r"; + $this->conditions[] = "d.content_type1='$target_type'"; + $this->conditions[] = "r.uid=$uid"; + $this->conditions[] = "d.content_id1<>r.content_id"; + $this->conditions[] = "d.content_id2=r.content_id"; + } + elseif ($query_type == 'fast') { + $db_average = db_query("SELECT sum/count as 'average' from {cre_average_vote} WHERE uid = %d",$uid); + $average_obj = db_fetch_object($db_average); + $average= $average_obj->average; + if (!$average) { + return; } - - $query_string = "SELECT "; - - // columns - $query_string = $query_string . implode(",",$this->columns); - - // tables - $query_string = $query_string . " FROM " . implode(",",$this->tables); - - // conditions - $query_string = $query_string . " WHERE " . implode(" AND ",$this->conditions); - - $query_string = $query_string. " GROUP BY d.content_id1 ORDER BY score DESC"; - - return $query_string; - } + $this->columns[] = "d.content_id1 as 'content_id'"; + $this->columns[] = "sum(d.sum+d.count)/sum(d.count)+". $average . " as 'score'"; + $this->tables[] = "{cre_similarity_matrix} d"; + $this->conditions[] = "d.content_type1='$target_type'"; + } + else { + /* + * SELECT d.content_id1,d.sum/d.count as 'score' + * FROM {cre_similarity_matrix} d, {votingapi_vote} r + * WHERE r.uid = 1 AND r.value = 1 AND d.content_id2 = r.content_id AND d.content_id1 <> d.content_id2 ORDER BY score DESC; + */ + $this->columns[] = "d.content_id1 as 'content_id'"; + $this->columns[] = "d.sum/d.count as 'score'"; + $this->tables[] = "{cre_similarity_matrix} d"; + $this->tables[] = "{votingapi_vote} r"; + $this->conditions[] = "d.content_type1 = '$target_type'"; + $this->conditions[] = "r.uid = $uid"; + $this->conditions[] = "r.value = 1"; + $this->conditions[] = "d.content_id2 = r.content_id"; + $this->conditions[] = "d.content_id1 <> d.content_id2"; + } + $this->execute = true; + } + + function add_column($column) { + $this->columns[] = $column; + } + + function add_table($proper_table_name) { + $this->tables[] = $proper_table_name; + } + + function add_where($new_condtion, $value = NULL) { + $this->conditions[] = $new_condtion; + if (!is_null($value)) { + $this->conditions_values[] = $value; + } + } + + function execute() { + if(!$this->execute) { + return false; + } + + $where = implode(" AND ", $this->conditions); + _db_query_callback($this->conditions_values, TRUE); + $where = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $where); + + $query_string = + 'SELECT '. implode(",",$this->columns) . + ' FROM ' . implode(", ", $this->tables) . + ' WHERE ' . $where . + ' GROUP BY d.content_id1 ORDER BY score DESC'; + return $query_string; + } } \ No newline at end of file ? Recommendation Modules/.DS_Store