Support » Plugin: crosswordsearch » MyCred integration

  • Resolved boszor

    (@boszor)


    Hello I wanted to have a function that once the timer has expired, or for each word what was found, or was found wrong because there points in the form of Mycred can be awarded. I’ve been busy for a long time but unfortunately nothing leads to success.

    Can you help me with that?

    I would like the code to be executed as soon as certain words are found. Therefore, there is already $ solved. The code is in function crw_log_text ($ submission) {but unfortunately without desired success.

    The following code works

    $user_id = get_current_user_id();
    $FAILED = mycred_add( 'crossword', $user_id, $solved, 'Crosswordsearch found word' );
    $FAILED;
    • This topic was modified 2 years, 2 months ago by boszor.
Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Author ccprog

    (@ccprog)

    I am not sure if what you want isn’t contrary to the basic design of the plugin, but maybe I am missunderstanding you. The frontend code only communicates with the server once the timer has expired or the riddle was solved, and the user explicitely requests that the server should be alerted. Only then the backend code will be called. Marking wrong words is a frontend-only action and never communicated back.

    As long as that is understood and acceptable for your needs, there is an API you can leverage to integrate with myCred. The submission of a solution will raise an action crw_solution_submitted. Define in your code something like this:

    function mycred_crw_submit ( $user, $submission ) {
        extract( $submission );
        $user_id = $user->get( 'id' );
        $FAILED = mycred_add( 'crossword', $user_id, $solved, 'Crosswordsearch found word' );
    }
    add_action( 'crw_solution_submitted', 'mycred_crw_submit', 10, 2 );

    The function crw_log_text you cited is only called if another plugin integration is active and wants to format a log message for consumption. I think I’ll add a note to that effect to the Wiki page.

    I don’t think you need that unless you want to add the text to the log message. Like this, using the mycred_log_entry filter inside the above function:

    add_filter( 'mycred_log_entry', function ( $content, $entry ) use ( $submission ) {
        $crw_message = crw_log_text( $submission );
        return $content . '<br/>' . $crw_message;
    } );

    To get some ideas how things can work, you can take a look at function crw_badgeos_submit in file badgeos.php which provides the integration with BadgeOS.

    • This reply was modified 2 years, 2 months ago by ccprog.

    I have now entered the following function in the crosswordsearch.php directly under the function crw_submit_solution () {.

    Unfortunately, not with the success, the points are credited.
    The points do not have to be deducted for wrong words, but there should be 1 point per correct word at the end.

    I am very grateful for further help

    Here again the code

    add_action( 'wp_ajax_nopriv_submit_solution', 'crw_submit_solution' );
    add_action( 'wp_ajax_submit_solution', 'crw_submit_solution' );
    
    function mycred_crw_submit ( $user, $submission ) {
    extract( $submission );
    $user_id = $user->get( 'id' );
    $FAILED = mycred_add( 'crossword', $user_id, $solved, 'Crosswordsearch found word' );
    }
    add_action( 'crw_solution_submitted', 'mycred_crw_submit', 10, 2 );
    
    /* ----------------------------------
     * Settings Page Load Routines
    • This reply was modified 2 years, 2 months ago by boszor.
    Plugin Author ccprog

    (@ccprog)

    Oops, I think my WP skills are geting stale. It should be $user->get( 'ID' ) or simply $user->ID.

    If that does not help, can you see if mycred_add() is called at all?

    MyCred has a log that directly logs the credit.
    There is nothing to find in the log.

    MyCred add works fine in other plugins

    This is a Hangman code, it works fine

    $user_id = get_current_user_id();
    $FAILED = mycred_add( 'hangman', $user_id, -50, 'hangman you are hanging' );
    $FAILED;

    Where I would like to have the -50 already like $solved.

    Plugin Author ccprog

    (@ccprog)

    Where do you call that? In function mycred_crw_submit? You could try to feed $FAILED back to the user feedback message like this:

    add_filter('crw_solution_message', function ( $message ) use ( $FAILED, $submission ) {
        $success = $FAILED ? 'recorded' : 'declined';
        return $success . '<br/>' . print_r( $submission, true);
    }

    After submitting the solution, the dialog should stay open and display some text:

    • If recorded is displayed, the credit should be logged
    • If declined shows, the function was called but failed to record anything
    • If nothing shows or the dialog hangs without closing, the function call has probably failed. I’d expect an error message in your PHP log.
    • This reply was modified 2 years, 2 months ago by ccprog.

    As soon as I insert this code under the add_action I get a direct error and nothing works anymore.

    Has the error done, there was one missing).
    I’ll see what I see.

    • This reply was modified 2 years, 2 months ago by boszor.
    Plugin Author ccprog

    (@ccprog)

    Not under add_action, but inide the mycred_crw_submit function.

    I have now inserted the following code there.
    The window is nothing at the end and you can close it over the button again. In the log, nothing is displayed to me.

    function mycred_crw_submit ( $user, $submission) {
    extract( $submission );
    $user_id = $user->ID;
    $FAILED = mycred_add( 'crossword', $user_id, $solved, 'Crosswordsearch found word' );
    add_filter('crw_solution_message', function ( $message ) use ( $FAILED, $submission ) {
        $success = $FAILED ? 'recorded' : 'declined';
        return $success . '<br/>' . print_r( $submission, true);
    });
    }
    add_action( 'crw_solution_submitted', 'mycred_crw_submit', 10, 2 );
    Plugin Author ccprog

    (@ccprog)

    OK, that looks like an error in connecting to myCred. I’ll test that myself, but that will take time. Expect an answer in the next few days.

    All right, I thank you very sincerely and await your answer.

    Plugin Author ccprog

    (@ccprog)

    I’ve now tested myself, but found no problem. What I did:

    1. Installed and activated myCred 1.7.9.3
    2. Copy&Paste your above code into crosswordsearch.php
    3. Solved a riddle with shortcode atributes mode="solve" timer=15 submitting="1"
    4. On the message that I’d found 4 of 9 possible words, pressed the “Submit” button
    5. The following message was displayed:

      You have found 4 of 9 hidden words during the alloted time.
      recorded
      Array
      (
      [project] => test
      [name] => test
      [time] => 15.1
      [solved] => 4
      [total] => 9
      )

    6. A log entry about 4 points gained was added.

    Additionally I tested with several accounts having different roles and submitting while logged out (which leads to the user being asked for his password in the dialog). Every time recording the points worked without a hitch.

    Things I can still think of: Did you use the adminstrator account for solving the riddle, but excluded point administrators in the myCred settings? Did you send in a riddle with 0 found words? (Both led to a “declined” message for me and no log entry was recorded.) Did define an extra Point type and looked in that log?

    • This reply was modified 2 years, 2 months ago by ccprog. Reason: formatting

    Thanks it was the shortcode that did not have the submit attribute. Thanks also now I got 4 points.

    Plugin Author ccprog

    (@ccprog)

    If you are using the shortcode wizzard (the button above the editor) for adding them, remember marking the “Let users submit their result” checkbox.

    Thanks for the tip, now we have solved the problem yet. Many Thanks

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘MyCred integration’ is closed to new replies.