Automatic installer.php lang files by installer_builder (20090120)
[moodle.git] / blog / tags.php
blob1d909ea0f51ae8ca0476a082c8ad2e10fec45cd8
1 <?php
2 require_once('../config.php');
3 require_login();
5 if (isguest()) {
6 error ('Guests can not modify tags!');
8 //form process
9 $mode = optional_param('mode','',PARAM_ALPHA);
11 switch ($mode) {
12 case 'addofficial': /// adding official tags
14 if (!isadmin() || !confirm_sesskey()) {
15 die('you can not add official tags');
18 if (($otag = optional_param('otag', '', PARAM_NOTAGS)) && (!get_record('tags','text',$otag))) {
19 $tag->userid = $USER->id;
20 $tag->text = $otag;
21 $tag->type = 'official';
22 $tagid = insert_record('tags', $tag);
24 /// write newly added tags back into window opener
25 echo '<script language="JavaScript" type="text/javascript">
26 var o = opener.document.createElement("option");
27 o.innerHTML = "<option>'.$tag->text.'</option>";
28 o.value = '.$tagid.';
29 opener.document.entry[\'otags[]\'].insertBefore(o, null);
30 </script>';
31 } else { // tag already exist
32 notify(get_string('tagalready'));
35 break;
37 case 'addpersonal': /// everyone can add personal tags
38 if (!confirm_sesskey() || isguest() || !isset($USER->id)) {
39 error ('you can not add tags');
42 if (($ptag = optional_param('ptag', '', PARAM_NOTAGS)) && (!get_record('tags','text',$ptag))) {
43 $tag->userid = $USER->id;
44 $tag->text = $ptag;
45 $tag->type = 'personal';
46 $tagid = insert_record('tags', $tag);
48 /// write newly added tags back into window opener
49 echo '<script language="JavaScript" type="text/javascript">
50 var o = opener.document.createElement("option");
51 o.innerHTML = "<option>'.$tag->text.'</option>";
52 o.value = '.$tagid.';
53 opener.document.entry[\'ptags[]\'].insertBefore(o, null);
54 </script>';
55 } else { //tag already exist
56 notify(get_string('tagalready'));
59 break;
61 case 'delete': /// delete a tag
62 if (!confirm_sesskey()) {
63 error('you can not delete tags');
66 if ($tags = optional_param('tags', 0, PARAM_INT)) {
68 foreach ($tags as $tag) {
70 $blogtag = get_record('tags','id',$tag);
72 // you can only delete your own tags, or you have to be an admin
73 if (!isadmin() and $USER->id != $blogtag->userid) {
74 notify(get_string('norighttodeletetag','blog', $blogtag->text));
75 continue;
78 /// Only admin can delete tags that are referenced
79 if (!isadmin() && get_records('blog_tag_instance','tagid', $tag)) {
80 notify('tag is used by other users, can not delete!');
81 continue;
84 delete_records('tags','id',$tag);
85 delete_records('blog_tag_instance', 'tagid', $tag);
87 /// remove parent window option via javascript
88 echo '<script>
89 var i=0;
90 while (i < window.opener.document.entry[\'otags[]\'].length) {
91 if (window.opener.document.entry[\'otags[]\'].options[i].value == '.$tag.') {
92 window.opener.document.entry[\'otags[]\'].removeChild(opener.document.entry[\'otags[]\'].options[i]);
94 i++;
97 var i=0;
98 while (i < window.opener.document.entry[\'ptags[]\'].length) {
99 if (window.opener.document.entry[\'ptags[]\'].options[i].value == '.$tag.') {
100 window.opener.document.entry[\'ptags[]\'].removeChild(opener.document.entry[\'ptags[]\'].options[i]);
102 i++;
105 </script>';
108 break;
110 default: // just displaying tags form
111 break;
114 //print the table
116 print_header();
118 include_once('tags.html');
120 print_footer();