surveymanager.lib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 = array();
  43. $list = array();
  44. $plain_array = 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. * @return int survey parent id
  67. *
  68. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  69. * @version September 2008
  70. */
  71. public function getParentId($id)
  72. {
  73. $node = $this->plainsurveylist[$id];
  74. if (is_array($node) && !empty($node['parent_id'])) {
  75. return $node['parent_id'];
  76. } else {
  77. return -1;
  78. }
  79. }
  80. /**
  81. * This function creates a list of all surveys id
  82. * @param array $list of nodes
  83. * @return array with the structure survey_id => survey_name
  84. * @author Julio Montoya <gugli100@gmail.com>
  85. * @version September 2008
  86. */
  87. public function createList($list)
  88. {
  89. $result = array();
  90. if (is_array($list)) {
  91. foreach ($list as $key => $node) {
  92. if (isset($node['children']) && is_array($node['children'])) {
  93. $result[$key] = $node['name'];
  94. $re = self::createList($node['children']);
  95. if (!empty($re)) {
  96. if (is_array($re)) {
  97. foreach ($re as $key => $r) {
  98. $result[$key] = ''.$r;
  99. }
  100. } else {
  101. $result[] = $re;
  102. }
  103. }
  104. } else {
  105. $result[$key] = $node['name'];
  106. }
  107. }
  108. }
  109. return $result;
  110. }
  111. }