|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00025 require_once("../../config.php"); 00026 require_once("lib.php"); 00027 require_once('import_form.php'); 00028 00029 // get parameters 00030 $id = required_param('id', PARAM_INT); 00031 $choosefile = optional_param('choosefile', false, PARAM_PATH); 00032 $action = optional_param('action', false, PARAM_ALPHA); 00033 00034 $url = new moodle_url('/mod/feedback/import.php', array('id'=>$id)); 00035 if ($choosefile !== false) { 00036 $url->param('choosefile', $choosefile); 00037 } 00038 if ($action !== false) { 00039 $url->param('action', $action); 00040 } 00041 $PAGE->set_url($url); 00042 00043 if (! $cm = get_coursemodule_from_id('feedback', $id)) { 00044 print_error('invalidcoursemodule'); 00045 } 00046 00047 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) { 00048 print_error('coursemisconf'); 00049 } 00050 00051 if (! $feedback = $DB->get_record("feedback", array("id"=>$cm->instance))) { 00052 print_error('invalidcoursemodule'); 00053 } 00054 00055 if (!$context = get_context_instance(CONTEXT_MODULE, $cm->id)) { 00056 print_error('badcontext'); 00057 } 00058 00059 require_login($course->id, true, $cm); 00060 00061 require_capability('mod/feedback:edititems', $context); 00062 00063 $mform = new feedback_import_form(); 00064 $newformdata = array('id'=>$id, 00065 'deleteolditems'=>'1', 00066 'action'=>'choosefile', 00067 'confirmadd'=>'1', 00068 'do_show'=>'templates'); 00069 $mform->set_data($newformdata); 00070 $formdata = $mform->get_data(); 00071 00072 if ($mform->is_cancelled()) { 00073 redirect('edit.php?id='.$id.'&do_show=templates'); 00074 } 00075 00076 // process if we are happy file is ok 00077 if ($choosefile) { 00078 $xmlcontent = $mform->get_file_content('choosefile'); 00079 00080 if (!$xmldata = feedback_load_xml_data($xmlcontent)) { 00081 print_error('cannotloadxml', 'feedback', 'edit.php?id='.$id); 00082 } 00083 00084 $importerror = feedback_import_loaded_data($xmldata, $feedback->id); 00085 if ($importerror->stat == true) { 00086 $url = 'edit.php?id='.$id.'&do_show=templates'; 00087 redirect($url, get_string('import_successfully', 'feedback'), 3); 00088 exit; 00089 } 00090 } 00091 00092 00094 $strfeedbacks = get_string("modulenameplural", "feedback"); 00095 $strfeedback = get_string("modulename", "feedback"); 00096 00097 $PAGE->set_heading(format_string($course->fullname)); 00098 $PAGE->set_title(format_string($feedback->name)); 00099 echo $OUTPUT->header(); 00100 00102 require('tabs.php'); 00103 00108 echo $OUTPUT->heading(get_string('import_questions', 'feedback')); 00109 00110 if (isset($importerror->msg) AND is_array($importerror->msg)) { 00111 echo $OUTPUT->box_start('generalbox errorboxcontent boxaligncenter'); 00112 foreach ($importerror->msg as $msg) { 00113 echo $msg.'<br />'; 00114 } 00115 echo $OUTPUT->box_end(); 00116 } 00117 00118 $mform->display(); 00119 00120 echo $OUTPUT->footer(); 00121 00122 function feedback_load_xml_data($xmlcontent) { 00123 global $CFG; 00124 require_once($CFG->dirroot.'/lib/xmlize.php'); 00125 00126 if (!$xmlcontent = feedback_check_xml_utf8($xmlcontent)) { 00127 return false; 00128 } 00129 00130 $data = xmlize($xmlcontent, 1, 'UTF-8'); 00131 00132 if (intval($data['FEEDBACK']['@']['VERSION']) != 200701) { 00133 return false; 00134 } 00135 $data = $data['FEEDBACK']['#']['ITEMS'][0]['#']['ITEM']; 00136 return $data; 00137 } 00138 00139 function feedback_import_loaded_data(&$data, $feedbackid) { 00140 global $CFG, $DB; 00141 00142 feedback_load_feedback_items(); 00143 00144 $deleteolditems = optional_param('deleteolditems', 0, PARAM_INT); 00145 00146 $error = new stdClass(); 00147 $error->stat = true; 00148 $error->msg = array(); 00149 00150 if (!is_array($data)) { 00151 $error->msg[] = get_string('data_is_not_an_array', 'feedback'); 00152 $error->stat = false; 00153 return $error; 00154 } 00155 00156 if ($deleteolditems) { 00157 feedback_delete_all_items($feedbackid); 00158 $position = 0; 00159 } else { 00160 //items will be add to the end of the existing items 00161 $position = $DB->count_records('feedback_item', array('feedback'=>$feedbackid)); 00162 } 00163 00164 //depend items we are storing temporary in an mapping list array(new id => dependitem) 00165 //we also store a mapping of all items array(oldid => newid) 00166 $dependitemsmap = array(); 00167 $itembackup = array(); 00168 foreach ($data as $item) { 00169 $position++; 00170 //check the typ 00171 $typ = $item['@']['TYPE']; 00172 00173 //check oldtypes first 00174 switch($typ) { 00175 case 'radio': 00176 $typ = 'multichoice'; 00177 $oldtyp = 'radio'; 00178 break; 00179 case 'dropdown': 00180 $typ = 'multichoice'; 00181 $oldtyp = 'dropdown'; 00182 break; 00183 case 'check': 00184 $typ = 'multichoice'; 00185 $oldtyp = 'check'; 00186 break; 00187 case 'radiorated': 00188 $typ = 'multichoicerated'; 00189 $oldtyp = 'radiorated'; 00190 break; 00191 case 'dropdownrated': 00192 $typ = 'multichoicerated'; 00193 $oldtyp = 'dropdownrated'; 00194 break; 00195 default: 00196 $oldtyp = $typ; 00197 } 00198 00199 $itemclass = 'feedback_item_'.$typ; 00200 if ($typ != 'pagebreak' AND !class_exists($itemclass)) { 00201 $error->stat = false; 00202 $error->msg[] = 'type ('.$typ.') not found'; 00203 continue; 00204 } 00205 $itemobj = new $itemclass(); 00206 00207 $newitem = new stdClass(); 00208 $newitem->feedback = $feedbackid; 00209 $newitem->template = 0; 00210 $newitem->typ = $typ; 00211 $newitem->name = trim($item['#']['ITEMTEXT'][0]['#']); 00212 $newitem->label = trim($item['#']['ITEMLABEL'][0]['#']); 00213 $newitem->options = trim($item['#']['OPTIONS'][0]['#']); 00214 $newitem->presentation = trim($item['#']['PRESENTATION'][0]['#']); 00215 //check old types of radio, check, and so on 00216 switch($oldtyp) { 00217 case 'radio': 00218 $newitem->presentation = 'r>>>>>'.$newitem->presentation; 00219 break; 00220 case 'dropdown': 00221 $newitem->presentation = 'd>>>>>'.$newitem->presentation; 00222 break; 00223 case 'check': 00224 $newitem->presentation = 'c>>>>>'.$newitem->presentation; 00225 break; 00226 case 'radiorated': 00227 $newitem->presentation = 'r>>>>>'.$newitem->presentation; 00228 break; 00229 case 'dropdownrated': 00230 $newitem->presentation = 'd>>>>>'.$newitem->presentation; 00231 break; 00232 } 00233 00234 if (isset($item['#']['DEPENDITEM'][0]['#'])) { 00235 $newitem->dependitem = intval($item['#']['DEPENDITEM'][0]['#']); 00236 } else { 00237 $newitem->dependitem = 0; 00238 } 00239 if (isset($item['#']['DEPENDVALUE'][0]['#'])) { 00240 $newitem->dependvalue = trim($item['#']['DEPENDVALUE'][0]['#']); 00241 } else { 00242 $newitem->dependvalue = ''; 00243 } 00244 $olditemid = intval($item['#']['ITEMID'][0]['#']); 00245 00246 if ($typ != 'pagebreak') { 00247 $newitem->hasvalue = $itemobj->get_hasvalue(); 00248 } else { 00249 $newitem->hasvalue = 0; 00250 } 00251 $newitem->required = intval($item['@']['REQUIRED']); 00252 $newitem->position = $position; 00253 $newid = $DB->insert_record('feedback_item', $newitem); 00254 00255 $itembackup[$olditemid] = $newid; 00256 if ($newitem->dependitem) { 00257 $dependitemsmap[$newid] = $newitem->dependitem; 00258 } 00259 00260 } 00261 //remapping the dependency 00262 foreach ($dependitemsmap as $key => $dependitem) { 00263 $newitem = $DB->get_record('feedback_item', array('id'=>$key)); 00264 $newitem->dependitem = $itembackup[$newitem->dependitem]; 00265 $DB->update_record('feedback_item', $newitem); 00266 } 00267 00268 return $error; 00269 } 00270 00271 function feedback_check_xml_utf8($text) { 00272 //find the encoding 00273 $searchpattern = '/^<\?xml.+(encoding=\"([a-z0-9-]*)\").+\?>/is'; 00274 00275 if (!preg_match($searchpattern, $text, $match)) { 00276 return false; //no xml-file 00277 } 00278 00279 //$match[0] = <\? xml ... \?> (without \) 00280 //$match[1] = encoding="...." 00281 //$match[2] = ISO-8859-1 or so on 00282 if (isset($match[0]) AND !isset($match[1])) { //no encoding given. we assume utf-8 00283 return $text; 00284 } 00285 00286 //encoding is given in $match[2] 00287 if (isset($match[0]) AND isset($match[1]) AND isset($match[2])) { 00288 $enc = $match[2]; 00289 $textlib = textlib_get_instance(); 00290 return $textlib->convert($text, $enc); 00291 } 00292 }