- Basic export/import functionality.. Does not export nor import the attached files.
[moodle.git] / mod / glossary / import.php
CommitLineData
748b1932 1<?php
2
3 require_once("../../config.php");
4 require_once("lib.php");
5 global $CFG, $USER;
6
7 require_variable($id); // Course Module ID
8
9 optional_variable($step,0);
10 optional_variable($dest,"current"); // current | new
11 optional_variable($file); // file to import
12 optional_variable($catsincl,0); // Import Categories too?
13
14 optional_variable($lasttab,GLOSSARY_STANDARD_VIEW);
15 optional_variable($l,"ALL");
16
17 if (! $cm = get_record("course_modules", "id", $id)) {
18 error("Course Module ID was incorrect");
19 }
20
21 if (! $course = get_record("course", "id", $cm->course)) {
22 error("Course is misconfigured");
23 }
24
25 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
26 error("Course module is incorrect");
27 }
28
29 require_login($course->id);
30 if (!isteacher($course->id)) {
31 error("You must be a teacher to use this page.");
32 }
33
34 if ($course->category) {
35 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
36 }
37
38 if ($dest != 'new' and $dest != 'current') {
39 $dest = 'current';
40 }
41 $strglossaries = get_string("modulenameplural", "glossary");
42 $strglossary = get_string("modulename", "glossary");
43 $strallcategories = get_string("allcategories", "glossary");
44 $straddentry = get_string("addentry", "glossary");
45 $strnoentries = get_string("noentries", "glossary");
46 $strsearchconcept = get_string("searchconcept", "glossary");
47 $strsearchindefinition = get_string("searchindefinition", "glossary");
48 $strsearch = get_string("search");
49
50 print_header(strip_tags("$course->shortname: $glossary->name"), "$course->fullname",
51 "$navigation <A HREF=index.php?id=$course->id>$strglossaries</A> -> $glossary->name",
52 "", "", true, update_module_button($cm->id, $course->id, $strglossary),
53 navmenu($course, $cm));
54
55 echo '<p align="center"><font size="3"><b>' . stripslashes_safe($glossary->name);
56 echo '</b></font></p>';
57
58/// Info box
59
60 if ( $glossary->intro ) {
61 print_simple_box_start('center','70%');
62 echo format_text($glossary->intro);
63 print_simple_box_end();
64 }
65
66/// Tabbed browsing sections
67 $tab = GLOSSARY_IMPORT_VIEW;
68 include("tabs.html");
69
70 if ( !$step ) {
71 include("import.html");
72
73 glossary_print_tabbed_table_end();
74 print_footer($course);
75 exit;
76 }
77
78 $form = data_submitted();
79 $file = $_FILES["file"];
80// if ($xml = glossary_read_imported_file("C:\\download\\moodle\\moodle\\cursos\\10\\glossary\\Testing_Glossary\\glossary.xml") ) {
81 if ($xml = glossary_read_imported_file($file['tmp_name']) ) {
82
83 $importedentries = 0;
84 $importedcats = 0;
85 $entriesrejected = 0;
86 $rejections = '';
87 if ($dest == 'new') {
88 // If the user chose to create a new glossary
89 $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
90
91 if ( $xmlglossary['NAME'][0]['#'] ) {
92 unset($glossary);
93 $glossary->name = $xmlglossary['NAME'][0]['#'];
94 $glossary->course = $course->id;
95 $glossary->globalglossary = $xmlglossary['GLOBALGLOSSARY'][0]['#'];
96 $glossary->intro = $xmlglossary['INTRO'][0]['#'];
97 $glossary->showspecial = $xmlglossary['SHOWSPECIAL'][0]['#'];
98 $glossary->showalphabet = $xmlglossary['SHOWALPHABET'][0]['#'];
99 $glossary->showall = $xmlglossary['SHOWALL'][0]['#'];
100 $glossary->timecreated = time();
101 $glossary->timemodified = time();
102
103 // Setting the default values if no values were passed
104 if ( isset($xmlglossary['STUDENTCANPOST'][0]['#']) ) {
105 $glossary->studentcanpost = $xmlglossary['STUDENTCANPOST'][0]['#'];
106 } else {
107 $glossary->studentcanpost = $CFG->cnfstudentcanpost;
108 }
109 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
110 $glossary->allowduplicatedentries = $xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#'];
111 } else {
112 $glossary->allowduplicatedentries = $CFG->cnfallowdupentries;
113 }
114 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
115 $glossary->allowcomments = $xmlglossary['ALLOWCOMMENTS'][0]['#'];
116 } else {
117 $glossary->allowcomments = $CFG->cnfallowcomments;
118 }
119 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
120 $glossary->usedynalink = $xmlglossary['USEDYNALINK'][0]['#'];
121 } else {
122 $glossary->usedynalink = $CFG->cnflinkglossaries;
123 }
124 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
125 $glossary->defaultapproval = $xmlglossary['DEFAULTAPPROVAL'][0]['#'];
126 } else {
127 $glossary->defaultapproval = $CFG->cnfapprovalstatus;
128 }
129
130 // Include new glossary and return the new ID
131 if ( !$glossary->id = glossary_add_instance($glossary) ) {
132 notify("Error while trying to create the new glossary.");
133 glossary_print_tabbed_table_end();
134 print_footer($course);
135 exit;
136 } else {
137 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
138 echo '<p>';
139 }
140 } else {
141 notify("Error while trying to create the new glossary.");
142 glossary_print_tabbed_table_end();
143 print_footer($course);
144 exit;
145 }
146 }
147
148 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
149 for($i = 0; $i < sizeof($xmlentries); $i++) {
150 // Inserting the entries
151 $xmlentry = $xmlentries[$i];
152
153 unset($newentry);
154 $newentry->concept = $xmlentry['#']['CONCEPT'][0]['#'];
155 $newentry->definition = $xmlentry['#']['DEFINITION'][0]['#'];
156
157 $permissiongranted = 1;
158 if ( $newentry->concept and $newentry->definition ) {
159 if ( !$glossary->allowduplicatedentries ) {
160 // checking if the entry is valid (checking if it is duplicated when should not be)
161 if ( $glossary->casesensitive ) {
162 $dupentry = get_record("glossary_entries","concept",$newentry->concept,"glossaryid",$glossary->id);
163 } else {
164 $dupentry = get_record("glossary_entries","ucase(concept)",strtoupper($newentry->concept),"glossaryid",$glossary->id);
165 }
166 if ($dupentry) {
167 $permissiongranted = 0;
168 }
169 }
170 } else {
171 $permissiongranted = 0;
172 }
173 if ($permissiongranted) {
174 $newentry->glossaryid = $glossary->id;
175 $newentry->sourceglossaryid = 0;
176 $newentry->approved = 1;
177 $newentry->userid = $USER->id;
178 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
179 $newentry->timecreated = time();
180 $newentry->timemodified = time();
181 $newentry->teacherentry = $xmlentry['#']['TEACHERENTRY'][0]['#'];
182
183 // Setting the default values if no values were passed
184 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
185 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
186 } else {
187 $newentry->usedynalink = $CFG->cnfusedynalink;
188 }
189 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
190 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
191 } else {
192 $newentry->casesensitive = $CFG->cnfcasesensitive;
193 }
194 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
195 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
196 } else {
197 $newentry->fullmatch = $CFG->cnffullmatch;
198 }
199
200 if ( $newentry->id = insert_record("glossary_entries",$newentry) ) {
201 $importedentries++;
202 if ( $catsincl ) {
203 // If the categories must be imported...
204 $xmlcats = $xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY'];
205 for($k = 0; $k < sizeof($xmlcats); $k++) {
206 $xmlcat = $xmlcats[$k];
207 unset($newcat);
208
209 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
210 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
211 // Create the category if it does not exist
212 unset($category);
213 $category->name = $newcat->name;
214 $category->glossaryid = $glossary->id;
215 if ( !$category->id = insert_record("glossary_categories",$category)) {
216 // add to exception report (can't insert category)
217 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
218 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
219 } else {
220 $importedcats++;
221 }
222 }
223 if ( $category ) {
224 // inserting the new relation
225 unset($entrycat);
226 $entrycat->entryid = $newentry->id;
227 $entrycat->categoryid = $category->id;
228 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
229 // add to exception report (can't insert relation)
230 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
231 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
232 }
233 }
234 }
235 }
236 } else {
237 $entriesrejected++;
238 // add to exception report (can't insert new record)
239 $rejections .= "<tr><td>$newentry->concept</td>" .
240 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
241 }
242 } else {
243 $entriesrejected++;
244 if ( $newentry->concept and $newentry->definition ) {
245 // add to exception report (duplicated entry))
246 $rejections .= "<tr><td>$newentry->concept</td>" .
247 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
248 } else {
249 // add to exception report (no concept or definition found))
250 $rejections .= "<tr><td>---</td>" .
251 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
252 }
253 }
254 }
255 // processed entries
256 echo '<table border=0 width=100%>';
257 echo '<tr>';
258 echo '<td width=50% align=right>';
259 echo get_string("totalentries","glossary");
260 echo ':</td>';
261 echo '<td width=50%>';
262 echo $importedentries + $entriesrejected;
263 echo '</td>';
264 echo '</tr>';
265 echo '<tr>';
266 echo '<td width=50% align=right>';
267 echo get_string("importedentries","glossary");
268 echo ':</td>';
269 echo '<td width=50%>';
270 echo $importedentries;
271 if ( $entriesrejected ) {
272 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
273 }
274 echo '</td>';
275 echo '</tr>';
276 if ( $catsincl ) {
277 echo '<tr>';
278 echo '<td width=50% align=right>';
279 echo get_string("importedcategories","glossary");
280 echo ':</td>';
281 echo '<td width=50%>';
282 echo $importedcats;
283 echo '</td>';
284 echo '</tr>';
285 }
286 echo '</table><hr width=75%>';
287
288 // rejected entries
289 if ($rejections) {
290 echo '<center><table border=0 width=70%>';
291 echo '<tr><td align=center colspan=2 width=100%><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
292 echo $rejections;
293 echo '</table></center><p><hr width=75%>';
294 }
295 } else {
296 notify("Error while trying to read the file.");
297 }
298
299 glossary_print_tabbed_table_end();
300
301/// Finish the page
302 print_footer($course);
303
304?>