ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
24  public $ctrl;
25 
29  public $tpl;
30 
34  protected $access;
35 
39  public $object;
43  protected $locator;
44 
48  protected $log;
49 
53  public $ilias;
54 
58  public $lng;
59 
64  protected $ref_id;
65 
69  protected $tree;
70 
75  protected $modal_id;
76 
81 
82  /*
83  * @var ilToolbar
84  */
85  public $toolbar;
86 
87  public function __construct($a_ref_id) {
88  global $DIC;
89  $tpl = $DIC['tpl'];
90  $ilCtrl = $DIC['ilCtrl'];
91  $ilAccess = $DIC['ilAccess'];
92  $ilToolbar = $DIC['ilToolbar'];
93  $ilLocator = $DIC['ilLocator'];
94  $tree = $DIC['tree'];
95  $lng = $DIC['lng'];
96  $ilLog = $DIC['ilLog'];
97  $ilias = $DIC['ilias'];
98  $ilSetting = $DIC['ilSetting'];
99 
100  $this->ref_id = $a_ref_id;
101  $this->tpl = $tpl;
102  $this->ctrl = $ilCtrl;
103  $this->access = $ilAccess;
104  $this->locator = $ilLocator;
105  $this->tree = $tree;
106  $this->toolbar = $ilToolbar;
107  $this->log = $ilLog;
108  $this->ilias = $ilias;
109  $this->lng = $lng;
110  $this->ilSetting = $ilSetting;
111  $this->modal_id = "tree_modal";
112  $this->async_output_handler = new ilAsyncOutputHandler();
113 
114  $this->initTree();
115 
116  $lng->loadLanguageModule("prg");
117  }
118 
119 
124  protected function initTree() {
125  $this->tree = new ilObjStudyProgrammeTreeExplorerGUI($this->ref_id, $this->modal_id, "prg_tree", $this, 'view');
126 
127  $js_url = rawurldecode($this->ctrl->getLinkTarget($this, 'saveTreeOrder', '', true, false));
128  $this->tree->addJsConf('save_tree_url', $js_url);
129  $this->tree->addJsConf('save_button_id', 'save_order_button');
130  $this->tree->addJsConf('cancel_button_id', 'cancel_order_button');
131  }
132 
133 
140  public function executeCommand() {
141  $cmd = $this->ctrl->getCmd();
142 
143  $this->getToolbar();
144 
145  if ($cmd == "") {
146  $cmd = "view";
147  }
148 
149  // handles tree commands ("openNode", "closeNode", "getNodeAsync")
150  if($this->tree->handleCommand()) {
151  exit();
152  }
153 
154  switch ($cmd) {
155  case "view":
156  case "create":
157  case "save":
158  case "cancel":
159  case "delete":
160  case "confirmedDelete":
161  case "cancelDelete":
162  case "getContainerSelectionExplorer":
163  case "saveTreeOrder":
164  case "createNewLeaf":
165 
166  $content = $this->$cmd();
167  break;
168  default:
169  throw new ilException("ilObjStudyProgrammeTreeGUI: ".
170  "Command not supported: $cmd");
171  }
172 
174  }
175 
176 
182  protected function view() {
183 
184  $output = $this->tree->getHTML();
185  $output .= $this->initAsyncUIElements();
186 
187  return $output;
188  }
189 
190 
196  protected function cancel() {
198  }
199 
200 
208  protected function saveTreeOrder() {
209  $this->checkAccessOrFail('write');
210 
211  if(!isset($_POST['tree']) || is_null(json_decode(stripslashes($_POST['tree'])))) {
212  throw new ilStudyProgrammeTreeException("There is no tree data to save!");
213  }
214 
215  // saves order recursive
216  $this->storeTreeOrder(json_decode(stripslashes($_POST['tree'])));
217 
218  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>true, 'message'=>$this->lng->txt('prg_saved_order_successful')));
219  }
220 
221 
229  protected function storeTreeOrder($nodes, $container_sorting = null, $parent_ref_id = null) {
230  $sorting_position = array();
231  $position_count = 10;
232 
233  $parent_node = ($parent_ref_id === null)? ilObjectFactoryWrapper::singleton()->getInstanceByRefId($this->ref_id) : ilObjectFactoryWrapper::singleton()->getInstanceByRefId($parent_ref_id);
234  $container_sorting = ($container_sorting === null) ? ilContainerSorting::_getInstance(ilObject::_lookupObjectId($this->ref_id)) : $container_sorting;
235 
236  foreach($nodes as $node) {
237  // get ref_id from json
238  $id = $node->attr->id;
239  $id = substr($id, strrpos($id, "_")+1);
240 
241  $sorting_position[$id] = $position_count;
242  $position_count+= 10;
243 
244  $node_obj = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($id);
245  if($node_obj instanceof ilObjStudyProgramme) {
246  $node_obj->moveTo($parent_node);
247  } else {
248  // TODO: implement a method on ilObjStudyProgramme to move leafs
249  global $DIC;
250  $tree = $DIC['tree'];
251  $rbacadmin = $DIC['rbacadmin'];
252 
253  $tree->moveTree($node_obj->getRefId(), $parent_node->getRefId());
254  $rbacadmin->adjustMovedObjectPermissions($node_obj->getRefId(), $parent_node->getRefId());
255  }
256 
257  // recursion if there are children
258  if(isset($node->children)) {
260  }
261  }
262  $container_sorting->savePost($sorting_position);
263  }
264 
265 
273  protected function createNewLeaf() {
274  $this->checkAccessOrFail('create', (int) $_POST['parent_id']);
275 
276  if(isset($_POST['target_id'], $_POST['type'], $_POST['parent_id'])) {
277  $target_id = (int) $_POST['target_id'];
278  $parent_id = (int) $_POST['parent_id'];
279 
280  // TODO: more generic way for implementing different type of leafs
281  $course_ref = new ilObjCourseReference();
282  $course_ref->setTitleType(ilObjCourseReference::TITLE_TYPE_REUSE);
283  $course_ref->setTargetRefId($target_id);
284 
285  $course_ref->create();
286  $course_ref->createReference();
287 
288  $course_ref->putInTree($parent_id);
289 
290  // This is how its done in ILIAS. If you set the target ID before the creation, it won't work
291  $course_ref->setTargetId(ilObject::_lookupObjectId($target_id));
292  $course_ref->update();
293  }
294 
295  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>true, 'message'=>$this->lng->txt('prg_added_course_ref_successful')));
296  }
297 
298 
306  protected function getContainerSelectionExplorer($convert_to_string = true) {
307  $create_leaf_form = new ilAsyncContainerSelectionExplorer(rawurldecode($this->ctrl->getLinkTarget($this, 'createNewLeaf', '', true, false)));
308  $create_leaf_form->setId("select_course_explorer");
309 
310  $ref_expand = ROOT_FOLDER_ID;
311  if(isset($_GET['ref_repexpand'])) {
312  $ref_expand = (int) $_GET['ref_repexpand'];
313  }
314 
315  $create_leaf_form->setExpand($ref_expand);
316  $create_leaf_form->setExpandTarget($this->ctrl->getLinkTarget($this,'getContainerSelectionExplorer'));
317  $create_leaf_form->setAsynchExpanding(true);
318  $create_leaf_form->setTargetGet('target_id');
319  $create_leaf_form->setFrameTarget("_self");
320  $create_leaf_form->setClickable('crs', true);
321  $create_leaf_form->setTargetType('crs');
322  $create_leaf_form->setOutput(0);
323 
324  if($convert_to_string)
325  return $create_leaf_form->getOutput();
326  else
327  return $create_leaf_form;
328  }
329 
330 
336  protected function getCreationForm() {
337  $tmp_obj = new ilObjStudyProgrammeGUI();
338 
339  $create_node_form = $tmp_obj->getAsyncCreationForm();
340  $create_node_form->setTitle("");
341  $this->ctrl->setParameterByClass("ilobjstudyprogrammegui", "new_type", "prg");
342  $create_node_form->setFormAction($this->ctrl->getFormActionByClass("ilobjstudyprogrammegui", "save"));
343 
344  return $create_node_form;
345  }
346 
347 
354  protected function create() {
355  $parent_id = (isset($_GET['ref_id']))? (int) $_GET['ref_id'] : null;
356  $this->checkAccessOrFail('create', $parent_id);
357 
358  $parent = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($parent_id);
359  $accordion = new ilAccordionGUI();
360 
361  $added_slides = 0;
362  if($parent instanceof ilObjStudyProgramme) {
363  // only allow adding new StudyProgramme-Node if there are no lp-children
364  if(!$parent->hasLPChildren()) {
365  $content_new_node = $this->getCreationForm()->getHTML();
366  $accordion->addItem($this->lng->txt('prg_create_new_node'), $content_new_node);
367  $added_slides++;
368  }
369 
370  /* only allow adding new LP-Children if there are no other StudyProgrammes
371  * AND creating crs references is activated in administration
372  */
373  if(!$parent->hasChildren() && $this->ilSetting->get("obj_dis_creation_crsr") === "") {
374  $content_new_leaf = $this->tpl->getMessageHTML($this->lng->txt('prg_please_select_a_course_for_creating_a_leaf'));
375  $content_new_leaf .= $this->getContainerSelectionExplorer();
376 
377  $accordion->addItem($this->lng->txt('prg_create_new_leaf'), $content_new_leaf);
378  $added_slides++;
379  }
380 
381  if($added_slides == 1) {
382  $accordion->setBehaviour(ilAccordionGUI::FIRST_OPEN);
383  }
384 
385  $content = $accordion->getHTML();
386  }
387 
388  // creating modal window output
389  $this->async_output_handler->setHeading($this->lng->txt("prg_async_".$this->ctrl->getCmd()));
390  $this->async_output_handler->setContent($content);
391  $this->async_output_handler->terminate();
392  }
393 
394 
400  protected function delete() {
401  global $DIC;
402  $ilSetting = $DIC['ilSetting'];
403 
404  $this->checkAccessOrFail("delete");
405 
406  if(!isset($_GET['ref_id'], $_GET['item_ref_id'])) {
407  throw new ilException("Nothing to delete!");
408  }
409 
410  $element_ref_id = (int) $_GET['ref_id'];
411 
412  $cgui = new ilConfirmationGUI();
413 
414  $msg = $this->lng->txt("info_delete_sure");
415 
416  if (!$ilSetting->get('enable_trash'))
417  {
418  $msg .= "<br/>".$this->lng->txt("info_delete_warning_no_trash");
419  }
420  $cgui->setFormAction($this->ctrl->getFormAction($this, 'confirmedDelete', '', true));
421  $cgui->setCancel($this->lng->txt("cancel"), "cancelDelete");
422  $cgui->setConfirm($this->lng->txt("confirm"), "confirmedDelete");
423  $cgui->setFormName('async_form');
424 
425  $obj_id = ilObject::_lookupObjectId($element_ref_id);
426  $type = ilObject::_lookupType($obj_id);
427  $title = call_user_func(array(ilObjectFactory::getClassByType($type),'_lookupTitle'),$obj_id);
428  $alt = $this->lng->txt("icon")." ".$this->lng->txt("obj_".$type);
429 
430  $cgui->addItem("id[]", $element_ref_id, $title,
431  ilObject::_getIcon($obj_id, "small", $type),
432  $alt);
433  $cgui->addHiddenItem('item_ref_id', $_GET['item_ref_id']);
434 
435  $content = $cgui->getHTML();
436 
437  // creating the modal window output
438  $this->async_output_handler->setHeading($msg);
439  $this->async_output_handler->setContent($content);
440  $this->async_output_handler->terminate();
441  }
442 
443 
450  protected function confirmedDelete() {
451  $this->checkAccessOrFail("delete");
452 
453  if(!isset($_POST['id'], $_POST['item_ref_id']) && is_array($_POST['id'])) {
454  throw new ilException("No item select for deletion!");
455  }
456 
457  $ids = $_POST['id'];
458  $current_node = (int) $_POST['item_ref_id'];
459  $result = true;
460 
461  foreach($ids as $id) {
462  $obj = ilObjectFactoryWrapper::singleton()->getInstanceByRefId($id);
463 
464  $not_parent_of_current = true;
465  $not_root = true;
466 
467  // do some additional validation if it is a StudyProgramme
468  if($obj instanceof ilObjStudyProgramme) {
469 
470  //check if you are not deleting a parent element of the current element
471  $children_of_node = ilObjStudyProgramme::getAllChildren($obj->getRefId());
472  $get_ref_ids = function ($obj) { return $obj->getRefId(); };
473 
474  $children_ref_ids = array_map($get_ref_ids, $children_of_node);
475  $not_parent_of_current = (!in_array($current_node, $children_ref_ids));
476 
477  $not_root = ($obj->getRoot() != null);
478  }
479 
480  if($current_node != $id && $not_root && $not_parent_of_current && $this->checkAccess('delete', $obj->getRefId())) {
481 
482  ilRepUtil::deleteObjects(null, $id);
483 
484  // deletes the tree-open-node-session storage
485  if(isset($children_of_node)) {
486  $this->tree->closeCertainNode($id);
487  foreach($children_of_node as $child) {
488  $this->tree->closeCertainNode($child->getRefId());
489  }
490  }
491 
492  $msg = $this->lng->txt("prg_deleted_safely");
493 
494  } else {
495  $msg = $this->lng->txt("prg_not_allowed_node_to_delete");
496  $result = false;
497  }
498  }
499 
500  return ilAsyncOutputHandler::encodeAsyncResponse(array('success'=>$result, 'message'=>$msg));
501  }
502 
503 
510  protected function cancelDelete() {
512  }
513 
514 
521  protected function initAsyncUIElements() {
522  // add js files
526 
527  // add bootstrap modal
528  $settings_modal = ilModalGUI::getInstance();
529  $settings_modal->setId($this->modal_id);
530  $settings_modal->setType(ilModalGUI::TYPE_LARGE);
531  $this->tpl->addOnLoadCode('$("#'.$this->modal_id.'").study_programme_modal();');
532 
533  $content = $settings_modal->getHTML();
534 
535  // init js notifications
536  $notifications = new ilAsyncNotifications();
537  $notifications->addJsConfig('events', array('success'=>array('study_programme-show_success')));
538  $notifications->initJs();
539 
540  // init tree selection explorer
541  $async_explorer = new ilAsyncContainerSelectionExplorer(rawurldecode($this->ctrl->getLinkTarget($this, 'createNewLeaf', '', true, false)));
542  $async_explorer->initJs();
543 
544  return $content;
545  }
546 
547 
551  protected function getToolbar() {
552  $save_order_btn = ilLinkButton::getInstance();
553  $save_order_btn->setId('save_order_button');
554  $save_order_btn->setUrl("javascript:void(0);");
555  $save_order_btn->setOnClick("$('body').trigger('study_programme-save_order');");
556  $save_order_btn->setCaption('prg_save_tree_order');
557 
558  $cancel_order_btn = ilLinkButton::getInstance();
559  $cancel_order_btn->setId('cancel_order_button');
560  $cancel_order_btn->setUrl("javascript:void(0);");
561  $cancel_order_btn->setOnClick("$('body').trigger('study_programme-cancel_order');");
562  $cancel_order_btn->setCaption('prg_cancel_tree_order');
563 
564  $this->toolbar->addButtonInstance($save_order_btn);
565  $this->toolbar->addButtonInstance($cancel_order_btn);
566  }
567 
568 
577  protected function checkAccess($permission, $ref_id = null) {
578  $ref_id = ($ref_id === null)? $this->ref_id : $ref_id;
579  $checker = $this->access->checkAccess($permission, '', $ref_id);
580 
581  return $checker;
582  }
583 
584 
593  protected function checkAccessOrFail($permission, $ref_id = null) {
594  if(!$this->checkAccess($permission, $ref_id)) {
595  throw new ilException("You have no permission for ".$permission." Object with ref_id ".$ref_id."!");
596  }
597  }
598 
599 }
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.
ILIAS Setting Class.
static _getIcon($a_obj_id="", $a_size="big", $a_type="", $a_offline=false)
Get icon for repository item.
static getAllChildren($a_ref_id)
Get a list of all ilObjStudyProgrammes in the subtree starting at $a_ref_id.
Base class for ILIAS Exception handling.
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
$_GET["client_id"]
Class ilAsyncNotifications Allows to display async notifications on a page.
$cmd
Definition: sahs_server.php:35
$target_id
Definition: goto.php:51
checkAccess($permission, $ref_id=null)
Checks permission of current tree or certain child of it.
static addJavaScript()
Add javascript files that are necessary to run accordion.
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 _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.
get($a_keyword, $a_default_value=false)
get setting
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...
global $DIC
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.