Skip to content
Snippets Groups Projects
Commit 3c869c77 authored by Patrick Barroca's avatar Patrick Barroca :grin:
Browse files

dev #56994 : update rating user key when giving it to another user

parent 790ae2c3
Branches
Tags
5 merge requests!2080Sandbox detach zf from storm,!2061Master,!2060Master,!2059Hotline master,!2056Dev#56994 migration des comptes lecteurs chambery
......@@ -198,6 +198,14 @@ class AvisNoticeLoader extends Storm_Model_Loader {
foreach ($comments as $comment)
$comment->fixLostUserId();
}
public function keyForUser($user) {
return $user
? sprintf('%s--%s--%s',
$user->getIdabon(), $user->getIdSite(), $user->getLogin())
: '';
}
}
......@@ -412,8 +420,7 @@ class Class_AvisNotice extends Storm_Model_Abstract {
$user = $this->getUser();
if (null !== $user)
$this->setUserKey(sprintf('%s--%s--%s',
$user->getIdabon(), $user->getIdSite(), $user->getLogin()));
$this->setUserKey($this->getLoader()->keyForUser($user));
$this->setAbonOuBib((null !== $user) && $user->isBibliothecaire() ? 1 : 0);
}
......
......@@ -21,35 +21,36 @@
class Class_User_Datas extends Class_Entity {
protected $_relations = [['notices_paniers', 'id_user'],
['notices_avis', 'id_user'],
['cms_avis', 'id_user'],
['suggestion_achat', 'user_id'],
['session_formation_inscriptions', 'stagiaire_id'],
['user_group_memberships', 'user_id'],
['formulaires', 'id_user'],
['multimedia_devicehold', 'id_user']];
protected $_relations = [];
public function __construct($user) {
$this
->setUser($user)
->setSql(Zend_Registry::get('sql'));
$this->setUser($user);
$this->_relations = [new Class_User_DatasRelation('notices_paniers', 'id_user'),
new Class_User_DatasRelationAvisNotice(),
new Class_User_DatasRelation('cms_avis', 'id_user'),
new Class_User_DatasRelation('suggestion_achat', 'user_id'),
new Class_User_DatasRelation('session_formation_inscriptions', 'stagiaire_id'),
new Class_User_DatasRelation('user_group_memberships', 'user_id'),
new Class_User_DatasRelation('formulaires', 'id_user'),
new Class_User_DatasRelation('multimedia_devicehold', 'id_user')];
}
public function hasDatas() {
$pattern = 'select count(id) from %s where %s=' . $this->getUser()->getId();
foreach($this->_relations as $relation) {
$result = $this->getSql()
->fetchOne(sprintf($pattern, $relation[0], $relation[1]));
if (0 < $result)
foreach($this->_relations as $relation)
if (0 < $this->_getCountOf($relation))
return true;
}
return false;
}
protected function _getCountOf($relation) {
return $relation->countFor($this->getUser());
}
public function giveTo($params, $for_real) {
if (!$target = $this->_getTargetWith($params))
return false;
......@@ -57,14 +58,19 @@ class Class_User_Datas extends Class_Entity {
if (!$for_real)
return $target;
$pattern = 'update %s set %s=' . $target->getId() . ' where %s=' . $this->getUser()->getId();
foreach($this->_relations as $relation)
$this->getSql()->query(sprintf($pattern, $relation[0], $relation[1], $relation[1]));
$this->_giveRelationTo($relation, $target);
return $target;
}
protected function _giveRelationTo($relation, $target) {
$relation->giveFromTo($this->getUser(), $target);
return $this;
}
public function _getTargetWith($params) {
$targets = Class_Users::findAllBy($params);
if (!$targets) {
......@@ -80,3 +86,45 @@ class Class_User_Datas extends Class_Entity {
return current($targets);
}
}
class Class_User_DatasRelation extends Class_Entity {
public function __construct($table, $key) {
$this->setTable($table)
->setKey($key)
->setSql(Zend_Registry::get('sql'));
}
public function countFor($user) {
return $this
->getSql()
->fetchOne(sprintf('select count(id) from %s where %s=%s',
$this->getTable(), $this->getKey(), $user->getId()));
}
public function giveFromTo($from, $to) {
$this
->getSql()
->query(sprintf('update %s set %s=' . $to->getId() . ' where %s=' . $from->getId(),
$this->getTable(), $this->getKey(), $this->getKey()));
}
}
class Class_User_DatasRelationAvisNotice extends Class_User_DatasRelation {
public function __construct() {
parent::__construct('notices_avis', 'id_user');
}
public function giveFromTo($from, $to) {
parent::giveFromTo($from, $to);
$this->getSql()
->query('update notice_avis set user_key=\'' . Class_AvisNotice::keyForUser($to) . '\' where id_user=' . $to->getId());
}
}
......@@ -108,8 +108,22 @@ class UserDatasTest extends UserDatasTestCase {
$query[0], $query[1], $query[1]))
->answers(1);
$this->_sql->whenCalled('query')
->with('update notice_avis set user_key=\'--0--actarus\' where id_user=45')
->answers(1);
$this->assertEquals(45,
$this->_datas->giveTo(['login' => 'actarus'], true)->getId());
$this
->assertTrue($this->_sql
->methodHasBeenCalledWithParams('query',
['update notices_avis set id_user=45 where id_user=44']));
$this
->assertTrue($this->_sql
->methodHasBeenCalledWithParams('query',
['update notice_avis set user_key=\'--0--actarus\' where id_user=45']));
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment