surveymanager.lib.php 3.4 KB

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