You are here

function _recommender_example_load_data in Recommender API 7.6

1 call to _recommender_example_load_data()
recommender_example_admin_form_submit in recommender_example/recommender_example.module

File

recommender_example/recommender_example.module, line 113

Code

function _recommender_example_load_data() {
  $uid_list = db_query_range("SELECT uid FROM {users} WHERE status = 1", 0, 4)
    ->fetchCol();
  $nid_list = db_query_range("SELECT nid FROM {node} WHERE status = 1", 0, 6)
    ->fetchCol();
  if (count($uid_list) != 4 || count($nid_list) != 6) {
    drupal_set_message(t('You have %num_user (need at least 4) and !num_node (need at least 6). Please create the missing users and nodes before loading example data.', array(
      '%num_user' => format_plural(count($uid_list), '1 user', '@count users'),
      '!num_node' => format_plural(count($nid_list), '1 node', '@count nodes'),
    )), 'error');
    return;
  }

  // truncate table first
  db_query("DELETE FROM {recommender_example_preference}");

  // load toy data from GroupLens paper
  // Ken: 1, Lee: 2, Meg: 3, Nan: 4; Items 1~6.
  // Expected correlation between Ken(1) and Lee(2) is -0.8, between Ken(1) and Meg(3) is 1, between Ken(1) and Nan(4) is 0.
  // Expected prediction for Ken on Item6 is: 4.56, for Nan(4) on item6 is: 3.75 (my calculation is 3.85)
  $values = array(
    array(
      1,
      1,
      1,
    ),
    array(
      1,
      2,
      5,
    ),
    array(
      1,
      4,
      2,
    ),
    array(
      1,
      5,
      4,
    ),
    array(
      2,
      1,
      4,
    ),
    array(
      2,
      2,
      2,
    ),
    array(
      2,
      4,
      5,
    ),
    array(
      2,
      5,
      1,
    ),
    array(
      2,
      6,
      2,
    ),
    array(
      3,
      1,
      2,
    ),
    array(
      3,
      2,
      4,
    ),
    array(
      3,
      3,
      3,
    ),
    array(
      3,
      6,
      5,
    ),
    array(
      4,
      1,
      2,
    ),
    array(
      4,
      2,
      4,
    ),
    array(
      4,
      4,
      5,
    ),
    array(
      4,
      5,
      1,
    ),
  );
  $timestamp = time();
  $insert = db_insert('recommender_example_preference')
    ->fields(array(
    'uid',
    'eid',
    'score',
    'updated',
  ));
  foreach ($values as $row) {
    $insert
      ->values(array(
      'uid' => $uid_list[$row[0] - 1],
      'eid' => $nid_list[$row[1] - 1],
      'score' => $row[2],
      'updated' => $timestamp,
    ));
  }
  $insert
    ->execute();
}