/**
  * Makes sure we have the settings attached to the nodes when we ask for them.
  */
 public function test_attach_config_settings_to_nodes()
 {
     global $DB;
     // Make all the test data and get a total count back.
     $this->make_module_submissions();
     // Make sure the current user is a teacher in the course.
     $teacher = reset($this->teachers);
     $this->setUser($teacher->id);
     $filters = array();
     $filters['courseid'] = 'nextnodefilter';
     // Make a setting to see if we get the value attached.
     $coursesetting = new stdClass();
     $coursesetting->userid = $teacher->id;
     $coursesetting->tablename = 'course';
     $coursesetting->instanceid = $this->course->id;
     $coursesetting->groupsdisplay = 0;
     $DB->insert_record('block_ajax_marking', $coursesetting);
     $nodes = block_ajax_marking_nodes_builder::unmarked_nodes($filters);
     // Should only be one.
     $coursenode = reset($nodes);
     $this->assertObjectHasAttribute('groupsdisplay', $coursenode, 'Course node does not have a groupsdisplay field');
     $this->assertNotNull($coursenode->groupsdisplay, 'Course groupsdisplay setting should be zero but is null.');
     $message = 'Expected to get the groupsdisplay setting attached to the course node, but it\'s not';
     $this->assertEquals(0, $coursenode->groupsdisplay, $message);
     // Now check for coursemodules.
     $assign = reset($this->assigns);
     $filters = array();
     $filters['courseid'] = $this->course->id;
     $filters['coursemoduleid'] = 'nextnodefilter';
     $nodes = block_ajax_marking_nodes_builder::unmarked_nodes($filters);
     $foundit = false;
     foreach ($nodes as $node) {
         if ($node->coursemoduleid == $assign->cmid) {
             $foundit = true;
             $this->assertObjectHasAttribute('groupsdisplay', $node, 'coursemodule node does not have a groupsdisplay field');
             $this->assertNull($node->groupsdisplay, 'Did not get groups display as null on coursemodule node');
         }
     }
     $this->assertTrue($foundit, 'Did not get coursemodule back in nodes array');
     // Now override so that 0 at course level becomes 1.
     $cmsetting = new stdClass();
     $cmsetting->userid = $teacher->id;
     $cmsetting->tablename = 'course_modules';
     $cmsetting->instanceid = $assign->cmid;
     $cmsetting->groupsdisplay = 1;
     $DB->insert_record('block_ajax_marking', $cmsetting);
     $nodes = block_ajax_marking_nodes_builder::unmarked_nodes($filters);
     $foundit = false;
     foreach ($nodes as $node) {
         if ($node->coursemoduleid == $assign->cmid) {
             $foundit = true;
             $this->assertObjectHasAttribute('groupsdisplay', $node, 'coursemodule node does not have a groupsdisplay field');
             $this->assertEquals(1, $node->groupsdisplay, 'Did not get groups display as 1 on coursemodule node after override');
         }
     }
     $this->assertTrue($foundit, 'Did not get coursemodule back in nodes array');
 }
require_once $CFG->dirroot . '/blocks/ajax_marking/classes/nodes_builder.class.php';
block_ajax_marking_login_error();
require_login(0, false);
// TODO might be in a course.
$PAGE->set_context(context_system::instance());
// Each ajax request will have different stuff that we want to pass to the callback function. Using
// required_param() means hard-coding them.
$params = array();
// Need to get the filters in the right order so that the query receives them in the right order.
foreach ($_POST as $name => $value) {
    $params[$name] = clean_param($value, PARAM_ALPHANUMEXT);
}
if (!isset($params['nodeindex'])) {
    print_error('No node index specified for the child node counts');
    die;
}
$nodes = block_ajax_marking_nodes_builder::unmarked_nodes($params);
// Saving bandwidth by stripping out all but the counts and the ids.
$thingswewant = array('recentcount', 'mediumcount', 'overduecount', 'itemcount');
foreach ($nodes as &$node) {
    foreach ($node as $key => $value) {
        if (!in_array($key, $thingswewant)) {
            unset($node->{$key});
        }
    }
}
// Reindex array so we pick it up in js as an array and can find the length. Associative arrays
// with strings for keys are automatically sent as objects.
$nodes = array_values($nodes);
$data = array('childnodecounts' => $nodes, 'nodeindex' => $params['nodeindex']);
echo json_encode($data);
require_once dirname(__FILE__) . '/../../../config.php';
// For unit tests to work.
global $CFG, $PAGE;
require_once $CFG->dirroot . '/blocks/ajax_marking/lib.php';
require_once $CFG->dirroot . '/blocks/ajax_marking/classes/module_base.class.php';
require_once $CFG->dirroot . '/blocks/ajax_marking/classes/nodes_builder.class.php';
block_ajax_marking_login_error();
require_login(0, false);
// TODO might be in a course.
$PAGE->set_context(context_system::instance());
// Each ajax request will have different stuff that we want to pass to the callback function. Using
// required_param() means hard-coding them.
$params = array();
foreach ($_POST as $name => $value) {
    $params[$name] = clean_param($value, PARAM_ALPHANUMEXT);
}
if (!isset($params['currentfilter'])) {
    print_error('No filter specified for node count');
    die;
}
if (!isset($params['filtervalue'])) {
    print_error('No filter value specified for node count');
    die;
}
if (!isset($params['nodeindex'])) {
    print_error('No node index specified for the count');
    die;
}
$nodecounts = block_ajax_marking_nodes_builder::get_count_for_single_node($params);
$data = array('counts' => $nodecounts, 'nodeindex' => $params['nodeindex']);
echo json_encode($data);