PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/php/webservices/content_object/create.class.php

https://bitbucket.org/chamilo/chamilo-repository-dev/
PHP | 193 lines | 166 code | 27 blank | 0 comment | 10 complexity | 1efe7ffff4f4465943b4bc4a81036b0d MD5 | raw file
  1. <?php
  2. namespace repository;
  3. use common\libraries\Translation;
  4. use common\libraries\CreateObjectWebserviceHandler;
  5. use common\libraries\DataClass;
  6. use common\libraries\EqualityCondition;
  7. use repository\content_object\assessment_multiple_choice_question\AssessmentMultipleChoiceQuestion;
  8. use repository\content_object\assessment_multiple_choice_question\AssessmentMultipleChoiceQuestionOption;
  9. use repository\content_object\assessment_matching_question\AssessmentMatchingQuestion;
  10. use repository\content_object\assessment_matching_question\AssessmentMatchingQuestionOption;
  11. use repository\content_object\assessment_match_numeric_question\AssessmentMatchNumericQuestion;
  12. use repository\content_object\assessment_match_numeric_question\AssessmentMatchNumericQuestionOption;
  13. use repository\content_object\assessment_match_text_question\AssessmentMatchTextQuestion;
  14. use repository\content_object\assessment_match_text_question\AssessmentMatchTextQuestionOption;
  15. use repository\content_object\assessment_matrix_question\AssessmentMatrixQuestion;
  16. use repository\content_object\assessment_matrix_question\AssessmentMatrixQuestionOption;
  17. use repository\content_object\assessment_select_question\AssessmentSelectQuestion;
  18. use repository\content_object\assessment_select_question\AssessmentSelectQuestionOption;
  19. use repository\content_object\document\Document;
  20. class ContentObjectCreateWebserviceHandler extends CreateObjectWebserviceHandler
  21. {
  22. function get_object()
  23. {
  24. $data = $this->get_data();
  25. $type = $data[self :: PROPERTIES][DataClass :: PROPERTIES_DEFAULT][ContentObject :: PROPERTY_TYPE];
  26. $content_object = ContentObject :: factory($type);
  27. if ($content_object instanceof ContentObject)
  28. {
  29. return $content_object;
  30. }
  31. return false;
  32. }
  33. function get_object_translation()
  34. {
  35. return Translation :: get('ContentObject');
  36. }
  37. function is_allowed_to_create_object($object)
  38. {
  39. if ($this->get_user()->is_platform_admin() || $object->get_parent_id() == 0)
  40. {
  41. return true;
  42. }
  43. $condition = new EqualityCondition(RepositoryCategory :: PROPERTY_ID, $object->get_parent_id());
  44. $category = RepositoryDataManager :: get_instance()->retrieve_categories($condition)->next_result();
  45. if ($category)
  46. {
  47. return ($category->get_user_id() == $this->get_user_id());
  48. }
  49. return false;
  50. }
  51. function manipulate_data($object, $extra = null)
  52. {
  53. $object->set_owner_id($this->get_user_id());
  54. if (! empty($extra) || $_FILES['file']['size'] > 0)
  55. {
  56. switch ($object->get_type())
  57. {
  58. case AssessmentMatchingQuestion :: get_type_name() :
  59. $object = $this->process_assessment_matching_question($object, $extra);
  60. break;
  61. case AssessmentMatchNumericQuestion :: get_type_name() :
  62. $object = $this->process_assessment_match_numeric_question($object, $extra);
  63. break;
  64. case AssessmentMatchTextQuestion :: get_type_name() :
  65. $object = $this->process_assessment_match_text_question($object, $extra);
  66. break;
  67. case AssessmentMultipleChoiceQuestion :: get_type_name() :
  68. $object = $this->process_assessment_multiple_choice_question($object, $extra);
  69. break;
  70. case AssessmentMatrixQuestion :: get_type_name() :
  71. $object = $this->process_assessment_matrix_question($object, $extra);
  72. break;
  73. case AssessmentSelectQuestion :: get_type_name() :
  74. $object = $this->process_assessment_select_question($object, $extra);
  75. break;
  76. case Document :: get_type_name() :
  77. $object = $this->process_document($object, $extra);
  78. break;
  79. default :
  80. return false;
  81. }
  82. }
  83. return $object;
  84. }
  85. private function process_assessment_multiple_choice_question($object, $extra)
  86. {
  87. $options = array();
  88. foreach ($extra[AssessmentMultipleChoiceQuestion :: PROPERTY_OPTIONS] as $option_array)
  89. {
  90. $option = new AssessmentMultipleChoiceQuestionOption($option_array[AssessmentMultipleChoiceQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentMultipleChoiceQuestionOption :: PROPERTY_CORRECT], $option_array[AssessmentMultipleChoiceQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentMultipleChoiceQuestionOption :: PROPERTY_FEEDBACK]);
  91. $options[] = $option;
  92. }
  93. $object->set_options($options);
  94. return $object;
  95. }
  96. private function process_assessment_matching_question($object, $extra)
  97. {
  98. $options = array();
  99. foreach ($extra[AssessmentMatchingQuestion :: PROPERTY_OPTIONS] as $option_array)
  100. {
  101. $option = new AssessmentMatchingQuestionOption($option_array[AssessmentMatchingQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentMatchingQuestionOption :: PROPERTY_MATCH], $option_array[AssessmentMatchingQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentMatchingQuestionOption :: PROPERTY_FEEDBACK]);
  102. $options[] = $option;
  103. }
  104. $object->set_options($options);
  105. $object->set_matches($extra[AssessmentMatchingQuestion :: PROPERTY_MATCHES]);
  106. return $object;
  107. }
  108. private function process_assessment_match_numeric_question($object, $extra)
  109. {
  110. $options = array();
  111. foreach ($extra[AssessmentMatchNumericQuestion :: PROPERTY_OPTIONS] as $option_array)
  112. {
  113. $option = new AssessmentMatchNumericQuestionOption($option_array[AssessmentMatchNumericQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentMatchNumericQuestionOption :: PROPERTY_TOLERANCE], $option_array[AssessmentMatchNumericQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentMatchNumericQuestionOption :: PROPERTY_FEEDBACK]);
  114. $options[] = $option;
  115. }
  116. $object->set_options($options);
  117. return $object;
  118. }
  119. private function process_assessment_match_text_question($object, $extra)
  120. {
  121. $options = array();
  122. foreach ($extra[AssessmentMatchTextQuestion :: PROPERTY_OPTIONS] as $option_array)
  123. {
  124. $option = new AssessmentMatchTextQuestionOption($option_array[AssessmentMatchTextQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentMatchTextQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentMatchTextQuestionOption :: PROPERTY_FEEDBACK]);
  125. $options[] = $option;
  126. }
  127. $object->set_options($options);
  128. return $object;
  129. }
  130. private function process_assessment_matrix_question($object, $extra)
  131. {
  132. $options = array();
  133. foreach ($extra[AssessmentMatrixQuestion :: PROPERTY_OPTIONS] as $option_array)
  134. {
  135. $option = new AssessmentMatrixQuestionOption($option_array[AssessmentMatrixQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentMatrixQuestionOption :: PROPERTY_MATCHES], $option_array[AssessmentMatrixQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentMatrixQuestionOption :: PROPERTY_FEEDBACK]);
  136. $options[] = $option;
  137. }
  138. $object->set_options($options);
  139. $object->set_matches($extra[AssessmentMatrixQuestion :: PROPERTY_MATCHES]);
  140. return $object;
  141. }
  142. private function process_assessment_select_question($object, $extra)
  143. {
  144. $options = array();
  145. foreach ($extra[AssessmentSelectQuestion :: PROPERTY_OPTIONS] as $option_array)
  146. {
  147. $option = new AssessmentSelectQuestionOption($option_array[AssessmentSelectQuestionOption :: PROPERTY_VALUE], $option_array[AssessmentSelectQuestionOption :: PROPERTY_CORRECT], $option_array[AssessmentSelectQuestionOption :: PROPERTY_SCORE], $option_array[AssessmentSelectQuestionOption :: PROPERTY_FEEDBACK]);
  148. $options[] = $option;
  149. }
  150. $object->set_options($options);
  151. return $object;
  152. }
  153. private function process_document($object, $extra)
  154. {
  155. if ($extra['file'])
  156. {
  157. $object->set_in_memory_file(base64_decode($extra['file']));
  158. }
  159. else
  160. {
  161. $object->set_temporary_file_path($_FILES['file']['tmp_name']);
  162. }
  163. return $object;
  164. }
  165. }
  166. ?>