Beispiel #1
0
 public IActionResult takequiz(string type)
 {
     if (Context.Session.GetInt32("on") == 1)
     {
         try
         {
             ViewBag.loggedon = 1;
             ViewBag.id       = Context.Session.GetInt32("id");
             if (type == "hard")
             {
                 ObjectResult       obj      = (ObjectResult) new MeAd.Raml.QuizController().GetHard();
                 Countries.Question question = (Countries.Question)obj.Value;
                 ViewBag.question    = question;
                 ViewBag.desc        = question.Description;
                 ViewBag.id_question = question.Id;
             }
         }
         catch (Exception e) { ViewBag.error = e.ToString(); }
     }
     else
     {
         ViewBag.loggedon = 0;
     }
     return(View());
 }
Beispiel #2
0
        public IActionResult GetHard()
        {
            Random rand = new Random();
            string code = "";
            Dictionary <string, string> answers = new Dictionary <string, string>();

            Countries.Question question;
            string             description = "";
            string             name        = "";
            int    error     = 0;
            string random_id = "";

            try
            {
                database db = new database(database.maindb);
Begin:
                error = 0;
                int id1 = rand.Next(1, 213771);
                int id2 = rand.Next(1, 213771);
                int id3 = rand.Next(1, 213771);
                int id4 = rand.Next(1, 213771);
                answers = new Dictionary <string, string>();
                string          answer_code = "";
                MySqlDataReader rd          = db.ExecuteReader("select code from diseasestatistics where id=" + id1 + " or id=" + id2 + " or id=" + id3 + " or id=" + id4);
                int             count       = 0;
                while (rd.Read())
                {
                    count++;
                    code = rd.GetString("code");
                    if (count == 1)
                    {
                        answer_code = code;
                    }

                    answers.Add(code, "");
                }
                ViewBag.answers = answers;
                SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org");

                foreach (KeyValuePair <string, string> answer in answers.ToList())
                {
                    code = answer.Key;
                    string query = @"SELECT * WHERE {
                            ?url <http://dbpedia.org/ontology/icd10> ?ID.
                            ?url rdfs:label ?name.
                            ?url <http://dbpedia.org/ontology/abstract> ?description.
                            filter regex(str(lcase(?ID)), concat(lcase('" + code[0] + "'), '[" + code[1] + "][" + code[2] + "][.]?[0-9]?') )" +
                                   "filter(langMatches(lang(?name), 'EN'))" +
                                   "filter(langMatches(lang(?description), 'EN'))" +
                                   "} limit 1";
                    SparqlResultSet results = endpoint.QueryWithResultSet(query);
                    if (results.Count > 0 && answer_code == answer.Key)
                    {
                        random_id   = RandomString(15);
                        name        = results[0]["name"].ToString().Remove(results[0]["name"].ToString().Length - 3);
                        description = results[0]["description"].ToString();
                        db.ExecuteNonQuery("insert into questions(random_id,answer) values ('" + random_id + "','" + name + "')");
                    }
                    else if (results.Count == 0)
                    {
                        query = @"SELECT * WHERE {
                            ?url <http://dbpedia.org/ontology/icd10> ?ID.
                            ?url rdfs:label ?name.
                            ?url <http://dbpedia.org/ontology/abstract> ?description.
                            filter regex(str(lcase(?ID)), concat(lcase('" + code[0] + "'), '[" + code[1] + "][0-9][.]?[0-9]?') )" +
                                "filter(langMatches(lang(?name), 'EN'))" +
                                "filter(langMatches(lang(?description), 'EN'))" +
                                "} limit 1";
                        results = endpoint.QueryWithResultSet(query);
                        if (results.Count > 0 && answer_code == answer.Key)
                        {
                            random_id   = RandomString(15);
                            name        = results[0]["name"].ToString().Remove(results[0]["name"].ToString().Length - 3);
                            description = results[0]["description"].ToString();
                            db.ExecuteNonQuery("insert into questions(random_id,answer) values ('" + random_id + "','" + name + "')");
                        }
                    }
                    if (results.Count > 0)
                    {
                        answers[answer.Key] = results[0]["name"].ToString().Remove(results[0]["name"].ToString().Length - 3);
                    }
                    else
                    {
                        error = 1;
                    }
                }
                if (error == 1)
                {
                    goto Begin;
                }
                description = description.Replace(name, "***");
                description = description.Replace(name.ToLower(), "***");
                description = description.Remove(description.Length - 3);
                question    = new Countries.Question(answers, description, random_id);
            }
            catch (Exception e) { return(new ObjectResult(e.ToString())); }
            return(new ObjectResult(question));
        }