/**
  * Returns a query object with the basics all set up to get assignment stuff
  *
  * @global moodle_database $DB
  * @return block_ajax_marking_query_base
  */
 public function query_factory()
 {
     global $DB;
     $query = new block_ajax_marking_query_base($this);
     $query->add_from(array('table' => $this->modulename, 'alias' => 'moduletable'));
     $query->add_from(array('table' => 'quiz_attempts', 'on' => 'moduletable.id = quiz_attempts.quiz'));
     $query->add_from(array('table' => 'question_attempts', 'on' => 'question_attempts.questionusageid = quiz_attempts.uniqueid'));
     $query->add_from(array('table' => 'question_attempt_steps', 'alias' => 'sub', 'on' => 'question_attempts.id = sub.questionattemptid'));
     $query->add_from(array('table' => 'question', 'on' => 'question_attempts.questionid = question.id'));
     // Standard userid for joins.
     $query->add_select(array('table' => 'quiz_attempts', 'column' => 'userid'));
     $query->add_select(array('table' => 'sub', 'column' => 'timecreated', 'alias' => 'timestamp'));
     $query->add_where(array('type' => 'AND', 'condition' => 'quiz_attempts.timefinish > 0'));
     $query->add_where(array('type' => 'AND', 'condition' => 'quiz_attempts.preview = 0'));
     $comparesql = $DB->sql_compare_text('question_attempts.behaviour') . " = 'manualgraded'";
     $query->add_where(array('type' => 'AND', 'condition' => $comparesql));
     $query->add_where(array('type' => 'AND', 'condition' => "sub.state = '" . question_state::$needsgrading . "' "));
     // We want to get a list of graded states so we can retrieve all questions that don't have
     // one.
     $gradedstates = array();
     $us = new ReflectionClass('question_state');
     foreach ($us->getStaticProperties() as $name => $class) {
         /* @var question_state $class */
         if ($class->is_graded()) {
             $gradedstates[] = $name;
         }
     }
     list($gradedsql, $gradedparams) = $DB->get_in_or_equal($gradedstates, SQL_PARAMS_NAMED, 'quizq001');
     $subsql = "NOT EXISTS( SELECT 1\n                                 FROM {question_attempt_steps} st\n                                WHERE st.state {$gradedsql}\n                                  AND st.questionattemptid = question_attempts.id)";
     $query->add_where(array('type' => 'AND', 'condition' => $subsql));
     $query->add_params($gradedparams);
     return $query;
 }
 /**
  * Returns a query object with the basics all set up to get assignment stuff
  *
  * @global moodle_database $DB
  * @return block_ajax_marking_query_base
  */
 public function query_factory()
 {
     global $USER;
     $query = new block_ajax_marking_query_base($this);
     // This currently does a simple check for whether or not the current user has added a
     // rating or not. No scope for another teacher to do all the marking, or some of it.
     list($notmyratingsql, $notmyratingparams) = $this->get_teacher_sql();
     $query->add_params($notmyratingparams);
     $query->add_from(array('table' => 'forum_posts', 'alias' => 'sub'));
     $query->add_from(array('table' => 'forum_discussions', 'alias' => 'discussions', 'on' => 'sub.discussion = discussions.id'));
     $query->add_from(array('table' => $this->modulename, 'alias' => 'moduletable', 'on' => 'discussions.forum = moduletable.id'));
     // We need the context id to check the ratings table in the teacher SQL.
     $query->add_from(array('table' => 'course_modules', 'alias' => 'forumcoursemodules', 'on' => 'moduletable.id = forumcoursemodules.instance ' . 'AND forumcoursemodules.module = ' . $this->get_module_id()));
     $query->add_from(array('table' => 'context', 'alias' => 'forumcontext', 'on' => 'forumcoursemodules.id = forumcontext.instanceid ' . 'AND forumcontext.contextlevel = ' . CONTEXT_MODULE));
     // Standard userid for joins.
     $query->add_select(array('table' => 'sub', 'column' => 'userid'));
     $query->add_select(array('table' => 'sub', 'column' => 'modified', 'alias' => 'timestamp'));
     $query->add_where(array('type' => 'AND', 'condition' => 'sub.userid <> :forumuserid'));
     $query->add_where(array('type' => 'AND', 'condition' => 'moduletable.assessed > 0'));
     $query->add_where(array('type' => 'AND', 'condition' => " {$notmyratingsql} "));
     $query->add_where(array('type' => 'AND', 'condition' => '( (moduletable.assesstimestart = 0) OR
                                              (sub.created >= moduletable.assesstimestart) ) '));
     $query->add_where(array('type' => 'AND', 'condition' => '( (moduletable.assesstimefinish = 0) OR
                                              (sub.created <= moduletable.assesstimefinish) )'));
     $query->add_param('forumuserid', $USER->id);
     return $query;
 }