General developer forum

Writing Quizzes in other programming languages

 
Picture of Charlie Daly
Re: Writing Quizzes in other programming languages
 


OK, I actually hacked the shortanswer plugin and now I can get the userid, courseid and question name. I was hoping to also get the question category.

The question object does have a category field, but at the moment it is just an number, it doesn't have the actual question category. 

Once I have userid, courseid, question category and question name, I can pass that to a url which will return a question specification or an answer as required.

The following is a section of the output when I use print_object with the question object (note that the category value is not the category):

["shownumcorrect"]=>
bool(false)
["id"]=>
string(1) "3"
["category"]=>
string(1) "9"
["contextid"]=>
string(2) "21"

Thanks.
 
Average of ratings: -
Picture of Charlie Daly
Re: Writing Quizzes in other programming languages
 
I discovered the answer and am documenting it here in case someone else needs it. It seems that the category id is stored in the category field. You can get the category using:


        $cat_id = $this->category;

        $query = $DB->get_record_sql('select name from mdl_question_categories where id="' . $cat_id . '"');

        print($query->name);


 
Average of ratings: -
Picture of Marcus Green
Re: Writing Quizzes in other programming languages
Core developersParticularly helpful MoodlersPlugin developersTesters

Thanks for posting Charlie, that should work but you should consider something like

$DB->get_record('question_categories',['id'=>$cat_id)  

Then it will work if someone has a prefix other than mdl.

See 

https://docs.moodle.org/dev/Data_manipulation_API#Main_info

I suspect most people most of the time leave the default mdl_ but  it is nice to cover unusual situations.




 
Average of ratings: -
Picture of Charlie Daly
Re: Writing Quizzes in other programming languages
 

Thanks Marcus,

It's good to see an elegant approach.

 
Average of ratings: Useful (1)