Internal: Update session course permissions in survey - refs BT#21604

pull/5698/head
christianbeeznst 1 year ago
parent 17ffb10396
commit 0b6c599bd4
  1. 48
      public/main/lp/learnpath.class.php
  2. 23
      public/main/survey/survey.lib.php
  3. 235
      public/main/survey/surveyUtil.class.php
  4. 4
      public/main/survey/survey_list.php
  5. 3
      src/CourseBundle/Entity/CSurvey.php

@ -5346,6 +5346,9 @@ class learnpath
// Get all the forums.
$forums = $this->get_forums();
// Get all surveys
$surveys = $this->getSurveys();
// Get the final item form (see BT#11048) .
$finish = $this->getFinalItemForm();
$size = ICON_SIZE_MEDIUM; //ICON_SIZE_BIG
@ -5356,6 +5359,7 @@ class learnpath
Display::getMdiIcon('inbox-full', 'ch-tool-icon-gradient', '', 64, get_lang('Assignments')),
Display::getMdiIcon('comment-quote', 'ch-tool-icon-gradient', '', 64, get_lang('Forums')),
Display::getMdiIcon('bookmark-multiple', 'ch-tool-icon-gradient', '', 64, get_lang('Add section')),
Display::getMdiIcon('form-dropdown', 'ch-tool-icon-gradient', '', 64, get_lang('Add survey')),
Display::getMdiIcon('certificate', 'ch-tool-icon-gradient', '', 64, get_lang('Certificate')),
];
$content = '';
@ -5375,6 +5379,7 @@ class learnpath
$works,
$forums,
$section,
$surveys,
$finish,
],
'resource_tab',
@ -6877,6 +6882,49 @@ class learnpath
return $return;
}
/**
* Creates a list with all the surveys in it.
*
* @return string
*/
public function getSurveys()
{
$return = '<ul class="mt-2 bg-white list-group lp_resource">';
// First add link
$return .= '<li class="list-group-item lp_resource_element disable_drag">';
$return .= Display::getMdiIcon('clipboard-question-outline', 'ch-tool-icon', null, 32, get_lang('CreateNewSurvey'));
$return .= Display::url(
get_lang('Create a new survey'),
api_get_path(WEB_CODE_PATH).'survey/create_new_survey.php?'.api_get_cidreq().'&'.http_build_query([
'action' => 'add',
'lp_id' => $this->lp_id,
]),
['title' => get_lang('Create a new survey')]
);
$return .= '</li>';
$surveys = SurveyManager::get_surveys(api_get_course_id(), api_get_session_id());
$moveIcon = Display::getMdiIcon('cursor-move', 'ch-tool-icon', '', 16, get_lang('Move'));
foreach ($surveys as $survey) {
if (!empty($survey['iid'])) {
$surveyTitle = strip_tags($survey['title']);
$return .= '<li class="list-group-item lp_resource_element" id="'.$survey['iid'].'" data-id="'.$survey['iid'].'">';
$return .= '<a class="moved" href="#">';
$return .= $moveIcon;
$return .= ' </a>';
$return .= Display::getMdiIcon('poll', 'ch-tool-icon', null, 16, get_lang('Survey'));
$return .= '<a class="moved link_with_id" data-id="'.$survey['iid'].'" data_type="'.TOOL_SURVEY.'" title="'.$surveyTitle.'" href="'.api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_SURVEY.'&survey_id='.$survey['iid'].'&lp_id='.$this->lp_id.'" style="vertical-align:middle">'.$surveyTitle.'</a>';
$return .= '</li>';
}
}
$return .= '</ul>';
return $return;
}
/**
* Temp function to be moved in main_api or the best place around for this.
* Creates a file path if it doesn't exist.

@ -100,25 +100,16 @@ class SurveyManager
* @return array
* @assert ('') === false
*/
public static function get_surveys($course_code, $session_id = 0)
public static function get_surveys($courseCode, int $sessionId = 0)
{
if (empty($course_code)) {
return false;
}
$course_info = api_get_course_info($course_code);
if (empty($course_info)) {
return false;
}
$sessionCondition = api_get_session_condition($session_id, true, true);
$courseId = api_get_course_int_id();
$repo = Container::getSurveyRepository();
$course = api_get_course_entity($courseId);
$session = api_get_session_entity($sessionId);
$table = Database::get_course_table(TABLE_SURVEY);
$sql = "SELECT * FROM $table
WHERE c_id = {$course_info['real_id']} $sessionCondition ";
$result = Database::query($sql);
$qb = $repo->getResourcesByCourse($course, $session, null, null, true, true);
return Database::store_result($result, 'ASSOC');
return $qb->getQuery()->getArrayResult();
}
/**

@ -2963,7 +2963,7 @@ class SurveyUtil
*
* @version January 2007
*/
public static function modify_filter($survey_id, $drh = false)
public static function modify_filter($survey_id, $url_params, $row)
{
$repo = Container::getSurveyRepository();
/** @var CSurvey|null $survey */
@ -2991,7 +2991,7 @@ class SurveyUtil
$codePath.'survey/reporting.php?'.http_build_query($params + ['survey_id' => $survey_id])
);
if ($drh) {
if (!$row['can_edit']) {
return $hideReportingButton ? '-' : $reportingLink;
}
@ -3265,29 +3265,23 @@ class SurveyUtil
* @version January 2007
*/
public static function get_survey_data(
$from,
$number_of_items,
$column,
$direction,
$isDrh = false
) {
int $from,
int $number_of_items,
int $column,
string $direction,
bool $isDrh = false
): array {
$repo = Container::getSurveyRepository();
$course = api_get_course_entity();
$sessionId = api_get_session_id();
$session = api_get_session_entity();
$qb = $repo->findAllByCourse($course, $session);
/** @var CSurvey[] $surveys */
$surveys = $qb->getQuery()->getResult();
$table_survey = Database::get_course_table(TABLE_SURVEY);
$table_user = Database::get_main_table(TABLE_MAIN_USER);
$table_survey_question = Database::get_course_table(TABLE_SURVEY_QUESTION);
$mandatoryAllowed = ('true' === api_get_setting('survey.allow_mandatory_survey'));
// Searching
$search_restriction = self::survey_search_restriction();
if ($search_restriction) {
$search_restriction = ' AND '.$search_restriction;
}
$from = (int) $from;
$number_of_items = (int) $number_of_items;
$column = (int) $column;
@ -3295,129 +3289,122 @@ class SurveyUtil
$direction = 'asc';
}
// Condition for the session
$session_id = api_get_session_id();
$condition_session = api_get_session_condition($session_id);
$course_id = api_get_course_int_id();
$sql = "
SELECT
survey.iid AS col0,
survey.title AS col1,
survey.code AS col2,
count(survey_question.iid) AS col3, "
.(api_is_western_name_order()
? "CONCAT(user.firstname, ' ', user.lastname)"
: "CONCAT(user.lastname, ' ', user.firstname)")
." AS col4,
survey.avail_from AS col5,
survey.avail_till AS col6,
survey.invited AS col7,
survey.anonymous AS col8,
survey.iid AS col9,
survey.session_id AS session_id,
survey.answered,
survey.invited,
survey.survey_type
FROM $table_survey survey
LEFT JOIN $table_survey_question survey_question
ON (survey.iid = survey_question.survey_id AND survey_question.c_id = $course_id)
LEFT JOIN $table_user user
ON (survey.author = user.id)
WHERE survey.c_id = $course_id
$search_restriction
$condition_session
GROUP BY survey.iid
ORDER BY col$column $direction
LIMIT $from,$number_of_items
";
$efv = new ExtraFieldValue('survey');
$list = [];
foreach ($surveys as $survey) {
$array = [];
$surveyId = $survey->getIid();
$array[0] = $surveyId;
$type = $survey->getSurveyType();
$title = $survey->getTitle();
if (self::checkHideEditionToolsByCode($survey->getCode())) {
$array[1] = $title;
} else {
// Doodle
if (3 == $type) {
$array[1] = Display::url(
$title,
api_get_path(WEB_CODE_PATH).'survey/meeting.php?survey_id='.$surveyId.'&'.api_get_cidreq()
);
} else {
$array[1] = Display::url(
$title,
api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$surveyId.'&'.api_get_cidreq()
);
if ($sessionId > 0) {
foreach ($surveys as $survey) {
$surveySession = $survey->getFirstResourceLink()->getSession();
if ($surveySession && $surveySession->getId() == $sessionId) {
$list[] = self::prepare_survey_array($survey, $efv, $mandatoryAllowed, $isDrh);
}
}
// Validation when belonging to a session
//$session_img = api_get_session_image($survey['session_id'], $_user['status']);
$session_img = '';
$array[2] = $survey->getCode();
$array[3] = $survey->getQuestions()->count();
$array[4] = UserManager::formatUserFullName($survey->getCreator());
// Dates
$array[5] = '';
$from = $survey->getAvailFrom() ? $survey->getAvailFrom()->format('Y-m-d H:i:s') : null;
if (null !== $from) {
$array[5] = api_convert_and_format_date(
$from,
DATE_TIME_FORMAT_LONG
);
$lpRepo = Container::getLpRepository();
$lpsInSessionQb = $lpRepo->findAllByCourse($course, $session);
$lpsInSession = $lpsInSessionQb->getQuery()->getResult();
foreach ($lpsInSession as $lpInSession) {
$lpInSessionSession = $lpInSession->getFirstResourceLink()->getSession();
if ($lpInSessionSession && $lpInSessionSession->getId() == $sessionId) {
foreach ($lpInSession->getItems() as $sessionItem) {
if ($sessionItem->getItemType() === 'survey') {
$itemId = (int) $sessionItem->getPath();
$survey = $repo->find($itemId);
if ($survey && $survey->getFirstResourceLink()->getSession() == null) {
$list[] = self::prepare_survey_array($survey, $efv, $mandatoryAllowed, $isDrh, false);
}
}
}
}
}
$till = $survey->getAvailTill() ? $survey->getAvailTill()->format('Y-m-d H:i:s') : null;
$array[6] = '';
if (null !== $till) {
$array[6] = api_convert_and_format_date(
$till,
DATE_TIME_FORMAT_LONG
);
} else {
foreach ($surveys as $survey) {
$list[] = self::prepare_survey_array($survey, $efv, $mandatoryAllowed, $isDrh);
}
}
$array[7] =
Display::url(
$survey->getAnswered(),
api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$surveyId.'&'
.api_get_cidreq()
).' / '.
Display::url(
$survey->getInvited(),
api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$surveyId.'&'
.api_get_cidreq()
);
// Anon
$array[8] = $survey->getAnonymous();
if ($mandatoryAllowed) {
$efvMandatory = $efv->get_values_by_handler_and_field_variable(
$surveyId,
'is_mandatory'
);
return $list;
}
$array[9] = $efvMandatory ? $efvMandatory['value'] : 0;
// Survey id
$array[10] = $surveyId;
private static function prepare_survey_array($survey, $efv, $mandatoryAllowed, $isDrh, $canEdit = true) {
$array = [];
$surveyId = $survey->getIid();
$array[0] = $surveyId;
$type = $survey->getSurveyType();
$title = $survey->getTitle();
if (self::checkHideEditionToolsByCode($survey->getCode())) {
$array[1] = $title;
} else {
// Doodle
if (3 == $type) {
$array[1] = Display::url(
$title,
api_get_path(WEB_CODE_PATH).'survey/meeting.php?survey_id='.$surveyId.'&'.api_get_cidreq()
);
} else {
// Survey id
$array[9] = $surveyId;
$array[1] = Display::url(
$title,
api_get_path(WEB_CODE_PATH).'survey/survey.php?survey_id='.$surveyId.'&'.api_get_cidreq()
);
}
}
if ($isDrh) {
$array[1] = $title;
$array[7] = strip_tags($survey->getInvited());
}
$array[2] = $survey->getCode();
$array[3] = $survey->getQuestions()->count();
$array[4] = UserManager::formatUserFullName($survey->getCreator());
$list[] = $array;
$array[5] = '';
$from = $survey->getAvailFrom() ? $survey->getAvailFrom()->format('Y-m-d H:i:s') : null;
if (null !== $from) {
$array[5] = api_convert_and_format_date(
$from,
DATE_TIME_FORMAT_LONG
);
}
return $list;
$array[6] = '';
$till = $survey->getAvailTill() ? $survey->getAvailTill()->format('Y-m-d H:i:s') : null;
if (null !== $till) {
$array[6] = api_convert_and_format_date(
$till,
DATE_TIME_FORMAT_LONG
);
}
$array[7] = Display::url(
$survey->getAnswered(),
api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=answered&survey_id='.$surveyId.'&'.api_get_cidreq()
).' / '.
Display::url(
$survey->getInvited(),
api_get_path(WEB_CODE_PATH).'survey/survey_invitation.php?view=invited&survey_id='.$surveyId.'&'.api_get_cidreq()
);
$array[8] = $survey->getAnonymous();
if ($mandatoryAllowed) {
$efvMandatory = $efv->get_values_by_handler_and_field_variable(
$surveyId,
'is_mandatory'
);
$array[9] = $efvMandatory ? $efvMandatory['value'] : 0;
$array[10] = $surveyId;
} else {
$array[9] = $surveyId;
}
if ($isDrh) {
$array[1] = $title;
$array[7] = strip_tags($survey->getInvited());
}
$array['can_edit'] = $canEdit;
return $array;
}
/**

@ -608,9 +608,9 @@ function get_survey_data($from, $number_of_items, $column, $direction)
return SurveyUtil::get_survey_data($from, $number_of_items, $column, $direction);
}
function modify_filter($survey_id)
function modify_filter($survey_id, $url_params, $row)
{
return SurveyUtil::modify_filter($survey_id, false);
return SurveyUtil::modify_filter($survey_id, $url_params, $row);
}
function modify_filter_drh($survey_id)

@ -8,6 +8,7 @@ namespace Chamilo\CourseBundle\Entity;
use Chamilo\CoreBundle\Entity\AbstractResource;
use Chamilo\CoreBundle\Entity\ResourceInterface;
use Chamilo\CoreBundle\Entity\ResourceShowCourseResourcesInSessionInterface;
use Chamilo\CourseBundle\Repository\CSurveyRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
@ -22,7 +23,7 @@ use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Index(columns: ['code'], name: 'idx_survey_code')]
#[Gedmo\Tree(type: 'nested')]
#[ORM\Entity(repositoryClass: CSurveyRepository::class)]
class CSurvey extends AbstractResource implements ResourceInterface, Stringable
class CSurvey extends AbstractResource implements ResourceInterface, ResourceShowCourseResourcesInSessionInterface, Stringable
{
#[ORM\Column(name: 'iid', type: 'integer')]
#[ORM\Id]

Loading…
Cancel
Save