add_course.lib.inc.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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 = (int) $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. ];
  695. $default_course_path = api_get_path(SYS_CODE_PATH).'default_course_document/';
  696. $default_document_array = [];
  697. foreach ($folders_to_copy_from_default_course as $folder) {
  698. $default_course_folder_path = $default_course_path.$folder.'/';
  699. $files = self::browse_folders(
  700. $default_course_folder_path,
  701. [],
  702. $folder
  703. );
  704. $sorted_array = self::sort_pictures($files, 'dir');
  705. $sorted_array = array_merge(
  706. $sorted_array,
  707. self::sort_pictures($files, 'file')
  708. );
  709. $default_document_array[$folder] = $sorted_array;
  710. }
  711. // Light protection (adding index.html in every document folder)
  712. $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>";
  713. $example_cert_id = 0;
  714. if (is_array($default_document_array) && count(
  715. $default_document_array
  716. ) > 0
  717. ) {
  718. foreach ($default_document_array as $media_type => $array_media) {
  719. $path_documents = "/$media_type/";
  720. //hack until feature #5242 is implemented
  721. if ($media_type == 'images') {
  722. $media_type = 'images/gallery';
  723. $images_folder = $sys_course_path.$course_repository."/document/images/";
  724. if (!is_dir($images_folder)) {
  725. //Creating index.html
  726. mkdir($images_folder, $perm);
  727. $fd = fopen($images_folder.'index.html', 'w');
  728. fwrite($fd, $htmlpage);
  729. @chmod($images_folder.'index.html', $perm_file);
  730. }
  731. }
  732. $course_documents_folder = $sys_course_path.$course_repository."/document/$media_type/";
  733. $default_course_path = api_get_path(SYS_CODE_PATH).'default_course_document'.$path_documents;
  734. if (!is_dir($course_documents_folder)) {
  735. // Creating index.html
  736. mkdir($course_documents_folder, $perm);
  737. $fd = fopen(
  738. $course_documents_folder.'index.html',
  739. 'w'
  740. );
  741. fwrite($fd, $htmlpage);
  742. @chmod(
  743. $course_documents_folder.'index.html',
  744. $perm_file
  745. );
  746. }
  747. if (is_array($array_media) && count($array_media) > 0) {
  748. foreach ($array_media as $key => $value) {
  749. if (isset($value['dir']) && !empty($value['dir'])) {
  750. if (!is_dir($course_documents_folder.$value['dir'])) {
  751. //Creating folder
  752. mkdir(
  753. $course_documents_folder.$value['dir'],
  754. $perm
  755. );
  756. //Creating index.html (for light protection)
  757. $index_html = $course_documents_folder.$value['dir'].'/index.html';
  758. $fd = fopen($index_html, 'w');
  759. fwrite($fd, $htmlpage);
  760. @chmod($index_html, $perm_file);
  761. //Inserting folder in the DB
  762. $folder_path = substr(
  763. $value['dir'],
  764. 0,
  765. strlen($value['dir']) - 1
  766. );
  767. $temp = explode('/', $folder_path);
  768. $title = $temp[count($temp) - 1];
  769. //hack until feature #5242 is implemented
  770. if ($title == 'gallery') {
  771. $title = get_lang(
  772. 'DefaultCourseImages'
  773. );
  774. }
  775. if ($media_type == 'images/gallery') {
  776. $folder_path = 'gallery/'.$folder_path;
  777. }
  778. Database::query(
  779. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  780. VALUES ($course_id,'$path_documents".$folder_path."','".$title."','folder','0')"
  781. );
  782. $image_id = Database:: insert_id();
  783. Database::insert(
  784. $TABLEITEMPROPERTY,
  785. [
  786. 'c_id' => $course_id,
  787. 'tool' => 'document',
  788. 'insert_user_id' => api_get_user_id(),
  789. 'insert_date' => $now,
  790. 'lastedit_date' => $now,
  791. 'ref' => $image_id,
  792. 'lastedit_type' => 'DocumentAdded',
  793. 'lastedit_user_id' => api_get_user_id(),
  794. 'to_group_id' => null,
  795. 'to_user_id' => null,
  796. 'visibility' => 0,
  797. ]
  798. );
  799. }
  800. }
  801. if (isset($value['file']) && !empty($value['file'])) {
  802. if (!file_exists(
  803. $course_documents_folder.$value['file']
  804. )
  805. ) {
  806. //Copying file
  807. copy(
  808. $default_course_path.$value['file'],
  809. $course_documents_folder.$value['file']
  810. );
  811. chmod(
  812. $course_documents_folder.$value['file'],
  813. $perm_file
  814. );
  815. //echo $default_course_path.$value['file']; echo ' - '; echo $course_documents_folder.$value['file']; echo '<br />';
  816. $temp = explode('/', $value['file']);
  817. $file_size = filesize(
  818. $course_documents_folder.$value['file']
  819. );
  820. //hack until feature #5242 is implemented
  821. if ($media_type == 'images/gallery') {
  822. $value["file"] = 'gallery/'.$value["file"];
  823. }
  824. //Inserting file in the DB
  825. Database::query(
  826. "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
  827. VALUES ($course_id,'$path_documents".$value["file"]."','".$temp[count($temp) - 1]."','file','$file_size')"
  828. );
  829. $image_id = Database:: insert_id();
  830. if ($image_id) {
  831. $sql = "UPDATE $TABLETOOLDOCUMENT SET id = iid WHERE iid = $image_id";
  832. Database::query($sql);
  833. if ($path_documents.$value['file'] == '/certificates/default.html') {
  834. $example_cert_id = $image_id;
  835. }
  836. $docId = Database::insert(
  837. $TABLEITEMPROPERTY,
  838. [
  839. 'c_id' => $course_id,
  840. 'tool' => 'document',
  841. 'insert_user_id' => api_get_user_id(),
  842. 'insert_date' => $now,
  843. 'lastedit_date' => $now,
  844. 'ref' => $image_id,
  845. 'lastedit_type' => 'DocumentAdded',
  846. 'lastedit_user_id' => api_get_user_id(),
  847. 'to_group_id' => null,
  848. 'to_user_id' => null,
  849. 'visibility' => 1,
  850. ]
  851. );
  852. if ($docId) {
  853. $sql = "UPDATE $TABLEITEMPROPERTY SET id = iid WHERE iid = $docId";
  854. Database::query($sql);
  855. }
  856. }
  857. }
  858. }
  859. }
  860. }
  861. }
  862. }
  863. $agenda = new Agenda('course');
  864. $agenda->set_course($courseInfo);
  865. $agenda->addEvent(
  866. $now,
  867. $now,
  868. 0,
  869. get_lang('AgendaCreationTitle'),
  870. get_lang('AgendaCreationContenu')
  871. );
  872. /* Links tool */
  873. $link = new Link();
  874. $link->setCourse($courseInfo);
  875. $links = [
  876. [
  877. 'c_id' => $course_id,
  878. 'url' => 'http://www.google.com',
  879. 'title' => 'Google',
  880. 'description' => get_lang('Google'),
  881. 'category_id' => 0,
  882. 'on_homepage' => 0,
  883. 'target' => '_self',
  884. 'session_id' => 0,
  885. ],
  886. [
  887. 'c_id' => $course_id,
  888. 'url' => 'http://www.wikipedia.org',
  889. 'title' => 'Wikipedia',
  890. 'description' => get_lang('Wikipedia'),
  891. 'category_id' => 0,
  892. 'on_homepage' => 0,
  893. 'target' => '_self',
  894. 'session_id' => 0,
  895. ],
  896. ];
  897. foreach ($links as $params) {
  898. $link->save($params);
  899. }
  900. /* Announcement tool */
  901. AnnouncementManager::add_announcement(
  902. $courseInfo,
  903. 0,
  904. get_lang('AnnouncementExampleTitle'),
  905. get_lang('AnnouncementEx'),
  906. ['everyone' => 'everyone'],
  907. null,
  908. null,
  909. $now
  910. );
  911. $manager = Database::getManager();
  912. /* Introduction text */
  913. $intro_text = '<p style="text-align: center;">
  914. <img src="'.api_get_path(REL_CODE_PATH).'img/mascot.png" alt="Mr. Chamilo" title="Mr. Chamilo" />
  915. <h2>'.get_lang('IntroductionText').'</h2>
  916. </p>';
  917. $toolIntro = new CToolIntro();
  918. $toolIntro
  919. ->setCId($course_id)
  920. ->setId(TOOL_COURSE_HOMEPAGE)
  921. ->setSessionId(0)
  922. ->setIntroText($intro_text);
  923. $manager->persist($toolIntro);
  924. $toolIntro = new CToolIntro();
  925. $toolIntro
  926. ->setCId($course_id)
  927. ->setId(TOOL_STUDENTPUBLICATION)
  928. ->setSessionId(0)
  929. ->setIntroText(get_lang('IntroductionTwo'));
  930. $manager->persist($toolIntro);
  931. $toolIntro = new CToolIntro();
  932. $toolIntro
  933. ->setCId($course_id)
  934. ->setId(TOOL_WIKI)
  935. ->setSessionId(0)
  936. ->setIntroText(get_lang('IntroductionWiki'));
  937. $manager->persist($toolIntro);
  938. $manager->flush();
  939. /* Exercise tool */
  940. $exercise = new Exercise($course_id);
  941. $exercise->exercise = get_lang('ExerciceEx');
  942. $html = '<table width="100%" border="0" cellpadding="0" cellspacing="0">
  943. <tr>
  944. <td width="220" valign="top" align="left">
  945. <img src="'.api_get_path(WEB_CODE_PATH).'default_course_document/images/mr_chamilo/doubts.png">
  946. </td>
  947. <td valign="top" align="left">'.get_lang('Antique').'</td></tr>
  948. </table>';
  949. $exercise->type = 1;
  950. $exercise->setRandom(0);
  951. $exercise->active = 1;
  952. $exercise->results_disabled = 0;
  953. $exercise->description = $html;
  954. $exercise->save();
  955. $exercise_id = $exercise->id;
  956. $question = new MultipleAnswer();
  957. $question->question = get_lang('SocraticIrony');
  958. $question->description = get_lang('ManyAnswers');
  959. $question->weighting = 10;
  960. $question->position = 1;
  961. $question->course = $courseInfo;
  962. $question->save($exercise);
  963. $questionId = $question->id;
  964. $answer = new Answer($questionId, $courseInfo['real_id']);
  965. $answer->createAnswer(get_lang('Ridiculise'), 0, get_lang('NoPsychology'), -5, 1);
  966. $answer->createAnswer(get_lang('AdmitError'), 0, get_lang('NoSeduction'), -5, 2);
  967. $answer->createAnswer(get_lang('Force'), 1, get_lang('Indeed'), 5, 3);
  968. $answer->createAnswer(get_lang('Contradiction'), 1, get_lang('NotFalse'), 5, 4);
  969. $answer->save();
  970. /* Forum tool */
  971. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  972. $params = [
  973. 'forum_category_title' => get_lang('ExampleForumCategory'),
  974. 'forum_category_comment' => '',
  975. ];
  976. $forumCategoryId = store_forumcategory($params, $courseInfo, false);
  977. $params = [
  978. 'forum_category' => $forumCategoryId,
  979. 'forum_title' => get_lang('ExampleForum'),
  980. 'forum_comment' => '',
  981. 'default_view_type_group' => ['default_view_type' => 'flat'],
  982. ];
  983. $forumId = store_forum($params, $courseInfo, true);
  984. $forumInfo = get_forum_information($forumId, $courseInfo['real_id']);
  985. $params = [
  986. 'post_title' => get_lang('ExampleThread'),
  987. 'forum_id' => $forumId,
  988. 'post_text' => get_lang('ExampleThreadContent'),
  989. 'calification_notebook_title' => '',
  990. 'numeric_calification' => '',
  991. 'weight_calification' => '',
  992. 'forum_category' => $forumCategoryId,
  993. 'thread_peer_qualify' => 0,
  994. ];
  995. store_thread($forumInfo, $params, $courseInfo, false);
  996. /* Gradebook tool */
  997. $course_code = $courseInfo['code'];
  998. // father gradebook
  999. Database::query(
  1000. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  1001. VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)"
  1002. );
  1003. $gbid = Database:: insert_id();
  1004. Database::query(
  1005. "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id)
  1006. VALUES ('$course_code','',1,'$course_code',$gbid,100,1,75,NULL,$example_cert_id)"
  1007. );
  1008. $gbid = Database:: insert_id();
  1009. Database::query(
  1010. "INSERT INTO $TABLEGRADEBOOKLINK (type, ref_id, user_id, course_code, category_id, created_at, weight, visible, locked)
  1011. VALUES (1,$exercise_id,1,'$course_code',$gbid,'$now',100,1,0)"
  1012. );
  1013. }
  1014. //Installing plugins in course
  1015. $app_plugin = new AppPlugin();
  1016. $app_plugin->install_course_plugins($course_id);
  1017. return true;
  1018. }
  1019. /**
  1020. * @param int $course_id
  1021. * @param int $counter
  1022. * @param array $file
  1023. * @param int $authorId
  1024. */
  1025. public static function insertDocument($course_id, $counter, $file, $authorId = 0)
  1026. {
  1027. $tableItem = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1028. $tableDocument = Database::get_course_table(TABLE_DOCUMENT);
  1029. $now = api_get_utc_datetime();
  1030. $sql = "INSERT INTO $tableDocument (id, c_id, path,title,filetype,size, readonly, session_id)
  1031. VALUES ($counter, $course_id, '".$file['path']."', '".$file['title']."', '".$file['filetype']."', '".$file['size']."', 0, 0)";
  1032. Database::query($sql);
  1033. $docId = Database:: insert_id();
  1034. $authorId = empty($authorId) ? api_get_user_id() : (int) $authorId;
  1035. if ($docId) {
  1036. $sql = "UPDATE $tableDocument SET id = iid WHERE iid = $docId";
  1037. Database::query($sql);
  1038. $id = Database::insert(
  1039. $tableItem,
  1040. [
  1041. 'id' => $counter,
  1042. 'c_id' => $course_id,
  1043. 'tool' => 'document',
  1044. 'insert_user_id' => $authorId,
  1045. 'insert_date' => $now,
  1046. 'lastedit_date' => $now,
  1047. 'ref' => $docId,
  1048. 'lastedit_type' => 'DocumentAdded',
  1049. 'lastedit_user_id' => $authorId,
  1050. 'to_group_id' => null,
  1051. 'to_user_id' => null,
  1052. 'visibility' => 0,
  1053. ]
  1054. );
  1055. if ($id) {
  1056. $sql = "UPDATE $tableItem SET id = iid WHERE iid = $id";
  1057. Database::query($sql);
  1058. }
  1059. }
  1060. }
  1061. /**
  1062. * string2binary converts the string "true" or "false" to the boolean true false (0 or 1)
  1063. * This is used for the Chamilo Config Settings as these store true or false as string
  1064. * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for
  1065. * the visibility of the tool).
  1066. *
  1067. * @param string $variable
  1068. *
  1069. * @return bool
  1070. *
  1071. * @author Patrick Cool, patrick.cool@ugent.be
  1072. * @assert ('true') === true
  1073. * @assert ('false') === false
  1074. */
  1075. public static function string2binary($variable)
  1076. {
  1077. if ($variable == 'true') {
  1078. return true;
  1079. }
  1080. if ($variable == 'false') {
  1081. return false;
  1082. }
  1083. }
  1084. /**
  1085. * Function register_course to create a record in the course table of the main database.
  1086. *
  1087. * @param array $params Course details (see code for details).
  1088. * @param int $accessUrlId Optional.
  1089. *
  1090. * @return int Created course ID
  1091. *
  1092. * @todo use an array called $params instead of lots of params
  1093. * @assert (null) === false
  1094. */
  1095. public static function register_course($params, $accessUrlId = 1)
  1096. {
  1097. global $error_msg, $firstExpirationDelay;
  1098. $title = $params['title'];
  1099. // Fix amp
  1100. $title = str_replace('&amp;', '&', $title);
  1101. $code = $params['code'];
  1102. $visual_code = $params['visual_code'];
  1103. $directory = $params['directory'];
  1104. $tutor_name = isset($params['tutor_name']) ? $params['tutor_name'] : null;
  1105. $category_code = isset($params['course_category']) ? $params['course_category'] : '';
  1106. $course_language = isset($params['course_language']) && !empty($params['course_language']) ? $params['course_language'] : api_get_setting(
  1107. 'platformLanguage'
  1108. );
  1109. $user_id = empty($params['user_id']) ? api_get_user_id() : intval($params['user_id']);
  1110. $department_name = isset($params['department_name']) ?
  1111. $params['department_name'] : null;
  1112. $department_url = isset($params['department_url']) ?
  1113. $params['department_url'] : null;
  1114. $disk_quota = isset($params['disk_quota']) ?
  1115. $params['disk_quota'] : null;
  1116. if (!isset($params['visibility'])) {
  1117. $default_course_visibility = api_get_setting(
  1118. 'courses_default_creation_visibility'
  1119. );
  1120. if (isset($default_course_visibility)) {
  1121. $visibility = $default_course_visibility;
  1122. } else {
  1123. $visibility = COURSE_VISIBILITY_OPEN_PLATFORM;
  1124. }
  1125. } else {
  1126. $visibility = $params['visibility'];
  1127. }
  1128. $subscribe = isset($params['subscribe']) ? (int) $params['subscribe'] : $visibility == COURSE_VISIBILITY_OPEN_PLATFORM ? 1 : 0;
  1129. $unsubscribe = isset($params['unsubscribe']) ? intval($params['unsubscribe']) : 0;
  1130. $expiration_date = isset($params['expiration_date']) ? $params['expiration_date'] : null;
  1131. $teachers = isset($params['teachers']) ? $params['teachers'] : null;
  1132. $status = isset($params['status']) ? $params['status'] : null;
  1133. $TABLECOURSE = Database::get_main_table(TABLE_MAIN_COURSE);
  1134. $TABLECOURSUSER = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1135. $ok_to_register_course = true;
  1136. // Check whether all the needed parameters are present.
  1137. if (empty($code)) {
  1138. $error_msg[] = 'courseSysCode is missing';
  1139. $ok_to_register_course = false;
  1140. }
  1141. if (empty($visual_code)) {
  1142. $error_msg[] = 'courseScreenCode is missing';
  1143. $ok_to_register_course = false;
  1144. }
  1145. if (empty($directory)) {
  1146. $error_msg[] = 'courseRepository is missing';
  1147. $ok_to_register_course = false;
  1148. }
  1149. if (empty($title)) {
  1150. $error_msg[] = 'title is missing';
  1151. $ok_to_register_course = false;
  1152. }
  1153. if (empty($expiration_date)) {
  1154. $expiration_date = api_get_utc_datetime(
  1155. time() + $firstExpirationDelay
  1156. );
  1157. } else {
  1158. $expiration_date = api_get_utc_datetime($expiration_date);
  1159. }
  1160. if ($visibility < 0 || $visibility > 4) {
  1161. $error_msg[] = 'visibility is invalid';
  1162. $ok_to_register_course = false;
  1163. }
  1164. if (empty($disk_quota)) {
  1165. $disk_quota = api_get_setting('default_document_quotum');
  1166. }
  1167. $time = api_get_utc_datetime();
  1168. if (stripos($department_url, 'http://') === false && stripos(
  1169. $department_url,
  1170. 'https://'
  1171. ) === false
  1172. ) {
  1173. $department_url = 'http://'.$department_url;
  1174. }
  1175. //just in case
  1176. if ($department_url == 'http://') {
  1177. $department_url = '';
  1178. }
  1179. $course_id = 0;
  1180. if ($ok_to_register_course) {
  1181. // Here we must add 2 fields.
  1182. $course_id = Database::insert(
  1183. $TABLECOURSE,
  1184. [
  1185. 'code' => $code,
  1186. 'directory' => $directory,
  1187. 'course_language' => $course_language,
  1188. 'title' => $title,
  1189. 'description' => get_lang('CourseDescription'),
  1190. 'category_code' => $category_code,
  1191. 'visibility' => $visibility,
  1192. 'show_score' => 1,
  1193. 'disk_quota' => intval($disk_quota),
  1194. 'creation_date' => $time,
  1195. 'expiration_date' => $expiration_date,
  1196. 'last_edit' => $time,
  1197. 'last_visit' => null,
  1198. 'tutor_name' => $tutor_name,
  1199. 'department_name' => $department_name,
  1200. 'department_url' => $department_url,
  1201. 'subscribe' => intval($subscribe),
  1202. 'unsubscribe' => intval($unsubscribe),
  1203. 'visual_code' => $visual_code,
  1204. ]
  1205. );
  1206. if ($course_id) {
  1207. $sort = api_max_sort_value('0', api_get_user_id());
  1208. // Default true
  1209. $addTeacher = isset($params['add_user_as_teacher']) ? $params['add_user_as_teacher'] : true;
  1210. if ($addTeacher) {
  1211. $i_course_sort = CourseManager:: userCourseSort(
  1212. $user_id,
  1213. $code
  1214. );
  1215. if (!empty($user_id)) {
  1216. $sql = "INSERT INTO ".$TABLECOURSUSER." SET
  1217. c_id = '".$course_id."',
  1218. user_id = '".intval($user_id)."',
  1219. status = '1',
  1220. is_tutor = '0',
  1221. sort = '".($i_course_sort)."',
  1222. relation_type = 0,
  1223. user_course_cat = '0'";
  1224. Database::query($sql);
  1225. }
  1226. }
  1227. if (!empty($teachers)) {
  1228. if (!is_array($teachers)) {
  1229. $teachers = [$teachers];
  1230. }
  1231. foreach ($teachers as $key) {
  1232. //just in case
  1233. if ($key == $user_id) {
  1234. continue;
  1235. }
  1236. if (empty($key)) {
  1237. continue;
  1238. }
  1239. $sql = "INSERT INTO ".$TABLECOURSUSER." SET
  1240. c_id = '".Database::escape_string($course_id)."',
  1241. user_id = '".Database::escape_string($key)."',
  1242. status = '1',
  1243. is_tutor = '0',
  1244. sort = '".($sort + 1)."',
  1245. relation_type = 0,
  1246. user_course_cat = '0'";
  1247. Database::query($sql);
  1248. }
  1249. }
  1250. // Adding the course to an URL.
  1251. UrlManager::add_course_to_url($course_id, $accessUrlId);
  1252. // Add event to the system log.
  1253. $user_id = api_get_user_id();
  1254. Event::addEvent(
  1255. LOG_COURSE_CREATE,
  1256. LOG_COURSE_CODE,
  1257. $code,
  1258. api_get_utc_datetime(),
  1259. $user_id,
  1260. $course_id
  1261. );
  1262. $send_mail_to_admin = api_get_setting(
  1263. 'send_email_to_admin_when_create_course'
  1264. );
  1265. // @todo Improve code to send to all current portal administrators.
  1266. if ($send_mail_to_admin == 'true') {
  1267. $siteName = api_get_setting('siteName');
  1268. $recipient_email = api_get_setting('emailAdministrator');
  1269. $recipient_name = api_get_person_name(
  1270. api_get_setting('administratorName'),
  1271. api_get_setting('administratorSurname')
  1272. );
  1273. $iname = api_get_setting('Institution');
  1274. $subject = get_lang(
  1275. 'NewCourseCreatedIn'
  1276. ).' '.$siteName.' - '.$iname;
  1277. $message = get_lang(
  1278. 'Dear'
  1279. ).' '.$recipient_name.",\n\n".get_lang(
  1280. 'MessageOfNewCourseToAdmin'
  1281. ).' '.$siteName.' - '.$iname."\n";
  1282. $message .= get_lang('CourseName').' '.$title."\n";
  1283. $message .= get_lang(
  1284. 'Category'
  1285. ).' '.$category_code."\n";
  1286. $message .= get_lang('Tutor').' '.$tutor_name."\n";
  1287. $message .= get_lang('Language').' '.$course_language;
  1288. $userInfo = api_get_user_info($user_id);
  1289. $additionalParameters = [
  1290. 'smsType' => SmsPlugin::NEW_COURSE_BEEN_CREATED,
  1291. 'userId' => $user_id,
  1292. 'courseName' => $title,
  1293. 'creatorUsername' => $userInfo['username'],
  1294. ];
  1295. api_mail_html(
  1296. $recipient_name,
  1297. $recipient_email,
  1298. $subject,
  1299. $message,
  1300. $siteName,
  1301. $recipient_email,
  1302. null,
  1303. null,
  1304. null,
  1305. $additionalParameters
  1306. );
  1307. }
  1308. }
  1309. }
  1310. return $course_id;
  1311. }
  1312. /**
  1313. * Generate a new id for c_tool table.
  1314. *
  1315. * @param int $courseId The course id
  1316. *
  1317. * @return int the new id
  1318. */
  1319. public static function generateToolId($courseId)
  1320. {
  1321. $newIdResultData = Database::select(
  1322. 'id + 1 AS new_id',
  1323. Database::get_course_table(TABLE_TOOL_LIST),
  1324. [
  1325. 'where' => ['c_id = ?' => intval($courseId)],
  1326. 'order' => 'id',
  1327. 'limit' => 1,
  1328. ],
  1329. 'first'
  1330. );
  1331. if ($newIdResultData === false) {
  1332. return 1;
  1333. }
  1334. return $newIdResultData['new_id'] > 0 ? $newIdResultData['new_id'] : 1;
  1335. }
  1336. }