forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tags.php
executable file
·147 lines (117 loc) · 4.68 KB
/
tags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php // $Id$
require_once('../config.php');
/// main switch for form processing to perform, add/delete etc
$action = optional_param('action','',PARAM_ALPHA);
require_login();
/// blogs could be disabled altogether
if (empty($CFG->bloglevel)) {
error('Blogging is disabled!');
}
if (isguest()) {
error(get_string('noguestpost', 'blog'));
}
/// blogs are site level
$sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
$error = '';
$script = '';
switch ($action) {
/// Adding an official tag from submitted value
case 'addofficial':
// Double check to make sure user has capability
if (!has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
error('Can not add official tags tags');
}
if (data_submitted() and confirm_sesskey()) {
$otag = trim(required_param('otag', PARAM_NOTAGS));
// When adding ofical tag, we see if there's already a personal tag
// With the same Name, if there is, we just change the type
if ($tag = get_record('tags', 'text', $otag)) {
if ($tag->type == 'official') {
// official tag already exist
$error = get_string('tagalready');
break;
} else {
$tag->type = 'official';
update_record('tags', $tag);
$tagid = $tag->id;
}
} else { // Brand new offical tag
$tag = new object();
$tag->userid = $USER->id;
$tag->text = $otag;
$tag->type = 'official';
if (!$tagid = insert_record('tags', $tag)) {
error('Can not create tag!');
}
}
/// Write newly added tags back into window opener.
$script = '<script type="text/javascript">
//<![CDATA[
var o = opener.document.createElement("option");
o.innerHTML = "<option>'.$tag->text.'</option>";
o.value = '.$tagid.';
opener.document.entry[\'otags[]\'].insertBefore(o, null);
//]]>
</script>';
}
break;
/// Deletes a tag.
case 'delete':
if (data_submitted() and confirm_sesskey()) {
$tagids = optional_param('tags', array(), PARAM_INT);
if (empty($tagids) or !is_array($tagids)) {
// TODO add error message here
// $error = 'no data selected';
break;
}
foreach ($tagids as $tagid) {
if (!$tag = get_record('tags', 'id', $tagid)) {
continue; // page refreshed?
}
if ($tag->type == 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
//can not delete
continue;
}
if ($tag->type == 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
//can not delete
continue;
}
// Delete the tag itself
if (!delete_records('tags', 'id', $tagid)) {
error('Can not delete tag');
}
// Deleteing all references to this tag
if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
error('Can not delete blog tag instances');
}
/// Remove parent window option via javascript.
$script = '<script>
//<![CDATA[
var i=0;
while (i < window.opener.document.entry[\'otags[]\'].length) {
if (window.opener.document.entry[\'otags[]\'].options[i].value == '.$tagid.') {
window.opener.document.entry[\'otags[]\'].removeChild(opener.document.entry[\'otags[]\'].options[i]);
}
i++;
}
var i=0;
while (i < window.opener.document.entry[\'ptags[]\'].length) {
if (window.opener.document.entry[\'ptags[]\'].options[i].value == '.$tagid.') {
window.opener.document.entry[\'ptags[]\'].removeChild(opener.document.entry[\'ptags[]\'].options[i]);
}
i++;
}
//]]>
</script>';
}
}
break;
default:
/// Just display the tags form.
break;
}
/// Print the table.
print_header (get_string('tagmanagement'), '', '', '', $script);
include_once('tags.html');
print_footer();
?>