Exemplo n.º 1
0
 /**
  * Deletes a message
  *
  * @param  int $messageid the message id
  * @param  int $userid the user id of who we want to delete the message for
  * @param  bool $read if is a message read (default to true)
  * @return external_description
  * @throws moodle_exception
  * @since 3.1
  */
 public static function delete_message($messageid, $userid, $read = true)
 {
     global $CFG, $DB;
     // 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, 'userid' => $userid, 'read' => $read);
     $params = self::validate_parameters(self::delete_message_parameters(), $params);
     // Validate context.
     $context = context_system::instance();
     self::validate_context($context);
     $messagestable = $params['read'] ? 'message_read' : 'message';
     $message = $DB->get_record($messagestable, array('id' => $params['messageid']), '*', MUST_EXIST);
     $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
     core_user::require_active_user($user);
     $status = false;
     if (message_can_delete_message($message, $user->id)) {
         $status = message_delete_message($message, $user->id);
     } else {
         throw new moodle_exception('You do not have permission to delete this message');
     }
     $results = array('status' => $status, 'warnings' => $warnings);
     return $results;
 }
Exemplo n.º 2
0
 /**
  * Document accesses.
  *
  * @return void
  */
 public function test_message_sent_access()
 {
     global $CFG;
     // Returns the instance as long as the area is supported.
     $searcharea = \core_search\manager::get_search_area($this->messagesentareaid);
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $user3 = self::getDataGenerator()->create_user();
     $this->preventResetByRollback();
     $sink = $this->redirectMessages();
     $message = new \core\message\message();
     $message->courseid = SITEID;
     $message->userfrom = $user1;
     $message->userto = $user2;
     $message->subject = "Test Subject";
     $message->smallmessage = "Test small messsage";
     $message->fullmessage = "Test full messsage";
     $message->fullmessageformat = 0;
     $message->fullmessagehtml = null;
     $message->notification = 0;
     $message->component = "moodle";
     $message->name = "instantmessage";
     $messageid = message_send($message);
     $messages = $sink->get_messages();
     $message = $messages[0];
     $this->setUser($user1);
     if ($CFG->messaging) {
         $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($messageid));
     } else {
         $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
     }
     $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
     message_delete_message($message, $user1->id);
     $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
     $this->setUser($user2);
     $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
     $this->setUser($user3);
     $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
     $this->setGuestUser();
     $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
     $this->setAdminUser();
     $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($messageid));
     delete_user($user2);
     $this->setUser($user1);
     $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($messageid));
 }
Exemplo n.º 3
0
 /**
  * Test the message deleted event.
  */
 public function test_message_deleted()
 {
     global $DB;
     // Create a message.
     $message = new stdClass();
     $message->useridfrom = '1';
     $message->useridto = '2';
     $message->subject = 'Subject';
     $message->message = 'Message';
     $message->timeuserfromdeleted = 0;
     $message->timeusertodeleted = 0;
     $message->id = $DB->insert_record('message', $message);
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     message_delete_message($message, $message->useridfrom);
     $events = $sink->get_events();
     $event = reset($events);
     // Check that the event data is valid.
     $this->assertInstanceOf('\\core\\event\\message_deleted', $event);
     $this->assertEquals($message->useridfrom, $event->userid);
     // The user who deleted it.
     $this->assertEquals($message->useridto, $event->relateduserid);
     $this->assertEquals('message', $event->other['messagetable']);
     $this->assertEquals($message->id, $event->other['messageid']);
     $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
     $this->assertEquals($message->useridto, $event->other['useridto']);
     // Create a read message.
     $message = new stdClass();
     $message->useridfrom = '2';
     $message->useridto = '1';
     $message->subject = 'Subject';
     $message->message = 'Message';
     $message->timeuserfromdeleted = 0;
     $message->timeusertodeleted = 0;
     $message->timeread = time();
     $message->id = $DB->insert_record('message_read', $message);
     // Trigger and capture the event.
     $sink = $this->redirectEvents();
     message_delete_message($message, $message->useridto);
     $events = $sink->get_events();
     $event = reset($events);
     // Check that the event data is valid.
     $this->assertInstanceOf('\\core\\event\\message_deleted', $event);
     $this->assertEquals($message->useridto, $event->userid);
     $this->assertEquals($message->useridfrom, $event->relateduserid);
     $this->assertEquals('message_read', $event->other['messagetable']);
     $this->assertEquals($message->id, $event->other['messageid']);
     $this->assertEquals($message->useridfrom, $event->other['useridfrom']);
     $this->assertEquals($message->useridto, $event->other['useridto']);
 }
