| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: import.php,v 1.43.2.1 2007/10/12 16:09:44 tjhunt Exp $ 2 3 require_once("../../config.php"); 4 require_once ("lib.php"); 5 require_once("$CFG->dirroot/course/lib.php"); 6 global $CFG, $USER; 7 8 $id = required_param('id', PARAM_INT); // Course Module ID 9 10 $step = optional_param('step', 0, PARAM_INT); 11 $dest = optional_param('dest', 'current', PARAM_ALPHA); // current | new 12 $file = optional_param('file', '', PARAM_FILE); // file to import 13 $catsincl = optional_param('catsincl', 0, PARAM_INT); // Import Categories too? 14 15 $mode = optional_param('mode', 'letter', PARAM_ALPHA ); 16 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM); 17 18 if (! $cm = get_coursemodule_from_id('glossary', $id)) { 19 error("Course Module ID was incorrect"); 20 } 21 22 if (! $course = get_record("course", "id", $cm->course)) { 23 error("Course is misconfigured"); 24 } 25 26 if (! $glossary = get_record("glossary", "id", $cm->instance)) { 27 error("Course module is incorrect"); 28 } 29 30 require_login($course->id, false, $cm); 31 32 $context = get_context_instance(CONTEXT_MODULE, $cm->id); 33 require_capability('mod/glossary:import', $context); 34 35 if ($dest != 'new' and $dest != 'current') { 36 $dest = 'current'; 37 } 38 $strglossaries = get_string("modulenameplural", "glossary"); 39 $strglossary = get_string("modulename", "glossary"); 40 $strallcategories = get_string("allcategories", "glossary"); 41 $straddentry = get_string("addentry", "glossary"); 42 $strnoentries = get_string("noentries", "glossary"); 43 $strsearchconcept = get_string("searchconcept", "glossary"); 44 $strsearchindefinition = get_string("searchindefinition", "glossary"); 45 $strsearch = get_string("search"); 46 $strimportentries = get_string('importentriesfromxml', 'glossary'); 47 48 $navigation = build_navigation($strimportentries, $cm); 49 print_header_simple(format_string($glossary->name), "", $navigation, 50 "", "", true, update_module_button($cm->id, $course->id, $strglossary), 51 navmenu($course, $cm)); 52 53 print_heading($strimportentries); 54 55 if ( !$step ) { 56 print_box_start('glossarydisplay generalbox'); 57 include ("import.html"); 58 print_box_end(); 59 60 print_footer($course); 61 exit; 62 } 63 64 $form = data_submitted(); 65 $file = $_FILES["file"]; 66 67 require_once($CFG->dirroot.'/lib/uploadlib.php'); 68 $um = new upload_manager('file',false,false,$course,false,0); 69 70 if (!$um->preprocess_files()) { 71 print_box_start('glossarydisplay generalbox'); 72 print_continue('import.php?id='.$id); 73 print_box_end(); 74 75 print_footer(); 76 die(); 77 } 78 79 if ($xml = glossary_read_imported_file($file['tmp_name']) ) { 80 81 $importedentries = 0; 82 $importedcats = 0; 83 $entriesrejected = 0; 84 $rejections = ''; 85 if ($dest == 'new') { 86 // If the user chose to create a new glossary 87 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#']; 88 89 if ( $xmlglossary['NAME'][0]['#'] ) { 90 unset($glossary); 91 $glossary->name = addslashes($xmlglossary['NAME'][0]['#']); 92 $glossary->course = $course->id; 93 $glossary->globalglossary = addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']); 94 $glossary->intro = addslashes($xmlglossary['INTRO'][0]['#']); 95 $glossary->showspecial = addslashes($xmlglossary['SHOWSPECIAL'][0]['#']); 96 $glossary->showalphabet = addslashes($xmlglossary['SHOWALPHABET'][0]['#']); 97 $glossary->showall = addslashes($xmlglossary['SHOWALL'][0]['#']); 98 $glossary->timecreated = time(); 99 $glossary->timemodified = time(); 100 101 // Setting the default values if no values were passed 102 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) { 103 $glossary->entbypage = addslashes($xmlglossary['ENTBYPAGE'][0]['#']); 104 } else { 105 $glossary->entbypage = $CFG->glossary_entbypage; 106 } 107 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) { 108 $glossary->allowduplicatedentries = addslashes($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']); 109 } else { 110 $glossary->allowduplicatedentries = $CFG->glossary_dupentries; 111 } 112 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) { 113 $glossary->displayformat = addslashes($xmlglossary['DISPLAYFORMAT'][0]['#']); 114 } else { 115 $glossary->displayformat = 2; 116 } 117 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) { 118 $glossary->allowcomments = addslashes($xmlglossary['ALLOWCOMMENTS'][0]['#']); 119 } else { 120 $glossary->allowcomments = $CFG->glossary_allowcomments; 121 } 122 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) { 123 $glossary->usedynalink = addslashes($xmlglossary['USEDYNALINK'][0]['#']); 124 } else { 125 $glossary->usedynalink = $CFG->glossary_linkentries; 126 } 127 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) { 128 $glossary->defaultapproval = addslashes($xmlglossary['DEFAULTAPPROVAL'][0]['#']); 129 } else { 130 $glossary->defaultapproval = $CFG->glossary_defaultapproval; 131 } 132 133 // Include new glossary and return the new ID 134 if ( !$glossary->id = glossary_add_instance($glossary) ) { 135 notify("Error while trying to create the new glossary."); 136 echo '</center>'; 137 glossary_print_tabbed_table_end(); 138 print_footer($course); 139 exit; 140 } else { 141 //The instance has been created, so lets do course_modules 142 //and course_sections 143 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course 144 145 $mod->instance = $glossary->id; 146 // course_modules and course_sections each contain a reference 147 // to each other, so we have to update one of them twice. 148 149 if (! $currmodule = get_record("modules", "name", 'glossary')) { 150 error("Glossary module doesn't exist"); 151 } 152 $mod->module = $currmodule->id; 153 $mod->course = $course->id; 154 $mod->modulename = 'glossary'; 155 $mod->section = 0; 156 157 if (! $mod->coursemodule = add_course_module($mod) ) { 158 error("Could not add a new course module"); 159 } 160 161 if (! $sectionid = add_mod_to_section($mod) ) { 162 error("Could not add the new course module to that section"); 163 } 164 //We get the section's visible field status 165 $visible = get_field("course_sections","visible","id",$sectionid); 166 167 if (! set_field("course_modules", "visible", $visible, "id", $mod->coursemodule)) { 168 error("Could not update the course module with the correct visibility"); 169 } 170 171 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) { 172 error("Could not update the course module with the correct section"); 173 } 174 add_to_log($course->id, "course", "add mod", 175 "../mod/$mod->modulename/view.php?id=$mod->coursemodule", 176 "$mod->modulename $mod->instance"); 177 add_to_log($course->id, $mod->modulename, "add", 178 "view.php?id=$mod->coursemodule", 179 "$mod->instance", $mod->coursemodule); 180 181 rebuild_course_cache($course->id); 182 183 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%"); 184 echo '<p>'; 185 } 186 } else { 187 notify("Error while trying to create the new glossary."); 188 print_footer($course); 189 exit; 190 } 191 } 192 193 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY']; 194 for($i = 0; $i < sizeof($xmlentries); $i++) { 195 // Inserting the entries 196 $xmlentry = $xmlentries[$i]; 197 unset($newentry); 198 $newentry->concept = trim(addslashes($xmlentry['#']['CONCEPT'][0]['#'])); 199 $newentry->definition = trusttext_strip(addslashes($xmlentry['#']['DEFINITION'][0]['#'])); 200 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) { 201 $newentry->casesensitive = addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']); 202 } else { 203 $newentry->casesensitive = $CFG->glossary_casesensitive; 204 } 205 206 $permissiongranted = 1; 207 if ( $newentry->concept and $newentry->definition ) { 208 if ( !$glossary->allowduplicatedentries ) { 209 // checking if the entry is valid (checking if it is duplicated when should not be) 210 if ( $newentry->casesensitive ) { 211 $dupentry = get_record("glossary_entries","concept",$newentry->concept,"glossaryid",$glossary->id); 212 } else { 213 $dupentry = get_record("glossary_entries","lower(concept)",moodle_strtolower($newentry->concept),"glossaryid",$glossary->id); 214 } 215 if ($dupentry) { 216 $permissiongranted = 0; 217 } 218 } 219 } else { 220 $permissiongranted = 0; 221 } 222 if ($permissiongranted) { 223 $newentry->glossaryid = $glossary->id; 224 $newentry->sourceglossaryid = 0; 225 $newentry->approved = 1; 226 $newentry->userid = $USER->id; 227 $newentry->teacherentry = 1; 228 $newentry->format = addslashes($xmlentry['#']['FORMAT'][0]['#']); 229 $newentry->timecreated = time(); 230 $newentry->timemodified = time(); 231 232 // Setting the default values if no values were passed 233 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) { 234 $newentry->usedynalink = addslashes($xmlentry['#']['USEDYNALINK'][0]['#']); 235 } else { 236 $newentry->usedynalink = $CFG->glossary_linkentries; 237 } 238 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) { 239 $newentry->fullmatch = addslashes($xmlentry['#']['FULLMATCH'][0]['#']); 240 } else { 241 $newentry->fullmatch = $CFG->glossary_fullmatch; 242 } 243 244 if ( $newentry->id = insert_record("glossary_entries",$newentry) ) { 245 $importedentries++; 246 247 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES 248 for($k = 0; $k < sizeof($xmlaliases); $k++) { 249 /// Importing aliases 250 $xmlalias = $xmlaliases[$k]; 251 $aliasname = $xmlalias['#']['NAME'][0]['#']; 252 253 if (!empty($aliasname)) { 254 unset($newalias); 255 $newalias->entryid = $newentry->id; 256 $newalias->alias = trim(addslashes($aliasname)); 257 $newalias->id = insert_record("glossary_alias",$newalias); 258 } 259 } 260 261 if ( $catsincl ) { 262 // If the categories must be imported... 263 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES 264 for($k = 0; $k < sizeof($xmlcats); $k++) { 265 $xmlcat = $xmlcats[$k]; 266 unset($newcat); 267 268 $newcat->name = addslashes($xmlcat['#']['NAME'][0]['#']); 269 $newcat->usedynalink = addslashes($xmlcat['#']['USEDYNALINK'][0]['#']); 270 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) { 271 // Create the category if it does not exist 272 unset($category); 273 $category->name = $newcat->name; 274 $category->glossaryid = $glossary->id; 275 if ( !$category->id = insert_record("glossary_categories",$category)) { 276 // add to exception report (can't insert category) 277 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" . 278 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>"; 279 } else { 280 $importedcats++; 281 } 282 } 283 if ( $category ) { 284 // inserting the new relation 285 unset($entrycat); 286 $entrycat->entryid = $newentry->id; 287 $entrycat->categoryid = $category->id; 288 if ( !insert_record("glossary_entries_categories",$entrycat) ) { 289 // add to exception report (can't insert relation) 290 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" . 291 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>"; 292 } 293 } 294 } 295 } 296 } else { 297 $entriesrejected++; 298 // add to exception report (can't insert new record) 299 $rejections .= "<tr><td>$newentry->concept</td>" . 300 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>"; 301 } 302 } else { 303 $entriesrejected++; 304 if ( $newentry->concept and $newentry->definition ) { 305 // add to exception report (duplicated entry)) 306 $rejections .= "<tr><td>$newentry->concept</td>" . 307 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>"; 308 } else { 309 // add to exception report (no concept or definition found)) 310 $rejections .= "<tr><td>---</td>" . 311 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>"; 312 } 313 } 314 } 315 // processed entries 316 print_box_start('glossarydisplay generalbox'); 317 echo '<table class="glossaryimportexport">'; 318 echo '<tr>'; 319 echo '<td width="50%" align="right">'; 320 echo get_string("totalentries","glossary"); 321 echo ':</td>'; 322 echo '<td width="50%" align="left">'; 323 echo $importedentries + $entriesrejected; 324 echo '</td>'; 325 echo '</tr>'; 326 echo '<tr>'; 327 echo '<td width="50%" align="right">'; 328 echo get_string("importedentries","glossary"); 329 echo ':</td>'; 330 echo '<td width="50%" align="left">'; 331 echo $importedentries; 332 if ( $entriesrejected ) { 333 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>"; 334 } 335 echo '</td>'; 336 echo '</tr>'; 337 if ( $catsincl ) { 338 echo '<tr>'; 339 echo '<td width="50%" align="right">'; 340 echo get_string("importedcategories","glossary"); 341 echo ':</td>'; 342 echo '<td width="50%">'; 343 echo $importedcats; 344 echo '</td>'; 345 echo '</tr>'; 346 } 347 echo '</table><hr />'; 348 349 // rejected entries 350 if ($rejections) { 351 echo '<table class="glossaryimportexport">'; 352 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>'; 353 echo $rejections; 354 echo '</table><hr />'; 355 } 356 /// Print continue button, based on results 357 if ($importedentries) { 358 print_continue('view.php?id='.$id); 359 } else { 360 print_continue('import.php?id='.$id); 361 } 362 print_box_end(); 363 } else { 364 notice(get_string('errorparsingxml', 'glossary')); 365 } 366 367 /// Finish the page 368 print_footer($course); 369 370 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |