Skip to content

Commit

Permalink
Add recursive category search
Browse files Browse the repository at this point in the history
  • Loading branch information
Syxton committed Dec 20, 2018
1 parent f5b249c commit 40ba3b8
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
27 changes: 23 additions & 4 deletions classes/processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ class tool_coursearchiver_processor {
/** @var int total processed. */
public $total = 0;

/** @var int total processed. */
/** @var int sub folder of archive process. */
public $folder = false;

/** @var int total processed. */
/** @var int only return empty courses. */
public $emptyonly = false;

/** @var int recursive category search. */
public $subcats = false;

/** @var int data passed into processor. */
protected $data = array();

Expand All @@ -105,6 +108,7 @@ class tool_coursearchiver_processor {
"idnum" => "idnumber",
"teacher" => "teacher",
"catid" => "category",
"subcats" => "subcats",
"createdbefore" => "timecreated",
"access" => "timeaccess",
"emptyonly" => "emptyonly");
Expand Down Expand Up @@ -1044,9 +1048,21 @@ public function get_courselist() {
)
)
)';
} else if ($truekey == "id" || $truekey == "category") {
} else if ($truekey == "id") {
$params[$truekey] = $value;
$searchsql .= " AND c.$truekey = :$truekey";
} else if ($truekey == "category") {
if (!empty($this->data["subcats"])) {
$params[$truekey] = $value;
$params["subcats"] = "%/$value/%";
$searchsql .= " AND (c.$truekey = :$truekey
OR " .
$DB->sql_like("b.path", ":subcats", false, false) .
")";
} else {
$params[$truekey] = $value;
$searchsql .= " AND c.$truekey = :$truekey";
}
} else if ($truekey == "timecreated") {
$params['createdbefore'] = $value;
// Course had to be created prior to this date.
Expand All @@ -1061,6 +1077,8 @@ public function get_courselist() {
$searchsql .= " AND (a.$truekey <= :$truekey OR a.timeaccess IS NULL)";
} else if ($truekey == "emptyonly") {
$this->emptyonly = true;
} else if ($truekey == "subcats") {
$this->subcats = true;
} else {
$params[$truekey] = '%' . $DB->sql_like_escape("$value") . '%';
$searchsql .= " AND " . $DB->sql_like($truekey, ":$truekey", false, false);
Expand All @@ -1082,8 +1100,9 @@ public function get_courselist() {
public function courselist_sql($searchsql, $params) {
global $DB;
$sql = "SELECT c.id, c.fullname, c.category, c.shortname, c.idnumber,
c.visible, a.timeaccess
c.visible, a.timeaccess, b.path
FROM {course} c
LEFT JOIN {course_categories} b ON c.category = b.id
LEFT JOIN (
SELECT a.courseid, a.timeaccess
FROM {user_lastaccess} a
Expand Down
3 changes: 3 additions & 0 deletions classes/step1_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public function definition () {
$mform->addElement('select', 'searches[catid]', get_string('category', 'tool_coursearchiver'), $displaylist);
$mform->setDefault('searches[catid]', "");

$mform->addElement('checkbox', 'subcats', get_string('includesubcat', 'tool_coursearchiver'));
$mform->disabledIf('subcats', 'searches[catid]', 'eq', 0);

$createdbefore = array();
$createdbefore[] =& $mform->createElement('date_selector', 'createdbefore');
$createdbefore[] =& $mform->createElement('checkbox', 'createdbeforeenabled', '', get_string('enable'));
Expand Down
3 changes: 3 additions & 0 deletions cli/coursearchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'idnum' => false,
'teacher' => false,
'catid' => false,
'subs' => false,
'help' => false,
'createdbefore' => false,
'access' => false,
Expand All @@ -53,6 +54,7 @@
'h' => 'help',
't' => 'teacher',
'c' => 'catid',
'r' => 'subcats',
'b' => 'createdbefore',
'a' => 'access',
'm' => 'mode',
Expand All @@ -74,6 +76,7 @@
-i, --id Search for courses matching the Moodle course id
-n, --idnum Search for courses matching the Moodle course idnumber
-c, --catid Search for courses matching the Moodle category id
-r, --subcats Recursively search the course categories
-t, --teacher Search for courses that have teacher with matching username or email
-b, --createdbefore Course created before UNIX TIMESTAMP
-a, --access Last accessed before UNIX TIMESTAMP
Expand Down
4 changes: 4 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
$formdata->searches["emptyonly"] = true;
}

if (!empty($formdata->subcats)) {
$formdata->searches["subcats"] = true;
}

$SESSION->formdata = serialize($formdata->searches);
$returnurl = new moodle_url('/admin/tool/coursearchiver/step2.php');
redirect($returnurl);
Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_coursearchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
$string['errorvalidarchive'] = 'Not a valid backup file.';
$string['hide'] = 'Hide Courses';
$string['hideemail'] = 'Send "Course to be Hidden" Emails';
$string['includesubcat'] = 'Include courses in subcategories';
$string['invalidmode'] = 'A valid mode for the tool was not given.';
$string['never'] = 'Never';
$string['nocoursesfound'] = 'The search has resulted in 0 courses found.';
Expand Down
2 changes: 1 addition & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
padding-left: 0;
}

.path-admin-tool-coursearchiver .form-check-input:not(#id_emptyonly) {
.path-admin-tool-coursearchiver .form-check-input:not(#id_emptyonly):not(#id_subcats) {
position: unset;
vertical-align: middle;
}
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();

$plugin = new stdClass();
$plugin->version = 2018121303; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2018121304; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014111000; // Requires this Moodle version.
$plugin->component = 'tool_coursearchiver'; // Full name of the plugin (used for diagnostics).
$plugin->release = '3.7.1 (Build: 2016090200)';
Expand Down

0 comments on commit 40ba3b8

Please sign in to comment.