ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilObjStudyProgrammeTreeGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 require_once("./Modules/StudyProgramme/classes/class.ilObjStudyProgrammeTreeExplorerGUI.php");
4 require_once("./Services/UIComponent/Modal/classes/class.ilModalGUI.php");
5 require_once("./Services/Accordion/classes/class.ilAccordionGUI.php");
6 require_once("./Modules/StudyProgramme/classes/helpers/class.ilAsyncContainerSelectionExplorer.php");
7 require_once("./Modules/StudyProgramme/classes/helpers/class.ilAsyncPropertyFormGUI.php");
8 require_once('./Services/Container/classes/class.ilContainerSorting.php');
9 require_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
10 require_once("./Modules/StudyProgramme/classes/helpers/class.ilAsyncNotifications.php");
11 require_once("./Modules/CourseReference/classes/class.ilObjCourseReference.php");
12 
21 {
25  public $ctrl;
26 
30  public $tpl;
31 
35  protected $access;
36 
40  public $object;
44  protected $locator;
45 
49  protected $log;
50 
54  public $ilias;
55 
59  public $lng;
60 
65  protected $ref_id;
66 
70  protected $tree;
71 
76  protected $modal_id;
77 
82 
83  /*
84  * @var ilToolbar
85  */
86  public $toolbar;
87 
88  public function __construct($a_ref_id)
89  {
90  global $DIC;
91  $tpl = $DIC['tpl'];
92  $ilCtrl = $DIC['ilCtrl'];
93  $ilAccess = $DIC['ilAccess'];
94  $ilToolbar = $DIC['ilToolbar'];
95  $ilLocator = $DIC['ilLocator'];
96  $tree = $DIC['tree'];
97  $lng = $DIC['lng'];
98  $ilLog = $DIC['ilLog'];
99  $ilias = $DIC['ilias'];
100  $ilSetting = $DIC['ilSetting'];
101 
102  $this->ref_id = $a_ref_id;
103  $this->tpl = $tpl;
104  $this->ctrl = $ilCtrl;
105  $this->access = $ilAccess;
106  $this->locator = $ilLocator;
107  $this->tree = $tree;
108  $this->toolbar = $ilToolbar;
109  $this->log = $ilLog;
110  $this->ilias = $ilias;
111  $this->lng = $lng;
112  $this->ilSetting = $ilSetting;
113  $this->modal_id = "tree_modal";
114  $this->async_output_handler = new ilAsyncOutputHandler();
115 
116  $this->initTree();
117 
118  $lng->loadLanguageModule("prg");
119  }
120 
121 
126  protected function initTree()
127  {
128  $this->tree = new ilObjStudyProgrammeTreeExplorerGUI($this->ref_id, $this->modal_id, "prg_tree", $this, 'view');
129 
130  $js_url = rawurldecode($this->ctrl->getLinkTarget($this, 'saveTreeOrder', '', true, false));
131  $this->tree->addJsConf('save_tree_url', $js_url);
132  $this->tree->addJsConf('save_button_id', 'save_order_button');
133  $this->tree->addJsConf('cancel_button_id', 'cancel_order_button');
134  }
135 
136 
143  public function executeCommand()
144  {
145  $cmd = $this->ctrl->getCmd();
146 
147  $this->getToolbar();
148 
149  if ($cmd == "") {
150  $cmd = "view";
151  }
152 
153  // handles tree commands ("openNode", "closeNode", "getNodeAsync")
154  if ($this->tree->handleCommand()) {
155  exit();
156  }
157 
158  switch ($cmd) {
159  case "view":
160  case "create":
161  case "save":
162  case "cancel":
163  case "delete":
164  case "confirmedDelete":
165  case "cancelDelete":
166  case "getContainerSelectionExplorer":
167  case "saveTreeOrder":
168  case "createNewLeaf":
169 
170  $content = $this->$cmd();
171  break;
172  default:
173  throw new ilException("ilObjStudyProgrammeTreeGUI: " .
174  "Command not supported: $cmd");
175  }
176 
178  }
179 
180 
186  protected function view()
187  {
188  $output = $this->tree->getHTML();
189  $output .= $this->initAsyncUIElements();
190 
191  return $output;
192  }
193 
194 
200  protected function cancel()
201  {
203  }
204 
205 
213  protected function saveTreeOrder()
214  {
215  $this->checkAccessOrFail('write');
216 
217  if (!isset($_POST['tree']) || is_null(json_decode(stripslashes($_POST['tree'])))) {
218  throw new ilStudyProgrammeTreeException("There is no tree data to save!");
219  }
220 
221  // saves order recursive
222  $this->storeTreeOrder(json_decode(stripslashes($_POST['tree'])));
223 
224  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>true, 'message'=>$this->lng->txt('prg_saved_order_successful')));
225  }
226 
227 
235  protected function storeTreeOrder($nodes, $container_sorting = null, $parent_ref_id = null)
236  {
237  $sorting_position = array();
238  $position_count = 10;
239 
240  $parent_node = ($parent_ref_id === null)? ilObjectFactoryWrapper::singleton()->getInstanceByRefId($this->ref_id) : ilObjectFactoryWrapper::singleton()->getInstanceByRefId($parent_ref_id);
241  $container_sorting = ($container_sorting === null) ? ilContainerSorting::_getInstance(ilObject::_lookupObjectId($this->ref_id)) : $container_sorting;
242 
243  foreach ($nodes as $node) {
244  // get ref_id from json
245  $id = $node->attr->id;
246  $id = substr($id, strrpos($id, "_")+1);
247 
248  $sorting_position[$id] = $position_count;
249  $position_count+= 10;
250 
251  $node_obj = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($id);
252  if ($node_obj instanceof ilObjStudyProgramme) {
253  $node_obj->moveTo($parent_node);
254  } else {
255  // TODO: implement a method on ilObjStudyProgramme to move leafs
256  global $DIC;
257  $tree = $DIC['tree'];
258  $rbacadmin = $DIC['rbacadmin'];
259 
260  $tree->moveTree($node_obj->getRefId(), $parent_node->getRefId());
261  $rbacadmin->adjustMovedObjectPermissions($node_obj->getRefId(), $parent_node->getRefId());
262  }
263 
264  // recursion if there are children
265  if (isset($node->children)) {
267  }
268  }
269  $container_sorting->savePost($sorting_position);
270  }
271 
272 
280  protected function createNewLeaf()
281  {
282  $this->checkAccessOrFail('create', (int) $_POST['parent_id']);
283 
284  if (isset($_POST['target_id'], $_POST['type'], $_POST['parent_id'])) {
285  $target_id = (int) $_POST['target_id'];
286  $parent_id = (int) $_POST['parent_id'];
287 
288  // TODO: more generic way for implementing different type of leafs
289  $course_ref = new ilObjCourseReference();
290  $course_ref->setTitleType(ilObjCourseReference::TITLE_TYPE_REUSE);
291  $course_ref->setTargetRefId($target_id);
292 
293  $course_ref->create();
294  $course_ref->createReference();
295 
296  $course_ref->putInTree($parent_id);
297 
298  // This is how its done in ILIAS. If you set the target ID before the creation, it won't work
299  $course_ref->setTargetId(ilObject::_lookupObjectId($target_id));
300  $course_ref->update();
301  }
302 
303  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>true, 'message'=>$this->lng->txt('prg_added_course_ref_successful')));
304  }
305 
306 
314  protected function getContainerSelectionExplorer($convert_to_string = true)
315  {
316  $create_leaf_form = new ilAsyncContainerSelectionExplorer(rawurldecode($this->ctrl->getLinkTarget($this, 'createNewLeaf', '', true, false)));
317  $create_leaf_form->setId("select_course_explorer");
318 
319  $ref_expand = ROOT_FOLDER_ID;
320  if (isset($_GET['ref_repexpand'])) {
321  $ref_expand = (int) $_GET['ref_repexpand'];
322  }
323 
324  $create_leaf_form->setExpand($ref_expand);
325  $create_leaf_form->setExpandTarget($this->ctrl->getLinkTarget($this, 'getContainerSelectionExplorer'));
326  $create_leaf_form->setAsynchExpanding(true);
327  $create_leaf_form->setTargetGet('target_id');
328  $create_leaf_form->setFrameTarget("_self");
329  $create_leaf_form->setClickable('crs', true);
330  $create_leaf_form->setTargetType('crs');
331  $create_leaf_form->setOutput(0);
332 
333  if ($convert_to_string) {
334  return $create_leaf_form->getOutput();
335  } else {
336  return $create_leaf_form;
337  }
338  }
339 
340 
346  protected function getCreationForm()
347  {
348  $tmp_obj = new ilObjStudyProgrammeGUI();
349 
350  $create_node_form = $tmp_obj->getAsyncCreationForm();
351  $create_node_form->setTitle("");
352  $this->ctrl->setParameterByClass("ilobjstudyprogrammegui", "new_type", "prg");
353  $create_node_form->setFormAction($this->ctrl->getFormActionByClass("ilobjstudyprogrammegui", "save"));
354 
355  return $create_node_form;
356  }
357 
358 
365  protected function create()
366  {
367  $parent_id = (isset($_GET['ref_id']))? (int) $_GET['ref_id'] : null;
368  $this->checkAccessOrFail('create', $parent_id);
369 
370  $parent = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($parent_id);
371  $accordion = new ilAccordionGUI();
372 
373  $added_slides = 0;
374  if ($parent instanceof ilObjStudyProgramme) {
375  // only allow adding new StudyProgramme-Node if there are no lp-children
376  if (!$parent->hasLPChildren()) {
377  $content_new_node = $this->getCreationForm()->getHTML();
378  $accordion->addItem($this->lng->txt('prg_create_new_node'), $content_new_node);
379  $added_slides++;
380  }
381 
382  /* only allow adding new LP-Children if there are no other StudyProgrammes
383  * AND creating crs references is activated in administration
384  */
385  if (!$parent->hasChildren() && $this->ilSetting->get("obj_dis_creation_crsr") === "") {
386  $content_new_leaf = $this->tpl->getMessageHTML($this->lng->txt('prg_please_select_a_course_for_creating_a_leaf'));
387  $content_new_leaf .= $this->getContainerSelectionExplorer();
388 
389  $accordion->addItem($this->lng->txt('prg_create_new_leaf'), $content_new_leaf);
390  $added_slides++;
391  }
392 
393  if ($added_slides == 1) {
394  $accordion->setBehaviour(ilAccordionGUI::FIRST_OPEN);
395  }
396 
397  $content = $accordion->getHTML();
398  }
399 
400  // creating modal window output
401  $this->async_output_handler->setHeading($this->lng->txt("prg_async_" . $this->ctrl->getCmd()));
402  $this->async_output_handler->setContent($content);
403  $this->async_output_handler->terminate();
404  }
405 
406 
412  protected function delete()
413  {
414  global $DIC;
415  $ilSetting = $DIC['ilSetting'];
416 
417  $this->checkAccessOrFail("delete");
418 
419  if (!isset($_GET['ref_id'], $_GET['item_ref_id'])) {
420  throw new ilException("Nothing to delete!");
421  }
422 
423  $element_ref_id = (int) $_GET['ref_id'];
424 
425  $cgui = new ilConfirmationGUI();
426 
427  $msg = $this->lng->txt("info_delete_sure");
428 
429  if (!$ilSetting->get('enable_trash')) {
430  $msg .= "<br/>" . $this->lng->txt("info_delete_warning_no_trash");
431  }
432  $cgui->setFormAction($this->ctrl->getFormAction($this, 'confirmedDelete', '', true));
433  $cgui->setCancel($this->lng->txt("cancel"), "cancelDelete");
434  $cgui->setConfirm($this->lng->txt("confirm"), "confirmedDelete");
435  $cgui->setFormName('async_form');
436 
437  $obj_id = ilObject::_lookupObjectId($element_ref_id);
438  $type = ilObject::_lookupType($obj_id);
439  $title = call_user_func(array(ilObjectFactory::getClassByType($type),'_lookupTitle'), $obj_id);
440  $alt = $this->lng->txt("icon") . " " . $this->lng->txt("obj_" . $type);
441 
442  $cgui->addItem(
443  "id[]",
444  $element_ref_id,
445  $title,
446  ilObject::_getIcon($obj_id, "small", $type),
447  $alt
448  );
449  $cgui->addHiddenItem('item_ref_id', $_GET['item_ref_id']);
450 
451  $content = $cgui->getHTML();
452 
453  // creating the modal window output
454  $this->async_output_handler->setHeading($msg);
455  $this->async_output_handler->setContent($content);
456  $this->async_output_handler->terminate();
457  }
458 
459 
466  protected function confirmedDelete()
467  {
468  $this->checkAccessOrFail("delete");
469 
470  if (!isset($_POST['id'], $_POST['item_ref_id']) && is_array($_POST['id'])) {
471  throw new ilException("No item select for deletion!");
472  }
473 
474  $ids = $_POST['id'];
475  $current_node = (int) $_POST['item_ref_id'];
476  $result = true;
477 
478  foreach ($ids as $id) {
479  $obj = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($id);
480 
481  $not_parent_of_current = true;
482  $not_root = true;
483 
484  // do some additional validation if it is a StudyProgramme
485  if ($obj instanceof ilObjStudyProgramme) {
486 
487  //check if you are not deleting a parent element of the current element
488  $children_of_node = ilObjStudyProgramme::getAllChildren($obj->getRefId());
489  $get_ref_ids = function ($obj) {
490  return $obj->getRefId();
491  };
492 
493  $children_ref_ids = array_map($get_ref_ids, $children_of_node);
494  $not_parent_of_current = (!in_array($current_node, $children_ref_ids));
495 
496  $not_root = ($obj->getRoot() != null);
497  }
498 
499  if ($current_node != $id && $not_root && $not_parent_of_current && $this->checkAccess('delete', $obj->getRefId())) {
500  ilRepUtil::deleteObjects(null, $id);
501 
502  // deletes the tree-open-node-session storage
503  if (isset($children_of_node)) {
504  $this->tree->closeCertainNode($id);
505  foreach ($children_of_node as $child) {
506  $this->tree->closeCertainNode($child->getRefId());
507  }
508  }
509 
510  $msg = $this->lng->txt("prg_deleted_safely");
511  } else {
512  $msg = $this->lng->txt("prg_not_allowed_node_to_delete");
513  $result = false;
514  }
515  }
516 
517  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>$result, 'message'=>$msg));
518  }
519 
520 
527  protected function cancelDelete()
528  {
530  }
531 
532 
539  protected function initAsyncUIElements()
540  {
541  // add js files
545 
546  // add bootstrap modal
547  $settings_modal = ilModalGUI::getInstance();
548  $settings_modal->setId($this->modal_id);
549  $settings_modal->setType(ilModalGUI::TYPE_LARGE);
550  $this->tpl->addOnLoadCode('$("#' . $this->modal_id . '").study_programme_modal();');
551 
552  $content = $settings_modal->getHTML();
553 
554  // init js notifications
555  $notifications = new ilAsyncNotifications();
556  $notifications->addJsConfig('events', array('success'=>array('study_programme-show_success')));
557  $notifications->initJs();
558 
559  // init tree selection explorer
560  $async_explorer = new ilAsyncContainerSelectionExplorer(rawurldecode($this->ctrl->getLinkTarget($this, 'createNewLeaf', '', true, false)));
561  $async_explorer->initJs();
562 
563  return $content;
564  }
565 
566 
570  protected function getToolbar()
571  {
572  $save_order_btn = ilLinkButton::getInstance();
573  $save_order_btn->setId('save_order_button');
574  $save_order_btn->setUrl("javascript:void(0);");
575  $save_order_btn->setOnClick("$('body').trigger('study_programme-save_order');");
576  $save_order_btn->setCaption('prg_save_tree_order');
577 
578  $cancel_order_btn = ilLinkButton::getInstance();
579  $cancel_order_btn->setId('cancel_order_button');
580  $cancel_order_btn->setUrl("javascript:void(0);");
581  $cancel_order_btn->setOnClick("$('body').trigger('study_programme-cancel_order');");
582  $cancel_order_btn->setCaption('prg_cancel_tree_order');
583 
584  $this->toolbar->addButtonInstance($save_order_btn);
585  $this->toolbar->addButtonInstance($cancel_order_btn);
586  }
587 
588 
597  protected function checkAccess($permission, $ref_id = null)
598  {
599  $ref_id = ($ref_id === null)? $this->ref_id : $ref_id;
600  $checker = $this->access->checkAccess($permission, '', $ref_id);
601 
602  return $checker;
603  }
604 
605 
614  protected function checkAccessOrFail($permission, $ref_id = null)
615  {
616  if (!$this->checkAccess($permission, $ref_id)) {
617  throw new ilException("You have no permission for " . $permission . " Object with ref_id " . $ref_id . "!");
618  }
619  }
620 }
static _getIcon( $a_obj_id="", $a_size="big", $a_type="", $a_offline=false)
Get icon for repository item.
static getClassByType($a_obj_type)
Get class by type.
createNewLeaf()
Creates a new leaf Currently only course references can be created.
Class ilAsyncOutputHandler Handles the output for async-requests.
static getAllChildren($a_ref_id)
Get a list of all ilObjStudyProgrammes in the subtree starting at $a_ref_id.
Class ilObjStudyProgrammeGUI class.
confirmedDelete()
Deletes a node or a leaf in the tree.
getContainerSelectionExplorer($convert_to_string=true)
Initialize the Course Explorer for creating a leaf.
$result
$type
global $DIC
Definition: saml.php:7
$_GET["client_id"]
Class ilAsyncNotifications Allows to display async notifications on a page.
if(!array_key_exists('StateId', $_REQUEST)) $id
$target_id
Definition: goto.php:49
checkAccess($permission, $ref_id=null)
Checks permission of current tree or certain child of it.
initTree()
Initialize Tree Creates tree instance and set tree configuration.
static addJavaScript($add_form_loader=false, $js_base_path=null)
Adds all needed js By default is called by ilAsyncPropertyFormGUI::getHTML()
static addJavaScript(ilTemplate $main_tpl=null)
Add javascript files that are necessary to run accordion.
static _lookupObjectId($a_ref_id)
lookup object id
checkAccessOrFail($permission, $ref_id=null)
Checks permission of a object and throws an exception if they are not granted.
global $ilCtrl
Definition: ilias.php:18
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
static encodeAsyncResponse(array $data=array())
Encode data as json for async output.
storeTreeOrder($nodes, $container_sorting=null, $parent_ref_id=null)
Recursive function for saving the tree order.
Class ilObjStudyProgramme.
saveTreeOrder()
Saves tree node order Data is json encoded from the jstree component.
redirection script todo: (a better solution should control the processing via a xml file) ...
cancelDelete()
Cancel deletion Return a json string for the async handling.
executeCommand()
Execute GUI-commands If there is a async request the response is sent as a json string.
static addJavascript()
Adds the javascript to template.
Create styles array
The data for the language used.
static _lookupType($a_id, $a_reference=false)
lookup object type
getCreationForm()
Returns the async creation form for StudyProgrammes.
static getInstance()
Get instance.
global $ilSetting
Definition: privfeed.php:17
Class ilStudyProgrammeTreeGUI ilObjStudyProgrammeTreeExplorerGUI generates the tree output for StudyP...
Class ilAsyncContainerSelectionExplorer A class for a async ilContainerSelectionExplorer which trigge...
static _getInstance($a_obj_id)
get instance by obj_id
Exception is thrown when invariants on the program tree would be violated by manipulation of tree...
Accordion user interface class.
initAsyncUIElements()
Initializes all elements used for async-interaction Adds HTML-skeleton for the bootstrap modal dialog...
$_POST["username"]
Class ilObjStudyProgrammeTreeGUI Generates the manage view for ilTrainingProgramme-Repository objects...
create()
Generates the modal window content for the creation form of nodes or leafs If there are already Study...
static handleAsyncOutput($normal_content, $async_content=null, $apply_to_tpl=true)
Handles async output.
Confirmation screen class.
static deleteObjects($a_cur_ref_id, $a_ids)
Delete objects.