PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/manage/context/forms/template_user_form.class.php

https://bitbucket.org/chamilo/chamilo-survey/
PHP | 300 lines | 225 code | 51 blank | 24 comment | 15 complexity | 3f923f9e4309add7507e6d552eec2f72 MD5 | raw file
  1. <?php
  2. namespace repository\content_object\survey;
  3. use group\GroupDataManager;
  4. use common\libraries\FormValidator;
  5. use common\libraries\Translation;
  6. use common\libraries\Path;
  7. use common\libraries\ResourceManager;
  8. use common\libraries\Utilities;
  9. use user\UserDataManager;
  10. class SurveyTemplateUserForm extends FormValidator
  11. {
  12. const TYPE_CREATE = 1;
  13. const TYPE_EDIT = 2;
  14. const RESULT_SUCCESS = 'TemplateUpdated';
  15. const RESULT_ERROR = 'TemplateUpdateFailed';
  16. const PARAM_TARGET_LEVEL = 'level';
  17. const PARAM_TARGET = 'target_users_and_groups';
  18. const PARAM_TARGET_ELEMENTS = 'target_users_and_groups_elements';
  19. const PARAM_TARGET_OPTION = 'target_users_and_groups_option';
  20. const PARAM_APPLICATION_NAME = 'survey_context_manager';
  21. private $survey_template_user;
  22. private $user;
  23. private $form_type;
  24. private $manager;
  25. /**
  26. * Creates a new LanguageForm
  27. */
  28. function __construct($form_type, $action, $survey_template_user, $user, $manager)
  29. {
  30. parent :: __construct('survey_template_user_form', 'post', $action);
  31. $this->survey_template_user = $survey_template_user;
  32. $this->user = $user;
  33. $this->form_type = $form_type;
  34. $this->manager = $manager;
  35. $this->build_header();
  36. if ($this->form_type == self :: TYPE_EDIT)
  37. {
  38. $this->build_editing_form();
  39. }
  40. else
  41. {
  42. $this->build_creation_form();
  43. }
  44. $this->setDefaults();
  45. }
  46. function build_header()
  47. {
  48. $this->addElement('html', '<div class="configuration_form">');
  49. $this->addElement('html', '<span class="category">' . Translation :: get('SurveyTemplateProperties') . '</span>');
  50. }
  51. function build_footer($action_name)
  52. {
  53. $this->addElement('html', '<div style="clear: both;"></div>');
  54. $this->addElement('html', '</div>');
  55. $buttons[] = $this->createElement('style_submit_button', 'create', Translation :: get($action_name), array(
  56. 'class' => 'positive'));
  57. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset'), array(
  58. 'class' => 'normal empty'));
  59. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  60. $this->addElement('html', ResourceManager :: get_instance()->get_resource_html(Path :: get(WEB_LIB_PATH) . 'javascript/survey_template_user_registration_form.js'));
  61. }
  62. /**
  63. * Creates a new basic form
  64. */
  65. function build_creation_form()
  66. {
  67. $attributes = array();
  68. $attributes['search_url'] = Path :: get(WEB_PATH) . 'common/libraries/php/xml_feeds/xml_user_group_feed.php';
  69. $locale = array();
  70. $locale['Display'] = Translation :: get('ShareWith', null, Utilities :: COMMON_LIBRARIES);
  71. $locale['Searching'] = Translation :: get('Searching', null, Utilities :: COMMON_LIBRARIES);
  72. $locale['NoResults'] = Translation :: get('NoResults', null, Utilities :: COMMON_LIBRARIES);
  73. $locale['Error'] = Translation :: get('Error', null, Utilities :: COMMON_LIBRARIES);
  74. $attributes['locale'] = $locale;
  75. $attributes['defaults'] = array();
  76. $attributes['options'] = array('load_elements' => false);
  77. $this->add_receivers(self :: PARAM_APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET, Translation :: get('Users'), $attributes);
  78. $survey_template_user = $this->survey_template_user;
  79. $property_names = $survey_template_user->get_additional_property_names(true);
  80. foreach ($property_names as $property_name => $type)
  81. {
  82. $labels = explode('_', $property_name);
  83. $label = $labels[0] . ' ' . $labels[1];
  84. $url = Path :: get_repository_content_object_path(true) . 'survey/php/xml_feeds/xml_context_feed.php?context_type=' . $type;
  85. $attributes = array();
  86. $attributes['search_url'] = $url;
  87. $locale = array();
  88. $locale['Display'] = Translation :: get('Choose');
  89. $locale['Searching'] = Translation :: get('Searching', null, Utilities :: COMMON_LIBRARIES);
  90. $locale['NoResults'] = Translation :: get('NoResults', null, Utilities :: COMMON_LIBRARIES);
  91. $locale['Error'] = Translation :: get('Error', null, Utilities :: COMMON_LIBRARIES);
  92. $attributes['locale'] = $locale;
  93. $attributes['defaults'] = array();
  94. $attributes['options'] = array('load_elements' => true);
  95. $elem = $this->addElement('element_finder', self :: PARAM_TARGET_LEVEL . '_' . $labels[1], $label, $attributes['search_url'], $attributes['locale'], $attributes['defaults'], $attributes['options']);
  96. // $elem->setDefaults($defaults);
  97. // $elem->setDefaultCollapsed(false);
  98. }
  99. $this->build_footer('Create');
  100. }
  101. /**
  102. * Builds an editing form
  103. */
  104. function build_editing_form()
  105. {
  106. // $survey_template_user = $this->survey_template_user;
  107. // $property_names = $survey_template_user->get_additional_property_names();
  108. //
  109. // foreach ($property_names as $property_name)
  110. // {
  111. // $this->add_textfield($property_name, $property_name, true);
  112. // }
  113. // $this->addElement('hidden', SurveyTemplate :: PROPERTY_ID);
  114. // $this->build_footer('Update');
  115. }
  116. function create_survey_template_user()
  117. {
  118. $values = $this->exportValues();
  119. $unique_user_ids = array();
  120. if ($values[self :: PARAM_APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_OPTION] != 0)
  121. {
  122. $user_ids = $values[self :: PARAM_APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_ELEMENTS]['user'];
  123. $group_ids = $values[self :: PARAM_APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_ELEMENTS]['group'];
  124. $users = array();
  125. foreach ($group_ids as $group_id)
  126. {
  127. $group = GroupDataManager :: get_instance()->retrieve_group($group_id);
  128. $users = array_merge($users, $group->get_users(true, true));
  129. }
  130. $user_ids = array_merge($user_ids, $users);
  131. $user_ids = array_unique($user_ids);
  132. }
  133. else
  134. {
  135. $users = UserDataManager :: get_instance()->retrieve_users();
  136. $user_ids = array();
  137. while ($user = $users->next_result())
  138. {
  139. $user_ids[] = $user->get_id();
  140. }
  141. }
  142. $user_count = count($user_ids);
  143. $levels = array();
  144. $level_value_counts = array();
  145. $level_count = 0;
  146. $array_count = 1;
  147. foreach ($values as $key => $value)
  148. {
  149. if (strpos($key, self :: PARAM_TARGET_LEVEL) !== false)
  150. {
  151. $keys = explode('_', $key);
  152. $level = $keys[1];
  153. $levels[$level] = $value['context'];
  154. $value_count = count($value['context']);
  155. $level_value_counts[$level] = $value_count;
  156. $level_count ++;
  157. $array_count = $array_count * $value_count;
  158. }
  159. }
  160. $colomn_shift_values = array();
  161. $shift_value = $array_count;
  162. foreach ($levels as $index => $level)
  163. {
  164. $shift_value = $shift_value / $level_value_counts[$index];
  165. $colomn_shift_values[$index] = $shift_value;
  166. }
  167. $survey_template_user = $this->survey_template_user;
  168. $property_names = $survey_template_user->get_additional_property_names();
  169. $check_level_count = count($property_names);
  170. if ($check_level_count != $level_count)
  171. {
  172. return false;
  173. }
  174. $property_values = array();
  175. $column = $level_count;
  176. while ($column > 0)
  177. {
  178. $index = 0;
  179. $sub_index = 0;
  180. $value_index = 0;
  181. while ($index < $array_count)
  182. {
  183. $index ++;
  184. if ($colomn_shift_values[$column] <= $sub_index)
  185. {
  186. $sub_index = 0;
  187. if ($value_index == $level_value_counts[$column] - 1)
  188. {
  189. $value_index = 0;
  190. }
  191. else
  192. {
  193. $value_index ++;
  194. }
  195. }
  196. $sub_index ++;
  197. $property_values[$index][$column] = $levels[$column][$value_index];
  198. }
  199. $column --;
  200. }
  201. $result = false;
  202. foreach ($user_ids as $user_id)
  203. {
  204. foreach ($property_values as $values)
  205. {
  206. $survey_template_user = $this->survey_template_user;
  207. $survey_template_user->set_user_id($user_id);
  208. foreach ($property_names as $property_name)
  209. {
  210. $split = explode('_', $property_name);
  211. $level = $split[1];
  212. $value = $values[$level];
  213. $survey_template_user->set_additional_property($property_name, $value);
  214. }
  215. $result = $survey_template_user->create();
  216. }
  217. }
  218. return $result;
  219. }
  220. function update_survey_template_user()
  221. {
  222. $survey_template_user = $this->survey_template_user;
  223. $property_names = $survey_template_user->get_additional_property_names();
  224. foreach ($property_names as $property_name)
  225. {
  226. $survey_template_user->set_additional_property($property_name, $this->exportValue($property_name));
  227. }
  228. return $survey_template_user->update();
  229. }
  230. /**
  231. * Sets default values.
  232. * @param array $defaults Default values for this form's parameters.
  233. */
  234. function setDefaults($defaults = array ())
  235. {
  236. if ($this->form_type == self :: TYPE_EDIT)
  237. {
  238. $survey_template_user = $this->survey_template_user;
  239. $property_names = $survey_template_user->get_additional_property_names();
  240. foreach ($property_names as $property_name)
  241. {
  242. $defaults[$property_name] = $survey_template_user->get_additional_property($property_name);
  243. }
  244. }
  245. $defaults[self :: PARAM_APPLICATION_NAME . '_opt_' . self :: PARAM_TARGET_OPTION] = 1;
  246. parent :: setDefaults($defaults);
  247. }
  248. }
  249. ?>