linkform.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 __construct(
  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(
  57. 'static',
  58. null,
  59. null,
  60. '"'.$this->link_object->get_name().'" '
  61. );
  62. $this->addElement('static', null, null, get_lang('MoveTo').' : ');
  63. $select = $this->addElement('select', 'move_cat', null, null);
  64. $line = '';
  65. foreach ($this->link_object->get_target_categories() as $cat) {
  66. for ($i = 0; $i < $cat[2]; $i++) {
  67. $line .= '&mdash;';
  68. }
  69. $select->addoption($line.' '.$cat[1], $cat[0]);
  70. $line = '';
  71. }
  72. $this->addElement('submit', null, get_lang('Ok'));
  73. }
  74. /**
  75. * Builds the form
  76. */
  77. protected function build_create()
  78. {
  79. $this->addElement('header', get_lang('MakeLink'));
  80. $select = $this->addElement(
  81. 'select',
  82. 'select_link',
  83. get_lang('ChooseLink'),
  84. null,
  85. array('onchange' => 'document.create_link.submit()')
  86. );
  87. $linkTypes = LinkFactory::get_all_types();
  88. $select->addoption('['.get_lang('ChooseLink').']', 0);
  89. $courseCode = $this->category_object->get_course_code();
  90. foreach ($linkTypes as $linkType) {
  91. // The hot potatoe link will be added "inside" the exercise option.
  92. if ($linkType == LINK_HOTPOTATOES) {
  93. continue;
  94. }
  95. $link = $this->createLink($linkType, $courseCode);
  96. // disable this element if the link works with a dropdownlist
  97. // and if there are no links left
  98. if (!$link->needs_name_and_description() && count($link->get_all_links()) == '0') {
  99. $select->addoption($link->get_type_name(), $linkType, 'disabled');
  100. } else {
  101. $select->addoption($link->get_type_name(), $linkType);
  102. }
  103. if ($link->get_type() == LINK_EXERCISE) {
  104. // Adding hot potatoes
  105. $linkHot = $this->createLink(LINK_HOTPOTATOES, $courseCode);
  106. $linkHot->setHp(true);
  107. if ($linkHot->get_all_links(true)) {
  108. $select->addoption(
  109. '&nbsp;&nbsp;&nbsp;'.$linkHot->get_type_name(),
  110. LINK_HOTPOTATOES
  111. );
  112. } else {
  113. $select->addoption(
  114. '&nbsp;&nbsp;&nbsp;'.$linkHot->get_type_name(),
  115. LINK_HOTPOTATOES,
  116. 'disabled'
  117. );
  118. }
  119. }
  120. }
  121. if (isset($this->extra)) {
  122. $this->setDefaults(array('select_link' => $this->extra));
  123. }
  124. }
  125. /**
  126. * @param integer $link
  127. * @param null|string $courseCode
  128. * @return AttendanceLink|DropboxLink|ExerciseLink|ForumThreadLink|LearnpathLink|null|StudentPublicationLink|SurveyLink
  129. */
  130. private function createLink($link, $courseCode)
  131. {
  132. $link = LinkFactory::create($link);
  133. if (!empty($courseCode)) {
  134. $link->set_course_code($courseCode);
  135. } elseif (!empty($_GET['course_code'])) {
  136. $link->set_course_code(Database::escape_string($_GET['course_code'], null, false));
  137. }
  138. return $link;
  139. }
  140. }