add_course.lib.inc.php 62 KB

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