This commit is contained in:
Xes
2025-08-14 22:39:38 +02:00
parent 3641e93527
commit 5403f346e3
3370 changed files with 327179 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
# Electronic Grading Form
v0.8
Generates a file for a specific grades registration form.
This plugin is more to be viewed as a template for similar exports, as it will
create very specific extra fields and allow you to export very specific reports
with those extra fields.
* Install the plugin
* Assign the `content_top` region to the plugin
* Enable the tool in the plugin configuration page
> During the installation this plugin creates three course extra fields
> (`plugin_gradingelectronic_provider_id`, `plugin_gradingelectronic_course_id`, `plugin_gradingelectronic_coursehours`)
> and one user extra field
> (`fcdice_or_acadis_student_id`)

View File

@@ -0,0 +1,184 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course;
use Chamilo\CoreBundle\Entity\CourseRelUser;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\CoreBundle\Entity\SessionRelUser;
use Chamilo\UserBundle\Entity\User;
use Doctrine\Common\Collections\Criteria;
require_once '../../main/inc/global.inc.php';
$allowed = api_is_teacher() || api_is_platform_admin() || api_is_course_tutor();
$gradingElectronic = GradingElectronicPlugin::create();
try {
if (!$allowed) {
throw new Exception(get_lang('NotAllowed'));
}
$toolIsEnabled = $gradingElectronic->get('tool_enable') === 'true';
if (!$toolIsEnabled) {
throw new Exception($gradingElectronic->get_lang('PluginDisabled'));
}
$form = $gradingElectronic->getForm();
if (!$form->validate()) {
throw new Exception(implode('<br>', $form->_errors));
}
$em = Database::getManager();
/** @var Course $course */
$course = $em->find('ChamiloCoreBundle:Course', api_get_course_int_id());
/** @var Session $session */
$session = $em->find('ChamiloCoreBundle:Session', api_get_session_id());
$values = $form->exportValues();
$cFieldValue = new ExtraFieldValue('course');
$uFieldValue = new ExtraFieldValue('user');
$cFieldValue->save([
'variable' => GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
'item_id' => $course->getId(),
'value' => $values['course'],
]);
$item = $cFieldValue->get_item_id_from_field_variable_and_field_value(
GradingElectronicPlugin::EXTRAFIELD_COURSE_ID,
$values['course']
);
$fieldProvider = $cFieldValue->get_values_by_handler_and_field_variable(
$course->getId(),
GradingElectronicPlugin::EXTRAFIELD_COURSE_PROVIDER_ID
);
$fieldHours = $cFieldValue->get_values_by_handler_and_field_variable(
$course->getId(),
GradingElectronicPlugin::EXTRAFIELD_COURSE_HOURS
);
$students = [];
if ($session) {
$criteria = Criteria::create()->where(
Criteria::expr()->eq('relationType', Session::STUDENT)
);
$subscriptions = $session->getUsers()->matching($criteria);
/** @var SessionRelUser $subscription */
foreach ($subscriptions as $subscription) {
$students[] = $subscription->getUser();
}
} else {
$subscriptions = $course->getStudents();
/** @var CourseRelUser $subscription */
foreach ($subscriptions as $subscription) {
$students[] = $subscription->getUser();
}
}
$cats = Category::load(
null,
null,
$course->getCode(),
null,
null,
$session ? $session->getId() : 0,
'ORDER By id'
);
/** @var \Category $gradebook */
$gradebook = $cats[0];
/** @var \ExerciseLink $exerciseLink */
/** commented until we get clear understanding of how to use the dates refs BT#12404.
$exerciseInfo = ExerciseLib::get_exercise_by_id($exerciseId, $course->getId());
*/
$dateStart = new DateTime($values['range_start'].' 00:00:00', new DateTimeZone('UTC'));
$dateEnd = new DateTime($values['range_end'].' 23:59:59', new DateTimeZone('UTC'));
$fileData = [];
$fileData[] = sprintf(
'1 %s %s%s',
$fieldProvider ? $fieldProvider['value'] : null,
$values['course'],
$dateStart->format('m/d/Y')
);
/** @var User $student */
foreach ($students as $student) {
$userFinishedCourse = Category::userFinishedCourse(
$student->getId(),
$gradebook,
true
);
if (!$userFinishedCourse) {
continue;
}
/** commented until we get clear understanding of how to use the dates refs BT#12404.
}
*/
$fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
$student->getId(),
GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
);
$scoretotal = $gradebook->calc_score($student->getId());
$scoredisplay = ScoreDisplay::instance();
$score = $scoredisplay->display_score(
$scoretotal,
SCORE_SIMPLE
);
/** old method to get the score.
);
*/
$fileData[] = sprintf(
"2 %sPASS%s %s %s",
$fieldStudent ? $fieldStudent['value'] : null,
$fieldHours ? $fieldHours['value'] : null,
$score,
$dateEnd->format('m/d/Y')
);
if (!$gradebook->getGenerateCertificates()) {
continue;
}
Category::generateUserCertificate(
$gradebook->get_id(),
$student->getId(),
true
);
}
$fileName = implode('_', [
$gradingElectronic->get_title(),
$values['course'],
$values['range_start'],
$values['range_end'],
]);
$fileName = api_replace_dangerous_char($fileName).'.txt';
$fileData[] = null;
file_put_contents(
api_get_path(SYS_ARCHIVE_PATH).$fileName,
implode("\r\n", $fileData)
);
echo Display::toolbarButton(
get_lang('Download'),
api_get_path(WEB_ARCHIVE_PATH).$fileName,
'download',
'success',
['target' => '_blank', 'download' => $fileName]
);
} catch (Exception $e) {
echo Display::return_message($e->getMessage(), 'error');
}

