weekly release 2.3dev
[moodle.git] / mod / chat / view.php
blob0b86e50b3344c9f4e1455d4fe10a0decc2f8df11
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /// This page prints a particular instance of chat
20 require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');
21 require_once($CFG->dirroot . '/mod/chat/lib.php');
22 require_once($CFG->libdir . '/completionlib.php');
24 $id = optional_param('id', 0, PARAM_INT);
25 $c = optional_param('c', 0, PARAM_INT);
26 $edit = optional_param('edit', -1, PARAM_BOOL);
28 if ($id) {
29 if (! $cm = get_coursemodule_from_id('chat', $id)) {
30 print_error('invalidcoursemodule');
33 if (! $course = $DB->get_record('course', array('id'=>$cm->course))) {
34 print_error('coursemisconf');
37 chat_update_chat_times($cm->instance);
39 if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) {
40 print_error('invalidid', 'chat');
43 } else {
44 chat_update_chat_times($c);
46 if (! $chat = $DB->get_record('chat', array('id'=>$c))) {
47 print_error('coursemisconf');
49 if (! $course = $DB->get_record('course', array('id'=>$chat->course))) {
50 print_error('coursemisconf');
52 if (! $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
53 print_error('invalidcoursemodule');
57 require_course_login($course, true, $cm);
59 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
60 $PAGE->set_context($context);
62 // show some info for guests
63 if (isguestuser()) {
64 $PAGE->set_title(format_string($chat->name));
65 echo $OUTPUT->header();
66 echo $OUTPUT->confirm('<p>'.get_string('noguests', 'chat').'</p>'.get_string('liketologin'),
67 get_login_url(), $CFG->wwwroot.'/course/view.php?id='.$course->id);
69 echo $OUTPUT->footer();
70 exit;
73 add_to_log($course->id, 'chat', 'view', "view.php?id=$cm->id", $chat->id, $cm->id);
75 $strenterchat = get_string('enterchat', 'chat');
76 $stridle = get_string('idle', 'chat');
77 $strcurrentusers = get_string('currentusers', 'chat');
78 $strnextsession = get_string('nextsession', 'chat');
80 $courseshortname = format_string($course->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
81 $title = $courseshortname . ': ' . format_string($chat->name);
83 // Mark viewed by user (if required)
84 $completion = new completion_info($course);
85 $completion->set_module_viewed($cm);
87 // Initialize $PAGE
88 $PAGE->set_url('/mod/chat/view.php', array('id' => $cm->id));
89 $PAGE->set_title($title);
90 $PAGE->set_heading($course->fullname);
92 /// Print the page header
93 echo $OUTPUT->header();
95 /// Check to see if groups are being used here
96 $groupmode = groups_get_activity_groupmode($cm);
97 $currentgroup = groups_get_activity_group($cm, true);
98 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/view.php?id=$cm->id");
100 // url parameters
101 $params = array();
102 if ($currentgroup) {
103 $groupselect = " AND groupid = '$currentgroup'";
104 $groupparam = "&amp;groupid=$currentgroup";
105 $params['groupid'] = $currentgroup;
106 } else {
107 $groupselect = "";
108 $groupparam = "";
111 echo $OUTPUT->heading(format_string($chat->name));
113 if ($chat->intro) {
114 echo $OUTPUT->box(format_module_intro('chat', $chat, $cm->id), 'generalbox', 'intro');
117 if (has_capability('mod/chat:chat', $context)) {
118 /// Print the main part of the page
119 echo $OUTPUT->box_start('generalbox', 'enterlink');
121 $now = time();
122 $span = $chat->chattime - $now;
123 if ($chat->chattime and $chat->schedule and ($span>0)) { // A chat is scheduled
124 echo '<p>';
125 echo get_string('sessionstart', 'chat', format_time($span));
126 echo '</p>';
129 if (empty($USER->screenreader)) {
130 $params['id'] = $chat->id;
131 $chattarget = new moodle_url("/mod/chat/gui_$CFG->chat_method/index.php", $params);
132 echo '<p>';
133 echo $OUTPUT->action_link($chattarget, $strenterchat, new popup_action('click', $chattarget, "chat$course->id$chat->id$groupparam", array('height' => 500, 'width' => 700)));
134 echo '</p>';
137 // if user is using screen reader, then there is no need to display this link again
138 // users with screenreader set, will only see 1 link, to the manual refresh page
139 // for better accessibility
140 // show frame/js-less alternative
141 $params['id'] = $chat->id;
142 $link = new moodle_url('/mod/chat/gui_basic/index.php', $params);
143 $action = new popup_action('click', $link, "chat{$course->id}{$chat->id}{$groupparam}", array('height' => 500, 'width' => 700));
144 echo '<p>';
145 echo $OUTPUT->action_link($link, get_string('noframesjs', 'message'), $action, array('title'=>get_string('modulename', 'chat')));
146 echo '</p>';
148 if ($chat->studentlogs or has_capability('mod/chat:readlog', $context)) {
149 if ($msg = $DB->get_records_select('chat_messages', "chatid = ? $groupselect", array($chat->id))) {
150 echo '<p>';
151 echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id'=>$cm->id)), get_string('viewreport', 'chat'));
152 echo '</p>';
157 echo $OUTPUT->box_end();
159 } else {
160 echo $OUTPUT->box_start('generalbox', 'notallowenter');
161 echo '<p>'.get_string('notallowenter', 'chat').'</p>';
162 echo $OUTPUT->box_end();
165 chat_delete_old_users();
167 if ($chatusers = chat_get_users($chat->id, $currentgroup, $cm->groupingid)) {
168 $timenow = time();
169 echo $OUTPUT->box_start('generalbox', 'chatcurrentusers');
170 echo $OUTPUT->heading($strcurrentusers, 4);
171 echo '<table>';
172 foreach ($chatusers as $chatuser) {
173 $lastping = $timenow - $chatuser->lastmessageping;
174 echo '<tr><td class="chatuserimage">';
175 $url = new moodle_url('/user/view.php', array('id'=>$chatuser->id, 'course'=>$chat->course));
176 echo html_writer::link($url, $OUTPUT->user_picture($chatuser));
177 echo '</td><td class="chatuserdetails">';
178 echo '<p>'.fullname($chatuser).'</p>';
179 echo '<span class="idletime">'.$stridle.': '.format_time($lastping).'</span>';
180 echo '</td></tr>';
182 echo '</table>';
183 echo $OUTPUT->box_end();
186 echo $OUTPUT->footer();