How underscore works for dropdown?

How underscore works for dropdown?

by Nitu Dhaka -
Number of replies: 9

Hi All,

I am working for forum rating on mobile app. I created the web services for forum rating and its working fine. I have done with displaying the rating on mobile app and also created the dropdown as per the scale of the rating. Mean here drop down length is dynamic. Because I am new on Javascript and underscore, so confused to make the interactive rating on mobile app. Mean if user wanted to rate on mobile app, then how this rated value will store and hit back to the web services. I know, I need to follow the template only but yet there is nothing like selecting one option out of others of dropdown and send it back to web service. Any clue regarding the issue???

Average of ratings: -
In reply to Nitu Dhaka

Re: How underscore works for dropdown?

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Nitu,

as far as I understand, your problem is not related to underscore but yes to how to handle a change in a drop down (you mean a select html element?)

Can you share some code in order to see what is your problem?

In reply to Juan Leyva

Re: How underscore works for dropdown?

by Nitu Dhaka -

Hi Juan,

First of all thanks to a reply.  Here What I want is if I select any value of the dropdown the change event should call the route and the route will call the rating function in which I defined to hit the web service to update the rating in the database.

I have done to display the rating of the particular post. and yes you point out the actual problem. Below I am sharing the code.

 

<%
function ratecall(){
<!-- document.getElementById("rate").onchange = function () {-->
alert("ON CHANGE FIRED");
<!--};-->
}%>
<% if (post.canrate) { %>
<select id="rate" onchange="ratecall()">
<% for(var i=0;i<post.scales-1;i++){
if(i==post.rated){
var a= 'selected';
}else{
a='';
}
%>
<option id="<%=i%>"<%=a %> > <%= i%></option>
<%}%>
</select>
<%}else{
}
%>

 

here now I am only calling an alert on change event. But it is not working. I planned  to create new link in ratecall function which will call the route  which call the web service to update the in database.

In reply to Nitu Dhaka

Re: How underscore works for dropdown?

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi,

you don't need to call a Route to directly call a function related to that route, what I mean is that you can call directly your function in the onchange event

Instead using plain javascript, you should use jquery and event listener for the elements ids, something like:

In the main.js, afer the template has been printed

$("#rate").on('change', function() {

var value = $(this).val();

});

Average of ratings: Useful (1)
In reply to Juan Leyva

Re: How underscore works for dropdown?

by Nitu Dhaka -

Hi,

Ok I got your point. I don't need to call the route, I can also call the function by event only. But I already told you this framework is new to work for me. Can you please tell me where I need to attach this event? Actually I tried at my end I attach this event in main.js of forumrating. Below is the description of my directory structure

forum/

discussionposts.html  (here I am creating the dynamic dropdown. This template is displaying link of reply after click that a pop up opened for posting a reply.)

discussions.html (This template is displaying the list of discussions in a particular forum)

forums.html (This template is displaying the list of forum)

main.js (This js calling the web services for displaying the list of forum and the discussions)

 

forumpost/

main.js (This js is calling the web services for updating the database for new posted reply)

forumrating/

main.js (This js is calling the web service for updating the database for new rate on a particular post)

 

Here forumrating/main.js I am writing the event. I got attached but its not working. I firstly write an alert within event function. and no alert is coming out. Is there any template or protocol to call this event.?

Yet  I got the template of everything eg for posting the reply I used the send a message template. But for rating there is no template to follow. And that is why I am blank here. Please guide me here. Is there anything that I can follow else tell me atleast how should I do it? And also share some documentation which can help me to work out.

 

This is to code that I write in main.js for a event. In main.js I commented the route and replace with event. Is it the write process to do?
$(".rate").on('change', function() {

alert('this is to check');
});

 

In reply to Nitu Dhaka

Re: How underscore works for dropdown?

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

I suppose that you use the function MM.tpl.render for rendering the template "discussionposts.html " , just after rendering the template you should add the code with the handler

In this case, I see that rate is a class not and id, be sure of that (in a previous post seems to be an element id not a class)

$(".rate").on('change', function() {

alert('this is to check');
});

Average of ratings: Useful (1)
In reply to Juan Leyva

Re: How underscore works for dropdown?

by Nitu Dhaka -

Hi Juan,

Its working now. Thank you so much for the help. I was completely blank here. Once again Thanks for lightening my mind....smile

In reply to Juan Leyva

Re: How underscore works for dropdown?

by Nitu Dhaka -

Hi Juan,

Need little more help, again I am getting confused.

The following code is written in main.js . Here I am calling the web service to update the database for rating. The web service is working fine(checked by client.php). but when in moodle mobile interface it comes on updating it gives an error "We lost the connection you need to reconnect again. Your token is invalid", and thereafter it throw at login page(mean log out the user) However all other web services and functions are working fine with the same token. I am checking the issue for 4-5 hours, but did not get the problem. And the web service have multidimension structure for parameter and return function.

The alert box for "now working fine' is not coming. Mean the error is before it. Please highlight the error.

ratepost: function(forumId, userid, ratevalue, postid, scaleid ) {

var data = {
"post[0][forumid]" : forumId,
"post[0][itemid]" : postid,
"post[0][scaleid]" : scaleid,
"post[0][rating]" : ratevalue,
"post[0][userid]" : userid
};
var d = new Array();
d[0] = data;


MM.moodleWSCall('local_forum_rating', d, function(res) {
alert('now working fine');
});
},

 

In reply to Juan Leyva

Re: How underscore works for dropdown?

by Nitu Dhaka -

Hi Juan,

I am waiting for the reply. Below is the description of the web service for forum rating.

I am passing following parameters for calling forum rating

forumid, itemid, scaleid, rating, userid.

It also need cm and context id that I am calculating in the forum rating function. Its working fine if I run from client.php, but when I call the web service from mobile app It gives an error "Invalid parameters are detected." And the message in debug info is "Missing required key in single structure: rate" . There are actually 2 cases: 1. if I pass data "Invalid parameters are detected." error comes. 2. If I pass d  "We lost connection you need to reconnect.Your token is invalid."  However if I call the forum_rating webservice by client.php it works fine.

Below I am sharing the ratepost function code.

ratepost: function(forumId, userid, ratevalue, postid, scaleid ) {
           
            var data = {
            "post[0][forumid]" : forumId,
            "post[0][itemid]" : postid,
            "post[0][scaleid]" : scaleid,
            "post[0][rating]" : ratevalue,
            "post[0][userid]" : userid
            };
            var d = new Array();
            d[0] = data;

            MM.moodleWSCall('local_forum_rating', data, function(resrate) {
                alert("check");                    
            });                         
        }

 

Forum rating webservice

    public static function forum_rating_parameters() {
        return new external_function_parameters(
                array(
            'rate' => new external_multiple_structure(
                    new external_single_structure(
                    array(
                'forumid' => new external_value(PARAM_INT, 'contextid'),
                'itemid' => new external_value(PARAM_INT, 'itemid'),
                'scaleid' => new external_value(PARAM_INT, 'scaleid'),
                'rating' => new external_value(PARAM_INT, 'rating'),
                'userid' => new external_value(PARAM_INT, 'userid'),
                    )
                    )
            )
                )
        );
    }

    public static function forum_rating($rate = array()) {
        global $CFG, $DB;

        require_once($CFG->dirroot . '/rating/lib.php');
        $ob = array();
        $params = self::validate_parameters(self::forum_rating_parameters(), array('rate' => $rate));
        $rateperpost = $params['rate'];
        foreach ($rateperpost as $rateonpost) {
            $userrating = $rateonpost['rating'];
            $forumid = $rateonpost['forumid'];
            $cm = get_coursemodule_from_id('forum', $forumid);
            $context = context_module::instance($cm->id);
            if ($userrating != RATING_UNSET_RATING) {
                $ratingoptions = new stdClass;
                $ratingoptions->context = $context;
                $ratingoptions->component = 'mod_forum';
                $ratingoptions->ratingarea = 'post';
                $ratingoptions->itemid = $rateonpost['itemid'];
                $ratingoptions->scaleid = $rateonpost['scaleid'];
                $ratingoptions->userid = $rateonpost['userid'];
                $rating = new rating($ratingoptions);
                $rating->update_rating($userrating);
               // $html[] = (array)$group;
                $html[] = array('rate' => $userrating);
            }
        }
        return $html;
    }

    
    public static function forum_rating_returns() {
        return new external_multiple_structure(
            new external_single_structure(
                array(
                    'rate' => new external_value(PARAM_RAW, 'rating'),
                )
            )
        );
    }

 

Please let me know why am I unable to call the web service by mobile app? Thanks in advance.

In reply to Nitu Dhaka

Re: How underscore works for dropdown?

by Nitu Dhaka -

I did a stupid mistake here. Now its resolved. Thanks for the help.