see #5192: adding but not enabling link changes

skala
Laurent Opprecht 13 years ago
parent ddb04557ee
commit 29678ca577
  1. 289
      main/link/ajax_controller.class.php
  2. 69
      main/link/category_form.class.php
  3. 518
      main/link/controller.class.php
  4. 180
      main/link/import_csv.class.php
  5. 382
      main/link/link.class.php
  6. 240
      main/link/link_category.class.php
  7. 239
      main/link/link_category_repository.class.php
  8. 113
      main/link/link_form.class.php
  9. 366
      main/link/link_repository.class.php
  10. 125
      main/link/request.class.php
  11. 105
      main/link/resources/js/main.js
  12. 82
      main/link/upload_file_form.class.php

@ -0,0 +1,289 @@
<?php
namespace Link;
use \Model\Course;
use \CourseDescription;
use \CourseDescriptionRoutes;
use \Display;
use \Template;
use \FormValidator;
use \Security;
use \Uri;
use Header;
/**
* Ajax controller. Dispatch request and perform required action.
*
* - delete category/link
* - hide/show link
* - sort links/categories
*
* Usage:
*
* $controller = AjaxController::instance();
* $controller->run();
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class AjaxController extends \Controller
{
const ACTION_DELETE_CATEGORY = 'delete_category';
const ACTION_HIDE_LINK = 'hide_link';
const ACTION_SHOW_LINK = 'show_link';
const ACTION_DELETE_LINK = 'delete_link';
const ACTION_DELETE_BY_COURSE = 'delete_by_course';
const ACTION_SORT_CATEGORIES = 'sort_categories';
const ACTION_SORT_LINKS = 'sort_links';
const ACTION_VALIDATE_LINK = 'validate_link';
/**
* Return the instance of the controller.
*
* @return \Link\AjaxController
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
protected function __construct()
{
}
/**
* Prepare the environment. Set up breadcrumps and raise tracking event.
*/
protected function prolog()
{
event_access_tool(TOOL_LINK);
}
public function authorize()
{
$authorize = api_protect_course_script();
if (!$authorize) {
return false;
}
$c_id = Request::get_c_id();
if (empty($c_id)) {
return false;
}
if (Request::is_student_view()) {
return false;
}
if (!$this->is_allowed_to_edit()) {
return false;
}
return true;
}
public function is_allowed_to_edit()
{
$session_id = Request::get_session_id();
if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
return false;
}
// if (!Security::check_token('get')) {
// return false;
// }
if (!api_is_allowed_to_edit(false, true, true)) {
return false;
}
return true;
}
/**
*
*/
public function hide_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$success = LinkRepository::instance()->make_invisible($c_id, $id);
$this->response($success);
}
/**
*
*/
public function show_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$success = LinkRepository::instance()->make_visible($c_id, $id);
$this->response($success);
}
/**
*
*/
public function delete_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$link = (object) array();
$link->c_id = Request::get_c_id();
$link->id = Request::get_id();
$success = LinkRepository::instance()->remove($link);
$this->response($success);
}
/**
*
*/
public function delete_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->c_id = Request::get_c_id();
$category->id = Request::get_id();
$success = LinkCategoryRepository::instance()->remove($category);
$this->response($success);
}
/**
*
*/
public function delete_by_course()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$success_link = LinkRepository::instance()->remove_by_course($c_id, $session_id);
$success_cat = LinkCategoryRepository::instance()->remove_by_course($c_id, $session_id);
$this->response($success_link && $success_cat);
}
public function sort_categories()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$ids = Request::get_ids();
if (empty($ids)) {
return;
}
$repo = LinkCategoryRepository::instance();
$success = $repo->order($c_id, $ids);
$this->response($success);
}
public function sort_links()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$ids = Request::get_ids();
if (empty($ids)) {
return;
}
$repo = LinkRepository::instance();
$success = $repo->order($c_id, $ids);
$this->response($success);
}
public function validate_link()
{
$c_id = Request::get_c_id();
$id = Request::get_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$success = $link ? $link->validate() : false;
$this->response($success);
}
function forbidden()
{
$this->response(false, get_lang('YourAreNotAuthorized'));
}
public function unknown()
{
$this->response(false, get_lang('UnknownAction'));
}
/**
* Action exists but implementation is missing.
*/
public function missing()
{
$this->response(false, get_lang('NoImplementation'));
}
/**
* Display a standard json responce.
*
* @param bool $success
* @param string $message
* @param object $data
*/
public function response($success = false, $message = '', $data = null)
{
$message = trim($message);
$response = (object) array();
$response->success = $success;
if ($message) {
$response->message = Display::return_message($message, $success ? 'normal' : 'error');
} else {
$response->message = '';
}
$response->data = $data;
$this->render_json($response);
}
}

@ -0,0 +1,69 @@
<?php
namespace Link;
/**
* Edit/create a LinkCategory.
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class CategoryForm extends \FormValidator
{
protected $category;
function __construct($form_name = 'category', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
{
parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
}
/**
*
* @return object
*/
public function get_category()
{
return $this->category;
}
public function set_category($value)
{
$this->category = $value;
}
/**
*
* @param \Link\LinkCategory $category
*/
function init($category = null)
{
$this->set_category($category);
$defaults = array();
$defaults['category_title'] = $category->category_title;
$defaults['category_description'] = $category->description;
$this->addElement('hidden', 'c_id', $category->c_id);
$this->addElement('hidden', 'id', $category->id);
$this->addElement('hidden', 'session_id', $category->session_id);
$form_name = $category->id ? get_lang('ModifyCategory') : get_lang('AddCategory');
$this->add_header($form_name);
$this->add_textfield('category_title', get_lang('Title'));
$this->addRule('category_title', get_lang('Required'), 'required');
$this->addElement('textarea', 'category_description', get_lang('Description'));
$this->addElement('button', 'save', get_lang('Save'), array('class' => 'btn save'));
$this->setDefaults($defaults);
}
function update_model()
{
$values = $this->exportValues();
$category = $this->get_category();
$category->category_title = $values['category_title'];
$category->description = $values['category_description'];
}
}

