Length of the General Comment

Usapuka's picture

Hi,
In the Multiple Choice Question, within a Questions Set: how can I make the General Comment (at the end of each question) has more than 255 characters long?

Thanks!

Content types: 
otacke's picture

Hi Usapuka!

Either the H5P core team is fine with having longer general comments and someone contributes the code changes (very minimal), or you use the customization functions that the H5P plugin offers ("alter_semantics") to set the field to a larger value, see https://h5p.org/documentation/for-developers/authoring-tool-customization - but I fear at least some basic knowledge of PHP will be required to do that. Downside here: If you share the files, others won't see the comment in full length unless they applied the same customization that you used.

Best,
Oliver

Usapuka's picture

Hello Oliver,

Thanks for your help, as I am working on WordPress I have installed the Plugin (h5pmods) and made some Visual Changes. The newbies guide has been very helpful for this.

Is there a newbies guide to implement the "alter_semantics"? With Chrome's development tools it is very easy to find the CSS modifications, but in order to increase the number of characters of the General Comment: where is the information of the semantics to be modified and the necessary steps to do it?

Thanks again,

Best
Gabriel

otacke's picture

Hi Gabriel!

I don't think there's something tutorial-ish, but you can find similar examples in the thread at https://h5p.org/node/188086. You basically need to retrieve the field for the comment inside semantics.json (that follows the specification at https://h5p.org/semantics), and you'd have to set the max attribute for that field.

Best,

Oliver 

Usapuka's picture

Hi Oliver,

Thanks again for your help. I think I understand the logic of what I should do. I have studied the semantics of Question Set (https://github.com/h5p/h5p-question-set/blob/master/semantics.json). So I see that in the case of the General Comment, it goes into a "placeholder" that must be limiting the amount to 255 characters. This does not happen in the case that the feedback goes after any answer (see attached).

Since in one case or another the number of characters is not specified in the semantics of the Question Set, what should I modify and where to increase the number of characters in the General Comment?

Thank you.

Best
Gabriel

 

Attachments: 
otacke's picture

Hi Gabriel!

H5P uses a modular approach: Content types can include other content types. For instance, Question Set is just a wrapper for Multiple Choice, Fill-in-the-Blanks, etc. (https://github.com/h5p/h5p-question-set/blob/master/semantics.json#L126-L132) I believe you want to and should alter the semantics of Multiple Choice. You could simply use the $library_name variable as a switch for customizing particular semantics files of particular content types.

Best,
Oliver

Usapuka's picture

Hi Oliver,

I'm sorry but this time I don't understand your answer and what I should do.
The General Comment semantics for the Question Set are here https://github.com/h5p/h5p-question-set/blob/5fa83227f9e58905271ee74247a... and not in the Multiple Choice semantics.

Please be so kind as to explain to me what you mean by that? "You could simply use the $library_name variable as a switch for customizing particular semantics files of particular content types".

Best
Gabriel

otacke's picture

Hi Gabriel!

The image that you attached didn't show the general feedback of Question Set, but the specific feedback to answers that you can set in Multiple Choice, see https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L115-L148.

In the code examples at https://h5p.org/node/188086 you can see how $library_name is used to determine what library is currently loaded (the hook will be called for all of them) to decide whether you want to override the semantics or not. A switch could be something like:

if ($library_name === 'H5P.MultiChoice') {
  /*
   * $semantics holds the semantics of H5P.MultiChoice
   * and can be overridden.
   */
}
elseif ($library_name === 'H5P.QuestionSet') {
  /*
   * $semantics holds the semantics of H5P.QuestionSet
   * and can be overridden.
   */
}
elseif ($library_name === 'H5P.Blanks') {
  // ...
}

If you prefer, you could use the switch function, too, hence the name switch.

Cheers,
Oliver

Usapuka's picture

Hi Oliver,

I'm sorry, I still don't understand the point of what I'm supposed to do, I'm a newbie at this.
My need is to increase the number of characters to more than 255 characters of the General Comment. For this you indicated me that I should modify the Semantics. I assumed that I would get to a place where I would see the definition that limits the number of characters to 255 and could change it to a higher number.

Is my reasoning correct? How does what you are telling me to do relate to increasing the number of characters?

Thank you for your help,

Best
Gabriel

 

otacke's picture

Hi Gabriel!

Sorry, I can neither write the whole script for you nor introduce you to coding in PHP. I used to do that before, but I learned that I a cannot be responsible for solving everyone's problems.

The steps would be:

A) Be sure what you want to override. You say you want to override the overall feedback of Question Set, but the image that you attached before showed the per answer feedback of Multple Choice.

B) Locate the appropriate field in semantics. For the overall feedback of Question Set it's feedback (https://github.com/h5p/h5p-question-set/blob/master/semantics.json#L341-L348). For the per answer feedback of Multiple Choice it's chosenFeedback or notChosenFeedback (https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L115-L147). Mind the hierarchy those fields are put in.

