Helo. I'm trying to port the post count rank module to drupal 4.7. I think is almost finish all the porting, but I can't make the settings page saves the postcount_rank_fieldid and postcount_rank_metrics vars.

I'm a little noob whit drupal so I can't figure where I'm wrong. Here is the code please help me.

<?php
define('PROFILE_HIDDEN',4);

function postcount_rank_help($section)  {
  switch($section) {
    case 'admin/modules#description':
      return t("This module allows you to give users a custom rank based on postcount and assign it to a custom 'list' type profile field");
      break;
  }
}

function postcount_rank_settings()  {
  $result = db_query("SELECT fid, title, options FROM {profile_fields} WHERE visibility = %d AND type = 'selection'",PROFILE_HIDDEN); 
  $fid = variable_get('postcount_rank_fieldid',-1);
  $list_values = '';
  while($field = db_fetch_array($result))  {
    $options[$field['fid']] = $field['title'];
    if ($field['fid'] == $fid)
      $list_values = $field['options'];
  }

  $form['profile_field'] = array(
    '#type' => 'fieldset',
    '#title' => t('Postcount rank settings'),
  );

  if ($options){
    $form['profile_field']['field'] = array(
       '#type'          => 'select',
       '#title'         => t('Profile Field'),
       '#default_value' => $fid,
       '#options'       => $options,
       '#description'   => t('The profile field that contains a list of ranks' . $fid),
    );
  }
  else { 
    $form['profile_field']['field'] = array(
       '#type'  => 'markup',
       '#value' => t('You need to have at least one hidden list selection field in your user profiles for this module to function'),
    );
  }

  $form['custom_ranks'] = array(
    '#type' => 'fieldset',
    '#title' => t('Custom ranks'),
  );

  $form['custom_ranks']['rank_metrics'] = array(
     '#type'  => 'markup',
     '#value' => t('Enter the minimum number of posts required to attain each rank'),
  );

  if ($list_values)  {
    $list_metrics = variable_get('postcount_rank_metrics',array(''));
    $values = split("[,\n\r]",$list_values);

    foreach ($values as $value)  {
        if ($value = trim($value))  {
             $form['custom_ranks']['rank_metrics' . $value] = array(
               '#type'         => 'textfield',
               '#title'        => $value,
               '#size'         => 5,
               '#maxlength'    => 10,
               '#defaultvalue' => array_shift($list_metrics),
             );
        }
    }
  }
  return $form;

}

function postcount_rank_nodeapi(&$node,$op,$arg = 0)  {
  if ($op == "insert")
    postcount_rank_update();
}

function postcount_rank_update()  {

  global $user;

  profile_load_profile($user);
  if (isset($user->uid) && ($fid = variable_get('postcount_rank_fieldid',-1)) != -1) {
    $name = db_result(db_query("SELECT name FROM {profile_fields} where fid=%d",$fid));
    if (!$name)
      return;
    $count = db_result(db_query("SELECT COUNT(*) from {node} where uid=%d",$user->uid));
    $count += db_result(db_query("SELECT COUNT(*) from {comments} where uid=%d",$user->uid));
    $ranks = variable_get('postcount_rank_metrics',array(''));
    while (current($ranks) !== FALSE && current($ranks) < $count) next($ranks);
    prev($ranks);
    if (key($ranks) != $user->{$name})  { // if the calculated rank isn't what's set
      if (!isset($user->{$name}))
        db_query("INSERT INTO {profile_values} (fid,uid,value) VALUES (%d,%d,'%s')",$fid,$user->uid,key($ranks));
      else db_query("UPDATE {profile_values} SET value='%s' WHERE fid=%d AND uid=%d",key($ranks),$fid,$user->uid);
    }
  }
}

function postcount_rank_comment($op,&$node)  {
  if ($op == "insert")
    postcount_rank_update();
}

?>

Comments

desm0n’s picture

I'd like to have a play around with this also. I can't help with the code i'm afraid but would like to see the module ported.

atin81’s picture

mmmmmmm

I'm afraid that this is going to be on limbo for some time, I can't find why is not working and nobody want to help us.

desm0n’s picture

Thats a real shame :(

ryivhnn’s picture

I'd also like to see this done, seems like the maintainer abandoned it.

I think the reason people won't help is coz the ones that can are probably busy doing other things and everyone else is trying to use UIEForums (which while it looks cool I found it a bit on the terrifying side to install, I don't like anything that has a high chance of destroying your web server if you sneeze while clicking a button during setup :P). Or using the phpbb bridge coz they can't handle the thought of not using phpbb :)

Not that there's anything wrong with those modules, they're not not my thing.

Anyway. Have you done much more work on this? I completely suck at php that's this hardcore but I can have a poke at it if I can find a second or so here or there. Or see if my programming god friend can find a second or so to have a look :)

works at bekandloz | plays at technonaturalist

ckeo’s picture

i'll see what i can figure out.

Craig.

(tho... i havnt done much php stuff before)

ckeo’s picture

line 14: $fid = variable_get('postcount_rank_fieldid',-1);

the persistent variable 'postcount_rank_fieldid' is not set.... so the custom ranks that i defined do not show up.
If I change the default value -1 to 5 which is the fid for that field (mine anyways) it works.

I changed the line to variable_set('postcount_rank_fieldid',5);
then refreshed my browser....
changed it back to what you had there originally and it loaded the custom ranks.

so.... 'postcount_rank_metrics' is where the values should be stored... which is also a persistent variable that is also not set.

these functions are located in bootstrap.inc.

its too late for me to test 'setting postcount_rank_metrics' to see if it will store the values... is past 4am here and i need to get some sleep.... so if anybody else gets a chance to try this.... good luck.

Craig

atin81’s picture

Hi ckeo. After study the code I arrived to the same point.

'postcount_rank_fieldid' & 'postcount_rank_metrics' are persistent variables that are never set before, but as soon as I read on the drupal docs as you are on the admin/settings page set the persistent variables must be auto after the first time you click on the Send button.

About using the variable_set funtion I can't figure where and how use.

Anyway ths for the help and hope we finish the converting soon.

ryivhnn’s picture

So do we have an updated this thing to work with or does the code from the last comment/post still stand?

works at bekandloz | plays at technonaturalist

ckeo’s picture

changed line: return $form;

to:

return system_settings_form('postcount_rank_settings', $form);

and now it seems that the postcount_rank_fieldid is working as it should.

ive never actually read the docs or anything... i think i'll do a bit of reading about the forms api.

( i tried setting the postcount_rank_metrics manually, but that didnt work)

craig.

atin81’s picture

return system_settings_form('postcount_rank_settings', $form);

this function is for the drupal 4.8 are you using it on the 4.7 version??? I can't make work saids undefined function.

ckeo’s picture

i dont actually know what that function does... i got the idea from a different module and it worked for me.

ryivhnn’s picture

Sorry I haven't had time to work on this guys, things kinda going into overdrive here atm (projects projects everywhere, hurray!)

How's it going, still hacking or given up?

works at bekandloz | plays at technonaturalist

atin81’s picture

I've being very bussy too, today I'm goint to continue working on the module, I'll keep informed about any progress