3 require_once("../../config.php");
4 require_once("lib.php");
5 require_once("$CFG->dirroot/course/lib.php");
8 $id = required_param('id', PARAM_INT
); // Course Module ID
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?
15 $mode = optional_param('mode', 'letter', PARAM_ALPHA
);
16 $hook = optional_param('hook', 'ALL', PARAM_ALPHANUM
);
18 if (! $cm = get_coursemodule_from_id('glossary', $id)) {
19 error("Course Module ID was incorrect");
22 if (! $course = get_record("course", "id", $cm->course
)) {
23 error("Course is misconfigured");
26 if (! $glossary = get_record("glossary", "id", $cm->instance
)) {
27 error("Course module is incorrect");
30 require_login($course->id
, false, $cm);
32 $context = get_context_instance(CONTEXT_MODULE
, $cm->id
);
33 require_capability('mod/glossary:import', $context);
35 if ($dest != 'new' and $dest != 'current') {
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');
49 $navlinks[] = array('name' => $strglossaries, 'link' => "index.php?id=$course->id", 'type' => 'activity');
50 $navlinks[] = array('name' => format_string($glossary->name
), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
51 $navlinks[] = array('name' => $strimportentries, 'link' => '', 'type' => 'title');
53 $navigation = build_navigation($navlinks);
55 print_header_simple(format_string($glossary->name
), "", $navigation,
56 "", "", true, update_module_button($cm->id
, $course->id
, $strglossary),
57 navmenu($course, $cm));
59 print_heading($strimportentries);
62 print_box_start('glossarydisplay generalbox');
63 include("import.html");
66 print_footer($course);
70 $form = data_submitted();
71 $file = $_FILES["file"];
73 require_once($CFG->dirroot
.'/lib/uploadlib.php');
74 $um = new upload_manager('file',false,false,$course,false,0);
76 if (!$um->preprocess_files()) {
77 print_box_start('glossarydisplay generalbox');
78 print_continue('import.php?id='.$id);
85 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
92 // If the user chose to create a new glossary
93 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
95 if ( $xmlglossary['NAME'][0]['#'] ) {
97 $glossary->name
= addslashes($xmlglossary['NAME'][0]['#']);
98 $glossary->course
= $course->id
;
99 $glossary->globalglossary
= addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']);
100 $glossary->intro
= addslashes($xmlglossary['INTRO'][0]['#']);
101 $glossary->showspecial
= addslashes($xmlglossary['SHOWSPECIAL'][0]['#']);
102 $glossary->showalphabet
= addslashes($xmlglossary['SHOWALPHABET'][0]['#']);
103 $glossary->showall
= addslashes($xmlglossary['SHOWALL'][0]['#']);
104 $glossary->timecreated
= time();
105 $glossary->timemodified
= time();
107 // Setting the default values if no values were passed
108 if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
109 $glossary->entbypage
= addslashes($xmlglossary['ENTBYPAGE'][0]['#']);
111 $glossary->entbypage
= $CFG->glossary_entbypage
;
113 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
114 $glossary->allowduplicatedentries
= addslashes($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
116 $glossary->allowduplicatedentries
= $CFG->glossary_dupentries
;
118 if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
119 $glossary->displayformat
= addslashes($xmlglossary['DISPLAYFORMAT'][0]['#']);
121 $glossary->displayformat
= 2;
123 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
124 $glossary->allowcomments
= addslashes($xmlglossary['ALLOWCOMMENTS'][0]['#']);
126 $glossary->allowcomments
= $CFG->glossary_allowcomments
;
128 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
129 $glossary->usedynalink
= addslashes($xmlglossary['USEDYNALINK'][0]['#']);
131 $glossary->usedynalink
= $CFG->glossary_linkentries
;
133 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
134 $glossary->defaultapproval
= addslashes($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
136 $glossary->defaultapproval
= $CFG->glossary_defaultapproval
;
139 // Include new glossary and return the new ID
140 if ( !$glossary->id
= glossary_add_instance($glossary) ) {
141 notify("Error while trying to create the new glossary.");
143 glossary_print_tabbed_table_end();
144 print_footer($course);
147 //The instance has been created, so lets do course_modules
148 //and course_sections
149 $mod->groupmode
= $course->groupmode
; /// Default groupmode the same as course
151 $mod->instance
= $glossary->id
;
152 // course_modules and course_sections each contain a reference
153 // to each other, so we have to update one of them twice.
155 if (! $currmodule = get_record("modules", "name", 'glossary')) {
156 error("Glossary module doesn't exist");
158 $mod->module
= $currmodule->id
;
159 $mod->course
= $course->id
;
160 $mod->modulename
= 'glossary';
163 if (! $mod->coursemodule
= add_course_module($mod) ) {
164 error("Could not add a new course module");
167 if (! $sectionid = add_mod_to_section($mod) ) {
168 error("Could not add the new course module to that section");
170 //We get the section's visible field status
171 $visible = get_field("course_sections","visible","id",$sectionid);
173 if (! set_field("course_modules", "visible", $visible, "id", $mod->coursemodule
)) {
174 error("Could not update the course module with the correct visibility");
177 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule
)) {
178 error("Could not update the course module with the correct section");
180 add_to_log($course->id
, "course", "add mod",
181 "../mod/$mod->modulename/view.php?id=$mod->coursemodule",
182 "$mod->modulename $mod->instance");
183 add_to_log($course->id
, $mod->modulename
, "add",
184 "view.php?id=$mod->coursemodule",
185 "$mod->instance", $mod->coursemodule
);
187 rebuild_course_cache($course->id
);
189 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
193 notify("Error while trying to create the new glossary.");
194 print_footer($course);
199 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
200 for($i = 0; $i < sizeof($xmlentries); $i++
) {
201 // Inserting the entries
202 $xmlentry = $xmlentries[$i];
204 $newentry->concept
= trim(addslashes($xmlentry['#']['CONCEPT'][0]['#']));
205 $newentry->definition
= trusttext_strip(addslashes($xmlentry['#']['DEFINITION'][0]['#']));
206 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
207 $newentry->casesensitive
= addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']);
209 $newentry->casesensitive
= $CFG->glossary_casesensitive
;
212 $permissiongranted = 1;
213 if ( $newentry->concept
and $newentry->definition
) {
214 if ( !$glossary->allowduplicatedentries
) {
215 // checking if the entry is valid (checking if it is duplicated when should not be)
216 if ( $newentry->casesensitive
) {
217 $dupentry = get_record("glossary_entries","concept",$newentry->concept
,"glossaryid",$glossary->id
);
219 $dupentry = get_record("glossary_entries","lower(concept)",moodle_strtolower($newentry->concept
),"glossaryid",$glossary->id
);
222 $permissiongranted = 0;
226 $permissiongranted = 0;
228 if ($permissiongranted) {
229 $newentry->glossaryid
= $glossary->id
;
230 $newentry->sourceglossaryid
= 0;
231 $newentry->approved
= 1;
232 $newentry->userid
= $USER->id
;
233 $newentry->teacherentry
= 1;
234 $newentry->format
= addslashes($xmlentry['#']['FORMAT'][0]['#']);
235 $newentry->timecreated
= time();
236 $newentry->timemodified
= time();
238 // Setting the default values if no values were passed
239 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
240 $newentry->usedynalink
= addslashes($xmlentry['#']['USEDYNALINK'][0]['#']);
242 $newentry->usedynalink
= $CFG->glossary_linkentries
;
244 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
245 $newentry->fullmatch
= addslashes($xmlentry['#']['FULLMATCH'][0]['#']);
247 $newentry->fullmatch
= $CFG->glossary_fullmatch
;
250 if ( $newentry->id
= insert_record("glossary_entries",$newentry) ) {
253 $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
254 for($k = 0; $k < sizeof($xmlaliases); $k++
) {
255 /// Importing aliases
256 $xmlalias = $xmlaliases[$k];
257 $aliasname = $xmlalias['#']['NAME'][0]['#'];
259 if (!empty($aliasname)) {
261 $newalias->entryid
= $newentry->id
;
262 $newalias->alias
= trim(addslashes($aliasname));
263 $newalias->id
= insert_record("glossary_alias",$newalias);
268 // If the categories must be imported...
269 $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
270 for($k = 0; $k < sizeof($xmlcats); $k++
) {
271 $xmlcat = $xmlcats[$k];
274 $newcat->name
= addslashes($xmlcat['#']['NAME'][0]['#']);
275 $newcat->usedynalink
= addslashes($xmlcat['#']['USEDYNALINK'][0]['#']);
276 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id
,"name",$newcat->name
) ) {
277 // Create the category if it does not exist
279 $category->name
= $newcat->name
;
280 $category->glossaryid
= $glossary->id
;
281 if ( !$category->id
= insert_record("glossary_categories",$category)) {
282 // add to exception report (can't insert category)
283 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
284 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
290 // inserting the new relation
292 $entrycat->entryid
= $newentry->id
;
293 $entrycat->categoryid
= $category->id
;
294 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
295 // add to exception report (can't insert relation)
296 $rejections .= "<tr><td> <strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
297 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
304 // add to exception report (can't insert new record)
305 $rejections .= "<tr><td>$newentry->concept</td>" .
306 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
310 if ( $newentry->concept
and $newentry->definition
) {
311 // add to exception report (duplicated entry))
312 $rejections .= "<tr><td>$newentry->concept</td>" .
313 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
315 // add to exception report (no concept or definition found))
316 $rejections .= "<tr><td>---</td>" .
317 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
322 print_box_start('glossarydisplay generalbox');
323 echo '<table class="glossaryimportexport">';
325 echo '<td width="50%" align="right">';
326 echo get_string("totalentries","glossary");
328 echo '<td width="50%" align="left">';
329 echo $importedentries +
$entriesrejected;
333 echo '<td width="50%" align="right">';
334 echo get_string("importedentries","glossary");
336 echo '<td width="50%" align="left">';
337 echo $importedentries;
338 if ( $entriesrejected ) {
339 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
345 echo '<td width="50%" align="right">';
346 echo get_string("importedcategories","glossary");
348 echo '<td width="50%">';
353 echo '</table><hr />';
357 echo '<table class="glossaryimportexport">';
358 echo '<tr><td align="center" colspan="2" width="100%"><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
360 echo '</table><hr />';
362 /// Print continue button, based on results
363 if ($importedentries) {
364 print_continue('view.php?id='.$id);
366 print_continue('import.php?id='.$id);
370 notice(get_string('errorparsingxml', 'glossary'));
374 print_footer($course);