[moodle] / contrib / plugins / question / type / sojunit / edit_sojunit_form.php Repository:

View of /contrib/plugins/question/type/sojunit/edit_sojunit_form.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (download) (annotate)
Wed Feb 4 05:10:02 2009 WST (9 months, 2 weeks ago) by suerec
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +6 -6 lines
tested final version with screenshots in example_files folder
<?php
/**
 * The editing form code for this question type.
 *
 * @copyright &copy; 2008 Süreç Özcan
 * @author suerec@darkjade.net
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package so_questiontypes
 */

require_once($CFG->dirroot.'/question/type/edit_question_form.php');

/**
 * SourceCode JUnit editing form definition.
 * 
 * See http://docs.moodle.org/en/Development:lib/formslib.php for information
 * about the Moodle forms library, which is based on the HTML Quickform PEAR library.
 */
class question_edit_sojunit_form extends question_edit_form {
    var $_textarea_or_htmleditor_generalfb;   //addElement type for general feedback
    var $_editor_options_generalfb;           //in dependence of editor type set a different array for its options
    
    function definition_inner(&$mform) {
        global $COURSE, $CFG, $question;
        $mform->setDefault('penalty', 0); //set to 0 by default in order to use it in adaptive mode and
                                          //many attempts are possible without subtracting penalty
        $mform->removeElement('generalfeedback');   //change this to allow html/text-editor selection
        
        
        //----------------------- make checks for reloadings of editor
        // check which editor to display (text/html-editor)
        //needed for opening editing page and using db-value, later use the reloaded value
        $loaded_initialy = optional_param('reloaded_initialy',1,PARAM_INT);
                    
        if (isset($question->id) and $loaded_initialy){		//do this only if re-editing question and once
            $questionid = $question->id;
            $useeditor_db = get_record_select('question_sojunit', "questionid=$questionid",
                                              'useeditor_genfeedb');
            if ($useeditor_db->useeditor_genfeedb == 0) {
                $useeditor_genfeedb = optional_param('useeditor_genfeedb', $useeditor_db->useeditor_genfeedb);
                $this->_textarea_or_htmleditor_generalfb = 'textarea';        // options for textarea
                $this->_editor_options_generalfb = array('rows' => 3, 'cols' => 57, 'course' => $COURSE->id);
                $loaded_initialy = 0;
            }
            if ($useeditor_db->useeditor_genfeedb == 1){
                $useeditor_genfeedb = optional_param('useeditor_genfeedb', $useeditor_db->useeditor_genfeedb);
                $this->_textarea_or_htmleditor_generalfb = 'htmleditor';      //options for htmleditor
                $this->_editor_options_generalfb = array('rows' => 8, 'course' => $COURSE->id);
                $loaded_initialy = 0;
            }
        }
        else{ // do this when creating a new question and after having reloaded once
            $useeditor_genfeedb = optional_param('useeditor_genfeedb', false);
            if ($useeditor_genfeedb) {
                $this->_textarea_or_htmleditor_generalfb = 'htmleditor';      //options for htmleditor
                $this->_editor_options_generalfb = array('rows' => 8, 'course' => $COURSE->id);
            } else {
                $this->_textarea_or_htmleditor_generalfb = 'textarea';        // options for textarea
                $this->_editor_options_generalfb = array('rows' => 3, 'cols' => 57, 'course' => $COURSE->id);
            }
            $loaded_initialy = 0;
        }
        $mform->addElement('hidden', 'reloaded_initialy', $loaded_initialy);
		
		
		//-------------------------- Source Code: drop-down-menu to select source code to display
        $categoryid = $question->category; //in order to select only the questions of that category
		
        //get records for drop-down menu: 
        //where $categoryid is a question category id, and $qtype is the question type name, like 'multichoice'
        //$table, $select='', $sort='', $fields='*', $limitfrom='', $limitnum=''
        $scquestionlist_without_initial = get_records_select_menu('question', "category = $categoryid AND qtype = 'sosourcecode'", 'name', 'id,name');
				
		if (empty($scquestionlist_without_initial)){ //if there is no source code created yet, display an alternative text to select and save in the database
			$scquestionlist = array('0' => 'No source code created yet!');
		} else {					// otherwise display always initially another alternative text - if not changed - so that no source code gets displayed if not needed
			$scquestionlist = array( 0 => 'Select a source code if needed!');
			//keep the keys like they are without overwriting them
			foreach($scquestionlist_without_initial as $key => $value){
				$scquestionlist[$key] = $value;
			}
		}
		
		//get the sourcecode-id of the already selected record and display it initially. If there is no record selected yet - because we create a new question, display "No source code created yet!" from above
		$selected_code = get_record_select('question_sojunit', "questionid=$question->id", 'sourcecode');
		$selected_code_array = (array) $selected_code;
		foreach ($selected_code_array as $key=>$value){ 
			$initial_display = $value;
		}
		
        $scid = optional_param('sourcecode', $initial_display);
        $formid = $mform->_attributes['id']; // get form id
        
		//reload sourcecode in order to keep changes made in HTML-editor - if made
		$attributes = array("onChange" => "reload('sourcecode'), document.forms['$formid'].submit();");
        $mform->insertElementBefore($mform->createElement('select', 'sourcecode', get_string('sourcecode', 'qtype_sojunit'), 
                                                            $scquestionlist, $attributes), 'questiontext');
        $mform->setHelpButton('sourcecode', array('sosourcecodehint', get_string('sourcecode','qtype_sojunit'), 
                                                    'qtype_sojunit'));
		$mform->getElement('sourcecode')->setSelected($initial_display); //display initially the first value of drop-down-menu which is no source code

		
        //-------------------------- Selected Source Code: displays statically selected Source Code
		$formatoptions = new stdClass;
        $formatoptions->noclean = true;
        $formatoptions->para = false;
		
        // the value of the selected element is the id to look at in the database
		$in_db = get_record_select('question', "category=$categoryid AND qtype='sosourcecode' AND id='$scid'",
                                        'questiontext');
			//print_object('in_db: '.$in_db->questiontext);	//for debugging
		if ($in_db){	// the source code is an entry in the db
			$in_db = true;
		} else {		// in case of no source code beeing selected there should be no database entry for this id
			$in_db = false;
		}
		
		if (!$in_db){	// in case no source code is selected, display the following text
			$myformatted_text = 'You have not selected a source code yet.';
		} else {		// the source code is an entry in the db
			$sc_to_display = get_record_select('question', "category=$categoryid AND qtype='sosourcecode' AND id='$scid'",
                                        'questiontext');
			$myformatted_text = format_text($sc_to_display->questiontext, FORMAT_MOODLE, $formatoptions);
		}
		
        // prints out a source code as a static field
        // + syntax highlighting; - columns not setable -> questiontext doesn't fit properly
		$mform->insertElementBefore($mform->createElement('static', 'noneditiblequestiontext', 
										get_string('selectedsourcecode', 'qtype_sojunit'), 
										$myformatted_text), 'questiontext');
        
        
        //-------------------------- adding "Given Code" Text-Area                                             
        $mform->addElement('textarea', 'givencode', get_string('givencode', 'qtype_sojunit'),
                                    array('cols'=>80, 'rows'=>20, 'course' => $COURSE->id));
        $mform->setType('givencode', PARAM_RAW);
        $mform->setHelpButton('givencode', array('sogivencodehint', get_string('givencode', 'qtype_sojunit'), 'qtype_sojunit'));
        //$mform->setDefault('givencode', get_string("table_structure","qtype_sojunit")); //don't need this, because it is redundant with the help button info
        
        
        //-------------------------- GENERALFEEDBACK - editor
        $mform->addElement($this->_textarea_or_htmleditor_generalfb, 'generalfeedback', 
                                    get_string('generalfeedback', 'quiz'),$this->_editor_options_generalfb);                                                    
        $mform->setType('generalfeedback', PARAM_RAW);
        $mform->setHelpButton('generalfeedback', array('generalfeedback', get_string('generalfeedback', 'quiz'), 'quiz'));
        //-------------------------- USE HTML EDITOR - checkbox - for generalfeedback-editor
        $editor_attributes = array("onChange" => "reload('generalfeedback')"); //set onChange attribute
        $mform->addElement('checkbox', 'useeditor_genfeedb', get_string('useeditor', 'qtype_sojunit'),
                                    '', $editor_attributes);
        $mform->setHelpButton('useeditor_genfeedb', array('souseeditorhint', get_string('useeditor', 'qtype_sojunit'), 'qtype_sojunit'));
        $mform->addElement('hidden','reloaded', '0'); 	//needed to save the reloaded value in java script
		$mform->registerNoSubmitButton('reloaded');		//needed so that it works when reediting the question and changing the editor - this way it doesn't close the question-edit form
        
        
        //-------------------------- new section: Load test class
        //adding header
        $mform->addElement('header', 'loadedtestclassheader', get_string('loadedtestclassheader', 'qtype_sojunit'));
                        
        // choose or upload the appropriate JUnit-file; copied from mod/lesson/modform.php
        $mform->addElement('choosecoursefile', 'testclassname', get_string('loadneededtestclasses', 'qtype_sojunit'), array('courseid'=>$COURSE->id));
        $mform->setHelpButton('testclassname', array('sotestclassnamehint', get_string('loadneededtestclasses', 'qtype_sojunit'), 'qtype_sojunit'));
        $mform->setDefault('testclassname', get_string('selectTestFile','qtype_sojunit'));

        
        //test if a file has been selected, otherwise reload the page and show a message
        function testFileSelected($field){
            $selectTestFile = get_string('selectTestFile','qtype_sojunit');
            
            $field_testclassname = $field['testclassname'];
            $field_testclassname_value = $field_testclassname['value']; // testclassname is in an array of an array
            
            if (in_array($selectTestFile, $field['testclassname']) || empty($field_testclassname_value)) {
                return array('testclassname' => 'Please select a test class file!');
            }
            return true;
        }
    
        // test if the selected test file ends with .java, otherwise reload the page and show a message
        function testJavaFile($field){
            $field_testclassname = $field['testclassname'];
            $field_testclassname_value = $field_testclassname['value'];
            if (!(substr($field_testclassname_value,-5) == ".java")){
                return array ('testclassname' => 'Please select a test class file which ends with .java!');
            }
            return true;
        }
        
	   /**
	         * test: do I have access to Java?
	         * yes-> no problem
	         * no-> Moodle-Server-Admin needs to set Java in order to use this module
	         * Test first if the paths are set and later if the paths are correct!
	         */
		function testJava(){
			// Are the Java-paths set?
			if((PATH_TO_JAVAC == 'PathNotSet') or (PATH_TO_JAVA == 'PathNotSet')){
				//print_object('path not set'); //debug
				return array ('testclassname' => 'No permission for Java. Please let the Moodle-Admin SET the paths 
						for Java for the used server in order to be able to use this question type 
						(Admin: see RADME.txt of this module)!');
			}
			
			// Are the correct Java-paths set?
			if ((!file_exists(PATH_TO_JAVAC)) or (!file_exists(PATH_TO_JAVA))){
				return array ('testclassname' => 'No permission for Java. Please let the Moodle-Admin RECHECK the paths
						for Java for the used server in order to be able to use this question type 
						(Admin: see RADME.txt of this module)!');
			}
			//else{								//for debugging
			//    return array ('testclassname' => 'Java available');
			//}
			return true;
		}
		
		$mform->addFormRule('testJava');
		$mform->addFormRule('testFileSelected');
		$mform->addFormRule('testJavaFile');
    }

    
    function set_data($question) {
        //these are all the new fields for the junit question type of table mdl_question_sojunit
        if (isset($question->options)){
            $default_values['givencode'] = $question->options->givencode;
            $default_values['testclassname'] = $question->options->testclassname;
			$default_values['useeditor_genfeedb'] = $question->options->useeditor_genfeedb;
			$default_values['sourcecode'] = $question->options->sourcecode;
            $question = (object)((array)$question + $default_values);
        }
        $question->reloaded = 0;
        parent::set_data($question);
    }
    
	
	function validation($data, $files) {
        $errors = parent::validation($data, $files);     
        return $errors;
    }

	
    function qtype() {
        return 'sojunit';
    }
}
?>

Moodle CVS Admin
ViewVC Help
Powered by ViewVC 1.0.7