@ -0,0 +1,518 @@
<?php
namespace Link;
use Security;
use Uri;
use Redirect;
use Chamilo;
use Javascript;
use Header;
/**
* Html controller. Dispatch request and perform required action:
*
* - list
* - add/edit/delete link
* - add/edit/delete category
* - make visible/invisible link
* - go to link target
*
* Note:
* Currently some actions are only implemented in the Ajax controller.
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class Controller extends \Controller
{
const ACTION_LISTING = 'listing';
const ACTION_VIEW = 'view';
const ACTION_ADD_LINK = 'add_link';
const ACTION_EDIT_LINK = 'edit_link';
const ACTION_DELETE_LINK = 'delete_link';
const ACTION_MAKE_VISIBLE = 'make_visisble';
const ACTION_MAKE_INVISIBLE = 'make_invisible';
const ACTION_ADD_CATEGORY = 'add_category';
const ACTION_EDIT_CATEGORY = 'edit_category';
const ACTION_DELETE_CATEGORY = 'delete_category';
const ACTION_GO = 'go';
const ACTION_IMPORT_CSV = 'import_csv';
const ACTION_EXPORT_CSV = 'export_csv';
const ACTION_DEFAULT = 'listing';
/**
*
* @return \Link\Controller
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
public function is_allowed_to_edit()
{
if (Request::is_student_view()) {
return false;
}
//$c_id = self::params()->get_c_id();
//$id = self::params()->get_id();
$session_id = Request::get_session_id();
if ($session_id != 0 && api_is_allowed_to_session_edit(false, true) == false) {
return false;
}
if (!api_is_allowed_to_edit(false, true, true)) {
return false;
}
return true;
}
/**
* Action to perform.
* Returns the request parameter.
*
* @return string
*/
public function get_action()
{
$result = parent::get_action();
// if (self::params()->is_student_view()) {
// if ($result != self::ACTION_LISTING && $result != self::ACTION_VIEW) {
// return self::ACTION_LISTING;
// }
// }
$result = $result ? $result : self::ACTION_DEFAULT;
return $result;
}
public function prolog()
{
event_access_tool(TOOL_LINK);
//legacy
global $interbreadcrumb;
$interbreadcrumb = array();
$interbreadcrumb[] = array('url' => 'index.php', 'name' => get_lang('Links'));
global $current_course_tool;
global $this_section;
global $nameTools;
$current_course_tool = TOOL_LINK;
$this_section = SECTION_COURSES;
$nameTools = get_lang('Links');
}
/**
* Whether the call is authorized or not.
*
* @return boolean
*/
public function authorize()
{
$authorize = api_protect_course_script();
if (!$authorize) {
return false;
}
$c_id = Request::get_c_id();
if (empty($c_id)) {
return false;
}
return true;
}
/**
* Javascript used by the controller
*
* @return string
*/
public function javascript()
{
$src = Chamilo::url('/main/link/resources/js/main.js');
$result = Javascript::tag($src);
$www = Chamilo::url();
$code = "var www = '$www';\n";
//$code .= Javascript::get_lang('');
$result .= Javascript::tag_code($code);
return $result;
}
/**
* Returns a url for an action that the controller can process
*
* @param string $action
* @param array $params
* @return string
*/
public function url($action, $params = array())
{
$url_params = Uri::course_params();
if ($c_id = Request::get_c_id()) {
$url_params[Request::PARAM_C_ID] = $c_id;
}
if ($id = Request::get_id()) {
$url_params[Request::PARAM_ID] = $id;
}
if($session_id = Request::get_session_id()){
$url_params[Request::PARAM_SESSION_ID] = $session_id;
}
$url_params[Request::PARAM_ACTION] = $action;
foreach ($params as $key => $value) {
$url_params[$key] = $value;
}
$result = Uri::url('/main/link/index.php', $url_params, false);
return $result;
}
public function listing()
{
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$root = (object) array();
$root->c_id = $c_id;
$root->id = 0;
$root->session_id = $session_id;
$links = LinkRepository::instance()->find_by_category($root);
$repo = LinkCategory::repository();
$categories = $repo->find_by_course($c_id, $session_id);
//$data = compact('links', 'categories');
$data = (object) array();
$data->categories = $categories;
$data->links = $links;
$this->render('index', $data);
}
public function add_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$link = new Link();
$link->id = 0;
$link->c_id = Request::get_c_id();
$link->session_id = Request::get_session_id();
/**
* @todo: ensure session_id is correctly defaulted
*/
$action = $this->url(self::ACTION_ADD_LINK);
$form = new LinkForm('link', 'post', $action);
$form->init($link);
if ($form->validate()) {
$form->update_model();
$repo = LinkRepository::instance();
$success = $repo->save($link);
$message = $success ? get_lang('LinkAdded') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddLink'));
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function import_csv()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$action = $this->url(self::ACTION_IMPORT_CSV);
$form = new UploadFileForm('import_csv', 'post', $action);
$form->init();
if ($form->validate()) {
$file = $form->get_file();
$path = $file['tmp_name'];
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$import = new ImportCsv($c_id, $session_id, $path);
$import->run();
//import_csvfile();
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function export_csv()
{
$c_id = Request::get_c_id();
$session_id = Request::get_session_id();
$root = (object) array();
$root->c_id = $c_id;
$root->id = 0;
$root->session_id = $session_id;
$links = LinkRepository::instance()->find_by_category($root);
$repo = LinkCategory::repository();
$categories = $repo->find_by_course($c_id, $session_id);
$temp = Chamilo::temp_file();
$writer = \CsvWriter::create(new \FileWriter($temp));
$headers = array();
$headers[] = 'url';
$headers[] = 'title';
$headers[] = 'description';
$headers[] = 'target';
$headers[] = 'category_title';
$headers[] = 'category_description';
$writer->put($headers);
foreach ($links as $link) {
$data = array();
$data[] = $link->url;
$data[] = $link->title;
$data[] = $link->description;
$data[] = $link->target;
$data[] = '';
$data[] = '';
$writer->put($data);
}
foreach ($categories as $category) {
foreach ($category->links as $link) {
$data = array();
$data[] = $link->url;
$data[] = $link->title;
$data[] = $link->description;
$data[] = $link->target;
$data[] = $category->category_title;
$data[] = $category->description;
$writer->put($data);
}
}
\DocumentManager :: file_send_for_download($temp, true, get_lang('Links').'.csv');
}
public function delete_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function edit_link()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$id = Request::get_id();
$c_id = Request::get_c_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$action = $this->url(self::ACTION_EDIT_LINK);
$form = new LinkForm('link', 'post', $action);
$form->init($link);
if ($form->validate()) {
$form->update_model();
$success = $repo->save($link);
$message = $success ? get_lang('LinkUpdated') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditLink'));
$data = (object) array();
$data->form = $form;
$this->render('edit_link', $data);
}
public function make_visible()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function make_invisible()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
/**
* See AjaxController
*/
$this->missing();
}
public function add_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->id = 0;
$category->c_id = Request::get_c_id();
$category->session_id = Request::get_session_id();
$category->category_title = '';
$category->description = '';
$category->display_order = 0;
$action = $this->url(self::ACTION_ADD_CATEGORY);
$form = new CategoryForm('category', 'post', $action);
$form->init($category);
if ($form->validate()) {
$form->update_model();
$repo = LinkCategoryRepository::instance();
$success = $repo->save($category);
$message = $success ? get_lang('CategoryAdded') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('AddCategory'));
$data = (object) array();
$data->form = $form;
$this->render('edit_category', $data);
}
public function edit_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$c_id = Request::get_c_id();
$id = Request::get_id();
$repo = LinkCategoryRepository::instance();
$category = $repo->find_one_by_id($c_id, $id);
$action = $this->url(self::ACTION_EDIT_CATEGORY);
$form = new CategoryForm('category', 'post', $action);
$form->init($category);
if ($form->validate()) {
$form->update_model();
$repo = LinkCategoryRepository::instance();
$success = $repo->save($category);
$message = $success ? get_lang('CategorySaved') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
global $interbreadcrumb;
$interbreadcrumb[] = array('url' => '#', 'name' => get_lang('EditCategory'));
$data = (object) array();
$data->form = $form;
$this->render('edit_category', $data);
}
public function delete_category()
{
if (!$this->is_allowed_to_edit()) {
$this->forbidden();
return;
}
$category = (object) array();
$category->id = Request::get_id();
$category->c_id = Request::get_c_id();
$success = $repo = CategoryRepo::instance()->remove($category);
$message = $success ? get_lang('CategoryRemoved') : get_lang('Error');
$home = $this->url(self::ACTION_DEFAULT);
Redirect::go($home);
}
public function go()
{
$id = Request::get_id();
$c_id = Request::get_c_id();
$repo = LinkRepository::instance();
$link = $repo->find_one_by_id($c_id, $id);
$url = $link->url;
event_link($id);
Header::cache_control('no-store, no-cache, must-revalidate');
Header::pragma('no-cache');
Redirect::go($url);
}
/**
* Render a template using data. Adds a few common parameters to the data array.
*
* @see /main/template/default/course_description/
* @param string $template
* @param array $data
*/
protected function render($template, $data)
{
$data = $data ? $data : (object) array();
$_user = api_get_user_info();
$session_id = Request::get_session_id();
$data->session_image = api_get_session_image($session_id, $_user);
$sec_token = Security::get_token();
$data->sec_token = $sec_token;
$context = Uri::course_params();
$data->root = Uri::url('/main/link/index.php', $context);
$data->session_id = $session_id;
$data->c_id = Request::get_c_id();
$data->is_allowed_to_edit = $this->is_allowed_to_edit();
parent::render("link/$template.tpl", $data);
}
}

