2 require_once('../config.php');
4 /// main switch for form processing to perform, add/delete etc
5 $action = optional_param('action','',PARAM_ALPHA
);
9 /// blogs could be disabled altogether
10 if (empty($CFG->bloglevel
)) {
11 error('Blogging is disabled!');
15 error(get_string('noguestpost', 'blog'));
18 /// blogs are site level
19 $sitecontext = get_context_instance(CONTEXT_SYSTEM
, SITEID
);
25 /// Adding an official tag from submitted value
27 // Double check to make sure user has capability
28 if (!has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
29 error('Can not add official tags tags');
31 if (data_submitted() and confirm_sesskey()) {
33 $otag = trim(required_param('otag', PARAM_NOTAGS
));
34 // When adding ofical tag, we see if there's already a personal tag
35 // With the same Name, if there is, we just change the type
36 if ($tag = get_record('tags', 'text', $otag)) {
37 if ($tag->type
== 'official') {
38 // official tag already exist
39 $error = get_string('tagalready');
42 $tag->type
= 'official';
43 update_record('tags', $tag);
47 } else { // Brand new offical tag
50 $tag->userid
= $USER->id
;
52 $tag->type
= 'official';
54 if (!$tagid = insert_record('tags', $tag)) {
55 error('Can not create tag!');
59 /// Write newly added tags back into window opener.
60 $script = '<script type="text/javascript">
62 var o = opener.document.createElement("option");
63 o.innerHTML = "<option>'.$tag->text
.'</option>";
65 opener.document.entry[\'otags[]\'].insertBefore(o, null);
75 if (data_submitted() and confirm_sesskey()) {
76 $tagids = optional_param('tags', array(), PARAM_INT
);
78 if (empty($tagids) or !is_array($tagids)) {
79 // TODO add error message here
80 // $error = 'no data selected';
84 foreach ($tagids as $tagid) {
86 if (!$tag = get_record('tags', 'id', $tagid)) {
87 continue; // page refreshed?
90 if ($tag->type
== 'official' and !has_capability('moodle/blog:manageofficialtags', $sitecontext)) {
95 if ($tag->type
== 'personal' and !has_capability('moodle/blog:managepersonaltags', $sitecontext)) {
100 // Delete the tag itself
101 if (!delete_records('tags', 'id', $tagid)) {
102 error('Can not delete tag');
105 // Deleteing all references to this tag
106 if (!delete_records('blog_tag_instance', 'tagid', $tagid)) {
107 error('Can not delete blog tag instances');
110 /// Remove parent window option via javascript.
114 while (i < window.opener.document.entry[\'otags[]\'].length) {
115 if (window.opener.document.entry[\'otags[]\'].options[i].value == '.$tagid.') {
116 window.opener.document.entry[\'otags[]\'].removeChild(opener.document.entry[\'otags[]\'].options[i]);
122 while (i < window.opener.document.entry[\'ptags[]\'].length) {
123 if (window.opener.document.entry[\'ptags[]\'].options[i].value == '.$tagid.') {
124 window.opener.document.entry[\'ptags[]\'].removeChild(opener.document.entry[\'ptags[]\'].options[i]);
136 /// Just display the tags form.
142 print_header (get_string('tagmanagement'), '', '', '', $script);
143 include_once('tags.html');