mod-forum MDL-19808 Added set_url calls, replaced deprecated functions
[moodle.git] / mod / glossary / import.php
CommitLineData
4528ab53 1<?php // $Id$
748b1932 2
3 require_once("../../config.php");
4 require_once("lib.php");
73e6ac63 5 require_once("$CFG->dirroot/course/lib.php");
748b1932 6
aa813941 7 $id = required_param('id', PARAM_INT); // Course Module ID
748b1932 8
aa813941 9 $step = optional_param('step', 0, PARAM_INT);
81b0eec8 10 $dest = optional_param('dest', 'current', PARAM_ALPHA); // current | new
aa813941 11 $file = optional_param('file', '', PARAM_FILE); // file to import
12 $catsincl = optional_param('catsincl', 0, PARAM_INT); // Import Categories too?
748b1932 13
aa813941 14 $mode = optional_param('mode', 'letter', PARAM_ALPHA );
15 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
748b1932 16
f9d5371b 17 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
5973a4c4 18 print_error('invalidcoursemodule');
fe32b4f6 19 }
9f555b8f 20
ae8c3566 21 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
5973a4c4 22 print_error('coursemisconf');
fe32b4f6 23 }
24
ae8c3566 25 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
5973a4c4 26 print_error('invalidid', 'glossary');
fe32b4f6 27 }
28
9f555b8f 29 require_login($course->id, false, $cm);
30
dabfd0ed 31 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 32 require_capability('mod/glossary:import', $context);
748b1932 33
748b1932 34 if ($dest != 'new' and $dest != 'current') {
35 $dest = 'current';
36 }
37 $strglossaries = get_string("modulenameplural", "glossary");
38 $strglossary = get_string("modulename", "glossary");
39 $strallcategories = get_string("allcategories", "glossary");
40 $straddentry = get_string("addentry", "glossary");
41 $strnoentries = get_string("noentries", "glossary");
42 $strsearchconcept = get_string("searchconcept", "glossary");
43 $strsearchindefinition = get_string("searchindefinition", "glossary");
44 $strsearch = get_string("search");
20355e5a 45 $strimportentries = get_string('importentriesfromxml', 'glossary');
fe32b4f6 46
0961861e 47 $PAGE->set_url(new moodle_url($CFG->wwwroot.'/mod/glossary/import.php', array('id'=>$cm->id, 'mode'=>$mode, 'hook'=>$hook)));
e6855d1a 48 $PAGE->navbar->add($strimportentries);
49 $PAGE->set_title(format_string($glossary->name));
50 $PAGE->set_button(update_module_button($cm->id, $course->id, $strglossary));
51
52 echo $OUTPUT->header();
2d8e042e 53 echo $OUTPUT->heading($strimportentries);
748b1932 54
55 if ( !$step ) {
9f555b8f 56 echo $OUTPUT->box_start('glossarydisplay generalbox');
fe32b4f6 57 include("import.html");
9f555b8f 58 echo $OUTPUT->box_end();
748b1932 59
0578c9a2 60 echo $OUTPUT->footer();
748b1932 61 exit;
fe32b4f6 62 }
748b1932 63
64 $form = data_submitted();
65 $file = $_FILES["file"];
73e6ac63 66
18b8fbfa 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()) {
9f555b8f 71 echo $OUTPUT->box_start('glossarydisplay generalbox');
ee9e63b5 72 echo $OUTPUT->continue_button('import.php?id='.$id);
9f555b8f 73 echo $OUTPUT->box_end();
6ca98074 74
0578c9a2 75 echo $OUTPUT->footer();
18b8fbfa 76 die();
77 }
78
748b1932 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]['#'];
fe32b4f6 88
748b1932 89 if ( $xmlglossary['NAME'][0]['#'] ) {
90 unset($glossary);
c18269c7 91 $glossary->name = ($xmlglossary['NAME'][0]['#']);
73e6ac63 92 $glossary->course = $course->id;
c18269c7 93 $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
94 $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
95 $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
96 $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
97 $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
73e6ac63 98 $glossary->timecreated = time();
99 $glossary->timemodified = time();
748b1932 100
101 // Setting the default values if no values were passed
73e6ac63 102 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
c18269c7 103 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
748b1932 104 } else {
73e6ac63 105 $glossary->entbypage = $CFG->glossary_entbypage;
748b1932 106 }
107 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
c18269c7 108 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
748b1932 109 } else {
73e6ac63 110 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
111 }
112 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
c18269c7 113 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
73e6ac63 114 } else {
115 $glossary->displayformat = 2;
748b1932 116 }
117 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
c18269c7 118 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
748b1932 119 } else {
73e6ac63 120 $glossary->allowcomments = $CFG->glossary_allowcomments;
748b1932 121 }
122 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
c18269c7 123 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
748b1932 124 } else {
73e6ac63 125 $glossary->usedynalink = $CFG->glossary_linkentries;
748b1932 126 }
127 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
c18269c7 128 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
748b1932 129 } else {
73e6ac63 130 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
748b1932 131 }
132
133 // Include new glossary and return the new ID
134 if ( !$glossary->id = glossary_add_instance($glossary) ) {
ee9e63b5 135 echo $OUTPUT->notification("Error while trying to create the new glossary.");
d3709d95 136 echo '</center>';
748b1932 137 glossary_print_tabbed_table_end();
0578c9a2 138 echo $OUTPUT->footer();
748b1932 139 exit;
140 } else {
73e6ac63 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
ae8c3566 149 if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
5973a4c4 150 print_error('modulenotexist', 'debug', '', 'Glossary');
73e6ac63 151 }
152 $mod->module = $currmodule->id;
153 $mod->course = $course->id;
154 $mod->modulename = 'glossary';
81b0eec8 155 $mod->section = 0;
73e6ac63 156
157 if (! $mod->coursemodule = add_course_module($mod) ) {
5973a4c4 158 print_error('cannotaddcoursemodule');
73e6ac63 159 }
160
161 if (! $sectionid = add_mod_to_section($mod) ) {
5973a4c4 162 print_error('cannotaddcoursemoduletosection');
73e6ac63 163 }
164 //We get the section's visible field status
ae8c3566 165 $visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
73e6ac63 166
a8c31db2 167 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
168 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
73e6ac63 169
73e6ac63 170 add_to_log($course->id, "course", "add mod",
171 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
172 "$mod->modulename $mod->instance");
173 add_to_log($course->id, $mod->modulename, "add",
174 "view.php?id=$mod->coursemodule",
175 "$mod->instance", $mod->coursemodule);
176
177 rebuild_course_cache($course->id);
178
ee9e63b5 179 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
748b1932 180 echo '<p>';
181 }
182 } else {
ee9e63b5 183 echo $OUTPUT->notification("Error while trying to create the new glossary.");
0578c9a2 184 echo $OUTPUT->footer();
748b1932 185 exit;
186 }
187 }
188
189 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
190 for($i = 0; $i < sizeof($xmlentries); $i++) {
191 // Inserting the entries
192 $xmlentry = $xmlentries[$i];
748b1932 193 unset($newentry);
294ce987 194 $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
195 $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
73e6ac63 196 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
294ce987 197 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
73e6ac63 198 } else {
294ce987 199 $newentry->casesensitive = $CFG->glossary_casesensitive;
73e6ac63 200 }
748b1932 201
202 $permissiongranted = 1;
203 if ( $newentry->concept and $newentry->definition ) {
204 if ( !$glossary->allowduplicatedentries ) {
fe32b4f6 205 // checking if the entry is valid (checking if it is duplicated when should not be)
73e6ac63 206 if ( $newentry->casesensitive ) {
ae8c3566 207 $dupentry = $DB->get_record("glossary_entries", array("concept"=>$newentry->concept, "glossaryid"=>$glossary->id));
748b1932 208 } else {
ae8c3566 209 $dupentry = $DB->get_record("glossary_entries", array("lower(concept)"=>moodle_strtolower($newentry->concept)), array("glossaryid"=>$glossary->id));
748b1932 210 }
211 if ($dupentry) {
212 $permissiongranted = 0;
213 }
214 }
215 } else {
216 $permissiongranted = 0;
73e6ac63 217}
748b1932 218 if ($permissiongranted) {
219 $newentry->glossaryid = $glossary->id;
220 $newentry->sourceglossaryid = 0;
221 $newentry->approved = 1;
222 $newentry->userid = $USER->id;
73e6ac63 223 $newentry->teacherentry = 1;
ae8c3566 224 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
748b1932 225 $newentry->timecreated = time();
226 $newentry->timemodified = time();
fe32b4f6 227
748b1932 228 // Setting the default values if no values were passed
229 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
ae8c3566 230 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
748b1932 231 } else {
73e6ac63 232 $newentry->usedynalink = $CFG->glossary_linkentries;
748b1932 233 }
234 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
ae8c3566 235 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
748b1932 236 } else {
73e6ac63 237 $newentry->fullmatch = $CFG->glossary_fullmatch;
748b1932 238 }
239
ae8c3566 240 if ( $newentry->id = $DB->insert_record("glossary_entries",$newentry) ) {
748b1932 241 $importedentries++;
7965be79 242
81b0eec8 243 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
7965be79 244 for($k = 0; $k < sizeof($xmlaliases); $k++) {
245 /// Importing aliases
246 $xmlalias = $xmlaliases[$k];
e1013df8 247 $aliasname = $xmlalias['#']['NAME'][0]['#'];
248
249 if (!empty($aliasname)) {
ae8c3566 250 $newalias = new object();
e1013df8 251 $newalias->entryid = $newentry->id;
ae8c3566 252 $newalias->alias = trim($aliasname);
253 $newalias->id = $DB->insert_record("glossary_alias",$newalias);
e1013df8 254 }
7965be79 255 }
256
748b1932 257 if ( $catsincl ) {
258 // If the categories must be imported...
81b0eec8 259 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
748b1932 260 for($k = 0; $k < sizeof($xmlcats); $k++) {
261 $xmlcat = $xmlcats[$k];
fe32b4f6 262
ae8c3566 263 $newcat = new object();
264 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
265 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
266 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
748b1932 267 // Create the category if it does not exist
ae8c3566 268 $category = new object();
748b1932 269 $category->name = $newcat->name;
270 $category->glossaryid = $glossary->id;
9d749339 271 $category->id = $DB->insert_record("glossary_categories",$category);
272 $importedcats++;
748b1932 273 }
274 if ( $category ) {
275 // inserting the new relation
ae8c3566 276 $entrycat = new opbject();
748b1932 277 $entrycat->entryid = $newentry->id;
278 $entrycat->categoryid = $category->id;
9d749339 279 $DB->insert_record("glossary_entries_categories",$entrycat);
748b1932 280 }
281 }
282 }
283 } else {
284 $entriesrejected++;
285 // add to exception report (can't insert new record)
286 $rejections .= "<tr><td>$newentry->concept</td>" .
287 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
288 }
289 } else {
290 $entriesrejected++;
291 if ( $newentry->concept and $newentry->definition ) {
292 // add to exception report (duplicated entry))
293 $rejections .= "<tr><td>$newentry->concept</td>" .
294 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
295 } else {
296 // add to exception report (no concept or definition found))
297 $rejections .= "<tr><td>---</td>" .
298 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
299 }
300 }
301 }
302 // processed entries
9f555b8f 303 echo $OUTPUT->box_start('glossarydisplay generalbox');
6ca98074 304 echo '<table class="glossaryimportexport">';
73e6ac63 305 echo '<tr>';
41905731 306 echo '<td width="50%" align="right">';
73e6ac63 307 echo get_string("totalentries","glossary");
308 echo ':</td>';
6ca98074 309 echo '<td width="50%" align="left">';
73e6ac63 310 echo $importedentries + $entriesrejected;
311 echo '</td>';
312 echo '</tr>';
313 echo '<tr>';
41905731 314 echo '<td width="50%" align="right">';
73e6ac63 315 echo get_string("importedentries","glossary");
316 echo ':</td>';
6ca98074 317 echo '<td width="50%" align="left">';
73e6ac63 318 echo $importedentries;
319 if ( $entriesrejected ) {
320 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
321 }
322 echo '</td>';
323 echo '</tr>';
324 if ( $catsincl ) {
748b1932 325 echo '<tr>';
41905731 326 echo '<td width="50%" align="right">';
73e6ac63 327 echo get_string("importedcategories","glossary");
328 echo ':</td>';
41905731 329 echo '<td width="50%">';
73e6ac63 330 echo $importedcats;
331 echo '</td>';
748b1932 332 echo '</tr>';
73e6ac63 333 }
6ca98074 334 echo '</table><hr />';
748b1932 335
fe32b4f6 336 // rejected entries
748b1932 337 if ($rejections) {
6ca98074 338 echo '<table class="glossaryimportexport">';
41905731 339 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
73e6ac63 340 echo $rejections;
6ca98074 341 echo '</table><hr />';
748b1932 342 }
8d2191e6 343 /// Print continue button, based on results
344 if ($importedentries) {
ee9e63b5 345 echo $OUTPUT->continue_button('view.php?id='.$id);
8d2191e6 346 } else {
ee9e63b5 347 echo $OUTPUT->continue_button('import.php?id='.$id);
8d2191e6 348 }
9f555b8f 349 echo $OUTPUT->box_end();
748b1932 350 } else {
20355e5a 351 notice(get_string('errorparsingxml', 'glossary'));
748b1932 352 }
353
748b1932 354/// Finish the page
0578c9a2 355 echo $OUTPUT->footer();
748b1932 356
9b690d2b 357?>