1

I have a system to evaluate student grades. I should be able to define evaluation criteria dynamically in a JSON object. For example a student will pass Java Programming module if:

(courseWorkMarks > 30 && inClassTest > 40) || examMarks > 40 // pass

The conditions vary from module to module, so each module will have a json object that describes the evaluation criteria. I thought of using Jquery QueryBuilder to do this. I can build the formula using its UI and save it as a JSON and I have student's marks list for a module in a JSON object like this:

{courseWorkMarks: 50, inClassTest: 35, examMarks: 45}
  1. How do I check whether this student fulfill the pass criteria using the JSON generated by jQuery QueryBuilder?

  2. I want to display the pass/fail criteria for each condition separately. Example for the above case:

    • courseWorkMarks Result - PASS
    • inClassTest Result - FAIL
    • examMarks Result - PASS
    • overall Result - PASS

I can do it with JSON path, but it doesn't have a nice graphical query builder like jQuery QueryBuilder. The main problem I have is I don't understand how to use the JSON output generated by jQuery QueryBuilder to evaluate the data I have. Is it possible to do what I want with jQuery QueryBuilder? Are there any other javascript tools to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Deepan Cool
  • 476
  • 1
  • 6
  • 16

1 Answers1

2

I am bit late in responding to this question, hopefully you had a solution by now.

Recently, I have also used jQuery QueryBuilder in one of my project and figured out the following solution.

var lastOperator = [];
var rulesLengths = [];
var ruleLevel = 0;
var equationString = '';
var rulesData = {
     "condition": "AND",
     "rules": [
        {
           "id": "genderSelect",
           "field": "genderSelect",
           "type": "string",
           "input": "select",
           "operator": "equal",
           "value": "Male"
        },
        {
           "id": "ageSelect",
           "field": "ageSelect",
           "type": "string",
           "input": "select",
           "operator": "equal",
           "value": "18-26"
        }
     ],
     "valid": true,
  };

var expr = checkDisplayLogic(rulesData);
var flag = eval(expr);
/** Recursive function to convert json data to expression **/
function checkDisplayLogic(rulesData) {
    if (rulesData['rules'] !== undefined) {
        ruleLevel++;
        var operator = rulesData['condition'];
        lastOperator.push(operator);
        rulesLengths.push(rulesData.rules.length);
        equationString += '(';
        $.each(rulesData.rules, function (idx, elem) {
            checkDisplayLogic(elem);
            if (rulesLengths[rulesLengths.length - 1] == idx + 1) {
                equationString += ')';;
                rulesLengths.pop();
                ruleLevel--;
                lastOperator.pop();
            }
            else {
                if (lastOperator.length !== 0)
                    equationString += ' ' + (operator === 'AND' ? '&&' : '||' ) + ' ';
            }
        });
    }
    else {
        var inputType = rulesData.input;
        switch (inputType) {
            case 'select':
                var ctrl = $('#' + rulesData.id);
                //Below logic will change depending upon the operator selected in QueryBuilder. 
                //In my case operator is 'equal' that's why I am using '===' operator for comparison
                equationString += (ctrl.val() === rulesData.value).toString();
                break;
            /****** Similarly create cases for other input types QueryBuilder provide ********/
        }
    }

    return equationString;
} 

Happy Coding!

anurag_it
  • 27
  • 1
  • 5