@ -0,0 +1,180 @@
<?php
namespace Link;
/**
* Import a csv file into the course/session.
*
*
*
* @license /licence.txt
* @author Laurent Opprecht <laurent@opprecht.info>
*/
class ImportCsv
{
protected $c_id;
protected $session_id;
protected $path;
protected $links_imported = 0;
protected $links_skipped = 0;
protected $update_existing_entries = false;
public function __construct($c_id, $session_id, $path, $update_existing_entries = false)
{
$this->c_id = $c_id;
$this->session_id = $session_id;
$this->path = $path;
$this->update_existing_entries = $update_existing_entries;
}
public function get_path()
{
return $this->path;
}
public function get_c_id()
{
return $this->c_id;
}
public function get_session_id()
{
return $this->session_id;
}
public function get_links_imported()
{
return $this->links_imported;
}
public function get_links_skipped()
{
return $this->links_skipped;
}
public function get_update_existing_entries()
{
return $this->update_existing_entries;
}
/**
* Read file and returns an array filled up with its' content.
*
* @return array of objects
*/
public function get_data()
{
$result = array();
$path = $this->path;
if (!is_readable($path)) {
return array();
}
$items = \Import::csv_reader($path);
foreach ($items as $item) {
$item = (object) $item;
$url = isset($item->url) ? trim($item->url) : '';
$title = isset($item->title) ? trim($item->title) : '';
$description = isset($item->description) ? trim($item->description) : '';
$target = isset($item->target) ? trim($item->target) : '';
$category_title = isset($item->category_title) ? trim($item->category_title) : '';
$category_description = isset($item->category_description) ? trim($item->category_description) : '';
if (empty($url)) {
continue;
}
if ($category_title) {
$category_title = \Security::remove_XSS($category_title);
$category_description = \Security::remove_XSS($category_description);
} else {
$category_description = '';
}
$url = \Security::remove_XSS($url);
$title = \Security::remove_XSS($title);
$description = \Security::remove_XSS($description);
$target = \Security::remove_XSS($target);
$item->url = $url;
$item->title = $title;
$item->description = $description;
$item->target = $target;
$item->category_title = $category_title;
$item->category_description = $category_description;
$result[] = $item;
}
return $result;
}
public function run()
{
$path = $this->path;
if (!is_readable($path)) {
return false;
}
$this->links_imported = 0;
$this->links_skipped = 0;
$items = $this->get_data();
foreach ($items as $item) {
$url = $item->url;
$title = $item->title;
$description = $item->description;
$target = $item->target;
$category_title = $item->category_title;
$category_description = $item->category_description;
if ($category_title) {
$category = $this->ensure_category($category_title, $category_description);
}
$link = $this->find_link_by_url($url);
if ($link && $this->update_existing_entries == false) {
$this->links_skipped++;
continue;
}
if (empty($link)) {
$link = new Link();
$link->c_id = $this->c_id;
$link->session_id = $this->session_id;
$link->url = $url;
}
$link->title = $title;
$link->description = $description;
$link->target = $target;
$link->category_id = $category ? $category->id : 0;
$repo = LinkRepository::instance();
$repo->save($link);
$this->links_imported++;
}
}
public function ensure_category($title, $description = '')
{
$c_id = $this->c_id;
$session_id = $this->session_id;
$repo = LinkCategoryRepository::instance();
$result = $repo->find_one_by_course_and_name($c_id, $session_id, $title);
if (empty($result)) {
$result = new LinkCategory();
$result->c_id = $c_id;
$result->category_title = $title;
$result->description = $description;
$result->session_id = $session_id;
$repo->save($result);
}
return $result;
}
public function find_link_by_url($url)
{
$c_id = $this->c_id;
$session_id = $this->session_id;
$repo = LinkRepository::instance();
$link = $repo->find_one_by_course_and_url($c_id, $session_id, $url);
return $link;
}
}

