MDL-14431
[moodle.git] / question / import_form.php
1 <?php  //$Id$
3 require_once($CFG->libdir.'/formslib.php');
5 class question_import_form extends moodleform {
7     function definition() {
8         global $COURSE;
9         $mform    =& $this->_form;
11         $defaultcategory   = $this->_customdata['defaultcategory'];
12         $contexts   = $this->_customdata['contexts'];
14 //--------------------------------------------------------------------------------
15         $mform->addElement('header','fileformat', get_string('fileformat','quiz'));
16         $fileformatnames = get_import_export_formats('import');
17         $radioarray = array();
18         foreach ($fileformatnames as $id => $fileformatname) {
19             $radioelement = &MoodleQuickForm::createElement('radio','format','',$fileformatname,$id );
20             $radioelement->setHelpButton(array("format{$id}",$fileformatname,'quiz'));
21             $radioarray[] = $radioelement;
22         }
23         $mform->addGroup($radioarray,'format', '', array('<br />'), false);     
24         $mform->addRule('format', null, 'required', null, 'client' );
25 //--------------------------------------------------------------------------------
26         $mform->addElement('header','general', get_string('general', 'form'));
28         $mform->addElement('questioncategory', 'category', get_string('category','quiz'), compact('contexts'));
29         $mform->setDefault('category', $defaultcategory);
30         $mform->setHelpButton('category', array('importcategory', get_string('importcategory','quiz'), 'quiz'));
32         $categorygroup = array();
33         $categorygroup[] =& $mform->createElement('checkbox', 'catfromfile', '', get_string('getcategoryfromfile', 'question'));
34         $categorygroup[] =& $mform->createElement('checkbox', 'contextfromfile', '', get_string('getcontextfromfile', 'question'));
35         $mform->addGroup($categorygroup, 'categorygroup', '', '', false);
36         $mform->disabledIf('categorygroup', 'catfromfile', 'notchecked');
37         $mform->setDefault('catfromfile', 1);
38         $mform->setDefault('contextfromfile', 1);
41         $matchgrades = array();
42         $matchgrades['error'] = get_string('matchgradeserror','quiz');
43         $matchgrades['nearest'] = get_string('matchgradesnearest','quiz');
44         $mform->addElement('select', 'matchgrades', get_string('matchgrades','quiz'), $matchgrades);
45         $mform->setHelpButton('matchgrades', array('matchgrades', get_string('matchgrades','quiz'), 'quiz'));
46         $mform->setDefault('matchgrades', 'error');
48         $mform->addElement('selectyesno', 'stoponerror', get_string('stoponerror', 'quiz'));
49         $mform->setDefault('stoponerror', 1);
50         $mform->setHelpButton('stoponerror', array('stoponerror', get_string('stoponerror', 'quiz'), 'quiz'));
52 //--------------------------------------------------------------------------------
53         $mform->addElement('header', 'importfileupload', get_string('importfileupload','quiz'));
55         $this->set_upload_manager(new upload_manager('newfile', true, false, $COURSE, false, 0, false, true, false));
56         $mform->addElement('file', 'newfile', get_string('upload'));
57 //--------------------------------------------------------------------------------
58         $mform->addElement('submit', 'submitbutton', get_string('uploadthisfile'));
60 //--------------------------------------------------------------------------------
61         if (has_capability('moodle/course:managefiles', get_context_instance(CONTEXT_COURSE, $COURSE->id))){
62             $mform->addElement('header', 'importfilearea', get_string('importfilearea','quiz'));
64             $mform->addElement('choosecoursefile', 'choosefile', get_string('choosefile','quiz'));
65 //--------------------------------------------------------------------------------
66             $mform->addElement('submit', 'submitbutton', get_string('importfromthisfile','quiz'));
67         }
68 //--------------------------------------------------------------------------------
69         $mform->addElement('static', 'dummy', '');
70         $mform->closeHeaderBefore('dummy');
71      
72         // set a template for the format select elements   
73         $renderer =& $mform->defaultRenderer();
74         $template = "{help} {element}\n";
75         $renderer->setGroupElementTemplate($template, 'format');
76     }
77     function get_importfile_name(){
78         if ($this->is_submitted() and $this->is_validated()) {
79             // return the temporary filename to process
80             return $this->_upload_manager->files['newfile']['tmp_name'];
81         }else{
82             return  NULL;
83         }
84     }
85 }
86 ?>