scorm_admin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. // This file is probably deprecated - 2009-05-14 - ywarnier
  4. // No calls found in Chamilo file is deprecated. Adding an exit - 2011-06 jmontoya
  5. /**
  6. * This script handles SCO administration features
  7. * @package chamilo.learnpath.scorm
  8. * @author Denes Nagy, principal author
  9. * @author Isthvan Mandak, several new features
  10. * @author Roan Embrechts, code improvements and refactoring
  11. * @author Yannick Warnier, complete refactoring <ywarnier@beeznest.org>
  12. */
  13. /**
  14. * Code (exit)
  15. */
  16. exit;
  17. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  18. $use_anonymous = true;
  19. // Name of the language file that needs to be included.
  20. $language_file = 'scormdocument';
  21. $uncompress = 1; // TODO: This variable shouldn't be found here (find its usage before removal).
  22. require_once 'back_compat.inc.php';
  23. include 'learnpath_functions.inc.php';
  24. include_once 'scorm.lib.php';
  25. $is_allowedToEdit = api_is_allowed_to_edit();
  26. /* Variables */
  27. // Escapable integers.
  28. if ($_REQUEST['id'] != strval(intval($_REQUEST['id']))) { $id = $_REQUEST['id']; } else { $id = null; }
  29. // Imported strings.
  30. $path = (!empty($_REQUEST['path']) ? $_REQUEST['path'] : null);
  31. $Submit = (!empty($_POST['Submit']) ? $_POST['Submit'] : null);
  32. $submitImage = (!empty($_POST['submitImage']) ? $_POST['submitImage'] : null);
  33. $cancelSubmitImage = (!empty($_POST['cancelSubmitImage']) ? $_POST['cancelSubmitImage'] : null);
  34. $action = (!empty($_REQUEST['action']) ? $_REQUEST['action'] : null);
  35. $delete = (!empty($_REQUEST['delete']) ? $_REQUEST['delete'] : null);
  36. $createDir = (!empty($_REQUEST['createDir']) ? $_REQUEST['createDir'] : null);
  37. $make_directory_visible = (!empty($_REQUEST['make_directory_visible']) ? $_REQUEST['make_directory_visible'] : '');
  38. $make_directory_invisible = (!empty($_REQUEST['make_directory_invisible']) ? $_REQUEST['make_directory_invisible'] : '');
  39. // Values from POST form to add directory.
  40. $newDirPath = (!empty($_POST['newDirPath']) ? $_POST['newDirPath'] : null);
  41. $newDirName = (!empty($_POST['newDirName']) ? $_POST['newDirName'] : null);
  42. // Initialising internal variables.
  43. $dialogbox = '';
  44. $course_id = api_get_course_int_id();
  45. api_protect_course_script();
  46. //if (! $is_allowed_in_course) api_not_allowed();
  47. $is_allowedToUnzip = $is_courseAdmin;
  48. /* Main code */
  49. switch ($action) {
  50. case 'exportpath':
  51. if (!empty($id)) {
  52. $export = exportpath($id);
  53. $dialogBox .= "This LP has been exported to the Document folder "
  54. ."of your course.";
  55. }
  56. break;
  57. case 'exportscorm':
  58. exportSCORM($path);
  59. break;
  60. case 'deletepath':
  61. /*
  62. DELETE A DOKEOS LEARNPATH
  63. and all the items in it
  64. */
  65. if (!empty($id)){
  66. $l="learnpath/learnpath_handler.php?learnpath_id=$id";
  67. $sql="DELETE FROM $tbl_tool where (link='$l' AND image='scormbuilder.gif')";
  68. $result=Database::query($sql);
  69. $sql="SELECT * FROM $tbl_learnpath_chapter where learnpath_id=$id";
  70. $result=Database::query($sql);
  71. while ($row=Database::fetch_array($result))
  72. {
  73. $c=$row['id'];
  74. $sql2="DELETE FROM $tbl_learnpath_item where chapter_id=$c";
  75. $result2=Database::query($sql2);
  76. }
  77. $sql="DELETE FROM $tbl_learnpath_chapter where learnpath_id=$id";
  78. $result=Database::query($sql);
  79. deletepath($id);
  80. $dialogBox=get_lang('_learnpath_deleted');
  81. }
  82. break;
  83. case 'publishpath':
  84. /* PUBLISHING (SHOWING) A DOKEOS LEARNPATH */
  85. if (!empty($id)){
  86. $sql = "SELECT * FROM $tbl_learnpath_main where learnpath_id=$id";
  87. $result = Database::query($sql);
  88. $row = Database::fetch_array($result);
  89. $name = domesticate($row['learnpath_name']);
  90. if ($set_visibility == 'i') {
  91. $s = $name.' '.get_lang('_no_published');
  92. $dialogBox = $s;
  93. $v = 0;
  94. }
  95. if ($set_visibility == 'v') {
  96. $s=$name.' '.get_lang('_published');
  97. $dialogBox = $s;
  98. $v = 1;
  99. }
  100. $sql = "SELECT * FROM $tbl_tool where (name='$name' and image='scormbuilder.gif')";
  101. $result = Database::query($sql);
  102. $row2 = Database::fetch_array($result);
  103. $num = Database::num_rows($result);
  104. if (($set_visibility == 'i') && ($num > 0)) {
  105. // It is visible or hidden but once was published.
  106. if (($row2['visibility']) == 1) {
  107. $sql = "DELETE FROM $tbl_tool WHERE (name='$name' and image='scormbuilder.gif' AND c_id = $course_id )";
  108. } else {
  109. $sql = "UPDATE $tbl_tool set visibility=1 WHERE (name='$name' and image='scormbuilder.gif' AND c_id = $course_id)";
  110. }
  111. } elseif (($set_visibility == 'v') && ($num == 0)) {
  112. $sql ="INSERT INTO $tbl_tool (c_id, id, name, link, image, visibility, admin, address, added_tool) VALUES
  113. ($course_id, '$theid','$name','learnpath/learnpath_handler.php?learnpath_id=$id','scormbuilder.gif','$v','0','pastillegris.gif',0)";
  114. } else {
  115. // Parameter and database incompatible, do nothing.
  116. }
  117. $result = Database::query($sql);
  118. }
  119. break;
  120. case 'editpath':
  121. /* EDITING A DOKEOS NEW LEARNPATH */
  122. if (!empty($Submit)) {
  123. $l = "learnpath/learnpath_handler.php?learnpath_id=$id";
  124. $sql = "UPDATE $tbl_tool set name='".domesticate($learnpath_name)."' where (link='$l' and image='scormbuilder.gif')";
  125. $result = Database::query($sql);
  126. $sql = "UPDATE $tbl_learnpath_main SET learnpath_name='".domesticate($learnpath_name)."', learnpath_description='".domesticate($learnpath_description)."' WHERE learnpath_id=$id";
  127. $result = Database::query($sql);
  128. $dialogBox = get_lang('_learnpath_edited');
  129. }
  130. break;
  131. case 'add':
  132. /* ADDING A NEW LEARNPATH : treating the form */
  133. if (!empty($Submit)) {
  134. $sql = "INSERT INTO $tbl_learnpath_main (c_id, learnpath_name, learnpath_description) VALUES ($course_id , '".domesticate($learnpath_name)."','".domesticate($learnpath_description)."')";
  135. Database::query($sql);
  136. $my_lp_id = Database::insert_id();
  137. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool) VALUES ($course_id , '".domesticate($learnpath_name)."','learnpath/learnpath_handler.php?learnpath_id=$my_lp_id','scormbuilder.gif','1','0','pastillegris.gif',0)";
  138. Database::query($sql);
  139. // Instead of displaying this info text, get the user directly to the learnpath edit page.
  140. //$dialogBox = get_lang('_learnpath_added');
  141. header('location:../learnpath/learnpath_handler.php?'.api_get_cidreq().'&learnpath_id='.$my_lp_id);
  142. exit();
  143. }
  144. break;
  145. case 'editscorm':
  146. /* EDITING A SCORM PACKAGE */
  147. if (!empty($Submit)) {
  148. $sql = "UPDATE $tbl_document SET comment='".domesticate($learnpath_description)."', name='".domesticate($learnpath_name)."' WHERE path='$path' AND c_id = $course_id ";
  149. $result = Database::query($sql);
  150. $dialogBox = get_lang('_learnpath_edited');
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. if ($is_allowedToEdit) { // TEACHER ONLY
  157. /* UPLOAD SCORM */
  158. /*
  159. * Check the request method instead of a variable from POST
  160. * because if the file size exceeds the maximum file upload
  161. * size set in php.ini, all variables from POST are cleared !
  162. */
  163. if ($_SERVER['REQUEST_METHOD'] == 'POST' && count($_FILES) > 0 && empty($submitImage) && empty($cancelSubmitImage) && empty($action)) {
  164. // A SCORM upload has been detected, now deal with the file...
  165. // Directory creation.
  166. $s = $_FILES['userFile']['name'];
  167. $pathInfo = pathinfo($s);
  168. // Check the filename has at least several letters in it :-)
  169. // This is a very loose check as later on we might accept other formats of packages.
  170. // Sent than just "zip".
  171. if (preg_match('/[\w-_]+/', $pathInfo['basename'])) {
  172. // get the basename without extension.
  173. $newDirName = substr($pathInfo['basename'], 0, strlen($pathInfo['basename']) - (strlen($pathInfo['extension']) + 1));
  174. $newDirName = replace_dangerous_char(trim($newDirName), 'strict');
  175. if (check_name_exist($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName)) {
  176. /** @todo change this output. Inaccurate at least in french. In this case, the
  177. * file might not exist or the transfer might have been wrong (no $_FILES at all)
  178. * but we still get the error message
  179. */
  180. $dialogBox = get_lang('FileExists');
  181. $createDir = $newDirPath; unset($newDirPath); // Return to step 1.
  182. } else {
  183. if (mkdir($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName, api_get_permissions_for_new_directories())) {
  184. FileManager::set_default_settings($newDirPath.$openDir, $newDirName, 'folder', $tbl_document);
  185. // RH: was: set_default_settings($newDirPath.$openDir, $newDirName, 'folder');
  186. $dialogBox = get_lang('DirCr');
  187. } else {
  188. //Display msg "could not create dir..."
  189. //exit();
  190. }
  191. // Directory creation end.
  192. $uploadPath = $openDir.'/'.$newDirName;
  193. if (!$_FILES['userFile']['size']) {
  194. $dialogBox .= get_lang('FileError').'<br />'.get_lang('Notice').' : '.get_lang('MaxFileSize').' '.ini_get('upload_max_filesize');
  195. } else { // The file size is alright, we can assume the file is OK too.
  196. if ($uncompress == 1 && $is_allowedToUnzip) {
  197. $unzip = 'unzip';
  198. } else {
  199. $unzip = '';
  200. }
  201. if (treat_uploaded_file($_FILES['userFile'], $baseWorkDir, $uploadPath, $maxFilledSpace, $unzip)) {
  202. if ($uncompress == 1) {
  203. //$dialogBox .= get_lang('DownloadAndZipEnd');
  204. // Modified by darkden : I omitted this part, so the user can see
  205. // the scorm content message at once.
  206. } else {
  207. $dialogBox = get_lang('DownloadEnd');
  208. }
  209. // "WHAT'S NEW" notification: update table last_tooledit.
  210. //update_last_tooledit($_course, $nameTools, $id, get_lang('_new_document'), $_user['user_id']);
  211. item_property_update($_course, TOOL_LEARNPATH, $id, "LearnpathAdded", $_user['user_id']);
  212. } else {
  213. if (api_failure::get_last_failure() == 'not_enough_space') {
  214. $dialogBox = get_lang('NoSpace');
  215. } elseif (api_failure::get_last_failure() == 'php_file_in_zip_file') {
  216. $dialogBox = get_lang('ZipNoPhp');
  217. } elseif (api_failure::get_last_failure() == 'not_scorm_content') {
  218. $dialogBox = get_lang('NotScormContent');
  219. }
  220. }
  221. }
  222. $uploadPath = '';
  223. if (api_failure::get_last_failure()) {
  224. rmdir($baseWorkDir.$newDirPath.$openDir.'/'.$newDirName);
  225. }
  226. }
  227. } else { // The filename doesn't contain any alphanum chars (empty filename?)
  228. // Get a more detailed message?
  229. $dialogBox .= get_lang('FileError').'<br />';
  230. }
  231. /* DELETE FILE OR DIRECTORY */
  232. if (isset($delete)) {
  233. if ( scorm_delete($baseWorkDir.$delete)) {
  234. //$tbl_document = substr($tbl_document, 1, strlen($tbl_document) - 2); // RH...
  235. update_db_info('delete', $delete);
  236. $dialogBox = get_lang('DocDeleted');
  237. }
  238. }
  239. /* CREATE DIRECTORY */
  240. /*
  241. * The code begin with STEP 2 so it allows to return to STEP 1 if STEP 2 unsucceds.
  242. */
  243. /* STEP 2 */
  244. if (isset($newDirPath) && isset($newDirName)) {
  245. // echo $newDirPath . $newDirName;
  246. $newDirName = replace_dangerous_char(trim(stripslashes($newDirName)), 'strict');
  247. if (check_name_exist($baseWorkDir.$newDirPath.'/'.$newDirName)) {
  248. $dialogBox = get_lang('FileExists');
  249. $createDir = $newDirPath; unset($newDirPath);// return to step 1
  250. } else {
  251. if (mkdir($baseWorkDir.$newDirPath.'/'.$newDirName, api_get_permissions_for_new_directories()))
  252. FileManager::set_default_settings($newDirPath, $newDirName, 'folder', $tbl_document);
  253. // RH: was: set_default_settings($newDirPath, $newDirName, 'folder');
  254. $dialogBox = get_lang('DirCr');
  255. }
  256. }
  257. /* STEP 1 */
  258. if (isset($createDir)) {
  259. $dialogBox .= "<!-- create dir -->\n"
  260. ."<form name='createdir' action='' method='POST'>\n"
  261. ."<input type=\"hidden\" name=\"newDirPath\" value=\"$createDir\" />\n"
  262. .get_lang('NameDir')." : \n"
  263. ."<input type=\"text\" name=\"newDirName\" />\n"
  264. ."<input type=\"submit\" value=\"".get_lang('Ok')."\" />\n"
  265. ."</form>\n";
  266. }
  267. /* VISIBILITY COMMANDS */
  268. if (!empty($make_directory_visible) || !empty($make_directory_invisible)) {
  269. $visibilityPath = $make_directory_visible.$make_directory_invisible;
  270. // At least one of these variables are empty. So it's okay to proceed this way
  271. /* Check if there is yet a record for this file in the DB */
  272. $result = Database::query ("SELECT * FROM $tbl_document WHERE path LIKE '".$visibilityPath."' ");
  273. while($row = Database::fetch_array($result, 'ASSOC')) {
  274. $attribute['path' ] = $row['path' ];
  275. $attribute['visibility'] = $row['visibility'];
  276. $attribute['comment' ] = $row['comment' ];
  277. }
  278. if ($make_directory_visible) {
  279. $newVisibilityStatus = 'v';
  280. } elseif ($make_directory_invisible) {
  281. $newVisibilityStatus = 'i';
  282. }
  283. $query = "UPDATE $tbl_document SET visibility='$newVisibilityStatus' WHERE path=\"".$visibilityPath."\" AND c_id = $course_id "; // Added by Toon.
  284. Database::query($query);
  285. if (Database::affected_rows() == 0) { // Extra check added by Toon, normally not necessary anymore because all files are in the db.
  286. Database::query("INSERT INTO $tbl_document SET
  287. c_id = $course_id,
  288. path=\"".$visibilityPath."\",
  289. visibility=\"".$newVisibilityStatus."\"");
  290. }
  291. unset($attribute);
  292. $dialogBox = get_lang('ViMod');
  293. }
  294. } // END is Allowed to edit;.
  295. }