1

I've been reading for days, trying several examples but I'm just not getting it.

I have the Rules module as well as the H5P module. I'm attempting to create a Rules event from the existing H5PFinished event (which may or may not be the way to go about this).

Their function constructs an array of five fields, which then update in the GDB.

// which fields to update
$fields = [
  'content_id' => $content_id,
  'uid' => $uid,
  'finished' => time(),
  'points' => $score,
  'max_points' => $max_score,
];

They then invoke an event.

$this->eventDispatcher->dispatch(FinishedEvent::FINISHED_EVENT, new FinishedEvent($fields));

In the FinishedEvent.php file, they name the event and return the fields as an array.

class FinishedEvent extends Event {

  const FINISHED_EVENT = 'h5p.finished';

  /**
   * @var array
   */
  protected $quizData;

  /**
   * Constructs a FinishedEvent object.
   *
   * @param array $quiz_data
   *   Database connection service.
   */
  public function __construct(array $quiz_data) {
    $this->quizData = $quiz_data;
  }

  /**
   * @return array
   *   Quiz data.
   */
  public function getQuizFields() {
    return $this->quizData;
  }

}

I'd like to be able to have a custom rule event which basically is fired when the database is updated, and if the user has score == max_score, a data value is set in their user profile (I do have the actions working fine. I tested using a simple "if page is viewed). I'm just super lost.

I've created the custom module to add the Event to Rules. I've used the event name defined in and subscribed to in H5P ("h5p.finished") and added four integer type context_definitions (which worked earlier, but now throws an error that the data needs to be of type array, string given).

mymodule.rules.events.yml

h5p.finished:
label: 'Interactive Content is Solved'
category: 'H5P'
context_definitions:
   content_id:
      type:'integer'
      label:'Content ID'
   max_score:
      type:'integer'
      label:'Max Score'
   score:
      type:'integer'
      label:'Score'
2
  • 1
    FWIW, I might look at the ECA: Event - Condition - Action module before starting a new project with the Rules module.
    – sonfd
    Jan 8, 2023 at 15:56
  • 1
    Hi. I've been playing with ECA as well, but since it's so new, the documentation is still on the light side. The documentation for Rules is somewhat more hearty.
    – Jennifer
    Jan 8, 2023 at 19:47

1 Answer 1

1
  1. type:'integer' is not valid yaml, you must have a space after the colon, type: 'integer'
  2. You didn't indent fields that are after the event machine name, h5p.finished:
  3. Use the same keys as the $fields array.
h5p.finished:
  label: 'Interactive Content is Solved'
  category: 'H5P'
  context_definitions:
    content_id:
      type: 'integer'
      label: 'Content ID'
    max_points:
      type: 'integer'
      label: 'Max Score'
    points:
      type: 'integer'
      label: 'Score'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.