1<?php
2 /*
3   +----------------------------------------------------------------------------+
4   | ILIAS open source                                                          |
5   +----------------------------------------------------------------------------+
6   | Copyright (c) 1998-2001 ILIAS open source, University of Cologne           |
7   |                                                                            |
8   | This program is free software; you can redistribute it and/or              |
9   | modify it under the terms of the GNU General Public License                |
10   | as published by the Free Software Foundation; either version 2             |
11   | of the License, or (at your option) any later version.                     |
12   |                                                                            |
13   | This program is distributed in the hope that it will be useful,            |
14   | but WITHOUT ANY WARRANTY; without even the implied warranty of             |
15   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              |
16   | GNU General Public License for more details.                               |
17   |                                                                            |
18   | You should have received a copy of the GNU General Public License          |
19   | along with this program; if not, write to the Free Software                |
20   | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21   +----------------------------------------------------------------------------+
22*/
23
24/**
25* Class for search actions in ILIAS survey tool
26*
27* The SurveySearch class defines and encapsulates basic methods and attributes
28* to search the ILIAS survey tool for questions.
29*
30* @author		Helmut Schottmüller <helmut.schottmueller@mac.com>
31* @version	$Id$
32* @ingroup ModulesSurvey
33*/
34class SurveySearch
35{
36    /**
37     * @var ilRbacSystem
38     */
39    protected $rbacsystem;
40
41    const CONCAT_AND = 0;
42    const CONCAT_OR = 1;
43
44    /**
45    * Search terms
46    *
47    * An array containing all search terms
48    *
49    * @var array
50    */
51    public $search_terms;
52
53    /**
54    * Concatenation
55    *
56    * The concatenation type of the search terms
57    *
58    * @var integer
59    */
60    public $concatenation;
61
62    /**
63    * Search field
64    *
65    * A database field to restrict the search results
66    *
67    * @var string
68    */
69    public $search_field;
70
71    /**
72    * Search type
73    *
74    * A question type to restrict the search results
75    *
76    * @var string
77    */
78    public $search_type;
79
80    /**
81    * Search results
82    *
83    * An array containing the results of a search
84    *
85    * @var array
86    */
87    public $search_results;
88
89    /**
90    * The reference to the ILIAS database class
91    *
92    * The reference to the ILIAS database class
93    *
94    * @var object
95    */
96    public $ilDB;
97
98
99    /**
100    * SurveySearch constructor
101    *
102    * The constructor takes possible arguments an creates an instance of the SurveySearch object.
103    *
104    * @param string $title A title string to describe the question
105    * @param string $description A description string to describe the question
106    * @param string $author A string containing the name of the questions author
107    * @param integer $owner A numerical ID to identify the owner/creator
108    * @access public
109    */
110    public function __construct($search_text = "", $concatenation = self::CONCAT_AND, $search_field = "all", $search_type = "all")
111    {
112        global $DIC;
113
114        $this->rbacsystem = $DIC->rbac()->system();
115        $ilDB = $DIC->database();
116
117        $this->ilDB = $ilDB;
118
119        $this->search_terms = explode(" +", $search_text);
120        $this->concatenation = $concatenation;
121        $this->search_field = $search_field;
122        $this->search_type = $search_type;
123        $this->search_results = array();
124    }
125
126    /**
127    * Executes a search
128    *
129    * Executes a search
130    *
131    * @access public
132    */
133    public function search()
134    {
135        $ilDB = $this->ilDB;
136
137        $where = "";
138        $fields = array();
139        if (strcmp($this->search_type, "all") != 0) {
140            $where = "svy_qtype.type_tag = " . $ilDB->quote($this->search_type, 'text');
141        }
142        foreach ($this->search_terms as $term) {
143            switch ($this->search_field) {
144                case "all":
145                    $fields["$term"] = array();
146                    array_push($fields["$term"], $ilDB->like("svy_question.title", 'text', "%" . $term . "%"));
147                    array_push($fields["$term"], $ilDB->like("svy_question.description", 'text', "%" . $term . "%"));
148                    array_push($fields["$term"], $ilDB->like("svy_question.author", 'text', "%" . $term . "%"));
149                    array_push($fields["$term"], $ilDB->like("svy_question.questiontext", 'text', "%" . $term . "%"));
150                    break;
151                default:
152                    $fields["$term"] = array();
153                    array_push($fields["$term"], $ilDB->like("svy_question." . $this->search_field, 'text', "%" . $term . "%"));
154                    break;
155            }
156        }
157        $cumulated_fields = array();
158        foreach ($fields as $params) {
159            array_push($cumulated_fields, "(" . join(" OR ", $params) . ")");
160        }
161        $str_where = "";
162        if ($this->concatenation == self::CONCAT_AND) {
163            $str_where = "(" . join(" AND ", $cumulated_fields) . ")";
164        } else {
165            $str_where = "(" . join(" OR ", $cumulated_fields) . ")";
166        }
167        if ($str_where) {
168            $str_where = " AND $str_where";
169        }
170        if ($where) {
171            $str_where .= " AND (" . $where . ")";
172        }
173        $result = $ilDB->query("SELECT svy_question.*, svy_qtype.type_tag, object_reference.ref_id FROM " .
174            "svy_question, svy_qtype, object_reference WHERE svy_question.questiontype_fi = svy_qtype.questiontype_id " .
175            "AND svy_question.original_id IS NULL AND svy_question.obj_fi = object_reference.obj_id AND " .
176            "svy_question.obj_fi > 0$str_where");
177        $result_array = array();
178        $rbacsystem = $this->rbacsystem;
179        if ($result->numRows() > 0) {
180            while ($row = $ilDB->fetchAssoc($result)) {
181                if (($row["complete"] == 1) and ($rbacsystem->checkAccess('write', $row["ref_id"]))) {
182                    array_push($result_array, $row);
183                }
184            }
185        }
186        $this->search_results = &$result_array;
187    }
188}
189