Skip to content

Commit

Permalink
Merge pull request #37 from learnweb/feature/customlogo
Browse files Browse the repository at this point in the history
Feature/customlogo
  • Loading branch information
Laur0r authored Apr 5, 2023
2 parents 99e79d9 + afa2f49 commit 28aecf8
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 21 deletions.
36 changes: 36 additions & 0 deletions block_qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function get_content() {
}

$this->page->requires->js_call_amd('block_qrcode/fullscreenqrcode', 'init', array($qrcode));

return $this->content;
}

Expand All @@ -88,4 +89,39 @@ public function has_config() {
return true;
}

/**
* method for saving custom logos in database
* @param mixed $data
* @param bool $nolongerused
* @return void
* @throws coding_exception
*/
public function instance_config_save($data, $nolongerused = false) {

if (isset($data->customlogosvg)) {
// Saves logo from the customlogosvg filearea.
file_save_draft_area_files(
$data->customlogosvg,
$this->context->id,
'block_qrcode',
'customlogosvg',
0,
['subdirs' => 0, 'maxfiles' => 1]
);

}
if (isset($data->customlogopng)) {
// Saves files from the customlogopng filearea.
file_save_draft_area_files(
$data->customlogopng,
$this->context->id,
'block_qrcode',
'customlogopng',
0,
['subdirs' => 0, 'maxfiles' => 1]
);

}
return parent::instance_config_save($data, $nolongerused);
}
}
82 changes: 61 additions & 21 deletions classes/output_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class output_image {

/**
* Output file type.
* 0 - png, 1 - svg
* 1 - svg, 2 - png
* @var int
*/
protected $format;
Expand Down Expand Up @@ -81,6 +81,11 @@ class output_image {
*/
protected $course;

/**
* Block instanceID.
* @var int
*/
protected $instanceid;
/**
* output_image constructor.
* @param int $format file type
Expand All @@ -93,6 +98,7 @@ public function __construct($format, $size, $courseid, $instanceid) {
$this->format = $format;
$this->size = (int)$size;
$this->course = get_course($courseid);
$this->instanceid = $instanceid;
$file = $CFG->localcachedir . '/block_qrcode/course-' .
(int)$courseid . '-' . $this->size; // Set file path.

Expand Down Expand Up @@ -268,29 +274,63 @@ private function modify_svg($svgqrcode, $logopath) {
}

/**
* Returns the stored file of the logo, if possible.
* Returns the stored file of the customlogo, if possible.
* If not, returns the stored file of the default logo.
* @return \stored_file|null stored file
*/
private function get_logo() {
if ($this->format == 2) {
$filearea = 'logo_png';
$completepath = get_config('block_qrcode', 'logofile_png');
} else {
$filearea = 'logo_svg';
$completepath = get_config('block_qrcode', 'logofile_svg');
}
public function get_logo() {

$fssvg = get_file_storage();
$fspng = get_file_storage();

// Get File.
$filessvg = $fssvg->get_area_files(\context_block::instance($this->instanceid)->id,
'block_qrcode', 'customlogosvg', 0, 'sortorder', false);
$filespng = $fspng->get_area_files(\context_block::instance($this->instanceid)->id,
'block_qrcode', 'customlogopng', 0, 'sortorder', false);

$fs = get_file_storage();
$filepath = pathinfo($completepath, PATHINFO_DIRNAME);
$filename = pathinfo($completepath, PATHINFO_BASENAME);
$file = $fs->get_file(\context_system::instance()->id,
'block_qrcode',
$filearea,
0,
$filepath,
$filename);
if ($file) {
return $file;
if ($this->format == 1) {
if (count($filessvg) == 1 AND get_config('block_qrcode', 'allow_customlogo') == 1) {
$filesvg = reset($filessvg);
return $filesvg;
} else {
$filearea = 'logo_svg';
$completepath = get_config('block_qrcode', 'logofile_svg');

$fs = get_file_storage();
$filepath = pathinfo($completepath, PATHINFO_DIRNAME);
$filename = pathinfo($completepath, PATHINFO_BASENAME);
$file = $fs->get_file(\context_system::instance()->id,
'block_qrcode',
$filearea,
0,
$filepath,
$filename);
if ($file) {
return $file;
}
}
} else {
if (count($filespng) == 1 AND get_config('block_qrcode', 'allow_customlogo') == 1) {
$filepng = reset($filespng);
return $filepng;
} else {
$filearea = 'logo_png';
$completepath = get_config('block_qrcode', 'logofile_png');

$fs = get_file_storage();
$filepath = pathinfo($completepath, PATHINFO_DIRNAME);
$filename = pathinfo($completepath, PATHINFO_BASENAME);
$file = $fs->get_file(\context_system::instance()->id,
'block_qrcode',
$filearea,
0,
$filepath,
$filename);
if ($file) {
return $file;
}
}
}
return null;
}
Expand Down
50 changes: 50 additions & 0 deletions edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,55 @@ protected function specific_definition($mform) {
$mform->disabledIf('config_instc_uselogo', 'config_usedefault', 'checked');
$mform->setDefault('config_instc_uselogo', true);
$mform->setType('config_instc_uselogo', PARAM_BOOL);

// If the admin settings don't allow a customlogo, the upload option should be disabled.
if (get_config('block_qrcode', 'allow_customlogo') == 1) {

// File Area for Customlogo as svg.
$mform->addElement('filemanager', 'config_customlogosvg', get_string('customfilesvg', 'block_qrcode'),
null,
[
'subdirs' => 0,
'areamaxbytes' => 10485760,
'maxfiles' => 1,
'accepted_types' => ['.svg'],
]
);

$mform->addElement('checkbox', 'uploadpng', get_string('uploadpng', 'block_qrcode'));

// File Area for Customlogo as png.
$mform->addElement('filemanager', 'config_customlogopng', get_string('customfilepng', 'block_qrcode'),
null,
[
'subdirs' => 0,
'areamaxbytes' => 10485760,
'maxfiles' => 1,
'accepted_types' => ['.png'],
]
);

// Only the svg custom logo is displayed in the Qr code.
// Therefore, the file area for .png is hidden until the checkbox is checked,
// in order to discourage uploading only a .png file.
$mform->hideIf('config_customlogopng', 'uploadpng', 'notchecked');

}

}

/**
* Copies existing logos into draft areas.
* @param mixed $defaults
* @return void
*/
public function set_data($defaults) {
$draftitemidsvg = file_get_submitted_draft_itemid('customlogosvg');
$draftitemidpng = file_get_submitted_draft_itemid('customlogopng');
file_prepare_draft_area($draftitemidsvg, $defaults->parentcontextid, 'block_qrcode', 'customlogosvg', 0);
file_prepare_draft_area($draftitemidpng, $defaults->parentcontextid, 'block_qrcode', 'customlogopng', 0);
$defaults->customlogosvg = $draftitemidsvg;
$defaults->customlogopng = $draftitemidpng;
parent::set_data($defaults);
}
}
5 changes: 5 additions & 0 deletions lang/en/block_qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,8 @@
$string['usedefault'] = 'Use default settings';
$string['instc_uselogo'] = 'Use logo';
$string['privacy:metadata'] = 'The qrcode block only displays information about a course, but does not effect or store any personal data.';
$string['customfilesvg'] = 'Custom File .svg';
$string['customfilepng'] = 'Custom File .png';
$string['uploadpng'] = 'Upload Customlogo as .png also';
$string['allow_customlogo'] = 'Allow Customlogo';
$string['allow_customlogo_help'] = 'If the checkbox is checked, it allows teachers to upload a custom logo to be shown in the QR code';
7 changes: 7 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@
get_string('logofile_svg', 'block_qrcode'),
'', 'logo_svg', 0, ['accepted_types' => '.svg']
));

$settings->add(new admin_setting_configcheckbox('block_qrcode/allow_customlogo',
get_string('allow_customlogo', 'block_qrcode'),
get_string('allow_customlogo_help', 'block_qrcode'),
'0'
));

}
1 change: 1 addition & 0 deletions tests/behat/behat_block_qrcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_once(__DIR__ . '/../../../../lib/behat/behat_base.php');
use Behat\Mink\Exception\ExpectationException as ExpectationException;


/**
* Block QR code functionalities for behat-testing.
*
Expand Down
1 change: 1 addition & 0 deletions tests/output_image_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@ public function test_no_logo() {
$outputimg->create_image();
$this->assertFileExists($file);
}

}

0 comments on commit 28aecf8

Please sign in to comment.