portfolio.class.php 15 KB

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