@ -0,0 +1,382 @@
<?php
namespace Link;
/**
* Model for Link/Urls
*
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class Link
{
/**
* @return \Entity\Repository\LinkRepository
*/
public static function repository()
{
return \Entity\Repository\LinkRepository::instance();
}
/**
* @return \Entity\Link
*/
public static function create($data = null)
{
return new self($data);
}
function __construct($data = null)
{
if ($data) {
foreach ($this as $key => $value) {
if (isset($data->{$key})) {
$this->{$key} = $data->{$key};
}
}
}
}
function __get($name)
{
$f = array($this, "get_$name");
return call_user_func($f);
}
function __isset($name)
{
$f = array($this, "get_$name");
return is_callable($f);
}
function __set($name, $value)
{
$f = array($this, "set_$name");
if (!is_callable($f)) {
return;
}
call_user_func($f, $value);
}
/**
* @var integer $c_id
*/
protected $c_id;
/**
* @var integer $id
*/
protected $id;
/**
* @var text $url
*/
protected $url;
/**
* @var string $title
*/
protected $title;
/**
* @var text $description
*/
protected $description;
/**
* @var integer $category_id
*/
protected $category_id;
/**
* @var integer $display_order
*/
protected $display_order;
/**
* @var string $on_homepage
*/
protected $on_homepage;
/**
* @var string $target
*/
protected $target;
/**
* @var integer $session_id
*/
protected $session_id;
protected $visibility = 0;
/**
* Set c_id
*
* @param integer $value
* @return Link
*/
public function set_c_id($value)
{
$this->c_id = $value;
return $this;
}
/**
* Get c_id
*
* @return integer
*/
public function get_c_id()
{
return $this->c_id;
}
/**
* Set id
*
* @param integer $value
* @return Link
*/
public function set_id($value)
{
$this->id = $value;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function get_id()
{
return $this->id;
}
/**
* Set url
*
* @param text $value
* @return Link
*/
public function set_url($value)
{
$this->url = $value;
return $this;
}
/**
* Get url
*
* @return text
*/
public function get_url()
{
return $this->url;
}
/**
* Set title
*
* @param string $value
* @return Link
*/
public function set_title($value)
{
$this->title = $value;
return $this;
}
/**
* Get title
*
* @return string
*/
public function get_title()
{
return $this->title;
}
/**
* Set description
*
* @param text $value
* @return Link
*/
public function set_description($value)
{
$this->description = $value;
return $this;
}
/**
* Get description
*
* @return text
*/
public function get_description()
{
return $this->description;
}
/**
* Set category_id
*
* @param integer $value
* @return Link
*/
public function set_category_id($value)
{
$this->category_id = $value;
return $this;
}
/**
* Get category_id
*
* @return integer
*/
public function get_category_id()
{
return $this->category_id;
}
/**
* Set display_order
*
* @param integer $value
* @return Link
*/
public function set_display_order($value)
{
$this->display_order = $value;
return $this;
}
/**
* Get display_order
*
* @return integer
*/
public function get_display_order()
{
return $this->display_order;
}
/**
* Set on_homepage
*
* @param string $value
* @return Link
*/
public function set_on_homepage($value)
{
$this->on_homepage = $value;
return $this;
}
/**
* Get on_homepage
*
* @return string
*/
public function get_on_homepage()
{
return $this->on_homepage;
}
/**
* Set target
*
* @param string $value
* @return Link
*/
public function set_target($value)
{
$this->target = $value;
return $this;
}
/**
* Get target
*
* @return string
*/
public function get_target()
{
return $this->target;
}
/**
* Set session_id
*
* @param integer $value
* @return Link
*/
public function set_session_id($value)
{
$this->session_id = $value;
return $this;
}
/**
* Get session_id
*
* @return integer
*/
public function get_session_id()
{
return $this->session_id;
}
public function get_visibility()
{
return (int) $this->visibility;
}
public function set_visibility($value)
{
$this->visibility = (int) $value;
return $this;
}
public function is_visible()
{
return $this->get_visibility() == 1;
}
public function is_hidden()
{
return $this->get_visibility() == 0;
}
public function validate()
{
$url = $this->get_url();
if (empty($url)) {
return false;
}
if (!in_array('curl', get_loaded_extensions())) {
true;
}
$defaults = array(
CURLOPT_URL => $url,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => 0,
CURLOPT_NOBODY => true,
CURLOPT_TIMEOUT => 4,
CURLOPT_RETURNTRANSFER => false
);
$ch = curl_init();
curl_setopt_array($ch, $defaults);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}

@ -0,0 +1,240 @@
<?php
namespace Link;
/**
* Model for link_category.
*
* Links can be added to a category.
* A link belong to at most one category.
* A link may not belong to a category.
* Categories cannot be nested, i.e. it is not possible to have categories inside a category.
*
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class LinkCategory
{
/**
* @return \Link\LinkCategoryRepository
*/
public static function repository()
{
return LinkCategoryRepository::instance();
}
/**
* @return \Entity\LinkCategory
*/
public static function create($data = null)
{
return new self($data);
}
/**
* @var integer $c_id
*/
protected $c_id;
/**
* @var integer $id
*/
protected $id;
/**
* @var string $category_title
*/
protected $category_title;
/**
* @var text $description
*/
protected $description;
/**
* @var integer $display_order
*/
protected $display_order;
/**
* @var integer $session_id
*/
protected $session_id;
protected $links = null;
function __construct($data = null)
{
if ($data) {
foreach ($this as $key => $value) {
if (isset($data->{$key})) {
$this->{$key} = $data->{$key};
}
}
}
}
function __get($name)
{
$f = array($this, "get_$name");
return call_user_func($f);
}
function __isset($name)
{
$f = array($this, "get_$name");
return is_callable($f);
}
function __set($name, $value)
{
$f = array($this, "set_$name");
if (!is_callable($f)) {
return;
}
call_user_func($f, $value);
}
/**
* Set c_id
*
* @param integer $value
* @return LinkCategory
*/
public function set_c_id($value)
{
$this->c_id = $value;
return $this;
}
/**
* Get c_id
*
* @return integer
*/
public function get_c_id()
{
return $this->c_id;
}
/**
* Set id
*
* @param integer $value
* @return LinkCategory
*/
public function set_id($value)
{
$this->id = $value;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function get_id()
{
return $this->id;
}
/**
* Set category_title
*
* @param string $value
* @return LinkCategory
*/
public function set_category_title($value)
{
$value = trim($value);
$this->category_title = $value;
return $this;
}
/**
* Get category_title
*
* @return string
*/
public function get_category_title()
{
return $this->category_title;
}
/**
* Set description
*
* @param text $value
* @return LinkCategory
*/
public function set_description($value)
{
$this->description = $value;
return $this;
}
/**
* Get description
*
* @return text
*/
public function get_description()
{
return $this->description;
}
/**
* Set display_order
*
* @param integer $value
* @return LinkCategory
*/
public function set_display_order($value)
{
$this->display_order = $value;
return $this;
}
/**
* Get display_order
*
* @return integer
*/
public function get_display_order()
{
return $this->display_order;
}
/**
* Set session_id
*
* @param integer $value
* @return LinkCategory
*/
public function set_session_id($value)
{
$this->session_id = $value;
return $this;
}
/**
* Get session_id
*
* @return integer
*/
public function get_session_id()
{
return $this->session_id;
}
public function get_links()
{
if (is_null($this->links)) {
$this->links = LinkRepository::instance()->find_by_category($this);
}
return $this->links;
}
}

@ -0,0 +1,239 @@
<?php
namespace Link;
use Database;
/**
* Database interface for link_category
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class LinkCategoryRepository
{
/**
*
* @return \Link\LinkCategoryRepository
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
public function save($category)
{
$id = $category->id;
if (empty($id)) {
return $this->insert($category);
} else {
return $this->update($category);
}
}
public function insert($category)
{
$c_id = (int) $category->c_id;
$session_id = (int) $category->session_id;
$session_id = $session_id ? $session_id : '0';
$category_title = trim($category->category_title);
$category_title = Database::escape_string($category_title);
$description = trim($category->description);
$description = Database::escape_string($description);
$display_order = $this->next_display_order($c_id);
$table = Database :: get_course_table(TABLE_LINK_CATEGORY);
$sql = "INSERT INTO $table
(c_id, category_title, description, display_order, session_id)
VALUES
($c_id , '$category_title', '$description', $display_order, $session_id)";
$result = (bool) Database :: query($sql);
if ($result) {
$id = Database::insert_id();
$category->id = $id;
$category->display_order = $display_order;
}
return $result;
}
function update($category)
{
$c_id = (int) $category->c_id;
$id = (int) $category->id;
$session_id = (int) $category->session_id;
$session_id = $session_id ? $session_id : '0';
$category_title = trim($category->category_title);
$category_title = Database::escape_string($category_title);
$description = trim($category->description);
$description = Database::escape_string($description);
$display_order = (int) $category->display_order;
$table = Database :: get_course_table(TABLE_LINK_CATEGORY);
$sql = "UPDATE $table SET
category_title = '$category_title',
description = '$description',
display_order = $display_order,
session_id = $session_id
WHERE
c_id = $c_id AND
id = $id";
$result = (bool) Database :: query($sql);
return $result;
}
function remove($category)
{
$table = Database :: get_course_table(TABLE_LINK_CATEGORY);
$c_id = (int) $category->c_id;
$id = (int) $category->id;
$sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
$success = (bool) Database :: query($sql);
if ($success) {
LinkRepository::instance()->remove_by_category($category);
}
return $success;
}
function remove_by_course($c_id, $session_id = 0)
{
$result = true;
$categories = $this->find_by_course($c_id, $session_id);
foreach($categories as $category){
$success = $this->remove($category);
if(!$success){
$result = false;
}
}
return $result;
}
function next_display_order($c_id)
{
$table = Database :: get_course_table(TABLE_LINK_CATEGORY);
$sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
$rs = Database :: query($sql);
list ($result) = Database :: fetch_row($rs);
$result = $result + 1;
$result = intval($result);
return $result;
}
/**
*
* @param string $where
* @return array
*/
public function find($where)
{
$result = array();
$table = Database :: get_course_table(TABLE_LINK_CATEGORY);
$where = $where ? "WHERE $where" : '';
$sql = "SELECT * FROM $table $where ORDER BY display_order DESC";
$rs = Database :: query($sql);
while ($data = Database::fetch_object($rs)) {
$result[] = LinkCategory::create($data);
}
return $result;
}
/**
*
* @param string $where
* @return \Link\LinkCategory
*/
public function find_one($where)
{
$items = $this->find($where);
foreach ($items as $item) {
return $item;
}
return null;
}
/**
*
* @param int $c_id
* @param int $id
* @return \Link\LinkCategory
*/
public function find_one_by_id($c_id, $id)
{
$where = "c_id = $c_id AND id = $id";
return $this->find_one($where);
}
/**
*
* @param int $c_id
* @param int $session_id
* @param string $title
* @return \Link\LinkCategory
*/
public function find_one_by_course_and_name($c_id, $session_id = 0, $title)
{
$c_id = (int) $c_id;
$session_id = (int) $session_id;
$title = Database::escape_string($title);
$condition_session = api_get_session_condition($session_id, true, true);
$where = "c_id = $c_id $condition_session AND category_title = '$title'";
return $this->find_one($where);
}
function find_by_course($c_id, $session_id = 0)
{
$c_id = (int) $c_id;
$session_id = (int) $session_id;
$condition_session = api_get_session_condition($session_id, true, true);
$where = "c_id = $c_id $condition_session";
return $this->find($where);
}
/**
* Ensure $ids are sorted in the order given from greater to lower.
*
* Simple algorithm, works only if all ids are given at the same time.
* This would not work if pagination were used and only a subset were sorted.
* On the other hand the algorithm is sturdy, it will work even if current
* weights/display orders are not correctly set up in the db.
*
* @param int $c_id
* @param array $ids
*/
public function order($c_id, $ids)
{
$result = true;
$ids = array_map('intval', $ids);
$table = Database::get_course_table(TABLE_LINK_CATEGORY);
$counter = 0;
$weight = count($ids) + 1;
foreach ($ids as $id) {
$sql = "UPDATE $table SET display_order = $weight WHERE c_id = $c_id AND id = $id";
$success = Database::query($sql);
if (!$success) {
$result = false;
}
--$weight;
}
return $result;
}
}

