1<?php
2define('AJAX_SCRIPT', true);
3
4require_once('../../config.php');
5require_once('lib.php');
6require_once($CFG->libdir . '/filelib.php');
7
8$concept  = optional_param('concept', '', PARAM_CLEAN);
9$courseid = optional_param('courseid', 0, PARAM_INT);
10$eid      = optional_param('eid', 0, PARAM_INT); // glossary entry id
11$displayformat = optional_param('displayformat',-1, PARAM_SAFEDIR);
12
13$url = new moodle_url('/mod/glossary/showentry.php');
14$url->param('concept', $concept);
15$url->param('courseid', $courseid);
16$url->param('eid', $eid);
17$url->param('displayformat', $displayformat);
18$PAGE->set_url($url);
19
20if ($CFG->forcelogin) {
21    require_login();
22}
23
24if ($eid) {
25    $entry = $DB->get_record('glossary_entries', array('id'=>$eid), '*', MUST_EXIST);
26    $glossary = $DB->get_record('glossary', array('id'=>$entry->glossaryid), '*', MUST_EXIST);
27    $cm = get_coursemodule_from_instance('glossary', $glossary->id, 0, false, MUST_EXIST);
28    $course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
29    require_course_login($course, true, $cm);
30    $entry->glossaryname = $glossary->name;
31    $entry->cmid = $cm->id;
32    $entry->courseid = $cm->course;
33    $entries = array($entry);
34
35} else if ($concept) {
36    $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
37    require_course_login($course);
38    $entries = glossary_get_entries_search($concept, $courseid);
39
40} else {
41    print_error('invalidelementid');
42}
43
44if ($entries) {
45    foreach ($entries as $key => $entry) {
46        // Need to get the course where the entry is,
47        // in order to check for visibility/approve permissions there
48        $entrycourse = $DB->get_record('course', array('id' => $entry->courseid), '*', MUST_EXIST);
49        $modinfo = get_fast_modinfo($entrycourse);
50        // make sure the entry is visible
51        if (empty($modinfo->cms[$entry->cmid]->uservisible)) {
52            unset($entries[$key]);
53            continue;
54        }
55        // make sure the entry is approved (or approvable by current user)
56        if (!$entry->approved and ($USER->id != $entry->userid)) {
57            $context = context_module::instance($entry->cmid);
58            if (!has_capability('mod/glossary:approve', $context)) {
59                unset($entries[$key]);
60                continue;
61            }
62        }
63
64        // Make sure entry is not autolinking itself.
65        $GLOSSARY_EXCLUDEENTRY = $entry->id;
66
67        $context = context_module::instance($entry->cmid);
68        $definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $context->id, 'mod_glossary', 'entry', $entry->id);
69
70        $options = new stdClass();
71        $options->para = false;
72        $options->trusted = $entry->definitiontrust;
73        $options->context = $context;
74        $entries[$key]->definition = format_text($definition, $entry->definitionformat, $options);
75
76        if (core_tag_tag::is_enabled('mod_glossary', 'glossary_entries')) {
77            $entries[$key]->definition .= $OUTPUT->tag_list(
78                core_tag_tag::get_item_tags('mod_glossary', 'glossary_entries', $entry->id), null, 'glossary-tags');
79        }
80
81        $entries[$key]->attachments = '';
82        if (!empty($entries[$key]->attachment)) {
83            $attachments = glossary_print_attachments($entry, $cm, 'html');
84            $entries[$key]->attachments = html_writer::tag('p', $attachments);
85        }
86
87        $entries[$key]->footer = "<p style=\"text-align:right\">&raquo;&nbsp;<a href=\"$CFG->wwwroot/mod/glossary/view.php?g=$entry->glossaryid\">".format_string($entry->glossaryname,true)."</a></p>";
88        glossary_entry_view($entry, $modinfo->cms[$entry->cmid]->context);
89    }
90}
91
92echo $OUTPUT->header();
93
94$result = new stdClass;
95$result->success = true;
96$result->entries = $entries;
97echo json_encode($result);
98
99echo $OUTPUT->footer();
100
101