// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * Returns list of teachers matching query.
 *
 * @package   report_ncccscensus
 * @author    Remote-Learner.net Inc
 * @copyright 2014 Remote Learner - http://www.remote-learner.net/
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once '../../config.php';
require_once 'lib.php';
require_login();
$context = context_system::instance();
require_capability('report/ncccscensus:view', $context);
$query = required_param('q', PARAM_RAW);
$callback = required_param('callback', PARAM_RAW);
$courses = json_decode(required_param('c', PARAM_RAW), true);
$categories = json_decode(required_param('cc', PARAM_RAW), true);
$results = report_ncccscensus_teacher_search($query, $courses, $categories);
$json = json_encode($results);
header('Content-Type: application/javascript');
echo "{$callback}({$json})";
 /**
  * This function tests the teacher searching when no course category or course is selected.
  */
 public function test_teachersquery_no_filter()
 {
     global $CFG;
     $this->resetAfterTest(true);
     require_once $CFG->dirroot . '/report/ncccscensus/lib.php';
     $data = $this->createdata_for_teacherfilter();
     // Assign role and remove capability.
     $coursecontext = context_course::instance($data['course1']->id);
     $roles = get_role_names_with_caps_in_context($coursecontext, array('moodle/grade:edit'));
     $roleid = 0;
     foreach ($roles as $rid => $role) {
         $roleid = $rid;
         $this->getDataGenerator()->enrol_user($data['user1']->id, $data['course1']->id, $roleid);
         break;
     }
     // Test: one user is returned.
     $results = report_ncccscensus_teacher_search('teacher', array(), array());
     $this->assertEquals(1, count($results));
     $this->assertArrayHasKey('name', $results[0]);
     $this->assertArrayHasKey('id', $results[0]);
     $this->assertEquals($data['user1']->id, $results[0]['id']);
     // Remove the capability from the user's role in the course.
     role_change_permission($roleid, $coursecontext, 'moodle/grade:edit', CAP_PREVENT);
     // Test: no user is returned
     $results = report_ncccscensus_teacher_search('teacher', array(), array());
     $this->assertEquals(1, count($results));
     $this->assertArrayHasKey('name', $results[0]);
     $this->assertArrayNotHasKey('id', $results[0]);
     // Test: one of two users are returned.
     $this->getDataGenerator()->enrol_user($data['user2']->id, $data['course3']->id, $roleid);
     $results = report_ncccscensus_teacher_search('teacher', array(), array());
     $this->assertEquals(1, count($results));
     $this->assertArrayHasKey('name', $results[0]);
     $this->assertArrayHasKey('id', $results[0]);
     $this->assertEquals($data['user2']->id, $results[0]['id']);
 }