@ -0,0 +1,113 @@
<?php
namespace Link;
use Chamilo;
/**
* Edit/Create link form.
*
* @license /licence.txt
* @author Laurent Opprecht <laurent@opprecht.info>
*/
class LinkForm extends \FormValidator
{
protected $link;
function __construct($form_name = 'link', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
{
parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
}
/**
*
* @return \Link\LinkCategory
*/
public function get_link()
{
return $this->link;
}
public function set_link($value)
{
$this->link = $value;
}
/**
*
* @param \Link\LinkCategory $link
*/
function init($link = null)
{
$this->set_link($link);
$defaults = array();
$defaults['url'] = $link->url ? $link->url : 'http://';
$defaults['title'] = $link->title;
$defaults['description'] = $link->description;
$defaults['category_id'] = $link->category_id;
$defaults['display_order'] = $link->display_order;
$defaults['on_homepage'] = $link->on_homepage;
$defaults['target'] = $link->target;
$this->add_hidden('c_id', $link->c_id);
$this->add_hidden('id', $link->id);
$this->add_hidden('session_id', $link->session_id);
$form_name = $category->id ? get_lang('LinkMod') : get_lang('LinkAdd');
$this->add_header($form_name);
$this->add_textfield('url', get_lang('Url'), $required = true, array('class' => 'span6'));
$this->addRule('url', get_lang('MalformedUrl'), 'regex', '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i');
$this->add_textfield('title', get_lang('Title'), $required = false, array('class' => 'span6'));
$this->add_textarea('description', get_lang('Description'), array('class' => 'span3'));
$this->add_checkbox('on_homepage', '', get_lang('OnHomepage'));
$id = $link->id;
if ($id) {
$url = Chamilo::url('/main/metadata/index.php', array('eid' => "Link.$id"));
$metadata = '<a class="control-text" href="' . $url . '">' . get_lang('AddMetadata') . '</a>';
$this->add_label(get_lang('Metadata'), $metadata);
}
$options = array();
$options[0] = '--';
$categories = LinkCategoryRepository::instance()->find_by_course($link->c_id, $link->session_id);
foreach ($categories as $category) {
$options[$category->id] = $category->category_title;
}
$this->add_select('category_id', get_lang('Category'), $options);
$targets = array(
'_self' => get_lang('LinkOpenSelf'),
'_blank' => get_lang('LinkOpenBlank'),
'_parent' => get_lang('LinkOpenParent'),
'_top' => get_lang('LinkOpenTop')
);
$this->add_select('target', get_lang('LinkTarget'), $targets);
//$help = '<span class="help-block">' . get_lang('AddTargetOfLinkOnHomepage') . '</span>';
//$this->add_label('', $help);
$this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
$this->setDefaults($defaults);
}
function update_model()
{
$values = $this->exportValues();
$link = $this->get_link();
$link->url = $values['url'];
$link->title = $values['title'];
$link->description = $values['description'];
$link->category_id = $values['category_id'];
$link->on_homepage = isset($values['on_homepage']) ? $values['on_homepage'] : false;
$link->target = $values['target'];
}
}

@ -0,0 +1,366 @@
<?php
namespace Link;
use Database;
/**
* Database interface for Link
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class LinkRepository
{
/**
*
* @return \Link\LinkRepository
*/
public static function instance()
{
static $result = null;
if (empty($result)) {
$result = new self();
}
return $result;
}
public function save($link)
{
$id = $link->id;
if (empty($id)) {
return $this->insert($link);
} else {
return $this->update($link);
}
}
/**
*
* @param \Link\Link $link
* @return bool
*/
public function insert($link)
{
$c_id = (int) $link->c_id;
$session_id = (int) $link->session_id;
$session_id = $session_id ? $session_id : '0';
$url = trim($link->url);
$url = Database::escape_string($url);
$title = trim($link->title);
$title = Database::escape_string($title);
$description = trim($link->description);
$description = Database::escape_string($description);
$category_id = intval($link->category_id);
$category_id = $category_id ? $category_id : '0';
$display_order = $this->next_display_order($c_id);
$on_homepage = $link->on_homepage;
$on_homepage = $on_homepage ? '1' : '0';
$target = $link->target;
$target = Database::escape_string($target);
$table = Database :: get_course_table(TABLE_LINK);
$sql = "INSERT INTO $table
(c_id, url, title, description, category_id, display_order, on_homepage, target, session_id)
VALUES
($c_id , '$url', '$title', '$description', $category_id, $display_order, '$on_homepage', '$target', $session_id)";
$result = (bool) Database :: query($sql);
if ($result) {
$id = Database::insert_id();
$link->id = $id;
$link->display_order = $display_order;
$_course = api_get_course_info_by_id($c_id);
$tool = TOOL_LINK;
$user_id = api_get_user_id();
api_item_property_update($_course, $tool, $id, 'LinkAdded', $user_id);
}
return $result;
}
function update($link)
{
$c_id = (int) $link->c_id;
$id = (int) $link->id;
$session_id = (int) $link->session_id;
$session_id = $session_id ? $session_id : '0';
$url = trim($link->url);
$url = Database::escape_string($url);
$title = trim($link->title);
$title = Database::escape_string($title);
$description = trim($link->description);
$description = Database::escape_string($description);
$category_id = intval($link->category_id);
$category_id = $category_id ? $category_id : '0';
$display_order = (int) $link->display_order;
$on_homepage = $link->on_homepage;
$on_homepage = $on_homepage ? '1' : '0';
$target = $link->target;
$target = Database::escape_string($target);
$table = Database :: get_course_table(TABLE_LINK);
$sql = "UPDATE $table SET
url = '$url',
title = '$title',
description = '$description',
category_id = $category_id,
display_order = $display_order,
on_homepage = '$on_homepage',
target = '$target',
session_id = $session_id
WHERE
c_id = $c_id AND
id = $id";
$result = (bool) Database :: query($sql);
if ($result) {
$_course = api_get_course_info_by_id($c_id);
$tool = TOOL_LINK;
$user_id = api_get_user_id();
api_item_property_update($_course, $tool, $id, 'LinkUpdated', $user_id);
}
return $result;
}
public function remove_by_category($category)
{
$result = true;
$c_id = (int) $category->c_id;
$id = (int) $category->id;
$where = "l.c_id=$c_id AND l.category_id=$id";
$links = $links = $this->find($where);
foreach ($links as $link) {
$success = $this->remove($link);
if (!$success) {
$result = false;
}
}
return $result;
}
public function remove_by_course($c_id, $session_id = 0)
{
$result = true;
$session_where = api_get_session_condition($session_id, true, true);
$where = "l.c_id=$c_id $session_where";
$links = $links = $this->find($where);
foreach ($links as $link) {
$success = $this->remove($link);
if (!$success) {
$result = false;
}
}
return $result;
}
/**
*
* Note:
*
* Note as ids are reused when objects are deleted it is either we delete
* everything or nothing.
*
* @param \Link\Link|object $link
* @return bool
*/
public function remove($link)
{
$table = Database :: get_course_table(TABLE_LINK);
$c_id = (int) $link->c_id;
$id = (int) $link->id;
$sql = "DELETE FROM $table WHERE c_id=$c_id AND id=$id";
$result = Database :: query($sql);
if ($result) {
$tool = TOOL_LINK;
$tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
$sql = "DELETE FROM $tbl_property WHERE c_id=$c_id AND ref=$id AND tool='$tool'";
Database :: query($sql);
$id = "Link.$id";
$table = Database :: get_course_table(TABLE_METADATA);
$sql = "DELETE FROM $table WHERE c_id=$c_id AND eid='$id'";
Database :: query($sql);
}
return (bool) $result;
}
/**
*
* @param string $where
* @return \Link\Link
*/
public function find_one($where)
{
$items = $this->find($where);
foreach ($items as $item) {
return $item;
}
return null;
}
/**
*
* @param int $c_id
* @param int $id
* @return \Link\Link
*/
public function find_one_by_id($c_id, $id)
{
$where = "l.c_id = $c_id AND l.id = $id";
return $this->find_one($where);
}
/**
*
* @param int $c_id
* @param int $session_id
* @param string $url
* @return \Link\Link
*/
public function find_one_by_course_and_url($c_id, $session_id, $url)
{
$c_id = (int)$c_id;
$session_id = (int)$session_id;
$url = Database::escape_string($url);
$session_where =api_get_session_condition($session_id, true, true);
$where = "l.c_id = $c_id $session_where AND l.url = '$url'";
return $this->find_one($where);
}
/**
*
* @param string $where
* @return array
*/
public function find($where = '')
{
$result = array();
$tbl_link = Database :: get_course_table(TABLE_LINK);
$tbl_property = Database :: get_course_table(TABLE_ITEM_PROPERTY);
$tool = TOOL_LINK;
$where = $where ? " AND ($where)" : '';
$sql = "SELECT
l.*,
p.visibility
FROM
$tbl_link AS l,
$tbl_property AS p
WHERE
p.tool='$tool' AND
l.id = p.ref AND
l.c_id = p.c_id
$where
ORDER BY
l.display_order DESC";
$rs = Database :: query($sql);
while ($data = Database::fetch_object($rs)) {
$result[] = Link::create($data);
}
return $result;
}
/**
*
* @param \Link\LinkCategory $category
*/
public function find_by_category($category, $visible_only = false)
{
$session_id = $category->session_id;
$id = $category->id;
$where = $visible_only ? 'p.visibility=1' : '(p.visibility=0 OR p.visibility=1)';
$where .=api_get_session_condition($session_id, true, true);
$where .= "AND l.category_id = $id";
return $this->find($where);
}
public function make_visible($c_id, $id)
{
$_course = api_get_course_info_by_id($c_id);
$user_id = api_get_user_id();
$result = api_item_property_update($_course, TOOL_LINK, $id, 'visible', $user_id);
return $result;
}
public function make_invisible($c_id, $id)
{
$_course = api_get_course_info_by_id($c_id);
$user_id = api_get_user_id();
$result = api_item_property_update($_course, TOOL_LINK, $id, 'invisible', $user_id);
return $result;
}
function next_display_order($c_id)
{
$table = Database :: get_course_table(TABLE_LINK);
$sql = "SELECT MAX(display_order) FROM $table WHERE c_id = $c_id ";
$rs = Database :: query($sql);
list ($result) = Database :: fetch_row($rs);
$result = $result + 1;
$result = intval($result);
return $result;
}
/**
* Ensure $ids are sorted in the order given from greater to lower
*
* Simple algorithm, works only if all ids are given at the same time.
* This would not work if pagination were used and only a subset were sorted.
* On the other hand the algorithm is sturdy, it will work even if current
* weights/display orders are not correctly set up in the db.
*
* @param int $c_id
* @param array $ids
*/
public function order($c_id, $ids)
{
$result = true;
$ids = array_map('intval', $ids);
$table = Database :: get_course_table(TABLE_LINK);
$counter = 0;
$weight = count($ids) + 1;
foreach ($ids as $id) {
$sql = "UPDATE $table SET display_order = $weight WHERE c_id = $c_id AND id = $id";
$success = Database::query($sql);
if (!$success) {
$result = false;
}
--$weight;
}
return $result;
}
}

@ -0,0 +1,125 @@
<?php
namespace Link;
/**
* Html request for Link.
*
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Genevas
* @license /license.txt
*/
class Request extends \Request
{
const PARAM_ID = 'id';
const PARAM_IDS = 'ids';
const PARAM_C_ID = 'c_id';
const PARAM_SESSION_ID = 'id_session';
const PARAM_ACTION = 'action';
const PARAM_SECURITY_TOKEN = 'sec_token';
const PARAM_IS_STUDENT_VIEW = 'isStudentView';
const PARAM_GROUP_ID = 'toolgroup';
/**
* Action to perform. *
* @return string
*/
public static function get_action()
{
$result = Request::get(self::PARAM_ACTION, '');
return $result;
}
/**
* Returns the object id.
*
* @return int
*/
public static function get_id()
{
$result = \Request::get(self::PARAM_ID, 0);
$result = intval($result);
return $result;
}
/**
* List of objet ids
*
* @return array
*/
public static function get_ids()
{
$result = Request::get(self::PARAM_IDS, array());
if (is_array($result)) {
return $result;
}
$result = trim($result);
if (empty($result)) {
return array();
}
$result = explode(',', $result);
return $result;
}
/**
* Returns the course id.
*
* @return int
*/
public static function get_c_id()
{
$result = Request::get(self::PARAM_C_ID, 0);
$result = intval($result);
$result = $result ? $result : api_get_real_course_id();
$result = $result ? $result : 0;
return $result;
}
/**
* Returns the session id.
*
* @return int
*/
public static function get_session_id()
{
$result = Request::get(self::PARAM_SESSION_ID, 0);
$result = intval($result);
return $result;
}
/**
* Returns the session id.
*
* @return int
*/
public static function get_group_id()
{
$result = Request::get(self::PARAM_GROUP_ID, 0);
$result = intval($result);
return $result;
}
/**
* Returns the security token.
*
* @return string
*/
public static function get_security_token()
{
$result = Request::get(self::PARAM_SEC_TOKEN, 0);
return $result;
}
/**
* Returns true if the user is in "student view". False otherwise.
*
* @return bool
*/
public static function is_student_view()
{
return Request::get(self::PARAM_IS_STUDENT_VIEW, false) == 'true';
}
}

@ -0,0 +1,105 @@
function Proxy() {};
Proxy.prototype.root = function(){
return www + '/main/inc/ajax/link.ajax.php';
}
Proxy.prototype.post = function(data, f){
if(typeof(sec_token)!=='undefined'){
data.sec_token = sec_token;
}
$.post(this.root(), data, f, 'json');
}
var Link = new Proxy();
Link.hide = function(c_id, id, f)
{
var data = {
c_id: c_id,
id: id,
action: 'hide_link'
};
this.post(data, f);
};
Link.show = function(c_id, id, f)
{
var data = {
c_id: c_id,
id: id,
action: 'show_link'
};
this.post(data, f);
};
Link.del = function(c_id, id, f)
{
var data = {
c_id: c_id,
id: id,
action: 'delete_link'
};
this.post(data, f);
};
Link.delete_by_course = function(c_id, session_id, f)
{
var data = {
c_id: c_id,
session_id: session_id,
action: 'delete_by_course'
};
this.post(data, f);
};
Link.sort = function(c_id, ids, f){
var data = {
c_id: c_id,
ids: ids,
action: 'sort_links'
};
this.post(data, f);
};
Link.validate = function(c_id, id, f)
{
var data = {
c_id: c_id,
id: id,
action: 'validate_link'
};
this.post(data, f);
};
var LinkCategory = new Proxy();
LinkCategory.del = function(c_id, id, f)
{
var data = {
c_id: c_id,
id: id,
action: 'delete_category'
};
this.post(data, f);
};
LinkCategory.sort = function(c_id, ids, f){
var data = {
c_id: c_id,
ids: ids,
action: 'sort_categories'
};
this.post(data, f);
};
var message = {};
message.update = function(data){
text = typeof(data)=='string' ? data : data.message;
$('#messages').html(text)
}

@ -0,0 +1,82 @@
<?php
namespace Link;
use Chamilo;
/**
* Form to upload a file.
*
* @license /licence.txt
* @author Laurent Opprecht <laurent@opprecht.info>
*/
class UploadFileForm extends \FormValidator
{
function __construct($form_name = 'upload_file', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
{
parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
}
/**
*
*
*/
function init()
{
$form_name = get_lang('UploadFile');
$this->add_header($form_name);
$label = get_lang('File');
$this->add_file('file', $label);
$this->addRule('file', get_lang('ThisFieldIsRequired'), 'required');
//$this->add_checkbox('replace', '', get_lang('ReplaceExistingEntries'));
$this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
$label = get_lang('CSVMustLookLike');
$label = "<h4>$label</h4>";
$help = '<pre>
<strong>"url"</strong>;"title";"description";"target";"category_title";"category_description"
"http://chamilo.org";"Chamilo";"";"_self";"";""
"http://google.com";"Google";"";"_self";"Google";""
"http://mail.google.com";"Google";"";"_self";"Google";""
</pre>';
$this->add_html($label . $help);
}
/**
*
* @return array
*/
public function get_file()
{
$result = Request::file('file', array());
if (empty($result)) {
return array();
}
$error = isset($result['error']) ? (bool) $result['error'] : false;
if ($error) {
return array();
}
return $result;
}
public function validate()
{
$result = (bool) parent::validate();
if ($result == false) {
return false;
}
$file = $this->get_file();
if (empty($file)) {
return false;
}
return true;
}
// public function get_update_existing_entries(){
// return (bool)$this->exportValue('replace');
// }
}
Loading…
Cancel
Save