• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

World of Warcraft admin features

in Project codes

This CodeLet is from one of the live project. This is specific to one site built with Drupal 5.x. You can learn the code and can know how to achieve certain features at admin side. This CodeLet displays an interface to admin to update data per user. This data will then be displayed to end user which is not editable.

.install file:

<?php
//$Id$

/**
* @file
*
* Create schema for wow module
*
* hook = wow
*
* @author Beautifulmind
*/


/**
* Implementation of hook_install
*
* @author Beautifulmind
*/
function wow_install() {
    switch(
$GLOBALS['db_type']) {
        case
'mysql':
    case
'mysqli':
       
db_query("CREATE TABLE IF NOT EXISTS {wow_user_gold} (
         wow_id int(11) NOT NULL auto_increment,
         uid int(11) NOT NULL,
         system1_earned int(11) NOT NULL,
         system1_spent int(11) NOT NULL,
         system2_spent int(11) NOT NULL,
         picture varchar(255),
         PRIMARY KEY (wow_id)
        )/*!40100 DEFAULT CHARACTER SET utf8 */"
);



       
$path = file_directory_path() ."/worior";
       
file_check_directory($path, 1);
       
drupal_set_message(t("Worior directory has been created."));
        break;
    }
}


/**
* Implementation of hook_update
*
* @author Beautifulmind
*/
function wow_update_5001() {
 
db_query("CREATE TABLE IF NOT EXISTS {wow_sysone_info} (
   sys1_id int(11) NOT NULL auto_increment,
   uid int(11) NOT NULL,
   date varchar(255) NOT NULL,
   freesite varchar(255) NOT NULL,
   email varchar(255) NOT NULL,
   gold int(11),
   PRIMARY KEY (sys1_id)
  )/*!40100 DEFAULT CHARACTER SET utf8 */"
);
}

/**
* Implementation of hook_uninstall
*
* @author Beautifulmind
*/
function wow_uninstall() {
   
db_query("DROP TABLE IF EXISTS {wow_user_gold}");
   
db_query("DROP TABLE IF EXISTS {wow_sysone_info}");
}
?>

.module file

<?php
//$Id$

/**
* @file
*
* Display World Of Warcraft dashboard
*
* @author Beautifulmind
*/


/**
* Implementation of hook_help
*
* @author Beautifulmind
*/
function wow_help($path) {
  switch (
$path) {
    case
'admin/warcraft':
      return
t("Manage World of Warcraft settings");
      break;
    case
'admin/warcraft/manage':
      return
t("Set gold earned and spent by a waror.");
      break;
  }
}

/**
* Implementation of hook_menu
*
* @author Beautifulmind
*/
function wow_menu($may_cache) {
 
$items = array();
  global
$user;
  if (
$may_cache) {
     
$items[] = array (
       
'path' => 'admin/warcraft',
       
'title' => t("World of Warcraft"),
     
'description' => t("Manage Worldd of Warcraft"),
     
'position' => 'left',
     
'weight' => -8,
     
'callback' => 'wow_admin_wow',
     
'access' => user_access('administer site configuration'),
      );

     
$items[] = array (
      
'path' => 'system1/get',
      
'type' => MENU_CALLBACK,
    
'callback' => '_get_sys1_info',
      
'access' => user_access('administer site configuration'),
      );

     
$items[] = array (
      
'path' => 'admin/sys1/remove',
      
'type' => MENU_CALLBACK,
    
'callback' => '_remove_sys1',
    
'callback arguments' => arg(3),
      
'access' => user_access('administer site configuration'),
      );

     
$items[] = array (
     
'path' => 'admin/warcraft/manage',
     
'title' => t("Mange World of Warcraft"),
     
'description' => t("Adminstrate wariors and gold they've earned"),
     
'weight' => -10,
       
'callback' => 'drupal_get_form',
     
'callback arguments' => array('wow_admin_form', $form),
     
'access' => user_access('administer site configuration'),
    );

   
$items[] = array (
     
'path' => 'admin/warcraft/system1',
     
'title' => t("System one Gold earned"),
     
'description' => t("Adminstrate wariors and gold they've earned"),
     
'weight' => -9,
     
'callback' => 'drupal_get_form',
     
'callback arguments' => array('wow_admin_sys2_form', $form),
     
'access' => user_access('administer site configuration'),
    );

   
$items[] = array(
       
'path' => 'dashboard',
       
'title' => t("Dashboard"),
       
'type' => MENU_NORMAL_ITEM,
       
'callback' => 'wow_dashboard',
       
'access' => wow_access(),
       
'weight' => -10,
      );

     
$items[] = array (
     
'path' => 'user/'. $user->uid .'/system1',
     
'title' => t("System 1"),
     
'access' => wow_access(),
     
'callback' => 'wow_system1',
     
'type' => MENU_LOCAL_TASK,
     
'weight' => -9,
    );

   
$items[] = array (
     
'path' => 'user/'. $user->uid .'/system2',
     
'title' => t("System 2"),
     
'access' => wow_access(),
     
'callback' => 'wow_system2',
     
'type' => MENU_LOCAL_TASK,
     
'weight' => -8,
    );

   
$items[] = array (
     
'path' => 'user/'. $user->uid .'/request-gold',
     
'title' => t("Request gold"),
     
'access' => wow_access(),
     
'type' => MENU_LOCAL_TASK,
     
'weight' => -7,
    );
  }
  return
$items;
}