View File

@@ -0,0 +1,16 @@
<?php
/* For licensing terms, see /license.txt */
if (strpos($_SERVER['SCRIPT_NAME'], 'gradebook/index.php') === false) {
return;
}
$gradingElectronic = GradingElectronicPlugin::create();
if (!$gradingElectronic->isAllowed()) {
return;
}
$_template['show'] = true;
$_template['plugin_title'] = $gradingElectronic->get_title();
$_template['form'] = $gradingElectronic->getForm();

View File

@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
GradingElectronicPlugin::create()->install();

View File

@@ -0,0 +1,16 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = 'Electronic grading export';
$strings['plugin_comment'] = 'This plugin creates a text file with additional information about of the course. Use this as a template for similar plugins creating specific reports with additional extra fields.';
$strings['tool_enable'] = 'Enable tool';
$strings['tool_enable_help'] = '
Allow add the <i>Generate File</i> button in the <i>Assessments</i> page.
You need assign the <code>content_top</code> region.
';
$strings['PluginDisabled'] = 'Electronic grading is disabed';
$strings['StudentId'] = 'Student ID';
$strings['ProviderId'] = 'Provider ID';
$strings['CourseHours'] = 'Course hours';

View File

@@ -0,0 +1,5 @@
<?php
/* For licensing terms, see /license.txt */
$plugin_info = GradingElectronicPlugin::create()->get_info();
$plugin_info['templates'] = ['view/grading.html.twig'];

View File

