linkform.class.php 4.0 KB

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