Skip to content

Commit

Permalink
Refactored aiming at filter_manager::course_default_filters_to_course
Browse files Browse the repository at this point in the history
  • Loading branch information
Canx committed Mar 8, 2017
1 parent f3b01e5 commit b174f18
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 40 deletions.
24 changes: 24 additions & 0 deletions classes/filter_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@

class block_xp_filter_course extends block_xp_filter {

public function __construct($courseid) {
$this->courseid = $courseid;
}

// TODO: refactor with filter_default?
public function load($filter_object) {
// TODO: check null
$this->ruledata = $filter_object->ruledata;
$this->points = $filter_object->points;
$this->sortorder = $filter_object->sortorder;
}

public function save() {
// TODO: check null
$record = (object) array(
'id' => $this->id,
'courseid' => $this->courseid,
'ruledata' => $this->ruledata,
'points' => $this->points,
'sortorder' => $this->sortorder,
);

$this->insert_or_update('block_xp_filters', $record);
}

}

Expand Down
3 changes: 3 additions & 0 deletions classes/filter_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
class block_xp_filter_default extends block_xp_filter {

public function load($filter_object) {

// TODO: check null
$this->ruledata = $filter_object->ruledata;
$this->points = $filter_object->points;
$this->sortorder = $filter_object->sortorder;
}

public function save() {
$record = (object) array(
'id' => $this->id,
'ruledata' => $this->ruledata,
'points' => $this->points,
'sortorder' => $this->sortorder,
Expand Down
26 changes: 4 additions & 22 deletions classes/filter_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,18 @@ public static function save_default_filters() {
$default_filters = new block_xp_filters_default();

$default_filters->import($static_filters);

//$filters = self::get_static_filters();

//foreach($filters as $filter) {
// $filter->save_default();
//}
}

/**
* Used when adding block to course
*
* @return void */
public function copy_default_filters_to_course() {
global $DB;

$course_filters = $DB->get_records('block_xp_filters', array('courseid' => $this->get_courseid()));

// Only copy default filters if there are no course filters
if (empty($course_filters)) {
$default_filters = $DB->get_recordset('block_xp_default_filters', array(),
'sortorder ASC, id ASC');
$filters = array();
foreach ($default_filters as $key => $filterdata) {
$filterdata->courseid = $this->get_courseid();
unset($filterdata->id);
$default_filters = new block_xp_filters_default();
$course_filters = new block_xp_filters_course($this->get_courseid());

$filter = block_xp_filter::load_from_data($filterdata);
$filter->save();
}
$default_filters->close();
if ($course_filters->empty()) {
$course_filters->import($default_filters);
}
}

Expand Down
14 changes: 11 additions & 3 deletions classes/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ abstract class block_xp_filters {

protected $filters;

public function __construct() {
$this->filters = array();
$this->load();
}

public abstract function create_filter();

public abstract function load();
Expand All @@ -14,15 +19,14 @@ public function save() {
}
}

// Should import delete previous filters? Dont' think so...
public function import($filters_object) {

if (!method_exists($this, 'create_filter')) {
throw new coding_exception(get_class($this) , " can't import filters");
}

unset($this->filters);

foreach($filters_object->filters as $filter) {
foreach($filters_object->get() as $filter) {
$cloned_filter = $this->create_filter();
$cloned_filter->load($filter);
$this->filters[] = $cloned_filter;
Expand All @@ -31,6 +35,10 @@ public function import($filters_object) {
$this->save();
}

public function empty() {
return empty($this->filters);
}

public function get() {
return $this->filters;
}
Expand Down
25 changes: 16 additions & 9 deletions classes/filters_course.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
<?php

class block_xp_course_filters extends block_xp_filters {
class block_xp_filters_course extends block_xp_filters {

protected $courseid;

public function __construct($id) {
$this->$courseid = $id;
$this->courseid = $id;

parent::__construct();
}

public function load() {
throw new coding_exception('not implemented.');
}
global $DB;

$records = $DB->get_recordset('block_xp_filters',
array('courseid' => $this->courseid));

public function save() {
throw new coding_exception('not implemented.');
unset($this->filters);

foreach ($records as $key => $filter_data) {
$filter = $this->create_filter();
$filter->load($filter_data);
$this->filters[] = $filter;
}
}

public function create_filter() {
return new block_xp_filter_course($this->courseid);
}



}

?>
13 changes: 12 additions & 1 deletion classes/filters_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
class block_xp_filters_default extends block_xp_filters {

public function load() {
throw new coding_exception('not implemented.');
global $DB;

$records = $DB->get_recordset('block_xp_default_filters',
array(), 'sortorder ASC, id ASC');

unset($this->filters);

foreach ($records as $key => $filter_data) {
$filter = $this->create_filter();
$filter->load($filter_data);
$this->filters[] = $filter;
}
}

public function create_filter() {
Expand Down
5 changes: 0 additions & 5 deletions classes/filters_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ public function create_filter() {
throw new coding_exception('Static filters cannot be modified.');
}

public function __construct() {
$this->filters = array();
$this->load();
}

public function load() {
if (empty($this->filters)) {
$this->load_set_1();
Expand Down

0 comments on commit b174f18

Please sign in to comment.