123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CoreBundle\Entity\GradebookCategory;
- use Doctrine\Common\Collections\Criteria;
- use Knp\Component\Pager\Paginator;
- require_once __DIR__.'/../inc/global.inc.php';
- api_protect_admin_script();
- $allow = api_get_configuration_value('gradebook_dependency');
- if ($allow == false) {
- api_not_allowed(true);
- }
- $em = Database::getManager();
- $repo = $em->getRepository('ChamiloCoreBundle:GradebookCategory');
- $maxItems = 20;
- $page = isset($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
- $categoryId = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 1;
- $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
- $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
- if (empty($keyword)) {
- $gradeBookList = $repo->findAll();
- } else {
- $criteria = new Criteria();
- $criteria->where(
- Criteria::expr()->orX(
- Criteria::expr()->contains('courseCode', $keyword),
- Criteria::expr()->contains('name', $keyword)
- )
- );
- $gradeBookList = $repo->matching($criteria);
- }
- $currentUrl = api_get_self().'?';
- $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
- $contentForm = '';
- $toolbar = Display::url(
- Display::return_icon('add.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
- $currentUrl.'&action=add'
- );
- $toolName = get_lang('Assessments');
- switch ($action) {
- case 'add':
- case 'edit':
- $interbreadcrumb[] = [
- 'url' => $currentUrl,
- 'name' => get_lang('Assessments'),
- ];
- $toolName = get_lang(ucfirst($action));
- break;
- }
- $tpl = new Template($toolName);
- switch ($action) {
- case 'add':
- $toolbar = Display::url(
- Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
- $currentUrl
- );
- $form = new FormValidator(
- 'category_add',
- 'post',
- $currentUrl.'&action=add'
- );
- $form->addText('name', get_lang('Name'));
- $form->addText('weight', get_lang('Weight'));
- $form->addSelectAjax(
- 'course_id',
- get_lang('Course'),
- null,
- [
- 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
- ]
- );
- $form->addSelectAjax(
- 'depends',
- get_lang('DependsOnAssessments'),
- null,
- [
- 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
- 'multiple' => 'multiple',
- ]
- );
- $form->addText(
- 'gradebooks_to_validate_in_dependence',
- get_lang('NumberOfAssessmentsToValidateInDependence')
- );
- $form->addText(
- 'minimum',
- get_lang('MinimumAssessmentsToValidate'),
- false
- );
- $form->addButtonSave(get_lang('Add'));
- $contentForm = $form->returnForm();
- if ($form->validate()) {
- $values = $form->getSubmitValues();
- $courseId = isset($values['course_id']) ? $values['course_id'] : 0;
- $courseInfo = api_get_course_info_by_id($courseId);
- $courseEntity = api_get_course_entity($courseId);
- $courseCode = $courseInfo['code'];
- $criteria = ['courseCode' => $courseCode];
- $exists = $repo->findBy($criteria);
- if (empty($exists) || empty($courseId)) {
- if (empty($courseId)) {
- $courseCode = '';
- }
- $category = new GradebookCategory();
- $category
- ->setName($values['name'])
- ->setWeight($values['weight'])
- ->setVisible(1)
- ->setLocked(0)
- ->setGenerateCertificates(0)
- ->setIsRequirement(false)
- ->setCourse($courseEntity)
- ->setUser(api_get_user_entity(api_get_user_id()));
- $em->persist($category);
- $em->flush();
- if ($category->getId()) {
- $params = [];
- if (!empty($values['depends'])) {
- $depends = $values['depends'];
- $depends = array_map('intval', $depends);
- $value = serialize($depends);
- $params['depends'] = $value;
- }
- if (!empty($values['minimum'])) {
- $params['minimum_to_validate'] = (int) $values['minimum'];
- }
- if (!empty($values['gradebooks_to_validate_in_dependence'])) {
- $params['gradebooks_to_validate_in_dependence'] = (int) $values['gradebooks_to_validate_in_dependence'];
- }
- if (!empty($params)) {
- Database::update(
- $table,
- $params,
- ['id = ?' => $category->getId()]
- );
- }
- Display::addFlash(Display::return_message(get_lang('Added')));
- header('Location: '.$currentUrl);
- exit;
- }
- } else {
- Display::addFlash(Display::return_message(get_lang('Category exists')));
- }
- }
- break;
- case 'edit':
- $toolbar = Display::url(
- Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
- $currentUrl
- );
- /** @var GradebookCategory $category */
- $category = $repo->find($categoryId);
- if (!empty($category)) {
- $form = new FormValidator(
- 'category_edit',
- 'post',
- $currentUrl.'&action=edit&id='.$categoryId
- );
- $form->addText('name', get_lang('Name'));
- $form->addText('weight', get_lang('Weight'));
- $form->addLabel(get_lang('Course'), $category->getCourseCode());
- $sql = "SELECT
- depends,
- minimum_to_validate,
- gradebooks_to_validate_in_dependence
- FROM $table WHERE id = ".$categoryId;
- $result = Database::query($sql);
- $categoryData = Database::fetch_array($result, 'ASSOC');
- $options = [];
- if (!empty($categoryData['depends'])) {
- $list = UnserializeApi::unserialize('not_allowed_classes', $categoryData['depends']);
- foreach ($list as $itemId) {
- $courseInfo = api_get_course_info_by_id($itemId);
- $options[$itemId] = $courseInfo['name'];
- }
- }
- $form->addSelectAjax(
- 'depends',
- get_lang('DependsOnAssessments'),
- $options,
- [
- 'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
- 'multiple' => 'multiple',
- ]
- );
- $form->addText(
- 'gradebooks_to_validate_in_dependence',
- get_lang('NumberOfAssessmentsToValidateInDependence')
- );
- $form->addText(
- 'minimum',
- get_lang('MinimumAssessmentsToValidate'),
- false
- );
- $form->addButtonSave(get_lang('Edit'));
- $defaults = [
- 'name' => $category->getName(),
- 'weight' => $category->getWeight(),
- 'gradebooks_to_validate_in_dependence' => $categoryData['gradebooks_to_validate_in_dependence'],
- 'depends' => array_keys($options),
- 'minimum' => $categoryData['minimum_to_validate'],
- ];
- $form->setDefaults($defaults);
- $contentForm = $form->returnForm();
- if ($form->validate()) {
- $values = $form->getSubmitValues();
- $category->setName($values['name']);
- $category->setWeight($values['weight']);
- $em->merge($category);
- $em->flush();
- if (!empty($values['depends'])) {
- $depends = $values['depends'];
- $depends = array_map('intval', $depends);
- $value = serialize($depends);
- $params['depends'] = $value;
- }
- if (!empty($values['minimum'])) {
- $params['minimum_to_validate'] = (int) $values['minimum'];
- }
- if (!empty($values['gradebooks_to_validate_in_dependence'])) {
- $params['gradebooks_to_validate_in_dependence'] = (int) $values['gradebooks_to_validate_in_dependence'];
- }
- if (!empty($params)) {
- Database::update(
- $table,
- $params,
- ['id = ?' => $category->getId()]
- );
- }
- Display::addFlash(Display::return_message(get_lang('Update successful')));
- header('Location: '.$currentUrl);
- exit;
- }
- }
- break;
- case 'list':
- default:
- $paginator = new Paginator();
- $pagination = $paginator->paginate(
- $gradeBookList,
- $page,
- $maxItems
- );
- // pagination.tpl needs current_url with out "page" param
- $pagination->setCustomParameters(['current_url' => $currentUrl]);
- $pagination->renderer = function ($data) use ($tpl) {
- foreach ($data as $key => $value) {
- $tpl->assign($key, $value);
- }
- $layout = $tpl->get_template('admin/pagination.tpl');
- $content = $tpl->fetch($layout);
- return $content;
- };
- break;
- }
- $searchForm = new FormValidator(
- 'course_filter',
- 'get',
- '',
- '',
- [],
- FormValidator::LAYOUT_INLINE
- );
- $searchForm->addText('keyword', '', false);
- $searchForm->addButtonSearch(get_lang('Search'));
- $tpl->assign('current_url', $currentUrl);
- $tpl->assign(
- 'actions',
- Display::toolbarAction(
- 'toolbar',
- [$toolbar, $searchForm->returnForm()],
- [1, 4]
- )
- );
- $tpl->assign('form', $contentForm);
- if (!empty($pagination)) {
- $tpl->assign('gradebook_list', $pagination);
- }
- $layout = $tpl->get_template('admin/gradebook_list.tpl');
- $tpl->display($layout);
|