Custom web-services return backslash "\". I want to remove it

Custom web-services return backslash "\". I want to remove it

by Baiju Sharma -
Number of replies: 0

Below is my web- services code: I know I should not use "json_encode". Moodle will do it. What should I return I am very confused.


 public static function get_quiz_data_parameters()

    {

        return new external_function_parameters(array(

            'quizid' => new external_value(PARAM_TEXT, 'This will check quiz id')

        ));

    }

    

    /**

     * Returns welcome message

     * @return string welcome message

     */

    public static function get_quiz_data($quizid)

    {

        global $USER;

        global $DB;

        $response["question"] = array();

        $result               = array();

        $quest[]              = array();


        $params = self::validate_parameters(self::get_quiz_data_parameters(), array(

            'quizid' => $quizid

        ));

        

        $data        = array();

        $quizdetails = $DB->get_recordset_sql('SELECT que.id,que.name,que.questiontext,que.qtype,que.generalfeedback

                                                    FROM mdl_quiz AS qz

                                                    WHERE qz.id = ?', array(

            $quizid

        ));

        

        foreach ($quizdetails as $id => $rec) {

            $id               = $rec->id;

            $name             = $rec->name;

            $questiontext     = trim(strip_tags($rec->questiontext));

            $str_qtext        = preg_replace("~\x{00a0}~siu", "", $questiontext);

            $qtype            = $rec->qtype;

            $explaination     = trim(strip_tags($rec->generalfeedback));

            $str_explaination = preg_replace("~\x{00a0}~siu", " ", $explaination);

 

            $quest["QuestionId"]    = $rec->id;

            $quest["Name"]          = $rec->name;

            $quest["Type"]          = $qtype;

            $quest["Text"]          = $str_qtext;

            $quest["ImagePath"]     = "";

            $quest["Explanation"]   = $str_explaination;

            $quest["Options"]       = array();

             $quest["CorrectAnswer"] = "";

            $qz_option = $DB->get_recordset_sql('SELECT  qa.answer,qa.id

                                                            FROM mdl_quiz

                                                            WHERE que.id=?', array($id));

            

            foreach ($qz_option as $id => $get_option) {

                $option   = trim(strip_tags($get_option->answer));

               // $str      = preg_replace("~\x{00a0}~siu", "", $option);

                  $str      = $option;

                $result[] = $get_option;

                

                $options["OptionId"]  = $get_option->id;

                $options["Value"]     = $str;

                $options["ImagePath"] = "";

                array_push($quest["Options"], $options);

            }

            array_push($response["question"], $quest);            

        }

        $quiz_final = json_encode($response);

    //    echo $json_string;

       //  $quiz_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$json_string);

        return $quiz_final;

    }   

    /**

     * Returns description of method result value

     * @return external_description

     */

    public static function get_quiz_data_returns()

    {

        return new external_value(PARAM_TEXT, 'The welcome message + user first name');

    }

-----------------------------Out put-------

{\"question\":[{\"0\":[],\"QuestionId\":\"3518\",\"Name\":\"wireline operations\",\"Type\":\"multichoice\",\"Text\":\"What are wireline operations?\",\"ImagePath\":\"\",\"Explanation\":\"Explanation: After drilling to a certain depth, wireline operations are used to test the nearby formations by lowering tools that contain sensors that can test for the presence of petroleum into the hole.\",\"Options\":[{\"OptionId\":\"10757\",\"Value\":\"Operations involved to lower the casing tube into the well.\\u00a0\",\"ImagePath\":\"\"},{\"OptionId\":\"10758\",\"Value\":\"Processes to stop a kick from entering the well.\",\"ImagePath\":\"\"},{\"OptionId\":\"10759\",\"Value\":\"Testing processes that involve lowering wire into the well\",\"ImagePath\":\"\"},{\"OptionId\":\"10760\",\"Value\":\"Operations performed to make sure a well can be drilled in an area.\",\"ImagePath\":\"\"}],\"CorrectAnswer\":\"\"}

Average of ratings: -