1<?php
2 /**********************************************************************\
3 * phpGroupWare - KnowledgeBase						*
4 * http://www.phpgroupware.org						*
5 * This program is part of the GNU project, see http://www.gnu.org/	*
6 *									*
7 * Copyright 2003 Free Software Foundation, Inc.			*
8 *									*
9 * Originally Written by Dave Hall - <skwashd at phpgroupware.org>	*
10 * Loosely Based on phpBrain from Bart Veldhuizen - http://vrotvrot.com *
11 * This program is Free Software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or 	*
14 * at your option) any later version.					*
15 \**********************************************************************/
16 /* $Id: class.bokb.inc.php 21050 2010-03-25 22:43:48Z Caeies $ */
17
18	class bokb
19	{
20		var $cats;
21		var $rated;
22		var $so;
23		var $viewed;
24
25  		function bokb()
26  		{
27  			$this->cats = createObject('phpgwapi.categories');
28				$this->rated = $GLOBALS['phpgw']->session->appsession('rated','phpbrain');
29				$this->so = createObject('phpbrain.sokb');
30				$this->viewed = $GLOBALS['phpgw']->session->appsession('viewed','phpbrain');
31				$GLOBALS['phpgw_info']['apps']['phpkb']['config'] = $this->get_config();
32  		}
33
34		function get_cat_data($cat_id)
35		{
36			if($cat_id)
37			{
38				$level = 'subs';
39			}
40			else
41			{
42				$level = 'mains';
43			}
44
45			$cats = $this->cats->return_array($level, 0, False, '', '', '', False, $cat_id);
46
47			if( count($cats) )
48			{
49	  			foreach ($cats as $c_key => $c_vals)
50  				{
51  					$id = $c_vals['id'];
52  					$return_cats[$id] = array('name'	=> $c_vals['name'],
53								'num_entries'	=> $this->so->get_count($id)
54							);
55
56					$sub_cats = $this->cats->return_array('subs', 0, False, '', '', '', False, $id);
57					if( count($sub_cats) )
58					{
59						foreach($sub_cats as $sub_key => $sub_vals)
60						{
61							$sub_id = $sub_vals['id'];
62							$return_cats[$id]['subs'][$sub_id]
63								= array('name'	=> $sub_vals['name'],
64									'num_entries'	=> $this->so->get_count($sub_id)
65									);
66						}
67						unset($sub_cats);
68					}
69				}
70				return $return_cats;
71			}
72			else //no cats
73			{
74				return array();
75			}
76
77		}
78
79		function delete_comment($comment_id)
80		{
81			$comment_id = (int) $comment_id;
82			if($comment_id)
83			{
84				return $this->so->delete_comment($comment_id);
85			}
86			return false;
87		}
88
89		function delete_answer($answers)
90		{
91			return $this->so->delete_answer($answers);
92		}
93
94
95		function get_comments($faq_id)
96		{
97			$comments = $this->so->get_comments($faq_id);
98			if(is_array($comments))
99			{
100				foreach($comments as $key => $vals)
101				{
102					$comments[$key]['comment_date'] = date('d-M-Y', $vals['entered']);
103					$comments[$key]['comment_user'] = $GLOBALS['phpgw']->common->grab_owner_name($vals['user_id']);
104				}//end foreach(comment)
105			}//end is_array(comments)
106			return $comments;
107		}//end get_comments
108
109		function get_config()
110		{
111			if(!is_object($GLOBALS['phpgw']->config))
112			{
113				$config = createObject('phpgwapi.config');
114			}
115			else
116			{
117				$config = $GLOBALS['phpgw']->config;
118			}
119
120			$config->read_repository();
121			return $config->config_data;
122
123		}//end get_config
124
125		function get_faq_list($cat_id = '', $unpublished = false)
126		{
127			if(!$this->is_admin() && $unpublished)
128			{
129				$unpublished = false;
130			}
131
132			$faqs = $this->so->get_faq_list($cat_id, $unpublished);
133			if(is_array($faqs))
134			{
135  			foreach($faqs as $faq_id => $faq_vals)
136  			{
137  				$faqs[$faq_id]['vote_avg'] = (($faq_vals['total'] && $faq_vals['votes'])
138												? round(($faq_vals['total'] / $faq_vals['votes']),2) : 0);
139  				$faqs[$faq_id]['last_mod'] = date('d-M-Y', $faqs[$faq_id]['modified']);
140					$faqs[$faq_id]['score'] = '1.00';
141					$faqs[$faq_id]['title'] = ($item['is_faq']
142											? lang('question') . ': '. $faqs[$faq_id]['title']
143											: lang('tutorial') . ': '. $faqs[$faq_id]['title']);
144
145  			}
146			}
147			return $faqs;
148		}//end get_faq_list
149
150		function get_item($faq_id, $show_type = True)
151		{
152			$item = $this->so->get_item($faq_id, !@$this->viewed[$faq_id]);
153			if(is_array($item))
154			{
155  				$item['last_mod']	= date('d-M-Y', $item['modified']);
156				$item['username']	= $GLOBALS['phpgw']->common->grab_owner_name($item['user_id']);
157  				$item['rating']		= ($item['votes']
158								? round(($item['total']/$item['votes']),2) : 0);
159				$item['comments']	= $this->get_comments($faq_id);
160				if($show_type)
161				{
162					$item['title'] = ($item['is_faq']
163								? lang('faq') . ': '. $item['title']
164								: lang('tutorial') . ': '. $item['title']);
165				}
166				$this->viewed[$faq_id] = True;
167				$GLOBALS['phpgw']->session->appsession('viewed','phpbrain', $this->viewed);
168
169			}//end if is_array(item)
170
171			return $item;
172
173		}//end get_item
174
175		function get_latest()
176		{
177			return $this->so->get_latest();
178		}// end get_latest
179
180		function get_questions($pending = false)
181		{
182			if(!$this->is_admin() && $pending)
183			{
184				return null;
185			}
186			else
187			{
188				return $this->so->get_questions($pending);
189			}
190		}//end questions
191
192		function get_search_results($search, $show)
193		{
194			$results = $this->so->get_search_results($search, $show);
195			if(is_array($results))
196			{
197  			foreach($results as $id => $vals)
198  			{
199    				$results[$id]['vote_avg'] = (($vals['total'] && $vals['votes'])
200  												? round(($vals['total'] / $vals['votes']),2) : 0);
201    				$results[$id]['last_mod'] = date('d-M-Y', $vals['modified']);
202
203						$results[$id]['title'] = ($results[$id]['is_faq']
204												? lang('question') . ': '. $results[$id]['title']
205												: lang('tutorial') . ': '. $results[$id]['title']);
206  			}
207			}
208			return $results;
209		}//end get search results
210
211		function get_stats()
212		{
213			return $this->so->get_stats();
214		}//end get_stats
215
216		function is_admin()
217		{
218			return isset($GLOBALS['phpgw_info']['user']['apps']['admin']);
219		}//end is_admin
220
221		function is_anon()
222		{
223			return ($GLOBALS['phpgw_info']['apps']['phpkb']['config']['anon_user']
224						== $GLOBALS['phpgw_info']['user']['account_id']);
225		}//end is_anon
226
227		function save($faq_id, $faq, $question_id)
228		{
229			if(!$GLOBALS['phpgw_info']['apps']['phpkb']['config']['alow_tags'])
230			{
231  				$faq['title'] = strip_tags($faq['title']);
232  				$faq['keywords'] = strip_tags($faq['keywords']);
233  				$faq['text'] = strip_tags($faq['text']);
234			}
235
236			if($faq['published'] && !$this->is_admin())
237			{
238				$faq['published'] = False;
239			}
240			elseif($this->is_admin() && !$faq_id)
241			{
242				$faq['published'] = True;
243			}
244
245			$faq['user_id'] = (isset($faq['user_id']) ? $faq['user_id'] : $GLOBALS['phpgw_info']['user']['account_id']);
246			$new_faq_id = $this->so->save($faq_id, $faq, $this->is_admin());
247			if($new_faq_id && $question_id && !$faq_id)
248			{
249				$this->so->delete_question($question_id);
250			}
251			return $new_faq_id;
252
253		}//end save
254
255		function set_active_answer($faq_ids)
256		{
257			return $this->so->set_active_answer($faq_ids);
258		}//end set active answer
259
260		function set_active_question($question_ids)
261		{
262			return $this->so->set_active_question($question_ids);
263		}//end set active question
264
265		function set_comment($comment_id, $comment_data)
266		{
267			$comment_id = (int) $comment_id;
268			$comment_data['faq_id']	= (int) $comment_data['faq_id'];
269			$comment_data['user_id'] = $GLOBALS['phpgw_info']['user']['account_id'];
270			$this->so->set_comment($comment_id, $comment_data);
271		}//end set comment
272
273		function set_question($question)
274		{
275			return $this->so->set_question($question, $this->is_admin());
276		}//end set question
277
278		function set_rating($faq_id, $rating)
279		{
280			if(!@$this->rated[$faq_id])//only rate if not already done so
281			{
282				//make sure values are within a valid range
283				$rating = ($rating < 1 ? 1 : $rating);
284				$rating = ($rating > 5 ? 5 : $rating);
285
286				$this->so->set_rating($faq_id, $rating);
287				$this->rated[$faq_id] = True;
288				$GLOBALS['phpgw']->session->appsession('rated','phpbrain', $this->rated);
289			}
290		}//end set_rating
291
292	}//end class bokb
293
294
295