Exemplo n.º 4
0
 /**
  * Test get_messages.
  */
 public function test_get_messages()
 {
     global $CFG, $DB;
     $this->resetAfterTest(true);
     $this->preventResetByRollback();
     // This mark the messages as read!.
     $sink = $this->redirectMessages();
     $user1 = self::getDataGenerator()->create_user();
     $user2 = self::getDataGenerator()->create_user();
     $user3 = self::getDataGenerator()->create_user();
     $course = self::getDataGenerator()->create_course();
     // Send a message from one user to another.
     message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE);
     message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE);
     message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE);
     message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE);
     message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE);
     $this->setUser($user1);
     // Get read conversations from user1 to user2.
     $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Delete the message.
     $message = array_shift($messages['messages']);
     $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
     message_delete_message($messagetobedeleted, $user1->id);
     $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Get unread conversations from user1 to user2.
     $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Get read messages send from user1.
     $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     $this->setUser($user2);
     // Get read conversations from any user to user2.
     $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(2, $messages['messages']);
     // Conversations from user3 to user2.
     $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Delete the message.
     $message = array_shift($messages['messages']);
     $messagetobedeleted = $DB->get_record('message_read', array('id' => $message['id']));
     message_delete_message($messagetobedeleted, $user2->id);
     $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     $this->setUser($user3);
     // Get read notifications received by user3.
     $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Now, create some notifications...
     // We are creating fake notifications but based on real ones.
     // This one omits notification = 1.
     $eventdata = new \core\message\message();
     $eventdata->courseid = $course->id;
     $eventdata->modulename = 'moodle';
     $eventdata->component = 'enrol_paypal';
     $eventdata->name = 'paypal_enrolment';
     $eventdata->userfrom = get_admin();
     $eventdata->userto = $user1;
     $eventdata->subject = "Moodle: PayPal payment";
     $eventdata->fullmessage = "Your PayPal payment is pending.";
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = '';
     $eventdata->smallmessage = '';
     message_send($eventdata);
     $message = new \core\message\message();
     $message->courseid = $course->id;
     $message->notification = 1;
     $message->component = 'enrol_manual';
     $message->name = 'expiry_notification';
     $message->userfrom = $user2;
     $message->userto = $user1;
     $message->subject = 'Enrolment expired';
     $message->fullmessage = 'Enrolment expired blah blah blah';
     $message->fullmessageformat = FORMAT_MARKDOWN;
     $message->fullmessagehtml = markdown_to_html($message->fullmessage);
     $message->smallmessage = $message->subject;
     $message->contexturlname = $course->fullname;
     $message->contexturl = (string) new moodle_url('/course/view.php', array('id' => $course->id));
     message_send($message);
     $userfrom = core_user::get_noreply_user();
     $userfrom->maildisplay = true;
     $eventdata = new \core\message\message();
     $eventdata->courseid = $course->id;
     $eventdata->component = 'moodle';
     $eventdata->name = 'badgecreatornotice';
     $eventdata->userfrom = $userfrom;
     $eventdata->userto = $user1;
     $eventdata->notification = 1;
     $eventdata->subject = 'New badge';
     $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML);
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = $eventdata->subject;
     $eventdata->smallmessage = $eventdata->subject;
     message_send($eventdata);
     $eventdata = new \core\message\message();
     $eventdata->courseid = $course->id;
     $eventdata->name = 'submission';
     $eventdata->component = 'mod_feedback';
     $eventdata->userfrom = $user1;
     $eventdata->userto = $user2;
     $eventdata->subject = 'Feedback submitted';
     $eventdata->fullmessage = 'Feedback submitted from an user';
     $eventdata->fullmessageformat = FORMAT_PLAIN;
     $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>';
     $eventdata->smallmessage = '';
     message_send($eventdata);
     $this->setUser($user1);
     // Get read notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(3, $messages['messages']);
     // Get one read notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Get unread notifications from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(0, $messages['messages']);
     // Get read both type of messages from any user to user1.
     $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(4, $messages['messages']);
     // Get read notifications from no-reply-user to user1.
     $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Get notifications send by user1 to any user.
     $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['messages']);
     // Test warnings.
     $CFG->messaging = 0;
     $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0);
     $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages);
     $this->assertCount(1, $messages['warnings']);
     // Test exceptions.
     // Messaging disabled.
     try {
         $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due messaging disabled.');
     } catch (moodle_exception $e) {
         $this->assertEquals('disabled', $e->errorcode);
     }
     $CFG->messaging = 1;
     // Invalid users.
     try {
         $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid users.');
     } catch (moodle_exception $e) {
         $this->assertEquals('accessdenied', $e->errorcode);
     }
     // Invalid user ids.
     try {
         $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid users.');
     } catch (moodle_exception $e) {
         $this->assertEquals('invaliduser', $e->errorcode);
     }
     // Invalid users (permissions).
     $this->setUser($user2);
     try {
         $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0);
         $this->fail('Exception expected due invalid user.');
     } catch (moodle_exception $e) {
         $this->assertEquals('accessdenied', $e->errorcode);
     }
 }
