WebService: Add GET_COURSE_QUIZ_MDL_COMPAT - refs BT#17145

This service roughly matches what the call to MDL's API core_course_get_contents function returns.
pull/3213/head^2
Angel Fernando Quiroz Campos 6 years ago
parent 6ece51cea9
commit d8400a456b
  1. 47
      main/exercise/exercise.class.php
  2. 91
      main/inc/lib/course_home.lib.php
  3. 56
      main/inc/lib/webservices/Rest.php
  4. 5
      main/webservices/api/v2.php

@ -8413,15 +8413,24 @@ class Exercise
* Return an HTML table of exercises for on-screen printing, including * Return an HTML table of exercises for on-screen printing, including
* action icons. If no exercise is present and the user can edit the * action icons. If no exercise is present and the user can edit the
* course, show a "create test" button. * course, show a "create test" button.
* *
* @param int $categoryId * @param int $categoryId
* @param string $keyword * @param string $keyword
* @param int $userId Optional.
* @param int $courseId Optional.
* @param int $sessionId Optional.
* @param bool $returnData Optional.
* *
* @return string * @return string
*/ */
public static function exerciseGrid($categoryId, $keyword = '') public static function exerciseGrid(
{ $categoryId,
$keyword = '',
$userId = 0,
$courseId = 0,
$sessionId = 0,
$returnData = false
) {
$TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT); $TBL_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
$TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); $TBL_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION); $TBL_EXERCISE_QUESTION = Database::get_course_table(TABLE_QUIZ_TEST_QUESTION);
@ -8441,17 +8450,17 @@ class Exercise
} }
$is_allowedToEdit = api_is_allowed_to_edit(null, true); $is_allowedToEdit = api_is_allowed_to_edit(null, true);
$courseInfo = api_get_course_info(); $courseInfo = $courseId ? api_get_course_info_by_id($courseId) : api_get_course_info();
$sessionId = api_get_session_id(); $sessionId = $sessionId ? (int) $sessionId : api_get_session_id();
$courseId = $courseInfo['real_id']; $courseId = $courseInfo['real_id'];
$tableRows = []; $tableRows = [];
$uploadPath = DIR_HOTPOTATOES; //defined in main_api $uploadPath = DIR_HOTPOTATOES; //defined in main_api
$exercisePath = api_get_self(); $exercisePath = api_get_self();
$origin = api_get_origin(); $origin = api_get_origin();
$userInfo = api_get_user_info(); $userInfo = $userId ? api_get_user_info($userId) : api_get_user_info();
$charset = 'utf-8'; $charset = 'utf-8';
$token = Security::get_token(); $token = Security::get_token();
$userId = api_get_user_id(); $userId = $userId ? (int) $userId : api_get_user_id();
$isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh($userId, $courseInfo); $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh($userId, $courseInfo);
$documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
$limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access'); $limitTeacherAccess = api_get_configuration_value('limit_exercise_teacher_access');
@ -8664,6 +8673,8 @@ class Exercise
} }
} }
$webPath = api_get_path(WEB_CODE_PATH);
if (!empty($exerciseList)) { if (!empty($exerciseList)) {
if ($origin !== 'learnpath') { if ($origin !== 'learnpath') {
$visibilitySetting = api_get_configuration_value('show_hidden_exercise_added_to_lp'); $visibilitySetting = api_get_configuration_value('show_hidden_exercise_added_to_lp');
@ -8675,7 +8686,7 @@ class Exercise
$my_exercise_id = $row['id']; $my_exercise_id = $row['id'];
$attempt_text = ''; $attempt_text = '';
$actions = ''; $actions = '';
$exercise = new Exercise(); $exercise = new Exercise($returnData ? $courseId : 0);
$exercise->read($my_exercise_id, false); $exercise->read($my_exercise_id, false);
if (empty($exercise->id)) { if (empty($exercise->id)) {
@ -9107,6 +9118,10 @@ class Exercise
// Link of the exercise. // Link of the exercise.
$currentRow['title'] = $url.' '.$session_img; $currentRow['title'] = $url.' '.$session_img;
if ($returnData) {
$currentRow['title'] = $exercise->getUnformattedTitle();
}
// This query might be improved later on by ordering by the new "tms" field rather than by exe_id // This query might be improved later on by ordering by the new "tms" field rather than by exe_id
// Don't remove this marker: note-query-exe-results // Don't remove this marker: note-query-exe-results
$sql = "SELECT * FROM $TBL_TRACK_EXERCISES $sql = "SELECT * FROM $TBL_TRACK_EXERCISES
@ -9222,6 +9237,10 @@ class Exercise
} }
} }
} }
if ($returnData) {
$attempt_text = $num;
}
} }
$currentRow['attempt'] = $attempt_text; $currentRow['attempt'] = $attempt_text;
@ -9249,6 +9268,14 @@ class Exercise
$currentRow[] = '<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'. $currentRow[] = '<a href="exercise_report.php?'.api_get_cidreq().'&exerciseId='.$row['id'].'">'.
Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>'; Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).'</a>';
} }
if ($returnData) {
$currentRow['id'] = $exercise->id;
$currentRow['url'] = $webPath.'exercise/overview.php?'
.api_get_cidreq_params($courseInfo['code'], $sessionId).'&'
."$mylpid$mylpitemid&exerciseId={$row['id']}";
$currentRow['name'] = $currentRow[0];
}
} }
$tableRows[] = $currentRow; $tableRows[] = $currentRow;
@ -9420,6 +9447,10 @@ class Exercise
} }
} }
if ($returnData) {
return $tableRows;
}
if (empty($tableRows) && empty($categoryId)) { if (empty($tableRows) && empty($categoryId)) {
if ($is_allowedToEdit && $origin !== 'learnpath') { if ($is_allowedToEdit && $origin !== 'learnpath') {
$content .= '<div id="no-data-view">'; $content .= '<div id="no-data-view">';

@ -36,47 +36,47 @@ class CourseHome
if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) &&
$_SESSION['studentview'] != 'studentview' $_SESSION['studentview'] != 'studentview'
) { ) {
$condition_display_tools = ' WHERE $condition_display_tools = ' WHERE
a.c_id = '.$course_id.' AND a.c_id = '.$course_id.' AND
a.link=t.link AND a.link=t.link AND
(t.position="basic" OR a.name = "'.TOOL_TRACKING.'") (t.position="basic" OR a.name = "'.TOOL_TRACKING.'")
'; ';
} }
$sql = "SELECT a.*, t.image img, t.row, t.column $sql = "SELECT a.*, t.image img, t.row, t.column
FROM $TBL_ACCUEIL a, $TABLE_TOOLS t FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
$condition_display_tools ORDER BY t.row, t.column"; $condition_display_tools ORDER BY t.row, t.column";
break; break;
case 'External': case 'External':
if (api_is_allowed_to_edit()) { if (api_is_allowed_to_edit()) {
$sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
WHERE WHERE
a.c_id = $course_id AND a.c_id = $course_id AND
((a.link=t.link AND t.position='external') OR ((a.link=t.link AND t.position='external') OR
(a.visibility <= 1 AND (a.visibility <= 1 AND
(a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND (a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND
a.image=t.image)) a.image=t.image))
ORDER BY a.id"; ORDER BY a.id";
} else { } else {
$sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
WHERE WHERE
a.c_id = $course_id AND a.c_id = $course_id AND
(a.visibility = 1 AND ((a.link=t.link AND t.position='external') OR (a.visibility = 1 AND ((a.link=t.link AND t.position='external') OR
((a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND ((a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND
a.image=t.image))) a.image=t.image)))
ORDER BY a.id"; ORDER BY a.id";
} }
break; break;
case 'courseAdmin': case 'courseAdmin':
$sql = "SELECT a.*, t.image img, t.row, t.column $sql = "SELECT a.*, t.image img, t.row, t.column
FROM $TBL_ACCUEIL a, $TABLE_TOOLS t FROM $TBL_ACCUEIL a, $TABLE_TOOLS t
WHERE a.c_id = $course_id AND admin=1 AND a.link=t.link WHERE a.c_id = $course_id AND admin=1 AND a.link=t.link
ORDER BY t.row, t.column"; ORDER BY t.row, t.column";
break; break;
case 'platformAdmin': case 'platformAdmin':
$sql = "SELECT *, image img FROM $TBL_ACCUEIL $sql = "SELECT *, image img FROM $TBL_ACCUEIL
WHERE c_id = $course_id AND visibility = 2 WHERE c_id = $course_id AND visibility = 2
ORDER BY id"; ORDER BY id";
} }
$result = Database::query($sql); $result = Database::query($sql);
@ -96,7 +96,7 @@ class CourseHome
$sql_links = "SELECT tl.*, tip.visibility $sql_links = "SELECT tl.*, tip.visibility
FROM $tbl_link tl FROM $tbl_link tl
LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
WHERE WHERE
tl.c_id = $course_id AND tl.c_id = $course_id AND
tip.c_id = $course_id AND tip.c_id = $course_id AND
tl.on_homepage='1' AND tl.on_homepage='1' AND
@ -105,7 +105,7 @@ class CourseHome
$sql_links = "SELECT tl.*, tip.visibility $sql_links = "SELECT tl.*, tip.visibility
FROM $tbl_link tl FROM $tbl_link tl
LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id
WHERE WHERE
tl.c_id = $course_id AND tl.c_id = $course_id AND
tip.c_id = $course_id AND tip.c_id = $course_id AND
tl.on_homepage='1' AND tl.on_homepage='1' AND
@ -275,8 +275,8 @@ class CourseHome
if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) &&
$_SESSION['studentview'] != 'studentview' $_SESSION['studentview'] != 'studentview'
) { ) {
$condition_display_tools = ' WHERE $condition_display_tools = ' WHERE
c_id = '.$course_id.' AND c_id = '.$course_id.' AND
(visibility = 1 OR (visibility = 0 AND name = "'.TOOL_TRACKING.'")) '; (visibility = 1 OR (visibility = 0 AND name = "'.TOOL_TRACKING.'")) ';
} }
$result = Database::query("SELECT * FROM $course_tool_table $condition_display_tools ORDER BY id"); $result = Database::query("SELECT * FROM $course_tool_table $condition_display_tools ORDER BY id");
@ -312,14 +312,14 @@ class CourseHome
case TOOL_PUBLIC: case TOOL_PUBLIC:
$sql_links = "SELECT tl.*, tip.visibility $sql_links = "SELECT tl.*, tip.visibility
FROM $course_link_table tl FROM $course_link_table tl
LEFT JOIN $course_item_property_table tip LEFT JOIN $course_item_property_table tip
ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
WHERE tl.on_homepage='1' AND tip.visibility = 1"; WHERE tl.on_homepage='1' AND tip.visibility = 1";
break; break;
case TOOL_PUBLIC_BUT_HIDDEN: case TOOL_PUBLIC_BUT_HIDDEN:
$sql_links = "SELECT tl.*, tip.visibility $sql_links = "SELECT tl.*, tip.visibility
FROM $course_link_table tl FROM $course_link_table tl
LEFT JOIN $course_item_property_table tip LEFT JOIN $course_item_property_table tip
ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id ON tip.tool='link' AND tl.c_id = tip.c_id AND tl.c_id = $course_id AND tip.ref=tl.id
WHERE tl.on_homepage='1' AND tip.visibility = 0"; WHERE tl.on_homepage='1' AND tip.visibility = 0";
@ -524,29 +524,29 @@ class CourseHome
$orderBy = ' ORDER BY id '; $orderBy = ' ORDER BY id ';
switch ($course_tool_category) { switch ($course_tool_category) {
case TOOL_STUDENT_VIEW: case TOOL_STUDENT_VIEW:
$conditions = ' WHERE visibility = 1 AND $conditions = ' WHERE visibility = 1 AND
(category = "authoring" OR category = "interaction" OR category = "plugin") AND (category = "authoring" OR category = "interaction" OR category = "plugin") AND
t.name <> "notebookteacher" '; t.name <> "notebookteacher" ';
if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) &&
$_SESSION['studentview'] != 'studentview' $_SESSION['studentview'] != 'studentview'
) { ) {
$conditions = ' WHERE ( $conditions = ' WHERE (
visibility = 1 AND ( visibility = 1 AND (
category = "authoring" OR category = "authoring" OR
category = "interaction" OR category = "interaction" OR
category = "plugin" category = "plugin"
) OR (t.name = "'.TOOL_TRACKING.'") ) OR (t.name = "'.TOOL_TRACKING.'")
)'; )';
} }
// Add order if there are LPs // Add order if there are LPs
$sql = "SELECT t.* FROM $course_tool_table t $sql = "SELECT t.* FROM $course_tool_table t
LEFT JOIN $lpTable l LEFT JOIN $lpTable l
ON (t.c_id = l.c_id AND link LIKE concat('%/lp_controller.php?action=view&lp_id=', l.id, '&%')) ON (t.c_id = l.c_id AND link LIKE concat('%/lp_controller.php?action=view&lp_id=', l.id, '&%'))
LEFT JOIN $tblLpCategory lc LEFT JOIN $tblLpCategory lc
ON (t.c_id = lc.c_id AND l.category_id = lc.iid) ON (t.c_id = lc.c_id AND l.category_id = lc.iid)
$conditions AND $conditions AND
t.c_id = $course_id $condition_session t.c_id = $course_id $condition_session
ORDER BY ORDER BY
CASE WHEN l.category_id IS NULL THEN 0 ELSE 1 END, CASE WHEN l.category_id IS NULL THEN 0 ELSE 1 END,
CASE WHEN l.display_order IS NULL THEN 0 ELSE 1 END, CASE WHEN l.display_order IS NULL THEN 0 ELSE 1 END,
@ -1819,4 +1819,35 @@ class CourseHome
false false
); );
} }
/**
* @param string $toolName
* @param int $courseId
* @param int $sessionId Optional.
*
* @return bool
*/
public static function getToolVisibility($toolName, $courseId, $sessionId = 0)
{
$allowEditionInSession = api_get_configuration_value('allow_edit_tool_visibility_in_session');
$em = Database::getManager();
$toolRepo = $em->getRepository('ChamiloCourseBundle:CTool');
/** @var CTool $tool */
$tool = $toolRepo->findOneBy(['cId' => $courseId, 'sessionId' => 0, 'name' => $toolName]);
$visibility = $tool->getVisibility();
if ($allowEditionInSession && $sessionId) {
$tool = $toolRepo->findOneBy(
['cId' => $courseId, 'sessionId' => $sessionId, 'name' => $toolName]
);
if ($tool) {
$visibility = $tool->getVisibility();
}
}
return $visibility;
}
} }

@ -66,6 +66,7 @@ class Rest extends WebService
const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field'; const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field';
const UPDATE_USER_FROM_USERNAME = 'update_user_from_username'; const UPDATE_USER_FROM_USERNAME = 'update_user_from_username';
const USERNAME_EXIST = 'username_exist'; const USERNAME_EXIST = 'username_exist';
const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat';
/** /**
* @var Session * @var Session
@ -1998,4 +1999,59 @@ class Rest extends WebService
return json_encode($params); return json_encode($params);
} }
/**
* This service roughly matches what the call to MDL's API core_course_get_contents function returns.
*
* @return array
*/
public function getCourseQuizMdlCompat()
{
$userId = $this->user->getId();
$courseId = $this->course->getId();
$sessionId = $this->session ? $this->session->getId() : 0;
$toolVisibility = CourseHome::getToolVisibility(TOOL_QUIZ, $courseId, $sessionId);
$json = [
"id" => $this->course->getId(),
"name" => get_lang('Exercises'),
"visible" => (int) $toolVisibility,
"summary" => '',
"summaryformat" => 1,
"section" => 1,
"hiddenbynumsections" => 0,
"uservisible" => $toolVisibility,
"modules" => [],
];
$quizIcon = Display::return_icon('quiz.png', '', [], ICON_SIZE_SMALL, false, true);
$json['modules'] = array_map(
function (array $exercise) use ($quizIcon) {
return [
'id' => $exercise['id'],
'url' => $exercise['url'],
'name' => $exercise['name'],
'instance' => 1,
'visible' => 1,
'uservisible' => true,
'visibleoncoursepage' => 0,
'modicon' => $quizIcon,
'modname' => 'quiz',
'modplural' => get_lang('Exercises'),
'availability' => null,
'indent' => 0,
'onclick' => '',
'afterlink' => null,
'customdata' => "",
'noviewlink' => false,
'completion' => (int) ($exercise[1] > 0),
];
},
Exercise::exerciseGrid(0, '', $userId, $courseId, $sessionId, true)
);
return [$json];
}
} }

@ -335,6 +335,11 @@ try {
$data = $restApi->usernameExist($_POST['loginname']); $data = $restApi->usernameExist($_POST['loginname']);
$restResponse->setData([$data]); $restResponse->setData([$data]);
break; break;
case Rest::GET_COURSE_QUIZ_MDL_COMPAT:
$data = $restApi->getCourseQuizMdlCompat();
echo json_encode($data, JSON_PRETTY_PRINT);
exit;
default: default:
throw new Exception(get_lang('InvalidAction')); throw new Exception(get_lang('InvalidAction'));
} }

Loading…
Cancel
Save