PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/php/lib/course/course.class.php

https://bitbucket.org/chamilo/chamilo-app-weblcms/
PHP | 1477 lines | 1127 code | 167 blank | 183 comment | 151 complexity | dc406d6b0120fc15712b260126cf7209 MD5 | raw file
  1. <?php
  2. namespace application\weblcms;
  3. use user\UserDataManager;
  4. use common\libraries\Session;
  5. use common\libraries\Utilities;
  6. use common\libraries\ObjectTableOrder;
  7. use common\libraries\EqualityCondition;
  8. use common\libraries\Path;
  9. use common\libraries\DataClass;
  10. use common\libraries\Translation;
  11. /**
  12. * $Id: course.class.php 216 2009-11-13 14:08:06Z kariboe $
  13. * @package application.lib.weblcms.course
  14. */
  15. /**
  16. * This class represents a course in the weblcms.
  17. *
  18. * courses have a number of default properties:
  19. * - id: the numeric ID of the course object;
  20. * - visual: the visual code of the course;
  21. * - name: the name of the course object;
  22. * - path: the course's path;
  23. * - titular: the titular of this course object;
  24. * - language: the language of the course object;
  25. * - extlink url: the URL department;
  26. * - extlink name: the name of the department;
  27. * - category code: the category code of the object;
  28. * - category name: the name of the category;
  29. *
  30. * To access the values of the properties, this class and its subclasses
  31. * should provide accessor methods. The names of the properties should be
  32. * defined as class constants, for standardization purposes. It is recommended
  33. * that the names of these constants start with the string "PROPERTY_".
  34. *
  35. */
  36. class Course extends DataClass
  37. {
  38. const CLASS_NAME = __CLASS__;
  39. const PROPERTY_COURSE_TYPE_ID = 'course_type_id';
  40. const PROPERTY_VISUAL = 'visual_code';
  41. const PROPERTY_NAME = 'title';
  42. const PROPERTY_TITULAR = 'titular_id';
  43. const PROPERTY_EXTERNAL_URL = 'external_url';
  44. const PROPERTY_EXTERNAL_NAME = 'external_name';
  45. const PROPERTY_CATEGORY = 'category_id';
  46. // Remnants from the old Chamilo system
  47. const PROPERTY_LAST_VISIT = 'last_visit';
  48. const PROPERTY_LAST_EDIT = 'last_edit';
  49. const PROPERTY_CREATION_DATE = 'creation_date';
  50. const PROPERTY_EXPIRATION_DATE = 'expiration_date';
  51. private $settings;
  52. private $layout;
  53. private $tools;
  54. private $rights;
  55. private $request;
  56. private $course_type = false;
  57. function __construct($defaultProperties = array(), $optionalProperties = array())
  58. {
  59. parent :: __construct($defaultProperties, $optionalProperties);
  60. }
  61. /**
  62. * Get the default properties of all courses.
  63. * @return array The property names.
  64. */
  65. static function get_default_property_names($extended_property_names = array())
  66. {
  67. return parent :: get_default_property_names(array(
  68. self :: PROPERTY_COURSE_TYPE_ID, self :: PROPERTY_VISUAL, self :: PROPERTY_CATEGORY, self :: PROPERTY_NAME, self :: PROPERTY_TITULAR, self :: PROPERTY_EXTERNAL_URL, self :: PROPERTY_EXTERNAL_NAME,
  69. self :: PROPERTY_CREATION_DATE, self :: PROPERTY_EXPIRATION_DATE, self :: PROPERTY_LAST_EDIT, self :: PROPERTY_LAST_VISIT));
  70. }
  71. /**
  72. * inherited
  73. */
  74. function get_data_manager()
  75. {
  76. return WeblcmsDataManager :: get_instance();
  77. }
  78. /**
  79. * Returns the course type id of this course object
  80. * @return string the course type id
  81. */
  82. function get_course_type_id()
  83. {
  84. return $this->get_default_property(self :: PROPERTY_COURSE_TYPE_ID);
  85. }
  86. /**
  87. * Returns the visual code of this course object
  88. * @return string the visual code
  89. */
  90. function get_visual()
  91. {
  92. return $this->get_default_property(self :: PROPERTY_VISUAL);
  93. }
  94. /**
  95. * Returns the category code of this course object
  96. * @return string the category code
  97. */
  98. function get_category()
  99. {
  100. return $this->get_default_property(self :: PROPERTY_CATEGORY);
  101. }
  102. /**
  103. * Returns the name (Title) of this course object
  104. * @return string The Name
  105. */
  106. function get_name()
  107. {
  108. return $this->get_default_property(self :: PROPERTY_NAME);
  109. }
  110. /**
  111. * Returns the titular of this course object
  112. * @return String The Titular
  113. */
  114. function get_titular()
  115. {
  116. return $this->get_default_property(self :: PROPERTY_TITULAR);
  117. }
  118. /**
  119. * Returns the titular as a string
  120. */
  121. function get_titular_string()
  122. {
  123. $titular_id = $this->get_titular();
  124. if (!is_null($titular_id))
  125. {
  126. $udm = UserDataManager :: get_instance();
  127. $user = $udm->retrieve_user($titular_id);
  128. return $user->get_lastname() . ' ' . $user->get_firstname();
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. /**
  136. * Returns the ext url of this course object
  137. * @return String The URL
  138. */
  139. function get_external_url()
  140. {
  141. return $this->get_default_property(self :: PROPERTY_EXTERNAL_URL);
  142. }
  143. /**
  144. * Returns the ext link name of this course object
  145. * @return String The Name
  146. */
  147. function get_external_name()
  148. {
  149. return $this->get_default_property(self :: PROPERTY_EXTERNAL_NAME);
  150. }
  151. function get_creation_date()
  152. {
  153. return $this->get_default_property(self :: PROPERTY_CREATION_DATE);
  154. }
  155. function get_expiration_date()
  156. {
  157. return $this->get_default_property(self :: PROPERTY_EXPIRATION_DATE);
  158. }
  159. function get_last_edit()
  160. {
  161. return $this->get_default_property(self :: PROPERTY_LAST_EDIT);
  162. }
  163. function get_last_visit()
  164. {
  165. return $this->get_default_property(self :: PROPERTY_LAST_VISIT);
  166. }
  167. function get_settings()
  168. {
  169. if (is_null($this->settings))
  170. {
  171. $settings = $this->get_data_manager()->retrieve_course_settings($this->get_id());
  172. if (empty($settings))
  173. {
  174. $settings = new CourseSettings();
  175. $settings->set_course_id($this->get_id());
  176. if (!is_null($this->get_id()))
  177. $settings->create();
  178. }
  179. $this->set_settings($settings);
  180. }
  181. return $this->settings;
  182. }
  183. function get_request()
  184. {
  185. return $this->request;
  186. }
  187. function get_layout_settings()
  188. {
  189. if (is_null($this->layout))
  190. {
  191. $layout = $this->get_data_manager()->retrieve_course_layout($this->get_id());
  192. if (empty($layout))
  193. {
  194. $layout = new CourseLayout();
  195. $layout->set_course_id($this->get_id());
  196. if (!is_null($this->get_id()))
  197. $layout->create();
  198. }
  199. $this->set_layout_settings($layout);
  200. }
  201. return $this->layout;
  202. }
  203. function get_tools($require = true)
  204. {
  205. if (!$this->tools)
  206. {
  207. $wdm = WeblcmsDataManager :: get_instance();
  208. $this->tools = $wdm->get_course_modules($this->get_id());
  209. if ($require)
  210. {
  211. foreach ($this->tools as $index => $tool)
  212. {
  213. require_once Path :: get_application_path() . 'weblcms/tool/' . $tool->name . '/php/' . $tool->name . '_tool.class.php';
  214. }
  215. }
  216. }
  217. return $this->tools;
  218. }
  219. function get_rights()
  220. {
  221. if (is_null($this->rights))
  222. {
  223. $rights = $this->get_data_manager()->retrieve_course_rights($this->get_id());
  224. if (empty($rights))
  225. {
  226. $rights = new CourseRights();
  227. $rights->set_course_id($this->get_id());
  228. if (!is_null($this->get_id()))
  229. $rights->create();
  230. }
  231. $this->set_rights($rights);
  232. }
  233. return $this->rights;
  234. }
  235. function get_course_type()
  236. {
  237. if ($this->course_type === false)
  238. {
  239. $course_type = $this->get_data_manager()->retrieve_course_type($this->get_course_type_id());
  240. if (empty($course_type))
  241. {
  242. $course_type = NULL;
  243. }
  244. $this->set_course_type($course_type);
  245. }
  246. return $this->course_type;
  247. }
  248. /**
  249. * Sets the course type id of this course object
  250. * @param int $type The course type id
  251. */
  252. function set_course_type_id($type)
  253. {
  254. $this->set_default_property(self :: PROPERTY_COURSE_TYPE_ID, $type);
  255. }
  256. /**
  257. * Sets the visual code of this course object
  258. * @param String $visual The visual code
  259. */
  260. function set_visual($visual)
  261. {
  262. $this->set_default_property(self :: PROPERTY_VISUAL, $visual);
  263. }
  264. /**
  265. * Sets the category code of this course object
  266. * @param String $visual The category code
  267. */
  268. function set_category($category)
  269. {
  270. $this->set_default_property(self :: PROPERTY_CATEGORY, $category);
  271. }
  272. /**
  273. * Sets the course name of this course object
  274. * @param String $name The name of this course object
  275. */
  276. function set_name($name)
  277. {
  278. $this->set_default_property(self :: PROPERTY_NAME, $name);
  279. }
  280. /**
  281. * Sets the titular of this course object
  282. * @param String $titular The titular of this course object
  283. */
  284. function set_titular($titular)
  285. {
  286. $this->set_default_property(self :: PROPERTY_TITULAR, $titular);
  287. }
  288. /**
  289. * Sets the extlink URL of this course object
  290. * @param String $url The URL if the extlink
  291. */
  292. function set_external_url($url)
  293. {
  294. if(!empty($url))
  295. {
  296. $url = self :: complete_url($url);
  297. }
  298. $this->set_default_property(self :: PROPERTY_EXTERNAL_URL, $url);
  299. }
  300. /**
  301. * Sets the extlink Name of this course object
  302. * @param String $name The name of the exlink
  303. */
  304. function set_external_name($name)
  305. {
  306. $this->set_default_property(self :: PROPERTY_EXTERNAL_NAME, $name);
  307. }
  308. function set_creation_date($creation_date)
  309. {
  310. $this->set_default_property(self :: PROPERTY_CREATION_DATE, $creation_date);
  311. }
  312. function set_expiration_date($expiration_date)
  313. {
  314. $this->set_default_property(self :: PROPERTY_EXPIRATION_DATE, $expiration_date);
  315. }
  316. function set_last_edit($last_edit)
  317. {
  318. $this->set_default_property(self :: PROPERTY_LAST_EDIT, $last_edit);
  319. }
  320. function set_last_visit($last_visit)
  321. {
  322. $this->set_default_property(self :: PROPERTY_LAST_VISIT, $last_visit);
  323. }
  324. /**
  325. * Sets the settings of this course object
  326. * @param CourseSettings $settings the settings of this course object
  327. */
  328. function set_settings($settings)
  329. {
  330. $this->settings = $settings;
  331. }
  332. function set_request($request)
  333. {
  334. $this->request = $request;
  335. }
  336. /**
  337. * Sets the layout of this course object
  338. * @param CourseLayout $layout the layout of this course object
  339. */
  340. function set_layout_settings($layout)
  341. {
  342. $this->layout = $layout;
  343. }
  344. /**
  345. * Sets the tools of this course object
  346. * @param array $tools the tools of this course object
  347. */
  348. function set_tools($tools)
  349. {
  350. $this->tools = $tools;
  351. }
  352. /**
  353. * Sets the rights of this course object
  354. * @param array $rights the rights of this course object
  355. */
  356. function set_rights($rights)
  357. {
  358. $this->rights = $rights;
  359. }
  360. /**
  361. * Sets the course_type of this course object
  362. * @param array $course_type the course_type of this course object
  363. */
  364. function set_course_type($course_type)
  365. {
  366. $this->course_type = $course_type;
  367. }
  368. /**
  369. * Direct access to the setters and getters for the course settings
  370. * All setters include a validation to see whether or not the property is writeable
  371. */
  372. /*
  373. * Getters and validation whether or not the property is readable from the course's own settings
  374. */
  375. function get_language()
  376. {
  377. if (!$this->get_language_fixed())
  378. {
  379. return $this->get_settings()->get_language();
  380. }
  381. else
  382. return $this->get_course_type()->get_settings()->get_language();
  383. }
  384. function get_visibility()
  385. {
  386. if (!$this->get_visibility_fixed())
  387. return $this->get_settings()->get_visibility();
  388. else
  389. return $this->get_course_type()->get_settings()->get_visibility();
  390. }
  391. /**
  392. *
  393. * @return int access_type
  394. */
  395. function get_access()
  396. {
  397. if (!$this->get_access_fixed())
  398. {
  399. return $this->get_settings()->get_access();
  400. }
  401. else
  402. {
  403. return $this->get_course_type()->get_settings()->get_access();
  404. }
  405. }
  406. function get_max_number_of_members()
  407. {
  408. if (!$this->get_max_number_of_members_fixed())
  409. return $this->get_settings()->get_max_number_of_members();
  410. else
  411. return $this->get_course_type()->get_settings()->get_max_number_of_members();
  412. }
  413. /**
  414. * Setters and validation to see whether they are writable
  415. */
  416. function get_titular_fixed()
  417. {
  418. if (!is_null($this->get_course_type()))
  419. return $this->get_course_type()->get_settings()->get_titular_fixed();
  420. else
  421. return 0;
  422. }
  423. function get_language_fixed()
  424. {
  425. if (!is_null($this->get_course_type()))
  426. return $this->get_course_type()->get_settings()->get_language_fixed();
  427. else
  428. return 0;
  429. }
  430. function set_language($language)
  431. {
  432. if (!$this->get_language_fixed())
  433. $this->get_settings()->set_language($language);
  434. else
  435. $this->get_settings()->set_language($this->get_course_type()->get_settings()->get_language());
  436. }
  437. function get_visibility_fixed()
  438. {
  439. if (!is_null($this->get_course_type()))
  440. return $this->get_course_type()->get_settings()->get_visibility_fixed();
  441. else
  442. return 0;
  443. }
  444. function set_visibility($visibility)
  445. {
  446. if (!$this->get_visibility_fixed())
  447. $this->get_settings()->set_visibility($visibility);
  448. else
  449. $this->get_settings()->set_visibility($this->get_course_type()->get_settings()->get_visibility());
  450. }
  451. function get_access_fixed()
  452. {
  453. if (!is_null($this->get_course_type()))
  454. return $this->get_course_type()->get_settings()->get_access_fixed();
  455. else
  456. return 0;
  457. }
  458. function set_access($access)
  459. {
  460. if (!$this->get_access_fixed())
  461. $this->get_settings()->set_access($access);
  462. else
  463. $this->get_settings()->set_access($this->get_course_type()->get_settings()->get_access());
  464. }
  465. function get_max_number_of_members_fixed()
  466. {
  467. if (!is_null($this->get_course_type()))
  468. return $this->get_course_type()->get_settings()->get_max_number_of_members_fixed();
  469. else
  470. return 0;
  471. }
  472. function set_max_number_of_members($max_number_of_members)
  473. {
  474. if (!$this->get_max_number_of_members_fixed())
  475. $this->get_settings()->set_max_number_of_members($max_number_of_members);
  476. else
  477. $this->get_settings()->set_max_number_of_members($this->get_course_type()->get_settings()->get_max_number_of_members());
  478. }
  479. /**
  480. * Direct access to the setters and getters for the course layout
  481. * All setters include a validation to see whether or not the property is writeable
  482. */
  483. /*
  484. * Getters and validation whether or not the property is readable from the course's own settings
  485. */
  486. function get_intro_text()
  487. {
  488. if (!$this->get_intro_text_fixed())
  489. return $this->get_layout_settings()->get_intro_text();
  490. else
  491. return $this->get_course_type()->get_layout_settings()->get_intro_text();
  492. }
  493. function get_student_view()
  494. {
  495. if (!$this->get_student_view_fixed())
  496. return $this->get_layout_settings()->get_student_view();
  497. else
  498. return $this->get_course_type()->get_layout_settings()->get_student_view();
  499. }
  500. function get_layout()
  501. {
  502. if (!$this->get_layout_fixed())
  503. return $this->get_layout_settings()->get_layout();
  504. else
  505. return $this->get_course_type()->get_layout_settings()->get_layout();
  506. }
  507. function get_tool_shortcut()
  508. {
  509. if (!$this->get_tool_shortcut_fixed())
  510. return $this->get_layout_settings()->get_tool_shortcut();
  511. else
  512. return $this->get_course_type()->get_layout_settings()->get_tool_shortcut();
  513. }
  514. function get_menu()
  515. {
  516. if (!$this->get_menu_fixed())
  517. return $this->get_layout_settings()->get_menu();
  518. else
  519. return $this->get_course_type()->get_layout_settings()->get_menu();
  520. }
  521. function get_breadcrumb()
  522. {
  523. if (!$this->get_breadcrumb_fixed())
  524. return $this->get_layout_settings()->get_breadcrumb();
  525. else
  526. return $this->get_course_type()->get_layout_settings()->get_breadcrumb();
  527. }
  528. function get_feedback()
  529. {
  530. if (!$this->get_feedback_fixed())
  531. return $this->get_layout_settings()->get_feedback();
  532. else
  533. return $this->get_course_type()->get_layout_settings()->get_feedback();
  534. }
  535. function get_course_code_visible()
  536. {
  537. if (!$this->get_course_code_visible_fixed())
  538. return $this->get_layout_settings()->get_course_code_visible();
  539. else
  540. return $this->get_course_type()->get_layout_settings()->get_course_code_visible();
  541. }
  542. function get_course_manager_name_visible()
  543. {
  544. if (!$this->get_course_manager_name_visible_fixed())
  545. return $this->get_layout_settings()->get_course_manager_name_visible();
  546. else
  547. return $this->get_course_type()->get_layout_settings()->get_course_manager_name_visible();
  548. }
  549. function get_course_languages_visible()
  550. {
  551. if (!$this->get_course_languages_visible_fixed())
  552. return $this->get_layout_settings()->get_course_languages_visible();
  553. else
  554. return $this->get_course_type()->get_layout_settings()->get_course_languages_visible();
  555. }
  556. /**
  557. * Setters and validation to see whether they are writable
  558. */
  559. function get_feedback_fixed()
  560. {
  561. if (!is_null($this->get_course_type()))
  562. return $this->get_course_type()->get_layout_settings()->get_feedback_fixed();
  563. else
  564. return 0;
  565. }
  566. function set_feedback($feedback)
  567. {
  568. if (!$this->get_feedback_fixed())
  569. $this->get_layout_settings()->set_feedback($feedback);
  570. else
  571. $this->get_layout_settings()->set_feedback($this->get_course_type()->get_layout_settings()->get_feedback());
  572. }
  573. function get_layout_fixed()
  574. {
  575. if (!is_null($this->get_course_type()))
  576. return $this->get_course_type()->get_layout_settings()->get_layout_fixed();
  577. else
  578. return 0;
  579. }
  580. function set_layout($layout)
  581. {
  582. if (!$this->get_layout_fixed())
  583. $this->get_layout_settings()->set_layout($layout);
  584. else
  585. $this->get_layout_settings()->set_layout($this->get_course_type()->get_layout_settings()->get_layout());
  586. }
  587. function get_tool_shortcut_fixed()
  588. {
  589. if (!is_null($this->get_course_type()))
  590. return $this->get_course_type()->get_layout_settings()->get_tool_shortcut_fixed();
  591. else
  592. return 0;
  593. }
  594. function set_tool_shortcut($tool_shortcut)
  595. {
  596. if (!$this->get_tool_shortcut_fixed())
  597. $this->get_layout_settings()->set_tool_shortcut($tool_shortcut);
  598. else
  599. $this->get_layout_settings()->set_tool_shortcut($this->get_course_type()->get_layout_settings()->get_tool_shortcut());
  600. }
  601. function get_menu_fixed()
  602. {
  603. if (!is_null($this->get_course_type()))
  604. return $this->get_course_type()->get_layout_settings()->get_menu_fixed();
  605. else
  606. return 0;
  607. }
  608. function set_menu($menu)
  609. {
  610. if (!$this->get_menu_fixed())
  611. $this->get_layout_settings()->set_menu($menu);
  612. else
  613. $this->get_layout_settings()->set_menu($this->get_course_type()->get_layout_settings()->get_menu());
  614. }
  615. function get_breadcrumb_fixed()
  616. {
  617. if (!is_null($this->get_course_type()))
  618. return $this->get_course_type()->get_layout_settings()->get_breadcrumb_fixed();
  619. else
  620. return 0;
  621. }
  622. function set_breadcrumb($breadcrumb)
  623. {
  624. if (!$this->get_breadcrumb_fixed())
  625. $this->get_layout_settings()->set_breadcrumb($breadcrumb);
  626. else
  627. $this->get_layout_settings()->set_breadcrumb($this->get_course_type()->get_layout_settings()->get_breadcrumb());
  628. }
  629. function get_intro_text_fixed()
  630. {
  631. if (!is_null($this->get_course_type()))
  632. return $this->get_course_type()->get_layout_settings()->get_intro_text_fixed();
  633. else
  634. return 0;
  635. }
  636. function set_intro_text($intro_text)
  637. {
  638. if (!$this->get_intro_text_fixed())
  639. $this->get_layout_settings()->set_intro_text($intro_text);
  640. else
  641. $this->get_layout_settings()->set_intro_text($this->get_course_type()->get_layout_settings()->get_intro_text());
  642. }
  643. function get_student_view_fixed()
  644. {
  645. if (!is_null($this->get_course_type()))
  646. return $this->get_course_type()->get_layout_settings()->get_student_view_fixed();
  647. else
  648. return 0;
  649. }
  650. function set_student_view($student_view)
  651. {
  652. if (!$this->get_student_view_fixed())
  653. $this->get_layout_settings()->set_student_view($student_view);
  654. else
  655. $this->get_layout_settings()->set_student_view($this->get_course_type()->get_layout_settings()->get_student_view());
  656. }
  657. function get_course_code_visible_fixed()
  658. {
  659. if (!is_null($this->get_course_type()))
  660. return $this->get_course_type()->get_layout_settings()->get_course_code_visible_fixed();
  661. else
  662. return 0;
  663. }
  664. function set_course_code_visible($course_code_visible)
  665. {
  666. if (!$this->get_course_code_visible_fixed())
  667. $this->get_layout_settings()->set_course_code_visible($course_code_visible);
  668. else
  669. $this->get_layout_settings()->set_course_code_visible($this->get_course_type()->get_layout_settings()->get_course_code_visible());
  670. }
  671. function get_course_manager_name_visible_fixed()
  672. {
  673. if (!is_null($this->get_course_type()))
  674. return $this->get_course_type()->get_layout_settings()->get_course_manager_name_visible_fixed();
  675. else
  676. return 0;
  677. }
  678. function set_course_manager_name_visible($course_manager_name_visible)
  679. {
  680. if (!$this->get_course_manager_name_visible_fixed())
  681. $this->get_layout_settings()->set_course_manager_name_visible($course_manager_name_visible);
  682. else
  683. $this->get_layout_settings()->set_course_manager_name_visible($this->get_course_type()->get_layout_settings()->get_course_manager_name_visible());
  684. }
  685. function get_course_languages_visible_fixed()
  686. {
  687. if (!is_null($this->get_course_type()))
  688. return $this->get_course_type()->get_layout_settings()->get_course_languages_visible_fixed();
  689. else
  690. return 0;
  691. }
  692. function set_course_languages_visible($course_languages_visible)
  693. {
  694. if (!$this->get_course_languages_visible_fixed())
  695. $this->get_layout_settings()->set_course_languages_visible($course_languages_visible);
  696. else
  697. $this->get_layout_settings()->set_course_languages_visible($this->get_course_type()->get_layout_settings()->get_course_languages_visible());
  698. }
  699. /**
  700. * Direct access to the setters and getters for the rights settings
  701. * All setters include a validation to see whether or not the property is writeable
  702. */
  703. /*
  704. * Getters and validation whether or not the property is readable from the course's own settings
  705. */
  706. function can_user_subscribe($user)
  707. {
  708. $max_members = $this->get_max_number_of_members();
  709. if ($max_members != 0)
  710. {
  711. $subscribed_users = $this->has_subscribed_users();
  712. if ($subscribed_users >= $max_members)
  713. {
  714. return CourseGroupSubscribeRight :: SUBSCRIBE_NONE;
  715. }
  716. }
  717. $current_right = $this->can_group_subscribe(0);
  718. $group_ids = $user->get_groups(true);
  719. foreach ($group_ids as $group_id)
  720. {
  721. $right = $this->can_group_subscribe($group_id);
  722. if ($right > $current_right)
  723. $current_right = $right;
  724. }
  725. return $current_right;
  726. }
  727. function can_user_unsubscribe($user)
  728. {
  729. //TODO : remove 0 !!
  730. $current_right = $this->can_group_unsubscribe(0);
  731. $group_ids = $user->get_groups(true);
  732. foreach ($group_ids as $group_id)
  733. {
  734. $right = $this->can_group_unsubscribe($group_id);
  735. if ($right > $current_right)
  736. $current_right = $right;
  737. }
  738. return $current_right;
  739. }
  740. function can_group_subscribe($group_id)
  741. {
  742. $right = $this->get_rights()->can_group_subscribe($group_id);
  743. switch ($right)
  744. {
  745. case CourseGroupSubscribeRight :: SUBSCRIBE_DIRECT :
  746. if (!$this->get_direct_subscribe_available())
  747. return CourseGroupSubscribeRight :: SUBSCRIBE_NONE;
  748. break;
  749. case CourseGroupSubscribeRight :: SUBSCRIBE_REQUEST :
  750. if (!$this->get_request_subscribe_available())
  751. return CourseGroupSubscribeRight :: SUBSCRIBE_NONE;
  752. break;
  753. case CourseGroupSubscribeRight :: SUBSCRIBE_CODE :
  754. if (!$this->get_code_subscribe_available())
  755. return CourseGroupSubscribeRight :: SUBSCRIBE_NONE;
  756. break;
  757. default :
  758. return CourseGroupSubscribeRight :: SUBSCRIBE_NONE;
  759. }
  760. return $right;
  761. }
  762. function can_group_unsubscribe($group_id)
  763. {
  764. if ($this->get_unsubscribe_available())
  765. return $this->get_rights()->can_group_unsubscribe($group_id);
  766. else
  767. return 0;
  768. }
  769. function get_code()
  770. {
  771. return $this->get_rights()->get_code();
  772. }
  773. function get_direct_subscribe_available()
  774. {
  775. if (!$this->get_direct_subscribe_fixed())
  776. return $this->get_rights()->get_direct_subscribe_available();
  777. else
  778. return $this->get_course_type()->get_rights()->get_direct_subscribe_available();
  779. }
  780. function get_request_subscribe_available()
  781. {
  782. if (!$this->get_request_subscribe_fixed())
  783. return $this->get_rights()->get_request_subscribe_available();
  784. else
  785. return $this->get_course_type()->get_rights()->get_request_subscribe_available();
  786. }
  787. function get_code_subscribe_available()
  788. {
  789. if (!$this->get_code_subscribe_fixed())
  790. return $this->get_rights()->get_code_subscribe_available();
  791. else
  792. return $this->get_course_type()->get_rights()->get_code_subscribe_available();
  793. }
  794. function get_unsubscribe_available()
  795. {
  796. if (!$this->get_unsubscribe_fixed())
  797. return $this->get_rights()->get_unsubscribe_available();
  798. else
  799. return $this->get_course_type()->get_rights()->get_unsubscribe_available();
  800. }
  801. /**
  802. * Setters and validation to see whether they are writable
  803. */
  804. function set_code($code)
  805. {
  806. if ($this->get_code_subscribe_available())
  807. $this->get_rights()->set_code($code);
  808. else
  809. $this->get_rights()->set_code(null);
  810. }
  811. function get_direct_subscribe_fixed()
  812. {
  813. if (!is_null($this->get_course_type()))
  814. return $this->get_course_type()->get_rights()->get_direct_subscribe_fixed();
  815. else
  816. return 0;
  817. }
  818. function set_direct_subscribe_available($direct)
  819. {
  820. if (!$this->get_direct_subscribe_fixed())
  821. $this->get_rights()->set_direct_subscribe_available($direct);
  822. else
  823. $this->get_rights()->set_direct_subscribe_available($this->get_course_type()->get_rights()->get_direct_subscribe_available());
  824. }
  825. function get_request_subscribe_fixed()
  826. {
  827. if (!is_null($this->get_course_type()))
  828. return $this->get_course_type()->get_rights()->get_request_subscribe_fixed();
  829. else
  830. return 0;
  831. }
  832. function set_request_subscribe_available($request)
  833. {
  834. if (!$this->get_request_subscribe_fixed())
  835. $this->get_rights()->set_request_subscribe_available($request);
  836. else
  837. $this->get_rights()->set_request_subscribe_available($this->get_course_type()->get_rights()->get_request_subscribe_available());
  838. }
  839. function get_code_subscribe_fixed()
  840. {
  841. if (!is_null($this->get_course_type()))
  842. return $this->get_course_type()->get_rights()->get_code_subscribe_fixed();
  843. else
  844. return 0;
  845. }
  846. function set_code_subscribe_available($code)
  847. {
  848. if (!$this->get_code_subscribe_fixed())
  849. $this->get_rights()->set_code_subscribe_available($code);
  850. else
  851. $this->get_rights()->set_code_subscribe_available($this->get_course_type()->get_rights()->get_code_subscribe_available());
  852. }
  853. function get_unsubscribe_fixed()
  854. {
  855. if (!is_null($this->get_course_type()))
  856. return $this->get_course_type()->get_rights()->get_unsubscribe_fixed();
  857. else
  858. return 0;
  859. }
  860. function set_unsubscribe_available($code)
  861. {
  862. if (!$this->get_unsubscribe_fixed())
  863. $this->get_rights()->set_unsubscribe_available($code);
  864. else
  865. $this->get_rights()->set_unsubscribe_available($this->get_course_type()->get_rights()->get_unsubscribe_available());
  866. }
  867. /**
  868. * Creates the course object in persistent storage
  869. * @return boolean
  870. */
  871. function create($automated_values = true, $create_in_batch = false)
  872. {
  873. if ($automated_values)
  874. {
  875. $now = time();
  876. $this->set_last_visit($now);
  877. $this->set_last_edit($now);
  878. $this->set_creation_date($now);
  879. $this->set_expiration_date($now);
  880. }
  881. $wdm = WeblcmsDataManager :: get_instance();
  882. if (!$wdm->create_course($this))
  883. {
  884. return false;
  885. }
  886. $settings = $this->get_settings();
  887. $settings->set_course_id($this->get_id());
  888. if (!$settings->create())
  889. {
  890. return false;
  891. }
  892. $layout = $this->get_layout_settings();
  893. $layout->set_course_id($this->get_id());
  894. if (!$layout->create())
  895. {
  896. return false;
  897. }
  898. $rights = $this->get_rights();
  899. $rights->set_course_id($this->get_id());
  900. if (!$rights->create())
  901. {
  902. return false;
  903. }
  904. if (!$this->initialize_course_sections())
  905. {
  906. return false;
  907. }
  908. if (!$this->create_location($create_in_batch))
  909. {
  910. return false;
  911. }
  912. if (!$this->tools)
  913. {
  914. $course_type_id = $this->get_course_type_id();
  915. if (!empty($course_type_id))
  916. {
  917. $this->tools = CourseModule :: convert_tools($this->get_course_type()->get_tools(), $this->get_id(), true);
  918. }
  919. else
  920. {
  921. $this->tools = CourseModule :: convert_tools(WeblcmsDataManager :: get_tools('basic'), $this->get_id());
  922. }
  923. }
  924. else
  925. {
  926. foreach ($this->tools as $tool)
  927. {
  928. $tool->set_course_code($this->get_id());
  929. }
  930. }
  931. if (!$wdm->create_course_modules($this->tools, $this->get_id()))
  932. {
  933. return false;
  934. }
  935. foreach ($this->tools as $tool)
  936. {
  937. if ($tool->get_name() == 'document')
  938. {
  939. require_once (dirname(__FILE__) . '/../category_manager/content_object_publication_category.class.php');
  940. $dropbox = new ContentObjectPublicationCategory();
  941. $dropbox->create_dropbox($this->get_id());
  942. }
  943. }
  944. if (!$this->create_root_course_group())
  945. {
  946. return false;
  947. }
  948. //give everyone (subscribed in course) the view right
  949. if(!WeblcmsRights :: get_instance()->invert_location_entity_right(WeblcmsManager::APPLICATION_NAME,
  950. WeblcmsRights::VIEW_RIGHT, 0, 0,
  951. WeblcmsRights :: get_instance()->get_courses_subtree_root_id($this->get_id())))
  952. {
  953. return false;
  954. }
  955. return true;
  956. }
  957. function create_all()
  958. {
  959. return $this->create(false);
  960. }
  961. function create_location($create_in_batch = false)
  962. {
  963. $parent_id = WeblcmsRights :: get_instance()->get_weblcms_location_id_by_identifier(WeblcmsRights :: TYPE_CATEGORY, $this->get_category());
  964. if (!$parent_id)
  965. {
  966. $parent_id = WeblcmsRights :: get_instance()->get_courses_subtree_root_id(0);
  967. }
  968. $succes = WeblcmsRights :: get_instance()->create_location_in_courses_subtree(WeblcmsRights :: TYPE_COURSE,
  969. $this->get_id(), $parent_id, 0, $create_in_batch);
  970. if (!$succes)
  971. {
  972. return false;
  973. }
  974. return WeblcmsRights::get_instance()-> create_subtree_root_location(
  975. $this->get_id(), WeblcmsRights :: TREE_TYPE_COURSE);
  976. }
  977. function delete()
  978. {
  979. $location = WeblcmsRights :: get_instance()->get_courses_subtree_root($this->get_id());
  980. if ($location)
  981. {
  982. if (!$location->delete())
  983. {
  984. return false;
  985. }
  986. }
  987. $dm = $this->get_data_manager();
  988. return $dm->delete_course($this->get_id());
  989. }
  990. private $is_course_admin_cache;
  991. /**
  992. * Checks whether the given user is a course admin in this course
  993. * @param int $user_id
  994. * @return boolean
  995. */
  996. function is_course_admin($user)
  997. {
  998. if (is_null($this->is_course_admin_cache[$user->get_id()]))
  999. {
  1000. $studentview = Session :: retrieve('studentview');
  1001. if ($studentview)
  1002. {
  1003. return false;
  1004. }
  1005. if ($user->is_platform_admin())
  1006. {
  1007. return true;
  1008. }
  1009. $wdm = WeblcmsDataManager :: get_instance();
  1010. if ($wdm->is_course_admin($this, $user->get_id()))
  1011. {
  1012. $this->is_course_admin_cache[$user->get_id()] = true;
  1013. }
  1014. else
  1015. $this->is_course_admin_cache[$user->get_id()] = WeblcmsDataManager :: is_teacher_through_platform_groups($this, $user);
  1016. }
  1017. return $this->is_course_admin_cache[$user->get_id()];
  1018. }
  1019. private $course_admin_users_cache;
  1020. private $course_admin_groups_cache;
  1021. /**
  1022. * Retrieves the course admins for this course
  1023. * @return Array
  1024. */
  1025. function get_course_admin_users()
  1026. {
  1027. if(!$this->course_admin_users_cache)
  1028. {
  1029. $this->course_admin_users_cache = WeblcmsDataManager :: retrieve_course_admin_users($this->get_id());
  1030. }
  1031. return $this->course_admin_users_cache;
  1032. }
  1033. function get_course_admin_groups()
  1034. {
  1035. if(!$this->course_admin_groups_cache)
  1036. {
  1037. $this->course_admin_groups_cache = WeblcmsDataManager :: retrieve_course_admin_groups($this->get_id());
  1038. }
  1039. return $this->course_admin_groups_cache;
  1040. }
  1041. /**
  1042. * Determines if this course has a theme
  1043. * @return boolean
  1044. */
  1045. function has_theme()
  1046. {
  1047. return (!is_null($this->get_layout()->get_theme()) ? true : false);
  1048. }
  1049. function has_subscribed_users()
  1050. {
  1051. $relation_condition = new EqualityCondition(CourseUserRelation :: PROPERTY_COURSE, $this->get_id());
  1052. return $this->get_data_manager()->count_course_user_relations($relation_condition);
  1053. }
  1054. /**
  1055. * Gets the subscribed users of this course
  1056. * @return array An array of CourseUserRelation objects
  1057. */
  1058. function get_subscribed_users()
  1059. {
  1060. $relation_condition = new EqualityCondition(CourseUserRelation :: PROPERTY_COURSE, $this->get_id());
  1061. return $this->get_data_manager()->retrieve_course_user_relations($relation_condition)->as_array();
  1062. }
  1063. function has_subscribed_groups()
  1064. {
  1065. $relation_condition = new EqualityCondition(CourseGroupRelation :: PROPERTY_COURSE_ID, $this->get_id());
  1066. return $this->get_data_manager()->count_course_group_relations($relation_condition);
  1067. }
  1068. /**
  1069. * Gets the subscribed groups of this course
  1070. * @return array An array of CourseGroupRelation objects
  1071. */
  1072. function get_subscribed_groups()
  1073. {
  1074. $relation_condition = new EqualityCondition(CourseGroupRelation :: PROPERTY_COURSE_ID, $this->get_id());
  1075. return $this->get_data_manager()->retrieve_course_group_relations($relation_condition)->as_array();
  1076. }
  1077. /**
  1078. * Gets the course_groups defined in this course
  1079. * @return array An array of CourseGroup objects
  1080. */
  1081. function get_course_groups($as_array = true)
  1082. {
  1083. $wdm = WeblcmsDataManager :: get_instance();
  1084. $condition = new EqualityCondition(CourseGroup :: PROPERTY_COURSE_CODE, $this->get_id());
  1085. $result = $wdm->retrieve_course_groups($condition, null, null, array(new ObjectTableOrder(CourseGroup :: PROPERTY_NAME)));
  1086. return ($as_array ? $result->as_array() : $result);
  1087. }
  1088. static function get_table_name()
  1089. {
  1090. return Utilities :: get_classname_from_namespace(self :: CLASS_NAME, true);
  1091. }
  1092. function initialize_course_sections()
  1093. {
  1094. $sections = array();
  1095. $sections[] = array('name' => Translation :: get('SectionTools'), 'type' => 1, 'order' => 1);
  1096. $sections[] = array('name' => Translation :: get('SectionLinks'), 'type' => 2, 'order' => 2);
  1097. $sections[] = array('name' => Translation :: get('SectionDisabled'), 'type' => 0, 'order' => 3);
  1098. $sections[] = array('name' => Translation :: get('SectionCourseAdministration'), 'type' => 3, 'order' => 4);
  1099. foreach ($sections as $section)
  1100. {
  1101. $course_section = new CourseSection();
  1102. $course_section->set_course_code($this->get_id());
  1103. $course_section->set_name($section['name']);
  1104. $course_section->set_type($section['type']);
  1105. $course_section->set_visible(true);
  1106. if (!$course_section->create())
  1107. {
  1108. return false;
  1109. }
  1110. }
  1111. return true;
  1112. }
  1113. function create_root_course_group()
  1114. {
  1115. $group = new CourseGroup();
  1116. $group->set_course_code($this->get_id());
  1117. $group->set_name($this->get_name());
  1118. return $group->create();
  1119. }
  1120. function update_by_course_type($course_type)
  1121. {
  1122. if (is_numeric($course_type))
  1123. $course_type = $this->get_data_manager()->retrieve_course_type($course_type);
  1124. $this->course_type = $course_type;
  1125. $this->set_course_type_id($course_type->get_id());
  1126. if (!$this->update())
  1127. return false;
  1128. $this->fill_settings($course_type);
  1129. if (!$this->get_settings()->update())
  1130. return false;
  1131. $this->fill_layout_settings($course_type);
  1132. if (!$this->get_layout_settings()->update())
  1133. return false;
  1134. $this->fill_rights($course_type);
  1135. if (!$this->get_rights()->update())
  1136. return false;
  1137. $selected_tools = $course_type->get_tools();
  1138. $course_tools = $this->get_tools();
  1139. $course_modules = array();
  1140. foreach ($selected_tools as $tool)
  1141. {
  1142. $sub_validation = false;
  1143. foreach ($course_tools as $index => $course_tool)
  1144. {
  1145. if ($tool->get_name() == $course_tool->name)
  1146. {
  1147. $sub_validation = true;
  1148. unset($course_tools[$index]);
  1149. break;
  1150. }
  1151. }
  1152. if (!$sub_validation)
  1153. {
  1154. $course_module = new CourseModule();
  1155. $course_module->set_course_code($this->get_id());
  1156. $course_module->set_name($tool->get_name());
  1157. $course_module->set_visible($tool->get_visible_default());
  1158. $course_module->set_section("basic");
  1159. $course_modules[] = $course_module;
  1160. }
  1161. }
  1162. foreach ($course_tools as $tool)
  1163. {
  1164. if (!$this->get_data_manager()->delete_course_module($tool->course_id, $tool->name))
  1165. return false;
  1166. }
  1167. if (!$this->get_data_manager()->create_course_modules($course_modules, $this->get_id()))
  1168. return false;
  1169. for ($i = 0; $i < 4; $i++)
  1170. {
  1171. $method = null;
  1172. $right = null;
  1173. $course_type_rights = null;
  1174. switch ($i)
  1175. {
  1176. case 0 :
  1177. $method = get_direct_subscribe_fixed;
  1178. $right = CourseGroupSubscribeRight :: SUBSCRIBE_DIRECT;
  1179. break;
  1180. case 1 :
  1181. $method = get_request_subscribe_fixed;
  1182. $right = CourseGroupSubscribeRight :: SUBSCRIBE_REQUEST;
  1183. break;
  1184. case 2 :
  1185. $method = get_code_subscribe_fixed;
  1186. $right = CourseGroupSubscribeRight :: SUBSCRIBE_CODE;
  1187. break;
  1188. case 3 :
  1189. $method = get_unsubscribe_fixed;
  1190. $right = CourseGroupSubscribeRight :: UNSUBSCRIBE;
  1191. break;
  1192. }
  1193. if ($course_type->get_rights()->$method())
  1194. {
  1195. $course_type_rights = $this->get_data_manager()->retrieve_course_type_group_rights_by_type($course_type->get_id(), $right);
  1196. $course_rights = $this->get_data_manager()->retrieve_course_group_rights_by_type($this->get_id(), $right)->as_array();
  1197. $course_type_rights_to_add = array();
  1198. while ($course_type_right = $course_type_rights->next_result())
  1199. {
  1200. $validation = true;
  1201. foreach ($course_rights as $index => $right)
  1202. {
  1203. if ($right->get_group_id() == $course_type_right->get_group_id())
  1204. {
  1205. $validation = false;
  1206. unset($course_rights[$index]);
  1207. }
  1208. }
  1209. if ($validation)
  1210. $course_type_rights_to_add[] = $course_type_right;
  1211. }
  1212. foreach ($course_type_rights_to_add as $course_type_right)
  1213. {
  1214. if ($right != CourseGroupSubscribeRight :: UNSUBSCRIBE)
  1215. {
  1216. $course_right = CourseGroupSubscribeRight :: convert_course_type_right_to_course_right($course_type_right, $this->get_id());
  1217. $this->get_data_manager()->delete_course_group_subscribe_right($course_right);
  1218. $this->get_data_manager()->create_course_group_subscribe_right($course_right);
  1219. }
  1220. else
  1221. $this->get_data_manager()->create_course_group_unsubscribe_right(CourseGroupUnsubscribeRight :: convert_course_type_right_to_course_right($course_type_right, $this->get_id()));
  1222. }
  1223. foreach ($course_rights as $right)
  1224. {
  1225. if ($right != CourseGroupSubscribeRight :: UNSUBSCRIBE)
  1226. $this->get_data_manager()->delete_course_group_subscribe_right($right);
  1227. else
  1228. $this->get_data_manager()->delete_course_group_unsubscribe_right($right);
  1229. }
  1230. }
  1231. }
  1232. return true;
  1233. }
  1234. private function fill_settings($course_type)
  1235. {
  1236. if ($course_type->get_settings()->get_language_fixed())
  1237. $this->get_settings()->set_language($course_type->get_settings()->get_language());
  1238. if ($course_type->get_settings()->get_visibility_fixed())
  1239. $this->get_settings()->set_visibility($course_type->get_settings()->get_visibility());
  1240. if ($course_type->get_settings()->get_access_fixed())
  1241. $this->get_settings()->set_access($course_type->get_settings()->get_access());
  1242. if ($course_type->get_settings()->get_max_number_of_members_fixed())
  1243. $this->get_settings()->set_max_number_of_members($course_type->get_settings()->get_max_number_of_members());
  1244. }
  1245. private function fill_layout_settings($course_type)
  1246. {
  1247. if ($course_type->get_layout_settings()->get_intro_text_fixed())
  1248. $this->get_layout_settings()->set_intro_text($course_type->get_layout_settings()->get_intro_text());
  1249. if ($course_type->get_layout_settings()->get_student_view_fixed())
  1250. $this->get_layout_settings()->set_student_view($course_type->get_layout_settings()->get_student_view());
  1251. if ($course_type->get_layout_settings()->get_layout_fixed())
  1252. $this->get_layout_settings()->set_layout($course_type->get_layout_settings()->get_layout());
  1253. if ($course_type->get_layout_settings()->get_tool_shortcut_fixed())
  1254. $this->get_layout_settings()->set_tool_shortcut($course_type->get_layout_settings()->get_tool_shortcut());
  1255. if ($course_type->get_layout_settings()->get_menu_fixed())
  1256. $this->get_layout_settings()->set_menu($course_type->get_layout_settings()->get_menu());
  1257. if ($course_type->get_layout_settings()->get_breadcrumb_fixed())
  1258. $this->get_layout_settings()->set_breadcrumb($course_type->get_layout_settings()->get_breadcrumb());
  1259. if ($course_type->get_layout_settings()->get_feedback_fixed())
  1260. $this->get_layout_settings()->set_feedback($course_type->get_layout_settings()->get_feedback());
  1261. if ($course_type->get_layout_settings()->get_course_code_visible_fixed())
  1262. $this->get_layout_settings()->set_course_code_visible($course_type->get_layout_settings()->get_course_code_visible());
  1263. if ($course_type->get_layout_settings()->get_course_manager_name_visible_fixed())
  1264. $this->get_layout_settings()->set_course_manager_name_visible($course_type->get_layout_settings()->get_course_manager_name_visible());
  1265. if ($course_type->get_layout_settings()->get_course_languages_visible_fixed())
  1266. $this->get_layout_settings()->set_course_languages_visible($course_type->get_layout_settings()->get_course_languages_visible());
  1267. }
  1268. private function fill_rights($course_type)
  1269. {
  1270. if ($course_type->get_rights()->get_direct_subscribe_fixed())
  1271. $this->get_rights()->set_direct_subscribe_available($course_type->get_rights()->get_direct_subscribe_available());
  1272. if ($course_type->get_rights()->get_request_subscribe_fixed())
  1273. $this->get_rights()->set_request_subscribe_available($course_type->get_rights()->get_request_subscribe_available());
  1274. if ($course_type->get_rights()->get_code_subscribe_fixed())
  1275. $this->get_rights()->set_code_subscribe_available($course_type->get_rights()->get_code_subscribe_available());
  1276. if ($course_type->get_rights()->get_unsubscribe_fixed())
  1277. $this->get_rights()->set_unsubscribe_available($course_type->get_rights()->get_unsubscribe_available());
  1278. }
  1279. function update()
  1280. {
  1281. $course_group = $this->get_data_manager()->retrieve_course_group_root($this->get_id());
  1282. if($course_group)
  1283. {
  1284. if($course_group->get_name() != $this->get_name())
  1285. {
  1286. $course_group->set_name($this->get_name());
  1287. $course_group->update();
  1288. }
  1289. }
  1290. return parent :: update();
  1291. }
  1292. /**
  1293. * Validates the url, URL beginning with / are internal URL's and considered complete, URLS that contain :// are considered complete as well.
  1294. * In any other case the URL is appended with 'http://' at the beginning.
  1295. * @param String $url
  1296. * @return String completed url
  1297. */
  1298. static function complete_url($url)
  1299. {
  1300. if(substr($url, 0, 1) == '/' || strstr($url, '://'))
  1301. {
  1302. return $url;
  1303. }
  1304. else
  1305. {
  1306. return 'http://' . $url;
  1307. }
  1308. }
  1309. }
  1310. ?>