Score:1

Custom event from existing event

cn flag

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'
sonfd avatar
in flag
FWIW, I might look at the [ECA: Event - Condition - Action](https://www.drupal.org/project/eca) module before starting a new project with the Rules module.
Jennifer avatar
cn flag
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.
Score:1
ua flag
  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'
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.