surveymanager.lib.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. public $surveylist;
  16. public $plainsurveylist;
  17. public $numbersurveys;
  18. /**
  19. * Sets the surveylist and the plainsurveylist
  20. */
  21. public function __construct() {
  22. // Database table definitions
  23. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  24. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  25. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  26. // searching
  27. $search_restriction = SurveyUtil::survey_search_restriction();
  28. if ($search_restriction) {
  29. $search_restriction = ' AND '.$search_restriction;
  30. }
  31. $course_id = api_get_course_int_id();
  32. $sql = "SELECT survey.survey_id , survey.parent_id, survey_version, survey.code as name
  33. FROM $table_survey survey LEFT JOIN $table_survey_question survey_question
  34. ON survey.survey_id = survey_question.survey_id , $table_user user
  35. WHERE
  36. survey.c_id = $course_id AND
  37. survey_question.c_id = $course_id AND
  38. survey.author = user.user_id
  39. GROUP BY survey.survey_id";
  40. $res = Database::query($sql);
  41. $surveys_parents = array ();
  42. $refs = array();
  43. $list = array();
  44. $last=array();
  45. $plain_array=array();
  46. while ($survey = Database::fetch_array($res,'ASSOC'))
  47. {
  48. $plain_array[$survey['survey_id']]=$survey;
  49. $surveys_parents[]=$survey['survey_version'];
  50. $thisref = &$refs[ $survey['survey_id'] ];
  51. $thisref['parent_id'] = $survey['parent_id'];
  52. $thisref['name'] = $survey['name'];
  53. $thisref['id'] = $survey['survey_id'];
  54. $thisref['survey_version'] = $survey['survey_version'];
  55. if ($survey['parent_id'] == 0)
  56. {
  57. $list[ $survey['survey_id'] ] = &$thisref;
  58. }
  59. else
  60. {
  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. $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. * This function creates a list of all surveys id
  85. * @param list of nodes
  86. * @return array with the structure survey_id => survey_name
  87. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  88. * @version September 2008
  89. *
  90. */
  91. public function createList ($list) {
  92. $result=array();
  93. if(is_array($list)) {
  94. foreach ($list as $key=>$node) {
  95. if (is_array($node['children']))
  96. {
  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. {
  104. if (is_array($re))
  105. foreach ($re as $key=>$r)
  106. {
  107. $result[$key]=''.$r;
  108. }
  109. else
  110. {
  111. $result[]=$re;
  112. }
  113. }
  114. }
  115. else
  116. {
  117. //echo $key; echo '-<br>';
  118. $result[$key]=$node['name'];
  119. }
  120. }
  121. }
  122. return $result;
  123. }
  124. }