portfolio.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /*
  4. * This file contains several classes related to portfolios management to avoid
  5. * having too much files under the lib/.
  6. *
  7. * Once external libraries are moved to their own directory it would be worth
  8. * moving them to their own files under a common portfolio directory.
  9. * @package chamilo.portfolio
  10. */
  11. /**
  12. * Init
  13. */
  14. use Model\Document;
  15. use Model\Course;
  16. /**
  17. * A portfolio is used to present content to other people. In most cases it is
  18. * an external application.
  19. *
  20. * From the application point of view it is an end point to which the user can send
  21. * content.
  22. *
  23. * Available portfolios are configured in /main/inc/config/portfolio.conf.php
  24. *
  25. * The Portfolio class serves as an entry point to other portfolio components:
  26. *
  27. * - portfolio controller
  28. * - portfolio share button
  29. * - portfolio action
  30. *
  31. * Note:
  32. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  33. */
  34. //class Portfolio extends Portfolio\Portfolio
  35. class Portfolio
  36. {
  37. /**
  38. * Returns all portfolios available
  39. *
  40. * @return array
  41. */
  42. public static function all()
  43. {
  44. $conf = api_get_path(SYS_CODE_PATH).'inc/conf/portfolio.conf.php';
  45. if (!is_readable($conf)) {
  46. return array();
  47. }
  48. include $conf;
  49. return isset($portfolios) ? $portfolios : array();
  50. }
  51. /**
  52. * Returns a portfolio from its name.
  53. *
  54. * @param string $name
  55. * @return Portfolio\Portfolio
  56. */
  57. public static function get($name)
  58. {
  59. $items = self::all();
  60. foreach ($items as $item) {
  61. if ($item->get_name() == $name) {
  62. return $item;
  63. }
  64. }
  65. return Portfolio\Portfolio::none();
  66. }
  67. /**
  68. * True if portfolios are enabled. False otherwise.
  69. *
  70. * @return boolean
  71. */
  72. public static function is_enabled()
  73. {
  74. if (api_is_anonymous()) {
  75. return false;
  76. }
  77. $user_id = api_get_user_id();
  78. if (empty($user_id)) {
  79. return false;
  80. }
  81. $portfolios = self::all();
  82. if (count($portfolios) == 0) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. /**
  88. * The controller for portfolio.
  89. *
  90. * @return \PortfolioController
  91. */
  92. public static function controller()
  93. {
  94. return PortfolioController::instance();
  95. }
  96. /**
  97. * Returns a share component/button.
  98. *
  99. * @param string $tool
  100. * @param int $id
  101. * @param array $attributes
  102. * @return \PortfolioShare
  103. */
  104. public static function share($tool, $id, $attributes = array())
  105. {
  106. return PortfolioShare::factory($tool, $id, $attributes);
  107. }
  108. /**
  109. * Returns the list of actions.
  110. *
  111. * @return array
  112. */
  113. public static function actions()
  114. {
  115. return PortfolioController::actions();
  116. }
  117. /**
  118. * Returns a temporary url to download files and/or folders.
  119. *
  120. * @param string|array $ids
  121. * @return string
  122. */
  123. public static function download_url($ids, $tool)
  124. {
  125. $ids = is_array($ids) ? implode(',', $ids) : $ids;
  126. $params = Uri::course_params();
  127. $params['id'] = $ids;
  128. $params[KeyAuth::PARAM_ACCESS_TOKEN] = KeyAuth::create_temp_token();
  129. $result = Uri::url("/main/$tool/file.php", $params, false);
  130. return $result;
  131. }
  132. }
  133. /**
  134. * The portfolio controller. Responsible to dispatch/process portfolio actions.
  135. *
  136. * Usage:
  137. *
  138. * if(Porfolio::contoller()->accept()){
  139. * Portfolio::controller()->run();
  140. * }
  141. *
  142. *
  143. */
  144. class PortfolioController
  145. {
  146. const PARAM_ACTION = 'action';
  147. const PARAM_ID = 'id';
  148. const PARAM_TOOL = 'tool';
  149. const PARAM_PORTFOLIO = 'portfolio';
  150. const PARAM_CONTROLLER = 'controller';
  151. const PARAM_SECURITY_TOKEN = 'sec_token';
  152. const ACTION_SHARE = 'share';
  153. const NAME = 'portfolio';
  154. /**
  155. *
  156. * @return \PortfolioController
  157. */
  158. static function instance()
  159. {
  160. static $result = null;
  161. if (empty($result)) {
  162. $result = new self();
  163. }
  164. return $result;
  165. }
  166. protected $message = '';
  167. protected function __construct()
  168. {
  169. }
  170. public static function portfolios()
  171. {
  172. return Portfolio::all();
  173. }
  174. /**
  175. * List of actions for the SortableTable.
  176. *
  177. * @return array
  178. */
  179. public static function actions()
  180. {
  181. static $result = null;
  182. if (!is_null($result)) {
  183. return $result;
  184. }
  185. $items = self::portfolios();
  186. if (empty($items)) {
  187. $result = array();
  188. return $result;
  189. }
  190. $result = array();
  191. foreach ($items as $item) {
  192. $action = PortfolioBulkAction::create($item);
  193. $result[] = $action;
  194. }
  195. return $result;
  196. }
  197. /**
  198. * Returns true if the controller accept to process the current request.
  199. * Returns false otherwise.
  200. *
  201. * @return boolean
  202. */
  203. function accept()
  204. {
  205. if (!Portfolio::is_enabled()) {
  206. return false;
  207. }
  208. $actions = self::actions();
  209. foreach ($actions as $action) {
  210. if ($action->accept()) {
  211. return true;
  212. }
  213. }
  214. if ($this->get_controller() != self::NAME) {
  215. return false;
  216. }
  217. if (!Security::check_token('get')) {
  218. return false;
  219. }
  220. $id = $this->get_id();
  221. if (empty($id)) {
  222. return false;
  223. }
  224. return $this->get_action() == self::ACTION_SHARE;
  225. }
  226. /**
  227. * Returns the value of the current controller request parameters. That is
  228. * the name of the controller which shall handle the current request.
  229. *
  230. * @return string
  231. */
  232. function get_controller()
  233. {
  234. return Request::get(self::PARAM_CONTROLLER);
  235. }
  236. /**
  237. * Returns the value of the action parameter. That is which action shall be
  238. * performed. That is share to send an object to a portfolio.
  239. *
  240. * @return string
  241. */
  242. function get_action()
  243. {
  244. $result = Request::get(self::PARAM_ACTION);
  245. return ($result == self::ACTION_SHARE) ? self::ACTION_SHARE : '';
  246. }
  247. /**
  248. * Returns the value of the id parameter: id of object to send.
  249. *
  250. * @return int
  251. */
  252. function get_id()
  253. {
  254. return (int) Request::get(self::PARAM_ID);
  255. }
  256. /**
  257. * The course code (id) to which the object belongs.
  258. *
  259. * @return string
  260. */
  261. function course_code()
  262. {
  263. return ChamiloSession::instance()->course()->code();
  264. }
  265. /**
  266. * The name of the porfolio where to send.
  267. *
  268. * @return type
  269. */
  270. function get_portfolio()
  271. {
  272. return Request::get(self::PARAM_PORTFOLIO);
  273. }
  274. /**
  275. * Name of the tool: document, work, etc. Defaults to current_course_tool.
  276. *
  277. * @global string $current_course_tool
  278. * @return string
  279. */
  280. function get_tool()
  281. {
  282. global $current_course_tool;
  283. return Request::get(self::PARAM_TOOL, $current_course_tool);
  284. }
  285. /**
  286. * Returns the end user message after running the controller..
  287. * @return string
  288. */
  289. function message()
  290. {
  291. return $this->message;
  292. }
  293. /**
  294. * Execute the controller action as required. If a registered action accept
  295. * the current request the controller calls it.
  296. *
  297. * If not action is accept the current request and current action is "share"
  298. * the controller execute the "send to portfolio" action
  299. *
  300. * @return PortfolioController
  301. */
  302. function run()
  303. {
  304. if (!$this->accept()) {
  305. return $this;
  306. }
  307. $actions = self::actions();
  308. foreach ($actions as $action) {
  309. if ($action->accept()) {
  310. return $action->run();
  311. }
  312. }
  313. $action = $this->get_action();
  314. if ($action == self::ACTION_SHARE) {
  315. $user = new \Portfolio\User();
  316. $user->email = Chamilo::user()->email();
  317. $tool = $this->get_tool();
  318. $id = $this->get_id();
  319. $url = Portfolio::download_url($id, $tool);
  320. $artefact = new Portfolio\Artefact($url);
  321. $name = $this->get_portfolio();
  322. $result = Portfolio::get($name)->send($user, $artefact);
  323. if ($result) {
  324. $this->message = Display::return_message(get_lang('SentSuccessfully'), 'normal');
  325. } else {
  326. $this->message = Display::return_message(get_lang('SentFailed'), 'error');
  327. }
  328. return $this;
  329. } else {
  330. $this->message = '';
  331. }
  332. return $this;
  333. }
  334. }
  335. /**
  336. * This component is used to display a "send to portfolio" button for a specific
  337. * object.
  338. *
  339. * Note that the component implement the __toString() magic method and can be
  340. * therefore used in situation where a string is expected: for ex echo $button.
  341. *
  342. * Usage
  343. *
  344. * $button = Portfolio::share(...);
  345. * echo $button;
  346. *
  347. */
  348. class PortfolioShare
  349. {
  350. /**
  351. * Create a "send to portfolio" button
  352. *
  353. * @param string $tool The name of the tool: document, work.
  354. * @param int $c_id The id of the course
  355. * @param int $id The id of the object
  356. * @param array $attributes Html attributes
  357. * @return \PortfolioShare
  358. */
  359. static function factory($tool, $id, $attributes = array())
  360. {
  361. $result = new self($tool, $id, $attributes);
  362. return $result;
  363. }
  364. /**
  365. * Returns the current secuirty token. Used to avoid see surfing attacks.
  366. *
  367. * @return type
  368. */
  369. static function security_token()
  370. {
  371. static $result = null;
  372. if (empty($result)) {
  373. $result = Security::get_token();
  374. }
  375. return $result;
  376. }
  377. protected $id = 0;
  378. protected $attributes = array();
  379. protected $tool = '';
  380. function __construct($tool, $id, $attributes = array())
  381. {
  382. $this->tool = $tool;
  383. $this->id = (int) $id;
  384. $this->attributes = $attributes;
  385. }
  386. /**
  387. * Object id to send
  388. * @return int
  389. */
  390. function get_id()
  391. {
  392. return $this->id;
  393. }
  394. /**
  395. * Object id to send
  396. * @return int
  397. */
  398. function get_c_id()
  399. {
  400. return $this->c_id;
  401. }
  402. /**
  403. * Html attributes.
  404. *
  405. * @return array
  406. */
  407. function get_attributes()
  408. {
  409. return $this->attributes;
  410. }
  411. /**
  412. * Name of the tool. I.e. the type of the id parameter. Can be document, work.
  413. *
  414. * @return string
  415. */
  416. function get_tool()
  417. {
  418. return $this->tool;
  419. }
  420. /**
  421. * Display the component.
  422. *
  423. * @return string
  424. */
  425. function display()
  426. {
  427. if (!Portfolio::is_enabled()) {
  428. return '';
  429. }
  430. $id = $this->id;
  431. $tool = $this->tool;
  432. $attributes = $this->attributes;
  433. $attributes['z-index'] = 100000;
  434. $s = ' ';
  435. foreach ($attributes as $key => $value) {
  436. $s .= $key . '="' . $value . '" ';
  437. }
  438. $result = array();
  439. $result[] = '<span ' . $s . ' >';
  440. $result[] = '<span class="dropdown" >';
  441. $result[] = '<a href="#" data-toggle="dropdown" class="dropdown-toggle">';
  442. $result[] = Display::return_icon('document_send.png', get_lang('Send'), array(), ICON_SIZE_SMALL) . '<b class="caret"></b>';
  443. $result[] = '</a>';
  444. $result[] = '<ul class="dropdown-menu">';
  445. $portfolios = Portfolio::all();
  446. foreach ($portfolios as $portfolio) {
  447. $parameters = Uri::course_params();
  448. $parameters[PortfolioController::PARAM_ACTION] = PortfolioController::ACTION_SHARE;
  449. $parameters[PortfolioController::PARAM_CONTROLLER] = PortfolioController::NAME;
  450. $parameters[PortfolioController::PARAM_PORTFOLIO] = $portfolio->get_name();
  451. $parameters[PortfolioController::PARAM_SECURITY_TOKEN] = self::security_token();
  452. $parameters[PortfolioController::PARAM_TOOL] = $this->get_tool();
  453. $parameters[PortfolioController::PARAM_ID] = $id;
  454. $parameters[PortfolioController::PARAM_TOOL] = $tool;
  455. $url = api_get_path(WEB_CODE_PATH).'portfolio/share.php?';
  456. $result[] = '<li>';
  457. $result[] = '<a href="' . $url . '">' . $portfolio->get_title() . '</a>';
  458. $result[] = '</li>';
  459. }
  460. $result[] = '</ul>';
  461. $result[] = '</span>';
  462. $result[] = '</span>';
  463. return implode("\n", $result);
  464. }
  465. function __toString()
  466. {
  467. return $this->display();
  468. }
  469. }
  470. /**
  471. * A "send to this portfolio" action. Actions are used by the SortableTable to
  472. * perform actions on a set of objects. An action is composed of
  473. *
  474. * - a name
  475. * - a title (displayed to the user)
  476. * - code to execute
  477. *
  478. * Usage:
  479. *
  480. * $form_actions = array();
  481. * $form_action['...'] = get_lang('...');
  482. * $portfolio_actions = Portfolio::actions();
  483. * foreach($portfolio_actions as $action){
  484. * $form_action[$action->get_name()] = $action->get_title();
  485. * }
  486. * $table->set_form_actions($form_action, 'path');
  487. *
  488. * @see SortableTable
  489. */
  490. class PortfolioBulkAction
  491. {
  492. /**
  493. *
  494. * @param \Portfolio\Portfolio $portfolio
  495. * @return PortfolioBulkAction
  496. */
  497. public static function create($portfolio)
  498. {
  499. return new self($portfolio);
  500. }
  501. protected $name = '';
  502. protected $title = '';
  503. protected $portfolio = null;
  504. /**
  505. *
  506. * @param \Portfolio\Portfolio $portfolio
  507. */
  508. public function __construct($portfolio)
  509. {
  510. $this->name = md5(__CLASS__) . '_' . $portfolio->get_name();
  511. $this->title = $portfolio->get_title() ? $portfolio->get_title() : get_lang('SendTo') . ' ' . $portfolio->get_name();
  512. $this->portfolio = $portfolio;
  513. }
  514. public function get_name()
  515. {
  516. return $this->name;
  517. }
  518. public function get_title()
  519. {
  520. return $this->title;
  521. }
  522. /**
  523. *
  524. * @return \Portfolio\Portfolio
  525. */
  526. public function get_portfolio()
  527. {
  528. return $this->portfolio;
  529. }
  530. public function accept()
  531. {
  532. $name = $this->get_name();
  533. $action = Request::get(PortfolioController::PARAM_ACTION);
  534. if ($name != $action) {
  535. return false;
  536. }
  537. $pathes = Request::get('path');
  538. if (empty($pathes)) {
  539. return false;
  540. }
  541. $course = Course::current();
  542. if (empty($course)) {
  543. return false;
  544. }
  545. return true;
  546. }
  547. public function run()
  548. {
  549. if (!$this->accept()) {
  550. return false;
  551. }
  552. $course = Course::current();
  553. $pathes = Request::get('path');
  554. $pathes = is_array($pathes) ? $pathes : array($pathes);
  555. $ids = array();
  556. foreach ($pathes as $path) {
  557. $doc = Document::get_by_path($course, $path);
  558. if ($doc) {
  559. $ids[] = $doc->get_id();
  560. }
  561. }
  562. if (empty($ids)) {
  563. return false;
  564. }
  565. $user = new \Portfolio\User();
  566. $user->email = Chamilo::user()->email();
  567. $artefact = new Portfolio\Artefact();
  568. $artefact->url = Portfolio::download_url($ids);
  569. $portfolio = $this->get_portfolio();
  570. $result = $portfolio->send($user, $artefact);
  571. return $result;
  572. }
  573. }