bos =& $Dippler->backoffice;
		if ($id) {
			$this->id = $id; 
		}
    }
    
    public function create() {
        $created = $this->bos->createAssignment($this->dataXML());
        if ($created) {
            $this->id = $created->id[0][0];
            return $this->id;
        }
        return false;
    }
   
    public function save() {      
        return $this->bos->editAssignment($this->dataXML());
    }
    
    public function delete() {   
        return $this->bos->deleteAssignment($this->idXML());
    }
  
    public function load($loader=false) {
        if (!$loader) {  
            $loader = $this->bos->loadAssignment($this->idXML());
        }
		if ($loader) {
			$this->type = "assignment";
            $this->id = $loader->id[0][0];
            $this->subtype = $loader->subtype[0][0];
            $this->title = $loader->title[0][0];
			$this->description = $loader->description[0][0];
			$this->structure = json_decode($loader->structure[0][0]);
			$this->maxpoints = $loader->maxpoints[0][0];
		    $this->start = $loader->start[0][0];
		    $this->deadline = $loader->deadline[0][0];
			if ($loader->late_submission[0][0] && $loader->late_submission[0][0] == 'true') $this->late_submission = 1;
			if ($loader->submission_visibility[0][0] && $loader->submission_visibility[0][0] == 'true') $this->submission_visibility = 1;
			$this->setTargetGroup($loader->{'target-group'});
            $this->owner = $loader->creator[0][0];
			$this->created = $loader->created[0][0];
            $this->course = $loader->course[0][0];
            if (isset($loader->answers[0]['answer-id'])) {
                $this->setAnswers($loader);
            }
			if (isset($loader->tags[0]["tag"])) {
			    foreach ($loader->tags[0]["tag"] as $tag) {
			        $this->tags []= $tag["name"][0][0];
			    }
			}
			if (isset($loader->learningoutcomes[0]["learningoutcome"])) {
			    foreach ($loader->learningoutcomes[0]["learningoutcome"] as $single) {
					$o = new LearningOutcome();
					$o->load((object) $single);
					$this->outcomes[$o->id] = $o;
				}
			}
        }
    }
    public function setAnswers($loader) {
        if ($this->subtype != "test") {
            foreach ($loader->answers[0]["answer-id"] as $answer_id) {
                $this->answers []= $answer_id[0];
            }
        } else {
            foreach ($loader->answers[0]["answer-id"] as $answer_id) {
                $answer = new Answer($answer_id[0]);
                $answer->load();
                if ($answer->graded != $answer->created) {
                    $this->answers []= $answer_id;
                } 
            }
        }
    }
    public function loadAnswers() {   
        return $this->bos->loadAnswers($this->idXML());
    }
    
    public function getURL() {
        return WWW_ROOT."course/assignments/{$this->course}/assignment/{$this->id}/edit";
    }
    public function getSubmissionsURL() {
        return WWW_ROOT."course/assignments/{$this->course}/assignment/{$this->id}/submissions";
    }
    
    public function getType() {
        return $this->type;
    }
    
    public function canEdit() {
        if ($this->subtype == "test") {
            return 0;
        }
        if (!is_logged_in()) {
            return 0;
        }
        if (is_admin() || ($this->owner == get_logged_in_user_id()) || $this->getCourseEntity()->canEdit()) {
            return 1;
        }
        return 0;
    }
    /**
     * Determines if submission is visible to current viewer.
     * Public submissions are shown to everyone.
     * Private submissions are shown to logged in user with canEdit permission.
     *
     * @return bool
     */
    public function isSubmissionVisible() {
        if ($this->submission_visibility === 1) {
            return true;
        }
        if ($this->canEdit()) {
            return true;
        }
        return false;
    }
    
    public function getId() {
        return $this->id;
    }
    
    public function getOwner() {
        return $this->owner;
    }
    
	public function getOwnerEntity() {
		if (!isset($this->owner_entity)) {
			$user = new User($this->owner);
			$user->load();
			$this->owner_entity = $user;
			return $user;
		}
        return $this->owner_entity;
    }
    public function getCourse() {
        return $this->course;
    }
    public function getCourseEntity() {
        if (!isset($this->course_entity)) {
            $course = new Course($this->course);
            $course->load();
            $this->course_entity = $course;
            return $course;
        }
        return $this->course_entity;
    }
    
    function setTargetGroup($tg) {
        if (array_key_exists("group-id", $tg[0])) {
            if (is_array($tg[0]["group-id"])) {
                foreach ($tg[0]["group-id"] as $tagr) {
                    $this->target_group []= $tagr[0];
                }
            }   
        } 
    }
    
    private function getTargetXML() {
        $xml = "";
        foreach ($this->target_group as $id) {
            $xml .= "{$id}";
        }  
        return $xml;
	}
	public function getAnswers() {
        return $this->answers;
	}
    public function getLearnersAnswers() {
        // Structured learner_id => answer
        $answers = array();
        $data = $this->bos->loadAnswers($this->idXML());
        if (isset($data->learners[0])) {
            foreach ($data->learners[0]["learner"] as $single) {
                if (isset($single['answer'][0])) {
                    $answer = new Answer();
                    $answer->load((object) $single['answer'][0]);
                    $answers[$single['id']['0']['0']] = $answer;
                }
            }
        }
        return $answers;
    }
	
	public function getSubtypeString() {
	    $subtypes = array("blogpost"=>_("Blog Post"),
	                      "reflection"=>_("Reflection"),
	                      "offline"=>_("Offline Task"),
	                      "exercise"=>_("Exercise"),
	                      "file"=>_("Upload File"),
	                      "group" => _("Group"),
	                      "test"=>_("Test"));
		return $subtypes[$this->subtype];
	}
    
    function idXML() {
        $data = "";
        $data .= "";
        $data .= "{$this->id}";
		$data .= "";
		return $data;
    }
    
    function outcomesXML() {
        $data = "";
        foreach ($this->outcomes as $outcome) {
            $data .= "";
            $data .= "id."]]>";
		    $data .= "";
        }
		return $data;
    }
    
    function tagsXML() {
        $data = "";
        foreach ($this->tags as $tag) {
            $data .= "";
            $data .= "";
		    $data .= "";
        }
		return $data;
    }
    
    function dataXML() {
        $tags = $this->tagsXML();
        $outcomes = $this->outcomesXML();
        $data = "";
        $data .= "";
        $data .= "{$this->id}";
        $data .= "subtype}]]>";
		$data .= "title}]]>";
		$data .= "description}]]>";
		$data .= "structure}]]>";
		$data .= "maxpoints}]]>";
		$data .= "start}]]>";
		$data .= "deadline}]]>";
		$data .= "".$this->getTargetXML()."";
		$data .= "{$this->submission_visibility}";
		$data .= "{$this->late_submission}";
		$data .= "".$this->course."";
		$data .= "".$this->owner."";
		$data .= "{$tags}";
		$data .= "{$outcomes}";
		$data .= "";
		return $data;
    }
}
?>