Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sub find_connections {
- my ($this, $degree, $is_course) = @_;
- my ($type, $type_id);
- if ($is_course) {
- $sql = "SELECT * FROM course WHERE course_id = '$this'";
- $type = "course";
- $type_id = 0;
- }
- else {
- $already_expanded[@already_expanded] = $this;
- $sql = "SELECT * FROM friend WHERE user1 = '$this' LIMIT 0, 200";
- $type = "friend";
- $type_id = 1;
- }
- my $return = $db->query($sql);
- while (my %row = $return->fetchhash()) {
- my $new_id;
- if (!$is_course) {
- # get the new id from the friend pair
- #if ($row{user1} eq $this) {
- # $new_id = $row{user2};
- #} else {
- # $new_id = $row{user1};
- #}
- $new_id = $row{user2};
- }
- else {
- $new_id = $row{id};
- }
- next unless ($degree > $num_degrees or $cs{$new_id} or can_see($user, $new_id, $type));
- # see if the id is already a node; if not, add it
- my $node_exists = 0;
- for (my $i = 0; $i < @node and !$node_exists; $i++) {
- check_connection_row("#...pass vars by reference...");
- }
- }
- sub check_connection_row {
- $node_exists = 1 if ($node[$i]{id} eq $new_id and $node[$i]{type} eq 1);
- if (!$node_exists and $degree <= $num_degrees) {
- my $next_node = @node;
- $node[$next_node]{id} = $new_id;
- $node[$next_node]{type} = 1;
- }
- # see if there's an edge between the two already; if not, add it
- if ($node_exists or $degree <= $num_degrees) {
- my $edge_exists = 0;
- my $this_index = find_node ($this, $type_id);
- my $new_index = find_node ($new_id, 1);
- for (my $i = 0; $i < @edge and !$edge_exists; $i++) {
- my $cond1 = $edge[$i][0] eq $this_index and $edge[$i][1] eq $new_index; # would use meaninful condition name if I knew the subject
- my $cond2 = $edge[$i][0] eq $new_index and $edge[$i][1] eq $this_index; # would use meaninful condition name if I knew the subject
- $edge_exists = 1 if ($cond1 or $cond2);
- }
- unless ($edge_exists) {
- my $next_edge = @edge;
- $edge[$next_edge][0] = $this_index;
- $edge[$next_edge][1] = $new_index;
- }
- }
- return unless ($degree < $num_degrees + 1);
- # if we want to consider friends farther out, do so now
- my $expansion_exists = 0;
- for (my $i = 0; $i < @already_expanded and !$expansion_exists; $i++) {
- $expansion_exists = 1 if ($already_expanded[$i] eq $new_id);
- }
- find_connections ($new_id, $degree + 1, 0) if (!$expansion_exists && !$is_course or can_see ($user, $new_id, "friend"));
- }
Advertisement
Add Comment
Please, Sign In to add comment