Question Authors' Forum

sample for php and javascript question

 
Picture of jupa poma
sample for php and javascript question
by jupa poma - Friday, 22 January 2016, 10:02 AM
 

Hi,

I need some examples about questions in php and javascript, I try with this response for php

Answer

function algo($entero){

  return ($entero +1);

}

Test case 1

echo(algo(1));

Expected output

2

and the respose is  "Detailed test results unavailable. Perhaps an empty answer, or question not using Adaptive Mode?"


please help me


Picture of Nils Promer
Re: sample for php and javascript question
by Nils Promer - Friday, 22 January 2016, 8:36 PM
 
Hi jupa poma,

I'm not familiar with php and javascript, so I'm not able to check your code but regarding the adaptive mode I have an idea cause we had a similar problem.

It is maybe caused by the settings of the question behaviour.

Open the preview of your question. In "Attempt options" you can change the question behaviour on adaptive mode as shown on the picture.




After you saved this config "Start again with these options" your exercise should run the way want it to.

Hope this works.

Nils
Picture of jupa poma
Re: sample for php and javascript question
by jupa poma - Saturday, 23 January 2016, 4:13 AM
 

thanks finally I put the questions, with the adaptive mode

<?php  function algo($entero){

  echo ($entero * $entero);

}?>

Test case 1

<?php algo(2) ?>

Expected output


4

(In a newline )


I don't know how to make questions for javascript yet


Picture of Richard Lobb
Re: sample for php and javascript question
by Richard Lobb - Saturday, 23 January 2016, 7:01 AM
 

Hi Jupa,

Good to know that the php question is working. Thanks for helping out, Nils. It sounds like I'd better improve the documentation and/or the error message with regard to the need to use adaptive mode for all CodeRunner questions.

I'm on leave at present with only limited internet capabilities and using only a cellphone, so I can't give very full answers to questions. I'll be back on line properly next week. In the meantime though here are some tips for writing JavaScript questions.

  • Assuming you're using a standard Jobe server as a sandbox (either our default one or a standard github install), JavaScript code is run using nodejs.
  • There is a nodejs question type but it isn't included in the default CodeRunner install. As a Moodle administrator, use Moodle's question import capability to import the MoodleXML questions in the file .../question/type/coderunner/db/uoc_prototypes.xml. Then, when creating a new coderunner question, you should be able to select nodejs as a coderunner question type (where you were selecting php before).
  • The nodejs question type is very similar to the php question type in that it just runs the student code followed by the test code for each testcase (but without the complication of '<? ... >',  of course).

Hope that's enough to get you going.

Richard

Picture of jupa poma
Re: sample for php and javascript question
by jupa poma - Saturday, 23 January 2016, 9:32 AM
 
Hi, 

everything works fine with the import , my fault , I should have read all the documentation 

thanks

Picture of Sayee Krishnan
Re: sample for php and javascript question
by Sayee Krishnan - Wednesday, 18 January 2017, 7:52 PM
 

By trying the same code i got error.I have enclosed the screen shot of the error


Picture of Richard Lobb
Re: sample for php and javascript question
by Richard Lobb - Wednesday, 18 January 2017, 11:08 PM
 

Hi Sayee

The problem here is with the use of the "<?php" and "?>" tags. The template for the built-in PHP question type is currently

{{ STUDENT_ANSWER }}

define('SEPARATOR', '##');

{% for TEST in TESTCASES %}
{{ TEST.testcode }};
{% if not loop.last %}
echo SEPARATOR;
{% endif %}
{% endfor %}
@17943918#@>

For this to work, the student answer must start with <?php but should not include the closing tag. The test code should not use either opening or closing tags.

I think the template that Jupa was using was probably a simple per-test template (i.e. not a combinator). Something like

{{ STUDENT_ANSWER }}
{{ TEST.testcode }}

The documentation for the PHP question type is wrong, sorry. It applies to the above template not the current version.

Richard

Picture of Sayee Krishnan
Re: sample for php and javascript question
by Sayee Krishnan - Wednesday, 18 January 2017, 11:20 PM
 

Sorry I can't understand please give me a clear explanation.Can you update with sample PHP programs with suitable test cases.

Thank you.

Picture of Richard Lobb
Re: sample for php and javascript question
by Richard Lobb - Friday, 20 January 2017, 8:13 AM
 

Sorry, it appears there's a bug in the PHP question type template. I'm on holiday at present and will push a fix next week. But if you want to fix your own local copy, change the template line

echo SEPARATOR;

to

echo SEPARATOR . "\n";

With that change in place, the following question should work fine:

Question: Write a function double($n) that returns twice the value of its parameter $n. Your answer should begin with "<?php" but should NOT include the closing "?>" tag.

Sample answer: 

<?php
function double($n) {
    return 2 * $n;
}

Test case 1:

echo double(-11);

Expected 1:

-22

Test case 2:

echo double(17);

Expected 2:

34
Picture of Sayee Krishnan
Re: sample for php and javascript question
by Sayee Krishnan - Wednesday, 25 January 2017, 7:34 PM
 
I have enclosed the screenshot of the error. Do check and reply as soon as possible. What next i can do to rectify this problem.

Picture of Richard Lobb
Re: sample for php and javascript question
by Richard Lobb - Wednesday, 25 January 2017, 9:19 PM
 

This looks like a broken template. With the fix to the built-in PHP question type above, my output from the double question looks like the following:

A correctly working double question in php


Could you click the Customise checkbox and copy and paste the template into your reply, please.

I'm also puzzled that your browser appears to be trying to process the '<' in the separator string as if it were an opening tag, which shouldn't happen because the '<' character should have been converted to an HTML entity ('&lt;'). Is it possible you're using either a very old version of CodeRunner or a very old Moodle system?

Richard

Picture of Sayee Krishnan
Re: sample for php and javascript question
by Sayee Krishnan - Wednesday, 25 January 2017, 10:21 PM
 
This was my template code.If this is not a correct template means update me with the right code.

{{ STUDENT_ANSWER }}

define('SEPARATOR', '##');

{% for TEST in TESTCASES %}
{{ TEST.testcode }};
{% if not loop.last %}
echo SEPARATOR . "\n";
{% endif %}
{% endfor %}
@17943918#@>
Picture of Richard Lobb
Re: sample for php and javascript question
by Richard Lobb - Friday, 27 January 2017, 11:47 AM
 

That's a seriously broken template. And the template for your Java program in another thread is also seriously broken. It looks like someone has edited the built-in prototype questions (BUILT_IN_PROTOTYPE_java_program etc). That's not a good idea. Even if you know exactly what you're doing it's much better to create new prototypes than edit the built-in ones, because every upgrade replaces the built-in ones and you'll lose any changes you've made. If you don't know exactly what you're doing it's generally catastrophic.

I suggest you pull the latest version of CodeRunner (V3.1.2 - pushed yesterday) and go through the usual version upgrade process. That will replace all the built-in prototypes so you should be able to write questions using them. Please don't edit or copy those built in prototypes. If you need a different template you can just create a new question of one of the existing types, then customise it as necessary.

Richard

Picture of Sayee Krishnan
Re: sample for php and javascript question
by Sayee Krishnan - Sunday, 29 January 2017, 10:20 AM
 

Sorry I cant able to update you with my coderunner version, i didn't install in our department.Is any other possibility to rectify the problem.

This discussion is being continued by email.

(Edited by Richard Lobb - original submission Sunday, 29 January 2017, 12:21 AM)