surveymanager.lib.php 3.8 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. $surveys_parents = array ();
  43. $refs = array();
  44. $list = array();
  45. $plain_array=array();
  46. while ($survey = Database::fetch_array($res,'ASSOC')) {
  47. $plain_array[$survey['survey_id']]=$survey;
  48. $surveys_parents[]=$survey['survey_version'];
  49. $thisref = &$refs[ $survey['survey_id'] ];
  50. $thisref['parent_id'] = $survey['parent_id'];
  51. $thisref['name'] = $survey['name'];
  52. $thisref['id'] = $survey['survey_id'];
  53. $thisref['survey_version'] = $survey['survey_version'];
  54. if ($survey['parent_id'] == 0) {
  55. $list[ $survey['survey_id'] ] = &$thisref;
  56. } else {
  57. $refs[ $survey['parent_id'] ]['children'][ $survey['survey_id'] ] = &$thisref;
  58. }
  59. }
  60. $this->surveylist = $list;
  61. $this->plainsurveylist = $plain_array;
  62. }
  63. /**
  64. * This function gets the parent id of a survey
  65. *
  66. * @param int $id survey id
  67. * @return int survey parent id
  68. *
  69. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  70. * @version September 2008
  71. */
  72. public function getParentId($id)
  73. {
  74. $node = $this->plainsurveylist[$id];
  75. if (is_array($node)&& !empty($node['parent_id'])) {
  76. return $node['parent_id'];
  77. } else {
  78. return -1;
  79. }
  80. }
  81. /**
  82. * This function creates a list of all surveys id
  83. * @param array $list of nodes
  84. * @return array with the structure survey_id => survey_name
  85. * @author Julio Montoya <gugli100@gmail.com>
  86. * @version September 2008
  87. */
  88. public function createList($list)
  89. {
  90. $result = array();
  91. if (is_array($list)) {
  92. foreach ($list as $key => $node) {
  93. if (isset($node['children']) && is_array($node['children'])) {
  94. $result[$key]= $node['name'];
  95. $re = self::createList($node['children']);
  96. if (!empty($re)) {
  97. if (is_array($re)) {
  98. foreach ($re as $key => $r) {
  99. $result[$key] = '' . $r;
  100. }
  101. } else {
  102. $result[] = $re;
  103. }
  104. }
  105. } else {
  106. $result[$key] = $node['name'];
  107. }
  108. }
  109. }
  110. return $result;
  111. }
  112. }