MDL-43987 core: Remove port numbers in cleanremoteaddr
[moodle.git] / tag / coursetagslib.php
blob76e98fa72029861ef1b6952f13a63ec2e0e95618
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
18 /**
19 * coursetagslib.php
21 * @package core_tag
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';
29 /**
30 * Returns an ordered array of tags associated with visible courses
31 * (boosted replacement of get_all_tags() allowing association with user and tagtype).
33 * @package core_tag
34 * @category tag
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()
41 * @return array
43 function coursetag_get_tags($courseid, $userid=0, $tagtype='', $numtags=0, $unused = '') {
45 global $CFG, $DB;
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) {
52 $courselist[] = $key;
57 // get tags from the db ordered by highest count first
58 $params = array();
59 $sql = "SELECT id as tkey, name, id, tagtype, rawname, f.timemodified, flag, count
60 FROM {tag} t,
61 (SELECT tagid, MAX(timemodified) as timemodified, COUNT(id) as count
62 FROM {tag_instance}
63 WHERE itemtype = 'course' ";
65 if ($courseid > 0) {
66 $sql .= " AND itemid = :courseid ";
67 $params['courseid'] = $courseid;
68 } else {
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;
76 if ($userid > 0) {
77 $sql .= " AND tiuserid = :userid ";
78 $params['userid'] = $userid;
81 $sql .= " GROUP BY tagid) f
82 WHERE t.id = f.tagid ";
83 if ($tagtype != '') {
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
90 if ($numtags == 0) {
91 $tags = $DB->get_records_sql($sql, $params);
92 } else {
93 $tags = $DB->get_records_sql($sql, $params, 0, $numtags);
96 // prepare the return
97 $return = array();
98 if ($tags) {
99 // avoid print_tag_cloud()'s ksort upsetting ordering by setting the key here
100 foreach ($tags as $value) {
101 $return[] = $value;
105 return $return;
110 * Returns an ordered array of tags
111 * (replaces popular_tags_count() allowing sorting).
113 * @package core_tag
114 * @category tag
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
117 * @return array
119 function coursetag_get_all_tags($unused='', $numtags=0) {
121 global $CFG, $DB;
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
125 FROM {tag} t,
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
129 WHERE c.visible = 0
130 AND ti.itemtype = 'course'
131 AND ti.itemid = c.id)
132 GROUP BY tagid) f
133 WHERE t.id = f.tagid
134 ORDER BY count DESC, name ASC";
135 if ($numtags == 0) {
136 $tags = $DB->get_records_sql($sql);
137 } else {
138 $tags = $DB->get_records_sql($sql, null, 0, $numtags);
141 $return = array();
142 if ($tags) {
143 foreach ($tags as $value) {
144 $return[] = $value;
148 return $return;
152 * Returns javascript for use in tags block and supporting pages
154 * @package core_tag
155 * @category tag
156 * @return null
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');
172 return '';
176 * Returns javascript to create the links in the tag block footer.
178 * @package core_tag
179 * @category tag
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) {
185 global $PAGE;
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);
193 return '';
197 * Returns all tags created by a user for a course
199 * @package core_tag
200 * @category tag
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) {
205 global $CFG, $DB;
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
212 ORDER BY name ASC";
214 return $DB->get_records_sql($sql, array('userid'=>$userid, 'courseid'=>$courseid));
218 * Stores a tag for a course for a user
220 * @package core_tag
221 * @category tag
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='') {
230 global $CFG;
232 if (is_array($tags) and !empty($tags)) {
233 foreach ($tags as $tag) {
234 $tag = trim($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)];
243 //ordering
244 $ordering = 0;
245 if ($current_ids = tag_get_tags_ids('course', $courseid)) {
246 end($current_ids);
247 $ordering = key($current_ids) + 1;
249 //set type
250 tag_type_set($tagid, $tagtype);
252 //tag_instance entry
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.
263 * @package core_tag
264 * @category tag
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
277 * @package core_tag
278 * @category tag
279 * @param int $tagid
280 * @return array of course objects
282 function coursetag_get_tagged_courses($tagid) {
283 global $DB;
285 $courses = array();
287 $ctxselect = context_helper::get_preload_record_columns_sql('ctx');
289 $sql = "SELECT c.*, $ctxselect
290 FROM {course} c
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;
305 return $courses;
309 * Course tagging function used only during the deletion of a course (called by lib/moodlelib.php) to clean up associated tags
311 * @package core_tag
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) {
316 global $DB, $OUTPUT;
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));
324 if ($showfeedback) {
325 echo $OUTPUT->notification(get_string('deletedcoursetags', 'tag'), 'notifysuccess');