MDL-45625 mod_forum: Deprecate many old forum functions
[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 *
210 * @deprecated since
211 * @return array
212 * @deprecated since Moodle 1.7 - please do not use this function any more.
213 */
214function forum_get_open_modes() {
215 debugging('forum_get_open_modes() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
216 return array();
217}
218
219
220// Since Moodle 1.9.
221
222/**
223 * Gets posts with all info ready for forum_print_post
224 * We pass forumid in because we always know it so no need to make a
225 * complicated join to find it out.
226 *
227 * @global object
228 * @global object
229 * @param int $parent
230 * @param int $forumid
231 * @return array
232 * @deprecated since Moodle 1.9 MDL-13303 - please do not use this function any more.
233 */
234function forum_get_child_posts($parent, $forumid) {
235 debugging('forum_get_child_posts() is deprecated.', DEBUG_DEVELOPER);
236
237 global $CFG, $DB;
238
239 $allnames = get_all_user_name_fields(true, 'u');
240 return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
241 FROM {forum_posts} p
242 LEFT JOIN {user} u ON p.userid = u.id
243 WHERE p.parent = ?
244 ORDER BY p.created ASC", array($parent));
245}
246
247/**
248 * Gets posts with all info ready for forum_print_post
249 * We pass forumid in because we always know it so no need to make a
250 * complicated join to find it out.
251 *
252 * @global object
253 * @global object
254 * @return mixed array of posts or false
255 * @deprecated since Moodle 1.9 MDL-13303 - please do not use this function any more.
256 */
257function forum_get_discussion_posts($discussion, $sort, $forumid) {
258 debugging('forum_get_discussion_posts() is deprecated.', DEBUG_DEVELOPER);
259
260 global $CFG, $DB;
261
262 $allnames = get_all_user_name_fields(true, 'u');
263 return $DB->get_records_sql("SELECT p.*, $forumid AS forum, $allnames, u.email, u.picture, u.imagealt
264 FROM {forum_posts} p
265 LEFT JOIN {user} u ON p.userid = u.id
266 WHERE p.discussion = ?
267 AND p.parent > 0 $sort", array($discussion));
268}
269
270
271// Since Moodle 2.0.
272
273/**
274 * Returns a list of ratings for a particular post - sorted.
275 *
276 * @param stdClass $context
277 * @param int $postid
278 * @param string $sort
279 * @return array Array of ratings or false
280 * @deprecated since Moodle 2.0 MDL-21657 - please do not use this function any more.
281 */
282function forum_get_ratings($context, $postid, $sort = "u.firstname ASC") {
283 debugging('forum_get_ratings() is deprecated.', DEBUG_DEVELOPER);
284 $options = new stdClass;
285 $options->context = $context;
286 $options->component = 'mod_forum';
287 $options->ratingarea = 'post';
288 $options->itemid = $postid;
289 $options->sort = "ORDER BY $sort";
290
291 $rm = new rating_manager();
292 return $rm->get_all_ratings_for_item($options);
293}
294
295/**
296 * Generate and return the track or no track link for a forum.
297 *
298 * @global object
299 * @global object
300 * @global object
301 * @param object $forum the forum. Fields used are $forum->id and $forum->forcesubscribe.
302 * @param array $messages
303 * @param bool $fakelink
304 * @return string
305 * @deprecated since Moodle 2.0 MDL-14632 - please do not use this function any more.
306 */
307function forum_get_tracking_link($forum, $messages=array(), $fakelink=true) {
308 debugging('forum_get_tracking_link() is deprecated.', DEBUG_DEVELOPER);
309
310 global $CFG, $USER, $PAGE, $OUTPUT;
311
312 static $strnotrackforum, $strtrackforum;
313
314 if (isset($messages['trackforum'])) {
315 $strtrackforum = $messages['trackforum'];
316 }
317 if (isset($messages['notrackforum'])) {
318 $strnotrackforum = $messages['notrackforum'];
319 }
320 if (empty($strtrackforum)) {
321 $strtrackforum = get_string('trackforum', 'forum');
322 }
323 if (empty($strnotrackforum)) {
324 $strnotrackforum = get_string('notrackforum', 'forum');
325 }
326
327 if (forum_tp_is_tracked($forum)) {
328 $linktitle = $strnotrackforum;
329 $linktext = $strnotrackforum;
330 } else {
331 $linktitle = $strtrackforum;
332 $linktext = $strtrackforum;
333 }
334
335 $link = '';
336 if ($fakelink) {
337 $PAGE->requires->js('/mod/forum/forum.js');
338 $PAGE->requires->js_function_call('forum_produce_tracking_link', Array($forum->id, $linktext, $linktitle));
339 // use <noscript> to print button in case javascript is not enabled
340 $link .= '<noscript>';
341 }
342 $url = new moodle_url('/mod/forum/settracking.php', array('id'=>$forum->id));
343 $link .= $OUTPUT->single_button($url, $linktext, 'get', array('title'=>$linktitle));
344
345 if ($fakelink) {
346 $link .= '</noscript>';
347 }
348
349 return $link;
350}
351
352/**
353 * Returns the count of records for the provided user and discussion.
354 *
355 * @global object
356 * @global object
357 * @param int $userid
358 * @param int $discussionid
359 * @return int
360 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
361 */
362function forum_tp_count_discussion_unread_posts($userid, $discussionid) {
363 debugging('forum_tp_count_discussion_unread_posts() is deprecated.', DEBUG_DEVELOPER);
364 global $CFG, $DB;
365
366 $cutoffdate = isset($CFG->forum_oldpostdays) ? (time() - ($CFG->forum_oldpostdays*24*60*60)) : 0;
367
368 $sql = 'SELECT COUNT(p.id) '.
369 'FROM {forum_posts} p '.
370 'LEFT JOIN {forum_read} r ON r.postid = p.id AND r.userid = ? '.
371 'WHERE p.discussion = ? '.
372 'AND p.modified >= ? AND r.id is NULL';
373
374 return $DB->count_records_sql($sql, array($userid, $discussionid, $cutoffdate));
375}
376
377/**
378 * Converts a forum to use the Roles System
379 *
380 * @deprecated since Moodle 2.0 MDL-23479 - please do not use this function any more.
381 */
382function forum_convert_to_roles() {
383 debugging('forum_convert_to_roles() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
384}
385
386/**
387 * Returns all records in the 'forum_read' table matching the passed keys, indexed
388 * by userid.
389 *
390 * @global object
391 * @param int $userid
392 * @param int $postid
393 * @param int $discussionid
394 * @param int $forumid
395 * @return array
396 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
397 */
398function forum_tp_get_read_records($userid=-1, $postid=-1, $discussionid=-1, $forumid=-1) {
399 debugging('forum_tp_get_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
400
401 global $DB;
402 $select = '';
403 $params = array();
404
405 if ($userid > -1) {
406 if ($select != '') $select .= ' AND ';
407 $select .= 'userid = ?';
408 $params[] = $userid;
409 }
410 if ($postid > -1) {
411 if ($select != '') $select .= ' AND ';
412 $select .= 'postid = ?';
413 $params[] = $postid;
414 }
415 if ($discussionid > -1) {
416 if ($select != '') $select .= ' AND ';
417 $select .= 'discussionid = ?';
418 $params[] = $discussionid;
419 }
420 if ($forumid > -1) {
421 if ($select != '') $select .= ' AND ';
422 $select .= 'forumid = ?';
423 $params[] = $forumid;
424 }
425
426 return $DB->get_records_select('forum_read', $select, $params);
427}
428
429/**
430 * Returns all read records for the provided user and discussion, indexed by postid.
431 *
432 * @global object
433 * @param inti $userid
434 * @param int $discussionid
435 * @deprecated since Moodle 2.0 MDL-14113 - please do not use this function any more.
436 */
437function forum_tp_get_discussion_read_records($userid, $discussionid) {
438 debugging('forum_tp_get_discussion_read_records() is deprecated and will not be replaced.', DEBUG_DEVELOPER);
439
440 global $DB;
441 $select = 'userid = ? AND discussionid = ?';
442 $fields = 'postid, firstread, lastread';
443 return $DB->get_records_select('forum_read', $select, array($userid, $discussionid), '', $fields);
444}
445
446// Deprecated in 2.3.
447
448/**
449 * This function gets run whenever user is enrolled into course
450 *
451 * @deprecated since Moodle 2.3 MDL-33166 - please do not use this function any more.
452 * @param stdClass $cp
453 * @return void
454 */
455function forum_user_enrolled($cp) {
456 debugging('forum_user_enrolled() is deprecated. Please use forum_user_role_assigned instead.', DEBUG_DEVELOPER);
457 global $DB;
458
459 // NOTE: this has to be as fast as possible - we do not want to slow down enrolments!
460 // Originally there used to be 'mod/forum:initialsubscriptions' which was
461 // introduced because we did not have enrolment information in earlier versions...
462
463 $sql = "SELECT f.id
464 FROM {forum} f
465 LEFT JOIN {forum_subscriptions} fs ON (fs.forum = f.id AND fs.userid = :userid)
466 WHERE f.course = :courseid AND f.forcesubscribe = :initial AND fs.id IS NULL";
467 $params = array('courseid'=>$cp->courseid, 'userid'=>$cp->userid, 'initial'=>FORUM_INITIALSUBSCRIBE);
468
469 $forums = $DB->get_records_sql($sql, $params);
470 foreach ($forums as $forum) {
471 forum_subscribe($cp->userid, $forum->id);
472 }
473}
474
475
476// Deprecated in 2.4.
477
478/**
479 * Checks to see if a user can view a particular post.
480 *
481 * @deprecated since Moodle 2.4 use forum_user_can_see_post() instead
482 *
483 * @param object $post
484 * @param object $course
485 * @param object $cm
486 * @param object $forum
487 * @param object $discussion
488 * @param object $user
489 * @return boolean
490 */
491function forum_user_can_view_post($post, $course, $cm, $forum, $discussion, $user=null){
492 debugging('forum_user_can_view_post() is deprecated. Please use forum_user_can_see_post() instead.', DEBUG_DEVELOPER);
493 return forum_user_can_see_post($forum, $discussion, $post, $user, $cm);
494}
495
496
497// Deprecated in 2.6.
498
499/**
500 * FORUM_TRACKING_ON - deprecated alias for FORUM_TRACKING_FORCED.
501 * @deprecated since 2.6
502 */
503define('FORUM_TRACKING_ON', 2);
504
505/**
506 * This function is now deprecated. Use shorten_text($message, $CFG->forum_shortpost) instead.
507 *
508 * Given a post object that we already know has a long message
509 * this function truncates the message nicely to the first
510 * sane place between $CFG->forum_longpost and $CFG->forum_shortpost
511 *
512 * @deprecated since Moodle 2.6
513 * @see shorten_text()
514 * @todo finalise deprecation in 2.8 in MDL-40851
515 * @global object
516 * @param string $message
517 * @return string
518 */
519function forum_shorten_post($message) {
520 global $CFG;
521 debugging('forum_shorten_post() is deprecated since Moodle 2.6. Please use shorten_text($message, $CFG->forum_shortpost) instead.', DEBUG_DEVELOPER);
522 return shorten_text($message, $CFG->forum_shortpost);
523}