3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
7 $id = required_param('id', PARAM_INT); // Course Module ID
9 $step = optional_param('step', 0, PARAM_INT);
10 $dest = optional_param('dest', 'current', PARAM_ALPHA); // current | new
11 $file = optional_param('file', '', PARAM_FILE); // file to import
12 $catsincl = optional_param('catsincl', 0, PARAM_INT); // Import Categories too?
14 $mode = optional_param('mode', 'letter', PARAM_ALPHA );
15 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM);
17 $url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
19 $url->param('step', $step);
21 if ($dest !== 'current') {
22 $url->param('dest', $dest);
25 $url->param('file', $file);
27 if ($catsincl !== 0) {
28 $url->param('catsincl', $catsincl);
30 if ($mode !== 'letter') {
31 $url->param('mode', $mode);
33 if ($hook !== 'ALL') {
34 $url->param('hook', $hook);
38 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
39 print_error('invalidcoursemodule');
42 if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
43 print_error('coursemisconf');
46 if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
47 print_error('invalidid', 'glossary');
50 require_login($course->id, false, $cm);
52 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
53 require_capability('mod/glossary:import', $context);
55 if ($dest != 'new' and $dest != 'current') {
58 $strglossaries = get_string("modulenameplural", "glossary");
59 $strglossary = get_string("modulename", "glossary");
60 $strallcategories = get_string("allcategories", "glossary");
61 $straddentry = get_string("addentry", "glossary");
62 $strnoentries = get_string("noentries", "glossary");
63 $strsearchconcept = get_string("searchconcept", "glossary");
64 $strsearchindefinition = get_string("searchindefinition", "glossary");
65 $strsearch = get_string("search");
66 $strimportentries = get_string('importentriesfromxml', 'glossary');
68 $PAGE->set_url('/mod/glossary/import.php', array('id'=>$cm->id, 'mode'=>$mode, 'hook'=>$hook));
69 $PAGE->navbar->add($strimportentries);
70 $PAGE->set_title(format_string($glossary->name));
72 echo $OUTPUT->header();
73 echo $OUTPUT->heading($strimportentries);
76 echo $OUTPUT->box_start('glossarydisplay generalbox');
77 include("import.html");
78 echo $OUTPUT->box_end();
80 echo $OUTPUT->footer();
85 $form = data_submitted();
86 $file = $_FILES["file"];
88 require_once($CFG->dirroot.'/lib/uploadlib.php');
89 $um = new upload_manager('file',false,false,$course,false,0);
91 if (!$um->preprocess_files()) {
92 echo $OUTPUT->box_start('glossarydisplay generalbox');
93 echo $OUTPUT->continue_button('import.php?id='.$id);
94 echo $OUTPUT->box_end();
96 echo $OUTPUT->footer();
100 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
102 $importedentries = 0;
104 $entriesrejected = 0;
106 if ($dest == 'new') {
107 // If the user chose to create a new glossary
108 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
110 if ( $xmlglossary['NAME'][0]['#'] ) {
112 $glossary->name = ($xmlglossary['NAME'][0]['#']);
113 $glossary->course = $course->id;
114 $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
115 $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
116 $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
117 $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
118 $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
119 $glossary->timecreated = time();
120 $glossary->timemodified = time();
122 // Setting the default values if no values were passed
123 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
124 $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
126 $glossary->entbypage = $CFG->glossary_entbypage;
128 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
129 $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
131 $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
133 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
134 $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
136 $glossary->displayformat = 2;
138 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
139 $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
141 $glossary->allowcomments = $CFG->glossary_allowcomments;
143 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
144 $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
146 $glossary->usedynalink = $CFG->glossary_linkentries;
148 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
149 $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
151 $glossary->defaultapproval = $CFG->glossary_defaultapproval;
154 // Include new glossary and return the new ID
155 if ( !$glossary->id = glossary_add_instance($glossary) ) {
156 echo $OUTPUT->notification("Error while trying to create the new glossary.");
158 glossary_print_tabbed_table_end();
159 echo $OUTPUT->footer();
162 //The instance has been created, so lets do course_modules
163 //and course_sections
164 $mod->groupmode = $course->groupmode; /// Default groupmode the same as course
166 $mod->instance = $glossary->id;
167 // course_modules and course_sections each contain a reference
168 // to each other, so we have to update one of them twice.
170 if (! $currmodule = $DB->get_record("modules", array("name"=>'glossary'))) {
171 print_error('modulenotexist', 'debug', '', 'Glossary');
173 $mod->module = $currmodule->id;
174 $mod->course = $course->id;
175 $mod->modulename = 'glossary';
178 if (! $mod->coursemodule = add_course_module($mod) ) {
179 print_error('cannotaddcoursemodule');
182 if (! $sectionid = add_mod_to_section($mod) ) {
183 print_error('cannotaddcoursemoduletosection');
185 //We get the section's visible field status
186 $visible = $DB->get_field("course_sections", "visible", array("id"=>$sectionid));
188 $DB->set_field("course_modules", "visible", $visible, array("id"=>$mod->coursemodule));
189 $DB->set_field("course_modules", "section", $sectionid, array("id"=>$mod->coursemodule));
191 add_to_log($course->id, "course", "add mod",
192 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
193 "$mod->modulename $mod->instance");
194 add_to_log($course->id, $mod->modulename, "add",
195 "view.php?id=$mod->coursemodule",
196 "$mod->instance", $mod->coursemodule);
198 rebuild_course_cache($course->id);
200 echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
204 echo $OUTPUT->notification("Error while trying to create the new glossary.");
205 echo $OUTPUT->footer();
210 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
211 for($i = 0; $i < sizeof($xmlentries); $i++) {
212 // Inserting the entries
213 $xmlentry = $xmlentries[$i];
215 $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
216 $newentry->definition = trusttext_strip($xmlentry['#']['DEFINITION'][0]['#']);
217 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
218 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
220 $newentry->casesensitive = $CFG->glossary_casesensitive;
223 $permissiongranted = 1;
224 if ( $newentry->concept and $newentry->definition ) {
225 if ( !$glossary->allowduplicatedentries ) {
226 // checking if the entry is valid (checking if it is duplicated when should not be)
227 if ( $newentry->casesensitive ) {
228 $dupentry = $DB->get_record("glossary_entries", array("concept"=>$newentry->concept, "glossaryid"=>$glossary->id));
230 $dupentry = $DB->get_record("glossary_entries", array("lower(concept)"=>moodle_strtolower($newentry->concept), "glossaryid"=>$glossary->id));
233 $permissiongranted = 0;
237 $permissiongranted = 0;
239 if ($permissiongranted) {
240 $newentry->glossaryid = $glossary->id;
241 $newentry->sourceglossaryid = 0;
242 $newentry->approved = 1;
243 $newentry->userid = $USER->id;
244 $newentry->teacherentry = 1;
245 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
246 $newentry->timecreated = time();
247 $newentry->timemodified = time();
249 // Setting the default values if no values were passed
250 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
251 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
253 $newentry->usedynalink = $CFG->glossary_linkentries;
255 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
256 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
258 $newentry->fullmatch = $CFG->glossary_fullmatch;
261 if ( $newentry->id = $DB->insert_record("glossary_entries",$newentry) ) {
264 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
265 for($k = 0; $k < sizeof($xmlaliases); $k++) {
266 /// Importing aliases
267 $xmlalias = $xmlaliases[$k];
268 $aliasname = $xmlalias['#']['NAME'][0]['#'];
270 if (!empty($aliasname)) {
271 $newalias = new object();
272 $newalias->entryid = $newentry->id;
273 $newalias->alias = trim($aliasname);
274 $newalias->id = $DB->insert_record("glossary_alias",$newalias);
279 // If the categories must be imported...
280 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
281 for($k = 0; $k < sizeof($xmlcats); $k++) {
282 $xmlcat = $xmlcats[$k];
284 $newcat = new object();
285 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
286 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
287 if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
288 // Create the category if it does not exist
289 $category = new object();
290 $category->name = $newcat->name;
291 $category->glossaryid = $glossary->id;
292 $category->id = $DB->insert_record("glossary_categories",$category);
296 // inserting the new relation
297 $entrycat = new object();
298 $entrycat->entryid = $newentry->id;
299 $entrycat->categoryid = $category->id;
300 $DB->insert_record("glossary_entries_categories",$entrycat);
306 // add to exception report (can't insert new record)
307 $rejections .= "<tr><td>$newentry->concept</td>" .
308 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
312 if ( $newentry->concept and $newentry->definition ) {
313 // add to exception report (duplicated entry))
314 $rejections .= "<tr><td>$newentry->concept</td>" .
315 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
317 // add to exception report (no concept or definition found))
318 $rejections .= "<tr><td>---</td>" .
319 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
324 echo $OUTPUT->box_start('glossarydisplay generalbox');
325 echo '<table class="glossaryimportexport">';
327 echo '<td width="50%" align="right">';
328 echo get_string("totalentries","glossary");
330 echo '<td width="50%" align="left">';
331 echo $importedentries + $entriesrejected;
335 echo '<td width="50%" align="right">';
336 echo get_string("importedentries","glossary");
338 echo '<td width="50%" align="left">';
339 echo $importedentries;
340 if ( $entriesrejected ) {
341 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
347 echo '<td width="50%" align="right">';
348 echo get_string("importedcategories","glossary");
350 echo '<td width="50%">';
355 echo '</table><hr />';
359 echo '<table class="glossaryimportexport">';
360 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
362 echo '</table><hr />';
364 /// Print continue button, based on results
365 if ($importedentries) {
366 echo $OUTPUT->continue_button('view.php?id='.$id);
368 echo $OUTPUT->continue_button('import.php?id='.$id);
370 echo $OUTPUT->box_end();
372 notice(get_string('errorparsingxml', 'glossary'));
376 echo $OUTPUT->footer();