C) Set up a very basic alter_semantics hook as a customization for your platform, see https://h5p.org/node/2692. As a reference for coding, you can use the examples depicted at https://h5p.org/comment/18767#comment-18767 or https://h5p.org/comment/18898#comment-18898. Try to understand what they are doing. The latter tries to explain how the hierarchy of semantics works.

D) Implement your changes: You need to

  1. Use the $library_name variable to determine what content type the semantics belongs to that H5P offers you to override.
  2. Search the $semantics variable for the field object that you want to change.
  3. The field objects' type property has the value text, so according to the semantics specification at https://h5p.org/semantics#type-text you can set the property maxLength to what you prefer (if nothing is specified, it defaults to 255 as mentioned at https://h5p.org/semantics#attribute-max-length).
    Side note: If you in fact want to override the maxLength of the per answer feedback of Multiple Choice, then that's not possible as I just noticed in the specification. Those field have use the HTML widget which does not support the maxLength property.

Best,
Oliver

Usapuka's picture

Hi Oliver,

Thank you for your patience and help. I think I'm understanding.

In the two examples that you indicated me, the value of the attribute to be modified is specified in the semantics and the default value is changed.

In the case of the "feedback" where the "type": "text": how may I change an attribute that is defined by default (or in another place) in 255 ? (see attachment).

Best
Gabriel

Attachments: 
otacke's picture

Hi Gabriel!

You're welcome!

