Browse Source

Allow to platform admin and DRH assign skill to users - refs #7901

Angel Fernando Quiroz Campos 9 years ago
parent
commit
97c36bb23f

+ 34 - 0
app/Migrations/Schema/V20/Version20151019170300.php

@@ -0,0 +1,34 @@
+<?php
+/* For licensing terms, see /license.txt */
+namespace Application\Migrations\Schema\V20;
+
+use Application\Migrations\AbstractMigrationChamilo;
+use Doctrine\DBAL\Schema\Schema;
+
+/**
+ * Migrations for assigning skills
+ *
+ * @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
+ */
+class Version20151019170300 extends AbstractMigrationChamilo
+{
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        $skillRelUser = $schema->getTable('skill_rel_user');
+        $skillRelUser->addColumn('argumentation', \Doctrine\DBAL\Types\Type::TEXT);
+        $skillRelUser->getColumn('course_id')->setNotnull(false);
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        $skillRelUser = $schema->getTable('skill_rel_user');
+        $skillRelUser->dropColumn('argumentation');
+        $skillRelUser->getColumn('course_id')->setNotnull(true);
+    }
+}

+ 9 - 0
main/admin/user_list.php

@@ -528,6 +528,15 @@ function modify_filter($user_id, $url_params, $row) {
 		}
 	}
 
+    $alloAssignSkill = api_is_platform_admin(false, true);
+
+    if ($alloAssignSkill) {
+        $result .= Display::url(
+            Display::return_icon('skill-badges.png', get_lang('AssignSkill'), null, ICON_SIZE_SMALL),
+            get_lang('AssignSkill')
+        );
+    }
+
 	if ($is_admin) {
 		$result .= Display::return_icon('admin_star.png', get_lang('IsAdministrator'),array('width'=> ICON_SIZE_SMALL, 'heigth'=> ICON_SIZE_SMALL));
 	} else {

+ 100 - 0
main/badge/assign.php

@@ -0,0 +1,100 @@
+<?php
+/* For license terms, see /license.txt */
+use \Chamilo\CoreBundle\Entity\Skill;
+use \Chamilo\CoreBundle\Entity\SkillRelUser;
+
+require_once '../inc/global.inc.php';
+
+if (!api_is_platform_admin(false, true)) {
+    api_not_allowed();
+}
+
+$entityManager = Database::getManager();
+$skillRepo = $entityManager->getRepository('ChamiloCoreBundle:Skill');
+$skillUserRepo = $entityManager->getRepository('ChamiloCoreBundle:SkillRelUser');
+$user = $entityManager->find('ChamiloUserBundle:User', $_REQUEST['user']);
+
+if (!$user) {
+    Display::addFlash(
+        Display::return_message(get_lang('NoUser'), 'error')
+    );
+
+    header('Location: ' . api_get_path(WEB_PATH));
+    exit;
+}
+
+$skills = $skillRepo->findBy([
+    'status' => Skill::STATUS_ENABLED
+]);
+
+$skillsOptions = [];
+
+foreach ($skills as $skill) {
+    $skillsOptions[$skill->getId()] = $skill->getName();
+}
+
+$form = new FormValidator('assign_skill');
+$form->addText('user_name', get_lang('UserName'), false);
+$form->addSelect('skill', get_lang('Skill'), $skillsOptions);
+$form->addHidden('user', $user->getId());
+$form->addRule('skill', get_lang('ThisFieldIsRequired'), 'required');
+$form->addTextarea('argumentation', get_lang('Argumentation'), ['rows' => 6]);
+$form->applyFilter('argumentation', 'trim');
+$form->addRule('argumentation', get_lang('ThisFieldIsRequired'), 'required');
+$form->addButtonSave(get_lang('Save'));
+
+if ($form->validate()) {
+    $values = $form->exportValues();
+
+    $skill = $skillRepo->find($values['skill']);
+
+    if (!$skill) {
+        Display::addFlash(
+            Display::return_message(get_lang('SkillNotFound'), 'error')
+        );
+
+        header('Location: ' . api_get_self() . '?' . http_build_query(['user' => $user->getId()]));
+        exit;
+    }
+
+    if ($user->hasSkill($skill)) {
+        Display::addFlash(
+            Display::return_message(
+                sprintf(get_lang('TheUserXHasAlreadyAchievedTheSkillX'), $user->getCompleteName(), $skill->getName()),
+                'warning'
+            )
+        );
+
+        header('Location: ' . api_get_self() . '?' . http_build_query(['user' => $user->getId()]));
+        exit;
+    }
+
+    $skillUser = new SkillRelUser();
+    $skillUser->setUser($user);
+    $skillUser->setSkill($skill);
+    $skillUser->setArgumentation($values['argumentation']);
+    $skillUser->setAcquiredSkillAt(new DateTime());
+    $skillUser->setAssignedBy(0);
+
+    $entityManager->persist($skillUser);
+    $entityManager->flush();
+
+    Display::addFlash(
+        Display::return_message(
+            sprintf(get_lang('SkillXAssignedToUserX'), $skill->getName(), $user->getCompleteName()),
+            'success'
+        )
+    );
+
+    header('Location: ' . api_get_path(WEB_PATH) . "badge/{$skill->getId()}/user/{$user->getId()}");
+    exit;
+}
+
+$form->setDefaults(['user_name' => $user->getCompleteName()]);
+$form->freeze(['user_name']);
+
+//View
+$template = new Template('');
+$template->assign('header', get_lang('AssignSkill'));
+$template->assign('content', $form->returnForm());
+$template->display_one_col_template();

+ 10 - 1
main/mySpace/myStudents.php

@@ -24,6 +24,8 @@ if (!api_is_allowed_to_create_course() &&
     }
 }
 
