MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / tag / coursetags_edit.php
1 <?php
3 /**
4  * coursetags_edit.php
5  * displays personal tags for a course with some editing facilites
6  * @author j.beedell@open.ac.uk June07
7  */
9 require_once('../config.php');
10 require_once($CFG->dirroot.'/tag/coursetagslib.php');
11 require_once($CFG->dirroot.'/tag/lib.php');
13 $courseid = optional_param('courseid', 0, PARAM_INT);
14 $keyword = optional_param('coursetag_new_tag', '', PARAM_TEXT);
15 $deltag = optional_param('del_tag', 0, PARAM_INT);
17 $url = new moodle_url($CFG->wwwroot.'/tag/coursetags_edit.php');
18 if ($courseid !== 0) {
19     $url->param('courseid', $courseid);
20 }
21 if ($keyword !== '') {
22     $url->param('coursetag_new_tag', $keyword);
23 }
24 if ($deltag !== 0) {
25     $url->param('del_tag', $deltag);
26 }
27 $PAGE->set_url($url);
29 require_login();
31 if (empty($CFG->usetags)) {
32     print_error('tagsaredisabled', 'tag');
33 }
35 if ($courseid != SITEID) {
36     if (! ($course = $DB->get_record('course', array('id' => $courseid), '*')) ) {
37         print_error('invalidcourse');
38     }
39 } else {
40     print_error('errortagfrontpage', 'tag');
41 }
43 // Permissions
44 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
45 require_login($course->id);
46 $canedit = has_capability('moodle/tag:create', $sitecontext);
48 // Language strings
49 $tagslang = 'block_tags';
51 // Store data
52 if ($data = data_submitted()) {
53     if (confirm_sesskey() and $courseid > 0 and $USER->id > 0 and $canedit) {
54         // store personal tag
55         if (trim(strip_tags($keyword))) {
56             $myurl = 'tag/search.php';
57             $keywords = explode(',', $keyword);
58             coursetag_store_keywords($keywords, $courseid, $USER->id, 'default', $myurl);
59         }
60         // delete personal tag
61         if ($deltag > 0) {
62             coursetag_delete_keyword($deltag, $USER->id, $courseid);
63         }
64     }
65 }
67 // The title and breadcrumb
68 $title = get_string('edittitle', $tagslang);
69 $coursefullname = format_string($course->fullname);
70 $courseshortname = format_string($course->shortname);
71 $PAGE->navbar->add($title);
72 $PAGE->set_title($title);
73 $PAGE->set_cacheable(false);
74 echo $OUTPUT->header();
76     // Print personal tags for all courses
77     $title = get_string('edittitle', $tagslang);
78     echo $OUTPUT->heading($title, 2, 'center');
80     $mytags = coursetag_print_cloud(coursetag_get_tags(0, $USER->id, 'default'), true);
81     $outstr = '
82         <div class="coursetag_edit_centered">
83             <div>
84                 '.get_string('editmytags', $tagslang).'
85             </div>
86             <div>';
88     if ($mytags) {
89         $outstr .= $mytags;
90     } else {
91         $outstr .= get_string('editnopersonaltags', $tagslang);
92     }
94     $outstr .= '
95             </div>
96         </div>';
97     echo $outstr;
99     // Personal tag editing
100     if ($canedit) {
101         $title = get_string('editmytagsfor', $tagslang, '"'.$coursefullname.' ('.$courseshortname.')"');
102         echo $OUTPUT->heading($title);
104         // Deletion here is open to the users own tags for this course only
105         $selectoptions = '<option value="0">'.get_string('select', $tagslang).'</option>';
106         $coursetabs = '';
107         if ($options = coursetag_get_records($courseid, $USER->id)) {
108             $coursetabs = '"';
109             foreach ($options as $option) {
110                 $selectoptions .= '<option value="'.$option->id.'">'.$option->rawname.'</option>';
111                 $coursetabs .= $option->rawname . ', ';
112             }
113             $coursetabs = rtrim($coursetabs, ', ');
114             $coursetabs .= '"';
115         }
116         if ($coursetabs) {
117             $outstr = '
118             <div class="coursetag_edit_centered">
119                 '.get_string('editthiscoursetags', $tagslang, $coursetabs).'
120             </div>';
121         } else {
122             $outstr = '
123             <div class="coursetag_edit_centered">
124                 '.get_string('editnopersonaltags', $tagslang).'
125             </div>';
126         }
128         // Print the add and delete form
129         coursetag_get_jscript();
130         $addtagshelp = $OUTPUT->help_icon(moodle_help_icon::make('addtags', 'adding tags', $tagslang));
131         $edittagthisunit = get_string('edittagthisunit', $tagslang);
132         $arrowtitle = get_string('arrowtitle', $tagslang);
133         $sesskey = sesskey();
134         $leftarrow = $OUTPUT->old_icon_url('t/arrow_left');
135         $outstr .= <<<EOT
136             <form action="$CFG->wwwroot/tag/coursetags_edit.php" method="post" id="coursetag">
137                 <div style="display: none;">
138                     <input type="hidden" name="courseid" value="$course->id" />
139                     <input type="hidden" name="sesskey" value="$sesskey" />
140                 </div>
141                 <div class="coursetag_edit_centered">
142                     <div class="coursetag_edit_row">
143                         <div class="coursetag_edit_left">
144                             $addtagshelp$edittagthisunit
145                         </div>
146                         <div class="coursetag_edit_right">
147                             <div class="coursetag_form_input1">
148                                 <input type="text" name="coursetag_sug_keyword" class="coursetag_form_input1a" disabled="disabled" />
149                             </div>
150                             <div class="coursetag_form_input2">
151                                 <input type="text" name="coursetag_new_tag" id="coursetag_new_tag" class="coursetag_form_input2a"
152                                     onfocus="ctags_getKeywords()" onkeyup="ctags_getKeywords()" maxlength="50" />
153                             </div>
154                             <div class="coursetag_edit_input3" id="coursetag_sug_btn">
155                                 <a title="$arrowtitle">
156                                     <img src="$leftarrow" width="10" height="10" alt="enter" onclick="ctags_setKeywords()" />
157                                 </a>
158                             </div>
159                         </div>
160                     </div>
161 EOT;
162         if ($coursetabs) {
163             $deletetagshelp = $OUTPUT->help_icon(moodle_help_icon::make('deletetags', 'deleting tags', $tagslang));
164             $editdeletemytag = get_string('editdeletemytag', $tagslang);
165             $outstr .= <<<EOT1
166                     <div class="coursetag_edit_row">
167                         <div class="coursetag_edit_left">
168                             $deletetagshelp$editdeletemytag
169                         </div>
170                         <div class="coursetag_edit_right">
171                             <select name="del_tag">
172                                 $selectoptions
173                             </select>
174                         </div>
175                     </div>
176 EOT1;
177         }
178         $submitstr = get_string('submit');
179         $outstr .= <<<EOT2
180                     <div class="clearer"></div>
181                     <div class="coursetag_edit_row">
182                         <button type="submit">$submitstr</button>
183                     </div>
184                 </div>
185             </form>
186 EOT2;
187         echo $outstr;
188     }
190 echo $OUTPUT->footer();