add_course.lib.inc.php 63 KB

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