Validate session subscription with sequences - refs BT#9893 #TMI

1.10.x
Angel Fernando Quiroz Campos 10 years ago
parent 555addfe85
commit 0e0cae4447
  1. 24
      main/auth/courses.php
  2. 65
      main/inc/ajax/sequence.ajax.php
  3. 117
      main/inc/lib/SecuenceResourceManager.php
  4. 16
      main/template/default/sequence_resource/session_requirements.tpl

@ -1,5 +1,6 @@
<?php
/* For licensing terms, see /license.txt */
use \Chamilo\CoreBundle\Entity\SequenceResource;
/**
* Template (front controller in MVC pattern) used for distpaching
@ -211,6 +212,29 @@ switch ($action) {
case 'subscribe_to_session':
$registrationAllowed = api_get_setting('catalog_allow_session_auto_subscription');
if ($registrationAllowed === 'true') {
$entityManager = Database::getManager();
$repository = $entityManager->getRepository('ChamiloCoreBundle:SequenceResource');
$sequences = $repository->getRequirements(
$_GET['session_id'],
SequenceResource::SESSION_TYPE
);
if (count($sequences) > 0) {
$requirementsData = SecuenceResourceManager::checkRequirementsForUser(
$sequences,
api_get_user_id(),
SequenceResource::SESSION_TYPE
);
$continueWithSubscription = SecuenceResourceManager::checkSequenceAreCompleted($requirementsData);
if (!$continueWithSubscription) {
header('Location: ' . api_get_path(WEB_CODE_PATH) . 'auth/courses.php');
exit;
}
}
SessionManager::suscribe_users_to_session(
$_GET['session_id'],
array($_GET['user_id'])

@ -290,10 +290,6 @@ switch ($action) {
case SequenceResource::SESSION_TYPE:
$session = api_get_session_info($id);
$gradebookCategoryRepo = $em->getRepository(
'ChamiloCoreBundle:GradebookCategory'
);
$sequences = $repository->getRequirements(
$session['id'],
$type
@ -303,68 +299,13 @@ switch ($action) {
break;
}
$data = [];
foreach ($sequences as $sequenceId => $sequence) {
$item = [
'name' => $sequence['name'],
'sessions' => []
];
foreach ($sequence['requirements'] as $sessionRequired) {
$itemSession = [
'name' => $sessionRequired->getName(),
'status' => true
];
$sessionsCourses = $sessionRequired->getCourses();
foreach ($sessionsCourses as $sessionCourse) {
$course = $sessionCourse->getCourse();
$gradebooks = $gradebookCategoryRepo->findBy([
'courseCode' => $course->getCode(),
'sessionId' => $sessionRequired->getId(),
'isRequirement' => true
]);
foreach ($gradebooks as $gradebook) {
$category = Category::createCategoryObjectFromEntity(
$gradebook
);
$itemSession['status'] = $itemSession['status'] && Category::userFinishedCourse(
$userId,
$category
);
}
}
$item['sessions'][$sessionRequired->getId()] = $itemSession;
}
$data[$sequenceId] = $item;
}
$allowSubscription = false;
foreach ($data as $secuence) {
$status = true;
foreach ($secuence['sessions'] as $item) {
$status = $status && $item['status'];
}
if ($status) {
$allowSubscription = true;
break;
}
}
$sequenceList = SecuenceResourceManager::checkRequirementsForUser($sequences, $userId, $type);
$allowSubscription = SecuenceResourceManager::checkSequenceAreCompleted($sequenceList);
$courseController = new CoursesController();
$view = new Template(null, false, false, false, false, false);
$view->assign('data', $data);
$view->assign('sequences', $sequenceList);
$view->assign('allow_subscription', $allowSubscription);
if ($allowSubscription) {

@ -0,0 +1,117 @@
<?php
/* For licensing terms, see /license.txt */
use \Chamilo\CoreBundle\Entity\SequenceResource;
/**
* SecuenceResourceManager class
* Helper for SequenceResource
*
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com>
*/
class SecuenceResourceManager
{
/**
* Check if the ser has completed the requirements for the sequences
* @param array $sequences The sequences
* @param int $userId The user ID
* @param int $type The type of sequence resource
* @return array
*/
public static function checkRequirementsForUser(array $sequences, $userId, $type)
{
$sequenceList = [];
switch ($type) {
case SequenceResource::SESSION_TYPE:
$sequenceList = self::checkSessionRequirementsForUser($sequences, $userId);
break;
}
return $sequenceList;
}
/**
* Check if the ser has completed the requirements for the session sequences
* @param array $sequences The sequences
* @param int $userId The user ID
* @return array
*/
private static function checkSessionRequirementsForUser(array $sequences, $userId)
{
$sequenceList = [];
$entityManager = Database::getManager();
$gradebookCategoryRepo = $entityManager->getRepository(
'ChamiloCoreBundle:GradebookCategory'
);
foreach ($sequences as $sequenceId => $sequence) {
$item = [
'name' => $sequence['name'],
'requirements' => []
];
foreach ($sequence['requirements'] as $sessionRequired) {
$itemSession = [
'name' => $sessionRequired->getName(),
'status' => true
];
$sessionsCourses = $sessionRequired->getCourses();
foreach ($sessionsCourses as $sessionCourse) {
$course = $sessionCourse->getCourse();
$gradebooks = $gradebookCategoryRepo->findBy([
'courseCode' => $course->getCode(),
'sessionId' => $sessionRequired->getId(),
'isRequirement' => true
]);
foreach ($gradebooks as $gradebook) {
$category = Category::createCategoryObjectFromEntity(
$gradebook
);
$itemSession['status'] = $itemSession['status'] && Category::userFinishedCourse(
$userId,
$category
);
}
}
$item['requirements'][$sessionRequired->getId()] = $itemSession;
}
$sequenceList[$sequenceId] = $item;
}
return $sequenceList;
}
/**
* Check if at least one sequence are completed
* @param array $sequences The sequences
* @param int $type The type of sequence resource
* @return boolean
*/
public static function checkSequenceAreCompleted(array $sequences)
{
foreach ($sequences as $secuence) {
$status = true;
foreach ($secuence['requirements'] as $item) {
$status = $status && $item['status'];
}
if ($status) {
return true;
}
}
return false;
}
}

@ -1,10 +1,10 @@
<h2 class="page-header">{{ 'SessionRequirements'|get_lang }}</h2>
{% for item in data %}
{% for item in sequences %}
<h4>{{ item.name }}</h4>
<div id="parents">
{% for session in item.sessions %}
{% for session in item.requirements %}
<div class="parent">
<div class="big-icon">
<img src="{{ 'item-sequence.png'|icon(48) }}">
@ -19,16 +19,14 @@
</div>
</div>
{% if loop.index != item.sessions|length %}
{% if loop.index != item.requirements|length %}
<i class="fa fa-plus fa-3x sequence-plus-icon"></i>
{% endif %}
{% endfor %}
</div>
{% endfor %}
<hr>
<p>
{% if allow_subscription %}
{{ subscribe_button }}
{% endif %}
</p>
{% if allow_subscription %}
<hr>
<p>{{ subscribe_button }}</p>
{% endif %}

Loading…
Cancel
Save