Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
require_once("../../config.php");
4
require_once("lib.php");
5
require_once("$CFG->dirroot/course/lib.php");
6
require_once("$CFG->dirroot/course/modlib.php");
7
require_once('import_form.php');
8
 
9
$id = required_param('id', PARAM_INT);    // Course Module ID
10
 
11
$mode     = optional_param('mode', 'letter', PARAM_ALPHA );
12
$hook     = optional_param('hook', 'ALL', PARAM_ALPHANUM);
13
 
14
$url = new moodle_url('/mod/glossary/import.php', array('id'=>$id));
15
if ($mode !== 'letter') {
16
    $url->param('mode', $mode);
17
}
18
if ($hook !== 'ALL') {
19
    $url->param('hook', $hook);
20
}
21
$PAGE->set_url($url);
22
 
23
if (! $cm = get_coursemodule_from_id('glossary', $id)) {
24
    throw new \moodle_exception('invalidcoursemodule');
25
}
26
 
27
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
28
    throw new \moodle_exception('coursemisconf');
29
}
30
 
31
if (! $glossary = $DB->get_record("glossary", array("id"=>$cm->instance))) {
32
    throw new \moodle_exception('invalidid', 'glossary');
33
}
34
 
35
require_login($course, false, $cm);
36
 
37
$context = context_module::instance($cm->id);
38
require_capability('mod/glossary:import', $context);
39
 
40
$strglossaries = get_string("modulenameplural", "glossary");
41
$strglossary = get_string("modulename", "glossary");
42
$strallcategories = get_string("allcategories", "glossary");
43
$straddentry = get_string("addentry", "glossary");
44
$strnoentries = get_string("noentries", "glossary");
45
$strsearchindefinition = get_string("searchindefinition", "glossary");
46
$strsearch = get_string("search");
47
$strimportentries = get_string('importentriesfromxml', 'glossary');
48
 
49
$PAGE->navbar->add($strimportentries);
50
$PAGE->set_title($glossary->name);
51
$PAGE->set_heading($course->fullname);
52
$PAGE->set_secondary_active_tab('modulepage');
53
$PAGE->activityheader->disable();
54
 
55
$form = new mod_glossary_import_form('');
56
if ($form->is_cancelled()) {
57
    redirect(new moodle_url('view.php', ['id' => $id]));
58
}
59
 
60
echo $OUTPUT->header();
61
echo $OUTPUT->heading($strimportentries);
62
 
63
if ( !$data = $form->get_data() ) {
64
    echo $OUTPUT->box_start('glossarydisplay generalbox');
65
    // display upload form
66
    $data = new stdClass();
67
    $data->id = $id;
68
    $form->set_data($data);
69
    $form->display();
70
    echo $OUTPUT->box_end();
71
    echo $OUTPUT->footer();
72
    exit;
73
}
74
 
75
$result = $form->get_file_content('file');
76
 
77
if (empty($result)) {
78
    echo $OUTPUT->box_start('glossarydisplay generalbox');
79
    echo $OUTPUT->continue_button('import.php?id='.$id);
80
    echo $OUTPUT->box_end();
81
    echo $OUTPUT->footer();
82
    die();
83
}
84
 
85
// Large exports are likely to take their time and memory.
86
core_php_time_limit::raise();
87
raise_memory_limit(MEMORY_EXTRA);
88
 
