ImsLtiTool.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace Chamilo\PluginBundle\Entity\ImsLti;
  4. use Chamilo\CoreBundle\Entity\Course;
  5. use Chamilo\CoreBundle\Entity\GradebookEvaluation;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9. * Class ImsLtiTool
  10. *
  11. * @ORM\Table(name="plugin_ims_lti_tool")
  12. * @ORM\Entity()
  13. */
  14. class ImsLtiTool
  15. {
  16. /**
  17. * @var integer
  18. *
  19. * @ORM\Column(name="id", type="integer")
  20. * @ORM\Id
  21. * @ORM\GeneratedValue
  22. */
  23. protected $id;
  24. /**
  25. * @var string
  26. *
  27. * @ORM\Column(name="name", type="string")
  28. */
  29. private $name = '';
  30. /**
  31. * @var string|null
  32. *
  33. * @ORM\Column(name="description", type="text", nullable=true)
  34. */
  35. private $description = null;
  36. /**
  37. * @var string
  38. *
  39. * @ORM\Column(name="launch_url", type="string")
  40. */
  41. private $launchUrl = '';
  42. /**
  43. * @var string
  44. *
  45. * @ORM\Column(name="consumer_key", type="string", nullable=true)
  46. */
  47. private $consumerKey = '';
  48. /**
  49. * @var string
  50. *
  51. * @ORM\Column(name="shared_secret", type="string", nullable=true)
  52. */
  53. private $sharedSecret = '';
  54. /**
  55. * @var string|null
  56. *
  57. * @ORM\Column(name="custom_params", type="text", nullable=true)
  58. */
  59. private $customParams = null;
  60. /**
  61. * @var bool
  62. *
  63. * @ORM\Column(name="active_deep_linking", type="boolean", nullable=false, options={"default": false})
  64. */
  65. private $activeDeepLinking = false;
  66. /**
  67. * @var null|string
  68. *
  69. * @ORM\Column(name="privacy", type="text", nullable=true, options={"default": null})
  70. */
  71. private $privacy = null;
  72. /**
  73. * @var Course|null
  74. *
  75. * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
  76. * @ORM\JoinColumn(name="c_id", referencedColumnName="id")
  77. */
  78. private $course = null;
  79. /**
  80. * @var GradebookEvaluation|null
  81. *
  82. * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\GradebookEvaluation")
  83. * @ORM\JoinColumn(name="gradebook_eval_id", referencedColumnName="id", onDelete="SET NULL")
  84. */
  85. private $gradebookEval = null;
  86. /**
  87. * @var ImsLtiTool|null
  88. *
  89. * @ORM\ManyToOne(targetEntity="Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool", inversedBy="children")
  90. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  91. */
  92. private $parent;
  93. /**
  94. * @var ArrayCollection
  95. *
  96. * @ORM\OneToMany(targetEntity="Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool", mappedBy="parent")
  97. */
  98. private $children;
  99. /**
  100. * ImsLtiTool constructor.
  101. */
  102. public function __construct()
  103. {
  104. $this->description = null;
  105. $this->customParams = null;
  106. $this->activeDeepLinking = false;
  107. $this->course = null;
  108. $this->gradebookEval =null;
  109. $this->privacy = null;
  110. $this->children = new ArrayCollection();
  111. $this->consumerKey = null;
  112. $this->sharedSecret = null;
  113. }
  114. /**
  115. * @return int
  116. */
  117. public function getId()
  118. {
  119. return $this->id;
  120. }
  121. /**
  122. * @return string
  123. */
  124. public function getName()
  125. {
  126. return $this->name;
  127. }
  128. /**
  129. * @param string $name
  130. * @return ImsLtiTool
  131. */
  132. public function setName($name)
  133. {
  134. $this->name = $name;
  135. return $this;
  136. }
  137. /**
  138. * @return null|string
  139. */
  140. public function getDescription()
  141. {
  142. return $this->description;
  143. }
  144. /**
  145. * @param null|string $description
  146. * @return ImsLtiTool
  147. */
  148. public function setDescription($description)
  149. {
  150. $this->description = $description;
  151. return $this;
  152. }
  153. /**
  154. * @return string
  155. */
  156. public function getLaunchUrl()
  157. {
  158. return $this->launchUrl;
  159. }
  160. /**
  161. * @param string $launchUrl
  162. * @return ImsLtiTool
  163. */
  164. public function setLaunchUrl($launchUrl)
  165. {
  166. $this->launchUrl = $launchUrl;
  167. return $this;
  168. }
  169. /**
  170. * @return string
  171. */
  172. public function getConsumerKey()
  173. {
  174. return $this->consumerKey;
  175. }
  176. /**
  177. * @param string $consumerKey
  178. * @return ImsLtiTool
  179. */
  180. public function setConsumerKey($consumerKey)
  181. {
  182. $this->consumerKey = $consumerKey;
  183. return $this;
  184. }
  185. /**
  186. * @return string
  187. */
  188. public function getSharedSecret()
  189. {
  190. return $this->sharedSecret;
  191. }
  192. /**
  193. * @param string $sharedSecret
  194. * @return ImsLtiTool
  195. */
  196. public function setSharedSecret($sharedSecret)
  197. {
  198. $this->sharedSecret = $sharedSecret;
  199. return $this;
  200. }
  201. /**
  202. * @return null|string
  203. */
  204. public function getCustomParams()
  205. {
  206. return $this->customParams;
  207. }
  208. /**
  209. * @param null|string $customParams
  210. * @return ImsLtiTool
  211. */
  212. public function setCustomParams($customParams)
  213. {
  214. $this->customParams = $customParams;
  215. return $this;
  216. }
  217. /**
  218. * @return bool
  219. */
  220. public function isGlobal()
  221. {
  222. return $this->course === null;
  223. }
  224. /**
  225. * @param array $params
  226. *
  227. * @return null|string
  228. */
  229. public function encodeCustomParams(array $params)
  230. {
  231. if (empty($params)) {
  232. return null;
  233. }
  234. $pairs = [];
  235. foreach ($params as $key => $value) {
  236. $pairs[] = "$key=$value";
  237. }
  238. return implode("\n", $pairs);
  239. }
  240. /**
  241. * @return array
  242. */
  243. public function parseCustomParams()
  244. {
  245. if (empty($this->customParams)) {
  246. return [];
  247. }
  248. $params = [];
  249. $strings = explode("\n", $this->customParams);
  250. foreach ($strings as $string) {
  251. if (empty($string)) {
  252. continue;
  253. }
  254. $pairs = explode('=', $string, 2);
  255. $key = self::parseCustomKey($pairs[0]);
  256. $value = $pairs[1];
  257. $params['custom_'.$key] = $value;
  258. }
  259. return $params;
  260. }
  261. /**
  262. * Map the key from custom param.
  263. *
  264. * @param string $key
  265. *
  266. * @return string
  267. */
  268. private static function parseCustomKey($key)
  269. {
  270. $newKey = '';
  271. $key = strtolower($key);
  272. $split = str_split($key);
  273. foreach ($split as $char) {
  274. if (
  275. ($char >= 'a' && $char <= 'z') || ($char >= '0' && $char <= '9')
  276. ) {
  277. $newKey .= $char;
  278. continue;
  279. }
  280. $newKey .= '_';
  281. }
  282. return $newKey;
  283. }
  284. /**
  285. * Set activeDeepLinking.
  286. *
  287. * @param bool $activeDeepLinking
  288. *
  289. * @return ImsLtiTool
  290. */
  291. public function setActiveDeepLinking($activeDeepLinking)
  292. {
  293. $this->activeDeepLinking = $activeDeepLinking;
  294. return $this;
  295. }
  296. /**
  297. * Get activeDeepLinking.
  298. *
  299. * @return bool
  300. */
  301. public function isActiveDeepLinking()
  302. {
  303. return $this->activeDeepLinking;
  304. }
  305. /**
  306. * Get course.
  307. *
  308. * @return Course|null
  309. */
  310. public function getCourse()
  311. {
  312. return $this->course;
  313. }
  314. /**
  315. * Set course.
  316. *
  317. * @param Course|null $course
  318. *
  319. * @return ImsLtiTool
  320. */
  321. public function setCourse(Course $course = null)
  322. {
  323. $this->course = $course;
  324. return $this;
  325. }
  326. /**
  327. * Get gradebookEval.
  328. *
  329. * @return GradebookEvaluation|null
  330. */
  331. public function getGradebookEval()
  332. {
  333. return $this->gradebookEval;
  334. }
  335. /**
  336. * Set gradebookEval.
  337. *
  338. * @param GradebookEvaluation|null $gradebookEval
  339. *
  340. * @return ImsLtiTool
  341. */
  342. public function setGradebookEval($gradebookEval)
  343. {
  344. $this->gradebookEval = $gradebookEval;
  345. return $this;
  346. }
  347. /**
  348. * Get privacy.
  349. *
  350. * @return null|string
  351. */
  352. public function getPrivacy()
  353. {
  354. return $this->privacy;
  355. }
  356. /**
  357. * Set privacy.
  358. *
  359. * @param bool $shareName
  360. * @param bool $shareEmail
  361. * @param bool $sharePicture
  362. *
  363. * @return ImsLtiTool
  364. */
  365. public function setPrivacy($shareName = false, $shareEmail = false, $sharePicture = false)
  366. {
  367. $this->privacy = serialize(
  368. [
  369. 'share_name' => $shareName,
  370. 'share_email' => $shareEmail,
  371. 'share_picture' => $sharePicture,
  372. ]
  373. );
  374. return $this;
  375. }
  376. /**
  377. * @return bool
  378. */
  379. public function isSharingName()
  380. {
  381. $unserialize = $this->unserializePrivacy();
  382. return (bool) $unserialize['share_name'];
  383. }
  384. /**
  385. * @return bool
  386. */
  387. public function isSharingEmail()
  388. {
  389. $unserialize = $this->unserializePrivacy();
  390. return (bool) $unserialize['share_email'];
  391. }
  392. /**
  393. * @return bool
  394. */
  395. public function isSharingPicture()
  396. {
  397. $unserialize = $this->unserializePrivacy();
  398. return (bool) $unserialize['share_picture'];
  399. }
  400. /**
  401. * @return mixed
  402. */
  403. public function unserializePrivacy()
  404. {
  405. return \UnserializeApi::unserialize('not_allowed_classes', $this->privacy);
  406. }
  407. /**
  408. * @return ImsLtiTool|null
  409. */
  410. public function getParent()
  411. {
  412. return $this->parent;
  413. }
  414. /**
  415. * @param ImsLtiTool $parent
  416. *
  417. * @return ImsLtiTool
  418. */
  419. public function setParent(ImsLtiTool $parent)
  420. {
  421. $this->parent = $parent;
  422. $this->sharedSecret = $parent->getSharedSecret();
  423. $this->consumerKey = $parent->getConsumerKey();
  424. $this->privacy = $parent->getPrivacy();
  425. return $this;
  426. }
  427. }