When you have completed step 2, then in PHP you will hold an object (let's call it $field) that contains these attributes. What's

{
  "name": "feedback",
  "type": "text",
  "label": "Feedback for defined score range",
  "importance": "low",
  "placeholder": "Fill in the feedback",
  "optional": true
}

in semantics would be represented by $field and could be read and set, e.g.

$foo = $field->type;

// $foo will now hold 'text'

$field->maxLength = 1000;

/*
 * The field now has the attribute maxLength
 * with the value 1000 and that's the amount
 * of characters that the field will accept.
 */

$field->label = 'LABEL';

/*
 * The label in the editor will now be
 * LABEL
 * instead of
 * Feedback for defined score range
 * ...
 */

Best,
Oliver

Usapuka's picture

Hello Oliver,

To simplify my work I am trying to make the changes in the Multiple Choice question (see attached) https://github.com/h5p/h5p-multi-choice/blob/99ef831a691680ac61a447fc273....
To see if I am on the right track I have tried to change the lebel of the question, without any result. This is what I did:

function h5pmods_alter_semantics(&$semantics, $library_name) {
  
  if ($library_name === 'H5P.MultiChoice') {
  
        if ($semantic_field->name === 'overallFeedback') {
        
        $group_items = $semantic_field->fields->field;
        
        foreach($group_items as $group_item) { 
          if ($group_item->name === 'feedback') {
            $group_item->label = 'Retroalimentacion';             
        }
      }  
    }
  }
}
action('h5p_alter_library_semantics', 'h5pmods_alter_semantics', 10, 4);

 

What am I doing wrong?

Thanks again, bests.

Gabriel

otacke's picture

Hi!

You're trying to access the variable $semantic_field that doesn't exist. You get $semantics which is an object that holds all the fields. You'll have to parse it to get to the correct field first. That's what the foreach loop in https://h5p.org/comment/18767#comment-18767 is doing (for one level of the object tree).

Best,
Oliver

Usapuka's picture

Hi Oliver,

Following your advice I have managed to replace 2 "Overall Feedback" titles (see attached) for which I have used this code:

function h5pmods_alter_semantics(&$semantics, $library_name) {
  
  if ($library_name === 'H5P.MultiChoice') {
             
    foreach ($semantics as $semantic_field) {
      if ($semantic_field->name === 'overallFeedback') {
        $semantic_field->label = 'Final feedback (this is a test)';

      $overallFeedback_fields = $semantic_field->fields;

        foreach($overallFeedback_fields as $overallFeedback_field) { 
         
          if ($overallFeedback_field->name === 'overallFeedback') {
              $overallFeedback_field->label = 'This is another test';             
          }
          if ($overallFeedback_field->name === 'feedback') {
              $overallFeedback_field->label = 'Gran retroalimentación';             
          }
        }
      }
    }
  }
}
add_action('h5p_alter_library_semantics', 'h5pmods_alter_semantics', 10, 4);

I have made several attempts to change the "feedback" label (to know that I reached the level where I will change the text maxLength), but I have not succeeded: I will be grateful for any hint you give me to know how to reach that level.

Thanks, best.

Gabriel

Attachments: 
otacke's picture

function h5pmods_alter_semantics( &$semantics, $library_name ) {
  if ( $library_name === 'H5P.MultiChoice' ) {

    foreach ( $semantics as $semantic_field ) {
      if ( $semantic_field->name === 'overallFeedback' ) {
        $semantic_field->label = 'Final feedback (this is a test)';

        $overall_feedback_fields = $semantic_field->fields;
        foreach( $overall_feedback_fields as $overall_feedback_field ) {

          if ( $overall_feedback_field->name === 'overallFeedback' ) {
              $overall_feedback_field->label = 'This is another test';
          }

          // Won't work, see explanation below
          if ( $overall_feedback_field->name === 'feedback' ) {
              $overall_feedback_field->label = 'Gran retroalimentación';
          }

          /*
           * The part of the editor's semantics that $overall_feedback_fields is
           * holding is https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L160-L210
           * It contains just one element ("name": "overallFeedback") that you
           * can see at
           * https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L161-L209
           * There's none that's named "feedback", so that cannot be found.
           * We need to dig deeper.
           * You can see that the overallFeedback field has other properties
           * next to name: type (list), importance (high), ... , AND "field".
           * https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L177-L208
           * So we would have to look at $overall_feedback_field->field that one is
           * also named "overallFeedback", so not the one you are looking for.
           * But that group has a property fields that contains three elements:
           * https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L181-L206
           * One of them is the one you are looking for. So, let's just loop
           * over $overall_feedback_field->field->fields until we find the one
           * that you want to change ...
           */
          $overall_feedback_elements = $overall_feedback_field->field->fields;
          foreach( $overall_feedback_elements as $overall_feedback_element ) {
            if ( $overall_feedback_element->name === 'feedback' ) {
              $overall_feedback_element->maxLength = 1000;
            }
          }
        }
      }
    }
  }
}
add_action( 'h5p_alter_library_semantics', 'h5pmods_alter_semantics', 10, 4 );
Usapuka's picture

Hi Oliver,

According to your indications, the code looks like this:

function h5pmods_alter_semantics(&$semantics, $library_name) {
  
  if ($library_name === 'H5P.MultiChoice') {
             
    foreach ($semantics as $semantic_field) {
        
      if ($semantic_field->name === 'overallFeedback') {
        $semantic_field->label = 'Final feedback (this is a test)';
      }

      $overallFeedback_fields = $semantic_field->fields;

      foreach($overallFeedback_fields as $overallFeedback_field) { 
  
        if ($overallFeedback_field->name === 'overallFeedback') {
              $overallFeedback_field->label = 'This is another test';             
        }

        $overall_feedback_elements = $overall_feedback_field->field->fields;

        foreach($overall_feedback_elements as $overall_feedback_element) {
          
          if ($overall_feedback_element->name === 'feedback') {
              $overall_feedback_element->maxLength = 1000;
          } 
        }
      }
    }
  }
}

Unfortunately it does not modify the maxLength of the text.

To check that we are in the right place, I tried the following:

          if ($overall_feedback_element->name === 'feedback') {
              $overall_feedback_element->label = 'Here we are (test)';
          }

And the lebel did not change either.

What is wrong?

Thanks, best.
Gabriel

otacke's picture

My bad. My example wasn't properly nested ...

function h5pmods_alter_semantics( &$semantics, $library_name ) {
  if ( $library_name === 'H5P.MultiChoice' ) {

    foreach ( $semantics as $semantic_field ) {
      if ( $semantic_field->name === 'overallFeedback' ) {
        // We're now at "overallFeedback"
        // https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L155

        $semantic_field->label = 'Final feedback (this is a test)';

        $overall_feedback_fields = $semantic_field->fields;
        foreach( $overall_feedback_fields as $overall_feedback_field ) {

          if ( $overall_feedback_field->name === 'overallFeedback' ) {
            // We're now at "overallFeedback/overallFeedback"
            // https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L162

            $overall_feedback_elements = $overall_feedback_field->field->fields;
            foreach( $overall_feedback_elements as $overall_feedback_element ) {

              if ( $overall_feedback_element->name === 'feedback' ) {
                // We're now at "overallFeedback/overallFeedback/overallFeedback/feedback"
                // https://github.com/h5p/h5p-multi-choice/blob/master/semantics.json#L200

                $overall_feedback_element->placeholder = 'I can not take up to 1000 chars!';
                $overall_feedback_element->maxLength = 1000;
              }
            }
          }
        }
      }
    }
  }
}
Usapuka's picture

Hi Oliver,

Excellent, now it's working! (see attachment).
Thanks for all your help, I learned a lot about H5P.

Best,
Gabriel

Attachments: 
otacke's picture

You're welcome! And kudos to you for your perseverance! Most people wouldn't even have started ("oh, that's coding, I can't do this").