Changeset 223:96ab2b4b251c in SMSSender


Ignore:
Timestamp:
May 30, 2012 1:40:20 PM (8 years ago)
Author:
Sämy Zehnder <saemy.zehnder@…>
Branch:
default
Message:

Schoolnet:

  • Implemented check for a changed eventTarget of a question when answering it. The eventTarget will then be adjusted, if a change was found. (fixes #47)
Location:
gateways/Schoolnet/src/business/deepthought
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gateways/Schoolnet/src/business/deepthought/deepthought_task_answertoschoolnet.cpp

    r222 r223  
    3131namespace Schoolnet {
    3232
    33 AnswerToSchoolnetTask::AnswerToSchoolnetTask(BCAccount* account, const Answer& answer)
     33AnswerToSchoolnetTask::AnswerToSchoolnetTask(BCAccount *account, const Answer &answer)
    3434    : SThreadedTask(0x5c001437, "Schoolnet::DeepThought")
    3535    , account_(account)
     
    5555    Question question = answer().question();
    5656
     57    // Get the question page
     58    QString html = QString::fromUtf8(account()->httpHelper()->syncGet(URL_GETPOINTSPAGE + question.nrNodeGuId()));
     59
     60    // Fix eventTarget if needed
     61    tryAdjustEventTarget(question, html);
     62
    5763    // Check if the question is already answered
    58     QString html = QString::fromUtf8(account()->httpHelper()->syncGet(URL_GETPOINTSPAGE + question.nrNodeGuId()));
    5964    if (qiAlreadyAnswered == setCorrectAnswerFromHtml(question, html)) {
    6065        // The question is already answered on schoolnet
     
    6570
    6671    // Answer it!
    67     QList<IParam*> posts;
     72    QList<IParam *> posts;
    6873    posts.append(new SKeyValueParam(QString(question.eventTarget()+":Answers").toUtf8(), answer().id().toUtf8()));
    6974    html = QString::fromUtf8(account()->httpHelper()->syncPostByTarget(question.eventTarget() + "$cmdAnswer", "", posts));
     
    9398
    9499/**
     100 * Tries to fix the eventTarget of the question if it has changed.
     101 */
     102void AnswerToSchoolnetTask::tryAdjustEventTarget(Question &question, const QString &html) const {
     103    // Try to fix the eventTarget if it is changed
     104    QRegExp rx("<span id=\"quizQuiz\">\\s*"
     105                   "<div id=\"divQuestion\">\\s*"
     106                       "<b>" + QRegExp::escape(question.text()) + "</b>\\s*"
     107                   "</div>\\s*"
     108                   "<div id=\"quizAnswers\">\\s*"
     109                       "<table[^>]*>\\s*"
     110                           "<tr>\\s*<td>\\s*(<input [^>]+>)");
     111    rx.setMinimal(false);
     112    rx.setCaseSensitivity(Qt::CaseInsensitive);
     113
     114    if (rx.indexIn(html) == -1) {
     115        return; // input not found
     116    }
     117
     118    QString input = rx.cap(1);
     119    rx.setPattern("name=\"([^\"]+)\"");
     120
     121    if (rx.indexIn(input) == -1) {
     122        return; // input name not found
     123    }
     124
     125    QString name = rx.cap(1);
     126    if (!name.endsWith(":Answers")) {
     127        return; // Invalid input name
     128    }
     129    QString eventTarget = name.remove(QRegExp(":Answers$")); // Remove unused part
     130
     131    if (question.eventTarget() != eventTarget) {
     132        question.setEventTarget(eventTarget); // Adjust it
     133        QuestionManager::instance()->saveQuestion(question);
     134    }
     135}
     136
     137/**
    95138 * Tries to find the correct or at least an incorrect answer to the given question from the given html
    96139 * and updates the question if any info is found.
     
    98141 * @return QuestionInfo The info about the correct answer which was extracted from schoolnet.
    99142 */
    100 AnswerToSchoolnetTask::QuestionInfo AnswerToSchoolnetTask::setCorrectAnswerFromHtml(Question& question, const QString& html) {
     143AnswerToSchoolnetTask::QuestionInfo AnswerToSchoolnetTask::setCorrectAnswerFromHtml(Question &question, const QString &html) {
    101144    QString submitId = question.eventTarget().replace(':', '_') + "_cmdAnswer";
    102145
     146    // Find the question box
    103147    QRegExp rx("<div class=\"boxChildContent[^\"]*\">.*"
    104148                 "<div id=\"quizPrompt\">([^<]+)</div>.*"
     
    222266}
    223267
    224 BCAccount* AnswerToSchoolnetTask::account() const {
     268BCAccount * AnswerToSchoolnetTask::account() const {
    225269    return account_;
    226270}
  • gateways/Schoolnet/src/business/deepthought/deepthought_tasks.h

    r217 r223  
    7676
    7777public:
    78     AnswerToSchoolnetTask(BCAccount* account, const Answer& answer);
     78    AnswerToSchoolnetTask(BCAccount *account, const Answer &answer);
    7979
    80     BCAccount* account() const;
    81     Answer     answer() const;
     80    BCAccount * account() const;
     81    Answer      answer() const;
    8282
    8383protected:
     
    8585
    8686private:
    87     QuestionInfo setCorrectAnswerFromHtml(Question& question, const QString& html);
     87    void tryAdjustEventTarget(Question &question, const QString &html) const;
     88    QuestionInfo setCorrectAnswerFromHtml(Question &question, const QString &html);
    8889
    8990private:
Note: See TracChangeset for help on using the changeset viewer.