Skip to content

Commit

Permalink
Refactor var assignment in if clause
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Jan 23, 2024
1 parent 98b0341 commit 9c47084
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 67 deletions.
3 changes: 2 additions & 1 deletion entries_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ public function get_entries($options = null) {

// Get perpage subset.
} else {
if (!$filter->groupby && $perpage = $filter->perpage) {
$perpage = $filter->perpage;
if (!$filter->groupby && $perpage) {

// A random set (filter->selection == 1).
if (!empty($filter->selection)) {
Expand Down
4 changes: 2 additions & 2 deletions field/editor/field_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public function update_content(stdClass $entry, array $values = null) {
$rec = new stdClass();
$rec->fieldid = $fieldid;
$rec->entryid = $entryid;

if (!$rec->id = $contentid) {
$rec->id = $contentid;
if (!$rec->id) {
$rec->id = $DB->insert_record('datalynx_contents', $rec);
}
// The editor's content is an array, so reset is used in order to access the data in the.
Expand Down
6 changes: 3 additions & 3 deletions field/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
}

// Any notifications.
if (!$fields = $df->get_fields(null, false, true,
flexible_table::get_sort_for_table('datalynxfieldsindex' . $df->id()))
) {
$fields = $df->get_fields(null, false, true,
flexible_table::get_sort_for_table('datalynxfieldsindex' . $df->id()));
if (!$fields) {
$df->notifications['bad'][] = get_string('fieldnoneindatalynx', 'datalynx'); // Nothing in.
// Datalynx.
}
Expand Down
6 changes: 4 additions & 2 deletions field/tag/field_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,17 @@ public function update_content(stdClass $entry, array $values = null) {
$rec->entryid = $entryid;

// Remove content from entry and remove tags from item when tags were removed in entry.
if (empty($tags) && $rec->id = $contentid) {
$rec->id = $contentid;
if (empty($tags) && $rec->id) {
$rec->content = "";
$DB->update_record('datalynx_contents', $rec);
core_tag_tag::remove_all_item_tags('mod_datalynx', 'datalynx_contents', $contentid);
return $rec->id;
}

// Create empty datalynx_contents entry in order to get id for processing tags.
if (!$rec->id = $contentid) {
$rec->id = $contentid;
if (!$rec->id) {
$rec->id = $DB->insert_record('datalynx_contents', $rec);
}
core_tag_tag::set_item_tags('mod_datalynx', 'datalynx_contents', $rec->id, $this->df->context, $tags);
Expand Down
3 changes: 2 additions & 1 deletion import.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
// Any notifications?
$df->notifications['bad']['defaultview'] = '';
$df->notifications['bad']['getstartedviews'] = '';
if (!$views = $df->get_views_by_type('csv', true)) {
$views = $df->get_views_by_type('csv', true);
if (!$views) {
$df->notifications['bad'][] = get_string('importnoneindatalynx', 'datalynx'); // Nothing in.
// Database.
}
Expand Down
8 changes: 4 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
require_once("$CFG->dirroot/mod/datalynx/lib.php");

$id = required_param('id', PARAM_INT); // Course id.

if (!$course = $DB->get_record('course', array('id' => $id))) {
$course = $DB->get_record('course', array('id' => $id));
if (!$course) {
throw new moodle_exception('invalidcourseid');
}

Expand All @@ -48,8 +48,8 @@
$PAGE->set_heading($course->fullname);

echo $OUTPUT->header();

if (!$datalynxs = get_all_instances_in_course("datalynx", $course)) {
$datalynxs = get_all_instances_in_course("datalynx", $course);
if (!$datalynxs) {
notice(get_string('thereareno', 'moodle', $modulenameplural),
new moodle_url('/course/view.php', array('id', $course->id)));
}
Expand Down
73 changes: 42 additions & 31 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ function datalynx_add_instance($data) {
if ($CFG->datalynx_maxentries) {
$data->maxentries = $CFG->datalynx_maxentries;
}

if (!$data->id = $DB->insert_record('datalynx', $data)) {
$data->id = $DB->insert_record('datalynx', $data);
if (!$data->id) {
return false;
}

Expand Down Expand Up @@ -165,8 +165,8 @@ function datalynx_update_instance($data) {
*/
function datalynx_delete_instance($id) {
global $DB;

if (!$data = $DB->get_record('datalynx', array('id' => $id))) {
$data = $DB->get_record('datalynx', array('id' => $id));
if (!$data) {
return false;
}

Expand Down Expand Up @@ -311,8 +311,8 @@ function datalynx_reset_userdata($data) {
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid => $unused) {
fulldelete("$CFG->dataroot/$data->courseid/moddata/datalynx/$dataid");

if (!$cm = get_coursemodule_from_instance('datalynx', $dataid)) {
$cm = get_coursemodule_from_instance('datalynx', $dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
Expand Down Expand Up @@ -347,7 +347,8 @@ function datalynx_reset_userdata($data) {
$record->userdeleted || !is_enrolled($coursecontext, $record->userid)
) {
// Delete ratings.
if (!$cm = get_coursemodule_from_instance('datalynx', $record->dataid)) {
$cm = get_coursemodule_from_instance('datalynx', $record->dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
Expand Down Expand Up @@ -382,7 +383,8 @@ function datalynx_reset_userdata($data) {
if (!empty($data->reset_datalynx_ratings)) {
if ($datas = $DB->get_records_sql($alldatassql, array($data->courseid))) {
foreach ($datas as $dataid => $unused) {
if (!$cm = get_coursemodule_from_instance('datalynx', $dataid)) {
$cm = get_coursemodule_from_instance('datalynx', $dataid);
if (!$cm) {
continue;
}
$datacontext = context_module::instance($cm->id);
Expand Down Expand Up @@ -477,17 +479,16 @@ function datalynx_get_file_info($browser, $areas, $course, $cm, $context, $filea
require_once($CFG->dirroot . '/mod/datalynx/locallib.php');
return new datalynx_file_info_container($browser, $course, $cm, $context, $areas, $filearea);
}

if (!$view = $DB->get_record('datalynx_views', array('id' => $itemid))) {
$view = $DB->get_record('datalynx_views', array('id' => $itemid));
if (!$view) {
return null;
}

$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
if (!($storedfile = $fs->get_file($context->id, 'mod_datalynx', $filearea, $itemid, $filepath,
$filename))
) {
$storedfile = $fs->get_file($context->id, 'mod_datalynx', $filearea, $itemid, $filepath, $filename);
if (!$storedfile) {
return null;
}

Expand Down Expand Up @@ -521,20 +522,20 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
) {

$contentid = (int) array_shift($args);

if (!$content = $DB->get_record('datalynx_contents', array('id' => $contentid))) {
$content = $DB->get_record('datalynx_contents', array('id' => $contentid));
if (!$content) {
return false;
}

if (!$field = $DB->get_record('datalynx_fields', array('id' => $content->fieldid))) {
$field = $DB->get_record('datalynx_fields', array('id' => $content->fieldid));
if (!$field) {
return false;
}

if (!$entry = $DB->get_record('datalynx_entries', array('id' => $content->entryid))) {
$entry = $DB->get_record('datalynx_entries', array('id' => $content->entryid));
if (!$entry) {
return false;
}

if (!$datalynx = $DB->get_record('datalynx', array('id' => $field->dataid))) {
$datalynx = $DB->get_record('datalynx', array('id' => $field->dataid));
if (!$datalynx) {
return false;
}

Expand Down Expand Up @@ -600,8 +601,10 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) || $file->is_directory()) {
if (!$file = $fs->get_file_by_hash(sha1($oldpath)) || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
Expand All @@ -620,8 +623,10 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) || $file->is_directory()) {
if (!$file = $fs->get_file_by_hash(sha1($oldpath)) || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
Expand All @@ -639,8 +644,10 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) || $file->is_directory()) {
if (!$file = $fs->get_file_by_hash(sha1($oldpath)) || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
Expand All @@ -657,8 +664,10 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
$oldpath = "/$context->id/mod_dataform/$filearea/$relativepath";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) || $file->is_directory()) {
if (!$file = $fs->get_file_by_hash(sha1($oldpath)) || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
Expand All @@ -677,8 +686,10 @@ function mod_datalynx_pluginfile($course, $cm, $context, $filearea, $args, $forc
$oldpath = "/$context->id/mod_dataform/$filearea/$itemid/$relativepath";

$fs = get_file_storage();
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) || $file->is_directory()) {
if (!$file = $fs->get_file_by_hash(sha1($oldpath)) || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($fullpath));
if (!$file || $file->is_directory()) {
$file = $fs->get_file_by_hash(sha1($oldpath));
if (!$file || $file->is_directory()) {
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public static function base_supported_formats() {
* @throws portfolio_caller_exception
*/
public function load_data() {
if (!$this->cm = get_coursemodule_from_id('datalynx', $this->id)) {
$this->cm = get_coursemodule_from_id('datalynx', $this->id);
if (!$this->cm) {
throw new portfolio_caller_exception('invalidid', 'datalynx');
}
}
Expand Down
3 changes: 2 additions & 1 deletion rule/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
}

// Any notifications?
if (!$rules = $rm->get_rules()) {
$rules = $rm->get_rules();
if (!$rules) {
$df->notifications['bad'][] = get_string('rulesnoneindatalynx', 'datalynx'); // Nothing in.
// Datalynx.
}
Expand Down
4 changes: 2 additions & 2 deletions rule/rule_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public function insert_rule($fromform = null) {
if (!empty($fromform)) {
$this->set_rule($fromform);
}

if (!$this->rule->id = $DB->insert_record('datalynx_rules', $this->rule)) {
$this->rule->id = $DB->insert_record('datalynx_rules', $this->rule);
if (!$this->rule->id) {
echo $OUTPUT->notification('Insertion of new rule failed!');
return false;
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/behat/approve_entries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Feature: Filter approved and not approved entries from multiple students
And I am on "Course 1" course homepage
And I follow "Datalynx Test Instance"
And I follow the datalynx "Manage" link
And I follow "Views"
And I follow the datalynx "Views" link
And I follow "Manage view"
And I click on "//td[text()='Text of student1']/following-sibling::td/a[@class='datalynxfield__approve']" "xpath_element"
Then I wait until "approved" "text" exists
Expand Down
3 changes: 3 additions & 0 deletions tests/behat/behat_mod_datalynx.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@ public function i_follow_the_datalynx_link($linktext) {
case 'Manage':
$link = $page->find('css', sprintf('.nav-item [title="Manage"]', $linktext));
break;
case 'Views':
$link = $page->find('css', sprintf('.nav-item [title="Views"]', $linktext));
break;
}
if (null === $link) {
throw new \RuntimeException(sprintf('The link "%s" was not found or is not visible', $linktext));
Expand Down
6 changes: 4 additions & 2 deletions tool/entryperuser/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public static function run($df) {
global $DB;

// Get gradebook users.
if (!$users = $df->get_gradebook_users()) {
$users = $df->get_gradebook_users();
if (!$users) {
return;
}

Expand All @@ -54,7 +55,8 @@ public static function run($df) {

if (is_array($processed)) {
list($strnotify, $processedeids) = $processed;
if ($entriesprocessed = ($processedeids ? count($processedeids) : 0)) {
$entriesprocessed = $processedeids ? count($processedeids) : 0;
if ($entriesprocessed) {
return array('good', $strnotify);
}
}
Expand Down
8 changes: 4 additions & 4 deletions view/csv/import_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ protected function field_settings() {
list($pattern, $header, ) = $column;
$patternname = trim($pattern, '[#]');
$header = $header ? $header : $patternname;

if (!$fieldid = $view->get_pattern_fieldid($pattern)) {
$fieldid = $view->get_pattern_fieldid($pattern);
if (!$fieldid) {
continue;
}

if (!$field = $df->get_field_from_id($fieldid)) {
$field = $df->get_field_from_id($fieldid);
if (!$field) {
continue;
}

Expand Down
7 changes: 4 additions & 3 deletions view/csv/view_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ public function process_export($range = self::EXPORT_PAGE) {
global $CFG;

require_once($CFG->libdir . '/csvlib.class.php');

if (!$csvcontent = $this->get_csv_content($range)) {
$csvcontent = $this->get_csv_content($range);
if (!$csvcontent) {
return;
}
$datalynxname = $this->_df->name();
Expand Down Expand Up @@ -490,7 +490,8 @@ public function process_csv(&$data, $csvcontent, $options = null) {
}

// Csv column headers.
if (!$fieldnames = $cir->get_columns()) {
$fieldnames = $cir->get_columns();
if (!$fieldnames) {
$data->error = $cir->get_error();
return $data;
}
Expand Down
3 changes: 2 additions & 1 deletion view/grid/view_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function generate_default_view() {
global $OUTPUT; // Needed for mustache implementation.

// Get all the fields.
if (!$fields = $this->_df->get_fields()) {
$fields = $this->_df->get_fields());
if (!$fields) {
return; // You shouldn't get that far if there are no user fields.
}

Expand Down
6 changes: 3 additions & 3 deletions view/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@

// Any notifications?
$dl->notifications['bad']['defaultview'] = '';
if (!$views = $dl->get_views(null, true,
flexible_table::get_sort_for_table('datalynxviewsindex' . $dl->id()))
) {
$views = $dl->get_views(null, true,
flexible_table::get_sort_for_table('datalynxviewsindex' . $dl->id()));
if (!$views) {
$dl->notifications['bad']['getstartedviews'] = get_string('viewnoneindatalynx', 'datalynx'); // Nothing.
// In.
// Database.
Expand Down
3 changes: 2 additions & 1 deletion view/pdf/view_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ public function display(array $options = array()) {
*/
public function generate_default_view() {
// Get all the fields.
if (!$fields = $this->_df->get_fields()) {
$fields = $this->_df->get_fields();
if (!$fields) {
return; // You shouldn't get that far if there are no user fields.
}

Expand Down
Loading

0 comments on commit 9c47084

Please sign in to comment.