/**
* wow_system1
*
* @author Beautifulmind
*/
function wow_system1() {
  global
$user;
 
$output = t("Please allow up to 12 hours for our system to display
    your earnings"
);
 
$r__system1 = db_query("SELECT system1_earned, system1_spent FROM
    {wow_user_gold} WHERE uid = %d"
, $user->uid);
 
$output .= "<ul>";
  while (
$om__system1 = db_fetch_object($r__system1)) {
   
$output .= "<li>". t("Gold earned") .": ". $om__system1->system1_earned ."</li>";
   
$output .= "<li>". t("Gold spent") .": ". $om__system1->system1_spent ."</li>";
   
$output .= "<li>". t("Gold available") .": ".
      (
$om__system1->system1_earned - $om__system1->system1_spent) ."</li>";
  }
 
$output .= "</ul>";

 
$output .= _sys1_info();
  return
$output;
}


function
wow_system2() {
   
$output = "";
 
$output = t("Please allow up to 12 hours for our system to display your
    earnings"
);
 
$output .= wow_referral_view();

  return
$output;
}



function
wow_referral_view() {
  global
$user;
  if (
variable_get(REFERRAL_DISPLAY_MODE, 0)) {
   
$output .= '<div class="referral_link">';
   
$output .= t('Your referral link: ');
   
$output .= url('referral/' . _referral_uid2ref($user->uid), NULL, NULL, TRUE);
   
$output .= '</div>';
  }

 
$header = array(
    array(
'data' => t('User'),  'field' => 'u.name'),
    array(
'data' => t('Time'),  'field' => 'r.created', 'sort' => 'desc'),
    array(
'data' => t('System 1 earnings')),
  );

 
$sql = 'SELECT u.uid, u.name, r.created, w.system1_earned
    FROM {referral} r INNER JOIN {users} u ON r.uid= u.uid LEFT JOIN
    {wow_user_gold} w ON u.uid = w.uid WHERE r.referral_uid = %d AND
    u.status = 1 ORDER BY r.created DESC'
;

 
$result = pager_query($sql, REFERRAL_PAGE_COUNT, 0, null, $user->uid);
  while (
$data = db_fetch_object($result)) {
   
$rows[] = array(
      array(
'data' => l($data->name, "user/$data->uid")),
      array(
'data' => format_date($data->created, 'custom', REFERRAL_DATE_FORMAT)),
      array(
'data' => ($data->system1_earned ? $data->system1_earned : "")),
    );
  }
  if (!
$rows) {
   
$rows[] = array(array('data' => t('No data.'), 'colspan' => '4'));
  }
 
$pager = theme('pager', null, REFERRAL_PAGE_COUNT, 0);
  if (!empty(
$pager)) {
   
$rows[] = array(array('data' => $pager, 'colspan' => '4'));
  }
  return
theme('table', $header, $rows);
}


function
wow_dashboard() {
    global
$user;
   
drupal_goto('user/'. $user->uid);
}


function
wow_access() {
    global
$user;
    if (
in_array('authenticated user', $user->roles)) {
        return
TRUE;
    }
    else {
        return
FALSE;
    }
}


