summaryrefslogtreecommitdiff
path: root/doc/proposals/instruction_checker.php
blob: 6897e28c13ef941dae8212bda028d249c3f2a3d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
//author: alex temal, doug treadwell
//date: 2009-12-29
//url: http://www.aiyosolutions.com/dtnet/instruction_checker.php.txt

$instructions = file_get_contents('instructions.txt');

$lines = explode("\n", $instructions);

function line_is_section_header($line) {
	if ( ucwords($line) == $line && substr(trim($line), -1) == ':' ) {
		return true;
	} else {
		return false;
	}
}

function get_section($line) {
	return substr(strtolower(trim($line)), 0, -1);
}

function test_out($text) {
	print $text . '<br />';
}

function line_is_blank($line) {
	if ( trim($line) == '' ) {
		return true;
	} else {
		return false;
	}
}

function line_is_not_blank($line) {
	return !line_is_blank($line);
}

$equipment = array();
$materials = array();

foreach ( $lines as $line ) {
	++$line_number;
	if ( line_is_section_header($line) ) {
		$section = get_section($line);
		test_out('Identified section as ' . $section);
	} else if ( line_is_not_blank($line) ) { // is instruction
		test_out('Checking line ' . $line_number);
		if ( preg_match('/([0-9]+\.[0-9]*)(.*)/', $line, $matches) ) {
			list(/*don't need*/,$instruction_number, $instruction) = $matches;			
		} else {
			$instruction_number = '';
			$instruction = $line;
		}

		if ( $section == 'equipment' ) {
			test_out('Adding ' . $instruction . ' to ' . $section);
			$equipment[] = $instruction;
		} else if ( $section == 'materials' ) {
			test_out('Adding ' . $instruction . ' to ' . $section);
			$materials[] = $instruction;
		} else if ( $section == 'instructions' ) {
			test_out('Processing instruction " ' . $instruction . '"');
			$has_equipment = false;
			foreach ( $equipment as $tool ) {
				if ( strpos($instruction, $tool) !== false ) {
					$has_equipment = true;
					break;
				}
			}

			$has_materials = false;
			foreach ( $materials as $material ) {
				if ( strpos($instruction, $material) !== false ) {
					$has_materials = true;
					break;
				}
			}

			if ( !$has_equipment ) {
				print '&nbsp;&nbsp;No equipment specified on instruction ' . $instruction_number . '<br />';
			}
			if ( !$has_materials ) {
				print '&nbsp;&nbsp;No materials specified on instruction ' . $instruction_number . '<br />';
			}
			if ( $has_equipment && $has_materials ) {
				print '&nbsp;&nbsp;Instruction is ok.';
			}
		}

	}
}
?>