Add grading electronic as is see BT#12404

pull/2487/head
jmontoyaa 9 years ago
parent fd62ba7211
commit c37015bd0e
  1. 13
      plugin/grading_electronic/README.md
  2. 181
      plugin/grading_electronic/generate.php
  3. 16
      plugin/grading_electronic/index.php
  4. 4
      plugin/grading_electronic/install.php
  5. 16
      plugin/grading_electronic/lang/english.php
  6. 5
      plugin/grading_electronic/plugin.php
  7. 200
      plugin/grading_electronic/src/GradingElectronicPlugin.php
  8. 4
      plugin/grading_electronic/uninstall.php
  9. 43
      plugin/grading_electronic/view/grading.html.twig

@ -0,0 +1,13 @@
# Grading Electronic Form
v0.6
Generate a file necesary to the Grading Electronic form for RickyRescue
* Install the plugin
* Asign the `content_top` region
* Enable tool in configuration page
> During the installation this plugin create 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`)

@ -0,0 +1,181 @@
<?php
/* For licensing terms, see /license.txt */
use Chamilo\CoreBundle\Entity\Course,
Chamilo\CoreBundle\Entity\Session,
Doctrine\Common\Collections\Criteria,
Chamilo\CoreBundle\Entity\SessionRelUser,
Chamilo\CoreBundle\Entity\CourseRelUser,
Chamilo\UserBundle\Entity\User;
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
);
if ($session) {
$criteria = Criteria::create()->where(
Criteria::expr()->eq('relationType', Session::STUDENT)
);
$subscriptions = $session
->getUsers()
->matching($criteria);
$students = [];
/** @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];
$dateStart = new DateTime($values['range_start'], new DateTimeZone('UTC'));
$dateEnd = new DateTime($values['range_end'], 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,
0,
$course->getCode(),
$session ? $session->getId() : 0,
true
);
if (!$userFinishedCourse) {
continue;
}
$fieldStudent = $uFieldValue->get_values_by_handler_and_field_variable(
$student->getId(),
GradingElectronicPlugin::EXTRAFIELD_STUDENT_ID
);
$score = Category::getCurrentScore(
$student->getId(),
$gradebook->get_id(),
$course->getCode(),
$session ? $session->getId() : 0
);
$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::register_user_certificate($gradebook->get_id(), $student->getId());
}
$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');
}

@ -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;
$_tempalte['plugin_title'] = $gradingElectronic->get_title();
$_template['form'] = $gradingElectronic->getForm();

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

@ -0,0 +1,16 @@
<?php
/* For licensing terms, see /license.txt */
$strings['plugin_title'] = 'Grading Electronic';
$strings['plugin_comment'] = 'This plugin create a text file with information about of the course';
$strings['tool_enable'] = 'Enable tool';
$strings['tool_enable_help'] = '
Allow add the <i>Generate File</i> button in the <i>Assessments</i> page.
You nee assign the <code>content_top</code> region.
';
$strings['PluginDisabled'] = 'Grading Electronic is disabed';
$strings['StudentId'] = 'Student ID';
$strings['ProviderId'] = 'Provider ID';
$strings['CourseHours'] = 'Course hours';

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

@ -0,0 +1,200 @@
<?php
/* For licensing terms, see /license.txt */
class GradingElectronicPlugin extends Plugin
{
const EXTRAFIELD_STUDENT_ID = 'fcdice_or_acadis_student_id';
const EXTRAFIELD_COURSE_PROVIDER_ID = 'plugin_gradingelectronic_provider_id';
const EXTRAFIELD_COURSE_ID = 'plugin_gradingelectronic_course_id';
const EXTRAFIELD_COURSE_HOURS = 'plugin_gradingelectronic_coursehours';
/**
* @return \GradingElectronicPlugin|null
*/
public static function create()
{
static $result = null;
return $result ? $result : $result = new self();
}
/**
* @inheritDoc
*/
protected function __construct()
{
parent::__construct(
'0.6',
'Angel Fernando Quiroz Campos',
array(
'tool_enable' => 'boolean',
)
);
}
/**
* @inheritDoc
*/
public function get_name()
{
return 'grading_electronic';
}
/**
* Actions for install
*/
public function install()
{
$this->setUpExtraFields();
}
/**
* Actions for uninstall
*/
public function uninstall()
{
$this->setDownExtraFields();
}
/**
* 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');
$proviedIdField = $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 ($proviedIdField) {
$cExtraField->delete($proviedIdField['id']);
}
if ($courseIdField) {
$cExtraField->delete($courseIdField['id']);
}
if ($courseHoursField) {
$cExtraField->delete($courseHoursField['id']);
}
}
/**
* @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;
}
}

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

@ -0,0 +1,43 @@
{% 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>
$(document).on('ready', 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 %}
Loading…
Cancel
Save