/**
*
* wow_manage_wow
*/
function wow_admin_form($form) {
    global
$user;
   
$form = array();
   
$form['#method'] = 'GET';
 
$form['#attributes'] = array('enctype' => "multipart/form-data");

 
$am__user = wow_get_user();
   
$am__user[0] = "-- ". t("Select") ." --";

  if (
$_GET['op']) {
   
wow_submit($_GET);
  }
   
$form['users'] = array(
     
'#type' => 'select',
     
'#title' => t("Warior"),
     
'#description' => t("Select the warior"),
     
'#options' => $am__user,
     
'#default_value' => $_GET['users'] ? $_GET['users'] : 0,
     
'#attributes' => array('onchange' => 'this.form.submit();'),
     
'#weight' => -10,
    );

   
$form['system1_earned'] = array(
   
'#type' => 'textfield',
   
'#title' => t("System 1 Gold earned"),
   
'#required' => TRUE,
     
'#description' => t("Provide the units of gold user has earned
        through system1"
),
   
'#default_value' =>  ($_GET['users'] ? _get_gold($_GET['users'],
     
'system1_earned') : 0),
   
'#weight' => -9,
  );

 
$form['system1_spent'] = array(
   
'#type' => 'textfield',
   
'#title' => t("System 1 Gold spent"),
   
'#required' => TRUE,
   
'#description' => t("Provide the units of gold user has spent from system1"),
   
'#default_value' => ($_GET['users'] ? _get_gold($_GET['users'],
     
'system1_spent') : 0),
   
'#weight' => -8,
  );

 
$form['system2_spent'] = array(
   
'#type' => 'textfield',
   
'#title' => t("System 2 Gold spent"),
   
'#required' => TRUE,
   
'#description' => t("Provide the units of gold user has spent from system2"),
   
'#default_value' => ($_GET['users'] ? _get_gold($_GET['users'],
     
'system2_spent') : 0),
   
'#weight' => -7,
  );

 
$form['image'] = array(
   
'#type' => 'file',
   
'#title' => t("System 1 picture"),
   
'#description' => t("Upload a picture for user for system1"),
   
'#weight' => -6,
  );

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t("Save settings"),
   
'#submit' => TRUE,
   
'#weight' => -5,
  );

 
$form['#submit'] = array('wow_submit');

    return
$form;
}


/**
* wow_user_submit
*
* @author Beautifulmind
*/
function wow_submit($form_values) {
    if (
$form_values['users'] > 0) {
       
$sn__id = db_result(db_query("SELECT wow_id FROM {wow_user_gold}
          WHERE uid = %d"
, $form_values['users']));
      if (
$form_values['files']['image']) {
       
$ss__path = file_directory_path() ."/worior";
       
$om__file = file_save_upload($form_values['files']['image'],
         
$ss__path, TRUE);
    }
    if (
$om__file) {
     
$ss__user_pic = $om__file->filepath;
    }
      if (!
$sn__id) {
       
db_query("INSERT INTO {wow_user_gold}
          VALUES(wow_id, %d, %d, %d, %d, '%s')"
,
         
$form_values['users'], $form_values['system1_earned'],
         
$form_values['system1_spent'], $form_values['system2_spent'],
           
$ss__user_pic);
      }
      else {
       
db_query("UPDATE {wow_user_gold} SET system1_earned = %d,
        system1_spent = %d, system2_spent = %d WHERE uid = %d AND wow_id = %d"
,
       
$form_values['system1_earned'], $form_values['system1_spent'],
       
$form_values['system2_spent'], $form_values['users'], $sn__id);
      }
    }
    return
NULL;
}


/**
* wow_get_user
*
* @author Beautifulmind
*/
function wow_get_user() {
    global
$user;
   
$r__user = db_query("SELECT uid, name FROM {users} WHERE status = 1");

    while (
$om__user = db_fetch_object($r__user)) {
       
$am__user[$om__user->uid] = $om__user->name;
    }

    return
$am__user;
}


/**
* wow_admin_wow
*
* @author Beautifulmind
*/
function wow_admin_wow() {
   
$menu = menu_get_item(NULL, 'admin/warcraft');
 
$content = system_admin_menu_block($menu);
 
$output = theme('admin_block_content', $content);
  return
$output;
}

/**
* _get_gold
*
* @author Beautifulmind
*/
function _get_gold($sn__user, $ss__field) {
    if (
$sn__user) {
       
$sn__gold db_result(db_query("SELECT ". $ss__field ." FROM {wow_user_gold} WHERE
          uid = %d"
, $sn__user));

        return (
$sn__gold ? $sn__gold : 0);
    }
    else {
     return
0;
    }
}


