Hello.

Your module is really what I need, but i have a very little experience in code writing. Please help, if you can and want.
I read your achievements.api.php file and understand most of it. I decided, that i will not create a new module folder, just modify yours. And now I can't understand what file in your folder I must change - achievements.module, achievement.tpl.php or achievements.api.php itself. When I change achievements.api.php nothing happens and when I change achievements.module, my site don't open at all.

I know that a question is stupid, but somehow I must start to uindestand a drupal system better.

Thanks a lot.

Comments

morbus iff’s picture

Component: Code » Documentation
Status: Active » Fixed

I decided, that i will not create a new module folder, just modify yours.

That's your biggest problem. Don't do that. Follow "Creating achievements" in the README or on the project page.

taecelle’s picture

Thanks.
Now I understand it.

But i need help anyway. I created a folder "my_achieve", in folder - two files: my_achieve.info and my_achieve.module.
In my_achieve.module i put a part of your example code:


function my_achieve_achievements_info() {
  $achievements = array(
    'comment-count-500' => array(
      'title'       => t('Posted 500 comments!'),
      'description' => t("We no longer think you're a spam bot. Maybe."),
      'storage'     => 'comment-count',
      'points'      => 50,
    ),
        ),

  return $achievements;
}

/**
 * Implements hook_comment_insert().
 */
function my_achieve_comment_insert($comment) {
  $current_count = achievements_storage_get('comment-count', $comment->uid) + 1;
  achievements_storage_set('comment-count', $current_count, $comment->uid);

  foreach (array(500) as $count) {
    if ($current_count == $count) {
      achievements_unlocked('comment-count-' . $count, $comment->uid);
    }
  }
}

When I try to enable this module, my site just goes blank. What i made wrong? (I know, that i made ALL wrong, but may be it can be helped)))

morbus iff’s picture

You'd need to check your web server's error or PHP log, and/or do a syntax check with whatever code editor you're using. Full whitescreens like that tend to mean a syntax error which, here, is likely given that you've got a comma where you should have put a semi-colon (in my_achieve_achievements_info(). There might be others. Note also that your actual achievement unlock code is unlikely to work since you've only defined one achievement for 500 comments - not 500 achievements for 1 comment, 2 comments, 3 comments, 4 comments, etc.

taecelle’s picture

Thanks, i just found the error. Now site works ok, but in profile no achievements is unlocked (I have a 490 comments). What i must do to unlock them for every user who reached milestones earlier??

Code is:


function my_achieve_achievements_info() {
  $achievements = array(
    'comment-count-50' => array(
      'title'       => t('Posted 50 comments!'),
      'description' => t("We no longer think you're a spam bot. Maybe."),
      'storage'     => 'comment-count',
      'points'      => 50,
    ),
    'comment-count-100' => array(
      'title'       => t('Posted 100 comments!'),
      'description' => t('But what about the children?!'),
      'storage'     => 'comment-count',
      'points'      => 100,
      'images' => array(
        'unlocked'  => '/sites/default/files/example1.png',
        // 'secret' and 'locked' will use the defaults.
      ),
    ),
'article-creation' => array(
      'title' => t('Article creation'),
      'achievements' => array(
        'node-mondays' => array(
          'title'       => t('Published some content on a Monday'),
          'description' => t("Go back to bed: it's still the weekend!"),
          'points'      => 5,
        ),
      ),
    ),
  );

  return $achievements;
}

/**
 * Implements hook_comment_insert().
 */
function my_achieve_comment_insert($comment) {
  $current_count = achievements_storage_get('comment-count', $comment->uid) + 1;
  achievements_storage_set('comment-count', $current_count, $comment->uid);

  foreach (array(50, 100) as $count) {
    if ($current_count == $count) {
      achievements_unlocked('comment-count-' . $count, $comment->uid);
    }
  }
}

function my_achieve_achievements_info_alter(&$achievements) {
  $achievements['comment-count-100']['points'] = 200;
}
morbus iff’s picture

If you want to offer retroactive achievements, you'd have to code them yourself (either by writing a one-time importer script to determine and grant them all), or specifically tweak your code so that the next time they write a comment, you'd count what they have, set the internal achievement storage to that value, then unlock all previous achievements.

taecelle’s picture

OOO, it works!!
Thank you.
And one little question (pleeese!)
How can i make, that only comments to specific node type are taken into account?

morbus iff’s picture

Check the $comment variable (print it out with print_r() or some such, etc.). If it doesn't include the node type (I don't recall off the top of my head), then it does contain $comment->nid so you could always $node = node_load($comment->nid) and check the $node->type.

taecelle’s picture

Yep, it doesn't contain a type in comment variable. Please, can you help to write a right code for this?

/**
 * Implements hook_comment_insert().
 */
function my_achieve_comment_insert($comment) {
$node = node_load($comment->nid);
if ($node->type == 'XXX' && $node->status == 1) {  
$current_count = achievements_storage_get('comment-count', $comment->uid) + 1;
  achievements_storage_set('comment-count', $current_count, $comment->uid);

  foreach (array(2, 100) as $count) {
    if ($current_count == $count) {
      achievements_unlocked('comment-count-' . $count, $comment->uid);
    }
  }
}

Is it right?

Thank you very much for your help)

morbus iff’s picture

Did you try it?

taecelle’s picture

yes, it works fine)
Thank you)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.