Browse Source

Additional gradebook dependencies BT#13099
Requires DB changes:

ALTER TABLE gradebook_category ADD COLUMN depends TEXT DEFAULT NULL;
ALTER TABLE gradebook_category ADD COLUMN minimum_to_validate INT DEFAULT NULL;

jmontoyaa 7 years ago
parent
commit
2764fbcafe

+ 253 - 0
main/admin/gradebook_list.php

@@ -0,0 +1,253 @@
+<?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');
+
+$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 \Doctrine\Common\Collections\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 = '';
+
+switch ($action) {
+    case 'add':
+        $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('DependsOnGradebook'),
+            null,
+            [
+                'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
+                'multiple' => 'multiple'
+            ]
+        );
+
+        $form->addText('minimum', get_lang('MinimumGradebookToValidate'));
+
+        $form->addButtonSave(get_lang('Add'));
+        $contentForm = $form->returnForm();
+        if ($form->validate()) {
+            $values = $form->getSubmitValues();
+            $courseId = $values['course_id'];
+            $courseInfo = api_get_course_info_by_id($courseId);
+            $courseCode = $courseInfo['code'];
+            $criteria = ['courseCode' => $courseCode];
+            $exists = $repo->findBy($criteria);
+            if (empty($exists)) {
+                $category = new GradebookCategory();
+                $category
+                    ->setName($values['name'])
+                    ->setWeight($values['weight'])
+                    ->setVisible(1)
+                    ->setLocked(0)
+                    ->setGenerateCertificates(0)
+                    ->setIsRequirement(false)
+                    ->setCourseCode($courseCode)
+                    ->setUserId(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($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('CategoryExists')));
+            }
+        }
+        break;
+    case 'edit':
+        /** @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->addText('minimum', get_lang('MinimumGradebookToValidate'));
+            $form->addLabel(get_lang('CourseCode'), $category->getCourseCode());
+
+             $sql = "SELECT depends, minimum_to_validate 
+                    FROM $table WHERE id = ".$categoryId;
+            $result = Database::query($sql);
+            $categoryData = Database::fetch_array($result, 'ASSOC');
+
+            $options = [];
+            if (!empty($categoryData['depends'])) {
+                $list = unserialize($categoryData['depends']);
+                foreach ($list as $itemId) {
+                    $courseInfo = api_get_course_info_by_id($itemId);
+                    $options[$itemId] = $courseInfo['name'];
+                }
+            }
+
+            $form->addSelectAjax(
+                'depends',
+                get_lang('DependsOnGradebook'),
+                $options,
+                [
+                    'url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course',
+                    'multiple' => 'multiple'
+                ]
+            );
+
+            $form->addButtonSave(get_lang('Edit'));
+            $defaults = [
+                'name' => $category->getName(),
+                'weight' => $category->getWeight(),
+                '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($params)) {
+                    Database::update(
+                        $table,
+                        $params,
+                        ['id = ?' => $category->getId()]
+                    );
+                }
+
+                Display::addFlash(Display::return_message(get_lang('Updated')));
+                header('Location: '.$currentUrl);
+                exit;
+            }
+        }
+        break;
+}
+
+$paginator = new Paginator;
+$pagination = $paginator->paginate(
+    $gradeBookList,
+    $page,
+    5
+);
+
+// pagination.tpl needs current_url with out "page" param
+$pagination->setCustomParameters(['current_url' => $currentUrl]);
+
+$tpl = new Template(get_lang('Gradebook'));
+$toolbar = Display::url(
+    Display::return_icon('add.png', get_lang('Add'), [], ICON_SIZE_MEDIUM),
+    $currentUrl.'&action=add'
+);
+
+$searchForm = new FormValidator(
+    'course_filter',
+    'get',
+    '',
+    '',
+    array(),
+    FormValidator::LAYOUT_INLINE
+);
+$searchForm->addText('keyword', '', false);
+$searchForm->addButtonSearch(get_lang('Search'));
+
+
+$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;
+};
+
+$tpl->assign('current_url', $currentUrl);
+$tpl->assign(
+    'actions',
+    Display::toolbarAction(
+        'toolbar',
+        [$toolbar, $searchForm->returnForm()],
+        [1, 4]
+    )
+);
+$tpl->assign('form', $contentForm);
+$tpl->assign('gradebook_list', $pagination);
+$layout = $tpl->get_template('admin/gradebook_list.tpl');
+$tpl->display($layout);

+ 25 - 1
main/admin/index.php

@@ -452,12 +452,36 @@ if (api_is_platform_admin()) {
         $blocks['skills']['extra'] = null;
         $blocks['skills']['search_form'] = null;
     }
+
+    $allow = api_get_configuration_value('gradebook_dependency');
+    if ($allow) {
+        $blocks['gradebook']['icon'] = Display::return_icon(
+            'gradebook.png',
+            get_lang('Gradebook'),
+            array(),
+            ICON_SIZE_MEDIUM,
+            false
+        );
+        $blocks['gradebook']['label'] = get_lang('Gradebook');
+        $blocks['gradebook']['class'] = 'block-admin-gradebook';
+
+        $items = array();
+        $items[] = array(
+            'url' => 'gradebook_list.php',
+            'label' => get_lang('List')
+        );
+        $blocks['gradebook']['items'] = $items;
+        $blocks['gradebook']['extra'] = null;
+        $blocks['gradebook']['search_form'] = null;
+    }
 }
 
 if (api_is_platform_admin()) {
     /* Plugins */
     global $_plugins;
-    if (isset($_plugins['menu_administrator']) && count($_plugins['menu_administrator']) > 0) {
+    if (isset($_plugins['menu_administrator']) &&
+        count($_plugins['menu_administrator']) > 0
+    ) {
         $menuAdministratorItems = [];
 
         $plugin_obj = new AppPlugin();

+ 4 - 0
main/install/configuration.dist.php

@@ -579,3 +579,7 @@ $_configuration['score_grade_model'] = [
 // Skills can only visible for admins, teachers (related to a user via a course),
 // and HRM users (if related to a user).
 // $_configuration['allow_private_skills'] = false;
+// Additional gradebook dependencies BT#13099
+// ALTER TABLE gradebook_category ADD COLUMN depends TEXT DEFAULT NULL;
+// ALTER TABLE gradebook_category ADD COLUMN minimum_to_validate INT DEFAULT NULL;
+// $_configuration['gradebook_dependency'] = false;

+ 32 - 0
main/template/default/admin/gradebook_list.tpl

@@ -0,0 +1,32 @@
+{% extends template ~ "/layout/layout_1_col.tpl" %}
+
+{% block content %}
+    {{ form }}
+    <table class="table promotions">
+        <thead class="title">
+            <tr>
+                <th>{{ 'Name' | get_lang }}</th>
+                <th>{{ 'Course' | get_lang }}</th>
+                <th>{{ 'Actions' | get_lang }} </th>
+            </tr>
+        </thead>
+
+    {% for item in gradebook_list %}
+        <tr>
+            <td>
+                {{ item.name }}
+            </td>
+            <td>
+                {{ item.courseCode }}
+            </td>
+            <td>
+                <a href="{{ current_url }}&action=edit&id={{ item.id }}">
+                    <img src="{{ 'edit.png'|icon(22) }}" />
+                </a>
+            </td>
+        </tr>
+    {% endfor %}
+    </table>
+
+   {{ gradebook_list }}
+{% endblock %}