add_course.lib.inc.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class AddCourse
  5. */
  6. class AddCourse
  7. {
  8. /**
  9. * Defines the four needed keys to create a course based on several parameters.
  10. * @param string The code you want for this course
  11. * @param string Prefix added for ALL keys
  12. * @param string Prefix added for databases only
  13. * @param string Prefix added for paths only
  14. * @param bool Add unique prefix
  15. * @param bool Use code-independent keys
  16. * @return array An array with the needed keys ['currentCourseCode'], ['currentCourseId'], ['currentCourseDbName'], ['currentCourseRepository']
  17. * @todo Eliminate the global variables.
  18. * @assert (null) === false
  19. */
  20. public static function define_course_keys(
  21. $wanted_code,
  22. $prefix_for_all = '',
  23. $prefix_for_base_name = '',
  24. $prefix_for_path = '',
  25. $add_unique_prefix = false,
  26. $use_code_indepedent_keys = true
  27. ) {
  28. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  29. $wanted_code = CourseManager::generate_course_code($wanted_code);
  30. $keys_course_code = $wanted_code;
  31. if (!$use_code_indepedent_keys) {
  32. $wanted_code = '';
  33. }
  34. if ($add_unique_prefix) {
  35. $unique_prefix = substr(md5(uniqid(rand())), 0, 10);
  36. } else {
  37. $unique_prefix = '';
  38. }
  39. $keys = array();
  40. $final_suffix = array('CourseId' => '', 'CourseDb' => '', 'CourseDir' => '');
  41. $limit_numb_try = 100;
  42. $keys_are_unique = false;
  43. $try_new_fsc_id = $try_new_fsc_db = $try_new_fsc_dir = 0;
  44. while (!$keys_are_unique) {
  45. $keys_course_id = $prefix_for_all . $unique_prefix . $wanted_code . $final_suffix['CourseId'];
  46. $keys_course_repository = $prefix_for_path . $unique_prefix . $wanted_code . $final_suffix['CourseDir'];
  47. $keys_are_unique = true;
  48. // Check whether they are unique.
  49. $query = "SELECT 1 FROM $course_table
  50. WHERE code='".$keys_course_id."'
  51. LIMIT 0, 1";
  52. $result = Database::query($query);
  53. if (Database::num_rows($result)) {
  54. $keys_are_unique = false;
  55. $try_new_fsc_id ++;
  56. $final_suffix['CourseId'] = substr(md5(uniqid(rand())), 0, 4);
  57. }
  58. if (file_exists(api_get_path(SYS_COURSE_PATH).$keys_course_repository)) {
  59. $keys_are_unique = false;
  60. $try_new_fsc_dir ++;
  61. $final_suffix['CourseDir'] = substr(md5(uniqid(rand())), 0, 4);
  62. }
  63. if (($try_new_fsc_id + $try_new_fsc_db + $try_new_fsc_dir) > $limit_numb_try) {
  64. return $keys;
  65. }
  66. }
  67. $keys['currentCourseCode'] = $keys_course_code;
  68. $keys['currentCourseId'] = $keys_course_id;
  69. $keys['currentCourseRepository'] = $keys_course_repository;
  70. return $keys;
  71. }
  72. /**
  73. * Initializes a file repository for a newly created course.
  74. * @param string Course repository
  75. * @param string Course code
  76. * @return int 0
  77. * @assert (null,null) === false
  78. */
  79. public static function prepare_course_repository($course_repository, $course_code)
  80. {
  81. $perm = api_get_permissions_for_new_directories();
  82. $perm_file = api_get_permissions_for_new_files();
  83. $htmlpage = "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <title>Not authorized</title>\n </head>\n <body>\n </body>\n</html>";
  84. $cp = api_get_path(SYS_COURSE_PATH) . $course_repository;
  85. //Creating document folder
  86. mkdir($cp, $perm);
  87. mkdir($cp . '/document', $perm);
  88. $cpt = $cp . '/document/index.html';
  89. $fd = fopen($cpt, 'w');
  90. fwrite($fd, $htmlpage);
  91. fclose($fd);
  92. /*
  93. @chmod($cpt, $perm_file);
  94. @copy($cpt, $cp . '/document/index.html');
  95. mkdir($cp . '/document/images', $perm);
  96. @copy($cpt, $cp . '/document/images/index.html');
  97. mkdir($cp . '/document/images/gallery/', $perm);
  98. @copy($cpt, $cp . '/document/images/gallery/index.html');
  99. mkdir($cp . '/document/shared_folder/', $perm);
  100. @copy($cpt, $cp . '/document/shared_folder/index.html');
  101. mkdir($cp . '/document/audio', $perm);
  102. @copy($cpt, $cp . '/document/audio/index.html');
  103. mkdir($cp . '/document/flash', $perm);
  104. @copy($cpt, $cp . '/document/flash/index.html');
  105. mkdir($cp . '/document/video', $perm);
  106. @copy($cpt, $cp . '/document/video/index.html'); */
  107. //Creatind dropbox folder
  108. mkdir($cp . '/dropbox', $perm);
  109. $cpt = $cp . '/dropbox/index.html';
  110. $fd = fopen($cpt, 'w');
  111. fwrite($fd, $htmlpage);
  112. fclose($fd);
  113. @chmod($cpt, $perm_file);
  114. mkdir($cp . '/group', $perm);
  115. @copy($cpt, $cp . '/group/index.html');
  116. mkdir($cp . '/page', $perm);
  117. @copy($cpt, $cp . '/page/index.html');
  118. mkdir($cp . '/scorm', $perm);
  119. @copy($cpt, $cp . '/scorm/index.html');
  120. mkdir($cp . '/upload', $perm);
  121. @copy($cpt, $cp . '/upload/index.html');
  122. mkdir($cp . '/upload/forum', $perm);
  123. @copy($cpt, $cp . '/upload/forum/index.html');
  124. mkdir($cp . '/upload/forum/images', $perm);
  125. @copy($cpt, $cp . '/upload/forum/images/index.html');
  126. mkdir($cp . '/upload/test', $perm);
  127. @copy($cpt, $cp . '/upload/test/index.html');
  128. mkdir($cp . '/upload/blog', $perm);
  129. @copy($cpt, $cp . '/upload/blog/index.html');
  130. mkdir($cp . '/upload/learning_path', $perm);
  131. @copy($cpt, $cp . '/upload/learning_path/index.html');
  132. mkdir($cp . '/upload/learning_path/images', $perm);
  133. @copy($cpt, $cp . '/upload/learning_path/images/index.html');
  134. mkdir($cp . '/upload/calendar', $perm);
  135. @copy($cpt, $cp . '/upload/calendar/index.html');
  136. mkdir($cp . '/upload/calendar/images', $perm);
  137. @copy($cpt, $cp . '/upload/calendar/images/index.html');
  138. mkdir($cp . '/work', $perm);
  139. @copy($cpt, $cp . '/work/index.html');
  140. mkdir($cp . '/upload/announcements', $perm);
  141. @copy($cpt, $cp . '/upload/announcements/index.html');
  142. mkdir($cp . '/upload/announcements/images', $perm);
  143. @copy($cpt, $cp . '/upload/announcements/images/index.html');
  144. //Oral expression question type
  145. mkdir($cp . '/exercises', $perm);
  146. @copy($cpt, $cp . '/exercises/index.html');
  147. // Create .htaccess in the dropbox directory.
  148. $fp = fopen($cp . '/dropbox/.htaccess', 'w');
  149. fwrite(
  150. $fp,
  151. "AuthName AllowLocalAccess
  152. AuthType Basic
  153. order deny,allow
  154. deny from all
  155. php_flag zlib.output_compression off"
  156. );
  157. fclose($fp);
  158. // Build index.php of the course.
  159. /*$fd = fopen($cp . '/index.php', 'w');
  160. // str_replace() removes \r that cause squares to appear at the end of each line
  161. //@todo fix the harcoded include
  162. $string = str_replace(
  163. "\r",
  164. "",
  165. "<?" . "php
  166. \$cidReq = \"$course_code\";
  167. \$dbname = \"$course_code\";
  168. include(\"" . api_get_path(SYS_CODE_PATH) . "course_home/course_home.php\");
  169. ?>"
  170. );
  171. fwrite($fd, $string);
  172. @chmod($cp . '/index.php', $perm_file);*/
  173. return 0;
  174. }
  175. /**
  176. * Gets an array with all the course tables (deprecated?)
  177. * @return string[]
  178. * @assert (null) !== null
  179. */
  180. public static function get_course_tables()
  181. {
  182. $tables = array();
  183. $tables[] = 'item_property';
  184. $tables[] = 'tool';
  185. $tables[] = 'tool_intro';
  186. $tables[] = 'group_info';
  187. $tables[] = 'group_category';
  188. $tables[] = 'group_rel_user';
  189. $tables[] = 'group_rel_tutor';
  190. $tables[] = 'userinfo_content';
  191. $tables[] = 'userinfo_def';
  192. $tables[] = 'course_description';
  193. $tables[] = 'calendar_event';
  194. $tables[] = 'calendar_event_repeat';
  195. $tables[] = 'calendar_event_repeat_not';
  196. $tables[] = 'calendar_event_attachment';
  197. $tables[] = 'announcement';
  198. $tables[] = 'announcement_attachment';
  199. $tables[] = 'resource';
  200. $tables[] = 'student_publication';
  201. $tables[] = 'student_publication_assignment';
  202. $tables[] = 'document';
  203. $tables[] = 'forum_category';
  204. $tables[] = 'forum_forum';
  205. $tables[] = 'forum_thread';
  206. $tables[] = 'forum_post';
  207. $tables[] = 'forum_mailcue';
  208. $tables[] = 'forum_attachment';
  209. $tables[] = 'forum_notification';
  210. $tables[] = 'forum_thread_qualify';
  211. $tables[] = 'forum_thread_qualify_log';
  212. $tables[] = 'link';
  213. $tables[] = 'link_category';
  214. $tables[] = 'online_connected';
  215. $tables[] = 'online_link';
  216. $tables[] = 'chat_connected';
  217. $tables[] = 'quiz';
  218. $tables[] = 'quiz_rel_question';
  219. $tables[] = 'quiz_question';
  220. $tables[] = 'quiz_answer';
  221. $tables[] = 'quiz_question_option';
  222. $tables[] = 'quiz_question_category';
  223. $tables[] = 'quiz_question_rel_category';
  224. $tables[] = 'dropbox_post';
  225. $tables[] = 'dropbox_file';
  226. $tables[] = 'dropbox_person';
  227. $tables[] = 'dropbox_category';
  228. $tables[] = 'dropbox_feedback';
  229. $tables[] = 'lp';
  230. $tables[] = 'lp_item';
  231. $tables[] = 'lp_view';
  232. $tables[] = 'lp_item_view';
  233. $tables[] = 'lp_iv_interaction';
  234. $tables[] = 'lp_iv_objective';
  235. $tables[] = 'blog';
  236. $tables[] = 'blog_comment';
  237. $tables[] = 'blog_post';
  238. $tables[] = 'blog_rating';
  239. $tables[] = 'blog_rel_user';
  240. $tables[] = 'blog_task';
  241. $tables[] = 'blog_task_rel_user';
  242. $tables[] = 'blog_attachment';
  243. $tables[] = 'permission_group';
  244. $tables[] = 'permission_user';
  245. $tables[] = 'permission_task';
  246. $tables[] = 'role';
  247. $tables[] = 'role_group';
  248. $tables[] = 'role_permissions';
  249. $tables[] = 'role_user';
  250. $tables[] = 'survey';
  251. $tables[] = 'survey_question';
  252. $tables[] = 'survey_question_option';
  253. $tables[] = 'survey_invitation';
  254. $tables[] = 'survey_answer';
  255. $tables[] = 'survey_group';
  256. $tables[] = 'wiki';
  257. $tables[] = 'wiki_conf';
  258. $tables[] = 'wiki_discuss';
  259. $tables[] = 'wiki_mailcue';
  260. $tables[] = 'course_setting';
  261. $tables[] = 'glossary';
  262. $tables[] = 'notebook';
  263. $tables[] = 'attendance';
  264. $tables[] = 'attendance_sheet';
  265. $tables[] = 'attendance_calendar';
  266. $tables[] = 'attendance_result';
  267. $tables[] = 'attendance_sheet_log';
  268. $tables[] = 'thematic';
  269. $tables[] = 'thematic_plan';
  270. $tables[] = 'thematic_advance';
  271. return $tables;
  272. }
  273. /**
  274. * Executed only before create_course_tables()
  275. * @return void
  276. * @assert (null) === null
  277. */
  278. public static function drop_course_tables()
  279. {
  280. $list = self::get_course_tables();
  281. foreach ($list as $table) {
  282. $sql = "DROP TABLE IF EXISTS " . DB_COURSE_PREFIX . $table;
  283. Database::query($sql);
  284. }
  285. }
  286. /**
  287. * Returns a list of all files in the given course directory. The requested
  288. * directory will be checked against a "checker" directory to avoid access to
  289. * protected/unauthorized files
  290. * @param string Complete path to directory we want to list
  291. * @param array A list of files to which we want to add the files found
  292. * @param string Type of base directory from which we want to recover the files
  293. * @param string $path
  294. * @param string $media
  295. * @return array
  296. * @assert (null,null,null) === false
  297. * @assert ('abc',array(),'') === array()
  298. */
  299. public static function browse_folders($path, $files, $media)
  300. {
  301. if ($media == 'images') {
  302. $code_path = api_get_path(
  303. SYS_CODE_PATH
  304. ) . 'default_course_document/images/';
  305. }
  306. if ($media == 'audio') {
  307. $code_path = api_get_path(
  308. SYS_CODE_PATH
  309. ) . 'default_course_document/audio/';
  310. }
  311. if ($media == 'flash') {
  312. $code_path = api_get_path(
  313. SYS_CODE_PATH
  314. ) . 'default_course_document/flash/';
  315. }
  316. if ($media == 'video') {
  317. $code_path = api_get_path(
  318. SYS_CODE_PATH
  319. ) . 'default_course_document/video/';
  320. }
  321. if ($media == 'certificates') {
  322. $code_path = api_get_path(
  323. SYS_CODE_PATH
  324. ) . 'default_course_document/certificates/';
  325. }
  326. if (is_dir($path)) {
  327. $handle = opendir($path);
  328. while (false !== ($file = readdir($handle))) {
  329. if (is_dir($path . $file) && strpos($file, '.') !== 0) {
  330. $files[]['dir'] = str_replace(
  331. $code_path,
  332. '',
  333. $path . $file . '/'
  334. );
  335. $files = self::browse_folders(
  336. $path . $file . '/',
  337. $files,
  338. $media
  339. );
  340. } elseif (is_file($path . $file) && strpos($file, '.') !== 0) {
  341. $files[]['file'] = str_replace(
  342. $code_path,
  343. '',
  344. $path . $file
  345. );
  346. }
  347. }
  348. }
  349. return $files;
  350. }
  351. /**
  352. * Sorts pictures by type (used?)
  353. * @param array List of files (sthg like array(0=>array('png'=>1)))
  354. * @param string File type
  355. * @param string $type
  356. * @return array The received array without files not matching type
  357. * @assert (array(),null) === array()
  358. */
  359. public static function sort_pictures($files, $type)
  360. {
  361. $pictures = array();
  362. foreach ($files as $key => $value) {
  363. if (isset($value[$type]) && $value[$type] != '') {
  364. $pictures[][$type] = $value[$type];
  365. }
  366. }
  367. return $pictures;
  368. }
  369. /**
  370. * Fills the course database with some required content and example content.
  371. * @param int Course (int) ID
  372. * @param string Course directory name (e.g. 'ABC')
  373. * @param string Language used for content (e.g. 'spanish')
  374. * @param bool Whether to fill the course with example content
  375. * @param int $authorId
  376. * @return bool False on error, true otherwise
  377. * @version 1.2
  378. * @assert (null, '', '', null) === false
  379. * @assert (1, 'ABC', null, null) === false
  380. * @assert (1, 'TEST', 'spanish', true) === true
  381. */
  382. public static function fill_db_course(
  383. $course_id,
  384. $course_repository,
  385. $language,
  386. $fill_with_exemplary_content = null,
  387. $authorId = 0
  388. ) {
  389. if (is_null($fill_with_exemplary_content)) {
  390. $fill_with_exemplary_content = api_get_setting('example_material_course_creation') != 'false';
  391. }
  392. $course_id = intval($course_id);
  393. if (empty($course_id)) {
  394. return false;
  395. }
  396. $courseInfo = api_get_course_info_by_id($course_id);
  397. $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
  398. $tbl_course_homepage = Database::get_course_table(TABLE_TOOL_LIST);
  399. $TABLEGROUPCATEGORIES = Database::get_course_table(
  400. TABLE_GROUP_CATEGORY
  401. );
  402. $TABLEITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  403. $TABLETOOLDOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  404. $TABLESETTING = Database::get_course_table(TABLE_COURSE_SETTING);
  405. $TABLEGRADEBOOK = Database::get_main_table(
  406. TABLE_MAIN_GRADEBOOK_CATEGORY
  407. );
  408. $TABLEGRADEBOOKLINK = Database::get_main_table(
  409. TABLE_MAIN_GRADEBOOK_LINK
  410. );
  411. $visible_for_all = 1;
  412. $visible_for_course_admin = 0;
  413. $visible_for_platform_admin = 2;
  414. /* Course tools */
  415. Database::query(
  416. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  417. VALUES ($course_id, 1, '" . TOOL_COURSE_DESCRIPTION . "','course_description/','info.gif','" . self::string2binary(
  418. api_get_setting(
  419. 'course_create_active_tools',
  420. 'course_description'
  421. )
  422. ) . "','0','squaregrey.gif', 0,'_self','authoring','0')"
  423. );
  424. Database::query(
  425. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  426. VALUES ($course_id, 2, '" . TOOL_CALENDAR_EVENT . "','calendar/agenda.php','agenda.gif','" . self::string2binary(
  427. api_get_setting('course_create_active_tools', 'agenda')
  428. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  429. );
  430. Database::query(
  431. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  432. VALUES ($course_id, 3, '" . TOOL_DOCUMENT . "','document/document.php','folder_document.gif','" . self::string2binary(
  433. api_get_setting('course_create_active_tools', 'documents')
  434. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  435. );
  436. Database::query(
  437. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  438. VALUES ($course_id, 4, '" . TOOL_LEARNPATH . "','lp/lp_controller.php','scorms.gif','" . self::string2binary(
  439. api_get_setting('course_create_active_tools', 'learning_path')
  440. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  441. );
  442. Database::query(
  443. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  444. VALUES ($course_id, 5, '" . TOOL_LINK . "','link/link.php','links.gif','" . self::string2binary(
  445. api_get_setting('course_create_active_tools', 'links')
  446. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  447. );
  448. Database::query(
  449. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  450. VALUES ($course_id, 6, '" . TOOL_QUIZ . "','exercise/exercise.php','quiz.gif','" . self::string2binary(
  451. api_get_setting('course_create_active_tools', 'quiz')
  452. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  453. );
  454. Database::query(
  455. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  456. VALUES ($course_id, 7, '" . TOOL_ANNOUNCEMENT . "','announcements/announcements.php','valves.gif','" . self::string2binary(
  457. api_get_setting('course_create_active_tools', 'announcements')
  458. ) . "','0','squaregrey.gif', 0,'_self','authoring','0')"
  459. );
  460. Database::query(
  461. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  462. VALUES ($course_id, 8, '" . TOOL_FORUM . "','forum/index.php','forum.gif','" . self::string2binary(
  463. api_get_setting('course_create_active_tools', 'forums')
  464. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  465. );
  466. Database::query(
  467. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  468. VALUES ($course_id, 9, '" . TOOL_DROPBOX . "','dropbox/index.php','dropbox.gif','" . self::string2binary(
  469. api_get_setting('course_create_active_tools', 'dropbox')
  470. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  471. );
  472. Database::query(
  473. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  474. VALUES ($course_id, 10, '" . TOOL_USER . "','user/user.php','members.gif','" . self::string2binary(
  475. api_get_setting('course_create_active_tools', 'users')
  476. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  477. );
  478. Database::query(
  479. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  480. VALUES ($course_id, 11, '" . TOOL_GROUP . "','group/group.php','group.gif','" . self::string2binary(
  481. api_get_setting('course_create_active_tools', 'groups')
  482. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  483. );
  484. Database::query(
  485. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  486. VALUES ($course_id, 12, '" . TOOL_CHAT . "','chat/chat.php','chat.gif','" . self::string2binary(
  487. api_get_setting('course_create_active_tools', 'chat')
  488. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  489. );
  490. Database::query(
  491. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  492. VALUES ($course_id, 13, '" . TOOL_STUDENTPUBLICATION . "','work/work.php','works.gif','" . self::string2binary(
  493. api_get_setting(
  494. 'course_create_active_tools',
  495. 'student_publications'
  496. )
  497. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  498. );
  499. Database::query(
  500. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  501. VALUES ($course_id, 14, '" . TOOL_SURVEY . "','survey/survey_list.php','survey.gif','" . self::string2binary(
  502. api_get_setting('course_create_active_tools', 'survey')
  503. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  504. );
  505. Database::query(
  506. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  507. VALUES ($course_id, 15, '" . TOOL_WIKI . "','wiki/index.php','wiki.gif','" . self::string2binary(
  508. api_get_setting('course_create_active_tools', 'wiki')
  509. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  510. );
  511. Database::query(
  512. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  513. VALUES ($course_id, 16, '" . TOOL_GRADEBOOK . "','gradebook/index.php','gradebook.gif','" . self::string2binary(
  514. api_get_setting('course_create_active_tools', 'gradebook')
  515. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  516. );
  517. Database::query(
  518. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  519. VALUES ($course_id, 17, '" . TOOL_GLOSSARY . "','glossary/index.php','glossary.gif','" . self::string2binary(
  520. api_get_setting('course_create_active_tools', 'glossary')
  521. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  522. );
  523. Database::query(
  524. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  525. VALUES ($course_id, 18, '" . TOOL_NOTEBOOK . "','notebook/index.php','notebook.gif','" . self::string2binary(
  526. api_get_setting('course_create_active_tools', 'notebook')
  527. ) . "','0','squaregrey.gif',0,'_self','interaction','0')"
  528. );
  529. $setting = intval(self::string2binary(
  530. api_get_setting('course_create_active_tools', 'attendances')
  531. ));
  532. Database::query(
  533. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  534. VALUES ($course_id, 19, '" . TOOL_ATTENDANCE . "','attendance/index.php','attendance.gif','" . $setting. "','0','squaregrey.gif',0,'_self','authoring','0')"
  535. );
  536. Database::query(
  537. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  538. VALUES ($course_id, 20, '" . TOOL_COURSE_PROGRESS . "','course_progress/index.php','course_progress.gif','" . self::string2binary(
  539. intval(api_get_setting('course_create_active_tools', 'course_progress'))
  540. ) . "','0','squaregrey.gif',0,'_self','authoring','0')"
  541. );
  542. if (api_get_setting('search_enabled') === 'true') {
  543. Database::query(
  544. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  545. VALUES ($course_id, 23, '" . TOOL_SEARCH . "','search/','info.gif','" . self::string2binary(
  546. api_get_setting(
  547. 'course_create_active_tools',
  548. 'enable_search'
  549. )
  550. ) . "','0','search.gif',0,'_self','authoring','0')"
  551. );
  552. }
  553. $sql = "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  554. VALUES ($course_id, 24,'" . TOOL_BLOGS . "','blog/blog_admin.php','blog_admin.gif','" . intval(self::string2binary(
  555. api_get_setting('course_create_active_tools', 'blogs'))
  556. ) . "','1','squaregrey.gif',0,'_self','admin','0')";
  557. Database::query($sql);
  558. /* Course homepage tools for course admin only */
  559. Database::query(
  560. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  561. VALUES ($course_id, 25, '" . TOOL_TRACKING . "','tracking/courseLog.php','statistics.gif','$visible_for_course_admin','1','', 0,'_self','admin','0')"
  562. );
  563. Database::query(
  564. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  565. VALUES ($course_id, 26, '" . TOOL_COURSE_SETTING . "','course_info/infocours.php','reference.gif','$visible_for_course_admin','1','', 0,'_self','admin','0')"
  566. );
  567. Database::query(
  568. "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
  569. VALUES ($course_id, 27, '" . TOOL_COURSE_MAINTENANCE . "','course_info/maintenance.php','backup.gif','$visible_for_course_admin','1','',0,'_self', 'admin','0')"
  570. );
  571. $alert = api_get_setting('email_alert_manager_on_new_quiz');
  572. if ($alert === 'true') {
  573. $defaultEmailExerciseAlert = 1;
  574. } else {
  575. $defaultEmailExerciseAlert = 0;
  576. }
  577. /* course_setting table (courseinfo tool) */
  578. $settings = [
  579. 'email_alert_manager_on_new_doc' => ['title' => '', 'default' => 0, 'category' => 'work'],
  580. 'email_alert_on_new_doc_dropbox' => ['default' => 0, 'category' => 'dropbox'],
  581. 'allow_user_edit_agenda' => ['default' => 0, 'category' => 'agenda'],
  582. 'allow_user_edit_announcement' => ['default' => 0, 'category' => 'announcement'],
  583. 'email_alert_manager_on_new_quiz' => ['default' => $defaultEmailExerciseAlert, 'category' => 'quiz'],
  584. 'allow_user_image_forum' => ['default' => 1, 'category' => 'forum'],
  585. 'course_theme' => ['default' => '', 'category' => 'theme'],
  586. 'allow_learning_path_theme' => ['default' => 1, 'category' => 'theme'],
  587. 'allow_open_chat_window' => ['default' => 1, 'category' => 'chat'],
  588. 'email_alert_to_teacher_on_new_user_in_course' => ['default' => 0, 'category' =>'registration'],
  589. 'allow_user_view_user_list' => ['default' =>1, 'category' =>'user'],
  590. 'display_info_advance_inside_homecourse' => ['default' => 1, 'category' =>'thematic_advance'],
  591. 'email_alert_students_on_new_homework' => ['default' => 0, 'category' =>'work'],
  592. 'enable_lp_auto_launch' => ['default' => 0, 'category' =>'learning_path'],
  593. 'pdf_export_watermark_text' => ['default' =>'', 'category' =>'learning_path'],
  594. 'allow_public_certificates' => [
  595. 'default' => api_get_setting('allow_public_certificates') === 'true' ? 1 : '',
  596. 'category' =>'certificates'
  597. ],
  598. 'documents_default_visibility' => ['default' =>'visible', 'category' =>'document'],
  599. 'show_course_in_user_language' => ['default' => 2, 'category' => null],
  600. ];
  601. $counter = 1;
  602. foreach ($settings as $variable => $setting) {
  603. $title = isset($setting['title']) ? $setting['title'] : '';
  604. Database::query(
  605. "INSERT INTO $TABLESETTING (id, c_id, title, variable, value, category)
  606. VALUES ($counter, $course_id, '".$title."', '".$variable."', '".$setting['default']."', '".$setting['category']."')"
  607. );
  608. $counter++;
  609. }
  610. /* Course homepage tools for platform admin only */
  611. /* Group tool */
  612. Database::insert(
  613. $TABLEGROUPCATEGORIES,
  614. [
  615. 'c_id' => $course_id,
  616. 'id' => 2,
  617. 'title' => get_lang('DefaultGroupCategory'),
  618. 'description' => '',
  619. 'max_student' => 8,
  620. 'self_reg_allowed' => 0,
  621. 'self_unreg_allowed' => 0,
  622. 'groups_per_user' => 0,
  623. 'display_order' => 0,
  624. 'doc_state' => 1,
  625. 'calendar_state' => 1,
  626. 'work_state' => 1,
  627. 'announcements_state' => 1,
  628. 'forum_state' => 1,
  629. 'wiki_state' => 1,
  630. 'chat_state' => 1
  631. ]
  632. );
  633. $now = api_get_utc_datetime();
  634. $files = [
  635. ['path' => '/shared_folder', 'title' => get_lang('UserFolders'), 'filetype' => 'folder', 'size' => 0],
  636. ['path' => '/chat_files', 'title' => get_lang('ChatFiles'), 'filetype' => 'folder', 'size' => 0],
  637. ];
  638. $counter = 1;
  639. foreach ($files as $file) {
  640. self::insertDocument($course_id, $counter, $file, $authorId);
  641. $counter++;
  642. }
  643. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  644. $perm = api_get_permissions_for_new_directories();
  645. $perm_file = api_get_permissions_for_new_files();
  646. $chat_path = $sys_course_path . $course_repository . '/document/chat_files';
  647. if (!is_dir($chat_path)) {
  648. @mkdir($chat_path, api_get_permissions_for_new_directories());
  649. }
  650. /* Documents */
  651. if ($fill_with_exemplary_content) {
  652. $files = [
  653. ['path' => '/images', 'title' => get_lang('Images'), 'filetype' => 'folder', 'size' => 0],
  654. ['path' => '/images/gallery', 'title' => get_lang('DefaultCourseImages'), 'filetype' => 'folder', 'size' => 0],
  655. ['path' => '/audio', 'title' => get_lang('Audio'), 'filetype' => 'folder', 'size' => 0],
  656. ['path' => '/flash', 'title' => get_lang('Flash'), 'filetype' => 'folder', 'size' => 0],
  657. ['path' => '/video', 'title' => get_lang('Video'), 'filetype' => 'folder', 'size' => 0],
  658. ['path' => '/certificates', 'title' => get_lang('Certificates'), 'filetype' => 'folder', 'size' => 0]
  659. ];
  660. foreach ($files as $file) {
  661. self::insertDocument($course_id, $counter, $file, $authorId);
  662. $counter++;
  663. }
  664. // FILL THE COURSE DOCUMENT WITH DEFAULT COURSE PICTURES
  665. $folders_to_copy_from_default_course = array(
  666. 'images',
  667. 'audio',
  668. 'flash',
  669. 'video',
  670. 'certificates',
  671. );
  672. $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document/';
  673. $default_document_array = array();
  674. foreach ($folders_to_copy_from_default_course as $folder) {
  675. $default_course_folder_path = $default_course_path . $folder . '/';
  676. $files = self::browse_folders(
  677. $default_course_folder_path,
  678. array(),
  679. $folder
  680. );
  681. $sorted_array = self::sort_pictures($files, 'dir');
  682. $sorted_array = array_merge(
  683. $sorted_array,
  684. self::sort_pictures($files, 'file')
  685. );
  686. $default_document_array[$folder] = $sorted_array;
  687. }
  688. // Light protection (adding index.html in every document folder)
  689. $htmlpage = "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <title>Not authorized</title>\n </head>\n <body>\n </body>\n</html>";
  690. $example_cert_id = 0;
  691. if (is_array($default_document_array) && count(
  692. $default_document_array
  693. ) > 0
  694. ) {
  695. foreach ($default_document_array as $media_type => $array_media) {
  696. $path_documents = "/$media_type/";
  697. //hack until feature #5242 is implemented
  698. if ($media_type == 'images') {
  699. $media_type = 'images/gallery';
  700. $images_folder = $sys_course_path . $course_repository . "/document/images/";
  701. if (!is_dir($images_folder)) {
  702. //Creating index.html
  703. mkdir($images_folder, $perm);
  704. $fd = fopen($images_folder . 'index.html', 'w');
  705. fwrite($fd, $htmlpage);
  706. @chmod($images_folder . 'index.html', $perm_file);
  707. }
  708. }
  709. $course_documents_folder = $sys_course_path . $course_repository . "/document/$media_type/";
  710. $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document' . $path_documents;
  711. if (!is_dir($course_documents_folder)) {
  712. // Creating index.html
  713. mkdir($course_documents_folder, $perm);
  714. $fd = fopen(
  715. $course_documents_folder . 'index.html',
  716. 'w'
  717. );
  718. fwrite($fd, $htmlpage);
  719. @chmod(
  720. $course_documents_folder . 'index.html',
  721. $perm_file
  722. );
  723. }
  724. if (is_array($array_media) && count($array_media) > 0) {
  725. foreach ($array_media as $key => $value) {
  726. if (isset($value['dir']) && !empty($value['dir'])) {
  727. if (!is_dir($course_documents_folder . $value['dir'])) {
  728. //Creating folder
  729. mkdir(
  730. $course_documents_folder . $value['dir'],
  731. $perm
  732. );
  733. //Creating index.html (for light protection)
  734. $index_html = $course_documents_folder . $value['dir'] . '/index.html';
  735. $fd = fopen($index_html, 'w');
  736. fwrite($fd, $htmlpage);
  737. @chmod($index_html, $perm_file);
  738. //Inserting folder in the DB
  739. $folder_path = substr(
  740. $value['dir'],
  741. 0,
  742. strlen($value['dir']) - 1
  743. );
  744. $temp = explode('/', $folder_path);
  745. $title = $temp[count($temp) - 1];
  746. //hack until feature #5242 is implemented
  747. if ($title == 'gallery') {
  748. $title = get_lang(
  749. 'DefaultCourseImages'
  750. );
  751. }
  752. if ($media_type == 'images/gallery') {
  753. $folder_path = 'gallery/' . $folder_path;
  754. }
  755. Database::query(
  756. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  757. VALUES ($course_id,'$path_documents" . $folder_path . "','" . $title . "','folder','0')"
  758. );
  759. $image_id = Database:: insert_id();
  760. Database::insert(
  761. $TABLEITEMPROPERTY,
  762. [
  763. 'c_id' => $course_id,
  764. 'tool' => 'document',
  765. 'insert_user_id' => api_get_user_id(),
  766. 'insert_date' => $now,
  767. 'lastedit_date' => $now,
  768. 'ref' => $image_id,
  769. 'lastedit_type' => 'DocumentAdded',
  770. 'lastedit_user_id' => api_get_user_id(),
  771. 'to_group_id' => null,
  772. 'to_user_id' => null,
  773. 'visibility' => 0
  774. ]
  775. );
  776. }
  777. }
  778. if (isset($value['file']) && !empty($value['file'])) {
  779. if (!file_exists(
  780. $course_documents_folder . $value['file']
  781. )
  782. ) {
  783. //Copying file
  784. copy(
  785. $default_course_path . $value['file'],
  786. $course_documents_folder . $value['file']
  787. );
  788. chmod(
  789. $course_documents_folder . $value['file'],
  790. $perm_file
  791. );
  792. //echo $default_course_path.$value['file']; echo ' - '; echo $course_documents_folder.$value['file']; echo '<br />';
  793. $temp = explode('/', $value['file']);
  794. $file_size = filesize(
  795. $course_documents_folder . $value['file']
  796. );
  797. //hack until feature #5242 is implemented
  798. if ($media_type == 'images/gallery') {
  799. $value["file"] = 'gallery/' . $value["file"];
  800. }
  801. //Inserting file in the DB
  802. Database::query(
  803. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  804. VALUES ($course_id,'$path_documents" . $value["file"] . "','" . $temp[count($temp) - 1] . "','file','$file_size')"
  805. );
  806. $image_id = Database:: insert_id();
  807. if ($image_id) {
  808. $sql = "UPDATE $TABLETOOLDOCUMENT SET id = iid WHERE iid = $image_id";
  809. Database::query($sql);
  810. if ($path_documents . $value['file'] == '/certificates/default.html') {
  811. $example_cert_id = $image_id;
  812. }
  813. $docId = Database::insert(
  814. $TABLEITEMPROPERTY,
  815. [
  816. 'c_id' => $course_id,
  817. 'tool' => 'document',
  818. 'insert_user_id' => api_get_user_id(),
  819. 'insert_date' => $now,
  820. 'lastedit_date' => $now,
  821. 'ref' => $image_id,
  822. 'lastedit_type' => 'DocumentAdded',
  823. 'lastedit_user_id' => api_get_user_id(),
  824. 'to_group_id' => null,
  825. 'to_user_id' => null,
  826. 'visibility' => 1
  827. ]
  828. );
  829. if ($docId) {
  830. $sql = "UPDATE $TABLEITEMPROPERTY SET id = iid WHERE iid = $docId";
  831. Database::query($sql);
  832. }
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. $agenda = new Agenda();
  841. $agenda->setType('course');
  842. $agenda->set_course($courseInfo);
  843. $agenda->addEvent(
  844. $now,
  845. $now,
  846. 0,
  847. get_lang('AgendaCreationTitle'),
  848. get_lang('AgendaCreationContenu')
  849. );
  850. /* Links tool */
  851. $link = new Link();
  852. $link->setCourse($courseInfo);
  853. $links = [
  854. [
  855. 'c_id' => $course_id,
  856. 'url' => 'http://www.google.com',
  857. 'title' => 'Google',
  858. 'description' => get_lang('Google') ,
  859. 'category_id' => 0,
  860. 'on_homepage' => 0,
  861. 'target' => '_self',
  862. 'session_id' => 0
  863. ],
  864. [
  865. 'c_id' => $course_id,
  866. 'url' => 'http://www.wikipedia.org',
  867. 'title' => 'Wikipedia',
  868. 'description' => get_lang('Wikipedia') ,
  869. 'category_id' => 0,
  870. 'on_homepage' => 0,
  871. 'target' => '_self',
  872. 'session_id' => 0
  873. ]
  874. ];
  875. foreach($links as $params) {
  876. $link->save($params);
  877. }
  878. /* Announcement tool */
  879. AnnouncementManager::add_announcement(
  880. get_lang('AnnouncementExampleTitle'),
  881. get_lang('AnnouncementEx'),
  882. ['everyone' => 'everyone'],
  883. null,
  884. null,
  885. $now
  886. );
  887. $manager = Database::getManager();
  888. /* Introduction text */
  889. $intro_text = '<p style="text-align: center;">
  890. <img src="' . api_get_path(REL_CODE_PATH) . 'img/mascot.png" alt="Mr. Chamilo" title="Mr. Chamilo" />
  891. <h2>' . get_lang('IntroductionText') . '</h2>
  892. </p>';
  893. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  894. $toolIntro
  895. ->setCId($course_id)
  896. ->setId(TOOL_COURSE_HOMEPAGE)
  897. ->setSessionId(0)
  898. ->setIntroText($intro_text);
  899. $manager->persist($toolIntro);
  900. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  901. $toolIntro
  902. ->setCId($course_id)
  903. ->setId(TOOL_STUDENTPUBLICATION)
  904. ->setSessionId(0)
  905. ->setIntroText(get_lang('IntroductionTwo'));
  906. $manager->persist($toolIntro);
  907. $toolIntro = new Chamilo\CourseBundle\Entity\CToolIntro();
  908. $toolIntro
  909. ->setCId($course_id)
  910. ->setId(TOOL_WIKI)
  911. ->setSessionId(0)
  912. ->setIntroText(get_lang('IntroductionWiki'));
  913. $manager->persist($toolIntro);
  914. $manager->flush();
  915. /* Exercise tool */
  916. $exercise = new Exercise($course_id);
  917. $exercise->exercise = get_lang('ExerciceEx');
  918. $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0">
  919. <tr>
  920. <td width="220" valign="top" align="left">
  921. <img src="' . api_get_path(WEB_CODE_PATH) . 'default_course_document/images/mr_chamilo/doubts.png">
  922. </td>
  923. <td valign="top" align="left">' . get_lang('Antique') . '</td></tr>
  924. </table>';
  925. $exercise->type = 1;
  926. $exercise->setRandom(0);
  927. $exercise->active = 1;
  928. $exercise->results_disabled = 0;
  929. $exercise->description = $html;
  930. $exercise->save();
  931. $exercise_id = $exercise->id;
  932. $question = new MultipleAnswer();
  933. $question->question = get_lang('SocraticIrony');
  934. $question->description = get_lang('ManyAnswers');
  935. $question->weighting = 10;
  936. $question->position = 1;
  937. $question->course = $courseInfo;
  938. $question->save($exercise_id);
  939. $questionId = $question->id;
  940. $answer = new Answer($questionId, $courseInfo['real_id']);
  941. $answer->createAnswer(get_lang('Ridiculise'), 0, get_lang('NoPsychology'), -5, 1);
  942. $answer->createAnswer(get_lang('AdmitError'), 0, get_lang('NoSeduction'), -5, 2);
  943. $answer->createAnswer(get_lang('Force'), 1, get_lang('Indeed'), 5, 3);
  944. $answer->createAnswer(get_lang('Contradiction'), 1, get_lang('NotFalse'), 5, 4);
  945. $answer->save();
  946. /* Forum tool */
  947. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  948. $params = [
  949. 'forum_category_title' => get_lang('ExampleForumCategory'),
  950. 'forum_category_comment' => ''
  951. ];
  952. $forumCategoryId = store_forumcategory($params, $courseInfo, false);
  953. $params = [
  954. 'forum_category' => $forumCategoryId,
  955. 'forum_title' => get_lang('ExampleForum'),
  956. 'forum_comment' => '',
  957. 'default_view_type_group' => ['default_view_type' => 'flat'],
  958. ];
  959. $forumId = store_forum($params, $courseInfo, true);
  960. $forumInfo = get_forum_information($forumId, $courseInfo['real_id']);
  961. $params = [
  962. 'post_title' => get_lang('ExampleThread'),
  963. 'forum_id' => $forumId,
  964. 'post_text' => get_lang('ExampleThreadContent'),
  965. 'calification_notebook_title' => '',
  966. 'numeric_calification' => '',
  967. 'weight_calification' => '',
  968. 'forum_category' => $forumCategoryId,
  969. 'thread_peer_qualify' => 0,
  970. ];
  971. store_thread($forumInfo, $params, $courseInfo, false);
  972. /* Gradebook tool */
  973. $course_code = $courseInfo['code'];
  974. // father gradebook
  975. Database::query(
  976. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  977. VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)"
  978. );
  979. $gbid = Database:: insert_id();
  980. Database::query(
  981. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  982. VALUES ('$course_code','',1,'$course_code',$gbid,100,1,75,NULL,$example_cert_id)"
  983. );
  984. $gbid = Database:: insert_id();
  985. Database::query(
  986. "INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, course_code, category_id, created_at, weight, visible, locked)
  987. VALUES (1,$exercise_id,1,'$course_code',$gbid,'$now',100,1,0)"
  988. );
  989. }
  990. //Installing plugins in course
  991. $app_plugin = new AppPlugin();
  992. $app_plugin->install_course_plugins($course_id);
  993. return true;
  994. }
  995. /**
  996. * @param int $course_id
  997. * @param int $counter
  998. * @param array $file
  999. * @param int $authorId
  1000. */
  1001. public static function insertDocument($course_id, $counter, $file, $authorId = 0)
  1002. {
  1003. $tableItem = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1004. $tableDocument = Database::get_course_table(TABLE_DOCUMENT);
  1005. $now = api_get_utc_datetime();
  1006. $sql = "INSERT INTO $tableDocument (id, c_id, path,title,filetype,size, readonly, session_id)
  1007. VALUES ($counter, $course_id, '".$file['path']."', '".$file['title']."', '".$file['filetype']."', '".$file['size']."', 0, 0)";
  1008. Database::query($sql);
  1009. $docId = Database:: insert_id();
  1010. $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
  1011. if ($docId) {
  1012. $sql = "UPDATE $tableDocument SET id = iid WHERE iid = $docId";
  1013. Database::query($sql);
  1014. $id = Database::insert(
  1015. $tableItem,
  1016. [
  1017. 'id' => $counter,
  1018. 'c_id' => $course_id,
  1019. 'tool' => 'document',
  1020. 'insert_user_id' => $authorId,
  1021. 'insert_date' => $now,
  1022. 'lastedit_date' => $now,
  1023. 'ref' => $docId,
  1024. 'lastedit_type' => 'DocumentAdded',
  1025. 'lastedit_user_id' => $authorId,
  1026. 'to_group_id' => null,
  1027. 'to_user_id' => null,
  1028. 'visibility' => 0
  1029. ]
  1030. );
  1031. if ($id) {
  1032. $sql = "UPDATE $tableItem SET id = iid WHERE iid = $id";
  1033. Database::query($sql);
  1034. }
  1035. }
  1036. }
  1037. /**
  1038. * string2binary converts the string "true" or "false" to the boolean true false (0 or 1)
  1039. * This is used for the Chamilo Config Settings as these store true or false as string
  1040. * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for
  1041. * the visibility of the tool)
  1042. * @param string $variable
  1043. * @author Patrick Cool, patrick.cool@ugent.be
  1044. * @assert ('true') === true
  1045. * @assert ('false') === false
  1046. */
  1047. public static function string2binary($variable)
  1048. {
  1049. if ($variable == 'true') {
  1050. return true;
  1051. }
  1052. if ($variable == 'false') {
  1053. return false;
  1054. }
  1055. }
  1056. /**
  1057. * Function register_course to create a record in the course table of the main database
  1058. * @param array Course details (see code for details)
  1059. * @return int Created course ID
  1060. * @todo use an array called $params instead of lots of params
  1061. * @assert (null) === false
  1062. */
  1063. public static function register_course($params)
  1064. {
  1065. global $error_msg, $firstExpirationDelay;
  1066. $title = $params['title'];
  1067. $code = $params['code'];
  1068. $visual_code = $params['visual_code'];
  1069. $directory = $params['directory'];
  1070. $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null;
  1071. $category_code = isset($params['course_category']) ? $params['course_category'] : '';
  1072. $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting(
  1073. 'platformLanguage'
  1074. );
  1075. $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']);
  1076. $department_name = isset($params['department_name']) ?
  1077. $params['department_name'] : null;
  1078. $department_url = isset($params['department_url']) ?
  1079. $params['department_url'] : null;
  1080. $disk_quota = isset($params['disk_quota']) ?
  1081. $params['disk_quota'] : null;
  1082. if (!isset($params['visibility'])) {
  1083. $default_course_visibility = api_get_setting(
  1084. 'courses_default_creation_visibility'
  1085. );
  1086. if (isset($default_course_visibility)) {
  1087. $visibility = $default_course_visibility;
  1088. } else {
  1089. $visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
  1090. }
  1091. } else {
  1092. $visibility = $params['visibility'];
  1093. }
  1094. $subscribe = isset($params['subscribe']) ? (int) $params['subscribe'] : $visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0;
  1095. $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0;
  1096. $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null;
  1097. $teachers = isset($params['teachers']) ? $params['teachers'] : null;
  1098. $status = isset($params['status']) ? $params['status'] : null;
  1099. $TABLECOURSE = Database:: get_main_table(TABLE_MAIN_COURSE);
  1100. $TABLECOURSUSER = Database:: get_main_table(TABLE_MAIN_COURSE_USER);
  1101. $ok_to_register_course = true;
  1102. // Check whether all the needed parameters are present.
  1103. if (empty($code)) {
  1104. $error_msg[] = 'courseSysCode is missing';
  1105. $ok_to_register_course = false;
  1106. }
  1107. if (empty($visual_code)) {
  1108. $error_msg[] = 'courseScreenCode is missing';
  1109. $ok_to_register_course = false;
  1110. }
  1111. if (empty($directory)) {
  1112. $error_msg[] = 'courseRepository is missing';
  1113. $ok_to_register_course = false;
  1114. }
  1115. if (empty($title)) {
  1116. $error_msg[] = 'title is missing';
  1117. $ok_to_register_course = false;
  1118. }
  1119. if (empty($expiration_date)) {
  1120. $expiration_date = api_get_utc_datetime(
  1121. time() + $firstExpirationDelay
  1122. );
  1123. } else {
  1124. $expiration_date = api_get_utc_datetime($expiration_date);
  1125. }
  1126. if ($visibility < 0 || $visibility > 4) {
  1127. $error_msg[] = 'visibility is invalid';
  1128. $ok_to_register_course = false;
  1129. }
  1130. if (empty($disk_quota)) {
  1131. $disk_quota = api_get_setting('default_document_quotum');
  1132. }
  1133. $time = api_get_utc_datetime();
  1134. if (stripos($department_url, 'http://') === false && stripos(
  1135. $department_url,
  1136. 'https://'
  1137. ) === false
  1138. ) {
  1139. $department_url = 'http://' . $department_url;
  1140. }
  1141. //just in case
  1142. if ($department_url == 'http://') {
  1143. $department_url = '';
  1144. }
  1145. $course_id = 0;
  1146. if ($ok_to_register_course) {
  1147. // Here we must add 2 fields.
  1148. $course_id = Database::insert(
  1149. $TABLECOURSE,
  1150. [
  1151. 'code' => $code,
  1152. 'directory' => $directory,
  1153. 'course_language' => $course_language,
  1154. 'title' => $title,
  1155. 'description' => get_lang('CourseDescription'),
  1156. 'category_code' => $category_code,
  1157. 'visibility' => $visibility,
  1158. 'show_score' => 1,
  1159. 'disk_quota' => intval($disk_quota),
  1160. 'creation_date' => $time,
  1161. 'expiration_date' => $expiration_date,
  1162. 'last_edit' => $time,
  1163. 'last_visit' => NULL,
  1164. 'tutor_name' => $tutor_name,
  1165. 'department_name' => $department_name,
  1166. 'department_url' => $department_url,
  1167. 'subscribe' => intval($subscribe),
  1168. 'unsubscribe' => intval($unsubscribe),
  1169. 'visual_code' => $visual_code
  1170. ]
  1171. );
  1172. if ($course_id) {
  1173. $sort = api_max_sort_value('0', api_get_user_id());
  1174. // Default true
  1175. $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true;
  1176. if ($addTeacher) {
  1177. $i_course_sort = CourseManager:: userCourseSort(
  1178. $user_id,
  1179. $code
  1180. );
  1181. if (!empty($user_id)) {
  1182. $sql = "INSERT INTO " . $TABLECOURSUSER . " SET
  1183. c_id = '" . $course_id . "',
  1184. user_id = '" . intval($user_id) . "',
  1185. status = '1',
  1186. is_tutor = '0',
  1187. sort = '" . ($i_course_sort) . "',
  1188. relation_type = 0,
  1189. user_course_cat = '0'";
  1190. Database::query($sql);
  1191. }
  1192. }
  1193. if (!empty($teachers)) {
  1194. if (!is_array($teachers)) {
  1195. $teachers = array($teachers);
  1196. }
  1197. foreach ($teachers as $key) {
  1198. //just in case
  1199. if ($key == $user_id) {
  1200. continue;
  1201. }
  1202. if (empty($key)) {
  1203. continue;
  1204. }
  1205. $sql = "INSERT INTO " . $TABLECOURSUSER . " SET
  1206. c_id = '" . Database::escape_string($course_id) . "',
  1207. user_id = '" . Database::escape_string($key) . "',
  1208. status = '1',
  1209. is_tutor = '0',
  1210. sort = '" . ($sort + 1) . "',
  1211. relation_type = 0,
  1212. user_course_cat = '0'";
  1213. Database::query($sql);
  1214. }
  1215. }
  1216. // Adding the course to an URL.
  1217. if (api_is_multiple_url_enabled()) {
  1218. $url_id = 1;
  1219. if (api_get_current_access_url_id() != -1) {
  1220. $url_id = api_get_current_access_url_id();
  1221. }
  1222. UrlManager::add_course_to_url($course_id, $url_id);
  1223. } else {
  1224. UrlManager::add_course_to_url($course_id, 1);
  1225. }
  1226. // Add event to the system log.
  1227. $user_id = api_get_user_id();
  1228. Event::addEvent(
  1229. LOG_COURSE_CREATE,
  1230. LOG_COURSE_CODE,
  1231. $code,
  1232. api_get_utc_datetime(),
  1233. $user_id,
  1234. $course_id
  1235. );
  1236. $send_mail_to_admin = api_get_setting(
  1237. 'send_email_to_admin_when_create_course'
  1238. );
  1239. // @todo Improve code to send to all current portal administrators.
  1240. if ($send_mail_to_admin == 'true') {
  1241. $siteName = api_get_setting('siteName');
  1242. $recipient_email = api_get_setting('emailAdministrator');
  1243. $recipient_name = api_get_person_name(
  1244. api_get_setting('administratorName'),
  1245. api_get_setting('administratorSurname')
  1246. );
  1247. $iname = api_get_setting('Institution');
  1248. $subject = get_lang(
  1249. 'NewCourseCreatedIn'
  1250. ) . ' ' . $siteName . ' - ' . $iname;
  1251. $message = get_lang(
  1252. 'Dear'
  1253. ) . ' ' . $recipient_name . ",\n\n" . get_lang(
  1254. 'MessageOfNewCourseToAdmin'
  1255. ) . ' ' . $siteName . ' - ' . $iname . "\n";
  1256. $message .= get_lang('CourseName') . ' ' . $title . "\n";
  1257. $message .= get_lang(
  1258. 'Category'
  1259. ) . ' ' . $category_code . "\n";
  1260. $message .= get_lang('Tutor') . ' ' . $tutor_name . "\n";
  1261. $message .= get_lang('Language') . ' ' . $course_language;
  1262. $userInfo = api_get_user_info($user_id);
  1263. $additionalParameters = array(
  1264. 'smsType' => SmsPlugin::NEW_COURSE_BEEN_CREATED,
  1265. 'userId' => $user_id,
  1266. 'courseName' => $title,
  1267. 'creatorUsername' => $userInfo['username']
  1268. );
  1269. api_mail_html(
  1270. $recipient_name,
  1271. $recipient_email,
  1272. $subject,
  1273. $message,
  1274. $siteName,
  1275. $recipient_email,
  1276. null,
  1277. null,
  1278. null,
  1279. $additionalParameters
  1280. );
  1281. }
  1282. }
  1283. }
  1284. return $course_id;
  1285. }
  1286. /**
  1287. * Generate a new id for c_tool table
  1288. * @param int $courseId The course id
  1289. * @return int the new id
  1290. */
  1291. public static function generateToolId($courseId)
  1292. {
  1293. $newIdResultData = Database::select(
  1294. 'id + 1 AS new_id',
  1295. Database::get_course_table(TABLE_TOOL_LIST),
  1296. [
  1297. 'where' => ['c_id = ?' => intval($courseId)],
  1298. 'order' => 'id',
  1299. 'limit' => 1
  1300. ],
  1301. 'first'
  1302. );
  1303. if ($newIdResultData === false) {
  1304. return 1;
  1305. }
  1306. return $newIdResultData['new_id'] > 0 ? $newIdResultData['new_id'] : 1;
  1307. }
  1308. }