LP: Use entity and resources to check visibility. WIP

pull/3064/head
Julio 6 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 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(); api_not_allowed();
} }

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

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

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

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

@ -1031,7 +1031,7 @@ switch ($action) {
// Teachers can export to PDF // Teachers can export to PDF
if (!$is_allowed_to_edit) { 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(); api_not_allowed();
} }
} }

@ -257,7 +257,7 @@ foreach ($categories as $item) {
continue; 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. // Check if the learnpath is visible for student.
if (!$is_allowed_to_edit) { if (!$is_allowed_to_edit) {

@ -39,10 +39,17 @@ $course_code = api_get_course_id();
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
$user_id = api_get_user_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) // Check if the learning path is visible for student - (LP requisites)
if (!api_is_platform_admin()) { if (!api_is_platform_admin()) {
if (!api_is_allowed_to_edit(null, true, false, false) && 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); api_not_allowed(true);
} }
@ -64,18 +71,6 @@ if ($visibility === 0 &&
api_not_allowed(true); 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(); $lp_item_id = $lp->get_current_item_id();
$lpType = $lp->get_type(); $lpType = $lp->get_type();

@ -4,7 +4,7 @@
namespace Chamilo\CourseBundle\Repository; 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\ResourceRepository;
use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface; use Chamilo\CoreBundle\Repository\ResourceWithLinkInterface;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
@ -14,9 +14,9 @@ use Symfony\Component\Routing\RouterInterface;
*/ */
final class CQuizRepository extends ResourceRepository implements ResourceWithLinkInterface 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)) { if (!empty($extraParams)) {
$params = array_merge($params, $extraParams); $params = array_merge($params, $extraParams);
} }

Loading…
Cancel
Save