public function test_search_index() { $message1 = local_mail_message::create(201, 101); $message1->add_recipient('to', 202); $message1->save('subject', 'content', 301, 1234567890); $message2 = local_mail_message::create(201, 101); $message2->add_recipient('to', 202); $message2->save('subject foo bar', 'content', 301, 1234567890); $message3 = local_mail_message::create(201, 101); $message3->save('subject', 'content <p>foo</p> <p>bar</p>', 301, 1234567891); $message4 = local_mail_message::create(201, 101); $message4->save('subject', 'content', 301, 1234567891); $message4->set_unread(201, true); $message5 = local_mail_message::create(202, 101); $message5->add_recipient('to', 201); $message5->save('subject5', 'content5', 301, 1234567890, true); // Subject and content $query = array('pattern' => ' foo bar '); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message3, $message2), $result); // Users $query = array('pattern' => fullname($this->user2)); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message2, $message1), $result); // Unread $query = array('unread' => true); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message4), $result); // Date $query = array('time' => 1234567890); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message2, $message1), $result); // Limit $query = array('limit' => 2); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message4, $message3), $result); // Before $query = array('before' => $message2->id()); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message1), $result); // After $query = array('after' => $message1->id(), 'limit' => 2); $result = local_mail_message::search_index(201, 'course', 101, $query); $this->assertEquals(array($message3, $message2), $result); // Attach $query = array('attach' => true); $result = local_mail_message::search_index(202, 'course', 101, $query); $this->assertEquals(array($message5), $result); // From $query = array('searchfrom' => fullname($this->user2)); $result = local_mail_message::search_index(202, 'course', 101, $query); $this->assertEquals(array($message5), $result); // To $query = array('searchto' => fullname($this->user1)); $result = local_mail_message::search_index(202, 'course', 101, $query); $this->assertEquals(array($message5), $result); }
function local_mail_searchmessages($type, $itemid, $query, $offset = false, $perpage = false) { global $USER, $PAGE; $prev = $next = false; $date = $nummsgs = ''; $url = new moodle_url('/local/mail/view.php', array('t' => $type)); $PAGE->set_url($url); $mailoutput = $PAGE->get_renderer('local_mail'); if (!empty($query['time'])) { $date = $query['time']; $time = explode(',', $query['time']); if (count($time) == 3) { $query['time'] = make_timestamp($time[0], $time[1], $time[2], 23, 59, 59); } else { $query['time'] = ''; } } $query['before'] = $query['before'] == 0 ? '' : $query['before']; $query['after'] = $query['after'] == 0 ? '' : $query['after']; $mailpagesize = $query['limit']; $query['limit'] += 1; if ($perpage) { $query['before'] = $query['perpageid'] == 0 ? '' : $query['perpageid']; $query['after'] = ''; } $messages = local_mail_message::search_index($USER->id, $type, $itemid, $query); $nummsgs = count($messages); if ($nummsgs == $query['limit']) { if (!empty($query['after'])) { $query['perpageid'] = $messages[0]->id(); $messages = array_slice($messages, 1, count($messages)); $prev = true; } else { $messages = array_slice($messages, 0, count($messages) - 1); $next = true; } } else { if (!empty($query['after']) and $nummsgs < $query['limit']) { $query['limit'] -= $nummsgs; $query['after'] = ''; $query['before'] = isset($messages[$nummsgs - 1]) ? $messages[$nummsgs - 1]->id() : ''; $newmessages = local_mail_message::search_index($USER->id, $type, $itemid, $query); if (count($newmessages) == $query['limit']) { $newmessages = array_slice($newmessages, 0, count($newmessages) - 1); $next = true; } $query['before'] = ''; $messages = array_merge($messages, $newmessages); } } $content = local_mail_print_messages($itemid, $type, 0, $messages, false); $prev = ($prev or !empty($query['before'])); $next = ($next or !empty($query['after'])); if (!$prev) { $query['perpageid'] = 0; } $data = array('query' => $query['pattern'], 'searchfrom' => $query['searchfrom'], 'searchto' => $query['searchto'], 'unread' => !empty($query['unread']), 'attach' => !empty($query['attach']), 'date' => $date, 'prev' => $prev, 'next' => $next, 'idafter' => !empty($query['after']) ? $query['after'] : false, 'idbefore' => !empty($query['before']) ? $query['before'] : false, 'perpageid' => $query['perpageid']); return array('info' => '', 'html' => $content, 'search' => $data, 'perpage' => $offset !== false ? $mailoutput->perpage($offset, $mailpagesize) : ''); }