configuration value: 'allow_exercise_categories'pull/2990/head
parent
55c3bed3ca
commit
fb3483efbb
@ -0,0 +1,146 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
require_once __DIR__.'/../inc/global.inc.php'; |
||||
|
||||
if (api_get_configuration_value('allow_exercise_categories') === false) { |
||||
api_not_allowed(); |
||||
} |
||||
|
||||
api_protect_course_script(); |
||||
|
||||
if (!api_is_allowed_to_edit()) { |
||||
api_not_allowed(true); |
||||
} |
||||
|
||||
$interbreadcrumb[] = [ |
||||
'url' => api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq(), |
||||
'name' => get_lang('Exercises') |
||||
]; |
||||
|
||||
$courseId = api_get_course_int_id(); |
||||
|
||||
$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_exercise_categories&c_id='.$courseId.'&'.api_get_cidreq(); |
||||
$action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : ''; |
||||
|
||||
$obj = new ExerciseCategoryManager(); |
||||
|
||||
$check = Security::check_token('request'); |
||||
$token = Security::get_token(); |
||||
|
||||
//Add the JS needed to use the jqgrid |
||||
$htmlHeadXtra[] = api_get_jqgrid_js(); |
||||
|
||||
//The order is important you need to check the the $column variable in the model.ajax.php file |
||||
$columns = [ |
||||
get_lang('Name'), |
||||
get_lang('Actions'), |
||||
]; |
||||
|
||||
// Column config |
||||
$column_model = [ |
||||
[ |
||||
'name' => 'name', |
||||
'index' => 'name', |
||||
'width' => '140', |
||||
'align' => 'left', |
||||
], |
||||
[ |
||||
'name' => 'actions', |
||||
'index' => 'actions', |
||||
'width' => '40', |
||||
'align' => 'left', |
||||
'formatter' => 'action_formatter', |
||||
'sortable' => 'false', |
||||
], |
||||
]; |
||||
|
||||
// Autowidth |
||||
$extra_params['autowidth'] = 'true'; |
||||
// height auto |
||||
$extra_params['height'] = 'auto'; |
||||
|
||||
$action_links = $obj->getJqgridActionLinks($token); |
||||
|
||||
$htmlHeadXtra[] = '<script> |
||||
$(function() { |
||||
// grid definition see the $obj->display() function |
||||
'.Display::grid_js( |
||||
'categories', |
||||
$url, |
||||
$columns, |
||||
$column_model, |
||||
$extra_params, |
||||
[], |
||||
$action_links, |
||||
true |
||||
).' |
||||
}); |
||||
</script>'; |
||||
|
||||
$url = api_get_self().'?'.api_get_cidreq(); |
||||
|
||||
switch ($action) { |
||||
case 'add': |
||||
$interbreadcrumb[] = ['url' => $url, 'name' => get_lang('Categories')]; |
||||
$interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')]; |
||||
$form = $obj->return_form($url.'&action=add', 'add'); |
||||
// The validation or display |
||||
if ($form->validate()) { |
||||
$values = $form->exportValues(); |
||||
unset($values['id']); |
||||
$res = $obj->save($values); |
||||
if ($res) { |
||||
Display::addFlash(Display::return_message(get_lang('ItemAdded'), 'confirmation')); |
||||
} |
||||
header('Location: '.$url); |
||||
exit; |
||||
} else { |
||||
$content = '<div class="actions">'; |
||||
$content .= '<a href="'.$url.'">'. |
||||
Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>'; |
||||
$content .= '</div>'; |
||||
$form->addElement('hidden', 'sec_token'); |
||||
$form->setConstants(['sec_token' => $token]); |
||||
$content .= $form->returnForm(); |
||||
} |
||||
break; |
||||
case 'edit': |
||||
$interbreadcrumb[] = ['url' => $url, 'name' => get_lang('Categories')]; |
||||
$interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')]; |
||||
$form = $obj->return_form($url.'&action=edit&id='.intval($_GET['id']), 'edit'); |
||||
|
||||
// The validation or display |
||||
if ($form->validate()) { |
||||
$values = $form->exportValues(); |
||||
$res = $obj->update($values); |
||||
if ($res) { |
||||
Display::addFlash(Display::return_message(get_lang('ItemUpdated'), 'confirmation')); |
||||
} |
||||
header('Location: '.$url); |
||||
exit; |
||||
} else { |
||||
$content = '<div class="actions">'; |
||||
$content .= '<a href="'.$url.'">'. |
||||
Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>'; |
||||
$content .= '</div>'; |
||||
$form->addElement('hidden', 'sec_token'); |
||||
$form->setConstants(['sec_token' => $token]); |
||||
$content .= $form->returnForm(); |
||||
} |
||||
break; |
||||
case 'delete': |
||||
$res = $obj->delete($_GET['id']); |
||||
if ($res) { |
||||
Display::addFlash(Display::return_message(get_lang('ItemDeleted'), 'confirmation')); |
||||
} |
||||
header('Location: '.$url); |
||||
exit; |
||||
break; |
||||
default: |
||||
$content = $obj->display(); |
||||
break; |
||||
} |
||||
|
||||
Display::display_header(); |
||||
echo $content; |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,270 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\CourseBundle\Entity\CExerciseCategory; |
||||
|
||||
/** |
||||
* Class ExtraFieldValue |
||||
* Declaration for the ExtraFieldValue class, managing the values in extra |
||||
* fields for any data type. |
||||
* |
||||
* @package chamilo.library |
||||
*/ |
||||
class ExerciseCategoryManager extends Model |
||||
{ |
||||
public $type = ''; |
||||
public $columns = [ |
||||
'id', |
||||
'name', |
||||
'c_id', |
||||
'description', |
||||
'created_at', |
||||
'updated_at', |
||||
]; |
||||
|
||||
/** |
||||
* Formats the necessary elements for the given datatype. |
||||
* |
||||
* @param string $type The type of data to which this extra field |
||||
* applies (user, course, session, ...) |
||||
* |
||||
* @assert (-1) === false |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
parent::__construct(); |
||||
$this->is_course_model = true; |
||||
$this->table = Database::get_course_table('exercise_category'); |
||||
} |
||||
|
||||
/** |
||||
* Gets the number of values stored in the table (all fields together) |
||||
* for this type of resource. |
||||
* |
||||
* @param int $courseId |
||||
* |
||||
* @return int Number of rows in the table |
||||
*/ |
||||
public function getCourseCount($courseId) |
||||
{ |
||||
$em = Database::getManager(); |
||||
$query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); |
||||
$query->select('count(e.id)'); |
||||
$query->where('e.cId = :cId'); |
||||
$query->setParameter('cId', $courseId); |
||||
|
||||
return $query->getQuery()->getSingleScalarResult(); |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getCategories($courseId) |
||||
{ |
||||
$em = Database::getManager(); |
||||
$query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); |
||||
$query->where('e.cId = :cId'); |
||||
$query->setParameter('cId', $courseId); |
||||
$query->orderBy('e.position'); |
||||
|
||||
return $query->getQuery()->getResult(); |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getCategoriesForSelect($courseId) |
||||
{ |
||||
$categories = $this->getCategories($courseId); |
||||
$options = []; |
||||
|
||||
if (!empty($categories)) { |
||||
/** @var CExerciseCategory $category */ |
||||
foreach ($categories as $category) { |
||||
$options[$category->getId()] = $category->getName(); |
||||
} |
||||
} |
||||
|
||||
return $options; |
||||
} |
||||
|
||||
/** |
||||
* @param int $id |
||||
* |
||||
*/ |
||||
public function delete($id) |
||||
{ |
||||
$em = Database::getManager(); |
||||
$repo = Database::getManager()->getRepository('ChamiloCourseBundle:CExerciseCategory'); |
||||
$category = $repo->find($id); |
||||
if ($category) { |
||||
$em->remove($category); |
||||
$em->flush(); |
||||
|
||||
$courseId = api_get_course_int_id(); |
||||
$table = Database::get_course_table(TABLE_QUIZ_TEST); |
||||
$id = (int) $id; |
||||
|
||||
$sql = "UPDATE $table SET exercise_category_id = 0 |
||||
WHERE c_id = $courseId AND exercise_category_id = $id"; |
||||
Database::query($sql); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Save values in the *_field_values table. |
||||
* |
||||
* @param array $params Structured array with the values to save |
||||
* @param bool $showQuery Whether to show the insert query (passed to the parent save() method) |
||||
* |
||||
*/ |
||||
public function save($params, $showQuery = false) |
||||
{ |
||||
$courseId = api_get_course_int_id(); |
||||
$em = Database::getManager(); |
||||
$category = new CExerciseCategory(); |
||||
$category |
||||
->setName($params['name']) |
||||
->setCId(api_get_course_int_id()) |
||||
->setDescription($params['name']) |
||||
; |
||||
/* |
||||
// Update position |
||||
$query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); |
||||
$query |
||||
->where('e.cId = :cId') |
||||
->setParameter('cId', $courseId) |
||||
->setMaxResults(1) |
||||
->orderBy('e.position', 'DESC'); |
||||
$last = $query->getQuery()->getOneOrNullResult(); |
||||
$position = 0; |
||||
if (!empty($last)) { |
||||
$position = $last->getPosition() + 1; |
||||
} |
||||
$category->setPosition($position); |
||||
*/ |
||||
$em->persist($category); |
||||
$em->flush(); |
||||
|
||||
return $category; |
||||
} |
||||
|
||||
/** |
||||
* @param $token |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function getJqgridActionLinks($token) |
||||
{ |
||||
//With this function we can add actions to the jgrid (edit, delete, etc) |
||||
$editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL); |
||||
$deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL); |
||||
$confirmMessage = addslashes( |
||||
api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES) |
||||
); |
||||
|
||||
$courseParams = api_get_cidreq(); |
||||
|
||||
$editButton = <<<JAVASCRIPT |
||||
<a href="?action=edit&{$courseParams}&id=' + options.rowId + '" class="btn btn-link btn-xs">\ |
||||
$editIcon\ |
||||
</a> |
||||
JAVASCRIPT; |
||||
$deleteButton = <<<JAVASCRIPT |
||||
<a \ |
||||
onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \ |
||||
href="?sec_token=$token&{$courseParams}&id=' + options.rowId + '&action=delete" \ |
||||
class="btn btn-link btn-xs">\ |
||||
$deleteIcon\ |
||||
</a> |
||||
JAVASCRIPT; |
||||
|
||||
return "function action_formatter(cellvalue, options, rowObject) { |
||||
return '$editButton $deleteButton'; |
||||
}"; |
||||
} |
||||
|
||||
/** |
||||
* @param string $url |
||||
* @param string $action |
||||
* |
||||
* @return FormValidator |
||||
*/ |
||||
public function return_form($url, $action) |
||||
{ |
||||
$form = new FormValidator('category', 'post', $url); |
||||
$id = isset($_GET['id']) ? (int) $_GET['id'] : null; |
||||
$form->addElement('hidden', 'id', $id); |
||||
|
||||
// Setting the form elements |
||||
$header = get_lang('Add'); |
||||
$defaults = []; |
||||
|
||||
if ($action === 'edit') { |
||||
$header = get_lang('Modify'); |
||||
// Setting the defaults |
||||
$defaults = $this->get($id, false); |
||||
} |
||||
|
||||
$form->addElement('header', $header); |
||||
|
||||
$form->addText( |
||||
'name', |
||||
get_lang('Name') |
||||
); |
||||
|
||||
$form->addHtmlEditor('description', get_lang('Description')); |
||||
|
||||
if ($action === 'edit') { |
||||
$form->addButtonUpdate(get_lang('Modify')); |
||||
} else { |
||||
$form->addButtonCreate(get_lang('Add')); |
||||
} |
||||
|
||||
/*if (!empty($defaults['created_at'])) { |
||||
$defaults['created_at'] = api_convert_and_format_date($defaults['created_at']); |
||||
} |
||||
if (!empty($defaults['updated_at'])) { |
||||
$defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']); |
||||
}*/ |
||||
$form->setDefaults($defaults); |
||||
|
||||
// Setting the rules |
||||
$form->addRule('name', get_lang('ThisFieldIsRequired'), 'required'); |
||||
|
||||
return $form; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function display() |
||||
{ |
||||
// action links |
||||
$content = '<div class="actions">'; |
||||
$content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'; |
||||
$content .= Display::return_icon( |
||||
'back.png', |
||||
get_lang('BackTo').' '.get_lang('PlatformAdmin'), |
||||
'', |
||||
ICON_SIZE_MEDIUM |
||||
); |
||||
$content .= '</a>'; |
||||
$content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">'; |
||||
$content .= Display::return_icon( |
||||
'add.png', |
||||
get_lang('Add'), |
||||
'', |
||||
ICON_SIZE_MEDIUM |
||||
); |
||||
$content .= '</a>'; |
||||
$content .= '</div>'; |
||||
$content .= Display::grid_html('categories'); |
||||
|
||||
return $content; |
||||
} |
||||
} |
@ -0,0 +1,219 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CourseBundle\Entity; |
||||
|
||||
use Doctrine\ORM\Mapping as ORM; |
||||
use Gedmo\Mapping\Annotation as Gedmo; |
||||
|
||||
/** |
||||
* CExerciseCategory. |
||||
* |
||||
* @ORM\Table(name="c_exercise_category") |
||||
* ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository") |
||||
*/ |
||||
class CExerciseCategory |
||||
{ |
||||
/** |
||||
* @var int |
||||
* |
||||
* @ORM\Column(name="id", type="bigint") |
||||
* @ORM\Id |
||||
* @ORM\GeneratedValue |
||||
*/ |
||||
protected $id; |
||||
|
||||
/** |
||||
* @var int |
||||
* |
||||
* @Gedmo\SortableGroup |
||||
* @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CExerciseCategory", inversedBy="children") |
||||
* @ORM\JoinColumn(referencedColumnName="id", onDelete="SET NULL") |
||||
* |
||||
* @ORM\Column(name="c_id", type="integer") |
||||
*/ |
||||
protected $cId; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="name", type="string", length=255, nullable=false) |
||||
*/ |
||||
protected $name; |
||||
|
||||
/** |
||||
* @var string |
||||
* |
||||
* @ORM\Column(name="description", type="text", nullable=true) |
||||
*/ |
||||
protected $description; |
||||
|
||||
/** |
||||
* @Gedmo\SortablePosition |
||||
* @ORM\Column(name="position", type="integer") |
||||
*/ |
||||
protected $position; |
||||
|
||||
/** |
||||
* @var \DateTime |
||||
* |
||||
* @ORM\Column(name="created_at", type="datetime", nullable=false) |
||||
*/ |
||||
protected $createdAt; |
||||
|
||||
/** |
||||
* @var \DateTime |
||||
* |
||||
* @ORM\Column(name="updated_at", type="datetime", nullable=false) |
||||
*/ |
||||
protected $updatedAt; |
||||
|
||||
/** |
||||
* Project constructor. |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
$this->createdAt = new \DateTime(); |
||||
$this->updatedAt = new \DateTime(); |
||||
} |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getId() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
/** |
||||
* @param int $id |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setId($id) |
||||
{ |
||||
$this->id = $id; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return int |
||||
*/ |
||||
public function getCId() |
||||
{ |
||||
return $this->cId; |
||||
} |
||||
|
||||
/** |
||||
* @param int $cId |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setCId($cId) |
||||
{ |
||||
$this->cId = $cId; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
/** |
||||
* @param string $name |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setName($name) |
||||
{ |
||||
$this->name = $name; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function getDescription() |
||||
{ |
||||
return $this->description; |
||||
} |
||||
|
||||
/** |
||||
* @param string $description |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setDescription($description) |
||||
{ |
||||
$this->description = $description; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return \DateTime |
||||
*/ |
||||
public function getCreatedAt() |
||||
{ |
||||
return $this->createdAt; |
||||
} |
||||
|
||||
/** |
||||
* @param \DateTime $createdAt |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setCreatedAt($createdAt) |
||||
{ |
||||
$this->createdAt = $createdAt; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return \DateTime |
||||
*/ |
||||
public function getUpdatedAt() |
||||
{ |
||||
return $this->updatedAt; |
||||
} |
||||
|
||||
/** |
||||
* @param \DateTime $updatedAt |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setUpdatedAt($updatedAt) |
||||
{ |
||||
$this->updatedAt = $updatedAt; |
||||
|
||||
return $this; |
||||
} |
||||
|
||||
/** |
||||
* @return mixed |
||||
*/ |
||||
public function getPosition() |
||||
{ |
||||
return $this->position; |
||||
} |
||||
|
||||
/** |
||||
* @param mixed $position |
||||
* |
||||
* @return CExerciseCategory |
||||
*/ |
||||
public function setPosition($position) |
||||
{ |
||||
$this->position = $position; |
||||
|
||||
return $this; |
||||
} |
||||
} |
Loading…
Reference in new issue