ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
fixTestQuestionOrderingGaps.php
Go to the documentation of this file.
1 <?php
2 // ---------------------------------------------------------------------------------------------------------------------
3 
34 // ---------------------------------------------------------------------------------------------------------------------
35 
36 $task = 'show';
37 
38 if( PHP_SAPI == 'cli' )
39 {
40  include_once('include/inc.ilias_version.php');
41 
42  if($_SERVER['argc'] < 4)
43  {
44  die("Usage: php fixTestQuestionOrderingGaps.php username password client [show|fix]\n");
45  }
46 
47  if( version_compare(ILIAS_VERSION_NUMERIC, '5.2.0', '>=') )
48  {
49  include_once './Services/Cron/classes/class.ilCronStartUp.php';
50 
51  $cron = new ilCronStartUp($_SERVER['argv'][3], $_SERVER['argv'][1], $_SERVER['argv'][2]);
52 
53  try
54  {
55  $cron->initIlias();
56  $cron->authenticate();
57  }
58  catch (Exception $e)
59  {
60  echo 'Unknown trouble during ilInit. Exception: ' . $e->getMessage();
61  exit(126);
62  }
63  }
64  else
65  {
66  include_once "Services/Context/classes/class.ilContext.php";
68 
69  include_once 'Services/Authentication/classes/class.ilAuthFactory.php';
71 
72  $_COOKIE["ilClientId"] = $_SERVER['argv'][3];
73  $_POST['username'] = $_SERVER['argv'][1];
74  $_POST['password'] = $_SERVER['argv'][2];
75 
76  try
77  {
78  require_once 'include/inc.header.php';
79  }
80  catch(Exception $e)
81  {
82  echo 'Unknown trouble during ilInit. Exception: '.$e->getMessage();
83  exit(126);
84  }
85  }
86 
87  if( isset($_SERVER['argv'][4]) )
88  {
89  switch($_SERVER['argv'][4])
90  {
91  case 'show':
92  case 'fix':
93 
94  $task = $_SERVER['argv'][4];
95  }
96  }
97 
98  $PROCEED = "
99 
100  To fix your database call this script with the additional parameter 'fix' (!)
101 
102  Usage: php fixTestQuestionOrderingGaps.php username password client [show|fix]
103 
104  ";
105 }
106 else
107 {
108  if( isset($_GET['fix']) )
109  {
110  $task = 'fix';
111  }
112 
113  $URL = "<br />
114  <a href='http://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}?fix'>HTTP</a>
115  or
116  <a href='https://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}?fix'>HTTPS</a>
117  ";
118 
120 
121  To fix your database use the link below (!)
122  {$URL}
123 
124  ";
125 
126  require_once 'include/inc.header.php';
127 }
128 
129 if(!$rbacsystem->checkAccess('visible,read', SYSTEM_FOLDER_ID))
130 {
131  echo 'Sorry, this script requires administrative privileges!';
132  exit(125);
133 }
134 
135 // ---------------------------------------------------------------------------------------------------------------------
136 
137 if( $task == 'show' )
138 {
139  $res = $ilDB->query("
140  SELECT COUNT(DISTINCT test_fi) num_tst, COUNT(test_question_id) num_qst
141  FROM tst_test_question WHERE test_fi IN(
142  SELECT test_fi FROM tst_test_question
143  GROUP BY test_fi HAVING COUNT(test_fi) < MAX(sequence)
144  )
145  ");
146 
147  $row = $ilDB->fetchAssoc($res);
148 
149 
150  if( $row )
151  {
152  $numTests = $row['num_tst'];
153  $numQuestions = $row['num_qst'];
154  echo "<pre>
155 
156  DEAR ADMINISTRATOR !!
157 
158  Please read the following instructions CAREFULLY!
159 
160  -> Due to a bug in almost all earlier versions of ILIAS question orderings
161  from the assessment component are broken but repairable.
162 
163  -> This fixing script can exhaust any php enviroment settings like
164  max_execution_time or memory_limit for example.
165 
166  -> In the case of any php fatal error the script causes that is about exhausting
167  any ressource or time restriction you just need to refresh the page by using F5 for example.
168 
169  Mantis Bug Report: https://ilias.de/mantis/view.php?id=20382
170 
171  In your database there were > {$numTests} tests < detected having > {$numQuestions} questions < overall,
172  that are stored with gaps in the ordering index.
173 
174  </pre>
175 
176  {$PROCEED}
177 
178  ";
179  }
180 }
181 elseif( $task == 'fix' )
182 {
183  $res = $ilDB->query("
184  SELECT test_fi, test_question_id
185  FROM tst_test_question WHERE test_fi IN(
186  SELECT test_fi FROM tst_test_question
187  GROUP BY test_fi HAVING COUNT(test_fi) < MAX(sequence)
188  ) ORDER BY test_fi ASC, sequence ASC
189  ");
190 
191  $tests = array();
192 
193  while($row = $ilDB->fetchAssoc($res))
194  {
195  if( !isset($tests[ $row['test_fi'] ]) )
196  {
197  $tests[ $row['test_fi'] ] = array();
198  }
199 
200  $tests[ $row['test_fi'] ][] = $row['test_question_id'];
201  }
202 
203  foreach($tests as $testFi => $testQuestions)
204  {
205  for($i = 0, $m = count($testQuestions); $i <= $m; $i++)
206  {
207  $testQuestionId = $testQuestions[$i];
208 
209  $position = $i + 1;
210 
211  $ilDB->update('tst_test_question',
212  array( 'sequence' => array('integer', $position) ),
213  array( 'test_question_id' => array('integer', $testQuestionId) )
214  );
215  }
216  }
217 
218  echo "fixing TestQuestionOrderingGaps successfully finished :-)\n";
219 }
220 
221 // ---------------------------------------------------------------------------------------------------------------------
exit
Definition: login.php:54
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$_POST['username']
Definition: cron.php:12
const ILIAS_VERSION_NUMERIC
$_GET["client_id"]
const CONTEXT_CRON
$_COOKIE["ilClientId"]
Definition: cron.php:11
static setContext($a_context)
set context
static init($a_type)
Init context by type.
global $ilDB
$task
GENERAL INFORMATION: