[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 * Chat external API 19 * 20 * @package mod_chat 21 * @category external 22 * @copyright 2015 Juan Leyva <juan@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 * @since Moodle 3.0 25 */ 26 27 defined('MOODLE_INTERNAL') || die; 28 29 require_once($CFG->libdir . '/externallib.php'); 30 require_once($CFG->dirroot . '/mod/chat/lib.php'); 31 32 /** 33 * Chat external functions 34 * 35 * @package mod_chat 36 * @category external 37 * @copyright 2015 Juan Leyva <juan@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 * @since Moodle 3.0 40 */ 41 class mod_chat_external extends external_api { 42 43 /** 44 * Returns description of method parameters 45 * 46 * @return external_function_parameters 47 * @since Moodle 3.0 48 */ 49 public static function login_user_parameters() { 50 return new external_function_parameters( 51 array( 52 'chatid' => new external_value(PARAM_INT, 'chat instance id'), 53 'groupid' => new external_value(PARAM_INT, 'group id, 0 means that the function will determine the user group', 54 VALUE_DEFAULT, 0), 55 ) 56 ); 57 } 58 59 /** 60 * Log the current user into a chat room in the given chat. 61 * 62 * @param int $chatid the chat instance id 63 * @param int $groupid the user group id 64 * @return array of warnings and the chat unique session id 65 * @since Moodle 3.0 66 * @throws moodle_exception 67 */ 68 public static function login_user($chatid, $groupid = 0) { 69 global $DB; 70 71 $params = self::validate_parameters(self::login_user_parameters(), 72 array( 73 'chatid' => $chatid, 74 'groupid' => $groupid 75 )); 76 $warnings = array(); 77 78 // Request and permission validation. 79 $chat = $DB->get_record('chat', array('id' => $params['chatid']), '*', MUST_EXIST); 80 list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); 81 82 $context = context_module::instance($cm->id); 83 self::validate_context($context); 84 85 require_capability('mod/chat:chat', $context); 86 87 if (!empty($params['groupid'])) { 88 $groupid = $params['groupid']; 89 // Determine is the group is visible to user. 90 if (!groups_group_visible($groupid, $course, $cm)) { 91 throw new moodle_exception('notingroup'); 92 } 93 } else { 94 // Check to see if groups are being used here. 95 if ($groupmode = groups_get_activity_groupmode($cm)) { 96 $groupid = groups_get_activity_group($cm); 97 // Determine is the group is visible to user (this is particullary for the group 0). 98 if (!groups_group_visible($groupid, $course, $cm)) { 99 throw new moodle_exception('notingroup'); 100 } 101 } else { 102 $groupid = 0; 103 } 104 } 105 106 // Get the unique chat session id. 107 // Since we are going to use the chat via Web Service requests we set the ajax version (since it's the most similar). 108 if (!$chatsid = chat_login_user($chat->id, 'ajax', $groupid, $course)) { 109 throw moodle_exception('cantlogin', 'chat'); 110 } 111 112 $result = array(); 113 $result['chatsid'] = $chatsid; 114 $result['warnings'] = $warnings; 115 return $result; 116 } 117 118 /** 119 * Returns description of method result value 120 * 121 * @return external_description 122 * @since Moodle 3.0 123 */ 124 public static function login_user_returns() { 125 return new external_single_structure( 126 array( 127 'chatsid' => new external_value(PARAM_ALPHANUM, 'unique chat session id'), 128 'warnings' => new external_warnings() 129 ) 130 ); 131 } 132 133 /** 134 * Returns description of method parameters 135 * 136 * @return external_function_parameters 137 * @since Moodle 3.0 138 */ 139 public static function get_chat_users_parameters() { 140 return new external_function_parameters( 141 array( 142 'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)') 143 ) 144 ); 145 } 146 147 /** 148 * Get the list of users in the given chat session. 149 * 150 * @param int $chatsid the chat session id 151 * @return array of warnings and the user lists 152 * @since Moodle 3.0 153 * @throws moodle_exception 154 */ 155 public static function get_chat_users($chatsid) { 156 global $DB, $PAGE; 157 158 $params = self::validate_parameters(self::get_chat_users_parameters(), 159 array( 160 'chatsid' => $chatsid 161 )); 162 $warnings = array(); 163 164 // Request and permission validation. 165 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) { 166 throw new moodle_exception('notlogged', 'chat'); 167 } 168 $chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST); 169 list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); 170 171 $context = context_module::instance($cm->id); 172 self::validate_context($context); 173 174 require_capability('mod/chat:chat', $context); 175 176 // First, delete old users from the chats. 177 chat_delete_old_users(); 178 179 $users = chat_get_users($chatuser->chatid, $chatuser->groupid, $cm->groupingid); 180 $returnedusers = array(); 181 182 foreach ($users as $user) { 183 184 $userpicture = new user_picture($user); 185 $userpicture->size = 1; // Size f1. 186 $profileimageurl = $userpicture->get_url($PAGE)->out(false); 187 188 $returnedusers[] = array( 189 'id' => $user->id, 190 'fullname' => fullname($user), 191 'profileimageurl' => $profileimageurl 192 ); 193 } 194 195 $result = array(); 196 $result['users'] = $returnedusers; 197 $result['warnings'] = $warnings; 198 return $result; 199 } 200 201 /** 202 * Returns description of method result value 203 * 204 * @return external_description 205 * @since Moodle 3.0 206 */ 207 public static function get_chat_users_returns() { 208 return new external_single_structure( 209 array( 210 'users' => new external_multiple_structure( 211 new external_single_structure( 212 array( 213 'id' => new external_value(PARAM_INT, 'user id'), 214 'fullname' => new external_value(PARAM_NOTAGS, 'user full name'), 215 'profileimageurl' => new external_value(PARAM_URL, 'user picture URL'), 216 ) 217 ), 218 'list of users' 219 ), 220 'warnings' => new external_warnings() 221 ) 222 ); 223 } 224 225 /** 226 * Returns description of method parameters 227 * 228 * @return external_function_parameters 229 * @since Moodle 3.0 230 */ 231 public static function send_chat_message_parameters() { 232 return new external_function_parameters( 233 array( 234 'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)'), 235 'messagetext' => new external_value(PARAM_RAW, 'the message text'), 236 'beepid' => new external_value(PARAM_RAW, 'the beep id', VALUE_DEFAULT, ''), 237 238 ) 239 ); 240 } 241 242 /** 243 * Send a message on the given chat session. 244 * 245 * @param int $chatsid the chat session id 246 * @param string $messagetext the message text 247 * @param string $beepid the beep message id 248 * @return array of warnings and the new message id (0 if the message was empty) 249 * @since Moodle 3.0 250 * @throws moodle_exception 251 */ 252 public static function send_chat_message($chatsid, $messagetext, $beepid = '') { 253 global $DB; 254 255 $params = self::validate_parameters(self::send_chat_message_parameters(), 256 array( 257 'chatsid' => $chatsid, 258 'messagetext' => $messagetext, 259 'beepid' => $beepid 260 )); 261 $warnings = array(); 262 263 // Request and permission validation. 264 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) { 265 throw new moodle_exception('notlogged', 'chat'); 266 } 267 $chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST); 268 list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); 269 270 $context = context_module::instance($cm->id); 271 self::validate_context($context); 272 273 require_capability('mod/chat:chat', $context); 274 275 $chatmessage = clean_text($params['messagetext'], FORMAT_MOODLE); 276 277 if (!empty($params['beepid'])) { 278 $chatmessage = 'beep ' . $params['beepid']; 279 } 280 281 if (!empty($chatmessage)) { 282 // Send the message. 283 $messageid = chat_send_chatmessage($chatuser, $chatmessage, 0, $cm); 284 // Update ping time. 285 $chatuser->lastmessageping = time() - 2; 286 $DB->update_record('chat_users', $chatuser); 287 } else { 288 $messageid = 0; 289 } 290 291 $result = array(); 292 $result['messageid'] = $messageid; 293 $result['warnings'] = $warnings; 294 return $result; 295 } 296 297 /** 298 * Returns description of method result value 299 * 300 * @return external_description 301 * @since Moodle 3.0 302 */ 303 public static function send_chat_message_returns() { 304 return new external_single_structure( 305 array( 306 'messageid' => new external_value(PARAM_INT, 'message sent id'), 307 'warnings' => new external_warnings() 308 ) 309 ); 310 } 311 312 /** 313 * Returns description of method parameters 314 * 315 * @return external_function_parameters 316 * @since Moodle 3.0 317 */ 318 public static function get_chat_latest_messages_parameters() { 319 return new external_function_parameters( 320 array( 321 'chatsid' => new external_value(PARAM_ALPHANUM, 'chat session id (obtained via mod_chat_login_user)'), 322 'chatlasttime' => new external_value(PARAM_INT, 'last time messages were retrieved (epoch time)', VALUE_DEFAULT, 0) 323 ) 324 ); 325 } 326 327 /** 328 * Get the latest messages from the given chat session. 329 * 330 * @param int $chatsid the chat session id 331 * @param int $chatlasttime last time messages were retrieved (epoch time) 332 * @return array of warnings and the new message id (0 if the message was empty) 333 * @since Moodle 3.0 334 * @throws moodle_exception 335 */ 336 public static function get_chat_latest_messages($chatsid, $chatlasttime = 0) { 337 global $DB, $CFG; 338 339 $params = self::validate_parameters(self::get_chat_latest_messages_parameters(), 340 array( 341 'chatsid' => $chatsid, 342 'chatlasttime' => $chatlasttime 343 )); 344 $warnings = array(); 345 346 // Request and permission validation. 347 if (!$chatuser = $DB->get_record('chat_users', array('sid' => $params['chatsid']))) { 348 throw new moodle_exception('notlogged', 'chat'); 349 } 350 $chat = $DB->get_record('chat', array('id' => $chatuser->chatid), '*', MUST_EXIST); 351 list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); 352 353 $context = context_module::instance($cm->id); 354 self::validate_context($context); 355 356 require_capability('mod/chat:chat', $context); 357 358 $chatlasttime = $params['chatlasttime']; 359 if ((time() - $chatlasttime) > $CFG->chat_old_ping) { 360 chat_delete_old_users(); 361 } 362 363 // Set default chat last time (to not retrieve all the conversations). 364 if ($chatlasttime == 0) { 365 $chatlasttime = time() - $CFG->chat_old_ping; 366 } 367 368 if ($latestmessage = chat_get_latest_message($chatuser->chatid, $chatuser->groupid)) { 369 $chatnewlasttime = $latestmessage->timestamp; 370 } else { 371 $chatnewlasttime = 0; 372 } 373 374 $messages = chat_get_latest_messages($chatuser, $chatlasttime); 375 $returnedmessages = array(); 376 377 foreach ($messages as $message) { 378 379 // FORMAT_MOODLE is mandatory in the chat plugin. 380 list($messageformatted, $format) = external_format_text($message->message, FORMAT_MOODLE, $context->id, 'mod_chat', 381 '', 0); 382 383 $returnedmessages[] = array( 384 'id' => $message->id, 385 'userid' => $message->userid, 386 'system' => (bool) $message->system, 387 'message' => $messageformatted, 388 'timestamp' => $message->timestamp, 389 ); 390 } 391 392 // Update our status since we are active in the chat. 393 $DB->set_field('chat_users', 'lastping', time(), array('id' => $chatuser->id)); 394 395 $result = array(); 396 $result['messages'] = $returnedmessages; 397 $result['chatnewlasttime'] = $chatnewlasttime; 398 $result['warnings'] = $warnings; 399 return $result; 400 } 401 402 /** 403 * Returns description of method result value 404 * 405 * @return external_description 406 * @since Moodle 3.0 407 */ 408 public static function get_chat_latest_messages_returns() { 409 return new external_single_structure( 410 array( 411 'messages' => new external_multiple_structure( 412 new external_single_structure( 413 array( 414 'id' => new external_value(PARAM_INT, 'message id'), 415 'userid' => new external_value(PARAM_INT, 'user id'), 416 'system' => new external_value(PARAM_BOOL, 'true if is a system message (like user joined)'), 417 'message' => new external_value(PARAM_RAW, 'message text'), 418 'timestamp' => new external_value(PARAM_INT, 'timestamp for the message'), 419 ) 420 ), 421 'list of users' 422 ), 423 'chatnewlasttime' => new external_value(PARAM_INT, 'new last time'), 424 'warnings' => new external_warnings() 425 ) 426 ); 427 } 428 429 /** 430 * Returns description of method parameters 431 * 432 * @return external_function_parameters 433 * @since Moodle 3.0 434 */ 435 public static function view_chat_parameters() { 436 return new external_function_parameters( 437 array( 438 'chatid' => new external_value(PARAM_INT, 'chat instance id') 439 ) 440 ); 441 } 442 443 /** 444 * Trigger the course module viewed event and update the module completion status. 445 * 446 * @param int $chatid the chat instance id 447 * @return array of warnings and status result 448 * @since Moodle 3.0 449 * @throws moodle_exception 450 */ 451 public static function view_chat($chatid) { 452 global $DB, $CFG; 453 454 $params = self::validate_parameters(self::view_chat_parameters(), 455 array( 456 'chatid' => $chatid 457 )); 458 $warnings = array(); 459 460 // Request and permission validation. 461 $chat = $DB->get_record('chat', array('id' => $params['chatid']), '*', MUST_EXIST); 462 list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat'); 463 464 $context = context_module::instance($cm->id); 465 self::validate_context($context); 466 467 require_capability('mod/chat:chat', $context); 468 469 // Call the url/lib API. 470 chat_view($chat, $course, $cm, $context); 471 472 $result = array(); 473 $result['status'] = true; 474 $result['warnings'] = $warnings; 475 return $result; 476 } 477 478 /** 479 * Returns description of method result value 480 * 481 * @return external_description 482 * @since Moodle 3.0 483 */ 484 public static function view_chat_returns() { 485 return new external_single_structure( 486 array( 487 'status' => new external_value(PARAM_BOOL, 'status: true if success'), 488 'warnings' => new external_warnings() 489 ) 490 ); 491 } 492 493 494 /** 495 * Describes the parameters for get_chats_by_courses. 496 * 497 * @return external_external_function_parameters 498 * @since Moodle 3.0 499 */ 500 public static function get_chats_by_courses_parameters() { 501 return new external_function_parameters ( 502 array( 503 'courseids' => new external_multiple_structure( 504 new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array() 505 ), 506 ) 507 ); 508 } 509 510 /** 511 * Returns a list of chats in a provided list of courses, 512 * if no list is provided all chats that the user can view will be returned. 513 * 514 * @param array $courseids the course ids 515 * @return array of chats details 516 * @since Moodle 3.0 517 */ 518 public static function get_chats_by_courses($courseids = array()) { 519 global $CFG; 520 521 $returnedchats = array(); 522 $warnings = array(); 523 524 $params = self::validate_parameters(self::get_chats_by_courses_parameters(), array('courseids' => $courseids)); 525 526 $courses = array(); 527 if (empty($params['courseids'])) { 528 $courses = enrol_get_my_courses(); 529 $params['courseids'] = array_keys($courses); 530 } 531 532 // Ensure there are courseids to loop through. 533 if (!empty($params['courseids'])) { 534 535 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $courses); 536 537 // Get the chats in this course, this function checks users visibility permissions. 538 // We can avoid then additional validate_context calls. 539 $chats = get_all_instances_in_courses("chat", $courses); 540 foreach ($chats as $chat) { 541 $chatcontext = context_module::instance($chat->coursemodule); 542 // Entry to return. 543 $chatdetails = array(); 544 // First, we return information that any user can see in the web interface. 545 $chatdetails['id'] = $chat->id; 546 $chatdetails['coursemodule'] = $chat->coursemodule; 547 $chatdetails['course'] = $chat->course; 548 $chatdetails['name'] = external_format_string($chat->name, $chatcontext->id); 549 // Format intro. 550 list($chatdetails['intro'], $chatdetails['introformat']) = 551 external_format_text($chat->intro, $chat->introformat, $chatcontext->id, 'mod_chat', 'intro', null); 552 $chatdetails['introfiles'] = external_util::get_area_files($chatcontext->id, 'mod_chat', 'intro', false, false); 553 554 if (has_capability('mod/chat:chat', $chatcontext)) { 555 $chatdetails['chatmethod'] = $CFG->chat_method; 556 $chatdetails['keepdays'] = $chat->keepdays; 557 $chatdetails['studentlogs'] = $chat->studentlogs; 558 $chatdetails['chattime'] = $chat->chattime; 559 $chatdetails['schedule'] = $chat->schedule; 560 } 561 562 if (has_capability('moodle/course:manageactivities', $chatcontext)) { 563 $chatdetails['timemodified'] = $chat->timemodified; 564 $chatdetails['section'] = $chat->section; 565 $chatdetails['visible'] = $chat->visible; 566 $chatdetails['groupmode'] = $chat->groupmode; 567 $chatdetails['groupingid'] = $chat->groupingid; 568 } 569 $returnedchats[] = $chatdetails; 570 } 571 } 572 $result = array(); 573 $result['chats'] = $returnedchats; 574 $result['warnings'] = $warnings; 575 return $result; 576 } 577 578 /** 579 * Describes the get_chats_by_courses return value. 580 * 581 * @return external_single_structure 582 * @since Moodle 3.0 583 */ 584 public static function get_chats_by_courses_returns() { 585 return new external_single_structure( 586 array( 587 'chats' => new external_multiple_structure( 588 new external_single_structure( 589 array( 590 'id' => new external_value(PARAM_INT, 'Chat id'), 591 'coursemodule' => new external_value(PARAM_INT, 'Course module id'), 592 'course' => new external_value(PARAM_INT, 'Course id'), 593 'name' => new external_value(PARAM_RAW, 'Chat name'), 594 'intro' => new external_value(PARAM_RAW, 'The Chat intro'), 595 'introformat' => new external_format_value('intro'), 596 'introfiles' => new external_files('Files in the introduction text', VALUE_OPTIONAL), 597 'chatmethod' => new external_value(PARAM_ALPHA, 'chat method (sockets, daemon)', VALUE_OPTIONAL), 598 'keepdays' => new external_value(PARAM_INT, 'keep days', VALUE_OPTIONAL), 599 'studentlogs' => new external_value(PARAM_INT, 'student logs visible to everyone', VALUE_OPTIONAL), 600 'chattime' => new external_value(PARAM_INT, 'chat time', VALUE_OPTIONAL), 601 'schedule' => new external_value(PARAM_INT, 'schedule type', VALUE_OPTIONAL), 602 'timemodified' => new external_value(PARAM_INT, 'time of last modification', VALUE_OPTIONAL), 603 'section' => new external_value(PARAM_INT, 'course section id', VALUE_OPTIONAL), 604 'visible' => new external_value(PARAM_BOOL, 'visible', VALUE_OPTIONAL), 605 'groupmode' => new external_value(PARAM_INT, 'group mode', VALUE_OPTIONAL), 606 'groupingid' => new external_value(PARAM_INT, 'group id', VALUE_OPTIONAL), 607 ), 'Chats' 608 ) 609 ), 610 'warnings' => new external_warnings(), 611 ) 612 ); 613 } 614 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |