/**
  * Makes SQL for the text labels for the course nodes.
  *
  * @static
  * @param block_ajax_marking_query $query
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // Same order as the super query will need them. Prefixed so we will have it as the
     // first column for the GROUP BY.
     $query->add_from(array('join' => 'INNER JOIN', 'table' => 'course_modules', 'on' => 'course_modules.id = countwrapperquery.id'));
     $query->add_select(array('table' => 'course_modules', 'column' => 'id', 'alias' => 'coursemoduleid'));
     // The javascript needs this for styling.
     $query->add_select(array('table' => 'countwrapperquery', 'column' => 'modulename'));
     // This will add the stuff that joins to the various module tables and gets the right names.
     $moduleclasses = block_ajax_marking_get_module_classes();
     $introcoalesce = array();
     $namecoalesce = array();
     $orderbycoalesce = array();
     foreach ($moduleclasses as $moduleclass) {
         $moduletablename = $moduleclass->get_module_name();
         $query->add_from(array('join' => 'LEFT JOIN', 'table' => $moduletablename, 'on' => "(course_modules.instance = " . $moduletablename . ".id\n                                            AND course_modules.module = '" . $moduleclass->get_module_id() . "')"));
         $namecoalesce[$moduletablename] = 'name';
         $introcoalesce[$moduletablename] = 'intro';
         $orderbycoalesce[$moduletablename] = $moduletablename . '.name';
     }
     $query->add_select(array('table' => 'course_modules', 'column' => 'id', 'alias' => 'coursemoduleid'));
     $query->add_select(array('table' => $namecoalesce, 'function' => 'COALESCE', 'column' => 'name', 'alias' => 'name'));
     $query->add_select(array('table' => $introcoalesce, 'function' => 'COALESCE', 'column' => 'intro', 'alias' => 'tooltip'));
     $query->add_orderby('COALESCE(' . implode(', ', $orderbycoalesce) . ') ASC');
     // This will add the stuff that will show us the name of the actual module instance.
     // We use the same stuff for both config and marking trees, but the config tree doesn't need
     // the stuff to pull through submission counts.
     // TODO separate counts.
     // This allows us to have separate decorators, but may obfuscate what's happening a bit.
     // Code is not duplicated, though.
 }
 /**
  * Applies the filter needed for course nodes or their descendants
  *
  * @param block_ajax_marking_query $query
  * @SuppressWarnings(PHPMD.UnusedPrivateMethod) Dynamic method names don't register
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // This is for the displayquery when we are making course nodes.
     $query->add_from(array('table' => 'course', 'alias' => 'course', 'on' => 'countwrapperquery.id = course.id'));
     $query->add_select(array('table' => 'course', 'column' => 'shortname', 'alias' => 'name'));
     $query->add_select(array('table' => 'course', 'column' => 'fullname', 'alias' => 'tooltip'));
     $query->add_orderby('course.shortname ASC');
 }
 /**
  * Applies the filter needed for course nodes or their descendants
  *
  * @param block_ajax_marking_query $query
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // This is for the displayquery when we are making course nodes.
     $query->add_from(array('join' => 'LEFT JOIN', 'table' => 'groups', 'on' => 'countwrapperquery.id = groups.id'));
     // We may get a load of people in no group.
     $query->add_select(array('function' => 'COALESCE', 'table' => array('groups' => 'name', get_string('notingroup', 'block_ajax_marking')), 'alias' => 'name'));
     $query->add_select(array('function' => 'COALESCE', 'table' => array('groups' => 'description', get_string('notingroupdescription', 'block_ajax_marking')), 'alias' => 'tooltip'));
     $query->add_orderby("COALESCE(groups.name, '" . get_string('notingroup', 'block_ajax_marking') . "') ASC");
 }
 /**
  * Makes a set of question nodes by grouping submissions by questionid.
  *
  * @static
  * @param block_ajax_marking_query $query
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // Outer bit to get display name.
     $query->add_from(array('join' => 'INNER JOIN', 'table' => 'question', 'on' => 'question.id = countwrapperquery.id'));
     $query->add_select(array('table' => 'question', 'column' => 'name'));
     $query->add_select(array('table' => 'question', 'column' => 'questiontext', 'alias' => 'tooltip'));
     // This is only needed to add the right callback function.
     $query->add_select(array('column' => "'quiz'", 'alias' => 'modulename'));
     $query->add_orderby("question.name ASC");
 }
 /**
  * Applies the filter needed for course nodes or their descendants
  *
  * @param block_ajax_marking_query $query
  * @SuppressWarnings(PHPMD.UnusedPrivateMethod) Dynamic method names don't register
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // What do we need for the nodes?
     $table = array('join' => 'INNER JOIN', 'table' => 'cohort', 'on' => 'countwrapperquery.id = cohort.id');
     $query->add_from($table);
     $conditions = array('table' => 'cohort', 'column' => 'name');
     $query->add_select($conditions);
     $conditions = array('table' => 'cohort', 'column' => 'description');
     $query->add_select($conditions);
     $query->add_orderby('cohort.name ASC');
 }
 /**
  * Applies the filter needed for course nodes or their descendants
  *
  * @param block_ajax_marking_query $query
  * @SuppressWarnings(PHPMD.UnusedPrivateMethod) Dynamic method names don't register
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     global $USER;
     // This is for the displayquery when we are making course nodes.
     $query->add_from(array('table' => 'course', 'alias' => 'course', 'on' => 'course_modules.course = course.id'));
     $query->add_select(array('table' => 'course', 'column' => 'id', 'alias' => 'courseid', 'distinct' => true));
     $query->add_select(array('table' => 'course', 'column' => 'shortname', 'alias' => 'name'));
     $query->add_select(array('table' => 'course', 'column' => 'fullname', 'alias' => 'tooltip'));
     // We need the config settings too, if there are any.
     // TODO this should be in the config filter.
     $query->add_from(array('join' => 'LEFT JOIN', 'table' => 'block_ajax_marking', 'alias' => 'settings', 'on' => "settings.instanceid = course.id\n                                 AND settings.tablename = 'course'\n                                 AND settings.userid = :settingsuserid"));
     $query->add_param('settingsuserid', $USER->id);
     $query->add_select(array('table' => 'settings', 'column' => 'display'));
     $query->add_select(array('table' => 'settings', 'column' => 'groupsdisplay'));
     $query->add_select(array('table' => 'settings', 'column' => 'id', 'alias' => 'settingsid'));
     $query->add_orderby('course.shortname ASC');
 }
 /**
  * Adds SQL to construct a set of discussion nodes.
  *
  * @static
  * @param block_ajax_marking_query $query
  */
 protected function alter_query(block_ajax_marking_query $query)
 {
     // This will be derived form the coursemodule id, but how to get it cleanly?
     // The query will know, but not easy to get it out. Might have been prefixed.
     // TODO pass this properly somehow.
     $coursemoduleid = required_param('coursemoduleid', PARAM_INT);
     // Normal forum needs discussion title as label, participant usernames as
     // description eachuser needs username as title and discussion subject as
     // description.
     if (block_ajax_marking_forum::forum_is_eachuser($coursemoduleid)) {
         $query->add_select(array('table' => 'firstpost', 'column' => 'subject', 'alias' => 'description'));
     } else {
         $query->add_select(array('table' => 'firstpost', 'column' => 'subject', 'alias' => 'label'));
         // TODO need a SELECT bit to get all userids of people in the discussion
         // instead.
         $query->add_select(array('table' => 'firstpost', 'column' => 'message', 'alias' => 'tooltip'));
     }
     $query->add_from(array('join' => 'INNER JOIN', 'table' => 'forum_discussions', 'alias' => 'outerdiscussions', 'on' => 'countwrapperquery.id = outerdiscussions.id'));
     $query->add_from(array('join' => 'INNER JOIN', 'table' => 'forum_posts', 'alias' => 'firstpost', 'on' => 'firstpost.id = outerdiscussions.firstpost'));
     $query->add_orderby("timestamp ASC");
 }