/** * Returns description of method result value * * @return external_description * @since Moodle 2.1 * @deprecated Moodle 2.2 MDL-29106 - Please do not call this function any more. * @todo MDL-31194 This will be deleted in Moodle 2.5. * @see core_user_external::get_course_user_profiles_returns() */ public static function get_course_participants_by_id_returns() { return core_user_external::get_course_user_profiles_returns(); }
/** * Returns description of method result value * * @return external_description * @since Moodle 3.1 */ public static function get_participant_returns() { $userdescription = core_user_external::user_description(); $userdescription->default = []; $userdescription->required = VALUE_OPTIONAL; return new external_single_structure(array('id' => new external_value(PARAM_INT, 'ID of the user'), 'fullname' => new external_value(PARAM_NOTAGS, 'The fullname of the user'), 'submitted' => new external_value(PARAM_BOOL, 'have they submitted their assignment'), 'requiregrading' => new external_value(PARAM_BOOL, 'is their submission waiting for grading'), 'blindmarking' => new external_value(PARAM_BOOL, 'is blind marking enabled for this assignment'), 'groupid' => new external_value(PARAM_INT, 'for group assignments this is the group id', VALUE_OPTIONAL), 'groupname' => new external_value(PARAM_NOTAGS, 'for group assignments this is the group name', VALUE_OPTIONAL), 'user' => $userdescription)); }
/** * Test add user device */ public function test_add_user_device() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $device = array('appid' => 'com.moodle.moodlemobile', 'name' => 'occam', 'model' => 'Nexus 4', 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', 'uuid' => 'asdnfl348qlksfaasef859'); // Call the external function. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); $created = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); $created = (array) $created; $this->assertEquals($device, array_intersect_key((array) $created, $device)); }
/** * Test update_users */ public function test_update_users() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $user1 = self::getDataGenerator()->create_user(); $user1 = array( 'id' => $user1->id, 'username' => 'usernametest1', 'password' => 'Moodle2012!', 'idnumber' => 'idnumbertest1', 'firstname' => 'First Name User Test 1', 'lastname' => 'Last Name User Test 1', 'email' => '*****@*****.**', 'description' => 'This is a description for user 1', 'city' => 'Perth', 'country' => 'au' ); $context = context_system::instance(); $roleid = $this->assignUserCapability('moodle/user:update', $context->id); // Call the external function. core_user_external::update_users(array($user1)); $dbuser = $DB->get_record('user', array('id' => $user1['id'])); $this->assertEquals($dbuser->username, $user1['username']); $this->assertEquals($dbuser->idnumber, $user1['idnumber']); $this->assertEquals($dbuser->firstname, $user1['firstname']); $this->assertEquals($dbuser->lastname, $user1['lastname']); $this->assertEquals($dbuser->email, $user1['email']); $this->assertEquals($dbuser->description, $user1['description']); $this->assertEquals($dbuser->city, $user1['city']); $this->assertEquals($dbuser->country, $user1['country']); // Call without required capability. $this->unassignUserCapability('moodle/user:update', $context->id, $roleid); $this->setExpectedException('required_capability_exception'); core_user_external::update_users(array($user1)); }
/** * Test for the type of the user-related properties in mod_assign_external::list_participants_returns(). */ public function test_list_participants_returns_user_property_types() { // Get user properties. $userdesc = core_user_external::user_description(); $this->assertTrue(isset($userdesc->keys)); $userproperties = array_keys($userdesc->keys); // Get returns description for mod_assign_external::list_participants_returns(). $listreturns = mod_assign_external::list_participants_returns(); $this->assertTrue(isset($listreturns->content)); $listreturnsdesc = $listreturns->content->keys; // Iterate over list returns description's keys. foreach ($listreturnsdesc as $key => $desc) { // Check if key exists in user properties and the description has a type attribute. if (in_array($key, $userproperties) && isset($desc->type)) { try { // The core_user::get_property_type() method might throw a coding_exception since // core_user_external::user_description() might contain properties that are not yet included in // core_user's $propertiescache. $propertytype = core_user::get_property_type($key); // Assert that user-related property types match those of the defined in core_user. $this->assertEquals($propertytype, $desc->type); } catch (coding_exception $e) { // All good. } } } }
/** * Get user information * * @param array $usernames array of user ids * @return array An array of arrays describing users */ public static function get_users_by_username($usernames) { global $CFG; require_once $CFG->dirroot . "/user/lib.php"; require_once $CFG->dirroot . "/user/profile/lib.php"; require_once $CFG->dirroot . "/user/externallib.php"; $params = self::validate_parameters(self::get_users_by_username_parameters(), array('usernames' => $usernames)); if (empty($params)) { header($_SERVER["SERVER_PROTOCOL"] . ' 422 Unprocessable Entity ($params[\'usernames\'] empty.)', true, 422); } // Changing out deprecated core function for new one. // get_users_by_field ONLY EXISTS IN MOODLE 2.5 AND ONWARDS! // TODO: Check if this is going to work, and fail gracefully if not. $users = core_user_external::get_users_by_field('username', $params['usernames']); $result = array(); foreach ($users as $user) { $context = context_user::instance($user['id']); try { self::validate_context($context); } catch (Exception $e) { $exceptionparam = new stdClass(); $exceptionparam->message = $e->getMessage(); $exceptionparam->userid = $user['id']; throw new moodle_exception(get_string('errorusercontextnotvalid', 'local_leapwebservices', $exceptionparam)); } require_capability('moodle/user:viewalldetails', $context); if (empty($user->deleted)) { $userarray = array(); $userarray['id'] = $user['id']; $userarray['username'] = $user['username']; $userarray['firstname'] = $user['firstname']; $userarray['lastname'] = $user['lastname']; $userarray['email'] = $user['email']; //$userarray['customfields'] = array(); //$customfields = profile_user_record($user->id); //$customfields = (array) $customfields; //foreach ($customfields as $key => $value) { // $userarray['customfields'][] = array('type' => $key, 'value' => $value); //} $result[] = $userarray; } } return $result; }
/** * Test agree_site_policy */ public function test_agree_site_policy() { global $CFG, $DB, $USER; $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); $this->setUser($user); // Site policy not set. $result = core_user_external::agree_site_policy(); $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('nositepolicy', $result['warnings'][0]['warningcode']); // Set a policy issue. $CFG->sitepolicy = 'https://moodle.org'; $this->assertEquals(0, $USER->policyagreed); $result = core_user_external::agree_site_policy(); $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertTrue($result['status']); $this->assertCount(0, $result['warnings']); $this->assertEquals(1, $USER->policyagreed); $this->assertEquals(1, $DB->get_field('user', 'policyagreed', array('id' => $USER->id))); // Try again, we should get a warning. $result = core_user_external::agree_site_policy(); $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result); $this->assertFalse($result['status']); $this->assertCount(1, $result['warnings']); $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']); // Set something to make require_login throws an exception. $otheruser = self::getDataGenerator()->create_user(); $this->setUser($otheruser); $DB->set_field('user', 'lastname', '', array('id' => $USER->id)); $USER->lastname = ''; try { $result = core_user_external::agree_site_policy(); $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown'); } catch (moodle_exception $e) { $this->assertEquals('usernotfullysetup', $e->errorcode); } catch (Exception $e) { $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.'); } }
/** * Test remove user device */ public function test_remove_user_device() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $device = array('appid' => 'com.moodle.moodlemobile', 'name' => 'occam', 'model' => 'Nexus 4', 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', 'uuid' => 'ABCDE3723ksdfhasfaasef859'); // A device with the same properties except the appid and pushid. $device2 = $device; $device2['pushid'] = "0987654321"; $device2['appid'] = "other.app.com"; $this->setAdminUser(); // Create a user device using the external API function. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); // Create the same device but for a different app. core_user_external::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'], $device2['version'], $device2['pushid'], $device2['uuid']); // Try to remove a device that does not exist. $result = core_user_external::remove_user_device('1234567890'); $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertFalse($result['removed']); $this->assertCount(1, $result['warnings']); // Try to remove a device that does not exist for an existing app. $result = core_user_external::remove_user_device('1234567890', $device['appid']); $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertFalse($result['removed']); $this->assertCount(1, $result['warnings']); // Remove an existing device for an existing app. This will remove one of the two devices. $result = core_user_external::remove_user_device($device['uuid'], $device['appid']); $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertTrue($result['removed']); // Remove all the devices. This must remove the remaining device. $result = core_user_external::remove_user_device($device['uuid']); $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result); $this->assertTrue($result['removed']); }
/** * Test update_picture disabled */ public function test_update_picture_disabled() { global $CFG; $this->resetAfterTest(true); $CFG->disableuserimages = true; $this->setAdminUser(); $this->expectException('moodle_exception'); core_user_external::update_picture(0); }
/** * Test add user device */ public function test_add_user_device() { global $USER, $CFG, $DB; $this->resetAfterTest(true); $device = array( 'appid' => 'com.moodle.moodlemobile', 'name' => 'occam', 'model' => 'Nexus 4', 'platform' => 'Android', 'version' => '4.2.2', 'pushid' => 'apushdkasdfj4835', 'uuid' => 'asdnfl348qlksfaasef859' ); // Call the external function. core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); $created = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); $created = (array) $created; $this->assertEquals($device, array_intersect_key((array)$created, $device)); // Test reuse the same pushid value. $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); // We need to execute the return values cleaning process to simulate the web service server. $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertCount(1, $warnings); // Test update and existing device. $device['pushid'] = 'different than before'; $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertEquals(1, $DB->count_records('user_devices')); $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid'])); $this->assertEquals($device, array_intersect_key((array)$updated, $device)); // Test creating a new device just changing the uuid. $device['uuid'] = 'newuidforthesameuser'; $device['pushid'] = 'new different than before'; $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'], $device['version'], $device['pushid'], $device['uuid']); $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings); $this->assertEquals(2, $DB->count_records('user_devices')); }
/** * Test get_user_preferences */ public function test_get_user_preferences() { $this->resetAfterTest(true); $user = self::getDataGenerator()->create_user(); set_user_preference('calendar_maxevents', 1, $user); set_user_preference('some_random_text', 'text', $user); $this->setUser($user); $result = core_user_external::get_user_preferences(); $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); // Expect 3, _lastloaded is always returned. $this->assertCount(3, $result['preferences']); foreach ($result['preferences'] as $pref) { if ($pref['name'] === '_lastloaded') { continue; } // Check we receive the expected preferences. $this->assertEquals(get_user_preferences($pref['name']), $pref['value']); } // Retrieve just one preference. $result = core_user_external::get_user_preferences('some_random_text'); $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['preferences']); $this->assertEquals('text', $result['preferences'][0]['value']); // Retrieve non-existent preference. $result = core_user_external::get_user_preferences('non_existent'); $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(1, $result['preferences']); $this->assertEquals(null, $result['preferences'][0]['value']); // Check that as admin we can retrieve all the preferences for any user. $this->setAdminUser(); $result = core_user_external::get_user_preferences('', $user->id); $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result); $this->assertCount(0, $result['warnings']); $this->assertCount(3, $result['preferences']); foreach ($result['preferences'] as $pref) { if ($pref['name'] === '_lastloaded') { continue; } // Check we receive the expected preferences. $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']); } // Check that as a non admin user we cannot retrieve other users preferences. $anotheruser = self::getDataGenerator()->create_user(); $this->setUser($anotheruser); $this->setExpectedException('required_capability_exception'); $result = core_user_external::get_user_preferences('', $user->id); }