ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.SurveyCategories.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
11 {
12 
16  protected $log;
17 
26  public $categories;
27 
32  public function __construct()
33  {
34  $this->categories = array();
35  $this->log = ilLoggerFactory::getLogger("svy");
36  }
37 
47  public function getCategoryCount()
48  {
49  return count($this->categories);
50  }
51 
62  public function addCategoryAtPosition($categoryname, $position, $other = 0, $neutral = 0, $label = null)
63  {
64  if (array_key_exists($position, $this->categories)) {
65  $head = array_slice($this->categories, 0, $position);
66  $tail = array_slice($this->categories, $position);
67  $this->categories = array_merge($head, array(new ilSurveyCategory($categoryname, $other, $neutral, $label)), $tail);
68  } else {
69  array_push($this->categories, new ilSurveyCategory($categoryname, $other, $neutral, $label));
70  }
71  }
72 
73  public function moveCategoryUp($index)
74  {
75  if ($index > 0) {
76  $temp = $this->categories[$index - 1];
77  $this->categories[$index - 1] = $this->categories[$index];
78  $this->categories[$index] = $temp;
79  }
80  }
81 
82  public function moveCategoryDown($index)
83  {
84  if ($index < (count($this->categories) - 1)) {
85  $temp = $this->categories[$index + 1];
86  $this->categories[$index + 1] = $this->categories[$index];
87  $this->categories[$index] = $temp;
88  }
89  }
90 
100  public function addCategory($categoryname, $other = 0, $neutral = 0, $label = null, $scale = null)
101  {
102  array_push($this->categories, new ilSurveyCategory($categoryname, $other, $neutral, $label, $scale));
103  }
104 
114  public function addCategoryArray($categories)
115  {
116  $this->categories = array_merge($this->categories, $categories);
117  }
118 
128  public function removeCategory($index)
129  {
130  unset($this->categories[$index]);
131  $this->categories = array_values($this->categories);
132  }
133 
143  public function removeCategories($array)
144  {
145  foreach ($array as $index) {
146  unset($this->categories[$index]);
147  }
148  $this->categories = array_values($this->categories);
149  }
150 
160  public function removeCategoryWithName($name)
161  {
162  foreach ($this->categories as $index => $category) {
163  if (strcmp($category->title, $name) == 0) {
164  $this->removeCategory($index);
165  return;
166  }
167  }
168  }
169 
180  public function getCategory($index)
181  {
182  if (array_key_exists($index, $this->categories)) {
183  return $this->categories[$index];
184  } else {
185  return "";
186  }
187  }
188 
195  public function getCategoryForScale($scale)
196  {
197  foreach ($this->categories as $cat) {
198  if ($cat->scale == $scale) {
199  return $cat;
200  }
201  }
202  return null;
203  }
204 
212  public function getCategoryIndex($name)
213  {
214  foreach ($this->categories as $index => $category) {
215  if (strcmp($category->title, $name) == 0) {
216  return $index;
217  }
218  }
219  return null;
220  }
221 
229  public function getIndex($category)
230  {
231  foreach ($this->categories as $index => $cat) {
232  if ($cat == $category) {
233  return $index;
234  }
235  }
236  return null;
237  }
238 
239  public function getNewScale()
240  {
241  $max = 0;
242  foreach ($this->categories as $index => $category) {
243  if (is_object($category) && $category->scale > 0) {
244  if ($category->scale > $max) {
245  $max = $category->scale;
246  }
247  }
248  }
249  return $max + 1;
250  }
251 
252  public function getScale($index)
253  {
254  $obj = $this->categories[$index];
255  if (is_object($obj) && $obj->scale > 0) {
256  $this->log->debug("getScale has scale =" . $obj->scale);
257  return $obj->scale;
258  } else {
259  $obj->scale = $this->getNewScale();
260  $this->log->debug("getScale needed new scale, scale =" . $obj->scale);
261  return $obj->scale;
262  }
263  }
264 
273  public function flushCategories()
274  {
275  $this->categories = array();
276  }
277 
284  public function getCategories()
285  {
286  return $this->categories;
287  }
288 }
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.
addCategory($categoryname, $other=0, $neutral=0, $label=null, $scale=null)
Adds a category.
flushCategories()
Empties the categories list.
$index
Definition: metadata.php:128
removeCategories($array)
Removes many categories from the list of categories.
if($format !==null) $name
Definition: metadata.php:230
Class SurveyCategories.
addCategoryAtPosition($categoryname, $position, $other=0, $neutral=0, $label=null)
Adds a category at a given position.
static getLogger($a_component_id)
Get component logger.
Survey category class.
__construct()
Constructor public.