ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.SurveyCategories.php
Go to the documentation of this file.
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 include_once "Modules/SurveyQuestionPool/classes/class.ilSurveyCategory.php";
24 
35 {
36 
40  protected $log;
41 
50  public $categories;
51 
56  public function __construct()
57  {
58  $this->categories = array();
59  $this->log = ilLoggerFactory::getLogger("svy");
60  }
61 
71  public function getCategoryCount()
72  {
73  return count($this->categories);
74  }
75 
86  public function addCategoryAtPosition($categoryname, $position, $other = 0, $neutral = 0, $label = null)
87  {
88  if (array_key_exists($position, $this->categories)) {
89  $head = array_slice($this->categories, 0, $position);
90  $tail = array_slice($this->categories, $position);
91  $this->categories = array_merge($head, array(new ilSurveyCategory($categoryname, $other, $neutral, $label)), $tail);
92  } else {
93  array_push($this->categories, new ilSurveyCategory($categoryname, $other, $neutral, $label));
94  }
95  }
96 
97  public function moveCategoryUp($index)
98  {
99  if ($index > 0) {
100  $temp = $this->categories[$index-1];
101  $this->categories[$index - 1] = $this->categories[$index];
102  $this->categories[$index] = $temp;
103  }
104  }
105 
106  public function moveCategoryDown($index)
107  {
108  if ($index < (count($this->categories)-1)) {
109  $temp = $this->categories[$index+1];
110  $this->categories[$index + 1] = $this->categories[$index];
111  $this->categories[$index] = $temp;
112  }
113  }
114 
124  public function addCategory($categoryname, $other = 0, $neutral = 0, $label = null, $scale = null)
125  {
126  array_push($this->categories, new ilSurveyCategory($categoryname, $other, $neutral, $label, $scale));
127  }
128 
138  public function addCategoryArray($categories)
139  {
140  $this->categories = array_merge($this->categories, $categories);
141  }
142 
152  public function removeCategory($index)
153  {
154  unset($this->categories[$index]);
155  $this->categories = array_values($this->categories);
156  }
157 
167  public function removeCategories($array)
168  {
169  foreach ($array as $index) {
170  unset($this->categories[$index]);
171  }
172  $this->categories = array_values($this->categories);
173  }
174 
184  public function removeCategoryWithName($name)
185  {
186  foreach ($this->categories as $index => $category) {
187  if (strcmp($category->title, $name) == 0) {
188  $this->removeCategory($index);
189  return;
190  }
191  }
192  }
193 
204  public function getCategory($index)
205  {
206  if (array_key_exists($index, $this->categories)) {
207  return $this->categories[$index];
208  } else {
209  return "";
210  }
211  }
212 
219  public function getCategoryForScale($scale)
220  {
221  foreach ($this->categories as $cat) {
222  if ($cat->scale == $scale) {
223  return $cat;
224  }
225  }
226  return null;
227  }
228 
236  public function getCategoryIndex($name)
237  {
238  foreach ($this->categories as $index => $category) {
239  if (strcmp($category->title, $name) == 0) {
240  return $index;
241  }
242  }
243  return null;
244  }
245 
253  public function getIndex($category)
254  {
255  foreach ($this->categories as $index => $cat) {
256  if ($cat == $category) {
257  return $index;
258  }
259  }
260  return null;
261  }
262 
263  public function getNewScale()
264  {
265  $max = 0;
266  foreach ($this->categories as $index => $category) {
267  if (is_object($category) && $category->scale > 0) {
268  if ($category->scale > $max) {
269  $max = $category->scale;
270  }
271  }
272  }
273  return $max+1;
274  }
275 
276  public function getScale($index)
277  {
278  $obj = $this->categories[$index];
279  if (is_object($obj) && $obj->scale > 0) {
280  $this->log->debug("getScale has scale =" . $obj->scale);
281  return $obj->scale;
282  } else {
283  $obj->scale = $this->getNewScale();
284  $this->log->debug("getScale needed new scale, scale =" . $obj->scale);
285  return $obj->scale;
286  }
287  }
288 
297  public function flushCategories()
298  {
299  $this->categories = array();
300  }
301 
308  public function getCategories()
309  {
310  return $this->categories;
311  }
312 }
getCategory($index)
Returns the name of a category for a given index.
getCategoryIndex($name)
Returns the index of a category with a given name.
getCategoryCount()
Returns the number of categories.
getIndex($category)
Returns the index of a category.
removeCategory($index)
Removes a category from the list of categories.
getCategories()
Get categories.
getCategoryForScale($scale)
Returns the name of a category for a given index.
addCategoryArray($categories)
Adds a category array.
removeCategoryWithName($name)
Removes a category from the list of categories.
$index
Definition: metadata.php:60
addCategory($categoryname, $other=0, $neutral=0, $label=null, $scale=null)
Adds a category.
flushCategories()
Empties the categories list.
removeCategories($array)
Removes many categories from the list of categories.
Class SurveyCategories.
if($format !==null) $name
Definition: metadata.php:146
addCategoryAtPosition($categoryname, $position, $other=0, $neutral=0, $label=null)
Adds a category at a given position.
Create styles array
The data for the language used.
static getLogger($a_component_id)
Get component logger.
Survey category class.
__construct()
Constructor public.