ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilDataLoader.php
Go to the documentation of this file.
1 <?php
2  /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
13  {
14 
20  function setEnableLog($a_val)
21  {
22  $this->enable_log = $a_val;
23  }
24 
30  function getEnableLog()
31  {
32  return $this->enable_log;
33  }
34 
41  function log($a_str)
42  {
43  if ($this->getEnableLog())
44  {
45  echo "<br>".$a_str; flush();
46  }
47  }
48 
55  function generateUsers($a_login_base = "learner", $a_start = 1,
56  $a_end = 1000, $a_firstname = "John", $a_lastname_base = "Learner", $a_pw = "learnerpw",
57  $a_email = "de@de.de", $a_gender = "m", $a_lang = "en")
58  {
59  global $rbacadmin;
60 
61  // new users
62  $this->log("Creating Users");
63  for ($i = $a_start; $i <= $a_end; $i++)
64  {
65  $this->log($a_login_base.$i);
66  $user = new ilObjUser();
67  $user->setLogin($a_login_base.$i);
68  $user->setFirstname($a_firstname);
69  $user->setLastname($a_lastname_base." ".$i);
70  $user->setGender($a_gender);
71  $user->setEmail($a_email);
72  $user->setAgreeDate(ilUtil::now());
73  $user->setPasswd($a_pw, IL_PASSWD_PLAIN);
74  $user->setTitle($user->getFullname());
75  $user->setDescription($user->getEmail());
76  $user->setLastPasswordChangeTS( time() );
77  $user->setActive(true);
78  $user->setTimeLimitUnlimited(true);
79  $user->create();
80  $user->setLanguage($a_lang);
81  $user->saveAsNew(false);
82  $user->writePrefs();
83  $rbacadmin->assignUser(4, $user->getId(),true);
84  }
85  }
86 
93  function generateCategories($a_start = 1, $a_end = 500, $a_sub_cats_per_cat = 10, $a_title_base = "Category",
94  $a_node = "", $a_init_cnt = true, $a_depth = 1)
95  {
96  global $tree;
97 
98  include_once("./Modules/Category/classes/class.ilObjCategory.php");
99 
100  if ($a_init_cnt)
101  {
102  $this->item_cnt = $a_start;
103  $this->max_depth = ceil(log($a_end - $a_start, $a_sub_cats_per_cat));
104  }
105 
106  if ($a_depth > $this->max_depth)
107  {
108  return;
109  }
110 
111  if ($a_node == "")
112  {
113  $a_node = $tree->getRootId();
114  $this->log("Creating Categories");
115  }
116 
117  $sub_cat_cnt = 0;
118  $sub_cats = array();
119  while ($sub_cat_cnt < $a_sub_cats_per_cat && $this->item_cnt <= $a_end)
120  {
121  if ($this->item_cnt <= $a_end)
122  {
123  $this->log($a_title_base." ".$this->item_cnt);
124  $new_cat = new ilObjCategory();
125  $new_cat->setTitle($a_title_base." ".$this->item_cnt);
126  $new_cat->create();
127  $new_cat->createReference();
128  $new_cat->putInTree($a_node);
129  $new_cat->setPermissions($a_node);
130  $sub_cats[] = $new_cat;
131  $sub_cat_cnt++;
132  $this->item_cnt++;
133  }
134  }
135 
136  foreach ($sub_cats as $sub_cat)
137  {
138  $this->generateCategories($this->item_cnt, $a_end, $a_sub_cats_per_cat, $a_title_base,
139  $sub_cat->getRefId(), false, $a_depth + 1);
140  }
141  }
142 
149  function generateCourses($a_start = 1, $a_end = 500, $a_course_per_cat = 10,
150  $a_title_base = "Course")
151  {
152  global $tree;
153 
154  include_once("./Modules/Course/classes/class.ilObjCourse.php");
155 
156  $this->log("Creating Courses");
157 
158  $a_current = $a_start;
159 
160  // how many categories do we need?
161  $needed_cats = ceil(($a_end - $a_start + 1) / $a_course_per_cat);
162 
163  // get all categories and sort them by depth
164  $nodes = $tree->getFilteredSubTree($tree->getRootId(), array("adm", "crs", "fold", "grp"));
165  $nodes = ilUtil::sortArray($nodes, "depth", "desc");
166 
167  foreach ($nodes as $node)
168  {
169  if ($node["type"] == "cat" && $a_current <= $a_end)
170  {
171  for ($i = 1; $i <= $a_course_per_cat; $i++)
172  {
173  if ($a_current <= $a_end)
174  {
175  $this->log($a_title_base." ".$a_current);
176  $new_crs = new ilObjCourse();
177  $new_crs->setTitle($a_title_base." ".$a_current);
178  $new_crs->create();
179  $new_crs->createReference();
180  $new_crs->putInTree($node["child"]);
181  $new_crs->setPermissions($node["child"]);
182 
183  $a_current++;
184  }
185  }
186  }
187  }
188  }
189 
196  function generateFiles($a_test_file, $a_files_per_course = 10, $a_title_base = "File")
197  {
198  global $tree;
199 
200  include_once("./Modules/File/classes/class.ilObjFile.php");
201 
202  $this->log("Creating Files");
203 
204  $a_current = $a_start;
205 
206  // get all categories and sort them by depth
207  $crs_ref_ids = ilUtil::_getObjectsByOperations("crs", "read",
208  0, $limit = 1000000);
209  $cnt = 1;
210  foreach ($crs_ref_ids as $rid)
211  {
212  for ($i = 1; $i <= $a_files_per_course; $i++)
213  {
214  $this->log($a_title_base." ".$cnt);
215  $fileObj = new ilObjFile();
216  $fileObj->setTitle($a_title_base." ".$cnt);
217  $fileObj->setFileName("file_".$cnt.".txt");
218  $fileObj->create();
219  $fileObj->createReference();
220  $fileObj->putInTree($rid);
221  $fileObj->setPermissions($rid);
222  $fileObj->createDirectory();
223  $fileObj->getUploadFile($a_test_file,
224  "file_".$cnt.".txt");
225  $cnt++;
226 
227  }
228  }
229  }
230 
237  function generateCalendarEntries($a_num_per_course = 10)
238  {
239  include_once("./Services/Calendar/classes/class.ilDateTime.php");
240  include_once("./Services/Calendar/classes/class.ilCalendarEntry.php");
241  include_once("./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php");
242  include_once("./Services/Calendar/classes/class.ilCalendarCategories.php");
243 
244  $this->log("Creating Calendar Entries");
245 
246  $crs_ref_ids = ilUtil::_getObjectsByOperations("crs", "read",
247  0, $limit = 1000000);
248  $cnt = 1;
249  foreach ($crs_ref_ids as $rid)
250  {
251  $obj_id = ilObject::_lookupObjId($rid);
253 
254  $start = new ilDate(time(),IL_CAL_UNIX);
255  $end = new ilDate(time(),IL_CAL_UNIX);
256  $end->increment(IL_CAL_HOUR, 2);
257 
258  for ($i = 1; $i <= $a_num_per_course; $i++)
259  {
260  $this->log("Event ".$cnt);
261  $entry = new ilCalendarEntry();
262  $entry->setStart($start); //ilDateTiem
263  $entry->setEnd($end); //ilDateTiem
264  $entry->setFullday(false); //ilDateTiem
265  $entry->setTitle("Event ".$cnt); //ilDateTiem
266  $entry->save();
267  $id = $entry->getEntryId();
268  $ass = new ilCalendarCategoryAssignments($id);
269  $ass->addAssignment($cat_id);
270  $start->increment(IL_CAL_DAY, 1);
271  $end->increment(IL_CAL_DAY, 1);
272 //echo "-$cat_id-";
273 //echo "+".ilDatePresentation::formatDate($start)."+";
274 
275  $cnt++;
276  }
277  }
278  }
279 
287  {
288  global $ilDB;
289 
290  // mysql
291  if ($ilDB->getDBType() == "mysql")
292  {
293  $fp = fopen($file, 'r');
294  if (!$fp)
295  {
296  $this->log("Error reading file $file.");
297  return;
298  }
299  $this->log("Dropping Tables.");
300  $set = $ilDB->query("SHOW tables");
301  while ($rec = $ilDB->fetchAssoc($set))
302  {
303  foreach ($rec as $v)
304  {
305  $ilDB->query("DROP TABLE ".$v);
306  }
307  }
308 
309  $this->log("Read Dump.");
310  while(!feof($fp))
311  {
312  //$line = trim(fgets($fp, 200000));
313  $line = trim($this->getline($fp, "\n"));
314 
315  if ($line != "" && substr($line,0,1)!="#"
316  && substr($line,0,1)!="-")
317  {
318  //take line per line, until last char is ";"
319  if (substr($line,-1)==";")
320  {
321  //query is complete
322  $q .= " ".substr($line,0,-1);
323  $r = $ilDB->query($q);
324  if (mysql_errno() > 0)
325  {
326  echo "<br />ERROR: ".mysql_error().
327  "<br />SQL: $q";
328  return false;
329  }
330  unset($q);
331  unset($line);
332  } //if
333  else
334  {
335  $q .= " ".$line;
336  } //else
337  } //if
338  } //for
339 
340  fclose($fp);
341  }
342 
343  if ($ilDB->getDBType() == "oracle")
344  {
345  include_once("./setup/sql/ilDBTemplate.php");
346  setupILIASDatabase();
347  }
348  }
349 
353  function getline( $fp, $delim )
354  {
355  $result = "";
356  while( !feof( $fp ) )
357  {
358  $tmp = fgetc( $fp );
359  if( $tmp == $delim )
360  return $result;
361  $result .= $tmp;
362  }
363  return $result;
364  }
365 
371  function createDump($a_target_file, $a_dump_cmd, $a_db_user = "root",
372  $a_db_pw = "", $a_db_name = "loadtest")
373  {
374  $this->log("Writing Dump");
375  $a_pw = ($a_pwd == "")
376  ? ""
377  : " -p".$a_pw;
378  $cmd = $a_dump_cmd." -u".$a_db_user." ".$a_pw." --skip-lock-tables -Q --extended-insert=FALSE --default-character-set=utf8".
379  " --add-drop-table=FALSE --add-locks=FALSE --compatible=mysql40 ".$a_db_name." > ".$a_target_file;
380  if (!is_file($a_target_file))
381  {
382  $this->log("Something went wrong when writing the dump:<br>".$cmd);
383  }
384  }
385 
392  function writeUserCsv($a_target_file, $a_user_base_name = "learner")
393  {
394  global $ilDB;
395 
396  $this->log("Writing User CSV");
397  $set = $ilDB->query("SELECT * FROM usr_data");
398  $file = fopen($a_target_file, "w");
399  if ($file)
400  {
401  while ($rec = $ilDB->fetchAssoc($set))
402  {
403  if (substr($rec["login"], 0, strlen($a_user_base_name)) == $a_user_base_name)
404  {
405  fwrite($file, $rec["login"]."\n");
406  }
407  }
408  fclose($file);
409  }
410  else
411  {
412  $this->log("Could not write USer CSV.");
413  }
414 
415  }
416 
423  function writeCourseCsv($a_target_file)
424  {
425  global $ilDB;
426 
427  $this->log("Writing Course CSV");
428  $crs_ref_ids = ilUtil::_getObjectsByOperations("crs", "read",
429  0, $limit = 1000000);
430  $file = fopen($a_target_file, "w");
431  if ($file)
432  {
433  foreach ($crs_ref_ids as $r)
434  {
435  fwrite($file, $r."\n");
436  }
437  fclose($file);
438  }
439  else
440  {
441  $this->log("Could not write Course CSV.");
442  }
443 
444  }
445 
452  function writeCategoryCsv($a_target_file)
453  {
454  global $ilDB;
455 
456  $this->log("Writing Category CSV");
457  $cat_ref_ids = ilUtil::_getObjectsByOperations("cat", "read",
458  0, $limit = 1000000);
459  $file = fopen($a_target_file, "w");
460  if ($file)
461  {
462  foreach ($cat_ref_ids as $r)
463  {
464  fwrite($file, $r."\n");
465  }
466  fclose($file);
467  }
468  else
469  {
470  $this->log("Could not write Category CSV.");
471  }
472 
473  }
474 
479  function assignUsersAsCourseMembers($a_user_login_base = "learner",
480  $a_start = 1, $a_end = 100)
481  {
482  global $ilDB;
483 
484  $this->log("Assigning Course Members");
485  $set = $ilDB->query("SELECT usr_id, login FROM usr_data WHERE ".
486  " login LIKE ".$ilDB->quote($a_user_login_base."%", "text")
487  );
488  $user_ids = array();
489  while ($rec = $ilDB->fetchAssoc($set))
490  {
491  $rest = substr($rec["login"], strlen($a_user_login_base));
492  if (is_numeric($rest) &&
493  ((int) $rest >= $a_start && (int) $rest <= $a_end))
494  {
495  $user_ids[] = $rec["usr_id"];
496  }
497  }
498  $cnt = 1;
499  $crs_ref_ids = ilUtil::_getObjectsByOperations("crs", "read",
500  0, $limit = 1000000);
501  include_once "./Modules/Course/classes/class.ilCourseParticipants.php";
502  foreach ($crs_ref_ids as $r)
503  {
504  $crs_id = ilObject::_lookupObjId($r);
505  $mem_obj = ilCourseParticipants::_getInstanceByObjId($crs_id);
506  foreach ($user_ids as $u)
507  {
508  $this->log("$cnt: add user $u as member to course ".$crs_id);
509  $mem_obj->add($u, 1);
510  $cnt++;
511  }
512  }
513  }
514 
519  {
520  global $ilDB;
521 
522  $this->log("Remove Desktop Items.");
523  $ilDB->manipulate("DELETE FROM desktop_item");
524  }
525 
530  {
531  global $ilDB;
532 
533  $this->log("Deactivate calendars on personal desktop.");
534  $set = $ilDB->query("SELECT * FROM usr_data");
535  while ($rec = $ilDB->fetchAssoc($set))
536  {
537  $ilDB->manipulate("REPLACE INTO il_block_setting ".
538  "(type, user_id, block_id, setting, value) VALUES (".
539  $ilDB->quote("pdcal", "text").",".
540  $ilDB->quote($rec["usr_id"], "integer").",".
541  $ilDB->quote(0, "integer").",".
542  $ilDB->quote("detail", "text").",".
543  $ilDB->quote(0, "integer").
544  ")");
545  }
546  }
547 
548 
549  }
550 ?>