89
if ($xml = glossary_read_imported_file($result)) {
90
    $importedentries = 0;
91
    $importedcats    = 0;
92
    $entriesrejected = 0;
93
    $rejections      = '';
94
    $glossarycontext = $context;
95
 
96
    if ($data->dest == 'newglossary') {
97
        // If the user chose to create a new glossary
98
        $xmlglossary = $xml['GLOSSARY']['#']['INFO'][0]['#'];
99
 
100
        if ( $xmlglossary['NAME'][0]['#'] ) {
101
            $glossary = new stdClass();
102
            $glossary->modulename = 'glossary';
103
            $glossary->module = $cm->module;
104
            $glossary->name = ($xmlglossary['NAME'][0]['#']);
105
            $glossary->globalglossary = ($xmlglossary['GLOBALGLOSSARY'][0]['#']);
106
            $glossary->intro = ($xmlglossary['INTRO'][0]['#']);
107
            $glossary->introformat = isset($xmlglossary['INTROFORMAT'][0]['#']) ? $xmlglossary['INTROFORMAT'][0]['#'] : FORMAT_MOODLE;
108
            $glossary->showspecial = ($xmlglossary['SHOWSPECIAL'][0]['#']);
109
            $glossary->showalphabet = ($xmlglossary['SHOWALPHABET'][0]['#']);
110
            $glossary->showall = ($xmlglossary['SHOWALL'][0]['#']);
111
            $glossary->cmidnumber = null;
112
 
113
            // Setting the default values if no values were passed
114
            if ( isset($xmlglossary['ENTBYPAGE'][0]['#']) ) {
115
                $glossary->entbypage = ($xmlglossary['ENTBYPAGE'][0]['#']);
116
            } else {
117
                $glossary->entbypage = $CFG->glossary_entbypage;
118
            }
119
            if ( isset($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']) ) {
120
                $glossary->allowduplicatedentries = ($xmlglossary['ALLOWDUPLICATEDENTRIES'][0]['#']);
121
            } else {
122
                $glossary->allowduplicatedentries = $CFG->glossary_dupentries;
123
            }
124
            if ( isset($xmlglossary['DISPLAYFORMAT'][0]['#']) ) {
125
                $glossary->displayformat = ($xmlglossary['DISPLAYFORMAT'][0]['#']);
126
            } else {
127
                $glossary->displayformat = 2;
128
            }
129
            if ( isset($xmlglossary['ALLOWCOMMENTS'][0]['#']) ) {
130
                $glossary->allowcomments = ($xmlglossary['ALLOWCOMMENTS'][0]['#']);
131
            } else {
132
                $glossary->allowcomments = $CFG->glossary_allowcomments;
133
            }
134
            if ( isset($xmlglossary['USEDYNALINK'][0]['#']) ) {
135
                $glossary->usedynalink = ($xmlglossary['USEDYNALINK'][0]['#']);
136
            } else {
137
                $glossary->usedynalink = $CFG->glossary_linkentries;
138
            }
139
            if ( isset($xmlglossary['DEFAULTAPPROVAL'][0]['#']) ) {
140
                $glossary->defaultapproval = ($xmlglossary['DEFAULTAPPROVAL'][0]['#']);
141
            } else {
142
                $glossary->defaultapproval = $CFG->glossary_defaultapproval;
143
            }
144
 
145
            // These fields were not included in export, assume zero.
146
            $glossary->assessed = 0;
147
            $glossary->availability = null;
148
 
149
            // Check if we're creating the new glossary on the front page or inside a course.
150
            if ($cm->course == SITEID) {
151
                // On the front page, activities are in section 1.
152
                $glossary->section = 1;
153
            } else {
154
                // Inside a course, add to the general section (0).
155
                $glossary->section = 0;
156
            }
157
            // New glossary is always visible.
158
            $glossary->visible = 1;
159
            $glossary->visibleoncoursepage = 1;
160
 
161
            // Include new glossary and return the new ID
162
            if ( !($glossary = add_moduleinfo($glossary, $course)) ) {
163
                echo $OUTPUT->notification("Error while trying to create the new glossary.");
164
                glossary_print_tabbed_table_end();
165
                echo $OUTPUT->footer();
166
                exit;
167
            } else {
168
                $glossarycontext = context_module::instance($glossary->coursemodule);
169
                glossary_xml_import_files($xmlglossary, 'INTROFILES', $glossarycontext->id, 'intro', 0);
170
                echo $OUTPUT->box(get_string("newglossarycreated","glossary"),'generalbox boxaligncenter boxwidthnormal');
171
            }
172
        } else {
173
            echo $OUTPUT->notification("Error while trying to create the new glossary.");
174
            echo $OUTPUT->footer();
175
            exit;
176
        }
177
    }
178
 
179
    $xmlentries = $xml['GLOSSARY']['#']['INFO'][0]['#']['ENTRIES'][0]['#']['ENTRY'];
180
    $sizeofxmlentries = is_array($xmlentries) ? count($xmlentries) : 0;
181
    for($i = 0; $i < $sizeofxmlentries; $i++) {
182
        // Inserting the entries
183
        $xmlentry = $xmlentries[$i];
184
        $newentry = new stdClass();
185
        $newentry->concept = trim($xmlentry['#']['CONCEPT'][0]['#']);
186
        $definition = $xmlentry['#']['DEFINITION'][0]['#'];
187
        if (!is_string($definition)) {
188
            throw new \moodle_exception('errorparsingxml', 'glossary');
189
        }
190
        $newentry->definition = trusttext_strip($definition);
1441 ariadna 191
 
192
        if (isset($xmlentry['#']['DEFINITIONTRUST'][0]['#'])) {
193
            $newentry->definitiontrust = !empty($xmlentry['#']['DEFINITIONTRUST'][0]['#']) && trusttext_trusted($context);
194
        }
195
 
1 efrain 196
        if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
197
            $newentry->casesensitive = $xmlentry['#']['CASESENSITIVE'][0]['#'];
198
        } else {
199
            $newentry->casesensitive = $CFG->glossary_casesensitive;
200
        }
201
 
202
        $permissiongranted = 1;
203
        if ( $newentry->concept and $newentry->definition ) {
204
            if ( !$glossary->allowduplicatedentries ) {
205
                // checking if the entry is valid (checking if it is duplicated when should not be)
206
                if ( $newentry->casesensitive ) {
207
                    $dupentry = $DB->record_exists_select('glossary_entries',
208
                                    'glossaryid = :glossaryid AND concept = :concept', array(
209
                                        'glossaryid' => $glossary->id,
210
                                        'concept'    => $newentry->concept));
211
                } else {
212
                    $dupentry = $DB->record_exists_select('glossary_entries',
213
                                    'glossaryid = :glossaryid AND LOWER(concept) = :concept', array(
214
                                        'glossaryid' => $glossary->id,
215
                                        'concept'    => core_text::strtolower($newentry->concept)));
216
                }
217
                if ($dupentry) {
218
                    $permissiongranted = 0;
219
                }
220
            }
221
        } else {
222
            $permissiongranted = 0;
223
        }
224
        if ($permissiongranted) {
225
            $newentry->glossaryid       = $glossary->id;
226
            $newentry->sourceglossaryid = 0;
227
            $newentry->approved         = 1;
228
            $newentry->userid           = $USER->id;
229
            $newentry->teacherentry     = 1;
230
            $newentry->definitionformat = $xmlentry['#']['FORMAT'][0]['#'];
231
            $newentry->timecreated      = time();
232
            $newentry->timemodified     = time();
233
 
234
            // Setting the default values if no values were passed
235
            if ( isset($xmlentry['#']['USEDYNALINK'][0]['#']) ) {
236
                $newentry->usedynalink      = $xmlentry['#']['USEDYNALINK'][0]['#'];
237
            } else {
238
                $newentry->usedynalink      = $CFG->glossary_linkentries;
239
            }
240
            if ( isset($xmlentry['#']['FULLMATCH'][0]['#']) ) {
241
                $newentry->fullmatch        = $xmlentry['#']['FULLMATCH'][0]['#'];
242
            } else {
243
                $newentry->fullmatch      = $CFG->glossary_fullmatch;
244
            }
245
 
246
            $newentry->id = $DB->insert_record("glossary_entries",$newentry);
247
            $importedentries++;
248
 
249
            $xmlaliases = @$xmlentry['#']['ALIASES'][0]['#']['ALIAS']; // ignore missing ALIASES
250
            $sizeofxmlaliases = is_array($xmlaliases) ? count($xmlaliases) : 0;
251
            for($k = 0; $k < $sizeofxmlaliases; $k++) {
252
            /// Importing aliases
253
                $xmlalias = $xmlaliases[$k];
254
                $aliasname = $xmlalias['#']['NAME'][0]['#'];
255
 
256
                if (!empty($aliasname)) {
257
                    $newalias = new stdClass();
258
                    $newalias->entryid = $newentry->id;
259
                    $newalias->alias = trim($aliasname);
260
                    $newalias->id = $DB->insert_record("glossary_alias",$newalias);
261
                }
262
            }
263
 
264
            if (!empty($data->catsincl)) {
265
                // If the categories must be imported...
266
                $xmlcats = @$xmlentry['#']['CATEGORIES'][0]['#']['CATEGORY']; // ignore missing CATEGORIES
267
                $sizeofxmlcats = is_array($xmlcats) ? count($xmlcats) : 0;
268
                for($k = 0; $k < $sizeofxmlcats; $k++) {
269
                    $xmlcat = $xmlcats[$k];
270
 
271
                    $newcat = new stdClass();
272
                    $newcat->name = $xmlcat['#']['NAME'][0]['#'];
273
                    $newcat->usedynalink = $xmlcat['#']['USEDYNALINK'][0]['#'];
274
                    if ( !$category = $DB->get_record("glossary_categories", array("glossaryid"=>$glossary->id,"name"=>$newcat->name))) {
275
                        // Create the category if it does not exist
276
                        $category = new stdClass();
277
                        $category->name = $newcat->name;
278
                        $category->glossaryid = $glossary->id;
279
                        $category->id = $DB->insert_record("glossary_categories",$category);
280
                        $importedcats++;
281
                    }
282
                    if ( $category ) {
283
                        // inserting the new relation
284
                        $entrycat = new stdClass();
285
                        $entrycat->entryid    = $newentry->id;
286
                        $entrycat->categoryid = $category->id;
287
                        $DB->insert_record("glossary_entries_categories",$entrycat);
288
                    }
289
                }
290
            }
291
 
292
            // Import files embedded in the entry text.
293
            glossary_xml_import_files($xmlentry['#'], 'ENTRYFILES', $glossarycontext->id, 'entry', $newentry->id);
294
 
295
            // Import files attached to the entry.
296
            if (glossary_xml_import_files($xmlentry['#'], 'ATTACHMENTFILES', $glossarycontext->id, 'attachment', $newentry->id)) {
297
                $DB->update_record("glossary_entries", array('id' => $newentry->id, 'attachment' => '1'));
298
            }
299
 
300
            // Import tags associated with the entry.
301
            if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) {
302
                $xmltags = @$xmlentry['#']['TAGS'][0]['#']['TAG']; // Ignore missing TAGS.
303
                $sizeofxmltags = is_array($xmltags) ? count($xmltags) : 0;
304
                for ($k = 0; $k < $sizeofxmltags; $k++) {
305
                    // Importing tags.
306
                    $tag = $xmltags[$k]['#'];
307
                    if (!empty($tag)) {
308
                        core_tag_tag::add_item_tag('mod_glossary', 'glossary_entries', $newentry->id, $glossarycontext, $tag);
309
                    }
310
                }
311
            }
312
 
313
        } else {
314
            $entriesrejected++;
315
            if ( $newentry->concept and $newentry->definition ) {
316
                // add to exception report (duplicated entry))
317
                $rejections .= "<tr><td>$newentry->concept</td>" .
318
                               "<td>" . get_string("duplicateentry","glossary"). "</td></tr>";
319
            } else {
320
                // add to exception report (no concept or definition found))
321
                $rejections .= "<tr><td>---</td>" .
322
                               "<td>" . get_string("noconceptfound","glossary"). "</td></tr>";
323
            }
324
        }
325
    }
