changes = array(); $this->altered_tags = array(); $this->id = $xml_data->getAttribute('id'); $this->state = $xml_data->getAttribute('state'); $this->auto_state = $xml_data->getAttribute('autostate'); // ---------- // Check if there are any altered tags, and if so extract // them. // ---------- if ( $xml_data->hasChildNodes() ) { $altered_data = $xml_data->getElementsByTagName('altered'); foreach ( $altered_data as $tag ) { $this->altered_tags[] = new Altered_Tag($tag); } } } // ---------------------------------------- // Constructor // // Initiates instance variables and creates classes based // on the partially extracted data. // // Arguments: // $id is the id of the sub part (ex: Ada.O1) // $state is the state to set for the sub part // $auto_state is the auto_state for the sub part // ---------------------------------------- public function __construct3($id, $state, $auto_state) { $this->changes = array(); $this->altered_tags = array(); $this->is_new = true; $this->id = $id; $this->state = $state; $this->auto_state = $auto_state; if ( (! isset($state)) && isset($auto_state) ) { $this->state = $auto_state; } } // ---------------------------------------- // get_id // // Returns the id of the result sub part. // ---------------------------------------- public function get_id() { return $this->id; } // ---------------------------------------- // get_state // // Returns the state set for the result part. // ---------------------------------------- public function get_state() { return $this->state; } // ---------------------------------------- // get_auto_state // // Returns the state set for the result part. // ---------------------------------------- public function get_auto_state() { return $this->state; } // ---------------------------------------- // set_auto_state // // Set auto state to a new value. // // Arguments: // $new_auto_state - string containing the new auto state. // ---------------------------------------- public function set_auto_state($new_auto_state) { $this->auto_state = $new_auto_state; } // ---------------------------------------- // set_state // // Set a new state for this result part. // // Arguments: // $new_state - string containing the new state. // ---------------------------------------- public function set_state($new_state) { if ( strcasecmp($this->state, "passed") != 0) { $this->state = $new_state; } } // ---------------------------------------- // set_state_unsafe // // Set a new state for this result part ignoring previous results // and thus permitting downgrade of previously passed results. // // Arguments: // $new_state - string containing the new state. // ---------------------------------------- public function set_state_unsafe($new_state) { $this->state = $new_state; } // ---------------------------------------- // get_altered_tags // // Returns an array with all the sub parts to this. // ---------------------------------------- public function get_altered_tags() { return $this->altered_tags; } // ---------------------------------------- // is_new // // Returns a boolean indicating whether the sub part is new to // this student. // ---------------------------------------- public function is_new() { return $this->is_new; } // ---------------------------------------- // has_changes // // Returns true if there are stored changes, false otherwise. // ---------------------------------------- public function has_changes() { return ( isset($this->changes) && ( ! empty($this->changes) ) ) ; } // ---------------------------------------- // insert_altered_tag // // Add another altered tag to this result sub part // // Arguments: // $date is the date of the alteration. // $by is the user making the alteration. // $to_state is the new state for the result sub part, null if // no modification is to be made. // $to_auto_state is the new auto state, null if not modified. // ---------------------------------------- public function insert_altered_tag($date, $by, $to_state, $to_auto_state) { $new_tag = new Altered_Tag($date, $by, $to_state, $to_auto_state); $this->altered_tags[] = $new_tag; $this->changes[] = $new_tag; } // ---------------------------------------- // print_dump // // Print function for debugging / testing. // ---------------------------------------- public function print_dump() { print "\t\t\tCoures sub part id: " . $this->id . ", state: " . $this->state . ", auto-state: " . $this->auto_state . "\n"; foreach ( $this->altered_tags as $tag ) { $tag->print_dump(); } } // ---------------------------------------- // to_json // // Creates a json string representation of the student result. // // Arguments: // $indentation is the count of tabs to indent the json data, // default 0. Used for when the json data should be nested // inside another json object. // // Returns a string containing the json representation of the // student result // ---------------------------------------- public function to_json($indentation = 0) { // Extract and "jsonify" attributes that are always present. $res = str_repeat("\t", $indentation) . "{\n" . str_repeat("\t", $indentation) . "\t\"id\": \"" . $this->id . "\",\n" . str_repeat("\t", $indentation) . "\t\"state\": \"" . $this->state . "\""; // ---------- // Check if the attributes the can be empty contain data, if // so "jsonify" and add them. // ---------- if ( isset($this->auto_state) ) { $res .= ",\n" . str_repeat("\t", $indentation) . "\t\"autostate\": \"" . $this->auto_state . "\""; } $res .= "\n" . str_repeat("\t", $indentation) . "}"; // We do not want to include altered tags in the json-report, // so we stop here. return $res; } // ---------------------------------------- // save_results_to_file // // Stores the results for this student result part on the given // file. We know the student exists in active students for this // course // // Arguments: // $xml_path is the DOMDXpath for the xml_handler document. // $xml_document is the DOMDocument in which to store the changes. // $deadlines is an array of deadlines fetched from course info. // $part_elem the part xml element to append results to. // $uid is the uid of the student, needed to extract xml data. // $part_name is the name of the parent part, needed to locate // xml data. // // Returns true if successfully saved, false otherwise, to handle // errors outside. // ---------------------------------------- public function save_results_to_file($xml_path, $xml_document, $deadlines, $part_elem, $uid, $part_name) { // Extract the student xml part from the xml document. $xml_element = $xml_path->query("//student[@uid='{$uid}']" ."/part[@name='{$part_name}']" ."/lab[@id='{$this->id}']"); if ( ! $xml_element->length || ( ! isset($xml_element) ) ) { // The student / part / subpart combination did not // previously exist in the results document, so we create // it and append the attributes. $sub_elem = $xml_document->createElement('lab'); $sub_elem->setAttribute('id', $this->id); $part_elem->appendChild($sub_elem); } else if ( $xml_element->length > 1 ) { $this->errors[] = "Student with uid: " . $uid . " has multiple entries of sub part: " . $part_name . ":" . $this->id . " in the results xml file."; return false; } else { // Only one entry of this student / course part / subpart // combination existed previously. $sub_elem = $xml_element->item(0); } // ---------- // Check the optional attributes and store them if they // have a value. // ---------- if ( isset($this->state) and ! empty($this->state) ) { $sub_elem->setAttribute('state', $this->state); } if ( isset($this->auto_state) and ! empty($this->auto_state) ) { $sub_elem->setAttribute('autostate', $this->auto_state); } // ---------- // Check if there are any changes (new tags), if so store // them. // ---------- if ( isset($this->changes) and (! empty($this->changes) ) ) { foreach ( $this->changes as $tag ) { $tag->save_results_to_file($xml_document, $sub_elem); } } return true; } // ---------------------------------------- // End of Result_Sub_Part class definition // ---------------------------------------- } // ---------------------------------------------------------------------------- ?>