linkform.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class LinkForm
  5. * Forms related to links
  6. * @author Stijn Konings
  7. * @author Bert Steppé (made more generic)
  8. * @package chamilo.gradebook
  9. */
  10. class LinkForm extends FormValidator
  11. {
  12. const TYPE_CREATE = 1;
  13. const TYPE_MOVE = 2;
  14. /** @var Category */
  15. private $category_object;
  16. private $link_object;
  17. private $extra;
  18. /**
  19. * Builds a form containing form items based on a given parameter
  20. * @param int form_type 1=choose link
  21. * @param obj cat_obj the category object
  22. * @param string form name
  23. * @param method
  24. * @param action
  25. */
  26. public function LinkForm(
  27. $form_type,
  28. $category_object,
  29. $link_object,
  30. $form_name,
  31. $method = 'post',
  32. $action = null,
  33. $extra = null
  34. ) {
  35. parent :: __construct($form_name, $method, $action);
  36. if (isset ($category_object)) {
  37. $this->category_object = $category_object;
  38. } else {
  39. if (isset($link_object)) {
  40. $this->link_object = $link_object;
  41. }
  42. }
  43. if (isset ($extra)) {
  44. $this->extra = $extra;
  45. }
  46. if ($form_type == self :: TYPE_CREATE) {
  47. $this->build_create();
  48. } elseif ($form_type == self :: TYPE_MOVE) {
  49. $this->build_move();
  50. }
  51. }
  52. protected function build_move()
  53. {
  54. $renderer =& $this->defaultRenderer();
  55. $renderer->setCustomElementTemplate('<span>{element}</span> ');
  56. $this->addElement('static',null,null,'"'.$this->link_object->get_name().'" ');
  57. $this->addElement('static',null,null,get_lang('MoveTo').' : ');
  58. $select = $this->addElement('select','move_cat',null,null);
  59. $line = '';
  60. foreach ($this->link_object->get_target_categories() as $cat) {
  61. for ($i=0;$i<$cat[2];$i++) {
  62. $line .= '&mdash;';
  63. }
  64. $select->addoption($line.' '.$cat[1],$cat[0]);
  65. $line = '';
  66. }
  67. $this->addElement('submit', null, get_lang('Ok'));
  68. }
  69. /**
  70. * Builds the form
  71. */
  72. protected function build_create()
  73. {
  74. $this->addElement('header', get_lang('MakeLink'));
  75. $select = $this->addElement(
  76. 'select',
  77. 'select_link',
  78. get_lang('ChooseLink'),
  79. null,
  80. array('onchange' => 'document.create_link.submit()')
  81. );
  82. $linkTypes = LinkFactory::get_all_types();
  83. $select->addoption('['.get_lang('ChooseLink').']', 0);
  84. $courseCode = $this->category_object->get_course_code();
  85. foreach ($linkTypes as $linkType) {
  86. // The hot potatoe link will be added "inside" the exercise option.
  87. if ($linkType == LINK_HOTPOTATOES) {
  88. continue;
  89. }
  90. $link = $this->createLink($linkType, $courseCode);
  91. // disable this element if the link works with a dropdownlist
  92. // and if there are no links left
  93. if (!$link->needs_name_and_description() && count($link->get_all_links()) == '0') {
  94. $select->addoption($link->get_type_name(), $linkType, 'disabled');
  95. } else {
  96. if ($link->get_type() == LINK_EXERCISE) {
  97. // Adding exercise
  98. $select->addoption($link->get_type_name(), $linkType);
  99. // Adding hot potatoes
  100. $linkHot = $this->createLink(LINK_HOTPOTATOES, $courseCode);
  101. $select->addoption(
  102. '&nbsp;&nbsp;&nbsp;'.$linkHot->get_type_name(),
  103. LINK_HOTPOTATOES
  104. );
  105. } else {
  106. $select->addoption($link->get_type_name(), $linkType);
  107. }
  108. }
  109. }
  110. if (isset($this->extra)) {
  111. $this->setDefaults(array('select_link' => $this->extra));
  112. }
  113. }
  114. /**
  115. * @param integer $link
  116. * @param null|string $courseCode
  117. * @return AttendanceLink|DropboxLink|ExerciseLink|ForumThreadLink|LearnpathLink|null|StudentPublicationLink|SurveyLink
  118. */
  119. private function createLink($link, $courseCode)
  120. {
  121. $link = LinkFactory::create($link);
  122. if (!empty($courseCode)) {
  123. $link->set_course_code($courseCode);
  124. } elseif(!empty($_GET['course_code'])) {
  125. $link->set_course_code(Database::escape_string($_GET['course_code'], null, false));
  126. }
  127. return $link;
  128. }
  129. }