Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/tag/manage.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00025 require_once('../config.php');
00026 require_once($CFG->libdir.'/tablelib.php');
00027 require_once('lib.php');
00028 
00029 define('SHOW_ALL_PAGE_SIZE', 50000);
00030 define('DEFAULT_PAGE_SIZE', 30);
00031 
00032 $tagschecked = optional_param_array('tagschecked', array(), PARAM_INT);
00033 $newnames    = optional_param_array('newname', array(), PARAM_TAG);
00034 $tagtypes    = optional_param_array('tagtypes', array(), PARAM_ALPHA);
00035 $action      = optional_param('action', '', PARAM_ALPHA);
00036 $perpage     = optional_param('perpage', DEFAULT_PAGE_SIZE, PARAM_INT);
00037 
00038 require_login();
00039 
00040 if (empty($CFG->usetags)) {
00041     print_error('tagsaredisabled', 'tag');
00042 }
00043 
00044 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
00045 require_capability('moodle/tag:manage', $systemcontext);
00046 
00047 $params = array();
00048 if ($perpage != DEFAULT_PAGE_SIZE) {
00049     $params['perpage'] = $perpage;
00050 }
00051 $PAGE->set_url('/tag/manage.php', $params);
00052 $PAGE->set_context($systemcontext);
00053 $PAGE->set_blocks_editing_capability('moodle/tag:editblocks');
00054 $PAGE->navbar->add(get_string('tags', 'tag'), new moodle_url('/tag/search.php'));
00055 $PAGE->navbar->add(get_string('managetags', 'tag'));
00056 $PAGE->set_title(get_string('managetags', 'tag'));
00057 $PAGE->set_heading($COURSE->fullname);
00058 $PAGE->set_pagelayout('standard');
00059 echo $OUTPUT->header();
00060 
00061 $err_notice = '';
00062 $notice = '';
00063 
00064 // get all the possible tag types from db
00065 $existing_tagtypes = array();
00066 if ($ptypes = $DB->get_records_sql("SELECT DISTINCT(tagtype) FROM {tag}")) {
00067     foreach ($ptypes as $ptype) {
00068         $existing_tagtypes[$ptype->tagtype] = $ptype->tagtype;
00069     }
00070 }
00071 $existing_tagtypes['official'] = get_string('tagtype_official', 'tag');
00072 $existing_tagtypes['default'] = get_string('tagtype_default', 'tag');
00073 
00074 switch($action) {
00075 
00076     case 'delete':
00077         if (!data_submitted() or !confirm_sesskey()) {
00078             break;
00079         }
00080 
00081         $str_tagschecked = implode(', ', tag_get_name($tagschecked));
00082         tag_delete($tagschecked);
00083         $notice = $str_tagschecked.' --  '.get_string('deleted','tag');
00084         break;
00085 
00086     case 'reset':
00087         if (!data_submitted() or !confirm_sesskey()) {
00088             break;
00089         }
00090         $str_tagschecked = implode(', ', tag_get_name($tagschecked));
00091         tag_unset_flag($tagschecked);
00092         $notice = $str_tagschecked .' -- '. get_string('reset', 'tag');
00093         break;
00094 
00095     case 'changetype':
00096         if (!data_submitted() or !confirm_sesskey()) {
00097             break;
00098         }
00099 
00100         $changed = array();
00101         foreach ($tagschecked as $tag_id) {
00102             if (!array_key_exists($tagtypes[$tag_id], $existing_tagtypes)) {
00103                 //can not add new types here!!
00104                 continue;
00105             }
00106 
00107             // update tag type;
00108             if (tag_type_set($tag_id, $tagtypes[$tag_id])) {
00109                 $changed[] = $tag_id;
00110             }
00111         }
00112 
00113         if (!empty($changed)) {
00114             $str_changed = implode(', ', tag_get_name($changed));
00115             $notice = $str_changed .' --  '. get_string('typechanged','tag');
00116         }
00117         break;
00118 
00119     case 'changename':
00120         if (!data_submitted() or !confirm_sesskey()) {
00121             break;
00122         }
00123 
00124         $tags_names_changed = array();
00125         foreach ($tagschecked as $tag_id) {
00126             if ($newnames[$tag_id] != '') {
00127                 if (! $tags_names_updated[] = tag_rename($tag_id, $newnames[$tag_id]) ) {
00128                     // if tag already exists, or is not a valid tag name, etc.
00129                     $err_notice .= $newnames[$tag_id]. '-- ' . get_string('namesalreadybeeingused','tag').'<br />';
00130                 } else {
00131                     $tags_names_changed[$tag_id] = $newnames[$tag_id];
00132                 }
00133             }
00134         }
00135 
00136         //notice to inform what tags had their names effectively updated
00137         if ($tags_names_changed){
00138             $notice = implode($tags_names_changed, ', ');
00139             $notice .= ' -- ' . get_string('updated','tag');
00140         }
00141         break;
00142     case 'addofficialtag':
00143         if (!data_submitted() or !confirm_sesskey()) {
00144             break;
00145         }
00146 
00147         $new_otags = explode(',', optional_param('otagsadd', '', PARAM_TAG));
00148         $notice = '';
00149         foreach ( $new_otags as $new_otag ) {
00150             if ( $new_otag_id = tag_get_id($new_otag) ) {
00151                 // tag exists, change the type
00152                 tag_type_set($new_otag_id, 'official');
00153             } else {
00154                 require_capability('moodle/tag:create', get_context_instance(CONTEXT_SYSTEM));
00155                 tag_add($new_otag, 'official');
00156             }
00157             $notice .= get_string('addedotag', 'tag', $new_otag) .' ';
00158         }
00159         break;
00160 }
00161 
00162 if ($err_notice) {
00163     echo $OUTPUT->notification($err_notice, 'red');
00164 }
00165 if ($notice) {
00166     echo $OUTPUT->notification($notice, 'green');
00167 }
00168 
00169 // small form to add an official tag
00170 print('<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php">');
00171 print('<input type="hidden" name="action" value="addofficialtag" />');
00172 print('<div class="tag-management-form generalbox"><label class="accesshide" for="id_otagsadd">'. get_string('addotags', 'tag') .'</label>'.
00173     '<input name="otagsadd" id="id_otagsadd" type="text" />'.
00174     '<input type="hidden" name="sesskey" value="'.sesskey().'">'.
00175     '<input name="addotags" value="'. get_string('addotags', 'tag') .'" onclick="skipClientValidation = true;" id="id_addotags" type="submit" />'.
00176     '</div>');
00177 print('</form>');
00178 
00179 //setup table
00180 
00181 $tablecolumns = array('id', 'name', 'fullname', 'count', 'flag', 'timemodified', 'rawname', 'tagtype', '');
00182 $tableheaders = array(get_string('id', 'tag'),
00183                       get_string('name', 'tag'),
00184                       get_string('owner', 'tag'),
00185                       get_string('count', 'tag'),
00186                       get_string('flag', 'tag'),
00187                       get_string('timemodified', 'tag'),
00188                       get_string('newname', 'tag'),
00189                       get_string('tagtype', 'tag'),
00190                       get_string('select', 'tag'));
00191 
00192 $table = new flexible_table('tag-management-list-'.$USER->id);
00193 
00194 $baseurl = $CFG->wwwroot.'/tag/manage.php?perpage='.$perpage;
00195 
00196 $table->define_columns($tablecolumns);
00197 $table->define_headers($tableheaders);
00198 $table->define_baseurl($baseurl);
00199 
00200 $table->sortable(true, 'flag', SORT_DESC);
00201 
00202 $table->set_attribute('cellspacing', '0');
00203 $table->set_attribute('id', 'tag-management-list');
00204 $table->set_attribute('class', 'generaltable generalbox');
00205 
00206 $table->set_control_variables(array(
00207 TABLE_VAR_SORT    => 'ssort',
00208 TABLE_VAR_HIDE    => 'shide',
00209 TABLE_VAR_SHOW    => 'sshow',
00210 TABLE_VAR_IFIRST  => 'sifirst',
00211 TABLE_VAR_ILAST   => 'silast',
00212 TABLE_VAR_PAGE    => 'spage'
00213 ));
00214 
00215 $table->setup();
00216 
00217 if ($table->get_sql_sort()) {
00218     $sort = 'ORDER BY '. $table->get_sql_sort();
00219 } else {
00220     $sort = '';
00221 }
00222 
00223 list($where, $params) = $table->get_sql_where();
00224 if ($where) {
00225     $where = 'WHERE '. $where;
00226 }
00227 
00228 $query = "
00229         SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
00230                u.id AS owner, u.firstname, u.lastname,
00231                COUNT(ti.id) AS count
00232           FROM {tag} tg
00233      LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
00234      LEFT JOIN {user} u ON u.id = tg.userid
00235                $where
00236       GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
00237                u.id, u.firstname, u.lastname
00238          $sort";
00239 
00240 $totalcount = $DB->count_records_sql("
00241         SELECT COUNT(DISTINCT(tg.id))
00242           FROM {tag} tg
00243      LEFT JOIN {user} u ON u.id = tg.userid
00244         $where", $params);
00245 
00246 $table->initialbars(true); // always initial bars
00247 $table->pagesize($perpage, $totalcount);
00248 
00249 echo '<form class="tag-management-form" method="post" action="'.$CFG->wwwroot.'/tag/manage.php"><div>';
00250 
00251 //retrieve tags from DB
00252 if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(),  $table->get_page_size())) {
00253 
00254     //populate table with data
00255     foreach ($tagrecords as $tag) {
00256         $id             =   $tag->id;
00257         $name           =   '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
00258         $owner          =   '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
00259         $count          =   $tag->count;
00260         $flag           =   $tag->flag;
00261         $timemodified   =   format_time(time() - $tag->timemodified);
00262         $checkbox       =   '<input type="checkbox" name="tagschecked[]" value="'.$tag->id.'" />';
00263         $text           =   '<input type="text" name="newname['.$tag->id.']" />';
00264         $tagtype        =   html_writer::select($existing_tagtypes, 'tagtypes['.$tag->id.']', $tag->tagtype, false);
00265 
00266         //if the tag if flagged, highlight it
00267         if ($tag->flag > 0) {
00268             $id = '<span class="flagged-tag">' . $id . '</span>';
00269             $name = '<span class="flagged-tag">' . $name . '</span>';
00270             $owner = '<span class="flagged-tag">' . $owner . '</span>';
00271             $count = '<span class="flagged-tag">' . $count . '</span>';
00272             $flag = '<span class="flagged-tag">' . $flag . '</span>';
00273             $timemodified = '<span class="flagged-tag">' . $timemodified . '</span>';
00274             $tagtype = '<span class="flagged-tag">'. $tagtype. '</span>';
00275         }
00276 
00277         $data = array($id, $name, $owner, $count, $flag, $timemodified, $text, $tagtype, $checkbox);
00278 
00279         $table->add_data($data);
00280     }
00281 
00282     echo '<input type="button" onclick="checkall()" value="'.get_string('selectall').'" /> ';
00283     echo '<input type="button" onclick="checknone()" value="'.get_string('deselectall').'" /> ';
00284     echo '<input type="hidden" name="sesskey" value="'.sesskey().'" /> ';
00285     echo '<br/><br/>';
00286     echo '<select id="menuformaction" name="action">
00287                 <option value="" selected="selected">'. get_string('withselectedtags', 'tag') .'</option>
00288                 <option value="reset">'. get_string('resetflag', 'tag') .'</option>
00289                 <option value="delete">'. get_string('delete', 'tag') .'</option>
00290                 <option value="changetype">'. get_string('changetype', 'tag') .'</option>
00291                 <option value="changename">'. get_string('changename', 'tag') .'</option>
00292             </select>';
00293 
00294     echo '<button id="tag-management-submit" type="submit">'. get_string('ok') .'</button>';
00295 }
00296 
00297 $table->print_html();
00298 echo '</div></form>';
00299 
00300 if ($perpage == SHOW_ALL_PAGE_SIZE) {
00301     echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. DEFAULT_PAGE_SIZE .'">'. get_string('showperpage', '', DEFAULT_PAGE_SIZE) .'</a></div>';
00302 
00303 } else if ($totalcount > 0 and $perpage < $totalcount) {
00304     echo '<div id="showall"><a href="'. $baseurl .'&amp;perpage='. SHOW_ALL_PAGE_SIZE .'">'. get_string('showall', '', $totalcount) .'</a></div>';
00305 }
00306 
00307 echo '<br/>';
00308 
00309 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations