123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643 |
- <?php
- use Model\Document;
- use Model\Course;
- class Portfolio
- {
-
- public static function all()
- {
- $conf = api_get_path(SYS_CODE_PATH).'inc/conf/portfolio.conf.php';
- if (!is_readable($conf)) {
- return array();
- }
- include $conf;
- return isset($portfolios) ? $portfolios : array();
- }
-
- public static function get($name)
- {
- $items = self::all();
- foreach ($items as $item) {
- if ($item->get_name() == $name) {
- return $item;
- }
- }
- return Portfolio\Portfolio::none();
- }
-
- public static function is_enabled()
- {
- if (api_is_anonymous()) {
- return false;
- }
- $user_id = api_get_user_id();
- if (empty($user_id)) {
- return false;
- }
- $portfolios = self::all();
- if (count($portfolios) == 0) {
- return false;
- }
- return true;
- }
-
- public static function controller()
- {
- return PortfolioController::instance();
- }
-
- public static function share($tool, $id, $attributes = array())
- {
- return PortfolioShare::factory($tool, $id, $attributes);
- }
-
- public static function actions()
- {
- return PortfolioController::actions();
- }
-
- public static function download_url($ids, $tool)
- {
- $ids = is_array($ids) ? implode(',', $ids) : $ids;
- $params = Uri::course_params();
- $params['id'] = $ids;
- $params[KeyAuth::PARAM_ACCESS_TOKEN] = KeyAuth::create_temp_token();
- $result = Uri::url("/main/$tool/file.php", $params, false);
- return $result;
- }
- }
- class PortfolioController
- {
- const PARAM_ACTION = 'action';
- const PARAM_ID = 'id';
- const PARAM_TOOL = 'tool';
- const PARAM_PORTFOLIO = 'portfolio';
- const PARAM_CONTROLLER = 'controller';
- const PARAM_SECURITY_TOKEN = 'sec_token';
- const ACTION_SHARE = 'share';
- const NAME = 'portfolio';
-
- static function instance()
- {
- static $result = null;
- if (empty($result)) {
- $result = new self();
- }
- return $result;
- }
- protected $message = '';
- protected function __construct()
- {
- }
- public static function portfolios()
- {
- return Portfolio::all();
- }
-
- public static function actions()
- {
- static $result = null;
- if (!is_null($result)) {
- return $result;
- }
- $items = self::portfolios();
- if (empty($items)) {
- $result = array();
- return $result;
- }
- $result = array();
- foreach ($items as $item) {
- $action = PortfolioBulkAction::create($item);
- $result[] = $action;
- }
- return $result;
- }
-
- function accept()
- {
- if (!Portfolio::is_enabled()) {
- return false;
- }
- $actions = self::actions();
- foreach ($actions as $action) {
- if ($action->accept()) {
- return true;
- }
- }
- if ($this->get_controller() != self::NAME) {
- return false;
- }
- if (!Security::check_token('get')) {
- return false;
- }
- $id = $this->get_id();
- if (empty($id)) {
- return false;
- }
- return $this->get_action() == self::ACTION_SHARE;
- }
-
- function get_controller()
- {
- return Request::get(self::PARAM_CONTROLLER);
- }
-
- function get_action()
- {
- $result = Request::get(self::PARAM_ACTION);
- return ($result == self::ACTION_SHARE) ? self::ACTION_SHARE : '';
- }
-
- function get_id()
- {
- return (int) Request::get(self::PARAM_ID);
- }
-
- function course_code()
- {
- return ChamiloSession::instance()->course()->code();
- }
-
- function get_portfolio()
- {
- return Request::get(self::PARAM_PORTFOLIO);
- }
-
- function get_tool()
- {
- global $current_course_tool;
- return Request::get(self::PARAM_TOOL, $current_course_tool);
- }
-
- function message()
- {
- return $this->message;
- }
-
- function run()
- {
- if (!$this->accept()) {
- return $this;
- }
- $actions = self::actions();
- foreach ($actions as $action) {
- if ($action->accept()) {
- return $action->run();
- }
- }
- $action = $this->get_action();
- if ($action == self::ACTION_SHARE) {
- $user = new \Portfolio\User();
- $user->email = Chamilo::user()->email();
- $tool = $this->get_tool();
- $id = $this->get_id();
- $url = Portfolio::download_url($id, $tool);
- $artefact = new Portfolio\Artefact($url);
- $name = $this->get_portfolio();
- $result = Portfolio::get($name)->send($user, $artefact);
- if ($result) {
- $this->message = Display::return_message(get_lang('SentSuccessfully'), 'normal');
- } else {
- $this->message = Display::return_message(get_lang('SentFailed'), 'error');
- }
- return $this;
- } else {
- $this->message = '';
- }
- return $this;
- }
- }
- class PortfolioShare
- {
-
- static function factory($tool, $id, $attributes = array())
- {
- $result = new self($tool, $id, $attributes);
- return $result;
- }
-
- static function security_token()
- {
- static $result = null;
- if (empty($result)) {
- $result = Security::get_token();
- }
- return $result;
- }
- protected $id = 0;
- protected $attributes = array();
- protected $tool = '';
- function __construct($tool, $id, $attributes = array())
- {
- $this->tool = $tool;
- $this->id = (int) $id;
- $this->attributes = $attributes;
- }
-
- function get_id()
- {
- return $this->id;
- }
-
- function get_c_id()
- {
- return $this->c_id;
- }
-
- function get_attributes()
- {
- return $this->attributes;
- }
-
- function get_tool()
- {
- return $this->tool;
- }
-
- function display()
- {
- if (!Portfolio::is_enabled()) {
- return '';
- }
- $id = $this->id;
- $tool = $this->tool;
- $attributes = $this->attributes;
- $attributes['z-index'] = 100000;
- $s = ' ';
- foreach ($attributes as $key => $value) {
- $s .= $key.'="'.$value.'" ';
- }
- $result = array();
- $result[] = '<span '.$s.' >';
- $result[] = '<span class="dropdown" >';
- $result[] = '<a href="#" data-toggle="dropdown" class="dropdown-toggle">';
- $result[] = Display::return_icon('document_send.png', get_lang('Send'), array(), ICON_SIZE_SMALL).'<b class="caret"></b>';
- $result[] = '</a>';
- $result[] = '<ul class="dropdown-menu">';
- $portfolios = Portfolio::all();
- foreach ($portfolios as $portfolio) {
- $parameters = Uri::course_params();
- $parameters[PortfolioController::PARAM_ACTION] = PortfolioController::ACTION_SHARE;
- $parameters[PortfolioController::PARAM_CONTROLLER] = PortfolioController::NAME;
- $parameters[PortfolioController::PARAM_PORTFOLIO] = $portfolio->get_name();
- $parameters[PortfolioController::PARAM_SECURITY_TOKEN] = self::security_token();
- $parameters[PortfolioController::PARAM_TOOL] = $this->get_tool();
- $parameters[PortfolioController::PARAM_ID] = $id;
- $parameters[PortfolioController::PARAM_TOOL] = $tool;
- $url = api_get_path(WEB_CODE_PATH).'portfolio/share.php?';
- $result[] = '<li>';
- $result[] = '<a href="'.$url.'">'.$portfolio->get_title().'</a>';
- $result[] = '</li>';
- }
- $result[] = '</ul>';
- $result[] = '</span>';
- $result[] = '</span>';
- return implode("\n", $result);
- }
- function __toString()
- {
- return $this->display();
- }
- }
- class PortfolioBulkAction
- {
-
- public static function create($portfolio)
- {
- return new self($portfolio);
- }
- protected $name = '';
- protected $title = '';
- protected $portfolio = null;
-
- public function __construct($portfolio)
- {
- $this->name = md5(__CLASS__).'_'.$portfolio->get_name();
- $this->title = $portfolio->get_title() ? $portfolio->get_title() : get_lang('SendTo').' '.$portfolio->get_name();
- $this->portfolio = $portfolio;
- }
- public function get_name()
- {
- return $this->name;
- }
- public function get_title()
- {
- return $this->title;
- }
-
- public function get_portfolio()
- {
- return $this->portfolio;
- }
- public function accept()
- {
- $name = $this->get_name();
- $action = Request::get(PortfolioController::PARAM_ACTION);
- if ($name != $action) {
- return false;
- }
- $pathes = Request::get('path');
- if (empty($pathes)) {
- return false;
- }
- $course = Course::current();
- if (empty($course)) {
- return false;
- }
- return true;
- }
- public function run()
- {
- if (!$this->accept()) {
- return false;
- }
- $course = Course::current();
- $pathes = Request::get('path');
- $pathes = is_array($pathes) ? $pathes : array($pathes);
- $ids = array();
- foreach ($pathes as $path) {
- $doc = Document::get_by_path($course, $path);
- if ($doc) {
- $ids[] = $doc->get_id();
- }
- }
- if (empty($ids)) {
- return false;
- }
- $user = new \Portfolio\User();
- $user->email = Chamilo::user()->email();
- $artefact = new Portfolio\Artefact();
- $artefact->url = Portfolio::download_url($ids);
- $portfolio = $this->get_portfolio();
- $result = $portfolio->send($user, $artefact);
- return $result;
- }
- }
|