Commit 8fa082a3 authored by Penny Leach's avatar Penny Leach Committed by Nigel McNie
Browse files

- removed another template related thing (TemplateParserException)

- beginnings of sourcing view block data from the database and some work on the BlockInstance class.
parent 54d3a055
......@@ -140,12 +140,30 @@ class BlockInstance {
throw new InvalidArgumentException("Field $field wasn't found in class " . get_class($this));
}
public function __construct() {
// @todo
public function __construct($id=0, $data=null) {
if (!empty($id)) {
if (empty($data)) {
if (!$data = get_record('block_instance','id',$id)) {
throw new BlockInstanceNotFoundException(get_string('blockinstancenotfound', 'error', $id));
}
}
$this->id = $id;
}
else {
$this->dirty = true;
}
if (empty($data)) {
$data = array();
}
foreach ((array)$data as $field => $value) {
if (property_exists($this, $field)) {
$this->{$field} = $value;
}
}
}
public static function factory($id) {
// @todo
public function to_stdclass() {
return (object)get_object_vars($this);
}
}
......
......@@ -80,4 +80,7 @@ $string['viewnotfoundexceptionmessage'] = 'You tried to access a view that didn\
$string['artefactnotfoundmaybedeleted'] = "Artefact with id %s not found (maybe it has been deleted already?)";
$string['notartefactowner'] = 'You do not own this artefact';
$string['blockinstancednotfound'] = 'Block instance with id %s not found';
?>
......@@ -74,5 +74,6 @@ $string['format.rendermetadata'] = 'Display metadata for this Artefact';
$string['addtutors'] = 'Add Tutors';
$string['viewcreatedsuccessfully'] = 'View created successfully';
$string['invalidcolumn'] = 'Column %s out of range';
?>
......@@ -688,6 +688,11 @@ class EmailException extends SystemException {}
*/
class ArtefactNotFoundException extends NotFoundException {}
/**
* Exception - block instance not found
*/
class BlockInstanceNotFoundException extends NotFoundException {}
/**
* Exception - view not found
*/
......@@ -708,11 +713,6 @@ class GroupNotFoundException extends NotFoundException {}
*/
class QuotaExceededException extends UserException {}
/**
* Exception - anything to do with template parsing
*/
class TemplateParserException extends ConfigException {}
/**
* Exception - Access denied. Throw this if a user is trying to view something they can't
*/
......
......@@ -46,6 +46,8 @@ class View {
private $artefact_hierarchy;
private $contents;
private $ownerobj;
private $numcolumns;
private $columns;
public function __construct($id=0, $data=null) {
if (!empty($id)) {
......@@ -274,6 +276,58 @@ class View {
display_name($releaseuser, $this->get_owner_object()))));
}
/**
* builds up the data structure for this view
* @private
* @return void
*/
private function build_column_datastructure() {
if (!empty($this->columns)) { // we've already built it up
return;
}
$sql = 'SELECT bi.*, vb.id AS vbid, vb.view, vb.block, vb.column, vb.order
FROM {view_block} vb
JOIN {block_instance} bi ON vb.block = bi.id
WHERE vb.view = ?
ORDER BY vb.column, vb.order';
if (!$data = get_records_sql_array($sql, array($this->get('id')))) {
$data = array();
}
// fill up empty columns array keys
for ($i = 1; $i <= $this->get('numcolumns'); $i++) {
$this->columns[$i] = array('blockinstances' => array());
}
foreach ($data as $block) {
$b = new BlockInstance($block->id, (array)$block);
$this->columns[$block->column]['blockinstances'][] = $b->to_stdclass();
}
}
/*
* returns the datastructure for the view's column(s)
*
* @param int $column optional, defaults to returning all columns
* @return mixed array
*/
public function get_column_datastructure($column=0) {
// make sure we've already built up the structure
$this->build_column_datastructure();
if (empty($column)) {
return $this->columns;
}
if (!array_key_exists($column, $this->columns)) {
throw new InvalidArgumentException(get_string('invalidcolumn', 'view', $column));
}
return $this->columns[$column];
}
}
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment