root/branches/ragnaroek/midcom/net.nemein.attention/calculator.php

Revision 18578, 5.1 kB (checked in by bergie, 2 years ago)

Find and fix most is_a calls to point to DBA classes, refs #456

Line 
1 <?php
2 /**
3  * @package net.nemein.attention
4  * @author The Midgard Project, http://www.midgard-project.org
5  * @version $Id$
6  * @copyright The Midgard Project, http://www.midgard-project.org
7  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
8  */
9
10 /**
11  * Attention profile analyzer class.
12  *
13  * @package net.nemein.attention
14  */
15 class net_nemein_attention_calculator extends midcom_baseclasses_components_purecode
16 {
17     /**
18      * Cached array of user's attention nodes
19      */
20     var $user_nodes = array();
21     
22     /**
23      * User to rate attention for
24      */
25     var $user = null;
26     
27     /**
28      * Profile to query attention for. By default all profiles are queried
29      */
30     var $profile = null;
31
32     /**
33      * Initializes the class.
34      *
35      * @param object $user     Midgard Person object to query attention for
36      * @param string $profile  Attention profile to use
37      */
38     function __construct($user = null, $profile = null)
39     {
40         $this->_component = 'net.nemein.attention';
41         parent::__construct();
42         
43         if (   $user
44             && is_object($user)
45             && $user->guid)
46         {
47             $this->user = $user;
48         }
49         elseif ($_MIDCOM->auth->user)
50         {
51             $this->user = $_MIDCOM->auth->user->get_storage();
52         }
53         
54         if ($profile)
55         {
56             $this->profile = $profile;
57         }
58     }
59     
60     /**
61      * Read user's concepts from database
62      */
63     private function read_user_concepts()
64     {
65         if (isset($this->user_nodes['concepts']))
66         {
67             // We already have the concepts populated
68             return;
69         }
70         
71         $this->user_nodes['concepts'] = array();
72         
73         if (!$this->user)
74         {
75             // No user, no attention data
76             return;
77         }
78         
79         $qb = net_nemein_attention_concept_dba::new_query_builder();
80         $qb->add_constraint('person', '=', $this->user->id);
81         
82         if ($this->profile)
83         {
84             $qb->add_constraint('profile', '=', $this->profile);
85         }
86         
87         $concepts = $qb->execute();
88         foreach ($concepts as $concept)
89         {
90             $this->user_nodes['concepts'][$concept->concept] = $concept->value;
91         }
92     }
93     
94     /**
95      * Give attention score for a set of concepts. 0 is neutral attention.
96      *
97      * @param array $concepts   Array of concepts (keywords) to rate for
98      * @return float Combined attention score of the concepts
99      */
100     function rate_concepts($concepts)
101     {
102         // Read user's attention concept nodes
103         $this->read_user_concepts();
104         
105         $score = 0;
106         
107         if (empty($this->user_nodes['concepts']))
108         {
109             return $score;
110         }
111         
112         foreach ($concepts as $concept)
113         {
114             if (isset($this->user_nodes['concepts'][$concept]))
115             {
116                 $score += $this->user_nodes['concepts'][$concept];
117             }
118         }
119         
120         return $score;
121     }
122
123     /**
124      * Give attention score for a set of authors. 0 is neutral attention.
125      *
126      * @param array $authors   Array of authors to rate for
127      * @return float Combined attention score of the authors
128      */
129     function rate_authors($authors)
130     {
131         return 0;
132     }
133
134     /**
135      * Give attention score for a source. 0 is neutral attention.
136      *
137      * @param string $source   Source to rate for
138      * @return float Combined attention score of a source
139      */
140     function rate_source($source)
141     {
142         return 0;
143     }
144
145     /**
146      * Give attention score for a Midgard object. 0 is neutral attention.
147      *
148      * This method reads the object for possible concepts, authors and sources
149      * and makes a combination rate based on them.
150      *
151      * @param string $source   Source to rate for
152      * @return float Combined attention score of a source
153      */
154     function rate_object($object)
155     {
156         $score = 0;
157         
158         // Read object tags as concepts
159         $_MIDCOM->load_library('net.nemein.tag');
160         $concepts = array();
161         $tags = net_nemein_tag_handler::get_tags_by_guid($object->guid);
162         foreach ($tags as $tag => $url)
163         {
164             $concepts[] = $tag;
165         }
166         
167         // Read possible article categories as concepts too
168         if (   is_a($object, 'midcom_baseclasses_database_article')
169             && strpos($object->extra1, '|') !== false)
170         {
171             $categories = explode('|', substr($object->extra1, 1, -1));
172             foreach ($categories as $category)
173             {
174                 if (empty($category))
175                 {
176                     continue;
177                 }
178                 
179                 $tag = strtolower($category);
180                 if (in_array($tag, $concepts))
181                 {
182                     // We have this already from tags, skip
183                     continue;
184                 }
185                 
186                 $concepts[] = $tag;
187             }
188         }
189         
190         // Rate by concepts
191         $score += $this->rate_concepts($concepts);
192         
193         return $score;
194     }
195 }
196 ?>
Note: See TracBrowser for help on using the browser.