surveymanager.lib.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Manage the "versioning" of a conditional survey.
  5. *
  6. * @package chamilo.survey
  7. */
  8. class SurveyTree
  9. {
  10. public $surveylist;
  11. public $plainsurveylist;
  12. public $numbersurveys;
  13. /**
  14. * Sets the surveylist and the plainsurveylist.
  15. */
  16. public function __construct()
  17. {
  18. // Database table definitions
  19. $table_survey = Database::get_course_table(TABLE_SURVEY);
  20. $table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
  21. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  22. // searching
  23. $search_restriction = SurveyUtil::survey_search_restriction();
  24. if ($search_restriction) {
  25. $search_restriction = ' AND '.$search_restriction;
  26. }
  27. $course_id = api_get_course_int_id();
  28. $sql = "SELECT
  29. survey.survey_id,
  30. survey.parent_id,
  31. survey_version,
  32. survey.code as name
  33. FROM $table_survey survey
  34. LEFT JOIN $table_survey_question survey_question
  35. ON survey.survey_id = survey_question.survey_id , $table_user user
  36. WHERE
  37. survey.c_id = $course_id AND
  38. survey_question.c_id = $course_id AND
  39. survey.author = user.user_id
  40. GROUP BY survey.survey_id";
  41. $res = Database::query($sql);
  42. $refs = [];
  43. $list = [];
  44. $plain_array = [];
  45. while ($survey = Database::fetch_array($res, 'ASSOC')) {
  46. $plain_array[$survey['survey_id']] = $survey;
  47. $surveys_parents[] = $survey['survey_version'];
  48. $thisref = &$refs[$survey['survey_id']];
  49. $thisref['parent_id'] = $survey['parent_id'];
  50. $thisref['name'] = $survey['name'];
  51. $thisref['id'] = $survey['survey_id'];
  52. $thisref['survey_version'] = $survey['survey_version'];
  53. if ($survey['parent_id'] == 0) {
  54. $list[$survey['survey_id']] = &$thisref;
  55. } else {
  56. $refs[$survey['parent_id']]['children'][$survey['survey_id']] = &$thisref;
  57. }
  58. }
  59. $this->surveylist = $list;
  60. $this->plainsurveylist = $plain_array;
  61. }
  62. /**
  63. * This function gets the parent id of a survey.
  64. *
  65. * @param int $id survey id
  66. *
  67. * @return int survey parent id
  68. *
  69. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  70. *
  71. * @version September 2008
  72. */
  73. public function getParentId($id)
  74. {
  75. $node = $this->plainsurveylist[$id];
  76. if (is_array($node) && !empty($node['parent_id'])) {
  77. return $node['parent_id'];
  78. } else {
  79. return -1;
  80. }
  81. }
  82. /**
  83. * This function creates a list of all surveys id.
  84. *
  85. * @param array $list of nodes
  86. *
  87. * @return array with the structure survey_id => survey_name
  88. *
  89. * @author Julio Montoya <gugli100@gmail.com>
  90. *
  91. * @version September 2008
  92. */
  93. public function createList($list)
  94. {
  95. $result = [];
  96. if (is_array($list)) {
  97. foreach ($list as $key => $node) {
  98. if (isset($node['children']) && is_array($node['children'])) {
  99. $result[$key] = $node['name'];
  100. $re = self::createList($node['children']);
  101. if (!empty($re)) {
  102. if (is_array($re)) {
  103. foreach ($re as $key => $r) {
  104. $result[$key] = ''.$r;
  105. }
  106. } else {
  107. $result[] = $re;
  108. }
  109. }
  110. } else {
  111. $result[$key] = $node['name'];
  112. }
  113. }
  114. }
  115. return $result;
  116. }
  117. }