MDL-38173 Swap conditions to make CI servers happy.
[moodle.git] / mod / glossary / showentry_ajax.php
blob5df4c0fe59fa46c6b28ebf49973d223511525d7b
1 <?php
2 define('AJAX_SCRIPT', true);
4 require_once('../../config.php');
5 require_once('lib.php');
6 require_once($CFG->libdir . '/filelib.php');
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);
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);
20 if ($CFG->forcelogin) {
21 require_login();
24 if ($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);
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);
40 } else {
41 print_error('invalidelementid');
44 if ($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;
55 // make sure the entry is approved (or approvable by current user)
56 if (!$entry->approved and ($USER->id != $entry->userid)) {
57 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
58 if (!has_capability('mod/glossary:approve', $context)) {
59 unset($entries[$key]);
60 continue;
64 $context = get_context_instance(CONTEXT_MODULE, $entry->cmid);
65 $definition = file_rewrite_pluginfile_urls($entry->definition, 'pluginfile.php', $context->id, 'mod_glossary', 'entry', $entry->id);
67 $options = new stdClass();
68 $options->para = false;
69 $options->trusted = $entry->definitiontrust;
70 $options->context = $context;
71 $entries[$key]->definition = format_text($definition, $entry->definitionformat, $options);
73 $entries[$key]->attachments = '';
74 if (!empty($entries[$key]->attachment)) {
75 $attachments = glossary_print_attachments($entry, $cm, 'html');
76 $entries[$key]->attachments = html_writer::tag('p', $attachments);
79 $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>";
80 add_to_log($entry->courseid, 'glossary', 'view entry', "showentry.php?eid=$entry->id", $entry->id, $entry->cmid);
84 echo $OUTPUT->header();
86 $result = new stdClass;
87 $result->success = true;
88 $result->entries = $entries;
89 echo json_encode($result);
91 echo $OUTPUT->footer();