Jump to content

Right Answers


One of the features of Formularize, is the ability to build quiz. One of the features of the quiz type is the ability to mark a right answer for for a field and give it a point value. However its a limited system, as you can only choose 1 right answer and definitively add points. That is where the advanced field option of Right Answers come into play. Here you can use PHP to expand the functionality of this feature, for example you might have an question that has a few "right" answers, but some answers are more "right" than others. With this feature, you can do more to determine what is a "right answer". for this example, we will be using a multiple choice questions, with 6 answers, each answer will be "right" to some extent, so we will assign them different point values.

First thing you want to do, is create the field on a quiz or quiz wizard type form, using the form builder.

Screen Shot 2022-02-24 at 1.12.36 AM.pngScreen Shot 2022-02-24 at 1.13.03 AM.png

as you can see on the field options, we created 6 answer, but on the Quiz tab, we can only select 1 of those answers as right. Now me being a huge DS9 fan, there is only one right answer to this question, but we will allow the other series some latitude, so we will need to create a "Right Answers" in the advanced field options in the ACP. Community->Formularize->Right Answers:

Screen Shot 2022-02-24 at 1.15.45 AM.png

Click on "create new". Here we will need to type in the name of the form that our question is in.

Screen Shot 2022-02-24 at 1.18.23 AM.png

Once we have selected the form we want, we will next need to select the field from the field section. 

Screen Shot 2022-02-24 at 1.19.20 AM.png

Now here we have a new option not found in all the other advanced field options, we have the options of 'Use Class', this allows you to create a class for right answers, and call it from here. we wont dive into detail here on exactly what that means, but all the abilities i'm about to talk about here, will also apply to the class. (the class option is mostly if you want build a class and use it on multiple different fields). So we will leave the Use class option as disabled and just use the validation code editor. like the other advanced field options, there is also a tags box next to it, that are used in the class and passed back to formularize.

Okay so we have several properties we can use or manipulate here, to change the output for the right answer. here is are a rundown of the ones we have access too:

  • $this->field: this is the field object for field in question.
  • $this->value: this is the answer submitted thru the form.
  • $this->data: this is all the data available from the form (other field data, this data is in a "raw" format)
  • $this->item: the form item itself, its configuration and available data.
  • $this->math: formularizes math class, to perform math operations if need.
  • $this->valid*: this is a boolean value, this has a default of false, meaning the answer given by the user is not the right answer, you can switch this to true if they got the answer right.
  • $this->points*: how many points this answer is worth.
  • $this->score*: this is set to null, but if you want each answer to have a different worth, then set this to the maximum possible points this question was worth. so if they choose say an answer that is only worth 2 points, but 10 points were available, you would set this to 10 and $this->points to 2.
  • $this->correctAnswer*: the value that should be the right answer.
  • $this->processRightAnswer*: this is a boolean value, default is set to true. this means if the answer is incorrect, and if the form has "show right answer" enabled, this will process the $this->correctAnswer thru the fields value formatting before sending it to the output. if you format the correct answer yourself in this, then you will need to set this to false.

out of all these properties, only a few of them are returned back to formularize. the ones marked with an asterisk above are the ones that need to be set or altered by your code to have an impact on the output of the right answer.

So now we will need to build our code in the code mirror editor, using any of the above properties of the class that we need to set or alter. since our field is a multiple choice, the value that is sent to right answer will be the key from the field options tab. (i.e. the value if the choice selected was janeway would be 4).

Screen Shot 2022-02-24 at 2.16.12 AM.png

So we have our code set, now it is time to test it:

Screen Shot 2022-02-24 at 2.17.11 AM.pngScreen Shot 2022-02-24 at 2.17.25 AM.pngScreen Shot 2022-02-24 at 2.17.47 AM.pngScreen Shot 2022-02-24 at 2.18.16 AM.pngScreen Shot 2022-02-24 at 2.18.32 AM.pngScreen Shot 2022-02-24 at 2.18.57 AM.png

as you can see, we have set the code to give each possible answer a different point total, but have the question be worth an overall total. 

//this question is worth 50 points
$this->score = 50; 

//if they chose an answer, its value will be greater or equal to 1, otherwise it will be null or 0.
	$this->valid = true;

$this->correctAnswer = 3;//we are set the right answer to be, and we are gonna let field class format it to sisko.

  case 1: //this is kirk
    $this->points = 40; //we are gonna give them 40 points if they chose kirk
  case 2: //this is picard
    $this->points = 35; //we are gonna give them 35 points if they chose picard
  case 3: //this is sisko
    $this->points = 50; //we are gonna give them 50 points here, cause really this is the ONLY RIGHT ANSWER
  case 4: //this is janeway
    $this->points = 5; //we are only gonna give 5, cause this person has never watched trek a day in their life
  case 5: //this is archer
    $this->valid = false;
    $this->points =-50; //okay this person is just disturbed, we are gonna punish them and give them negative points!
  case 6: //this is lorca
    $this->points = 1; //meh he gets one extra point, he was alright, till he turned evil.

i've provided the code i wrote above, to show what i've done. There is a lot you can do with this if you want, you have full access to PHP and IPS classes here, so with that in mind, access to this section should be restricted to those who are trusted and before inserting any code here, you should have it validated to make sure it is not doing anything malicious. 

If you have any questions or comments, ask below.  


There are no comments to display.

  • Create New...