MDL-33831 fix E_STRICT issues in glossary
[moodle.git] / mod / glossary / import.php
CommitLineData
5fa0208e 1<?php
748b1932 2
5fa0208e 3require_once("../../config.php");
4require_once("lib.php");
5require_once("$CFG->dirroot/course/lib.php");
ab16da67 6require_once('import_form.php');
748b1932 7
5fa0208e 8$id = required_param('id', PARAM_INT); // Course Module ID
748b1932 9
5fa0208e 10$mode = optional_param('mode', 'letter', PARAM_ALPHA );
11$hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
748b1932 12
a6855934 13$url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
5fa0208e 14if ($mode !== 'letter') {
15 $url->param('mode', $mode);
16}
17if ($hook !== 'ALL') {
18 $url->param('hook', $hook);
19}
20$PAGE->set_url($url);
9f555b8f 21
5fa0208e 22if (! $cm = get_coursemodule_from_id('glossary', $id)) {
23 print_error('invalidcoursemodule');
24}
fe32b4f6 25
5fa0208e 26if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
27 print_error('coursemisconf');
28}
fe32b4f6 29
5fa0208e 30if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
31 print_error('invalidid', 'glossary');
32}
9f555b8f 33
cdbea7ee 34require_login($course, false, $cm);
748b1932 35
5fa0208e 36$context = get_context_instance(CONTEXT_MODULE, $cm->id);
37require_capability('mod/glossary:import', $context);
748b1932 38
5fa0208e 39$strglossaries = get_string("modulenameplural", "glossary");
40$strglossary = get_string("modulename", "glossary");
41$strallcategories = get_string("allcategories", "glossary");
42$straddentry = get_string("addentry", "glossary");
43$strnoentries = get_string("noentries", "glossary");
5fa0208e 44$strsearchindefinition = get_string("searchindefinition", "glossary");
45$strsearch = get_string("search");
46$strimportentries = get_string('importentriesfromxml', 'glossary');
47
5fa0208e 48$PAGE->navbar->add($strimportentries);
49$PAGE->set_title(format_string($glossary->name));
2e4fd166 50$PAGE->set_heading($course->fullname);
5fa0208e 51
52echo $OUTPUT->header();
53echo $OUTPUT->heading($strimportentries);
54
ab16da67
DC
55$form = new mod_glossary_import_form();
56
57if ( !$data = $form->get_data() ) {
5fa0208e 58 echo $OUTPUT->box_start('glossarydisplay generalbox');
ab16da67 59 // display upload form
6bdfef5d 60 $data = new stdClass();
ab16da67
DC
61 $data->id = $id;
62 $form->set_data($data);
63 $form->display();
5fa0208e 64 echo $OUTPUT->box_end();
5fa0208e 65 echo $OUTPUT->footer();
66 exit;
67}
18b8fbfa 68
ab16da67 69$result = $form->get_file_content('file');
18b8fbfa 70
ab16da67 71if (empty($result)) {
5fa0208e 72 echo $OUTPUT->box_start('glossarydisplay generalbox');
73 echo $OUTPUT->continue_button('import.php?id='.$id);
74 echo $OUTPUT->box_end();
5fa0208e 75 echo $OUTPUT->footer();
76 die();
77}
73e6ac63 78
ab16da67 79if ($xml = glossary_read_imported_file($result)) {
5fa0208e 80 $importedentries = 0;
81 $importedcats = 0;
82 $entriesrejected = 0;
83 $rejections = '';
ab16da67
DC
84
85 if ($data->dest == 'newglossary') {
5fa0208e 86 // If the user chose to create a new glossary
87 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
88
89 if ( $xmlglossary['NAME'][0]['#'] ) {
9bba1308 90 $glossary = new stdClass();
5fa0208e 91 $glossary->name = ($xmlglossary['NAME'][0]['#']);
92 $glossary->course = $course->id;
93 $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
94 $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
72c4be39 95 $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
5fa0208e 96 $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
97 $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
98 $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
99 $glossary->timecreated = time();
100 $glossary->timemodified = time();
ab16da67 101 $glossary->cmidnumber = $cm->idnumber;
5fa0208e 102
103 // Setting the default values if no values were passed
104 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
105 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
106 } else {
107 $glossary->entbypage = $CFG->glossary_entbypage;
108 }
109 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
110 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
111 } else {
112 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
113 }
114 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
115 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
116 } else {
117 $glossary->displayformat = 2;
118 }
119 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
120 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
121 } else {
122 $glossary->allowcomments = $CFG->glossary_allowcomments;
123 }
124 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
125 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
126 } else {
127 $glossary->usedynalink = $CFG->glossary_linkentries;
128 }
129 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
130 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
131 } else {
132 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
133 }
73e6ac63 134
5fa0208e 135 // Include new glossary and return the new ID
136 if ( !$glossary->id = glossary_add_instance($glossary) ) {
137 echo $OUTPUT->notification("Error while trying to create the new glossary.");
5fa0208e 138 glossary_print_tabbed_table_end();
139 echo $OUTPUT->footer();
140 exit;
141 } else {
142 //The instance has been created, so lets do course_modules
143 //and course_sections
144 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course
73e6ac63 145
5fa0208e 146 $mod->instance = $glossary->id;
147 // course_modules and course_sections each contain a reference
148 // to each other, so we have to update one of them twice.
73e6ac63 149
5fa0208e 150 if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
151 print_error('modulenotexist', 'debug', '', 'Glossary');
152 }
153 $mod->module = $currmodule->id;
154 $mod->course = $course->id;
155 $mod->modulename = 'glossary';
156 $mod->section = 0;
73e6ac63 157
5fa0208e 158 if (! $mod->coursemodule = add_course_module($mod) ) {
159 print_error('cannotaddcoursemodule');
160 }
73e6ac63 161
5fa0208e 162 if (! $sectionid = add_mod_to_section($mod) ) {
163 print_error('cannotaddcoursemoduletosection');
164 }
165 //We get the section's visible field status
166 $visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
73e6ac63 167
5fa0208e 168 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
169 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
73e6ac63 170
5fa0208e 171 add_to_log($course->id, "course", "add mod",
172 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
173 "$mod->modulename $mod->instance");
174 add_to_log($course->id, $mod->modulename, "add",
175 "view.php?id=$mod->coursemodule",
176 "$mod->instance", $mod->coursemodule);
177
178 rebuild_course_cache($course->id);
179
180 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
748b1932 181 }
5fa0208e 182 } else {
183 echo $OUTPUT->notification("Error while trying to create the new glossary.");
184 echo $OUTPUT->footer();
185 exit;
748b1932 186 }
5fa0208e 187 }
748b1932 188
5fa0208e 189 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
f179cdb7
AB
190 $sizeofxmlentries = sizeof($xmlentries);
191 for($i = 0; $i < $sizeofxmlentries; $i++) {
5fa0208e 192 // Inserting the entries
193 $xmlentry = $xmlentries[$i];
9bba1308 194 $newentry = new stdClass();
5fa0208e 195 $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
196 $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
197 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
198 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
199 } else {
200 $newentry->casesensitive = $CFG->glossary_casesensitive;
201 }
748b1932 202
5fa0208e 203 $permissiongranted = 1;
204 if ( $newentry->concept and $newentry->definition ) {
205 if ( !$glossary->allowduplicatedentries ) {
206 // checking if the entry is valid (checking if it is duplicated when should not be)
207 if ( $newentry->casesensitive ) {
20d3bf61
EL
208 $dupentry = $DB->record_exists_select('glossary_entries',
209 'glossaryid = :glossaryid AND concept = :concept', array(
210 'glossaryid' => $glossary->id,
211 'concept' => $newentry->concept));
748b1932 212 } else {
20d3bf61
EL
213 $dupentry = $DB->record_exists_select('glossary_entries',
214 'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
215 'glossaryid' => $glossary->id,
6f3451e5 216 'concept' => textlib::strtolower($newentry->concept)));
748b1932 217 }
5fa0208e 218 if ($dupentry) {
219 $permissiongranted = 0;
748b1932 220 }
5fa0208e 221 }
222 } else {
223 $permissiongranted = 0;
72c4be39 224 }
5fa0208e 225 if ($permissiongranted) {
226 $newentry->glossaryid = $glossary->id;
227 $newentry->sourceglossaryid = 0;
228 $newentry->approved = 1;
229 $newentry->userid = $USER->id;
230 $newentry->teacherentry = 1;
72c4be39 231 $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
5fa0208e 232 $newentry->timecreated = time();
233 $newentry->timemodified = time();
234
235 // Setting the default values if no values were passed
236 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
237 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
238 } else {
239 $newentry->usedynalink = $CFG->glossary_linkentries;
240 }
241 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
242 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
243 } else {
244 $newentry->fullmatch = $CFG->glossary_fullmatch;
245 }
748b1932 246
a9637e7d
PS
247 $newentry->id = $DB->insert_record("glossary_entries",$newentry);
248 $importedentries++;
249
250 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
2b9ce62e 251 $sizeofxmlaliases = sizeof($xmlaliases);
f179cdb7 252 for($k = 0; $k < $sizeofxmlaliases; $k++) {
a9637e7d
PS
253 /// Importing aliases
254 $xmlalias = $xmlaliases[$k];
255 $aliasname = $xmlalias['#']['NAME'][0]['#'];
256
257 if (!empty($aliasname)) {
39790bd8 258 $newalias = new stdClass();
a9637e7d
PS
259 $newalias->entryid = $newentry->id;
260 $newalias->alias = trim($aliasname);
261 $newalias->id = $DB->insert_record("glossary_alias",$newalias);
5fa0208e 262 }
a9637e7d 263 }
7965be79 264
a9637e7d
PS
265 if (!empty($data->catsincl)) {
266 // If the categories must be imported...
267 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
2b9ce62e 268 $sizeofxmlcats = sizeof($xmlcats);
f179cdb7 269 for($k = 0; $k < $sizeofxmlcats; $k++) {
a9637e7d
PS
270 $xmlcat = $xmlcats[$k];
271
39790bd8 272 $newcat = new stdClass();
a9637e7d
PS
273 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
274 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
275 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
276 // Create the category if it does not exist
39790bd8 277 $category = new stdClass();
a9637e7d
PS
278 $category->name = $newcat->name;
279 $category->glossaryid = $glossary->id;
280 $category->id = $DB->insert_record("glossary_categories",$category);
281 $importedcats++;
282 }
283 if ( $category ) {
284 // inserting the new relation
39790bd8 285 $entrycat = new stdClass();
a9637e7d
PS
286 $entrycat->entryid = $newentry->id;
287 $entrycat->categoryid = $category->id;
288 $DB->insert_record("glossary_entries_categories",$entrycat);
748b1932 289 }
748b1932 290 }
5fa0208e 291 }
292 } else {
293 $entriesrejected++;
294 if ( $newentry->concept and $newentry->definition ) {
295 // add to exception report (duplicated entry))
296 $rejections .= "<tr><td>$newentry->concept</td>" .
297 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
298 } else {
299 // add to exception report (no concept or definition found))
300 $rejections .= "<tr><td>---</td>" .
301 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
748b1932 302 }
303 }
5fa0208e 304 }
305 // processed entries
306 echo $OUTPUT->box_start('glossarydisplay generalbox');
307 echo '<table class="glossaryimportexport">';
308 echo '<tr>';
309 echo '<td width="50%" align="right">';
310 echo get_string("totalentries","glossary");
311 echo ':</td>';
312 echo '<td width="50%" align="left">';
313 echo $importedentries + $entriesrejected;
314 echo '</td>';
315 echo '</tr>';
316 echo '<tr>';
317 echo '<td width="50%" align="right">';
318 echo get_string("importedentries","glossary");
319 echo ':</td>';
320 echo '<td width="50%" align="left">';
321 echo $importedentries;
322 if ( $entriesrejected ) {
323 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
324 }
325 echo '</td>';
326 echo '</tr>';
ab16da67 327 if (!empty($data->catsincl)) {
73e6ac63 328 echo '<tr>';
41905731 329 echo '<td width="50%" align="right">';
5fa0208e 330 echo get_string("importedcategories","glossary");
73e6ac63 331 echo ':</td>';
5fa0208e 332 echo '<td width="50%">';
333 echo $importedcats;
73e6ac63 334 echo '</td>';
335 echo '</tr>';
5fa0208e 336 }
337 echo '</table><hr />';
748b1932 338
5fa0208e 339 // rejected entries
340 if ($rejections) {
b61e3e80 341 echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
5fa0208e 342 echo '<table class="glossaryimportexport">';
5fa0208e 343 echo $rejections;
344 echo '</table><hr />';
345 }
346/// Print continue button, based on results
347 if ($importedentries) {
348 echo $OUTPUT->continue_button('view.php?id='.$id);
748b1932 349 } else {
5fa0208e 350 echo $OUTPUT->continue_button('import.php?id='.$id);
748b1932 351 }
5fa0208e 352 echo $OUTPUT->box_end();
353} else {
b61e3e80
DC
354 echo $OUTPUT->box_start('glossarydisplay generalbox');
355 echo get_string('errorparsingxml', 'glossary');
356 echo $OUTPUT->continue_button('import.php?id='.$id);
357 echo $OUTPUT->box_end();
5fa0208e 358}
748b1932 359
748b1932 360/// Finish the page
5fa0208e 361echo $OUTPUT->footer();