linkform.class.php 4.7 KB

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