Pogramming (source code) question types

Pogramming (source code) question types

by Süreç Özcan -
Number of replies: 54
I am using Moodle 1.8.2+

I Created a new discussion topic by copying three topics from: Syntax Highlighting filter-discussion

by Süreç Özcan - Tuesday, 12 February 2008, 03:08 PM:
What I want to do is expanding Moodle by allowing to create a question type called "source code" adding java code instead of a normal description with syntax highlighting. Other features of my new question type shall be:

1. Several editors: Each editor should include one java-class. If more classes shall be added to the "source code"-question type then this should be enabled through an "add new class"-button which displays a new editor.
(Note: using the html-editor needs some time for uploading - therefore it might not be smart using it?)

2. Source code editor arrangement: There shall be a select-option to display one or two source codes per row. This way longer source codes get a readable display on the monitor. (so far I had used the table option in the html-editor which didn't work all the times - I don't know why.)
(Note: I don't know if this is possible to realise in Moodle at all though.)

3. Next to it I would like to enable for each displayed source-code-editor uploading a .java-file from e.g. the users desktop into the questions (html-?)editor, downloading the current code into a .java-file on e.g. the users desktop and deleting the editor in case it won't be used anymore.

4. Requirement: It would be nice to be able to fill up an editor by whether:
* uploading a .java-file or
* through own input while creating this question type or
* through a copy and paste from another file into the editor.

So what interests me is if someone could tell if I should use the HTML-editor or if I should use another syntax-highlighting supporting editor-plugin which is not as complicate as the html-editor? I have no clue of how complicate it would be to enable such a feature in moodle. Therefore my first idea was to use the html-editor instead (only if I first get the syntax-highlighting set though wink.

by Paul Preney - Tuesday, 12 February 2008, 04:24 PM:
I like your idea of a specific editor configured for programming code. That might be the easiest way to make code work. However, it would require some modules to allow "multiple" information types --like the question module. So, for general use across modules more thought is required. Your idea ought to work very well for a programming question type, however.

Currently, Moodle supports only one editor AFAIK, however, from looking at code in v1.9 it looks like it is being expanded to support more than one. The last I checked (early Jan.) the documentation on this new ability is very poor. Hopefully, it has improved, but, I don't know how flexible it is. Ideally, I'd like to see what you suggest (i.e., a programming-specific editor) and the ability for the HTML-type editor to support inline code with it easy to specify which one to use.

by Süreç Özcan - Wednesday, 13 February 2008, 02:54 PM: (only a part of the post which fits here better)
So do I get you right that you suggest me using another editor and not the HTML-editor for my purposes?

This is how it looks like right now just to get an idea - the functionality is not implemented yet (the question text-part will be deleted and instead the source code part will be usable):
Attachment sourceCodeIF.JPG
Average of ratings: -
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -
by Süreç Özcan - Wednesday, 13 February 2008, 02:54 PM:

Actually the idea goes further.
I would like to separate the given source code (included by the "source code" question type) from the related questions. Therefore after I got this new question type "source code" working I would like to expand some question types (probably Multiple Choice, True/False, Short Answer and two others) by adding an extra part into the creating form above the "Question text"-part. For this I would like to include a button group and below it the Source-Code (not directly editable but through the buttons) from an created "source code"-question type.

In my conceptual design I have planned the following for the 3 buttons:
  • new: shall open a new window with the empty "source code" question type user interface in order to creating a new one, saving and displaying it in the question.
  • reuse: (maybe a select instead of button) shall list all so far created "source code"-question titles from which to select and display one in the current question-type.
  • change: opens the proper "source code" question type that had been displayed already in order to change and save and redisplay.

This way a teacher can see the source code in connection/combination to the currently creating new question(-type). Note that the source code won't be redisplayed in each question - it is more a help while creating a new question to the corresponding source code.

Later in a quiz there can be set "page breaks" whenever there starts a new "source code" question type with its corresponding questions. Do you get my point?

You mention some problems about multiple information types for general use across modules. Would I run directly into them with my bigger plan or do they stay out of reach since I am moving only inside of the question-folder?
We have 1.8.2+ installed at our department. That is why I am developing locally at the same version at the moment. Developing in v1.9 wouldn't make any sense then - what do you say?
In general - is this possible to implement in Moodle?
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
What you are proposing is almost certainly possible, but it sounds to me like it might be unnecessarily complicated.

The following might be a simpler way to achieve most of what you want:

1. Install the Code Syntax Highlighting filter: http://docs.moodle.org/en/Code_Syntax_Highlighting. This supports syntax highlighting of Java (and other languages) code. That lets you have one or more fragments of nicely syntax highlighted Java code in ordinary descriptions, as well as everywhere else in Moodle.

2. Change the quiz module slightly, so it is possible to have more than one copy of a description item in a quiz. This is the easiest way to reuse the same code sample several times in a quiz, just by putting the description item just before each question item it belongs to.


How well this meets your requirements:

1. and 2. Multiple editors and source code arrangement are not really necessary if you are embedding the bits of java code inside a larger bit of content authored using the HTML editor. You can do the layout in HTML, and have as many bits of code as you like inside the one editor.

3. and 4. I don't see much point in being able to upload .java files. If I was making questions like this, I would write the Java code in Eclipse (or whatever you favouring IDE is) - so the easiest way to get it into Moodle would be copy-and-paste. Similarly, copy and paste should be sufficient for getting code out.

Of course, you would have to manually add page-breaks before each description, so the description appears on a page with the correct following questions. If you wanted, you could change the repaginate option on the quiz to add a new option: 'page break before each description', but that is a refinement.


This is just a suggestion. I am sure you have thought about this much more than me, so I may be overlooking something.
In reply to Tim Hunt

Re: Pogramming (source code) question types

by Süreç Özcan -
Thanks a lot for your opinion!

So as far as I understood you would suggest me to reuse the HTML-Editor for coding. In order to be able to create easily and fast source codes maybe I would like to preset a table in the HTML-Editor and also set a smaller font-size. Do you know spontaneously if this would be possible? (Or maybe I could add two buttons: one to select between number of columns and the other to add more predefined tables into the HTML-Editor.) (I am still trying to get geshi working at the moment though: geshi-problems

Your second suggestion sounds interesting. I will have a thought about that.

To your second comment to my 3. and 4. requirements: So far I couldn't get syntax highlighting working (and the tabbing I have just configured yet). Therefor copying a program code so far was not really nice formatted. Maybe with the filter working hopefully soon I will think about it. But anyways I am interested if it would be possible to program download and upload buttons for the HTML-Editor - what do you say?

My second post deals with including the created source codes into a current question in order to write easier questions. Do you consider this as possible to implement using the HTML-Editor in Moodle as well?


In reply to Süreç Özcan

drop down menu to list questions of a special type

by Süreç Özcan -
Does anybody know more about how to make a drop-down menu that lists up all questions to a special question type?

So far I am using a copy of the category-drop-down-menu:
$mform->addElement('questioncategory', 'category', get_string('category', 'quiz'), null, array('courseid' => $COURSE->id, 'published' => true, 'only_editable' => true));

and my source code looks therefor like:
$mform->addElement('questionsourcecode', 'sourcecode', get_string('sourcecode', 'qtype_so_multichoice'), null, array('courseid' => $COURSE->id, 'published' => true, 'only_editable' => true));

I have found the definition for "questioncategory" in lib/formslib.php on the very bottom and found the corresponding file below lib/form/questioncategory.php. At the moment I have made a copy of that file and renamed it to questionsourceode.php. As a result I can see a drop-down menu listing all the categories.
At that point I got stucked since I don't know how to change the questionsourcecode.php to adapt to my problem.
And so far I couldn't find the place where the actual happening is coded when choosing one of the list.

Anyone any ideas?
Süreç
In reply to Süreç Özcan

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: drop down menu to list questions of a special type

by Süreç Özcan -
Hi Pierre,
oh, I didn't know. I thought this is the only place to do so.
As far as I understood for "minor development" I need to create a new issue in the tracker. May I point from the new entry to this discussion link?

Süreç
In reply to Süreç Özcan

Re: drop down menu to list questions of a special type

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think you probably just want the generic 'select' formslib field type. Look, for example, at the 'single', 'answernumbering' and 'fraction' fields in edit_multichoice_form.php.
In reply to Tim Hunt

Re: drop down menu to list questions of a special type

by Süreç Özcan -
yeah I guess so. I have tried the whole day to try it out. All I could manage is to print out a drop-down-menu with one selection of the current questions name:

$scquestionlist = array($question->name);
$mform->addElement('select', 'sourcecode1', get_string('sourcecode', 'qtype_so_multichoice'), $scquestionlist);

How can I get a list of all questions names (in order to display later only a list of question names of one specific question type)?

I have figured out that the following returns an array listing all the available question types:
echo "<div>QTYPES <pre>"; print_r($QTYPES); echo "</pre></div>";
and with the following line I can get the value of e.g. the key 'so_sourcecode'
echo "<div>so_sourcecode <pre>"; print_r($QTYPES['so_sourcecode']); echo "</pre></div>";
But I don't get any further. mixed
In reply to Süreç Özcan

Re: drop down menu to list questions of a special type

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
You probably need to get it from the database.

Something like

$scquestionlist = get_records_select_menu('questions', "category = $categoryid AND qtype = $qtype", 'name', 'id,name');

where $categoryid is a question category id, and $qtype is the question type name, like 'multichoice'. Sorry, I'm in a hurry now, so that I only have time to give you this hint.
In reply to Tim Hunt

Re: drop down menu to list questions of a special type

by Süreç Özcan -
Hi Tim,
thank you veeeeery much!!! I can see now a list of questions of a special question type.

Although I have not used the $categoryid since whenever I have used it the menu was back empty using one of those lines below and other trials. The second line seems to me the proper one, but still I get an empty menu list.
global $categoryid; // ->0
$categoryid = $SESSION->modform->category; //-> 30
Is it necessary to select the questions category id though?
In reply to Süreç Özcan

Re: drop down menu to list questions of a special type

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, do you really want all possible questions of a particular type from the question bank. That seems very unlikely to me.

You probably only want the questions from a particular question category, or perhaps a list of categories. I'm not sure. What exactly are you trying to achieve?
In reply to Tim Hunt

Re: drop down menu to list questions of a special type

by Süreç Özcan -
I see. It would be good to use the questions of the question category the current question belongs to. Do you know why my trials didn't work?
Thanks for your cooperation...
Süreç
In reply to Süreç Özcan

Re: drop down menu to list questions of a special type

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
In the definition() method, $question->category should contain the category id.
In reply to Tim Hunt

Re: drop down menu to list questions of a special type

by Süreç Özcan -
oh, I understand now! And it works - great - thanks!
In reply to Süreç Özcan

Re: drop down menu functionality

by Süreç Özcan -
I want to display the questiontext of a selected question (from the drop-down-menu) in a question-editing-form.

So far I am using the print_question-function I have found to be used in preview.php:
$print_question = print_question($questions[$id], $curstate, $number, $quiz, $options);

I have compared the outputs of $quiz with $myquiz and $questions with $question which seem to be alright and set $number=1. I am missing the variables for $curstate and $options though. Does anybody know how to reach them?

$myquiz = $SESSION->modform;
//global $curstate;
global $state; //todo
global $options; //todo
$options = new stdClass;//todo
$number = 1;
echo "<div>quiz <pre>"; print_r($myquiz); echo "</pre></div>";
echo "<div>question <pre>"; print_r($question); echo "</pre></div>";
echo "<div>curstate <pre>"; print_r($state); echo "</pre></div>";
echo "<div>options <pre>"; print_r($options); echo "</pre></div>";
echo "<div>number <pre>"; print_r($number); echo "</pre></div>";

$print_question = print_question($question['427'], $curstate, $number, $myquiz, $options);

Next it would be nice to be able not to give a special question id like I did (e.g. '427') but to refer to the chosen question from the drop-down-menu. Any ideas how to do that would help me bunches!

In reply to Süreç Özcan

Re: drop down menu functionality

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
How are you going to change what is displayed when the user changes the item selected in the dropdown?

Do you really want the full question displayed, or just the question text? The full question will contain form controls, which will probably mess up the editing form. If you just want the questiontext, then it is easier to do
 $formatoptions = new stdClass;
 $formatoptions->noclean = true;
 $formatoptions->para = false;
 echo '<div class="qtext">', format_text($question->questiontext,
 $question->questiontextformat, $formatoptions), '</div>';

In reply to Tim Hunt

Re: drop down menu functionality

by Süreç Özcan -
To the first question:
I don't know yet. I move step by step to achieve what I want to have in the end. It should be possible to implement what I want, don't you think so?
So far I had thought to do it over the question->id which is the key I get through the function get_records_select_menu e.g.:
Array
(
 [427] => a source_code
 [423] => source code test
 [424] => source code test 2
)
But exactly this is what I want to try next - I need to understand where to put this functionality first, but I couldn't find any simple example yet. Doing such stuff outside of moodle seems to be quiet simple but inside of this complex I am really having a hard time. I guess this is done very different inside of moodle?

To the second question:
Oh I thought the print_question-function would give me only the question text and not the full question. What you have written seems to do more of what I want.

The main problem so far is that I get only the questiontext of the currently created question. I want to use the questiontext for a given question id (selected through the drop-down menu finally).

At the same time I have tried to get the questiontext into the general-form part (see the pic below). There you can find the drop-down-menu with a selected source-code-question type and two trials to get the questiontext of (currently the current) questiontext displayed.
'Correct answers' or 'General feedback' should be replaced later by the chosen question->name (e.g. here 'a source_code').
For the part 'Correct answers' I have used a static element, which shows the source code with syntax-highlighting and tabbing, but I can't change the columns in order to fit it to be read better (especially if source codes get a little longer). In the second version below 'General feedback' I can change the columns-number, but therefor I don't have syntax-highlighting and I don't want the questiontext editible at that point.

Attachment display_sc_inmc.JPG
In reply to Süreç Özcan

How to redisplay selected item of drop down menu

by Süreç Özcan -
How to get an already selected value from a drop-down-menu get displayed again when re-editing a question?

Thoughts:
1. Is this only possible through saving the selected value in the database for the question (e.g. like it is in multichoice-question for the 'single'-element which choses between "one or multiple answers")?
2. Could I use a 'hidden'-element instead? If yes, how?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The only way Moodle can remember a value that the user entered in the past is by storing it in the database somewhere.
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
Aha, thank you very much!

So I have created a file including
ALTER TABLE `prefix_question_soregexp` ADD `sourcecode` TINYINT( 2 ) NULL DEFAULT '0';

I call this file in mysql.php-file by adding following:
if ($oldversion < 2008071700) {
$result = modify_database("$CFG->dirroot/question/type/soregexp/db/mysql03.sql");
return $result;
}


I have added necessary changes in questiontype.php.
Then I went to the Admin->Notifications expecting an upgrade to be seen.
If I did everything correct, should I see a message in Notifications?
Am I missing to do something?

I still can't see the selected "sourcecode" when reediting the editing form.
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Did you update version.php for your question type to trigger the upgrade?
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
good point. Now it upgraded successfully.
But the firstly selected sourcecode is not selected again in the drop-down menu when re-editing the quiz. Do you have an idea why this could happen?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
These days, the upgrade code should in in upgrade.php, and the code should use the database-independant XMLDB classes. Normally the easiest way to get the code is the XMLDB editor build into Moodle, I think it is under Admin -> Experimental. It can generate the code to add or remove a particular column based on the install.xml file, then you copy and paste it into upgrade.php
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
alright - now I can see the four fields in the database.

I deleted my earlier upgrade-trials and created an upgrade.php-file as you have suggested above with the following content:
********************************** upgrade file content
function xmldb_qtype_soregexp_upgrade($oldversion=0) {
global $CFG, $THEME, $db;
$result = true;

if ($result && $oldversion < 2008072100) {
/// Define field sourcecode to be added to question_soregexp
$table = new XMLDBTable('question_soregexp');

//select the source code you want to display
$field1 = new XMLDBField('sourcecode');
$field1->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'usehint');

//the displayed question text of the sourcecode-question type
$field2 = new XMLDBField('noneditablequestiontext');
$field2->setAttributes(XMLDB_TYPE_TEXT, 'big', null, null, null, null, null, null, 'sourcecode');

//Check-box for using text/HTML-editor for questiontext.
//Unchecked it uses text-editor, checked it uses HTML-editor.
$field3 = new XMLDBField('useeditor_questiontext');
$field3->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'noneditablequestiontext');

//Check box for Text/HTML-editor usage for general feedback.
//Checked it uses HTML-editor, unchecked it uses text-editor
$field4 = new XMLDBField('useeditor_genfeedb');
$field4->setAttributes(XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'useeditor_questiontext');

/// Launch add field sourcecode
$result = $result && add_field($table, $field1) && add_field($table, $field2)
&& add_field($table, $field3) && add_field($table, $field4);
}
return $result;
}

**********************************


Now trying to add a new question of this question type (which is regexp with some changes) after saving it I receive following error message:
********************************** error message
Notice: Undefined property: stdClass::$noneditablequestiontext in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/questiontype.php on line 104

Notice: Undefined property: stdClass::$useeditor_questiontext in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/questiontype.php on line 105
Column 'useeditor_questiontext' cannot be null

INSERT INTO mdl19b_question_soregexp ( QUESTION, ANSWERS, USEHINT, SOURCECODE, NONEDITABLEQUESTIONTEXT, USEEDITOR_QUESTIONTEXT, USEEDITOR_GENFEEDB ) VALUES ( 52, '57', 0, 14, null, null, 1 )
  • line 1532 of lib/dmllib.php: call to debugging()
  • line 107 of question/type/soregexp/questiontype.php: call to insert_record()
  • line 316 of question/type/questiontype.php: call to question_soregexp_qtype->save_question_options()
  • line 183 of question/question.php: call to default_questiontype->save_question()
Could not insert quiz regexp options!
Stack trace:
  • line 5670 of lib/weblib.php: call to debugging()
  • line 319 of question/type/questiontype.php: call to error()
  • line 183 of question/question.php: call to default_questiontype->save_question()
**********************************


In questiontype.php in save_question_options-function simply I add the new fields:
**********************************
if ($options = get_record("question_soregexp", "question", $question->id)) {
$options->answers = implode(",",$answers);
$options->usehint = $question->usehint;
$options->sourcecode = $question->sourcecode; //soregexp
$options->noneditablequestiontext = $question->noneditablequestiontext; //soregexp
$options->useeditor_questiontext = $question->useeditor_questiontext; //soregexp
$options->useeditor_genfeedb = $question->useeditor_genfeedb; //soregexp

if (!update_record("question_soregexp", $options)) {
$result->error = "Could not update quiz regexp options! (id=$options->id)";
return $result;
}
} else {
unset($options);
$options->question = $question->id;
$options->answers = implode(",",$answers);
$options->usehint = $question->usehint;
$options->sourcecode = $question->sourcecode; //soregexp
$options->noneditablequestiontext = $question->noneditablequestiontext; //soregexp line 104
$options->useeditor_questiontext = $question->useeditor_questiontext; //soregexp
$options->useeditor_genfeedb = $question->useeditor_genfeedb; //soregexp
if (!insert_record("question_soregexp", $options)) {
$result->error = "Could not insert quiz regexp options!";
return $result;
}
}

**********************************

'sourcecode' has been adapted like the three other new fields (noneditablequestiontext, useeditor_questiontext, useeditor_genfeedb). I don't understand why the error does not begin at the sourcecode-field.
Even though I don't get an error for 'sourcecode' I can't see it with the new value when re-editing the question.
Changing the data directly for this field doesn't do any differences as well. But after creating the question I can see the correct value in the database.
Do you have any idea what is going on wrong?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
$question->... is coming from the editing form. What are the form fields called in the editing form?
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
exactly the same. I copied the field names from the editing form:

************************* snippets of edit_soregexp_form.php
$mform->insertElementBefore($mform->createElement('select', 'sourcecode', get_string('sourcecode', 'qtype_soregexp'),
$scquestionlist, $attributes), 'questiontext');

$mform->insertElementBefore($mform->createElement('static', 'noneditablequestiontext',
get_string('selectedsourcecode', 'qtype_soregexp'),
$myformatted_text), 'questiontext');

$mform->insertElementBefore($mform->createElement('checkbox', 'useeditor_questiontext',
get_string('useeditor', 'qtype_soregexp'),'', $editor_attributes1), 'questiontextformat');

$mform->insertElementBefore($mform->createElement('checkbox', 'useeditor_genfeedb', get_string('useeditor', 'qtype_soregexp'),
'', $editor_attributes2), 'answersinstruct');

*************************
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Drat! I was sure that is what the problem was. I can't think what is going wrong. You will probably have to print_object($question) to see what is going on. If you want to post the source code of the question type, I can probably take a look tomorrow.
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
It would be great if you could take a look at my attachment!
Could it be a cache-issue problem?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
I think the problem is that you are doing
 $sc_to_display = get_record_select('question', "category=$categoryid AND qtype='sosourcecode' AND id='$scid'",//'519' $scid
 'questiontext');
in definition_inner. The form definition should not depend on the question being edited. Instead, set the value of this field in set_data, which gets passed a $question object with the values to use.

To make that work, you may have to change from using a static field type to a read-only htmleditor field type - acutally, no, a static field type is probably right for what this data is, and it should work to set its value in set_data.


In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
hmmm, I have tried following:
__________________________
function set_data($question) {
if (isset($question->options)){
$answers = $question->options->answers;
if (count($answers)) {
$key = 0;
foreach ($answers as $answer){
$default_values['answer['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;
$default_values['feedback['.$key.']'] = $answer->feedback;
$key++;
}
}
$default_values['image'] = "None";
$default_values['usehint'] = $question->options->usehint;
$default_values['sourcecode'] = $question->options->sourcecode;
$default_values['noneditablequestiontext'] = $question->options->noneditablequestiontext;
$default_values['useeditor_questiontext'] = $question->options->useeditor_questiontext;
$default_values['useeditor_genfeedb'] = $question->options->useeditor_genfeedb;
$question = (object)((array)$question + $default_values);
} else {
$key = 0;
$default_values['fraction['.$key.']'] = 1;
$question = (object)((array)$question + $default_values);
}
parent::set_data($question);
}
__________________________

But it still can't save the question and gives the same error-message. I have also tried to set only the 'sourcecode' without the other three bold lines.
By the way - I have the same problem with the check boxes for selecting whether to use the text/HTML-editor. The selection doesn't get redisplayed when re-editing. Same with 'noneditablequestiontext' which displays the selected drop-down-questiontext-value.

I have realized that in Moodle 1.9 there are two different pages used for creating (adding) a question and re-editing (changing) a question. Trying to e.g. select another Source Code from the drop-down-menu closed the question-editing-form and returned to the editing-quiz-view. Same happened when clicking on a check-box to change the editors. With the values set, this seems to be working now.


In the editing-quiz-view I can see my created question (which returned the above mentioned error-message) - but I don't see it in the database.
Re-editing the question I receive following messages:
__________________________
Error: Missing question options!

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 97

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 171

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 182

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 183

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 184

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 185

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line
186
__________________________
I can see the complete form, the 'questiontext' gets displayed, the 'answer' and 'sourcecode' not.
Maybe this helps to understand the problem?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Sorry, I had a bit of a look, and I can't work out what is going on. Formslib can be a struggle sometimes. Perhaps it is worth posting again in the General Developer Forum, just about the forms issue. Sorry I can't be more helpful.
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
Alright, I have posted an entry here.
I thank you for your help, Tim!
Süreç
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
Hi Tim,
I assume the upgrade of the database didn't work properly.
In the database-structure I see that both used check-boxes have a default value 0. While saving a new question it seems to be working properly when I check both check-boxes so that it can write 1 in the database.
So for some reason it doesn't take the default values and as a result it says e.g. 'useeditor_questiontext' can't be null.
Do you have any idea?

I have also tried out to delete and reinstall the question-type. I needed to type in the old version first and then change to the new back again in version.php in order to install it properly. I thought it would work automatically...
Trying it out again shows the same problems. I can safe a question only if I change the editors once.

Would you suggest trying it out without an upgrade.php-file but installing all the tables at once (so I would need to add my new fields to mysql.php)?

Do you have the same problem or does it work on your installation?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
Hi Tim,
in e.g. multichoice-question-type in update.php you have set_field twice mentioning MDL-7925. I took a look at MDL-7925 but there is not really much content typed. Could this issue have to do anything with my problem possibly?
Should I write my updates also twice?
In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
For the installation, you should really have an install.xml file, instead of separate mysql.sql and postgres7.sql files. The XMLDB editor will help you write one in the right format.

I don't understand why the upgrade is not working properly. Do you have a way of accessing the database directly? If so, can do do a DESCRIBE TABLE mdl_question_soregexp, or whatever the command is, to find out what definition the database is currently using for that table?
In reply to Tim Hunt

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
For the installation: I have just used it the way it was in regexp and added my new fields in update.php - but I can try summing up all in an install.xml - this way I would not even need an update.php.

For the upgrade: I have tried following sql-command in the XMLDB-editor, but it just returns the table like in the Structure-tab:
DESCRIBE `mdl19b_question_soregexp`
I guess you are talking about another access for example by using command line? I don't know how to work with it though and I don't know if I have access to it.

Did you try to install it? If yes, does it work with your installation?

In reply to Süreç Özcan

Re: How to redisplay selected item of drop down menu

by Süreç Özcan -
I first wanted to try out if this upgrade issue has to do something specifically with (so)regexp question type.
Therefor I have changed my somultichoice-question type (which is a copy of multichoice, having the same new fields as soregexp has). So far I had used the same database table the multichoice-question uses. I have changed it the way putting my new fields directly into install.xml.
The same problem as for soregexp seemed to occur. Then I have changed the last three fields having 'null' instead of 'not null'. This seems to be working although I don't understand why it doesn't work when set to 'not null'. As long as text-editor is selected there is 'NULL' in the database for that field. When I select the HTML-editor it writes 1. Do you think this should make any problems I can't think of yet?
In reply to Tim Hunt

Re: drop down menu functionality

by Süreç Özcan -
to the first question:
Maybe I can do sth. with the getSelected()-function (e.g. being used in edit_calculated_form.php)?
In reply to Süreç Özcan

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: Pogramming (source code) question types

by Süreç Özcan -
What a coincidence, yesterday I have seen in the edit_calculated_form.php how to insert elements before others. wink
You may have a look to my post from 'Tuesday, 11 March 2008' - I have tried it out with 'static' and 'textarea' elements. Maybe you can help me out there? mixed
And do we have to use createElement instead of addElement though? I didn't understand the difference yet.
And another thing I didn't understand yet is when I have to use 'new stdClass'. I couldn't find the place of where stdClass is defined. Is it only to create a new object? Can there be different objects created than stdClass?

"You could even put you source code as a ressource on Moodle using a format like pdf that allows you to build an index."
Why should I want to do this? Is the contrib mechanism coupled to this or are you talking about my development plans in general? I don't mind offering my specific question type developments to other moodlers. I am using others' codes and help as well. wink

thanks for all your help,
Süreç

In reply to Süreç Özcan

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: Pogramming (source code) question types

by Süreç Özcan -
Thanks for your advices!
I am working using Eclipse, so there is a very good search engine.

I really do get your last point, but I am writing my final thesis and there is no way around implementing what I have made a conceptual design of.
In reply to Süreç Özcan

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Süreç Özcan

Pogramming (source code) question types

by Süreç Özcan -

Hello Moodlers,

I want to share three modified question-types I have been working on lately with you. Basically it is about giving some comfort while creating quizzes for computer science classes bonded to an useful architecture suggestion.

You are welcome to give some feedback about them! smile

I have tried to find all the bugs - but if you should find any, let me know please!

In order to get some idea in the following posts I will copy the question types' general help file contents with some screen-shots (notice: the examples are in German language - but I thought better a screen-shot than none at all).

To this post I have attached the modified question-types.

In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -

Source Code - question type:

This type of question is not really a question. It is very similar to the "Description"-question type. All it does is print some source code without requiring any answers. The difference of this question type compared to the "Description"-question type is described as follows:

With it's preset values it can be used to display easily a nicely syntax-highlighted source code to be used by a following group of questions for computer science classes. You may reference from those single questions to special code lines of code fragments of the displayed "Source Code" to ask questions. You may display a "Source Code" with its corresponding questions on one page and make a page break before the next group while organizing the questions for a quiz.

If you have more code fragments displaying them one below the other can make the source code hard to read for the student because he/she will need to scroll a lot up and down. Here you would rather display codes next to each other. You may chose a table with two columns in a row to enter the source code for this issue.

You can change the code-tag-attribute "syntax" in order to get syntax- highlighting for other languages than Java (e. g. "c", "cpp", "mysql", "php", "matlab", "vb", "xml", "delphi",...). Per default this is set to Java. Your Moodle-Admin may change this setting to another default value if needed.

You may enter the source code directly into the proper HTML-Editor or you may copy a source code from another editor (e.g. Eclipse, Ultra Edit, Text Editor) and paste it into the proper table to get the wanted layout.

Initially there are implemented somultichoice and soregexp which are copies of the original Multiple-Choice and Regular expression short answer question types. The main difference is the option to search and display a Source Code question types' source code. This way you can create a new question type by getting a glimpse to the corresponding source code. You can also use sojunit-question-type for this concept.

Please notice: As soon as you allow mixing questions this "Source Code" will be mixed as well.

Attachment so_sc_edit.JPG
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -

SourceCode RegExp Short Answer Questions:

This question type is like the RegExp Short Answer question with following differences:

In computer science classes the usage of source codes is essential. Corresponding to a given Source Code question type which simply displays a nicely syntax-highlighted source code you can create a following group of questions.

You may display a Source Code with its corresponding questions on one page and make a page break before the next group while organising the questions for a quiz.

You may reference from this single question e.g. to special code lines of code fragments of the displayed Source Code to ask questions.

Examples for questions:

  • How many object are created when running the main-method?
  • How many blocks does the method/class X contain?
  • What output do you expect in line X?
  • There is hidden a bug in the source code (e.g. in the comment). In which line is it?
  • Which output-sequence do you expect (in e.g. sorting algortithms or for-loops)? Type your result like this "1,5,8,16", please.
  • In lines X till Y how often is the for-loop called?

In addition to the RegExp Short Answer question type you can type in general feedback. You can also switch between text/HTML-editor for the question text and general feedback.

In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -

SourceCode MultipleChoice:

This question type is like the Multiple Choice question with following differences:

In computer science classes the usage of source codes is essential. Corresponding to a given Source Code question type which simply displays a nicely syntax-highlighted source code you can create a following group of questions.

You may display a Source Code with its corresponding questions on one page and make a page break before the next group while organising the questions for a quiz.

You may reference from this single question e.g. to special code lines of code fragments of the displayed Source Code to ask questions.

Examples for questions:

  • How many object are created when running the main-method?
  • How many blocks does the method/class X contain?
  • What output do you expect in line X?
  • There is hidden a bug in the source code (e.g. in the comment). In which line is it?
  • Which output-sequence do you expect (in e.g. sorting algortithms or for-loops e.g. "1,5,8,16")?
  • In lines X till Y how often is the for-loop called?
  • How would you change "code-reference" in order to acchieve X?

In addition to the Multiple Choice question type the HTML-editors for the "Choice"s and "Overall Feedback" has been changed to textarea-editors in order to get a better overview while creating or editing this type of question. If you really need to you can use HTML for text formatting in the text-editor.

You can also switch between Text/HTML-editor for the question text and general feedback.

In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -
These are the final versions after some tests and debugging.
Enjoy! smile
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -
In the following next three posts (including this one) I want to add some architectural information about the source code question type that may be needed to understand the plugin better.
I want to start with the database table diagram. For this question type the only table I have added is question_sosourcecode (in the plugin download itself you can read what each field is for).
Attachment db_fragetyp_sc.jpg
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -
I have been using the plugin template prepared from Tim Hunt. This is how I have extended it (you may compare with the general template structure here):
Attachment Template_structure_sc.jpg
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Heng Chun Keat -
Dear Süreç Özcan,

I have seen ur post, which is about question type. currently i m doing to my thesis, and intending to modify the question type by adding HINT to each question (in short, if the student takes the hint, the marks will be less). So, i need to understand:
1. how the question types work.
2. how is the structure looks like.
3. where can i add my hint in the source code.
4. what is the use of display.html ? as it takes no effect on the interface (on the question editing page), even if i have deleted the file content.

so, if it is possible, i hope i can receive the all the diagrams that u created, which explain the structure of the question type. my email : heng_ck@yahoo.com.sg

thank you.

Regards,
simon
In reply to Süreç Özcan

Re: Pogramming (source code) question types

by Süreç Özcan -
and finally the question type architecture part especially for this question type. You may compare with the general question type architecture diagram at the link at the post above.
Attachment uml_klassendiagramm_sc.jpg