course_description.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains a class used like library provides functions for
  5. * course description tool. It's also used like model to
  6. * course_description_controller (MVC pattern).
  7. *
  8. * @author Christian Fasanando <christian1827@gmail.com>
  9. *
  10. * @package chamilo.course_description
  11. */
  12. /**
  13. * Class CourseDescription course descriptions.
  14. *
  15. * @package chamilo.course_description
  16. */
  17. class CourseDescription
  18. {
  19. private $id;
  20. private $course_id;
  21. private $title;
  22. private $content;
  23. private $session_id;
  24. private $description_type;
  25. private $progress;
  26. /**
  27. * Constructor.
  28. */
  29. public function __construct()
  30. {
  31. }
  32. /**
  33. * Returns an array of objects of type CourseDescription corresponding to
  34. * a specific course, without session ids (session id = 0).
  35. *
  36. * @param int $course_id
  37. *
  38. * @return array Array of CourseDescriptions
  39. */
  40. public static function get_descriptions($course_id)
  41. {
  42. // Get course code
  43. $course_info = api_get_course_info_by_id($course_id);
  44. if (!empty($course_info)) {
  45. $course_id = $course_info['real_id'];
  46. } else {
  47. return [];
  48. }
  49. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  50. $sql = "SELECT * FROM $table
  51. WHERE c_id = $course_id AND session_id = '0'";
  52. $sql_result = Database::query($sql);
  53. $results = [];
  54. while ($row = Database::fetch_array($sql_result)) {
  55. $desc_tmp = new CourseDescription();
  56. $desc_tmp->set_id($row['id']);
  57. $desc_tmp->set_title($row['title']);
  58. $desc_tmp->set_content($row['content']);
  59. $desc_tmp->set_session_id($row['session_id']);
  60. $desc_tmp->set_description_type($row['description_type']);
  61. $desc_tmp->set_progress($row['progress']);
  62. $results[] = $desc_tmp;
  63. }
  64. return $results;
  65. }
  66. /**
  67. * Get all data of course description by session id,
  68. * first you must set session_id property with the object CourseDescription.
  69. *
  70. * @return array
  71. */
  72. public function get_description_data()
  73. {
  74. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  75. $condition_session = api_get_session_condition(
  76. $this->session_id,
  77. true,
  78. true
  79. );
  80. $course_id = $this->course_id ?: api_get_course_int_id();
  81. $sql = "SELECT * FROM $table
  82. WHERE c_id = $course_id $condition_session
  83. ORDER BY id ";
  84. $rs = Database::query($sql);
  85. $data = [];
  86. while ($description = Database::fetch_array($rs)) {
  87. $data['descriptions'][$description['id']] = $description;
  88. }
  89. return $data;
  90. }
  91. /**
  92. * Get all data by description and session id,
  93. * first you must set session_id property with the object CourseDescription.
  94. *
  95. * @param int $description_type Description type
  96. * @param string $courseId Course code (optional)
  97. * @param int $session_id Session id (optional)
  98. *
  99. * @return array List of fields from the descriptions found of the given type
  100. */
  101. public function get_data_by_description_type(
  102. $description_type,
  103. $courseId = null,
  104. $session_id = null
  105. ) {
  106. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  107. if (empty($courseId)) {
  108. $courseId = api_get_course_int_id();
  109. }
  110. if (!isset($session_id)) {
  111. $session_id = $this->session_id;
  112. }
  113. $condition_session = api_get_session_condition($session_id);
  114. $description_type = intval($description_type);
  115. $sql = "SELECT * FROM $table
  116. WHERE
  117. c_id = $courseId AND
  118. description_type = '$description_type'
  119. $condition_session ";
  120. $rs = Database::query($sql);
  121. $data = [];
  122. if ($description = Database::fetch_array($rs)) {
  123. $data['description_title'] = $description['title'];
  124. $data['description_content'] = $description['content'];
  125. $data['progress'] = $description['progress'];
  126. $data['id'] = $description['id'];
  127. }
  128. return $data;
  129. }
  130. /**
  131. * @param int $id
  132. * @param string $course_code
  133. * @param int $session_id
  134. *
  135. * @return array
  136. */
  137. public function get_data_by_id($id, $course_code = '', $session_id = null)
  138. {
  139. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  140. $course_id = api_get_course_int_id();
  141. if (!isset($session_id)) {
  142. $session_id = $this->session_id;
  143. }
  144. $condition_session = api_get_session_condition($session_id);
  145. if (!empty($course_code)) {
  146. $course_info = api_get_course_info($course_code);
  147. $course_id = $course_info['real_id'];
  148. }
  149. $id = intval($id);
  150. $sql = "SELECT * FROM $table
  151. WHERE c_id = $course_id AND id='$id' $condition_session ";
  152. $rs = Database::query($sql);
  153. $data = [];
  154. if ($description = Database::fetch_array($rs)) {
  155. $data['description_type'] = $description['description_type'];
  156. $data['description_title'] = $description['title'];
  157. $data['description_content'] = $description['content'];
  158. $data['progress'] = $description['progress'];
  159. }
  160. return $data;
  161. }
  162. /**
  163. * Get maximum description type by session id,
  164. * first you must set session_id properties with the object CourseDescription.
  165. *
  166. * @return int maximum description time adding one
  167. */
  168. public function get_max_description_type()
  169. {
  170. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  171. $course_id = api_get_course_int_id();
  172. $sql = "SELECT MAX(description_type) as MAX
  173. FROM $table
  174. WHERE c_id = $course_id AND session_id='".$this->session_id."'";
  175. $rs = Database::query($sql);
  176. $max = Database::fetch_array($rs);
  177. if ($max['MAX'] >= 8) {
  178. $description_type = 8;
  179. } else {
  180. $description_type = $max['MAX'] + 1;
  181. }
  182. if ($description_type < ADD_BLOCK) {
  183. $description_type = ADD_BLOCK;
  184. }
  185. return $description_type;
  186. }
  187. /**
  188. * Insert a description to the course_description table,
  189. * first you must set description_type, title, content, progress and
  190. * session_id properties with the object CourseDescription.
  191. *
  192. * @return int affected rows
  193. */
  194. public function insert()
  195. {
  196. if (empty($this->course_id)) {
  197. $course_id = api_get_course_int_id();
  198. } else {
  199. $course_id = $this->course_id;
  200. }
  201. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  202. $params = [
  203. 'c_id' => $course_id,
  204. 'description_type' => $this->description_type,
  205. 'title' => $this->title,
  206. 'content' => $this->content,
  207. 'progress' => intval($this->progress),
  208. 'session_id' => $this->session_id,
  209. ];
  210. $last_id = Database::insert($table, $params);
  211. if ($last_id > 0) {
  212. $sql = "UPDATE $table SET id = iid WHERE iid = $last_id";
  213. Database::query($sql);
  214. // insert into item_property
  215. api_item_property_update(
  216. api_get_course_info(),
  217. TOOL_COURSE_DESCRIPTION,
  218. $last_id,
  219. 'CourseDescriptionAdded',
  220. api_get_user_id()
  221. );
  222. }
  223. return $last_id > 0 ? 1 : 0;
  224. }
  225. /**
  226. * Update a description, first you must set description_type, title, content, progress
  227. * and session_id properties with the object CourseDescription.
  228. *
  229. * @return int affected rows
  230. */
  231. public function update()
  232. {
  233. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  234. $params = [
  235. 'title' => $this->title,
  236. 'content' => $this->content,
  237. 'progress' => intval($this->progress),
  238. ];
  239. Database::update(
  240. $table,
  241. $params,
  242. [
  243. 'id = ? AND session_id = ? AND c_id = ?' => [
  244. $this->id,
  245. $this->session_id,
  246. $this->course_id ? $this->course_id : api_get_course_int_id(),
  247. ],
  248. ]
  249. );
  250. if ($this->id > 0) {
  251. // Insert into item_property
  252. api_item_property_update(
  253. api_get_course_info(),
  254. TOOL_COURSE_DESCRIPTION,
  255. $this->id,
  256. 'CourseDescriptionUpdated',
  257. api_get_user_id()
  258. );
  259. }
  260. return 1;
  261. }
  262. /**
  263. * Delete a description, first you must set description_type and session_id
  264. * properties with the object CourseDescription.
  265. *
  266. * @return int affected rows
  267. */
  268. public function delete()
  269. {
  270. $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  271. $course_id = api_get_course_int_id();
  272. $sql = "DELETE FROM $table
  273. WHERE
  274. c_id = $course_id AND
  275. id = '".intval($this->id)."' AND
  276. session_id = '".intval($this->session_id)."'";
  277. $result = Database::query($sql);
  278. $affected_rows = Database::affected_rows($result);
  279. if ($this->id > 0) {
  280. //insert into item_property
  281. api_item_property_update(
  282. api_get_course_info(),
  283. TOOL_COURSE_DESCRIPTION,
  284. $this->id,
  285. 'CourseDescriptionDeleted',
  286. api_get_user_id()
  287. );
  288. }
  289. return $affected_rows;
  290. }
  291. /**
  292. * Get description titles by default.
  293. *
  294. * @return array
  295. */
  296. public function get_default_description_title()
  297. {
  298. $default_description_titles = [];
  299. $default_description_titles[1] = get_lang('GeneralDescription');
  300. $default_description_titles[2] = get_lang('Objectives');
  301. $default_description_titles[3] = get_lang('Topics');
  302. $default_description_titles[4] = get_lang('Methodology');
  303. $default_description_titles[5] = get_lang('CourseMaterial');
  304. $default_description_titles[6] = get_lang('HumanAndTechnicalResources');
  305. $default_description_titles[7] = get_lang('Assessment');
  306. $default_description_titles[8] = get_lang('Other');
  307. return $default_description_titles;
  308. }
  309. /**
  310. * Get description titles editable by default.
  311. *
  312. * @return array
  313. */
  314. public function get_default_description_title_editable()
  315. {
  316. $default_description_title_editable = [];
  317. $default_description_title_editable[1] = true;
  318. $default_description_title_editable[2] = true;
  319. $default_description_title_editable[3] = true;
  320. $default_description_title_editable[4] = true;
  321. $default_description_title_editable[5] = true;
  322. $default_description_title_editable[6] = true;
  323. $default_description_title_editable[7] = true;
  324. //$default_description_title_editable[8] = true;
  325. return $default_description_title_editable;
  326. }
  327. /**
  328. * Get description icons by default.
  329. *
  330. * @return array
  331. */
  332. public function get_default_description_icon()
  333. {
  334. $default_description_icon = [];
  335. $default_description_icon[1] = 'info.png';
  336. $default_description_icon[2] = 'objective.png';
  337. $default_description_icon[3] = 'topics.png';
  338. $default_description_icon[4] = 'strategy.png';
  339. $default_description_icon[5] = 'laptop.png';
  340. $default_description_icon[6] = 'teacher.png';
  341. $default_description_icon[7] = 'assessment.png';
  342. $default_description_icon[8] = 'wizard.png';
  343. return $default_description_icon;
  344. }
  345. /**
  346. * Get questions by default for help.
  347. *
  348. * @return array
  349. */
  350. public function get_default_question()
  351. {
  352. $question = [];
  353. $question[1] = get_lang('GeneralDescriptionQuestions');
  354. $question[2] = get_lang('ObjectivesQuestions');
  355. $question[3] = get_lang('TopicsQuestions');
  356. $question[4] = get_lang('MethodologyQuestions');
  357. $question[5] = get_lang('CourseMaterialQuestions');
  358. $question[6] = get_lang('HumanAndTechnicalResourcesQuestions');
  359. $question[7] = get_lang('AssessmentQuestions');
  360. //$question[8]= get_lang('ThematicAdvanceQuestions');
  361. return $question;
  362. }
  363. /**
  364. * Get informations by default for help.
  365. *
  366. * @return array
  367. */
  368. public function get_default_information()
  369. {
  370. $information = [];
  371. $information[1] = get_lang('GeneralDescriptionInformation');
  372. $information[2] = get_lang('ObjectivesInformation');
  373. $information[3] = get_lang('TopicsInformation');
  374. $information[4] = get_lang('MethodologyInformation');
  375. $information[5] = get_lang('CourseMaterialInformation');
  376. $information[6] = get_lang('HumanAndTechnicalResourcesInformation');
  377. $information[7] = get_lang('AssessmentInformation');
  378. //$information[8]= get_lang('ThematicAdvanceInformation');
  379. return $information;
  380. }
  381. /**
  382. * Set description id.
  383. */
  384. public function set_id($id)
  385. {
  386. $this->id = $id;
  387. }
  388. /**
  389. * Set description's course id.
  390. *
  391. * @param int $id Course ID
  392. */
  393. public function set_course_id($id)
  394. {
  395. $this->course_id = intval($id);
  396. }
  397. /**
  398. * Set description title.
  399. *
  400. * @param string $title
  401. */
  402. public function set_title($title)
  403. {
  404. $this->title = $title;
  405. }
  406. /**
  407. * Set description content.
  408. *
  409. * @param string $content
  410. */
  411. public function set_content($content)
  412. {
  413. $this->content = $content;
  414. }
  415. /**
  416. * Set description session id.
  417. *
  418. * @param int $session_id
  419. */
  420. public function set_session_id($session_id)
  421. {
  422. $this->session_id = $session_id;
  423. }
  424. /**
  425. * Set description type.
  426. */
  427. public function set_description_type($description_type)
  428. {
  429. $this->description_type = $description_type;
  430. }
  431. /**
  432. * Set progress of a description.
  433. *
  434. * @param string $progress
  435. */
  436. public function set_progress($progress)
  437. {
  438. $this->progress = $progress;
  439. }
  440. /**
  441. * get description id.
  442. *
  443. * @return int
  444. */
  445. public function get_id()
  446. {
  447. return $this->id;
  448. }
  449. /**
  450. * get description title.
  451. *
  452. * @return string
  453. */
  454. public function get_title()
  455. {
  456. return $this->title;
  457. }
  458. /**
  459. * get description content.
  460. *
  461. * @return string
  462. */
  463. public function get_content()
  464. {
  465. return $this->content;
  466. }
  467. /**
  468. * get session id.
  469. *
  470. * @return int
  471. */
  472. public function get_session_id()
  473. {
  474. return $this->session_id;
  475. }
  476. /**
  477. * get description type.
  478. *
  479. * @return int
  480. */
  481. public function get_description_type()
  482. {
  483. return $this->description_type;
  484. }
  485. /**
  486. * get progress of a description.
  487. *
  488. * @return int
  489. */
  490. public function get_progress()
  491. {
  492. return $this->progress;
  493. }
  494. }