course_description.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. namespace CourseDescription;
  3. /**
  4. * Object Model for the "Course Description" database table. Allows to
  5. *
  6. * - query database
  7. * - create/insert new course descriptions
  8. * - update/edit course descriptions
  9. * - delete course descriptions
  10. *
  11. * Course descriptions used to provide descriptions for course/sessions.
  12. * A course/session can have several descriptions associated to it from various types.
  13. * Course descriptions are primarily made primarily of
  14. *
  15. * - a title
  16. * - some content
  17. * - a type (for ex: info, objectives, etc)
  18. *
  19. * Usage:
  20. *
  21. * Create
  22. *
  23. * $des = new CourseDescription();
  24. * $des->set_title('...');
  25. * $des->set_content('...');
  26. * $des->set_description_type(...);
  27. * $des->insert();
  28. *
  29. * Update
  30. *
  31. * $des = CourseDescription::get_by_id(..., ...);
  32. * $des->set_title('...');
  33. * $des->update();
  34. *
  35. * Delete
  36. *
  37. * $des = CourseDescription::get_by_id(..., ...);
  38. * $des->delete();
  39. *
  40. * @package chamilo.course_description
  41. * @author Christian Fasanando <christian1827@gmail.com>
  42. * @author Laurent Opprecht <laurent@opprecht.info> for the University of Geneva
  43. * @licence /license.txt
  44. */
  45. class CourseDescription
  46. {
  47. /**
  48. * Return the repository.
  49. *
  50. * @return \CourseDescription\CourseDescriptionRepository
  51. */
  52. public static function repository()
  53. {
  54. return CourseDescriptionRepository::instance();
  55. }
  56. /**
  57. * Returns the list of all available types
  58. *
  59. * @return array
  60. */
  61. public static function get_types()
  62. {
  63. return CourseDescriptionType::all();
  64. }
  65. // /**
  66. // * Deprecated (still used by web services)
  67. // *
  68. // * @param int Course id
  69. // * @deprecated use get_descriptions_by_course
  70. // * @return array Array of CourseDescriptions
  71. // */
  72. // public static function get_descriptions($course_id)
  73. // {
  74. // // Get course code
  75. // $course_info = api_get_course_info_by_id($course_id);
  76. // if (!empty($course_info)) {
  77. // $course_id = $course_info['real_id'];
  78. // } else {
  79. // return array();
  80. // }
  81. // $t_course_desc = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  82. // $sql = "SELECT * FROM $t_course_desc WHERE c_id = $course_id AND session_id = '0';";
  83. // $sql_result = Database::query($sql);
  84. // $results = array();
  85. // while ($row = Database::fetch_array($sql_result)) {
  86. // $desc_tmp = new CourseDescription();
  87. // $desc_tmp->set_id($row['id']);
  88. // $desc_tmp->set_title($row['title']);
  89. // $desc_tmp->set_content($row['content']);
  90. // $desc_tmp->set_session_id($row['session_id']);
  91. // $desc_tmp->set_description_type($row['description_type']);
  92. // $desc_tmp->set_progress($row['progress']);
  93. // $results[] = $desc_tmp;
  94. // }
  95. // return $results;
  96. // }
  97. /**
  98. *
  99. * @param object $data
  100. * @return \CourseDescription\CourseDescription
  101. */
  102. public static function create($data = null)
  103. {
  104. return new self($data);
  105. }
  106. protected $c_id;
  107. protected $id;
  108. protected $title;
  109. protected $content;
  110. protected $session_id;
  111. protected $description_type;
  112. protected $progress;
  113. protected $type = null;
  114. function __construct($data = null)
  115. {
  116. if ($data) {
  117. foreach ($this as $key => $value) {
  118. if (isset($data->{$key})) {
  119. $this->{$key} = $data->{$key};
  120. }
  121. }
  122. }
  123. }
  124. function __get($name)
  125. {
  126. $f = array($this, "get_$name");
  127. return call_user_func($f);
  128. }
  129. function __isset($name)
  130. {
  131. $f = array($this, "get_$name");
  132. return is_callable($f);
  133. }
  134. function __set($name, $value)
  135. {
  136. $f = array($this, "set_$name");
  137. if (!is_callable($f)) {
  138. return;
  139. }
  140. call_user_func($f, $value);
  141. }
  142. // /**
  143. // * Insert the course description object into the course_description table.
  144. // *
  145. // * @return bool True on success, false on failure
  146. // */
  147. // public function insert()
  148. // {
  149. // $course_id = $this->get_c_id();
  150. // $description_type = $this->get_description_type();
  151. // $title = Database::escape_string($this->get_title());
  152. // $content = Database::escape_string($this->get_content());
  153. // $progress = $this->get_progress();
  154. // $progress = $progress ? $progress : '0';
  155. // $session_id = $this->get_session_id();
  156. //
  157. // $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  158. // $sql = "INSERT IGNORE INTO $table SET
  159. // c_id = $course_id,
  160. // description_type = $description_type,
  161. // title = '$title',
  162. // content = '$content',
  163. // progress = $progress,
  164. // session_id = $session_id";
  165. //
  166. // Database::query($sql);
  167. //
  168. // $id = Database::insert_id();
  169. // if (empty($id)) {
  170. // return false;
  171. // }
  172. // $this->id = $id;
  173. //
  174. // /**
  175. // * @todo: course info should come from c_id
  176. // */
  177. // api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $id, 'CourseDescriptionAdded', api_get_user_id());
  178. //
  179. // return true;
  180. // }
  181. // /**
  182. // * Insert a row like history inside track_e_item_property table
  183. // *
  184. // * @param int description type
  185. // * @return int affected rows
  186. // */
  187. // public function insert_stats($description_type)
  188. // {
  189. // $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  190. // $description_id = $this->get_id_by_description_type($description_type);
  191. // $course_id = api_get_real_course_id();
  192. // $course_code = api_get_course_id();
  193. // $item_property_id = api_get_item_property_id($course_code, TOOL_COURSE_DESCRIPTION, $description_id);
  194. // $sql = "INSERT IGNORE INTO $tbl_stats_item_property SET
  195. // c_id = " . api_get_course_int_id() . ",
  196. // course_id = '$course_id',
  197. // item_property_id = '$item_property_id',
  198. // title = '" . Database::escape_string($this->title) . "',
  199. // content = '" . Database::escape_string($this->content) . "',
  200. // progress = '" . intval($this->progress) . "',
  201. // lastedit_date = '" . date('Y-m-d H:i:s') . "',
  202. // lastedit_user_id = '" . api_get_user_id() . "',
  203. // session_id = '" . intval($this->session_id) . "'";
  204. // Database::query($sql);
  205. // $affected_rows = Database::affected_rows();
  206. // return $affected_rows;
  207. // }
  208. // /**
  209. // * Update a course description object to the database.
  210. // *
  211. // * @return bool True on success, false on failure.
  212. // */
  213. // public function update()
  214. // {
  215. // $course_id = $this->get_c_id();
  216. // $id = $this->get_id();
  217. // $description_type = $this->get_description_type();
  218. // $title = Database::escape_string($this->get_title());
  219. // $content = Database::escape_string($this->get_content());
  220. // $progress = $this->get_progress();
  221. // $progress = $progress ? $progress : '0';
  222. // $session_id = $this->get_session_id();
  223. //
  224. // $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  225. // $sql = "UPDATE $table SET
  226. // title = '$title',
  227. // content = '$content',
  228. // progress = $progress
  229. // WHERE id = $id AND
  230. // c_id = $course_id ";
  231. //
  232. // Database::query($sql);
  233. // $result = (bool) Database::affected_rows();
  234. //
  235. // if ($result) {
  236. // //insert into item_property
  237. // /**
  238. // * @todo: course info should come from c_id
  239. // */
  240. // api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $this->id, 'CourseDescriptionUpdated', api_get_user_id());
  241. // }
  242. // return $result;
  243. // }
  244. // /**
  245. // * Delete a course description object from the database.
  246. // *
  247. // * @return bool True on success false on failure
  248. // */
  249. // public function delete()
  250. // {
  251. // $table = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  252. // $course_id = $this->get_c_id();
  253. // $id = $this->get_id();
  254. //
  255. // $sql = "DELETE FROM $table WHERE c_id = $course_id AND id = $id";
  256. // Database::query($sql);
  257. // $result = (bool) Database::affected_rows();
  258. // if ($result) {
  259. // /**
  260. // * @todo: should get course info from $this->c_id
  261. // */
  262. // api_item_property_update(api_get_course_info(), TOOL_COURSE_DESCRIPTION, $this->id, 'CourseDescriptionDeleted', api_get_user_id());
  263. // }
  264. // return $result;
  265. // }
  266. // /**
  267. // * Get description id by description type
  268. // * @param int description type
  269. // * @return int description id
  270. // */
  271. // public function get_id_by_description_type($description_type)
  272. // {
  273. // $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  274. // $course_id = api_get_course_int_id();
  275. //
  276. // $sql = "SELECT id FROM $tbl_course_description WHERE c_id = $course_id AND description_type = '" . intval($description_type) . "'";
  277. // $rs = Database::query($sql);
  278. // $row = Database::fetch_array($rs);
  279. // $description_id = $row['id'];
  280. // return $description_id;
  281. // }
  282. // /**
  283. // * get thematic progress in porcent for a course,
  284. // * first you must set session_id property with the object CourseDescription
  285. // * @param bool true for showing a icon about the progress, false otherwise (optional)
  286. // * @param int Description type (optional)
  287. // * @return string img html
  288. // */
  289. // public function get_progress_porcent($with_icon = false, $description_type = THEMATIC_ADVANCE)
  290. // {
  291. // $tbl_course_description = Database::get_course_table(TABLE_COURSE_DESCRIPTION);
  292. // $session_id = intval($session_id);
  293. // $course_id = api_get_course_int_id();
  294. //
  295. // $sql = "SELECT progress FROM $tbl_course_description WHERE c_id = $course_id AND description_type = '" . intval($description_type) . "' AND session_id = '" . intval($this->session_id) . "' ";
  296. // $rs = Database::query($sql);
  297. // $progress = '';
  298. // $img = '';
  299. // $title = '0%';
  300. // $image = 'level_0.png';
  301. // if (Database::num_rows($rs) > 0) {
  302. // $row = Database::fetch_array($rs);
  303. // $progress = $row['progress'] . '%';
  304. // $image = 'level_' . $row['progress'] . '.png';
  305. // }
  306. // if ($with_icon) {
  307. // $img = Display::return_icon($image, get_lang('ThematicAdvance'), array('style' => 'vertical-align:middle'));
  308. // }
  309. // $progress = $img . $progress;
  310. // return $progress;
  311. // }
  312. // /**
  313. // * Get description titles by default
  314. // * @return array
  315. // */
  316. // public function get_default_description_title()
  317. // {
  318. // $default_description_titles = array();
  319. // $default_description_titles[1] = get_lang('GeneralDescription');
  320. // $default_description_titles[2] = get_lang('Objectives');
  321. // $default_description_titles[3] = get_lang('Topics');
  322. // $default_description_titles[4] = get_lang('Methodology');
  323. // $default_description_titles[5] = get_lang('CourseMaterial');
  324. // $default_description_titles[6] = get_lang('HumanAndTechnicalResources');
  325. // $default_description_titles[7] = get_lang('Assessment');
  326. //
  327. // $default_description_titles[8] = get_lang('Other');
  328. // return $default_description_titles;
  329. // }
  330. /**
  331. * The course id.
  332. *
  333. * @see get_course() property to get access to the course object
  334. * @return int
  335. */
  336. public function get_c_id()
  337. {
  338. return $this->c_id;
  339. }
  340. /**
  341. * @return void
  342. */
  343. public function set_c_id($value)
  344. {
  345. $this->c_id = intval($value);
  346. }
  347. /**
  348. * The id of the object.
  349. *
  350. * @return int
  351. */
  352. public function get_id()
  353. {
  354. return $this->id;
  355. }
  356. /**
  357. *
  358. * @return void
  359. */
  360. public function set_id($value)
  361. {
  362. $this->id = intval($value);
  363. }
  364. /**
  365. * The title of the course description.
  366. *
  367. * @return string
  368. */
  369. public function get_title()
  370. {
  371. return $this->title;
  372. }
  373. /**
  374. *
  375. * @return void
  376. */
  377. public function set_title($title)
  378. {
  379. $this->title = $title;
  380. }
  381. /**
  382. * The content/description of the course description.
  383. * @return string
  384. */
  385. public function get_content()
  386. {
  387. return $this->content;
  388. }
  389. /**
  390. *
  391. * @return void
  392. */
  393. public function set_content($content)
  394. {
  395. $this->content = $content;
  396. }
  397. /**
  398. * The session id the object is associated with.
  399. *
  400. * @return int
  401. */
  402. public function get_session_id()
  403. {
  404. return $this->session_id;
  405. }
  406. /**
  407. *
  408. * @return void
  409. */
  410. public function set_session_id($value)
  411. {
  412. $this->session_id = intval($value);
  413. }
  414. /**
  415. * The type of the course description. Should match one of the id returns
  416. * by CourseDescription::get_types().
  417. *
  418. * @return int
  419. */
  420. public function get_description_type()
  421. {
  422. return $this->description_type;
  423. }
  424. /**
  425. *
  426. * @return void
  427. */
  428. public function set_description_type($value)
  429. {
  430. $this->description_type = intval($value);
  431. }
  432. /**
  433. * ???
  434. * @return int
  435. */
  436. public function get_progress()
  437. {
  438. return $this->progress;
  439. }
  440. /**
  441. *
  442. * @return void
  443. */
  444. public function set_progress($value)
  445. {
  446. $this->progress = intval($value);
  447. }
  448. /**
  449. * Return one type from its id
  450. *
  451. * @return \CourseDescription\CourseDescriptionType
  452. */
  453. public function get_type()
  454. {
  455. $type_id = $this->get_description_type();
  456. if ($this->type && $this->type->id == $type_id) {
  457. return $this->type;
  458. }
  459. $this->type = CourseDescriptionType::repository()->find_one_by_id($type_id);
  460. return $this->type;
  461. }
  462. /**
  463. * Returns the course object this object is associated with.
  464. * Lazy loaded from the value returned by get_c_id().
  465. * @return \Model\Course
  466. */
  467. public function get_course()
  468. {
  469. if ($this->course && $this->course->get_id() == $this->c_id) {
  470. return $this->course;
  471. }
  472. $this->course = Course::get_by_id($this->c_id);
  473. return $this->course;
  474. }
  475. /**
  476. * The item property this object is associated with.
  477. *
  478. * @return \Model\ItemProperty
  479. */
  480. public function get_item_property()
  481. {
  482. if ($this->item_property && $this->item_property->get_c_id() == $this->c_id && $this->item_property->get_ref() == $this->id) {
  483. return $this->item_property;
  484. }
  485. $this->item_property = ItemProperty::get_by_ref($this->id, TOOL_COURSE_DESCRIPTION);
  486. return $this->item_property;
  487. }
  488. }
  489. //
  490. ///**
  491. // * The common routes (urls) for course description objects:
  492. // *
  493. // * - create new course description
  494. // * - edit course description
  495. // * - delete course description
  496. // *
  497. // * @author Laurent Opprecht <laurent@opprecht.info> for the University of Geneva
  498. // * @licence /license.txt
  499. // */
  500. //class CourseDescriptionRoutes
  501. //{
  502. //
  503. // /**
  504. // *
  505. // * @return CourseDescriptionRoutes
  506. // */
  507. // public static function instance()
  508. // {
  509. // static $result = null;
  510. // if (empty($result)) {
  511. // $result = new self();
  512. // }
  513. // return $result;
  514. // }
  515. //
  516. // protected function __construct()
  517. // {
  518. // ;
  519. // }
  520. //
  521. // /**
  522. // * Returns the url used to create a new course description from a specific type.
  523. // *
  524. // * @param CourseDescriptionType $type
  525. // * @param bool $html True to html escape the url, false otherwise.
  526. // * @return string
  527. // */
  528. // function create($type = null, $html = true)
  529. // {
  530. // $type = is_object($type) ? $type->get_id() : (int) $type;
  531. //
  532. // $params = Uri::course_params();
  533. // $params['action'] = 'add';
  534. // if ($type) {
  535. // $params['description_type'] = $type;
  536. // }
  537. // $result = Chamilo::url('/main/course_description/index.php', $params, $html);
  538. // return $result;
  539. // }
  540. //
  541. // /**
  542. // * The url to edit a course description object
  543. // *
  544. // * @param CourseDescription $description
  545. // * @param bool $html True to html escape the url, false otherwise.
  546. // * @return string
  547. // */
  548. // function edit($description, $html = true)
  549. // {
  550. // $params = array();
  551. // $params['id'] = $description->get_id();
  552. // $params['cidReq'] = api_get_course_id();
  553. // $params['id_session'] = $description->get_session_id();
  554. // $params['description_type'] = $description->get_description_type();
  555. // $params['action'] = 'edit';
  556. // $result = Chamilo::url('/main/course_description/index.php', $params, $html);
  557. // return $result;
  558. // }
  559. //
  560. // /**
  561. // * The index route to list all course descriptions for the current course/session
  562. // *
  563. // * @param bool $html True to html escape the url, false otherwise.
  564. // * @return type
  565. // */
  566. // function index($html = true)
  567. // {
  568. // $params = Uri::course_params();
  569. // $result = Chamilo::url('/main/course_description/index.php', $params, $html);
  570. // return $result;
  571. // }
  572. //
  573. // /**
  574. // * Url to delete a course description object.
  575. // *
  576. // * @param CourseDescription $description
  577. // * @param bool $html True to html escape the url, false otherwise.
  578. // * @return string
  579. // */
  580. // function delete($description, $html = true)
  581. // {
  582. // $params = array();
  583. // $params['id'] = $description->get_id();
  584. // $params['cidReq'] = api_get_course_id();
  585. // $params['id_session'] = $description->get_session_id();
  586. // $params['description_type'] = $description->get_description_type();
  587. // $params['action'] = 'delete';
  588. // $result = Chamilo::url('/main/course_description/index.php', $params, $html);
  589. // return $result;
  590. // }
  591. //
  592. //}
  593. //