Too many bugs, not enough time!
[moodle.git] / mod / glossary / import.php
bloba273d945795381eac5db29027225b97c97ee4661
1 <?php
3 require_once("../../config.php");
4 require_once("lib.php");
5 global $CFG, $USER;
7 require_variable($id); // Course Module ID
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?
14 optional_variable($mode,'letter');
15 optional_variable($hook,"ALL");
17 if (! $cm = get_record("course_modules", "id", $id)) {
18 error("Course Module ID was incorrect");
21 if (! $course = get_record("course", "id", $cm->course)) {
22 error("Course is misconfigured");
25 if (! $glossary = get_record("glossary", "id", $cm->instance)) {
26 error("Course module is incorrect");
29 require_login($course->id);
30 if (!isteacher($course->id)) {
31 error("You must be a teacher to use this page.");
34 if ($course->category) {
35 $navigation = "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->";
38 if ($dest != 'new' and $dest != 'current') {
39 $dest = 'current';
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");
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));
55 echo '<p align="center"><font size="3"><b>' . stripslashes_safe($glossary->name);
56 echo '</b></font></p>';
58 /// Info box
60 if ( $glossary->intro ) {
61 print_simple_box_start('center','70%');
62 echo format_text($glossary->intro);
63 print_simple_box_end();
66 /// Tabbed browsing sections
67 $tab = GLOSSARY_IMPORT_VIEW;
68 include("tabs.html");
70 if ( !$step ) {
71 include("import.html");
73 glossary_print_tabbed_table_end();
74 print_footer($course);
75 exit;
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']) ) {
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]['#'];
91 if ( $xmlglossary['NAME'][0]['#'] ) {
92 unset($glossary);
93 $glossary->name = addslashes(utf8_decode($xmlglossary['NAME'][0]['#']));
94 $glossary->course = $course->id;
95 $glossary->globalglossary = $xmlglossary['GLOBALGLOSSARY'][0]['#'];
96 $glossary->entbypage = $xmlglossary['ENTBYPAGE'][0]['#'];
97 $glossary->intro = addslashes(utf8_decode($xmlglossary['INTRO'][0]['#']));
98 $glossary->showspecial = $xmlglossary['SHOWSPECIAL'][0]['#'];
99 $glossary->showalphabet = $xmlglossary['SHOWALPHABET'][0]['#'];
100 $glossary->showall = $xmlglossary['SHOWALL'][0]['#'];
101 $glossary->timecreated = time();
102 $glossary->timemodified = time();
104 // Setting the default values if no values were passed
105 if ( isset($xmlglossary['STUDENTCANPOST'][0]['#']) ) {
106 $glossary->studentcanpost = $xmlglossary['STUDENTCANPOST'][0]['#'];
107 } else {
108 $glossary->studentcanpost = $CFG->cnfstudentcanpost;
110 if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
111 $glossary->allowduplicatedentries = $xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#'];
112 } else {
113 $glossary->allowduplicatedentries = $CFG->cnfallowdupentries;
115 if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
116 $glossary->allowcomments = $xmlglossary['ALLOWCOMMENTS'][0]['#'];
117 } else {
118 $glossary->allowcomments = $CFG->cnfallowcomments;
120 if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
121 $glossary->usedynalink = $xmlglossary['USEDYNALINK'][0]['#'];
122 } else {
123 $glossary->usedynalink = $CFG->cnflinkglossaries;
125 if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
126 $glossary->defaultapproval = $xmlglossary['DEFAULTAPPROVAL'][0]['#'];
127 } else {
128 $glossary->defaultapproval = $CFG->cnfapprovalstatus;
131 // Include new glossary and return the new ID
132 if ( !$glossary->id = glossary_add_instance($glossary) ) {
133 notify("Error while trying to create the new glossary.");
134 glossary_print_tabbed_table_end();
135 print_footer($course);
136 exit;
137 } else {
138 print_simple_box(get_string("newglossarycreated","glossary"),"center","70%");
139 echo '<p>';
141 } else {
142 notify("Error while trying to create the new glossary.");
143 glossary_print_tabbed_table_end();
144 print_footer($course);
145 exit;
149 $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
150 for($i = 0; $i < sizeof($xmlentries); $i++) {
151 // Inserting the entries
152 $xmlentry = $xmlentries[$i];
154 unset($newentry);
155 $newentry->concept = addslashes(utf8_decode($xmlentry['#']['CONCEPT'][0]['#']));
156 $newentry->definition = addslashes(utf8_decode($xmlentry['#']['DEFINITION'][0]['#']));
158 $permissiongranted = 1;
159 if ( $newentry->concept and $newentry->definition ) {
160 if ( !$glossary->allowduplicatedentries ) {
161 // checking if the entry is valid (checking if it is duplicated when should not be)
162 if ( $glossary->casesensitive ) {
163 $dupentry = get_record("glossary_entries","concept",$newentry->concept,"glossaryid",$glossary->id);
164 } else {
165 $dupentry = get_record("glossary_entries","ucase(concept)",strtoupper($newentry->concept),"glossaryid",$glossary->id);
167 if ($dupentry) {
168 $permissiongranted = 0;
171 } else {
172 $permissiongranted = 0;
174 if ($permissiongranted) {
175 $newentry->glossaryid = $glossary->id;
176 $newentry->sourceglossaryid = 0;
177 $newentry->approved = 1;
178 $newentry->userid = $USER->id;
179 $newentry->format = $xmlentry['#']['FORMAT'][0]['#'];
180 $newentry->timecreated = time();
181 $newentry->timemodified = time();
182 $newentry->teacherentry = $xmlentry['#']['TEACHERENTRY'][0]['#'];
184 // Setting the default values if no values were passed
185 if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
186 $newentry->usedynalink = $xmlentry['#']['USEDYNALINK'][0]['#'];
187 } else {
188 $newentry->usedynalink = $CFG->cnfusedynalink;
190 if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
191 $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
192 } else {
193 $newentry->casesensitive = $CFG->cnfcasesensitive;
195 if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
196 $newentry->fullmatch = $xmlentry['#']['FULLMATCH'][0]['#'];
197 } else {
198 $newentry->fullmatch = $CFG->cnffullmatch;
201 if ( $newentry->id = insert_record("glossary_entries",$newentry) ) {
202 $importedentries++;
204 $xmlaliases = $xmlentry['#']['ALIASES'][0]['#']['ALIAS'];
205 for($k = 0; $k < sizeof($xmlaliases); $k++) {
206 /// Importing aliases
207 $xmlalias = $xmlaliases[$k];
208 unset($newalias);
210 $newalias->entryid = $newentry->id;
211 $newalias->alias = $xmlalias['#']['NAME'][0]['#'];
212 $newalias->id = insert_record("glossary_alias",$newalias);
215 if ( $catsincl ) {
216 // If the categories must be imported...
217 $xmlcats = $xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY'];
218 for($k = 0; $k < sizeof($xmlcats); $k++) {
219 $xmlcat = $xmlcats[$k];
220 unset($newcat);
222 $newcat->name = $xmlcat['#']['NAME'][0]['#'];
223 $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
224 if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
225 // Create the category if it does not exist
226 unset($category);
227 $category->name = $newcat->name;
228 $category->glossaryid = $glossary->id;
229 if ( !$category->id = insert_record("glossary_categories",$category)) {
230 // add to exception report (can't insert category)
231 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
232 "<td>" . get_string("cantinsertcat","glossary"). "</td></tr>";
233 } else {
234 $importedcats++;
237 if ( $category ) {
238 // inserting the new relation
239 unset($entrycat);
240 $entrycat->entryid = $newentry->id;
241 $entrycat->categoryid = $category->id;
242 if ( !insert_record("glossary_entries_categories",$entrycat) ) {
243 // add to exception report (can't insert relation)
244 $rejections .= "<tr><td>&nbsp;<strong>" . get_string("category","glossary") . ":</strong>$newcat->name</td>" .
245 "<td>" . get_string("cantinsertrel","glossary"). "</td></tr>";
250 } else {
251 $entriesrejected++;
252 // add to exception report (can't insert new record)
253 $rejections .= "<tr><td>$newentry->concept</td>" .
254 "<td>" . get_string("cantinsertrec","glossary"). "</td></tr>";
256 } else {
257 $entriesrejected++;
258 if ( $newentry->concept and $newentry->definition ) {
259 // add to exception report (duplicated entry))
260 $rejections .= "<tr><td>$newentry->concept</td>" .
261 "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
262 } else {
263 // add to exception report (no concept or definition found))
264 $rejections .= "<tr><td>---</td>" .
265 "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
269 // processed entries
270 echo '<table border=0 width=100%>';
271 echo '<tr>';
272 echo '<td width=50% align=right>';
273 echo get_string("totalentries","glossary");
274 echo ':</td>';
275 echo '<td width=50%>';
276 echo $importedentries + $entriesrejected;
277 echo '</td>';
278 echo '</tr>';
279 echo '<tr>';
280 echo '<td width=50% align=right>';
281 echo get_string("importedentries","glossary");
282 echo ':</td>';
283 echo '<td width=50%>';
284 echo $importedentries;
285 if ( $entriesrejected ) {
286 echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
288 echo '</td>';
289 echo '</tr>';
290 if ( $catsincl ) {
291 echo '<tr>';
292 echo '<td width=50% align=right>';
293 echo get_string("importedcategories","glossary");
294 echo ':</td>';
295 echo '<td width=50%>';
296 echo $importedcats;
297 echo '</td>';
298 echo '</tr>';
300 echo '</table><hr width=75%>';
302 // rejected entries
303 if ($rejections) {
304 echo '<center><table border=0 width=70%>';
305 echo '<tr><td align=center colspan=2 width=100%><strong>' . get_string("rejectionrpt","glossary") . '</strong></tr>';
306 echo $rejections;
307 echo '</table></center><p><hr width=75%>';
309 } else {
310 notify("Error while trying to read the file.");
313 glossary_print_tabbed_table_end();
315 /// Finish the page
316 print_footer($course);