From d8400a456bc4f6d0318839d383aed6d9bc05a999 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 15 May 2020 11:52:23 -0500 Subject: [PATCH] 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. --- main/exercise/exercise.class.php | 47 +++++++++++++--- main/inc/lib/course_home.lib.php | 91 +++++++++++++++++++++---------- main/inc/lib/webservices/Rest.php | 56 +++++++++++++++++++ main/webservices/api/v2.php | 5 ++ 4 files changed, 161 insertions(+), 38 deletions(-) diff --git a/main/exercise/exercise.class.php b/main/exercise/exercise.class.php index 074eedc4d6..5f27329063 100755 --- a/main/exercise/exercise.class.php +++ b/main/exercise/exercise.class.php @@ -8413,15 +8413,24 @@ class Exercise * Return an HTML table of exercises for on-screen printing, including * action icons. If no exercise is present and the user can edit the * course, show a "create test" button. - * * @param int $categoryId * @param string $keyword + * @param int $userId Optional. + * @param int $courseId Optional. + * @param int $sessionId Optional. + * @param bool $returnData Optional. * * @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_ITEM_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY); $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); - $courseInfo = api_get_course_info(); - $sessionId = api_get_session_id(); + $courseInfo = $courseId ? api_get_course_info_by_id($courseId) : api_get_course_info(); + $sessionId = $sessionId ? (int) $sessionId : api_get_session_id(); $courseId = $courseInfo['real_id']; $tableRows = []; $uploadPath = DIR_HOTPOTATOES; //defined in main_api $exercisePath = api_get_self(); $origin = api_get_origin(); - $userInfo = api_get_user_info(); + $userInfo = $userId ? api_get_user_info($userId) : api_get_user_info(); $charset = 'utf-8'; $token = Security::get_token(); - $userId = api_get_user_id(); + $userId = $userId ? (int) $userId : api_get_user_id(); $isDrhOfCourse = CourseManager::isUserSubscribedInCourseAsDrh($userId, $courseInfo); $documentPath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document'; $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 ($origin !== 'learnpath') { $visibilitySetting = api_get_configuration_value('show_hidden_exercise_added_to_lp'); @@ -8675,7 +8686,7 @@ class Exercise $my_exercise_id = $row['id']; $attempt_text = ''; $actions = ''; - $exercise = new Exercise(); + $exercise = new Exercise($returnData ? $courseId : 0); $exercise->read($my_exercise_id, false); if (empty($exercise->id)) { @@ -9107,6 +9118,10 @@ class Exercise // Link of the exercise. $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 // Don't remove this marker: note-query-exe-results $sql = "SELECT * FROM $TBL_TRACK_EXERCISES @@ -9222,6 +9237,10 @@ class Exercise } } } + + if ($returnData) { + $attempt_text = $num; + } } $currentRow['attempt'] = $attempt_text; @@ -9249,6 +9268,14 @@ class Exercise $currentRow[] = ''. Display::return_icon('test_results.png', get_lang('Results'), '', ICON_SIZE_SMALL).''; } + + 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; @@ -9420,6 +9447,10 @@ class Exercise } } + if ($returnData) { + return $tableRows; + } + if (empty($tableRows) && empty($categoryId)) { if ($is_allowedToEdit && $origin !== 'learnpath') { $content .= '
'; diff --git a/main/inc/lib/course_home.lib.php b/main/inc/lib/course_home.lib.php index 2edca45d67..337e0e6290 100755 --- a/main/inc/lib/course_home.lib.php +++ b/main/inc/lib/course_home.lib.php @@ -36,47 +36,47 @@ class CourseHome if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && $_SESSION['studentview'] != 'studentview' ) { - $condition_display_tools = ' WHERE - a.c_id = '.$course_id.' AND + $condition_display_tools = ' WHERE + a.c_id = '.$course_id.' 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 $condition_display_tools ORDER BY t.row, t.column"; break; case 'External': if (api_is_allowed_to_edit()) { $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t - WHERE - a.c_id = $course_id AND - ((a.link=t.link AND t.position='external') OR - (a.visibility <= 1 AND - (a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND + WHERE + a.c_id = $course_id AND + ((a.link=t.link AND t.position='external') OR + (a.visibility <= 1 AND + (a.image = 'external.gif' OR a.image = 'scormbuilder.gif' OR t.image = 'blog.gif') AND a.image=t.image)) ORDER BY a.id"; } else { $sql = "SELECT a.*, t.image img FROM $TBL_ACCUEIL a, $TABLE_TOOLS t - WHERE - a.c_id = $course_id AND - (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 + WHERE + a.c_id = $course_id AND + (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=t.image))) ORDER BY a.id"; } break; 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 - 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"; break; case 'platformAdmin': - $sql = "SELECT *, image img FROM $TBL_ACCUEIL - WHERE c_id = $course_id AND visibility = 2 + $sql = "SELECT *, image img FROM $TBL_ACCUEIL + WHERE c_id = $course_id AND visibility = 2 ORDER BY id"; } $result = Database::query($sql); @@ -96,7 +96,7 @@ class CourseHome $sql_links = "SELECT tl.*, tip.visibility FROM $tbl_link tl LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id - WHERE + WHERE tl.c_id = $course_id AND tip.c_id = $course_id AND tl.on_homepage='1' AND @@ -105,7 +105,7 @@ class CourseHome $sql_links = "SELECT tl.*, tip.visibility FROM $tbl_link tl LEFT JOIN $tbl_item_property tip ON tip.tool='link' AND tip.ref=tl.id - WHERE + WHERE tl.c_id = $course_id AND tip.c_id = $course_id AND tl.on_homepage='1' AND @@ -275,8 +275,8 @@ class CourseHome if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && $_SESSION['studentview'] != 'studentview' ) { - $condition_display_tools = ' WHERE - c_id = '.$course_id.' AND + $condition_display_tools = ' WHERE + c_id = '.$course_id.' AND (visibility = 1 OR (visibility = 0 AND name = "'.TOOL_TRACKING.'")) '; } $result = Database::query("SELECT * FROM $course_tool_table $condition_display_tools ORDER BY id"); @@ -312,14 +312,14 @@ class CourseHome case TOOL_PUBLIC: $sql_links = "SELECT tl.*, tip.visibility 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 WHERE tl.on_homepage='1' AND tip.visibility = 1"; break; case TOOL_PUBLIC_BUT_HIDDEN: $sql_links = "SELECT tl.*, tip.visibility 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 WHERE tl.on_homepage='1' AND tip.visibility = 0"; @@ -524,29 +524,29 @@ class CourseHome $orderBy = ' ORDER BY id '; switch ($course_tool_category) { case TOOL_STUDENT_VIEW: - $conditions = ' WHERE visibility = 1 AND - (category = "authoring" OR category = "interaction" OR category = "plugin") AND + $conditions = ' WHERE visibility = 1 AND + (category = "authoring" OR category = "interaction" OR category = "plugin") AND t.name <> "notebookteacher" '; if ((api_is_coach() || api_is_course_tutor() || api_is_platform_admin()) && $_SESSION['studentview'] != 'studentview' ) { $conditions = ' WHERE ( visibility = 1 AND ( - category = "authoring" OR - category = "interaction" OR + category = "authoring" OR + category = "interaction" OR category = "plugin" - ) OR (t.name = "'.TOOL_TRACKING.'") + ) OR (t.name = "'.TOOL_TRACKING.'") )'; } // Add order if there are LPs $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, '&%')) LEFT JOIN $tblLpCategory lc ON (t.c_id = lc.c_id AND l.category_id = lc.iid) $conditions AND - t.c_id = $course_id $condition_session + t.c_id = $course_id $condition_session ORDER BY CASE WHEN l.category_id 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 ); } + + /** + * @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; + } } diff --git a/main/inc/lib/webservices/Rest.php b/main/inc/lib/webservices/Rest.php index 47ea3969e3..6ab619c3ae 100644 --- a/main/inc/lib/webservices/Rest.php +++ b/main/inc/lib/webservices/Rest.php @@ -66,6 +66,7 @@ class Rest extends WebService const GET_SESSION_FROM_EXTRA_FIELD = 'get_session_from_extra_field'; const UPDATE_USER_FROM_USERNAME = 'update_user_from_username'; const USERNAME_EXIST = 'username_exist'; + const GET_COURSE_QUIZ_MDL_COMPAT = 'get_course_quiz_mdl_compat'; /** * @var Session @@ -1998,4 +1999,59 @@ class Rest extends WebService 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]; + } } diff --git a/main/webservices/api/v2.php b/main/webservices/api/v2.php index 1779234f71..2d92bd0145 100644 --- a/main/webservices/api/v2.php +++ b/main/webservices/api/v2.php @@ -335,6 +335,11 @@ try { $data = $restApi->usernameExist($_POST['loginname']); $restResponse->setData([$data]); break; + case Rest::GET_COURSE_QUIZ_MDL_COMPAT: + $data = $restApi->getCourseQuizMdlCompat(); + + echo json_encode($data, JSON_PRETTY_PRINT); + exit; default: throw new Exception(get_lang('InvalidAction')); }