Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
InterestDAO.inc.php
1 <?php
2 
17 import('lib.pkp.classes.controlledVocab.ControlledVocabDAO');
18 
19 define('CONTROLLED_VOCAB_INTEREST', 'interest');
20 
22 
27  function build() {
28  return parent::build(CONTROLLED_VOCAB_INTEREST);
29  }
30 
36  function getUserInterestIds($userId) {
37  $controlledVocab = $this->build();
38  $result = $this->retrieveRange(
39  'SELECT cve.controlled_vocab_entry_id FROM controlled_vocab_entries cve, user_interests ui WHERE cve.controlled_vocab_id = ? AND ui.controlled_vocab_entry_id = cve.controlled_vocab_entry_id AND ui.user_id = ?',
40  array((int) $controlledVocab->getId(), (int) $userId)
41  );
42 
43  $ids = array();
44  while (!$result->EOF) {
45  $row = $result->GetRowAssoc(false);
46  $ids[] = $row['controlled_vocab_entry_id'];
47  $result->MoveNext();
48  }
49  $result->Close();
50 
51  return $ids;
52  }
53 
59  function getUserIdsByInterest($interest) {
60  $result = $this->retrieve('
61  SELECT ui.user_id
62  FROM user_interests ui
63  INNER JOIN controlled_vocab_entry_settings cves ON (ui.controlled_vocab_entry_id = cves.controlled_vocab_entry_id)
64  WHERE cves.setting_name = ? AND cves.setting_value = ?',
65  array('interest', $interest)
66  );
67 
68  $returner = array();
69  while (!$result->EOF) {
70  $row = $result->GetRowAssoc(false);
71  $returner[] = $row['user_id'];
72  $result->MoveNext();
73  }
74  $result->Close();
75  return $returner;
76 
77  }
78 
84  function getAllInterests($filter = null) {
85  $controlledVocab = $this->build();
86  $interestEntryDao = DAORegistry::getDAO('InterestEntryDAO');
87  $iterator = $interestEntryDao->getByControlledVocabId($controlledVocab->getId(), null, $filter);
88 
89  // Sort by name.
90  $interests = $iterator->toArray();
91  usort($interests, create_function('$s1, $s2', 'return strcmp($s1->getInterest(), $s2->getInterest());'));
92 
93  // Turn back into an iterator.
94  import('lib.pkp.classes.core.ArrayItemIterator');
95  return new ArrayItemIterator($interests);
96  }
97 
103  function setUserInterests($interests, $userId) {
104  // Remove duplicates
105  $interests = isset($interests) ? $interests : array();
106  $interests = array_unique($interests);
107 
108  // Trim whitespace
109  $interests = array_map('trim', $interests);
110 
111  // Delete the existing interests association.
112  $this->update(
113  'DELETE FROM user_interests WHERE user_id = ?',
114  array((int) $userId)
115  );
116 
117  $interestEntryDao = DAORegistry::getDAO('InterestEntryDAO'); /* @var $interestEntryDao InterestEntryDAO */
118  $controlledVocab = $this->build();
119 
120  // Store the new interests.
121  foreach ((array) $interests as $interest) {
122  $interestEntry = $interestEntryDao->getBySetting($interest, $controlledVocab->getSymbolic(),
123  $controlledVocab->getAssocId(), $controlledVocab->getAssocType(), $controlledVocab->getSymbolic()
124  );
125 
126  if(!$interestEntry) {
127  $interestEntry = $interestEntryDao->newDataObject(); /* @var $interestEntry InterestEntry */
128  $interestEntry->setInterest($interest);
129  $interestEntry->setControlledVocabId($controlledVocab->getId());
130  $interestEntry->setId($interestEntryDao->insertObject($interestEntry));
131  }
132 
133  $this->update(
134  'INSERT INTO user_interests (user_id, controlled_vocab_entry_id) VALUES (?, ?)',
135  array((int) $userId, (int) $interestEntry->getId())
136  );
137  }
138  }
139 }
140 
141 ?>
static & getDAO($name, $dbconn=null)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getUserIdsByInterest($interest)
Provides paging and iteration for arrays.
& retrieveRange($sql, $params=false, $dbResultRange=null, $callHooks=true)
Definition: DAO.inc.php:174
Operations for retrieving and modifying ControlledVocab objects.
getAllInterests($filter=null)
setUserInterests($interests, $userId)
Operations for retrieving and modifying a user&#39;s review interests.
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
getUserInterestIds($userId)