| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Add social networking functionality to the user interface
|
| 7 |
*
|
| 8 |
* Main things: degree count, searching routes and measure separation
|
| 9 |
* computing clustering coefficient, searching groups in network
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* All the sna-related function is in routes.php
|
| 14 |
*/
|
| 15 |
require 'routes.php';
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_perm().
|
| 19 |
*/
|
| 20 |
function sna_perm() {
|
| 21 |
return array('access sna', 'browse sna map', 'realtime shortest route searching in sna', 'browse sna groups');
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Get all the operations of the module
|
| 26 |
*
|
| 27 |
* @return array The operations
|
| 28 |
*/
|
| 29 |
function sna_get_operations() {
|
| 30 |
$op = array(
|
| 31 |
array(
|
| 32 |
'title' => t('Searching routes'),
|
| 33 |
'desc' => t('Searching shortest route with Dijksta-algorithm or searching minimal step route with breadth-first search.'),
|
| 34 |
'path' => 'sna/routes',
|
| 35 |
'access' => 'access sna'),
|
| 36 |
array(
|
| 37 |
'title' => t('Map of the network'),
|
| 38 |
'desc' => t('A JAVA applet shows the network and you can explore within it.'),
|
| 39 |
'path' => 'sna/map',
|
| 40 |
'access' => 'browse sna map'),
|
| 41 |
array(
|
| 42 |
'title' => t('Distribution of edges'),
|
| 43 |
'desc' => t('Many basic features of the network topology can be read from the vertex
|
| 44 |
degree distribution. A vertex degree is the number corresponding to
|
| 45 |
number of vertices to which the vertex is directly connected. Small-world
|
| 46 |
network gives also the exponential dependence on vertex degree. Networks,
|
| 47 |
where the distribution of vertex degree is of power-law type are called a scale-free networks.'),
|
| 48 |
'path' => 'sna/dist',
|
| 49 |
'access' => 'access sna'),
|
| 50 |
array(
|
| 51 |
'title' => t('Explore groups'),
|
| 52 |
'desc' => t('Here you can see the strongly connected component of the social network.
|
| 53 |
A directed graph (the network is represented by a directed graph) is called strongly
|
| 54 |
connected if for every pair of vertices u and v there is a path from u to v and a path from v to u.'),
|
| 55 |
'path' => 'sna/groups',
|
| 56 |
'access' => 'browse sna groups'));
|
| 57 |
return $op;
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Implementation of hook_help()
|
| 62 |
*/
|
| 63 |
function sna_help($section = '') {
|
| 64 |
switch ($section) {
|
| 65 |
case "admin/modules#description":
|
| 66 |
$output .= t("Social network module");
|
| 67 |
break;
|
| 68 |
}
|
| 69 |
return $output;
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Implementation of hook_user()
|
| 74 |
*/
|
| 75 |
function sna_user($op, &$edit, &$account, $category = NULL) {
|
| 76 |
global $user;
|
| 77 |
if ($op == 'view') {
|
| 78 |
if (!user_access('access sna')) {
|
| 79 |
return array();
|
| 80 |
}
|
| 81 |
$edges = array();
|
| 82 |
get_graph($edges);
|
| 83 |
$output[] = array('title' => t("Number of direct connections"), 'value' => vertex_degree($edges, $account->uid));
|
| 84 |
$output[] = array('title' => t("Clustering coefficient"), 'value' => clustering_coefficient($edges, $account->uid));
|
| 85 |
if ($user->uid !== $account->uid) {
|
| 86 |
// If it's not our own user page
|
| 87 |
$possibilities[] = l(t('Shortest route from ') . $account->name . t(" to ") . $user->name, 'sna/a_to_b/' . $account->uid .'/' . $user->uid . '/0');
|
| 88 |
$possibilities[] = l(t('Shortest route from ') . $user->name . t(" to ") . $account->name, 'sna/a_to_b/' . $user->uid . '/' . $account->uid . '/0');
|
| 89 |
$possibilities[] = l(t('Minimal step route from ') . $account->name . t(" to ") . $user->name, 'sna/a_to_b/' . $account->uid .'/' . $user->uid . '/1');
|
| 90 |
$possibilities[] = l(t('Minimal step route from ') . $user->name . t(" to ") . $account->name, 'sna/a_to_b/' . $user->uid . '/' . $account->uid . '/1');
|
| 91 |
$output[] = array('title' => t("Search route"), 'value' => theme_item_list($possibilities));
|
| 92 |
}
|
| 93 |
if (user_access('browse sna map')) {
|
| 94 |
$draws[] = l(t('Draw user\'s tree of minimal routes'), 'sna/tree_draw/' . $account->uid . '/0');
|
| 95 |
$draws[] = l(t('Draw user\'s tree of breadth-first search'), 'sna/tree_draw/' . $account->uid . '/1');
|
| 96 |
$output[] = array('title' => t('Visualization'), 'value' => theme_item_list($draws));
|
| 97 |
}
|
| 98 |
return array(t("Social network informations") => $output);
|
| 99 |
}
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Implementation of hook_block().
|
| 104 |
*/
|
| 105 |
function sna_block($op = 'list', $delta = 0, $edit = array()) {
|
| 106 |
global $user;
|
| 107 |
|
| 108 |
$edges = array();
|
| 109 |
get_graph($edges);
|
| 110 |
|
| 111 |
if ($op == 'list') {
|
| 112 |
$blocks[0]['info'] = t('Most linked users');
|
| 113 |
$blocks[1]['info'] = t('Average step separation');
|
| 114 |
return $blocks;
|
| 115 |
}
|
| 116 |
else if ($op == 'view') {
|
| 117 |
$block = array();
|
| 118 |
|
| 119 |
switch ($delta) {
|
| 120 |
case 0:
|
| 121 |
$max_users = variable_get('user_block_max_list_count', 10);
|
| 122 |
$top_users = sort_by_popularity($edges, $max_users);
|
| 123 |
$num_top_users = count($top_users);
|
| 124 |
$how_many_list = $num_top_users > $max_users ? $max_users : $num_top_users ;
|
| 125 |
for ($i = 0; $i < $how_many_list; $i++) {
|
| 126 |
$items[] = user_load(array('uid' => $top_users[$i][1]));
|
| 127 |
}
|
| 128 |
|
| 129 |
$output .= theme('user_list', $items);
|
| 130 |
$block['subject'] = t('Most linked users');
|
| 131 |
$block['content'] = $output;
|
| 132 |
return $block;
|
| 133 |
|
| 134 |
case 1:
|
| 135 |
$block['subject'] = t('Average step separation');
|
| 136 |
$block['content'] = round(average_step_separation($edges), 3);
|
| 137 |
return $block;
|
| 138 |
}
|
| 139 |
}
|
| 140 |
}
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Create form of sna-related commands.
|
| 144 |
* Commands:
|
| 145 |
* - BFS
|
| 146 |
* - Dijkstra
|
| 147 |
*
|
| 148 |
*/
|
| 149 |
function sna_routes() {
|
| 150 |
// Generate form for searching route
|
| 151 |
$form['a_to_b'] = array(
|
| 152 |
'#type' => 'fieldset',
|
| 153 |
'#title' => t('Searching best route between users'),
|
| 154 |
'#tree' => TRUE);
|
| 155 |
$form['a_to_b']['from'] = array(
|
| 156 |
'#type' => 'select',
|
| 157 |
'#title' => t('From'),
|
| 158 |
'#options' => get_all_vertices_for_forms());
|
| 159 |
$form['a_to_b']['to'] = array(
|
| 160 |
'#type' => 'select',
|
| 161 |
'#title' => t('To'),
|
| 162 |
'#options' => get_all_vertices_for_forms());
|
| 163 |
$form['a_to_b']['min_or_step'] = array(
|
| 164 |
'#type' => 'radios',
|
| 165 |
'#title' => t('Type of route searching'),
|
| 166 |
'#options' => array(t('Shortest route'), t('Minimal step route')));
|
| 167 |
$form['a_to_b']['submit'] = array('#type' => 'submit', '#value' => t('Search'));
|
| 168 |
return drupal_get_form('a_to_b', $form);
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Create the map-viewer applet
|
| 173 |
* Currently use smallworld (http://www.cs.ubc.ca/~sfingram/cs533C/small_world.html)
|
| 174 |
*
|
| 175 |
* @param string $params The applet's parameter - list of edges
|
| 176 |
* @param string $title This string be written in the applet window
|
| 177 |
*
|
| 178 |
*/
|
| 179 |
function sna_applet($params, $title) {
|
| 180 |
$output = '<applet code = "app/SmallWorldApplet"
|
| 181 |
archive = "'. APPLET_PATH .'prefuse.jar,
|
| 182 |
'. APPLET_PATH .'colt.jar,
|
| 183 |
'. APPLET_PATH .'smallworld.jar" height = "400" width = "300">';
|
| 184 |
$output .= $params;
|
| 185 |
$output .= '<param name = "title" value = "'. $title .'"></applet>';
|
| 186 |
return $output;
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Show the entire social network map with the help of a java applet
|
| 191 |
* Can be very resource-consuming. Not suggested at large sites with many users!
|
| 192 |
*
|
| 193 |
*/
|
| 194 |
function sna_map() {
|
| 195 |
get_graph($edges);
|
| 196 |
return sna_applet(show_map($edges), t('The social network map of this site'));
|
| 197 |
}
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Draw the distribution of edges in an SVG file.
|
| 201 |
*
|
| 202 |
*/
|
| 203 |
function sna_dist() {
|
| 204 |
return '<object type="image/svg+xml" name="edge_dist" data="' . base_path() . '?q=sna/svg_distribution_of_edges" width="' . PIC_WIDTH .
|
| 205 |
'" height="' . PIC_HEIGHT . '">' . t('SVG support is needed') . '</object>';
|
| 206 |
}
|
| 207 |
|
| 208 |
function sna_groups() {
|
| 209 |
get_graph($edges);
|
| 210 |
$groups = search_groups($edges);
|
| 211 |
$num_of_groups = count($groups);
|
| 212 |
for ($act_group = 0; $act_group < $num_of_groups; $act_group++) {
|
| 213 |
$users_in_this_group = count($groups[$act_group]);
|
| 214 |
for ($act_user = 0; $act_user < $users_in_this_group; $act_user++) {
|
| 215 |
$group[] = user_load(array('uid' => $groups[$act_group][$act_user]));
|
| 216 |
}
|
| 217 |
$output .= theme_box(t('Group ' . ($act_group + 1)), theme('user_list', $group));
|
| 218 |
}
|
| 219 |
if (empty($output)) {
|
| 220 |
$output = t('No groups in the network yet.');
|
| 221 |
}
|
| 222 |
return $output;
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Handle the form of the sna_operations page
|
| 227 |
*
|
| 228 |
*/
|
| 229 |
function a_to_b_submit($form_id, $form_values) {
|
| 230 |
get_graph($edges);
|
| 231 |
|
| 232 |
$from = $form_values['a_to_b']['from'];
|
| 233 |
$to = $form_values['a_to_b']['to'];
|
| 234 |
$type = $form_values['a_to_b']['min_or_step'];
|
| 235 |
if ($type == 0) {
|
| 236 |
if (user_access('realtime shortest route searching in sna') || is_in_cache($from) !== FALSE) {
|
| 237 |
$min_route = a_to_b($edges, $from, $to);
|
| 238 |
}
|
| 239 |
else {
|
| 240 |
return drupal_set_message(t('Realtime shortest route searching is disabled'), 'error');
|
| 241 |
}
|
| 242 |
}
|
| 243 |
else {
|
| 244 |
$min_route = a_to_b($edges, $from, $to, TRUE);
|
| 245 |
}
|
| 246 |
if ($from == $to) {
|
| 247 |
drupal_set_message(t('The destination and the source is the same.'));
|
| 248 |
}
|
| 249 |
else if (count($min_route) == 0 || $min_route === FALSE) {
|
| 250 |
drupal_set_message(t('No route from ') . get_real_name($from) . t(' to ') . get_real_name($to));
|
| 251 |
}
|
| 252 |
else {
|
| 253 |
$index = 0;
|
| 254 |
$route[] = array(t("User"), t(($type == 0 ? "Distance " : "Step ") . "from the source"));
|
| 255 |
for ($i = end($min_route); is_array($i); $i = prev($min_route)) {
|
| 256 |
$route[] = array(get_real_name($i['n']), $i['d']);
|
| 257 |
}
|
| 258 |
drupal_set_message(theme_table(array(), $route, array()));
|
| 259 |
}
|
| 260 |
}
|
| 261 |
|
| 262 |
/**
|
| 263 |
* Display a route between two users
|
| 264 |
*
|
| 265 |
* @param integer $from The from drupal user id
|
| 266 |
* @param integer $to The to drupal user id
|
| 267 |
* @param integer $type If 0 - Dijkstra, if 1 - breadth-first search
|
| 268 |
*
|
| 269 |
*/
|
| 270 |
function sna_a_to_b_show($from = FALSE, $to = FALSE, $type = FALSE) {
|
| 271 |
if (!(is_numeric($from) && is_numeric($to) && is_numeric($type))) {
|
| 272 |
drupal_access_denied();
|
| 273 |
}
|
| 274 |
get_graph($edges);
|
| 275 |
if ($type == 0) { // Dijkstra search (slow!)
|
| 276 |
if (user_access('realtime shortest route searching in sna') || is_in_cache($from) !== FALSE) {
|
| 277 |
$min_route = a_to_b($edges, $from, $to);
|
| 278 |
}
|
| 279 |
else {
|
| 280 |
return drupal_set_message(t('Realtime shortest route searching is disabled'), 'error');
|
| 281 |
}
|
| 282 |
}
|
| 283 |
else { // Breadth-first search
|
| 284 |
$min_route = a_to_b($edges, $from, $to, TRUE);
|
| 285 |
}
|
| 286 |
if ($from == $to) {
|
| 287 |
return t('The destination and the source is the same.');
|
| 288 |
}
|
| 289 |
else if (count($min_route) == 0 || $min_route === FALSE) {
|
| 290 |
return t('No route from ') . get_real_name($from) . t(' to ') . get_real_name($to);
|
| 291 |
}
|
| 292 |
else {
|
| 293 |
$index = 0;
|
| 294 |
$route[] = array(t("User"), t(($type == 0 ? "Distance " : "Step ") . "from the source"));
|
| 295 |
for ($i = end($min_route); is_array($i); $i = prev($min_route)) {
|
| 296 |
$route[] = array(get_real_name($i['n']), $i['d']);
|
| 297 |
}
|
| 298 |
return theme_table(array(), $route, array());
|
| 299 |
}
|
| 300 |
}
|
| 301 |
|
| 302 |
/**
|
| 303 |
* Display a tree originating from <var>$uid</var>
|
| 304 |
*
|
| 305 |
* @param integer $uid Drupal uid
|
| 306 |
* @param integer $type If 0 - Dijkstra, if 1 - breadth-first search
|
| 307 |
*
|
| 308 |
*/
|
| 309 |
function sna_tree_draw($uid, $type) {
|
| 310 |
get_graph($edges);
|
| 311 |
if ($type == 0) { // Dijkstra search (slow!)
|
| 312 |
$tree = a_to_any($edges, $uid);
|
| 313 |
}
|
| 314 |
else { // Breadth-first search
|
| 315 |
$tree = breadth_first_walk($edges, $uid);
|
| 316 |
}
|
| 317 |
return sna_applet(show_tree($tree), t('The tree of user ') . get_real_name($uid));
|
| 318 |
}
|
| 319 |
|
| 320 |
function sna_svg_distribution() {
|
| 321 |
get_graph($edges);
|
| 322 |
header('Content-type: image/svg+xml');
|
| 323 |
print visualize_distribution(distribution_of_degree($edges));
|
| 324 |
}
|
| 325 |
|
| 326 |
function sna_page() {
|
| 327 |
$ops = sna_get_operations();
|
| 328 |
$num_ops = count($ops);
|
| 329 |
for ($i = 0; $i < $num_ops; $i++) {
|
| 330 |
if (user_access($ops[$i]['access'])) {
|
| 331 |
$output .= '<dt>' . l($ops[$i]['title'], $ops[$i]['path']) . '</dt><dd>' . $ops[$i]['desc'] . '</dd>';
|
| 332 |
}
|
| 333 |
else {
|
| 334 |
$output .= '<dt>' . $ops[$i]['title'] . '</dt><dd>' . t('function is disabled') . '</dd>';
|
| 335 |
}
|
| 336 |
}
|
| 337 |
return $output;
|
| 338 |
}
|
| 339 |
|
| 340 |
/**
|
| 341 |
* Implementation of hook_menu().
|
| 342 |
*
|
| 343 |
* @return array Menu elements
|
| 344 |
*/
|
| 345 |
function sna_menu($may_cache) {
|
| 346 |
$items[] = array('path' => 'sna', 'title' => t('Social networking'), 'callback' => 'sna_page',
|
| 347 |
'access' => user_access('access sna'), 'type' => MENU_ITEM_GROUPING);
|
| 348 |
// Make submenus
|
| 349 |
$ops = sna_get_operations();
|
| 350 |
$num_ops = count($ops);
|
| 351 |
for ($i = 0; $i < $num_ops; $i++) {
|
| 352 |
$items[] = array('path' => $ops[$i]['path'], 'title' => $ops[$i]['title'],
|
| 353 |
'callback' => str_replace('/', '_', $ops[$i]['path']) , 'access' => user_access($ops[$i]['access']));
|
| 354 |
}
|
| 355 |
$items[] = array('path' => 'sna/a_to_b', 'title' => t('Search routes'), 'callback' => 'sna_a_to_b_show',
|
| 356 |
'access' => user_access('access sna'), 'type' => MENU_CALLBACK);
|
| 357 |
$items[] = array('path' => 'sna/tree_draw', 'title' => t('Drawing tree'), 'callback' => 'sna_tree_draw',
|
| 358 |
'access' => TRUE, 'type' => MENU_CALLBACK);
|
| 359 |
$items[] = array('path' => 'sna/svg_distribution_of_edges', 'title' => t(''), 'callback' => 'sna_svg_distribution',
|
| 360 |
'access' => TRUE, 'type' => MENU_CALLBACK);
|
| 361 |
return $items;
|
| 362 |
}
|
| 363 |
|
| 364 |
/**
|
| 365 |
* Implementation of hook_settings()
|
| 366 |
*
|
| 367 |
* @return array The form of settings
|
| 368 |
*/
|
| 369 |
function sna_settings() {
|
| 370 |
/*if (!user_access("access administration pages")) {
|
| 371 |
return message_access();
|
| 372 |
}*/
|
| 373 |
if (array_search(DBA_HANDLER, array_keys(dba_handlers())) === FALSE) {
|
| 374 |
drupal_set_message(t('The proper dba_handler support is missing.'), 'error');
|
| 375 |
drupal_set_message(t('You have to choose a valid dba_handler in common.php'), 'error');
|
| 376 |
}
|
| 377 |
|
| 378 |
$form['sna_data_source'] = array('#title' => t('Data source of the network'),
|
| 379 |
'#type' => 'select',
|
| 380 |
'#options' => array('nodes' => 'nodes', 'buddy' => 'buddy', 'stats' => 'stats'),
|
| 381 |
'#default_value' => variable_get('sna_data_source', 'nodes'));
|
| 382 |
$form['sna_size_cache'] = array('#title' => t('Number of cached shortest routes'),
|
| 383 |
'#type' => 'textfield',
|
| 384 |
'#default_value' => variable_get('sna_size_cache', 10));
|
| 385 |
$form['sna_data_path'] = array('#title' => t('Data files path which is writeable by the web server'),
|
| 386 |
'#type' => 'textfield',
|
| 387 |
'#default_value' => variable_get('sna_data_path', FILES_PATH));
|
| 388 |
$form['sna_pic_size'] = array('#title' => t('Size of SVG picture in pixel'),
|
| 389 |
'#type' => 'textfield',
|
| 390 |
'#default_value' => variable_get('sna_pic_size', PIC_WIDTH));
|
| 391 |
$form['sna_limit'] = array('#title' => t('Ignore edges longer than'),
|
| 392 |
'#type' => 'textfield',
|
| 393 |
'#default_value' => variable_get('sna_limit', 0),
|
| 394 |
'#description' => t('Zero means no limit.'));
|
| 395 |
return $form;
|
| 396 |
}
|