+$alloAssignSkill = api_is_platform_admin(false, true);
+
 $htmlHeadXtra[] = '<script>
 function show_image(image,width,height) {
 	width = parseInt(width) + 20;
@@ -339,7 +341,14 @@ if (!empty($student_id)) {
     }
     if (api_can_login_as($student_id)) {
         echo '<a href="'.api_get_path(WEB_CODE_PATH).'admin/user_list.php?action=login_as&user_id='.$student_id.'&sec_token='.$token.'">'.
-            Display::return_icon('login_as.png', get_lang('LoginAs'), null, ICON_SIZE_MEDIUM).'</a>&nbsp;&nbsp;';
+            Display::return_icon('login_as.png', get_lang('LoginAs'), null, ICON_SIZE_MEDIUM).'</a>';
+    }
+
+    if ($alloAssignSkill) {
+        echo Display::url(
+            Display::return_icon('skill-badges.png', get_lang('AssignSkill'), null, ICON_SIZE_MEDIUM),
+            api_get_path(WEB_CODE_PATH) . 'badge/assign.php?' . http_build_query(['user' => $student_id])
+        );
     }
 
     echo '</div>';

+ 3 - 0
src/Chamilo/CoreBundle/Entity/Skill.php

@@ -12,6 +12,9 @@ use Doctrine\ORM\Mapping as ORM;
  */
 class Skill
 {
+    const STATUS_DISABLED = 0;
+    const STATUS_ENABLED = 1;
+
     /**
      * @var string
      *

+ 29 - 1
src/Chamilo/CoreBundle/Entity/SkillRelUser.php

@@ -47,7 +47,7 @@ class SkillRelUser
 
     /**
      * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course", inversedBy="issuedSkills", cascade={"persist"})
-     * @ORM\JoinColumn(name="course_id", referencedColumnName="id", nullable=false)
+     * @ORM\JoinColumn(name="course_id", referencedColumnName="id")
      */
     private $course;
 
@@ -66,6 +66,13 @@ class SkillRelUser
      */
     private $id;
 
+    /**
+     * @var string
+     *
+     * @ORM\Column(name="argumentation", type="text")
+     */
+    private $argumentation;
+
     /**
      * Set user
      * @param \Chamilo\UserBundle\Entity\User $user
@@ -205,4 +212,25 @@ class SkillRelUser
     {
         return $this->id;
     }
+
+    /**
+     * Set argumentation
+     * @param string $argumentation
+     * @return \Chamilo\CoreBundle\Entity\SkillRelUser
+     */
+    public function setArgumentation($argumentation)
+    {
+        $this->argumentation = $argumentation;
+
+        return $this;
+    }
+
+    /**
+     * Get argumentation
+     * @return string
+     */
+    public function getArgumentation()
+    {
+        return $this->argumentation;
+    }
 }

+ 18 - 0
src/Chamilo/UserBundle/Entity/User.php

@@ -1576,4 +1576,22 @@ class User extends BaseUser //implements ParticipantInterface, ThemeUser
     {
         return $this->achievedSkills;
     }
+
+    /**
+     * Check if the user has the skill
+     * @param \Chamilo\CoreBundle\Entity\Skill $skill The skill
+     * @return boolean
+     */
+    public function hasSkill(\Chamilo\CoreBundle\Entity\Skill $skill)
+    {
+        $achievedSkills = $this->getAchievedSkills();
+
+        foreach ($achievedSkills as $userSkill) {
+            if ($userSkill->getSkill()->getId() !== $skill->getId()) {
+                continue;
+            }
+
+            return true;
+        }
+    }
 }