surveymanager.lib.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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
  35. survey.survey_id , survey.parent_id, survey_version, survey.code as name
  36. FROM $table_survey survey
  37. LEFT JOIN $table_survey_question survey_question
  38. ON survey.survey_id = survey_question.survey_id , $table_user user
  39. WHERE
  40. survey.c_id = $course_id AND
  41. survey_question.c_id = $course_id AND
  42. survey.author = user.user_id
  43. GROUP BY survey.survey_id";
  44. $res = Database::query($sql);
  45. $surveys_parents = array ();
  46. $refs = array();
  47. $list = array();
  48. $last = array();
  49. $plain_array=array();
  50. while ($survey = Database::fetch_array($res,'ASSOC')) {
  51. $plain_array[$survey['survey_id']]=$survey;
  52. $surveys_parents[]=$survey['survey_version'];
  53. $thisref = &$refs[ $survey['survey_id'] ];
  54. $thisref['parent_id'] = $survey['parent_id'];
  55. $thisref['name'] = $survey['name'];
  56. $thisref['id'] = $survey['survey_id'];
  57. $thisref['survey_version'] = $survey['survey_version'];
  58. if ($survey['parent_id'] == 0) {
  59. $list[ $survey['survey_id'] ] = &$thisref;
  60. } else {
  61. $refs[ $survey['parent_id'] ]['children'][ $survey['survey_id'] ] = &$thisref;
  62. }
  63. }
  64. $this->surveylist = $list;
  65. $this->plainsurveylist = $plain_array;
  66. }
  67. /**
  68. * This function gets the parent id of a survey
  69. *
  70. * @param int survey id
  71. * @return int survey parent id
  72. *
  73. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  74. * @version September 2008
  75. */
  76. public function getParentId($id)
  77. {
  78. $node = $this->plainsurveylist[$id];
  79. if (is_array($node)&& !empty($node['parent_id'])) {
  80. return $node['parent_id'];
  81. } else {
  82. return -1;
  83. }
  84. }
  85. /**
  86. * This function creates a list of all surveys id
  87. * @param list of nodes
  88. * @return array with the structure survey_id => survey_name
  89. * @author Julio Montoya <gugli100@gmail.com>
  90. * @version September 2008
  91. */
  92. public function createList ($list)
  93. {
  94. $result = array();
  95. if (is_array($list)) {
  96. foreach ($list as $key=>$node) {
  97. if (isset($node['children']) && is_array($node['children'])) {
  98. //echo $key; echo '--<br>';
  99. //print_r($node);
  100. //echo '<br>';
  101. $result[$key]= $node['name'];
  102. $re = self::createList($node['children']);
  103. if (!empty($re)) {
  104. if (is_array($re)) {
  105. foreach ($re as $key=>$r) {
  106. $result[$key]=''.$r;
  107. }
  108. } else {
  109. $result[]=$re;
  110. }
  111. }
  112. } else {
  113. $result[$key]=$node['name'];
  114. }
  115. }
  116. }
  117. return $result;
  118. }
  119. }