/**
* wow_admin_sys2_form
*
* @author Beautifulmind
*/
function wow_admin_sys2_form($form) {
  global
$user;
 
$form = array();
 
$am__user = wow_get_user();
 
$am__user[0] = "-- ". t("Select") ." --";
 
$form['#method'] = "GET";
  if (
$_GET['op']) {
   
wow_sys1_submit($_GET);
  }

 
$form['users'] = array(
   
'#type' => 'select',
   
'#title' => t("Warior"),
   
'#description' => t("Select the warior"),
   
'#options' => $am__user,
   
'#default_value' => $_GET['users'] ? $_GET['users'] : 0,
   
'#attributes' => array('onchange' => 'this.form.submit()'),
   
'#weight' => -10,
  );

 
$form['date'] = array(
   
'#type' => 'date',
   
'#title' => t("Date"),
   
'#default_value' => "",
   
'#weight' => -9,
  );

 
$form['free_site'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Freesite"),
   
'#default_value' => "",
   
'#weight' => -8,
  );

 
$form['email'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Email address"),
   
'#default_value' => "",
   
'#weight' => -7,
  );

 
$form['gold_earned'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Gold earned"),
   
'#default_value' => "",
   
'#weight' => -6,
  );

 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t("Save settings"),
   
'#submit' => TRUE,
   
'#weight' => -5,
  );

 
$form['user_info'] = array(
   
'#type' => 'fieldset',
   
'#title' => t("System one information"),
   
'#weight' => -4,
  );

 
$ss__sys1_info = _get_sys1_info();
echo
"<pre>";
print_R($ss__sys1_info);
echo
"</pre>";
 
$form['user_info']['sys1_info'] = array(
   
'#type' => 'item',
   
'#prefix' => "<div id=\"sys1_info\">",
   
'#suffix' => "</div>",
   
'#value' => $ss__sys1_info ? $ss__sys1_info : t("No information available."),
   
'#weight' => -4,
  );

 
$form['#submit'] = array('wow_sys1_submit');

  return
$form;
}


/**
* wow_sys1_submit
*
* @author Beautifulmind
*/
function wow_sys1_submit($form_values) {
 
db_query("INSERT INTO {wow_sysone_info} VALUES (sys1_id, %d, '%s', '%s', '%s',
    %d)"
, $form_values['users'], implode("/", $form_values['date']),
   
$form_values['free_site'], $form_values['email'], $form_values['gold_earned']);
}

/**
* _get_sys1_info()
*
* @author Beautifulmind
*/
function _get_sys1_info() {
 
$output = "";
 
$r__result = db_query("SELECT * FROM {wow_sysone_info} WHERE uid = %d",
   
$_GET['users']);
 
$header = array(
   
NULL,
    array(
'data' => t("Date"), 'width' => '55px'),
    array(
'data' => t("Freesite"), 'width' => '55px'),
    array(
'data' => t("Email"), 'width' => '160px'),
    array(
'data' => t("Gold Earned"), 'width' => '40px'),
   
NULL,
  );
 
$i = 1;
 
$row = NULL;
  while (
$om__user = db_fetch_object($r__result)) {
   
$row[] = array(
     
$i,
     
format_date(strtotime($om__user->date), 'custom', "m/d/Y"),
     
$om__user->freesite,
     
$om__user->email,
     
$om__user->gold,
     
l(t("Remove"), 'admin/sys1/remove/'. $om__user->sys1_id),
    );
   
$i++;
  }
  if (
db_affected_rows()) {
   
$output = theme('table', $header, $row, NULL);
  }
  else {
   
$output = NULL;
  }

  return
$output;
}


/**
* _remove_sys1
*
* Remove system 1 gold information
*
* @author Beautifulmind
*/
function _remove_sys1($sys1_id) {
 
db_query("DELETE FROM {wow_sysone_info} WHERE sys1_id = %d", $sys1_id);
 
drupal_goto('admin/warcraft/system1');
}


/**
* _sys1_info()
*
* Display system 1 gold information
*
* @author Beautifulmind
*/
function _sys1_info() {
  global
$user;
 
$output = "";
 
$r__result = db_query("SELECT * FROM {wow_sysone_info} WHERE uid = %d",
   
$user->uid);
 
$header = array(
    array(
'data' => NULL, 'width' => '10px'),
    array(
'data' => t("Date"), 'width' => '80px'),
    array(
'data' => t("Freesite"), 'width' => '90px'),
    array(
'data' => t("Email"), 'width' => '160px'),
    array(
'data' => t("Gold Earned"), 'width' => '90px'),
  );
 
$i = 1;
 
$row = NULL;
  while (
$om__user = db_fetch_object($r__result)) {
   
$row[] = array(
     
$i,
     
format_date(strtotime($om__user->date), 'custom', "m/d/Y"),
     
$om__user->freesite,
     
$om__user->email,
     
$om__user->gold,
    );
   
$i++;
  }
  if (
db_affected_rows()) {
   
$output = theme('table', $header, $row, NULL);
  }
  else {
   
$output = NULL;
  }

  return
$output;
}
?>

5
Your rating: None Average: 5 (3 votes)
Syndicate content

Recent comments