CourseSelectForm.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'Course.class.php';
  4. /**
  5. * Class to show a form to select resources
  6. * @author Bart Mollet <bart.mollet@hogent.be>
  7. * @author Julio Montoya <gugli100@gmail.com>
  8. * @package chamilo.backup
  9. */
  10. class CourseSelectForm
  11. {
  12. /**
  13. * Display the form
  14. * @param array $hidden_fiels Hidden fields to add to the form.
  15. * @param boolean the document array will be serialize. This is used in the course_copy.php file
  16. */
  17. function display_form($course, $hidden_fields = null, $avoid_serialize=false) {
  18. global $charset;
  19. $resource_titles[RESOURCE_EVENT] = get_lang('Events');
  20. $resource_titles[RESOURCE_ANNOUNCEMENT] = get_lang('Announcements');
  21. $resource_titles[RESOURCE_DOCUMENT] = get_lang('Documents');
  22. $resource_titles[RESOURCE_LINK] = get_lang('Links');
  23. $resource_titles[RESOURCE_COURSEDESCRIPTION] = get_lang('CourseDescription');
  24. $resource_titles[RESOURCE_FORUM] = get_lang('Forums');
  25. $resource_titles[RESOURCE_QUIZ] = get_lang('Tests');
  26. $resource_titles[RESOURCE_LEARNPATH] = get_lang('Learnpaths');
  27. $resource_titles[RESOURCE_SCORM] = 'SCORM';
  28. $resource_titles[RESOURCE_TOOL_INTRO] = get_lang('ToolIntro');
  29. $resource_titles[RESOURCE_SURVEY] = get_lang('Survey');
  30. $resource_titles[RESOURCE_GLOSSARY] = get_lang('Glossary');
  31. $resource_titles[RESOURCE_WIKI] = get_lang('Wiki');
  32. $resource_titles[RESOURCE_THEMATIC] = get_lang('Thematic');
  33. $resource_titles[RESOURCE_ATTENDANCE] = get_lang('Attendance');
  34. ?>
  35. <script type="text/javascript">
  36. function exp(item) {
  37. el = document.getElementById('div_'+item);
  38. if (el.style.display=='none'){
  39. el.style.display='';
  40. document.getElementById('img_'+item).src='../img/1.gif';
  41. }
  42. else{
  43. el.style.display='none';
  44. document.getElementById('img_'+item).src='../img/0.gif';
  45. }
  46. }
  47. function setCheckbox(type,value) {
  48. d = document.course_select_form;
  49. for (i = 0; i < d.elements.length; i++) {
  50. if (d.elements[i].type == "checkbox") {
  51. var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
  52. if( name.indexOf(type) > 0 || type == 'all' ){
  53. d.elements[i].checked = value;
  54. }
  55. }
  56. }
  57. }
  58. function checkLearnPath(message){
  59. d = document.course_select_form;
  60. for (i = 0; i < d.elements.length; i++) {
  61. if (d.elements[i].type == "checkbox") {
  62. var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
  63. if( name.indexOf('learnpath') > 0){
  64. if(d.elements[i].checked){
  65. setCheckbox('document',true);
  66. alert(message);
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. </script>
  74. <?php
  75. //get destination course title
  76. if (!empty($hidden_fields['destination_course'])) {
  77. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  78. $course_infos = CourseManager::get_course_information($hidden_fields['destination_course']);
  79. echo '<h3>';
  80. echo get_lang('DestinationCourse').' : '.$course_infos['title'];
  81. echo '</h3>';
  82. }
  83. echo '<p>';
  84. echo get_lang('SelectResources');
  85. echo '</p>';
  86. echo '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>';
  87. echo '<script type="text/javascript">var myUpload = new upload(1000);</script>';
  88. echo '<form method="post" id="upload_form" name="course_select_form" onsubmit="javascript: myUpload.start(\'dynamic_div\',\''.api_get_path(WEB_CODE_PATH).'img/progress_bar.gif\',\''.get_lang('PleaseStandBy', '').'\',\'upload_form\')">';
  89. echo '<input type="hidden" name="action" value="course_select_form"/>';
  90. if (!empty($hidden_fields['destination_course']) && !empty($hidden_fields['origin_course']) && !empty($hidden_fields['destination_session']) && !empty($hidden_fields['origin_session']) ) {
  91. echo '<input type="hidden" name="destination_course" value="'.$hidden_fields['destination_course'].'"/>';
  92. echo '<input type="hidden" name="origin_course" value="'.$hidden_fields['origin_course'].'"/>';
  93. echo '<input type="hidden" name="destination_session" value="'.$hidden_fields['destination_session'].'"/>';
  94. echo '<input type="hidden" name="origin_session" value="'.$hidden_fields['origin_session'].'"/>';
  95. }
  96. $element_count = 0;
  97. foreach ($course->resources as $type => $resources) {
  98. if (count($resources) > 0) {
  99. switch ($type) {
  100. //Resources to avoid
  101. case RESOURCE_LINKCATEGORY :
  102. case RESOURCE_FORUMCATEGORY :
  103. case RESOURCE_FORUMPOST :
  104. case RESOURCE_FORUMTOPIC :
  105. case RESOURCE_QUIZQUESTION:
  106. case RESOURCE_SURVEYQUESTION:
  107. case RESOURCE_SURVEYINVITATION:
  108. case RESOURCE_SCORM:
  109. break;
  110. default :
  111. echo '<img id="img_'.$type.'" src="../img/1.gif" onclick="javascript:exp('."'$type'".');" />&nbsp;';
  112. echo '<b onclick="javascript:exp('."'$type'".');" >'.$resource_titles[$type].'</b><br />';
  113. echo '<div id="div_'.$type.'">';
  114. if ($type == RESOURCE_LEARNPATH) {
  115. Display::display_warning_message(get_lang('ToExportLearnpathWithQuizYouHaveToSelectQuiz'));
  116. Display::display_warning_message(get_lang('IfYourLPsHaveAudioFilesIncludedYouShouldSelectThemFromTheDocuments'));
  117. }
  118. if ($type == RESOURCE_DOCUMENT) {
  119. if (api_get_setting('show_glossary_in_documents') != 'none') {
  120. Display::display_warning_message(get_lang('ToExportDocumentsWithGlossaryYouHaveToSelectGlossary'));
  121. }
  122. }
  123. echo '<blockquote>';
  124. echo "[<a href=\"javascript: void(0);\" onclick=\"javascript: setCheckbox('$type',true);\" >".get_lang('All')."</a> | <a href=\"javascript: void(0);\" onclick=\"javascript:setCheckbox('$type',false);\" >".get_lang('None')."</a>]";
  125. echo '<br />';
  126. foreach ($resources as $id => $resource) {
  127. echo ' <label class="checkbox">';
  128. echo '<input type="checkbox" name="resource['.$type.']['.$id.']" id="resource['.$type.']['.$id.']" />';
  129. $resource->show();
  130. echo '</label>';
  131. }
  132. echo '</blockquote>';
  133. echo '</div>';
  134. echo '<script language="javascript">exp('."'$type'".')</script>';
  135. $element_count++;
  136. }
  137. }
  138. }
  139. if ($avoid_serialize) {
  140. /*Documents are avoided due the huge amount of memory that the serialize php function "eats"
  141. (when there are directories with hundred/thousand of files) */
  142. // this is a known issue of serialize
  143. $course->resources['document']= null;
  144. }
  145. echo '<input type="hidden" name="course" value="'.base64_encode(serialize($course)).'"/>';
  146. if (is_array($hidden_fields)) {
  147. foreach ($hidden_fields as $key => $value) {
  148. echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  149. }
  150. }
  151. if (empty($element_count)) {
  152. Display::display_warning_message(get_lang('NoDataAvailable'));
  153. } else {
  154. if (!empty($hidden_fields['destination_session'])) {
  155. echo '<br /><button class="save" type="submit" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES, $charset))."'".')) return false;" >'.get_lang('Ok').'</button>';
  156. } else {
  157. echo '<br /><button class="save" type="submit" onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.get_lang('Ok').'</button>';
  158. }
  159. }
  160. CourseSelectForm :: display_hidden_quiz_questions($course);
  161. CourseSelectForm :: display_hidden_scorm_directories($course);
  162. echo '</form>';
  163. echo '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
  164. }
  165. function display_hidden_quiz_questions($course) {
  166. if(is_array($course->resources)){
  167. foreach ($course->resources as $type => $resources) {
  168. if (count($resources) > 0) {
  169. switch ($type) {
  170. case RESOURCE_QUIZQUESTION:
  171. foreach ($resources as $id => $resource) {
  172. echo '<input type="hidden" name="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" id="resource['.RESOURCE_QUIZQUESTION.']['.$id.']" value="On" />';
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. function display_hidden_scorm_directories($course) {
  181. if(is_array($course->resources)){
  182. foreach ($course->resources as $type => $resources) {
  183. if(count($resources) > 0) {
  184. switch($type) {
  185. case RESOURCE_SCORM:
  186. foreach ($resources as $id=>$resource) {
  187. echo '<input type="hidden" name="resource['.RESOURCE_SCORM.']['.$id.']" id="resource['.RESOURCE_SCORM.']['.$id.']" value="On" />';
  188. }
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /**
  196. * Get the posted course
  197. * @param string who calls the function? It can be copy_course, create_backup, import_backup or recycle_course
  198. * @return course The course-object with all resources selected by the user
  199. * in the form given by display_form(...)
  200. */
  201. function get_posted_course($from='', $session_id = 0, $course_code = '') {
  202. $course = unserialize(base64_decode($_POST['course']));
  203. //Create the resource DOCUMENT objects
  204. //Loading the results from the checkboxes of the javascript
  205. $resource = $_POST['resource'][RESOURCE_DOCUMENT];
  206. $course_info = api_get_course_info($course_code);
  207. $table_doc = Database::get_course_table(TABLE_DOCUMENT);
  208. $table_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  209. $course_id = $course_info['real_id'];
  210. // Searching the documents resource that have been set to null because $avoid_serialize is true in the display_form() function
  211. if ($from == 'copy_course') {
  212. if (is_array($resource)) {
  213. $resource = array_keys($resource);
  214. foreach ($resource as $resource_item) {
  215. $condition_session = '';
  216. if (!empty($session_id)) {
  217. $session_id = intval($session_id);
  218. $condition_session = ' AND d.session_id ='.$session_id;
  219. }
  220. $sql = 'SELECT d.id, d.path, d.comment, d.title, d.filetype, d.size
  221. FROM '.$table_doc.' d, '.$table_prop.' p
  222. WHERE d.c_id = '.$course_id.' AND
  223. p.c_id = '.$course_id.' AND
  224. tool = \''.TOOL_DOCUMENT.'\' AND
  225. p.ref = d.id AND p.visibility != 2 AND
  226. d.id = '.$resource_item.$condition_session.'
  227. ORDER BY path';
  228. $db_result = Database::query($sql);
  229. while ($obj = Database::fetch_object($db_result)) {
  230. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  231. $course->add_resource($doc);
  232. // adding item property
  233. $sql = "SELECT * FROM $table_prop WHERE c_id = '.$course_id.' AND TOOL = '".RESOURCE_DOCUMENT."' AND ref='".$resource_item."'";
  234. $res = Database::query($sql);
  235. $all_properties = array ();
  236. while ($item_property = Database::fetch_array($res,'ASSOC')) {
  237. $all_properties[] = $item_property;
  238. }
  239. $course->resources[RESOURCE_DOCUMENT][$resource_item]->item_properties = $all_properties;
  240. }
  241. }
  242. }
  243. }
  244. /*else {
  245. $documents = $_POST['resource'][RESOURCE_DOCUMENT];
  246. //print_r($course->resources );
  247. foreach ($resource as $resource_item) {
  248. echo $resource_item;
  249. foreach($documents as $obj) {
  250. print_r($obj);
  251. if ($obj->id==$resource_item) {
  252. $doc = new Document($obj->id, $obj->path, $obj->comment, $obj->title, $obj->filetype, $obj->size);
  253. print_r($doc);
  254. $course->add_resource($doc);
  255. }
  256. }
  257. }
  258. }*/
  259. if (is_array($course->resources)) {
  260. foreach ($course->resources as $type => $resources) {
  261. switch ($type) {
  262. case RESOURCE_SURVEYQUESTION:
  263. foreach($resources as $id => $obj) {
  264. if(is_array($_POST['resource'][RESOURCE_SURVEY]) && !in_array($obj->survey_id,array_keys($_POST['resource'][RESOURCE_SURVEY]))) {
  265. unset ($course->resources[$type][$id]);
  266. }
  267. }
  268. break;
  269. case RESOURCE_LINKCATEGORY :
  270. case RESOURCE_FORUMCATEGORY :
  271. case RESOURCE_FORUMPOST :
  272. case RESOURCE_FORUMTOPIC :
  273. case RESOURCE_QUIZQUESTION :
  274. case RESOURCE_DOCUMENT:
  275. // Mark folders to import which are not selected by the user to import,
  276. // but in which a document was selected.
  277. $documents = $_POST['resource'][RESOURCE_DOCUMENT];
  278. if (is_array($resources))
  279. foreach($resources as $id => $obj) {
  280. if( $obj->file_type == 'folder' && ! isset($_POST['resource'][RESOURCE_DOCUMENT][$id]) && is_array($documents)) {
  281. foreach($documents as $id_to_check => $post_value) {
  282. $obj_to_check = $resources[$id_to_check];
  283. $shared_path_part = substr($obj_to_check->path,0,strlen($obj->path));
  284. if($id_to_check != $id && $obj->path == $shared_path_part) {
  285. $_POST['resource'][RESOURCE_DOCUMENT][$id] = 1;
  286. break;
  287. }
  288. }
  289. }
  290. }
  291. default :
  292. if (is_array($resources)) {
  293. foreach ($resources as $id => $obj) {
  294. $resource_is_used_elsewhere = $course->is_linked_resource($obj);
  295. // check if document is in a quiz (audio/video)
  296. if( $type == RESOURCE_DOCUMENT && $course->has_resources(RESOURCE_QUIZ))
  297. {
  298. foreach($course->resources[RESOURCE_QUIZ] as $qid => $quiz)
  299. {
  300. if($quiz->media == $id)
  301. {
  302. $resource_is_used_elsewhere = true;
  303. }
  304. }
  305. }
  306. if (!isset ($_POST['resource'][$type][$id]) && !$resource_is_used_elsewhere)
  307. {
  308. unset ($course->resources[$type][$id]);
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }
  315. return $course;
  316. }
  317. /**
  318. * Display the form session export
  319. * @param array $hidden_fiels Hidden fields to add to the form.
  320. * @param boolean the document array will be serialize. This is used in the course_copy.php file
  321. */
  322. function display_form_session_export($list_course, $hidden_fields = null, $avoid_serialize=false) {
  323. ?>
  324. <script type="text/javascript">
  325. function exp(item) {
  326. el = document.getElementById('div_'+item);
  327. if (el.style.display=='none'){
  328. el.style.display='';
  329. document.getElementById('img_'+item).src='../img/1.gif';
  330. }
  331. else{
  332. el.style.display='none';
  333. document.getElementById('img_'+item).src='../img/0.gif';
  334. }
  335. }
  336. function setCheckbox(type,value) {
  337. d = document.course_select_form;
  338. for (i = 0; i < d.elements.length; i++) {
  339. if (d.elements[i].type == "checkbox") {
  340. var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
  341. if( name.indexOf(type) > 0 || type == 'all' ){
  342. d.elements[i].checked = value;
  343. }
  344. }
  345. }
  346. }
  347. function checkLearnPath(message){
  348. d = document.course_select_form;
  349. for (i = 0; i < d.elements.length; i++) {
  350. if (d.elements[i].type == "checkbox") {
  351. var name = d.elements[i].attributes.getNamedItem('name').nodeValue;
  352. if( name.indexOf('learnpath') > 0){
  353. if(d.elements[i].checked){
  354. setCheckbox('document',true);
  355. alert(message);
  356. break;
  357. }
  358. }
  359. }
  360. }
  361. }
  362. </script>
  363. <?php
  364. //get destination course title
  365. if(!empty($hidden_fields['destination_course'])) {
  366. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  367. $course_infos = CourseManager::get_course_information($hidden_fields['destination_course']);
  368. echo '<h3>';
  369. echo get_lang('DestinationCourse').' : '.$course_infos['title'];
  370. echo '</h3>';
  371. }
  372. echo '<script src="'.api_get_path(WEB_CODE_PATH).'inc/lib/javascript/upload.js" type="text/javascript"></script>';
  373. echo '<script type="text/javascript">var myUpload = new upload(1000);</script>';
  374. echo '<form method="post" id="upload_form" name="course_select_form" onsubmit="myUpload.start(\'dynamic_div\',\''.api_get_path(WEB_CODE_PATH).'img/progress_bar.gif\',\''.get_lang('PleaseStandBy').'\',\'upload_form\')">';
  375. echo '<input type="hidden" name="action" value="course_select_form"/>';
  376. foreach($list_course as $course){
  377. foreach ($course->resources as $type => $resources) {
  378. if (count($resources) > 0) {
  379. echo '<img id="img_'.$course->code.'" src="../img/1.gif" onclick="javascript:exp('."'$course->code'".');" />';
  380. echo '<b onclick="javascript:exp('."'$course->code'".');" > '.$course->code.'</b><br />';
  381. echo '<div id="div_'.$course->code.'">';
  382. echo '<blockquote>';
  383. echo "[<a href=\"#\" onclick=\"javascript:setCheckbox('".$course->code."',true);\" >".get_lang('All')."</a> - <a href=\"#\" onclick=\"javascript:setCheckbox('".$course->code."',false);\" >".get_lang('None')."</a>]";
  384. echo '<br />';
  385. foreach ($resources as $id => $resource) {
  386. echo '<input type="checkbox" name="resource['.$course->code.']['.$id.']" id="resource['.$course->code.']['.$id.']"/>';
  387. echo ' <label for="resource['.$course->code.']['.$id.']">';
  388. $resource->show();
  389. echo '</label>';
  390. echo '<br />';
  391. echo "\n";
  392. }
  393. echo '</blockquote>';
  394. echo '</div>';
  395. echo '<script type="text/javascript">exp('."'$course->code'".')</script>';
  396. }
  397. }
  398. }
  399. if ($avoid_serialize) {
  400. //Documents are avoided due the huge amount of memory that the serialize php function "eats" (when there are directories with hundred/thousand of files)
  401. // this is a known issue of serialize
  402. $course->resources['document']= null;
  403. }
  404. echo '<input type="hidden" name="course" value="'.base64_encode(serialize($course)).'"/>';
  405. if (is_array($hidden_fields)) {
  406. foreach ($hidden_fields as $key => $value) {
  407. echo "\n";
  408. echo '<input type="hidden" name="'.$key.'" value="'.$value.'"/>';
  409. }
  410. }
  411. echo '<br /><button class="save" type="submit" onclick="checkLearnPath(\''.addslashes(get_lang('DocumentsWillBeAddedToo')).'\')">'.get_lang('Ok').'</button>';
  412. CourseSelectForm :: display_hidden_quiz_questions($course);
  413. CourseSelectForm :: display_hidden_scorm_directories($course);
  414. echo '</form>';
  415. echo '<div id="dynamic_div" style="display:block;margin-left:40%;margin-top:10px;height:50px;"></div>';
  416. }
  417. }
  418. ?>