Commit ffb4eaa9 authored by Dan Poltawski's avatar Dan Poltawski
Browse files

Merge branch 'MDL-49501-master' of git://github.com/jleyva/moodle

Conflicts:
	lib/db/services.php
	version.php
parents b6c34ef9 b6795827
......@@ -837,6 +837,15 @@ $functions = array(
'capabilities' => '',
),
'core_message_mark_message_read' => array(
'classname' => 'core_message_external',
'methodname' => 'mark_message_read',
'classpath' => 'message/externallib.php',
'description' => 'Mark a single message as read, trigger message_viewed event.',
'type' => 'write',
'capabilities' => '',
),
// === notes related functions ===
'moodle_notes_create_notes' => array(
......@@ -1085,6 +1094,7 @@ $services = array(
'core_notes_get_course_notes',
'core_completion_get_course_completion_status',
'core_user_view_user_list',
'core_message_mark_message_read',
),
'enabled' => 0,
'restrictedusers' => 0,
......
......@@ -927,6 +927,84 @@ class core_message_external extends external_api {
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since 2.9
*/
public static function mark_message_read_parameters() {
return new external_function_parameters(
array(
'messageid' => new external_value(PARAM_INT, 'id of the message (in the message table)'),
'timeread' => new external_value(PARAM_INT, 'timestamp for when the message should be marked read')
)
);
}
/**
* Mark a single message as read, trigger message_viewed event
*
* @param int $messageid id of the message (in the message table)
* @param int $timeread timestamp for when the message should be marked read
* @return external_description
* @throws invalid_parameter_exception
* @throws moodle_exception
* @since 2.9
*/
public static function mark_message_read($messageid, $timeread) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot . "/message/lib.php");
// Check if private messaging between users is allowed.
if (empty($CFG->messaging)) {
throw new moodle_exception('disabled', 'message');
}
// Warnings array, it can be empty at the end but is mandatory.
$warnings = array();
// Validate params.
$params = array(
'messageid' => $messageid,
'timeread' => $timeread
);
$params = self::validate_parameters(self::mark_message_read_parameters(), $params);
// Validate context.
$context = context_system::instance();
self::validate_context($context);
$message = $DB->get_record('message', array('id' => $params['messageid']), '*', MUST_EXIST);
if ($message->useridto != $USER->id) {
throw new invalid_parameter_exception('Invalid messageid, you don\'t have permissions to mark this message as read');
}
$messageid = message_mark_message_read($message, $params['timeread']);
$results = array(
'messageid' => $messageid,
'warnings' => $warnings
);
return $results;
}
/**
* Returns description of method result value
*
* @return external_description
* @since 2.9
*/
public static function mark_message_read_returns() {
return new external_single_structure(
array(
'messageid' => new external_value(PARAM_INT, 'the id of the message in the message_read table'),
'warnings' => new external_warnings()
)
);
}
}
/**
......
......@@ -618,4 +618,64 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
}
/**
* Test mark_message_read.
*/
public function test_mark_message_read() {
$this->resetAfterTest(true);
$user1 = self::getDataGenerator()->create_user();
$user2 = self::getDataGenerator()->create_user();
$user3 = self::getDataGenerator()->create_user();
// Login as user1.
$this->setUser($user1);
$this->assertEquals(array(), core_message_external::create_contacts(
array($user2->id, $user3->id)));
// The user2 sends a couple of messages to user1.
$this->send_message($user2, $user1, 'Hello there!');
$this->send_message($user2, $user1, 'How you goin?');
$this->send_message($user3, $user1, 'How you goin?');
$this->send_message($user3, $user2, 'How you goin?');
// Retrieve all messages sent by user2 (they are currently unread).
$lastmessages = message_get_messages($user1->id, $user2->id, 0, false);
$messageids = array();
foreach ($lastmessages as $m) {
$messageid = core_message_external::mark_message_read($m->id, time());
$messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid);
}
// Retrieve all messages sent (they are currently read).
$lastmessages = message_get_messages($user1->id, $user2->id, 0, true);
$this->assertCount(2, $lastmessages);
$this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages);
$this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages);
// Retrieve all messages sent by any user (that are currently unread).
$lastmessages = message_get_messages($user1->id, 0, 0, false);
$this->assertCount(1, $lastmessages);
// Invalid message ids.
try {
$messageid = core_message_external::mark_message_read($messageids[0]['messageid'] * 2, time());
$this->fail('Exception expected due invalid messageid.');
} catch (dml_missing_record_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
// A message to a different user.
$lastmessages = message_get_messages($user2->id, $user3->id, 0, false);
$messageid = array_pop($lastmessages)->id;
try {
$messageid = core_message_external::mark_message_read($messageid, time());
$this->fail('Exception expected due invalid messageid.');
} catch (invalid_parameter_exception $e) {
$this->assertEquals('invalidparameter', $e->errorcode);
}
}
}
......@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015040600.01; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015040700.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment