item_property.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. <?php
  2. namespace Model;
  3. use ResultSet;
  4. use Database;
  5. /**
  6. * Represent a database "item_property" object - i.e. common properties for tool
  7. * objects: created date, modified date, etc.
  8. *
  9. * Note:
  10. *
  11. * Each database column is mapped to a property.
  12. *
  13. *
  14. * Some db query functions exists in this class and would need to be adapted
  15. * to Sympony once it is moved to production. Yet the object structure should
  16. * stay.
  17. *
  18. * @see \Model\ItemProperty
  19. * @see table c_item_property
  20. * @license see /license.txt
  21. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  22. */
  23. class ItemProperty
  24. {
  25. const VISIBILITY_INVISIBLE = 0;
  26. const VISIBILITY_VISIBLE = 1;
  27. const VISIBILITY_DELETED = 2;
  28. /**
  29. *
  30. * @return ItemPropertyRepository
  31. */
  32. public static function repository()
  33. {
  34. return ItemPropertyRepository::instance();
  35. }
  36. /**
  37. *
  38. * @param string $where
  39. * @return \ResultSet
  40. */
  41. public static function query($where)
  42. {
  43. return self::repository()->query($where);
  44. }
  45. /**
  46. *
  47. * @param id $ref
  48. * @param string $tool
  49. * @return \Model\ItemProperty
  50. */
  51. public static function get_by_ref($ref, $tool)
  52. {
  53. return self::repository()->get_by_ref($ref, $tool);
  54. }
  55. /**
  56. *
  57. * @param array|object $data
  58. * @return \Model\ItemProperty
  59. */
  60. static function create($data)
  61. {
  62. return new self($data);
  63. }
  64. protected $c_id = 0;
  65. protected $id = 0;
  66. protected $tool = '';
  67. protected $insert_user_id = 0;
  68. protected $insert_date = 0;
  69. protected $lastedit_date = 0;
  70. protected $ref = '';
  71. protected $lastedit_type = '';
  72. protected $lastedit_user_id = 0;
  73. protected $to_group_id = null;
  74. protected $to_user_id = null;
  75. protected $visibility = self::VISIBILITY_VISIBLE;
  76. protected $start_visible = 0;
  77. protected $end_visible = 0;
  78. protected $id_session = 0;
  79. public function __construct($data)
  80. {
  81. $data = (object) $data;
  82. $this->c_id = (int) $data->c_id;
  83. $this->id = (int) $data->id;
  84. $this->tool = $data->tool;
  85. $this->insert_user_id = (int) $data->insert_user_id;
  86. $this->insert_date = is_numeric($data->insert_date) ? $data->insert_date : strtotime($data->insert_date);
  87. $this->lastedit_date = is_numeric($data->lastedit_date) ? $data->lastedit_date : strtotime($data->lastedit_date);
  88. $this->ref = (int) $data->ref;
  89. $this->lastedit_type = $data->lastedit_type;
  90. $this->lastedit_user_id = (int) $data->lastedit_user_id;
  91. $this->to_group_id = (int) $data->to_group_id;
  92. $this->to_user_id = (int) $data->to_user_id;
  93. $this->visibility = (int) $data->visibility;
  94. $this->start_visible = is_numeric($data->start_visible) ? $data->start_visible : strtotime($data->start_visible);
  95. $this->end_visible = is_numeric($data->end_visible) ? $data->end_visible : strtotime($data->end_visible);
  96. $this->id_session = $data->id_session;
  97. }
  98. /**
  99. *
  100. * @return int
  101. */
  102. public function get_c_id()
  103. {
  104. return $this->c_id;
  105. }
  106. public function set_c_id($value)
  107. {
  108. $this->c_id = (int) $value;
  109. }
  110. /**
  111. *
  112. * @return int
  113. */
  114. public function get_id()
  115. {
  116. return $this->id;
  117. }
  118. public function set_id($value)
  119. {
  120. $this->id = (int) $value;
  121. }
  122. public function get_tool()
  123. {
  124. return $this->tool;
  125. }
  126. public function set_tool($value)
  127. {
  128. $this->tool = $value;
  129. }
  130. /**
  131. *
  132. * @return int
  133. */
  134. public function get_insert_user_id()
  135. {
  136. return $this->insert_user_id;
  137. }
  138. public function set_insert_user_id($value)
  139. {
  140. $this->insert_user_id = $value;
  141. }
  142. /**
  143. *
  144. * @return int
  145. */
  146. public function get_insert_date()
  147. {
  148. return $this->insert_date;
  149. }
  150. public function set_insert_date($value)
  151. {
  152. $value = is_numeric($value) ? $value : strtotime($value);
  153. $this->insert_date = $value;
  154. }
  155. /**
  156. *
  157. * @return int
  158. */
  159. public function get_lastedit_date()
  160. {
  161. return $this->lastedit_date;
  162. }
  163. public function set_lastedit_date($value)
  164. {
  165. $value = is_numeric($value) ? $value : strtotime($value);
  166. $this->lastedit_date = $value;
  167. }
  168. /**
  169. *
  170. * @return int
  171. */
  172. public function get_ref()
  173. {
  174. return $this->ref;
  175. }
  176. public function set_ref($value)
  177. {
  178. $this->ref = $value;
  179. }
  180. /**
  181. *
  182. * @return string
  183. */
  184. public function get_lastedit_type()
  185. {
  186. return $this->lastedit_type;
  187. }
  188. public function set_lastedit_type($value)
  189. {
  190. $this->lastedit_type = $value;
  191. }
  192. /**
  193. *
  194. * @return int
  195. */
  196. public function get_lastedit_user_id()
  197. {
  198. return $this->lastedit_user_id;
  199. }
  200. public function set_lastedit_user_id($value)
  201. {
  202. $this->lastedit_user_id = $value;
  203. }
  204. /**
  205. *
  206. * @return int
  207. */
  208. public function get_to_group_id()
  209. {
  210. return $this->to_group_id;
  211. }
  212. public function set_to_group_id($value)
  213. {
  214. $this->to_group_id = $value;
  215. }
  216. /**
  217. *
  218. * @return int
  219. */
  220. public function get_to_user_id()
  221. {
  222. return $this->to_user_id;
  223. }
  224. public function set_to_user_id($value)
  225. {
  226. $this->to_user_id = $value;
  227. }
  228. /**
  229. *
  230. * @return int
  231. */
  232. public function get_visibility()
  233. {
  234. return $this->visibility;
  235. }
  236. public function set_visibility($value)
  237. {
  238. $this->visibility = $value;
  239. }
  240. /**
  241. *
  242. * @return int
  243. */
  244. public function get_start_visible()
  245. {
  246. return $this->start_visible;
  247. }
  248. public function set_start_visible($value)
  249. {
  250. $value = is_numeric($value) ? $value : strtotime($value);
  251. $this->start_visible = $value;
  252. }
  253. /**
  254. *
  255. * @return int
  256. */
  257. public function get_end_visible()
  258. {
  259. return $this->end_visible;
  260. }
  261. public function set_end_visible($value)
  262. {
  263. $value = is_numeric($value) ? $value : strtotime($value);
  264. $this->end_visible = $value;
  265. }
  266. /**
  267. *
  268. * @return int
  269. */
  270. public function get_id_session()
  271. {
  272. return $this->id_session;
  273. }
  274. public function set_id_session($value)
  275. {
  276. $this->id_session = $value;
  277. }
  278. public function mark_deleted()
  279. {
  280. $this->set_visibility(self::VISIBILITY_DELETED);
  281. $tool = $this->get_tool();
  282. $lastedit_type = str_replace('_', '', ucwords($tool)) . 'Deleted';
  283. $this->set_lastedit_type($lastedit_type);
  284. $user_id = api_get_user_id();
  285. $this->set_insert_user_id($user_id);
  286. }
  287. public function mark_visible()
  288. {
  289. $this->set_visibility(self::VISIBILITY_VISIBLE);
  290. $tool = $this->get_tool();
  291. $lastedit_type = str_replace('_', '', ucwords($tool)) . 'Visible';
  292. $this->set_lastedit_type($lastedit_type);
  293. $user_id = api_get_user_id();
  294. $this->set_insert_user_id($user_id);
  295. }
  296. public function mark_invisible()
  297. {
  298. $this->set_visibility(self::VISIBILITY_INVISIBLE);
  299. $tool = $this->get_tool();
  300. $lastedit_type = str_replace('_', '', ucwords($tool)) . 'Invisible';
  301. $this->set_lastedit_type($lastedit_type);
  302. $user_id = api_get_user_id();
  303. $this->set_insert_user_id($user_id);
  304. }
  305. }
  306. /**
  307. *
  308. */
  309. class ItemPropertyRepository
  310. {
  311. /**
  312. *
  313. * @return \Model\ItemPropertyRepository
  314. */
  315. public static function instance()
  316. {
  317. static $result = null;
  318. if (empty($result)) {
  319. $result = new self();
  320. }
  321. return $result;
  322. }
  323. /**
  324. *
  325. * @param string $where
  326. * @return \ResultSet
  327. */
  328. public function query($where)
  329. {
  330. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  331. $sql = "SELECT * FROM $table ";
  332. $sql .= $where ? "WHERE $where" : '';
  333. $result = new ResultSet($sql);
  334. return $result->return_type(__CLASS__);
  335. }
  336. /**
  337. *
  338. * @param id $ref
  339. * @param string $tool
  340. * @return \Model\ItemProperty
  341. */
  342. public function get_by_ref($ref, $tool)
  343. {
  344. return $this->query("ref=$ref AND tool = '$tool'")->first();
  345. }
  346. /**
  347. *
  348. * @param ItemProperty $item
  349. */
  350. function save($item)
  351. {
  352. if ($this->exists($item)) {
  353. $this->update($item);
  354. } else {
  355. $this->insert($item);
  356. }
  357. }
  358. /**
  359. * Returns true if item is not new, false otherwise.
  360. *
  361. * @param ItemProperty $item
  362. * @return bool
  363. */
  364. public function exists($item)
  365. {
  366. $id = $item->get_id();
  367. $c_id = $item->get_c_id();
  368. return !empty($id) && !empty($c_id);
  369. }
  370. /**
  371. *
  372. * @param ItemProperty $item
  373. * @return bool
  374. */
  375. public function insert($item)
  376. {
  377. $this->defaults($item);
  378. $user_id = api_get_user_id();
  379. $item->set_insert_user_id($user_id);
  380. $c_id = $item->get_c_id();
  381. $id = $item->get_id();
  382. $tool = Database::escape_string($item->get_tool());
  383. $insert_user_id = $item->get_insert_user_id();
  384. $insert_date = api_get_utc_datetime($item->get_insert_date());
  385. $lastedit_date = api_get_utc_datetime($item->get_lastedit_date());
  386. $ref = $item->get_ref();
  387. $lastedit_type = Database::escape_string($item->get_lastedit_type());
  388. $last_edit_user_id = $item->get_lastedit_user_id();
  389. $to_group_id = $item->get_to_group_id();
  390. $to_group_id = empty($to_group_id) ? '0' : $to_group_id;
  391. $to_user_id = $item->get_to_user_id();
  392. $to_user_id = empty($to_user_id) ? '0' : $to_user_id;
  393. $visibility = $item->get_visibility();
  394. $visibility = $visibility ? $visibility : '0';
  395. $start_visible = $item->get_start_visible();
  396. $start_visible = empty($start_visible) ? '0000-00-00 00:00:00' : api_get_utc_datetime($start_visible);
  397. $end_visible = $item->get_end_visible();
  398. $end_visible = empty($end_visible) ? '0000-00-00 00:00:00' : api_get_utc_datetime($end_visible);
  399. $session_id = $item->get_id_session();
  400. $TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  401. $sql = "INSERT INTO $TABLE_ITEMPROPERTY (
  402. c_id,
  403. tool,
  404. insert_user_id,
  405. insert_date,
  406. lastedit_date,
  407. ref,
  408. lastedit_type,
  409. lastedit_user_id,
  410. to_group_id,
  411. to_user_id,
  412. visibility,
  413. start_visible,
  414. end_visible,
  415. id_session
  416. ) VALUES (
  417. $c_id,
  418. '$tool',
  419. $insert_user_id,
  420. '$insert_date',
  421. '$lastedit_date',
  422. $ref,
  423. '$lastedit_type',
  424. $last_edit_user_id,
  425. $to_group_id,
  426. $to_user_id,
  427. $visibility,
  428. '$start_visible',
  429. '$end_visible',
  430. '$session_id'
  431. )";
  432. $result = Database::query($sql);
  433. $id = Database::insert_id();
  434. if ($id) {
  435. $item->set_id($id);
  436. }
  437. return (bool) $result;
  438. }
  439. /**
  440. *
  441. * @param ItemProperty $item
  442. */
  443. public function update($item)
  444. {
  445. $this->defaults($item);
  446. $user_id = api_get_user_id();
  447. $item->set_insert_user_id($user_id);
  448. $c_id = $item->get_c_id();
  449. $id = $item->get_id();
  450. //$tool = Database::escape_string($item->get_tool());
  451. //$insert_user_id = $item->get_insert_user_id();
  452. //$insert_date = api_get_utc_datetime($item->get_insert_date());
  453. $lastedit_date = api_get_utc_datetime($item->get_lastedit_date());
  454. //$ref = $item->get_ref();
  455. $lastedit_type = Database::escape_string($item->get_lastedit_type());
  456. $last_edit_user_id = $item->get_lastedit_user_id();
  457. $to_group_id = $item->get_to_group_id();
  458. $to_group_id = empty($to_group_id) ? '0' : $to_group_id;
  459. $to_user_id = $item->get_to_user_id();
  460. $to_user_id = empty($to_user_id) ? '0' : $to_user_id;
  461. $visibility = $item->get_visibility();
  462. $visibility = $visibility ? $visibility : '0';
  463. $start_visible = $item->get_start_visible();
  464. $start_visible = empty($start_visible) ? '0000-00-00 00:00:00' : api_get_utc_datetime($start_visible);
  465. $end_visible = $item->get_end_visible();
  466. $end_visible = empty($end_visible) ? '0000-00-00 00:00:00' : api_get_utc_datetime($end_visible);
  467. $session_id = $item->get_id_session();
  468. $TABLE = Database::get_course_table(TABLE_ITEM_PROPERTY);
  469. $sql = "UPDATE
  470. $TABLE
  471. SET
  472. lastedit_date = '$lastedit_date',
  473. lastedit_type = '$lastedit_type',
  474. lastedit_user_id = $last_edit_user_id,
  475. to_group_id = $to_group_id,
  476. to_user_id = $to_user_id,
  477. visibility = $visibility,
  478. start_visible = '$start_visible',
  479. end_visible = '$end_visible',
  480. id_session = $session_id
  481. WHERE
  482. c_id = $c_id AND
  483. id = $id";
  484. $result = Database::query($sql);
  485. return (bool) $result;
  486. }
  487. /**
  488. *
  489. * @param ItemProperty $item
  490. */
  491. function defaults($item)
  492. {
  493. $now = time();
  494. $user = api_get_user_id();
  495. $value = $item->get_insert_user_id();
  496. if (empty($value)) {
  497. $item->set_insert_user_id($user);
  498. }
  499. $value = get_insert_date();
  500. if (empty($value)) {
  501. $item->set_insert_date($now);
  502. }
  503. $value = get_lastedit_date();
  504. if (empty($value)) {
  505. $item->set_lastedit_date($now);
  506. }
  507. $value = $item->get_lastedit_user_id();
  508. if (empty($value)) {
  509. $item->set_insert_user_id($user);
  510. }
  511. $value = $item->get_id_session();
  512. if (empty($value)) {
  513. $value = api_get_session_id();
  514. $item->set_session_id($value);
  515. }
  516. }
  517. }