예제 #1
0
파일: api_test.php 프로젝트: dg711/moodle
 /**
  * Tests searching users in a course.
  */
 public function test_search_users_in_course()
 {
     // Create some users.
     $user1 = new stdClass();
     $user1->firstname = 'User';
     $user1->lastname = 'One';
     $user1 = self::getDataGenerator()->create_user($user1);
     // The person doing the search.
     $this->setUser($user1);
     // Second user is going to have their last access set to now, so they are online.
     $user2 = new stdClass();
     $user2->firstname = 'User';
     $user2->lastname = 'Two';
     $user2->lastaccess = time();
     $user2 = self::getDataGenerator()->create_user($user2);
     // Block the second user.
     message_block_contact($user2->id, $user1->id);
     $user3 = new stdClass();
     $user3->firstname = 'User';
     $user3->lastname = 'Three';
     $user3 = self::getDataGenerator()->create_user($user3);
     // Create a course.
     $course1 = new stdClass();
     $course1->fullname = 'Course';
     $course1->shortname = 'One';
     $course1 = $this->getDataGenerator()->create_course($course1);
     // Enrol the searcher and one user in the course.
     $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
     $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
     // Perform a search.
     $results = \core_message\api::search_users_in_course($user1->id, $course1->id, 'User');
     $this->assertEquals(1, count($results));
     $user = $results[0];
     $this->assertEquals($user2->id, $user->userid);
     $this->assertEquals(fullname($user2), $user->fullname);
     $this->assertFalse($user->ismessaging);
     $this->assertNull($user->lastmessage);
     $this->assertNull($user->messageid);
     $this->assertTrue($user->isonline);
     $this->assertFalse($user->isread);
     $this->assertTrue($user->isblocked);
     $this->assertNull($user->unreadcount);
 }
예제 #2
0
 /**
  * Get messagearea search users in course results.
  *
  * @param int $userid The id of the user who is performing the search
  * @param int $courseid The id of the course
  * @param string $search The string being searched
  * @param int $limitfrom
  * @param int $limitnum
  * @return stdClass
  * @throws moodle_exception
  * @since 3.2
  */
 public static function data_for_messagearea_search_users_in_course($userid, $courseid, $search, $limitfrom = 0, $limitnum = 0)
 {
     global $CFG, $PAGE, $USER;
     // Check if messaging is enabled.
     if (empty($CFG->messaging)) {
         throw new moodle_exception('disabled', 'message');
     }
     $systemcontext = context_system::instance();
     $params = array('userid' => $userid, 'courseid' => $courseid, 'search' => $search, 'limitfrom' => $limitfrom, 'limitnum' => $limitnum);
     self::validate_parameters(self::data_for_messagearea_search_users_in_course_parameters(), $params);
     self::validate_context($systemcontext);
     if ($USER->id != $userid && !has_capability('moodle/site:readallmessages', $systemcontext)) {
         throw new moodle_exception('You do not have permission to perform this action.');
     }
     $users = \core_message\api::search_users_in_course($userid, $courseid, $search, $limitfrom, $limitnum);
     $results = new \core_message\output\messagearea\user_search_results($users);
     $renderer = $PAGE->get_renderer('core_message');
     return $results->export_for_template($renderer);
 }