PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/php/lib/course/course_user_category_form.class.php

https://bitbucket.org/chamilo/chamilo-app-weblcms/
PHP | 266 lines | 198 code | 50 blank | 18 comment | 17 complexity | 3ce715ee66f4c1bf2c80a86d2e303383 MD5 | raw file
  1. <?php
  2. namespace application\weblcms;
  3. use common\libraries\FormValidator;
  4. use common\libraries\EqualityCondition;
  5. use common\libraries\Path;
  6. use common\libraries\Translation;
  7. use common\libraries\Utilities;
  8. use repository\ContentObject;
  9. /**
  10. * $Id: course_user_category_form.class.php 216 2009-11-13 14:08:06Z kariboe $
  11. * @package application.lib.weblcms.course
  12. */
  13. require_once dirname(__FILE__) . '/course.class.php';
  14. require_once dirname(__FILE__) . '/course_user_category.class.php';
  15. class CourseUserCategoryForm extends FormValidator
  16. {
  17. const TYPE_CREATE = 1;
  18. const TYPE_EDIT = 2;
  19. const COURSE_TYPE_TARGET = 'course_type_target';
  20. const COURSE_TYPE_TARGET_ELEMENTS = 'course_type_target_elements';
  21. const COURSE_TYPE_TARGET_OPTION = 'course_type_target_option';
  22. private $course_user_category;
  23. private $user;
  24. private $parent;
  25. function __construct($form_type, $course_user_category, $user, $action, $parent)
  26. {
  27. parent :: __construct('course_settings', 'post', $action);
  28. $this->course_user_category = $course_user_category;
  29. $this->user = $user;
  30. $this->parent = $parent;
  31. $this->form_type = $form_type;
  32. if ($this->form_type == self :: TYPE_EDIT)
  33. {
  34. $this->build_editing_form();
  35. }
  36. elseif ($this->form_type == self :: TYPE_CREATE)
  37. {
  38. $this->build_creation_form();
  39. }
  40. $this->setDefaults();
  41. }
  42. function build_basic_form()
  43. {
  44. $this->addElement('text', CourseUserCategory :: PROPERTY_TITLE, Translation :: get('Title', null ,Utilities:: COMMON_LIBRARIES), array("maxlength" => 50, "size" => 50));
  45. $this->addRule(CourseUserCategory :: PROPERTY_TITLE, Translation :: get('ThisFieldIsRequired', null ,Utilities:: COMMON_LIBRARIES), 'required');
  46. $attributes = array();
  47. $attributes['search_url'] = Path :: get(WEB_PATH) . 'application/weblcms/php/xml_feeds/xml_course_type_feed.php';
  48. $locale = array();
  49. $locale['Display'] = Translation :: get('SelectRecipients');
  50. $locale['Searching'] = Translation :: get('Searching', null ,Utilities:: COMMON_LIBRARIES);
  51. $locale['NoResults'] = Translation :: get('NoResults', null ,Utilities:: COMMON_LIBRARIES);
  52. $locale['Error'] = Translation :: get('Error', null ,Utilities:: COMMON_LIBRARIES);
  53. $attributes['locale'] = $locale;
  54. $attributes['defaults'] = array();
  55. $attributes['options']['load_elements'] = true;
  56. $element_finder = $this->createElement('user_group_finder',
  57. self :: COURSE_TYPE_TARGET_ELEMENTS, Translation :: get('CourseType'),
  58. $attributes['search_url'], $attributes['locale'], $attributes['defaults'], $attributes['options']);
  59. $element_finder->excludeElements($attributes['exclude']);
  60. $this->addElement($element_finder);
  61. }
  62. function build_editing_form()
  63. {
  64. $course_user_category = $this->course_user_category;
  65. $parent = $this->parent;
  66. $this->build_basic_form();
  67. $this->addElement('hidden', CourseUserCategory :: PROPERTY_ID);
  68. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Update', null ,Utilities:: COMMON_LIBRARIES), array('class' => 'positive update'));
  69. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null ,Utilities:: COMMON_LIBRARIES), array('class' => 'normal empty'));
  70. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  71. }
  72. function build_creation_form()
  73. {
  74. $this->build_basic_form();
  75. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Create', null ,Utilities:: COMMON_LIBRARIES), array('class' => 'positive'));
  76. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null ,Utilities:: COMMON_LIBRARIES), array('class' => 'normal empty'));
  77. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  78. }
  79. function update_course_user_category()
  80. {
  81. $course_user_category = $this->course_user_category;
  82. $values = $this->exportValues();
  83. // EDIT TITLE
  84. $course_user_category->set_title($values[CourseUserCategory :: PROPERTY_TITLE]);
  85. if (! $course_user_category->update())
  86. {
  87. return false;
  88. }
  89. // EDIT COURSE TYPE
  90. $wdm = WeblcmsDataManager :: get_instance();
  91. // get the existing course types
  92. $condition = new EqualityCondition(CourseTypeUserCategory :: PROPERTY_COURSE_USER_CATEGORY_ID, $course_user_category->get_id());
  93. $existing_types = $wdm->retrieve_course_type_user_categories($condition);
  94. if($existing_types)
  95. {
  96. $existing_types = $existing_types->as_array();
  97. }
  98. else
  99. {
  100. $existing_types = array();
  101. }
  102. // get the selected course types
  103. $selected_types = $this->get_selected_course_types();
  104. // uses the compare function of CourseTypeUserCategory
  105. $compare_class = "application\weblcms\CourseTypeUserCategory";
  106. $compare_method = "compare";
  107. // create the types that are selected but don't exist, and delete the types that are no longer selected.
  108. // update is not needed at the moment, but if it ever is: just update all elements that are in both arrays
  109. $to_be_created = array_udiff($selected_types, $existing_types, array($compare_class, $compare_method));
  110. $to_be_deleted = array_udiff($existing_types, $selected_types, array($compare_class, $compare_method));
  111. // process changes
  112. $failures = 0;
  113. foreach($to_be_created as $create_this)
  114. {
  115. if(!$create_this->create())
  116. {
  117. $failures++;
  118. }
  119. }
  120. foreach($to_be_deleted as $delete_this)
  121. {
  122. if(!$delete_this->delete())
  123. {
  124. $failures++;
  125. }
  126. }
  127. return ($failures == 0);
  128. }
  129. function create_course_user_category()
  130. {
  131. $values = $this->exportValues();
  132. $course_types = $this->get_selected_course_types();
  133. if (count($course_types) == 0)
  134. {
  135. return false;
  136. }
  137. $this->course_user_category->set_id($values[CourseUserCategory :: PROPERTY_ID]);
  138. $this->course_user_category->set_title($values[CourseUserCategory :: PROPERTY_TITLE]);
  139. if (! $this->course_user_category->create())
  140. {
  141. return false;
  142. }
  143. foreach ($course_types as $course_type)
  144. {
  145. $course_type->set_course_user_category_id($this->course_user_category->get_id());
  146. if (! $course_type->create())
  147. {
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. function get_selected_course_types()
  154. {
  155. $values = $this->exportValues();
  156. $course_type_user_category_id = $this->course_user_category->get_id();
  157. $selected_course_types_array = array();
  158. foreach ($values[self :: COURSE_TYPE_TARGET_ELEMENTS]['coursetype'] as $value)
  159. {
  160. $selected_course_type_user_category = new CourseTypeUserCategory();
  161. $selected_course_type_user_category->set_course_type_id($value);
  162. $selected_course_type_user_category->set_user_id($this->user->get_id());
  163. $selected_course_type_user_category->set_course_user_category_id($course_type_user_category_id);
  164. $selected_course_types_array[] = $selected_course_type_user_category;
  165. }
  166. return $selected_course_types_array;
  167. }
  168. /**
  169. * Sets default values. Traditionally, you will want to extend this method
  170. * so it sets default for your learning object type's additional
  171. * properties.
  172. * @param array $defaults Default values for this form's parameters.
  173. */
  174. function setDefaults($defaults = array ())
  175. {
  176. $course_user_category = $this->course_user_category;
  177. $defaults[CourseUserCategory :: PROPERTY_TITLE] = $course_user_category->get_title();
  178. if (! is_null($course_user_category->get_id()))
  179. {
  180. $wdm = WeblcmsDataManager :: get_instance();
  181. $condition = new EqualityCondition(CourseTypeUserCategory :: PROPERTY_COURSE_USER_CATEGORY_ID, $course_user_category->get_id());
  182. $course_types = $wdm->retrieve_course_type_user_categories($condition);
  183. while ($type = $course_types->next_result())
  184. {
  185. $selected_course_type = $this->get_course_type_array($type->get_course_type_id(), $wdm);
  186. $defaults[self :: COURSE_TYPE_TARGET_ELEMENTS][$selected_course_type['id']] = $selected_course_type;
  187. }
  188. if (count($defaults[self :: COURSE_TYPE_TARGET_ELEMENTS]) > 0)
  189. {
  190. $active = $this->getElement(self :: COURSE_TYPE_TARGET_ELEMENTS);
  191. $active->setValue($defaults[self :: COURSE_TYPE_TARGET_ELEMENTS]);
  192. }
  193. }
  194. parent :: setDefaults($defaults);
  195. }
  196. function get_course_type_array($course_type_id, $wdm)
  197. {
  198. $selected_course_type = array();
  199. $selected_course_type['classes'] = 'type type_course_type';
  200. if ($course_type_id != 0)
  201. {
  202. $course_type = $wdm->retrieve_course_type($course_type_id);
  203. $selected_course_type['id'] = 'coursetype_' . $course_type->get_id();
  204. $selected_course_type['title'] = $course_type->get_name();
  205. $selected_course_type['description'] = $course_type->get_name();
  206. }
  207. else
  208. {
  209. $selected_course_type['id'] = 'coursetype_0';
  210. $selected_course_type['title'] = Translation :: get('NoCourseType');
  211. $selected_course_type['description'] = Translation :: get('NoCourseType');
  212. }
  213. return $selected_course_type;
  214. }
  215. }
  216. ?>