Exemplo n.º 5
0
 public function test_one_message_deleted()
 {
     global $DB;
     $this->resetAfterTest();
     $generator = $this->getDataGenerator();
     $userfrom = $generator->create_user();
     $userto = $generator->create_user();
     $message = new \core\message\message();
     $message->component = 'moodle';
     $message->name = 'instantmessage';
     $message->userfrom = $userfrom;
     $message->userto = $userto;
     $message->subject = 'message subject 1';
     $message->fullmessage = 'message body';
     $message->fullmessageformat = FORMAT_MARKDOWN;
     $message->fullmessagehtml = '<p>message body</p>';
     $message->smallmessage = 'small message';
     $message->notification = '0';
     $messageid = message_send($message);
     $actual = local::get_user_messages($userfrom->id);
     $this->assertCount(0, $actual);
     $actual = local::get_user_messages($userto->id);
     $this->assertCount(1, $actual);
     $todelete = $DB->get_record('message', ['id' => $messageid]);
     message_delete_message($todelete, $userto->id);
     $actual = local::get_user_messages($userto->id);
     $this->assertCount(0, $actual);
 }
Exemplo n.º 6
0
        // Check that we are allowed to delete this message.
        if (message_can_delete_message($message, $user1->id)) {
            if (!$deletemessageconfirm) {
                $confirmurl = new moodle_url('/message/index.php', array('user1' => $user1->id, 'user2' => $user2->id,
                    'viewing' => $viewing, 'deletemessageid' => $message->id, 'deletemessagetype' => $deletemessagetype,
                    'deletemessageconfirm' => 1, 'sesskey' => sesskey()));
                $confirmbutton = new single_button($confirmurl, get_string('delete'), 'post');
                $strdeletemessage = get_string('deletemessage', 'message');
                $PAGE->set_title($strdeletemessage);
                echo $OUTPUT->header();
                echo $OUTPUT->heading($strdeletemessage);
                echo $OUTPUT->confirm(get_string('deletemessageconfirmation', 'message'), $confirmbutton, $url);
                echo $OUTPUT->footer();
                exit();
            }
            message_delete_message($message, $user1->id);
        }
    }
    redirect($url);
}

//was a message sent? Do NOT allow someone looking at someone else's messages to send them.
$messageerror = null;
if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $systemcontext)) {
    // Check that the user is not blocking us!!
    if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
        if ($contact->blocked and !has_capability('moodle/site:readallmessages', $systemcontext)) {
            $messageerror = get_string('userisblockingyou', 'message');
        }
    }
    $userpreferences = get_user_preferences(NULL, NULL, $user2->id);