mod-folder MDL-19799 Added set_url calls, replaced deprecated functions
[moodle.git] / mod / forum / user.php
CommitLineData
f9a0ea69 1<?php // $Id$
2
3// Display user activity reports for a course
4
bb8f3759 5require_once('../../config.php');
6require_once('lib.php');
7
8// Course ID
9$course = required_param('course', PARAM_INT);
10// User ID
11$id = optional_param('id', 0, PARAM_INT);
12$mode = optional_param('mode', 'posts', PARAM_ALPHA);
13$page = optional_param('page', 0, PARAM_INT);
14$perpage = optional_param('perpage', 5, PARAM_INT);
15
16if (empty($id)) { // See your own profile by default
17 require_login();
18 $id = $USER->id;
19}
20
21if (! $user = $DB->get_record("user", array("id" => $id))) {
22 print_error('invaliduserid');
23}
24
25if (! $course = $DB->get_record("course", array("id" => $course))) {
26 print_error('invalidcourseid');
27}
28
29$syscontext = get_context_instance(CONTEXT_SYSTEM);
30$usercontext = get_context_instance(CONTEXT_USER, $id);
31
32// do not force parents to enrol
33if (!$DB->get_record('role_assignments', array('userid' => $USER->id, 'contextid' => $usercontext->id))) {
34 require_course_login($course);
35}
36
37if ($user->deleted) {
15ca5e5e 38 echo $OUTPUT->header();
f43f1834 39 echo $OUTPUT->heading(get_string('userdeleted'));
396fb912 40 echo $OUTPUT->footer();
bb8f3759 41 die;
42}
f9a0ea69 43
bb8f3759 44add_to_log($course->id, "forum", "user report",
45 "user.php?course=$course->id&amp;id=$user->id&amp;mode=$mode", "$user->id");
f5fc83e8 46
bb8f3759 47$strforumposts = get_string('forumposts', 'forum');
48$strparticipants = get_string('participants');
49$strmode = get_string($mode, 'forum');
50$fullname = fullname($user, has_capability('moodle/site:viewfullnames', $syscontext));
f9a0ea69 51
15ca5e5e 52$link = null;
bb8f3759 53if (has_capability('moodle/course:viewparticipants', get_context_instance(CONTEXT_COURSE, $course->id)) || has_capability('moodle/site:viewparticipants', $syscontext)) {
15ca5e5e 54 $link = new moodle_url($CFG->wwwroot.'/user/index.php',array('id'=>$course->id));
bb8f3759 55}
15ca5e5e 56$PAGE->navbar->add($strparticipants, $link);
57$PAGE->navbar->add($fullname, new moodle_url($CFG->wwwroot.'/user/view.php',array('id'=>$user->id,'course'=>$course->id)));
58$PAGE->navbar->add($strforumposts);
59$PAGE->navbar->add($strmode);
60
61$PAGE->set_title("$course->shortname: $fullname: $strmode");
62$PAGE->set_heading($course->fullname);
63echo $OUTPUT->header();
2011a95b 64
bb8f3759 65$currenttab = $mode;
66$showroles = 1;
67require($CFG->dirroot.'/user/tabs.php'); /// Prints out tabs as part of user page
f9a0ea69 68
f9a0ea69 69
bb8f3759 70switch ($mode) {
71 case 'posts' :
72 $searchterms = array('userid:'.$user->id);
73 $extrasql = '';
74 break;
fa22fd5f 75
bb8f3759 76 default:
77 $searchterms = array('userid:'.$user->id);
78 $extrasql = 'AND p.parent = 0';
79 break;
80}
f9a0ea69 81
bb8f3759 82echo '<div class="user-content">';
2011a95b 83
bb8f3759 84if ($course->id == SITEID) {
85 if (empty($CFG->forceloginforprofiles) || isloggedin()) {
86 // Search throughout the whole site.
87 $searchcourse = 0;
67875aa1 88 } else {
bb8f3759 89 $searchcourse = SITEID;
67875aa1 90 }
bb8f3759 91} else {
92 // Search only for posts the user made in this course.
93 $searchcourse = $course->id;
94}
2011a95b 95
bb8f3759 96// Get the posts.
97if ($posts = forum_search_posts($searchterms, $searchcourse, $page*$perpage, $perpage,
98 $totalcount, $extrasql)) {
4e412c45 99
100 $baseurl = new moodle_url('user.php', array('id' => $user->id, 'course' => $course->id, 'mode' => $mode, 'perpage' => $perpage));
101 echo $OUTPUT->paging_bar(moodle_paging_bar::make($totalcount, $page, $perpage, $baseurl));
65bcf17b 102
bb8f3759 103 $discussions = array();
104 $forums = array();
105 $cms = array();
2011a95b 106
bb8f3759 107 foreach ($posts as $post) {
65bcf17b 108
bb8f3759 109 if (!isset($discussions[$post->discussion])) {
110 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion))) {
111 print_error('invaliddiscussionid', 'forum');
f9a0ea69 112 }
bb8f3759 113 $discussions[$post->discussion] = $discussion;
114 } else {
115 $discussion = $discussions[$post->discussion];
116 }
65bcf17b 117
bb8f3759 118 if (!isset($forums[$discussion->forum])) {
119 if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
120 print_error('invalidforumid', 'forum');
f9a0ea69 121 }
bb8f3759 122 $forums[$discussion->forum] = $forum;
123 } else {
124 $forum = $forums[$discussion->forum];
125 }
65bcf17b 126
bb8f3759 127 $ratings = null;
128 if ($forum->assessed) {
129 if ($scale = make_grades_menu($forum->scale)) {
130 $ratings =new object();
131 $ratings->scale = $scale;
132 $ratings->assesstimestart = $forum->assesstimestart;
133 $ratings->assesstimefinish = $forum->assesstimefinish;
134 $ratings->allow = false;
2011a95b 135 }
bb8f3759 136 }
2011a95b 137
bb8f3759 138 if (!isset($cms[$forum->id])) {
139 if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) {
140 print_error('invalidcoursemodule');
65bcf17b 141 }
bb8f3759 142 $cms[$forum->id] = $cm;
143 unset($cm); // do not use cm directly, it would break caching
144 }
65bcf17b 145
bb8f3759 146 $fullsubject = "<a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
147 if ($forum->type != 'single') {
148 $fullsubject .= " -> <a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a>";
149 if ($post->parent != 0) {
150 $fullsubject .= " -> <a href=\"discuss.php?d=$post->discussion&amp;parent=$post->id\">".format_string($post->subject,true)."</a>";
f9a0ea69 151 }
bb8f3759 152 }
2011a95b 153
bb8f3759 154 if ($course->id == SITEID && has_capability('moodle/site:config', $syscontext)) {
155 $postcoursename = $DB->get_field('course', array('shortname' => 'id'), $forum->course);
156 $fullsubject = '<a href="'.$CFG->wwwroot.'/course/view.php?id='.$forum->course.'">'.$postcoursename.'</a> -> '. $fullsubject;
157 }
bbbf2d40 158
bb8f3759 159 $post->subject = $fullsubject;
bbbf2d40 160
bb8f3759 161 $fulllink = "<a href=\"discuss.php?d=$post->discussion#p$post->id\">".
162 get_string("postincontext", "forum")."</a>";
f9a0ea69 163
bb8f3759 164 forum_print_post($post, $discussion, $forum, $cms[$forum->id], $course, false, false, false, $ratings, $fulllink);
165 echo "<br />";
166 }
2011a95b 167
4e412c45 168 echo $OUTPUT->paging_bar(moodle_paging_bar::make($totalcount, $page, $perpage, $baseurl));
bb8f3759 169} else {
170 if ($mode == 'posts') {
f43f1834 171 echo $OUTPUT->heading(get_string('noposts', 'forum'));
f9a0ea69 172 } else {
f43f1834 173 echo $OUTPUT->heading(get_string('nodiscussionsstartedby', 'forum'));
f9a0ea69 174 }
bb8f3759 175}
176echo '</div>';
396fb912 177echo $OUTPUT->footer();
f9a0ea69 178
9de96c06 179?>