<?php

function cobaltusers_init() {
  if (user_access('use cobalt')) {
    drupal_add_js(drupal_get_path('module', 'cobalt') . '/js/cobalt.users.js');
  }
}

function cobaltusers_menu() {
  $items = array();
  $items['cobalt/data/users_json/%'] = array(
    'title' => 'Serialized users',
    'page callback' => 'cobaltusers_json',
    'page arguments' => array(2),
    'access arguments' => array('use cobalt'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function cobaltusers_json($last_update) {
  // We're just fetching the bare minimum of what we need for the
  // user_*_access methods and display.
  $res = db_query("SELECT uid, name, access, status FROM {users}
    WHERE created > :last_update
    ORDER BY uid DESC
    LIMIT 100", array(
      ':last_update' => $last_update,
    ));

  $users = array();
  foreach ($res as $account) {
    if (user_access('administer users')) {
      $access = 'rwd';
    }
    else {
      $access = (user_view_access($account)?'r':'') . (user_edit_access($account)?'w':'');
    }

    if (!empty($access)) {
      $users[] = array($account->uid, $account->name, $access);
    }
  }

  print drupal_json_output($users);
  exit;
}