list.tpl 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. {% extends app.template_style ~ "/layout/layout_1_col.tpl" %}
  2. {% macro widget_prototype(widget, remove_text) %}
  3. {% if widget.get('prototype') %}
  4. {% set form = widget.get('prototype') %}
  5. {% set name = widget.get('prototype').get('description') %}
  6. {% else %}
  7. {% set form = widget %}
  8. {% set name = widget.get('full_name') %}
  9. {% endif %}
  10. <div data-content="{{ name }}">
  11. {# <a class="btn-remove" data-related="{{ name }}">{{ remove_text }}</a> #}
  12. {{ form_widget(form) }}
  13. </div>
  14. {% endmacro %}
  15. {% block content %}
  16. <script>
  17. $(function() {
  18. // Create delete buttons:
  19. $("#list form input:text").each(function(index, value) {
  20. var input = $(this);
  21. var removeForm = $('<a class="btn btn-danger" href="#"><i class="icon-minus-sign icon-large"></i></a>');
  22. var maxRepeat = input.parent().parent().parent().parent().attr('data-max');
  23. if (maxRepeat != 1) {
  24. input.parent().append(removeForm);
  25. addTagFormDeleteLink(removeForm);
  26. }
  27. });
  28. // Create add buttons:
  29. $(".items").each(function(index, value) {
  30. var itemId = $(this).attr('id');
  31. var lastInput = $(this).find("input:text").last();
  32. var addButton = $('<a class="btn-add btn btn-primary" data-target="'+itemId+'"><i class="icon-plus-sign icon-large"></i></a>');
  33. });
  34. // When clicking in the add button:
  35. $('.btn-add[data-target]').live('click', function(event) {
  36. var collectionHolder = $('#' + $(this).attr('data-target'));
  37. var maxRepeat = collectionHolder.attr('data-max');
  38. var countInput = collectionHolder.find('input:text').length;
  39. // Disables the add button when reached the max count
  40. var showButtonWhenIsHidden = false;
  41. if (countInput > maxRepeat - 2) {
  42. showButtonWhenIsHidden = true;
  43. }
  44. // Disables the clicking when it's disabled
  45. if (countInput > maxRepeat - 1) {
  46. $(this).addClass('disabled');
  47. return false;
  48. }
  49. if (!collectionHolder.attr('data-counter')) {
  50. collectionHolder.attr('data-counter', collectionHolder.children().length);
  51. }
  52. var prototype = collectionHolder.attr('data-prototype');
  53. var form = prototype.replace(/__name__/g, collectionHolder.attr('data-counter'));
  54. collectionHolder.attr('data-counter', Number(collectionHolder.attr('data-counter')) + 1);
  55. var removeForm = $('<a class="btn btn-danger" href="#"><i class="icon-minus-sign icon-large"></i></a>');
  56. var liItem = $('<li id="'+collectionHolder.attr('data-counter')+'">'+form+'</li>');
  57. liItem.find('.controls').append(removeForm);
  58. var item = collectionHolder.find('ul').append(liItem);
  59. if (showButtonWhenIsHidden) {
  60. addTagFormDeleteLink(removeForm, $(this));
  61. } else {
  62. addTagFormDeleteLink(removeForm);
  63. }
  64. event && event.preventDefault();
  65. });
  66. $('#saveQuestionBar').affix();
  67. });
  68. function addTagFormDeleteLink($tagFormLi, showButton) {
  69. $tagFormLi.on('click', function(e) {
  70. e.preventDefault();
  71. $(this).parent().remove();
  72. // Restore the add button.
  73. if (showButton) {
  74. showButton.removeClass('disabled');
  75. }
  76. });
  77. }
  78. function save(itemId) {
  79. var form = $("#"+itemId).parent();
  80. var serializedForm = form.serialize();
  81. $.ajax({
  82. 'url' : '{{ url('curriculum_user.controller:saveUserItemAction', {'course' : course.code, 'id_session' : course_session.id }) }}',
  83. 'data' : serializedForm,
  84. 'async': false,
  85. 'type' : 'post'
  86. }
  87. );
  88. }
  89. function saveAll() {
  90. var items = $("form").find(".items");
  91. $(items).each(function(index, value) {
  92. var itemId = $(this).attr('id');
  93. save(itemId);
  94. });
  95. window.location = '{{ url('index') }}';
  96. }
  97. </script>
  98. <div class="row">
  99. <div class="span10">
  100. {% if is_granted('ROLE_ADMIN') and isAllowed %}
  101. <div class="actions">
  102. <a href="{{ url('curriculum_category.controller:indexAction', { 'course' : course.code, 'id_session' : course_session.id }) }}">
  103. {{ "Categories" | trans }}
  104. </a>
  105. <a href="{{ url('curriculum_category.controller:resultsAction', { 'course' : course.code, 'id_session' : course_session.id }) }}">
  106. {{ "Results" | trans }}
  107. </a>
  108. </div>
  109. {% endif %}
  110. <h2>Trayectoria</h2>
  111. <p>Explicaciones</p>
  112. <p>Las respuestas a este formulario son de carácter jurado.</p>
  113. <div id="list" class="trajectory">
  114. {% for subcategory in categories %}
  115. {% if subcategory.lvl == 0 %}
  116. {# <h3> {{ subcategory.title }} </h3>
  117. <hr /> #}
  118. {% else %}
  119. <h4> {{ subcategory.title }}</h4>
  120. {% endif %}
  121. {% for item in subcategory.items %}
  122. {# Items #}
  123. {{ item.title }} (item) - {{ 'CurriculumMaximumItem' | trans }} {{ item.maxRepeat }}
  124. <div class="row">
  125. {{ form_start(form_list[item.id]) }}
  126. <div id="items_{{ item.id }}" class="items span8" data-max="{{ item.maxRepeat }}" data-prototype="{{ form_widget(form_list[item.id].userItems.vars.prototype)|e }}" >
  127. {% for widget in form_list[item.id].userItems.children %}
  128. {{ _self.widget_prototype(widget, 'Remove item') }}
  129. {% endfor %}
  130. <ul>
  131. </ul>
  132. </div>
  133. <div class="span8">
  134. <div class="btn-group">
  135. {% if item.maxRepeat > 1 %}
  136. <a class="btn-add btn btn-primary" data-target="items_{{ item.id }}"><i class="icon-plus-sign icon-large"></i></a>
  137. {% endif %}
  138. </div>
  139. </div>
  140. {{ form_end(form_list[item.id]) }}
  141. </div>
  142. {% endfor %}
  143. {% endfor %}
  144. </div>
  145. </div>
  146. <div class="span2">
  147. <div id="saveQuestionBar" data-spy="affix" data-offset-top="500">
  148. <a class="btn btn-success btn-large btn-block" onclick="saveAll();">{{ 'Save all' | get_lang }}</a>
  149. </div>
  150. </div>
  151. </div>
  152. {% endblock %}