Plugin: Extra reports by author: Improve plugin code structure and documentation

pull/4550/head
Yannick Warnier 3 years ago
parent 771c12f75c
commit 9969f7f0ab
  1. 248
      plugin/check_extra_field_author_company/CheckExtraFieldAuthorsCompanyPlugin.php
  2. 37
      plugin/check_extra_field_author_company/README.md
  3. 10
      plugin/check_extra_field_author_company/plugin.php

@ -17,12 +17,32 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
/**
* @var bool
*/
protected $authorsExist;
protected $companyExists;
/**
* @var bool
*/
protected $companyExist;
protected $authorsExists;
/**
* @var bool
*/
protected $priceExists;
/**
* @var bool
*/
protected $authorLpItemExists;
/**
* @var bool
*/
protected $authorLpExists;
/**
* @var array
*/
protected $companyField;
/**
* @var array
@ -32,7 +52,17 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
/**
* @var array
*/
protected $companyField;
protected $priceField;
/**
* @var array
*/
protected $authorLpItemField;
/**
* @var array
*/
protected $authorLpField;
public function __construct()
{
@ -44,9 +74,9 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
$this->tblExtraFieldOption = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
$field = new ExtraField('user');
$companyField = $field->get_handler_field_info_by_field_variable('company');
$this->companyExist = false;
if (empty($companyField)) {
$this->companyExist = true;
$this->companyExists = false;
if (!empty($companyField)) {
$this->companyExists = true;
$this->companyField = $companyField;
} else {
$this->companyField = [
@ -64,9 +94,9 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
$field = new ExtraField('lp');
$authorsField = $field->get_handler_field_info_by_field_variable('authors');
$this->authorsExist = false;
$this->authorsExists = false;
if (empty($authorsField)) {
$this->authorsExist = true;
$this->authorsExists = true;
$this->authorsField = $authorsField;
}
}
@ -102,18 +132,20 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
public function saveCompanyField()
{
$data = $this->companyField;
$data['field_type'] = (int) $data['field_type'];
$data['field_order'] = (int) $data['field_order'];
$data['visible_to_self'] = (int) $data['visible_to_self'];
$data['visible_to_others'] = (int) $data['visible_to_others'];
$data['changeable'] = (int) $data['changeable'];
$data['filter'] = (int) $data['filter'];
$data['default_value'] = '';
$data['variable'] = 'company';
$data['visible'] = 1;
$data['display_text'] = strtolower(Database::escape_string($data['display_text']));
$schedule = new ExtraField('user');
$this->companyField['id'] = $schedule->save($data);
if (!empty($data)) {
$data['field_type'] = (int) $data['field_type'];
$data['field_order'] = (int) $data['field_order'];
$data['visible_to_self'] = (int) $data['visible_to_self'];
$data['visible_to_others'] = (int) $data['visible_to_others'];
$data['changeable'] = (int) $data['changeable'];
$data['filter'] = (int) $data['filter'];
$data['default_value'] = '';
$data['variable'] = 'company';
$data['visible'] = 1;
$data['display_text'] = strtolower(Database::escape_string($data['display_text']));
$schedule = new ExtraField('user');
$this->companyField['id'] = $schedule->save($data);
}
}
/**
@ -230,14 +262,26 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
*/
public function uninstall()
{
$companyExist = $this->companyFieldExist();
if ($companyExist == true) {
$companyExists = $this->companyFieldExists();
if ($companyExists == true) {
// $this->removeCompanyField();
}
$authorsExist = $this->authorsFieldExist();
if ($authorsExist == true) {
$authorsExists = $this->authorsFieldExists();
if ($authorsExists == true) {
// $this->removeAuthorsField();
}
$priceExists = $this->priceFieldExists();
if ($priceExists == true) {
// $this->>removePriceField();
}
$authorLpItemExists = $this->authorLpItemFieldExists();
if ($authorLpItemExists == true) {
// $this->removeAuthorLpItemField();
}
$authorLpExists = $this->authorLpFieldExists();
if ($authorLpExists == true) {
// $this->removeAuthorLpField();
}
}
/**
@ -245,12 +289,12 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
*
* @return bool
*/
public function companyFieldExist()
public function companyFieldExists(): bool
{
$this->getCompanyField();
$this->companyExist = (isset($this->companyField['id'])) ? true : false;
$this->companyExists = (isset($this->companyField['id'])) ? true : false;
return $this->companyExist;
return $this->companyExists;
}
/**
@ -276,12 +320,12 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
*
* @return bool
*/
public function authorsFieldExist()
public function authorsFieldExists(): bool
{
$this->getAuthorsField();
$this->authorsExist = (isset($this->authorsField['id'])) ? true : false;
$this->authorsExists = (isset($this->authorsField['id'])) ? true : false;
return $this->authorsExist;
return $this->authorsExists;
}
/**
@ -303,6 +347,102 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
return $data;
}
/**
* Verify that the "price" field exists in the database.
*
* @return bool
*/
public function priceFieldExists(): bool
{
$this->getPriceField();
$this->priceExists = (isset($this->priceField['id'])) ? true : false;
return $this->priceExists;
}
/**
* Returns the content of the extra field "price" if it exists in the database, if not, it returns an arrangement
* with the basic elements for its operation.
*
* @return array
*/
public function getPriceField()
{
$schedule = new ExtraField('lp_item');
$data = $schedule->get_handler_field_info_by_field_variable('price');
if (empty($data)) {
$this->priceField = $data;
} else {
$data = $this->priceField;
}
return $data;
}
/**
* Verify that the "authorlpitem" field exists in the database.
*
* @return bool
*/
public function authorLpItemFieldExists(): bool
{
$this->getAuthorLpItemField();
$this->authorLpItemExists = (isset($this->authorLpItemField['id'])) ? true : false;
return $this->authorLpItemExists;
}
/**
* Returns the content of the extra field "authorlpitem" if it exists in the database, if not, it returns an arrangement
* with the basic elements for its operation.
*
* @return array
*/
public function getAuthorLpItemField()
{
$schedule = new ExtraField('lp_item');
$data = $schedule->get_handler_field_info_by_field_variable('authorlpitem');
if (empty($data)) {
$this->authorLpItemField = $data;
} else {
$data = $this->authorLpItemField;
}
return $data;
}
/**
* Verify that the "authorlp" field exists in the database.
*
* @return bool
*/
public function authorLpFieldExists(): bool
{
$this->getAuthorLpField();
$this->authorLpExists = (isset($this->authorLpField['id'])) ? true : false;
return $this->authorLpExists;
}
/**
* Returns the content of the extra field "authorlp" if it exists in the database, if not, it returns an arrangement
* with the basic elements for its operation.
*
* @return array
*/
public function getAuthorLpField()
{
$field = new ExtraField('user');
$data = $field->get_handler_field_info_by_field_variable('authorlp');
if (empty($data)) {
$this->authorLpField = $data;
} else {
$data = $this->authorLpField;
}
return $data;
}
/**
* Remove the extra fields "company".
*/
@ -321,6 +461,33 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
// $this->deleteQuery($data);
}
/**
* Remove the extra fields "price".
*/
public function removePriceField()
{
$data = $this->getPriceField();
// $this->deleteQuery($data);
}
/**
* Remove the extra fields "authorlpitem".
*/
public function removeAuthorLpItemField()
{
$data = $this->getAuthorLpItemField();
// $this->deleteQuery($data);
}
/**
* Remove the extra fields "authorlp".
*/
public function removeAuthorLpField()
{
$data = $this->getAuthorLpField();
// $this->deleteQuery($data);
}
/**
* Executes fix removal for authors or company.
*
@ -328,19 +495,30 @@ class CheckExtraFieldAuthorsCompanyPlugin extends Plugin
*/
protected function deleteQuery($data)
{
$exist = null;
$validVariable = false;
$variable = $data['variable'];
$extraFieldTypeInt = (int) $data['extra_field_type'];
$FieldType = (int) $data['field_type'];
$id = (int) $data['id'];
$extraFieldType = null;
switch ($variable) {
case 'company':
case 'authorlp':
$validVariable = true;
$extraFieldType = 'user';
break;
case 'authors':
$validVariable = true;
$extraFieldType = 'lp';
break;
case 'price':
case 'authorlpitem':
$validVariable = true;
$extraFieldType = 'lp_item';
break;
}
if ($variable === 'company') {
$validVariable = true;
$extraFieldType = 'user';
} elseif ($variable === 'authors') {
$validVariable = true;
$extraFieldType = 'lp';
}
if ($validVariable == true && $id != 0 && !empty($extraFieldType)) {
$query = "SELECT id

@ -1,17 +1,30 @@
Check Extra Fields 'author' and 'company'
======
Distribution by entity report enable the administrator to select a date range to show the number of users that
have been subscribed to a learning path or a course during this time frame. The number users are group by entity/company.
The "User by organization" report allows the administrator to select a
date range to show the number of users who have been subscribed to a learning
path or a course during this time frame. The number of users are grouped by
entity/company.
Distribution by author report enable the administrator to define for each user if it is an author or not.
Then for each item in a Learning Path the administrator can select who is its author from the indentified list and indicate the price.
Finaly the reports enable the adminstrator to select a date range to show for each author how many of his content (LP item)
has been given access to users (based on the learning path subscription of the users) and show the amount of money they should be paid
based on the number of access authorised in this period.
The "Learning path by author" report allows the administrator to define, for
each user, if (s)he is an author or not. Then, for each item in a Learning
Path, the administrator can select who is its author from the identified list
and indicate the cost of that item.
This plugin adds the extra fields necessary to display "Distribution by entity" and "Distribution by author" reports.
* For the "Distribution by entity" report to be displayed, it is necessary to have the type of extra user field, with a multiple selector.
The name of this field must be "company".
* For the "Distribution by author" report to be displayed, it is necessary to have the field type extra learning path, with a multiple selector drop-down.
The name of this field must be "authors".
Finally, the reports allow the administrator to select a date range to show
for each author how many of his/her content (LP item) users have been given
access to (based on the learning path subscriptions by users) and show the
amount of money they should be paid based on the number of accesses given
during this period.
This plugin adds the extra fields necessary to display the reports:
* The "User by organization" report requires the 'company' extra field to be created on user.
* The "Learning path by author" report requires the 'authors' extra field to be created on lp.
* The "LP Item by author" report additional reports requires the 'authorlpitem' extra field to be created on lp_item and the 'authorlp' extra field to be created on 'user'.
* For prices to be adequately shown, the 'price' extra field needs to be created on 'lp_item'.
## Uninstall
When uninstalling this plugin, the extra fields created will not be removed,
for data persistence reasons.

@ -7,11 +7,11 @@
* (course plugins are slightly different).
* These settings will be used in the administration interface for plugins (Chamilo configuration settings->Plugins).
*
* @author Carlos Alvarado <alvaradocarlo@gmail.com>
* @author Carlos Alvarado <carlos.alvarado@beeznest.com>
*/
$plugin_info['title'] = 'Reports: "Distribution by entity" and "Distribution by author"';
$plugin_info['comment'] = 'To enable this report, you must activate user subscription to a learning path through the '.
'"Course settings" -> "Subscribe users to learning path" <br>'.
'You can then go to /main/mySpace/ to see the new reports.';
$plugin_info['title'] = 'Extra reports: User by organization", "Learning path by author" and "LP item by author"';
$plugin_info['comment'] = 'To enable these reports, enable user subscription to a learning path through the '.
'"Learning path settings" -> "Subscribe users to learning path" <br>'.
'You can then go to /main/mySpace/ (as administrator), then to the "admin" section (star icon) to see the new reports.';
$plugin_info['version'] = '1.2';
$plugin_info['author'] = 'Carlos Alvarado, Julio Montoya';

Loading…
Cancel
Save