1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating custom trophies

Discussion in 'XenForo Questions and Support' started by bashy, May 6, 2012.

  1. bashy

    bashy Member

    I have searched the forums but people post threads without putting descriptive titles :p
    So I'm wanting to create a custom trophy when someone enters something in to a custom profile field I made.

    Here's a preview of what I have so far but I don't think this box means "User value contains text", more of "User value contains THIS CERTAIN text"?

    I want the user to get the trophy IF they enter text into it (every entry will be different so I need it to be anything a-z.)

    [​IMG]

    And;

    [​IMG]
     
  2. Martyn

    Martyn Active Member

    maybe you can use some custom setting in the Custom User Field Criteria in the Trophy Options section?

    like this "^[a-zA-Z0-9 _.]+$" unsure if it will work..
     
  3. bashy

    bashy Member

    Ah could try using regex, didn't think of that, will report back.
     
  4. bashy

    bashy Member

    Sadly using regex didn't work.
     
  5. Martyn

    Martyn Active Member

    ah must be away
     
  6. bashy

    bashy Member

    Yeah, just need it to notice if the field if(!empty($field)) or if(isset($field))
     
  7. Martyn

    Martyn Active Member

    I tried using regex and I also tried leaving the field blank! As surely if no requirements are set then anything warren I there would be classed as it being entered if you get me? :)
     
  8. bashy

    bashy Member

    Yeah, I myself did try ticking the box and leaving it empty, no luck. Seems you can only set it for exact text in field, no rules.
    Thanks for trying anyway, will see if someone else replies with a brainy idea or hack for it :)
     
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    If you are curious about the code:

    library/XenForo/Helper/Criteria.php

    Code:
    			// custom user fields
    			if (strpos($criterion['rule'], self::$_userFieldPrefix) === 0)
    			{
    				$userFieldId = substr($criterion['rule'], self::$_userFieldPrefixLength);
    
    				if (!isset($user['customFields'][$userFieldId]))
    				{
    					return false;
    				}
    
    				$userField = $user['customFields'][$userFieldId];
    
    				// text fields - check that data exists within the text value
    				if (isset($data['text']))
    				{
    					if (stripos($userField, $data['text']) === false)
    					{
    						return false;
    					}
    				}
    				// choice fields - check that data is in the choice array
    				else if (isset($data['choices']))
    				{
    					// multi-choice
    					if (is_array($userField))
    					{
    						if (!array_intersect($userField, $data['choices']))
    						{
    							return false;
    						}
    					}
    					// single choice
    					else
    					{
    						if (!in_array($userField, $data['choices']))
    						{
    							return false;
    						}
    					}
    				}
    			}
    
    There is no pattern matching. It checks specific values. You can modify this code, but then you have to redo it when you upgrade.
     
  10. Martyn

    Martyn Active Member

    maybe theres an away to add multiply options "User value contains text:" so maybe you could have multiple entrys? such as a,b,c,d,e etc or a;b;c;d;e? if you get me?
     
  11. bashy

    bashy Member

    I don't mind about redoing code, doubt I'll be upgrading anytime soon either (unless there's some major new features of bug fixes).

    So if I wanted a trophy to apply when someone enters ANY text into that custom field and maybe add another checkbox to the options "Award on completing field" what would I have to change/add?

    I know basic PHP so any example would be helpful and apprecaited.
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Untested but...

    Code:
    			// custom user fields
    			if (strpos($criterion['rule'], self::$_userFieldPrefix) === 0)
    			{
    				$userFieldId = substr($criterion['rule'], self::$_userFieldPrefixLength);
    
    				if (!isset($user['customFields'][$userFieldId]))
    				{
    					return false;
    				}
    
    				$userField = $user['customFields'][$userFieldId];
    
    				// text fields - check that data exists within the text value
    				if (isset($data['text']))
    				{
    					if ($data['text'] == 'anything' AND empty($userField))
    					{
    						return false;
    					}
    					else if ($data['text'] != 'anything' AND stripos($userField, $data['text']) === false)
    					{
    						return false;
    					}
    				}
    				// choice fields - check that data is in the choice array
    				else if (isset($data['choices']))
    				{
    					// multi-choice
    					if (is_array($userField))
    					{
    						if (!array_intersect($userField, $data['choices']))
    						{
    							return false;
    						}
    					}
    					// single choice
    					else
    					{
    						if (!in_array($userField, $data['choices']))
    						{
    							return false;
    						}
    					}
    				}
    			}
    
    This uses the word anything as a keyword to mean exactly that.
     
  13. bashy

    bashy Member

    Oh, nice idea!
    Will test it.
     
  14. bashy

    bashy Member

    Just tested it, works fine :)

    [​IMG]

    Many thanks Jake!
     
  15. 0xym0r0n

    0xym0r0n Well-Known Member

    worked for me too.

    Side note for people looking at this. Make sure you enter 'anything' in the text field that you want as a wildcard.
     
  16. MasterPiece

    MasterPiece Active Member

    Cool thank you and it works on 1.2.0 :)
     
    Jake Bunce likes this.
  17. WhoIsDo

    WhoIsDo Active Member

    I've been looking for a solution to this for awhile. Thank you!
     
  18. Echelon

    Echelon Active Member

    do i need to fill in the word anything here.......?

    anything.png
     
  19. zastavra

    zastavra Formally Stevez

    What if a user removes the text from that custom field? Are the related points also getting removed from his total trophy points?
     
  20. Mike

    Mike XenForo Developer Staff Member

    Trophies are not revoked. They represent one-time triggers/occurrences.
     
    zastavra likes this.

Share This Page