surveymanager.lib.php 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. <?php // $Id: usermanager.lib.php,v 1.9.2.3 2005/10/28 14:31:45 bmol Exp $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This library provides functions for user management.
  22. * Include/require it in your code to use its functionality.
  23. *
  24. * @author Bart Mollet, main author
  25. * @package dokeos.library
  26. ==============================================================================
  27. */
  28. class SurveyManager
  29. {
  30. /**
  31. * Creates a new survey for the platform
  32. */
  33. function select_survey_list($seleced_surveyid='', $extra_script='')
  34. {
  35. $survey_table = Database :: get_course_table('survey');
  36. $sql = "SELECT * FROM $survey_table";// WHERE is_shared='1'";
  37. $sql_result = api_sql_query($sql,__FILE__,__LINE__);
  38. if(mysql_num_rows($sql_result)>0)
  39. {
  40. $str_survey_list = "";
  41. $str_survey_list .= "<select name=\"exiztingsurvey\" $extra_script>\n";
  42. $str_survey_list .= "<option value=\"\">--Select Survey--</option>";
  43. while($result=mysql_fetch_array($sql_result))
  44. {
  45. $selected = ($seleced_surveyid==$result[survey_id])?"selected":"";
  46. $str_survey_list .= "\n<option value=\"".$result[survey_id]."\" ".$selected.">".$result[title]."</option>";
  47. }
  48. $str_survey_list .= "</select>";
  49. return $str_survey_list;
  50. }
  51. else
  52. {
  53. return false;
  54. }
  55. }
  56. /*function getsurveyid($existing)
  57. {
  58. $survey_table = Database :: get_course_table(TABLE_MAIN_SURVEY);
  59. $sql = "SELECT survey_id FROM $survey_table WHERE title='$existing'" ;
  60. $result = api_sql_query($sql,__FILE__,__LINE__);
  61. $i=0;
  62. $survey_id=mysql_result($result,$i,'survey_id');
  63. echo "in getsurveyid".$survey_id;
  64. return(survey_id);
  65. }
  66. */
  67. function create_group($survey_id,$group_title,$introduction,$table_group)
  68. {
  69. $sql_query = "SELECT * FROM $table_group where groupname='".$group_title."' AND survey_id=".intval($survey_id);
  70. $res = api_sql_query($sql_query, __FILE__, __LINE__);
  71. if(mysql_num_rows($res))
  72. {
  73. return false;
  74. }
  75. else
  76. {
  77. $sql = 'SELECT MAX(sortby) FROM '.$table_group.' WHERE survey_id="'.$survey_id.'"';
  78. $rs = api_sql_query($sql, __FILE__, __LINE__);
  79. list($sortby) = mysql_fetch_array($rs);
  80. $sortby++;
  81. $sql="INSERT INTO $table_group(group_id,survey_id,groupname,introduction, sortby) values('','$survey_id','$group_title','$introduction','$sortby')";
  82. $result=api_sql_query($sql);
  83. return mysql_insert_id();
  84. }
  85. }
  86. function get_survey_author($authorid)
  87. {
  88. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  89. $sql_query = "SELECT * FROM $user_table where user_id='$authorid'";
  90. $res = api_sql_query($sql_query, __FILE__, __LINE__);
  91. $firstname=@mysql_result($res,0,'firstname');
  92. return $firstname;
  93. }
  94. function get_author($db_name,$survey_id)
  95. {
  96. //$table_survey = Database :: get_course_table('survey');
  97. $sql = "SELECT author FROM $db_name.survey WHERE survey_id='$survey_id'";
  98. $res = api_sql_query($sql, __FILE__, __LINE__);
  99. $author=@mysql_result($res,0,'author');
  100. return $author;
  101. }
  102. function get_surveyid($db_name,$group_id)
  103. {
  104. //$group_table = Database :: get_course_table('survey_group');
  105. $sql = "SELECT survey_id FROM $db_name.survey_group WHERE group_id='$group_id'";
  106. $res = api_sql_query($sql, __FILE__, __LINE__);
  107. $surveyid=@mysql_result($res,0,'survey_id');
  108. return $surveyid;
  109. }
  110. /*function get_survey_code($table_survey,$survey_code)
  111. {
  112. $sql="SELECT code FROM $table_survey where code='$survey_code'";
  113. $result=api_sql_query($sql);
  114. $code=mysql_result($result,0,'code');
  115. return($code);
  116. }*/
  117. function get_groupname($db_name,$gid)
  118. {
  119. //$grouptable = Database :: get_course_table('survey_group');
  120. $sql = "SELECT * FROM $db_name.survey_group WHERE group_id='$gid'";
  121. $res=api_sql_query($sql);
  122. $code=@mysql_result($res,0,'groupname');
  123. return($code);
  124. }
  125. function insert_into_group($survey_id,$group_title,$introduction,$tb)
  126. {
  127. $sql="INSERT INTO $tb(group_id,survey_id,group_title,introduction) values('','$survey_id','$group_title','$introduction')";
  128. $result=api_sql_query($sql);
  129. return mysql_insert_id();
  130. }
  131. function get_survey_code($table_survey,$survey_code)
  132. {
  133. $sql="SELECT code FROM $table_survey where code='$survey_code'";
  134. //echo $sql;
  135. //exit;
  136. $result=api_sql_query($sql);
  137. $code=@mysql_result($result,0,'code');
  138. //echo $code;exit;
  139. return($code);
  140. }
  141. function get_survey_list()
  142. {
  143. $survey_table = Database :: get_course_table('survey');
  144. $sql_query = "SELECT survey_id,title FROM $survey_table where title!='' ";
  145. $sql_result = api_sql_query($sql_query,__FILE__,__LINE__);
  146. echo "<select name=\"author\">";
  147. echo "<option value=\"\"><--Select Survey--></optional>";
  148. while ($result =@mysql_fetch_array($sql_result))
  149. {
  150. echo "\n<option value=\"".$result[survey_id]."\">".$result[title]."</option>";
  151. }
  152. echo "</select>";
  153. }
  154. function create_survey($surveycode,$surveytitle, $surveysubtitle, $author, $survey_language, $availablefrom, $availabletill,$isshare, $surveytemplate, $surveyintroduction, $surveythanks, $table_survey, $table_group)
  155. {
  156. //$table_survey = Database :: get_course_table('survey');
  157. $sql = "INSERT INTO $table_survey (code,title, subtitle, author,lang,avail_from,avail_till, is_shared,template,intro,surveythanks,creation_date) values('$surveycode','$surveytitle','$surveysubtitle','$author','$survey_language','$availablefrom','$availabletill','$isshare','$surveytemplate','$surveyintroduction','$surveythanks',curdate())";
  158. $result = api_sql_query($sql, __FILE__, __LINE__);
  159. //$result = api_sql_query($sql);
  160. $survey_id = mysql_insert_id();
  161. $sql2 = "INSERT INTO $table_group(group_id,survey_id,groupname,introduction) values('','$survey_id','No Group','This is your Default Group')";
  162. $result = api_sql_query($sql2, __FILE__, __LINE__);
  163. return $survey_id;
  164. }
  165. function create_survey_in_another_language($id, $lang){
  166. global $_course;
  167. $original_survey = SurveyManager::get_all_datas($id);
  168. // copy the survey itself
  169. $sql = 'INSERT INTO '.$_course['dbName'].'.survey SET
  170. code = "'.$original_survey->code.'",
  171. title = "'.addslashes($original_survey->title).'",
  172. subtitle = "'.addslashes($original_survey->subtitle).'",
  173. author = "'.$original_survey->author.'",
  174. lang = "'.$lang.'",
  175. avail_from = "'.$original_survey->avail_from.'",
  176. avail_till = "'.$original_survey->avail_till.'",
  177. is_shared = "'.$original_survey->is_shared.'",
  178. template = "'.$original_survey->template.'",
  179. intro = "'.addslashes($original_survey->intro).'",
  180. surveythanks = "'.addslashes($original_survey->surveythanks).'",
  181. creation_date = "NOW()"';
  182. $result = api_sql_query($sql, __FILE__, __LINE__);
  183. $new_survey_id = mysql_insert_id();
  184. // copy the groups
  185. $groups = SurveyManager::listGroups($id);
  186. foreach($groups as $group)
  187. {
  188. SurveyManager::import_group($new_survey_id, $group['group_id'], $_course['dbName'], $_course['dbName']);
  189. }
  190. }
  191. function create_survey_attach($surveycode,$surveytitle, $surveysubtitle, $author, $survey_language, $availablefrom, $availabletill,$isshare, $surveytemplate, $surveyintroduction, $surveythanks, $table_survey, $table_group)
  192. {
  193. //$table_survey = Database :: get_course_table('survey');
  194. $sql = "INSERT INTO $table_survey (code,title, subtitle, author,lang,avail_from,avail_till, is_shared,template,intro,surveythanks,creation_date) values('$surveycode','$surveytitle','$surveysubtitle','$author','$survey_language','$availablefrom','$availabletill','$isshare','$surveytemplate','$surveyintroduction','$surveythanks',curdate())";
  195. $result = api_sql_query($sql, __FILE__, __LINE__);
  196. $survey_id = mysql_insert_id();
  197. return $survey_id;
  198. }
  199. function update_survey($surveyid,$surveycode,$surveytitle, $surveysubtitle, $author, $survey_language, $availablefrom, $availabletill,$isshare, $surveytemplate, $surveyintroduction, $surveythanks, $cidReq,$table_course)
  200. {
  201. $sql_course = "SELECT * FROM $table_course WHERE code = '$cidReq'";
  202. $res_course = api_sql_query($sql_course,__FILE__,__LINE__);
  203. $obj_course=@mysql_fetch_object($res_course);
  204. $curr_dbname = $obj_course->db_name ;
  205. $sql = "UPDATE $curr_dbname.survey SET code='$surveycode', title='$surveytitle', subtitle='$surveysubtitle', lang='$survey_language', avail_from='$availablefrom', avail_till='$availabletill', is_shared='$isshare', template='$surveytemplate', intro='$surveyintroduction',surveythanks='$surveythanks' WHERE survey_id='$surveyid'";
  206. api_sql_query($sql, __FILE__, __LINE__);
  207. return $curr_dbname;
  208. }
  209. /*
  210. function create_question($gid,$type,$caption,$answers,$open_ans,$answerT,$answerD,$rating,$table_question)
  211. {
  212. for($i=0;$i<10;$i++)
  213. {
  214. $x.= "'".$answers[$i]."',";
  215. }
  216. //echo "Hello".$x;
  217. for($j=0;$j<10;$j++)
  218. {
  219. if($j==9)
  220. {
  221. $y.= "'".$rating[$j]."'";
  222. }
  223. else
  224. {
  225. $y.= "'".$rating[$j]."',";
  226. }
  227. }
  228. $anst = implode(", " ,$answerT);
  229. $ansd = implode(", " ,$answerD);
  230. $table_question = Database :: get_course_table(TABLE_MAIN_SURVEYQUESTION);
  231. $sql = "INSERT INTO $table_question (gid,type,caption,ans1,ans2,ans3,ans4,ans5,ans6,ans7,ans8,ans9,ans10,open_ans,anst,ansd,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('$gid','$type','$caption',$x'$open_ans','$anst','$ansd',$y)";
  232. $result = api_sql_query($sql);
  233. return mysql_insert_id();
  234. }
  235. function get_question($questionid)
  236. {
  237. $table_question = Database :: get_course_table(TABLE_MAIN_SURVEYQUESTION);
  238. $sql = "SELECT * FROM $table_question where qid='$questionid'";
  239. $res=api_sql_query($sql);
  240. $code=@mysql_result($res,0,'caption');
  241. return($code);
  242. }
  243. */
  244. function create_question($gid,$surveyid,$qtype,$caption,$alignment,$answers,$open_ans,$answerT,$answerD,$rating,$curr_dbname)
  245. {
  246. $sql_sort = "SELECT max(sortby) AS sortby FROM $curr_dbname.questions ";
  247. $res_sort=api_sql_query($sql_sort);
  248. $rs=mysql_fetch_object($res_sort);
  249. $sortby=$rs->sortby;
  250. if(empty($sortby))
  251. {
  252. $sortby=1;
  253. }
  254. else
  255. {
  256. $sortby=$sortby+1;
  257. }
  258. for($i=0;$i<10;$i++)
  259. {
  260. $x.= "'".$answers[$i]."',";
  261. }
  262. for($j=0;$j<10;$j++)
  263. {
  264. if($j==9)
  265. {
  266. $y.= "'".$rating[$j]."'";
  267. }
  268. else
  269. {
  270. $y.= "'".$rating[$j]."',";
  271. }
  272. }
  273. /*if($qtype=='Multiple Choice (multiple answer)')
  274. {
  275. $anst = implode(", " ,$answerT);
  276. $ansd = implode(", " ,$answerD);
  277. }
  278. else
  279. {*/
  280. $anst = $answerT;
  281. $ansd = $answerD;
  282. //}
  283. $sql = "INSERT INTO $curr_dbname.questions (gid,survey_id,qtype,caption,alignment,sortby,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('$gid','$surveyid','$qtype','$caption','$alignment','$sortby',$x'$anst','$ansd',$y)";
  284. $result = api_sql_query($sql);
  285. return mysql_insert_id();
  286. }
  287. function update_question($qid,$qtype,$caption,$alignment,$answers,$open_ans,$curr_dbname)
  288. {
  289. for($i=0;$i<10;$i++)
  290. {
  291. $k=$i+1;
  292. $a.$k= $answers[$i];
  293. }
  294. $anst = $answerT;
  295. $ansd = $answerD;
  296. $sql = "UPDATE $curr_dbname.questions SET qtype='$qtype',caption='$caption',alignment='$alignment',a1='$answers[0]',a2='$answers[1]',a3='$answers[2]',a4='$answers[3]',a5='$answers[4]',a6='$answers[5]',a7='$answers[6]',a8='$answers[7]',a9='$answers[8]',a10='$answers[9]' WHERE qid='$qid'";
  297. $result = api_sql_query($sql);
  298. return mysql_insert_id();
  299. }
  300. function get_question_type($questionid)
  301. {
  302. $table_question = Database :: get_course_table('questions');
  303. $sql = "SELECT * FROM $table_question where qid='$questionid'";
  304. $res=api_sql_query($sql);
  305. $code=@mysql_result($res,0,'type');
  306. return($code);
  307. }
  308. function no_of_question($db_name,$gid)
  309. {
  310. //$table_question = Database :: get_course_table('questions');
  311. $sql = "SELECT * FROM $db_name.questions where gid='$gid'";
  312. $res=api_sql_query($sql);
  313. $code=@mysql_num_rows($res);
  314. return($code);
  315. }
  316. function get_question_data($qid,$curr_dbname)
  317. {
  318. $sql = "SELECT * FROM $curr_dbname.questions where qid='$qid'";
  319. $res=api_sql_query($sql);
  320. $rs=mysql_fetch_object($res);
  321. $properties = get_object_vars($rs);
  322. foreach ($properties as $property=>$val){
  323. $val = stripslashes($val);
  324. $rs->$property = $val;
  325. }
  326. return $rs;
  327. }
  328. function get_data($id, $field) {
  329. global $_course;
  330. $sql='SELECT '.$field.' FROM '.$_course['dbName'].'.survey WHERE survey_id='.intval($id);
  331. $res=api_sql_query($sql);
  332. $code=@mysql_result($res,0);
  333. return($code);
  334. }
  335. function get_all_datas($id) {
  336. global $_course;
  337. $sql='SELECT * FROM '.$_course['dbName'].'.survey WHERE survey_id='.intval($id);
  338. $res=api_sql_query($sql);
  339. return mysql_fetch_object($res);
  340. }
  341. function get_surveyname($db_name,$sid)
  342. {
  343. //$surveytable=Database:: get_course_table('survey');
  344. $sql="SELECT * FROM $db_name.survey WHERE survey_id=$sid";
  345. $res=api_sql_query($sql);
  346. $code=@mysql_result($res,0,'title');
  347. return($code);
  348. }
  349. function get_surveyname_display($sid)
  350. {
  351. $surveytable=Database:: get_course_table('survey');
  352. $sql="SELECT * FROM $surveytable WHERE survey_id=$sid";
  353. $res=api_sql_query($sql);
  354. $code=@mysql_result($res,0,'title');
  355. return($code);
  356. }
  357. /*
  358. function join_survey($question_type)
  359. {
  360. $table_survey = Database :: get_course_table(TABLE_MAIN_SURVEY);
  361. $table_group = Database :: get_course_table(TABLE_MAIN_GROUP);
  362. $table_question = Database :: get_course_table(TABLE_MAIN_SURVEYQUESTION);
  363. echo $sql="select t1.title as stitle, t3.type as type, t3.caption as caption, t2.groupname as groupname from $table_survey t1, $table_group t2, $table_question t3 where t1.survey_id=t2.survey_id and t3.gid=t2.group_id and t3.type='$question_type'";
  364. $sql_result = api_sql_query($sql,__FILE__,__LINE__);
  365. $result = mysql_fetch_object($sql_result);
  366. return ($result);
  367. }
  368. */
  369. function import_questions($import_type, $ids)
  370. {
  371. //$groupname=surveymanager::get_groupname($gid_arr[$index]);
  372. switch ($import_type){
  373. case "survey":
  374. {
  375. }
  376. case "groups":
  377. {
  378. foreach ($ids as $gid){
  379. $sql="insert into $table_question SELECT ('',gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) FROM $table_question where gid=$gid";
  380. }
  381. }
  382. case "questions":
  383. {
  384. }
  385. }
  386. $table_question = Database :: get_course_table('questions');
  387. if(isset($selected_group)){
  388. if($selected_group!=''){
  389. $sql = "SELECT $table_group('survey_id', 'groupname') values('$sid', '$groupname')";
  390. $res = api_sql_query($sql);
  391. $sql = "INSERT INTO $table_group('survey_id', 'groupname') values('$sid', '$groupname')";
  392. $res = api_sql_query($sql);
  393. $gid_arr[$index]+= mysql_insert_id();
  394. $groupids=implode(",",$gid_arr);
  395. }
  396. }
  397. echo $groupids;
  398. }
  399. /**
  400. * This function deletes a survey and all the groups and question belonging to it.
  401. *
  402. * @param unknown_type $survey_id
  403. *
  404. * @author unknown
  405. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, cleanup and refactoring
  406. *
  407. */
  408. function delete_survey($survey_id)
  409. {
  410. $table_survey = Database :: get_course_table('survey');
  411. $table_group = Database :: get_course_table('survey_group');
  412. $table_question = Database :: get_course_table('questions');
  413. // Deleting the survey
  414. $sql = "DELETE FROM $table_survey WHERE survey_id='".$survey_id."'";
  415. api_sql_query($sql,__FILE__,__LINE__);
  416. // Deleting all the questions of the survey
  417. $sql = "select * FROM $table_group WHERE survey_id='".$survey_id."'";
  418. $res = api_sql_query($sql,__FILE__,__LINE__);
  419. while($obj = mysql_fetch_object($res))
  420. {
  421. $sql = "DELETE FROM $table_question WHERE gid='".$obj->group_id."'";
  422. api_sql_query($sql,__FILE__,__LINE__);
  423. }
  424. // Deleting the groups of the survey
  425. $sql = "DELETE FROM $table_group WHERE survey_id='".$survey_id."'";
  426. api_sql_query($sql,__FILE__,__LINE__);
  427. return true;
  428. }
  429. /**
  430. * This function deletes a
  431. *
  432. * @param unknown_type $group_id
  433. * @param unknown_type $curr_dbname
  434. *
  435. * @author unknown
  436. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, refactoring: using correct database calls and removing useless second parameter
  437. */
  438. function delete_group($group_id)
  439. {
  440. // Database table definitions
  441. /** @todo use database constants for the survey tables */
  442. $table_question = Database :: get_course_table('questions');
  443. $table_survey_group = Database :: get_course_table('survey_group');
  444. $sql = "DELETE FROM $table_question WHERE gid='".$group_id."'";
  445. api_sql_query($sql,__FILE__,__LINE__);
  446. $sql = "DELETE FROM $table_survey_group WHERE group_id='".$group_id."'";
  447. api_sql_query($sql,__FILE__,__LINE__);
  448. }
  449. function ques_id_group_name($qid)
  450. {
  451. $ques_table=Database::get_course_table('questions');
  452. $sql="SELECT gid FROM $ques_table where qid=$qid";
  453. $res=api_sql_query($sql);
  454. $id=@mysql_result($res,0,'gid');
  455. $gname=surveymanager::get_groupname($id);
  456. return($gname);
  457. }
  458. function insert_questions($sid,$newgid,$gid,$table_group)
  459. {
  460. $sql_select = "SELECT * FROM $table_group WHERE group_id IN (".$gid.")";
  461. $res = api_sql_query($sql_select);
  462. $num = mysql_num_rows($res);
  463. $i=0;
  464. while($i<$num)
  465. {
  466. $sql_insert = "INSERT INTO $table_group(group_id, survey_id, groupname) values('', '$sid', 'Imported Group')";
  467. $result = api_sql_query($sql_insert);
  468. $i++;
  469. }
  470. }
  471. function select_group_list($survey_id, $seleced_groupid='', $extra_script='')
  472. {
  473. $group_table = Database :: get_course_table('survey_group');
  474. $sql = "SELECT * FROM $group_table WHERE survey_id='$survey_id'";
  475. $sql_result = api_sql_query($sql,__FILE__,__LINE__);
  476. if(mysql_num_rows($sql_result)>0)
  477. {
  478. $str_group_list = "";
  479. $str_group_list .= "<select name=\"exiztinggroup\" $extra_script>\n";
  480. while($result=mysql_fetch_array($sql_result))
  481. {
  482. $selected = ($seleced_groupid==$result[group_id])?"selected":"";
  483. $str_group_list .= "\n<option value=\"".$result[group_id]."\" ".$selected.">".$result[groupname]."</option>\n";
  484. }
  485. $str_group_list .= "</select>";
  486. return $str_group_list;
  487. }
  488. else
  489. {
  490. return false;
  491. }
  492. }
  493. //For importing the groups to new survey
  494. function insert_groups($sid,$newgid,$gids,$table_group,$table_question)
  495. {
  496. $gid_arr = explode(",",$gids);
  497. $num = count($gid_arr);
  498. $queryone = "SELECT * FROM $table_question WHERE gid = '$newgid'";
  499. $rs = api_sql_query($queryone);
  500. $numrs=mysql_num_rows($rs);
  501. for($k=0;$k<$numrs;$k++)
  502. {
  503. $imp[]=mysql_result($rs,$k,"imported_group");
  504. }
  505. $imp=@array_unique($imp);
  506. for($n=0;$n<count($gid_arr);$n++)
  507. {
  508. if(@in_array($gid_arr[$n],$imp))
  509. {
  510. $gname=surveymanager::get_groupname($gid_arr[$n]);
  511. $alr[]=$gname;
  512. //$alr[]=$gid_arr[$n];
  513. //$unique_arr = @array_unique($alr);
  514. // print_r($unique_arr);
  515. }
  516. }
  517. //$unique_arr = @array_unique($alr);
  518. //print_r($unique_arr);
  519. /*if($msg)
  520. {
  521. echo "You have already imported the following group(s)";
  522. echo "<br>".$msg;
  523. }*/
  524. for($index=0;$index<$num;$index++)
  525. {
  526. if(@!in_array($gid_arr[$index],$imp))
  527. {
  528. $temp_gid = $gid_arr[$index];
  529. $sql = "SELECT * FROM $table_question WHERE gid = '$temp_gid'";
  530. $res = api_sql_query($sql);
  531. $num_rows = mysql_num_rows($res);
  532. while($obj = mysql_fetch_object($res))
  533. {
  534. $temp_qtype = $obj->qtype;
  535. $temp_caption = $obj->caption;
  536. $y="";
  537. $x="";
  538. for($i=1;$i<=10;$i++)
  539. {
  540. $temp = "a".$i;
  541. $x.= "'".$obj->$temp."',"; /*this variable contains concatenated values and need to be refreshed each time before the loop starts!*/
  542. }
  543. for($j=1;$j<=10;$j++)
  544. {
  545. if($j==10)
  546. {
  547. $temps = "r".$j;
  548. $y.= "'".$obj->$temps."'";
  549. }
  550. else
  551. {
  552. $temps = "r".$j;
  553. $y.= "'".$obj->$temps."',";
  554. }
  555. }
  556. $sql_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$newgid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$temp_gid')";
  557. $res2 = api_sql_query($sql_insert);
  558. }
  559. }
  560. }
  561. }
  562. function display_imported_group($sid,$table_group,$table_question)
  563. {
  564. $sql = "SELECT group_id FROM $table_group WHERE survey_id='$sid'";
  565. $res = api_sql_query($sql);
  566. $num = @mysql_num_rows($res);
  567. //echo "ths is num".$num;
  568. $parameters = array();
  569. $displays = array();
  570. while($obj = @mysql_fetch_object($res))
  571. {
  572. $groupid = $obj->group_id;
  573. $query = "SELECT * FROM $table_question WHERE gid = '$groupid'";
  574. $result = api_sql_query($query);
  575. while($object = @mysql_fetch_object($result))
  576. {
  577. $display = array();
  578. $display[] = '<input type="checkbox" name="course[]" value="'.$object->qid.'"/>';
  579. $display[] = $object->caption;
  580. $display[] = $object->qtype;
  581. $id = $object->gid;
  582. //echo "THIS IS GROUP NAME ID".$id;
  583. $gname = surveymanager::get_groupname($id);
  584. $display[] = $gname;
  585. $displays[] = $display;
  586. }
  587. }
  588. $table_header[] = array('', false);
  589. $table_header[] = array(get_lang('Question'), true);
  590. $table_header[] = array(get_lang('QuestionType'), true);
  591. $table_header[] = array(get_lang('Group'), true);
  592. Display :: display_sortable_table($table_header, $displays, array (), array (), $parameters);
  593. }
  594. function attach_survey($surveyid,$newsurveyid,$db_name,$curr_dbname)
  595. //For attaching the whole survey with its groups and questions
  596. {
  597. $sql = "SELECT * FROM $db_name.survey_group WHERE survey_id = '$surveyid'";
  598. $res = api_sql_query($sql,__FILE__,__LINE__);
  599. while($obj=@mysql_fetch_object($res))
  600. {
  601. $groupname=addslashes($obj->groupname);
  602. $introduction=addslashes($obj->introduction);
  603. $sql_insert = "INSERT INTO $curr_dbname.survey_group(group_id,survey_id,groupname,introduction) values('','$newsurveyid','$groupname','$introduction')";
  604. $resnext = api_sql_query($sql_insert,__FILE__,__LINE__);
  605. $groupid = mysql_insert_id();
  606. $sql_q = "SELECT * FROM $db_name.questions WHERE gid = '$obj->group_id'";
  607. $res_q = api_sql_query($sql_q,__FILE__,__LINE__);
  608. while($obj_q = mysql_fetch_object($res_q))
  609. {
  610. $caption1=addslashes($obj_q->caption);
  611. $a1=addslashes($obj_q->a1);
  612. $a2=addslashes($obj_q->a2);
  613. $a3=addslashes($obj_q->a3);
  614. $a4=addslashes($obj_q->a4);
  615. $a5=addslashes($obj_q->a5);
  616. $a6=addslashes($obj_q->a6);
  617. $a7=addslashes($obj_q->a7);
  618. $a8=addslashes($obj_q->a8);
  619. $a9=addslashes($obj_q->a9);
  620. $a10=addslashes($obj_q->a10);
  621. $at=addslashes($obj_q->at);
  622. $ad=addslashes($obj_q->ad);
  623. $r1=addslashes($obj_q->r1);
  624. $r2=addslashes($obj_q->r2);
  625. $r3=addslashes($obj_q->r3);
  626. $r4=addslashes($obj_q->r4);
  627. $r5=addslashes($obj_q->r5);
  628. $r6=addslashes($obj_q->r6);
  629. $r7=addslashes($obj_q->r7);
  630. $r8=addslashes($obj_q->r8);
  631. $r9=addslashes($obj_q->r9);
  632. $r10=addslashes($obj_q->r10);
  633. $sql_sort = "SELECT max(sortby) AS sortby FROM $curr_dbname.questions ";
  634. $res_sort=api_sql_query($sql_sort);
  635. $rs=mysql_fetch_object($res_sort);
  636. $sortby=$rs->sortby;
  637. if(empty($sortby))
  638. {$sortby=1;}
  639. else{$sortby=$sortby+1;}
  640. $sql_q_insert = "INSERT INTO $curr_dbname.questions (qid,gid,survey_id,qtype,caption,alignment,sortby,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('','$groupid','$newsurveyid','$obj_q->qtype','$caption1','$obj_q->alignment','$sortby','$a1','$a2','$a3','$a4','$a5','$a6','$a7','$a8','$a9','$a10','$at','$ad','$r1','$r2','$r3','$r4','$r5','$r6','$r7','$r8','$r9','$r10')";
  641. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  642. }
  643. }
  644. }
  645. function update_group($groupid,$surveyid,$groupnamme,$introduction,$curr_dbname)
  646. {
  647. $sql = "UPDATE $curr_dbname.survey_group SET group_id='$groupid', survey_id='$surveyid', groupname='$groupnamme', introduction='$introduction' WHERE group_id='$groupid'";
  648. api_sql_query($sql, __FILE__, __LINE__);
  649. }
  650. /*
  651. function insert_old_groups($sid,$gids,$table_group,$table_question)
  652. {
  653. $gid_arr = explode(",",$gids);
  654. $index = count($gid_arr);
  655. ($gid_arr);
  656. for($p=0;$p<$index;$p++)
  657. {
  658. $sql = "SELECT * FROM $table_group WHERE group_id = '$gid_arr[$p]'";
  659. $res = api_sql_query($sql);
  660. $obj = mysql_fetch_object($res);
  661. $gname = $obj->groupname;
  662. if($gname=='Default')
  663. {
  664. $query = "SELECT * FROM $table_group WHERE survey_id = '$sid' AND groupname = 'Default'";
  665. $result = api_sql_query($query);
  666. $object = mysql_fetch_object($result);
  667. $gid = $object->group_id;
  668. $sql_def_check = "SELECT * FROM $table_question WHERE gid = '$gid'";
  669. $res_def_check = api_sql_query($sql_def_check);
  670. $count_def_check = mysql_num_rows($res_def_check);
  671. for($ctr=0;$ctr<$count_def_check;$ctr++)
  672. {
  673. $imp[]=mysql_result($res_def_check,$ctr,"imported_group");
  674. }
  675. $imp = @array_unique($imp);
  676. if(!@in_array($gid_arr[$p],$imp))
  677. {
  678. $sql_ques = "SELECT * FROM $table_question WHERE gid= '$gid_arr[$p]'";
  679. $res_ques = api_sql_query($sql_ques);
  680. $num = mysql_num_rows($res_ques);
  681. while($obj_ques = mysql_fetch_object($res_ques))
  682. {
  683. $temp_qtype = $obj_ques->qtype;
  684. $temp_caption = $obj_ques->caption;
  685. $anst = $obj_ques->at;
  686. $ansd = $obj_ques->ad;
  687. $y="";
  688. $x="";
  689. for($i=1;$i<=10;$i++)
  690. {
  691. $temp = "a".$i;
  692. $x.= "'".$obj_ques->$temp."',";
  693. }
  694. for($j=1;$j<=10;$j++)
  695. {
  696. if($j==10)
  697. {
  698. $temps = "r".$j;
  699. $y.= "'".$obj_ques->$temps."'";
  700. }
  701. else
  702. {
  703. $temps = "r".$j;
  704. $y.= "'".$obj_ques->$temps."',";
  705. }
  706. }
  707. $sql_ques_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  708. $res_ques_insert = api_sql_query($sql_ques_insert);
  709. }
  710. }
  711. else
  712. {
  713. $flag = 1;
  714. }
  715. }
  716. else
  717. {
  718. $intro = $obj->introduction;
  719. $sql_check = "SELECT * FROM $table_group WHERE survey_id = '$sid'";
  720. $res_check = api_sql_query($sql_check);
  721. $num_check = mysql_num_rows($res_check);
  722. for($k=0;$k<$num_check;$k++)
  723. {
  724. $imp[]=mysql_result($res_check,$k,"imported_group");
  725. }
  726. $imp = @array_unique($imp);
  727. if(!@in_array($gid_arr[$p],$imp))
  728. {
  729. $sql_insert = "INSERT INTO $table_group(group_id,survey_id,groupname,introduction,imported_group) values('','$sid','$gname','$intro','$gid_arr[$p]')";
  730. $res_insert = api_sql_query($sql_insert);
  731. $new_gid = mysql_insert_id();
  732. $sql_ques = "SELECT * FROM $table_question WHERE gid= '$gid_arr[$p]'";
  733. $res_ques = api_sql_query($sql_ques);
  734. $num = mysql_num_rows($res_ques);
  735. while($obj_ques = mysql_fetch_object($res_ques))
  736. {
  737. $temp_qtype = $obj_ques->qtype;
  738. $temp_caption = $obj_ques->caption;
  739. $anst = $obj_ques->at;
  740. $ansd = $obj_ques->ad;
  741. $y="";
  742. $x="";
  743. for($i=1;$i<=10;$i++)
  744. {
  745. $temp = "a".$i;
  746. $x.= "'".$obj_ques->$temp."',";
  747. }
  748. for($j=1;$j<=10;$j++)
  749. {
  750. if($j==10)
  751. {
  752. $temps = "r".$j;
  753. $y.= "'".$obj_ques->$temps."'";
  754. }
  755. else
  756. {
  757. $temps = "r".$j;
  758. $y.= "'".$obj_ques->$temps."',";
  759. }
  760. }
  761. $sql_ques_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$new_gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  762. $res_ques_insert = api_sql_query($sql_ques_insert);
  763. }
  764. }
  765. else
  766. {
  767. $flag = 1;
  768. }
  769. }
  770. }
  771. return ($flag);
  772. }
  773. */
  774. function insert_old_groups($sid,$gids,$table_group,$table_question,$db_name,$cidReq)
  775. {
  776. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  777. $sql = "SELECT * FROM $table_course WHERE code = '$cidReq'";
  778. $res = api_sql_query($sql,__FILE__,__LINE__);
  779. $obj_name=@mysql_fetch_object($res);
  780. $current_db_name = $obj_name->db_name ;
  781. $gid_arr = explode(",",$gids);
  782. $index = count($gid_arr);
  783. ($gid_arr);
  784. for($p=0;$p<$index;$p++)
  785. {
  786. $sql = "SELECT * FROM $db_name.survey_group WHERE group_id = '$gid_arr[$p]'";
  787. $res = api_sql_query($sql);
  788. $obj = mysql_fetch_object($res);
  789. $gname = $obj->groupname;
  790. if($gname=='No Group')
  791. {
  792. $query = "SELECT * FROM $db_name.survey_group WHERE survey_id = '$sid' AND groupname = 'No Group'";
  793. $result = api_sql_query($query);
  794. $object = mysql_fetch_object($result);
  795. $gid = $object->group_id;
  796. $sql_def_check = "SELECT * FROM $db_name.questions WHERE gid = '$gid'";
  797. $res_def_check = api_sql_query($sql_def_check);
  798. $count_def_check = mysql_num_rows($res_def_check);
  799. for($ctr=0;$ctr<$count_def_check;$ctr++)
  800. {
  801. $imp[]=mysql_result($res_def_check,$ctr,"imported_group");
  802. }
  803. $imp = @array_unique($imp);
  804. $gid_arr[$p];
  805. if(!@in_array($gid_arr[$p],$imp))
  806. {
  807. $sql_ques = "SELECT * FROM $db_name.questions WHERE gid= '$gid_arr[$p]'";
  808. $res_ques = api_sql_query($sql_ques);
  809. $num = mysql_num_rows($res_ques);
  810. while($obj_ques = mysql_fetch_object($res_ques))
  811. {
  812. $temp_qtype = $obj_ques->qtype;
  813. $temp_caption = $obj_ques->caption;
  814. $anst = $obj_ques->at;
  815. $ansd = $obj_ques->ad;
  816. $y="";
  817. $x="";
  818. for($i=1;$i<=10;$i++)
  819. {
  820. $temp = "a".$i;
  821. $x.= "'".$obj_ques->$temp."',"; /*this variable contains concatenated values and need to be refreshed each time before the loop starts!*/
  822. }
  823. for($j=1;$j<=10;$j++)
  824. {
  825. if($j==10)
  826. {
  827. $temps = "r".$j;
  828. $y.= "'".$obj_ques->$temps."'";
  829. }
  830. else
  831. {
  832. $temps = "r".$j;
  833. $y.= "'".$obj_ques->$temps."',";
  834. }
  835. }
  836. $sql_ques_insert = "INSERT INTO $current_db_name.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  837. $res_ques_insert = api_sql_query($sql_ques_insert);
  838. }
  839. }
  840. else
  841. {
  842. $flag = 1;
  843. }
  844. }
  845. else
  846. {
  847. $intro = $obj->introduction;
  848. $sql_check = "SELECT * FROM $db_name.survey_group WHERE survey_id = '$sid'";
  849. $res_check = api_sql_query($sql_check);
  850. $num_check = mysql_num_rows($res_check);
  851. for($k=0;$k<$num_check;$k++)
  852. {
  853. $imp[]=mysql_result($res_check,$k,"imported_group");
  854. }
  855. $imp = @array_unique($imp);
  856. if(!@in_array($gid_arr[$p],$imp))
  857. {
  858. $sql_insert = "INSERT INTO $current_db_name.survey_group(group_id,survey_id,groupname,introduction,imported_group) values('','$sid','$gname','$intro','$gid_arr[$p]')";
  859. $res_insert = api_sql_query($sql_insert);
  860. $new_gid = mysql_insert_id();
  861. $sql_ques = "SELECT * FROM $db_name.questions WHERE gid= '$gid_arr[$p]'";
  862. $res_ques = api_sql_query($sql_ques);
  863. $num = mysql_num_rows($res_ques);
  864. while($obj_ques = mysql_fetch_object($res_ques))
  865. {
  866. $temp_qtype = $obj_ques->qtype;
  867. $temp_caption = $obj_ques->caption;
  868. $anst = $obj_ques->at;
  869. $ansd = $obj_ques->ad;
  870. $y="";
  871. $x="";
  872. for($i=1;$i<=10;$i++)
  873. {
  874. $temp = "a".$i;
  875. $x.= "'".$obj_ques->$temp."',"; /*this variable contains concatenated values and need to be refreshed each time before the loop starts!*/
  876. }
  877. for($j=1;$j<=10;$j++)
  878. {
  879. if($j==10)
  880. {
  881. $temps = "r".$j;
  882. $y.= "'".$obj_ques->$temps."'";
  883. }
  884. else
  885. {
  886. $temps = "r".$j;
  887. $y.= "'".$obj_ques->$temps."',";
  888. }
  889. }
  890. $sql_ques_insert = "INSERT INTO $current_db_name.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$new_gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  891. $res_ques_insert = api_sql_query($sql_ques_insert);
  892. }
  893. }
  894. else
  895. {
  896. $flag = 1;
  897. }
  898. }
  899. }
  900. return ($flag);
  901. }
  902. function import_question($surveyid,$qids,$table_group,$table_question,$db_name,$cidReq,$yes)
  903. {
  904. $table_course = Database :: get_main_table(TABLE_MAIN_COURSE);
  905. $sql_course = "SELECT * FROM $table_course WHERE code = '$cidReq'";
  906. $res_course = api_sql_query($sql_course,__FILE__,__LINE__);
  907. $obj_name=@mysql_fetch_object($res_course);
  908. $current_db_name = $obj_name->db_name ;
  909. $qid=explode(",",$qids);
  910. $count = count($qid);
  911. for($i=0; $i<$count; $i++)
  912. {
  913. $sql_q = "SELECT * FROM $table_question WHERE qid = '$qid[$i]'";
  914. $res_q = api_sql_query($sql_q,__FILE__,__LINE__);
  915. $obj=@mysql_fetch_object($res_q);
  916. $oldgid=$obj->gid;
  917. $sql = "SELECT * FROM $table_group WHERE group_id = '$oldgid'";
  918. $res = api_sql_query($sql,__FILE__,__LINE__);
  919. $obj_gr = @mysql_fetch_object($res);
  920. $gname = $obj_gr->groupname;
  921. $gintro = $obj_gr->introduction;
  922. $sql_gid = "SELECT * FROM $table_group WHERE survey_id = '$surveyid' AND groupname = '$gname'";
  923. $res_gid = api_sql_query($sql_gid,__FILE__,__LINE__);
  924. $num=mysql_num_rows($res_gid);
  925. $obj_gid=@mysql_fetch_object($res_gid);
  926. $sql_quesid = "SELECT * FROM $table_question WHERE gid = '$obj_gid->group_id' AND caption = '$obj->caption'";
  927. $res_quesid = api_sql_query($sql_quesid,__FILE__,__LINE__);
  928. $num_ques=mysql_num_rows($res_quesid);
  929. if($num_ques>0)
  930. {
  931. $message=1;
  932. //echo "<div align=\"center\"><strong><font color=\"#FF0000\">Already Imported !</font></strong></div>" ;
  933. }
  934. else
  935. {
  936. if($num>0 && $yes=="yes")
  937. {
  938. $sql_q_insert = "INSERT INTO $current_db_name.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('','$obj_gid->group_id','$obj->qtype','$obj->caption','$obj->a1','$obj->a2','$obj->a3','$obj->a4','$obj->a5','$obj->a6','$obj->a7','$obj->a8','$obj->a9','$obj->a10','$obj->at','$obj->ad','$obj->r1','$obj->r2','$obj->r3','$obj->r4','$obj->r5','$obj->r6','$obj->r7','$obj->r8','$obj->r9','$obj->r10')";
  939. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  940. }
  941. else
  942. {
  943. $sql_ginsert="INSERT INTO $current_db_name.survey_group(group_id,survey_id,groupname,introduction) values('','$surveyid','$gname','$gintro')";
  944. api_sql_query($sql_ginsert,__FILE__,__LINE__);
  945. $new_gid = mysql_insert_id();
  946. $sql_q_insert = "INSERT INTO $current_db_name.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('','$new_gid','$obj->qtype','$obj->caption','$obj->a1','$obj->a2','$obj->a3','$obj->a4','$obj->a5','$obj->a6','$obj->a7','$obj->a8','$obj->a9','$obj->a10','$obj->at','$obj->ad','$obj->r1','$obj->r2','$obj->r3','$obj->r4','$obj->r5','$obj->r6','$obj->r7','$obj->r8','$obj->r9','$obj->r10')";
  947. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  948. }
  949. }
  950. }
  951. return $message;
  952. }
  953. function create_course_survey_rel($cidReq,$survey_id,$table_course,$table_course_survey_rel)
  954. {
  955. $sql = "SELECT * FROM $table_course WHERE code = '$cidReq'";
  956. $res = api_sql_query($sql,__FILE__,__LINE__);
  957. $obj=@mysql_fetch_object($res);
  958. $db_name = $obj->db_name ;
  959. $sql="INSERT INTO $table_course_survey_rel(id,course_code,db_name,survey_id) values('','$cidReq','$db_name','$survey_id')";
  960. api_sql_query($sql,__FILE__,__LINE__);
  961. return $db_name;
  962. }
  963. function import_existing_question($surveyid,$qids,$table_group,$table_question,$yes)
  964. {
  965. $qid=explode(",",$qids);
  966. $count = count($qid);
  967. for($i=0; $i<$count; $i++)
  968. {
  969. $sql_q = "SELECT * FROM $table_question WHERE qid = '$qid[$i]'";
  970. $res_q = api_sql_query($sql_q,__FILE__,__LINE__);
  971. $obj=@mysql_fetch_object($res_q);
  972. $oldgid=$obj->gid;
  973. $sql = "SELECT * FROM $table_group WHERE group_id = '$oldgid'";
  974. $res = api_sql_query($sql,__FILE__,__LINE__);
  975. $obj_gr = @mysql_fetch_object($res);
  976. $gname = $obj_gr->groupname;
  977. $gintro = $obj_gr->introduction;
  978. $sql_gid = "SELECT * FROM $table_group WHERE survey_id = '$surveyid' AND groupname = '$gname'";
  979. $res_gid = api_sql_query($sql_gid,__FILE__,__LINE__);
  980. $num=mysql_num_rows($res_gid);
  981. $obj_gid=@mysql_fetch_object($res_gid);
  982. $sql_quesid = "SELECT * FROM $table_question WHERE gid = '$obj_gid->group_id' AND caption = '$obj->caption'";
  983. $res_quesid = api_sql_query($sql_quesid,__FILE__,__LINE__);
  984. $num_ques=mysql_num_rows($res_quesid);
  985. if($num_ques>0)
  986. {
  987. $message=1;
  988. //echo "<div align=\"center\"><strong><font color=\"#FF0000\">Already Imported !</font></strong></div>" ;
  989. }
  990. else
  991. {
  992. if($num>0 && $yes=="yes")
  993. {
  994. $sql_q_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('','$obj_gid->group_id','$obj->qtype','$obj->caption','$obj->a1','$obj->a2','$obj->a3','$obj->a4','$obj->a5','$obj->a6','$obj->a7','$obj->a8','$obj->a9','$obj->a10','$obj->at','$obj->ad','$obj->r1','$obj->r2','$obj->r3','$obj->r4','$obj->r5','$obj->r6','$obj->r7','$obj->r8','$obj->r9','$obj->r10')";
  995. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  996. }
  997. else
  998. {
  999. $sql_ginsert="INSERT INTO $table_group(group_id,survey_id,groupname,introduction) values('','$surveyid','$gname','$gintro')";
  1000. api_sql_query($sql_ginsert,__FILE__,__LINE__);
  1001. $new_gid = mysql_insert_id();
  1002. $sql_q_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10) values('','$new_gid','$obj->qtype','$obj->caption','$obj->a1','$obj->a2','$obj->a3','$obj->a4','$obj->a5','$obj->a6','$obj->a7','$obj->a8','$obj->a9','$obj->a10','$obj->at','$obj->ad','$obj->r1','$obj->r2','$obj->r3','$obj->r4','$obj->r5','$obj->r6','$obj->r7','$obj->r8','$obj->r9','$obj->r10')";
  1003. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  1004. }
  1005. }
  1006. }
  1007. return $message;
  1008. }
  1009. function insert_existing_groups($sid,$gids,$table_group,$table_question)
  1010. {
  1011. $gid_arr = explode(",",$gids);
  1012. $index = count($gid_arr);
  1013. ($gid_arr);
  1014. for($p=0;$p<$index;$p++)
  1015. {
  1016. $sql = "SELECT * FROM $table_group WHERE group_id = '$gid_arr[$p]'";
  1017. $res = api_sql_query($sql);
  1018. $obj = mysql_fetch_object($res);
  1019. $gname = $obj->groupname;
  1020. if($gname=='No Group')
  1021. {
  1022. $query = "SELECT * FROM $table_group WHERE survey_id = '$sid' AND groupname = 'No Group'";
  1023. $result = api_sql_query($query);
  1024. $object = mysql_fetch_object($result);
  1025. $gid = $object->group_id;
  1026. $sql_def_check = "SELECT * FROM $table_question WHERE gid = '$gid'";
  1027. $res_def_check = api_sql_query($sql_def_check);
  1028. $count_def_check = mysql_num_rows($res_def_check);
  1029. for($ctr=0;$ctr<$count_def_check;$ctr++)
  1030. {
  1031. $imp[]=mysql_result($res_def_check,$ctr,"imported_group");
  1032. }
  1033. $imp = @array_unique($imp);
  1034. if(!@in_array($gid_arr[$p],$imp))
  1035. {
  1036. $sql_ques = "SELECT * FROM $table_question WHERE gid= '$gid_arr[$p]'";
  1037. $res_ques = api_sql_query($sql_ques);
  1038. $num = mysql_num_rows($res_ques);
  1039. while($obj_ques = mysql_fetch_object($res_ques))
  1040. {
  1041. $temp_qtype = $obj_ques->qtype;
  1042. $temp_caption = $obj_ques->caption;
  1043. $anst = $obj_ques->at;
  1044. $ansd = $obj_ques->ad;
  1045. $y="";
  1046. $x="";
  1047. for($i=1;$i<=10;$i++)
  1048. {
  1049. $temp = "a".$i;
  1050. $x.= "'".$obj_ques->$temp."',"; /*this variable contains concatenated values and need to be refreshed each time before the loop starts!*/
  1051. }
  1052. for($j=1;$j<=10;$j++)
  1053. {
  1054. if($j==10)
  1055. {
  1056. $temps = "r".$j;
  1057. $y.= "'".$obj_ques->$temps."'";
  1058. }
  1059. else
  1060. {
  1061. $temps = "r".$j;
  1062. $y.= "'".$obj_ques->$temps."',";
  1063. }
  1064. }
  1065. $sql_ques_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  1066. $res_ques_insert = api_sql_query($sql_ques_insert);
  1067. }
  1068. }
  1069. else
  1070. {
  1071. $flag = 1;
  1072. }
  1073. }
  1074. else
  1075. {
  1076. $intro = $obj->introduction;
  1077. $sql_check = "SELECT * FROM $table_group WHERE survey_id = '$sid'";
  1078. $res_check = api_sql_query($sql_check);
  1079. $num_check = mysql_num_rows($res_check);
  1080. for($k=0;$k<$num_check;$k++)
  1081. {
  1082. $imp[]=mysql_result($res_check,$k,"imported_group");
  1083. }
  1084. $imp = @array_unique($imp);
  1085. if(!@in_array($gid_arr[$p],$imp))
  1086. {
  1087. $sql_insert = "INSERT INTO $table_group(group_id,survey_id,groupname,introduction,imported_group) values('','$sid','$gname','$intro','$gid_arr[$p]')";
  1088. $res_insert = api_sql_query($sql_insert);
  1089. $new_gid = mysql_insert_id();
  1090. $sql_ques = "SELECT * FROM $table_question WHERE gid= '$gid_arr[$p]'";
  1091. $res_ques = api_sql_query($sql_ques);
  1092. $num = mysql_num_rows($res_ques);
  1093. while($obj_ques = mysql_fetch_object($res_ques))
  1094. {
  1095. $temp_qtype = $obj_ques->qtype;
  1096. $temp_caption = $obj_ques->caption;
  1097. $anst = $obj_ques->at;
  1098. $ansd = $obj_ques->ad;
  1099. $y="";
  1100. $x="";
  1101. for($i=1;$i<=10;$i++)
  1102. {
  1103. $temp = "a".$i;
  1104. $x.= "'".$obj_ques->$temp."',"; /*this variable contains concatenated values and need to be refreshed each time before the loop starts!*/
  1105. }
  1106. for($j=1;$j<=10;$j++)
  1107. {
  1108. if($j==10)
  1109. {
  1110. $temps = "r".$j;
  1111. $y.= "'".$obj_ques->$temps."'";
  1112. }
  1113. else
  1114. {
  1115. $temps = "r".$j;
  1116. $y.= "'".$obj_ques->$temps."',";
  1117. }
  1118. }
  1119. $sql_ques_insert = "INSERT INTO $table_question (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_group) values('','$new_gid','$temp_qtype','$temp_caption',$x'$anst','$ansd',$y,'$gid_arr[$p]')";
  1120. $res_ques_insert = api_sql_query($sql_ques_insert);
  1121. }
  1122. }
  1123. else
  1124. {
  1125. $flag = 1;
  1126. }
  1127. }
  1128. }
  1129. return ($flag);
  1130. }
  1131. function pick_surveyname($sid)
  1132. {
  1133. $surveytable=Database:: get_course_table('survey');
  1134. $sql="SELECT * FROM $surveytable WHERE survey_id=$sid";
  1135. $res=api_sql_query($sql);
  1136. $code=@mysql_result($res,0,'title');
  1137. return($code);
  1138. }
  1139. function pick_author($survey_id)
  1140. {
  1141. $survey_table = Database :: get_course_table('survey');
  1142. $sql = "SELECT author FROM $survey_table WHERE survey_id='$survey_id'";
  1143. $res = api_sql_query($sql, __FILE__, __LINE__);
  1144. $author=@mysql_result($res,0,'author');
  1145. return $author;
  1146. }
  1147. function question_import($surveyid,$qids,$db_name,$curr_dbname)
  1148. {
  1149. $qid=explode(",",$qids);
  1150. $count = count($qid);
  1151. for($i=0; $i<$count; $i++)
  1152. {
  1153. $sql_sort = "SELECT max(sortby) AS sortby FROM $curr_dbname.questions ";
  1154. $res_sort=api_sql_query($sql_sort);
  1155. $rs=mysql_fetch_object($res_sort);
  1156. $sortby=$rs->sortby;
  1157. if(empty($sortby))
  1158. {$sortby=1;}
  1159. else{$sortby=$sortby+1;}
  1160. $sql_q = "SELECT * FROM $db_name.questions WHERE qid = '$qid[$i]'";
  1161. $res_q = api_sql_query($sql_q,__FILE__,__LINE__);
  1162. $obj=@mysql_fetch_object($res_q);
  1163. $oldgid=$obj->gid;
  1164. $caption1=addslashes($obj->caption);
  1165. $a1=addslashes($obj->a1);
  1166. $a2=addslashes($obj->a2);
  1167. $a3=addslashes($obj->a3);
  1168. $a4=addslashes($obj->a4);
  1169. $a5=addslashes($obj->a5);
  1170. $a6=addslashes($obj->a6);
  1171. $a7=addslashes($obj->a7);
  1172. $a8=addslashes($obj->a8);
  1173. $a9=addslashes($obj->a9);
  1174. $a10=addslashes($obj->a10);
  1175. $at=addslashes($obj->at);
  1176. $ad=addslashes($obj->ad);
  1177. $r1=addslashes($obj->r1);
  1178. $r2=addslashes($obj->r2);
  1179. $r3=addslashes($obj->r3);
  1180. $r4=addslashes($obj->r4);
  1181. $r5=addslashes($obj->r5);
  1182. $r6=addslashes($obj->r6);
  1183. $r7=addslashes($obj->r7);
  1184. $r8=addslashes($obj->r8);
  1185. $r9=addslashes($obj->r9);
  1186. $r10=addslashes($obj_q->r10);
  1187. //$sql_gr = "SELECT * FROM $db_name.survey_group WHERE group_id = '$oldgid'";
  1188. //$res_gr = api_sql_query($sql_gr,__FILE__,__LINE__);
  1189. // $obj_gr=@mysql_fetch_object($res_gr);
  1190. //$groupname = $obj_gr->groupname
  1191. $sql_quesid = "SELECT * FROM $curr_dbname.questions WHERE survey_id = '$surveyid' AND imported_question = '$qid[$i]' AND db_name = '$db_name'";
  1192. $res_quesid = api_sql_query($sql_quesid,__FILE__,__LINE__);
  1193. $num_ques=mysql_num_rows($res_quesid);
  1194. if($num_ques>0)
  1195. {
  1196. $message=1;
  1197. }
  1198. else
  1199. {
  1200. $sql_group = "SELECT * FROM $db_name.survey_group WHERE group_id = '$oldgid'";
  1201. $res_group = api_sql_query($sql_group,__FILE__,__LINE__);
  1202. $obj_group=@mysql_fetch_object($res_group);
  1203. $groupname = $obj_group->groupname;
  1204. $sql = "SELECT * FROM $curr_dbname.survey_group WHERE groupname = '$groupname' AND survey_id = '$surveyid'";
  1205. $res = api_sql_query($sql,__FILE__,__LINE__);
  1206. $obj_gro = mysql_fetch_object($res);
  1207. $num_group=mysql_num_rows($res);
  1208. if($num_group>0)
  1209. {
  1210. $sql_q_insert = "INSERT INTO $curr_dbname.questions (qid,gid,survey_id,qtype,caption,alignment,sortby,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_question,db_name) values('','$obj_gro->group_id','$surveyid','$obj->qtype','$caption1','$obj->alignment','$sortby','$a1','$a2','$a3','$a4','$a5','$a6','$a7','$a8','$a9','$a10','$at','$ad','$r1','$r2','$r3','$r4','$r5','$r6','$r7','$r8','$r9','$r10','$qid[$i]','$db_name')";
  1211. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  1212. }
  1213. else
  1214. {
  1215. //$num_group;
  1216. $sql_ginsert="INSERT INTO $curr_dbname.survey_group(group_id,survey_id,groupname,introduction,imported_group, db_name) values('','$surveyid','$groupname','$obj_group->introduction','$oldgid','$db_name')";
  1217. api_sql_query($sql_ginsert,__FILE__,__LINE__);
  1218. $new_gid = mysql_insert_id();
  1219. $sql_q_insert = "INSERT INTO $curr_dbname.questions (qid,gid,survey_id,qtype,caption,alignment,sortby,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_question,db_name) values('','$new_gid','$surveyid','$obj->qtype','$caption1','$obj->alignment','$sortby','$a1','$a2','$a3','$a4','$a5','$a6','$a7','$a8','$a9','$a10','$at','$ad','$r1','$r2','$r3','$r4','$r5','$r6','$r7','$r8','$r9','$r10','$qid[$i]','$db_name')";
  1220. api_sql_query($sql_q_insert,__FILE__,__LINE__);
  1221. }
  1222. }
  1223. }
  1224. return $message;
  1225. }
  1226. /*
  1227. function import_group($surveyid,$gids,$db_name,$curr_dbname)
  1228. {
  1229. $gid_arr = explode(",",$gids);
  1230. $index = count($gid_arr);
  1231. for($i=0;$i<$index;$i++)
  1232. {
  1233. $sql = "SELECT * FROM $db_name.survey_group WHERE group_id = '$gid_arr[$i]'";
  1234. $res = api_sql_query($sql,__FILE__,__LINE__);
  1235. $obj = mysql_fetch_object($res);
  1236. $sql_ques = "SELECT * FROM $db_name.questions WHERE gid = '$gid_arr[$i]'";
  1237. $res_ques = api_sql_query($sql_ques,__FILE__,__LINE__);
  1238. $obj_ques = mysql_fetch_object($res_ques);
  1239. $sql_check = "SELECT * FROM $curr_dbname.survey_group WHERE survey_id = '$surveyid' AND imported_group = '$gid_arr[$i]' AND db_name = '$db_name'";
  1240. $res_check = api_sql_query($sql_check);
  1241. $obj_check = mysql_fetch_object($res_check);
  1242. $num = mysql_num_rows($res_check);
  1243. if($num>0)
  1244. {
  1245. $sql_question = "SELECT * FROM $curr_dbname.questions WHERE survey_id='$surveyid' AND imported_question = '$obj_ques->qid' AND db_name = '$db_name'";
  1246. $res_question = api_sql_query($sql_question,__FILE__,__LINE__);
  1247. $num_ques = mysql_num_rows($res_question);
  1248. if($num_ques>0)
  1249. {
  1250. $message=1;
  1251. }
  1252. else
  1253. {
  1254. $sql_insert_ques = "INSERT INTO $curr_dbname.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_question,db_name) values('','$obj_check->group_id','$surveyid','$obj_ques->qtype','$obj_ques->caption','$obj_ques->a1','$obj_ques->a2','$obj_ques->a3','$obj_ques->a4','$obj_ques->a5','$obj_ques->a6','$obj_ques->a7','$obj_ques->a8','$obj_ques->a9','$obj_ques->a10','$obj_ques->at','$obj_ques->ad','$obj_ques->r1','$obj_ques->r2','$obj_ques->r3','$obj_ques->r4','$obj_ques->r5','$obj_ques->r6','$obj_ques->r7','$obj_ques->r8','$obj_ques->r9','$obj_ques->r10','$obj_ques->qid','$db_name')";
  1255. api_sql_query($sql_insert_ques);
  1256. }
  1257. }
  1258. else
  1259. {
  1260. $insert_group = "INSERT INTO $curr_dbname.survey_group (group_id,survey_id,groupname,introduction,imported_group,db_name) values('','$surveyid','$obj->groupname','$obj->introduction','$obj->group_id','$db_name')";
  1261. $res_insert_group=api_sql_query($insert_group);
  1262. $new_gid = mysql_insert_id();
  1263. $sql_insert_grp = "INSERT INTO $curr_dbname.questions (qid,gid,qtype,caption,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,at,ad,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,imported_question,db_name) values('','$new_gid','$surveyid','$obj_ques->qtype','$obj_ques->caption','$obj_ques->a1','$obj_ques->a2','$obj_ques->a3','$obj_ques->a4','$obj_ques->a5','$obj_ques->a6','$obj_ques->a7','$obj_ques->a8','$obj_ques->a9','$obj_ques->a10','$obj_ques->at','$obj_ques->ad','$obj_ques->r1','$obj_ques->r2','$obj_ques->r3','$obj_ques->r4','$obj_ques->r5','$obj_ques->r6','$obj_ques->r7','$obj_ques->r8','$obj_ques->r9','$obj_ques->r10','$obj_ques->qid','$db_name')";
  1264. api_sql_query($sql_insert_grp);
  1265. }
  1266. }
  1267. return $message;
  1268. }
  1269. */
  1270. function import_group($sid,$gids,$db_name,$curr_dbname)
  1271. {
  1272. $gid_arr = explode(",",$gids);
  1273. $index = count($gid_arr);
  1274. for($i=0;$i<$index;$i++)
  1275. {
  1276. $sql = "SELECT * FROM $db_name.survey_group WHERE group_id = '$gid_arr[$i]'";
  1277. $res = api_sql_query($sql);
  1278. $obj = mysql_fetch_object($res);
  1279. $groupname=addslashes($obj->groupname);
  1280. $introduction=addslashes($obj->introduction);
  1281. $g_sortby = intval($obj->sortby);
  1282. $sql_curr = "SELECT * FROM $curr_dbname.survey_group WHERE survey_id = '$sid' AND groupname = '$obj->groupname'";
  1283. $res_curr = api_sql_query($sql_curr);
  1284. $obj_curr = mysql_fetch_object($res_curr);
  1285. $gid = $obj_curr->group_id;
  1286. $num = mysql_num_rows($res_curr);
  1287. if($num>0) //the group name exists and the questions will be imported in this group.
  1288. {
  1289. $sql_ques = "SELECT * FROM $curr_dbname.questions WHERE gid = '$gid'";
  1290. $res_ques = api_sql_query($sql_ques);
  1291. $obj_ques = mysql_fetch_object($res_ques);
  1292. $count = mysql_num_rows($res_ques);
  1293. for($j=0;$j<$count;$j++)
  1294. {
  1295. $check_qid[] = mysql_result($res_ques,$j,"imported_question");
  1296. $check_db[] = mysql_result($res_ques,$j,"db_name");
  1297. }
  1298. $check_qid = @array_unique($check_qid);
  1299. $check_db = @array_unique($check_db);
  1300. $sql_old = "SELECT * FROM $db_name.questions WHERE gid = '$gid_arr[$i]'";
  1301. $res_old = api_sql_query($sql_old);
  1302. while($obj_old = mysql_fetch_object($res_old))
  1303. {
  1304. $caption1=addslashes($obj_old->caption);
  1305. $a1=addslashes($obj_old->a1);
  1306. $a2=addslashes($obj_old->a2);
  1307. $a3=addslashes($obj_old->a3);
  1308. $a4=addslashes($obj_old->a4);
  1309. $a5=addslashes($obj_old->a5);
  1310. $a6=addslashes($obj_old->a6);
  1311. $a7=addslashes($obj_old->a7);
  1312. $a8=addslashes($obj_old->a8);
  1313. $a9=addslashes($obj_old->a9);
  1314. $a10=addslashes($obj_old->a10);
  1315. $at=addslashes($obj_old->at);
  1316. $ad=addslashes($obj_old->ad);
  1317. $r1=addslashes($obj_old->r1);
  1318. $r2=addslashes($obj_old->r2);
  1319. $r3=addslashes($obj_old->r3);
  1320. $r4=addslashes($obj_old->r4);
  1321. $r5=addslashes($obj_old->r5);
  1322. $r6=addslashes($obj_old->r6);
  1323. $r7=addslashes($obj_old->r7);
  1324. $r8=addslashes($obj_old->r8);
  1325. $r9=addslashes($obj_old->r9);
  1326. $r10=addslashes($obj_old->r10);
  1327. $sql_sort = "SELECT max(sortby) AS sortby FROM $curr_dbname.questions ";
  1328. $res_sort=api_sql_query($sql_sort);
  1329. $rs=mysql_fetch_object($res_sort);
  1330. $sortby=$rs->sortby;
  1331. if(empty($sortby))
  1332. {$sortby=1;}
  1333. else{$sortby=$sortby+1;}
  1334. if(@in_array($obj_old->qid,$check_qid)&&@in_array($db_name,$check_db)) //the question has already been imported
  1335. {
  1336. $flag=1;
  1337. continue;
  1338. }
  1339. else
  1340. {
  1341. $sql_insertq = "INSERT INTO $curr_dbname.questions (qid, gid, survey_id, qtype, caption, alignment, sortby, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, at, ad, alt_text, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, imported_question, db_name) VALUES('', '$gid', '$sid', '$obj_old->qtype', '$caption1', '$obj_old->alignment', '$sortby', '$a1', '$a2', '$a3', '$a4', '$a5', '$a6', '$a7', '$a8', '$a9', '$a10', '$at', '$ad', '$alt_text', '$r1', '$r2', '$r3', '$r4', '$r5', '$r6', '$r7', '$r8', '$r9', '$r10', '$obj_old->qid', '$db_name')";
  1342. api_sql_query($sql_insertq);
  1343. }
  1344. }
  1345. }
  1346. else //the groupname does not exist. Create group with this name and insert questions in this new group.
  1347. {
  1348. $sql_insertg = "INSERT INTO $curr_dbname.survey_group (group_id, survey_id, groupname, introduction, imported_group, db_name, sortby) VALUES ('', '$sid', '$groupname', '$introduction', '$obj->group_id', '$db_name', $g_sortby)";
  1349. api_sql_query($sql_insertg);
  1350. $group_id = mysql_insert_id();
  1351. $sql_old = "SELECT * FROM $db_name.questions WHERE gid = '$gid_arr[$i]'";
  1352. $res_old = api_sql_query($sql_old);
  1353. while($obj_old = mysql_fetch_object($res_old))
  1354. {
  1355. $caption1=addslashes($obj_old->caption);
  1356. $a1=addslashes($obj_old->a1);
  1357. $a2=addslashes($obj_old->a2);
  1358. $a3=addslashes($obj_old->a3);
  1359. $a4=addslashes($obj_old->a4);
  1360. $a5=addslashes($obj_old->a5);
  1361. $a6=addslashes($obj_old->a6);
  1362. $a7=addslashes($obj_old->a7);
  1363. $a8=addslashes($obj_old->a8);
  1364. $a9=addslashes($obj_old->a9);
  1365. $a10=addslashes($obj_old->a10);
  1366. $at=addslashes($obj_old->at);
  1367. $ad=addslashes($obj_old->ad);
  1368. $r1=addslashes($obj_old->r1);
  1369. $r2=addslashes($obj_old->r2);
  1370. $r3=addslashes($obj_old->r3);
  1371. $r4=addslashes($obj_old->r4);
  1372. $r5=addslashes($obj_old->r5);
  1373. $r6=addslashes($obj_old->r6);
  1374. $r7=addslashes($obj_old->r7);
  1375. $r8=addslashes($obj_old->r8);
  1376. $r9=addslashes($obj_old->r9);
  1377. $r10=addslashes($obj_old->r10);
  1378. $sql_sort = "SELECT max(sortby) AS sortby FROM $curr_dbname.questions ";
  1379. $res_sort=api_sql_query($sql_sort);
  1380. $rs=mysql_fetch_object($res_sort);
  1381. $sortby=$rs->sortby;
  1382. if(empty($sortby))
  1383. {$sortby=1;}
  1384. else{$sortby=$sortby+1;}
  1385. $sql_insertq = "INSERT INTO $curr_dbname.questions (qid, gid, survey_id, qtype, caption, alignment, sortby, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, at, ad, alt_text, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, imported_question, db_name) VALUES('', '$group_id', '$sid', '$obj_old->qtype', '$caption1', '$obj_old->alignment', '$sortby', '$a1', '$a2', '$a3', '$a4', '$a5', '$a6', '$a7', '$a8', '$a9', '$a10', '$at', '$ad', '$obj_old->alt_text', '$r1', '$r2', '$r3', '$r4', '$r5', '$r6', '$r7', '$r8', '$r9', '$r10', '$obj_old->qid', '$db_name')";
  1386. api_sql_query($sql_insertq);
  1387. }
  1388. }
  1389. }
  1390. return ($flag);
  1391. }
  1392. /**
  1393. * Enter description here...
  1394. *
  1395. * @return unknown
  1396. * @todo remove this function because this is of no use and should not be used.
  1397. */
  1398. function get_status()
  1399. {
  1400. global $_user;
  1401. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1402. $sqlm = "SELECT status FROM $table_user WHERE user_id = '".mysql_real_escape_string($_user['user_id'])."'";
  1403. $resm = api_sql_query($sqlm,__FILE__,__LINE__);
  1404. $objm=@mysql_fetch_object($resm);
  1405. $ss = $objm->status ;
  1406. return $ss;
  1407. }
  1408. function move_question($direction,$qid,$sort,$curr_dbname)
  1409. {
  1410. $questions=SurveyManager::get_questions_move($curr_dbname);
  1411. foreach ($questions as $key=>$value)
  1412. {
  1413. if ($qid==$value['qid'])
  1414. {
  1415. $source_course=$value;
  1416. if ($direction=="up")
  1417. {
  1418. $target_course=$questions[$key-1];
  1419. }
  1420. else
  1421. {$target_course=$questions[$key+1];}
  1422. }
  1423. }
  1424. $sql_update1="UPDATE $curr_dbname.questions SET sortby='".$target_course['sortby']."' WHERE qid='".$source_course['qid']."'";
  1425. $sql_update2="UPDATE $curr_dbname.questions SET sortby='".$source_course['sortby']."' WHERE qid='".$target_course['qid']."'";
  1426. mysql_query($sql_update2);
  1427. mysql_query($sql_update1);
  1428. //return ;
  1429. }
  1430. function get_questions_move($curr_dbname)
  1431. {
  1432. $sql_select_questions="SELECT * from $curr_dbname.questions order by `sortby` asc";
  1433. $result=mysql_query($sql_select_questions);
  1434. while ($row=mysql_fetch_array($result))
  1435. {
  1436. // we only need the database name of the course
  1437. $question1[]=array("caption"=> $row['caption'], "qid" => $row['qid'],"sortby" => $row['sortby']);
  1438. }
  1439. return $question1;
  1440. }
  1441. /**
  1442. * Displays a sortable table for the surveys
  1443. *
  1444. * @param unknown_type $groupid
  1445. * @param unknown_type $surveyid
  1446. * @param unknown_type $curr_dbname
  1447. * @param unknown_type $header
  1448. * @param unknown_type $content
  1449. * @param unknown_type $sorting_options
  1450. * @param unknown_type $paging_options
  1451. * @param unknown_type $query_vars
  1452. *
  1453. * @todo check if this function is needed as it seems that this is a copy of the Display::display_sortable_table function.
  1454. */
  1455. function display_sortable_table($groupid,$surveyid,$curr_dbname,$header, $content, $sorting_options = array (), $paging_options = array (), $query_vars = null)
  1456. {
  1457. global $origin;
  1458. // Database table definitions
  1459. /** @todo use database constants for the survey tables */
  1460. $table_survey = Database :: get_course_table('survey');
  1461. $table_group = Database :: get_course_table('survey_group');
  1462. $table_question = Database :: get_course_table('questions');
  1463. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1464. $table_survey_group = Database :: get_course_table('survey_group');
  1465. require_once ('tablesort.lib.php');
  1466. if (!isset ($paging_options['per_page_default']))
  1467. {
  1468. $paging_options['per_page_default'] = 10;
  1469. }
  1470. if (!isset ($paging_options['page_nr']))
  1471. {
  1472. $paging_options['page_nr'] = (isset ($_GET['page_nr']) ? $_GET['page_nr'] : 1);
  1473. }
  1474. if (!isset ($paging_options['per_page']))
  1475. {
  1476. $paging_options['per_page'] = (isset ($_GET['per_page']) ? $_GET['per_page'] : $paging_options['per_page_default']);
  1477. }
  1478. if (!isset ($sorting_options['column']))
  1479. {
  1480. $sorting_options['column'] = (isset ($_GET['column']) ? $_GET['column'] : 0);
  1481. }
  1482. if (!isset ($sorting_options['direction']))
  1483. {
  1484. $sorting_options['direction'] = (isset ($_GET['direction']) ? $_GET['direction'] : SORT_ASC);
  1485. }
  1486. // Build the query_string
  1487. if (is_array($query_vars))
  1488. {
  1489. foreach ($query_vars as $key => $value)
  1490. {
  1491. $query_string .= '&amp;'.urlencode($key).'='.urlencode($value);
  1492. }
  1493. }
  1494. $content = SurveyManager :: sort_table($content, $sorting_options['column'],'');
  1495. // Get data for selected page
  1496. $page_nav = '';
  1497. $page_content = array ();
  1498. $pages = array_chunk($content, intval($paging_options['per_page']));
  1499. if( $paging_options['page_nr'] > count($pages))
  1500. {
  1501. $paging_options['page_nr'] = count($pages);
  1502. }
  1503. $page_content = $pages[$paging_options['page_nr'] - 1];
  1504. // Build navigation bar
  1505. if (count($pages) > 1)
  1506. {
  1507. $page_nav = get_lang('Page').' : ';
  1508. if ($paging_options['page_nr'] > 1)
  1509. {
  1510. $page_nav .= '<a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr='. ($paging_options['page_nr'] - 1).'&amp;per_page='.$paging_options['per_page'].''.$query_string.'">&laquo;</a> ';
  1511. }
  1512. for ($i = $paging_options['page_nr'] - 3; $i < $paging_options['page_nr']; $i ++)
  1513. {
  1514. if ($i > 0)
  1515. {
  1516. $page_nav .= '<a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr='.$i.'&amp;per_page='.$paging_options['per_page'].''.$query_string.'">'.$i.'</a> ';
  1517. }
  1518. }
  1519. if ($i == $paging_options['page_nr'])
  1520. {
  1521. $page_nav .= '<b>'.$i.'</b> ';
  1522. }
  1523. for ($i = $paging_options['page_nr'] + 1; $i < $paging_options['page_nr'] + 4; $i ++)
  1524. {
  1525. if ($i <= count($pages))
  1526. {
  1527. $page_nav .= '<a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr='.$i.'&amp;per_page='.$paging_options['per_page'].''.$query_string.'">'.$i.'</a> ';
  1528. }
  1529. }
  1530. if ($paging_options['page_nr'] < count($pages))
  1531. {
  1532. $page_nav .= '<a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr='. ($paging_options['page_nr'] + 1).'&amp;per_page='.$paging_options['per_page'].''.$query_string.'">&raquo;</a> ';
  1533. }
  1534. }
  1535. $view_switch = '';
  1536. if (count($pages) == 1 && count($page_content) > $paging_options['per_page_default'])
  1537. {
  1538. $view_switch = ' <a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr=1&amp;per_page='.$paging_options['per_page_default'].''.$query_string.'">'.get_lang('Show').' '.$paging_options['per_page_default'].'</a>';
  1539. }
  1540. elseif (count($pages) > 1)
  1541. {
  1542. $view_switch = ' <a href="'.api_get_self().'?origin='.$origin.'&amp;column='.$sorting_options['column'].'&amp;direction='.$sorting_options['direction'].'&amp;page_nr=1&amp;per_page='.count($content).''.$query_string.'">'.get_lang('ShowAll').'</a>';
  1543. }
  1544. $page_nav = '<table width="100%"><tr><td>'.$view_switch.'</td><td align="right">'.$page_nav.'</td></tr></table>';
  1545. // Determine new direction
  1546. $new_direction = ($sorting_options['direction'] == SORT_ASC ? SORT_DESC : SORT_ASC);
  1547. echo "\n";
  1548. echo $page_nav;
  1549. // Show the table
  1550. echo '<table class="data_table" width="100%">';
  1551. echo "\n";
  1552. echo '<tr>';
  1553. foreach ($header as $key => $value)
  1554. {
  1555. echo '<th '.$value[2].'>';
  1556. if ($value[1])
  1557. {
  1558. echo $value[0];
  1559. if ($sorting_options['column'] == $key)
  1560. {
  1561. echo $sorting_options['direction'] == SORT_ASC ? '&nbsp;&#8595; ' : '&nbsp;&#8593; ';
  1562. }
  1563. }
  1564. else
  1565. {
  1566. echo $value[0];
  1567. }
  1568. echo '</th>';
  1569. }
  1570. echo '</tr>';
  1571. echo "\n";
  1572. if( is_array($page_content))
  1573. {
  1574. $x=0;
  1575. $y=count($page_content);
  1576. $page_nr=$paging_options['page_nr'];
  1577. $per_page=$paging_options['per_page'];
  1578. $sql="SELECT * FROM $table_question WHERE survey_id = '$surveyid'";
  1579. $res1=api_sql_query($sql,__FILE__,__LINE__);
  1580. $num1=mysql_num_rows($res1);
  1581. $number_q=ceil($num1/10);
  1582. $sql_gr="SELECT * FROM $table_question WHERE gid='$groupid' AND survey_id = '$surveyid'";
  1583. $result_gr=api_sql_query($sql_gr,__FILE__,__LINE__);
  1584. $num_gr=mysql_num_rows($result_gr);
  1585. $questions=mysql_fetch_array($result_gr);
  1586. foreach ($page_content as $row => $data)
  1587. {
  1588. echo '<tr class="'. ($row % 2 == 0 ? 'row_even' : 'row_odd').'">';
  1589. foreach( $data as $column => $value)
  1590. {
  1591. echo '<td '.(isset($header[$column][3])? $header[$column][3] : '' ).'>';
  1592. if($x==0 && $page_nr==1)
  1593. {
  1594. echo str_replace('<img src="../img/up.gif" border="0" title="lang_move_up">',"",$value);
  1595. }
  1596. elseif(($page_nr==$number_q) && ($x==$y-1))
  1597. {
  1598. echo str_replace('<img src="../img/down.gif" border="0" title="lang_move_down">',"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;",$value);
  1599. }
  1600. else{
  1601. echo $value;
  1602. }
  1603. echo '</td>';
  1604. }
  1605. echo '</tr>';
  1606. echo "\n";
  1607. $x++;
  1608. }
  1609. }
  1610. echo '</table>';
  1611. echo $page_nav;
  1612. }
  1613. function sort_table($data, $column = 0, $direction = SORT_ASC, $type = SORT_REGULAR)
  1614. {
  1615. switch ($type)
  1616. {
  1617. case SORT_REGULAR :
  1618. if (TableSort::is_image_column($data, $column))
  1619. {
  1620. return TableSort::sort_table($data, $column, $direction, SORT_IMAGE);
  1621. }
  1622. elseif (TableSort::is_date_column($data, $column))
  1623. {
  1624. return TableSort::sort_table($data, $column, $direction, SORT_DATE);
  1625. }
  1626. elseif (TableSort::is_numeric_column($data, $column))
  1627. {
  1628. return TableSort::sort_table($data, $column, $direction, SORT_NUMERIC);
  1629. }
  1630. return TableSort::sort_table($data, $column, $direction, SORT_STRING);
  1631. break;
  1632. case SORT_NUMERIC :
  1633. $compare_function = 'strip_tags($el1) > strip_tags($el2)';
  1634. break;
  1635. case SORT_STRING :
  1636. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1)),TableSort::orderingstring(strip_tags($el2))) > 0';
  1637. break;
  1638. case SORT_IMAGE :
  1639. $compare_function = 'strnatcmp(TableSort::orderingstring(strip_tags($el1,"<img>")),TableSort::orderingstring(strip_tags($el2,"<img>"))) > 0';
  1640. break;
  1641. case SORT_DATE :
  1642. $compare_function = 'strtotime(strip_tags($el1)) > strtotime(strip_tags($el2))';
  1643. }
  1644. $function_body = '$el1 = $a['.$column.']; $el2 = $b['.$column.']; return ('.$direction.' == SORT_ASC ? ('.$compare_function.') : !('.$compare_function.'));';
  1645. // Sort the content
  1646. usort($data, create_function('$a,$b', $function_body));
  1647. return $data;
  1648. }
  1649. function listGroups($id_survey, $fields = '*'){
  1650. $groups_table = Database :: get_course_table(TABLE_MAIN_GROUP);
  1651. $sql = 'SELECT '.$fields.' FROM '.$groups_table.'
  1652. WHERE survey_id='.$id_survey.' ORDER BY sortby';
  1653. $rs = api_sql_query($sql, __FILE__, __LINE__);
  1654. $groups = array();
  1655. while($row = mysql_fetch_array($rs)){
  1656. $groups[] = $row;
  1657. }
  1658. return $groups;
  1659. }
  1660. function listQuestions($id_survey, $fields = '*'){
  1661. $questions_table = Database :: get_course_table(TABLE_MAIN_SURVEYQUESTION);
  1662. $groups_table = Database :: get_course_table('survey_group');
  1663. $sql = 'SELECT '.$fields.'
  1664. FROM '.$questions_table.' questions
  1665. INNER JOIN '.$groups_table.' groups
  1666. ON questions.gid = groups.group_id
  1667. WHERE questions.survey_id='.$id_survey.'
  1668. ORDER BY groups.sortby, questions.sortby';
  1669. $rs = api_sql_query($sql, __FILE__, __LINE__);
  1670. $questions = array();
  1671. while($row = mysql_fetch_array($rs)){
  1672. $questions[] = $row;
  1673. }
  1674. return $questions;
  1675. }
  1676. function listAnswers($qid){
  1677. $answers_table = Database :: get_course_table('survey_report');
  1678. $sql = 'SELECT DISTINCT answer FROM '.$answers_table.'
  1679. WHERE qid='.$qid;
  1680. $rs = api_sql_query($sql, __FILE__, __LINE__);
  1681. $answers = array();
  1682. while($row = mysql_fetch_array($rs)){
  1683. $answers[] = $row;
  1684. }
  1685. return $answers;
  1686. }
  1687. function listUsers($survey_id, $dbname, $fields='id, user_id, firstname, lastname, email, organization') {
  1688. $tbl_survey_users = Database :: get_main_table(TABLE_MAIN_SURVEY_USER);
  1689. $sql = 'SELECT '.$fields.' FROM '.$tbl_survey_users.'
  1690. WHERE survey_id='.$survey_id.'
  1691. AND db_name="'.$dbname.'"
  1692. ORDER BY lastname, firstname ';
  1693. $rs = api_sql_query($sql, __FILE__, __LINE__);
  1694. $users = array();
  1695. while($row = mysql_fetch_array($rs))
  1696. $users[] = $row;
  1697. return $users;
  1698. }
  1699. function getUserAnswersDetails($id_userAnswers, $params=''){
  1700. $table_answers = Database :: get_main_table(TABLE_MAIN_SURVEY_USER);
  1701. $sql = 'SELECT * FROM '.$table_answers.' '.$where.' '.$order;
  1702. $rs = api_sql_query($sql, __FILE__, __LINE__);
  1703. $answers = array();
  1704. while($row = mysql_fetch_array($rs))
  1705. $answers[] = $row;
  1706. return $answers;
  1707. }
  1708. }
  1709. /**
  1710. *
  1711. * Manage the "versioning" of a conditional survey
  1712. *
  1713. * */
  1714. class SurveyTree
  1715. {
  1716. var $surveylist;
  1717. var $plainsurveylist;
  1718. var $numbersurveys;
  1719. /**
  1720. * Sets the surveylist and the plainsurveylist
  1721. */
  1722. function __construct()
  1723. {
  1724. // Database table definitions
  1725. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  1726. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  1727. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1728. // searching
  1729. $search_restriction = SurveyUtil::survey_search_restriction();
  1730. if ($search_restriction)
  1731. {
  1732. $search_restriction = ' AND '.$search_restriction;
  1733. }
  1734. $sql = "SELECT survey.survey_id , survey.parent_id, survey_version, survey.code as name
  1735. FROM $table_survey survey
  1736. LEFT JOIN $table_survey_question survey_question
  1737. ON survey.survey_id = survey_question.survey_id , $table_user user
  1738. WHERE survey.author = user.user_id
  1739. GROUP BY survey.survey_id";
  1740. $res = api_sql_query($sql, __FILE__, __LINE__);
  1741. $surveys_parents = array ();
  1742. $refs = array();
  1743. $list = array();
  1744. $last=array();
  1745. $plain_array=array();
  1746. while ($survey = Database::fetch_array($res,'ASSOC'))
  1747. {
  1748. $plain_array[$survey['survey_id']]=$survey;
  1749. $surveys_parents[]=$survey['survey_version'];
  1750. $thisref = &$refs[ $survey['survey_id'] ];
  1751. $thisref['parent_id'] = $survey['parent_id'];
  1752. $thisref['name'] = $survey['name'];
  1753. $thisref['id'] = $survey['survey_id'];
  1754. $thisref['survey_version'] = $survey['survey_version'];
  1755. if ($survey['parent_id'] == 0)
  1756. {
  1757. $list[ $survey['survey_id'] ] = &$thisref;
  1758. }
  1759. else
  1760. {
  1761. $refs[ $survey['parent_id'] ]['children'][ $survey['survey_id'] ] = &$thisref;
  1762. }
  1763. }
  1764. $this->surveylist = $list;
  1765. $this->plainsurveylist = $plain_array;
  1766. }
  1767. /*
  1768. function read_children($space=array(),$i=0)
  1769. {
  1770. foreach ( $this->surveylist as $key=>$node)
  1771. {
  1772. if (is_array($node['children']))
  1773. {
  1774. if (($node['parent_id']==0))
  1775. {
  1776. //echo '1<br>';
  1777. }
  1778. else
  1779. {
  1780. $space[]='2>';
  1781. }
  1782. //if have children
  1783. for ($j=0;$j<count($space);$j++)
  1784. {
  1785. //echo $space[$j]; echo 'dd<br>';
  1786. }
  1787. //echo $node['name']; echo '3<br>';
  1788. read_children($node['children'],$space,$i);
  1789. }
  1790. else
  1791. {
  1792. for ($j=0;$j<count($space);$j++)
  1793. {
  1794. echo $space[$j]; echo '4<br>';
  1795. }
  1796. echo $node['name']; echo '5<br>';
  1797. }
  1798. }
  1799. }
  1800. */
  1801. /*
  1802. if (($key==$id))
  1803. {
  1804. echo $node['name']; echo '<br>';
  1805. //$node['children']=array();
  1806. return $node['children'];
  1807. }
  1808. else
  1809. {
  1810. if (is_array($node['children']))
  1811. {
  1812. return SurveyTree::get_children($node['children'],$id);
  1813. }
  1814. else
  1815. {
  1816. //return SurveyTree::get_children($node, $id);
  1817. }
  1818. }
  1819. */
  1820. /**
  1821. * This function gets all the children of a given survey id
  1822. *
  1823. * @param array the list where we are going to search the children
  1824. * @param id the id of the survey
  1825. * @return array the children of a given survey id
  1826. *
  1827. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  1828. * @version September 2008
  1829. */
  1830. function get_children($list,$id)
  1831. {
  1832. $result=array();
  1833. foreach ($list as $key=>$node)
  1834. {
  1835. if ($key==$id)
  1836. {
  1837. $result = $node['children'];
  1838. break;
  1839. }
  1840. if (is_array($node['children']))
  1841. {
  1842. //echo $key; echo '--<br>';
  1843. $re=SurveyTree::get_children($node['children'],$id);
  1844. if (!empty($re))
  1845. {
  1846. $result=$re;
  1847. }
  1848. }
  1849. else
  1850. {
  1851. //echo $key; echo '-<br>';
  1852. }
  1853. }
  1854. return $result;
  1855. }
  1856. /**
  1857. * This function gets the parent id of a survey
  1858. *
  1859. * @param int survey id
  1860. * @return int survey parent id
  1861. *
  1862. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  1863. * @version September 2008
  1864. */
  1865. function getParentId($id)
  1866. {
  1867. $node = $this->plainsurveylist[$id];
  1868. if (is_array($node)&& !empty($node['parent_id']))
  1869. return $node['parent_id'];
  1870. else
  1871. return -1;
  1872. }
  1873. /**
  1874. * This function gets all the siblings of a given survey id
  1875. *
  1876. * @param array the list where we are going to search the children
  1877. * @param id the id of the survey
  1878. * @return array the children of a given survey id
  1879. *
  1880. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  1881. * @version September 2008
  1882. */
  1883. function nextSibling($id)
  1884. {
  1885. $result=array();
  1886. $parent_id = SurveyTree::getParentId($id);
  1887. $siblings = SurveyTree::get_children($this->surveylist ,$parent_id);
  1888. //print_r($siblings);
  1889. if (count($siblings) > 1)
  1890. {
  1891. // $key> $id means that the siblings are order 1 2 3 and we suppose that you can't change that order
  1892. foreach ($siblings as $key=>$bro)
  1893. {
  1894. if ($key> $id && $key != $id)
  1895. {
  1896. $result[$key]=($bro);
  1897. }
  1898. }
  1899. }
  1900. return $result;
  1901. }
  1902. /**
  1903. *
  1904. * This function shows the last sibling from a given list of surveys
  1905. * @param id of the survey
  1906. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  1907. * @version September 2008
  1908. *
  1909. */
  1910. function lastSibling($id)
  1911. {
  1912. $result=array();
  1913. $parent_id = SurveyTree::getParentId($id);
  1914. $siblings = SurveyTree::get_children($this->surveylist ,$parent_id);
  1915. //print_r($siblings);
  1916. if (count($siblings) > 1)
  1917. {
  1918. // $key> $id means that the siblings are order 1 2 3 and we suppose that you can't change that order
  1919. $i=0;
  1920. foreach ($siblings as $key=>$bro)
  1921. {
  1922. if ($key> $id && $key != $id&& $i==count($siblings)-1)
  1923. {
  1924. $result[$key]=($bro);
  1925. }
  1926. $i++;
  1927. }
  1928. }
  1929. return $result;
  1930. }
  1931. /**
  1932. *
  1933. * This function shows the last children of a branch
  1934. * @param list of nodes
  1935. * @return array of the lastest node
  1936. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  1937. * @version September 2008
  1938. *
  1939. */
  1940. function get_last_children_from_branch($list)
  1941. {
  1942. $result=array();
  1943. foreach ($list as $key=>$node)
  1944. {
  1945. //echo 'frist<br>'; echo $key;
  1946. //print_r($node);
  1947. if ($node['parent_id']!=0)
  1948. {
  1949. $list_bros = SurveyTree::lastSibling($key);
  1950. //echo ' list_bro <br>';
  1951. //print_r($list_bros);
  1952. if (is_array($list_bros) && !empty($list_bros))
  1953. {
  1954. foreach ($list_bros as $bro)
  1955. {
  1956. //echo '0';
  1957. if (is_array($bro['children']))
  1958. {
  1959. //print_r($bro['children']);
  1960. return $result[]=SurveyTree::get_last_children_from_branch($bro['children']);
  1961. }
  1962. else
  1963. {
  1964. //echo 'esl';
  1965. $result=$bro;
  1966. //print_r($bro);
  1967. return $result;
  1968. }
  1969. }
  1970. }
  1971. else
  1972. {
  1973. //SurveyTree::get_last_children_from_branch($list_bros);
  1974. //echo 'sss';
  1975. //if if (is_array($node['children']))
  1976. $children = SurveyTree::get_children($node,$key);
  1977. //return $result[]=SurveyTree::get_last_children_from_branch($node);
  1978. if (is_array($node['children']))
  1979. {
  1980. return $result[]=SurveyTree::get_last_children_from_branch($node['children']);
  1981. }
  1982. else
  1983. {
  1984. //return $result[]=SurveyTree::get_last_children_from_branch($node['children']);
  1985. return $result[]=$node;
  1986. }
  1987. }
  1988. }
  1989. else
  1990. {
  1991. //print_r($key);print_r($node['children']);
  1992. if (is_array($node['children']))
  1993. {
  1994. $result[]=SurveyTree::get_last_children_from_branch($node['children']);
  1995. }
  1996. else
  1997. {
  1998. $result[]=$node;
  1999. }
  2000. }
  2001. }
  2002. return $result;
  2003. }
  2004. /*
  2005. *
  2006. *
  2007. * This function show the last children of list
  2008. * @param id
  2009. * @param array
  2010. * @return array of the lastest node
  2011. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  2012. * @version September 2008
  2013. *
  2014. function get_last_children($id,$last)
  2015. {
  2016. foreach ( $this->_list as $key=>$node)
  2017. {
  2018. if (($node['parent_id']==$id))
  2019. {
  2020. $last=$node['name']; echo '<br>';
  2021. }
  2022. else
  2023. {
  2024. if (is_array($node['children']))
  2025. {
  2026. return $last = get_last_children($node['children'],$id);
  2027. }
  2028. }
  2029. }
  2030. return $last;
  2031. }
  2032. */
  2033. /**
  2034. * This function creates a list of all surveys id
  2035. * @param list of nodes
  2036. * @return array with the structure survey_id => survey_name
  2037. * @author Julio Montoya <gugli100@gmail.com>, Dokeos
  2038. * @version September 2008
  2039. *
  2040. */
  2041. function createList($list)
  2042. {
  2043. $result=array();
  2044. foreach ($list as $key=>$node)
  2045. {
  2046. if (is_array($node['children']))
  2047. {
  2048. //echo $key; echo '--<br>';
  2049. //print_r($node);
  2050. //echo '<br>';
  2051. $result[$key]= $node['name'];
  2052. $re=SurveyTree::createList($node['children']);
  2053. if (!empty($re))
  2054. {
  2055. if (is_array($re))
  2056. foreach ($re as $key=>$r)
  2057. {
  2058. $result[$key]=''.$r;
  2059. }
  2060. else
  2061. {
  2062. $result[]=$re;
  2063. }
  2064. }
  2065. }
  2066. else
  2067. {
  2068. //echo $key; echo '-<br>';
  2069. $result[$key]=$node['name'];
  2070. }
  2071. }
  2072. return $result;
  2073. }
  2074. }
  2075. ?>