2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
22 * @copyright 2007 j.beedell@open.ac.uk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once $CFG->dirroot
.'/tag/lib.php';
27 require_once $CFG->dirroot
.'/tag/locallib.php';
30 * Returns an ordered array of tags associated with visible courses
31 * (boosted replacement of get_all_tags() allowing association with user and tagtype).
35 * @param int $courseid A course id. Passing 0 will return all distinct tags for all visible courses
36 * @param int $userid (optional) the user id, a default of 0 will return all users tags for the course
37 * @param string $tagtype (optional) The type of tag, empty string returns all types. Currently (Moodle 2.2) there are two
38 * types of tags which are used within Moodle, they are 'official' and 'default'.
39 * @param int $numtags (optional) number of tags to display, default of 80 is set in the block, 0 returns all
40 * @param string $unused (optional) was selected sorting, moved to tag_print_cloud()
43 function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $unused = '') {
47 // get visible course ids
48 $courselist = array();
49 if ($courseid === 0) {
50 if ($courses = $DB->get_records_select('course', 'visible=1 AND category>0', null, '', 'id')) {
51 foreach ($courses as $key => $value) {
57 // get tags from the db ordered by highest count first
59 $sql = "SELECT id as tkey, name, id, tagtype, rawname, f.timemodified, flag, count
61 (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
63 WHERE itemtype = 'course' ";
66 $sql .= " AND itemid = :courseid ";
67 $params['courseid'] = $courseid;
69 if (!empty($courselist)) {
70 list($usql, $uparams) = $DB->get_in_or_equal($courselist, SQL_PARAMS_NAMED
);
71 $sql .= "AND itemid $usql ";
72 $params = $params +
$uparams;
77 $sql .= " AND tiuserid = :userid ";
78 $params['userid'] = $userid;
81 $sql .= " GROUP BY tagid) f
82 WHERE t.id = f.tagid ";
84 $sql .= "AND tagtype = :tagtype ";
85 $params['tagtype'] = $tagtype;
87 $sql .= "ORDER BY count DESC, name ASC";
89 // limit the number of tags for output
91 $tags = $DB->get_records_sql($sql, $params);
93 $tags = $DB->get_records_sql($sql, $params, 0, $numtags);
99 // avoid print_tag_cloud()'s ksort upsetting ordering by setting the key here
100 foreach ($tags as $value) {
110 * Returns an ordered array of tags
111 * (replaces popular_tags_count() allowing sorting).
115 * @param string $unused (optional) was selected sorting - moved to tag_print_cloud()
116 * @param int $numtags (optional) number of tags to display, default of 20 is set in the block, 0 returns all
119 function coursetag_get_all_tags($unused='', $numtags=0) {
123 // note that this selects all tags except for courses that are not visible
124 $sql = "SELECT id, name, tagtype, rawname, f.timemodified, flag, count
126 (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
127 FROM {tag_instance} WHERE tagid NOT IN
128 (SELECT tagid FROM {tag_instance} ti, {course} c
130 AND ti.itemtype = 'course'
131 AND ti.itemid = c.id)
134 ORDER BY count DESC, name ASC";
136 $tags = $DB->get_records_sql($sql);
138 $tags = $DB->get_records_sql($sql, null, 0, $numtags);
143 foreach ($tags as $value) {
152 * Returns javascript for use in tags block and supporting pages
158 function coursetag_get_jscript() {
159 global $CFG, $DB, $PAGE;
161 $PAGE->requires
->js('/tag/tag.js');
162 $PAGE->requires
->strings_for_js(array('jserror1', 'jserror2'), 'block_tags');
164 if ($coursetags = $DB->get_records('tag', null, 'name ASC', 'name, id')) {
165 foreach ($coursetags as $key => $value) {
166 $PAGE->requires
->js_function_call('set_course_tag', array($key));
170 $PAGE->requires
->js('/blocks/tags/coursetags.js');
176 * Returns javascript to create the links in the tag block footer.
180 * @param string $elementid the element to attach the footer to
181 * @param array $coursetagslinks links arrays each consisting of 'title', 'onclick' and 'text' elements
182 * @return string always returns a blank string
184 function coursetag_get_jscript_links($elementid, $coursetagslinks) {
187 if (!empty($coursetagslinks)) {
188 foreach ($coursetagslinks as $a) {
189 $PAGE->requires
->js_function_call('add_tag_footer_link', array($elementid, $a['title'], $a['onclick'], $a['text']), true);
197 * Returns all tags created by a user for a course
201 * @param int $courseid tags are returned for the course that has this courseid
202 * @param int $userid return tags which were created by this user
204 function coursetag_get_records($courseid, $userid) {
207 $sql = "SELECT t.id, name, rawname
208 FROM {tag} t, {tag_instance} ti
209 WHERE t.id = ti.tagid
210 AND ti.tiuserid = :userid
211 AND ti.itemid = :courseid
214 return $DB->get_records_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid));
218 * Stores a tag for a course for a user
222 * @param array $tags simple array of keywords to be stored
223 * @param int $courseid the id of the course we wish to store a tag for
224 * @param int $userid the id of the user we wish to store a tag for
225 * @param string $tagtype official or default only
226 * @param string $myurl (optional) for logging creation of course tags
228 function coursetag_store_keywords($tags, $courseid, $userid=0, $tagtype='official', $myurl='') {
232 if (is_array($tags) and !empty($tags)) {
233 foreach ($tags as $tag) {
235 if (strlen($tag) > 0) {
236 //tag_set_add('course', $courseid, $tag, $userid); //deletes official tags
238 //add tag if does not exist
239 if (!$tagid = tag_get_id($tag)) {
240 $tag_id_array = tag_add(array($tag), $tagtype);
241 $tagid = $tag_id_array[core_text
::strtolower($tag)];
245 if ($current_ids = tag_get_tags_ids('course', $courseid)) {
247 $ordering = key($current_ids) +
1;
250 tag_type_set($tagid, $tagtype);
253 tag_assign('course', $courseid, $tagid, $ordering, $userid, 'core', context_course
::instance($courseid)->id
);
261 * Deletes a personal tag for a user for a course.
265 * @param int $tagid the tag we wish to delete
266 * @param int $userid the user that the tag is associated with
267 * @param int $courseid the course that the tag is associated with
269 function coursetag_delete_keyword($tagid, $userid, $courseid) {
270 tag_delete_instance('course', $courseid, $tagid, $userid);
274 * Get courses tagged with a tag
276 * @global moodle_database $DB
280 * @return array of course objects
282 function coursetag_get_tagged_courses($tagid) {
287 $ctxselect = context_helper
::get_preload_record_columns_sql('ctx');
289 $sql = "SELECT c.*, $ctxselect
291 JOIN {tag_instance} t ON t.itemid = c.id
292 JOIN {context} ctx ON ctx.instanceid = c.id
293 WHERE t.tagid = :tagid AND
294 t.itemtype = 'course' AND
295 ctx.contextlevel = :contextlevel
296 ORDER BY c.sortorder ASC";
297 $params = array('tagid' => $tagid, 'contextlevel' => CONTEXT_COURSE
);
298 $rs = $DB->get_recordset_sql($sql, $params);
299 foreach ($rs as $course) {
300 context_helper
::preload_from_record($course);
301 if ($course->visible
== 1 ||
has_capability('moodle/course:viewhiddencourses', context_course
::instance($course->id
))) {
302 $courses[$course->id
] = $course;
309 * Course tagging function used only during the deletion of a course (called by lib/moodlelib.php) to clean up associated tags
312 * @param int $courseid the course we wish to delete tag instances from
313 * @param bool $showfeedback if we should output a notification of the delete to the end user
315 function coursetag_delete_course_tags($courseid, $showfeedback=false) {
318 if ($taginstances = $DB->get_fieldset_select('tag_instance', 'tagid', "itemtype = 'course' AND itemid = :courseid",
319 array('courseid' => $courseid))) {
321 tag_delete(array_values($taginstances));
325 echo $OUTPUT->notification(get_string('deletedcoursetags', 'tag'), 'notifysuccess');