326
 
327
    // Reset caches.
328
    \mod_glossary\local\concept_cache::reset_glossary($glossary);
329
 
330
    // processed entries
331
    echo $OUTPUT->box_start('glossarydisplay generalbox');
1441 ariadna 332
    echo '<table class="glossaryimportexport table-reboot">';
1 efrain 333
    echo '<tr>';
334
    echo '<td width="50%" align="right">';
335
    echo get_string("totalentries","glossary");
336
    echo ':</td>';
337
    echo '<td width="50%" align="left">';
338
    echo $importedentries + $entriesrejected;
339
    echo '</td>';
340
    echo '</tr>';
341
    echo '<tr>';
342
    echo '<td width="50%" align="right">';
343
    echo get_string("importedentries","glossary");
344
    echo ':</td>';
345
    echo '<td width="50%" align="left">';
346
    echo $importedentries;
347
    if ( $entriesrejected ) {
348
        echo ' <small>(' . get_string("rejectedentries","glossary") . ": $entriesrejected)</small>";
349
    }
350
    echo '</td>';
351
    echo '</tr>';
352
    if (!empty($data->catsincl)) {
353
        echo '<tr>';
354
        echo '<td width="50%" align="right">';
355
        echo get_string("importedcategories","glossary");
356
        echo ':</td>';
357
        echo '<td width="50%">';
358
        echo $importedcats;
359
        echo '</td>';
360
        echo '</tr>';
361
    }
362
    echo '</table><hr />';
363
 
364
    // rejected entries
365
    if ($rejections) {
366
        echo $OUTPUT->heading(get_string("rejectionrpt","glossary"), 4);
1441 ariadna 367
        echo '<table class="glossaryimportexport table-reboot">';
1 efrain 368
        echo $rejections;
369
        echo '</table><hr />';
370
    }
371
/// Print continue button, based on results
372
    if ($importedentries) {
373
        echo $OUTPUT->continue_button('view.php?id='.$id);
374
    } else {
375
        echo $OUTPUT->continue_button('import.php?id='.$id);
376
    }
377
    echo $OUTPUT->box_end();
378
} else {
379
    echo $OUTPUT->box_start('glossarydisplay generalbox');
380
    echo get_string('errorparsingxml', 'glossary');
381
    echo $OUTPUT->continue_button('import.php?id='.$id);
382
    echo $OUTPUT->box_end();
383
}
384
 
385
/// Finish the page
386
echo $OUTPUT->footer();