Jump to content

[SOLVED]saving user and field in cron hook module


benbyf
 Share

Recommended Posts

Hello, having trouble getting PW to save my user or field in cron hook.

It seems to report the same field info each time unchanged. It should be counting down in seconds.

	public function init() {

		// set to every30Seconds in settings
		$this->defaultInterval = $this->cron_check;

		// add hooks to CRON
	    $this->addHookAfter("LazyCron::{$this->defaultInterval}", $this, 'checkUserSubscription');
	}

	public function ___checkUserSubscription(HookEvent $e){

		// seconds since last lazycron
		$seconds = $e->arguments[0];

		$log = wire('log');

		$field = $this->exclusiveRole;

		// users with specific role and field
		$users = $this->users->find("roles=$field");
		foreach ($users as $user) {

			// get stored time
			$currentTime = $user->get($this->fieldName);

			// if time not null
			if($currentTime){

				// evalute stored time minus time past
				$resultTime = intval($currentTime) - intval($seconds);

				$log->message("result ".$resultTime);

				// save user page
				$user->set($this->fieldName, $resultTime);
				$this->users->save($user);
		}
	}
}

 

Link to comment
Share on other sites

19 minutes ago, benbyf said:

$resultTime = intval($currentTime) - intval($seconds);

$log->message("result ".$resultTime); // save user page

$user->set($this->fieldName, $resultTime);

$this->users->save($user); }

think i fixed it! adding

$user->setOutputFormatting(false);

GRRRRRRRRRRRRR OF() trips me up everytime. is there a good reason why this is here?? can't it default to not always being a pain :(

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...