MDL-21695 adding help string
[moodle.git] / tag / manage.php
blobf87e2ed4ff4d54eabbbb92752048d999eec60533
1 <?php
3 require_once('../config.php');
4 require_once($CFG->libdir.'/tablelib.php');
5 require_once('lib.php');
7 define('SHOW_ALL_PAGE_SIZE', 50000);
8 define('DEFAULT_PAGE_SIZE', 30);
10 $tagschecked = optional_param('tagschecked', array(), PARAM_INT);
11 $newnames = optional_param('newname', array(), PARAM_TAG);
12 $tagtypes = optional_param('tagtypes', array(), PARAM_ALPHA);
13 $action = optional_param('action', '', PARAM_ALPHA);
14 $perpage = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
16 require_login();
18 if (empty($CFG->usetags)) {
19 print_error('tagsaredisabled', 'tag');
22 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
23 require_capability('moodle/tag:manage', $systemcontext);
25 $params = array();
26 if ($perpage != DEFAULT_PAGE_SIZE) {
27 $params['perpage'] = $perpage;
29 $PAGE->set_url('/tag/manage.php', $params);
30 $PAGE->set_context($systemcontext);
31 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
32 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
33 $PAGE->navbar->add(get_string('managetags', 'tag'));
34 $PAGE->set_title(get_string('managetags', 'tag'));
35 $PAGE->set_heading(get_string('managetags', 'tag'));
36 $PAGE->set_pagelayout('standard');
37 echo $OUTPUT->header();
39 $err_notice = '';
40 $notice = '';
42 // get all the possible tag types from db
43 $existing_tagtypes = array();
44 if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
45 foreach ($ptypes as $ptype) {
46 $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
49 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
50 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
52 switch($action) {
54 case 'delete':
55 if (!data_submitted() or !confirm_sesskey()) {
56 break;
59 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
60 tag_delete($tagschecked);
61 $notice = $str_tagschecked.' -- '.get_string('deleted','tag');
62 break;
64 case 'reset':
65 if (!data_submitted() or !confirm_sesskey()) {
66 break;
68 $str_tagschecked = implode(', ', tag_get_name($tagschecked));
69 tag_unset_flag($tagschecked);
70 $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
71 break;
73 case 'changetype':
74 if (!data_submitted() or !confirm_sesskey()) {
75 break;
78 $changed = array();
79 foreach ($tagschecked as $tag_id) {
80 if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
81 //can not add new types here!!
82 continue;
85 // update tag type;
86 if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
87 $changed[] = $tag_id;
91 if (!empty($changed)) {
92 $str_changed = implode(', ', tag_get_name($changed));
93 $notice = $str_changed .' -- '. get_string('typechanged','tag');
95 break;
97 case 'changename':
98 if (!data_submitted() or !confirm_sesskey()) {
99 break;
102 $tags_names_changed = array();
103 foreach ($tagschecked as $tag_id) {
104 if ($newnames[$tag_id] != '') {
105 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
106 // if tag already exists, or is not a valid tag name, etc.
107 $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
108 } else {
109 $tags_names_changed[$tag_id] = $newnames[$tag_id];
114 //notice to inform what tags had their names effectively updated
115 if ($tags_names_changed){
116 $notice = implode($tags_names_changed, ', ');
117 $notice .= ' -- ' . get_string('updated','tag');
119 break;
120 case 'addofficialtag':
121 if (!data_submitted() or !confirm_sesskey()) {
122 break;
125 $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
126 $notice = '';
127 foreach ( $new_otags as $new_otag ) {
128 if ( $new_otag_id = tag_get_id($new_otag) ) {
129 // tag exists, change the type
130 tag_type_set($new_otag_id, 'official');
131 } else {
132 require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
133 tag_add($new_otag, 'official');
135 $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
137 break;
140 if ($err_notice) {
141 echo $OUTPUT->notification($err_notice, 'red');
143 if ($notice) {
144 echo $OUTPUT->notification($notice, 'green');
147 // small form to add an official tag
148 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
149 print('<input type="hidden" name="action" value="addofficialtag" />');
150 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
151 '<input name="otagsadd" id="id_otagsadd" type="text" />'.
152 '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
153 '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
154 '</div>');
155 print('</form>');
157 //setup table
159 $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
160 $tableheaders = array(get_string('id', 'tag'),
161 get_string('name', 'tag'),
162 get_string('owner', 'tag'),
163 get_string('count', 'tag'),
164 get_string('flag', 'tag'),
165 get_string('timemodified', 'tag'),
166 get_string('newname', 'tag'),
167 get_string('tagtype', 'tag'),
168 get_string('select', 'tag'));
170 $table = new flexible_table('tag-management-list-'.$USER->id);
172 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
174 $table->define_columns($tablecolumns);
175 $table->define_headers($tableheaders);
176 $table->define_baseurl($baseurl);
178 $table->sortable(true, 'flag', SORT_DESC);
180 $table->set_attribute('cellspacing', '0');
181 $table->set_attribute('id', 'tag-management-list');
182 $table->set_attribute('class', 'generaltable generalbox');
184 $table->set_control_variables(array(
185 TABLE_VAR_SORT => 'ssort',
186 TABLE_VAR_HIDE => 'shide',
187 TABLE_VAR_SHOW => 'sshow',
188 TABLE_VAR_IFIRST => 'sifirst',
189 TABLE_VAR_ILAST => 'silast',
190 TABLE_VAR_PAGE => 'spage'
193 $table->setup();
195 $params = array();
197 if ($table->get_sql_sort()) {
198 $sort = 'ORDER BY '. $table->get_sql_sort();
199 } else {
200 $sort = '';
203 if ($table->get_sql_where()) {
204 $where = 'WHERE '. $table->get_sql_where();
205 } else {
206 $where = '';
209 $query = 'SELECT tg.id, tg.name, tg.rawname, tg.tagtype, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified, u.firstname, u.lastname
210 FROM {tag_instance} ti RIGHT JOIN {tag} tg ON tg.id = ti.tagid LEFT JOIN {user} u ON tg.userid = u.id
211 '.$where.'
212 GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname
213 '.$sort;
214 $totalcount = $DB->count_records_sql('SELECT COUNT(DISTINCT(tg.id))
215 FROM {tag} tg LEFT JOIN {user} u ON u.id = tg.userid '. $where, $params);
217 $table->initialbars(true); // always initial bars
218 $table->pagesize($perpage, $totalcount);
220 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
222 //retrieve tags from DB
223 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {
225 $taglist = array_values($tagrecords);
227 //print_tag_cloud(array_values($DB->get_records_sql($query)), false);
228 //populate table with data
229 foreach ($taglist as $tag ){
230 $id = $tag->id;
231 $name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
232 $owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
233 $count = $tag->count;
234 $flag = $tag->flag;
235 $timemodified = format_time(time() - $tag->timemodified);
236 $checkbox = '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
237 $text = '<input type="text" name="newname['.$tag->id.']" />';
238 $tagtype = html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false);
240 //if the tag if flagged, highlight it
241 if ($tag->flag > 0) {
242 $id = '<span class="flagged-tag">' . $id . '</span>';
243 $name = '<span class="flagged-tag">' . $name . '</span>';
244 $owner = '<span class="flagged-tag">' . $owner . '</span>';
245 $count = '<span class="flagged-tag">' . $count . '</span>';
246 $flag = '<span class="flagged-tag">' . $flag . '</span>';
247 $timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
248 $tagtype = '<span class="flagged-tag">'. $tagtype. '</span>';
251 $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
253 $table->add_data($data);
256 echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
257 echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
258 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
259 echo '<br/><br/>';
260 echo '<select id="menuformaction" name="action">
261 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
262 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
263 <option value="delete">'. get_string('delete', 'tag') .'</option>
264 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
265 <option value="changename">'. get_string('changename', 'tag') .'</option>
266 </select>';
268 echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
271 $table->print_html();
272 echo '</div></form>';
274 if ($perpage == SHOW_ALL_PAGE_SIZE) {
275 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. DEFAULT_PAGE_SIZE .'">'. get_string('showperpage', '', DEFAULT_PAGE_SIZE) .'</a></div>';
277 } else if ($totalcount > 0 and $perpage < $totalcount) {
278 echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. SHOW_ALL_PAGE_SIZE .'">'. get_string('showall', '', $totalcount) .'</a></div>';
281 echo '<br/>';
283 echo $OUTPUT->footer();