MDL-1626 mod_forum: Create new discussion subscription tracking table
[moodle.git] / mod / forum / deprecatedlib.php
CommitLineData
39de876c
AN
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.
13//
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/>.
16
17/**
18 * @package mod_forum
19 * @copyright 2014 Andrew Robert Nicols <andrew@nicols.co.uk>
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21 */
22
23defined('MOODLE_INTERNAL') || die();
24
25// Deprecated a very long time ago.
26
27/**
28 * How many posts by other users are unrated by a given user in the given discussion?
29 *
30 * @param int $discussionid
31 * @param int $userid
32 * @return mixed
33 * @deprecated since Moodle 1.1 - please do not use this function any more.
34 */
35function forum_count_unrated_posts($discussionid, $userid) {
36 global $CFG, $DB;
37 debugging('forum_count_unrated_posts() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
38
39 $sql = "SELECT COUNT(*) as num
40 FROM {forum_posts}
41 WHERE parent > 0
42 AND discussion = :discussionid
43 AND userid <> :userid";
44 $params = array('discussionid' => $discussionid, 'userid' => $userid);
45 $posts = $DB->get_record_sql($sql, $params);
46 if ($posts) {
47 $sql = "SELECT count(*) as num
48 FROM {forum_posts} p,
49 {rating} r
50 WHERE p.discussion = :discussionid AND
51 p.id = r.itemid AND
52 r.userid = userid AND
53 r.component = 'mod_forum' AND
54 r.ratingarea = 'post'";
55 $rated = $DB->get_record_sql($sql, $params);
56 if ($rated) {
57 if ($posts->num > $rated->num) {
58 return $posts->num - $rated->num;
59 } else {
60 return 0; // Just in case there was a counting error
61 }
62 } else {
63 return $posts->num;
64 }
65 } else {
66 return 0;
67 }
68}
69
70
71// Since Moodle 1.5.
72
73/**
74 * Returns the count of records for the provided user and discussion.
75 *
76 * @global object
77 * @global object
78 * @param int $userid
79 * @param int $discussionid
80 * @return bool
81 * @deprecated since Moodle 1.5 - please do not use this function any more.
82 */
83function forum_tp_count_discussion_read_records($userid, $discussionid) {
84 debugging('forum_tp_count_discussion_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
85
86 global $CFG, $DB;
87
88 $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
89
90 $sql = 'SELECT COUNT(DISTINCT p.id) '.
91 'FROM {forum_discussions} d '.
92 'LEFT JOIN {forum_read} r ON d.id = r.discussionid AND r.userid = ? '.
93 'LEFT JOIN {forum_posts} p ON p.discussion = d.id '.
94 'AND (p.modified < ? OR p.id = r.postid) '.
95 'WHERE d.id = ? ';
96
97 return ($DB->count_records_sql($sql, array($userid, $cutoffdate, $discussionid)));
98}
99
100/**
101 * Get all discussions started by a particular user in a course (or group)
102 *
103 * @global object
104 * @global object
105 * @param int $courseid
106 * @param int $userid
107 * @param int $groupid
108 * @return array
109 * @deprecated since Moodle 1.5 - please do not use this function any more.
110 */
111function forum_get_user_discussions($courseid, $userid, $groupid=0) {
112 debugging('forum_get_user_discussions() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
113
114 global $CFG, $DB;
115 $params = array($courseid, $userid);
116 if ($groupid) {
117 $groupselect = " AND d.groupid = ? ";
118 $params[] = $groupid;
119 } else {
120 $groupselect = "";
121 }
122
123 $allnames = get_all_user_name_fields(true, 'u');
124 return $DB->get_records_sql("SELECT p.*, d.groupid, $allnames, u.email, u.picture, u.imagealt,
125 f.type as forumtype, f.name as forumname, f.id as forumid
126 FROM {forum_discussions} d,
127 {forum_posts} p,
128 {user} u,
129 {forum} f
130 WHERE d.course = ?
131 AND p.discussion = d.id
132 AND p.parent = 0
133 AND p.userid = u.id
134 AND u.id = ?
135 AND d.forum = f.id $groupselect
136 ORDER BY p.created DESC", $params);
137}
138
139
140// Since Moodle 1.6.
141
142/**
143 * Returns the count of posts for the provided forum and [optionally] group.
144 * @global object
145 * @global object
146 * @param int $forumid
147 * @param int|bool $groupid
148 * @return int
149 * @deprecated since Moodle 1.6 - please do not use this function any more.
150 */
151function forum_tp_count_forum_posts($forumid, $groupid=false) {
152 debugging('forum_tp_count_forum_posts() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
153
154 global $CFG, $DB;
155 $params = array($forumid);
156 $sql = 'SELECT COUNT(*) '.
157 'FROM {forum_posts} fp,{forum_discussions} fd '.
158 'WHERE fd.forum = ? AND fp.discussion = fd.id';
159 if ($groupid !== false) {
160 $sql .= ' AND (fd.groupid = ? OR fd.groupid = -1)';
161 $params[] = $groupid;
162 }
163 $count = $DB->count_records_sql($sql, $params);
164
165
166 return $count;
167}
168
169/**
170 * Returns the count of records for the provided user and forum and [optionally] group.
171 * @global object
172 * @global object
173 * @param int $userid
174 * @param int $forumid
175 * @param int|bool $groupid
176 * @return int
177 * @deprecated since Moodle 1.6 - please do not use this function any more.
178 */
179function forum_tp_count_forum_read_records($userid, $forumid, $groupid=false) {
180 debugging('forum_tp_count_forum_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
181
182 global $CFG, $DB;
183
184 $cutoffdate = time() - ($CFG->forum_oldpostdays*24*60*60);
185
186 $groupsel = '';
187 $params = array($userid, $forumid, $cutoffdate);
188 if ($groupid !== false) {
189 $groupsel = "AND (d.groupid = ? OR d.groupid = -1)";
190 $params[] = $groupid;
191 }
192
193 $sql = "SELECT COUNT(p.id)
194 FROM {forum_posts} p
195 JOIN {forum_discussions} d ON d.id = p.discussion
196 LEFT JOIN {forum_read} r ON (r.postid = p.id AND r.userid= ?)
197 WHERE d.forum = ?
198 AND (p.modified < $cutoffdate OR (p.modified >= ? AND r.id IS NOT NULL))
199 $groupsel";
200
201 return $DB->get_field_sql($sql, $params);
202}
203
204
205// Since Moodle 1.7.
206
207/**
208 * Returns array of forum open modes.
209 *
39de876c
AN
210 * @return array
211 * @deprecated since Moodle 1.7 - please do not use this function any more.
212 */
213function forum_get_open_modes() {
214 debugging('forum_get_open_modes() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
215 return array();
216}
217
218
219// Since Moodle 1.9.
220
221/**
222 * Gets posts with all info ready for forum_print_post
223 * We pass forumid in because we always know it so no need to make a
224 * complicated join to find it out.
225 *
226 * @global object
227 * @global object
228 * @param int $parent
229 * @param int $forumid
230 * @return array
231 * @deprecated since Moodle 1.9 MDL-13303 - please do not use this function any more.
232 */
233function forum_get_child_posts($parent, $forumid) {
234 debugging('forum_get_child_posts() is deprecated.', DEBUG_DEVELOPER);
235
236 global $CFG, $DB;
237
238 $allnames = get_all_user_name_fields(true, 'u');
239 return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
240 FROM {forum_posts} p
241 LEFT JOIN {user} u ON p.userid = u.id
242 WHERE p.parent = ?
243 ORDER BY p.created ASC", array($parent));
244}
245
246/**
247 * Gets posts with all info ready for forum_print_post
248 * We pass forumid in because we always know it so no need to make a
249 * complicated join to find it out.
250 *
251 * @global object
252 * @global object
253 * @return mixed array of posts or false
254 * @deprecated since Moodle 1.9 MDL-13303 - please do not use this function any more.
255 */
256function forum_get_discussion_posts($discussion, $sort, $forumid) {
257 debugging('forum_get_discussion_posts() is deprecated.', DEBUG_DEVELOPER);
258
259 global $CFG, $DB;
260
261 $allnames = get_all_user_name_fields(true, 'u');
262 return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
263 FROM {forum_posts} p
264 LEFT JOIN {user} u ON p.userid = u.id
265 WHERE p.discussion = ?
266 AND p.parent > 0 $sort", array($discussion));
267}
268
269
270// Since Moodle 2.0.
271
272/**
273 * Returns a list of ratings for a particular post - sorted.
274 *
275 * @param stdClass $context
276 * @param int $postid
277 * @param string $sort
278 * @return array Array of ratings or false
279 * @deprecated since Moodle 2.0 MDL-21657 - please do not use this function any more.
280 */
281function forum_get_ratings($context, $postid, $sort = "u.firstname ASC") {
282 debugging('forum_get_ratings() is deprecated.', DEBUG_DEVELOPER);
283 $options = new stdClass;
284 $options->context = $context;
285 $options->component = 'mod_forum';
286 $options->ratingarea = 'post';
287 $options->itemid = $postid;
288 $options->sort = "ORDER BY $sort";
289
290 $rm = new rating_manager();
291 return $rm->get_all_ratings_for_item($options);
292}
293
294/**
295 * Generate and return the track or no track link for a forum.
296 *
297 * @global object
298 * @global object
299 * @global object
300 * @param object $forum the forum. Fields used are $forum->id and $forum->forcesubscribe.
301 * @param array $messages
302 * @param bool $fakelink
303 * @return string
304 * @deprecated since Moodle 2.0 MDL-14632 - please do not use this function any more.
305 */
306function forum_get_tracking_link($forum, $messages=array(), $fakelink=true) {
307 debugging('forum_get_tracking_link() is deprecated.', DEBUG_DEVELOPER);
308
309 global $CFG, $USER, $PAGE, $OUTPUT;
310
311 static $strnotrackforum, $strtrackforum;
312
313 if (isset($messages['trackforum'])) {
314 $strtrackforum = $messages['trackforum'];
315 }
316 if (isset($messages['notrackforum'])) {
317 $strnotrackforum = $messages['notrackforum'];
318 }
319 if (empty($strtrackforum)) {
320 $strtrackforum = get_string('trackforum', 'forum');
321 }
322 if (empty($strnotrackforum)) {
323 $strnotrackforum = get_string('notrackforum', 'forum');
324 }
325
326 if (forum_tp_is_tracked($forum)) {
327 $linktitle = $strnotrackforum;
328 $linktext = $strnotrackforum;
329 } else {
330 $linktitle = $strtrackforum;
331 $linktext = $strtrackforum;
332 }
333
334 $link = '';
335 if ($fakelink) {
336 $PAGE->requires->js('/mod/forum/forum.js');
337 $PAGE->requires->js_function_call('forum_produce_tracking_link', Array($forum->id, $linktext, $linktitle));
338 // use <noscript> to print button in case javascript is not enabled
339 $link .= '<noscript>';
340 }
341 $url = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
342 $link .= $OUTPUT->single_button($url, $linktext, 'get', array('title'=>$linktitle));
343
344 if ($fakelink) {
345 $link .= '</noscript>';
346 }
347
348 return $link;
349}
350
351/**
352 * Returns the count of records for the provided user and discussion.
353 *
354 * @global object
355 * @global object
356 * @param int $userid
357 * @param int $discussionid
358 * @return int
359 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
360 */
361function forum_tp_count_discussion_unread_posts($userid, $discussionid) {
362 debugging('forum_tp_count_discussion_unread_posts() is deprecated.', DEBUG_DEVELOPER);
363 global $CFG, $DB;
364
365 $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
366
367 $sql = 'SELECT COUNT(p.id) '.
368 'FROM {forum_posts} p '.
369 'LEFT JOIN {forum_read} r ON r.postid = p.id AND r.userid = ? '.
370 'WHERE p.discussion = ? '.
371 'AND p.modified >= ? AND r.id is NULL';
372
373 return $DB->count_records_sql($sql, array($userid, $discussionid, $cutoffdate));
374}
375
376/**
377 * Converts a forum to use the Roles System
378 *
379 * @deprecated since Moodle 2.0 MDL-23479 - please do not use this function any more.
380 */
381function forum_convert_to_roles() {
382 debugging('forum_convert_to_roles() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
383}
384
385/**
386 * Returns all records in the 'forum_read' table matching the passed keys, indexed
387 * by userid.
388 *
389 * @global object
390 * @param int $userid
391 * @param int $postid
392 * @param int $discussionid
393 * @param int $forumid
394 * @return array
395 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
396 */
397function forum_tp_get_read_records($userid=-1, $postid=-1, $discussionid=-1, $forumid=-1) {
398 debugging('forum_tp_get_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
399
400 global $DB;
401 $select = '';
402 $params = array();
403
404 if ($userid > -1) {
405 if ($select != '') $select .= ' AND ';
406 $select .= 'userid = ?';
407 $params[] = $userid;
408 }
409 if ($postid > -1) {
410 if ($select != '') $select .= ' AND ';
411 $select .= 'postid = ?';
412 $params[] = $postid;
413 }
414 if ($discussionid > -1) {
415 if ($select != '') $select .= ' AND ';
416 $select .= 'discussionid = ?';
417 $params[] = $discussionid;
418 }
419 if ($forumid > -1) {
420 if ($select != '') $select .= ' AND ';
421 $select .= 'forumid = ?';
422 $params[] = $forumid;
423 }
424
425 return $DB->get_records_select('forum_read', $select, $params);
426}
427
428/**
429 * Returns all read records for the provided user and discussion, indexed by postid.
430 *
431 * @global object
432 * @param inti $userid
433 * @param int $discussionid
434 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
435 */
436function forum_tp_get_discussion_read_records($userid, $discussionid) {
437 debugging('forum_tp_get_discussion_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
438
439 global $DB;
440 $select = 'userid = ? AND discussionid = ?';
441 $fields = 'postid, firstread, lastread';
442 return $DB->get_records_select('forum_read', $select, array($userid, $discussionid), '', $fields);
443}
444
445// Deprecated in 2.3.
446
447/**
448 * This function gets run whenever user is enrolled into course
449 *
450 * @deprecated since Moodle 2.3 MDL-33166 - please do not use this function any more.
451 * @param stdClass $cp
452 * @return void
453 */
454function forum_user_enrolled($cp) {
455 debugging('forum_user_enrolled() is deprecated. Please use forum_user_role_assigned instead.', DEBUG_DEVELOPER);
456 global $DB;
457
458 // NOTE: this has to be as fast as possible - we do not want to slow down enrolments!
459 // Originally there used to be 'mod/forum:initialsubscriptions' which was
460 // introduced because we did not have enrolment information in earlier versions...
461
462 $sql = "SELECT f.id
463 FROM {forum} f
464 LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
465 WHERE f.course = :courseid AND f.forcesubscribe = :initial AND fs.id IS NULL";
466 $params = array('courseid'=>$cp->courseid, 'userid'=>$cp->userid, 'initial'=>FORUM_INITIALSUBSCRIBE);
467
468 $forums = $DB->get_records_sql($sql, $params);
469 foreach ($forums as $forum) {
470 forum_subscribe($cp->userid, $forum->id);
471 }
472}
473
474
475// Deprecated in 2.4.
476
477/**
478 * Checks to see if a user can view a particular post.
479 *
480 * @deprecated since Moodle 2.4 use forum_user_can_see_post() instead
481 *
482 * @param object $post
483 * @param object $course
484 * @param object $cm
485 * @param object $forum
486 * @param object $discussion
487 * @param object $user
488 * @return boolean
489 */
490function forum_user_can_view_post($post, $course, $cm, $forum, $discussion, $user=null){
491 debugging('forum_user_can_view_post() is deprecated. Please use forum_user_can_see_post() instead.', DEBUG_DEVELOPER);
492 return forum_user_can_see_post($forum, $discussion, $post, $user, $cm);
493}
494
495
496// Deprecated in 2.6.
497
498/**
499 * FORUM_TRACKING_ON - deprecated alias for FORUM_TRACKING_FORCED.
500 * @deprecated since 2.6
501 */
502define('FORUM_TRACKING_ON', 2);
503
504/**
39de876c
AN
505 * @deprecated since Moodle 2.6
506 * @see shorten_text()
39de876c
AN
507 */
508function forum_shorten_post($message) {
052933f4 509 throw new coding_exception('forum_shorten_post() can not be used any more. Please use shorten_text($message, $CFG->forum_shortpost) instead.');
39de876c 510}