LP: Use entity and resources to check visibility. WIP

pull/3064/head
Julio 5 years ago
parent 6869525522
commit 3eecb9d287
  1. 2
      public/main/document/download_scorm.php
  2. 5
      public/main/exercise/exercise.class.php
  3. 16
      public/main/inc/lib/api.lib.php
  4. 2
      public/main/lp/download.php
  5. 102
      public/main/lp/learnpath.class.php
  6. 16
      public/main/lp/learnpathList.class.php
  7. 2
      public/main/lp/lp_controller.php
  8. 2
      public/main/lp/lp_list.php
  9. 21
      public/main/lp/lp_view.php
  10. 6
      src/CourseBundle/Repository/CQuizRepository.php

@ -29,7 +29,7 @@ if (empty($obj)) {
}
// If is visible for the current user
if (!learnpath::is_lp_visible_for_student($obj->get_id(), api_get_user_id(), $_course)) {
if (!learnpath::is_lp_visible_for_student($obj->getEntity(), api_get_user_id(), $_course)) {
api_not_allowed();
}

@ -6232,9 +6232,10 @@ class Exercise
];
}
} else {
$lp = Container::getLpRepository()->find($lpId);
// 2.1 LP is loaded
if ($this->active == 0 &&
!learnpath::is_lp_visible_for_student($lpId, api_get_user_id())
if ($lp && $this->active == 0 &&
!learnpath::is_lp_visible_for_student($lp, api_get_user_id())
) {
return [
'value' => false,

@ -2050,7 +2050,7 @@ function api_get_course_entity($courseId = 0)
/**
* @param int $id
*
* @return SessionEntity
* @return SessionEntity|null
*/
function api_get_session_entity($id = 0)
{
@ -2058,6 +2058,10 @@ function api_get_session_entity($id = 0)
$id = api_get_session_id();
}
if (empty($id)) {
return null;
}
return Database::getManager()->getRepository('ChamiloCoreBundle:Session')->find($id);
}
@ -3719,6 +3723,16 @@ function api_get_item_visibility(
return -1;
}
// 0 visible
// 1 visible
// 2 deleted
switch ($tool) {
case 'learnpath':
break;
}
$tool = Database::escape_string($tool);
$id = (int) $id;
$session = (int) $session;

@ -40,7 +40,7 @@ if ($lp) {
$lp_item_id = $lp->current;
$lp_item_info = new learnpathItem($lp_item_id);
if (!empty($lp_item_info)) {
$visible = learnpath::is_lp_visible_for_student($lp_id, $user_id);
$visible = learnpath::is_lp_visible_for_student($lp->getEntity(), $user_id);
if ($visible) {
Event::event_download($doc_url);

@ -7,7 +7,6 @@ use Chamilo\CoreBundle\Repository\CourseRepository;
use Chamilo\CourseBundle\Component\CourseCopy\CourseArchiver;
use Chamilo\CourseBundle\Component\CourseCopy\CourseBuilder;
use Chamilo\CourseBundle\Component\CourseCopy\CourseRestorer;
use Chamilo\CourseBundle\Entity\CItemProperty;
use Chamilo\CourseBundle\Entity\CLp;
use Chamilo\CourseBundle\Entity\CLpCategory;
use Chamilo\CourseBundle\Entity\CLpItem;
@ -97,6 +96,7 @@ class learnpath
public $course_int_id;
public $course_info = [];
public $categoryId;
public $entity;
/**
* Constructor.
@ -111,9 +111,6 @@ class learnpath
{
$debug = $this->debug;
$this->encoding = api_get_system_encoding();
if ($debug) {
error_log('In learnpath::__construct('.$course.','.$lp_id.','.$user_id.')');
}
if (empty($course)) {
$course = api_get_course_id();
}
@ -133,54 +130,48 @@ class learnpath
if (empty($lp_id) || empty($course_id)) {
$this->error = "Parameter is empty: LpId:'$lp_id', courseId: '$lp_id'";
} else {
// TODO: Make it flexible to use any course_code (still using env course code here).
$lp_table = Database::get_course_table(TABLE_LP_MAIN);
$sql = "SELECT * FROM $lp_table
WHERE iid = $lp_id";
if ($debug) {
error_log('learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0);
}
$res = Database::query($sql);
if (Database::num_rows($res) > 0) {
$repo = Container::getLpRepository();
/** @var CLp $entity */
$entity = $repo->find($lp_id);
if ($entity) {
$this->entity = $entity;
$this->lp_id = $lp_id;
$row = Database::fetch_array($res);
$this->type = $row['lp_type'];
$this->name = stripslashes($row['name']);
$this->proximity = $row['content_local'];
$this->theme = $row['theme'];
$this->maker = $row['content_maker'];
$this->prevent_reinit = $row['prevent_reinit'];
$this->seriousgame_mode = $row['seriousgame_mode'];
$this->license = $row['content_license'];
$this->scorm_debug = $row['debug'];
$this->js_lib = $row['js_lib'];
$this->path = $row['path'];
$this->preview_image = $row['preview_image'];
$this->author = $row['author'];
$this->hide_toc_frame = $row['hide_toc_frame'];
$this->lp_session_id = $row['session_id'];
$this->use_max_score = $row['use_max_score'];
$this->subscribeUsers = $row['subscribe_users'];
$this->created_on = $row['created_on'];
$this->modified_on = $row['modified_on'];
$this->ref = $row['ref'];
$this->categoryId = $row['category_id'];
$this->accumulateScormTime = isset($row['accumulate_scorm_time']) ? $row['accumulate_scorm_time'] : 'true';
$this->accumulateWorkTime = isset($row['accumulate_work_time']) ? $row['accumulate_work_time'] : 0;
if (!empty($row['publicated_on'])) {
$this->publicated_on = $row['publicated_on'];
}
if (!empty($row['expired_on'])) {
$this->expired_on = $row['expired_on'];
$this->type = $entity->getLpType();
$this->name = stripslashes($entity->getName());
$this->proximity = $entity->getContentLocal();
$this->theme = $entity->getTheme();
$this->maker = $entity->getContentLocal();
$this->prevent_reinit = $entity->getPreventReinit();
$this->seriousgame_mode = $entity->getSeriousgameMode();
$this->license = $entity->getContentLicense();
$this->scorm_debug = $entity->getDebug();
$this->js_lib = $entity->getJsLib();
$this->path = $entity->getPath();
$this->preview_image = $entity->getPreviewImage();
$this->author = $entity->getAuthor();
$this->hide_toc_frame = $entity->getHideTocFrame();
$this->lp_session_id = $entity->getSessionId();
$this->use_max_score = $entity->getUseMaxScore();
$this->subscribeUsers = $entity->getSubscribeUsers();
$this->created_on = $entity->getCreatedOn()->format('Y-m-d H:i:s');
$this->modified_on = $entity->getModifiedOn()->format('Y-m-d H:i:s');
$this->ref = $entity->getRef();
$this->categoryId = $entity->getCategoryId();
$this->accumulateScormTime = $entity->getAccumulateWorkTime();
if (!empty($entity->getPublicatedOn())) {
$this->publicated_on = $entity->getPublicatedOn()->format('Y-m-d H:i:s');
}
if (!empty($entity->getExpiredOn())) {
$this->expired_on = $entity->getExpiredOn()->format('Y-m-d H:i:s');
}
if ($this->type == 2) {
if ($row['force_commit'] == 1) {
if ($entity->getForceCommit() == 1) {
$this->force_commit = true;
}
}
$this->mode = $row['default_view_mod'];
$this->mode = $entity->getDefaultViewMod();
// Check user ID.
if (empty($user_id)) {
@ -225,9 +216,6 @@ class learnpath
$this->progress_db = $row['progress'];
$this->lp_view_session_id = $row['session_id'];
} elseif (!api_is_invitee()) {
if ($debug) {
error_log('learnpath::__construct() '.__LINE__.' - NOT Found previous view');
}
$this->attempt = 1;
$params = [
'c_id' => $course_id,
@ -431,6 +419,11 @@ class learnpath
}
}
public function getEntity(): CLp
{
return $this->entity;
}
/**
* @return string
*/
@ -2216,13 +2209,12 @@ class learnpath
* @return bool
*/
public static function is_lp_visible_for_student(
$lp_id,
CLp $lp,
$student_id,
$courseInfo = [],
$sessionId = 0
) {
$courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
$lp_id = (int) $lp_id;
$sessionId = (int) $sessionId;
if (empty($courseInfo)) {
@ -2235,18 +2227,20 @@ class learnpath
$courseId = $courseInfo['real_id'];
$itemInfo = api_get_item_property_info(
/*$itemInfo = api_get_item_property_info(
$courseId,
TOOL_LEARNPATH,
$lp_id,
$sessionId
);
);*/
$visibility = $lp->isVisible($courseInfo['entity'], api_get_session_entity($sessionId));
// If the item was deleted.
if (isset($itemInfo['visibility']) && $itemInfo['visibility'] == 2) {
if ($visibility === false) {
return false;
}
$lp_id = $lp->getIid();
// @todo remove this query and load the row info as a parameter
$table = Database::get_course_table(TABLE_LP_MAIN);
// Get current prerequisite

@ -104,11 +104,13 @@ class LearnpathList
$showBlockedPrerequisite = api_get_configuration_value('show_prerequisite_as_blocked');
$names = [];
$isAllowToEdit = api_is_allowed_to_edit();
$courseEntity = api_get_course_entity($course_id);
$sessionEntity = api_get_session_entity($session_id);
/** @var CLp $row */
foreach ($learningPaths as $row) {
$name = Database::escape_string($row->getName());
$link = 'lp/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
$oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
//$name = Database::escape_string($row->getName());
//$link = 'lp/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
//$oldLink = 'newscorm/lp_controller.php?action=view&lp_id='.$row->getId().'&id_session='.$session_id;
/*$sql2 = "SELECT visibility FROM $tbl_tool
WHERE
@ -128,18 +130,19 @@ class LearnpathList
}*/
// Check if visible.
$visibility = api_get_item_visibility(
/*$visibility = api_get_item_visibility(
$courseInfo,
'learnpath',
$row->getId(),
$session_id
);
);*/
$visibility = $row->isVisible($courseEntity, $sessionEntity);
// If option is not true then don't show invisible LP to user
if ($ignoreLpVisibility === false) {
if ($showBlockedPrerequisite !== true && !$isAllowToEdit) {
$lpVisibility = learnpath::is_lp_visible_for_student(
$row->getId(),
$row,
$user_id,
$courseInfo
);
@ -178,6 +181,7 @@ class LearnpathList
'lp_old_id' => $row->getId(),
'iid' => $row->getIid(),
'prerequisite' => $row->getPrerequisite(),
'entity' => $row,
];
$names[$row->getName()] = $row->getIid();
}

@ -1031,7 +1031,7 @@ switch ($action) {
// Teachers can export to PDF
if (!$is_allowed_to_edit) {
if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->lp_id, api_get_user_id(), $_course)) {
if (!learnpath::is_lp_visible_for_student($_SESSION['oLP']->getEntity(), api_get_user_id(), $_course)) {
api_not_allowed();
}
}

@ -257,7 +257,7 @@ foreach ($categories as $item) {
continue;
}
$lpVisibility = learnpath::is_lp_visible_for_student($id, $userId, $courseInfo);
$lpVisibility = learnpath::is_lp_visible_for_student($details['entity'], $userId, $courseInfo);
// Check if the learnpath is visible for student.
if (!$is_allowed_to_edit) {

@ -39,10 +39,17 @@ $course_code = api_get_course_id();
$course_id = api_get_course_int_id();
$user_id = api_get_user_id();
/** @var learnpath $lp */
$lp = Session::read('oLP');
if (empty($lp)) {
api_not_allowed(true);
}
// Check if the learning path is visible for student - (LP requisites)
if (!api_is_platform_admin()) {
if (!api_is_allowed_to_edit(null, true, false, false) &&
!learnpath::is_lp_visible_for_student($lp_id, api_get_user_id())
!learnpath::is_lp_visible_for_student($lp->getEntity(), api_get_user_id())
) {
api_not_allowed(true);
}
@ -64,18 +71,6 @@ if ($visibility === 0 &&
api_not_allowed(true);
}
/** @var learnpath $lp */
$lp = Session::read('oLP');
if (empty($lp)) {
api_not_allowed(true);
}
$debug = 0;
if ($debug) {
error_log('------ Entering lp_view.php -------');
}
$lp_item_id = $lp->get_current_item_id();
$lpType = $lp->get_type();

@ -4,7 +4,7 @@
namespace Chamilo\CourseBundle\Repository;
use Chamilo\CoreBundle\Entity\Resource\AbstractResource;
use Chamilo\CoreBundle\Entity\Resource\ResourceInterface;
use Chamilo\CoreBundle\Repository\ResourceRepository;
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
use Symfony\Component\Routing\RouterInterface;
@ -14,9 +14,9 @@ use Symfony\Component\Routing\RouterInterface;
*/
final class CQuizRepository extends ResourceRepository implements ResourceWithLinkInterface
{
public function getLink(AbstractResource $exercise, RouterInterface $router, $extraParams = []): string
public function getLink(ResourceInterface $exercise, RouterInterface $router, $extraParams = []): string
{
$params = ['name' => 'exercise/overview.php', 'exerciseId' => $exercise->getIid()];
$params = ['name' => 'exercise/overview.php', 'exerciseId' => $exercise->getResourceIdentifier()];
if (!empty($extraParams)) {
$params = array_merge($params, $extraParams);
}

Loading…
Cancel
Save