linkaddeditform.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Form used to add or edit links.
  5. *
  6. * @author Stijn Konings
  7. * @author Bert Steppé
  8. *
  9. * @package chamilo.gradebook
  10. */
  11. class LinkAddEditForm extends FormValidator
  12. {
  13. public const TYPE_ADD = 1;
  14. public const TYPE_EDIT = 2;
  15. /**
  16. * Constructor
  17. * To add link, define category_object and link_type
  18. * To edit link, define link_object.
  19. */
  20. public function __construct(
  21. $form_type,
  22. $category_object,
  23. $link_type,
  24. $link_object,
  25. $form_name,
  26. $action = null
  27. ) {
  28. parent::__construct($form_name, 'post', $action);
  29. // set or create link object
  30. if (isset($link_object)) {
  31. $link = $link_object;
  32. } elseif (isset($link_type) && isset($category_object)) {
  33. $link = LinkFactory:: create($link_type);
  34. $link->set_course_code(api_get_course_id());
  35. $link->set_category_id($category_object[0]->get_id());
  36. } else {
  37. die('LinkAddEditForm error: define link_type/category_object or link_object');
  38. }
  39. $defaults = [];
  40. if (!empty($_GET['editlink'])) {
  41. $this->addElement('header', '', get_lang('Edit link'));
  42. }
  43. // ELEMENT: name
  44. if ($form_type == self::TYPE_ADD || $link->is_allowed_to_change_name()) {
  45. if ($link->needs_name_and_description()) {
  46. $this->addText('name', get_lang('Name'), true, ['size' => '40', 'maxlength' => '40']);
  47. } else {
  48. $select = $this->addElement('select', 'select_link', get_lang('Choose activity to assess'));
  49. foreach ($link->get_all_links() as $newlink) {
  50. $select->addoption($newlink[1], $newlink[0]);
  51. }
  52. }
  53. } else {
  54. $this->addElement(
  55. 'label',
  56. get_lang('Name'),
  57. '<span class="freeze">'.$link->get_name().' ['.$link->get_type_name().']</span>'
  58. );
  59. $this->addElement(
  60. 'hidden',
  61. 'name_link',
  62. $link->get_name(),
  63. ['id' => 'name_link']
  64. );
  65. }
  66. if (count($category_object) == 1) {
  67. $this->addElement('hidden', 'select_gradebook', $category_object[0]->get_id());
  68. } else {
  69. $select_gradebook = $this->addElement(
  70. 'select',
  71. 'select_gradebook',
  72. get_lang('Select assessment'),
  73. [],
  74. ['id' => 'hide_category_id']
  75. );
  76. $this->addRule('select_gradebook', get_lang('Required field'), 'nonzero');
  77. $default_weight = 0;
  78. if (!empty($category_object)) {
  79. foreach ($category_object as $my_cat) {
  80. if ($my_cat->get_course_code() == api_get_course_id()) {
  81. $grade_model_id = $my_cat->get_grade_model_id();
  82. if (empty($grade_model_id)) {
  83. if ($my_cat->get_parent_id() == 0) {
  84. $default_weight = $my_cat->get_weight();
  85. $select_gradebook->addoption(get_lang('Default'), $my_cat->get_id());
  86. } else {
  87. $select_gradebook->addoption($my_cat->get_name(), $my_cat->get_id());
  88. }
  89. } else {
  90. $select_gradebook->addoption(get_lang('Select'), 0);
  91. }
  92. if ($link->get_category_id() == $my_cat->get_id()) {
  93. $default_weight = $my_cat->get_weight();
  94. }
  95. }
  96. }
  97. }
  98. }
  99. $this->addFloat(
  100. 'weight_mask',
  101. [
  102. get_lang('Weight'),
  103. null,
  104. ' [0 .. <span id="max_weight">'.$category_object[0]->get_weight(
  105. ).'</span>] ',
  106. ],
  107. true,
  108. [
  109. 'size' => '4',
  110. 'maxlength' => '5',
  111. ]
  112. );
  113. $this->addElement('hidden', 'weight');
  114. if ($form_type == self::TYPE_EDIT) {
  115. $parent_cat = Category::load($link->get_category_id());
  116. if ($parent_cat[0]->get_parent_id() == 0) {
  117. $values['weight'] = $link->get_weight();
  118. } else {
  119. $cat = Category::load($parent_cat[0]->get_parent_id());
  120. //$global_weight = $cat[0]->get_weight();
  121. //$values['weight'] = $link->get_weight()/$parent_cat[0]->get_weight()*$global_weight;
  122. //var_dump($global_weight, $link->get_weight(), $parent_cat[0]->get_weight());
  123. //$weight = $parent_cat[0]->get_weight()* $link->get_weight() / $global_weight;
  124. //$values['weight'] = $weight;
  125. $values['weight'] = $link->get_weight();
  126. }
  127. $defaults['weight_mask'] = $values['weight'];
  128. $defaults['select_gradebook'] = $link->get_category_id();
  129. }
  130. // ELEMENT: max
  131. if ($link->needs_max()) {
  132. if ($form_type == self::TYPE_EDIT && $link->has_results()) {
  133. $this->addText(
  134. 'max',
  135. get_lang('Maximum score'),
  136. false,
  137. [
  138. 'size' => '4',
  139. 'maxlength' => '5',
  140. 'disabled' => 'disabled',
  141. ]
  142. );
  143. } else {
  144. $this->addText('max', get_lang('Maximum score'), true, ['size' => '4', 'maxlength' => '5']);
  145. $this->addRule('max', get_lang('Only numbers'), 'numeric');
  146. $this->addRule(
  147. 'max',
  148. get_lang('Negative value'),
  149. 'compare',
  150. '>=',
  151. 'server',
  152. false,
  153. false,
  154. 0
  155. );
  156. }
  157. if ($form_type == self::TYPE_EDIT) {
  158. $defaults['max'] = $link->get_max();
  159. }
  160. }
  161. // ELEMENT: description
  162. if ($link->needs_name_and_description()) {
  163. $this->addElement(
  164. 'textarea',
  165. 'description',
  166. get_lang('Description'),
  167. ['rows' => '3', 'cols' => '34']
  168. );
  169. if ($form_type == self::TYPE_EDIT) {
  170. $defaults['description'] = $link->get_description();
  171. }
  172. }
  173. // ELEMENT: visible
  174. $visible = ($form_type == self::TYPE_EDIT && $link->is_visible()) ? '1' : '0';
  175. $this->addElement('checkbox', 'visible', null, get_lang('Visible'), $visible);
  176. if ($form_type == self::TYPE_EDIT) {
  177. $defaults['visible'] = $link->is_visible();
  178. }
  179. // ELEMENT: add results
  180. if ($form_type == self::TYPE_ADD && $link->needs_results()) {
  181. $this->addElement('checkbox', 'addresult', get_lang('Grade learners'));
  182. }
  183. // submit button
  184. if ($form_type == self::TYPE_ADD) {
  185. $this->addButtonCreate(get_lang('Add this learning activity to the assessment'));
  186. } else {
  187. $this->addButtonUpdate(get_lang('Edit link'));
  188. }
  189. if ($form_type == self::TYPE_ADD) {
  190. $setting = api_get_setting('tool_visible_by_default_at_creation');
  191. $visibility_default = 1;
  192. if (isset($setting['gradebook']) && $setting['gradebook'] == 'false') {
  193. $visibility_default = 0;
  194. }
  195. $defaults['visible'] = $visibility_default;
  196. }
  197. // set default values
  198. $this->setDefaults($defaults);
  199. }
  200. }