Merge branch 'MDL-62217_master' of git://github.com/dmonllao/moodle
[moodle.git] / mod / chat / view.php
blob74ef55ce13ea5b167ffcb27a42abe9da11882a8b
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/>.
17 // This page prints a particular instance of chat.
19 require(__DIR__.'/../../config.php');
20 require_once($CFG->dirroot . '/mod/chat/lib.php');
21 require_once($CFG->libdir . '/completionlib.php');
23 $id = optional_param('id', 0, PARAM_INT);
24 $c = optional_param('c', 0, PARAM_INT);
25 $edit = optional_param('edit', -1, PARAM_BOOL);
27 if ($id) {
28 if (! $cm = get_coursemodule_from_id('chat', $id)) {
29 print_error('invalidcoursemodule');
32 if (! $course = $DB->get_record('course', array('id' => $cm->course))) {
33 print_error('coursemisconf');
36 chat_update_chat_times($cm->instance);
38 if (! $chat = $DB->get_record('chat', array('id' => $cm->instance))) {
39 print_error('invalidid', 'chat');
42 } else {
43 chat_update_chat_times($c);
45 if (! $chat = $DB->get_record('chat', array('id' => $c))) {
46 print_error('coursemisconf');
48 if (! $course = $DB->get_record('course', array('id' => $chat->course))) {
49 print_error('coursemisconf');
51 if (! $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
52 print_error('invalidcoursemodule');
56 require_course_login($course, true, $cm);
58 $context = context_module::instance($cm->id);
59 $PAGE->set_context($context);
61 // Show some info for guests.
62 if (isguestuser()) {
63 $PAGE->set_title($chat->name);
64 echo $OUTPUT->header();
65 echo $OUTPUT->confirm('<p>'.get_string('noguests', 'chat').'</p>'.get_string('liketologin'),
66 get_login_url(), $CFG->wwwroot.'/course/view.php?id='.$course->id);
68 echo $OUTPUT->footer();
69 exit;
72 // Completion and trigger events.
73 chat_view($chat, $course, $cm, $context);
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' => context_course::instance($course->id)));
81 $title = $courseshortname . ': ' . format_string($chat->name);
83 // Initialize $PAGE.
84 $PAGE->set_url('/mod/chat/view.php', array('id' => $cm->id));
85 $PAGE->set_title($title);
86 $PAGE->set_heading($course->fullname);
88 // Print the page header.
89 echo $OUTPUT->header();
91 // Check to see if groups are being used here.
92 $groupmode = groups_get_activity_groupmode($cm);
93 $currentgroup = groups_get_activity_group($cm, true);
95 // URL parameters.
96 $params = array();
97 if ($currentgroup) {
98 $groupselect = " AND groupid = '$currentgroup'";
99 $groupparam = "_group{$currentgroup}";
100 $params['groupid'] = $currentgroup;
101 } else {
102 $groupselect = "";
103 $groupparam = "";
106 echo $OUTPUT->heading(format_string($chat->name), 2);
108 if ($chat->intro) {
109 echo $OUTPUT->box(format_module_intro('chat', $chat, $cm->id), 'generalbox', 'intro');
112 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/view.php?id=$cm->id");
114 if (has_capability('mod/chat:chat', $context)) {
115 // Print the main part of the page.
116 echo $OUTPUT->box_start('generalbox', 'enterlink');
118 $now = time();
119 $span = $chat->chattime - $now;
120 if ($chat->chattime and $chat->schedule and ($span > 0)) { // A chat is scheduled.
121 echo '<p>';
122 $chatinfo = new stdClass();
123 $chatinfo->date = userdate($chat->chattime);
124 $chatinfo->fromnow = format_time($span);
125 echo get_string('sessionstart', 'chat', $chatinfo);
126 echo '</p>';
129 $params['id'] = $chat->id;
130 $chattarget = new moodle_url("/mod/chat/gui_$CFG->chat_method/index.php", $params);
131 echo '<p>';
132 echo $OUTPUT->action_link($chattarget,
133 $strenterchat,
134 new popup_action('click', $chattarget, "chat{$course->id}_{$chat->id}{$groupparam}",
135 array('height' => 500, 'width' => 700)));
136 echo '</p>';
138 $params['id'] = $chat->id;
139 $link = new moodle_url('/mod/chat/gui_basic/index.php', $params);
140 $action = new popup_action('click', $link, "chat{$course->id}_{$chat->id}{$groupparam}",
141 array('height' => 500, 'width' => 700));
142 echo '<p>';
143 echo $OUTPUT->action_link($link, get_string('noframesjs', 'message'), $action,
144 array('title' => get_string('modulename', 'chat')));
145 echo '</p>';
147 if ($chat->studentlogs or has_capability('mod/chat:readlog', $context)) {
148 if ($msg = chat_get_session_messages($chat->id, $currentgroup)) {
149 echo '<p>';
150 echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id' => $cm->id)),
151 get_string('viewreport', 'chat'));
152 echo '</p>';
156 echo $OUTPUT->box_end();
158 } else {
159 echo $OUTPUT->box_start('generalbox', 'notallowenter');
160 echo '<p>'.get_string('notallowenter', 'chat').'</p>';
161 echo $OUTPUT->box_end();
164 chat_delete_old_users();
166 if ($chatusers = chat_get_users($chat->id, $currentgroup, $cm->groupingid)) {
167 $timenow = time();
168 echo $OUTPUT->box_start('generalbox', 'chatcurrentusers');
169 echo $OUTPUT->heading($strcurrentusers, 3);
170 echo '<table>';
171 foreach ($chatusers as $chatuser) {
172 $lastping = $timenow - $chatuser->lastmessageping;
173 echo '<tr><td class="chatuserimage">';
174 $url = new moodle_url('/user/view.php', array('id' => $chatuser->id, 'course' => $chat->course));
175 echo html_writer::link($url, $OUTPUT->user_picture($chatuser));
176 echo '</td><td class="chatuserdetails">';
177 echo '<p>'.fullname($chatuser).'</p>';
178 echo '<span class="idletime">'.$stridle.': '.format_time($lastping).'</span>';
179 echo '</td></tr>';
181 echo '</table>';
182 echo $OUTPUT->box_end();
185 echo $OUTPUT->footer();