@@ -0,0 +1,221 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Class GradingElectronicPlugin.
*/
class GradingElectronicPlugin extends Plugin
{
public const EXTRAFIELD_STUDENT_ID = 'fcdice_or_acadis_student_id';
public const EXTRAFIELD_COURSE_PROVIDER_ID = 'plugin_gradingelectronic_provider_id';
public const EXTRAFIELD_COURSE_ID = 'plugin_gradingelectronic_course_id';
public const EXTRAFIELD_COURSE_HOURS = 'plugin_gradingelectronic_coursehours';
/**
* {@inheritdoc}
*/
protected function __construct()
{
parent::__construct(
'0.8',
'Angel Fernando Quiroz Campos, Julio Montoya',
[
'tool_enable' => 'boolean',
]
);
}
/**
* @return \GradingElectronicPlugin|null
*/
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* {@inheritdoc}
*/
public function get_name()
{
return 'grading_electronic';
}
/**
* Actions for install.
*/
public function install()
{
$this->setUpExtraFields();
}
/**
* Actions for uninstall.
*/
public function uninstall()
{
$this->setDownExtraFields();
}
/**
* @return \FormValidator|void
*/
public function getForm()
{
$extraField = new ExtraField('course');
$courseIdField = $extraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_ID
);
if (!$courseIdField) {
return null;
}
$extraFieldValue = new ExtraFieldValue('course');
$courseIdValue = $extraFieldValue->get_values_by_handler_and_field_variable(
api_get_course_int_id(),
self::EXTRAFIELD_COURSE_ID
);
$form = new FormValidator('frm_grading_electronic');
$form->addDateRangePicker(
'range',
get_lang('DateRange'),
true,
[
'id' => 'range',
'format' => 'YYYY-MM-DD',
'timePicker' => 'false',
'validate_format' => 'Y-m-d',
]
);
$form->addText('course', $this->get_lang('CourseId'));
$form->addButtonDownload(get_lang('Generate'));
$form->addRule('course', get_lang('ThisFieldIsRequired'), 'required');
$form->setDefaults([
'course' => $courseIdValue ? $courseIdValue['value'] : null,
]);
return $form;
}
/**
* Check if the current use is allowed to see the button.
*
* @return bool
*/
public function isAllowed()
{
$allowed = api_is_teacher() || api_is_platform_admin() || api_is_course_tutor();
if (!$allowed) {
return false;
}
$toolIsEnabled = $this->get('tool_enable') === 'true';
if (!$toolIsEnabled) {
return false;
}
return true;
}
/**
* Create extra fields for this plugin.
*/
private function setUpExtraFields()
{
$uExtraField = new ExtraField('user');
if (!$uExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_STUDENT_ID
)) {
$uExtraField->save([
'variable' => self::EXTRAFIELD_STUDENT_ID,
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'display_text' => $this->get_lang('StudentId'),
'visible_to_self' => true,
'changeable' => true,
]);
}
$cExtraField = new ExtraField('course');
if (!$cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_PROVIDER_ID
)) {
$cExtraField->save([
'variable' => self::EXTRAFIELD_COURSE_PROVIDER_ID,
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'display_text' => $this->get_lang('ProviderId'),
'visible_to_self' => true,
'changeable' => true,
]);
}
if (!$cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_ID
)) {
$cExtraField->save([
'variable' => self::EXTRAFIELD_COURSE_ID,
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'display_text' => $this->get_lang('CourseId'),
'visible_to_self' => true,
'changeable' => true,
]);
}
if (!$cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_HOURS
)) {
$cExtraField->save([
'variable' => self::EXTRAFIELD_COURSE_HOURS,
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'display_text' => $this->get_lang('CourseHours'),
'visible_to_self' => true,
'changeable' => true,
]);
}
}
/**
* Remove extra fields for this plugin.
*/
private function setDownExtraFields()
{
$uExtraField = new ExtraField('user');
$studentIdField = $uExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_STUDENT_ID
);
if ($studentIdField) {
$uExtraField->delete($studentIdField['id']);
}
$cExtraField = new ExtraField('course');
$providerIdField = $cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_PROVIDER_ID
);
$courseIdField = $cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_ID
);
$courseHoursField = $cExtraField->get_handler_field_info_by_field_variable(
self::EXTRAFIELD_COURSE_HOURS
);
if ($providerIdField) {
$cExtraField->delete($providerIdField['id']);
}
if ($courseIdField) {
$cExtraField->delete($courseIdField['id']);
}
if ($courseHoursField) {
$cExtraField->delete($courseHoursField['id']);
}
}
}

View File

@@ -0,0 +1,4 @@
<?php
/* For licensing terms, see /license.txt */
GradingElectronicPlugin::create()->uninstall();

View File

@@ -0,0 +1,40 @@
{% if grading_electronic.show %}
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-grading-electronic">
{{ 'GenerateFile'|get_plugin_lang('GradingElectronicPlugin') }}
</button>
<div class="modal fade" id="modal-grading-electronic" tabindex="-1" role="dialog"
aria-labelledby="modal-grading-electronic-title">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="{{ 'Close'|get_lang }}">
<span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="modal-grading-electronic-title">
{{ 'plugin_title'|get_plugin_lang('GradingElectronicPlugin') }}
</h4>
</div>
<div class="modal-body">
{{ grading_electronic.form.display() }}
<div id="modal-grading-electronic-result" class="text-center"></div>
</div>
</div>
</div>
</div>
<script>
$(function () {
$('form[name="frm_grading_electronic"]').on('submit', function (e) {
e.preventDefault();
var $self = $(this);
$.post('{{ _p.web_plugin }}grading_electronic/generate.php', $self.serialize())
.done(function (response) {
$('#modal-grading-electronic-result').html(
response
);
});
});
});
</script>
{% endif %}